blob: 802542eaf26d8162359a589175b02f9cffb37279 [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,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700150 K_OBJ_DRIVER_COUNTER,
151 K_OBJ_DRIVER_CRYPTO,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152 K_OBJ_DRIVER_FLASH,
153 K_OBJ_DRIVER_GPIO,
154 K_OBJ_DRIVER_I2C,
155 K_OBJ_DRIVER_I2S,
156 K_OBJ_DRIVER_IPM,
157 K_OBJ_DRIVER_PINMUX,
158 K_OBJ_DRIVER_PWM,
159 K_OBJ_DRIVER_RANDOM,
160 K_OBJ_DRIVER_RTC,
161 K_OBJ_DRIVER_SENSOR,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700162 K_OBJ_DRIVER_SPI,
163 K_OBJ_DRIVER_UART,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700164
Andrew Boie945af952017-08-22 13:15:23 -0700165 K_OBJ_LAST
166};
167
168#ifdef CONFIG_USERSPACE
169/* Table generated by gperf, these objects are retrieved via
170 * _k_object_find() */
171struct _k_object {
172 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700173 u8_t perms[CONFIG_MAX_THREAD_BYTES];
174 u8_t type;
175 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700176 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700177} __packed;
178
Andrew Boie877f82e2017-10-17 11:20:22 -0700179struct _k_object_assignment {
180 struct k_thread *thread;
181 void * const *objects;
182};
183
184/**
185 * @brief Grant a static thread access to a list of kernel objects
186 *
187 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
188 * a set of kernel objects. These objects do not need to be in an initialized
189 * state. The permissions will be granted when the threads are initialized
190 * in the early boot sequence.
191 *
192 * All arguments beyond the first must be pointers to kernel objects.
193 *
194 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
195 */
196#define K_THREAD_ACCESS_GRANT(name_, ...) \
197 static void * const _CONCAT(_object_list_, name_)[] = \
198 { __VA_ARGS__, NULL }; \
199 static __used __in_section_unique(object_access) \
200 const struct _k_object_assignment \
201 _CONCAT(_object_access_, name_) = \
202 { (&_k_thread_obj_ ## name_), \
203 (_CONCAT(_object_list_, name_)) }
204
Andrew Boie945af952017-08-22 13:15:23 -0700205#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700206#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700207
208/**
209 * Lookup a kernel object and init its metadata if it exists
210 *
211 * Calling this on an object will make it usable from userspace.
212 * Intended to be called as the last statement in kernel object init
213 * functions.
214 *
215 * @param object Address of the kernel object
216 */
217void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700218#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700219
220#define K_THREAD_ACCESS_GRANT(thread, ...)
221
Andrew Boie743e4682017-10-04 12:25:50 -0700222static inline void _k_object_init(void *obj)
223{
224 ARG_UNUSED(obj);
225}
226
227static inline void _impl_k_object_access_grant(void *object,
228 struct k_thread *thread)
229{
230 ARG_UNUSED(object);
231 ARG_UNUSED(thread);
232}
233
Andrew Boiea89bf012017-10-09 14:47:55 -0700234static inline void _impl_k_object_access_revoke(void *object,
235 struct k_thread *thread)
236{
237 ARG_UNUSED(object);
238 ARG_UNUSED(thread);
239}
240
Andrew Boie41bab6e2017-10-14 14:42:23 -0700241static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700242{
243 ARG_UNUSED(object);
244}
245#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700246
247/**
248 * grant a thread access to a kernel object
249 *
250 * The thread will be granted access to the object if the caller is from
251 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700252 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700253 *
254 * @param object Address of kernel object
255 * @param thread Thread to grant access to the object
256 */
Andrew Boie743e4682017-10-04 12:25:50 -0700257__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700258
Andrew Boiea89bf012017-10-09 14:47:55 -0700259/**
260 * grant a thread access to a kernel object
261 *
262 * The thread will lose access to the object if the caller is from
263 * supervisor mode, or the caller is from user mode AND has permissions
264 * on both the object and the thread whose access is being revoked.
265 *
266 * @param object Address of kernel object
267 * @param thread Thread to remove access to the object
268 */
269__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700270
271/**
272 * grant all present and future threads access to an object
273 *
274 * If the caller is from supervisor mode, or the caller is from user mode and
275 * have sufficient permissions on the object, then that object will have
276 * permissions granted to it for *all* current and future threads running in
277 * the system, effectively becoming a public kernel object.
278 *
279 * Use of this API should be avoided on systems that are running untrusted code
280 * as it is possible for such code to derive the addresses of kernel objects
281 * and perform unwanted operations on them.
282 *
Andrew Boie04caa672017-10-13 13:57:07 -0700283 * It is not possible to revoke permissions on public objects; once public,
284 * any thread may use it.
285 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700286 * @param object Address of kernel object
287 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700288void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700289
Andrew Boiebca15da2017-10-15 14:17:48 -0700290/* Using typedef deliberately here, this is quite intended to be an opaque
291 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
292 *
293 * The purpose of this data type is to clearly distinguish between the
294 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
295 * buffer which composes the stack data actually used by the underlying
296 * thread; they cannot be used interchangably as some arches precede the
297 * stack buffer region with guard areas that trigger a MPU or MMU fault
298 * if written to.
299 *
300 * APIs that want to work with the buffer inside should continue to use
301 * char *.
302 *
303 * Stacks should always be created with K_THREAD_STACK_DEFINE().
304 */
305struct __packed _k_thread_stack_element {
306 char data;
307};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700308typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700309
Andrew Boie73abd322017-04-04 13:19:13 -0700310/* timeouts */
311
312struct _timeout;
313typedef void (*_timeout_func_t)(struct _timeout *t);
314
315struct _timeout {
316 sys_dnode_t node;
317 struct k_thread *thread;
318 sys_dlist_t *wait_q;
319 s32_t delta_ticks_from_prev;
320 _timeout_func_t func;
321};
322
323extern s32_t _timeout_remaining_get(struct _timeout *timeout);
324
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700325/**
326 * @typedef k_thread_entry_t
327 * @brief Thread entry point function type.
328 *
329 * A thread's entry point function is invoked when the thread starts executing.
330 * Up to 3 argument values can be passed to the function.
331 *
332 * The thread terminates execution permanently if the entry point function
333 * returns. The thread is responsible for releasing any shared resources
334 * it may own (such as mutexes and dynamically allocated memory), prior to
335 * returning.
336 *
337 * @param p1 First argument.
338 * @param p2 Second argument.
339 * @param p3 Third argument.
340 *
341 * @return N/A
342 */
343typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700344
345#ifdef CONFIG_THREAD_MONITOR
346struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700347 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700348 void *parameter1;
349 void *parameter2;
350 void *parameter3;
351};
352#endif
353
354/* can be used for creating 'dummy' threads, e.g. for pending on objects */
355struct _thread_base {
356
357 /* this thread's entry in a ready/wait queue */
358 sys_dnode_t k_q_node;
359
360 /* user facing 'thread options'; values defined in include/kernel.h */
361 u8_t user_options;
362
363 /* thread state */
364 u8_t thread_state;
365
366 /*
367 * scheduler lock count and thread priority
368 *
369 * These two fields control the preemptibility of a thread.
370 *
371 * When the scheduler is locked, sched_locked is decremented, which
372 * means that the scheduler is locked for values from 0xff to 0x01. A
373 * thread is coop if its prio is negative, thus 0x80 to 0xff when
374 * looked at the value as unsigned.
375 *
376 * By putting them end-to-end, this means that a thread is
377 * non-preemptible if the bundled value is greater than or equal to
378 * 0x0080.
379 */
380 union {
381 struct {
382#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
383 u8_t sched_locked;
384 s8_t prio;
385#else /* LITTLE and PDP */
386 s8_t prio;
387 u8_t sched_locked;
388#endif
389 };
390 u16_t preempt;
391 };
392
393 /* data returned by APIs */
394 void *swap_data;
395
396#ifdef CONFIG_SYS_CLOCK_EXISTS
397 /* this thread's entry in a timeout queue */
398 struct _timeout timeout;
399#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700400
401#ifdef CONFIG_USERSPACE
402 /* Bit position in kernel object permissions bitfield for this thread */
403 unsigned int perm_index;
404#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700405};
406
407typedef struct _thread_base _thread_base_t;
408
409#if defined(CONFIG_THREAD_STACK_INFO)
410/* Contains the stack information of a thread */
411struct _thread_stack_info {
412 /* Stack Start */
413 u32_t start;
414 /* Stack Size */
415 u32_t size;
416};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700417
418typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700419#endif /* CONFIG_THREAD_STACK_INFO */
420
Chunlin Hane9c97022017-07-07 20:29:30 +0800421#if defined(CONFIG_USERSPACE)
422struct _mem_domain_info {
423 /* memory domain queue node */
424 sys_dnode_t mem_domain_q_node;
425 /* memory domain of the thread */
426 struct k_mem_domain *mem_domain;
427};
428
429#endif /* CONFIG_USERSPACE */
430
Andrew Boie73abd322017-04-04 13:19:13 -0700431struct k_thread {
432
433 struct _thread_base base;
434
435 /* defined by the architecture, but all archs need these */
436 struct _caller_saved caller_saved;
437 struct _callee_saved callee_saved;
438
439 /* static thread init data */
440 void *init_data;
441
442 /* abort function */
443 void (*fn_abort)(void);
444
445#if defined(CONFIG_THREAD_MONITOR)
446 /* thread entry and parameters description */
447 struct __thread_entry *entry;
448
449 /* next item in list of all threads */
450 struct k_thread *next_thread;
451#endif
452
453#ifdef CONFIG_THREAD_CUSTOM_DATA
454 /* crude thread-local storage */
455 void *custom_data;
456#endif
457
458#ifdef CONFIG_ERRNO
459 /* per-thread errno variable */
460 int errno_var;
461#endif
462
463#if defined(CONFIG_THREAD_STACK_INFO)
464 /* Stack Info */
465 struct _thread_stack_info stack_info;
466#endif /* CONFIG_THREAD_STACK_INFO */
467
Chunlin Hane9c97022017-07-07 20:29:30 +0800468#if defined(CONFIG_USERSPACE)
469 /* memory domain info of the thread */
470 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700471 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700472 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800473#endif /* CONFIG_USERSPACE */
474
Andrew Boie73abd322017-04-04 13:19:13 -0700475 /* arch-specifics: must always be at the end */
476 struct _thread_arch arch;
477};
478
479typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400480typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700481#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400482
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400483enum execution_context_types {
484 K_ISR = 0,
485 K_COOP_THREAD,
486 K_PREEMPT_THREAD,
487};
488
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400489/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100490 * @defgroup profiling_apis Profiling APIs
491 * @ingroup kernel_apis
492 * @{
493 */
494
495/**
496 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
497 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700498 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100499 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
500 *
501 * CONFIG_MAIN_STACK_SIZE
502 * CONFIG_IDLE_STACK_SIZE
503 * CONFIG_ISR_STACK_SIZE
504 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
505 *
506 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
507 * produce output.
508 *
509 * @return N/A
510 */
511extern void k_call_stacks_analyze(void);
512
513/**
514 * @} end defgroup profiling_apis
515 */
516
517/**
Allan Stephensc98da842016-11-11 15:45:03 -0500518 * @defgroup thread_apis Thread APIs
519 * @ingroup kernel_apis
520 * @{
521 */
522
Benjamin Walshed240f22017-01-22 13:05:08 -0500523#endif /* !_ASMLANGUAGE */
524
525
526/*
527 * Thread user options. May be needed by assembly code. Common part uses low
528 * bits, arch-specific use high bits.
529 */
530
531/* system thread that must not abort */
532#define K_ESSENTIAL (1 << 0)
533
534#if defined(CONFIG_FP_SHARING)
535/* thread uses floating point registers */
536#define K_FP_REGS (1 << 1)
537#endif
538
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700539/* This thread has dropped from supervisor mode to user mode and consequently
540 * has additional restrictions
541 */
542#define K_USER (1 << 2)
543
Andrew Boie47f8fd12017-10-05 11:11:02 -0700544/* Indicates that the thread being created should inherit all kernel object
545 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
546 * is not enabled.
547 */
548#define K_INHERIT_PERMS (1 << 3)
549
Benjamin Walshed240f22017-01-22 13:05:08 -0500550#ifdef CONFIG_X86
551/* x86 Bitmask definitions for threads user options */
552
553#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
554/* thread uses SSEx (and also FP) registers */
555#define K_SSE_REGS (1 << 7)
556#endif
557#endif
558
559/* end - thread options */
560
561#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400562/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700563 * @brief Create a thread.
564 *
565 * This routine initializes a thread, then schedules it for execution.
566 *
567 * The new thread may be scheduled for immediate execution or a delayed start.
568 * If the newly spawned thread does not have a delayed start the kernel
569 * scheduler may preempt the current thread to allow the new thread to
570 * execute.
571 *
572 * Thread options are architecture-specific, and can include K_ESSENTIAL,
573 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
574 * them using "|" (the logical OR operator).
575 *
576 * Historically, users often would use the beginning of the stack memory region
577 * to store the struct k_thread data, although corruption will occur if the
578 * stack overflows this region and stack protection features may not detect this
579 * situation.
580 *
581 * @param new_thread Pointer to uninitialized struct k_thread
582 * @param stack Pointer to the stack space.
583 * @param stack_size Stack size in bytes.
584 * @param entry Thread entry function.
585 * @param p1 1st entry point parameter.
586 * @param p2 2nd entry point parameter.
587 * @param p3 3rd entry point parameter.
588 * @param prio Thread priority.
589 * @param options Thread options.
590 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
591 *
592 * @return ID of new thread.
593 */
Andrew Boie662c3452017-10-02 10:51:18 -0700594__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700595 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700596 size_t stack_size,
597 k_thread_entry_t entry,
598 void *p1, void *p2, void *p3,
599 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700600
Andrew Boie3f091b52017-08-30 14:34:14 -0700601/**
602 * @brief Drop a thread's privileges permanently to user mode
603 *
604 * @param entry Function to start executing from
605 * @param p1 1st entry point parameter
606 * @param p2 2nd entry point parameter
607 * @param p3 3rd entry point parameter
608 */
609extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
610 void *p1, void *p2,
611 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700612
Andrew Boied26cf2d2017-03-30 13:07:02 -0700613/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700614 * @brief Grant a thread access to a NULL-terminated set of kernel objects
615 *
616 * This is a convenience function. For the provided thread, grant access to
617 * the remaining arguments, which must be pointers to kernel objects.
618 * The final argument must be a NULL.
619 *
620 * The thread object must be initialized (i.e. running). The objects don't
621 * need to be.
622 *
623 * @param thread Thread to grant access to objects
624 * @param ... NULL-terminated list of kernel object pointers
625 */
626extern void __attribute__((sentinel))
627 k_thread_access_grant(struct k_thread *thread, ...);
628
629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500630 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400631 *
Allan Stephensc98da842016-11-11 15:45:03 -0500632 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500633 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500635 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 *
637 * @return N/A
638 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700639__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400640
641/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400643 *
644 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500645 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400647 * @return N/A
648 */
Kumar Galacc334c72017-04-21 10:55:34 -0500649extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650
651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500652 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500654 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657 *
658 * @return N/A
659 */
Andrew Boie468190a2017-09-29 14:00:48 -0700660__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400661
662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500665 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500667 * If @a thread is not currently sleeping, the routine has no effect.
668 *
669 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400670 *
671 * @return N/A
672 */
Andrew Boie468190a2017-09-29 14:00:48 -0700673__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674
675/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500676 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500678 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400679 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700680__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681
682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * This routine prevents @a thread from executing if it has not yet started
686 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500688 * @param thread ID of thread to cancel.
689 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700690 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500691 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 */
Andrew Boie468190a2017-09-29 14:00:48 -0700693__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695/**
Allan Stephensc98da842016-11-11 15:45:03 -0500696 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * This routine permanently stops execution of @a thread. The thread is taken
699 * off all kernel queues it is part of (i.e. the ready queue, the timeout
700 * queue, or a kernel object wait queue). However, any kernel resources the
701 * thread might currently own (such as mutexes or memory blocks) are not
702 * released. It is the responsibility of the caller of this routine to ensure
703 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500705 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400706 *
707 * @return N/A
708 */
Andrew Boie468190a2017-09-29 14:00:48 -0700709__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400710
Andrew Boie7d627c52017-08-30 11:01:56 -0700711
712/**
713 * @brief Start an inactive thread
714 *
715 * If a thread was created with K_FOREVER in the delay parameter, it will
716 * not be added to the scheduling queue until this function is called
717 * on it.
718 *
719 * @param thread thread to start
720 */
Andrew Boie468190a2017-09-29 14:00:48 -0700721__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700722
Allan Stephensc98da842016-11-11 15:45:03 -0500723/**
724 * @cond INTERNAL_HIDDEN
725 */
726
Benjamin Walshd211a522016-12-06 11:44:01 -0500727/* timeout has timed out and is not on _timeout_q anymore */
728#define _EXPIRED (-2)
729
730/* timeout is not in use */
731#define _INACTIVE (-1)
732
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400733struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700734 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700735 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400736 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700737 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500738 void *init_p1;
739 void *init_p2;
740 void *init_p3;
741 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500742 u32_t init_options;
743 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500744 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500745 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400746};
747
Andrew Boied26cf2d2017-03-30 13:07:02 -0700748#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400749 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500750 prio, options, delay, abort, groups) \
751 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700752 .init_thread = (thread), \
753 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500754 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700755 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400756 .init_p1 = (void *)p1, \
757 .init_p2 = (void *)p2, \
758 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500759 .init_prio = (prio), \
760 .init_options = (options), \
761 .init_delay = (delay), \
762 .init_abort = (abort), \
763 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764 }
765
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400766/**
Allan Stephensc98da842016-11-11 15:45:03 -0500767 * INTERNAL_HIDDEN @endcond
768 */
769
770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * @brief Statically define and initialize a thread.
772 *
773 * The thread may be scheduled for immediate execution or a delayed start.
774 *
775 * Thread options are architecture-specific, and can include K_ESSENTIAL,
776 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
777 * them using "|" (the logical OR operator).
778 *
779 * The ID of the thread can be accessed using:
780 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500781 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 *
783 * @param name Name of the thread.
784 * @param stack_size Stack size in bytes.
785 * @param entry Thread entry function.
786 * @param p1 1st entry point parameter.
787 * @param p2 2nd entry point parameter.
788 * @param p3 3rd entry point parameter.
789 * @param prio Thread priority.
790 * @param options Thread options.
791 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400792 *
793 * @internal It has been observed that the x86 compiler by default aligns
794 * these _static_thread_data structures to 32-byte boundaries, thereby
795 * wasting space. To work around this, force a 4-byte alignment.
796 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500797#define K_THREAD_DEFINE(name, stack_size, \
798 entry, p1, p2, p3, \
799 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700800 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700801 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500802 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500803 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700804 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
805 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500806 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700807 NULL, 0); \
808 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400809
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * @param thread ID of thread whose priority is needed.
816 *
817 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700819__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820
821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * Rescheduling can occur immediately depending on the priority @a thread is
827 * set to:
828 *
829 * - If its priority is raised above the priority of the caller of this
830 * function, and the caller is preemptible, @a thread will be scheduled in.
831 *
832 * - If the caller operates on itself, it lowers its priority below that of
833 * other threads in the system, and the caller is preemptible, the thread of
834 * highest priority will be scheduled in.
835 *
836 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
837 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
838 * highest priority.
839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 * @param prio New priority.
842 *
843 * @warning Changing the priority of a thread currently involved in mutex
844 * priority inheritance may result in undefined behavior.
845 *
846 * @return N/A
847 */
Andrew Boie468190a2017-09-29 14:00:48 -0700848__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400849
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * This routine prevents the kernel scheduler from making @a thread the
854 * current thread. All other internal operations on @a thread are still
855 * performed; for example, any timeout it is waiting on keeps ticking,
856 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * If @a thread is already suspended, the routine has no effect.
859 *
860 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
862 * @return N/A
863 */
Andrew Boie468190a2017-09-29 14:00:48 -0700864__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865
866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * This routine allows the kernel scheduler to make @a thread the current
870 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * If @a thread is not currently suspended, the routine has no effect.
873 *
874 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
876 * @return N/A
877 */
Andrew Boie468190a2017-09-29 14:00:48 -0700878__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * This routine specifies how the scheduler will perform time slicing of
884 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * To enable time slicing, @a slice must be non-zero. The scheduler
887 * ensures that no thread runs for more than the specified time limit
888 * before other threads of that priority are given a chance to execute.
889 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700890 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 * execute. Once the scheduler selects a thread for execution, there is no
894 * minimum guaranteed time the thread will execute before threads of greater or
895 * equal priority are scheduled.
896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 * for execution, this routine has no effect; the thread is immediately
899 * rescheduled after the slice period expires.
900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * To disable timeslicing, set both @a slice and @a prio to zero.
902 *
903 * @param slice Maximum time slice length (in milliseconds).
904 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905 *
906 * @return N/A
907 */
Kumar Galacc334c72017-04-21 10:55:34 -0500908extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400909
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910/**
Allan Stephensc98da842016-11-11 15:45:03 -0500911 * @} end defgroup thread_apis
912 */
913
914/**
915 * @addtogroup isr_apis
916 * @{
917 */
918
919/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400921 *
Allan Stephensc98da842016-11-11 15:45:03 -0500922 * This routine allows the caller to customize its actions, depending on
923 * whether it is a thread or an ISR.
924 *
925 * @note Can be called by ISRs.
926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * @return 0 if invoked by a thread.
928 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500930extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400931
Benjamin Walsh445830d2016-11-10 15:54:27 -0500932/**
933 * @brief Determine if code is running in a preemptible thread.
934 *
Allan Stephensc98da842016-11-11 15:45:03 -0500935 * This routine allows the caller to customize its actions, depending on
936 * whether it can be preempted by another thread. The routine returns a 'true'
937 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500938 *
Allan Stephensc98da842016-11-11 15:45:03 -0500939 * - The code is running in a thread, not at ISR.
940 * - The thread's priority is in the preemptible range.
941 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500942 *
Allan Stephensc98da842016-11-11 15:45:03 -0500943 * @note Can be called by ISRs.
944 *
945 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500946 * @return Non-zero if invoked by a preemptible thread.
947 */
Andrew Boie468190a2017-09-29 14:00:48 -0700948__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500949
Allan Stephensc98da842016-11-11 15:45:03 -0500950/**
951 * @} end addtogroup isr_apis
952 */
953
954/**
955 * @addtogroup thread_apis
956 * @{
957 */
958
959/**
960 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500961 *
Allan Stephensc98da842016-11-11 15:45:03 -0500962 * This routine prevents the current thread from being preempted by another
963 * thread by instructing the scheduler to treat it as a cooperative thread.
964 * If the thread subsequently performs an operation that makes it unready,
965 * it will be context switched out in the normal manner. When the thread
966 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500967 *
Allan Stephensc98da842016-11-11 15:45:03 -0500968 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500969 *
Allan Stephensc98da842016-11-11 15:45:03 -0500970 * @note k_sched_lock() and k_sched_unlock() should normally be used
971 * when the operation being performed can be safely interrupted by ISRs.
972 * However, if the amount of processing involved is very small, better
973 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500974 *
975 * @return N/A
976 */
977extern void k_sched_lock(void);
978
Allan Stephensc98da842016-11-11 15:45:03 -0500979/**
980 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500981 *
Allan Stephensc98da842016-11-11 15:45:03 -0500982 * This routine reverses the effect of a previous call to k_sched_lock().
983 * A thread must call the routine once for each time it called k_sched_lock()
984 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500985 *
986 * @return N/A
987 */
988extern void k_sched_unlock(void);
989
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * Custom data is not used by the kernel itself, and is freely available
996 * for a thread to use as it sees fit. It can be used as a framework
997 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
1001 * @return N/A
1002 */
Andrew Boie468190a2017-09-29 14:00:48 -07001003__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004
1005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011 */
Andrew Boie468190a2017-09-29 14:00:48 -07001012__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001013
1014/**
Allan Stephensc98da842016-11-11 15:45:03 -05001015 * @} end addtogroup thread_apis
1016 */
1017
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001018#include <sys_clock.h>
1019
Allan Stephensc2f15a42016-11-17 12:24:22 -05001020/**
1021 * @addtogroup clock_apis
1022 * @{
1023 */
1024
1025/**
1026 * @brief Generate null timeout delay.
1027 *
1028 * This macro generates a timeout delay that that instructs a kernel API
1029 * not to wait if the requested operation cannot be performed immediately.
1030 *
1031 * @return Timeout delay value.
1032 */
1033#define K_NO_WAIT 0
1034
1035/**
1036 * @brief Generate timeout delay from milliseconds.
1037 *
1038 * This macro generates a timeout delay that that instructs a kernel API
1039 * to wait up to @a ms milliseconds to perform the requested operation.
1040 *
1041 * @param ms Duration in milliseconds.
1042 *
1043 * @return Timeout delay value.
1044 */
Johan Hedberg14471692016-11-13 10:52:15 +02001045#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001046
1047/**
1048 * @brief Generate timeout delay from seconds.
1049 *
1050 * This macro generates a timeout delay that that instructs a kernel API
1051 * to wait up to @a s seconds to perform the requested operation.
1052 *
1053 * @param s Duration in seconds.
1054 *
1055 * @return Timeout delay value.
1056 */
Johan Hedberg14471692016-11-13 10:52:15 +02001057#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001058
1059/**
1060 * @brief Generate timeout delay from minutes.
1061 *
1062 * This macro generates a timeout delay that that instructs a kernel API
1063 * to wait up to @a m minutes to perform the requested operation.
1064 *
1065 * @param m Duration in minutes.
1066 *
1067 * @return Timeout delay value.
1068 */
Johan Hedberg14471692016-11-13 10:52:15 +02001069#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001070
1071/**
1072 * @brief Generate timeout delay from hours.
1073 *
1074 * This macro generates a timeout delay that that instructs a kernel API
1075 * to wait up to @a h hours to perform the requested operation.
1076 *
1077 * @param h Duration in hours.
1078 *
1079 * @return Timeout delay value.
1080 */
Johan Hedberg14471692016-11-13 10:52:15 +02001081#define K_HOURS(h) K_MINUTES((h) * 60)
1082
Allan Stephensc98da842016-11-11 15:45:03 -05001083/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001084 * @brief Generate infinite timeout delay.
1085 *
1086 * This macro generates a timeout delay that that instructs a kernel API
1087 * to wait as long as necessary to perform the requested operation.
1088 *
1089 * @return Timeout delay value.
1090 */
1091#define K_FOREVER (-1)
1092
1093/**
1094 * @} end addtogroup clock_apis
1095 */
1096
1097/**
Allan Stephensc98da842016-11-11 15:45:03 -05001098 * @cond INTERNAL_HIDDEN
1099 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001100
Benjamin Walsh62092182016-12-20 14:39:08 -05001101/* kernel clocks */
1102
1103#if (sys_clock_ticks_per_sec == 1000) || \
1104 (sys_clock_ticks_per_sec == 500) || \
1105 (sys_clock_ticks_per_sec == 250) || \
1106 (sys_clock_ticks_per_sec == 125) || \
1107 (sys_clock_ticks_per_sec == 100) || \
1108 (sys_clock_ticks_per_sec == 50) || \
1109 (sys_clock_ticks_per_sec == 25) || \
1110 (sys_clock_ticks_per_sec == 20) || \
1111 (sys_clock_ticks_per_sec == 10) || \
1112 (sys_clock_ticks_per_sec == 1)
1113
1114 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1115#else
1116 /* yields horrible 64-bit math on many architectures: try to avoid */
1117 #define _NON_OPTIMIZED_TICKS_PER_SEC
1118#endif
1119
1120#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001121extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001122#else
Kumar Galacc334c72017-04-21 10:55:34 -05001123static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001124{
Kumar Galacc334c72017-04-21 10:55:34 -05001125 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001126}
1127#endif
1128
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001129/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001130#ifdef CONFIG_TICKLESS_KERNEL
1131#define _TICK_ALIGN 0
1132#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001133#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001134#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001135
Kumar Galacc334c72017-04-21 10:55:34 -05001136static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001137{
Benjamin Walsh62092182016-12-20 14:39:08 -05001138#ifdef CONFIG_SYS_CLOCK_EXISTS
1139
1140#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001141 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001142#else
Kumar Galacc334c72017-04-21 10:55:34 -05001143 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001144#endif
1145
1146#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001147 __ASSERT(ticks == 0, "");
1148 return 0;
1149#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001150}
1151
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001152struct k_timer {
1153 /*
1154 * _timeout structure must be first here if we want to use
1155 * dynamic timer allocation. timeout.node is used in the double-linked
1156 * list of free timers
1157 */
1158 struct _timeout timeout;
1159
Allan Stephens45bfa372016-10-12 12:39:42 -05001160 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161 _wait_q_t wait_q;
1162
1163 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001164 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165
1166 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001167 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001168
1169 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001170 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171
Allan Stephens45bfa372016-10-12 12:39:42 -05001172 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001173 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001175 /* user-specific data, also used to support legacy features */
1176 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177
Anas Nashif2f203c22016-12-18 06:57:45 -05001178 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179};
1180
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001181#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001183 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001184 .timeout.wait_q = NULL, \
1185 .timeout.thread = NULL, \
1186 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001188 .expiry_fn = expiry, \
1189 .stop_fn = stop, \
1190 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001191 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001192 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193 }
1194
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001195#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1196
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197/**
Allan Stephensc98da842016-11-11 15:45:03 -05001198 * INTERNAL_HIDDEN @endcond
1199 */
1200
1201/**
1202 * @defgroup timer_apis Timer APIs
1203 * @ingroup kernel_apis
1204 * @{
1205 */
1206
1207/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001208 * @typedef k_timer_expiry_t
1209 * @brief Timer expiry function type.
1210 *
1211 * A timer's expiry function is executed by the system clock interrupt handler
1212 * each time the timer expires. The expiry function is optional, and is only
1213 * invoked if the timer has been initialized with one.
1214 *
1215 * @param timer Address of timer.
1216 *
1217 * @return N/A
1218 */
1219typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1220
1221/**
1222 * @typedef k_timer_stop_t
1223 * @brief Timer stop function type.
1224 *
1225 * A timer's stop function is executed if the timer is stopped prematurely.
1226 * The function runs in the context of the thread that stops the timer.
1227 * The stop function is optional, and is only invoked if the timer has been
1228 * initialized with one.
1229 *
1230 * @param timer Address of timer.
1231 *
1232 * @return N/A
1233 */
1234typedef void (*k_timer_stop_t)(struct k_timer *timer);
1235
1236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001241 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 *
1243 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * @param expiry_fn Function to invoke each time the timer expires.
1245 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001247#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001248 struct k_timer name \
1249 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001250 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001251
Allan Stephens45bfa372016-10-12 12:39:42 -05001252/**
1253 * @brief Initialize a timer.
1254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001255 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001256 *
1257 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001258 * @param expiry_fn Function to invoke each time the timer expires.
1259 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001260 *
1261 * @return N/A
1262 */
1263extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001264 k_timer_expiry_t expiry_fn,
1265 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001266
Allan Stephens45bfa372016-10-12 12:39:42 -05001267/**
1268 * @brief Start a timer.
1269 *
1270 * This routine starts a timer, and resets its status to zero. The timer
1271 * begins counting down using the specified duration and period values.
1272 *
1273 * Attempting to start a timer that is already running is permitted.
1274 * The timer's status is reset to zero and the timer begins counting down
1275 * using the new duration and period values.
1276 *
1277 * @param timer Address of timer.
1278 * @param duration Initial timer duration (in milliseconds).
1279 * @param period Timer period (in milliseconds).
1280 *
1281 * @return N/A
1282 */
Andrew Boiea354d492017-09-29 16:22:28 -07001283__syscall void k_timer_start(struct k_timer *timer,
1284 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001285
1286/**
1287 * @brief Stop a timer.
1288 *
1289 * This routine stops a running timer prematurely. The timer's stop function,
1290 * if one exists, is invoked by the caller.
1291 *
1292 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001294 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001295 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1296 * if @a k_timer_stop is to be called from ISRs.
1297 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001298 * @param timer Address of timer.
1299 *
1300 * @return N/A
1301 */
Andrew Boiea354d492017-09-29 16:22:28 -07001302__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001303
1304/**
1305 * @brief Read timer status.
1306 *
1307 * This routine reads the timer's status, which indicates the number of times
1308 * it has expired since its status was last read.
1309 *
1310 * Calling this routine resets the timer's status to zero.
1311 *
1312 * @param timer Address of timer.
1313 *
1314 * @return Timer status.
1315 */
Andrew Boiea354d492017-09-29 16:22:28 -07001316__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001317
1318/**
1319 * @brief Synchronize thread to timer expiration.
1320 *
1321 * This routine blocks the calling thread until the timer's status is non-zero
1322 * (indicating that it has expired at least once since it was last examined)
1323 * or the timer is stopped. If the timer status is already non-zero,
1324 * or the timer is already stopped, the caller continues without waiting.
1325 *
1326 * Calling this routine resets the timer's status to zero.
1327 *
1328 * This routine must not be used by interrupt handlers, since they are not
1329 * allowed to block.
1330 *
1331 * @param timer Address of timer.
1332 *
1333 * @return Timer status.
1334 */
Andrew Boiea354d492017-09-29 16:22:28 -07001335__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001336
1337/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001339 *
1340 * This routine computes the (approximate) time remaining before a running
1341 * timer next expires. If the timer is not running, it returns zero.
1342 *
1343 * @param timer Address of timer.
1344 *
1345 * @return Remaining time (in milliseconds).
1346 */
Andrew Boiea354d492017-09-29 16:22:28 -07001347__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1348
1349static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001350{
1351 return _timeout_remaining_get(&timer->timeout);
1352}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001353
Allan Stephensc98da842016-11-11 15:45:03 -05001354/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001355 * @brief Associate user-specific data with a timer.
1356 *
1357 * This routine records the @a user_data with the @a timer, to be retrieved
1358 * later.
1359 *
1360 * It can be used e.g. in a timer handler shared across multiple subsystems to
1361 * retrieve data specific to the subsystem this timer is associated with.
1362 *
1363 * @param timer Address of timer.
1364 * @param user_data User data to associate with the timer.
1365 *
1366 * @return N/A
1367 */
Andrew Boiea354d492017-09-29 16:22:28 -07001368__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1369
1370static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1371 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001372{
1373 timer->user_data = user_data;
1374}
1375
1376/**
1377 * @brief Retrieve the user-specific data from a timer.
1378 *
1379 * @param timer Address of timer.
1380 *
1381 * @return The user data.
1382 */
Andrew Boiea354d492017-09-29 16:22:28 -07001383__syscall void *k_timer_user_data_get(struct k_timer *timer);
1384
1385static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001386{
1387 return timer->user_data;
1388}
1389
1390/**
Allan Stephensc98da842016-11-11 15:45:03 -05001391 * @} end defgroup timer_apis
1392 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001393
Allan Stephensc98da842016-11-11 15:45:03 -05001394/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001395 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001396 * @{
1397 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001398
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001399/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001400 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001402 * This routine returns the elapsed time since the system booted,
1403 * in milliseconds.
1404 *
1405 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001406 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001407__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001408
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001409#ifdef CONFIG_TICKLESS_KERNEL
1410/**
1411 * @brief Enable clock always on in tickless kernel
1412 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001413 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001414 * there are no timer events programmed in tickless kernel
1415 * scheduling. This is necessary if the clock is used to track
1416 * passage of time.
1417 *
1418 * @retval prev_status Previous status of always on flag
1419 */
1420static inline int k_enable_sys_clock_always_on(void)
1421{
1422 int prev_status = _sys_clock_always_on;
1423
1424 _sys_clock_always_on = 1;
1425 _enable_sys_clock();
1426
1427 return prev_status;
1428}
1429
1430/**
1431 * @brief Disable clock always on in tickless kernel
1432 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001433 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001434 * there are no timer events programmed in tickless kernel
1435 * scheduling. To save power, this routine should be called
1436 * immediately when clock is not used to track time.
1437 */
1438static inline void k_disable_sys_clock_always_on(void)
1439{
1440 _sys_clock_always_on = 0;
1441}
1442#else
1443#define k_enable_sys_clock_always_on() do { } while ((0))
1444#define k_disable_sys_clock_always_on() do { } while ((0))
1445#endif
1446
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * This routine returns the lower 32-bits of the elapsed time since the system
1451 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * This routine can be more efficient than k_uptime_get(), as it reduces the
1454 * need for interrupt locking and 64-bit math. However, the 32-bit result
1455 * cannot hold a system uptime time larger than approximately 50 days, so the
1456 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001459 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001460__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461
1462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001463 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001465 * This routine computes the elapsed time between the current system uptime
1466 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * @param reftime Pointer to a reference time, which is updated to the current
1469 * uptime upon return.
1470 *
1471 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001472 */
Kumar Galacc334c72017-04-21 10:55:34 -05001473extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001474
1475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * This routine computes the elapsed time between the current system uptime
1479 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1482 * need for interrupt locking and 64-bit math. However, the 32-bit result
1483 * cannot hold an elapsed time larger than approximately 50 days, so the
1484 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001486 * @param reftime Pointer to a reference time, which is updated to the current
1487 * uptime upon return.
1488 *
1489 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001490 */
Kumar Galacc334c72017-04-21 10:55:34 -05001491extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001492
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001493/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001494 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * This routine returns the current time, as measured by the system's hardware
1497 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001501#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001504 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505 */
1506
Allan Stephensc98da842016-11-11 15:45:03 -05001507/**
1508 * @cond INTERNAL_HIDDEN
1509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001510
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001511struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001512 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001513 union {
1514 _wait_q_t wait_q;
1515
1516 _POLL_EVENT;
1517 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001518
1519 _OBJECT_TRACING_NEXT_PTR(k_queue);
1520};
1521
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001522#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001523 { \
1524 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1525 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001526 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001527 _OBJECT_TRACING_INIT \
1528 }
1529
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001530#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1531
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001532/**
1533 * INTERNAL_HIDDEN @endcond
1534 */
1535
1536/**
1537 * @defgroup queue_apis Queue APIs
1538 * @ingroup kernel_apis
1539 * @{
1540 */
1541
1542/**
1543 * @brief Initialize a queue.
1544 *
1545 * This routine initializes a queue object, prior to its first use.
1546 *
1547 * @param queue Address of the queue.
1548 *
1549 * @return N/A
1550 */
1551extern void k_queue_init(struct k_queue *queue);
1552
1553/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001554 * @brief Cancel waiting on a queue.
1555 *
1556 * This routine causes first thread pending on @a queue, if any, to
1557 * return from k_queue_get() call with NULL value (as if timeout expired).
1558 *
1559 * @note Can be called by ISRs.
1560 *
1561 * @param queue Address of the queue.
1562 *
1563 * @return N/A
1564 */
1565extern void k_queue_cancel_wait(struct k_queue *queue);
1566
1567/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001568 * @brief Append an element to the end of a queue.
1569 *
1570 * This routine appends a data item to @a queue. A queue data item must be
1571 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1572 * reserved for the kernel's use.
1573 *
1574 * @note Can be called by ISRs.
1575 *
1576 * @param queue Address of the queue.
1577 * @param data Address of the data item.
1578 *
1579 * @return N/A
1580 */
1581extern void k_queue_append(struct k_queue *queue, void *data);
1582
1583/**
1584 * @brief Prepend an element to a queue.
1585 *
1586 * This routine prepends a data item to @a queue. A queue data item must be
1587 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1588 * reserved for the kernel's use.
1589 *
1590 * @note Can be called by ISRs.
1591 *
1592 * @param queue Address of the queue.
1593 * @param data Address of the data item.
1594 *
1595 * @return N/A
1596 */
1597extern void k_queue_prepend(struct k_queue *queue, void *data);
1598
1599/**
1600 * @brief Inserts an element to a queue.
1601 *
1602 * This routine inserts a data item to @a queue after previous item. A queue
1603 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1604 * item are reserved for the kernel's use.
1605 *
1606 * @note Can be called by ISRs.
1607 *
1608 * @param queue Address of the queue.
1609 * @param prev Address of the previous data item.
1610 * @param data Address of the data item.
1611 *
1612 * @return N/A
1613 */
1614extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1615
1616/**
1617 * @brief Atomically append a list of elements to a queue.
1618 *
1619 * This routine adds a list of data items to @a queue in one operation.
1620 * The data items must be in a singly-linked list, with the first 32 bits
1621 * in each data item pointing to the next data item; the list must be
1622 * NULL-terminated.
1623 *
1624 * @note Can be called by ISRs.
1625 *
1626 * @param queue Address of the queue.
1627 * @param head Pointer to first node in singly-linked list.
1628 * @param tail Pointer to last node in singly-linked list.
1629 *
1630 * @return N/A
1631 */
1632extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1633
1634/**
1635 * @brief Atomically add a list of elements to a queue.
1636 *
1637 * This routine adds a list of data items to @a queue in one operation.
1638 * The data items must be in a singly-linked list implemented using a
1639 * sys_slist_t object. Upon completion, the original list is empty.
1640 *
1641 * @note Can be called by ISRs.
1642 *
1643 * @param queue Address of the queue.
1644 * @param list Pointer to sys_slist_t object.
1645 *
1646 * @return N/A
1647 */
1648extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1649
1650/**
1651 * @brief Get an element from a queue.
1652 *
1653 * This routine removes first data item from @a queue. The first 32 bits of the
1654 * data item are reserved for the kernel's use.
1655 *
1656 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1657 *
1658 * @param queue Address of the queue.
1659 * @param timeout Waiting period to obtain a data item (in milliseconds),
1660 * or one of the special values K_NO_WAIT and K_FOREVER.
1661 *
1662 * @return Address of the data item if successful; NULL if returned
1663 * without waiting, or waiting period timed out.
1664 */
Kumar Galacc334c72017-04-21 10:55:34 -05001665extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001666
1667/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001668 * @brief Remove an element from a queue.
1669 *
1670 * This routine removes data item from @a queue. The first 32 bits of the
1671 * data item are reserved for the kernel's use. Removing elements from k_queue
1672 * rely on sys_slist_find_and_remove which is not a constant time operation.
1673 *
1674 * @note Can be called by ISRs
1675 *
1676 * @param queue Address of the queue.
1677 * @param data Address of the data item.
1678 *
1679 * @return true if data item was removed
1680 */
1681static inline bool k_queue_remove(struct k_queue *queue, void *data)
1682{
1683 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1684}
1685
1686/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001687 * @brief Query a queue to see if it has data available.
1688 *
1689 * Note that the data might be already gone by the time this function returns
1690 * if other threads are also trying to read from the queue.
1691 *
1692 * @note Can be called by ISRs.
1693 *
1694 * @param queue Address of the queue.
1695 *
1696 * @return Non-zero if the queue is empty.
1697 * @return 0 if data is available.
1698 */
1699static inline int k_queue_is_empty(struct k_queue *queue)
1700{
1701 return (int)sys_slist_is_empty(&queue->data_q);
1702}
1703
1704/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001705 * @brief Peek element at the head of queue.
1706 *
1707 * Return element from the head of queue without removing it.
1708 *
1709 * @param queue Address of the queue.
1710 *
1711 * @return Head element, or NULL if queue is empty.
1712 */
1713static inline void *k_queue_peek_head(struct k_queue *queue)
1714{
1715 return sys_slist_peek_head(&queue->data_q);
1716}
1717
1718/**
1719 * @brief Peek element at the tail of queue.
1720 *
1721 * Return element from the tail of queue without removing it.
1722 *
1723 * @param queue Address of the queue.
1724 *
1725 * @return Tail element, or NULL if queue is empty.
1726 */
1727static inline void *k_queue_peek_tail(struct k_queue *queue)
1728{
1729 return sys_slist_peek_tail(&queue->data_q);
1730}
1731
1732/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001733 * @brief Statically define and initialize a queue.
1734 *
1735 * The queue can be accessed outside the module where it is defined using:
1736 *
1737 * @code extern struct k_queue <name>; @endcode
1738 *
1739 * @param name Name of the queue.
1740 */
1741#define K_QUEUE_DEFINE(name) \
1742 struct k_queue name \
1743 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001744 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001745
1746/**
1747 * @} end defgroup queue_apis
1748 */
1749
1750/**
1751 * @cond INTERNAL_HIDDEN
1752 */
1753
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001755 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001756};
1757
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001758#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001759 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001760 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001761 }
1762
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001763#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1764
Allan Stephensc98da842016-11-11 15:45:03 -05001765/**
1766 * INTERNAL_HIDDEN @endcond
1767 */
1768
1769/**
1770 * @defgroup fifo_apis Fifo APIs
1771 * @ingroup kernel_apis
1772 * @{
1773 */
1774
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781 *
1782 * @return N/A
1783 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001784#define k_fifo_init(fifo) \
1785 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786
1787/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001788 * @brief Cancel waiting on a fifo.
1789 *
1790 * This routine causes first thread pending on @a fifo, if any, to
1791 * return from k_fifo_get() call with NULL value (as if timeout
1792 * expired).
1793 *
1794 * @note Can be called by ISRs.
1795 *
1796 * @param fifo Address of the fifo.
1797 *
1798 * @return N/A
1799 */
1800#define k_fifo_cancel_wait(fifo) \
1801 k_queue_cancel_wait((struct k_queue *) fifo)
1802
1803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * This routine adds a data item to @a fifo. A fifo data item must be
1807 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1808 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @note Can be called by ISRs.
1811 *
1812 * @param fifo Address of the fifo.
1813 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
1815 * @return N/A
1816 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001817#define k_fifo_put(fifo, data) \
1818 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819
1820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * This routine adds a list of data items to @a fifo in one operation.
1824 * The data items must be in a singly-linked list, with the first 32 bits
1825 * each data item pointing to the next data item; the list must be
1826 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001830 * @param fifo Address of the fifo.
1831 * @param head Pointer to first node in singly-linked list.
1832 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833 *
1834 * @return N/A
1835 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001836#define k_fifo_put_list(fifo, head, tail) \
1837 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838
1839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001842 * This routine adds a list of data items to @a fifo in one operation.
1843 * The data items must be in a singly-linked list implemented using a
1844 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845 * and must be re-initialized via sys_slist_init().
1846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * @note Can be called by ISRs.
1848 *
1849 * @param fifo Address of the fifo.
1850 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
1852 * @return N/A
1853 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001854#define k_fifo_put_slist(fifo, list) \
1855 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856
1857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * This routine removes a data item from @a fifo in a "first in, first out"
1861 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001863 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1864 *
1865 * @param fifo Address of the fifo.
1866 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867 * or one of the special values K_NO_WAIT and K_FOREVER.
1868 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001869 * @return Address of the data item if successful; NULL if returned
1870 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001872#define k_fifo_get(fifo, timeout) \
1873 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001874
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001876 * @brief Query a fifo to see if it has data available.
1877 *
1878 * Note that the data might be already gone by the time this function returns
1879 * if other threads is also trying to read from the fifo.
1880 *
1881 * @note Can be called by ISRs.
1882 *
1883 * @param fifo Address of the fifo.
1884 *
1885 * @return Non-zero if the fifo is empty.
1886 * @return 0 if data is available.
1887 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001888#define k_fifo_is_empty(fifo) \
1889 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001890
1891/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001892 * @brief Peek element at the head of fifo.
1893 *
1894 * Return element from the head of fifo without removing it. A usecase
1895 * for this is if elements of the fifo are themselves containers. Then
1896 * on each iteration of processing, a head container will be peeked,
1897 * and some data processed out of it, and only if the container is empty,
1898 * it will be completely remove from the fifo.
1899 *
1900 * @param fifo Address of the fifo.
1901 *
1902 * @return Head element, or NULL if the fifo is empty.
1903 */
1904#define k_fifo_peek_head(fifo) \
1905 k_queue_peek_head((struct k_queue *) fifo)
1906
1907/**
1908 * @brief Peek element at the tail of fifo.
1909 *
1910 * Return element from the tail of fifo (without removing it). A usecase
1911 * for this is if elements of the fifo are themselves containers. Then
1912 * it may be useful to add more data to the last container in fifo.
1913 *
1914 * @param fifo Address of the fifo.
1915 *
1916 * @return Tail element, or NULL if fifo is empty.
1917 */
1918#define k_fifo_peek_tail(fifo) \
1919 k_queue_peek_tail((struct k_queue *) fifo)
1920
1921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001926 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001928 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001931 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001932 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001933 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934
Allan Stephensc98da842016-11-11 15:45:03 -05001935/**
1936 * @} end defgroup fifo_apis
1937 */
1938
1939/**
1940 * @cond INTERNAL_HIDDEN
1941 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001942
1943struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001944 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945};
1946
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001947#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001948 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001949 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001950 }
1951
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001952#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1953
Allan Stephensc98da842016-11-11 15:45:03 -05001954/**
1955 * INTERNAL_HIDDEN @endcond
1956 */
1957
1958/**
1959 * @defgroup lifo_apis Lifo APIs
1960 * @ingroup kernel_apis
1961 * @{
1962 */
1963
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001965 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970 *
1971 * @return N/A
1972 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001973#define k_lifo_init(lifo) \
1974 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975
1976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * This routine adds a data item to @a lifo. A lifo data item must be
1980 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1981 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @note Can be called by ISRs.
1984 *
1985 * @param lifo Address of the lifo.
1986 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987 *
1988 * @return N/A
1989 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001990#define k_lifo_put(lifo, data) \
1991 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992
1993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001994 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001996 * This routine removes a data item from @a lifo in a "last in, first out"
1997 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001999 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2000 *
2001 * @param lifo Address of the lifo.
2002 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 * or one of the special values K_NO_WAIT and K_FOREVER.
2004 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002005 * @return Address of the data item if successful; NULL if returned
2006 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002008#define k_lifo_get(lifo, timeout) \
2009 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002010
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002014 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002016 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002020#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002021 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002022 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002023 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002024
Allan Stephensc98da842016-11-11 15:45:03 -05002025/**
2026 * @} end defgroup lifo_apis
2027 */
2028
2029/**
2030 * @cond INTERNAL_HIDDEN
2031 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032
2033struct k_stack {
2034 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002035 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036
Anas Nashif2f203c22016-12-18 06:57:45 -05002037 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002038};
2039
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002040#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002041 { \
2042 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2043 .base = stack_buffer, \
2044 .next = stack_buffer, \
2045 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002046 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002047 }
2048
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002049#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2050
Allan Stephensc98da842016-11-11 15:45:03 -05002051/**
2052 * INTERNAL_HIDDEN @endcond
2053 */
2054
2055/**
2056 * @defgroup stack_apis Stack APIs
2057 * @ingroup kernel_apis
2058 * @{
2059 */
2060
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061/**
2062 * @brief Initialize a stack.
2063 *
2064 * This routine initializes a stack object, prior to its first use.
2065 *
2066 * @param stack Address of the stack.
2067 * @param buffer Address of array used to hold stacked values.
2068 * @param num_entries Maximum number of values that can be stacked.
2069 *
2070 * @return N/A
2071 */
Andrew Boiee8734462017-09-29 16:42:07 -07002072__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002073 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074
2075/**
2076 * @brief Push an element onto a stack.
2077 *
2078 * This routine adds a 32-bit value @a data to @a stack.
2079 *
2080 * @note Can be called by ISRs.
2081 *
2082 * @param stack Address of the stack.
2083 * @param data Value to push onto the stack.
2084 *
2085 * @return N/A
2086 */
Andrew Boiee8734462017-09-29 16:42:07 -07002087__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088
2089/**
2090 * @brief Pop an element from a stack.
2091 *
2092 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2093 * manner and stores the value in @a data.
2094 *
2095 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2096 *
2097 * @param stack Address of the stack.
2098 * @param data Address of area to hold the value popped from the stack.
2099 * @param timeout Waiting period to obtain a value (in milliseconds),
2100 * or one of the special values K_NO_WAIT and K_FOREVER.
2101 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002102 * @retval 0 Element popped from stack.
2103 * @retval -EBUSY Returned without waiting.
2104 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 */
Andrew Boiee8734462017-09-29 16:42:07 -07002106__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002113 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @param name Name of the stack.
2116 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002118#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002119 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002120 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002121 struct k_stack name \
2122 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002123 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002124 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002126/**
Allan Stephensc98da842016-11-11 15:45:03 -05002127 * @} end defgroup stack_apis
2128 */
2129
Allan Stephens6bba9b02016-11-16 14:56:54 -05002130struct k_work;
2131
Allan Stephensc98da842016-11-11 15:45:03 -05002132/**
2133 * @defgroup workqueue_apis Workqueue Thread APIs
2134 * @ingroup kernel_apis
2135 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002136 */
2137
Allan Stephens6bba9b02016-11-16 14:56:54 -05002138/**
2139 * @typedef k_work_handler_t
2140 * @brief Work item handler function type.
2141 *
2142 * A work item's handler function is executed by a workqueue's thread
2143 * when the work item is processed by the workqueue.
2144 *
2145 * @param work Address of the work item.
2146 *
2147 * @return N/A
2148 */
2149typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150
2151/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002152 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002154
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002156 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002157 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158};
2159
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002161 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162};
2163
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002165 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002166 k_work_handler_t handler;
2167 atomic_t flags[1];
2168};
2169
Allan Stephens6bba9b02016-11-16 14:56:54 -05002170struct k_delayed_work {
2171 struct k_work work;
2172 struct _timeout timeout;
2173 struct k_work_q *work_q;
2174};
2175
2176extern struct k_work_q k_sys_work_q;
2177
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002179 * INTERNAL_HIDDEN @endcond
2180 */
2181
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002182#define _K_WORK_INITIALIZER(work_handler) \
2183 { \
2184 ._reserved = NULL, \
2185 .handler = work_handler, \
2186 .flags = { 0 } \
2187 }
2188
2189#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2190
Allan Stephens6bba9b02016-11-16 14:56:54 -05002191/**
2192 * @brief Initialize a statically-defined work item.
2193 *
2194 * This macro can be used to initialize a statically-defined workqueue work
2195 * item, prior to its first use. For example,
2196 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002197 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002198 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002199 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002200 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002201 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002202#define K_WORK_DEFINE(work, work_handler) \
2203 struct k_work work \
2204 __in_section(_k_work, static, work) = \
2205 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002206
2207/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002208 * @brief Initialize a work item.
2209 *
2210 * This routine initializes a workqueue work item, prior to its first use.
2211 *
2212 * @param work Address of work item.
2213 * @param handler Function to invoke each time work item is processed.
2214 *
2215 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216 */
2217static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2218{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002219 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002221 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222}
2223
2224/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002226 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002227 * This routine submits work item @a work to be processed by workqueue
2228 * @a work_q. If the work item is already pending in the workqueue's queue
2229 * as a result of an earlier submission, this routine has no effect on the
2230 * work item. If the work item has already been processed, or is currently
2231 * being processed, its work is considered complete and the work item can be
2232 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002233 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * @warning
2235 * A submitted work item must not be modified until it has been processed
2236 * by the workqueue.
2237 *
2238 * @note Can be called by ISRs.
2239 *
2240 * @param work_q Address of workqueue.
2241 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002242 *
2243 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 */
2245static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2246 struct k_work *work)
2247{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002248 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002249 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 }
2251}
2252
2253/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002254 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002256 * This routine indicates if work item @a work is pending in a workqueue's
2257 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @note Can be called by ISRs.
2260 *
2261 * @param work Address of work item.
2262 *
2263 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002264 */
2265static inline int k_work_pending(struct k_work *work)
2266{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002267 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002268}
2269
2270/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002271 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * This routine starts workqueue @a work_q. The workqueue spawns its work
2274 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002277 * @param stack Pointer to work queue thread's stack space, as defined by
2278 * K_THREAD_STACK_DEFINE()
2279 * @param stack_size Size of the work queue thread's stack (in bytes), which
2280 * should either be the same constant passed to
2281 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 *
2284 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285 */
Andrew Boie507852a2017-07-25 18:47:07 -07002286extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002287 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002288 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002291 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002293 * This routine initializes a workqueue delayed work item, prior to
2294 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002296 * @param work Address of delayed work item.
2297 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298 *
2299 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002301extern void k_delayed_work_init(struct k_delayed_work *work,
2302 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303
2304/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * This routine schedules work item @a work to be processed by workqueue
2308 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002309 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * Only when the countdown completes is the work item actually submitted to
2311 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * Submitting a previously submitted delayed work item that is still
2314 * counting down cancels the existing submission and restarts the countdown
2315 * using the new delay. If the work item is currently pending on the
2316 * workqueue's queue because the countdown has completed it is too late to
2317 * resubmit the item, and resubmission fails without impacting the work item.
2318 * If the work item has already been processed, or is currently being processed,
2319 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002321 * @warning
2322 * A delayed work item must not be modified until it has been processed
2323 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * @note Can be called by ISRs.
2326 *
2327 * @param work_q Address of workqueue.
2328 * @param work Address of delayed work item.
2329 * @param delay Delay before submitting the work item (in milliseconds).
2330 *
2331 * @retval 0 Work item countdown started.
2332 * @retval -EINPROGRESS Work item is already pending.
2333 * @retval -EINVAL Work item is being processed or has completed its work.
2334 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002336extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2337 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002338 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339
2340/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002343 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002344 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002345 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002346 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002347 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002349 * @param work Address of delayed work item.
2350 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002351 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002352 * @retval -EINPROGRESS Work item is already pending.
2353 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002355extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358 * @brief Submit a work item to the system workqueue.
2359 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002360 * This routine submits work item @a work to be processed by the system
2361 * workqueue. If the work item is already pending in the workqueue's queue
2362 * as a result of an earlier submission, this routine has no effect on the
2363 * work item. If the work item has already been processed, or is currently
2364 * being processed, its work is considered complete and the work item can be
2365 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002367 * @warning
2368 * Work items submitted to the system workqueue should avoid using handlers
2369 * that block or yield since this may prevent the system workqueue from
2370 * processing other work items in a timely manner.
2371 *
2372 * @note Can be called by ISRs.
2373 *
2374 * @param work Address of work item.
2375 *
2376 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 */
2378static inline void k_work_submit(struct k_work *work)
2379{
2380 k_work_submit_to_queue(&k_sys_work_q, work);
2381}
2382
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002383/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 * @brief Submit a delayed work item to the system workqueue.
2385 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002386 * This routine schedules work item @a work to be processed by the system
2387 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002388 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002389 * Only when the countdown completes is the work item actually submitted to
2390 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002392 * Submitting a previously submitted delayed work item that is still
2393 * counting down cancels the existing submission and restarts the countdown
2394 * using the new delay. If the work item is currently pending on the
2395 * workqueue's queue because the countdown has completed it is too late to
2396 * resubmit the item, and resubmission fails without impacting the work item.
2397 * If the work item has already been processed, or is currently being processed,
2398 * its work is considered complete and the work item can be resubmitted.
2399 *
2400 * @warning
2401 * Work items submitted to the system workqueue should avoid using handlers
2402 * that block or yield since this may prevent the system workqueue from
2403 * processing other work items in a timely manner.
2404 *
2405 * @note Can be called by ISRs.
2406 *
2407 * @param work Address of delayed work item.
2408 * @param delay Delay before submitting the work item (in milliseconds).
2409 *
2410 * @retval 0 Work item countdown started.
2411 * @retval -EINPROGRESS Work item is already pending.
2412 * @retval -EINVAL Work item is being processed or has completed its work.
2413 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414 */
2415static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002416 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002418 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419}
2420
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002422 * @brief Get time remaining before a delayed work gets scheduled.
2423 *
2424 * This routine computes the (approximate) time remaining before a
2425 * delayed work gets executed. If the delayed work is not waiting to be
2426 * schedules, it returns zero.
2427 *
2428 * @param work Delayed work item.
2429 *
2430 * @return Remaining time (in milliseconds).
2431 */
Kumar Galacc334c72017-04-21 10:55:34 -05002432static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002433{
2434 return _timeout_remaining_get(&work->timeout);
2435}
2436
2437/**
Allan Stephensc98da842016-11-11 15:45:03 -05002438 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439 */
2440
Allan Stephensc98da842016-11-11 15:45:03 -05002441/**
2442 * @cond INTERNAL_HIDDEN
2443 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444
2445struct k_mutex {
2446 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002447 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002448 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450
Anas Nashif2f203c22016-12-18 06:57:45 -05002451 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452};
2453
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002454#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455 { \
2456 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2457 .owner = NULL, \
2458 .lock_count = 0, \
2459 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002460 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461 }
2462
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002463#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2464
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465/**
Allan Stephensc98da842016-11-11 15:45:03 -05002466 * INTERNAL_HIDDEN @endcond
2467 */
2468
2469/**
2470 * @defgroup mutex_apis Mutex APIs
2471 * @ingroup kernel_apis
2472 * @{
2473 */
2474
2475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002480 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002484#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002485 struct k_mutex name \
2486 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002487 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * Upon completion, the mutex is available and does not have an owner.
2495 *
2496 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497 *
2498 * @return N/A
2499 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002500__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501
2502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505 * This routine locks @a mutex. If the mutex is locked by another thread,
2506 * the calling thread waits until the mutex becomes available or until
2507 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * A thread is permitted to lock a mutex it has already locked. The operation
2510 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * @param mutex Address of the mutex.
2513 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 * or one of the special values K_NO_WAIT and K_FOREVER.
2515 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002516 * @retval 0 Mutex locked.
2517 * @retval -EBUSY Returned without waiting.
2518 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002520__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521
2522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * This routine unlocks @a mutex. The mutex must already be locked by the
2526 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
2528 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 * thread.
2531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
2534 * @return N/A
2535 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002536__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537
Allan Stephensc98da842016-11-11 15:45:03 -05002538/**
2539 * @} end defgroup mutex_apis
2540 */
2541
2542/**
2543 * @cond INTERNAL_HIDDEN
2544 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
2546struct k_sem {
2547 _wait_q_t wait_q;
2548 unsigned int count;
2549 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002550 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551
Anas Nashif2f203c22016-12-18 06:57:45 -05002552 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553};
2554
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002555#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002556 { \
2557 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2558 .count = initial_count, \
2559 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002560 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002561 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002562 }
2563
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002564#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2565
Allan Stephensc98da842016-11-11 15:45:03 -05002566/**
2567 * INTERNAL_HIDDEN @endcond
2568 */
2569
2570/**
2571 * @defgroup semaphore_apis Semaphore APIs
2572 * @ingroup kernel_apis
2573 * @{
2574 */
2575
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002576/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * @param sem Address of the semaphore.
2582 * @param initial_count Initial semaphore count.
2583 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002584 *
2585 * @return N/A
2586 */
Andrew Boie99280232017-09-29 14:17:47 -07002587__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2588 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589
2590/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2596 *
2597 * @param sem Address of the semaphore.
2598 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 * or one of the special values K_NO_WAIT and K_FOREVER.
2600 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002601 * @note When porting code from the nanokernel legacy API to the new API, be
2602 * careful with the return value of this function. The return value is the
2603 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2604 * non-zero means failure, while the nano_sem_take family returns 1 for success
2605 * and 0 for failure.
2606 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002607 * @retval 0 Semaphore taken.
2608 * @retval -EBUSY Returned without waiting.
2609 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002610 */
Andrew Boie99280232017-09-29 14:17:47 -07002611__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002612
2613/**
2614 * @brief Give a semaphore.
2615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * This routine gives @a sem, unless the semaphore is already at its maximum
2617 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002622 *
2623 * @return N/A
2624 */
Andrew Boie99280232017-09-29 14:17:47 -07002625__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002626
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627/**
2628 * @brief Reset a semaphore's count to zero.
2629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002633 *
2634 * @return N/A
2635 */
Andrew Boie990bf162017-10-03 12:36:49 -07002636__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002637
2638static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639{
2640 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641}
2642
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643/**
2644 * @brief Get a semaphore's count.
2645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002651 */
Andrew Boie990bf162017-10-03 12:36:49 -07002652__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002653
2654static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655{
2656 return sem->count;
2657}
2658
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002663 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002664 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * @param name Name of the semaphore.
2667 * @param initial_count Initial semaphore count.
2668 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002669 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002671 struct k_sem name \
2672 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002673 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
Allan Stephensc98da842016-11-11 15:45:03 -05002675/**
2676 * @} end defgroup semaphore_apis
2677 */
2678
2679/**
2680 * @defgroup alert_apis Alert APIs
2681 * @ingroup kernel_apis
2682 * @{
2683 */
2684
Allan Stephens5eceb852016-11-16 10:16:30 -05002685/**
2686 * @typedef k_alert_handler_t
2687 * @brief Alert handler function type.
2688 *
2689 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002690 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002691 * and is only invoked if the alert has been initialized with one.
2692 *
2693 * @param alert Address of the alert.
2694 *
2695 * @return 0 if alert has been consumed; non-zero if alert should pend.
2696 */
2697typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002698
2699/**
2700 * @} end defgroup alert_apis
2701 */
2702
2703/**
2704 * @cond INTERNAL_HIDDEN
2705 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002707#define K_ALERT_DEFAULT NULL
2708#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002710struct k_alert {
2711 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 atomic_t send_count;
2713 struct k_work work_item;
2714 struct k_sem sem;
2715
Anas Nashif2f203c22016-12-18 06:57:45 -05002716 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717};
2718
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002719extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002721#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002723 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002725 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2726 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002727 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 }
2729
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002730#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2731
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732/**
Allan Stephensc98da842016-11-11 15:45:03 -05002733 * INTERNAL_HIDDEN @endcond
2734 */
2735
2736/**
2737 * @addtogroup alert_apis
2738 * @{
2739 */
2740
2741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002743 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002744 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002745 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002746 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002748 * @param name Name of the alert.
2749 * @param alert_handler Action to take when alert is sent. Specify either
2750 * the address of a function to be invoked by the system workqueue
2751 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2752 * K_ALERT_DEFAULT (which causes the alert to pend).
2753 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002755#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002756 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002757 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002758 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002759 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002766 * @param alert Address of the alert.
2767 * @param handler Action to take when alert is sent. Specify either the address
2768 * of a function to be invoked by the system workqueue thread,
2769 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2770 * K_ALERT_DEFAULT (which causes the alert to pend).
2771 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002772 *
2773 * @return N/A
2774 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002775extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2776 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002777
2778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002781 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2784 *
2785 * @param alert Address of the alert.
2786 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787 * or one of the special values K_NO_WAIT and K_FOREVER.
2788 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002789 * @retval 0 Alert received.
2790 * @retval -EBUSY Returned without waiting.
2791 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 */
Andrew Boie310e9872017-09-29 04:41:15 -07002793__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794
2795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * This routine signals @a alert. The action specified for @a alert will
2799 * be taken, which may trigger the execution of an alert handler function
2800 * and/or cause the alert to pend (assuming the alert has not reached its
2801 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * @note Can be called by ISRs.
2804 *
2805 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 *
2807 * @return N/A
2808 */
Andrew Boie310e9872017-09-29 04:41:15 -07002809__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810
2811/**
Allan Stephensc98da842016-11-11 15:45:03 -05002812 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813 */
2814
Allan Stephensc98da842016-11-11 15:45:03 -05002815/**
2816 * @cond INTERNAL_HIDDEN
2817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818
2819struct k_msgq {
2820 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002821 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002822 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823 char *buffer_start;
2824 char *buffer_end;
2825 char *read_ptr;
2826 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002827 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828
Anas Nashif2f203c22016-12-18 06:57:45 -05002829 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830};
2831
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002832#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833 { \
2834 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002835 .max_msgs = q_max_msgs, \
2836 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002838 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839 .read_ptr = q_buffer, \
2840 .write_ptr = q_buffer, \
2841 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002842 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843 }
2844
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002845#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2846
Peter Mitsis1da807e2016-10-06 11:36:59 -04002847/**
Allan Stephensc98da842016-11-11 15:45:03 -05002848 * INTERNAL_HIDDEN @endcond
2849 */
2850
2851/**
2852 * @defgroup msgq_apis Message Queue APIs
2853 * @ingroup kernel_apis
2854 * @{
2855 */
2856
2857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2861 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002862 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2863 * message is similarly aligned to this boundary, @a q_msg_size must also be
2864 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * The message queue can be accessed outside the module where it is defined
2867 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002869 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @param q_name Name of the message queue.
2872 * @param q_msg_size Message size (in bytes).
2873 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002874 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002875 */
2876#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2877 static char __noinit __aligned(q_align) \
2878 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002879 struct k_msgq q_name \
2880 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002881 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002882 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883
Peter Mitsisd7a37502016-10-13 11:37:40 -04002884/**
2885 * @brief Initialize a message queue.
2886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * This routine initializes a message queue object, prior to its first use.
2888 *
Allan Stephensda827222016-11-09 14:23:58 -06002889 * The message queue's ring buffer must contain space for @a max_msgs messages,
2890 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2891 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2892 * that each message is similarly aligned to this boundary, @a q_msg_size
2893 * must also be a multiple of N.
2894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @param q Address of the message queue.
2896 * @param buffer Pointer to ring buffer that holds queued messages.
2897 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002898 * @param max_msgs Maximum number of messages that can be queued.
2899 *
2900 * @return N/A
2901 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002902__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2903 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904
2905/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002909 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002910 * @note Can be called by ISRs.
2911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @param q Address of the message queue.
2913 * @param data Pointer to the message.
2914 * @param timeout Waiting period to add the message (in milliseconds),
2915 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002916 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002917 * @retval 0 Message sent.
2918 * @retval -ENOMSG Returned without waiting or queue purged.
2919 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002920 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002921__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002922
2923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002924 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * This routine receives a message from message queue @a q in a "first in,
2927 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002928 *
Allan Stephensc98da842016-11-11 15:45:03 -05002929 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * @param q Address of the message queue.
2932 * @param data Address of area to hold the received message.
2933 * @param timeout Waiting period to receive the message (in milliseconds),
2934 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002935 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002936 * @retval 0 Message received.
2937 * @retval -ENOMSG Returned without waiting.
2938 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002939 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002940__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002941
2942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * This routine discards all unreceived messages in a message queue's ring
2946 * buffer. Any threads that are blocked waiting to send a message to the
2947 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002950 *
2951 * @return N/A
2952 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002953__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002954
Peter Mitsis67be2492016-10-07 11:44:34 -04002955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * This routine returns the number of unused entries in a message queue's
2959 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param q Address of the message queue.
2962 *
2963 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002964 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002965__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2966
2967static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002968{
2969 return q->max_msgs - q->used_msgs;
2970}
2971
Peter Mitsisd7a37502016-10-13 11:37:40 -04002972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @param q Address of the message queue.
2978 *
2979 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002980 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002981__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2982
2983static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002984{
2985 return q->used_msgs;
2986}
2987
Allan Stephensc98da842016-11-11 15:45:03 -05002988/**
2989 * @} end defgroup msgq_apis
2990 */
2991
2992/**
2993 * @defgroup mem_pool_apis Memory Pool APIs
2994 * @ingroup kernel_apis
2995 * @{
2996 */
2997
Andy Ross73cb9582017-05-09 10:42:39 -07002998/* Note on sizing: the use of a 20 bit field for block means that,
2999 * assuming a reasonable minimum block size of 16 bytes, we're limited
3000 * to 16M of memory managed by a single pool. Long term it would be
3001 * good to move to a variable bit size based on configuration.
3002 */
3003struct k_mem_block_id {
3004 u32_t pool : 8;
3005 u32_t level : 4;
3006 u32_t block : 20;
3007};
3008
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003011 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012};
3013
Allan Stephensc98da842016-11-11 15:45:03 -05003014/**
3015 * @} end defgroup mem_pool_apis
3016 */
3017
3018/**
3019 * @defgroup mailbox_apis Mailbox APIs
3020 * @ingroup kernel_apis
3021 * @{
3022 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023
3024struct k_mbox_msg {
3025 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003026 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003028 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003030 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003031 /** sender's message data buffer */
3032 void *tx_data;
3033 /** internal use only - needed for legacy API support */
3034 void *_rx_data;
3035 /** message data block descriptor */
3036 struct k_mem_block tx_block;
3037 /** source thread id */
3038 k_tid_t rx_source_thread;
3039 /** target thread id */
3040 k_tid_t tx_target_thread;
3041 /** internal use only - thread waiting on send (may be a dummy) */
3042 k_tid_t _syncing_thread;
3043#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3044 /** internal use only - semaphore used during asynchronous send */
3045 struct k_sem *_async_sem;
3046#endif
3047};
3048
Allan Stephensc98da842016-11-11 15:45:03 -05003049/**
3050 * @cond INTERNAL_HIDDEN
3051 */
3052
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053struct k_mbox {
3054 _wait_q_t tx_msg_queue;
3055 _wait_q_t rx_msg_queue;
3056
Anas Nashif2f203c22016-12-18 06:57:45 -05003057 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058};
3059
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003060#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 { \
3062 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3063 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003064 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065 }
3066
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003067#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3068
Peter Mitsis12092702016-10-14 12:57:23 -04003069/**
Allan Stephensc98da842016-11-11 15:45:03 -05003070 * INTERNAL_HIDDEN @endcond
3071 */
3072
3073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003077 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003078 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003080 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003081 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003083 struct k_mbox name \
3084 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003085 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086
Peter Mitsis12092702016-10-14 12:57:23 -04003087/**
3088 * @brief Initialize a mailbox.
3089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * This routine initializes a mailbox object, prior to its first use.
3091 *
3092 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003093 *
3094 * @return N/A
3095 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096extern void k_mbox_init(struct k_mbox *mbox);
3097
Peter Mitsis12092702016-10-14 12:57:23 -04003098/**
3099 * @brief Send a mailbox message in a synchronous manner.
3100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * This routine sends a message to @a mbox and waits for a receiver to both
3102 * receive and process it. The message data may be in a buffer, in a memory
3103 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003105 * @param mbox Address of the mailbox.
3106 * @param tx_msg Address of the transmit message descriptor.
3107 * @param timeout Waiting period for the message to be received (in
3108 * milliseconds), or one of the special values K_NO_WAIT
3109 * and K_FOREVER. Once the message has been received,
3110 * this routine waits as long as necessary for the message
3111 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003112 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003113 * @retval 0 Message sent.
3114 * @retval -ENOMSG Returned without waiting.
3115 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003116 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003117extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003118 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003119
Peter Mitsis12092702016-10-14 12:57:23 -04003120/**
3121 * @brief Send a mailbox message in an asynchronous manner.
3122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * This routine sends a message to @a mbox without waiting for a receiver
3124 * to process it. The message data may be in a buffer, in a memory pool block,
3125 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3126 * will be given when the message has been both received and completely
3127 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * @param mbox Address of the mailbox.
3130 * @param tx_msg Address of the transmit message descriptor.
3131 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003132 *
3133 * @return N/A
3134 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003135extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136 struct k_sem *sem);
3137
Peter Mitsis12092702016-10-14 12:57:23 -04003138/**
3139 * @brief Receive a mailbox message.
3140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * This routine receives a message from @a mbox, then optionally retrieves
3142 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @param mbox Address of the mailbox.
3145 * @param rx_msg Address of the receive message descriptor.
3146 * @param buffer Address of the buffer to receive data, or NULL to defer data
3147 * retrieval and message disposal until later.
3148 * @param timeout Waiting period for a message to be received (in
3149 * milliseconds), or one of the special values K_NO_WAIT
3150 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003151 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003152 * @retval 0 Message received.
3153 * @retval -ENOMSG Returned without waiting.
3154 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003155 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003156extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003157 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003158
3159/**
3160 * @brief Retrieve mailbox message data into a buffer.
3161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * This routine completes the processing of a received message by retrieving
3163 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003164 *
3165 * Alternatively, this routine can be used to dispose of a received message
3166 * without retrieving its data.
3167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003168 * @param rx_msg Address of the receive message descriptor.
3169 * @param buffer Address of the buffer to receive data, or NULL to discard
3170 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003171 *
3172 * @return N/A
3173 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003174extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003175
3176/**
3177 * @brief Retrieve mailbox message data into a memory pool block.
3178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * This routine completes the processing of a received message by retrieving
3180 * its data into a memory pool block, then disposing of the message.
3181 * The memory pool block that results from successful retrieval must be
3182 * returned to the pool once the data has been processed, even in cases
3183 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003184 *
3185 * Alternatively, this routine can be used to dispose of a received message
3186 * without retrieving its data. In this case there is no need to return a
3187 * memory pool block to the pool.
3188 *
3189 * This routine allocates a new memory pool block for the data only if the
3190 * data is not already in one. If a new block cannot be allocated, the routine
3191 * returns a failure code and the received message is left unchanged. This
3192 * permits the caller to reattempt data retrieval at a later time or to dispose
3193 * of the received message without retrieving its data.
3194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @param rx_msg Address of a receive message descriptor.
3196 * @param pool Address of memory pool, or NULL to discard data.
3197 * @param block Address of the area to hold memory pool block info.
3198 * @param timeout Waiting period to wait for a memory pool block (in
3199 * milliseconds), or one of the special values K_NO_WAIT
3200 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003201 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003202 * @retval 0 Data retrieved.
3203 * @retval -ENOMEM Returned without waiting.
3204 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003205 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003206extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003207 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003208 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209
Allan Stephensc98da842016-11-11 15:45:03 -05003210/**
3211 * @} end defgroup mailbox_apis
3212 */
3213
3214/**
3215 * @cond INTERNAL_HIDDEN
3216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217
3218struct k_pipe {
3219 unsigned char *buffer; /* Pipe buffer: may be NULL */
3220 size_t size; /* Buffer size */
3221 size_t bytes_used; /* # bytes used in buffer */
3222 size_t read_index; /* Where in buffer to read from */
3223 size_t write_index; /* Where in buffer to write */
3224
3225 struct {
3226 _wait_q_t readers; /* Reader wait queue */
3227 _wait_q_t writers; /* Writer wait queue */
3228 } wait_q;
3229
Anas Nashif2f203c22016-12-18 06:57:45 -05003230 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231};
3232
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003233#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 { \
3235 .buffer = pipe_buffer, \
3236 .size = pipe_buffer_size, \
3237 .bytes_used = 0, \
3238 .read_index = 0, \
3239 .write_index = 0, \
3240 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3241 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003242 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243 }
3244
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003245#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3246
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003247/**
Allan Stephensc98da842016-11-11 15:45:03 -05003248 * INTERNAL_HIDDEN @endcond
3249 */
3250
3251/**
3252 * @defgroup pipe_apis Pipe APIs
3253 * @ingroup kernel_apis
3254 * @{
3255 */
3256
3257/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003261 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003262 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * @param name Name of the pipe.
3265 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3266 * or zero if no ring buffer is used.
3267 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003268 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003269#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3270 static unsigned char __noinit __aligned(pipe_align) \
3271 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003272 struct k_pipe name \
3273 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003274 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * This routine initializes a pipe object, prior to its first use.
3280 *
3281 * @param pipe Address of the pipe.
3282 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3283 * is used.
3284 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3285 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286 *
3287 * @return N/A
3288 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003289__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3290 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291
3292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @param pipe Address of the pipe.
3298 * @param data Address of data to write.
3299 * @param bytes_to_write Size of data (in bytes).
3300 * @param bytes_written Address of area to hold the number of bytes written.
3301 * @param min_xfer Minimum number of bytes to write.
3302 * @param timeout Waiting period to wait for the data to be written (in
3303 * milliseconds), or one of the special values K_NO_WAIT
3304 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003306 * @retval 0 At least @a min_xfer bytes of data were written.
3307 * @retval -EIO Returned without waiting; zero data bytes were written.
3308 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003311__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3312 size_t bytes_to_write, size_t *bytes_written,
3313 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314
3315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003320 * @param pipe Address of the pipe.
3321 * @param data Address to place the data read from pipe.
3322 * @param bytes_to_read Maximum number of data bytes to read.
3323 * @param bytes_read Address of area to hold the number of bytes read.
3324 * @param min_xfer Minimum number of data bytes to read.
3325 * @param timeout Waiting period to wait for the data to be read (in
3326 * milliseconds), or one of the special values K_NO_WAIT
3327 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003329 * @retval 0 At least @a min_xfer bytes of data were read.
3330 * @retval -EIO Returned without waiting; zero data bytes were read.
3331 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003334__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3335 size_t bytes_to_read, size_t *bytes_read,
3336 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337
3338/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * This routine writes the data contained in a memory block to @a pipe.
3342 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003346 * @param block Memory block containing data to send
3347 * @param size Number of data bytes in memory block to send
3348 * @param sem Semaphore to signal upon completion (else NULL)
3349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351 */
3352extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3353 size_t size, struct k_sem *sem);
3354
3355/**
Allan Stephensc98da842016-11-11 15:45:03 -05003356 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357 */
3358
Allan Stephensc98da842016-11-11 15:45:03 -05003359/**
3360 * @cond INTERNAL_HIDDEN
3361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003363struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003364 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003365 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003366 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367 char *buffer;
3368 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003369 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370
Anas Nashif2f203c22016-12-18 06:57:45 -05003371 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003372};
3373
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003374#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003375 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376 { \
3377 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003378 .num_blocks = slab_num_blocks, \
3379 .block_size = slab_block_size, \
3380 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381 .free_list = NULL, \
3382 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003383 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384 }
3385
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003386#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3387
3388
Peter Mitsis578f9112016-10-07 13:50:31 -04003389/**
Allan Stephensc98da842016-11-11 15:45:03 -05003390 * INTERNAL_HIDDEN @endcond
3391 */
3392
3393/**
3394 * @defgroup mem_slab_apis Memory Slab APIs
3395 * @ingroup kernel_apis
3396 * @{
3397 */
3398
3399/**
Allan Stephensda827222016-11-09 14:23:58 -06003400 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003401 *
Allan Stephensda827222016-11-09 14:23:58 -06003402 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003404 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3405 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003406 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003407 *
Allan Stephensda827222016-11-09 14:23:58 -06003408 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003410 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003411 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param name Name of the memory slab.
3414 * @param slab_block_size Size of each memory block (in bytes).
3415 * @param slab_num_blocks Number memory blocks.
3416 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003417 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003418#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3419 char __noinit __aligned(slab_align) \
3420 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3421 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003422 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003423 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003424 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003426/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003427 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003430 *
Allan Stephensda827222016-11-09 14:23:58 -06003431 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3432 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3433 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3434 * To ensure that each memory block is similarly aligned to this boundary,
3435 * @a slab_block_size must also be a multiple of N.
3436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @param slab Address of the memory slab.
3438 * @param buffer Pointer to buffer used for the memory blocks.
3439 * @param block_size Size of each memory block (in bytes).
3440 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003441 *
3442 * @return N/A
3443 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003444extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003445 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446
3447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003452 * @param slab Address of the memory slab.
3453 * @param mem Pointer to block address area.
3454 * @param timeout Maximum time to wait for operation to complete
3455 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3456 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003457 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003458 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003460 * @retval -ENOMEM Returned without waiting.
3461 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003462 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003463extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003464 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003465
3466/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003467 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * This routine releases a previously allocated memory block back to its
3470 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003472 * @param slab Address of the memory slab.
3473 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003474 *
3475 * @return N/A
3476 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003477extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003478
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003479/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * This routine gets the number of memory blocks that are currently
3483 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003488 */
Kumar Galacc334c72017-04-21 10:55:34 -05003489static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003491 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003492}
3493
Peter Mitsisc001aa82016-10-13 13:53:37 -04003494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003495 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * This routine gets the number of memory blocks that are currently
3498 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003503 */
Kumar Galacc334c72017-04-21 10:55:34 -05003504static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003505{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003506 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003507}
3508
Allan Stephensc98da842016-11-11 15:45:03 -05003509/**
3510 * @} end defgroup mem_slab_apis
3511 */
3512
3513/**
3514 * @cond INTERNAL_HIDDEN
3515 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003516
Andy Ross73cb9582017-05-09 10:42:39 -07003517struct k_mem_pool_lvl {
3518 union {
3519 u32_t *bits_p;
3520 u32_t bits;
3521 };
3522 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003523};
3524
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003525struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003526 void *buf;
3527 size_t max_sz;
3528 u16_t n_max;
3529 u8_t n_levels;
3530 u8_t max_inline_level;
3531 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003532 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003533};
3534
Andy Ross73cb9582017-05-09 10:42:39 -07003535#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536
Andy Ross73cb9582017-05-09 10:42:39 -07003537#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3538
3539#define _MPOOL_LVLS(maxsz, minsz) \
3540 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3541 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3542 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3543 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3544 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3545 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3546 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3547 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3548 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3549 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3550 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3551 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3552 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3553 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3554 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3555 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3556
3557/* Rounds the needed bits up to integer multiples of u32_t */
3558#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3559 ((((n_max) << (2*(l))) + 31) / 32)
3560
3561/* One word gets stored free unioned with the pointer, otherwise the
3562 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003563 */
Andy Ross73cb9582017-05-09 10:42:39 -07003564#define _MPOOL_LBIT_WORDS(n_max, l) \
3565 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3566 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003567
Andy Ross73cb9582017-05-09 10:42:39 -07003568/* How many bytes for the bitfields of a single level? */
3569#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3570 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3571 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003572
Andy Ross73cb9582017-05-09 10:42:39 -07003573/* Size of the bitmap array that follows the buffer in allocated memory */
3574#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3575 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3576 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3577 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3578 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3579 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3580 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3581 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3582 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3583 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3584 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3585 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3586 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3587 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3588 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3589 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3590 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003591
3592/**
Allan Stephensc98da842016-11-11 15:45:03 -05003593 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003594 */
3595
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003596/**
Allan Stephensc98da842016-11-11 15:45:03 -05003597 * @addtogroup mem_pool_apis
3598 * @{
3599 */
3600
3601/**
3602 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3605 * long. The memory pool allows blocks to be repeatedly partitioned into
3606 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003607 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003608 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003609 * If the pool is to be accessed outside the module where it is defined, it
3610 * can be declared via
3611 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003612 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003615 * @param minsz Size of the smallest blocks in the pool (in bytes).
3616 * @param maxsz Size of the largest blocks in the pool (in bytes).
3617 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003619 */
Andy Ross73cb9582017-05-09 10:42:39 -07003620#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3621 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3622 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3623 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3624 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3625 .buf = _mpool_buf_##name, \
3626 .max_sz = maxsz, \
3627 .n_max = nmax, \
3628 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3629 .levels = _mpool_lvls_##name, \
3630 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003631
Peter Mitsis937042c2016-10-13 13:18:26 -04003632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003633 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003637 * @param pool Address of the memory pool.
3638 * @param block Pointer to block descriptor for the allocated memory.
3639 * @param size Amount of memory to allocate (in bytes).
3640 * @param timeout Maximum time to wait for operation to complete
3641 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3642 * or K_FOREVER to wait as long as necessary.
3643 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003644 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003646 * @retval -ENOMEM Returned without waiting.
3647 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003648 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003649extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003650 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003651
3652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * This routine releases a previously allocated memory block back to its
3656 * memory pool.
3657 *
3658 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 *
3660 * @return N/A
3661 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003663
3664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003665 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003666 *
Andy Ross73cb9582017-05-09 10:42:39 -07003667 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 *
Andy Ross73cb9582017-05-09 10:42:39 -07003669 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003670 *
3671 * @return N/A
3672 */
Andy Ross73cb9582017-05-09 10:42:39 -07003673static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003674
3675/**
Allan Stephensc98da842016-11-11 15:45:03 -05003676 * @} end addtogroup mem_pool_apis
3677 */
3678
3679/**
3680 * @defgroup heap_apis Heap Memory Pool APIs
3681 * @ingroup kernel_apis
3682 * @{
3683 */
3684
3685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003689 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003694 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003695extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003696
3697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003699 *
3700 * This routine provides traditional free() semantics. The memory being
3701 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003702 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003703 * If @a ptr is NULL, no operation is performed.
3704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003706 *
3707 * @return N/A
3708 */
3709extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710
Allan Stephensc98da842016-11-11 15:45:03 -05003711/**
3712 * @} end defgroup heap_apis
3713 */
3714
Benjamin Walshacc68c12017-01-29 18:57:45 -05003715/* polling API - PRIVATE */
3716
Benjamin Walshb0179862017-02-02 16:39:57 -05003717#ifdef CONFIG_POLL
3718#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3719#else
3720#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3721#endif
3722
Benjamin Walshacc68c12017-01-29 18:57:45 -05003723/* private - implementation data created as needed, per-type */
3724struct _poller {
3725 struct k_thread *thread;
3726};
3727
3728/* private - types bit positions */
3729enum _poll_types_bits {
3730 /* can be used to ignore an event */
3731 _POLL_TYPE_IGNORE,
3732
3733 /* to be signaled by k_poll_signal() */
3734 _POLL_TYPE_SIGNAL,
3735
3736 /* semaphore availability */
3737 _POLL_TYPE_SEM_AVAILABLE,
3738
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003739 /* queue/fifo/lifo data availability */
3740 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003741
3742 _POLL_NUM_TYPES
3743};
3744
3745#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3746
3747/* private - states bit positions */
3748enum _poll_states_bits {
3749 /* default state when creating event */
3750 _POLL_STATE_NOT_READY,
3751
Benjamin Walshacc68c12017-01-29 18:57:45 -05003752 /* signaled by k_poll_signal() */
3753 _POLL_STATE_SIGNALED,
3754
3755 /* semaphore is available */
3756 _POLL_STATE_SEM_AVAILABLE,
3757
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003758 /* data is available to read on queue/fifo/lifo */
3759 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003760
3761 _POLL_NUM_STATES
3762};
3763
3764#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3765
3766#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003767 (32 - (0 \
3768 + 8 /* tag */ \
3769 + _POLL_NUM_TYPES \
3770 + _POLL_NUM_STATES \
3771 + 1 /* modes */ \
3772 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773
Benjamin Walshacc68c12017-01-29 18:57:45 -05003774/* end of polling API - PRIVATE */
3775
3776
3777/**
3778 * @defgroup poll_apis Async polling APIs
3779 * @ingroup kernel_apis
3780 * @{
3781 */
3782
3783/* Public polling API */
3784
3785/* public - values for k_poll_event.type bitfield */
3786#define K_POLL_TYPE_IGNORE 0
3787#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3788#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003789#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3790#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003791
3792/* public - polling modes */
3793enum k_poll_modes {
3794 /* polling thread does not take ownership of objects when available */
3795 K_POLL_MODE_NOTIFY_ONLY = 0,
3796
3797 K_POLL_NUM_MODES
3798};
3799
3800/* public - values for k_poll_event.state bitfield */
3801#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003802#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3803#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003804#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3805#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003806
3807/* public - poll signal object */
3808struct k_poll_signal {
3809 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003810 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811
3812 /*
3813 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3814 * user resets it to 0.
3815 */
3816 unsigned int signaled;
3817
3818 /* custom result value passed to k_poll_signal() if needed */
3819 int result;
3820};
3821
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003822#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003823 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003824 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003825 .signaled = 0, \
3826 .result = 0, \
3827 }
3828
3829struct k_poll_event {
3830 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003831 sys_dnode_t _node;
3832
3833 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834 struct _poller *poller;
3835
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003836 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003837 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003838
Benjamin Walshacc68c12017-01-29 18:57:45 -05003839 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003840 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003841
3842 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003843 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003844
3845 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003846 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847
3848 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003849 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003850
3851 /* per-type data */
3852 union {
3853 void *obj;
3854 struct k_poll_signal *signal;
3855 struct k_sem *sem;
3856 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003857 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858 };
3859};
3860
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003861#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003862 { \
3863 .poller = NULL, \
3864 .type = event_type, \
3865 .state = K_POLL_STATE_NOT_READY, \
3866 .mode = event_mode, \
3867 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003868 { .obj = event_obj }, \
3869 }
3870
3871#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3872 event_tag) \
3873 { \
3874 .type = event_type, \
3875 .tag = event_tag, \
3876 .state = K_POLL_STATE_NOT_READY, \
3877 .mode = event_mode, \
3878 .unused = 0, \
3879 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003880 }
3881
3882/**
3883 * @brief Initialize one struct k_poll_event instance
3884 *
3885 * After this routine is called on a poll event, the event it ready to be
3886 * placed in an event array to be passed to k_poll().
3887 *
3888 * @param event The event to initialize.
3889 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3890 * values. Only values that apply to the same object being polled
3891 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3892 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003893 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894 * @param obj Kernel object or poll signal.
3895 *
3896 * @return N/A
3897 */
3898
Kumar Galacc334c72017-04-21 10:55:34 -05003899extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003900 int mode, void *obj);
3901
3902/**
3903 * @brief Wait for one or many of multiple poll events to occur
3904 *
3905 * This routine allows a thread to wait concurrently for one or many of
3906 * multiple poll events to have occurred. Such events can be a kernel object
3907 * being available, like a semaphore, or a poll signal event.
3908 *
3909 * When an event notifies that a kernel object is available, the kernel object
3910 * is not "given" to the thread calling k_poll(): it merely signals the fact
3911 * that the object was available when the k_poll() call was in effect. Also,
3912 * all threads trying to acquire an object the regular way, i.e. by pending on
3913 * the object, have precedence over the thread polling on the object. This
3914 * means that the polling thread will never get the poll event on an object
3915 * until the object becomes available and its pend queue is empty. For this
3916 * reason, the k_poll() call is more effective when the objects being polled
3917 * only have one thread, the polling thread, trying to acquire them.
3918 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003919 * When k_poll() returns 0, the caller should loop on all the events that were
3920 * passed to k_poll() and check the state field for the values that were
3921 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003922 *
3923 * Before being reused for another call to k_poll(), the user has to reset the
3924 * state field to K_POLL_STATE_NOT_READY.
3925 *
3926 * @param events An array of pointers to events to be polled for.
3927 * @param num_events The number of events in the array.
3928 * @param timeout Waiting period for an event to be ready (in milliseconds),
3929 * or one of the special values K_NO_WAIT and K_FOREVER.
3930 *
3931 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003932 * @retval -EAGAIN Waiting period timed out.
3933 */
3934
3935extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003936 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003937
3938/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003939 * @brief Initialize a poll signal object.
3940 *
3941 * Ready a poll signal object to be signaled via k_poll_signal().
3942 *
3943 * @param signal A poll signal.
3944 *
3945 * @return N/A
3946 */
3947
3948extern void k_poll_signal_init(struct k_poll_signal *signal);
3949
3950/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003951 * @brief Signal a poll signal object.
3952 *
3953 * This routine makes ready a poll signal, which is basically a poll event of
3954 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3955 * made ready to run. A @a result value can be specified.
3956 *
3957 * The poll signal contains a 'signaled' field that, when set by
3958 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3959 * be reset by the user before being passed again to k_poll() or k_poll() will
3960 * consider it being signaled, and will return immediately.
3961 *
3962 * @param signal A poll signal.
3963 * @param result The value to store in the result field of the signal.
3964 *
3965 * @retval 0 The signal was delivered successfully.
3966 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3967 */
3968
3969extern int k_poll_signal(struct k_poll_signal *signal, int result);
3970
3971/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003972extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003973
3974/**
3975 * @} end defgroup poll_apis
3976 */
3977
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003978/**
3979 * @brief Make the CPU idle.
3980 *
3981 * This function makes the CPU idle until an event wakes it up.
3982 *
3983 * In a regular system, the idle thread should be the only thread responsible
3984 * for making the CPU idle and triggering any type of power management.
3985 * However, in some more constrained systems, such as a single-threaded system,
3986 * the only thread would be responsible for this if needed.
3987 *
3988 * @return N/A
3989 */
3990extern void k_cpu_idle(void);
3991
3992/**
3993 * @brief Make the CPU idle in an atomic fashion.
3994 *
3995 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3996 * must be done atomically before making the CPU idle.
3997 *
3998 * @param key Interrupt locking key obtained from irq_lock().
3999 *
4000 * @return N/A
4001 */
4002extern void k_cpu_atomic_idle(unsigned int key);
4003
Kumar Galacc334c72017-04-21 10:55:34 -05004004extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004005
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004006#include <arch/cpu.h>
4007
Andrew Boiecdb94d62017-04-18 15:22:05 -07004008#ifdef _ARCH_EXCEPT
4009/* This archtecture has direct support for triggering a CPU exception */
4010#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4011#else
4012
4013#include <misc/printk.h>
4014
4015/* NOTE: This is the implementation for arches that do not implement
4016 * _ARCH_EXCEPT() to generate a real CPU exception.
4017 *
4018 * We won't have a real exception frame to determine the PC value when
4019 * the oops occurred, so print file and line number before we jump into
4020 * the fatal error handler.
4021 */
4022#define _k_except_reason(reason) do { \
4023 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4024 _NanoFatalErrorHandler(reason, &_default_esf); \
4025 CODE_UNREACHABLE; \
4026 } while (0)
4027
4028#endif /* _ARCH__EXCEPT */
4029
4030/**
4031 * @brief Fatally terminate a thread
4032 *
4033 * This should be called when a thread has encountered an unrecoverable
4034 * runtime condition and needs to terminate. What this ultimately
4035 * means is determined by the _fatal_error_handler() implementation, which
4036 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4037 *
4038 * If this is called from ISR context, the default system fatal error handler
4039 * will treat it as an unrecoverable system error, just like k_panic().
4040 */
4041#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4042
4043/**
4044 * @brief Fatally terminate the system
4045 *
4046 * This should be called when the Zephyr kernel has encountered an
4047 * unrecoverable runtime condition and needs to terminate. What this ultimately
4048 * means is determined by the _fatal_error_handler() implementation, which
4049 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4050 */
4051#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4052
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004053/*
4054 * private APIs that are utilized by one or more public APIs
4055 */
4056
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004057#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004058extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004059#else
4060#define _init_static_threads() do { } while ((0))
4061#endif
4062
4063extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004064extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004065
Andrew Boiedc5d9352017-06-02 12:56:47 -07004066/* arch/cpu.h may declare an architecture or platform-specific macro
4067 * for properly declaring stacks, compatible with MMU/MPU constraints if
4068 * enabled
4069 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004070
4071/**
4072 * @brief Obtain an extern reference to a stack
4073 *
4074 * This macro properly brings the symbol of a thread stack declared
4075 * elsewhere into scope.
4076 *
4077 * @param sym Thread stack symbol name
4078 */
4079#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4080
Andrew Boiedc5d9352017-06-02 12:56:47 -07004081#ifdef _ARCH_THREAD_STACK_DEFINE
4082#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4083#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4084 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4085#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4086#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004087static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004088{
4089 return _ARCH_THREAD_STACK_BUFFER(sym);
4090}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004091#else
4092/**
4093 * @brief Declare a toplevel thread stack memory region
4094 *
4095 * This declares a region of memory suitable for use as a thread's stack.
4096 *
4097 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4098 * 'noinit' section so that it isn't zeroed at boot
4099 *
Andrew Boie507852a2017-07-25 18:47:07 -07004100 * The declared symbol will always be a k_thread_stack_t which can be passed to
4101 * k_thread_create, but should otherwise not be manipulated. If the buffer
4102 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004103 *
4104 * It is legal to precede this definition with the 'static' keyword.
4105 *
4106 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4107 * parameter of k_thread_create(), it may not be the same as the
4108 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4109 *
4110 * @param sym Thread stack symbol name
4111 * @param size Size of the stack memory region
4112 */
4113#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004114 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004115
4116/**
4117 * @brief Declare a toplevel array of thread stack memory regions
4118 *
4119 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4120 * definition for additional details and constraints.
4121 *
4122 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4123 * 'noinit' section so that it isn't zeroed at boot
4124 *
4125 * @param sym Thread stack symbol name
4126 * @param nmemb Number of stacks to declare
4127 * @param size Size of the stack memory region
4128 */
4129
4130#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004131 struct _k_thread_stack_element __noinit \
4132 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004133
4134/**
4135 * @brief Declare an embedded stack memory region
4136 *
4137 * Used for stacks embedded within other data structures. Use is highly
4138 * discouraged but in some cases necessary. For memory protection scenarios,
4139 * it is very important that any RAM preceding this member not be writable
4140 * by threads else a stack overflow will lead to silent corruption. In other
4141 * words, the containing data structure should live in RAM owned by the kernel.
4142 *
4143 * @param sym Thread stack symbol name
4144 * @param size Size of the stack memory region
4145 */
4146#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004147 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004148
4149/**
4150 * @brief Return the size in bytes of a stack memory region
4151 *
4152 * Convenience macro for passing the desired stack size to k_thread_create()
4153 * since the underlying implementation may actually create something larger
4154 * (for instance a guard area).
4155 *
4156 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004157 * passed to K_THREAD_STACK_DEFINE.
4158 *
4159 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4160 * it is not guaranteed to return the original value since each array
4161 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004162 *
4163 * @param sym Stack memory symbol
4164 * @return Size of the stack
4165 */
4166#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4167
4168/**
4169 * @brief Get a pointer to the physical stack buffer
4170 *
4171 * Convenience macro to get at the real underlying stack buffer used by
4172 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4173 * This is really only intended for diagnostic tools which want to examine
4174 * stack memory contents.
4175 *
4176 * @param sym Declared stack symbol name
4177 * @return The buffer itself, a char *
4178 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004179static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004180{
4181 return (char *)sym;
4182}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004183
4184#endif /* _ARCH_DECLARE_STACK */
4185
Chunlin Hane9c97022017-07-07 20:29:30 +08004186/**
4187 * @defgroup mem_domain_apis Memory domain APIs
4188 * @ingroup kernel_apis
4189 * @{
4190 */
4191
4192/** @def MEM_PARTITION_ENTRY
4193 * @brief Used to declare a memory partition entry
4194 */
4195#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4196 {\
4197 .start = _start, \
4198 .size = _size, \
4199 .attr = _attr, \
4200 }
4201
4202/** @def K_MEM_PARTITION_DEFINE
4203 * @brief Used to declare a memory partition
4204 */
4205#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4206#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4207 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4208 struct k_mem_partition name = \
4209 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4210#else
4211#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4212 struct k_mem_partition name = \
4213 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4214#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4215
4216
4217/* memory partition */
4218struct k_mem_partition {
4219 /* start address of memory partition */
4220 u32_t start;
4221 /* size of memory partition */
4222 u32_t size;
4223 /* attribute of memory partition */
4224 u32_t attr;
4225};
4226
4227#if defined(CONFIG_USERSPACE)
4228/* memory domian */
4229struct k_mem_domain {
4230 /* number of partitions in the domain */
4231 u32_t num_partitions;
4232 /* partitions in the domain */
4233 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4234 /* domain q */
4235 sys_dlist_t mem_domain_q;
4236};
4237#endif /* CONFIG_USERSPACE */
4238
4239/**
4240 * @brief Initialize a memory domain.
4241 *
4242 * Initialize a memory domain with given name and memory partitions.
4243 *
4244 * @param domain The memory domain to be initialized.
4245 * @param num_parts The number of array items of "parts" parameter.
4246 * @param parts An array of pointers to the memory partitions. Can be NULL
4247 * if num_parts is zero.
4248 */
4249
4250extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4251 struct k_mem_partition *parts[]);
4252/**
4253 * @brief Destroy a memory domain.
4254 *
4255 * Destroy a memory domain.
4256 *
4257 * @param domain The memory domain to be destroyed.
4258 */
4259
4260extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4261
4262/**
4263 * @brief Add a memory partition into a memory domain.
4264 *
4265 * Add a memory partition into a memory domain.
4266 *
4267 * @param domain The memory domain to be added a memory partition.
4268 * @param part The memory partition to be added
4269 */
4270
4271extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4272 struct k_mem_partition *part);
4273
4274/**
4275 * @brief Remove a memory partition from a memory domain.
4276 *
4277 * Remove a memory partition from a memory domain.
4278 *
4279 * @param domain The memory domain to be removed a memory partition.
4280 * @param part The memory partition to be removed
4281 */
4282
4283extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4284 struct k_mem_partition *part);
4285
4286/**
4287 * @brief Add a thread into a memory domain.
4288 *
4289 * Add a thread into a memory domain.
4290 *
4291 * @param domain The memory domain that the thread is going to be added into.
4292 * @param thread ID of thread going to be added into the memory domain.
4293 */
4294
4295extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4296 k_tid_t thread);
4297
4298/**
4299 * @brief Remove a thread from its memory domain.
4300 *
4301 * Remove a thread from its memory domain.
4302 *
4303 * @param thread ID of thread going to be removed from its memory domain.
4304 */
4305
4306extern void k_mem_domain_remove_thread(k_tid_t thread);
4307
4308/**
4309 * @} end defgroup mem_domain_apis
4310 */
4311
Andrew Boie756f9072017-10-10 16:01:49 -07004312/**
4313 * @brief Emit a character buffer to the console device
4314 *
4315 * @param c String of characters to print
4316 * @param n The length of the string
4317 */
4318__syscall void k_str_out(char *c, size_t n);
4319
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004320#ifdef __cplusplus
4321}
4322#endif
4323
Andrew Boiee004dec2016-11-07 09:01:19 -08004324#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4325/*
4326 * Define new and delete operators.
4327 * At this moment, the operators do nothing since objects are supposed
4328 * to be statically allocated.
4329 */
4330inline void operator delete(void *ptr)
4331{
4332 (void)ptr;
4333}
4334
4335inline void operator delete[](void *ptr)
4336{
4337 (void)ptr;
4338}
4339
4340inline void *operator new(size_t size)
4341{
4342 (void)size;
4343 return NULL;
4344}
4345
4346inline void *operator new[](size_t size)
4347{
4348 (void)size;
4349 return NULL;
4350}
4351
4352/* Placement versions of operator new and delete */
4353inline void operator delete(void *ptr1, void *ptr2)
4354{
4355 (void)ptr1;
4356 (void)ptr2;
4357}
4358
4359inline void operator delete[](void *ptr1, void *ptr2)
4360{
4361 (void)ptr1;
4362 (void)ptr2;
4363}
4364
4365inline void *operator new(size_t size, void *ptr)
4366{
4367 (void)size;
4368 return ptr;
4369}
4370
4371inline void *operator new[](size_t size, void *ptr)
4372{
4373 (void)size;
4374 return ptr;
4375}
4376
4377#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4378
Andrew Boiefa94ee72017-09-28 16:54:35 -07004379#include <syscalls/kernel.h>
4380
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004381#endif /* !_ASMLANGUAGE */
4382
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004383#endif /* _kernel__h_ */