blob: 180c9ae95137e67697985ac046e77aed583ae689 [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/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700619 * @brief Grant a thread access to a NULL-terminated set of kernel objects
620 *
621 * This is a convenience function. For the provided thread, grant access to
622 * the remaining arguments, which must be pointers to kernel objects.
623 * The final argument must be a NULL.
624 *
625 * The thread object must be initialized (i.e. running). The objects don't
626 * need to be.
627 *
628 * @param thread Thread to grant access to objects
629 * @param ... NULL-terminated list of kernel object pointers
630 */
631extern void __attribute__((sentinel))
632 k_thread_access_grant(struct k_thread *thread, ...);
633
634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500635 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 *
Allan Stephensc98da842016-11-11 15:45:03 -0500637 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500638 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500640 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
642 * @return N/A
643 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700644__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400645
646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500647 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400648 *
649 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500650 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400652 * @return N/A
653 */
Kumar Galacc334c72017-04-21 10:55:34 -0500654extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655
656/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500657 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500659 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400662 *
663 * @return N/A
664 */
Andrew Boie468190a2017-09-29 14:00:48 -0700665__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666
667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500668 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * If @a thread is not currently sleeping, the routine has no effect.
673 *
674 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 *
676 * @return N/A
677 */
Andrew Boie468190a2017-09-29 14:00:48 -0700678__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400679
680/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700685__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686
687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500688 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * This routine prevents @a thread from executing if it has not yet started
691 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500693 * @param thread ID of thread to cancel.
694 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700695 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500696 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 */
Andrew Boie468190a2017-09-29 14:00:48 -0700698__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700/**
Allan Stephensc98da842016-11-11 15:45:03 -0500701 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * This routine permanently stops execution of @a thread. The thread is taken
704 * off all kernel queues it is part of (i.e. the ready queue, the timeout
705 * queue, or a kernel object wait queue). However, any kernel resources the
706 * thread might currently own (such as mutexes or memory blocks) are not
707 * released. It is the responsibility of the caller of this routine to ensure
708 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500710 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711 *
712 * @return N/A
713 */
Andrew Boie468190a2017-09-29 14:00:48 -0700714__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400715
Andrew Boie7d627c52017-08-30 11:01:56 -0700716
717/**
718 * @brief Start an inactive thread
719 *
720 * If a thread was created with K_FOREVER in the delay parameter, it will
721 * not be added to the scheduling queue until this function is called
722 * on it.
723 *
724 * @param thread thread to start
725 */
Andrew Boie468190a2017-09-29 14:00:48 -0700726__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700727
Allan Stephensc98da842016-11-11 15:45:03 -0500728/**
729 * @cond INTERNAL_HIDDEN
730 */
731
Benjamin Walshd211a522016-12-06 11:44:01 -0500732/* timeout has timed out and is not on _timeout_q anymore */
733#define _EXPIRED (-2)
734
735/* timeout is not in use */
736#define _INACTIVE (-1)
737
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400738struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700739 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700740 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400741 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700742 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500743 void *init_p1;
744 void *init_p2;
745 void *init_p3;
746 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500747 u32_t init_options;
748 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500749 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500750 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400751};
752
Andrew Boied26cf2d2017-03-30 13:07:02 -0700753#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400754 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500755 prio, options, delay, abort, groups) \
756 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700757 .init_thread = (thread), \
758 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500759 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700760 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400761 .init_p1 = (void *)p1, \
762 .init_p2 = (void *)p2, \
763 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500764 .init_prio = (prio), \
765 .init_options = (options), \
766 .init_delay = (delay), \
767 .init_abort = (abort), \
768 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769 }
770
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400771/**
Allan Stephensc98da842016-11-11 15:45:03 -0500772 * INTERNAL_HIDDEN @endcond
773 */
774
775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * @brief Statically define and initialize a thread.
777 *
778 * The thread may be scheduled for immediate execution or a delayed start.
779 *
780 * Thread options are architecture-specific, and can include K_ESSENTIAL,
781 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
782 * them using "|" (the logical OR operator).
783 *
784 * The ID of the thread can be accessed using:
785 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500786 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 *
788 * @param name Name of the thread.
789 * @param stack_size Stack size in bytes.
790 * @param entry Thread entry function.
791 * @param p1 1st entry point parameter.
792 * @param p2 2nd entry point parameter.
793 * @param p3 3rd entry point parameter.
794 * @param prio Thread priority.
795 * @param options Thread options.
796 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400797 *
798 * @internal It has been observed that the x86 compiler by default aligns
799 * these _static_thread_data structures to 32-byte boundaries, thereby
800 * wasting space. To work around this, force a 4-byte alignment.
801 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500802#define K_THREAD_DEFINE(name, stack_size, \
803 entry, p1, p2, p3, \
804 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700805 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700806 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500807 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500808 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700809 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
810 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500811 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700812 NULL, 0); \
813 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400814
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @param thread ID of thread whose priority is needed.
821 *
822 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700824__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825
826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
831 * Rescheduling can occur immediately depending on the priority @a thread is
832 * set to:
833 *
834 * - If its priority is raised above the priority of the caller of this
835 * function, and the caller is preemptible, @a thread will be scheduled in.
836 *
837 * - If the caller operates on itself, it lowers its priority below that of
838 * other threads in the system, and the caller is preemptible, the thread of
839 * highest priority will be scheduled in.
840 *
841 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
842 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
843 * highest priority.
844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 * @param prio New priority.
847 *
848 * @warning Changing the priority of a thread currently involved in mutex
849 * priority inheritance may result in undefined behavior.
850 *
851 * @return N/A
852 */
Andrew Boie468190a2017-09-29 14:00:48 -0700853__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400854
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * This routine prevents the kernel scheduler from making @a thread the
859 * current thread. All other internal operations on @a thread are still
860 * performed; for example, any timeout it is waiting on keeps ticking,
861 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * If @a thread is already suspended, the routine has no effect.
864 *
865 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 *
867 * @return N/A
868 */
Andrew Boie468190a2017-09-29 14:00:48 -0700869__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870
871/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * This routine allows the kernel scheduler to make @a thread the current
875 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * If @a thread is not currently suspended, the routine has no effect.
878 *
879 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
881 * @return N/A
882 */
Andrew Boie468190a2017-09-29 14:00:48 -0700883__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * This routine specifies how the scheduler will perform time slicing of
889 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * To enable time slicing, @a slice must be non-zero. The scheduler
892 * ensures that no thread runs for more than the specified time limit
893 * before other threads of that priority are given a chance to execute.
894 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700895 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 * execute. Once the scheduler selects a thread for execution, there is no
899 * minimum guaranteed time the thread will execute before threads of greater or
900 * equal priority are scheduled.
901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500902 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903 * for execution, this routine has no effect; the thread is immediately
904 * rescheduled after the slice period expires.
905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 * To disable timeslicing, set both @a slice and @a prio to zero.
907 *
908 * @param slice Maximum time slice length (in milliseconds).
909 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 *
911 * @return N/A
912 */
Kumar Galacc334c72017-04-21 10:55:34 -0500913extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400914
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915/**
Allan Stephensc98da842016-11-11 15:45:03 -0500916 * @} end defgroup thread_apis
917 */
918
919/**
920 * @addtogroup isr_apis
921 * @{
922 */
923
924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 *
Allan Stephensc98da842016-11-11 15:45:03 -0500927 * This routine allows the caller to customize its actions, depending on
928 * whether it is a thread or an ISR.
929 *
930 * @note Can be called by ISRs.
931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * @return 0 if invoked by a thread.
933 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500935extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400936
Benjamin Walsh445830d2016-11-10 15:54:27 -0500937/**
938 * @brief Determine if code is running in a preemptible thread.
939 *
Allan Stephensc98da842016-11-11 15:45:03 -0500940 * This routine allows the caller to customize its actions, depending on
941 * whether it can be preempted by another thread. The routine returns a 'true'
942 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500943 *
Allan Stephensc98da842016-11-11 15:45:03 -0500944 * - The code is running in a thread, not at ISR.
945 * - The thread's priority is in the preemptible range.
946 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500947 *
Allan Stephensc98da842016-11-11 15:45:03 -0500948 * @note Can be called by ISRs.
949 *
950 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500951 * @return Non-zero if invoked by a preemptible thread.
952 */
Andrew Boie468190a2017-09-29 14:00:48 -0700953__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500954
Allan Stephensc98da842016-11-11 15:45:03 -0500955/**
956 * @} end addtogroup isr_apis
957 */
958
959/**
960 * @addtogroup thread_apis
961 * @{
962 */
963
964/**
965 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500966 *
Allan Stephensc98da842016-11-11 15:45:03 -0500967 * This routine prevents the current thread from being preempted by another
968 * thread by instructing the scheduler to treat it as a cooperative thread.
969 * If the thread subsequently performs an operation that makes it unready,
970 * it will be context switched out in the normal manner. When the thread
971 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500972 *
Allan Stephensc98da842016-11-11 15:45:03 -0500973 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500974 *
Allan Stephensc98da842016-11-11 15:45:03 -0500975 * @note k_sched_lock() and k_sched_unlock() should normally be used
976 * when the operation being performed can be safely interrupted by ISRs.
977 * However, if the amount of processing involved is very small, better
978 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500979 *
980 * @return N/A
981 */
982extern void k_sched_lock(void);
983
Allan Stephensc98da842016-11-11 15:45:03 -0500984/**
985 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500986 *
Allan Stephensc98da842016-11-11 15:45:03 -0500987 * This routine reverses the effect of a previous call to k_sched_lock().
988 * A thread must call the routine once for each time it called k_sched_lock()
989 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500990 *
991 * @return N/A
992 */
993extern void k_sched_unlock(void);
994
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * Custom data is not used by the kernel itself, and is freely available
1001 * for a thread to use as it sees fit. It can be used as a framework
1002 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
1006 * @return N/A
1007 */
Andrew Boie468190a2017-09-29 14:00:48 -07001008__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009
1010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 */
Andrew Boie468190a2017-09-29 14:00:48 -07001017__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001018
1019/**
Allan Stephensc98da842016-11-11 15:45:03 -05001020 * @} end addtogroup thread_apis
1021 */
1022
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001023#include <sys_clock.h>
1024
Allan Stephensc2f15a42016-11-17 12:24:22 -05001025/**
1026 * @addtogroup clock_apis
1027 * @{
1028 */
1029
1030/**
1031 * @brief Generate null timeout delay.
1032 *
1033 * This macro generates a timeout delay that that instructs a kernel API
1034 * not to wait if the requested operation cannot be performed immediately.
1035 *
1036 * @return Timeout delay value.
1037 */
1038#define K_NO_WAIT 0
1039
1040/**
1041 * @brief Generate timeout delay from milliseconds.
1042 *
1043 * This macro generates a timeout delay that that instructs a kernel API
1044 * to wait up to @a ms milliseconds to perform the requested operation.
1045 *
1046 * @param ms Duration in milliseconds.
1047 *
1048 * @return Timeout delay value.
1049 */
Johan Hedberg14471692016-11-13 10:52:15 +02001050#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001051
1052/**
1053 * @brief Generate timeout delay from seconds.
1054 *
1055 * This macro generates a timeout delay that that instructs a kernel API
1056 * to wait up to @a s seconds to perform the requested operation.
1057 *
1058 * @param s Duration in seconds.
1059 *
1060 * @return Timeout delay value.
1061 */
Johan Hedberg14471692016-11-13 10:52:15 +02001062#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001063
1064/**
1065 * @brief Generate timeout delay from minutes.
1066 *
1067 * This macro generates a timeout delay that that instructs a kernel API
1068 * to wait up to @a m minutes to perform the requested operation.
1069 *
1070 * @param m Duration in minutes.
1071 *
1072 * @return Timeout delay value.
1073 */
Johan Hedberg14471692016-11-13 10:52:15 +02001074#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001075
1076/**
1077 * @brief Generate timeout delay from hours.
1078 *
1079 * This macro generates a timeout delay that that instructs a kernel API
1080 * to wait up to @a h hours to perform the requested operation.
1081 *
1082 * @param h Duration in hours.
1083 *
1084 * @return Timeout delay value.
1085 */
Johan Hedberg14471692016-11-13 10:52:15 +02001086#define K_HOURS(h) K_MINUTES((h) * 60)
1087
Allan Stephensc98da842016-11-11 15:45:03 -05001088/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001089 * @brief Generate infinite timeout delay.
1090 *
1091 * This macro generates a timeout delay that that instructs a kernel API
1092 * to wait as long as necessary to perform the requested operation.
1093 *
1094 * @return Timeout delay value.
1095 */
1096#define K_FOREVER (-1)
1097
1098/**
1099 * @} end addtogroup clock_apis
1100 */
1101
1102/**
Allan Stephensc98da842016-11-11 15:45:03 -05001103 * @cond INTERNAL_HIDDEN
1104 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001105
Benjamin Walsh62092182016-12-20 14:39:08 -05001106/* kernel clocks */
1107
1108#if (sys_clock_ticks_per_sec == 1000) || \
1109 (sys_clock_ticks_per_sec == 500) || \
1110 (sys_clock_ticks_per_sec == 250) || \
1111 (sys_clock_ticks_per_sec == 125) || \
1112 (sys_clock_ticks_per_sec == 100) || \
1113 (sys_clock_ticks_per_sec == 50) || \
1114 (sys_clock_ticks_per_sec == 25) || \
1115 (sys_clock_ticks_per_sec == 20) || \
1116 (sys_clock_ticks_per_sec == 10) || \
1117 (sys_clock_ticks_per_sec == 1)
1118
1119 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1120#else
1121 /* yields horrible 64-bit math on many architectures: try to avoid */
1122 #define _NON_OPTIMIZED_TICKS_PER_SEC
1123#endif
1124
1125#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001126extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001127#else
Kumar Galacc334c72017-04-21 10:55:34 -05001128static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001129{
Kumar Galacc334c72017-04-21 10:55:34 -05001130 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001131}
1132#endif
1133
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001134/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001135#ifdef CONFIG_TICKLESS_KERNEL
1136#define _TICK_ALIGN 0
1137#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001138#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001139#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001140
Kumar Galacc334c72017-04-21 10:55:34 -05001141static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001142{
Benjamin Walsh62092182016-12-20 14:39:08 -05001143#ifdef CONFIG_SYS_CLOCK_EXISTS
1144
1145#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001146 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001147#else
Kumar Galacc334c72017-04-21 10:55:34 -05001148 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001149#endif
1150
1151#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001152 __ASSERT(ticks == 0, "");
1153 return 0;
1154#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001155}
1156
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157struct k_timer {
1158 /*
1159 * _timeout structure must be first here if we want to use
1160 * dynamic timer allocation. timeout.node is used in the double-linked
1161 * list of free timers
1162 */
1163 struct _timeout timeout;
1164
Allan Stephens45bfa372016-10-12 12:39:42 -05001165 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166 _wait_q_t wait_q;
1167
1168 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001169 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170
1171 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001172 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173
1174 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001175 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176
Allan Stephens45bfa372016-10-12 12:39:42 -05001177 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001178 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001180 /* user-specific data, also used to support legacy features */
1181 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182
Anas Nashif2f203c22016-12-18 06:57:45 -05001183 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001184};
1185
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001186#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001188 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001189 .timeout.wait_q = NULL, \
1190 .timeout.thread = NULL, \
1191 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001193 .expiry_fn = expiry, \
1194 .stop_fn = stop, \
1195 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001196 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001197 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001198 }
1199
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001200#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1201
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202/**
Allan Stephensc98da842016-11-11 15:45:03 -05001203 * INTERNAL_HIDDEN @endcond
1204 */
1205
1206/**
1207 * @defgroup timer_apis Timer APIs
1208 * @ingroup kernel_apis
1209 * @{
1210 */
1211
1212/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001213 * @typedef k_timer_expiry_t
1214 * @brief Timer expiry function type.
1215 *
1216 * A timer's expiry function is executed by the system clock interrupt handler
1217 * each time the timer expires. The expiry function is optional, and is only
1218 * invoked if the timer has been initialized with one.
1219 *
1220 * @param timer Address of timer.
1221 *
1222 * @return N/A
1223 */
1224typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1225
1226/**
1227 * @typedef k_timer_stop_t
1228 * @brief Timer stop function type.
1229 *
1230 * A timer's stop function is executed if the timer is stopped prematurely.
1231 * The function runs in the context of the thread that stops the timer.
1232 * The stop function is optional, and is only invoked if the timer has been
1233 * initialized with one.
1234 *
1235 * @param timer Address of timer.
1236 *
1237 * @return N/A
1238 */
1239typedef void (*k_timer_stop_t)(struct k_timer *timer);
1240
1241/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001245 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001246 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247 *
1248 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * @param expiry_fn Function to invoke each time the timer expires.
1250 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001251 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001252#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001253 struct k_timer name \
1254 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001255 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001256
Allan Stephens45bfa372016-10-12 12:39:42 -05001257/**
1258 * @brief Initialize a timer.
1259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001261 *
1262 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * @param expiry_fn Function to invoke each time the timer expires.
1264 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001265 *
1266 * @return N/A
1267 */
1268extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001269 k_timer_expiry_t expiry_fn,
1270 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001271
Allan Stephens45bfa372016-10-12 12:39:42 -05001272/**
1273 * @brief Start a timer.
1274 *
1275 * This routine starts a timer, and resets its status to zero. The timer
1276 * begins counting down using the specified duration and period values.
1277 *
1278 * Attempting to start a timer that is already running is permitted.
1279 * The timer's status is reset to zero and the timer begins counting down
1280 * using the new duration and period values.
1281 *
1282 * @param timer Address of timer.
1283 * @param duration Initial timer duration (in milliseconds).
1284 * @param period Timer period (in milliseconds).
1285 *
1286 * @return N/A
1287 */
Andrew Boiea354d492017-09-29 16:22:28 -07001288__syscall void k_timer_start(struct k_timer *timer,
1289 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001290
1291/**
1292 * @brief Stop a timer.
1293 *
1294 * This routine stops a running timer prematurely. The timer's stop function,
1295 * if one exists, is invoked by the caller.
1296 *
1297 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001299 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001300 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1301 * if @a k_timer_stop is to be called from ISRs.
1302 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001303 * @param timer Address of timer.
1304 *
1305 * @return N/A
1306 */
Andrew Boiea354d492017-09-29 16:22:28 -07001307__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001308
1309/**
1310 * @brief Read timer status.
1311 *
1312 * This routine reads the timer's status, which indicates the number of times
1313 * it has expired since its status was last read.
1314 *
1315 * Calling this routine resets the timer's status to zero.
1316 *
1317 * @param timer Address of timer.
1318 *
1319 * @return Timer status.
1320 */
Andrew Boiea354d492017-09-29 16:22:28 -07001321__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001322
1323/**
1324 * @brief Synchronize thread to timer expiration.
1325 *
1326 * This routine blocks the calling thread until the timer's status is non-zero
1327 * (indicating that it has expired at least once since it was last examined)
1328 * or the timer is stopped. If the timer status is already non-zero,
1329 * or the timer is already stopped, the caller continues without waiting.
1330 *
1331 * Calling this routine resets the timer's status to zero.
1332 *
1333 * This routine must not be used by interrupt handlers, since they are not
1334 * allowed to block.
1335 *
1336 * @param timer Address of timer.
1337 *
1338 * @return Timer status.
1339 */
Andrew Boiea354d492017-09-29 16:22:28 -07001340__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001341
1342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001344 *
1345 * This routine computes the (approximate) time remaining before a running
1346 * timer next expires. If the timer is not running, it returns zero.
1347 *
1348 * @param timer Address of timer.
1349 *
1350 * @return Remaining time (in milliseconds).
1351 */
Andrew Boiea354d492017-09-29 16:22:28 -07001352__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1353
1354static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001355{
1356 return _timeout_remaining_get(&timer->timeout);
1357}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001358
Allan Stephensc98da842016-11-11 15:45:03 -05001359/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001360 * @brief Associate user-specific data with a timer.
1361 *
1362 * This routine records the @a user_data with the @a timer, to be retrieved
1363 * later.
1364 *
1365 * It can be used e.g. in a timer handler shared across multiple subsystems to
1366 * retrieve data specific to the subsystem this timer is associated with.
1367 *
1368 * @param timer Address of timer.
1369 * @param user_data User data to associate with the timer.
1370 *
1371 * @return N/A
1372 */
Andrew Boiea354d492017-09-29 16:22:28 -07001373__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1374
1375static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1376 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001377{
1378 timer->user_data = user_data;
1379}
1380
1381/**
1382 * @brief Retrieve the user-specific data from a timer.
1383 *
1384 * @param timer Address of timer.
1385 *
1386 * @return The user data.
1387 */
Andrew Boiea354d492017-09-29 16:22:28 -07001388__syscall void *k_timer_user_data_get(struct k_timer *timer);
1389
1390static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001391{
1392 return timer->user_data;
1393}
1394
1395/**
Allan Stephensc98da842016-11-11 15:45:03 -05001396 * @} end defgroup timer_apis
1397 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001398
Allan Stephensc98da842016-11-11 15:45:03 -05001399/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001400 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001401 * @{
1402 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001403
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001405 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * This routine returns the elapsed time since the system booted,
1408 * in milliseconds.
1409 *
1410 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001411 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001412__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001413
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001414#ifdef CONFIG_TICKLESS_KERNEL
1415/**
1416 * @brief Enable clock always on in tickless kernel
1417 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001418 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001419 * there are no timer events programmed in tickless kernel
1420 * scheduling. This is necessary if the clock is used to track
1421 * passage of time.
1422 *
1423 * @retval prev_status Previous status of always on flag
1424 */
1425static inline int k_enable_sys_clock_always_on(void)
1426{
1427 int prev_status = _sys_clock_always_on;
1428
1429 _sys_clock_always_on = 1;
1430 _enable_sys_clock();
1431
1432 return prev_status;
1433}
1434
1435/**
1436 * @brief Disable clock always on in tickless kernel
1437 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001438 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001439 * there are no timer events programmed in tickless kernel
1440 * scheduling. To save power, this routine should be called
1441 * immediately when clock is not used to track time.
1442 */
1443static inline void k_disable_sys_clock_always_on(void)
1444{
1445 _sys_clock_always_on = 0;
1446}
1447#else
1448#define k_enable_sys_clock_always_on() do { } while ((0))
1449#define k_disable_sys_clock_always_on() do { } while ((0))
1450#endif
1451
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * This routine returns the lower 32-bits of the elapsed time since the system
1456 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * This routine can be more efficient than k_uptime_get(), as it reduces the
1459 * need for interrupt locking and 64-bit math. However, the 32-bit result
1460 * cannot hold a system uptime time larger than approximately 50 days, so the
1461 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001463 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001464 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001465__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001466
1467/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001470 * This routine computes the elapsed time between the current system uptime
1471 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001473 * @param reftime Pointer to a reference time, which is updated to the current
1474 * uptime upon return.
1475 *
1476 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001477 */
Kumar Galacc334c72017-04-21 10:55:34 -05001478extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479
1480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * This routine computes the elapsed time between the current system uptime
1484 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001486 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1487 * need for interrupt locking and 64-bit math. However, the 32-bit result
1488 * cannot hold an elapsed time larger than approximately 50 days, so the
1489 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * @param reftime Pointer to a reference time, which is updated to the current
1492 * uptime upon return.
1493 *
1494 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001495 */
Kumar Galacc334c72017-04-21 10:55:34 -05001496extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001497
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001501 * This routine returns the current time, as measured by the system's hardware
1502 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001505 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001506#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001508/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001509 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001510 */
1511
Allan Stephensc98da842016-11-11 15:45:03 -05001512/**
1513 * @cond INTERNAL_HIDDEN
1514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001516struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001517 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001518 union {
1519 _wait_q_t wait_q;
1520
1521 _POLL_EVENT;
1522 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001523
1524 _OBJECT_TRACING_NEXT_PTR(k_queue);
1525};
1526
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001527#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001528 { \
1529 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1530 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001531 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001532 _OBJECT_TRACING_INIT \
1533 }
1534
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001535#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1536
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001537/**
1538 * INTERNAL_HIDDEN @endcond
1539 */
1540
1541/**
1542 * @defgroup queue_apis Queue APIs
1543 * @ingroup kernel_apis
1544 * @{
1545 */
1546
1547/**
1548 * @brief Initialize a queue.
1549 *
1550 * This routine initializes a queue object, prior to its first use.
1551 *
1552 * @param queue Address of the queue.
1553 *
1554 * @return N/A
1555 */
1556extern void k_queue_init(struct k_queue *queue);
1557
1558/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001559 * @brief Cancel waiting on a queue.
1560 *
1561 * This routine causes first thread pending on @a queue, if any, to
1562 * return from k_queue_get() call with NULL value (as if timeout expired).
1563 *
1564 * @note Can be called by ISRs.
1565 *
1566 * @param queue Address of the queue.
1567 *
1568 * @return N/A
1569 */
1570extern void k_queue_cancel_wait(struct k_queue *queue);
1571
1572/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001573 * @brief Append an element to the end of a queue.
1574 *
1575 * This routine appends 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_append(struct k_queue *queue, void *data);
1587
1588/**
1589 * @brief Prepend an element to a queue.
1590 *
1591 * This routine prepends a data item to @a queue. A queue data item must be
1592 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1593 * reserved for the kernel's use.
1594 *
1595 * @note Can be called by ISRs.
1596 *
1597 * @param queue Address of the queue.
1598 * @param data Address of the data item.
1599 *
1600 * @return N/A
1601 */
1602extern void k_queue_prepend(struct k_queue *queue, void *data);
1603
1604/**
1605 * @brief Inserts an element to a queue.
1606 *
1607 * This routine inserts a data item to @a queue after previous item. A queue
1608 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1609 * item are reserved for the kernel's use.
1610 *
1611 * @note Can be called by ISRs.
1612 *
1613 * @param queue Address of the queue.
1614 * @param prev Address of the previous data item.
1615 * @param data Address of the data item.
1616 *
1617 * @return N/A
1618 */
1619extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1620
1621/**
1622 * @brief Atomically append a list of elements to a queue.
1623 *
1624 * This routine adds a list of data items to @a queue in one operation.
1625 * The data items must be in a singly-linked list, with the first 32 bits
1626 * in each data item pointing to the next data item; the list must be
1627 * NULL-terminated.
1628 *
1629 * @note Can be called by ISRs.
1630 *
1631 * @param queue Address of the queue.
1632 * @param head Pointer to first node in singly-linked list.
1633 * @param tail Pointer to last node in singly-linked list.
1634 *
1635 * @return N/A
1636 */
1637extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1638
1639/**
1640 * @brief Atomically add a list of elements to a queue.
1641 *
1642 * This routine adds a list of data items to @a queue in one operation.
1643 * The data items must be in a singly-linked list implemented using a
1644 * sys_slist_t object. Upon completion, the original list is empty.
1645 *
1646 * @note Can be called by ISRs.
1647 *
1648 * @param queue Address of the queue.
1649 * @param list Pointer to sys_slist_t object.
1650 *
1651 * @return N/A
1652 */
1653extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1654
1655/**
1656 * @brief Get an element from a queue.
1657 *
1658 * This routine removes first data item from @a queue. The first 32 bits of the
1659 * data item are reserved for the kernel's use.
1660 *
1661 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1662 *
1663 * @param queue Address of the queue.
1664 * @param timeout Waiting period to obtain a data item (in milliseconds),
1665 * or one of the special values K_NO_WAIT and K_FOREVER.
1666 *
1667 * @return Address of the data item if successful; NULL if returned
1668 * without waiting, or waiting period timed out.
1669 */
Kumar Galacc334c72017-04-21 10:55:34 -05001670extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001671
1672/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001673 * @brief Remove an element from a queue.
1674 *
1675 * This routine removes data item from @a queue. The first 32 bits of the
1676 * data item are reserved for the kernel's use. Removing elements from k_queue
1677 * rely on sys_slist_find_and_remove which is not a constant time operation.
1678 *
1679 * @note Can be called by ISRs
1680 *
1681 * @param queue Address of the queue.
1682 * @param data Address of the data item.
1683 *
1684 * @return true if data item was removed
1685 */
1686static inline bool k_queue_remove(struct k_queue *queue, void *data)
1687{
1688 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1689}
1690
1691/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001692 * @brief Query a queue to see if it has data available.
1693 *
1694 * Note that the data might be already gone by the time this function returns
1695 * if other threads are also trying to read from the queue.
1696 *
1697 * @note Can be called by ISRs.
1698 *
1699 * @param queue Address of the queue.
1700 *
1701 * @return Non-zero if the queue is empty.
1702 * @return 0 if data is available.
1703 */
1704static inline int k_queue_is_empty(struct k_queue *queue)
1705{
1706 return (int)sys_slist_is_empty(&queue->data_q);
1707}
1708
1709/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001710 * @brief Peek element at the head of queue.
1711 *
1712 * Return element from the head of queue without removing it.
1713 *
1714 * @param queue Address of the queue.
1715 *
1716 * @return Head element, or NULL if queue is empty.
1717 */
1718static inline void *k_queue_peek_head(struct k_queue *queue)
1719{
1720 return sys_slist_peek_head(&queue->data_q);
1721}
1722
1723/**
1724 * @brief Peek element at the tail of queue.
1725 *
1726 * Return element from the tail of queue without removing it.
1727 *
1728 * @param queue Address of the queue.
1729 *
1730 * @return Tail element, or NULL if queue is empty.
1731 */
1732static inline void *k_queue_peek_tail(struct k_queue *queue)
1733{
1734 return sys_slist_peek_tail(&queue->data_q);
1735}
1736
1737/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001738 * @brief Statically define and initialize a queue.
1739 *
1740 * The queue can be accessed outside the module where it is defined using:
1741 *
1742 * @code extern struct k_queue <name>; @endcode
1743 *
1744 * @param name Name of the queue.
1745 */
1746#define K_QUEUE_DEFINE(name) \
1747 struct k_queue name \
1748 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001749 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001750
1751/**
1752 * @} end defgroup queue_apis
1753 */
1754
1755/**
1756 * @cond INTERNAL_HIDDEN
1757 */
1758
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001760 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001761};
1762
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001763#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001764 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001765 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001766 }
1767
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001768#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1769
Allan Stephensc98da842016-11-11 15:45:03 -05001770/**
1771 * INTERNAL_HIDDEN @endcond
1772 */
1773
1774/**
1775 * @defgroup fifo_apis Fifo APIs
1776 * @ingroup kernel_apis
1777 * @{
1778 */
1779
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001783 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786 *
1787 * @return N/A
1788 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001789#define k_fifo_init(fifo) \
1790 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791
1792/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001793 * @brief Cancel waiting on a fifo.
1794 *
1795 * This routine causes first thread pending on @a fifo, if any, to
1796 * return from k_fifo_get() call with NULL value (as if timeout
1797 * expired).
1798 *
1799 * @note Can be called by ISRs.
1800 *
1801 * @param fifo Address of the fifo.
1802 *
1803 * @return N/A
1804 */
1805#define k_fifo_cancel_wait(fifo) \
1806 k_queue_cancel_wait((struct k_queue *) fifo)
1807
1808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001809 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001811 * This routine adds a data item to @a fifo. A fifo data item must be
1812 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1813 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @note Can be called by ISRs.
1816 *
1817 * @param fifo Address of the fifo.
1818 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
1820 * @return N/A
1821 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001822#define k_fifo_put(fifo, data) \
1823 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824
1825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001826 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * This routine adds a list of data items to @a fifo in one operation.
1829 * The data items must be in a singly-linked list, with the first 32 bits
1830 * each data item pointing to the next data item; the list must be
1831 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001833 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @param fifo Address of the fifo.
1836 * @param head Pointer to first node in singly-linked list.
1837 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838 *
1839 * @return N/A
1840 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001841#define k_fifo_put_list(fifo, head, tail) \
1842 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001843
1844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * This routine adds a list of data items to @a fifo in one operation.
1848 * The data items must be in a singly-linked list implemented using a
1849 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850 * and must be re-initialized via sys_slist_init().
1851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @note Can be called by ISRs.
1853 *
1854 * @param fifo Address of the fifo.
1855 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 *
1857 * @return N/A
1858 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001859#define k_fifo_put_slist(fifo, list) \
1860 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861
1862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001863 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * This routine removes a data item from @a fifo in a "first in, first out"
1866 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1869 *
1870 * @param fifo Address of the fifo.
1871 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001872 * or one of the special values K_NO_WAIT and K_FOREVER.
1873 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001874 * @return Address of the data item if successful; NULL if returned
1875 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001877#define k_fifo_get(fifo, timeout) \
1878 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001881 * @brief Query a fifo to see if it has data available.
1882 *
1883 * Note that the data might be already gone by the time this function returns
1884 * if other threads is also trying to read from the fifo.
1885 *
1886 * @note Can be called by ISRs.
1887 *
1888 * @param fifo Address of the fifo.
1889 *
1890 * @return Non-zero if the fifo is empty.
1891 * @return 0 if data is available.
1892 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001893#define k_fifo_is_empty(fifo) \
1894 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001895
1896/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001897 * @brief Peek element at the head of fifo.
1898 *
1899 * Return element from the head of fifo without removing it. A usecase
1900 * for this is if elements of the fifo are themselves containers. Then
1901 * on each iteration of processing, a head container will be peeked,
1902 * and some data processed out of it, and only if the container is empty,
1903 * it will be completely remove from the fifo.
1904 *
1905 * @param fifo Address of the fifo.
1906 *
1907 * @return Head element, or NULL if the fifo is empty.
1908 */
1909#define k_fifo_peek_head(fifo) \
1910 k_queue_peek_head((struct k_queue *) fifo)
1911
1912/**
1913 * @brief Peek element at the tail of fifo.
1914 *
1915 * Return element from the tail of fifo (without removing it). A usecase
1916 * for this is if elements of the fifo are themselves containers. Then
1917 * it may be useful to add more data to the last container in fifo.
1918 *
1919 * @param fifo Address of the fifo.
1920 *
1921 * @return Tail element, or NULL if fifo is empty.
1922 */
1923#define k_fifo_peek_tail(fifo) \
1924 k_queue_peek_tail((struct k_queue *) fifo)
1925
1926/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001931 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001936 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001937 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001938 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939
Allan Stephensc98da842016-11-11 15:45:03 -05001940/**
1941 * @} end defgroup fifo_apis
1942 */
1943
1944/**
1945 * @cond INTERNAL_HIDDEN
1946 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001947
1948struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001949 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950};
1951
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001952#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001953 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001954 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001955 }
1956
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001957#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1958
Allan Stephensc98da842016-11-11 15:45:03 -05001959/**
1960 * INTERNAL_HIDDEN @endcond
1961 */
1962
1963/**
1964 * @defgroup lifo_apis Lifo APIs
1965 * @ingroup kernel_apis
1966 * @{
1967 */
1968
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001970 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
1976 * @return N/A
1977 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001978#define k_lifo_init(lifo) \
1979 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980
1981/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001982 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * This routine adds a data item to @a lifo. A lifo data item must be
1985 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1986 * 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.
1989 *
1990 * @param lifo Address of the lifo.
1991 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
1993 * @return N/A
1994 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001995#define k_lifo_put(lifo, data) \
1996 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001997
1998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001999 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * This routine removes a data item from @a lifo in a "last in, first out"
2002 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2005 *
2006 * @param lifo Address of the lifo.
2007 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 * or one of the special values K_NO_WAIT and K_FOREVER.
2009 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002010 * @return Address of the data item if successful; NULL if returned
2011 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002012 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002013#define k_lifo_get(lifo, timeout) \
2014 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002015
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002021 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002026 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002027 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002028 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029
Allan Stephensc98da842016-11-11 15:45:03 -05002030/**
2031 * @} end defgroup lifo_apis
2032 */
2033
2034/**
2035 * @cond INTERNAL_HIDDEN
2036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037
2038struct k_stack {
2039 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002040 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041
Anas Nashif2f203c22016-12-18 06:57:45 -05002042 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043};
2044
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002045#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002046 { \
2047 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2048 .base = stack_buffer, \
2049 .next = stack_buffer, \
2050 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002051 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002052 }
2053
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002054#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2055
Allan Stephensc98da842016-11-11 15:45:03 -05002056/**
2057 * INTERNAL_HIDDEN @endcond
2058 */
2059
2060/**
2061 * @defgroup stack_apis Stack APIs
2062 * @ingroup kernel_apis
2063 * @{
2064 */
2065
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066/**
2067 * @brief Initialize a stack.
2068 *
2069 * This routine initializes a stack object, prior to its first use.
2070 *
2071 * @param stack Address of the stack.
2072 * @param buffer Address of array used to hold stacked values.
2073 * @param num_entries Maximum number of values that can be stacked.
2074 *
2075 * @return N/A
2076 */
Andrew Boiee8734462017-09-29 16:42:07 -07002077__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002078 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002079
2080/**
2081 * @brief Push an element onto a stack.
2082 *
2083 * This routine adds a 32-bit value @a data to @a stack.
2084 *
2085 * @note Can be called by ISRs.
2086 *
2087 * @param stack Address of the stack.
2088 * @param data Value to push onto the stack.
2089 *
2090 * @return N/A
2091 */
Andrew Boiee8734462017-09-29 16:42:07 -07002092__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093
2094/**
2095 * @brief Pop an element from a stack.
2096 *
2097 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2098 * manner and stores the value in @a data.
2099 *
2100 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2101 *
2102 * @param stack Address of the stack.
2103 * @param data Address of area to hold the value popped from the stack.
2104 * @param timeout Waiting period to obtain a value (in milliseconds),
2105 * or one of the special values K_NO_WAIT and K_FOREVER.
2106 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002107 * @retval 0 Element popped from stack.
2108 * @retval -EBUSY Returned without waiting.
2109 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 */
Andrew Boiee8734462017-09-29 16:42:07 -07002111__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002118 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * @param name Name of the stack.
2121 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002123#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002124 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002125 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002126 struct k_stack name \
2127 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002128 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002129 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002130
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131/**
Allan Stephensc98da842016-11-11 15:45:03 -05002132 * @} end defgroup stack_apis
2133 */
2134
Allan Stephens6bba9b02016-11-16 14:56:54 -05002135struct k_work;
2136
Allan Stephensc98da842016-11-11 15:45:03 -05002137/**
2138 * @defgroup workqueue_apis Workqueue Thread APIs
2139 * @ingroup kernel_apis
2140 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002141 */
2142
Allan Stephens6bba9b02016-11-16 14:56:54 -05002143/**
2144 * @typedef k_work_handler_t
2145 * @brief Work item handler function type.
2146 *
2147 * A work item's handler function is executed by a workqueue's thread
2148 * when the work item is processed by the workqueue.
2149 *
2150 * @param work Address of the work item.
2151 *
2152 * @return N/A
2153 */
2154typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155
2156/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002157 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002159
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002161 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002162 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163};
2164
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002165enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002166 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167};
2168
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002170 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171 k_work_handler_t handler;
2172 atomic_t flags[1];
2173};
2174
Allan Stephens6bba9b02016-11-16 14:56:54 -05002175struct k_delayed_work {
2176 struct k_work work;
2177 struct _timeout timeout;
2178 struct k_work_q *work_q;
2179};
2180
2181extern struct k_work_q k_sys_work_q;
2182
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * INTERNAL_HIDDEN @endcond
2185 */
2186
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002187#define _K_WORK_INITIALIZER(work_handler) \
2188 { \
2189 ._reserved = NULL, \
2190 .handler = work_handler, \
2191 .flags = { 0 } \
2192 }
2193
2194#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2195
Allan Stephens6bba9b02016-11-16 14:56:54 -05002196/**
2197 * @brief Initialize a statically-defined work item.
2198 *
2199 * This macro can be used to initialize a statically-defined workqueue work
2200 * item, prior to its first use. For example,
2201 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002202 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002204 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002205 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002206 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002207#define K_WORK_DEFINE(work, work_handler) \
2208 struct k_work work \
2209 __in_section(_k_work, static, work) = \
2210 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211
2212/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * @brief Initialize a work item.
2214 *
2215 * This routine initializes a workqueue work item, prior to its first use.
2216 *
2217 * @param work Address of work item.
2218 * @param handler Function to invoke each time work item is processed.
2219 *
2220 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 */
2222static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2223{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002224 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002226 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227}
2228
2229/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002231 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002232 * This routine submits work item @a work to be processed by workqueue
2233 * @a work_q. If the work item is already pending in the workqueue's queue
2234 * as a result of an earlier submission, this routine has no effect on the
2235 * work item. If the work item has already been processed, or is currently
2236 * being processed, its work is considered complete and the work item can be
2237 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002238 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002239 * @warning
2240 * A submitted work item must not be modified until it has been processed
2241 * by the workqueue.
2242 *
2243 * @note Can be called by ISRs.
2244 *
2245 * @param work_q Address of workqueue.
2246 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002247 *
2248 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249 */
2250static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2251 struct k_work *work)
2252{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002253 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002254 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255 }
2256}
2257
2258/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002261 * This routine indicates if work item @a work is pending in a workqueue's
2262 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002264 * @note Can be called by ISRs.
2265 *
2266 * @param work Address of work item.
2267 *
2268 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002269 */
2270static inline int k_work_pending(struct k_work *work)
2271{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002272 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002273}
2274
2275/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * This routine starts workqueue @a work_q. The workqueue spawns its work
2279 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002282 * @param stack Pointer to work queue thread's stack space, as defined by
2283 * K_THREAD_STACK_DEFINE()
2284 * @param stack_size Size of the work queue thread's stack (in bytes), which
2285 * should either be the same constant passed to
2286 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002287 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
2289 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290 */
Andrew Boie507852a2017-07-25 18:47:07 -07002291extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002292 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002293 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002296 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002298 * This routine initializes a workqueue delayed work item, prior to
2299 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * @param work Address of delayed work item.
2302 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
2304 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002306extern void k_delayed_work_init(struct k_delayed_work *work,
2307 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308
2309/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002312 * This routine schedules work item @a work to be processed by workqueue
2313 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002314 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002315 * Only when the countdown completes is the work item actually submitted to
2316 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002318 * Submitting a previously submitted delayed work item that is still
2319 * counting down cancels the existing submission and restarts the countdown
2320 * using the new delay. If the work item is currently pending on the
2321 * workqueue's queue because the countdown has completed it is too late to
2322 * resubmit the item, and resubmission fails without impacting the work item.
2323 * If the work item has already been processed, or is currently being processed,
2324 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002325 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002326 * @warning
2327 * A delayed work item must not be modified until it has been processed
2328 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002329 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002330 * @note Can be called by ISRs.
2331 *
2332 * @param work_q Address of workqueue.
2333 * @param work Address of delayed work item.
2334 * @param delay Delay before submitting the work item (in milliseconds).
2335 *
2336 * @retval 0 Work item countdown started.
2337 * @retval -EINPROGRESS Work item is already pending.
2338 * @retval -EINVAL Work item is being processed or has completed its work.
2339 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002341extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2342 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002343 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344
2345/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002348 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002349 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002350 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002352 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002354 * @param work Address of delayed work item.
2355 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002356 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002357 * @retval -EINPROGRESS Work item is already pending.
2358 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002360extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 * @brief Submit a work item to the system workqueue.
2364 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002365 * This routine submits work item @a work to be processed by the system
2366 * workqueue. If the work item is already pending in the workqueue's queue
2367 * as a result of an earlier submission, this routine has no effect on the
2368 * work item. If the work item has already been processed, or is currently
2369 * being processed, its work is considered complete and the work item can be
2370 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002372 * @warning
2373 * Work items submitted to the system workqueue should avoid using handlers
2374 * that block or yield since this may prevent the system workqueue from
2375 * processing other work items in a timely manner.
2376 *
2377 * @note Can be called by ISRs.
2378 *
2379 * @param work Address of work item.
2380 *
2381 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382 */
2383static inline void k_work_submit(struct k_work *work)
2384{
2385 k_work_submit_to_queue(&k_sys_work_q, work);
2386}
2387
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002388/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389 * @brief Submit a delayed work item to the system workqueue.
2390 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002391 * This routine schedules work item @a work to be processed by the system
2392 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002393 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002394 * Only when the countdown completes is the work item actually submitted to
2395 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397 * Submitting a previously submitted delayed work item that is still
2398 * counting down cancels the existing submission and restarts the countdown
2399 * using the new delay. If the work item is currently pending on the
2400 * workqueue's queue because the countdown has completed it is too late to
2401 * resubmit the item, and resubmission fails without impacting the work item.
2402 * If the work item has already been processed, or is currently being processed,
2403 * its work is considered complete and the work item can be resubmitted.
2404 *
2405 * @warning
2406 * Work items submitted to the system workqueue should avoid using handlers
2407 * that block or yield since this may prevent the system workqueue from
2408 * processing other work items in a timely manner.
2409 *
2410 * @note Can be called by ISRs.
2411 *
2412 * @param work Address of delayed work item.
2413 * @param delay Delay before submitting the work item (in milliseconds).
2414 *
2415 * @retval 0 Work item countdown started.
2416 * @retval -EINPROGRESS Work item is already pending.
2417 * @retval -EINVAL Work item is being processed or has completed its work.
2418 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419 */
2420static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002421 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002423 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424}
2425
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002427 * @brief Get time remaining before a delayed work gets scheduled.
2428 *
2429 * This routine computes the (approximate) time remaining before a
2430 * delayed work gets executed. If the delayed work is not waiting to be
2431 * schedules, it returns zero.
2432 *
2433 * @param work Delayed work item.
2434 *
2435 * @return Remaining time (in milliseconds).
2436 */
Kumar Galacc334c72017-04-21 10:55:34 -05002437static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002438{
2439 return _timeout_remaining_get(&work->timeout);
2440}
2441
2442/**
Allan Stephensc98da842016-11-11 15:45:03 -05002443 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 */
2445
Allan Stephensc98da842016-11-11 15:45:03 -05002446/**
2447 * @cond INTERNAL_HIDDEN
2448 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449
2450struct k_mutex {
2451 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002452 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002453 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455
Anas Nashif2f203c22016-12-18 06:57:45 -05002456 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457};
2458
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002459#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460 { \
2461 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2462 .owner = NULL, \
2463 .lock_count = 0, \
2464 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002465 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466 }
2467
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002468#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2469
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470/**
Allan Stephensc98da842016-11-11 15:45:03 -05002471 * INTERNAL_HIDDEN @endcond
2472 */
2473
2474/**
2475 * @defgroup mutex_apis Mutex APIs
2476 * @ingroup kernel_apis
2477 * @{
2478 */
2479
2480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002485 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002490 struct k_mutex name \
2491 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002492 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * Upon completion, the mutex is available and does not have an owner.
2500 *
2501 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 *
2503 * @return N/A
2504 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002505__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506
2507/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * This routine locks @a mutex. If the mutex is locked by another thread,
2511 * the calling thread waits until the mutex becomes available or until
2512 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * A thread is permitted to lock a mutex it has already locked. The operation
2515 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @param mutex Address of the mutex.
2518 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 * or one of the special values K_NO_WAIT and K_FOREVER.
2520 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002521 * @retval 0 Mutex locked.
2522 * @retval -EBUSY Returned without waiting.
2523 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002525__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002526
2527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * This routine unlocks @a mutex. The mutex must already be locked by the
2531 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532 *
2533 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 * thread.
2536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 *
2539 * @return N/A
2540 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002541__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542
Allan Stephensc98da842016-11-11 15:45:03 -05002543/**
2544 * @} end defgroup mutex_apis
2545 */
2546
2547/**
2548 * @cond INTERNAL_HIDDEN
2549 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550
2551struct k_sem {
2552 _wait_q_t wait_q;
2553 unsigned int count;
2554 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002555 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Anas Nashif2f203c22016-12-18 06:57:45 -05002557 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558};
2559
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002560#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002561 { \
2562 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2563 .count = initial_count, \
2564 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002565 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002566 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002567 }
2568
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002569#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2570
Allan Stephensc98da842016-11-11 15:45:03 -05002571/**
2572 * INTERNAL_HIDDEN @endcond
2573 */
2574
2575/**
2576 * @defgroup semaphore_apis Semaphore APIs
2577 * @ingroup kernel_apis
2578 * @{
2579 */
2580
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @param sem Address of the semaphore.
2587 * @param initial_count Initial semaphore count.
2588 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589 *
2590 * @return N/A
2591 */
Andrew Boie99280232017-09-29 14:17:47 -07002592__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2593 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002594
2595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2601 *
2602 * @param sem Address of the semaphore.
2603 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002604 * or one of the special values K_NO_WAIT and K_FOREVER.
2605 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002606 * @note When porting code from the nanokernel legacy API to the new API, be
2607 * careful with the return value of this function. The return value is the
2608 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2609 * non-zero means failure, while the nano_sem_take family returns 1 for success
2610 * and 0 for failure.
2611 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002612 * @retval 0 Semaphore taken.
2613 * @retval -EBUSY Returned without waiting.
2614 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002615 */
Andrew Boie99280232017-09-29 14:17:47 -07002616__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002617
2618/**
2619 * @brief Give a semaphore.
2620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * This routine gives @a sem, unless the semaphore is already at its maximum
2622 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 *
2628 * @return N/A
2629 */
Andrew Boie99280232017-09-29 14:17:47 -07002630__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002632/**
2633 * @brief Reset a semaphore's count to zero.
2634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * This routine sets the count of @a sem to zero.
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 *
2639 * @return N/A
2640 */
Andrew Boie990bf162017-10-03 12:36:49 -07002641__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002642
2643static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002644{
2645 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646}
2647
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648/**
2649 * @brief Get a semaphore's count.
2650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002656 */
Andrew Boie990bf162017-10-03 12:36:49 -07002657__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002658
2659static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660{
2661 return sem->count;
2662}
2663
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002668 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002669 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * @param name Name of the semaphore.
2672 * @param initial_count Initial semaphore count.
2673 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002674 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002676 struct k_sem name \
2677 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002678 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679
Allan Stephensc98da842016-11-11 15:45:03 -05002680/**
2681 * @} end defgroup semaphore_apis
2682 */
2683
2684/**
2685 * @defgroup alert_apis Alert APIs
2686 * @ingroup kernel_apis
2687 * @{
2688 */
2689
Allan Stephens5eceb852016-11-16 10:16:30 -05002690/**
2691 * @typedef k_alert_handler_t
2692 * @brief Alert handler function type.
2693 *
2694 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002695 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002696 * and is only invoked if the alert has been initialized with one.
2697 *
2698 * @param alert Address of the alert.
2699 *
2700 * @return 0 if alert has been consumed; non-zero if alert should pend.
2701 */
2702typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002703
2704/**
2705 * @} end defgroup alert_apis
2706 */
2707
2708/**
2709 * @cond INTERNAL_HIDDEN
2710 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002712#define K_ALERT_DEFAULT NULL
2713#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002715struct k_alert {
2716 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 atomic_t send_count;
2718 struct k_work work_item;
2719 struct k_sem sem;
2720
Anas Nashif2f203c22016-12-18 06:57:45 -05002721 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722};
2723
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002724extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002726#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002728 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002730 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2731 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002732 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733 }
2734
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002735#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2736
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737/**
Allan Stephensc98da842016-11-11 15:45:03 -05002738 * INTERNAL_HIDDEN @endcond
2739 */
2740
2741/**
2742 * @addtogroup alert_apis
2743 * @{
2744 */
2745
2746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002748 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002749 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002751 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * @param name Name of the alert.
2754 * @param alert_handler Action to take when alert is sent. Specify either
2755 * the address of a function to be invoked by the system workqueue
2756 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2757 * K_ALERT_DEFAULT (which causes the alert to pend).
2758 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002759 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002760#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002761 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002762 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002763 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002764 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002771 * @param alert Address of the alert.
2772 * @param handler Action to take when alert is sent. Specify either the address
2773 * of a function to be invoked by the system workqueue thread,
2774 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2775 * K_ALERT_DEFAULT (which causes the alert to pend).
2776 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002777 *
2778 * @return N/A
2779 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002780extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2781 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782
2783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2789 *
2790 * @param alert Address of the alert.
2791 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 * or one of the special values K_NO_WAIT and K_FOREVER.
2793 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002794 * @retval 0 Alert received.
2795 * @retval -EBUSY Returned without waiting.
2796 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002797 */
Andrew Boie310e9872017-09-29 04:41:15 -07002798__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799
2800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * This routine signals @a alert. The action specified for @a alert will
2804 * be taken, which may trigger the execution of an alert handler function
2805 * and/or cause the alert to pend (assuming the alert has not reached its
2806 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002808 * @note Can be called by ISRs.
2809 *
2810 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002811 *
2812 * @return N/A
2813 */
Andrew Boie310e9872017-09-29 04:41:15 -07002814__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815
2816/**
Allan Stephensc98da842016-11-11 15:45:03 -05002817 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818 */
2819
Allan Stephensc98da842016-11-11 15:45:03 -05002820/**
2821 * @cond INTERNAL_HIDDEN
2822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823
2824struct k_msgq {
2825 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002826 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002827 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828 char *buffer_start;
2829 char *buffer_end;
2830 char *read_ptr;
2831 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002832 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833
Anas Nashif2f203c22016-12-18 06:57:45 -05002834 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835};
2836
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002837#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 { \
2839 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002840 .max_msgs = q_max_msgs, \
2841 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002843 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844 .read_ptr = q_buffer, \
2845 .write_ptr = q_buffer, \
2846 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002847 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848 }
2849
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002850#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2851
Peter Mitsis1da807e2016-10-06 11:36:59 -04002852/**
Allan Stephensc98da842016-11-11 15:45:03 -05002853 * INTERNAL_HIDDEN @endcond
2854 */
2855
2856/**
2857 * @defgroup msgq_apis Message Queue APIs
2858 * @ingroup kernel_apis
2859 * @{
2860 */
2861
2862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2866 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002867 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2868 * message is similarly aligned to this boundary, @a q_msg_size must also be
2869 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * The message queue can be accessed outside the module where it is defined
2872 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002874 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * @param q_name Name of the message queue.
2877 * @param q_msg_size Message size (in bytes).
2878 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002879 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002880 */
2881#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2882 static char __noinit __aligned(q_align) \
2883 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002884 struct k_msgq q_name \
2885 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002886 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002887 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888
Peter Mitsisd7a37502016-10-13 11:37:40 -04002889/**
2890 * @brief Initialize a message queue.
2891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * This routine initializes a message queue object, prior to its first use.
2893 *
Allan Stephensda827222016-11-09 14:23:58 -06002894 * The message queue's ring buffer must contain space for @a max_msgs messages,
2895 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2896 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2897 * that each message is similarly aligned to this boundary, @a q_msg_size
2898 * must also be a multiple of N.
2899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @param q Address of the message queue.
2901 * @param buffer Pointer to ring buffer that holds queued messages.
2902 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002903 * @param max_msgs Maximum number of messages that can be queued.
2904 *
2905 * @return N/A
2906 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002907__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2908 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002909
2910/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002914 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002915 * @note Can be called by ISRs.
2916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * @param q Address of the message queue.
2918 * @param data Pointer to the message.
2919 * @param timeout Waiting period to add the message (in milliseconds),
2920 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002921 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002922 * @retval 0 Message sent.
2923 * @retval -ENOMSG Returned without waiting or queue purged.
2924 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002925 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002926__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002927
2928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * This routine receives a message from message queue @a q in a "first in,
2932 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002933 *
Allan Stephensc98da842016-11-11 15:45:03 -05002934 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @param q Address of the message queue.
2937 * @param data Address of area to hold the received message.
2938 * @param timeout Waiting period to receive the message (in milliseconds),
2939 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002940 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002941 * @retval 0 Message received.
2942 * @retval -ENOMSG Returned without waiting.
2943 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002944 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002945__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002946
2947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * This routine discards all unreceived messages in a message queue's ring
2951 * buffer. Any threads that are blocked waiting to send a message to the
2952 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002955 *
2956 * @return N/A
2957 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002958__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959
Peter Mitsis67be2492016-10-07 11:44:34 -04002960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * This routine returns the number of unused entries in a message queue's
2964 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * @param q Address of the message queue.
2967 *
2968 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002969 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002970__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2971
2972static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002973{
2974 return q->max_msgs - q->used_msgs;
2975}
2976
Peter Mitsisd7a37502016-10-13 11:37:40 -04002977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002982 * @param q Address of the message queue.
2983 *
2984 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002985 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002986__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2987
2988static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989{
2990 return q->used_msgs;
2991}
2992
Allan Stephensc98da842016-11-11 15:45:03 -05002993/**
2994 * @} end defgroup msgq_apis
2995 */
2996
2997/**
2998 * @defgroup mem_pool_apis Memory Pool APIs
2999 * @ingroup kernel_apis
3000 * @{
3001 */
3002
Andy Ross73cb9582017-05-09 10:42:39 -07003003/* Note on sizing: the use of a 20 bit field for block means that,
3004 * assuming a reasonable minimum block size of 16 bytes, we're limited
3005 * to 16M of memory managed by a single pool. Long term it would be
3006 * good to move to a variable bit size based on configuration.
3007 */
3008struct k_mem_block_id {
3009 u32_t pool : 8;
3010 u32_t level : 4;
3011 u32_t block : 20;
3012};
3013
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003016 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003017};
3018
Allan Stephensc98da842016-11-11 15:45:03 -05003019/**
3020 * @} end defgroup mem_pool_apis
3021 */
3022
3023/**
3024 * @defgroup mailbox_apis Mailbox APIs
3025 * @ingroup kernel_apis
3026 * @{
3027 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028
3029struct k_mbox_msg {
3030 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003031 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003032 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003033 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003034 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003035 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036 /** sender's message data buffer */
3037 void *tx_data;
3038 /** internal use only - needed for legacy API support */
3039 void *_rx_data;
3040 /** message data block descriptor */
3041 struct k_mem_block tx_block;
3042 /** source thread id */
3043 k_tid_t rx_source_thread;
3044 /** target thread id */
3045 k_tid_t tx_target_thread;
3046 /** internal use only - thread waiting on send (may be a dummy) */
3047 k_tid_t _syncing_thread;
3048#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3049 /** internal use only - semaphore used during asynchronous send */
3050 struct k_sem *_async_sem;
3051#endif
3052};
3053
Allan Stephensc98da842016-11-11 15:45:03 -05003054/**
3055 * @cond INTERNAL_HIDDEN
3056 */
3057
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058struct k_mbox {
3059 _wait_q_t tx_msg_queue;
3060 _wait_q_t rx_msg_queue;
3061
Anas Nashif2f203c22016-12-18 06:57:45 -05003062 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063};
3064
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003065#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066 { \
3067 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3068 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003069 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070 }
3071
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003072#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3073
Peter Mitsis12092702016-10-14 12:57:23 -04003074/**
Allan Stephensc98da842016-11-11 15:45:03 -05003075 * INTERNAL_HIDDEN @endcond
3076 */
3077
3078/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003082 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003083 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003086 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003088 struct k_mbox name \
3089 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003090 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091
Peter Mitsis12092702016-10-14 12:57:23 -04003092/**
3093 * @brief Initialize a mailbox.
3094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * This routine initializes a mailbox object, prior to its first use.
3096 *
3097 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003098 *
3099 * @return N/A
3100 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101extern void k_mbox_init(struct k_mbox *mbox);
3102
Peter Mitsis12092702016-10-14 12:57:23 -04003103/**
3104 * @brief Send a mailbox message in a synchronous manner.
3105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003106 * This routine sends a message to @a mbox and waits for a receiver to both
3107 * receive and process it. The message data may be in a buffer, in a memory
3108 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * @param mbox Address of the mailbox.
3111 * @param tx_msg Address of the transmit message descriptor.
3112 * @param timeout Waiting period for the message to be received (in
3113 * milliseconds), or one of the special values K_NO_WAIT
3114 * and K_FOREVER. Once the message has been received,
3115 * this routine waits as long as necessary for the message
3116 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003117 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003118 * @retval 0 Message sent.
3119 * @retval -ENOMSG Returned without waiting.
3120 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003121 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003122extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003123 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003124
Peter Mitsis12092702016-10-14 12:57:23 -04003125/**
3126 * @brief Send a mailbox message in an asynchronous manner.
3127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * This routine sends a message to @a mbox without waiting for a receiver
3129 * to process it. The message data may be in a buffer, in a memory pool block,
3130 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3131 * will be given when the message has been both received and completely
3132 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * @param mbox Address of the mailbox.
3135 * @param tx_msg Address of the transmit message descriptor.
3136 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003137 *
3138 * @return N/A
3139 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003140extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141 struct k_sem *sem);
3142
Peter Mitsis12092702016-10-14 12:57:23 -04003143/**
3144 * @brief Receive a mailbox message.
3145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * This routine receives a message from @a mbox, then optionally retrieves
3147 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * @param mbox Address of the mailbox.
3150 * @param rx_msg Address of the receive message descriptor.
3151 * @param buffer Address of the buffer to receive data, or NULL to defer data
3152 * retrieval and message disposal until later.
3153 * @param timeout Waiting period for a message to be received (in
3154 * milliseconds), or one of the special values K_NO_WAIT
3155 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003156 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003157 * @retval 0 Message received.
3158 * @retval -ENOMSG Returned without waiting.
3159 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003160 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003161extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003162 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003163
3164/**
3165 * @brief Retrieve mailbox message data into a buffer.
3166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * This routine completes the processing of a received message by retrieving
3168 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003169 *
3170 * Alternatively, this routine can be used to dispose of a received message
3171 * without retrieving its data.
3172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * @param rx_msg Address of the receive message descriptor.
3174 * @param buffer Address of the buffer to receive data, or NULL to discard
3175 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003176 *
3177 * @return N/A
3178 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003179extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003180
3181/**
3182 * @brief Retrieve mailbox message data into a memory pool block.
3183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003184 * This routine completes the processing of a received message by retrieving
3185 * its data into a memory pool block, then disposing of the message.
3186 * The memory pool block that results from successful retrieval must be
3187 * returned to the pool once the data has been processed, even in cases
3188 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003189 *
3190 * Alternatively, this routine can be used to dispose of a received message
3191 * without retrieving its data. In this case there is no need to return a
3192 * memory pool block to the pool.
3193 *
3194 * This routine allocates a new memory pool block for the data only if the
3195 * data is not already in one. If a new block cannot be allocated, the routine
3196 * returns a failure code and the received message is left unchanged. This
3197 * permits the caller to reattempt data retrieval at a later time or to dispose
3198 * of the received message without retrieving its data.
3199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @param rx_msg Address of a receive message descriptor.
3201 * @param pool Address of memory pool, or NULL to discard data.
3202 * @param block Address of the area to hold memory pool block info.
3203 * @param timeout Waiting period to wait for a memory pool block (in
3204 * milliseconds), or one of the special values K_NO_WAIT
3205 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003206 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003207 * @retval 0 Data retrieved.
3208 * @retval -ENOMEM Returned without waiting.
3209 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003210 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003211extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003212 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003213 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214
Allan Stephensc98da842016-11-11 15:45:03 -05003215/**
3216 * @} end defgroup mailbox_apis
3217 */
3218
3219/**
3220 * @cond INTERNAL_HIDDEN
3221 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222
3223struct k_pipe {
3224 unsigned char *buffer; /* Pipe buffer: may be NULL */
3225 size_t size; /* Buffer size */
3226 size_t bytes_used; /* # bytes used in buffer */
3227 size_t read_index; /* Where in buffer to read from */
3228 size_t write_index; /* Where in buffer to write */
3229
3230 struct {
3231 _wait_q_t readers; /* Reader wait queue */
3232 _wait_q_t writers; /* Writer wait queue */
3233 } wait_q;
3234
Anas Nashif2f203c22016-12-18 06:57:45 -05003235 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236};
3237
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003238#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239 { \
3240 .buffer = pipe_buffer, \
3241 .size = pipe_buffer_size, \
3242 .bytes_used = 0, \
3243 .read_index = 0, \
3244 .write_index = 0, \
3245 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3246 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003247 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248 }
3249
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003250#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3251
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003252/**
Allan Stephensc98da842016-11-11 15:45:03 -05003253 * INTERNAL_HIDDEN @endcond
3254 */
3255
3256/**
3257 * @defgroup pipe_apis Pipe APIs
3258 * @ingroup kernel_apis
3259 * @{
3260 */
3261
3262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003265 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003266 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003267 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @param name Name of the pipe.
3270 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3271 * or zero if no ring buffer is used.
3272 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003273 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003274#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3275 static unsigned char __noinit __aligned(pipe_align) \
3276 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003277 struct k_pipe name \
3278 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003279 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * This routine initializes a pipe object, prior to its first use.
3285 *
3286 * @param pipe Address of the pipe.
3287 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3288 * is used.
3289 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3290 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291 *
3292 * @return N/A
3293 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003294__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3295 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296
3297/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003300 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * @param pipe Address of the pipe.
3303 * @param data Address of data to write.
3304 * @param bytes_to_write Size of data (in bytes).
3305 * @param bytes_written Address of area to hold the number of bytes written.
3306 * @param min_xfer Minimum number of bytes to write.
3307 * @param timeout Waiting period to wait for the data to be written (in
3308 * milliseconds), or one of the special values K_NO_WAIT
3309 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003311 * @retval 0 At least @a min_xfer bytes of data were written.
3312 * @retval -EIO Returned without waiting; zero data bytes were written.
3313 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003316__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3317 size_t bytes_to_write, size_t *bytes_written,
3318 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319
3320/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003321 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003323 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * @param pipe Address of the pipe.
3326 * @param data Address to place the data read from pipe.
3327 * @param bytes_to_read Maximum number of data bytes to read.
3328 * @param bytes_read Address of area to hold the number of bytes read.
3329 * @param min_xfer Minimum number of data bytes to read.
3330 * @param timeout Waiting period to wait for the data to be read (in
3331 * milliseconds), or one of the special values K_NO_WAIT
3332 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003334 * @retval 0 At least @a min_xfer bytes of data were read.
3335 * @retval -EIO Returned without waiting; zero data bytes were read.
3336 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003339__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3340 size_t bytes_to_read, size_t *bytes_read,
3341 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342
3343/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * This routine writes the data contained in a memory block to @a pipe.
3347 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351 * @param block Memory block containing data to send
3352 * @param size Number of data bytes in memory block to send
3353 * @param sem Semaphore to signal upon completion (else NULL)
3354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 */
3357extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3358 size_t size, struct k_sem *sem);
3359
3360/**
Allan Stephensc98da842016-11-11 15:45:03 -05003361 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362 */
3363
Allan Stephensc98da842016-11-11 15:45:03 -05003364/**
3365 * @cond INTERNAL_HIDDEN
3366 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003368struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003369 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003370 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003371 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003372 char *buffer;
3373 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003374 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003375
Anas Nashif2f203c22016-12-18 06:57:45 -05003376 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003377};
3378
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003379#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003380 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381 { \
3382 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003383 .num_blocks = slab_num_blocks, \
3384 .block_size = slab_block_size, \
3385 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003386 .free_list = NULL, \
3387 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003388 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003389 }
3390
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003391#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3392
3393
Peter Mitsis578f9112016-10-07 13:50:31 -04003394/**
Allan Stephensc98da842016-11-11 15:45:03 -05003395 * INTERNAL_HIDDEN @endcond
3396 */
3397
3398/**
3399 * @defgroup mem_slab_apis Memory Slab APIs
3400 * @ingroup kernel_apis
3401 * @{
3402 */
3403
3404/**
Allan Stephensda827222016-11-09 14:23:58 -06003405 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003406 *
Allan Stephensda827222016-11-09 14:23:58 -06003407 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003409 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3410 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003411 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003412 *
Allan Stephensda827222016-11-09 14:23:58 -06003413 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003415 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003416 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * @param name Name of the memory slab.
3419 * @param slab_block_size Size of each memory block (in bytes).
3420 * @param slab_num_blocks Number memory blocks.
3421 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003422 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003423#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3424 char __noinit __aligned(slab_align) \
3425 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3426 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003427 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003428 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003429 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003431/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003432 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003435 *
Allan Stephensda827222016-11-09 14:23:58 -06003436 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3437 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3438 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3439 * To ensure that each memory block is similarly aligned to this boundary,
3440 * @a slab_block_size must also be a multiple of N.
3441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * @param slab Address of the memory slab.
3443 * @param buffer Pointer to buffer used for the memory blocks.
3444 * @param block_size Size of each memory block (in bytes).
3445 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446 *
3447 * @return N/A
3448 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003449extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003450 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003451
3452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * @param slab Address of the memory slab.
3458 * @param mem Pointer to block address area.
3459 * @param timeout Maximum time to wait for operation to complete
3460 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3461 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003462 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003463 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003464 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003465 * @retval -ENOMEM Returned without waiting.
3466 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003467 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003468extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003469 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003470
3471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003472 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * This routine releases a previously allocated memory block back to its
3475 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003477 * @param slab Address of the memory slab.
3478 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003479 *
3480 * @return N/A
3481 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003482extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * This routine gets the number of memory blocks that are currently
3488 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003492 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003493 */
Kumar Galacc334c72017-04-21 10:55:34 -05003494static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003496 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003497}
3498
Peter Mitsisc001aa82016-10-13 13:53:37 -04003499/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * This routine gets the number of memory blocks that are currently
3503 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003505 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003508 */
Kumar Galacc334c72017-04-21 10:55:34 -05003509static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003510{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003511 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003512}
3513
Allan Stephensc98da842016-11-11 15:45:03 -05003514/**
3515 * @} end defgroup mem_slab_apis
3516 */
3517
3518/**
3519 * @cond INTERNAL_HIDDEN
3520 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521
Andy Ross73cb9582017-05-09 10:42:39 -07003522struct k_mem_pool_lvl {
3523 union {
3524 u32_t *bits_p;
3525 u32_t bits;
3526 };
3527 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003528};
3529
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003530struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003531 void *buf;
3532 size_t max_sz;
3533 u16_t n_max;
3534 u8_t n_levels;
3535 u8_t max_inline_level;
3536 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003537 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538};
3539
Andy Ross73cb9582017-05-09 10:42:39 -07003540#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003541
Andy Ross73cb9582017-05-09 10:42:39 -07003542#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3543
3544#define _MPOOL_LVLS(maxsz, minsz) \
3545 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3546 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3547 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3548 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3549 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3550 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3551 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3552 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3553 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3554 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3555 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3556 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3557 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3558 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3559 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3560 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3561
3562/* Rounds the needed bits up to integer multiples of u32_t */
3563#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3564 ((((n_max) << (2*(l))) + 31) / 32)
3565
3566/* One word gets stored free unioned with the pointer, otherwise the
3567 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003568 */
Andy Ross73cb9582017-05-09 10:42:39 -07003569#define _MPOOL_LBIT_WORDS(n_max, l) \
3570 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3571 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003572
Andy Ross73cb9582017-05-09 10:42:39 -07003573/* How many bytes for the bitfields of a single level? */
3574#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3575 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3576 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003577
Andy Ross73cb9582017-05-09 10:42:39 -07003578/* Size of the bitmap array that follows the buffer in allocated memory */
3579#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3580 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3581 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3582 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3583 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3584 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3585 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3586 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3587 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3588 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3589 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3590 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3591 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3592 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3593 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3594 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3595 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003596
3597/**
Allan Stephensc98da842016-11-11 15:45:03 -05003598 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003599 */
3600
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003601/**
Allan Stephensc98da842016-11-11 15:45:03 -05003602 * @addtogroup mem_pool_apis
3603 * @{
3604 */
3605
3606/**
3607 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003609 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3610 * long. The memory pool allows blocks to be repeatedly partitioned into
3611 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003612 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003613 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003614 * If the pool is to be accessed outside the module where it is defined, it
3615 * can be declared via
3616 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003617 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003620 * @param minsz Size of the smallest blocks in the pool (in bytes).
3621 * @param maxsz Size of the largest blocks in the pool (in bytes).
3622 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003624 */
Andy Ross73cb9582017-05-09 10:42:39 -07003625#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3626 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3627 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3628 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3629 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3630 .buf = _mpool_buf_##name, \
3631 .max_sz = maxsz, \
3632 .n_max = nmax, \
3633 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3634 .levels = _mpool_lvls_##name, \
3635 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003636
Peter Mitsis937042c2016-10-13 13:18:26 -04003637/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * @param pool Address of the memory pool.
3643 * @param block Pointer to block descriptor for the allocated memory.
3644 * @param size Amount of memory to allocate (in bytes).
3645 * @param timeout Maximum time to wait for operation to complete
3646 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3647 * or K_FOREVER to wait as long as necessary.
3648 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003649 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003651 * @retval -ENOMEM Returned without waiting.
3652 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003653 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003654extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003655 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003656
3657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * This routine releases a previously allocated memory block back to its
3661 * memory pool.
3662 *
3663 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003664 *
3665 * @return N/A
3666 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003667extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003668
3669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003671 *
Andy Ross73cb9582017-05-09 10:42:39 -07003672 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003673 *
Andy Ross73cb9582017-05-09 10:42:39 -07003674 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003675 *
3676 * @return N/A
3677 */
Andy Ross73cb9582017-05-09 10:42:39 -07003678static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003679
3680/**
Allan Stephensc98da842016-11-11 15:45:03 -05003681 * @} end addtogroup mem_pool_apis
3682 */
3683
3684/**
3685 * @defgroup heap_apis Heap Memory Pool APIs
3686 * @ingroup kernel_apis
3687 * @{
3688 */
3689
3690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003694 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003696 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003699 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003700extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003701
3702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003704 *
3705 * This routine provides traditional free() semantics. The memory being
3706 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003707 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003708 * If @a ptr is NULL, no operation is performed.
3709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003710 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003711 *
3712 * @return N/A
3713 */
3714extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715
Allan Stephensc98da842016-11-11 15:45:03 -05003716/**
3717 * @} end defgroup heap_apis
3718 */
3719
Benjamin Walshacc68c12017-01-29 18:57:45 -05003720/* polling API - PRIVATE */
3721
Benjamin Walshb0179862017-02-02 16:39:57 -05003722#ifdef CONFIG_POLL
3723#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3724#else
3725#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3726#endif
3727
Benjamin Walshacc68c12017-01-29 18:57:45 -05003728/* private - implementation data created as needed, per-type */
3729struct _poller {
3730 struct k_thread *thread;
3731};
3732
3733/* private - types bit positions */
3734enum _poll_types_bits {
3735 /* can be used to ignore an event */
3736 _POLL_TYPE_IGNORE,
3737
3738 /* to be signaled by k_poll_signal() */
3739 _POLL_TYPE_SIGNAL,
3740
3741 /* semaphore availability */
3742 _POLL_TYPE_SEM_AVAILABLE,
3743
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003744 /* queue/fifo/lifo data availability */
3745 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003746
3747 _POLL_NUM_TYPES
3748};
3749
3750#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3751
3752/* private - states bit positions */
3753enum _poll_states_bits {
3754 /* default state when creating event */
3755 _POLL_STATE_NOT_READY,
3756
Benjamin Walshacc68c12017-01-29 18:57:45 -05003757 /* signaled by k_poll_signal() */
3758 _POLL_STATE_SIGNALED,
3759
3760 /* semaphore is available */
3761 _POLL_STATE_SEM_AVAILABLE,
3762
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003763 /* data is available to read on queue/fifo/lifo */
3764 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003765
3766 _POLL_NUM_STATES
3767};
3768
3769#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3770
3771#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003772 (32 - (0 \
3773 + 8 /* tag */ \
3774 + _POLL_NUM_TYPES \
3775 + _POLL_NUM_STATES \
3776 + 1 /* modes */ \
3777 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003778
3779#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3780#error overflow of 32-bit word in struct k_poll_event
3781#endif
3782
3783/* end of polling API - PRIVATE */
3784
3785
3786/**
3787 * @defgroup poll_apis Async polling APIs
3788 * @ingroup kernel_apis
3789 * @{
3790 */
3791
3792/* Public polling API */
3793
3794/* public - values for k_poll_event.type bitfield */
3795#define K_POLL_TYPE_IGNORE 0
3796#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3797#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003798#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3799#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003800
3801/* public - polling modes */
3802enum k_poll_modes {
3803 /* polling thread does not take ownership of objects when available */
3804 K_POLL_MODE_NOTIFY_ONLY = 0,
3805
3806 K_POLL_NUM_MODES
3807};
3808
3809/* public - values for k_poll_event.state bitfield */
3810#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3812#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003813#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3814#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815
3816/* public - poll signal object */
3817struct k_poll_signal {
3818 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003819 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820
3821 /*
3822 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3823 * user resets it to 0.
3824 */
3825 unsigned int signaled;
3826
3827 /* custom result value passed to k_poll_signal() if needed */
3828 int result;
3829};
3830
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003831#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003832 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003833 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834 .signaled = 0, \
3835 .result = 0, \
3836 }
3837
3838struct k_poll_event {
3839 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003840 sys_dnode_t _node;
3841
3842 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843 struct _poller *poller;
3844
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003845 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003846 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003847
Benjamin Walshacc68c12017-01-29 18:57:45 -05003848 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003849 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003850
3851 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003852 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003853
3854 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003855 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003856
3857 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003858 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003859
3860 /* per-type data */
3861 union {
3862 void *obj;
3863 struct k_poll_signal *signal;
3864 struct k_sem *sem;
3865 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003866 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003867 };
3868};
3869
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003870#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003871 { \
3872 .poller = NULL, \
3873 .type = event_type, \
3874 .state = K_POLL_STATE_NOT_READY, \
3875 .mode = event_mode, \
3876 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003877 { .obj = event_obj }, \
3878 }
3879
3880#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3881 event_tag) \
3882 { \
3883 .type = event_type, \
3884 .tag = event_tag, \
3885 .state = K_POLL_STATE_NOT_READY, \
3886 .mode = event_mode, \
3887 .unused = 0, \
3888 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889 }
3890
3891/**
3892 * @brief Initialize one struct k_poll_event instance
3893 *
3894 * After this routine is called on a poll event, the event it ready to be
3895 * placed in an event array to be passed to k_poll().
3896 *
3897 * @param event The event to initialize.
3898 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3899 * values. Only values that apply to the same object being polled
3900 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3901 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003902 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003903 * @param obj Kernel object or poll signal.
3904 *
3905 * @return N/A
3906 */
3907
Kumar Galacc334c72017-04-21 10:55:34 -05003908extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003909 int mode, void *obj);
3910
3911/**
3912 * @brief Wait for one or many of multiple poll events to occur
3913 *
3914 * This routine allows a thread to wait concurrently for one or many of
3915 * multiple poll events to have occurred. Such events can be a kernel object
3916 * being available, like a semaphore, or a poll signal event.
3917 *
3918 * When an event notifies that a kernel object is available, the kernel object
3919 * is not "given" to the thread calling k_poll(): it merely signals the fact
3920 * that the object was available when the k_poll() call was in effect. Also,
3921 * all threads trying to acquire an object the regular way, i.e. by pending on
3922 * the object, have precedence over the thread polling on the object. This
3923 * means that the polling thread will never get the poll event on an object
3924 * until the object becomes available and its pend queue is empty. For this
3925 * reason, the k_poll() call is more effective when the objects being polled
3926 * only have one thread, the polling thread, trying to acquire them.
3927 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003928 * When k_poll() returns 0, the caller should loop on all the events that were
3929 * passed to k_poll() and check the state field for the values that were
3930 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003931 *
3932 * Before being reused for another call to k_poll(), the user has to reset the
3933 * state field to K_POLL_STATE_NOT_READY.
3934 *
3935 * @param events An array of pointers to events to be polled for.
3936 * @param num_events The number of events in the array.
3937 * @param timeout Waiting period for an event to be ready (in milliseconds),
3938 * or one of the special values K_NO_WAIT and K_FOREVER.
3939 *
3940 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003941 * @retval -EAGAIN Waiting period timed out.
3942 */
3943
3944extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003945 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003946
3947/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003948 * @brief Initialize a poll signal object.
3949 *
3950 * Ready a poll signal object to be signaled via k_poll_signal().
3951 *
3952 * @param signal A poll signal.
3953 *
3954 * @return N/A
3955 */
3956
3957extern void k_poll_signal_init(struct k_poll_signal *signal);
3958
3959/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003960 * @brief Signal a poll signal object.
3961 *
3962 * This routine makes ready a poll signal, which is basically a poll event of
3963 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3964 * made ready to run. A @a result value can be specified.
3965 *
3966 * The poll signal contains a 'signaled' field that, when set by
3967 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3968 * be reset by the user before being passed again to k_poll() or k_poll() will
3969 * consider it being signaled, and will return immediately.
3970 *
3971 * @param signal A poll signal.
3972 * @param result The value to store in the result field of the signal.
3973 *
3974 * @retval 0 The signal was delivered successfully.
3975 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3976 */
3977
3978extern int k_poll_signal(struct k_poll_signal *signal, int result);
3979
3980/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003981extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003982
3983/**
3984 * @} end defgroup poll_apis
3985 */
3986
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003987/**
3988 * @brief Make the CPU idle.
3989 *
3990 * This function makes the CPU idle until an event wakes it up.
3991 *
3992 * In a regular system, the idle thread should be the only thread responsible
3993 * for making the CPU idle and triggering any type of power management.
3994 * However, in some more constrained systems, such as a single-threaded system,
3995 * the only thread would be responsible for this if needed.
3996 *
3997 * @return N/A
3998 */
3999extern void k_cpu_idle(void);
4000
4001/**
4002 * @brief Make the CPU idle in an atomic fashion.
4003 *
4004 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4005 * must be done atomically before making the CPU idle.
4006 *
4007 * @param key Interrupt locking key obtained from irq_lock().
4008 *
4009 * @return N/A
4010 */
4011extern void k_cpu_atomic_idle(unsigned int key);
4012
Kumar Galacc334c72017-04-21 10:55:34 -05004013extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004014
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004015#include <arch/cpu.h>
4016
Andrew Boiecdb94d62017-04-18 15:22:05 -07004017#ifdef _ARCH_EXCEPT
4018/* This archtecture has direct support for triggering a CPU exception */
4019#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4020#else
4021
4022#include <misc/printk.h>
4023
4024/* NOTE: This is the implementation for arches that do not implement
4025 * _ARCH_EXCEPT() to generate a real CPU exception.
4026 *
4027 * We won't have a real exception frame to determine the PC value when
4028 * the oops occurred, so print file and line number before we jump into
4029 * the fatal error handler.
4030 */
4031#define _k_except_reason(reason) do { \
4032 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4033 _NanoFatalErrorHandler(reason, &_default_esf); \
4034 CODE_UNREACHABLE; \
4035 } while (0)
4036
4037#endif /* _ARCH__EXCEPT */
4038
4039/**
4040 * @brief Fatally terminate a thread
4041 *
4042 * This should be called when a thread has encountered an unrecoverable
4043 * runtime condition and needs to terminate. What this ultimately
4044 * means is determined by the _fatal_error_handler() implementation, which
4045 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4046 *
4047 * If this is called from ISR context, the default system fatal error handler
4048 * will treat it as an unrecoverable system error, just like k_panic().
4049 */
4050#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4051
4052/**
4053 * @brief Fatally terminate the system
4054 *
4055 * This should be called when the Zephyr kernel has encountered an
4056 * unrecoverable runtime condition and needs to terminate. What this ultimately
4057 * means is determined by the _fatal_error_handler() implementation, which
4058 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4059 */
4060#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4061
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004062/*
4063 * private APIs that are utilized by one or more public APIs
4064 */
4065
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004066#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004067extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004068#else
4069#define _init_static_threads() do { } while ((0))
4070#endif
4071
4072extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004073extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004074
Andrew Boiedc5d9352017-06-02 12:56:47 -07004075/* arch/cpu.h may declare an architecture or platform-specific macro
4076 * for properly declaring stacks, compatible with MMU/MPU constraints if
4077 * enabled
4078 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004079
4080/**
4081 * @brief Obtain an extern reference to a stack
4082 *
4083 * This macro properly brings the symbol of a thread stack declared
4084 * elsewhere into scope.
4085 *
4086 * @param sym Thread stack symbol name
4087 */
4088#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4089
Andrew Boiedc5d9352017-06-02 12:56:47 -07004090#ifdef _ARCH_THREAD_STACK_DEFINE
4091#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4092#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4093 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4094#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4095#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004096static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004097{
4098 return _ARCH_THREAD_STACK_BUFFER(sym);
4099}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004100#else
4101/**
4102 * @brief Declare a toplevel thread stack memory region
4103 *
4104 * This declares a region of memory suitable for use as a thread's stack.
4105 *
4106 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4107 * 'noinit' section so that it isn't zeroed at boot
4108 *
Andrew Boie507852a2017-07-25 18:47:07 -07004109 * The declared symbol will always be a k_thread_stack_t which can be passed to
4110 * k_thread_create, but should otherwise not be manipulated. If the buffer
4111 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004112 *
4113 * It is legal to precede this definition with the 'static' keyword.
4114 *
4115 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4116 * parameter of k_thread_create(), it may not be the same as the
4117 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4118 *
4119 * @param sym Thread stack symbol name
4120 * @param size Size of the stack memory region
4121 */
4122#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004123 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004124
4125/**
4126 * @brief Declare a toplevel array of thread stack memory regions
4127 *
4128 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4129 * definition for additional details and constraints.
4130 *
4131 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4132 * 'noinit' section so that it isn't zeroed at boot
4133 *
4134 * @param sym Thread stack symbol name
4135 * @param nmemb Number of stacks to declare
4136 * @param size Size of the stack memory region
4137 */
4138
4139#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004140 struct _k_thread_stack_element __noinit \
4141 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004142
4143/**
4144 * @brief Declare an embedded stack memory region
4145 *
4146 * Used for stacks embedded within other data structures. Use is highly
4147 * discouraged but in some cases necessary. For memory protection scenarios,
4148 * it is very important that any RAM preceding this member not be writable
4149 * by threads else a stack overflow will lead to silent corruption. In other
4150 * words, the containing data structure should live in RAM owned by the kernel.
4151 *
4152 * @param sym Thread stack symbol name
4153 * @param size Size of the stack memory region
4154 */
4155#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004156 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004157
4158/**
4159 * @brief Return the size in bytes of a stack memory region
4160 *
4161 * Convenience macro for passing the desired stack size to k_thread_create()
4162 * since the underlying implementation may actually create something larger
4163 * (for instance a guard area).
4164 *
4165 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004166 * passed to K_THREAD_STACK_DEFINE.
4167 *
4168 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4169 * it is not guaranteed to return the original value since each array
4170 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004171 *
4172 * @param sym Stack memory symbol
4173 * @return Size of the stack
4174 */
4175#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4176
4177/**
4178 * @brief Get a pointer to the physical stack buffer
4179 *
4180 * Convenience macro to get at the real underlying stack buffer used by
4181 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4182 * This is really only intended for diagnostic tools which want to examine
4183 * stack memory contents.
4184 *
4185 * @param sym Declared stack symbol name
4186 * @return The buffer itself, a char *
4187 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004188static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004189{
4190 return (char *)sym;
4191}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004192
4193#endif /* _ARCH_DECLARE_STACK */
4194
Chunlin Hane9c97022017-07-07 20:29:30 +08004195/**
4196 * @defgroup mem_domain_apis Memory domain APIs
4197 * @ingroup kernel_apis
4198 * @{
4199 */
4200
4201/** @def MEM_PARTITION_ENTRY
4202 * @brief Used to declare a memory partition entry
4203 */
4204#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4205 {\
4206 .start = _start, \
4207 .size = _size, \
4208 .attr = _attr, \
4209 }
4210
4211/** @def K_MEM_PARTITION_DEFINE
4212 * @brief Used to declare a memory partition
4213 */
4214#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4215#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4216 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4217 struct k_mem_partition name = \
4218 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4219#else
4220#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4221 struct k_mem_partition name = \
4222 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4223#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4224
4225
4226/* memory partition */
4227struct k_mem_partition {
4228 /* start address of memory partition */
4229 u32_t start;
4230 /* size of memory partition */
4231 u32_t size;
4232 /* attribute of memory partition */
4233 u32_t attr;
4234};
4235
4236#if defined(CONFIG_USERSPACE)
4237/* memory domian */
4238struct k_mem_domain {
4239 /* number of partitions in the domain */
4240 u32_t num_partitions;
4241 /* partitions in the domain */
4242 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4243 /* domain q */
4244 sys_dlist_t mem_domain_q;
4245};
4246#endif /* CONFIG_USERSPACE */
4247
4248/**
4249 * @brief Initialize a memory domain.
4250 *
4251 * Initialize a memory domain with given name and memory partitions.
4252 *
4253 * @param domain The memory domain to be initialized.
4254 * @param num_parts The number of array items of "parts" parameter.
4255 * @param parts An array of pointers to the memory partitions. Can be NULL
4256 * if num_parts is zero.
4257 */
4258
4259extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4260 struct k_mem_partition *parts[]);
4261/**
4262 * @brief Destroy a memory domain.
4263 *
4264 * Destroy a memory domain.
4265 *
4266 * @param domain The memory domain to be destroyed.
4267 */
4268
4269extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4270
4271/**
4272 * @brief Add a memory partition into a memory domain.
4273 *
4274 * Add a memory partition into a memory domain.
4275 *
4276 * @param domain The memory domain to be added a memory partition.
4277 * @param part The memory partition to be added
4278 */
4279
4280extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4281 struct k_mem_partition *part);
4282
4283/**
4284 * @brief Remove a memory partition from a memory domain.
4285 *
4286 * Remove a memory partition from a memory domain.
4287 *
4288 * @param domain The memory domain to be removed a memory partition.
4289 * @param part The memory partition to be removed
4290 */
4291
4292extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4293 struct k_mem_partition *part);
4294
4295/**
4296 * @brief Add a thread into a memory domain.
4297 *
4298 * Add a thread into a memory domain.
4299 *
4300 * @param domain The memory domain that the thread is going to be added into.
4301 * @param thread ID of thread going to be added into the memory domain.
4302 */
4303
4304extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4305 k_tid_t thread);
4306
4307/**
4308 * @brief Remove a thread from its memory domain.
4309 *
4310 * Remove a thread from its memory domain.
4311 *
4312 * @param thread ID of thread going to be removed from its memory domain.
4313 */
4314
4315extern void k_mem_domain_remove_thread(k_tid_t thread);
4316
4317/**
4318 * @} end defgroup mem_domain_apis
4319 */
4320
Andrew Boie756f9072017-10-10 16:01:49 -07004321/**
4322 * @brief Emit a character buffer to the console device
4323 *
4324 * @param c String of characters to print
4325 * @param n The length of the string
4326 */
4327__syscall void k_str_out(char *c, size_t n);
4328
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004329#ifdef __cplusplus
4330}
4331#endif
4332
Andrew Boiee004dec2016-11-07 09:01:19 -08004333#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4334/*
4335 * Define new and delete operators.
4336 * At this moment, the operators do nothing since objects are supposed
4337 * to be statically allocated.
4338 */
4339inline void operator delete(void *ptr)
4340{
4341 (void)ptr;
4342}
4343
4344inline void operator delete[](void *ptr)
4345{
4346 (void)ptr;
4347}
4348
4349inline void *operator new(size_t size)
4350{
4351 (void)size;
4352 return NULL;
4353}
4354
4355inline void *operator new[](size_t size)
4356{
4357 (void)size;
4358 return NULL;
4359}
4360
4361/* Placement versions of operator new and delete */
4362inline void operator delete(void *ptr1, void *ptr2)
4363{
4364 (void)ptr1;
4365 (void)ptr2;
4366}
4367
4368inline void operator delete[](void *ptr1, void *ptr2)
4369{
4370 (void)ptr1;
4371 (void)ptr2;
4372}
4373
4374inline void *operator new(size_t size, void *ptr)
4375{
4376 (void)size;
4377 return ptr;
4378}
4379
4380inline void *operator new[](size_t size, void *ptr)
4381{
4382 (void)size;
4383 return ptr;
4384}
4385
4386#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4387
Andrew Boiefa94ee72017-09-28 16:54:35 -07004388#include <syscalls/kernel.h>
4389
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004390#endif /* !_ASMLANGUAGE */
4391
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004392#endif /* _kernel__h_ */