blob: 8b7e230572b768745e74888588f887367d88d0dc [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;
177 char perms[CONFIG_MAX_THREAD_BYTES];
178 char type;
179 char flags;
180} __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
207static inline void _impl_k_object_access_all_grant(void *object)
208{
209 ARG_UNUSED(object);
210}
211#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700212
213/**
214 * grant a thread access to a kernel object
215 *
216 * The thread will be granted access to the object if the caller is from
217 * supervisor mode, or the caller is from user mode AND has permissions
218 * on the object already.
219 *
220 * @param object Address of kernel object
221 * @param thread Thread to grant access to the object
222 */
Andrew Boie743e4682017-10-04 12:25:50 -0700223__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700224
Andrew Boie3b5ae802017-10-04 12:10:32 -0700225
226/**
227 * grant all present and future threads access to an object
228 *
229 * If the caller is from supervisor mode, or the caller is from user mode and
230 * have sufficient permissions on the object, then that object will have
231 * permissions granted to it for *all* current and future threads running in
232 * the system, effectively becoming a public kernel object.
233 *
234 * Use of this API should be avoided on systems that are running untrusted code
235 * as it is possible for such code to derive the addresses of kernel objects
236 * and perform unwanted operations on them.
237 *
238 * @param object Address of kernel object
239 */
Andrew Boie743e4682017-10-04 12:25:50 -0700240__syscall void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700241
Andrew Boie73abd322017-04-04 13:19:13 -0700242/* timeouts */
243
244struct _timeout;
245typedef void (*_timeout_func_t)(struct _timeout *t);
246
247struct _timeout {
248 sys_dnode_t node;
249 struct k_thread *thread;
250 sys_dlist_t *wait_q;
251 s32_t delta_ticks_from_prev;
252 _timeout_func_t func;
253};
254
255extern s32_t _timeout_remaining_get(struct _timeout *timeout);
256
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700257/**
258 * @typedef k_thread_entry_t
259 * @brief Thread entry point function type.
260 *
261 * A thread's entry point function is invoked when the thread starts executing.
262 * Up to 3 argument values can be passed to the function.
263 *
264 * The thread terminates execution permanently if the entry point function
265 * returns. The thread is responsible for releasing any shared resources
266 * it may own (such as mutexes and dynamically allocated memory), prior to
267 * returning.
268 *
269 * @param p1 First argument.
270 * @param p2 Second argument.
271 * @param p3 Third argument.
272 *
273 * @return N/A
274 */
275typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700276
277#ifdef CONFIG_THREAD_MONITOR
278struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700279 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700280 void *parameter1;
281 void *parameter2;
282 void *parameter3;
283};
284#endif
285
286/* can be used for creating 'dummy' threads, e.g. for pending on objects */
287struct _thread_base {
288
289 /* this thread's entry in a ready/wait queue */
290 sys_dnode_t k_q_node;
291
292 /* user facing 'thread options'; values defined in include/kernel.h */
293 u8_t user_options;
294
295 /* thread state */
296 u8_t thread_state;
297
298 /*
299 * scheduler lock count and thread priority
300 *
301 * These two fields control the preemptibility of a thread.
302 *
303 * When the scheduler is locked, sched_locked is decremented, which
304 * means that the scheduler is locked for values from 0xff to 0x01. A
305 * thread is coop if its prio is negative, thus 0x80 to 0xff when
306 * looked at the value as unsigned.
307 *
308 * By putting them end-to-end, this means that a thread is
309 * non-preemptible if the bundled value is greater than or equal to
310 * 0x0080.
311 */
312 union {
313 struct {
314#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
315 u8_t sched_locked;
316 s8_t prio;
317#else /* LITTLE and PDP */
318 s8_t prio;
319 u8_t sched_locked;
320#endif
321 };
322 u16_t preempt;
323 };
324
325 /* data returned by APIs */
326 void *swap_data;
327
328#ifdef CONFIG_SYS_CLOCK_EXISTS
329 /* this thread's entry in a timeout queue */
330 struct _timeout timeout;
331#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700332
333#ifdef CONFIG_USERSPACE
334 /* Bit position in kernel object permissions bitfield for this thread */
335 unsigned int perm_index;
336#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700337};
338
339typedef struct _thread_base _thread_base_t;
340
341#if defined(CONFIG_THREAD_STACK_INFO)
342/* Contains the stack information of a thread */
343struct _thread_stack_info {
344 /* Stack Start */
345 u32_t start;
346 /* Stack Size */
347 u32_t size;
348};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700349
350typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700351#endif /* CONFIG_THREAD_STACK_INFO */
352
Chunlin Hane9c97022017-07-07 20:29:30 +0800353#if defined(CONFIG_USERSPACE)
354struct _mem_domain_info {
355 /* memory domain queue node */
356 sys_dnode_t mem_domain_q_node;
357 /* memory domain of the thread */
358 struct k_mem_domain *mem_domain;
359};
360
361#endif /* CONFIG_USERSPACE */
362
Andrew Boie73abd322017-04-04 13:19:13 -0700363struct k_thread {
364
365 struct _thread_base base;
366
367 /* defined by the architecture, but all archs need these */
368 struct _caller_saved caller_saved;
369 struct _callee_saved callee_saved;
370
371 /* static thread init data */
372 void *init_data;
373
374 /* abort function */
375 void (*fn_abort)(void);
376
377#if defined(CONFIG_THREAD_MONITOR)
378 /* thread entry and parameters description */
379 struct __thread_entry *entry;
380
381 /* next item in list of all threads */
382 struct k_thread *next_thread;
383#endif
384
385#ifdef CONFIG_THREAD_CUSTOM_DATA
386 /* crude thread-local storage */
387 void *custom_data;
388#endif
389
390#ifdef CONFIG_ERRNO
391 /* per-thread errno variable */
392 int errno_var;
393#endif
394
395#if defined(CONFIG_THREAD_STACK_INFO)
396 /* Stack Info */
397 struct _thread_stack_info stack_info;
398#endif /* CONFIG_THREAD_STACK_INFO */
399
Chunlin Hane9c97022017-07-07 20:29:30 +0800400#if defined(CONFIG_USERSPACE)
401 /* memory domain info of the thread */
402 struct _mem_domain_info mem_domain_info;
403#endif /* CONFIG_USERSPACE */
404
Andrew Boie73abd322017-04-04 13:19:13 -0700405 /* arch-specifics: must always be at the end */
406 struct _thread_arch arch;
407};
408
409typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400410typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700411#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400412
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400413enum execution_context_types {
414 K_ISR = 0,
415 K_COOP_THREAD,
416 K_PREEMPT_THREAD,
417};
418
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400419/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100420 * @defgroup profiling_apis Profiling APIs
421 * @ingroup kernel_apis
422 * @{
423 */
424
425/**
426 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
427 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700428 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100429 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
430 *
431 * CONFIG_MAIN_STACK_SIZE
432 * CONFIG_IDLE_STACK_SIZE
433 * CONFIG_ISR_STACK_SIZE
434 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
435 *
436 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
437 * produce output.
438 *
439 * @return N/A
440 */
441extern void k_call_stacks_analyze(void);
442
443/**
444 * @} end defgroup profiling_apis
445 */
446
447/**
Allan Stephensc98da842016-11-11 15:45:03 -0500448 * @defgroup thread_apis Thread APIs
449 * @ingroup kernel_apis
450 * @{
451 */
452
Benjamin Walshed240f22017-01-22 13:05:08 -0500453#endif /* !_ASMLANGUAGE */
454
455
456/*
457 * Thread user options. May be needed by assembly code. Common part uses low
458 * bits, arch-specific use high bits.
459 */
460
461/* system thread that must not abort */
462#define K_ESSENTIAL (1 << 0)
463
464#if defined(CONFIG_FP_SHARING)
465/* thread uses floating point registers */
466#define K_FP_REGS (1 << 1)
467#endif
468
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700469/* This thread has dropped from supervisor mode to user mode and consequently
470 * has additional restrictions
471 */
472#define K_USER (1 << 2)
473
Benjamin Walshed240f22017-01-22 13:05:08 -0500474#ifdef CONFIG_X86
475/* x86 Bitmask definitions for threads user options */
476
477#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
478/* thread uses SSEx (and also FP) registers */
479#define K_SSE_REGS (1 << 7)
480#endif
481#endif
482
483/* end - thread options */
484
485#if !defined(_ASMLANGUAGE)
486
Andrew Boie507852a2017-07-25 18:47:07 -0700487/* Using typedef deliberately here, this is quite intended to be an opaque
488 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
489 *
490 * The purpose of this data type is to clearly distinguish between the
491 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
492 * buffer which composes the stack data actually used by the underlying
493 * thread; they cannot be used interchangably as some arches precede the
494 * stack buffer region with guard areas that trigger a MPU or MMU fault
495 * if written to.
496 *
497 * APIs that want to work with the buffer inside should continue to use
498 * char *.
499 *
500 * Stacks should always be created with K_THREAD_STACK_DEFINE().
501 */
502struct __packed _k_thread_stack_element {
503 char data;
504};
505typedef struct _k_thread_stack_element *k_thread_stack_t;
506
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400507
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700509 * @brief Create a thread.
510 *
511 * This routine initializes a thread, then schedules it for execution.
512 *
513 * The new thread may be scheduled for immediate execution or a delayed start.
514 * If the newly spawned thread does not have a delayed start the kernel
515 * scheduler may preempt the current thread to allow the new thread to
516 * execute.
517 *
518 * Thread options are architecture-specific, and can include K_ESSENTIAL,
519 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
520 * them using "|" (the logical OR operator).
521 *
522 * Historically, users often would use the beginning of the stack memory region
523 * to store the struct k_thread data, although corruption will occur if the
524 * stack overflows this region and stack protection features may not detect this
525 * situation.
526 *
527 * @param new_thread Pointer to uninitialized struct k_thread
528 * @param stack Pointer to the stack space.
529 * @param stack_size Stack size in bytes.
530 * @param entry Thread entry function.
531 * @param p1 1st entry point parameter.
532 * @param p2 2nd entry point parameter.
533 * @param p3 3rd entry point parameter.
534 * @param prio Thread priority.
535 * @param options Thread options.
536 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
537 *
538 * @return ID of new thread.
539 */
Andrew Boie507852a2017-07-25 18:47:07 -0700540extern k_tid_t k_thread_create(struct k_thread *new_thread,
541 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700542 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700543 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700544 void *p1, void *p2, void *p3,
545 int prio, u32_t options, s32_t delay);
546
Andrew Boie3f091b52017-08-30 14:34:14 -0700547/**
548 * @brief Drop a thread's privileges permanently to user mode
549 *
550 * @param entry Function to start executing from
551 * @param p1 1st entry point parameter
552 * @param p2 2nd entry point parameter
553 * @param p3 3rd entry point parameter
554 */
555extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
556 void *p1, void *p2,
557 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700558
Andrew Boied26cf2d2017-03-30 13:07:02 -0700559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561 *
Allan Stephensc98da842016-11-11 15:45:03 -0500562 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500563 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500565 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400566 *
567 * @return N/A
568 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700569__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400570
571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 *
574 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577 * @return N/A
578 */
Kumar Galacc334c72017-04-21 10:55:34 -0500579extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580
581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500586 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400587 *
588 * @return N/A
589 */
Andrew Boie468190a2017-09-29 14:00:48 -0700590__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591
592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * If @a thread is not currently sleeping, the routine has no effect.
598 *
599 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600 *
601 * @return N/A
602 */
Andrew Boie468190a2017-09-29 14:00:48 -0700603__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604
605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500608 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700610__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400611
612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500615 * This routine prevents @a thread from executing if it has not yet started
616 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500618 * @param thread ID of thread to cancel.
619 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700620 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500621 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622 */
Andrew Boie468190a2017-09-29 14:00:48 -0700623__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400624
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625/**
Allan Stephensc98da842016-11-11 15:45:03 -0500626 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500628 * This routine permanently stops execution of @a thread. The thread is taken
629 * off all kernel queues it is part of (i.e. the ready queue, the timeout
630 * queue, or a kernel object wait queue). However, any kernel resources the
631 * thread might currently own (such as mutexes or memory blocks) are not
632 * released. It is the responsibility of the caller of this routine to ensure
633 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500635 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 *
637 * @return N/A
638 */
Andrew Boie468190a2017-09-29 14:00:48 -0700639__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400640
Andrew Boie7d627c52017-08-30 11:01:56 -0700641
642/**
643 * @brief Start an inactive thread
644 *
645 * If a thread was created with K_FOREVER in the delay parameter, it will
646 * not be added to the scheduling queue until this function is called
647 * on it.
648 *
649 * @param thread thread to start
650 */
Andrew Boie468190a2017-09-29 14:00:48 -0700651__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700652
Allan Stephensc98da842016-11-11 15:45:03 -0500653/**
654 * @cond INTERNAL_HIDDEN
655 */
656
Benjamin Walshd211a522016-12-06 11:44:01 -0500657/* timeout has timed out and is not on _timeout_q anymore */
658#define _EXPIRED (-2)
659
660/* timeout is not in use */
661#define _INACTIVE (-1)
662
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400663struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700664 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700665 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400666 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700667 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500668 void *init_p1;
669 void *init_p2;
670 void *init_p3;
671 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500672 u32_t init_options;
673 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500674 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500675 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400676};
677
Andrew Boied26cf2d2017-03-30 13:07:02 -0700678#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400679 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500680 prio, options, delay, abort, groups) \
681 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700682 .init_thread = (thread), \
683 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500684 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700685 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400686 .init_p1 = (void *)p1, \
687 .init_p2 = (void *)p2, \
688 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500689 .init_prio = (prio), \
690 .init_options = (options), \
691 .init_delay = (delay), \
692 .init_abort = (abort), \
693 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694 }
695
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400696/**
Allan Stephensc98da842016-11-11 15:45:03 -0500697 * INTERNAL_HIDDEN @endcond
698 */
699
700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * @brief Statically define and initialize a thread.
702 *
703 * The thread may be scheduled for immediate execution or a delayed start.
704 *
705 * Thread options are architecture-specific, and can include K_ESSENTIAL,
706 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
707 * them using "|" (the logical OR operator).
708 *
709 * The ID of the thread can be accessed using:
710 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500711 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500712 *
713 * @param name Name of the thread.
714 * @param stack_size Stack size in bytes.
715 * @param entry Thread entry function.
716 * @param p1 1st entry point parameter.
717 * @param p2 2nd entry point parameter.
718 * @param p3 3rd entry point parameter.
719 * @param prio Thread priority.
720 * @param options Thread options.
721 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400722 *
723 * @internal It has been observed that the x86 compiler by default aligns
724 * these _static_thread_data structures to 32-byte boundaries, thereby
725 * wasting space. To work around this, force a 4-byte alignment.
726 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500727#define K_THREAD_DEFINE(name, stack_size, \
728 entry, p1, p2, p3, \
729 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700730 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700731 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500732 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500733 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700734 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
735 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500736 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700737 NULL, 0); \
738 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400739
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500743 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500745 * @param thread ID of thread whose priority is needed.
746 *
747 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700749__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750
751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500754 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 *
756 * Rescheduling can occur immediately depending on the priority @a thread is
757 * set to:
758 *
759 * - If its priority is raised above the priority of the caller of this
760 * function, and the caller is preemptible, @a thread will be scheduled in.
761 *
762 * - If the caller operates on itself, it lowers its priority below that of
763 * other threads in the system, and the caller is preemptible, the thread of
764 * highest priority will be scheduled in.
765 *
766 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
767 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
768 * highest priority.
769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 * @param prio New priority.
772 *
773 * @warning Changing the priority of a thread currently involved in mutex
774 * priority inheritance may result in undefined behavior.
775 *
776 * @return N/A
777 */
Andrew Boie468190a2017-09-29 14:00:48 -0700778__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400779
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * This routine prevents the kernel scheduler from making @a thread the
784 * current thread. All other internal operations on @a thread are still
785 * performed; for example, any timeout it is waiting on keeps ticking,
786 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * If @a thread is already suspended, the routine has no effect.
789 *
790 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
792 * @return N/A
793 */
Andrew Boie468190a2017-09-29 14:00:48 -0700794__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795
796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * This routine allows the kernel scheduler to make @a thread the current
800 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * If @a thread is not currently suspended, the routine has no effect.
803 *
804 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
806 * @return N/A
807 */
Andrew Boie468190a2017-09-29 14:00:48 -0700808__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400809
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine specifies how the scheduler will perform time slicing of
814 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * To enable time slicing, @a slice must be non-zero. The scheduler
817 * ensures that no thread runs for more than the specified time limit
818 * before other threads of that priority are given a chance to execute.
819 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700820 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 * execute. Once the scheduler selects a thread for execution, there is no
824 * minimum guaranteed time the thread will execute before threads of greater or
825 * equal priority are scheduled.
826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 * for execution, this routine has no effect; the thread is immediately
829 * rescheduled after the slice period expires.
830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * To disable timeslicing, set both @a slice and @a prio to zero.
832 *
833 * @param slice Maximum time slice length (in milliseconds).
834 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
836 * @return N/A
837 */
Kumar Galacc334c72017-04-21 10:55:34 -0500838extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400839
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840/**
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * @} end defgroup thread_apis
842 */
843
844/**
845 * @addtogroup isr_apis
846 * @{
847 */
848
849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephensc98da842016-11-11 15:45:03 -0500852 * This routine allows the caller to customize its actions, depending on
853 * whether it is a thread or an ISR.
854 *
855 * @note Can be called by ISRs.
856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @return 0 if invoked by a thread.
858 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500860extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400861
Benjamin Walsh445830d2016-11-10 15:54:27 -0500862/**
863 * @brief Determine if code is running in a preemptible thread.
864 *
Allan Stephensc98da842016-11-11 15:45:03 -0500865 * This routine allows the caller to customize its actions, depending on
866 * whether it can be preempted by another thread. The routine returns a 'true'
867 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500868 *
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * - The code is running in a thread, not at ISR.
870 * - The thread's priority is in the preemptible range.
871 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500872 *
Allan Stephensc98da842016-11-11 15:45:03 -0500873 * @note Can be called by ISRs.
874 *
875 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500876 * @return Non-zero if invoked by a preemptible thread.
877 */
Andrew Boie468190a2017-09-29 14:00:48 -0700878__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500879
Allan Stephensc98da842016-11-11 15:45:03 -0500880/**
881 * @} end addtogroup isr_apis
882 */
883
884/**
885 * @addtogroup thread_apis
886 * @{
887 */
888
889/**
890 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500891 *
Allan Stephensc98da842016-11-11 15:45:03 -0500892 * This routine prevents the current thread from being preempted by another
893 * thread by instructing the scheduler to treat it as a cooperative thread.
894 * If the thread subsequently performs an operation that makes it unready,
895 * it will be context switched out in the normal manner. When the thread
896 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500897 *
Allan Stephensc98da842016-11-11 15:45:03 -0500898 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500899 *
Allan Stephensc98da842016-11-11 15:45:03 -0500900 * @note k_sched_lock() and k_sched_unlock() should normally be used
901 * when the operation being performed can be safely interrupted by ISRs.
902 * However, if the amount of processing involved is very small, better
903 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500904 *
905 * @return N/A
906 */
907extern void k_sched_lock(void);
908
Allan Stephensc98da842016-11-11 15:45:03 -0500909/**
910 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500911 *
Allan Stephensc98da842016-11-11 15:45:03 -0500912 * This routine reverses the effect of a previous call to k_sched_lock().
913 * A thread must call the routine once for each time it called k_sched_lock()
914 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500915 *
916 * @return N/A
917 */
918extern void k_sched_unlock(void);
919
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * Custom data is not used by the kernel itself, and is freely available
926 * for a thread to use as it sees fit. It can be used as a framework
927 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400930 *
931 * @return N/A
932 */
Andrew Boie468190a2017-09-29 14:00:48 -0700933__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934
935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500936 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500940 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400941 */
Andrew Boie468190a2017-09-29 14:00:48 -0700942__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943
944/**
Allan Stephensc98da842016-11-11 15:45:03 -0500945 * @} end addtogroup thread_apis
946 */
947
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400948#include <sys_clock.h>
949
Allan Stephensc2f15a42016-11-17 12:24:22 -0500950/**
951 * @addtogroup clock_apis
952 * @{
953 */
954
955/**
956 * @brief Generate null timeout delay.
957 *
958 * This macro generates a timeout delay that that instructs a kernel API
959 * not to wait if the requested operation cannot be performed immediately.
960 *
961 * @return Timeout delay value.
962 */
963#define K_NO_WAIT 0
964
965/**
966 * @brief Generate timeout delay from milliseconds.
967 *
968 * This macro generates a timeout delay that that instructs a kernel API
969 * to wait up to @a ms milliseconds to perform the requested operation.
970 *
971 * @param ms Duration in milliseconds.
972 *
973 * @return Timeout delay value.
974 */
Johan Hedberg14471692016-11-13 10:52:15 +0200975#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500976
977/**
978 * @brief Generate timeout delay from seconds.
979 *
980 * This macro generates a timeout delay that that instructs a kernel API
981 * to wait up to @a s seconds to perform the requested operation.
982 *
983 * @param s Duration in seconds.
984 *
985 * @return Timeout delay value.
986 */
Johan Hedberg14471692016-11-13 10:52:15 +0200987#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500988
989/**
990 * @brief Generate timeout delay from minutes.
991 *
992 * This macro generates a timeout delay that that instructs a kernel API
993 * to wait up to @a m minutes to perform the requested operation.
994 *
995 * @param m Duration in minutes.
996 *
997 * @return Timeout delay value.
998 */
Johan Hedberg14471692016-11-13 10:52:15 +0200999#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001000
1001/**
1002 * @brief Generate timeout delay from hours.
1003 *
1004 * This macro generates a timeout delay that that instructs a kernel API
1005 * to wait up to @a h hours to perform the requested operation.
1006 *
1007 * @param h Duration in hours.
1008 *
1009 * @return Timeout delay value.
1010 */
Johan Hedberg14471692016-11-13 10:52:15 +02001011#define K_HOURS(h) K_MINUTES((h) * 60)
1012
Allan Stephensc98da842016-11-11 15:45:03 -05001013/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001014 * @brief Generate infinite timeout delay.
1015 *
1016 * This macro generates a timeout delay that that instructs a kernel API
1017 * to wait as long as necessary to perform the requested operation.
1018 *
1019 * @return Timeout delay value.
1020 */
1021#define K_FOREVER (-1)
1022
1023/**
1024 * @} end addtogroup clock_apis
1025 */
1026
1027/**
Allan Stephensc98da842016-11-11 15:45:03 -05001028 * @cond INTERNAL_HIDDEN
1029 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001030
Benjamin Walsh62092182016-12-20 14:39:08 -05001031/* kernel clocks */
1032
1033#if (sys_clock_ticks_per_sec == 1000) || \
1034 (sys_clock_ticks_per_sec == 500) || \
1035 (sys_clock_ticks_per_sec == 250) || \
1036 (sys_clock_ticks_per_sec == 125) || \
1037 (sys_clock_ticks_per_sec == 100) || \
1038 (sys_clock_ticks_per_sec == 50) || \
1039 (sys_clock_ticks_per_sec == 25) || \
1040 (sys_clock_ticks_per_sec == 20) || \
1041 (sys_clock_ticks_per_sec == 10) || \
1042 (sys_clock_ticks_per_sec == 1)
1043
1044 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1045#else
1046 /* yields horrible 64-bit math on many architectures: try to avoid */
1047 #define _NON_OPTIMIZED_TICKS_PER_SEC
1048#endif
1049
1050#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001051extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001052#else
Kumar Galacc334c72017-04-21 10:55:34 -05001053static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001054{
Kumar Galacc334c72017-04-21 10:55:34 -05001055 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001056}
1057#endif
1058
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001059/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001060#ifdef CONFIG_TICKLESS_KERNEL
1061#define _TICK_ALIGN 0
1062#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001063#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001064#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001065
Kumar Galacc334c72017-04-21 10:55:34 -05001066static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001067{
Benjamin Walsh62092182016-12-20 14:39:08 -05001068#ifdef CONFIG_SYS_CLOCK_EXISTS
1069
1070#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001071 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001072#else
Kumar Galacc334c72017-04-21 10:55:34 -05001073 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001074#endif
1075
1076#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001077 __ASSERT(ticks == 0, "");
1078 return 0;
1079#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001080}
1081
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082struct k_timer {
1083 /*
1084 * _timeout structure must be first here if we want to use
1085 * dynamic timer allocation. timeout.node is used in the double-linked
1086 * list of free timers
1087 */
1088 struct _timeout timeout;
1089
Allan Stephens45bfa372016-10-12 12:39:42 -05001090 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001091 _wait_q_t wait_q;
1092
1093 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001094 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001095
1096 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001097 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098
1099 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001100 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001101
Allan Stephens45bfa372016-10-12 12:39:42 -05001102 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001103 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001104
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001105 /* user-specific data, also used to support legacy features */
1106 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107
Anas Nashif2f203c22016-12-18 06:57:45 -05001108 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109};
1110
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001111#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001113 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001114 .timeout.wait_q = NULL, \
1115 .timeout.thread = NULL, \
1116 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001118 .expiry_fn = expiry, \
1119 .stop_fn = stop, \
1120 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001121 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001122 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001123 }
1124
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001125#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1126
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001127/**
Allan Stephensc98da842016-11-11 15:45:03 -05001128 * INTERNAL_HIDDEN @endcond
1129 */
1130
1131/**
1132 * @defgroup timer_apis Timer APIs
1133 * @ingroup kernel_apis
1134 * @{
1135 */
1136
1137/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001138 * @typedef k_timer_expiry_t
1139 * @brief Timer expiry function type.
1140 *
1141 * A timer's expiry function is executed by the system clock interrupt handler
1142 * each time the timer expires. The expiry function is optional, and is only
1143 * invoked if the timer has been initialized with one.
1144 *
1145 * @param timer Address of timer.
1146 *
1147 * @return N/A
1148 */
1149typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1150
1151/**
1152 * @typedef k_timer_stop_t
1153 * @brief Timer stop function type.
1154 *
1155 * A timer's stop function is executed if the timer is stopped prematurely.
1156 * The function runs in the context of the thread that stops the timer.
1157 * The stop function is optional, and is only invoked if the timer has been
1158 * initialized with one.
1159 *
1160 * @param timer Address of timer.
1161 *
1162 * @return N/A
1163 */
1164typedef void (*k_timer_stop_t)(struct k_timer *timer);
1165
1166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001171 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172 *
1173 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * @param expiry_fn Function to invoke each time the timer expires.
1175 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001177#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001178 struct k_timer name \
1179 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001180 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001181
Allan Stephens45bfa372016-10-12 12:39:42 -05001182/**
1183 * @brief Initialize a timer.
1184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001186 *
1187 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001188 * @param expiry_fn Function to invoke each time the timer expires.
1189 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001190 *
1191 * @return N/A
1192 */
1193extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001194 k_timer_expiry_t expiry_fn,
1195 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001196
Allan Stephens45bfa372016-10-12 12:39:42 -05001197/**
1198 * @brief Start a timer.
1199 *
1200 * This routine starts a timer, and resets its status to zero. The timer
1201 * begins counting down using the specified duration and period values.
1202 *
1203 * Attempting to start a timer that is already running is permitted.
1204 * The timer's status is reset to zero and the timer begins counting down
1205 * using the new duration and period values.
1206 *
1207 * @param timer Address of timer.
1208 * @param duration Initial timer duration (in milliseconds).
1209 * @param period Timer period (in milliseconds).
1210 *
1211 * @return N/A
1212 */
Andrew Boiea354d492017-09-29 16:22:28 -07001213__syscall void k_timer_start(struct k_timer *timer,
1214 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001215
1216/**
1217 * @brief Stop a timer.
1218 *
1219 * This routine stops a running timer prematurely. The timer's stop function,
1220 * if one exists, is invoked by the caller.
1221 *
1222 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001224 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001225 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1226 * if @a k_timer_stop is to be called from ISRs.
1227 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001228 * @param timer Address of timer.
1229 *
1230 * @return N/A
1231 */
Andrew Boiea354d492017-09-29 16:22:28 -07001232__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001233
1234/**
1235 * @brief Read timer status.
1236 *
1237 * This routine reads the timer's status, which indicates the number of times
1238 * it has expired since its status was last read.
1239 *
1240 * Calling this routine resets the timer's status to zero.
1241 *
1242 * @param timer Address of timer.
1243 *
1244 * @return Timer status.
1245 */
Andrew Boiea354d492017-09-29 16:22:28 -07001246__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001247
1248/**
1249 * @brief Synchronize thread to timer expiration.
1250 *
1251 * This routine blocks the calling thread until the timer's status is non-zero
1252 * (indicating that it has expired at least once since it was last examined)
1253 * or the timer is stopped. If the timer status is already non-zero,
1254 * or the timer is already stopped, the caller continues without waiting.
1255 *
1256 * Calling this routine resets the timer's status to zero.
1257 *
1258 * This routine must not be used by interrupt handlers, since they are not
1259 * allowed to block.
1260 *
1261 * @param timer Address of timer.
1262 *
1263 * @return Timer status.
1264 */
Andrew Boiea354d492017-09-29 16:22:28 -07001265__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001266
1267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001269 *
1270 * This routine computes the (approximate) time remaining before a running
1271 * timer next expires. If the timer is not running, it returns zero.
1272 *
1273 * @param timer Address of timer.
1274 *
1275 * @return Remaining time (in milliseconds).
1276 */
Andrew Boiea354d492017-09-29 16:22:28 -07001277__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1278
1279static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001280{
1281 return _timeout_remaining_get(&timer->timeout);
1282}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001283
Allan Stephensc98da842016-11-11 15:45:03 -05001284/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001285 * @brief Associate user-specific data with a timer.
1286 *
1287 * This routine records the @a user_data with the @a timer, to be retrieved
1288 * later.
1289 *
1290 * It can be used e.g. in a timer handler shared across multiple subsystems to
1291 * retrieve data specific to the subsystem this timer is associated with.
1292 *
1293 * @param timer Address of timer.
1294 * @param user_data User data to associate with the timer.
1295 *
1296 * @return N/A
1297 */
Andrew Boiea354d492017-09-29 16:22:28 -07001298__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1299
1300static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1301 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001302{
1303 timer->user_data = user_data;
1304}
1305
1306/**
1307 * @brief Retrieve the user-specific data from a timer.
1308 *
1309 * @param timer Address of timer.
1310 *
1311 * @return The user data.
1312 */
Andrew Boiea354d492017-09-29 16:22:28 -07001313__syscall void *k_timer_user_data_get(struct k_timer *timer);
1314
1315static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001316{
1317 return timer->user_data;
1318}
1319
1320/**
Allan Stephensc98da842016-11-11 15:45:03 -05001321 * @} end defgroup timer_apis
1322 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001323
Allan Stephensc98da842016-11-11 15:45:03 -05001324/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001325 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001326 * @{
1327 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001328
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001329/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001330 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001332 * This routine returns the elapsed time since the system booted,
1333 * in milliseconds.
1334 *
1335 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001336 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001337__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001338
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001339#ifdef CONFIG_TICKLESS_KERNEL
1340/**
1341 * @brief Enable clock always on in tickless kernel
1342 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001343 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001344 * there are no timer events programmed in tickless kernel
1345 * scheduling. This is necessary if the clock is used to track
1346 * passage of time.
1347 *
1348 * @retval prev_status Previous status of always on flag
1349 */
1350static inline int k_enable_sys_clock_always_on(void)
1351{
1352 int prev_status = _sys_clock_always_on;
1353
1354 _sys_clock_always_on = 1;
1355 _enable_sys_clock();
1356
1357 return prev_status;
1358}
1359
1360/**
1361 * @brief Disable clock always on in tickless kernel
1362 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001363 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001364 * there are no timer events programmed in tickless kernel
1365 * scheduling. To save power, this routine should be called
1366 * immediately when clock is not used to track time.
1367 */
1368static inline void k_disable_sys_clock_always_on(void)
1369{
1370 _sys_clock_always_on = 0;
1371}
1372#else
1373#define k_enable_sys_clock_always_on() do { } while ((0))
1374#define k_disable_sys_clock_always_on() do { } while ((0))
1375#endif
1376
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001380 * This routine returns the lower 32-bits of the elapsed time since the system
1381 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001383 * This routine can be more efficient than k_uptime_get(), as it reduces the
1384 * need for interrupt locking and 64-bit math. However, the 32-bit result
1385 * cannot hold a system uptime time larger than approximately 50 days, so the
1386 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001389 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001390__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001391
1392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001395 * This routine computes the elapsed time between the current system uptime
1396 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001398 * @param reftime Pointer to a reference time, which is updated to the current
1399 * uptime upon return.
1400 *
1401 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001402 */
Kumar Galacc334c72017-04-21 10:55:34 -05001403extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001404
1405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001408 * This routine computes the elapsed time between the current system uptime
1409 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001411 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1412 * need for interrupt locking and 64-bit math. However, the 32-bit result
1413 * cannot hold an elapsed time larger than approximately 50 days, so the
1414 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001416 * @param reftime Pointer to a reference time, which is updated to the current
1417 * uptime upon return.
1418 *
1419 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001420 */
Kumar Galacc334c72017-04-21 10:55:34 -05001421extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001422
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001426 * This routine returns the current time, as measured by the system's hardware
1427 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001429 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001430 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001431#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001432
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001434 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435 */
1436
Allan Stephensc98da842016-11-11 15:45:03 -05001437/**
1438 * @cond INTERNAL_HIDDEN
1439 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001441struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001442 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001443 union {
1444 _wait_q_t wait_q;
1445
1446 _POLL_EVENT;
1447 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001448
1449 _OBJECT_TRACING_NEXT_PTR(k_queue);
1450};
1451
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001452#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001453 { \
1454 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1455 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001456 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001457 _OBJECT_TRACING_INIT \
1458 }
1459
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001460#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1461
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001462/**
1463 * INTERNAL_HIDDEN @endcond
1464 */
1465
1466/**
1467 * @defgroup queue_apis Queue APIs
1468 * @ingroup kernel_apis
1469 * @{
1470 */
1471
1472/**
1473 * @brief Initialize a queue.
1474 *
1475 * This routine initializes a queue object, prior to its first use.
1476 *
1477 * @param queue Address of the queue.
1478 *
1479 * @return N/A
1480 */
1481extern void k_queue_init(struct k_queue *queue);
1482
1483/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001484 * @brief Cancel waiting on a queue.
1485 *
1486 * This routine causes first thread pending on @a queue, if any, to
1487 * return from k_queue_get() call with NULL value (as if timeout expired).
1488 *
1489 * @note Can be called by ISRs.
1490 *
1491 * @param queue Address of the queue.
1492 *
1493 * @return N/A
1494 */
1495extern void k_queue_cancel_wait(struct k_queue *queue);
1496
1497/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001498 * @brief Append an element to the end of a queue.
1499 *
1500 * This routine appends a data item to @a queue. A queue data item must be
1501 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1502 * reserved for the kernel's use.
1503 *
1504 * @note Can be called by ISRs.
1505 *
1506 * @param queue Address of the queue.
1507 * @param data Address of the data item.
1508 *
1509 * @return N/A
1510 */
1511extern void k_queue_append(struct k_queue *queue, void *data);
1512
1513/**
1514 * @brief Prepend an element to a queue.
1515 *
1516 * This routine prepends a data item to @a queue. A queue data item must be
1517 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1518 * reserved for the kernel's use.
1519 *
1520 * @note Can be called by ISRs.
1521 *
1522 * @param queue Address of the queue.
1523 * @param data Address of the data item.
1524 *
1525 * @return N/A
1526 */
1527extern void k_queue_prepend(struct k_queue *queue, void *data);
1528
1529/**
1530 * @brief Inserts an element to a queue.
1531 *
1532 * This routine inserts a data item to @a queue after previous item. A queue
1533 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1534 * item are reserved for the kernel's use.
1535 *
1536 * @note Can be called by ISRs.
1537 *
1538 * @param queue Address of the queue.
1539 * @param prev Address of the previous data item.
1540 * @param data Address of the data item.
1541 *
1542 * @return N/A
1543 */
1544extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1545
1546/**
1547 * @brief Atomically append a list of elements to a queue.
1548 *
1549 * This routine adds a list of data items to @a queue in one operation.
1550 * The data items must be in a singly-linked list, with the first 32 bits
1551 * in each data item pointing to the next data item; the list must be
1552 * NULL-terminated.
1553 *
1554 * @note Can be called by ISRs.
1555 *
1556 * @param queue Address of the queue.
1557 * @param head Pointer to first node in singly-linked list.
1558 * @param tail Pointer to last node in singly-linked list.
1559 *
1560 * @return N/A
1561 */
1562extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1563
1564/**
1565 * @brief Atomically add a list of elements to a queue.
1566 *
1567 * This routine adds a list of data items to @a queue in one operation.
1568 * The data items must be in a singly-linked list implemented using a
1569 * sys_slist_t object. Upon completion, the original list is empty.
1570 *
1571 * @note Can be called by ISRs.
1572 *
1573 * @param queue Address of the queue.
1574 * @param list Pointer to sys_slist_t object.
1575 *
1576 * @return N/A
1577 */
1578extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1579
1580/**
1581 * @brief Get an element from a queue.
1582 *
1583 * This routine removes first data item from @a queue. The first 32 bits of the
1584 * data item are reserved for the kernel's use.
1585 *
1586 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1587 *
1588 * @param queue Address of the queue.
1589 * @param timeout Waiting period to obtain a data item (in milliseconds),
1590 * or one of the special values K_NO_WAIT and K_FOREVER.
1591 *
1592 * @return Address of the data item if successful; NULL if returned
1593 * without waiting, or waiting period timed out.
1594 */
Kumar Galacc334c72017-04-21 10:55:34 -05001595extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001596
1597/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001598 * @brief Remove an element from a queue.
1599 *
1600 * This routine removes data item from @a queue. The first 32 bits of the
1601 * data item are reserved for the kernel's use. Removing elements from k_queue
1602 * rely on sys_slist_find_and_remove which is not a constant time operation.
1603 *
1604 * @note Can be called by ISRs
1605 *
1606 * @param queue Address of the queue.
1607 * @param data Address of the data item.
1608 *
1609 * @return true if data item was removed
1610 */
1611static inline bool k_queue_remove(struct k_queue *queue, void *data)
1612{
1613 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1614}
1615
1616/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001617 * @brief Query a queue to see if it has data available.
1618 *
1619 * Note that the data might be already gone by the time this function returns
1620 * if other threads are also trying to read from the queue.
1621 *
1622 * @note Can be called by ISRs.
1623 *
1624 * @param queue Address of the queue.
1625 *
1626 * @return Non-zero if the queue is empty.
1627 * @return 0 if data is available.
1628 */
1629static inline int k_queue_is_empty(struct k_queue *queue)
1630{
1631 return (int)sys_slist_is_empty(&queue->data_q);
1632}
1633
1634/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001635 * @brief Peek element at the head of queue.
1636 *
1637 * Return element from the head of queue without removing it.
1638 *
1639 * @param queue Address of the queue.
1640 *
1641 * @return Head element, or NULL if queue is empty.
1642 */
1643static inline void *k_queue_peek_head(struct k_queue *queue)
1644{
1645 return sys_slist_peek_head(&queue->data_q);
1646}
1647
1648/**
1649 * @brief Peek element at the tail of queue.
1650 *
1651 * Return element from the tail of queue without removing it.
1652 *
1653 * @param queue Address of the queue.
1654 *
1655 * @return Tail element, or NULL if queue is empty.
1656 */
1657static inline void *k_queue_peek_tail(struct k_queue *queue)
1658{
1659 return sys_slist_peek_tail(&queue->data_q);
1660}
1661
1662/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001663 * @brief Statically define and initialize a queue.
1664 *
1665 * The queue can be accessed outside the module where it is defined using:
1666 *
1667 * @code extern struct k_queue <name>; @endcode
1668 *
1669 * @param name Name of the queue.
1670 */
1671#define K_QUEUE_DEFINE(name) \
1672 struct k_queue name \
1673 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001674 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001675
1676/**
1677 * @} end defgroup queue_apis
1678 */
1679
1680/**
1681 * @cond INTERNAL_HIDDEN
1682 */
1683
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001684struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001685 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001686};
1687
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001688#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001689 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001690 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001691 }
1692
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001693#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1694
Allan Stephensc98da842016-11-11 15:45:03 -05001695/**
1696 * INTERNAL_HIDDEN @endcond
1697 */
1698
1699/**
1700 * @defgroup fifo_apis Fifo APIs
1701 * @ingroup kernel_apis
1702 * @{
1703 */
1704
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711 *
1712 * @return N/A
1713 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001714#define k_fifo_init(fifo) \
1715 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001716
1717/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001718 * @brief Cancel waiting on a fifo.
1719 *
1720 * This routine causes first thread pending on @a fifo, if any, to
1721 * return from k_fifo_get() call with NULL value (as if timeout
1722 * expired).
1723 *
1724 * @note Can be called by ISRs.
1725 *
1726 * @param fifo Address of the fifo.
1727 *
1728 * @return N/A
1729 */
1730#define k_fifo_cancel_wait(fifo) \
1731 k_queue_cancel_wait((struct k_queue *) fifo)
1732
1733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * This routine adds a data item to @a fifo. A fifo data item must be
1737 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1738 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @note Can be called by ISRs.
1741 *
1742 * @param fifo Address of the fifo.
1743 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 *
1745 * @return N/A
1746 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001747#define k_fifo_put(fifo, data) \
1748 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749
1750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * This routine adds a list of data items to @a fifo in one operation.
1754 * The data items must be in a singly-linked list, with the first 32 bits
1755 * each data item pointing to the next data item; the list must be
1756 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * @param fifo Address of the fifo.
1761 * @param head Pointer to first node in singly-linked list.
1762 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
1764 * @return N/A
1765 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001766#define k_fifo_put_list(fifo, head, tail) \
1767 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768
1769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001770 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * This routine adds a list of data items to @a fifo in one operation.
1773 * The data items must be in a singly-linked list implemented using a
1774 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775 * and must be re-initialized via sys_slist_init().
1776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * @note Can be called by ISRs.
1778 *
1779 * @param fifo Address of the fifo.
1780 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781 *
1782 * @return N/A
1783 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001784#define k_fifo_put_slist(fifo, list) \
1785 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786
1787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * This routine removes a data item from @a fifo in a "first in, first out"
1791 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1794 *
1795 * @param fifo Address of the fifo.
1796 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797 * or one of the special values K_NO_WAIT and K_FOREVER.
1798 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001799 * @return Address of the data item if successful; NULL if returned
1800 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001802#define k_fifo_get(fifo, timeout) \
1803 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001806 * @brief Query a fifo to see if it has data available.
1807 *
1808 * Note that the data might be already gone by the time this function returns
1809 * if other threads is also trying to read from the fifo.
1810 *
1811 * @note Can be called by ISRs.
1812 *
1813 * @param fifo Address of the fifo.
1814 *
1815 * @return Non-zero if the fifo is empty.
1816 * @return 0 if data is available.
1817 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001818#define k_fifo_is_empty(fifo) \
1819 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001820
1821/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001822 * @brief Peek element at the head of fifo.
1823 *
1824 * Return element from the head of fifo without removing it. A usecase
1825 * for this is if elements of the fifo are themselves containers. Then
1826 * on each iteration of processing, a head container will be peeked,
1827 * and some data processed out of it, and only if the container is empty,
1828 * it will be completely remove from the fifo.
1829 *
1830 * @param fifo Address of the fifo.
1831 *
1832 * @return Head element, or NULL if the fifo is empty.
1833 */
1834#define k_fifo_peek_head(fifo) \
1835 k_queue_peek_head((struct k_queue *) fifo)
1836
1837/**
1838 * @brief Peek element at the tail of fifo.
1839 *
1840 * Return element from the tail of fifo (without removing it). A usecase
1841 * for this is if elements of the fifo are themselves containers. Then
1842 * it may be useful to add more data to the last container in fifo.
1843 *
1844 * @param fifo Address of the fifo.
1845 *
1846 * @return Tail element, or NULL if fifo is empty.
1847 */
1848#define k_fifo_peek_tail(fifo) \
1849 k_queue_peek_tail((struct k_queue *) fifo)
1850
1851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001856 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001860#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001861 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001862 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001863 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864
Allan Stephensc98da842016-11-11 15:45:03 -05001865/**
1866 * @} end defgroup fifo_apis
1867 */
1868
1869/**
1870 * @cond INTERNAL_HIDDEN
1871 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001872
1873struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001874 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001875};
1876
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001877#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001878 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001879 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001880 }
1881
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001882#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1883
Allan Stephensc98da842016-11-11 15:45:03 -05001884/**
1885 * INTERNAL_HIDDEN @endcond
1886 */
1887
1888/**
1889 * @defgroup lifo_apis Lifo APIs
1890 * @ingroup kernel_apis
1891 * @{
1892 */
1893
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001895 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001899 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900 *
1901 * @return N/A
1902 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001903#define k_lifo_init(lifo) \
1904 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001905
1906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 * This routine adds a data item to @a lifo. A lifo data item must be
1910 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1911 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @note Can be called by ISRs.
1914 *
1915 * @param lifo Address of the lifo.
1916 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917 *
1918 * @return N/A
1919 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001920#define k_lifo_put(lifo, data) \
1921 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922
1923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001926 * This routine removes a data item from @a lifo in a "last in, first out"
1927 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1930 *
1931 * @param lifo Address of the lifo.
1932 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001933 * or one of the special values K_NO_WAIT and K_FOREVER.
1934 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001935 * @return Address of the data item if successful; NULL if returned
1936 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001937 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001938#define k_lifo_get(lifo, timeout) \
1939 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001946 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001951 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001952 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001953 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954
Allan Stephensc98da842016-11-11 15:45:03 -05001955/**
1956 * @} end defgroup lifo_apis
1957 */
1958
1959/**
1960 * @cond INTERNAL_HIDDEN
1961 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962
1963struct k_stack {
1964 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001965 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966
Anas Nashif2f203c22016-12-18 06:57:45 -05001967 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968};
1969
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001971 { \
1972 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1973 .base = stack_buffer, \
1974 .next = stack_buffer, \
1975 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001976 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001977 }
1978
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001979#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1980
Allan Stephensc98da842016-11-11 15:45:03 -05001981/**
1982 * INTERNAL_HIDDEN @endcond
1983 */
1984
1985/**
1986 * @defgroup stack_apis Stack APIs
1987 * @ingroup kernel_apis
1988 * @{
1989 */
1990
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991/**
1992 * @brief Initialize a stack.
1993 *
1994 * This routine initializes a stack object, prior to its first use.
1995 *
1996 * @param stack Address of the stack.
1997 * @param buffer Address of array used to hold stacked values.
1998 * @param num_entries Maximum number of values that can be stacked.
1999 *
2000 * @return N/A
2001 */
Andrew Boiee8734462017-09-29 16:42:07 -07002002__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002003 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004
2005/**
2006 * @brief Push an element onto a stack.
2007 *
2008 * This routine adds a 32-bit value @a data to @a stack.
2009 *
2010 * @note Can be called by ISRs.
2011 *
2012 * @param stack Address of the stack.
2013 * @param data Value to push onto the stack.
2014 *
2015 * @return N/A
2016 */
Andrew Boiee8734462017-09-29 16:42:07 -07002017__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018
2019/**
2020 * @brief Pop an element from a stack.
2021 *
2022 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2023 * manner and stores the value in @a data.
2024 *
2025 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2026 *
2027 * @param stack Address of the stack.
2028 * @param data Address of area to hold the value popped from the stack.
2029 * @param timeout Waiting period to obtain a value (in milliseconds),
2030 * or one of the special values K_NO_WAIT and K_FOREVER.
2031 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002032 * @retval 0 Element popped from stack.
2033 * @retval -EBUSY Returned without waiting.
2034 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 */
Andrew Boiee8734462017-09-29 16:42:07 -07002036__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002043 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * @param name Name of the stack.
2046 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002047 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002048#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002049 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002050 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002051 struct k_stack name \
2052 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002053 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002054 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056/**
Allan Stephensc98da842016-11-11 15:45:03 -05002057 * @} end defgroup stack_apis
2058 */
2059
Allan Stephens6bba9b02016-11-16 14:56:54 -05002060struct k_work;
2061
Allan Stephensc98da842016-11-11 15:45:03 -05002062/**
2063 * @defgroup workqueue_apis Workqueue Thread APIs
2064 * @ingroup kernel_apis
2065 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 */
2067
Allan Stephens6bba9b02016-11-16 14:56:54 -05002068/**
2069 * @typedef k_work_handler_t
2070 * @brief Work item handler function type.
2071 *
2072 * A work item's handler function is executed by a workqueue's thread
2073 * when the work item is processed by the workqueue.
2074 *
2075 * @param work Address of the work item.
2076 *
2077 * @return N/A
2078 */
2079typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002080
2081/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002082 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002084
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002086 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002087 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088};
2089
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002091 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092};
2093
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002095 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096 k_work_handler_t handler;
2097 atomic_t flags[1];
2098};
2099
Allan Stephens6bba9b02016-11-16 14:56:54 -05002100struct k_delayed_work {
2101 struct k_work work;
2102 struct _timeout timeout;
2103 struct k_work_q *work_q;
2104};
2105
2106extern struct k_work_q k_sys_work_q;
2107
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002109 * INTERNAL_HIDDEN @endcond
2110 */
2111
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002112#define _K_WORK_INITIALIZER(work_handler) \
2113 { \
2114 ._reserved = NULL, \
2115 .handler = work_handler, \
2116 .flags = { 0 } \
2117 }
2118
2119#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2120
Allan Stephens6bba9b02016-11-16 14:56:54 -05002121/**
2122 * @brief Initialize a statically-defined work item.
2123 *
2124 * This macro can be used to initialize a statically-defined workqueue work
2125 * item, prior to its first use. For example,
2126 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002127 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002128 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002129 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002130 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002132#define K_WORK_DEFINE(work, work_handler) \
2133 struct k_work work \
2134 __in_section(_k_work, static, work) = \
2135 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002136
2137/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002138 * @brief Initialize a work item.
2139 *
2140 * This routine initializes a workqueue work item, prior to its first use.
2141 *
2142 * @param work Address of work item.
2143 * @param handler Function to invoke each time work item is processed.
2144 *
2145 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146 */
2147static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2148{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002149 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002151 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002152}
2153
2154/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002155 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002156 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002157 * This routine submits work item @a work to be processed by workqueue
2158 * @a work_q. If the work item is already pending in the workqueue's queue
2159 * as a result of an earlier submission, this routine has no effect on the
2160 * work item. If the work item has already been processed, or is currently
2161 * being processed, its work is considered complete and the work item can be
2162 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002163 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164 * @warning
2165 * A submitted work item must not be modified until it has been processed
2166 * by the workqueue.
2167 *
2168 * @note Can be called by ISRs.
2169 *
2170 * @param work_q Address of workqueue.
2171 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002172 *
2173 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174 */
2175static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2176 struct k_work *work)
2177{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002178 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002179 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180 }
2181}
2182
2183/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186 * This routine indicates if work item @a work is pending in a workqueue's
2187 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * @note Can be called by ISRs.
2190 *
2191 * @param work Address of work item.
2192 *
2193 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002194 */
2195static inline int k_work_pending(struct k_work *work)
2196{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002197 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002198}
2199
2200/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002201 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 * This routine starts workqueue @a work_q. The workqueue spawns its work
2204 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002206 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002207 * @param stack Pointer to work queue thread's stack space, as defined by
2208 * K_THREAD_STACK_DEFINE()
2209 * @param stack_size Size of the work queue thread's stack (in bytes), which
2210 * should either be the same constant passed to
2211 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
2214 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215 */
Andrew Boie507852a2017-07-25 18:47:07 -07002216extern void k_work_q_start(struct k_work_q *work_q,
2217 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002218 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002222 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002223 * This routine initializes a workqueue delayed work item, prior to
2224 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002226 * @param work Address of delayed work item.
2227 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228 *
2229 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002231extern void k_delayed_work_init(struct k_delayed_work *work,
2232 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233
2234/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002235 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002237 * This routine schedules work item @a work to be processed by workqueue
2238 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002239 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002240 * Only when the countdown completes is the work item actually submitted to
2241 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002243 * Submitting a previously submitted delayed work item that is still
2244 * counting down cancels the existing submission and restarts the countdown
2245 * using the new delay. If the work item is currently pending on the
2246 * workqueue's queue because the countdown has completed it is too late to
2247 * resubmit the item, and resubmission fails without impacting the work item.
2248 * If the work item has already been processed, or is currently being processed,
2249 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002251 * @warning
2252 * A delayed work item must not be modified until it has been processed
2253 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002255 * @note Can be called by ISRs.
2256 *
2257 * @param work_q Address of workqueue.
2258 * @param work Address of delayed work item.
2259 * @param delay Delay before submitting the work item (in milliseconds).
2260 *
2261 * @retval 0 Work item countdown started.
2262 * @retval -EINPROGRESS Work item is already pending.
2263 * @retval -EINVAL Work item is being processed or has completed its work.
2264 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002265 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002266extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2267 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002268 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269
2270/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002271 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002274 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002275 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002277 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * @param work Address of delayed work item.
2280 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002281 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * @retval -EINPROGRESS Work item is already pending.
2283 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002285extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002286
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288 * @brief Submit a work item to the system workqueue.
2289 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002290 * This routine submits work item @a work to be processed by the system
2291 * workqueue. If the work item is already pending in the workqueue's queue
2292 * as a result of an earlier submission, this routine has no effect on the
2293 * work item. If the work item has already been processed, or is currently
2294 * being processed, its work is considered complete and the work item can be
2295 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002297 * @warning
2298 * Work items submitted to the system workqueue should avoid using handlers
2299 * that block or yield since this may prevent the system workqueue from
2300 * processing other work items in a timely manner.
2301 *
2302 * @note Can be called by ISRs.
2303 *
2304 * @param work Address of work item.
2305 *
2306 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 */
2308static inline void k_work_submit(struct k_work *work)
2309{
2310 k_work_submit_to_queue(&k_sys_work_q, work);
2311}
2312
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314 * @brief Submit a delayed work item to the system workqueue.
2315 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002316 * This routine schedules work item @a work to be processed by the system
2317 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002318 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002319 * Only when the countdown completes is the work item actually submitted to
2320 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002322 * Submitting a previously submitted delayed work item that is still
2323 * counting down cancels the existing submission and restarts the countdown
2324 * using the new delay. If the work item is currently pending on the
2325 * workqueue's queue because the countdown has completed it is too late to
2326 * resubmit the item, and resubmission fails without impacting the work item.
2327 * If the work item has already been processed, or is currently being processed,
2328 * its work is considered complete and the work item can be resubmitted.
2329 *
2330 * @warning
2331 * Work items submitted to the system workqueue should avoid using handlers
2332 * that block or yield since this may prevent the system workqueue from
2333 * processing other work items in a timely manner.
2334 *
2335 * @note Can be called by ISRs.
2336 *
2337 * @param work Address of delayed work item.
2338 * @param delay Delay before submitting the work item (in milliseconds).
2339 *
2340 * @retval 0 Work item countdown started.
2341 * @retval -EINPROGRESS Work item is already pending.
2342 * @retval -EINVAL Work item is being processed or has completed its work.
2343 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344 */
2345static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002346 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002348 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349}
2350
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002352 * @brief Get time remaining before a delayed work gets scheduled.
2353 *
2354 * This routine computes the (approximate) time remaining before a
2355 * delayed work gets executed. If the delayed work is not waiting to be
2356 * schedules, it returns zero.
2357 *
2358 * @param work Delayed work item.
2359 *
2360 * @return Remaining time (in milliseconds).
2361 */
Kumar Galacc334c72017-04-21 10:55:34 -05002362static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002363{
2364 return _timeout_remaining_get(&work->timeout);
2365}
2366
2367/**
Allan Stephensc98da842016-11-11 15:45:03 -05002368 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 */
2370
Allan Stephensc98da842016-11-11 15:45:03 -05002371/**
2372 * @cond INTERNAL_HIDDEN
2373 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374
2375struct k_mutex {
2376 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002377 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002378 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380
Anas Nashif2f203c22016-12-18 06:57:45 -05002381 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382};
2383
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002384#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385 { \
2386 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2387 .owner = NULL, \
2388 .lock_count = 0, \
2389 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002390 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 }
2392
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002393#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2394
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395/**
Allan Stephensc98da842016-11-11 15:45:03 -05002396 * INTERNAL_HIDDEN @endcond
2397 */
2398
2399/**
2400 * @defgroup mutex_apis Mutex APIs
2401 * @ingroup kernel_apis
2402 * @{
2403 */
2404
2405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002406 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002409 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002410 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002415 struct k_mutex name \
2416 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002417 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * Upon completion, the mutex is available and does not have an owner.
2425 *
2426 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 *
2428 * @return N/A
2429 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002430__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002431
2432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * This routine locks @a mutex. If the mutex is locked by another thread,
2436 * the calling thread waits until the mutex becomes available or until
2437 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * A thread is permitted to lock a mutex it has already locked. The operation
2440 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @param mutex Address of the mutex.
2443 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 * or one of the special values K_NO_WAIT and K_FOREVER.
2445 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002446 * @retval 0 Mutex locked.
2447 * @retval -EBUSY Returned without waiting.
2448 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002450__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451
2452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002455 * This routine unlocks @a mutex. The mutex must already be locked by the
2456 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
2458 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 * thread.
2461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002462 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463 *
2464 * @return N/A
2465 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002466__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467
Allan Stephensc98da842016-11-11 15:45:03 -05002468/**
2469 * @} end defgroup mutex_apis
2470 */
2471
2472/**
2473 * @cond INTERNAL_HIDDEN
2474 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475
2476struct k_sem {
2477 _wait_q_t wait_q;
2478 unsigned int count;
2479 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002480 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481
Anas Nashif2f203c22016-12-18 06:57:45 -05002482 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483};
2484
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002485#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002486 { \
2487 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2488 .count = initial_count, \
2489 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002490 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002491 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002492 }
2493
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002494#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2495
Allan Stephensc98da842016-11-11 15:45:03 -05002496/**
2497 * INTERNAL_HIDDEN @endcond
2498 */
2499
2500/**
2501 * @defgroup semaphore_apis Semaphore APIs
2502 * @ingroup kernel_apis
2503 * @{
2504 */
2505
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002506/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * @param sem Address of the semaphore.
2512 * @param initial_count Initial semaphore count.
2513 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514 *
2515 * @return N/A
2516 */
Andrew Boie99280232017-09-29 14:17:47 -07002517__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2518 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002519
2520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2526 *
2527 * @param sem Address of the semaphore.
2528 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002529 * or one of the special values K_NO_WAIT and K_FOREVER.
2530 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002531 * @note When porting code from the nanokernel legacy API to the new API, be
2532 * careful with the return value of this function. The return value is the
2533 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2534 * non-zero means failure, while the nano_sem_take family returns 1 for success
2535 * and 0 for failure.
2536 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002537 * @retval 0 Semaphore taken.
2538 * @retval -EBUSY Returned without waiting.
2539 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002540 */
Andrew Boie99280232017-09-29 14:17:47 -07002541__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002542
2543/**
2544 * @brief Give a semaphore.
2545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * This routine gives @a sem, unless the semaphore is already at its maximum
2547 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002552 *
2553 * @return N/A
2554 */
Andrew Boie99280232017-09-29 14:17:47 -07002555__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002557/**
2558 * @brief Reset a semaphore's count to zero.
2559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002563 *
2564 * @return N/A
2565 */
Andrew Boie990bf162017-10-03 12:36:49 -07002566__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002567
2568static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569{
2570 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002571}
2572
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002573/**
2574 * @brief Get a semaphore's count.
2575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581 */
Andrew Boie990bf162017-10-03 12:36:49 -07002582__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002583
2584static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585{
2586 return sem->count;
2587}
2588
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002594 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @param name Name of the semaphore.
2597 * @param initial_count Initial semaphore count.
2598 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002601 struct k_sem name \
2602 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002603 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604
Allan Stephensc98da842016-11-11 15:45:03 -05002605/**
2606 * @} end defgroup semaphore_apis
2607 */
2608
2609/**
2610 * @defgroup alert_apis Alert APIs
2611 * @ingroup kernel_apis
2612 * @{
2613 */
2614
Allan Stephens5eceb852016-11-16 10:16:30 -05002615/**
2616 * @typedef k_alert_handler_t
2617 * @brief Alert handler function type.
2618 *
2619 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002620 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002621 * and is only invoked if the alert has been initialized with one.
2622 *
2623 * @param alert Address of the alert.
2624 *
2625 * @return 0 if alert has been consumed; non-zero if alert should pend.
2626 */
2627typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002628
2629/**
2630 * @} end defgroup alert_apis
2631 */
2632
2633/**
2634 * @cond INTERNAL_HIDDEN
2635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002637#define K_ALERT_DEFAULT NULL
2638#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002640struct k_alert {
2641 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642 atomic_t send_count;
2643 struct k_work work_item;
2644 struct k_sem sem;
2645
Anas Nashif2f203c22016-12-18 06:57:45 -05002646 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647};
2648
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002649extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002650
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002651#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002653 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002655 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2656 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002657 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658 }
2659
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002660#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2661
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002662/**
Allan Stephensc98da842016-11-11 15:45:03 -05002663 * INTERNAL_HIDDEN @endcond
2664 */
2665
2666/**
2667 * @addtogroup alert_apis
2668 * @{
2669 */
2670
2671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002674 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002676 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @param name Name of the alert.
2679 * @param alert_handler Action to take when alert is sent. Specify either
2680 * the address of a function to be invoked by the system workqueue
2681 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2682 * K_ALERT_DEFAULT (which causes the alert to pend).
2683 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002684 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002685#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002686 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002687 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002688 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002689 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * @param alert Address of the alert.
2697 * @param handler Action to take when alert is sent. Specify either the address
2698 * of a function to be invoked by the system workqueue thread,
2699 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2700 * K_ALERT_DEFAULT (which causes the alert to pend).
2701 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002702 *
2703 * @return N/A
2704 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002705extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2706 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002707
2708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002709 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002713 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2714 *
2715 * @param alert Address of the alert.
2716 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 * or one of the special values K_NO_WAIT and K_FOREVER.
2718 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002719 * @retval 0 Alert received.
2720 * @retval -EBUSY Returned without waiting.
2721 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002722 */
Andrew Boie310e9872017-09-29 04:41:15 -07002723__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002724
2725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002728 * This routine signals @a alert. The action specified for @a alert will
2729 * be taken, which may trigger the execution of an alert handler function
2730 * and/or cause the alert to pend (assuming the alert has not reached its
2731 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * @note Can be called by ISRs.
2734 *
2735 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 *
2737 * @return N/A
2738 */
Andrew Boie310e9872017-09-29 04:41:15 -07002739__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740
2741/**
Allan Stephensc98da842016-11-11 15:45:03 -05002742 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743 */
2744
Allan Stephensc98da842016-11-11 15:45:03 -05002745/**
2746 * @cond INTERNAL_HIDDEN
2747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748
2749struct k_msgq {
2750 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002751 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002752 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753 char *buffer_start;
2754 char *buffer_end;
2755 char *read_ptr;
2756 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002757 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758
Anas Nashif2f203c22016-12-18 06:57:45 -05002759 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760};
2761
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002762#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 { \
2764 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002765 .max_msgs = q_max_msgs, \
2766 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002768 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 .read_ptr = q_buffer, \
2770 .write_ptr = q_buffer, \
2771 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002772 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773 }
2774
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002775#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2776
Peter Mitsis1da807e2016-10-06 11:36:59 -04002777/**
Allan Stephensc98da842016-11-11 15:45:03 -05002778 * INTERNAL_HIDDEN @endcond
2779 */
2780
2781/**
2782 * @defgroup msgq_apis Message Queue APIs
2783 * @ingroup kernel_apis
2784 * @{
2785 */
2786
2787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2791 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002792 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2793 * message is similarly aligned to this boundary, @a q_msg_size must also be
2794 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * The message queue can be accessed outside the module where it is defined
2797 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002799 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @param q_name Name of the message queue.
2802 * @param q_msg_size Message size (in bytes).
2803 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002804 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002805 */
2806#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2807 static char __noinit __aligned(q_align) \
2808 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002809 struct k_msgq q_name \
2810 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002811 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002812 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813
Peter Mitsisd7a37502016-10-13 11:37:40 -04002814/**
2815 * @brief Initialize a message queue.
2816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * This routine initializes a message queue object, prior to its first use.
2818 *
Allan Stephensda827222016-11-09 14:23:58 -06002819 * The message queue's ring buffer must contain space for @a max_msgs messages,
2820 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2821 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2822 * that each message is similarly aligned to this boundary, @a q_msg_size
2823 * must also be a multiple of N.
2824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @param q Address of the message queue.
2826 * @param buffer Pointer to ring buffer that holds queued messages.
2827 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002828 * @param max_msgs Maximum number of messages that can be queued.
2829 *
2830 * @return N/A
2831 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002832__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2833 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002834
2835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002839 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002840 * @note Can be called by ISRs.
2841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * @param q Address of the message queue.
2843 * @param data Pointer to the message.
2844 * @param timeout Waiting period to add the message (in milliseconds),
2845 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002846 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002847 * @retval 0 Message sent.
2848 * @retval -ENOMSG Returned without waiting or queue purged.
2849 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002850 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002851__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002852
2853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * This routine receives a message from message queue @a q in a "first in,
2857 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002858 *
Allan Stephensc98da842016-11-11 15:45:03 -05002859 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * @param q Address of the message queue.
2862 * @param data Address of area to hold the received message.
2863 * @param timeout Waiting period to receive the message (in milliseconds),
2864 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002865 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002866 * @retval 0 Message received.
2867 * @retval -ENOMSG Returned without waiting.
2868 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002869 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002870__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002871
2872/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * This routine discards all unreceived messages in a message queue's ring
2876 * buffer. Any threads that are blocked waiting to send a message to the
2877 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002880 *
2881 * @return N/A
2882 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002883__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002884
Peter Mitsis67be2492016-10-07 11:44:34 -04002885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * This routine returns the number of unused entries in a message queue's
2889 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param q Address of the message queue.
2892 *
2893 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002894 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002895__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2896
2897static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002898{
2899 return q->max_msgs - q->used_msgs;
2900}
2901
Peter Mitsisd7a37502016-10-13 11:37:40 -04002902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @param q Address of the message queue.
2908 *
2909 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002910 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002911__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2912
2913static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914{
2915 return q->used_msgs;
2916}
2917
Allan Stephensc98da842016-11-11 15:45:03 -05002918/**
2919 * @} end defgroup msgq_apis
2920 */
2921
2922/**
2923 * @defgroup mem_pool_apis Memory Pool APIs
2924 * @ingroup kernel_apis
2925 * @{
2926 */
2927
Andy Ross73cb9582017-05-09 10:42:39 -07002928/* Note on sizing: the use of a 20 bit field for block means that,
2929 * assuming a reasonable minimum block size of 16 bytes, we're limited
2930 * to 16M of memory managed by a single pool. Long term it would be
2931 * good to move to a variable bit size based on configuration.
2932 */
2933struct k_mem_block_id {
2934 u32_t pool : 8;
2935 u32_t level : 4;
2936 u32_t block : 20;
2937};
2938
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002941 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942};
2943
Allan Stephensc98da842016-11-11 15:45:03 -05002944/**
2945 * @} end defgroup mem_pool_apis
2946 */
2947
2948/**
2949 * @defgroup mailbox_apis Mailbox APIs
2950 * @ingroup kernel_apis
2951 * @{
2952 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953
2954struct k_mbox_msg {
2955 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002956 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002958 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002960 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961 /** sender's message data buffer */
2962 void *tx_data;
2963 /** internal use only - needed for legacy API support */
2964 void *_rx_data;
2965 /** message data block descriptor */
2966 struct k_mem_block tx_block;
2967 /** source thread id */
2968 k_tid_t rx_source_thread;
2969 /** target thread id */
2970 k_tid_t tx_target_thread;
2971 /** internal use only - thread waiting on send (may be a dummy) */
2972 k_tid_t _syncing_thread;
2973#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2974 /** internal use only - semaphore used during asynchronous send */
2975 struct k_sem *_async_sem;
2976#endif
2977};
2978
Allan Stephensc98da842016-11-11 15:45:03 -05002979/**
2980 * @cond INTERNAL_HIDDEN
2981 */
2982
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983struct k_mbox {
2984 _wait_q_t tx_msg_queue;
2985 _wait_q_t rx_msg_queue;
2986
Anas Nashif2f203c22016-12-18 06:57:45 -05002987 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988};
2989
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002990#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991 { \
2992 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2993 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002994 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995 }
2996
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002997#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2998
Peter Mitsis12092702016-10-14 12:57:23 -04002999/**
Allan Stephensc98da842016-11-11 15:45:03 -05003000 * INTERNAL_HIDDEN @endcond
3001 */
3002
3003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003008 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003011 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003013 struct k_mbox name \
3014 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003015 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016
Peter Mitsis12092702016-10-14 12:57:23 -04003017/**
3018 * @brief Initialize a mailbox.
3019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * This routine initializes a mailbox object, prior to its first use.
3021 *
3022 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003023 *
3024 * @return N/A
3025 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026extern void k_mbox_init(struct k_mbox *mbox);
3027
Peter Mitsis12092702016-10-14 12:57:23 -04003028/**
3029 * @brief Send a mailbox message in a synchronous manner.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * This routine sends a message to @a mbox and waits for a receiver to both
3032 * receive and process it. The message data may be in a buffer, in a memory
3033 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * @param mbox Address of the mailbox.
3036 * @param tx_msg Address of the transmit message descriptor.
3037 * @param timeout Waiting period for the message to be received (in
3038 * milliseconds), or one of the special values K_NO_WAIT
3039 * and K_FOREVER. Once the message has been received,
3040 * this routine waits as long as necessary for the message
3041 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003042 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003043 * @retval 0 Message sent.
3044 * @retval -ENOMSG Returned without waiting.
3045 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003046 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003047extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003048 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003049
Peter Mitsis12092702016-10-14 12:57:23 -04003050/**
3051 * @brief Send a mailbox message in an asynchronous manner.
3052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * This routine sends a message to @a mbox without waiting for a receiver
3054 * to process it. The message data may be in a buffer, in a memory pool block,
3055 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3056 * will be given when the message has been both received and completely
3057 * processed by the receiver.
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 sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003062 *
3063 * @return N/A
3064 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003065extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066 struct k_sem *sem);
3067
Peter Mitsis12092702016-10-14 12:57:23 -04003068/**
3069 * @brief Receive a mailbox message.
3070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * This routine receives a message from @a mbox, then optionally retrieves
3072 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * @param mbox Address of the mailbox.
3075 * @param rx_msg Address of the receive message descriptor.
3076 * @param buffer Address of the buffer to receive data, or NULL to defer data
3077 * retrieval and message disposal until later.
3078 * @param timeout Waiting period for a message to be received (in
3079 * milliseconds), or one of the special values K_NO_WAIT
3080 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003081 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003082 * @retval 0 Message received.
3083 * @retval -ENOMSG Returned without waiting.
3084 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003085 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003086extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003087 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003088
3089/**
3090 * @brief Retrieve mailbox message data into a buffer.
3091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * This routine completes the processing of a received message by retrieving
3093 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003094 *
3095 * Alternatively, this routine can be used to dispose of a received message
3096 * without retrieving its data.
3097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @param rx_msg Address of the receive message descriptor.
3099 * @param buffer Address of the buffer to receive data, or NULL to discard
3100 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003101 *
3102 * @return N/A
3103 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003104extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003105
3106/**
3107 * @brief Retrieve mailbox message data into a memory pool block.
3108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * This routine completes the processing of a received message by retrieving
3110 * its data into a memory pool block, then disposing of the message.
3111 * The memory pool block that results from successful retrieval must be
3112 * returned to the pool once the data has been processed, even in cases
3113 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003114 *
3115 * Alternatively, this routine can be used to dispose of a received message
3116 * without retrieving its data. In this case there is no need to return a
3117 * memory pool block to the pool.
3118 *
3119 * This routine allocates a new memory pool block for the data only if the
3120 * data is not already in one. If a new block cannot be allocated, the routine
3121 * returns a failure code and the received message is left unchanged. This
3122 * permits the caller to reattempt data retrieval at a later time or to dispose
3123 * of the received message without retrieving its data.
3124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * @param rx_msg Address of a receive message descriptor.
3126 * @param pool Address of memory pool, or NULL to discard data.
3127 * @param block Address of the area to hold memory pool block info.
3128 * @param timeout Waiting period to wait for a memory pool block (in
3129 * milliseconds), or one of the special values K_NO_WAIT
3130 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003131 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003132 * @retval 0 Data retrieved.
3133 * @retval -ENOMEM Returned without waiting.
3134 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003135 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003136extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003137 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003138 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139
Allan Stephensc98da842016-11-11 15:45:03 -05003140/**
3141 * @} end defgroup mailbox_apis
3142 */
3143
3144/**
3145 * @cond INTERNAL_HIDDEN
3146 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003147
3148struct k_pipe {
3149 unsigned char *buffer; /* Pipe buffer: may be NULL */
3150 size_t size; /* Buffer size */
3151 size_t bytes_used; /* # bytes used in buffer */
3152 size_t read_index; /* Where in buffer to read from */
3153 size_t write_index; /* Where in buffer to write */
3154
3155 struct {
3156 _wait_q_t readers; /* Reader wait queue */
3157 _wait_q_t writers; /* Writer wait queue */
3158 } wait_q;
3159
Anas Nashif2f203c22016-12-18 06:57:45 -05003160 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161};
3162
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003163#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003164 { \
3165 .buffer = pipe_buffer, \
3166 .size = pipe_buffer_size, \
3167 .bytes_used = 0, \
3168 .read_index = 0, \
3169 .write_index = 0, \
3170 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3171 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003172 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 }
3174
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003175#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3176
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003177/**
Allan Stephensc98da842016-11-11 15:45:03 -05003178 * INTERNAL_HIDDEN @endcond
3179 */
3180
3181/**
3182 * @defgroup pipe_apis Pipe APIs
3183 * @ingroup kernel_apis
3184 * @{
3185 */
3186
3187/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003192 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003194 * @param name Name of the pipe.
3195 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3196 * or zero if no ring buffer is used.
3197 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003198 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003199#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3200 static unsigned char __noinit __aligned(pipe_align) \
3201 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003202 struct k_pipe name \
3203 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003204 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * This routine initializes a pipe object, prior to its first use.
3210 *
3211 * @param pipe Address of the pipe.
3212 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3213 * is used.
3214 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3215 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216 *
3217 * @return N/A
3218 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003219__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3220 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221
3222/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @param pipe Address of the pipe.
3228 * @param data Address of data to write.
3229 * @param bytes_to_write Size of data (in bytes).
3230 * @param bytes_written Address of area to hold the number of bytes written.
3231 * @param min_xfer Minimum number of bytes to write.
3232 * @param timeout Waiting period to wait for the data to be written (in
3233 * milliseconds), or one of the special values K_NO_WAIT
3234 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003236 * @retval 0 At least @a min_xfer bytes of data were written.
3237 * @retval -EIO Returned without waiting; zero data bytes were written.
3238 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003241__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3242 size_t bytes_to_write, size_t *bytes_written,
3243 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244
3245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * @param pipe Address of the pipe.
3251 * @param data Address to place the data read from pipe.
3252 * @param bytes_to_read Maximum number of data bytes to read.
3253 * @param bytes_read Address of area to hold the number of bytes read.
3254 * @param min_xfer Minimum number of data bytes to read.
3255 * @param timeout Waiting period to wait for the data to be read (in
3256 * milliseconds), or one of the special values K_NO_WAIT
3257 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003259 * @retval 0 At least @a min_xfer bytes of data were read.
3260 * @retval -EIO Returned without waiting; zero data bytes were read.
3261 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003264__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3265 size_t bytes_to_read, size_t *bytes_read,
3266 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267
3268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * This routine writes the data contained in a memory block to @a pipe.
3272 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276 * @param block Memory block containing data to send
3277 * @param size Number of data bytes in memory block to send
3278 * @param sem Semaphore to signal upon completion (else NULL)
3279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281 */
3282extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3283 size_t size, struct k_sem *sem);
3284
3285/**
Allan Stephensc98da842016-11-11 15:45:03 -05003286 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287 */
3288
Allan Stephensc98da842016-11-11 15:45:03 -05003289/**
3290 * @cond INTERNAL_HIDDEN
3291 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003293struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003295 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003296 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297 char *buffer;
3298 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003299 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300
Anas Nashif2f203c22016-12-18 06:57:45 -05003301 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302};
3303
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003304#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003305 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306 { \
3307 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003308 .num_blocks = slab_num_blocks, \
3309 .block_size = slab_block_size, \
3310 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311 .free_list = NULL, \
3312 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003313 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314 }
3315
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003316#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3317
3318
Peter Mitsis578f9112016-10-07 13:50:31 -04003319/**
Allan Stephensc98da842016-11-11 15:45:03 -05003320 * INTERNAL_HIDDEN @endcond
3321 */
3322
3323/**
3324 * @defgroup mem_slab_apis Memory Slab APIs
3325 * @ingroup kernel_apis
3326 * @{
3327 */
3328
3329/**
Allan Stephensda827222016-11-09 14:23:58 -06003330 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003331 *
Allan Stephensda827222016-11-09 14:23:58 -06003332 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003334 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3335 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003336 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003337 *
Allan Stephensda827222016-11-09 14:23:58 -06003338 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003340 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003341 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @param name Name of the memory slab.
3344 * @param slab_block_size Size of each memory block (in bytes).
3345 * @param slab_num_blocks Number memory blocks.
3346 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003347 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003348#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3349 char __noinit __aligned(slab_align) \
3350 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3351 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003352 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003353 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003354 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003355
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003356/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003357 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003360 *
Allan Stephensda827222016-11-09 14:23:58 -06003361 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3362 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3363 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3364 * To ensure that each memory block is similarly aligned to this boundary,
3365 * @a slab_block_size must also be a multiple of N.
3366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @param slab Address of the memory slab.
3368 * @param buffer Pointer to buffer used for the memory blocks.
3369 * @param block_size Size of each memory block (in bytes).
3370 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003371 *
3372 * @return N/A
3373 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003374extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003375 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003376
3377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @param slab Address of the memory slab.
3383 * @param mem Pointer to block address area.
3384 * @param timeout Maximum time to wait for operation to complete
3385 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3386 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003387 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003388 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003390 * @retval -ENOMEM Returned without waiting.
3391 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003392 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003393extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003394 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003395
3396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * This routine releases a previously allocated memory block back to its
3400 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @param slab Address of the memory slab.
3403 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003404 *
3405 * @return N/A
3406 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003407extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * This routine gets the number of memory blocks that are currently
3413 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003418 */
Kumar Galacc334c72017-04-21 10:55:34 -05003419static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003421 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003422}
3423
Peter Mitsisc001aa82016-10-13 13:53:37 -04003424/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * This routine gets the number of memory blocks that are currently
3428 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003433 */
Kumar Galacc334c72017-04-21 10:55:34 -05003434static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003435{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003436 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003437}
3438
Allan Stephensc98da842016-11-11 15:45:03 -05003439/**
3440 * @} end defgroup mem_slab_apis
3441 */
3442
3443/**
3444 * @cond INTERNAL_HIDDEN
3445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003446
Andy Ross73cb9582017-05-09 10:42:39 -07003447struct k_mem_pool_lvl {
3448 union {
3449 u32_t *bits_p;
3450 u32_t bits;
3451 };
3452 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003453};
3454
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003455struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003456 void *buf;
3457 size_t max_sz;
3458 u16_t n_max;
3459 u8_t n_levels;
3460 u8_t max_inline_level;
3461 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003462 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003463};
3464
Andy Ross73cb9582017-05-09 10:42:39 -07003465#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466
Andy Ross73cb9582017-05-09 10:42:39 -07003467#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3468
3469#define _MPOOL_LVLS(maxsz, minsz) \
3470 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3471 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3472 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3473 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3474 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3475 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3476 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3477 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3478 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3479 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3480 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3481 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3482 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3483 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3484 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3485 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3486
3487/* Rounds the needed bits up to integer multiples of u32_t */
3488#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3489 ((((n_max) << (2*(l))) + 31) / 32)
3490
3491/* One word gets stored free unioned with the pointer, otherwise the
3492 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003493 */
Andy Ross73cb9582017-05-09 10:42:39 -07003494#define _MPOOL_LBIT_WORDS(n_max, l) \
3495 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3496 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003497
Andy Ross73cb9582017-05-09 10:42:39 -07003498/* How many bytes for the bitfields of a single level? */
3499#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3500 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3501 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003502
Andy Ross73cb9582017-05-09 10:42:39 -07003503/* Size of the bitmap array that follows the buffer in allocated memory */
3504#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3505 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3506 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3507 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3508 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3509 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3510 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3511 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3512 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3513 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3514 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3515 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3516 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3517 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3518 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3519 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3520 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003521
3522/**
Allan Stephensc98da842016-11-11 15:45:03 -05003523 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003524 */
3525
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003526/**
Allan Stephensc98da842016-11-11 15:45:03 -05003527 * @addtogroup mem_pool_apis
3528 * @{
3529 */
3530
3531/**
3532 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3535 * long. The memory pool allows blocks to be repeatedly partitioned into
3536 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003537 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003538 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003539 * If the pool is to be accessed outside the module where it is defined, it
3540 * can be declared via
3541 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003542 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003545 * @param minsz Size of the smallest blocks in the pool (in bytes).
3546 * @param maxsz Size of the largest blocks in the pool (in bytes).
3547 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003549 */
Andy Ross73cb9582017-05-09 10:42:39 -07003550#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3551 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3552 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3553 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3554 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3555 .buf = _mpool_buf_##name, \
3556 .max_sz = maxsz, \
3557 .n_max = nmax, \
3558 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3559 .levels = _mpool_lvls_##name, \
3560 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003561
Peter Mitsis937042c2016-10-13 13:18:26 -04003562/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @param pool Address of the memory pool.
3568 * @param block Pointer to block descriptor for the allocated memory.
3569 * @param size Amount of memory to allocate (in bytes).
3570 * @param timeout Maximum time to wait for operation to complete
3571 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3572 * or K_FOREVER to wait as long as necessary.
3573 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003574 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003576 * @retval -ENOMEM Returned without waiting.
3577 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003578 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003579extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003580 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003581
3582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * This routine releases a previously allocated memory block back to its
3586 * memory pool.
3587 *
3588 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003589 *
3590 * @return N/A
3591 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003592extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003593
3594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003596 *
Andy Ross73cb9582017-05-09 10:42:39 -07003597 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 *
Andy Ross73cb9582017-05-09 10:42:39 -07003599 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003600 *
3601 * @return N/A
3602 */
Andy Ross73cb9582017-05-09 10:42:39 -07003603static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003604
3605/**
Allan Stephensc98da842016-11-11 15:45:03 -05003606 * @} end addtogroup mem_pool_apis
3607 */
3608
3609/**
3610 * @defgroup heap_apis Heap Memory Pool APIs
3611 * @ingroup kernel_apis
3612 * @{
3613 */
3614
3615/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003619 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003624 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003625extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003626
3627/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003628 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003629 *
3630 * This routine provides traditional free() semantics. The memory being
3631 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003632 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003633 * If @a ptr is NULL, no operation is performed.
3634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003636 *
3637 * @return N/A
3638 */
3639extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003640
Allan Stephensc98da842016-11-11 15:45:03 -05003641/**
3642 * @} end defgroup heap_apis
3643 */
3644
Benjamin Walshacc68c12017-01-29 18:57:45 -05003645/* polling API - PRIVATE */
3646
Benjamin Walshb0179862017-02-02 16:39:57 -05003647#ifdef CONFIG_POLL
3648#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3649#else
3650#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3651#endif
3652
Benjamin Walshacc68c12017-01-29 18:57:45 -05003653/* private - implementation data created as needed, per-type */
3654struct _poller {
3655 struct k_thread *thread;
3656};
3657
3658/* private - types bit positions */
3659enum _poll_types_bits {
3660 /* can be used to ignore an event */
3661 _POLL_TYPE_IGNORE,
3662
3663 /* to be signaled by k_poll_signal() */
3664 _POLL_TYPE_SIGNAL,
3665
3666 /* semaphore availability */
3667 _POLL_TYPE_SEM_AVAILABLE,
3668
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003669 /* queue/fifo/lifo data availability */
3670 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003671
3672 _POLL_NUM_TYPES
3673};
3674
3675#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3676
3677/* private - states bit positions */
3678enum _poll_states_bits {
3679 /* default state when creating event */
3680 _POLL_STATE_NOT_READY,
3681
Benjamin Walshacc68c12017-01-29 18:57:45 -05003682 /* signaled by k_poll_signal() */
3683 _POLL_STATE_SIGNALED,
3684
3685 /* semaphore is available */
3686 _POLL_STATE_SEM_AVAILABLE,
3687
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003688 /* data is available to read on queue/fifo/lifo */
3689 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003690
3691 _POLL_NUM_STATES
3692};
3693
3694#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3695
3696#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003697 (32 - (0 \
3698 + 8 /* tag */ \
3699 + _POLL_NUM_TYPES \
3700 + _POLL_NUM_STATES \
3701 + 1 /* modes */ \
3702 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003703
3704#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3705#error overflow of 32-bit word in struct k_poll_event
3706#endif
3707
3708/* end of polling API - PRIVATE */
3709
3710
3711/**
3712 * @defgroup poll_apis Async polling APIs
3713 * @ingroup kernel_apis
3714 * @{
3715 */
3716
3717/* Public polling API */
3718
3719/* public - values for k_poll_event.type bitfield */
3720#define K_POLL_TYPE_IGNORE 0
3721#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3722#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003723#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3724#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003725
3726/* public - polling modes */
3727enum k_poll_modes {
3728 /* polling thread does not take ownership of objects when available */
3729 K_POLL_MODE_NOTIFY_ONLY = 0,
3730
3731 K_POLL_NUM_MODES
3732};
3733
3734/* public - values for k_poll_event.state bitfield */
3735#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3737#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003738#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3739#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003740
3741/* public - poll signal object */
3742struct k_poll_signal {
3743 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003744 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003745
3746 /*
3747 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3748 * user resets it to 0.
3749 */
3750 unsigned int signaled;
3751
3752 /* custom result value passed to k_poll_signal() if needed */
3753 int result;
3754};
3755
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003756#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003757 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003758 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003759 .signaled = 0, \
3760 .result = 0, \
3761 }
3762
3763struct k_poll_event {
3764 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003765 sys_dnode_t _node;
3766
3767 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768 struct _poller *poller;
3769
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003770 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003771 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003772
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003774 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003775
3776 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003777 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003778
3779 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003780 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003781
3782 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003783 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003784
3785 /* per-type data */
3786 union {
3787 void *obj;
3788 struct k_poll_signal *signal;
3789 struct k_sem *sem;
3790 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003791 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003792 };
3793};
3794
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003795#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 { \
3797 .poller = NULL, \
3798 .type = event_type, \
3799 .state = K_POLL_STATE_NOT_READY, \
3800 .mode = event_mode, \
3801 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003802 { .obj = event_obj }, \
3803 }
3804
3805#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3806 event_tag) \
3807 { \
3808 .type = event_type, \
3809 .tag = event_tag, \
3810 .state = K_POLL_STATE_NOT_READY, \
3811 .mode = event_mode, \
3812 .unused = 0, \
3813 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003814 }
3815
3816/**
3817 * @brief Initialize one struct k_poll_event instance
3818 *
3819 * After this routine is called on a poll event, the event it ready to be
3820 * placed in an event array to be passed to k_poll().
3821 *
3822 * @param event The event to initialize.
3823 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3824 * values. Only values that apply to the same object being polled
3825 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3826 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003827 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003828 * @param obj Kernel object or poll signal.
3829 *
3830 * @return N/A
3831 */
3832
Kumar Galacc334c72017-04-21 10:55:34 -05003833extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834 int mode, void *obj);
3835
3836/**
3837 * @brief Wait for one or many of multiple poll events to occur
3838 *
3839 * This routine allows a thread to wait concurrently for one or many of
3840 * multiple poll events to have occurred. Such events can be a kernel object
3841 * being available, like a semaphore, or a poll signal event.
3842 *
3843 * When an event notifies that a kernel object is available, the kernel object
3844 * is not "given" to the thread calling k_poll(): it merely signals the fact
3845 * that the object was available when the k_poll() call was in effect. Also,
3846 * all threads trying to acquire an object the regular way, i.e. by pending on
3847 * the object, have precedence over the thread polling on the object. This
3848 * means that the polling thread will never get the poll event on an object
3849 * until the object becomes available and its pend queue is empty. For this
3850 * reason, the k_poll() call is more effective when the objects being polled
3851 * only have one thread, the polling thread, trying to acquire them.
3852 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003853 * When k_poll() returns 0, the caller should loop on all the events that were
3854 * passed to k_poll() and check the state field for the values that were
3855 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003856 *
3857 * Before being reused for another call to k_poll(), the user has to reset the
3858 * state field to K_POLL_STATE_NOT_READY.
3859 *
3860 * @param events An array of pointers to events to be polled for.
3861 * @param num_events The number of events in the array.
3862 * @param timeout Waiting period for an event to be ready (in milliseconds),
3863 * or one of the special values K_NO_WAIT and K_FOREVER.
3864 *
3865 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003866 * @retval -EAGAIN Waiting period timed out.
3867 */
3868
3869extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003870 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003871
3872/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003873 * @brief Initialize a poll signal object.
3874 *
3875 * Ready a poll signal object to be signaled via k_poll_signal().
3876 *
3877 * @param signal A poll signal.
3878 *
3879 * @return N/A
3880 */
3881
3882extern void k_poll_signal_init(struct k_poll_signal *signal);
3883
3884/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003885 * @brief Signal a poll signal object.
3886 *
3887 * This routine makes ready a poll signal, which is basically a poll event of
3888 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3889 * made ready to run. A @a result value can be specified.
3890 *
3891 * The poll signal contains a 'signaled' field that, when set by
3892 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3893 * be reset by the user before being passed again to k_poll() or k_poll() will
3894 * consider it being signaled, and will return immediately.
3895 *
3896 * @param signal A poll signal.
3897 * @param result The value to store in the result field of the signal.
3898 *
3899 * @retval 0 The signal was delivered successfully.
3900 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3901 */
3902
3903extern int k_poll_signal(struct k_poll_signal *signal, int result);
3904
3905/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003906extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003907
3908/**
3909 * @} end defgroup poll_apis
3910 */
3911
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003912/**
3913 * @brief Make the CPU idle.
3914 *
3915 * This function makes the CPU idle until an event wakes it up.
3916 *
3917 * In a regular system, the idle thread should be the only thread responsible
3918 * for making the CPU idle and triggering any type of power management.
3919 * However, in some more constrained systems, such as a single-threaded system,
3920 * the only thread would be responsible for this if needed.
3921 *
3922 * @return N/A
3923 */
3924extern void k_cpu_idle(void);
3925
3926/**
3927 * @brief Make the CPU idle in an atomic fashion.
3928 *
3929 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3930 * must be done atomically before making the CPU idle.
3931 *
3932 * @param key Interrupt locking key obtained from irq_lock().
3933 *
3934 * @return N/A
3935 */
3936extern void k_cpu_atomic_idle(unsigned int key);
3937
Kumar Galacc334c72017-04-21 10:55:34 -05003938extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003939
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003940#include <arch/cpu.h>
3941
Andrew Boiecdb94d62017-04-18 15:22:05 -07003942#ifdef _ARCH_EXCEPT
3943/* This archtecture has direct support for triggering a CPU exception */
3944#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3945#else
3946
3947#include <misc/printk.h>
3948
3949/* NOTE: This is the implementation for arches that do not implement
3950 * _ARCH_EXCEPT() to generate a real CPU exception.
3951 *
3952 * We won't have a real exception frame to determine the PC value when
3953 * the oops occurred, so print file and line number before we jump into
3954 * the fatal error handler.
3955 */
3956#define _k_except_reason(reason) do { \
3957 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3958 _NanoFatalErrorHandler(reason, &_default_esf); \
3959 CODE_UNREACHABLE; \
3960 } while (0)
3961
3962#endif /* _ARCH__EXCEPT */
3963
3964/**
3965 * @brief Fatally terminate a thread
3966 *
3967 * This should be called when a thread has encountered an unrecoverable
3968 * runtime condition and needs to terminate. What this ultimately
3969 * means is determined by the _fatal_error_handler() implementation, which
3970 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3971 *
3972 * If this is called from ISR context, the default system fatal error handler
3973 * will treat it as an unrecoverable system error, just like k_panic().
3974 */
3975#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3976
3977/**
3978 * @brief Fatally terminate the system
3979 *
3980 * This should be called when the Zephyr kernel has encountered an
3981 * unrecoverable runtime condition and needs to terminate. What this ultimately
3982 * means is determined by the _fatal_error_handler() implementation, which
3983 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3984 */
3985#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3986
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003987/*
3988 * private APIs that are utilized by one or more public APIs
3989 */
3990
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003991#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003992extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003993#else
3994#define _init_static_threads() do { } while ((0))
3995#endif
3996
3997extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003998extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999
Andrew Boiedc5d9352017-06-02 12:56:47 -07004000/* arch/cpu.h may declare an architecture or platform-specific macro
4001 * for properly declaring stacks, compatible with MMU/MPU constraints if
4002 * enabled
4003 */
4004#ifdef _ARCH_THREAD_STACK_DEFINE
4005#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4006#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4007 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4008#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4009#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004010static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4011{
4012 return _ARCH_THREAD_STACK_BUFFER(sym);
4013}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004014#else
4015/**
4016 * @brief Declare a toplevel thread stack memory region
4017 *
4018 * This declares a region of memory suitable for use as a thread's stack.
4019 *
4020 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4021 * 'noinit' section so that it isn't zeroed at boot
4022 *
Andrew Boie507852a2017-07-25 18:47:07 -07004023 * The declared symbol will always be a k_thread_stack_t which can be passed to
4024 * k_thread_create, but should otherwise not be manipulated. If the buffer
4025 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004026 *
4027 * It is legal to precede this definition with the 'static' keyword.
4028 *
4029 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4030 * parameter of k_thread_create(), it may not be the same as the
4031 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4032 *
4033 * @param sym Thread stack symbol name
4034 * @param size Size of the stack memory region
4035 */
4036#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004037 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004038
4039/**
4040 * @brief Declare a toplevel array of thread stack memory regions
4041 *
4042 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4043 * definition for additional details and constraints.
4044 *
4045 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4046 * 'noinit' section so that it isn't zeroed at boot
4047 *
4048 * @param sym Thread stack symbol name
4049 * @param nmemb Number of stacks to declare
4050 * @param size Size of the stack memory region
4051 */
4052
4053#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004054 struct _k_thread_stack_element __noinit \
4055 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004056
4057/**
4058 * @brief Declare an embedded stack memory region
4059 *
4060 * Used for stacks embedded within other data structures. Use is highly
4061 * discouraged but in some cases necessary. For memory protection scenarios,
4062 * it is very important that any RAM preceding this member not be writable
4063 * by threads else a stack overflow will lead to silent corruption. In other
4064 * words, the containing data structure should live in RAM owned by the kernel.
4065 *
4066 * @param sym Thread stack symbol name
4067 * @param size Size of the stack memory region
4068 */
4069#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004070 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004071
4072/**
4073 * @brief Return the size in bytes of a stack memory region
4074 *
4075 * Convenience macro for passing the desired stack size to k_thread_create()
4076 * since the underlying implementation may actually create something larger
4077 * (for instance a guard area).
4078 *
4079 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004080 * passed to K_THREAD_STACK_DEFINE.
4081 *
4082 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4083 * it is not guaranteed to return the original value since each array
4084 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004085 *
4086 * @param sym Stack memory symbol
4087 * @return Size of the stack
4088 */
4089#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4090
4091/**
4092 * @brief Get a pointer to the physical stack buffer
4093 *
4094 * Convenience macro to get at the real underlying stack buffer used by
4095 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4096 * This is really only intended for diagnostic tools which want to examine
4097 * stack memory contents.
4098 *
4099 * @param sym Declared stack symbol name
4100 * @return The buffer itself, a char *
4101 */
Andrew Boie507852a2017-07-25 18:47:07 -07004102static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4103{
4104 return (char *)sym;
4105}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004106
4107#endif /* _ARCH_DECLARE_STACK */
4108
Chunlin Hane9c97022017-07-07 20:29:30 +08004109/**
4110 * @defgroup mem_domain_apis Memory domain APIs
4111 * @ingroup kernel_apis
4112 * @{
4113 */
4114
4115/** @def MEM_PARTITION_ENTRY
4116 * @brief Used to declare a memory partition entry
4117 */
4118#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4119 {\
4120 .start = _start, \
4121 .size = _size, \
4122 .attr = _attr, \
4123 }
4124
4125/** @def K_MEM_PARTITION_DEFINE
4126 * @brief Used to declare a memory partition
4127 */
4128#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4129#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4130 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4131 struct k_mem_partition name = \
4132 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4133#else
4134#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4135 struct k_mem_partition name = \
4136 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4137#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4138
4139
4140/* memory partition */
4141struct k_mem_partition {
4142 /* start address of memory partition */
4143 u32_t start;
4144 /* size of memory partition */
4145 u32_t size;
4146 /* attribute of memory partition */
4147 u32_t attr;
4148};
4149
4150#if defined(CONFIG_USERSPACE)
4151/* memory domian */
4152struct k_mem_domain {
4153 /* number of partitions in the domain */
4154 u32_t num_partitions;
4155 /* partitions in the domain */
4156 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4157 /* domain q */
4158 sys_dlist_t mem_domain_q;
4159};
4160#endif /* CONFIG_USERSPACE */
4161
4162/**
4163 * @brief Initialize a memory domain.
4164 *
4165 * Initialize a memory domain with given name and memory partitions.
4166 *
4167 * @param domain The memory domain to be initialized.
4168 * @param num_parts The number of array items of "parts" parameter.
4169 * @param parts An array of pointers to the memory partitions. Can be NULL
4170 * if num_parts is zero.
4171 */
4172
4173extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4174 struct k_mem_partition *parts[]);
4175/**
4176 * @brief Destroy a memory domain.
4177 *
4178 * Destroy a memory domain.
4179 *
4180 * @param domain The memory domain to be destroyed.
4181 */
4182
4183extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4184
4185/**
4186 * @brief Add a memory partition into a memory domain.
4187 *
4188 * Add a memory partition into a memory domain.
4189 *
4190 * @param domain The memory domain to be added a memory partition.
4191 * @param part The memory partition to be added
4192 */
4193
4194extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4195 struct k_mem_partition *part);
4196
4197/**
4198 * @brief Remove a memory partition from a memory domain.
4199 *
4200 * Remove a memory partition from a memory domain.
4201 *
4202 * @param domain The memory domain to be removed a memory partition.
4203 * @param part The memory partition to be removed
4204 */
4205
4206extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4207 struct k_mem_partition *part);
4208
4209/**
4210 * @brief Add a thread into a memory domain.
4211 *
4212 * Add a thread into a memory domain.
4213 *
4214 * @param domain The memory domain that the thread is going to be added into.
4215 * @param thread ID of thread going to be added into the memory domain.
4216 */
4217
4218extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4219 k_tid_t thread);
4220
4221/**
4222 * @brief Remove a thread from its memory domain.
4223 *
4224 * Remove a thread from its memory domain.
4225 *
4226 * @param thread ID of thread going to be removed from its memory domain.
4227 */
4228
4229extern void k_mem_domain_remove_thread(k_tid_t thread);
4230
4231/**
4232 * @} end defgroup mem_domain_apis
4233 */
4234
Andrew Boie756f9072017-10-10 16:01:49 -07004235/**
4236 * @brief Emit a character buffer to the console device
4237 *
4238 * @param c String of characters to print
4239 * @param n The length of the string
4240 */
4241__syscall void k_str_out(char *c, size_t n);
4242
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004243#ifdef __cplusplus
4244}
4245#endif
4246
Andrew Boiee004dec2016-11-07 09:01:19 -08004247#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4248/*
4249 * Define new and delete operators.
4250 * At this moment, the operators do nothing since objects are supposed
4251 * to be statically allocated.
4252 */
4253inline void operator delete(void *ptr)
4254{
4255 (void)ptr;
4256}
4257
4258inline void operator delete[](void *ptr)
4259{
4260 (void)ptr;
4261}
4262
4263inline void *operator new(size_t size)
4264{
4265 (void)size;
4266 return NULL;
4267}
4268
4269inline void *operator new[](size_t size)
4270{
4271 (void)size;
4272 return NULL;
4273}
4274
4275/* Placement versions of operator new and delete */
4276inline void operator delete(void *ptr1, void *ptr2)
4277{
4278 (void)ptr1;
4279 (void)ptr2;
4280}
4281
4282inline void operator delete[](void *ptr1, void *ptr2)
4283{
4284 (void)ptr1;
4285 (void)ptr2;
4286}
4287
4288inline void *operator new(size_t size, void *ptr)
4289{
4290 (void)size;
4291 return ptr;
4292}
4293
4294inline void *operator new[](size_t size, void *ptr)
4295{
4296 (void)size;
4297 return ptr;
4298}
4299
4300#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4301
Andrew Boiefa94ee72017-09-28 16:54:35 -07004302#include <syscalls/kernel.h>
4303
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004304#endif /* !_ASMLANGUAGE */
4305
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004306#endif /* _kernel__h_ */