blob: 97cf712c9b88820444ca21670bbc758a2b0e2a45 [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 Boie945af952017-08-22 13:15:23 -0700145
Andrew Boie5bd891d2017-09-27 12:59:28 -0700146 /* Driver subsystems */
147 K_OBJ_DRIVER_ADC,
148 K_OBJ_DRIVER_AIO_CMP,
149 K_OBJ_DRIVER_CLOCK_CONTROL,
150 K_OBJ_DRIVER_COUNTER,
151 K_OBJ_DRIVER_CRYPTO,
152 K_OBJ_DRIVER_DMA,
153 K_OBJ_DRIVER_ETH,
154 K_OBJ_DRIVER_FLASH,
155 K_OBJ_DRIVER_GPIO,
156 K_OBJ_DRIVER_I2C,
157 K_OBJ_DRIVER_I2S,
158 K_OBJ_DRIVER_IPM,
159 K_OBJ_DRIVER_PINMUX,
160 K_OBJ_DRIVER_PWM,
161 K_OBJ_DRIVER_RANDOM,
162 K_OBJ_DRIVER_RTC,
163 K_OBJ_DRIVER_SENSOR,
164 K_OBJ_DRIVER_SHARED_IRQ,
165 K_OBJ_DRIVER_SPI,
166 K_OBJ_DRIVER_UART,
167 K_OBJ_DRIVER_WDT,
168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700177 u8_t perms[CONFIG_MAX_THREAD_BYTES];
178 u8_t type;
179 u8_t flags;
Andrew Boie945af952017-08-22 13:15:23 -0700180} __packed;
181
182#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie945af952017-08-22 13:15:23 -0700183
184/**
185 * Lookup a kernel object and init its metadata if it exists
186 *
187 * Calling this on an object will make it usable from userspace.
188 * Intended to be called as the last statement in kernel object init
189 * functions.
190 *
191 * @param object Address of the kernel object
192 */
193void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700194#else
Andrew Boie743e4682017-10-04 12:25:50 -0700195static inline void _k_object_init(void *obj)
196{
197 ARG_UNUSED(obj);
198}
199
200static inline void _impl_k_object_access_grant(void *object,
201 struct k_thread *thread)
202{
203 ARG_UNUSED(object);
204 ARG_UNUSED(thread);
205}
206
Andrew Boiea89bf012017-10-09 14:47:55 -0700207static inline void _impl_k_object_access_revoke(void *object,
208 struct k_thread *thread)
209{
210 ARG_UNUSED(object);
211 ARG_UNUSED(thread);
212}
213
Andrew Boie743e4682017-10-04 12:25:50 -0700214static inline void _impl_k_object_access_all_grant(void *object)
215{
216 ARG_UNUSED(object);
217}
218#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700219
220/**
221 * grant a thread access to a kernel object
222 *
223 * The thread will be granted access to the object if the caller is from
224 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700225 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700226 *
227 * @param object Address of kernel object
228 * @param thread Thread to grant access to the object
229 */
Andrew Boie743e4682017-10-04 12:25:50 -0700230__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700231
Andrew Boiea89bf012017-10-09 14:47:55 -0700232/**
233 * grant a thread access to a kernel object
234 *
235 * The thread will lose access to the object if the caller is from
236 * supervisor mode, or the caller is from user mode AND has permissions
237 * on both the object and the thread whose access is being revoked.
238 *
239 * @param object Address of kernel object
240 * @param thread Thread to remove access to the object
241 */
242__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700243
244/**
245 * grant all present and future threads access to an object
246 *
247 * If the caller is from supervisor mode, or the caller is from user mode and
248 * have sufficient permissions on the object, then that object will have
249 * permissions granted to it for *all* current and future threads running in
250 * the system, effectively becoming a public kernel object.
251 *
252 * Use of this API should be avoided on systems that are running untrusted code
253 * as it is possible for such code to derive the addresses of kernel objects
254 * and perform unwanted operations on them.
255 *
256 * @param object Address of kernel object
257 */
Andrew Boie743e4682017-10-04 12:25:50 -0700258__syscall void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700259
Andrew Boie73abd322017-04-04 13:19:13 -0700260/* timeouts */
261
262struct _timeout;
263typedef void (*_timeout_func_t)(struct _timeout *t);
264
265struct _timeout {
266 sys_dnode_t node;
267 struct k_thread *thread;
268 sys_dlist_t *wait_q;
269 s32_t delta_ticks_from_prev;
270 _timeout_func_t func;
271};
272
273extern s32_t _timeout_remaining_get(struct _timeout *timeout);
274
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700275/**
276 * @typedef k_thread_entry_t
277 * @brief Thread entry point function type.
278 *
279 * A thread's entry point function is invoked when the thread starts executing.
280 * Up to 3 argument values can be passed to the function.
281 *
282 * The thread terminates execution permanently if the entry point function
283 * returns. The thread is responsible for releasing any shared resources
284 * it may own (such as mutexes and dynamically allocated memory), prior to
285 * returning.
286 *
287 * @param p1 First argument.
288 * @param p2 Second argument.
289 * @param p3 Third argument.
290 *
291 * @return N/A
292 */
293typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700294
295#ifdef CONFIG_THREAD_MONITOR
296struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700297 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700298 void *parameter1;
299 void *parameter2;
300 void *parameter3;
301};
302#endif
303
304/* can be used for creating 'dummy' threads, e.g. for pending on objects */
305struct _thread_base {
306
307 /* this thread's entry in a ready/wait queue */
308 sys_dnode_t k_q_node;
309
310 /* user facing 'thread options'; values defined in include/kernel.h */
311 u8_t user_options;
312
313 /* thread state */
314 u8_t thread_state;
315
316 /*
317 * scheduler lock count and thread priority
318 *
319 * These two fields control the preemptibility of a thread.
320 *
321 * When the scheduler is locked, sched_locked is decremented, which
322 * means that the scheduler is locked for values from 0xff to 0x01. A
323 * thread is coop if its prio is negative, thus 0x80 to 0xff when
324 * looked at the value as unsigned.
325 *
326 * By putting them end-to-end, this means that a thread is
327 * non-preemptible if the bundled value is greater than or equal to
328 * 0x0080.
329 */
330 union {
331 struct {
332#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
333 u8_t sched_locked;
334 s8_t prio;
335#else /* LITTLE and PDP */
336 s8_t prio;
337 u8_t sched_locked;
338#endif
339 };
340 u16_t preempt;
341 };
342
343 /* data returned by APIs */
344 void *swap_data;
345
346#ifdef CONFIG_SYS_CLOCK_EXISTS
347 /* this thread's entry in a timeout queue */
348 struct _timeout timeout;
349#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700350
351#ifdef CONFIG_USERSPACE
352 /* Bit position in kernel object permissions bitfield for this thread */
353 unsigned int perm_index;
354#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700355};
356
357typedef struct _thread_base _thread_base_t;
358
359#if defined(CONFIG_THREAD_STACK_INFO)
360/* Contains the stack information of a thread */
361struct _thread_stack_info {
362 /* Stack Start */
363 u32_t start;
364 /* Stack Size */
365 u32_t size;
366};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700367
368typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700369#endif /* CONFIG_THREAD_STACK_INFO */
370
Chunlin Hane9c97022017-07-07 20:29:30 +0800371#if defined(CONFIG_USERSPACE)
372struct _mem_domain_info {
373 /* memory domain queue node */
374 sys_dnode_t mem_domain_q_node;
375 /* memory domain of the thread */
376 struct k_mem_domain *mem_domain;
377};
378
379#endif /* CONFIG_USERSPACE */
380
Andrew Boie73abd322017-04-04 13:19:13 -0700381struct k_thread {
382
383 struct _thread_base base;
384
385 /* defined by the architecture, but all archs need these */
386 struct _caller_saved caller_saved;
387 struct _callee_saved callee_saved;
388
389 /* static thread init data */
390 void *init_data;
391
392 /* abort function */
393 void (*fn_abort)(void);
394
395#if defined(CONFIG_THREAD_MONITOR)
396 /* thread entry and parameters description */
397 struct __thread_entry *entry;
398
399 /* next item in list of all threads */
400 struct k_thread *next_thread;
401#endif
402
403#ifdef CONFIG_THREAD_CUSTOM_DATA
404 /* crude thread-local storage */
405 void *custom_data;
406#endif
407
408#ifdef CONFIG_ERRNO
409 /* per-thread errno variable */
410 int errno_var;
411#endif
412
413#if defined(CONFIG_THREAD_STACK_INFO)
414 /* Stack Info */
415 struct _thread_stack_info stack_info;
416#endif /* CONFIG_THREAD_STACK_INFO */
417
Chunlin Hane9c97022017-07-07 20:29:30 +0800418#if defined(CONFIG_USERSPACE)
419 /* memory domain info of the thread */
420 struct _mem_domain_info mem_domain_info;
421#endif /* CONFIG_USERSPACE */
422
Andrew Boie73abd322017-04-04 13:19:13 -0700423 /* arch-specifics: must always be at the end */
424 struct _thread_arch arch;
425};
426
427typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400428typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700429#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400430
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400431enum execution_context_types {
432 K_ISR = 0,
433 K_COOP_THREAD,
434 K_PREEMPT_THREAD,
435};
436
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100438 * @defgroup profiling_apis Profiling APIs
439 * @ingroup kernel_apis
440 * @{
441 */
442
443/**
444 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
445 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700446 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100447 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
448 *
449 * CONFIG_MAIN_STACK_SIZE
450 * CONFIG_IDLE_STACK_SIZE
451 * CONFIG_ISR_STACK_SIZE
452 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
453 *
454 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
455 * produce output.
456 *
457 * @return N/A
458 */
459extern void k_call_stacks_analyze(void);
460
461/**
462 * @} end defgroup profiling_apis
463 */
464
465/**
Allan Stephensc98da842016-11-11 15:45:03 -0500466 * @defgroup thread_apis Thread APIs
467 * @ingroup kernel_apis
468 * @{
469 */
470
Benjamin Walshed240f22017-01-22 13:05:08 -0500471#endif /* !_ASMLANGUAGE */
472
473
474/*
475 * Thread user options. May be needed by assembly code. Common part uses low
476 * bits, arch-specific use high bits.
477 */
478
479/* system thread that must not abort */
480#define K_ESSENTIAL (1 << 0)
481
482#if defined(CONFIG_FP_SHARING)
483/* thread uses floating point registers */
484#define K_FP_REGS (1 << 1)
485#endif
486
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700487/* This thread has dropped from supervisor mode to user mode and consequently
488 * has additional restrictions
489 */
490#define K_USER (1 << 2)
491
Andrew Boie47f8fd12017-10-05 11:11:02 -0700492/* Indicates that the thread being created should inherit all kernel object
493 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
494 * is not enabled.
495 */
496#define K_INHERIT_PERMS (1 << 3)
497
Benjamin Walshed240f22017-01-22 13:05:08 -0500498#ifdef CONFIG_X86
499/* x86 Bitmask definitions for threads user options */
500
501#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
502/* thread uses SSEx (and also FP) registers */
503#define K_SSE_REGS (1 << 7)
504#endif
505#endif
506
507/* end - thread options */
508
509#if !defined(_ASMLANGUAGE)
510
Andrew Boie507852a2017-07-25 18:47:07 -0700511/* Using typedef deliberately here, this is quite intended to be an opaque
512 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
513 *
514 * The purpose of this data type is to clearly distinguish between the
515 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
516 * buffer which composes the stack data actually used by the underlying
517 * thread; they cannot be used interchangably as some arches precede the
518 * stack buffer region with guard areas that trigger a MPU or MMU fault
519 * if written to.
520 *
521 * APIs that want to work with the buffer inside should continue to use
522 * char *.
523 *
524 * Stacks should always be created with K_THREAD_STACK_DEFINE().
525 */
526struct __packed _k_thread_stack_element {
527 char data;
528};
529typedef struct _k_thread_stack_element *k_thread_stack_t;
530
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400531
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700533 * @brief Create a thread.
534 *
535 * This routine initializes a thread, then schedules it for execution.
536 *
537 * The new thread may be scheduled for immediate execution or a delayed start.
538 * If the newly spawned thread does not have a delayed start the kernel
539 * scheduler may preempt the current thread to allow the new thread to
540 * execute.
541 *
542 * Thread options are architecture-specific, and can include K_ESSENTIAL,
543 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
544 * them using "|" (the logical OR operator).
545 *
546 * Historically, users often would use the beginning of the stack memory region
547 * to store the struct k_thread data, although corruption will occur if the
548 * stack overflows this region and stack protection features may not detect this
549 * situation.
550 *
551 * @param new_thread Pointer to uninitialized struct k_thread
552 * @param stack Pointer to the stack space.
553 * @param stack_size Stack size in bytes.
554 * @param entry Thread entry function.
555 * @param p1 1st entry point parameter.
556 * @param p2 2nd entry point parameter.
557 * @param p3 3rd entry point parameter.
558 * @param prio Thread priority.
559 * @param options Thread options.
560 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
561 *
562 * @return ID of new thread.
563 */
Andrew Boie507852a2017-07-25 18:47:07 -0700564extern k_tid_t k_thread_create(struct k_thread *new_thread,
565 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700566 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700567 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700568 void *p1, void *p2, void *p3,
569 int prio, u32_t options, s32_t delay);
570
Andrew Boie3f091b52017-08-30 14:34:14 -0700571/**
572 * @brief Drop a thread's privileges permanently to user mode
573 *
574 * @param entry Function to start executing from
575 * @param p1 1st entry point parameter
576 * @param p2 2nd entry point parameter
577 * @param p3 3rd entry point parameter
578 */
579extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
580 void *p1, void *p2,
581 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700582
Andrew Boied26cf2d2017-03-30 13:07:02 -0700583/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
Allan Stephensc98da842016-11-11 15:45:03 -0500586 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500587 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500589 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
591 * @return N/A
592 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700593__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594
595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 *
598 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500599 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 * @return N/A
602 */
Kumar Galacc334c72017-04-21 10:55:34 -0500603extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604
605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500608 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500610 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400611 *
612 * @return N/A
613 */
Andrew Boie468190a2017-09-29 14:00:48 -0700614__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615
616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500617 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500619 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500621 * If @a thread is not currently sleeping, the routine has no effect.
622 *
623 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
625 * @return N/A
626 */
Andrew Boie468190a2017-09-29 14:00:48 -0700627__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628
629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500630 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500632 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700634__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400635
636/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500637 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500639 * This routine prevents @a thread from executing if it has not yet started
640 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * @param thread ID of thread to cancel.
643 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700644 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500645 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 */
Andrew Boie468190a2017-09-29 14:00:48 -0700647__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400648
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400649/**
Allan Stephensc98da842016-11-11 15:45:03 -0500650 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500652 * This routine permanently stops execution of @a thread. The thread is taken
653 * off all kernel queues it is part of (i.e. the ready queue, the timeout
654 * queue, or a kernel object wait queue). However, any kernel resources the
655 * thread might currently own (such as mutexes or memory blocks) are not
656 * released. It is the responsibility of the caller of this routine to ensure
657 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500659 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 *
661 * @return N/A
662 */
Andrew Boie468190a2017-09-29 14:00:48 -0700663__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400664
Andrew Boie7d627c52017-08-30 11:01:56 -0700665
666/**
667 * @brief Start an inactive thread
668 *
669 * If a thread was created with K_FOREVER in the delay parameter, it will
670 * not be added to the scheduling queue until this function is called
671 * on it.
672 *
673 * @param thread thread to start
674 */
Andrew Boie468190a2017-09-29 14:00:48 -0700675__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700676
Allan Stephensc98da842016-11-11 15:45:03 -0500677/**
678 * @cond INTERNAL_HIDDEN
679 */
680
Benjamin Walshd211a522016-12-06 11:44:01 -0500681/* timeout has timed out and is not on _timeout_q anymore */
682#define _EXPIRED (-2)
683
684/* timeout is not in use */
685#define _INACTIVE (-1)
686
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400687struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700688 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700689 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400690 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700691 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500692 void *init_p1;
693 void *init_p2;
694 void *init_p3;
695 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500696 u32_t init_options;
697 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500698 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500699 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400700};
701
Andrew Boied26cf2d2017-03-30 13:07:02 -0700702#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400703 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500704 prio, options, delay, abort, groups) \
705 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700706 .init_thread = (thread), \
707 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500708 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700709 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400710 .init_p1 = (void *)p1, \
711 .init_p2 = (void *)p2, \
712 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500713 .init_prio = (prio), \
714 .init_options = (options), \
715 .init_delay = (delay), \
716 .init_abort = (abort), \
717 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400718 }
719
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400720/**
Allan Stephensc98da842016-11-11 15:45:03 -0500721 * INTERNAL_HIDDEN @endcond
722 */
723
724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * @brief Statically define and initialize a thread.
726 *
727 * The thread may be scheduled for immediate execution or a delayed start.
728 *
729 * Thread options are architecture-specific, and can include K_ESSENTIAL,
730 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
731 * them using "|" (the logical OR operator).
732 *
733 * The ID of the thread can be accessed using:
734 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500735 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500736 *
737 * @param name Name of the thread.
738 * @param stack_size Stack size in bytes.
739 * @param entry Thread entry function.
740 * @param p1 1st entry point parameter.
741 * @param p2 2nd entry point parameter.
742 * @param p3 3rd entry point parameter.
743 * @param prio Thread priority.
744 * @param options Thread options.
745 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400746 *
747 * @internal It has been observed that the x86 compiler by default aligns
748 * these _static_thread_data structures to 32-byte boundaries, thereby
749 * wasting space. To work around this, force a 4-byte alignment.
750 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500751#define K_THREAD_DEFINE(name, stack_size, \
752 entry, p1, p2, p3, \
753 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700754 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700755 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500756 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500757 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700758 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
759 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500760 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700761 NULL, 0); \
762 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500767 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500769 * @param thread ID of thread whose priority is needed.
770 *
771 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700773__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774
775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779 *
780 * Rescheduling can occur immediately depending on the priority @a thread is
781 * set to:
782 *
783 * - If its priority is raised above the priority of the caller of this
784 * function, and the caller is preemptible, @a thread will be scheduled in.
785 *
786 * - If the caller operates on itself, it lowers its priority below that of
787 * other threads in the system, and the caller is preemptible, the thread of
788 * highest priority will be scheduled in.
789 *
790 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
791 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
792 * highest priority.
793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 * @param prio New priority.
796 *
797 * @warning Changing the priority of a thread currently involved in mutex
798 * priority inheritance may result in undefined behavior.
799 *
800 * @return N/A
801 */
Andrew Boie468190a2017-09-29 14:00:48 -0700802__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400803
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * This routine prevents the kernel scheduler from making @a thread the
808 * current thread. All other internal operations on @a thread are still
809 * performed; for example, any timeout it is waiting on keeps ticking,
810 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * If @a thread is already suspended, the routine has no effect.
813 *
814 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
816 * @return N/A
817 */
Andrew Boie468190a2017-09-29 14:00:48 -0700818__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819
820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * This routine allows the kernel scheduler to make @a thread the current
824 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * If @a thread is not currently suspended, the routine has no effect.
827 *
828 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
830 * @return N/A
831 */
Andrew Boie468190a2017-09-29 14:00:48 -0700832__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400833
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * This routine specifies how the scheduler will perform time slicing of
838 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * To enable time slicing, @a slice must be non-zero. The scheduler
841 * ensures that no thread runs for more than the specified time limit
842 * before other threads of that priority are given a chance to execute.
843 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700844 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 * execute. Once the scheduler selects a thread for execution, there is no
848 * minimum guaranteed time the thread will execute before threads of greater or
849 * equal priority are scheduled.
850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 * for execution, this routine has no effect; the thread is immediately
853 * rescheduled after the slice period expires.
854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * To disable timeslicing, set both @a slice and @a prio to zero.
856 *
857 * @param slice Maximum time slice length (in milliseconds).
858 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
860 * @return N/A
861 */
Kumar Galacc334c72017-04-21 10:55:34 -0500862extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400863
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864/**
Allan Stephensc98da842016-11-11 15:45:03 -0500865 * @} end defgroup thread_apis
866 */
867
868/**
869 * @addtogroup isr_apis
870 * @{
871 */
872
873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephensc98da842016-11-11 15:45:03 -0500876 * This routine allows the caller to customize its actions, depending on
877 * whether it is a thread or an ISR.
878 *
879 * @note Can be called by ISRs.
880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @return 0 if invoked by a thread.
882 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500884extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400885
Benjamin Walsh445830d2016-11-10 15:54:27 -0500886/**
887 * @brief Determine if code is running in a preemptible thread.
888 *
Allan Stephensc98da842016-11-11 15:45:03 -0500889 * This routine allows the caller to customize its actions, depending on
890 * whether it can be preempted by another thread. The routine returns a 'true'
891 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500892 *
Allan Stephensc98da842016-11-11 15:45:03 -0500893 * - The code is running in a thread, not at ISR.
894 * - The thread's priority is in the preemptible range.
895 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500896 *
Allan Stephensc98da842016-11-11 15:45:03 -0500897 * @note Can be called by ISRs.
898 *
899 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500900 * @return Non-zero if invoked by a preemptible thread.
901 */
Andrew Boie468190a2017-09-29 14:00:48 -0700902__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500903
Allan Stephensc98da842016-11-11 15:45:03 -0500904/**
905 * @} end addtogroup isr_apis
906 */
907
908/**
909 * @addtogroup thread_apis
910 * @{
911 */
912
913/**
914 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500915 *
Allan Stephensc98da842016-11-11 15:45:03 -0500916 * This routine prevents the current thread from being preempted by another
917 * thread by instructing the scheduler to treat it as a cooperative thread.
918 * If the thread subsequently performs an operation that makes it unready,
919 * it will be context switched out in the normal manner. When the thread
920 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500921 *
Allan Stephensc98da842016-11-11 15:45:03 -0500922 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500923 *
Allan Stephensc98da842016-11-11 15:45:03 -0500924 * @note k_sched_lock() and k_sched_unlock() should normally be used
925 * when the operation being performed can be safely interrupted by ISRs.
926 * However, if the amount of processing involved is very small, better
927 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500928 *
929 * @return N/A
930 */
931extern void k_sched_lock(void);
932
Allan Stephensc98da842016-11-11 15:45:03 -0500933/**
934 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500935 *
Allan Stephensc98da842016-11-11 15:45:03 -0500936 * This routine reverses the effect of a previous call to k_sched_lock().
937 * A thread must call the routine once for each time it called k_sched_lock()
938 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500939 *
940 * @return N/A
941 */
942extern void k_sched_unlock(void);
943
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500945 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500947 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 * Custom data is not used by the kernel itself, and is freely available
950 * for a thread to use as it sees fit. It can be used as a framework
951 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954 *
955 * @return N/A
956 */
Andrew Boie468190a2017-09-29 14:00:48 -0700957__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400958
959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 */
Andrew Boie468190a2017-09-29 14:00:48 -0700966__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400967
968/**
Allan Stephensc98da842016-11-11 15:45:03 -0500969 * @} end addtogroup thread_apis
970 */
971
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400972#include <sys_clock.h>
973
Allan Stephensc2f15a42016-11-17 12:24:22 -0500974/**
975 * @addtogroup clock_apis
976 * @{
977 */
978
979/**
980 * @brief Generate null timeout delay.
981 *
982 * This macro generates a timeout delay that that instructs a kernel API
983 * not to wait if the requested operation cannot be performed immediately.
984 *
985 * @return Timeout delay value.
986 */
987#define K_NO_WAIT 0
988
989/**
990 * @brief Generate timeout delay from milliseconds.
991 *
992 * This macro generates a timeout delay that that instructs a kernel API
993 * to wait up to @a ms milliseconds to perform the requested operation.
994 *
995 * @param ms Duration in milliseconds.
996 *
997 * @return Timeout delay value.
998 */
Johan Hedberg14471692016-11-13 10:52:15 +0200999#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001000
1001/**
1002 * @brief Generate timeout delay from seconds.
1003 *
1004 * This macro generates a timeout delay that that instructs a kernel API
1005 * to wait up to @a s seconds to perform the requested operation.
1006 *
1007 * @param s Duration in seconds.
1008 *
1009 * @return Timeout delay value.
1010 */
Johan Hedberg14471692016-11-13 10:52:15 +02001011#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001012
1013/**
1014 * @brief Generate timeout delay from minutes.
1015 *
1016 * This macro generates a timeout delay that that instructs a kernel API
1017 * to wait up to @a m minutes to perform the requested operation.
1018 *
1019 * @param m Duration in minutes.
1020 *
1021 * @return Timeout delay value.
1022 */
Johan Hedberg14471692016-11-13 10:52:15 +02001023#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001024
1025/**
1026 * @brief Generate timeout delay from hours.
1027 *
1028 * This macro generates a timeout delay that that instructs a kernel API
1029 * to wait up to @a h hours to perform the requested operation.
1030 *
1031 * @param h Duration in hours.
1032 *
1033 * @return Timeout delay value.
1034 */
Johan Hedberg14471692016-11-13 10:52:15 +02001035#define K_HOURS(h) K_MINUTES((h) * 60)
1036
Allan Stephensc98da842016-11-11 15:45:03 -05001037/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001038 * @brief Generate infinite timeout delay.
1039 *
1040 * This macro generates a timeout delay that that instructs a kernel API
1041 * to wait as long as necessary to perform the requested operation.
1042 *
1043 * @return Timeout delay value.
1044 */
1045#define K_FOREVER (-1)
1046
1047/**
1048 * @} end addtogroup clock_apis
1049 */
1050
1051/**
Allan Stephensc98da842016-11-11 15:45:03 -05001052 * @cond INTERNAL_HIDDEN
1053 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001054
Benjamin Walsh62092182016-12-20 14:39:08 -05001055/* kernel clocks */
1056
1057#if (sys_clock_ticks_per_sec == 1000) || \
1058 (sys_clock_ticks_per_sec == 500) || \
1059 (sys_clock_ticks_per_sec == 250) || \
1060 (sys_clock_ticks_per_sec == 125) || \
1061 (sys_clock_ticks_per_sec == 100) || \
1062 (sys_clock_ticks_per_sec == 50) || \
1063 (sys_clock_ticks_per_sec == 25) || \
1064 (sys_clock_ticks_per_sec == 20) || \
1065 (sys_clock_ticks_per_sec == 10) || \
1066 (sys_clock_ticks_per_sec == 1)
1067
1068 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1069#else
1070 /* yields horrible 64-bit math on many architectures: try to avoid */
1071 #define _NON_OPTIMIZED_TICKS_PER_SEC
1072#endif
1073
1074#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001075extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001076#else
Kumar Galacc334c72017-04-21 10:55:34 -05001077static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001078{
Kumar Galacc334c72017-04-21 10:55:34 -05001079 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001080}
1081#endif
1082
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001083/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001084#ifdef CONFIG_TICKLESS_KERNEL
1085#define _TICK_ALIGN 0
1086#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001087#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001088#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001089
Kumar Galacc334c72017-04-21 10:55:34 -05001090static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001091{
Benjamin Walsh62092182016-12-20 14:39:08 -05001092#ifdef CONFIG_SYS_CLOCK_EXISTS
1093
1094#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001095 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001096#else
Kumar Galacc334c72017-04-21 10:55:34 -05001097 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001098#endif
1099
1100#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001101 __ASSERT(ticks == 0, "");
1102 return 0;
1103#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001104}
1105
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106struct k_timer {
1107 /*
1108 * _timeout structure must be first here if we want to use
1109 * dynamic timer allocation. timeout.node is used in the double-linked
1110 * list of free timers
1111 */
1112 struct _timeout timeout;
1113
Allan Stephens45bfa372016-10-12 12:39:42 -05001114 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001115 _wait_q_t wait_q;
1116
1117 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001118 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119
1120 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001121 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001122
1123 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001124 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001125
Allan Stephens45bfa372016-10-12 12:39:42 -05001126 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001127 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001129 /* user-specific data, also used to support legacy features */
1130 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131
Anas Nashif2f203c22016-12-18 06:57:45 -05001132 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001133};
1134
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001135#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001136 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001137 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001138 .timeout.wait_q = NULL, \
1139 .timeout.thread = NULL, \
1140 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001142 .expiry_fn = expiry, \
1143 .stop_fn = stop, \
1144 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001145 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001146 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147 }
1148
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001149#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1150
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151/**
Allan Stephensc98da842016-11-11 15:45:03 -05001152 * INTERNAL_HIDDEN @endcond
1153 */
1154
1155/**
1156 * @defgroup timer_apis Timer APIs
1157 * @ingroup kernel_apis
1158 * @{
1159 */
1160
1161/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001162 * @typedef k_timer_expiry_t
1163 * @brief Timer expiry function type.
1164 *
1165 * A timer's expiry function is executed by the system clock interrupt handler
1166 * each time the timer expires. The expiry function is optional, and is only
1167 * invoked if the timer has been initialized with one.
1168 *
1169 * @param timer Address of timer.
1170 *
1171 * @return N/A
1172 */
1173typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1174
1175/**
1176 * @typedef k_timer_stop_t
1177 * @brief Timer stop function type.
1178 *
1179 * A timer's stop function is executed if the timer is stopped prematurely.
1180 * The function runs in the context of the thread that stops the timer.
1181 * The stop function is optional, and is only invoked if the timer has been
1182 * initialized with one.
1183 *
1184 * @param timer Address of timer.
1185 *
1186 * @return N/A
1187 */
1188typedef void (*k_timer_stop_t)(struct k_timer *timer);
1189
1190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001191 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001195 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
1197 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001198 * @param expiry_fn Function to invoke each time the timer expires.
1199 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001201#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001202 struct k_timer name \
1203 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001204 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205
Allan Stephens45bfa372016-10-12 12:39:42 -05001206/**
1207 * @brief Initialize a timer.
1208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001209 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001210 *
1211 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001212 * @param expiry_fn Function to invoke each time the timer expires.
1213 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001214 *
1215 * @return N/A
1216 */
1217extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001218 k_timer_expiry_t expiry_fn,
1219 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001220
Allan Stephens45bfa372016-10-12 12:39:42 -05001221/**
1222 * @brief Start a timer.
1223 *
1224 * This routine starts a timer, and resets its status to zero. The timer
1225 * begins counting down using the specified duration and period values.
1226 *
1227 * Attempting to start a timer that is already running is permitted.
1228 * The timer's status is reset to zero and the timer begins counting down
1229 * using the new duration and period values.
1230 *
1231 * @param timer Address of timer.
1232 * @param duration Initial timer duration (in milliseconds).
1233 * @param period Timer period (in milliseconds).
1234 *
1235 * @return N/A
1236 */
Andrew Boiea354d492017-09-29 16:22:28 -07001237__syscall void k_timer_start(struct k_timer *timer,
1238 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001239
1240/**
1241 * @brief Stop a timer.
1242 *
1243 * This routine stops a running timer prematurely. The timer's stop function,
1244 * if one exists, is invoked by the caller.
1245 *
1246 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001248 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001249 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1250 * if @a k_timer_stop is to be called from ISRs.
1251 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001252 * @param timer Address of timer.
1253 *
1254 * @return N/A
1255 */
Andrew Boiea354d492017-09-29 16:22:28 -07001256__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001257
1258/**
1259 * @brief Read timer status.
1260 *
1261 * This routine reads the timer's status, which indicates the number of times
1262 * it has expired since its status was last read.
1263 *
1264 * Calling this routine resets the timer's status to zero.
1265 *
1266 * @param timer Address of timer.
1267 *
1268 * @return Timer status.
1269 */
Andrew Boiea354d492017-09-29 16:22:28 -07001270__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001271
1272/**
1273 * @brief Synchronize thread to timer expiration.
1274 *
1275 * This routine blocks the calling thread until the timer's status is non-zero
1276 * (indicating that it has expired at least once since it was last examined)
1277 * or the timer is stopped. If the timer status is already non-zero,
1278 * or the timer is already stopped, the caller continues without waiting.
1279 *
1280 * Calling this routine resets the timer's status to zero.
1281 *
1282 * This routine must not be used by interrupt handlers, since they are not
1283 * allowed to block.
1284 *
1285 * @param timer Address of timer.
1286 *
1287 * @return Timer status.
1288 */
Andrew Boiea354d492017-09-29 16:22:28 -07001289__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001290
1291/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001293 *
1294 * This routine computes the (approximate) time remaining before a running
1295 * timer next expires. If the timer is not running, it returns zero.
1296 *
1297 * @param timer Address of timer.
1298 *
1299 * @return Remaining time (in milliseconds).
1300 */
Andrew Boiea354d492017-09-29 16:22:28 -07001301__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1302
1303static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001304{
1305 return _timeout_remaining_get(&timer->timeout);
1306}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001307
Allan Stephensc98da842016-11-11 15:45:03 -05001308/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001309 * @brief Associate user-specific data with a timer.
1310 *
1311 * This routine records the @a user_data with the @a timer, to be retrieved
1312 * later.
1313 *
1314 * It can be used e.g. in a timer handler shared across multiple subsystems to
1315 * retrieve data specific to the subsystem this timer is associated with.
1316 *
1317 * @param timer Address of timer.
1318 * @param user_data User data to associate with the timer.
1319 *
1320 * @return N/A
1321 */
Andrew Boiea354d492017-09-29 16:22:28 -07001322__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1323
1324static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1325 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001326{
1327 timer->user_data = user_data;
1328}
1329
1330/**
1331 * @brief Retrieve the user-specific data from a timer.
1332 *
1333 * @param timer Address of timer.
1334 *
1335 * @return The user data.
1336 */
Andrew Boiea354d492017-09-29 16:22:28 -07001337__syscall void *k_timer_user_data_get(struct k_timer *timer);
1338
1339static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001340{
1341 return timer->user_data;
1342}
1343
1344/**
Allan Stephensc98da842016-11-11 15:45:03 -05001345 * @} end defgroup timer_apis
1346 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001347
Allan Stephensc98da842016-11-11 15:45:03 -05001348/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001349 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001350 * @{
1351 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001352
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001353/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * This routine returns the elapsed time since the system booted,
1357 * in milliseconds.
1358 *
1359 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001360 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001361__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001362
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001363#ifdef CONFIG_TICKLESS_KERNEL
1364/**
1365 * @brief Enable clock always on in tickless kernel
1366 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001367 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001368 * there are no timer events programmed in tickless kernel
1369 * scheduling. This is necessary if the clock is used to track
1370 * passage of time.
1371 *
1372 * @retval prev_status Previous status of always on flag
1373 */
1374static inline int k_enable_sys_clock_always_on(void)
1375{
1376 int prev_status = _sys_clock_always_on;
1377
1378 _sys_clock_always_on = 1;
1379 _enable_sys_clock();
1380
1381 return prev_status;
1382}
1383
1384/**
1385 * @brief Disable clock always on in tickless kernel
1386 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001387 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001388 * there are no timer events programmed in tickless kernel
1389 * scheduling. To save power, this routine should be called
1390 * immediately when clock is not used to track time.
1391 */
1392static inline void k_disable_sys_clock_always_on(void)
1393{
1394 _sys_clock_always_on = 0;
1395}
1396#else
1397#define k_enable_sys_clock_always_on() do { } while ((0))
1398#define k_disable_sys_clock_always_on() do { } while ((0))
1399#endif
1400
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001402 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001404 * This routine returns the lower 32-bits of the elapsed time since the system
1405 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * This routine can be more efficient than k_uptime_get(), as it reduces the
1408 * need for interrupt locking and 64-bit math. However, the 32-bit result
1409 * cannot hold a system uptime time larger than approximately 50 days, so the
1410 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001413 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001414__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001415
1416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001417 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001419 * This routine computes the elapsed time between the current system uptime
1420 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001422 * @param reftime Pointer to a reference time, which is updated to the current
1423 * uptime upon return.
1424 *
1425 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001426 */
Kumar Galacc334c72017-04-21 10:55:34 -05001427extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001428
1429/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001430 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001432 * This routine computes the elapsed time between the current system uptime
1433 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001435 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1436 * need for interrupt locking and 64-bit math. However, the 32-bit result
1437 * cannot hold an elapsed time larger than approximately 50 days, so the
1438 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001440 * @param reftime Pointer to a reference time, which is updated to the current
1441 * uptime upon return.
1442 *
1443 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001444 */
Kumar Galacc334c72017-04-21 10:55:34 -05001445extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001446
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * This routine returns the current time, as measured by the system's hardware
1451 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001454 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001455#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001457/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001458 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 */
1460
Allan Stephensc98da842016-11-11 15:45:03 -05001461/**
1462 * @cond INTERNAL_HIDDEN
1463 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001465struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001466 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001467 union {
1468 _wait_q_t wait_q;
1469
1470 _POLL_EVENT;
1471 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001472
1473 _OBJECT_TRACING_NEXT_PTR(k_queue);
1474};
1475
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001476#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001477 { \
1478 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1479 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001480 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001481 _OBJECT_TRACING_INIT \
1482 }
1483
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001484#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1485
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001486/**
1487 * INTERNAL_HIDDEN @endcond
1488 */
1489
1490/**
1491 * @defgroup queue_apis Queue APIs
1492 * @ingroup kernel_apis
1493 * @{
1494 */
1495
1496/**
1497 * @brief Initialize a queue.
1498 *
1499 * This routine initializes a queue object, prior to its first use.
1500 *
1501 * @param queue Address of the queue.
1502 *
1503 * @return N/A
1504 */
1505extern void k_queue_init(struct k_queue *queue);
1506
1507/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001508 * @brief Cancel waiting on a queue.
1509 *
1510 * This routine causes first thread pending on @a queue, if any, to
1511 * return from k_queue_get() call with NULL value (as if timeout expired).
1512 *
1513 * @note Can be called by ISRs.
1514 *
1515 * @param queue Address of the queue.
1516 *
1517 * @return N/A
1518 */
1519extern void k_queue_cancel_wait(struct k_queue *queue);
1520
1521/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001522 * @brief Append an element to the end of a queue.
1523 *
1524 * This routine appends a data item to @a queue. A queue data item must be
1525 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1526 * reserved for the kernel's use.
1527 *
1528 * @note Can be called by ISRs.
1529 *
1530 * @param queue Address of the queue.
1531 * @param data Address of the data item.
1532 *
1533 * @return N/A
1534 */
1535extern void k_queue_append(struct k_queue *queue, void *data);
1536
1537/**
1538 * @brief Prepend an element to a queue.
1539 *
1540 * This routine prepends a data item to @a queue. A queue data item must be
1541 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1542 * reserved for the kernel's use.
1543 *
1544 * @note Can be called by ISRs.
1545 *
1546 * @param queue Address of the queue.
1547 * @param data Address of the data item.
1548 *
1549 * @return N/A
1550 */
1551extern void k_queue_prepend(struct k_queue *queue, void *data);
1552
1553/**
1554 * @brief Inserts an element to a queue.
1555 *
1556 * This routine inserts a data item to @a queue after previous item. A queue
1557 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1558 * item are reserved for the kernel's use.
1559 *
1560 * @note Can be called by ISRs.
1561 *
1562 * @param queue Address of the queue.
1563 * @param prev Address of the previous data item.
1564 * @param data Address of the data item.
1565 *
1566 * @return N/A
1567 */
1568extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1569
1570/**
1571 * @brief Atomically append a list of elements to a queue.
1572 *
1573 * This routine adds a list of data items to @a queue in one operation.
1574 * The data items must be in a singly-linked list, with the first 32 bits
1575 * in each data item pointing to the next data item; the list must be
1576 * NULL-terminated.
1577 *
1578 * @note Can be called by ISRs.
1579 *
1580 * @param queue Address of the queue.
1581 * @param head Pointer to first node in singly-linked list.
1582 * @param tail Pointer to last node in singly-linked list.
1583 *
1584 * @return N/A
1585 */
1586extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1587
1588/**
1589 * @brief Atomically add a list of elements to a queue.
1590 *
1591 * This routine adds a list of data items to @a queue in one operation.
1592 * The data items must be in a singly-linked list implemented using a
1593 * sys_slist_t object. Upon completion, the original list is empty.
1594 *
1595 * @note Can be called by ISRs.
1596 *
1597 * @param queue Address of the queue.
1598 * @param list Pointer to sys_slist_t object.
1599 *
1600 * @return N/A
1601 */
1602extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1603
1604/**
1605 * @brief Get an element from a queue.
1606 *
1607 * This routine removes first data item from @a queue. The first 32 bits of the
1608 * data item are reserved for the kernel's use.
1609 *
1610 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1611 *
1612 * @param queue Address of the queue.
1613 * @param timeout Waiting period to obtain a data item (in milliseconds),
1614 * or one of the special values K_NO_WAIT and K_FOREVER.
1615 *
1616 * @return Address of the data item if successful; NULL if returned
1617 * without waiting, or waiting period timed out.
1618 */
Kumar Galacc334c72017-04-21 10:55:34 -05001619extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001620
1621/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001622 * @brief Remove an element from a queue.
1623 *
1624 * This routine removes data item from @a queue. The first 32 bits of the
1625 * data item are reserved for the kernel's use. Removing elements from k_queue
1626 * rely on sys_slist_find_and_remove which is not a constant time operation.
1627 *
1628 * @note Can be called by ISRs
1629 *
1630 * @param queue Address of the queue.
1631 * @param data Address of the data item.
1632 *
1633 * @return true if data item was removed
1634 */
1635static inline bool k_queue_remove(struct k_queue *queue, void *data)
1636{
1637 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1638}
1639
1640/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001641 * @brief Query a queue to see if it has data available.
1642 *
1643 * Note that the data might be already gone by the time this function returns
1644 * if other threads are also trying to read from the queue.
1645 *
1646 * @note Can be called by ISRs.
1647 *
1648 * @param queue Address of the queue.
1649 *
1650 * @return Non-zero if the queue is empty.
1651 * @return 0 if data is available.
1652 */
1653static inline int k_queue_is_empty(struct k_queue *queue)
1654{
1655 return (int)sys_slist_is_empty(&queue->data_q);
1656}
1657
1658/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001659 * @brief Peek element at the head of queue.
1660 *
1661 * Return element from the head of queue without removing it.
1662 *
1663 * @param queue Address of the queue.
1664 *
1665 * @return Head element, or NULL if queue is empty.
1666 */
1667static inline void *k_queue_peek_head(struct k_queue *queue)
1668{
1669 return sys_slist_peek_head(&queue->data_q);
1670}
1671
1672/**
1673 * @brief Peek element at the tail of queue.
1674 *
1675 * Return element from the tail of queue without removing it.
1676 *
1677 * @param queue Address of the queue.
1678 *
1679 * @return Tail element, or NULL if queue is empty.
1680 */
1681static inline void *k_queue_peek_tail(struct k_queue *queue)
1682{
1683 return sys_slist_peek_tail(&queue->data_q);
1684}
1685
1686/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001687 * @brief Statically define and initialize a queue.
1688 *
1689 * The queue can be accessed outside the module where it is defined using:
1690 *
1691 * @code extern struct k_queue <name>; @endcode
1692 *
1693 * @param name Name of the queue.
1694 */
1695#define K_QUEUE_DEFINE(name) \
1696 struct k_queue name \
1697 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001698 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001699
1700/**
1701 * @} end defgroup queue_apis
1702 */
1703
1704/**
1705 * @cond INTERNAL_HIDDEN
1706 */
1707
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001708struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001709 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001710};
1711
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001712#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001713 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001714 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001715 }
1716
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001717#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1718
Allan Stephensc98da842016-11-11 15:45:03 -05001719/**
1720 * INTERNAL_HIDDEN @endcond
1721 */
1722
1723/**
1724 * @defgroup fifo_apis Fifo APIs
1725 * @ingroup kernel_apis
1726 * @{
1727 */
1728
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
1736 * @return N/A
1737 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001738#define k_fifo_init(fifo) \
1739 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740
1741/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001742 * @brief Cancel waiting on a fifo.
1743 *
1744 * This routine causes first thread pending on @a fifo, if any, to
1745 * return from k_fifo_get() call with NULL value (as if timeout
1746 * expired).
1747 *
1748 * @note Can be called by ISRs.
1749 *
1750 * @param fifo Address of the fifo.
1751 *
1752 * @return N/A
1753 */
1754#define k_fifo_cancel_wait(fifo) \
1755 k_queue_cancel_wait((struct k_queue *) fifo)
1756
1757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * This routine adds a data item to @a fifo. A fifo data item must be
1761 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1762 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * @note Can be called by ISRs.
1765 *
1766 * @param fifo Address of the fifo.
1767 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 *
1769 * @return N/A
1770 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001771#define k_fifo_put(fifo, data) \
1772 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773
1774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * This routine adds a list of data items to @a fifo in one operation.
1778 * The data items must be in a singly-linked list, with the first 32 bits
1779 * each data item pointing to the next data item; the list must be
1780 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001782 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001784 * @param fifo Address of the fifo.
1785 * @param head Pointer to first node in singly-linked list.
1786 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001787 *
1788 * @return N/A
1789 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001790#define k_fifo_put_list(fifo, head, tail) \
1791 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001792
1793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * This routine adds a list of data items to @a fifo in one operation.
1797 * The data items must be in a singly-linked list implemented using a
1798 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 * and must be re-initialized via sys_slist_init().
1800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * @note Can be called by ISRs.
1802 *
1803 * @param fifo Address of the fifo.
1804 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
1806 * @return N/A
1807 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001808#define k_fifo_put_slist(fifo, list) \
1809 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001810
1811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * This routine removes a data item from @a fifo in a "first in, first out"
1815 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1818 *
1819 * @param fifo Address of the fifo.
1820 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821 * or one of the special values K_NO_WAIT and K_FOREVER.
1822 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001823 * @return Address of the data item if successful; NULL if returned
1824 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001825 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001826#define k_fifo_get(fifo, timeout) \
1827 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001828
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001830 * @brief Query a fifo to see if it has data available.
1831 *
1832 * Note that the data might be already gone by the time this function returns
1833 * if other threads is also trying to read from the fifo.
1834 *
1835 * @note Can be called by ISRs.
1836 *
1837 * @param fifo Address of the fifo.
1838 *
1839 * @return Non-zero if the fifo is empty.
1840 * @return 0 if data is available.
1841 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001842#define k_fifo_is_empty(fifo) \
1843 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001844
1845/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001846 * @brief Peek element at the head of fifo.
1847 *
1848 * Return element from the head of fifo without removing it. A usecase
1849 * for this is if elements of the fifo are themselves containers. Then
1850 * on each iteration of processing, a head container will be peeked,
1851 * and some data processed out of it, and only if the container is empty,
1852 * it will be completely remove from the fifo.
1853 *
1854 * @param fifo Address of the fifo.
1855 *
1856 * @return Head element, or NULL if the fifo is empty.
1857 */
1858#define k_fifo_peek_head(fifo) \
1859 k_queue_peek_head((struct k_queue *) fifo)
1860
1861/**
1862 * @brief Peek element at the tail of fifo.
1863 *
1864 * Return element from the tail of fifo (without removing it). A usecase
1865 * for this is if elements of the fifo are themselves containers. Then
1866 * it may be useful to add more data to the last container in fifo.
1867 *
1868 * @param fifo Address of the fifo.
1869 *
1870 * @return Tail element, or NULL if fifo is empty.
1871 */
1872#define k_fifo_peek_tail(fifo) \
1873 k_queue_peek_tail((struct k_queue *) fifo)
1874
1875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001880 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001885 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001886 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001887 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888
Allan Stephensc98da842016-11-11 15:45:03 -05001889/**
1890 * @} end defgroup fifo_apis
1891 */
1892
1893/**
1894 * @cond INTERNAL_HIDDEN
1895 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896
1897struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001898 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001899};
1900
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001901#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001902 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001903 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001904 }
1905
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001906#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1907
Allan Stephensc98da842016-11-11 15:45:03 -05001908/**
1909 * INTERNAL_HIDDEN @endcond
1910 */
1911
1912/**
1913 * @defgroup lifo_apis Lifo APIs
1914 * @ingroup kernel_apis
1915 * @{
1916 */
1917
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 *
1925 * @return N/A
1926 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001927#define k_lifo_init(lifo) \
1928 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001929
1930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * This routine adds a data item to @a lifo. A lifo data item must be
1934 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1935 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001937 * @note Can be called by ISRs.
1938 *
1939 * @param lifo Address of the lifo.
1940 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941 *
1942 * @return N/A
1943 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001944#define k_lifo_put(lifo, data) \
1945 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001946
1947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001950 * This routine removes a data item from @a lifo in a "last in, first out"
1951 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001953 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1954 *
1955 * @param lifo Address of the lifo.
1956 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001957 * or one of the special values K_NO_WAIT and K_FOREVER.
1958 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001959 * @return Address of the data item if successful; NULL if returned
1960 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001962#define k_lifo_get(lifo, timeout) \
1963 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001966 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001968 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001970 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001975 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001976 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001977 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001978
Allan Stephensc98da842016-11-11 15:45:03 -05001979/**
1980 * @} end defgroup lifo_apis
1981 */
1982
1983/**
1984 * @cond INTERNAL_HIDDEN
1985 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001986
1987struct k_stack {
1988 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001989 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990
Anas Nashif2f203c22016-12-18 06:57:45 -05001991 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992};
1993
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001994#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001995 { \
1996 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1997 .base = stack_buffer, \
1998 .next = stack_buffer, \
1999 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002000 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002001 }
2002
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002003#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2004
Allan Stephensc98da842016-11-11 15:45:03 -05002005/**
2006 * INTERNAL_HIDDEN @endcond
2007 */
2008
2009/**
2010 * @defgroup stack_apis Stack APIs
2011 * @ingroup kernel_apis
2012 * @{
2013 */
2014
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002015/**
2016 * @brief Initialize a stack.
2017 *
2018 * This routine initializes a stack object, prior to its first use.
2019 *
2020 * @param stack Address of the stack.
2021 * @param buffer Address of array used to hold stacked values.
2022 * @param num_entries Maximum number of values that can be stacked.
2023 *
2024 * @return N/A
2025 */
Andrew Boiee8734462017-09-29 16:42:07 -07002026__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002027 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002028
2029/**
2030 * @brief Push an element onto a stack.
2031 *
2032 * This routine adds a 32-bit value @a data to @a stack.
2033 *
2034 * @note Can be called by ISRs.
2035 *
2036 * @param stack Address of the stack.
2037 * @param data Value to push onto the stack.
2038 *
2039 * @return N/A
2040 */
Andrew Boiee8734462017-09-29 16:42:07 -07002041__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042
2043/**
2044 * @brief Pop an element from a stack.
2045 *
2046 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2047 * manner and stores the value in @a data.
2048 *
2049 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2050 *
2051 * @param stack Address of the stack.
2052 * @param data Address of area to hold the value popped from the stack.
2053 * @param timeout Waiting period to obtain a value (in milliseconds),
2054 * or one of the special values K_NO_WAIT and K_FOREVER.
2055 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002056 * @retval 0 Element popped from stack.
2057 * @retval -EBUSY Returned without waiting.
2058 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002059 */
Andrew Boiee8734462017-09-29 16:42:07 -07002060__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002062/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002067 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * @param name Name of the stack.
2070 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002072#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002073 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002074 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002075 struct k_stack name \
2076 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002077 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002078 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002080/**
Allan Stephensc98da842016-11-11 15:45:03 -05002081 * @} end defgroup stack_apis
2082 */
2083
Allan Stephens6bba9b02016-11-16 14:56:54 -05002084struct k_work;
2085
Allan Stephensc98da842016-11-11 15:45:03 -05002086/**
2087 * @defgroup workqueue_apis Workqueue Thread APIs
2088 * @ingroup kernel_apis
2089 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090 */
2091
Allan Stephens6bba9b02016-11-16 14:56:54 -05002092/**
2093 * @typedef k_work_handler_t
2094 * @brief Work item handler function type.
2095 *
2096 * A work item's handler function is executed by a workqueue's thread
2097 * when the work item is processed by the workqueue.
2098 *
2099 * @param work Address of the work item.
2100 *
2101 * @return N/A
2102 */
2103typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104
2105/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002106 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002108
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002110 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002111 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112};
2113
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002115 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116};
2117
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002119 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120 k_work_handler_t handler;
2121 atomic_t flags[1];
2122};
2123
Allan Stephens6bba9b02016-11-16 14:56:54 -05002124struct k_delayed_work {
2125 struct k_work work;
2126 struct _timeout timeout;
2127 struct k_work_q *work_q;
2128};
2129
2130extern struct k_work_q k_sys_work_q;
2131
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002133 * INTERNAL_HIDDEN @endcond
2134 */
2135
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002136#define _K_WORK_INITIALIZER(work_handler) \
2137 { \
2138 ._reserved = NULL, \
2139 .handler = work_handler, \
2140 .flags = { 0 } \
2141 }
2142
2143#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2144
Allan Stephens6bba9b02016-11-16 14:56:54 -05002145/**
2146 * @brief Initialize a statically-defined work item.
2147 *
2148 * This macro can be used to initialize a statically-defined workqueue work
2149 * item, prior to its first use. For example,
2150 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002151 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002152 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002153 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002154 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002156#define K_WORK_DEFINE(work, work_handler) \
2157 struct k_work work \
2158 __in_section(_k_work, static, work) = \
2159 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160
2161/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002162 * @brief Initialize a work item.
2163 *
2164 * This routine initializes a workqueue work item, prior to its first use.
2165 *
2166 * @param work Address of work item.
2167 * @param handler Function to invoke each time work item is processed.
2168 *
2169 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002170 */
2171static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2172{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002173 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002175 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176}
2177
2178/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002179 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002180 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002181 * This routine submits work item @a work to be processed by workqueue
2182 * @a work_q. If the work item is already pending in the workqueue's queue
2183 * as a result of an earlier submission, this routine has no effect on the
2184 * work item. If the work item has already been processed, or is currently
2185 * being processed, its work is considered complete and the work item can be
2186 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002187 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002188 * @warning
2189 * A submitted work item must not be modified until it has been processed
2190 * by the workqueue.
2191 *
2192 * @note Can be called by ISRs.
2193 *
2194 * @param work_q Address of workqueue.
2195 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002196 *
2197 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198 */
2199static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2200 struct k_work *work)
2201{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002202 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002203 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204 }
2205}
2206
2207/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002208 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002209 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002210 * This routine indicates if work item @a work is pending in a workqueue's
2211 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002212 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * @note Can be called by ISRs.
2214 *
2215 * @param work Address of work item.
2216 *
2217 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002218 */
2219static inline int k_work_pending(struct k_work *work)
2220{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002221 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002222}
2223
2224/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002227 * This routine starts workqueue @a work_q. The workqueue spawns its work
2228 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002231 * @param stack Pointer to work queue thread's stack space, as defined by
2232 * K_THREAD_STACK_DEFINE()
2233 * @param stack_size Size of the work queue thread's stack (in bytes), which
2234 * should either be the same constant passed to
2235 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002236 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002237 *
2238 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 */
Andrew Boie507852a2017-07-25 18:47:07 -07002240extern void k_work_q_start(struct k_work_q *work_q,
2241 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002242 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002247 * This routine initializes a workqueue delayed work item, prior to
2248 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002250 * @param work Address of delayed work item.
2251 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
2253 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002255extern void k_delayed_work_init(struct k_delayed_work *work,
2256 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257
2258/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002261 * This routine schedules work item @a work to be processed by workqueue
2262 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002263 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002264 * Only when the countdown completes is the work item actually submitted to
2265 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * Submitting a previously submitted delayed work item that is still
2268 * counting down cancels the existing submission and restarts the countdown
2269 * using the new delay. If the work item is currently pending on the
2270 * workqueue's queue because the countdown has completed it is too late to
2271 * resubmit the item, and resubmission fails without impacting the work item.
2272 * If the work item has already been processed, or is currently being processed,
2273 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002275 * @warning
2276 * A delayed work item must not be modified until it has been processed
2277 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * @note Can be called by ISRs.
2280 *
2281 * @param work_q Address of workqueue.
2282 * @param work Address of delayed work item.
2283 * @param delay Delay before submitting the work item (in milliseconds).
2284 *
2285 * @retval 0 Work item countdown started.
2286 * @retval -EINPROGRESS Work item is already pending.
2287 * @retval -EINVAL Work item is being processed or has completed its work.
2288 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002290extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2291 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002292 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293
2294/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002297 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002298 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002299 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002303 * @param work Address of delayed work item.
2304 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002305 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002306 * @retval -EINPROGRESS Work item is already pending.
2307 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002309extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 * @brief Submit a work item to the system workqueue.
2313 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002314 * This routine submits work item @a work to be processed by the system
2315 * workqueue. If the work item is already pending in the workqueue's queue
2316 * as a result of an earlier submission, this routine has no effect on the
2317 * work item. If the work item has already been processed, or is currently
2318 * being processed, its work is considered complete and the work item can be
2319 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002321 * @warning
2322 * Work items submitted to the system workqueue should avoid using handlers
2323 * that block or yield since this may prevent the system workqueue from
2324 * processing other work items in a timely manner.
2325 *
2326 * @note Can be called by ISRs.
2327 *
2328 * @param work Address of work item.
2329 *
2330 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 */
2332static inline void k_work_submit(struct k_work *work)
2333{
2334 k_work_submit_to_queue(&k_sys_work_q, work);
2335}
2336
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338 * @brief Submit a delayed work item to the system workqueue.
2339 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002340 * This routine schedules work item @a work to be processed by the system
2341 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002342 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002343 * Only when the countdown completes is the work item actually submitted to
2344 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * Submitting a previously submitted delayed work item that is still
2347 * counting down cancels the existing submission and restarts the countdown
2348 * using the new delay. If the work item is currently pending on the
2349 * workqueue's queue because the countdown has completed it is too late to
2350 * resubmit the item, and resubmission fails without impacting the work item.
2351 * If the work item has already been processed, or is currently being processed,
2352 * its work is considered complete and the work item can be resubmitted.
2353 *
2354 * @warning
2355 * Work items submitted to the system workqueue should avoid using handlers
2356 * that block or yield since this may prevent the system workqueue from
2357 * processing other work items in a timely manner.
2358 *
2359 * @note Can be called by ISRs.
2360 *
2361 * @param work Address of delayed work item.
2362 * @param delay Delay before submitting the work item (in milliseconds).
2363 *
2364 * @retval 0 Work item countdown started.
2365 * @retval -EINPROGRESS Work item is already pending.
2366 * @retval -EINVAL Work item is being processed or has completed its work.
2367 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 */
2369static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002370 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002372 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373}
2374
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002376 * @brief Get time remaining before a delayed work gets scheduled.
2377 *
2378 * This routine computes the (approximate) time remaining before a
2379 * delayed work gets executed. If the delayed work is not waiting to be
2380 * schedules, it returns zero.
2381 *
2382 * @param work Delayed work item.
2383 *
2384 * @return Remaining time (in milliseconds).
2385 */
Kumar Galacc334c72017-04-21 10:55:34 -05002386static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002387{
2388 return _timeout_remaining_get(&work->timeout);
2389}
2390
2391/**
Allan Stephensc98da842016-11-11 15:45:03 -05002392 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393 */
2394
Allan Stephensc98da842016-11-11 15:45:03 -05002395/**
2396 * @cond INTERNAL_HIDDEN
2397 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398
2399struct k_mutex {
2400 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002401 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002402 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404
Anas Nashif2f203c22016-12-18 06:57:45 -05002405 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406};
2407
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002408#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 { \
2410 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2411 .owner = NULL, \
2412 .lock_count = 0, \
2413 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002414 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 }
2416
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002417#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2418
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419/**
Allan Stephensc98da842016-11-11 15:45:03 -05002420 * INTERNAL_HIDDEN @endcond
2421 */
2422
2423/**
2424 * @defgroup mutex_apis Mutex APIs
2425 * @ingroup kernel_apis
2426 * @{
2427 */
2428
2429/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002434 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002439 struct k_mutex name \
2440 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002441 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * Upon completion, the mutex is available and does not have an owner.
2449 *
2450 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451 *
2452 * @return N/A
2453 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002454__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455
2456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002457 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * This routine locks @a mutex. If the mutex is locked by another thread,
2460 * the calling thread waits until the mutex becomes available or until
2461 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * A thread is permitted to lock a mutex it has already locked. The operation
2464 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466 * @param mutex Address of the mutex.
2467 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 * or one of the special values K_NO_WAIT and K_FOREVER.
2469 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002470 * @retval 0 Mutex locked.
2471 * @retval -EBUSY Returned without waiting.
2472 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002474__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475
2476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * This routine unlocks @a mutex. The mutex must already be locked by the
2480 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
2482 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484 * thread.
2485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 *
2488 * @return N/A
2489 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002490__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491
Allan Stephensc98da842016-11-11 15:45:03 -05002492/**
2493 * @} end defgroup mutex_apis
2494 */
2495
2496/**
2497 * @cond INTERNAL_HIDDEN
2498 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499
2500struct k_sem {
2501 _wait_q_t wait_q;
2502 unsigned int count;
2503 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002504 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Anas Nashif2f203c22016-12-18 06:57:45 -05002506 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507};
2508
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002509#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002510 { \
2511 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2512 .count = initial_count, \
2513 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002514 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002515 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002516 }
2517
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002518#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2519
Allan Stephensc98da842016-11-11 15:45:03 -05002520/**
2521 * INTERNAL_HIDDEN @endcond
2522 */
2523
2524/**
2525 * @defgroup semaphore_apis Semaphore APIs
2526 * @ingroup kernel_apis
2527 * @{
2528 */
2529
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * @param sem Address of the semaphore.
2536 * @param initial_count Initial semaphore count.
2537 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002538 *
2539 * @return N/A
2540 */
Andrew Boie99280232017-09-29 14:17:47 -07002541__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2542 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002543
2544/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002545 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2550 *
2551 * @param sem Address of the semaphore.
2552 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002553 * or one of the special values K_NO_WAIT and K_FOREVER.
2554 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002555 * @note When porting code from the nanokernel legacy API to the new API, be
2556 * careful with the return value of this function. The return value is the
2557 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2558 * non-zero means failure, while the nano_sem_take family returns 1 for success
2559 * and 0 for failure.
2560 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002561 * @retval 0 Semaphore taken.
2562 * @retval -EBUSY Returned without waiting.
2563 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002564 */
Andrew Boie99280232017-09-29 14:17:47 -07002565__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002566
2567/**
2568 * @brief Give a semaphore.
2569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * This routine gives @a sem, unless the semaphore is already at its maximum
2571 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002576 *
2577 * @return N/A
2578 */
Andrew Boie99280232017-09-29 14:17:47 -07002579__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581/**
2582 * @brief Reset a semaphore's count to zero.
2583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587 *
2588 * @return N/A
2589 */
Andrew Boie990bf162017-10-03 12:36:49 -07002590__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002591
2592static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593{
2594 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595}
2596
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597/**
2598 * @brief Get a semaphore's count.
2599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002605 */
Andrew Boie990bf162017-10-03 12:36:49 -07002606__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002607
2608static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609{
2610 return sem->count;
2611}
2612
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002614 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002617 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002618 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * @param name Name of the semaphore.
2621 * @param initial_count Initial semaphore count.
2622 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002625 struct k_sem name \
2626 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002627 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628
Allan Stephensc98da842016-11-11 15:45:03 -05002629/**
2630 * @} end defgroup semaphore_apis
2631 */
2632
2633/**
2634 * @defgroup alert_apis Alert APIs
2635 * @ingroup kernel_apis
2636 * @{
2637 */
2638
Allan Stephens5eceb852016-11-16 10:16:30 -05002639/**
2640 * @typedef k_alert_handler_t
2641 * @brief Alert handler function type.
2642 *
2643 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002644 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002645 * and is only invoked if the alert has been initialized with one.
2646 *
2647 * @param alert Address of the alert.
2648 *
2649 * @return 0 if alert has been consumed; non-zero if alert should pend.
2650 */
2651typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002652
2653/**
2654 * @} end defgroup alert_apis
2655 */
2656
2657/**
2658 * @cond INTERNAL_HIDDEN
2659 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002661#define K_ALERT_DEFAULT NULL
2662#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002664struct k_alert {
2665 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666 atomic_t send_count;
2667 struct k_work work_item;
2668 struct k_sem sem;
2669
Anas Nashif2f203c22016-12-18 06:57:45 -05002670 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671};
2672
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002673extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002675#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002677 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002679 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2680 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002681 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682 }
2683
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002684#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2685
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686/**
Allan Stephensc98da842016-11-11 15:45:03 -05002687 * INTERNAL_HIDDEN @endcond
2688 */
2689
2690/**
2691 * @addtogroup alert_apis
2692 * @{
2693 */
2694
2695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002697 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002698 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002699 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002700 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @param name Name of the alert.
2703 * @param alert_handler Action to take when alert is sent. Specify either
2704 * the address of a function to be invoked by the system workqueue
2705 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2706 * K_ALERT_DEFAULT (which causes the alert to pend).
2707 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002709#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002710 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002711 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002712 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002713 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @param alert Address of the alert.
2721 * @param handler Action to take when alert is sent. Specify either the address
2722 * of a function to be invoked by the system workqueue thread,
2723 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2724 * K_ALERT_DEFAULT (which causes the alert to pend).
2725 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002726 *
2727 * @return N/A
2728 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002729extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2730 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002731
2732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2738 *
2739 * @param alert Address of the alert.
2740 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002741 * or one of the special values K_NO_WAIT and K_FOREVER.
2742 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002743 * @retval 0 Alert received.
2744 * @retval -EBUSY Returned without waiting.
2745 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002746 */
Andrew Boie310e9872017-09-29 04:41:15 -07002747__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002748
2749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * This routine signals @a alert. The action specified for @a alert will
2753 * be taken, which may trigger the execution of an alert handler function
2754 * and/or cause the alert to pend (assuming the alert has not reached its
2755 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * @note Can be called by ISRs.
2758 *
2759 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760 *
2761 * @return N/A
2762 */
Andrew Boie310e9872017-09-29 04:41:15 -07002763__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764
2765/**
Allan Stephensc98da842016-11-11 15:45:03 -05002766 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 */
2768
Allan Stephensc98da842016-11-11 15:45:03 -05002769/**
2770 * @cond INTERNAL_HIDDEN
2771 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772
2773struct k_msgq {
2774 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002775 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002776 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 char *buffer_start;
2778 char *buffer_end;
2779 char *read_ptr;
2780 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002781 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782
Anas Nashif2f203c22016-12-18 06:57:45 -05002783 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002784};
2785
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002786#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787 { \
2788 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002789 .max_msgs = q_max_msgs, \
2790 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002792 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793 .read_ptr = q_buffer, \
2794 .write_ptr = q_buffer, \
2795 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002796 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797 }
2798
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002799#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2800
Peter Mitsis1da807e2016-10-06 11:36:59 -04002801/**
Allan Stephensc98da842016-11-11 15:45:03 -05002802 * INTERNAL_HIDDEN @endcond
2803 */
2804
2805/**
2806 * @defgroup msgq_apis Message Queue APIs
2807 * @ingroup kernel_apis
2808 * @{
2809 */
2810
2811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2815 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002816 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2817 * message is similarly aligned to this boundary, @a q_msg_size must also be
2818 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * The message queue can be accessed outside the module where it is defined
2821 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002822 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002823 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @param q_name Name of the message queue.
2826 * @param q_msg_size Message size (in bytes).
2827 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002828 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002829 */
2830#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2831 static char __noinit __aligned(q_align) \
2832 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002833 struct k_msgq q_name \
2834 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002835 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002836 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837
Peter Mitsisd7a37502016-10-13 11:37:40 -04002838/**
2839 * @brief Initialize a message queue.
2840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * This routine initializes a message queue object, prior to its first use.
2842 *
Allan Stephensda827222016-11-09 14:23:58 -06002843 * The message queue's ring buffer must contain space for @a max_msgs messages,
2844 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2845 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2846 * that each message is similarly aligned to this boundary, @a q_msg_size
2847 * must also be a multiple of N.
2848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * @param q Address of the message queue.
2850 * @param buffer Pointer to ring buffer that holds queued messages.
2851 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002852 * @param max_msgs Maximum number of messages that can be queued.
2853 *
2854 * @return N/A
2855 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002856__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2857 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002858
2859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002863 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002864 * @note Can be called by ISRs.
2865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @param q Address of the message queue.
2867 * @param data Pointer to the message.
2868 * @param timeout Waiting period to add the message (in milliseconds),
2869 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002870 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002871 * @retval 0 Message sent.
2872 * @retval -ENOMSG Returned without waiting or queue purged.
2873 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002874 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002875__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002876
2877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002878 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * This routine receives a message from message queue @a q in a "first in,
2881 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002882 *
Allan Stephensc98da842016-11-11 15:45:03 -05002883 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param q Address of the message queue.
2886 * @param data Address of area to hold the received message.
2887 * @param timeout Waiting period to receive the message (in milliseconds),
2888 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002889 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002890 * @retval 0 Message received.
2891 * @retval -ENOMSG Returned without waiting.
2892 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002893 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002894__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002895
2896/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * This routine discards all unreceived messages in a message queue's ring
2900 * buffer. Any threads that are blocked waiting to send a message to the
2901 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904 *
2905 * @return N/A
2906 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002907__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908
Peter Mitsis67be2492016-10-07 11:44:34 -04002909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * This routine returns the number of unused entries in a message queue's
2913 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @param q Address of the message queue.
2916 *
2917 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002918 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002919__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2920
2921static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002922{
2923 return q->max_msgs - q->used_msgs;
2924}
2925
Peter Mitsisd7a37502016-10-13 11:37:40 -04002926/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * @param q Address of the message queue.
2932 *
2933 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002934 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002935__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2936
2937static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938{
2939 return q->used_msgs;
2940}
2941
Allan Stephensc98da842016-11-11 15:45:03 -05002942/**
2943 * @} end defgroup msgq_apis
2944 */
2945
2946/**
2947 * @defgroup mem_pool_apis Memory Pool APIs
2948 * @ingroup kernel_apis
2949 * @{
2950 */
2951
Andy Ross73cb9582017-05-09 10:42:39 -07002952/* Note on sizing: the use of a 20 bit field for block means that,
2953 * assuming a reasonable minimum block size of 16 bytes, we're limited
2954 * to 16M of memory managed by a single pool. Long term it would be
2955 * good to move to a variable bit size based on configuration.
2956 */
2957struct k_mem_block_id {
2958 u32_t pool : 8;
2959 u32_t level : 4;
2960 u32_t block : 20;
2961};
2962
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002964 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002965 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966};
2967
Allan Stephensc98da842016-11-11 15:45:03 -05002968/**
2969 * @} end defgroup mem_pool_apis
2970 */
2971
2972/**
2973 * @defgroup mailbox_apis Mailbox APIs
2974 * @ingroup kernel_apis
2975 * @{
2976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002977
2978struct k_mbox_msg {
2979 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002980 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002982 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002984 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 /** sender's message data buffer */
2986 void *tx_data;
2987 /** internal use only - needed for legacy API support */
2988 void *_rx_data;
2989 /** message data block descriptor */
2990 struct k_mem_block tx_block;
2991 /** source thread id */
2992 k_tid_t rx_source_thread;
2993 /** target thread id */
2994 k_tid_t tx_target_thread;
2995 /** internal use only - thread waiting on send (may be a dummy) */
2996 k_tid_t _syncing_thread;
2997#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2998 /** internal use only - semaphore used during asynchronous send */
2999 struct k_sem *_async_sem;
3000#endif
3001};
3002
Allan Stephensc98da842016-11-11 15:45:03 -05003003/**
3004 * @cond INTERNAL_HIDDEN
3005 */
3006
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007struct k_mbox {
3008 _wait_q_t tx_msg_queue;
3009 _wait_q_t rx_msg_queue;
3010
Anas Nashif2f203c22016-12-18 06:57:45 -05003011 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012};
3013
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003014#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015 { \
3016 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3017 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003018 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019 }
3020
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003021#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3022
Peter Mitsis12092702016-10-14 12:57:23 -04003023/**
Allan Stephensc98da842016-11-11 15:45:03 -05003024 * INTERNAL_HIDDEN @endcond
3025 */
3026
3027/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003031 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003032 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003035 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003037 struct k_mbox name \
3038 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003039 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040
Peter Mitsis12092702016-10-14 12:57:23 -04003041/**
3042 * @brief Initialize a mailbox.
3043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003044 * This routine initializes a mailbox object, prior to its first use.
3045 *
3046 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003047 *
3048 * @return N/A
3049 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003050extern void k_mbox_init(struct k_mbox *mbox);
3051
Peter Mitsis12092702016-10-14 12:57:23 -04003052/**
3053 * @brief Send a mailbox message in a synchronous manner.
3054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * This routine sends a message to @a mbox and waits for a receiver to both
3056 * receive and process it. The message data may be in a buffer, in a memory
3057 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * @param mbox Address of the mailbox.
3060 * @param tx_msg Address of the transmit message descriptor.
3061 * @param timeout Waiting period for the message to be received (in
3062 * milliseconds), or one of the special values K_NO_WAIT
3063 * and K_FOREVER. Once the message has been received,
3064 * this routine waits as long as necessary for the message
3065 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003066 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003067 * @retval 0 Message sent.
3068 * @retval -ENOMSG Returned without waiting.
3069 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003070 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003071extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003072 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003073
Peter Mitsis12092702016-10-14 12:57:23 -04003074/**
3075 * @brief Send a mailbox message in an asynchronous manner.
3076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003077 * This routine sends a message to @a mbox without waiting for a receiver
3078 * to process it. The message data may be in a buffer, in a memory pool block,
3079 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3080 * will be given when the message has been both received and completely
3081 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * @param mbox Address of the mailbox.
3084 * @param tx_msg Address of the transmit message descriptor.
3085 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003086 *
3087 * @return N/A
3088 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003089extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090 struct k_sem *sem);
3091
Peter Mitsis12092702016-10-14 12:57:23 -04003092/**
3093 * @brief Receive a mailbox message.
3094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * This routine receives a message from @a mbox, then optionally retrieves
3096 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @param mbox Address of the mailbox.
3099 * @param rx_msg Address of the receive message descriptor.
3100 * @param buffer Address of the buffer to receive data, or NULL to defer data
3101 * retrieval and message disposal until later.
3102 * @param timeout Waiting period for a message to be received (in
3103 * milliseconds), or one of the special values K_NO_WAIT
3104 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003105 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003106 * @retval 0 Message received.
3107 * @retval -ENOMSG Returned without waiting.
3108 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003109 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003110extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003111 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003112
3113/**
3114 * @brief Retrieve mailbox message data into a buffer.
3115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003116 * This routine completes the processing of a received message by retrieving
3117 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003118 *
3119 * Alternatively, this routine can be used to dispose of a received message
3120 * without retrieving its data.
3121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * @param rx_msg Address of the receive message descriptor.
3123 * @param buffer Address of the buffer to receive data, or NULL to discard
3124 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003125 *
3126 * @return N/A
3127 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003128extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003129
3130/**
3131 * @brief Retrieve mailbox message data into a memory pool block.
3132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * This routine completes the processing of a received message by retrieving
3134 * its data into a memory pool block, then disposing of the message.
3135 * The memory pool block that results from successful retrieval must be
3136 * returned to the pool once the data has been processed, even in cases
3137 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003138 *
3139 * Alternatively, this routine can be used to dispose of a received message
3140 * without retrieving its data. In this case there is no need to return a
3141 * memory pool block to the pool.
3142 *
3143 * This routine allocates a new memory pool block for the data only if the
3144 * data is not already in one. If a new block cannot be allocated, the routine
3145 * returns a failure code and the received message is left unchanged. This
3146 * permits the caller to reattempt data retrieval at a later time or to dispose
3147 * of the received message without retrieving its data.
3148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * @param rx_msg Address of a receive message descriptor.
3150 * @param pool Address of memory pool, or NULL to discard data.
3151 * @param block Address of the area to hold memory pool block info.
3152 * @param timeout Waiting period to wait for a memory pool block (in
3153 * milliseconds), or one of the special values K_NO_WAIT
3154 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003155 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003156 * @retval 0 Data retrieved.
3157 * @retval -ENOMEM Returned without waiting.
3158 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003159 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003160extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003161 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003162 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003163
Allan Stephensc98da842016-11-11 15:45:03 -05003164/**
3165 * @} end defgroup mailbox_apis
3166 */
3167
3168/**
3169 * @cond INTERNAL_HIDDEN
3170 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003171
3172struct k_pipe {
3173 unsigned char *buffer; /* Pipe buffer: may be NULL */
3174 size_t size; /* Buffer size */
3175 size_t bytes_used; /* # bytes used in buffer */
3176 size_t read_index; /* Where in buffer to read from */
3177 size_t write_index; /* Where in buffer to write */
3178
3179 struct {
3180 _wait_q_t readers; /* Reader wait queue */
3181 _wait_q_t writers; /* Writer wait queue */
3182 } wait_q;
3183
Anas Nashif2f203c22016-12-18 06:57:45 -05003184 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185};
3186
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003187#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188 { \
3189 .buffer = pipe_buffer, \
3190 .size = pipe_buffer_size, \
3191 .bytes_used = 0, \
3192 .read_index = 0, \
3193 .write_index = 0, \
3194 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3195 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003196 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003197 }
3198
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003199#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3200
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003201/**
Allan Stephensc98da842016-11-11 15:45:03 -05003202 * INTERNAL_HIDDEN @endcond
3203 */
3204
3205/**
3206 * @defgroup pipe_apis Pipe APIs
3207 * @ingroup kernel_apis
3208 * @{
3209 */
3210
3211/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003215 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003216 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @param name Name of the pipe.
3219 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3220 * or zero if no ring buffer is used.
3221 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003222 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003223#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3224 static unsigned char __noinit __aligned(pipe_align) \
3225 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003226 struct k_pipe name \
3227 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003228 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * This routine initializes a pipe object, prior to its first use.
3234 *
3235 * @param pipe Address of the pipe.
3236 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3237 * is used.
3238 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3239 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240 *
3241 * @return N/A
3242 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003243__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3244 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245
3246/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @param pipe Address of the pipe.
3252 * @param data Address of data to write.
3253 * @param bytes_to_write Size of data (in bytes).
3254 * @param bytes_written Address of area to hold the number of bytes written.
3255 * @param min_xfer Minimum number of bytes to write.
3256 * @param timeout Waiting period to wait for the data to be written (in
3257 * milliseconds), or one of the special values K_NO_WAIT
3258 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003260 * @retval 0 At least @a min_xfer bytes of data were written.
3261 * @retval -EIO Returned without waiting; zero data bytes were written.
3262 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003265__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3266 size_t bytes_to_write, size_t *bytes_written,
3267 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268
3269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * @param pipe Address of the pipe.
3275 * @param data Address to place the data read from pipe.
3276 * @param bytes_to_read Maximum number of data bytes to read.
3277 * @param bytes_read Address of area to hold the number of bytes read.
3278 * @param min_xfer Minimum number of data bytes to read.
3279 * @param timeout Waiting period to wait for the data to be read (in
3280 * milliseconds), or one of the special values K_NO_WAIT
3281 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003283 * @retval 0 At least @a min_xfer bytes of data were read.
3284 * @retval -EIO Returned without waiting; zero data bytes were read.
3285 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003288__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3289 size_t bytes_to_read, size_t *bytes_read,
3290 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291
3292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * This routine writes the data contained in a memory block to @a pipe.
3296 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300 * @param block Memory block containing data to send
3301 * @param size Number of data bytes in memory block to send
3302 * @param sem Semaphore to signal upon completion (else NULL)
3303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305 */
3306extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3307 size_t size, struct k_sem *sem);
3308
3309/**
Allan Stephensc98da842016-11-11 15:45:03 -05003310 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311 */
3312
Allan Stephensc98da842016-11-11 15:45:03 -05003313/**
3314 * @cond INTERNAL_HIDDEN
3315 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003316
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003317struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003319 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003320 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003321 char *buffer;
3322 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003323 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324
Anas Nashif2f203c22016-12-18 06:57:45 -05003325 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326};
3327
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003328#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003329 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330 { \
3331 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003332 .num_blocks = slab_num_blocks, \
3333 .block_size = slab_block_size, \
3334 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003335 .free_list = NULL, \
3336 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003337 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338 }
3339
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003340#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3341
3342
Peter Mitsis578f9112016-10-07 13:50:31 -04003343/**
Allan Stephensc98da842016-11-11 15:45:03 -05003344 * INTERNAL_HIDDEN @endcond
3345 */
3346
3347/**
3348 * @defgroup mem_slab_apis Memory Slab APIs
3349 * @ingroup kernel_apis
3350 * @{
3351 */
3352
3353/**
Allan Stephensda827222016-11-09 14:23:58 -06003354 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003355 *
Allan Stephensda827222016-11-09 14:23:58 -06003356 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003358 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3359 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003360 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003361 *
Allan Stephensda827222016-11-09 14:23:58 -06003362 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003364 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003365 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @param name Name of the memory slab.
3368 * @param slab_block_size Size of each memory block (in bytes).
3369 * @param slab_num_blocks Number memory blocks.
3370 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003371 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003372#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3373 char __noinit __aligned(slab_align) \
3374 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3375 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003376 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003377 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003378 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003380/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003381 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003384 *
Allan Stephensda827222016-11-09 14:23:58 -06003385 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3386 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3387 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3388 * To ensure that each memory block is similarly aligned to this boundary,
3389 * @a slab_block_size must also be a multiple of N.
3390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * @param slab Address of the memory slab.
3392 * @param buffer Pointer to buffer used for the memory blocks.
3393 * @param block_size Size of each memory block (in bytes).
3394 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003395 *
3396 * @return N/A
3397 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003398extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003399 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003400
3401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @param slab Address of the memory slab.
3407 * @param mem Pointer to block address area.
3408 * @param timeout Maximum time to wait for operation to complete
3409 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3410 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003411 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003412 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003414 * @retval -ENOMEM Returned without waiting.
3415 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003416 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003417extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003418 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003419
3420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * This routine releases a previously allocated memory block back to its
3424 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @param slab Address of the memory slab.
3427 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003428 *
3429 * @return N/A
3430 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003431extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003432
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * This routine gets the number of memory blocks that are currently
3437 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003439 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003442 */
Kumar Galacc334c72017-04-21 10:55:34 -05003443static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003445 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003446}
3447
Peter Mitsisc001aa82016-10-13 13:53:37 -04003448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003451 * This routine gets the number of memory blocks that are currently
3452 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003454 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003457 */
Kumar Galacc334c72017-04-21 10:55:34 -05003458static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003459{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003460 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003461}
3462
Allan Stephensc98da842016-11-11 15:45:03 -05003463/**
3464 * @} end defgroup mem_slab_apis
3465 */
3466
3467/**
3468 * @cond INTERNAL_HIDDEN
3469 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470
Andy Ross73cb9582017-05-09 10:42:39 -07003471struct k_mem_pool_lvl {
3472 union {
3473 u32_t *bits_p;
3474 u32_t bits;
3475 };
3476 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003477};
3478
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003479struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003480 void *buf;
3481 size_t max_sz;
3482 u16_t n_max;
3483 u8_t n_levels;
3484 u8_t max_inline_level;
3485 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003486 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487};
3488
Andy Ross73cb9582017-05-09 10:42:39 -07003489#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490
Andy Ross73cb9582017-05-09 10:42:39 -07003491#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3492
3493#define _MPOOL_LVLS(maxsz, minsz) \
3494 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3495 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3496 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3497 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3498 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3499 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3500 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3501 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3502 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3503 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3504 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3505 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3506 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3507 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3508 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3509 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3510
3511/* Rounds the needed bits up to integer multiples of u32_t */
3512#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3513 ((((n_max) << (2*(l))) + 31) / 32)
3514
3515/* One word gets stored free unioned with the pointer, otherwise the
3516 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003517 */
Andy Ross73cb9582017-05-09 10:42:39 -07003518#define _MPOOL_LBIT_WORDS(n_max, l) \
3519 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3520 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003521
Andy Ross73cb9582017-05-09 10:42:39 -07003522/* How many bytes for the bitfields of a single level? */
3523#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3524 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3525 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003526
Andy Ross73cb9582017-05-09 10:42:39 -07003527/* Size of the bitmap array that follows the buffer in allocated memory */
3528#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3529 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3530 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3531 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3532 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3533 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3534 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3535 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3536 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3537 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3538 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3539 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3540 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3541 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3542 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3543 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3544 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003545
3546/**
Allan Stephensc98da842016-11-11 15:45:03 -05003547 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003548 */
3549
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003550/**
Allan Stephensc98da842016-11-11 15:45:03 -05003551 * @addtogroup mem_pool_apis
3552 * @{
3553 */
3554
3555/**
3556 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3559 * long. The memory pool allows blocks to be repeatedly partitioned into
3560 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003561 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003562 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003563 * If the pool is to be accessed outside the module where it is defined, it
3564 * can be declared via
3565 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003566 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003569 * @param minsz Size of the smallest blocks in the pool (in bytes).
3570 * @param maxsz Size of the largest blocks in the pool (in bytes).
3571 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003573 */
Andy Ross73cb9582017-05-09 10:42:39 -07003574#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3575 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3576 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3577 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3578 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3579 .buf = _mpool_buf_##name, \
3580 .max_sz = maxsz, \
3581 .n_max = nmax, \
3582 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3583 .levels = _mpool_lvls_##name, \
3584 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003585
Peter Mitsis937042c2016-10-13 13:18:26 -04003586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003587 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003589 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @param pool Address of the memory pool.
3592 * @param block Pointer to block descriptor for the allocated memory.
3593 * @param size Amount of memory to allocate (in bytes).
3594 * @param timeout Maximum time to wait for operation to complete
3595 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3596 * or K_FOREVER to wait as long as necessary.
3597 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003598 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003600 * @retval -ENOMEM Returned without waiting.
3601 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003602 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003603extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003604 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003605
3606/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003609 * This routine releases a previously allocated memory block back to its
3610 * memory pool.
3611 *
3612 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003613 *
3614 * @return N/A
3615 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003617
3618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003620 *
Andy Ross73cb9582017-05-09 10:42:39 -07003621 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 *
Andy Ross73cb9582017-05-09 10:42:39 -07003623 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003624 *
3625 * @return N/A
3626 */
Andy Ross73cb9582017-05-09 10:42:39 -07003627static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003628
3629/**
Allan Stephensc98da842016-11-11 15:45:03 -05003630 * @} end addtogroup mem_pool_apis
3631 */
3632
3633/**
3634 * @defgroup heap_apis Heap Memory Pool APIs
3635 * @ingroup kernel_apis
3636 * @{
3637 */
3638
3639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003643 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003648 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003649extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003650
3651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003653 *
3654 * This routine provides traditional free() semantics. The memory being
3655 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003656 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003657 * If @a ptr is NULL, no operation is performed.
3658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003659 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003660 *
3661 * @return N/A
3662 */
3663extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664
Allan Stephensc98da842016-11-11 15:45:03 -05003665/**
3666 * @} end defgroup heap_apis
3667 */
3668
Benjamin Walshacc68c12017-01-29 18:57:45 -05003669/* polling API - PRIVATE */
3670
Benjamin Walshb0179862017-02-02 16:39:57 -05003671#ifdef CONFIG_POLL
3672#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3673#else
3674#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3675#endif
3676
Benjamin Walshacc68c12017-01-29 18:57:45 -05003677/* private - implementation data created as needed, per-type */
3678struct _poller {
3679 struct k_thread *thread;
3680};
3681
3682/* private - types bit positions */
3683enum _poll_types_bits {
3684 /* can be used to ignore an event */
3685 _POLL_TYPE_IGNORE,
3686
3687 /* to be signaled by k_poll_signal() */
3688 _POLL_TYPE_SIGNAL,
3689
3690 /* semaphore availability */
3691 _POLL_TYPE_SEM_AVAILABLE,
3692
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003693 /* queue/fifo/lifo data availability */
3694 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003695
3696 _POLL_NUM_TYPES
3697};
3698
3699#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3700
3701/* private - states bit positions */
3702enum _poll_states_bits {
3703 /* default state when creating event */
3704 _POLL_STATE_NOT_READY,
3705
Benjamin Walshacc68c12017-01-29 18:57:45 -05003706 /* signaled by k_poll_signal() */
3707 _POLL_STATE_SIGNALED,
3708
3709 /* semaphore is available */
3710 _POLL_STATE_SEM_AVAILABLE,
3711
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003712 /* data is available to read on queue/fifo/lifo */
3713 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003714
3715 _POLL_NUM_STATES
3716};
3717
3718#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3719
3720#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003721 (32 - (0 \
3722 + 8 /* tag */ \
3723 + _POLL_NUM_TYPES \
3724 + _POLL_NUM_STATES \
3725 + 1 /* modes */ \
3726 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003727
3728#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3729#error overflow of 32-bit word in struct k_poll_event
3730#endif
3731
3732/* end of polling API - PRIVATE */
3733
3734
3735/**
3736 * @defgroup poll_apis Async polling APIs
3737 * @ingroup kernel_apis
3738 * @{
3739 */
3740
3741/* Public polling API */
3742
3743/* public - values for k_poll_event.type bitfield */
3744#define K_POLL_TYPE_IGNORE 0
3745#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3746#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003747#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3748#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003749
3750/* public - polling modes */
3751enum k_poll_modes {
3752 /* polling thread does not take ownership of objects when available */
3753 K_POLL_MODE_NOTIFY_ONLY = 0,
3754
3755 K_POLL_NUM_MODES
3756};
3757
3758/* public - values for k_poll_event.state bitfield */
3759#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003760#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3761#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003762#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3763#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003764
3765/* public - poll signal object */
3766struct k_poll_signal {
3767 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003768 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003769
3770 /*
3771 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3772 * user resets it to 0.
3773 */
3774 unsigned int signaled;
3775
3776 /* custom result value passed to k_poll_signal() if needed */
3777 int result;
3778};
3779
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003780#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003781 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003782 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003783 .signaled = 0, \
3784 .result = 0, \
3785 }
3786
3787struct k_poll_event {
3788 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003789 sys_dnode_t _node;
3790
3791 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003792 struct _poller *poller;
3793
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003794 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003795 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003796
Benjamin Walshacc68c12017-01-29 18:57:45 -05003797 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003798 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003799
3800 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003801 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003802
3803 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003804 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003805
3806 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003807 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003808
3809 /* per-type data */
3810 union {
3811 void *obj;
3812 struct k_poll_signal *signal;
3813 struct k_sem *sem;
3814 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003815 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003816 };
3817};
3818
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003819#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820 { \
3821 .poller = NULL, \
3822 .type = event_type, \
3823 .state = K_POLL_STATE_NOT_READY, \
3824 .mode = event_mode, \
3825 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003826 { .obj = event_obj }, \
3827 }
3828
3829#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3830 event_tag) \
3831 { \
3832 .type = event_type, \
3833 .tag = event_tag, \
3834 .state = K_POLL_STATE_NOT_READY, \
3835 .mode = event_mode, \
3836 .unused = 0, \
3837 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 }
3839
3840/**
3841 * @brief Initialize one struct k_poll_event instance
3842 *
3843 * After this routine is called on a poll event, the event it ready to be
3844 * placed in an event array to be passed to k_poll().
3845 *
3846 * @param event The event to initialize.
3847 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3848 * values. Only values that apply to the same object being polled
3849 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3850 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003851 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003852 * @param obj Kernel object or poll signal.
3853 *
3854 * @return N/A
3855 */
3856
Kumar Galacc334c72017-04-21 10:55:34 -05003857extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858 int mode, void *obj);
3859
3860/**
3861 * @brief Wait for one or many of multiple poll events to occur
3862 *
3863 * This routine allows a thread to wait concurrently for one or many of
3864 * multiple poll events to have occurred. Such events can be a kernel object
3865 * being available, like a semaphore, or a poll signal event.
3866 *
3867 * When an event notifies that a kernel object is available, the kernel object
3868 * is not "given" to the thread calling k_poll(): it merely signals the fact
3869 * that the object was available when the k_poll() call was in effect. Also,
3870 * all threads trying to acquire an object the regular way, i.e. by pending on
3871 * the object, have precedence over the thread polling on the object. This
3872 * means that the polling thread will never get the poll event on an object
3873 * until the object becomes available and its pend queue is empty. For this
3874 * reason, the k_poll() call is more effective when the objects being polled
3875 * only have one thread, the polling thread, trying to acquire them.
3876 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003877 * When k_poll() returns 0, the caller should loop on all the events that were
3878 * passed to k_poll() and check the state field for the values that were
3879 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003880 *
3881 * Before being reused for another call to k_poll(), the user has to reset the
3882 * state field to K_POLL_STATE_NOT_READY.
3883 *
3884 * @param events An array of pointers to events to be polled for.
3885 * @param num_events The number of events in the array.
3886 * @param timeout Waiting period for an event to be ready (in milliseconds),
3887 * or one of the special values K_NO_WAIT and K_FOREVER.
3888 *
3889 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003890 * @retval -EAGAIN Waiting period timed out.
3891 */
3892
3893extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003894 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003895
3896/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003897 * @brief Initialize a poll signal object.
3898 *
3899 * Ready a poll signal object to be signaled via k_poll_signal().
3900 *
3901 * @param signal A poll signal.
3902 *
3903 * @return N/A
3904 */
3905
3906extern void k_poll_signal_init(struct k_poll_signal *signal);
3907
3908/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003909 * @brief Signal a poll signal object.
3910 *
3911 * This routine makes ready a poll signal, which is basically a poll event of
3912 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3913 * made ready to run. A @a result value can be specified.
3914 *
3915 * The poll signal contains a 'signaled' field that, when set by
3916 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3917 * be reset by the user before being passed again to k_poll() or k_poll() will
3918 * consider it being signaled, and will return immediately.
3919 *
3920 * @param signal A poll signal.
3921 * @param result The value to store in the result field of the signal.
3922 *
3923 * @retval 0 The signal was delivered successfully.
3924 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3925 */
3926
3927extern int k_poll_signal(struct k_poll_signal *signal, int result);
3928
3929/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003930extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003931
3932/**
3933 * @} end defgroup poll_apis
3934 */
3935
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003936/**
3937 * @brief Make the CPU idle.
3938 *
3939 * This function makes the CPU idle until an event wakes it up.
3940 *
3941 * In a regular system, the idle thread should be the only thread responsible
3942 * for making the CPU idle and triggering any type of power management.
3943 * However, in some more constrained systems, such as a single-threaded system,
3944 * the only thread would be responsible for this if needed.
3945 *
3946 * @return N/A
3947 */
3948extern void k_cpu_idle(void);
3949
3950/**
3951 * @brief Make the CPU idle in an atomic fashion.
3952 *
3953 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3954 * must be done atomically before making the CPU idle.
3955 *
3956 * @param key Interrupt locking key obtained from irq_lock().
3957 *
3958 * @return N/A
3959 */
3960extern void k_cpu_atomic_idle(unsigned int key);
3961
Kumar Galacc334c72017-04-21 10:55:34 -05003962extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003963
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964#include <arch/cpu.h>
3965
Andrew Boiecdb94d62017-04-18 15:22:05 -07003966#ifdef _ARCH_EXCEPT
3967/* This archtecture has direct support for triggering a CPU exception */
3968#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3969#else
3970
3971#include <misc/printk.h>
3972
3973/* NOTE: This is the implementation for arches that do not implement
3974 * _ARCH_EXCEPT() to generate a real CPU exception.
3975 *
3976 * We won't have a real exception frame to determine the PC value when
3977 * the oops occurred, so print file and line number before we jump into
3978 * the fatal error handler.
3979 */
3980#define _k_except_reason(reason) do { \
3981 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3982 _NanoFatalErrorHandler(reason, &_default_esf); \
3983 CODE_UNREACHABLE; \
3984 } while (0)
3985
3986#endif /* _ARCH__EXCEPT */
3987
3988/**
3989 * @brief Fatally terminate a thread
3990 *
3991 * This should be called when a thread has encountered an unrecoverable
3992 * runtime condition and needs to terminate. What this ultimately
3993 * means is determined by the _fatal_error_handler() implementation, which
3994 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3995 *
3996 * If this is called from ISR context, the default system fatal error handler
3997 * will treat it as an unrecoverable system error, just like k_panic().
3998 */
3999#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4000
4001/**
4002 * @brief Fatally terminate the system
4003 *
4004 * This should be called when the Zephyr kernel has encountered an
4005 * unrecoverable runtime condition and needs to terminate. What this ultimately
4006 * means is determined by the _fatal_error_handler() implementation, which
4007 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4008 */
4009#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4010
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004011/*
4012 * private APIs that are utilized by one or more public APIs
4013 */
4014
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004015#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004016extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004017#else
4018#define _init_static_threads() do { } while ((0))
4019#endif
4020
4021extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004022extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004023
Andrew Boiedc5d9352017-06-02 12:56:47 -07004024/* arch/cpu.h may declare an architecture or platform-specific macro
4025 * for properly declaring stacks, compatible with MMU/MPU constraints if
4026 * enabled
4027 */
4028#ifdef _ARCH_THREAD_STACK_DEFINE
4029#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4030#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4031 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4032#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4033#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004034static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4035{
4036 return _ARCH_THREAD_STACK_BUFFER(sym);
4037}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004038#else
4039/**
4040 * @brief Declare a toplevel thread stack memory region
4041 *
4042 * This declares a region of memory suitable for use as a thread's stack.
4043 *
4044 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4045 * 'noinit' section so that it isn't zeroed at boot
4046 *
Andrew Boie507852a2017-07-25 18:47:07 -07004047 * The declared symbol will always be a k_thread_stack_t which can be passed to
4048 * k_thread_create, but should otherwise not be manipulated. If the buffer
4049 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004050 *
4051 * It is legal to precede this definition with the 'static' keyword.
4052 *
4053 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4054 * parameter of k_thread_create(), it may not be the same as the
4055 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4056 *
4057 * @param sym Thread stack symbol name
4058 * @param size Size of the stack memory region
4059 */
4060#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004061 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004062
4063/**
4064 * @brief Declare a toplevel array of thread stack memory regions
4065 *
4066 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4067 * definition for additional details and constraints.
4068 *
4069 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4070 * 'noinit' section so that it isn't zeroed at boot
4071 *
4072 * @param sym Thread stack symbol name
4073 * @param nmemb Number of stacks to declare
4074 * @param size Size of the stack memory region
4075 */
4076
4077#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004078 struct _k_thread_stack_element __noinit \
4079 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004080
4081/**
4082 * @brief Declare an embedded stack memory region
4083 *
4084 * Used for stacks embedded within other data structures. Use is highly
4085 * discouraged but in some cases necessary. For memory protection scenarios,
4086 * it is very important that any RAM preceding this member not be writable
4087 * by threads else a stack overflow will lead to silent corruption. In other
4088 * words, the containing data structure should live in RAM owned by the kernel.
4089 *
4090 * @param sym Thread stack symbol name
4091 * @param size Size of the stack memory region
4092 */
4093#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004094 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004095
4096/**
4097 * @brief Return the size in bytes of a stack memory region
4098 *
4099 * Convenience macro for passing the desired stack size to k_thread_create()
4100 * since the underlying implementation may actually create something larger
4101 * (for instance a guard area).
4102 *
4103 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004104 * passed to K_THREAD_STACK_DEFINE.
4105 *
4106 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4107 * it is not guaranteed to return the original value since each array
4108 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004109 *
4110 * @param sym Stack memory symbol
4111 * @return Size of the stack
4112 */
4113#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4114
4115/**
4116 * @brief Get a pointer to the physical stack buffer
4117 *
4118 * Convenience macro to get at the real underlying stack buffer used by
4119 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4120 * This is really only intended for diagnostic tools which want to examine
4121 * stack memory contents.
4122 *
4123 * @param sym Declared stack symbol name
4124 * @return The buffer itself, a char *
4125 */
Andrew Boie507852a2017-07-25 18:47:07 -07004126static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4127{
4128 return (char *)sym;
4129}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004130
4131#endif /* _ARCH_DECLARE_STACK */
4132
Chunlin Hane9c97022017-07-07 20:29:30 +08004133/**
4134 * @defgroup mem_domain_apis Memory domain APIs
4135 * @ingroup kernel_apis
4136 * @{
4137 */
4138
4139/** @def MEM_PARTITION_ENTRY
4140 * @brief Used to declare a memory partition entry
4141 */
4142#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4143 {\
4144 .start = _start, \
4145 .size = _size, \
4146 .attr = _attr, \
4147 }
4148
4149/** @def K_MEM_PARTITION_DEFINE
4150 * @brief Used to declare a memory partition
4151 */
4152#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4153#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4154 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4155 struct k_mem_partition name = \
4156 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4157#else
4158#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4159 struct k_mem_partition name = \
4160 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4161#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4162
4163
4164/* memory partition */
4165struct k_mem_partition {
4166 /* start address of memory partition */
4167 u32_t start;
4168 /* size of memory partition */
4169 u32_t size;
4170 /* attribute of memory partition */
4171 u32_t attr;
4172};
4173
4174#if defined(CONFIG_USERSPACE)
4175/* memory domian */
4176struct k_mem_domain {
4177 /* number of partitions in the domain */
4178 u32_t num_partitions;
4179 /* partitions in the domain */
4180 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4181 /* domain q */
4182 sys_dlist_t mem_domain_q;
4183};
4184#endif /* CONFIG_USERSPACE */
4185
4186/**
4187 * @brief Initialize a memory domain.
4188 *
4189 * Initialize a memory domain with given name and memory partitions.
4190 *
4191 * @param domain The memory domain to be initialized.
4192 * @param num_parts The number of array items of "parts" parameter.
4193 * @param parts An array of pointers to the memory partitions. Can be NULL
4194 * if num_parts is zero.
4195 */
4196
4197extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4198 struct k_mem_partition *parts[]);
4199/**
4200 * @brief Destroy a memory domain.
4201 *
4202 * Destroy a memory domain.
4203 *
4204 * @param domain The memory domain to be destroyed.
4205 */
4206
4207extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4208
4209/**
4210 * @brief Add a memory partition into a memory domain.
4211 *
4212 * Add a memory partition into a memory domain.
4213 *
4214 * @param domain The memory domain to be added a memory partition.
4215 * @param part The memory partition to be added
4216 */
4217
4218extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4219 struct k_mem_partition *part);
4220
4221/**
4222 * @brief Remove a memory partition from a memory domain.
4223 *
4224 * Remove a memory partition from a memory domain.
4225 *
4226 * @param domain The memory domain to be removed a memory partition.
4227 * @param part The memory partition to be removed
4228 */
4229
4230extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4231 struct k_mem_partition *part);
4232
4233/**
4234 * @brief Add a thread into a memory domain.
4235 *
4236 * Add a thread into a memory domain.
4237 *
4238 * @param domain The memory domain that the thread is going to be added into.
4239 * @param thread ID of thread going to be added into the memory domain.
4240 */
4241
4242extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4243 k_tid_t thread);
4244
4245/**
4246 * @brief Remove a thread from its memory domain.
4247 *
4248 * Remove a thread from its memory domain.
4249 *
4250 * @param thread ID of thread going to be removed from its memory domain.
4251 */
4252
4253extern void k_mem_domain_remove_thread(k_tid_t thread);
4254
4255/**
4256 * @} end defgroup mem_domain_apis
4257 */
4258
Andrew Boie756f9072017-10-10 16:01:49 -07004259/**
4260 * @brief Emit a character buffer to the console device
4261 *
4262 * @param c String of characters to print
4263 * @param n The length of the string
4264 */
4265__syscall void k_str_out(char *c, size_t n);
4266
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004267#ifdef __cplusplus
4268}
4269#endif
4270
Andrew Boiee004dec2016-11-07 09:01:19 -08004271#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4272/*
4273 * Define new and delete operators.
4274 * At this moment, the operators do nothing since objects are supposed
4275 * to be statically allocated.
4276 */
4277inline void operator delete(void *ptr)
4278{
4279 (void)ptr;
4280}
4281
4282inline void operator delete[](void *ptr)
4283{
4284 (void)ptr;
4285}
4286
4287inline void *operator new(size_t size)
4288{
4289 (void)size;
4290 return NULL;
4291}
4292
4293inline void *operator new[](size_t size)
4294{
4295 (void)size;
4296 return NULL;
4297}
4298
4299/* Placement versions of operator new and delete */
4300inline void operator delete(void *ptr1, void *ptr2)
4301{
4302 (void)ptr1;
4303 (void)ptr2;
4304}
4305
4306inline void operator delete[](void *ptr1, void *ptr2)
4307{
4308 (void)ptr1;
4309 (void)ptr2;
4310}
4311
4312inline void *operator new(size_t size, void *ptr)
4313{
4314 (void)size;
4315 return ptr;
4316}
4317
4318inline void *operator new[](size_t size, void *ptr)
4319{
4320 (void)size;
4321 return ptr;
4322}
4323
4324#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4325
Andrew Boiefa94ee72017-09-28 16:54:35 -07004326#include <syscalls/kernel.h>
4327
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004328#endif /* !_ASMLANGUAGE */
4329
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004330#endif /* _kernel__h_ */