blob: d396acf043712b45f8308229e7b1b07bf3a11064 [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 Boie5bd891d2017-09-27 12:59:28 -0700134 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700135 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700136 K_OBJ_MSGQ,
137 K_OBJ_MUTEX,
138 K_OBJ_PIPE,
139 K_OBJ_SEM,
140 K_OBJ_STACK,
141 K_OBJ_THREAD,
142 K_OBJ_TIMER,
Andrew Boie945af952017-08-22 13:15:23 -0700143
Andrew Boie5bd891d2017-09-27 12:59:28 -0700144 /* Driver subsystems */
145 K_OBJ_DRIVER_ADC,
146 K_OBJ_DRIVER_AIO_CMP,
147 K_OBJ_DRIVER_CLOCK_CONTROL,
148 K_OBJ_DRIVER_COUNTER,
149 K_OBJ_DRIVER_CRYPTO,
150 K_OBJ_DRIVER_DMA,
151 K_OBJ_DRIVER_ETH,
152 K_OBJ_DRIVER_FLASH,
153 K_OBJ_DRIVER_GPIO,
154 K_OBJ_DRIVER_I2C,
155 K_OBJ_DRIVER_I2S,
156 K_OBJ_DRIVER_IPM,
157 K_OBJ_DRIVER_PINMUX,
158 K_OBJ_DRIVER_PWM,
159 K_OBJ_DRIVER_RANDOM,
160 K_OBJ_DRIVER_RTC,
161 K_OBJ_DRIVER_SENSOR,
162 K_OBJ_DRIVER_SHARED_IRQ,
163 K_OBJ_DRIVER_SPI,
164 K_OBJ_DRIVER_UART,
165 K_OBJ_DRIVER_WDT,
166
Andrew Boie945af952017-08-22 13:15:23 -0700167 K_OBJ_LAST
168};
169
170#ifdef CONFIG_USERSPACE
171/* Table generated by gperf, these objects are retrieved via
172 * _k_object_find() */
173struct _k_object {
174 char *name;
175 char perms[CONFIG_MAX_THREAD_BYTES];
176 char type;
177 char flags;
178} __packed;
179
180#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie945af952017-08-22 13:15:23 -0700181
182/**
183 * Lookup a kernel object and init its metadata if it exists
184 *
185 * Calling this on an object will make it usable from userspace.
186 * Intended to be called as the last statement in kernel object init
187 * functions.
188 *
189 * @param object Address of the kernel object
190 */
191void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700192#else
Andrew Boie743e4682017-10-04 12:25:50 -0700193static inline void _k_object_init(void *obj)
194{
195 ARG_UNUSED(obj);
196}
197
198static inline void _impl_k_object_access_grant(void *object,
199 struct k_thread *thread)
200{
201 ARG_UNUSED(object);
202 ARG_UNUSED(thread);
203}
204
205static inline void _impl_k_object_access_all_grant(void *object)
206{
207 ARG_UNUSED(object);
208}
209#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700210
211/**
212 * grant a thread access to a kernel object
213 *
214 * The thread will be granted access to the object if the caller is from
215 * supervisor mode, or the caller is from user mode AND has permissions
216 * on the object already.
217 *
218 * @param object Address of kernel object
219 * @param thread Thread to grant access to the object
220 */
Andrew Boie743e4682017-10-04 12:25:50 -0700221__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700222
Andrew Boie3b5ae802017-10-04 12:10:32 -0700223
224/**
225 * grant all present and future threads access to an object
226 *
227 * If the caller is from supervisor mode, or the caller is from user mode and
228 * have sufficient permissions on the object, then that object will have
229 * permissions granted to it for *all* current and future threads running in
230 * the system, effectively becoming a public kernel object.
231 *
232 * Use of this API should be avoided on systems that are running untrusted code
233 * as it is possible for such code to derive the addresses of kernel objects
234 * and perform unwanted operations on them.
235 *
236 * @param object Address of kernel object
237 */
Andrew Boie743e4682017-10-04 12:25:50 -0700238__syscall void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700239
Andrew Boie73abd322017-04-04 13:19:13 -0700240/* timeouts */
241
242struct _timeout;
243typedef void (*_timeout_func_t)(struct _timeout *t);
244
245struct _timeout {
246 sys_dnode_t node;
247 struct k_thread *thread;
248 sys_dlist_t *wait_q;
249 s32_t delta_ticks_from_prev;
250 _timeout_func_t func;
251};
252
253extern s32_t _timeout_remaining_get(struct _timeout *timeout);
254
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700255/**
256 * @typedef k_thread_entry_t
257 * @brief Thread entry point function type.
258 *
259 * A thread's entry point function is invoked when the thread starts executing.
260 * Up to 3 argument values can be passed to the function.
261 *
262 * The thread terminates execution permanently if the entry point function
263 * returns. The thread is responsible for releasing any shared resources
264 * it may own (such as mutexes and dynamically allocated memory), prior to
265 * returning.
266 *
267 * @param p1 First argument.
268 * @param p2 Second argument.
269 * @param p3 Third argument.
270 *
271 * @return N/A
272 */
273typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700274
275#ifdef CONFIG_THREAD_MONITOR
276struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700277 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700278 void *parameter1;
279 void *parameter2;
280 void *parameter3;
281};
282#endif
283
284/* can be used for creating 'dummy' threads, e.g. for pending on objects */
285struct _thread_base {
286
287 /* this thread's entry in a ready/wait queue */
288 sys_dnode_t k_q_node;
289
290 /* user facing 'thread options'; values defined in include/kernel.h */
291 u8_t user_options;
292
293 /* thread state */
294 u8_t thread_state;
295
296 /*
297 * scheduler lock count and thread priority
298 *
299 * These two fields control the preemptibility of a thread.
300 *
301 * When the scheduler is locked, sched_locked is decremented, which
302 * means that the scheduler is locked for values from 0xff to 0x01. A
303 * thread is coop if its prio is negative, thus 0x80 to 0xff when
304 * looked at the value as unsigned.
305 *
306 * By putting them end-to-end, this means that a thread is
307 * non-preemptible if the bundled value is greater than or equal to
308 * 0x0080.
309 */
310 union {
311 struct {
312#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
313 u8_t sched_locked;
314 s8_t prio;
315#else /* LITTLE and PDP */
316 s8_t prio;
317 u8_t sched_locked;
318#endif
319 };
320 u16_t preempt;
321 };
322
323 /* data returned by APIs */
324 void *swap_data;
325
326#ifdef CONFIG_SYS_CLOCK_EXISTS
327 /* this thread's entry in a timeout queue */
328 struct _timeout timeout;
329#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700330
331#ifdef CONFIG_USERSPACE
332 /* Bit position in kernel object permissions bitfield for this thread */
333 unsigned int perm_index;
334#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700335};
336
337typedef struct _thread_base _thread_base_t;
338
339#if defined(CONFIG_THREAD_STACK_INFO)
340/* Contains the stack information of a thread */
341struct _thread_stack_info {
342 /* Stack Start */
343 u32_t start;
344 /* Stack Size */
345 u32_t size;
346};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700347
348typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700349#endif /* CONFIG_THREAD_STACK_INFO */
350
Chunlin Hane9c97022017-07-07 20:29:30 +0800351#if defined(CONFIG_USERSPACE)
352struct _mem_domain_info {
353 /* memory domain queue node */
354 sys_dnode_t mem_domain_q_node;
355 /* memory domain of the thread */
356 struct k_mem_domain *mem_domain;
357};
358
359#endif /* CONFIG_USERSPACE */
360
Andrew Boie73abd322017-04-04 13:19:13 -0700361struct k_thread {
362
363 struct _thread_base base;
364
365 /* defined by the architecture, but all archs need these */
366 struct _caller_saved caller_saved;
367 struct _callee_saved callee_saved;
368
369 /* static thread init data */
370 void *init_data;
371
372 /* abort function */
373 void (*fn_abort)(void);
374
375#if defined(CONFIG_THREAD_MONITOR)
376 /* thread entry and parameters description */
377 struct __thread_entry *entry;
378
379 /* next item in list of all threads */
380 struct k_thread *next_thread;
381#endif
382
383#ifdef CONFIG_THREAD_CUSTOM_DATA
384 /* crude thread-local storage */
385 void *custom_data;
386#endif
387
388#ifdef CONFIG_ERRNO
389 /* per-thread errno variable */
390 int errno_var;
391#endif
392
393#if defined(CONFIG_THREAD_STACK_INFO)
394 /* Stack Info */
395 struct _thread_stack_info stack_info;
396#endif /* CONFIG_THREAD_STACK_INFO */
397
Chunlin Hane9c97022017-07-07 20:29:30 +0800398#if defined(CONFIG_USERSPACE)
399 /* memory domain info of the thread */
400 struct _mem_domain_info mem_domain_info;
401#endif /* CONFIG_USERSPACE */
402
Andrew Boie73abd322017-04-04 13:19:13 -0700403 /* arch-specifics: must always be at the end */
404 struct _thread_arch arch;
405};
406
407typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400408typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700409#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400410
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400411enum execution_context_types {
412 K_ISR = 0,
413 K_COOP_THREAD,
414 K_PREEMPT_THREAD,
415};
416
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100418 * @defgroup profiling_apis Profiling APIs
419 * @ingroup kernel_apis
420 * @{
421 */
422
423/**
424 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
425 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700426 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100427 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
428 *
429 * CONFIG_MAIN_STACK_SIZE
430 * CONFIG_IDLE_STACK_SIZE
431 * CONFIG_ISR_STACK_SIZE
432 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
433 *
434 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
435 * produce output.
436 *
437 * @return N/A
438 */
439extern void k_call_stacks_analyze(void);
440
441/**
442 * @} end defgroup profiling_apis
443 */
444
445/**
Allan Stephensc98da842016-11-11 15:45:03 -0500446 * @defgroup thread_apis Thread APIs
447 * @ingroup kernel_apis
448 * @{
449 */
450
Benjamin Walshed240f22017-01-22 13:05:08 -0500451#endif /* !_ASMLANGUAGE */
452
453
454/*
455 * Thread user options. May be needed by assembly code. Common part uses low
456 * bits, arch-specific use high bits.
457 */
458
459/* system thread that must not abort */
460#define K_ESSENTIAL (1 << 0)
461
462#if defined(CONFIG_FP_SHARING)
463/* thread uses floating point registers */
464#define K_FP_REGS (1 << 1)
465#endif
466
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700467/* This thread has dropped from supervisor mode to user mode and consequently
468 * has additional restrictions
469 */
470#define K_USER (1 << 2)
471
Benjamin Walshed240f22017-01-22 13:05:08 -0500472#ifdef CONFIG_X86
473/* x86 Bitmask definitions for threads user options */
474
475#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
476/* thread uses SSEx (and also FP) registers */
477#define K_SSE_REGS (1 << 7)
478#endif
479#endif
480
481/* end - thread options */
482
483#if !defined(_ASMLANGUAGE)
484
Andrew Boie507852a2017-07-25 18:47:07 -0700485/* Using typedef deliberately here, this is quite intended to be an opaque
486 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
487 *
488 * The purpose of this data type is to clearly distinguish between the
489 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
490 * buffer which composes the stack data actually used by the underlying
491 * thread; they cannot be used interchangably as some arches precede the
492 * stack buffer region with guard areas that trigger a MPU or MMU fault
493 * if written to.
494 *
495 * APIs that want to work with the buffer inside should continue to use
496 * char *.
497 *
498 * Stacks should always be created with K_THREAD_STACK_DEFINE().
499 */
500struct __packed _k_thread_stack_element {
501 char data;
502};
503typedef struct _k_thread_stack_element *k_thread_stack_t;
504
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400505
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400506/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700507 * @brief Create a thread.
508 *
509 * This routine initializes a thread, then schedules it for execution.
510 *
511 * The new thread may be scheduled for immediate execution or a delayed start.
512 * If the newly spawned thread does not have a delayed start the kernel
513 * scheduler may preempt the current thread to allow the new thread to
514 * execute.
515 *
516 * Thread options are architecture-specific, and can include K_ESSENTIAL,
517 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
518 * them using "|" (the logical OR operator).
519 *
520 * Historically, users often would use the beginning of the stack memory region
521 * to store the struct k_thread data, although corruption will occur if the
522 * stack overflows this region and stack protection features may not detect this
523 * situation.
524 *
525 * @param new_thread Pointer to uninitialized struct k_thread
526 * @param stack Pointer to the stack space.
527 * @param stack_size Stack size in bytes.
528 * @param entry Thread entry function.
529 * @param p1 1st entry point parameter.
530 * @param p2 2nd entry point parameter.
531 * @param p3 3rd entry point parameter.
532 * @param prio Thread priority.
533 * @param options Thread options.
534 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
535 *
536 * @return ID of new thread.
537 */
Andrew Boie507852a2017-07-25 18:47:07 -0700538extern k_tid_t k_thread_create(struct k_thread *new_thread,
539 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700540 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700541 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700542 void *p1, void *p2, void *p3,
543 int prio, u32_t options, s32_t delay);
544
Andrew Boie3f091b52017-08-30 14:34:14 -0700545/**
546 * @brief Drop a thread's privileges permanently to user mode
547 *
548 * @param entry Function to start executing from
549 * @param p1 1st entry point parameter
550 * @param p2 2nd entry point parameter
551 * @param p3 3rd entry point parameter
552 */
553extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
554 void *p1, void *p2,
555 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700556
Andrew Boied26cf2d2017-03-30 13:07:02 -0700557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500558 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400559 *
Allan Stephensc98da842016-11-11 15:45:03 -0500560 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500563 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
565 * @return N/A
566 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700567__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400568
569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500570 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 *
572 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500573 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 * @return N/A
576 */
Kumar Galacc334c72017-04-21 10:55:34 -0500577extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578
579/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500580 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
586 * @return N/A
587 */
Andrew Boie468190a2017-09-29 14:00:48 -0700588__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589
590/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500591 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * If @a thread is not currently sleeping, the routine has no effect.
596 *
597 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598 *
599 * @return N/A
600 */
Andrew Boie468190a2017-09-29 14:00:48 -0700601__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602
603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700608__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609
610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * This routine prevents @a thread from executing if it has not yet started
614 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500616 * @param thread ID of thread to cancel.
617 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700618 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500619 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400620 */
Andrew Boie468190a2017-09-29 14:00:48 -0700621__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623/**
Allan Stephensc98da842016-11-11 15:45:03 -0500624 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500626 * This routine permanently stops execution of @a thread. The thread is taken
627 * off all kernel queues it is part of (i.e. the ready queue, the timeout
628 * queue, or a kernel object wait queue). However, any kernel resources the
629 * thread might currently own (such as mutexes or memory blocks) are not
630 * released. It is the responsibility of the caller of this routine to ensure
631 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500633 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634 *
635 * @return N/A
636 */
Andrew Boie468190a2017-09-29 14:00:48 -0700637__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400638
Andrew Boie7d627c52017-08-30 11:01:56 -0700639
640/**
641 * @brief Start an inactive thread
642 *
643 * If a thread was created with K_FOREVER in the delay parameter, it will
644 * not be added to the scheduling queue until this function is called
645 * on it.
646 *
647 * @param thread thread to start
648 */
Andrew Boie468190a2017-09-29 14:00:48 -0700649__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700650
Allan Stephensc98da842016-11-11 15:45:03 -0500651/**
652 * @cond INTERNAL_HIDDEN
653 */
654
Benjamin Walshd211a522016-12-06 11:44:01 -0500655/* timeout has timed out and is not on _timeout_q anymore */
656#define _EXPIRED (-2)
657
658/* timeout is not in use */
659#define _INACTIVE (-1)
660
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400661struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700662 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700663 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400664 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700665 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500666 void *init_p1;
667 void *init_p2;
668 void *init_p3;
669 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500670 u32_t init_options;
671 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500672 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500673 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400674};
675
Andrew Boied26cf2d2017-03-30 13:07:02 -0700676#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400677 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500678 prio, options, delay, abort, groups) \
679 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700680 .init_thread = (thread), \
681 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500682 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700683 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400684 .init_p1 = (void *)p1, \
685 .init_p2 = (void *)p2, \
686 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500687 .init_prio = (prio), \
688 .init_options = (options), \
689 .init_delay = (delay), \
690 .init_abort = (abort), \
691 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400692 }
693
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400694/**
Allan Stephensc98da842016-11-11 15:45:03 -0500695 * INTERNAL_HIDDEN @endcond
696 */
697
698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500699 * @brief Statically define and initialize a thread.
700 *
701 * The thread may be scheduled for immediate execution or a delayed start.
702 *
703 * Thread options are architecture-specific, and can include K_ESSENTIAL,
704 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
705 * them using "|" (the logical OR operator).
706 *
707 * The ID of the thread can be accessed using:
708 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500709 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500710 *
711 * @param name Name of the thread.
712 * @param stack_size Stack size in bytes.
713 * @param entry Thread entry function.
714 * @param p1 1st entry point parameter.
715 * @param p2 2nd entry point parameter.
716 * @param p3 3rd entry point parameter.
717 * @param prio Thread priority.
718 * @param options Thread options.
719 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400720 *
721 * @internal It has been observed that the x86 compiler by default aligns
722 * these _static_thread_data structures to 32-byte boundaries, thereby
723 * wasting space. To work around this, force a 4-byte alignment.
724 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500725#define K_THREAD_DEFINE(name, stack_size, \
726 entry, p1, p2, p3, \
727 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700728 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700729 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500730 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500731 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700732 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
733 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500734 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700735 NULL, 0); \
736 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400737
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500739 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500741 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500743 * @param thread ID of thread whose priority is needed.
744 *
745 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400746 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700747__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748
749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400753 *
754 * Rescheduling can occur immediately depending on the priority @a thread is
755 * set to:
756 *
757 * - If its priority is raised above the priority of the caller of this
758 * function, and the caller is preemptible, @a thread will be scheduled in.
759 *
760 * - If the caller operates on itself, it lowers its priority below that of
761 * other threads in the system, and the caller is preemptible, the thread of
762 * highest priority will be scheduled in.
763 *
764 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
765 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
766 * highest priority.
767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 * @param prio New priority.
770 *
771 * @warning Changing the priority of a thread currently involved in mutex
772 * priority inheritance may result in undefined behavior.
773 *
774 * @return N/A
775 */
Andrew Boie468190a2017-09-29 14:00:48 -0700776__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400777
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500779 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * This routine prevents the kernel scheduler from making @a thread the
782 * current thread. All other internal operations on @a thread are still
783 * performed; for example, any timeout it is waiting on keeps ticking,
784 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * If @a thread is already suspended, the routine has no effect.
787 *
788 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Andrew Boie468190a2017-09-29 14:00:48 -0700792__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * This routine allows the kernel scheduler to make @a thread the current
798 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * If @a thread is not currently suspended, the routine has no effect.
801 *
802 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
804 * @return N/A
805 */
Andrew Boie468190a2017-09-29 14:00:48 -0700806__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400807
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * This routine specifies how the scheduler will perform time slicing of
812 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * To enable time slicing, @a slice must be non-zero. The scheduler
815 * ensures that no thread runs for more than the specified time limit
816 * before other threads of that priority are given a chance to execute.
817 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700818 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 * execute. Once the scheduler selects a thread for execution, there is no
822 * minimum guaranteed time the thread will execute before threads of greater or
823 * equal priority are scheduled.
824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 * for execution, this routine has no effect; the thread is immediately
827 * rescheduled after the slice period expires.
828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * To disable timeslicing, set both @a slice and @a prio to zero.
830 *
831 * @param slice Maximum time slice length (in milliseconds).
832 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
834 * @return N/A
835 */
Kumar Galacc334c72017-04-21 10:55:34 -0500836extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400837
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838/**
Allan Stephensc98da842016-11-11 15:45:03 -0500839 * @} end defgroup thread_apis
840 */
841
842/**
843 * @addtogroup isr_apis
844 * @{
845 */
846
847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephensc98da842016-11-11 15:45:03 -0500850 * This routine allows the caller to customize its actions, depending on
851 * whether it is a thread or an ISR.
852 *
853 * @note Can be called by ISRs.
854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @return 0 if invoked by a thread.
856 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500858extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400859
Benjamin Walsh445830d2016-11-10 15:54:27 -0500860/**
861 * @brief Determine if code is running in a preemptible thread.
862 *
Allan Stephensc98da842016-11-11 15:45:03 -0500863 * This routine allows the caller to customize its actions, depending on
864 * whether it can be preempted by another thread. The routine returns a 'true'
865 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500866 *
Allan Stephensc98da842016-11-11 15:45:03 -0500867 * - The code is running in a thread, not at ISR.
868 * - The thread's priority is in the preemptible range.
869 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500870 *
Allan Stephensc98da842016-11-11 15:45:03 -0500871 * @note Can be called by ISRs.
872 *
873 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500874 * @return Non-zero if invoked by a preemptible thread.
875 */
Andrew Boie468190a2017-09-29 14:00:48 -0700876__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500877
Allan Stephensc98da842016-11-11 15:45:03 -0500878/**
879 * @} end addtogroup isr_apis
880 */
881
882/**
883 * @addtogroup thread_apis
884 * @{
885 */
886
887/**
888 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500889 *
Allan Stephensc98da842016-11-11 15:45:03 -0500890 * This routine prevents the current thread from being preempted by another
891 * thread by instructing the scheduler to treat it as a cooperative thread.
892 * If the thread subsequently performs an operation that makes it unready,
893 * it will be context switched out in the normal manner. When the thread
894 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500895 *
Allan Stephensc98da842016-11-11 15:45:03 -0500896 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500897 *
Allan Stephensc98da842016-11-11 15:45:03 -0500898 * @note k_sched_lock() and k_sched_unlock() should normally be used
899 * when the operation being performed can be safely interrupted by ISRs.
900 * However, if the amount of processing involved is very small, better
901 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500902 *
903 * @return N/A
904 */
905extern void k_sched_lock(void);
906
Allan Stephensc98da842016-11-11 15:45:03 -0500907/**
908 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500909 *
Allan Stephensc98da842016-11-11 15:45:03 -0500910 * This routine reverses the effect of a previous call to k_sched_lock().
911 * A thread must call the routine once for each time it called k_sched_lock()
912 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500913 *
914 * @return N/A
915 */
916extern void k_sched_unlock(void);
917
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500919 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * Custom data is not used by the kernel itself, and is freely available
924 * for a thread to use as it sees fit. It can be used as a framework
925 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 *
929 * @return N/A
930 */
Andrew Boie468190a2017-09-29 14:00:48 -0700931__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932
933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500936 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939 */
Andrew Boie468190a2017-09-29 14:00:48 -0700940__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941
942/**
Allan Stephensc98da842016-11-11 15:45:03 -0500943 * @} end addtogroup thread_apis
944 */
945
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400946#include <sys_clock.h>
947
Allan Stephensc2f15a42016-11-17 12:24:22 -0500948/**
949 * @addtogroup clock_apis
950 * @{
951 */
952
953/**
954 * @brief Generate null timeout delay.
955 *
956 * This macro generates a timeout delay that that instructs a kernel API
957 * not to wait if the requested operation cannot be performed immediately.
958 *
959 * @return Timeout delay value.
960 */
961#define K_NO_WAIT 0
962
963/**
964 * @brief Generate timeout delay from milliseconds.
965 *
966 * This macro generates a timeout delay that that instructs a kernel API
967 * to wait up to @a ms milliseconds to perform the requested operation.
968 *
969 * @param ms Duration in milliseconds.
970 *
971 * @return Timeout delay value.
972 */
Johan Hedberg14471692016-11-13 10:52:15 +0200973#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500974
975/**
976 * @brief Generate timeout delay from seconds.
977 *
978 * This macro generates a timeout delay that that instructs a kernel API
979 * to wait up to @a s seconds to perform the requested operation.
980 *
981 * @param s Duration in seconds.
982 *
983 * @return Timeout delay value.
984 */
Johan Hedberg14471692016-11-13 10:52:15 +0200985#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500986
987/**
988 * @brief Generate timeout delay from minutes.
989 *
990 * This macro generates a timeout delay that that instructs a kernel API
991 * to wait up to @a m minutes to perform the requested operation.
992 *
993 * @param m Duration in minutes.
994 *
995 * @return Timeout delay value.
996 */
Johan Hedberg14471692016-11-13 10:52:15 +0200997#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500998
999/**
1000 * @brief Generate timeout delay from hours.
1001 *
1002 * This macro generates a timeout delay that that instructs a kernel API
1003 * to wait up to @a h hours to perform the requested operation.
1004 *
1005 * @param h Duration in hours.
1006 *
1007 * @return Timeout delay value.
1008 */
Johan Hedberg14471692016-11-13 10:52:15 +02001009#define K_HOURS(h) K_MINUTES((h) * 60)
1010
Allan Stephensc98da842016-11-11 15:45:03 -05001011/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001012 * @brief Generate infinite timeout delay.
1013 *
1014 * This macro generates a timeout delay that that instructs a kernel API
1015 * to wait as long as necessary to perform the requested operation.
1016 *
1017 * @return Timeout delay value.
1018 */
1019#define K_FOREVER (-1)
1020
1021/**
1022 * @} end addtogroup clock_apis
1023 */
1024
1025/**
Allan Stephensc98da842016-11-11 15:45:03 -05001026 * @cond INTERNAL_HIDDEN
1027 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001028
Benjamin Walsh62092182016-12-20 14:39:08 -05001029/* kernel clocks */
1030
1031#if (sys_clock_ticks_per_sec == 1000) || \
1032 (sys_clock_ticks_per_sec == 500) || \
1033 (sys_clock_ticks_per_sec == 250) || \
1034 (sys_clock_ticks_per_sec == 125) || \
1035 (sys_clock_ticks_per_sec == 100) || \
1036 (sys_clock_ticks_per_sec == 50) || \
1037 (sys_clock_ticks_per_sec == 25) || \
1038 (sys_clock_ticks_per_sec == 20) || \
1039 (sys_clock_ticks_per_sec == 10) || \
1040 (sys_clock_ticks_per_sec == 1)
1041
1042 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1043#else
1044 /* yields horrible 64-bit math on many architectures: try to avoid */
1045 #define _NON_OPTIMIZED_TICKS_PER_SEC
1046#endif
1047
1048#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001049extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001050#else
Kumar Galacc334c72017-04-21 10:55:34 -05001051static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001052{
Kumar Galacc334c72017-04-21 10:55:34 -05001053 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001054}
1055#endif
1056
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001057/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001058#ifdef CONFIG_TICKLESS_KERNEL
1059#define _TICK_ALIGN 0
1060#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001061#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001062#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001063
Kumar Galacc334c72017-04-21 10:55:34 -05001064static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001065{
Benjamin Walsh62092182016-12-20 14:39:08 -05001066#ifdef CONFIG_SYS_CLOCK_EXISTS
1067
1068#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001069 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001070#else
Kumar Galacc334c72017-04-21 10:55:34 -05001071 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001072#endif
1073
1074#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001075 __ASSERT(ticks == 0, "");
1076 return 0;
1077#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001078}
1079
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080struct k_timer {
1081 /*
1082 * _timeout structure must be first here if we want to use
1083 * dynamic timer allocation. timeout.node is used in the double-linked
1084 * list of free timers
1085 */
1086 struct _timeout timeout;
1087
Allan Stephens45bfa372016-10-12 12:39:42 -05001088 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001089 _wait_q_t wait_q;
1090
1091 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001092 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001093
1094 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001095 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096
1097 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001098 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099
Allan Stephens45bfa372016-10-12 12:39:42 -05001100 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001101 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001103 /* user-specific data, also used to support legacy features */
1104 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105
Anas Nashif2f203c22016-12-18 06:57:45 -05001106 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107};
1108
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001109#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001110 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001111 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001112 .timeout.wait_q = NULL, \
1113 .timeout.thread = NULL, \
1114 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001115 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001116 .expiry_fn = expiry, \
1117 .stop_fn = stop, \
1118 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001119 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001120 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121 }
1122
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001123#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1124
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125/**
Allan Stephensc98da842016-11-11 15:45:03 -05001126 * INTERNAL_HIDDEN @endcond
1127 */
1128
1129/**
1130 * @defgroup timer_apis Timer APIs
1131 * @ingroup kernel_apis
1132 * @{
1133 */
1134
1135/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001136 * @typedef k_timer_expiry_t
1137 * @brief Timer expiry function type.
1138 *
1139 * A timer's expiry function is executed by the system clock interrupt handler
1140 * each time the timer expires. The expiry function is optional, and is only
1141 * invoked if the timer has been initialized with one.
1142 *
1143 * @param timer Address of timer.
1144 *
1145 * @return N/A
1146 */
1147typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1148
1149/**
1150 * @typedef k_timer_stop_t
1151 * @brief Timer stop function type.
1152 *
1153 * A timer's stop function is executed if the timer is stopped prematurely.
1154 * The function runs in the context of the thread that stops the timer.
1155 * The stop function is optional, and is only invoked if the timer has been
1156 * initialized with one.
1157 *
1158 * @param timer Address of timer.
1159 *
1160 * @return N/A
1161 */
1162typedef void (*k_timer_stop_t)(struct k_timer *timer);
1163
1164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001169 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
1171 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001172 * @param expiry_fn Function to invoke each time the timer expires.
1173 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001175#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001176 struct k_timer name \
1177 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001178 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179
Allan Stephens45bfa372016-10-12 12:39:42 -05001180/**
1181 * @brief Initialize a timer.
1182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001184 *
1185 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @param expiry_fn Function to invoke each time the timer expires.
1187 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001188 *
1189 * @return N/A
1190 */
1191extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001192 k_timer_expiry_t expiry_fn,
1193 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001194
Allan Stephens45bfa372016-10-12 12:39:42 -05001195/**
1196 * @brief Start a timer.
1197 *
1198 * This routine starts a timer, and resets its status to zero. The timer
1199 * begins counting down using the specified duration and period values.
1200 *
1201 * Attempting to start a timer that is already running is permitted.
1202 * The timer's status is reset to zero and the timer begins counting down
1203 * using the new duration and period values.
1204 *
1205 * @param timer Address of timer.
1206 * @param duration Initial timer duration (in milliseconds).
1207 * @param period Timer period (in milliseconds).
1208 *
1209 * @return N/A
1210 */
Andrew Boiea354d492017-09-29 16:22:28 -07001211__syscall void k_timer_start(struct k_timer *timer,
1212 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001213
1214/**
1215 * @brief Stop a timer.
1216 *
1217 * This routine stops a running timer prematurely. The timer's stop function,
1218 * if one exists, is invoked by the caller.
1219 *
1220 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001222 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001223 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1224 * if @a k_timer_stop is to be called from ISRs.
1225 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001226 * @param timer Address of timer.
1227 *
1228 * @return N/A
1229 */
Andrew Boiea354d492017-09-29 16:22:28 -07001230__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001231
1232/**
1233 * @brief Read timer status.
1234 *
1235 * This routine reads the timer's status, which indicates the number of times
1236 * it has expired since its status was last read.
1237 *
1238 * Calling this routine resets the timer's status to zero.
1239 *
1240 * @param timer Address of timer.
1241 *
1242 * @return Timer status.
1243 */
Andrew Boiea354d492017-09-29 16:22:28 -07001244__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001245
1246/**
1247 * @brief Synchronize thread to timer expiration.
1248 *
1249 * This routine blocks the calling thread until the timer's status is non-zero
1250 * (indicating that it has expired at least once since it was last examined)
1251 * or the timer is stopped. If the timer status is already non-zero,
1252 * or the timer is already stopped, the caller continues without waiting.
1253 *
1254 * Calling this routine resets the timer's status to zero.
1255 *
1256 * This routine must not be used by interrupt handlers, since they are not
1257 * allowed to block.
1258 *
1259 * @param timer Address of timer.
1260 *
1261 * @return Timer status.
1262 */
Andrew Boiea354d492017-09-29 16:22:28 -07001263__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001264
1265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001266 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001267 *
1268 * This routine computes the (approximate) time remaining before a running
1269 * timer next expires. If the timer is not running, it returns zero.
1270 *
1271 * @param timer Address of timer.
1272 *
1273 * @return Remaining time (in milliseconds).
1274 */
Andrew Boiea354d492017-09-29 16:22:28 -07001275__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1276
1277static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001278{
1279 return _timeout_remaining_get(&timer->timeout);
1280}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001281
Allan Stephensc98da842016-11-11 15:45:03 -05001282/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001283 * @brief Associate user-specific data with a timer.
1284 *
1285 * This routine records the @a user_data with the @a timer, to be retrieved
1286 * later.
1287 *
1288 * It can be used e.g. in a timer handler shared across multiple subsystems to
1289 * retrieve data specific to the subsystem this timer is associated with.
1290 *
1291 * @param timer Address of timer.
1292 * @param user_data User data to associate with the timer.
1293 *
1294 * @return N/A
1295 */
Andrew Boiea354d492017-09-29 16:22:28 -07001296__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1297
1298static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1299 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001300{
1301 timer->user_data = user_data;
1302}
1303
1304/**
1305 * @brief Retrieve the user-specific data from a timer.
1306 *
1307 * @param timer Address of timer.
1308 *
1309 * @return The user data.
1310 */
Andrew Boiea354d492017-09-29 16:22:28 -07001311__syscall void *k_timer_user_data_get(struct k_timer *timer);
1312
1313static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001314{
1315 return timer->user_data;
1316}
1317
1318/**
Allan Stephensc98da842016-11-11 15:45:03 -05001319 * @} end defgroup timer_apis
1320 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001321
Allan Stephensc98da842016-11-11 15:45:03 -05001322/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001323 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001324 * @{
1325 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001326
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001327/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001328 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001330 * This routine returns the elapsed time since the system booted,
1331 * in milliseconds.
1332 *
1333 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001334 */
Kumar Galacc334c72017-04-21 10:55:34 -05001335extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001336
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001337#ifdef CONFIG_TICKLESS_KERNEL
1338/**
1339 * @brief Enable clock always on in tickless kernel
1340 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001341 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001342 * there are no timer events programmed in tickless kernel
1343 * scheduling. This is necessary if the clock is used to track
1344 * passage of time.
1345 *
1346 * @retval prev_status Previous status of always on flag
1347 */
1348static inline int k_enable_sys_clock_always_on(void)
1349{
1350 int prev_status = _sys_clock_always_on;
1351
1352 _sys_clock_always_on = 1;
1353 _enable_sys_clock();
1354
1355 return prev_status;
1356}
1357
1358/**
1359 * @brief Disable clock always on in tickless kernel
1360 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001361 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001362 * there are no timer events programmed in tickless kernel
1363 * scheduling. To save power, this routine should be called
1364 * immediately when clock is not used to track time.
1365 */
1366static inline void k_disable_sys_clock_always_on(void)
1367{
1368 _sys_clock_always_on = 0;
1369}
1370#else
1371#define k_enable_sys_clock_always_on() do { } while ((0))
1372#define k_disable_sys_clock_always_on() do { } while ((0))
1373#endif
1374
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * This routine returns the lower 32-bits of the elapsed time since the system
1379 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381 * This routine can be more efficient than k_uptime_get(), as it reduces the
1382 * need for interrupt locking and 64-bit math. However, the 32-bit result
1383 * cannot hold a system uptime time larger than approximately 50 days, so the
1384 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001387 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001388__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001389
1390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * This routine computes the elapsed time between the current system uptime
1394 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * @param reftime Pointer to a reference time, which is updated to the current
1397 * uptime upon return.
1398 *
1399 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001400 */
Kumar Galacc334c72017-04-21 10:55:34 -05001401extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001402
1403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001404 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * This routine computes the elapsed time between the current system uptime
1407 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1410 * need for interrupt locking and 64-bit math. However, the 32-bit result
1411 * cannot hold an elapsed time larger than approximately 50 days, so the
1412 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001414 * @param reftime Pointer to a reference time, which is updated to the current
1415 * uptime upon return.
1416 *
1417 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001418 */
Kumar Galacc334c72017-04-21 10:55:34 -05001419extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001420
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001421/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001422 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * This routine returns the current time, as measured by the system's hardware
1425 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001427 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001428 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001429#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001432 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433 */
1434
Allan Stephensc98da842016-11-11 15:45:03 -05001435/**
1436 * @cond INTERNAL_HIDDEN
1437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001439struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001440 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001441 union {
1442 _wait_q_t wait_q;
1443
1444 _POLL_EVENT;
1445 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001446
1447 _OBJECT_TRACING_NEXT_PTR(k_queue);
1448};
1449
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001450#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001451 { \
1452 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1453 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001454 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001455 _OBJECT_TRACING_INIT \
1456 }
1457
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001458#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1459
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001460/**
1461 * INTERNAL_HIDDEN @endcond
1462 */
1463
1464/**
1465 * @defgroup queue_apis Queue APIs
1466 * @ingroup kernel_apis
1467 * @{
1468 */
1469
1470/**
1471 * @brief Initialize a queue.
1472 *
1473 * This routine initializes a queue object, prior to its first use.
1474 *
1475 * @param queue Address of the queue.
1476 *
1477 * @return N/A
1478 */
1479extern void k_queue_init(struct k_queue *queue);
1480
1481/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001482 * @brief Cancel waiting on a queue.
1483 *
1484 * This routine causes first thread pending on @a queue, if any, to
1485 * return from k_queue_get() call with NULL value (as if timeout expired).
1486 *
1487 * @note Can be called by ISRs.
1488 *
1489 * @param queue Address of the queue.
1490 *
1491 * @return N/A
1492 */
1493extern void k_queue_cancel_wait(struct k_queue *queue);
1494
1495/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001496 * @brief Append an element to the end of a queue.
1497 *
1498 * This routine appends a data item to @a queue. A queue data item must be
1499 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1500 * reserved for the kernel's use.
1501 *
1502 * @note Can be called by ISRs.
1503 *
1504 * @param queue Address of the queue.
1505 * @param data Address of the data item.
1506 *
1507 * @return N/A
1508 */
1509extern void k_queue_append(struct k_queue *queue, void *data);
1510
1511/**
1512 * @brief Prepend an element to a queue.
1513 *
1514 * This routine prepends a data item to @a queue. A queue data item must be
1515 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1516 * reserved for the kernel's use.
1517 *
1518 * @note Can be called by ISRs.
1519 *
1520 * @param queue Address of the queue.
1521 * @param data Address of the data item.
1522 *
1523 * @return N/A
1524 */
1525extern void k_queue_prepend(struct k_queue *queue, void *data);
1526
1527/**
1528 * @brief Inserts an element to a queue.
1529 *
1530 * This routine inserts a data item to @a queue after previous item. A queue
1531 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1532 * item are reserved for the kernel's use.
1533 *
1534 * @note Can be called by ISRs.
1535 *
1536 * @param queue Address of the queue.
1537 * @param prev Address of the previous data item.
1538 * @param data Address of the data item.
1539 *
1540 * @return N/A
1541 */
1542extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1543
1544/**
1545 * @brief Atomically append a list of elements to a queue.
1546 *
1547 * This routine adds a list of data items to @a queue in one operation.
1548 * The data items must be in a singly-linked list, with the first 32 bits
1549 * in each data item pointing to the next data item; the list must be
1550 * NULL-terminated.
1551 *
1552 * @note Can be called by ISRs.
1553 *
1554 * @param queue Address of the queue.
1555 * @param head Pointer to first node in singly-linked list.
1556 * @param tail Pointer to last node in singly-linked list.
1557 *
1558 * @return N/A
1559 */
1560extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1561
1562/**
1563 * @brief Atomically add a list of elements to a queue.
1564 *
1565 * This routine adds a list of data items to @a queue in one operation.
1566 * The data items must be in a singly-linked list implemented using a
1567 * sys_slist_t object. Upon completion, the original list is empty.
1568 *
1569 * @note Can be called by ISRs.
1570 *
1571 * @param queue Address of the queue.
1572 * @param list Pointer to sys_slist_t object.
1573 *
1574 * @return N/A
1575 */
1576extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1577
1578/**
1579 * @brief Get an element from a queue.
1580 *
1581 * This routine removes first data item from @a queue. The first 32 bits of the
1582 * data item are reserved for the kernel's use.
1583 *
1584 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1585 *
1586 * @param queue Address of the queue.
1587 * @param timeout Waiting period to obtain a data item (in milliseconds),
1588 * or one of the special values K_NO_WAIT and K_FOREVER.
1589 *
1590 * @return Address of the data item if successful; NULL if returned
1591 * without waiting, or waiting period timed out.
1592 */
Kumar Galacc334c72017-04-21 10:55:34 -05001593extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001594
1595/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001596 * @brief Remove an element from a queue.
1597 *
1598 * This routine removes data item from @a queue. The first 32 bits of the
1599 * data item are reserved for the kernel's use. Removing elements from k_queue
1600 * rely on sys_slist_find_and_remove which is not a constant time operation.
1601 *
1602 * @note Can be called by ISRs
1603 *
1604 * @param queue Address of the queue.
1605 * @param data Address of the data item.
1606 *
1607 * @return true if data item was removed
1608 */
1609static inline bool k_queue_remove(struct k_queue *queue, void *data)
1610{
1611 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1612}
1613
1614/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001615 * @brief Query a queue to see if it has data available.
1616 *
1617 * Note that the data might be already gone by the time this function returns
1618 * if other threads are also trying to read from the queue.
1619 *
1620 * @note Can be called by ISRs.
1621 *
1622 * @param queue Address of the queue.
1623 *
1624 * @return Non-zero if the queue is empty.
1625 * @return 0 if data is available.
1626 */
1627static inline int k_queue_is_empty(struct k_queue *queue)
1628{
1629 return (int)sys_slist_is_empty(&queue->data_q);
1630}
1631
1632/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001633 * @brief Peek element at the head of queue.
1634 *
1635 * Return element from the head of queue without removing it.
1636 *
1637 * @param queue Address of the queue.
1638 *
1639 * @return Head element, or NULL if queue is empty.
1640 */
1641static inline void *k_queue_peek_head(struct k_queue *queue)
1642{
1643 return sys_slist_peek_head(&queue->data_q);
1644}
1645
1646/**
1647 * @brief Peek element at the tail of queue.
1648 *
1649 * Return element from the tail of queue without removing it.
1650 *
1651 * @param queue Address of the queue.
1652 *
1653 * @return Tail element, or NULL if queue is empty.
1654 */
1655static inline void *k_queue_peek_tail(struct k_queue *queue)
1656{
1657 return sys_slist_peek_tail(&queue->data_q);
1658}
1659
1660/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001661 * @brief Statically define and initialize a queue.
1662 *
1663 * The queue can be accessed outside the module where it is defined using:
1664 *
1665 * @code extern struct k_queue <name>; @endcode
1666 *
1667 * @param name Name of the queue.
1668 */
1669#define K_QUEUE_DEFINE(name) \
1670 struct k_queue name \
1671 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001672 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001673
1674/**
1675 * @} end defgroup queue_apis
1676 */
1677
1678/**
1679 * @cond INTERNAL_HIDDEN
1680 */
1681
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001682struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001683 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001684};
1685
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001686#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001687 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001688 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001689 }
1690
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001691#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1692
Allan Stephensc98da842016-11-11 15:45:03 -05001693/**
1694 * INTERNAL_HIDDEN @endcond
1695 */
1696
1697/**
1698 * @defgroup fifo_apis Fifo APIs
1699 * @ingroup kernel_apis
1700 * @{
1701 */
1702
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
1710 * @return N/A
1711 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001712#define k_fifo_init(fifo) \
1713 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714
1715/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001716 * @brief Cancel waiting on a fifo.
1717 *
1718 * This routine causes first thread pending on @a fifo, if any, to
1719 * return from k_fifo_get() call with NULL value (as if timeout
1720 * expired).
1721 *
1722 * @note Can be called by ISRs.
1723 *
1724 * @param fifo Address of the fifo.
1725 *
1726 * @return N/A
1727 */
1728#define k_fifo_cancel_wait(fifo) \
1729 k_queue_cancel_wait((struct k_queue *) fifo)
1730
1731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine adds a data item to @a fifo. A fifo data item must be
1735 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1736 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @note Can be called by ISRs.
1739 *
1740 * @param fifo Address of the fifo.
1741 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 *
1743 * @return N/A
1744 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001745#define k_fifo_put(fifo, data) \
1746 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001747
1748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * This routine adds a list of data items to @a fifo in one operation.
1752 * The data items must be in a singly-linked list, with the first 32 bits
1753 * each data item pointing to the next data item; the list must be
1754 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001756 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @param fifo Address of the fifo.
1759 * @param head Pointer to first node in singly-linked list.
1760 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761 *
1762 * @return N/A
1763 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001764#define k_fifo_put_list(fifo, head, tail) \
1765 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766
1767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001770 * This routine adds a list of data items to @a fifo in one operation.
1771 * The data items must be in a singly-linked list implemented using a
1772 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773 * and must be re-initialized via sys_slist_init().
1774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * @note Can be called by ISRs.
1776 *
1777 * @param fifo Address of the fifo.
1778 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 *
1780 * @return N/A
1781 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001782#define k_fifo_put_slist(fifo, list) \
1783 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001784
1785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001786 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * This routine removes a data item from @a fifo in a "first in, first out"
1789 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1792 *
1793 * @param fifo Address of the fifo.
1794 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 * or one of the special values K_NO_WAIT and K_FOREVER.
1796 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001797 * @return Address of the data item if successful; NULL if returned
1798 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001800#define k_fifo_get(fifo, timeout) \
1801 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001804 * @brief Query a fifo to see if it has data available.
1805 *
1806 * Note that the data might be already gone by the time this function returns
1807 * if other threads is also trying to read from the fifo.
1808 *
1809 * @note Can be called by ISRs.
1810 *
1811 * @param fifo Address of the fifo.
1812 *
1813 * @return Non-zero if the fifo is empty.
1814 * @return 0 if data is available.
1815 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001816#define k_fifo_is_empty(fifo) \
1817 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001818
1819/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001820 * @brief Peek element at the head of fifo.
1821 *
1822 * Return element from the head of fifo without removing it. A usecase
1823 * for this is if elements of the fifo are themselves containers. Then
1824 * on each iteration of processing, a head container will be peeked,
1825 * and some data processed out of it, and only if the container is empty,
1826 * it will be completely remove from the fifo.
1827 *
1828 * @param fifo Address of the fifo.
1829 *
1830 * @return Head element, or NULL if the fifo is empty.
1831 */
1832#define k_fifo_peek_head(fifo) \
1833 k_queue_peek_head((struct k_queue *) fifo)
1834
1835/**
1836 * @brief Peek element at the tail of fifo.
1837 *
1838 * Return element from the tail of fifo (without removing it). A usecase
1839 * for this is if elements of the fifo are themselves containers. Then
1840 * it may be useful to add more data to the last container in fifo.
1841 *
1842 * @param fifo Address of the fifo.
1843 *
1844 * @return Tail element, or NULL if fifo is empty.
1845 */
1846#define k_fifo_peek_tail(fifo) \
1847 k_queue_peek_tail((struct k_queue *) fifo)
1848
1849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001854 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001857 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001859 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001860 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001861 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001862
Allan Stephensc98da842016-11-11 15:45:03 -05001863/**
1864 * @} end defgroup fifo_apis
1865 */
1866
1867/**
1868 * @cond INTERNAL_HIDDEN
1869 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870
1871struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001872 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001873};
1874
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001875#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001876 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001877 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001878 }
1879
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001880#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1881
Allan Stephensc98da842016-11-11 15:45:03 -05001882/**
1883 * INTERNAL_HIDDEN @endcond
1884 */
1885
1886/**
1887 * @defgroup lifo_apis Lifo APIs
1888 * @ingroup kernel_apis
1889 * @{
1890 */
1891
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001895 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 *
1899 * @return N/A
1900 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001901#define k_lifo_init(lifo) \
1902 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903
1904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * This routine adds a data item to @a lifo. A lifo data item must be
1908 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1909 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001911 * @note Can be called by ISRs.
1912 *
1913 * @param lifo Address of the lifo.
1914 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915 *
1916 * @return N/A
1917 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001918#define k_lifo_put(lifo, data) \
1919 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920
1921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * This routine removes a data item from @a lifo in a "last in, first out"
1925 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1928 *
1929 * @param lifo Address of the lifo.
1930 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001931 * or one of the special values K_NO_WAIT and K_FOREVER.
1932 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001933 * @return Address of the data item if successful; NULL if returned
1934 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001936#define k_lifo_get(lifo, timeout) \
1937 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001944 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001949 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001950 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001951 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952
Allan Stephensc98da842016-11-11 15:45:03 -05001953/**
1954 * @} end defgroup lifo_apis
1955 */
1956
1957/**
1958 * @cond INTERNAL_HIDDEN
1959 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960
1961struct k_stack {
1962 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001963 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964
Anas Nashif2f203c22016-12-18 06:57:45 -05001965 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966};
1967
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001968#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001969 { \
1970 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1971 .base = stack_buffer, \
1972 .next = stack_buffer, \
1973 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001974 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001975 }
1976
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001977#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1978
Allan Stephensc98da842016-11-11 15:45:03 -05001979/**
1980 * INTERNAL_HIDDEN @endcond
1981 */
1982
1983/**
1984 * @defgroup stack_apis Stack APIs
1985 * @ingroup kernel_apis
1986 * @{
1987 */
1988
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989/**
1990 * @brief Initialize a stack.
1991 *
1992 * This routine initializes a stack object, prior to its first use.
1993 *
1994 * @param stack Address of the stack.
1995 * @param buffer Address of array used to hold stacked values.
1996 * @param num_entries Maximum number of values that can be stacked.
1997 *
1998 * @return N/A
1999 */
Andrew Boiee8734462017-09-29 16:42:07 -07002000__syscall void k_stack_init(struct k_stack *stack,
2001 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002
2003/**
2004 * @brief Push an element onto a stack.
2005 *
2006 * This routine adds a 32-bit value @a data to @a stack.
2007 *
2008 * @note Can be called by ISRs.
2009 *
2010 * @param stack Address of the stack.
2011 * @param data Value to push onto the stack.
2012 *
2013 * @return N/A
2014 */
Andrew Boiee8734462017-09-29 16:42:07 -07002015__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002016
2017/**
2018 * @brief Pop an element from a stack.
2019 *
2020 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2021 * manner and stores the value in @a data.
2022 *
2023 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2024 *
2025 * @param stack Address of the stack.
2026 * @param data Address of area to hold the value popped from the stack.
2027 * @param timeout Waiting period to obtain a value (in milliseconds),
2028 * or one of the special values K_NO_WAIT and K_FOREVER.
2029 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002030 * @retval 0 Element popped from stack.
2031 * @retval -EBUSY Returned without waiting.
2032 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 */
Andrew Boiee8734462017-09-29 16:42:07 -07002034__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002035
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002041 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002043 * @param name Name of the stack.
2044 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002046#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002047 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002048 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002049 struct k_stack name \
2050 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002051 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002052 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002054/**
Allan Stephensc98da842016-11-11 15:45:03 -05002055 * @} end defgroup stack_apis
2056 */
2057
Allan Stephens6bba9b02016-11-16 14:56:54 -05002058struct k_work;
2059
Allan Stephensc98da842016-11-11 15:45:03 -05002060/**
2061 * @defgroup workqueue_apis Workqueue Thread APIs
2062 * @ingroup kernel_apis
2063 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002064 */
2065
Allan Stephens6bba9b02016-11-16 14:56:54 -05002066/**
2067 * @typedef k_work_handler_t
2068 * @brief Work item handler function type.
2069 *
2070 * A work item's handler function is executed by a workqueue's thread
2071 * when the work item is processed by the workqueue.
2072 *
2073 * @param work Address of the work item.
2074 *
2075 * @return N/A
2076 */
2077typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078
2079/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002080 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002082
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002084 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002085 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002086};
2087
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002089 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090};
2091
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002093 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094 k_work_handler_t handler;
2095 atomic_t flags[1];
2096};
2097
Allan Stephens6bba9b02016-11-16 14:56:54 -05002098struct k_delayed_work {
2099 struct k_work work;
2100 struct _timeout timeout;
2101 struct k_work_q *work_q;
2102};
2103
2104extern struct k_work_q k_sys_work_q;
2105
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002107 * INTERNAL_HIDDEN @endcond
2108 */
2109
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002110#define _K_WORK_INITIALIZER(work_handler) \
2111 { \
2112 ._reserved = NULL, \
2113 .handler = work_handler, \
2114 .flags = { 0 } \
2115 }
2116
2117#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2118
Allan Stephens6bba9b02016-11-16 14:56:54 -05002119/**
2120 * @brief Initialize a statically-defined work item.
2121 *
2122 * This macro can be used to initialize a statically-defined workqueue work
2123 * item, prior to its first use. For example,
2124 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002125 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002126 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002127 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002128 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002130#define K_WORK_DEFINE(work, work_handler) \
2131 struct k_work work \
2132 __in_section(_k_work, static, work) = \
2133 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002134
2135/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002136 * @brief Initialize a work item.
2137 *
2138 * This routine initializes a workqueue work item, prior to its first use.
2139 *
2140 * @param work Address of work item.
2141 * @param handler Function to invoke each time work item is processed.
2142 *
2143 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144 */
2145static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2146{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002147 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002149 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150}
2151
2152/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002153 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002154 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002155 * This routine submits work item @a work to be processed by workqueue
2156 * @a work_q. If the work item is already pending in the workqueue's queue
2157 * as a result of an earlier submission, this routine has no effect on the
2158 * work item. If the work item has already been processed, or is currently
2159 * being processed, its work is considered complete and the work item can be
2160 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002161 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002162 * @warning
2163 * A submitted work item must not be modified until it has been processed
2164 * by the workqueue.
2165 *
2166 * @note Can be called by ISRs.
2167 *
2168 * @param work_q Address of workqueue.
2169 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002170 *
2171 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002172 */
2173static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2174 struct k_work *work)
2175{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002176 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002177 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178 }
2179}
2180
2181/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002182 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * This routine indicates if work item @a work is pending in a workqueue's
2185 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002187 * @note Can be called by ISRs.
2188 *
2189 * @param work Address of work item.
2190 *
2191 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002192 */
2193static inline int k_work_pending(struct k_work *work)
2194{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002195 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002196}
2197
2198/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002199 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002200 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002201 * This routine starts workqueue @a work_q. The workqueue spawns its work
2202 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002204 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002205 * @param stack Pointer to work queue thread's stack space, as defined by
2206 * K_THREAD_STACK_DEFINE()
2207 * @param stack_size Size of the work queue thread's stack (in bytes), which
2208 * should either be the same constant passed to
2209 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002210 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 *
2212 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213 */
Andrew Boie507852a2017-07-25 18:47:07 -07002214extern void k_work_q_start(struct k_work_q *work_q,
2215 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002216 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002219 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221 * This routine initializes a workqueue delayed work item, prior to
2222 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002224 * @param work Address of delayed work item.
2225 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 *
2227 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002229extern void k_delayed_work_init(struct k_delayed_work *work,
2230 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231
2232/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002233 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002235 * This routine schedules work item @a work to be processed by workqueue
2236 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002237 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238 * Only when the countdown completes is the work item actually submitted to
2239 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002241 * Submitting a previously submitted delayed work item that is still
2242 * counting down cancels the existing submission and restarts the countdown
2243 * using the new delay. If the work item is currently pending on the
2244 * workqueue's queue because the countdown has completed it is too late to
2245 * resubmit the item, and resubmission fails without impacting the work item.
2246 * If the work item has already been processed, or is currently being processed,
2247 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * @warning
2250 * A delayed work item must not be modified until it has been processed
2251 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 * @note Can be called by ISRs.
2254 *
2255 * @param work_q Address of workqueue.
2256 * @param work Address of delayed work item.
2257 * @param delay Delay before submitting the work item (in milliseconds).
2258 *
2259 * @retval 0 Work item countdown started.
2260 * @retval -EINPROGRESS Work item is already pending.
2261 * @retval -EINVAL Work item is being processed or has completed its work.
2262 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002264extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2265 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002266 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267
2268/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002269 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002271 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002272 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002275 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002277 * @param work Address of delayed work item.
2278 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002279 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002280 * @retval -EINPROGRESS Work item is already pending.
2281 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002283extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002286 * @brief Submit a work item to the system workqueue.
2287 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002288 * This routine submits work item @a work to be processed by the system
2289 * workqueue. If the work item is already pending in the workqueue's queue
2290 * as a result of an earlier submission, this routine has no effect on the
2291 * work item. If the work item has already been processed, or is currently
2292 * being processed, its work is considered complete and the work item can be
2293 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295 * @warning
2296 * Work items submitted to the system workqueue should avoid using handlers
2297 * that block or yield since this may prevent the system workqueue from
2298 * processing other work items in a timely manner.
2299 *
2300 * @note Can be called by ISRs.
2301 *
2302 * @param work Address of work item.
2303 *
2304 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 */
2306static inline void k_work_submit(struct k_work *work)
2307{
2308 k_work_submit_to_queue(&k_sys_work_q, work);
2309}
2310
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 * @brief Submit a delayed work item to the system workqueue.
2313 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002314 * This routine schedules work item @a work to be processed by the system
2315 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002316 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002317 * Only when the countdown completes is the work item actually submitted to
2318 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002320 * Submitting a previously submitted delayed work item that is still
2321 * counting down cancels the existing submission and restarts the countdown
2322 * using the new delay. If the work item is currently pending on the
2323 * workqueue's queue because the countdown has completed it is too late to
2324 * resubmit the item, and resubmission fails without impacting the work item.
2325 * If the work item has already been processed, or is currently being processed,
2326 * its work is considered complete and the work item can be resubmitted.
2327 *
2328 * @warning
2329 * Work items submitted to the system workqueue should avoid using handlers
2330 * that block or yield since this may prevent the system workqueue from
2331 * processing other work items in a timely manner.
2332 *
2333 * @note Can be called by ISRs.
2334 *
2335 * @param work Address of delayed work item.
2336 * @param delay Delay before submitting the work item (in milliseconds).
2337 *
2338 * @retval 0 Work item countdown started.
2339 * @retval -EINPROGRESS Work item is already pending.
2340 * @retval -EINVAL Work item is being processed or has completed its work.
2341 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 */
2343static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002344 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002346 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347}
2348
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002350 * @brief Get time remaining before a delayed work gets scheduled.
2351 *
2352 * This routine computes the (approximate) time remaining before a
2353 * delayed work gets executed. If the delayed work is not waiting to be
2354 * schedules, it returns zero.
2355 *
2356 * @param work Delayed work item.
2357 *
2358 * @return Remaining time (in milliseconds).
2359 */
Kumar Galacc334c72017-04-21 10:55:34 -05002360static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002361{
2362 return _timeout_remaining_get(&work->timeout);
2363}
2364
2365/**
Allan Stephensc98da842016-11-11 15:45:03 -05002366 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 */
2368
Allan Stephensc98da842016-11-11 15:45:03 -05002369/**
2370 * @cond INTERNAL_HIDDEN
2371 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372
2373struct k_mutex {
2374 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002375 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002376 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378
Anas Nashif2f203c22016-12-18 06:57:45 -05002379 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380};
2381
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002382#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002383 { \
2384 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2385 .owner = NULL, \
2386 .lock_count = 0, \
2387 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002388 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389 }
2390
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002391#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2392
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393/**
Allan Stephensc98da842016-11-11 15:45:03 -05002394 * INTERNAL_HIDDEN @endcond
2395 */
2396
2397/**
2398 * @defgroup mutex_apis Mutex APIs
2399 * @ingroup kernel_apis
2400 * @{
2401 */
2402
2403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002406 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002408 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002413 struct k_mutex name \
2414 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002415 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * Upon completion, the mutex is available and does not have an owner.
2423 *
2424 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 *
2426 * @return N/A
2427 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002428__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429
2430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * This routine locks @a mutex. If the mutex is locked by another thread,
2434 * the calling thread waits until the mutex becomes available or until
2435 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * A thread is permitted to lock a mutex it has already locked. The operation
2438 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002440 * @param mutex Address of the mutex.
2441 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442 * or one of the special values K_NO_WAIT and K_FOREVER.
2443 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002444 * @retval 0 Mutex locked.
2445 * @retval -EBUSY Returned without waiting.
2446 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002448__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449
2450/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * This routine unlocks @a mutex. The mutex must already be locked by the
2454 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 *
2456 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002457 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458 * thread.
2459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002460 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
2462 * @return N/A
2463 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002464__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465
Allan Stephensc98da842016-11-11 15:45:03 -05002466/**
2467 * @} end defgroup mutex_apis
2468 */
2469
2470/**
2471 * @cond INTERNAL_HIDDEN
2472 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473
2474struct k_sem {
2475 _wait_q_t wait_q;
2476 unsigned int count;
2477 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002478 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479
Anas Nashif2f203c22016-12-18 06:57:45 -05002480 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481};
2482
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002483#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002484 { \
2485 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2486 .count = initial_count, \
2487 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002488 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002489 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002490 }
2491
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002492#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2493
Allan Stephensc98da842016-11-11 15:45:03 -05002494/**
2495 * INTERNAL_HIDDEN @endcond
2496 */
2497
2498/**
2499 * @defgroup semaphore_apis Semaphore APIs
2500 * @ingroup kernel_apis
2501 * @{
2502 */
2503
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002504/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * @param sem Address of the semaphore.
2510 * @param initial_count Initial semaphore count.
2511 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512 *
2513 * @return N/A
2514 */
Andrew Boie99280232017-09-29 14:17:47 -07002515__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2516 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002517
2518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2524 *
2525 * @param sem Address of the semaphore.
2526 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002527 * or one of the special values K_NO_WAIT and K_FOREVER.
2528 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002529 * @note When porting code from the nanokernel legacy API to the new API, be
2530 * careful with the return value of this function. The return value is the
2531 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2532 * non-zero means failure, while the nano_sem_take family returns 1 for success
2533 * and 0 for failure.
2534 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002535 * @retval 0 Semaphore taken.
2536 * @retval -EBUSY Returned without waiting.
2537 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002538 */
Andrew Boie99280232017-09-29 14:17:47 -07002539__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002540
2541/**
2542 * @brief Give a semaphore.
2543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * This routine gives @a sem, unless the semaphore is already at its maximum
2545 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002550 *
2551 * @return N/A
2552 */
Andrew Boie99280232017-09-29 14:17:47 -07002553__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002555/**
2556 * @brief Reset a semaphore's count to zero.
2557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002561 *
2562 * @return N/A
2563 */
Andrew Boie990bf162017-10-03 12:36:49 -07002564__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002565
2566static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567{
2568 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569}
2570
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002571/**
2572 * @brief Get a semaphore's count.
2573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002579 */
Andrew Boie990bf162017-10-03 12:36:49 -07002580__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002581
2582static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583{
2584 return sem->count;
2585}
2586
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002591 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002592 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * @param name Name of the semaphore.
2595 * @param initial_count Initial semaphore count.
2596 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002599 struct k_sem name \
2600 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002601 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602
Allan Stephensc98da842016-11-11 15:45:03 -05002603/**
2604 * @} end defgroup semaphore_apis
2605 */
2606
2607/**
2608 * @defgroup alert_apis Alert APIs
2609 * @ingroup kernel_apis
2610 * @{
2611 */
2612
Allan Stephens5eceb852016-11-16 10:16:30 -05002613/**
2614 * @typedef k_alert_handler_t
2615 * @brief Alert handler function type.
2616 *
2617 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002618 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002619 * and is only invoked if the alert has been initialized with one.
2620 *
2621 * @param alert Address of the alert.
2622 *
2623 * @return 0 if alert has been consumed; non-zero if alert should pend.
2624 */
2625typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002626
2627/**
2628 * @} end defgroup alert_apis
2629 */
2630
2631/**
2632 * @cond INTERNAL_HIDDEN
2633 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002635#define K_ALERT_DEFAULT NULL
2636#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002638struct k_alert {
2639 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640 atomic_t send_count;
2641 struct k_work work_item;
2642 struct k_sem sem;
2643
Anas Nashif2f203c22016-12-18 06:57:45 -05002644 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645};
2646
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002647extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002648
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002649#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002650 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002651 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002653 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2654 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002655 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656 }
2657
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002658#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2659
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660/**
Allan Stephensc98da842016-11-11 15:45:03 -05002661 * INTERNAL_HIDDEN @endcond
2662 */
2663
2664/**
2665 * @addtogroup alert_apis
2666 * @{
2667 */
2668
2669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002672 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002674 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * @param name Name of the alert.
2677 * @param alert_handler Action to take when alert is sent. Specify either
2678 * the address of a function to be invoked by the system workqueue
2679 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2680 * K_ALERT_DEFAULT (which causes the alert to pend).
2681 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002683#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002684 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002685 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002686 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002687 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * @param alert Address of the alert.
2695 * @param handler Action to take when alert is sent. Specify either the address
2696 * of a function to be invoked by the system workqueue thread,
2697 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2698 * K_ALERT_DEFAULT (which causes the alert to pend).
2699 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002700 *
2701 * @return N/A
2702 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002703extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2704 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705
2706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002709 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2712 *
2713 * @param alert Address of the alert.
2714 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715 * or one of the special values K_NO_WAIT and K_FOREVER.
2716 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002717 * @retval 0 Alert received.
2718 * @retval -EBUSY Returned without waiting.
2719 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002720 */
Andrew Boie310e9872017-09-29 04:41:15 -07002721__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002722
2723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * This routine signals @a alert. The action specified for @a alert will
2727 * be taken, which may trigger the execution of an alert handler function
2728 * and/or cause the alert to pend (assuming the alert has not reached its
2729 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @note Can be called by ISRs.
2732 *
2733 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
2735 * @return N/A
2736 */
Andrew Boie310e9872017-09-29 04:41:15 -07002737__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738
2739/**
Allan Stephensc98da842016-11-11 15:45:03 -05002740 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741 */
2742
Allan Stephensc98da842016-11-11 15:45:03 -05002743/**
2744 * @cond INTERNAL_HIDDEN
2745 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746
2747struct k_msgq {
2748 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002749 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002750 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 char *buffer_start;
2752 char *buffer_end;
2753 char *read_ptr;
2754 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002755 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756
Anas Nashif2f203c22016-12-18 06:57:45 -05002757 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758};
2759
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002760#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 { \
2762 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002763 .max_msgs = q_max_msgs, \
2764 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002766 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 .read_ptr = q_buffer, \
2768 .write_ptr = q_buffer, \
2769 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002770 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 }
2772
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002773#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2774
Peter Mitsis1da807e2016-10-06 11:36:59 -04002775/**
Allan Stephensc98da842016-11-11 15:45:03 -05002776 * INTERNAL_HIDDEN @endcond
2777 */
2778
2779/**
2780 * @defgroup msgq_apis Message Queue APIs
2781 * @ingroup kernel_apis
2782 * @{
2783 */
2784
2785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2789 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002790 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2791 * message is similarly aligned to this boundary, @a q_msg_size must also be
2792 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * The message queue can be accessed outside the module where it is defined
2795 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002797 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * @param q_name Name of the message queue.
2800 * @param q_msg_size Message size (in bytes).
2801 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002802 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002803 */
2804#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2805 static char __noinit __aligned(q_align) \
2806 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002807 struct k_msgq q_name \
2808 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002809 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002810 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811
Peter Mitsisd7a37502016-10-13 11:37:40 -04002812/**
2813 * @brief Initialize a message queue.
2814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * This routine initializes a message queue object, prior to its first use.
2816 *
Allan Stephensda827222016-11-09 14:23:58 -06002817 * The message queue's ring buffer must contain space for @a max_msgs messages,
2818 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2819 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2820 * that each message is similarly aligned to this boundary, @a q_msg_size
2821 * must also be a multiple of N.
2822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @param q Address of the message queue.
2824 * @param buffer Pointer to ring buffer that holds queued messages.
2825 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002826 * @param max_msgs Maximum number of messages that can be queued.
2827 *
2828 * @return N/A
2829 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002830__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2831 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002832
2833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002838 * @note Can be called by ISRs.
2839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * @param q Address of the message queue.
2841 * @param data Pointer to the message.
2842 * @param timeout Waiting period to add the message (in milliseconds),
2843 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002845 * @retval 0 Message sent.
2846 * @retval -ENOMSG Returned without waiting or queue purged.
2847 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002848 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002849__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002850
2851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * This routine receives a message from message queue @a q in a "first in,
2855 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002856 *
Allan Stephensc98da842016-11-11 15:45:03 -05002857 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * @param q Address of the message queue.
2860 * @param data Address of area to hold the received message.
2861 * @param timeout Waiting period to receive the message (in milliseconds),
2862 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002863 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002864 * @retval 0 Message received.
2865 * @retval -ENOMSG Returned without waiting.
2866 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002867 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002868__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002869
2870/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * This routine discards all unreceived messages in a message queue's ring
2874 * buffer. Any threads that are blocked waiting to send a message to the
2875 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002878 *
2879 * @return N/A
2880 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002881__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882
Peter Mitsis67be2492016-10-07 11:44:34 -04002883/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * This routine returns the number of unused entries in a message queue's
2887 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * @param q Address of the message queue.
2890 *
2891 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002892 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002893__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2894
2895static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002896{
2897 return q->max_msgs - q->used_msgs;
2898}
2899
Peter Mitsisd7a37502016-10-13 11:37:40 -04002900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * @param q Address of the message queue.
2906 *
2907 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002908 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002909__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2910
2911static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912{
2913 return q->used_msgs;
2914}
2915
Allan Stephensc98da842016-11-11 15:45:03 -05002916/**
2917 * @} end defgroup msgq_apis
2918 */
2919
2920/**
2921 * @defgroup mem_pool_apis Memory Pool APIs
2922 * @ingroup kernel_apis
2923 * @{
2924 */
2925
Andy Ross73cb9582017-05-09 10:42:39 -07002926/* Note on sizing: the use of a 20 bit field for block means that,
2927 * assuming a reasonable minimum block size of 16 bytes, we're limited
2928 * to 16M of memory managed by a single pool. Long term it would be
2929 * good to move to a variable bit size based on configuration.
2930 */
2931struct k_mem_block_id {
2932 u32_t pool : 8;
2933 u32_t level : 4;
2934 u32_t block : 20;
2935};
2936
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002939 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940};
2941
Allan Stephensc98da842016-11-11 15:45:03 -05002942/**
2943 * @} end defgroup mem_pool_apis
2944 */
2945
2946/**
2947 * @defgroup mailbox_apis Mailbox APIs
2948 * @ingroup kernel_apis
2949 * @{
2950 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951
2952struct k_mbox_msg {
2953 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002954 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002956 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002958 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 /** sender's message data buffer */
2960 void *tx_data;
2961 /** internal use only - needed for legacy API support */
2962 void *_rx_data;
2963 /** message data block descriptor */
2964 struct k_mem_block tx_block;
2965 /** source thread id */
2966 k_tid_t rx_source_thread;
2967 /** target thread id */
2968 k_tid_t tx_target_thread;
2969 /** internal use only - thread waiting on send (may be a dummy) */
2970 k_tid_t _syncing_thread;
2971#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2972 /** internal use only - semaphore used during asynchronous send */
2973 struct k_sem *_async_sem;
2974#endif
2975};
2976
Allan Stephensc98da842016-11-11 15:45:03 -05002977/**
2978 * @cond INTERNAL_HIDDEN
2979 */
2980
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981struct k_mbox {
2982 _wait_q_t tx_msg_queue;
2983 _wait_q_t rx_msg_queue;
2984
Anas Nashif2f203c22016-12-18 06:57:45 -05002985 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986};
2987
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002988#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989 { \
2990 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2991 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002992 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993 }
2994
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002995#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2996
Peter Mitsis12092702016-10-14 12:57:23 -04002997/**
Allan Stephensc98da842016-11-11 15:45:03 -05002998 * INTERNAL_HIDDEN @endcond
2999 */
3000
3001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003005 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003006 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003009 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003011 struct k_mbox name \
3012 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003013 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014
Peter Mitsis12092702016-10-14 12:57:23 -04003015/**
3016 * @brief Initialize a mailbox.
3017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * This routine initializes a mailbox object, prior to its first use.
3019 *
3020 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003021 *
3022 * @return N/A
3023 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024extern void k_mbox_init(struct k_mbox *mbox);
3025
Peter Mitsis12092702016-10-14 12:57:23 -04003026/**
3027 * @brief Send a mailbox message in a synchronous manner.
3028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * This routine sends a message to @a mbox and waits for a receiver to both
3030 * receive and process it. The message data may be in a buffer, in a memory
3031 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param mbox Address of the mailbox.
3034 * @param tx_msg Address of the transmit message descriptor.
3035 * @param timeout Waiting period for the message to be received (in
3036 * milliseconds), or one of the special values K_NO_WAIT
3037 * and K_FOREVER. Once the message has been received,
3038 * this routine waits as long as necessary for the message
3039 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003040 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003041 * @retval 0 Message sent.
3042 * @retval -ENOMSG Returned without waiting.
3043 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003044 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003045extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003046 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003047
Peter Mitsis12092702016-10-14 12:57:23 -04003048/**
3049 * @brief Send a mailbox message in an asynchronous manner.
3050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * This routine sends a message to @a mbox without waiting for a receiver
3052 * to process it. The message data may be in a buffer, in a memory pool block,
3053 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3054 * will be given when the message has been both received and completely
3055 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * @param mbox Address of the mailbox.
3058 * @param tx_msg Address of the transmit message descriptor.
3059 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003060 *
3061 * @return N/A
3062 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003063extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064 struct k_sem *sem);
3065
Peter Mitsis12092702016-10-14 12:57:23 -04003066/**
3067 * @brief Receive a mailbox message.
3068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * This routine receives a message from @a mbox, then optionally retrieves
3070 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003072 * @param mbox Address of the mailbox.
3073 * @param rx_msg Address of the receive message descriptor.
3074 * @param buffer Address of the buffer to receive data, or NULL to defer data
3075 * retrieval and message disposal until later.
3076 * @param timeout Waiting period for a message to be received (in
3077 * milliseconds), or one of the special values K_NO_WAIT
3078 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003079 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003080 * @retval 0 Message received.
3081 * @retval -ENOMSG Returned without waiting.
3082 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003083 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003084extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003085 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003086
3087/**
3088 * @brief Retrieve mailbox message data into a buffer.
3089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * This routine completes the processing of a received message by retrieving
3091 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003092 *
3093 * Alternatively, this routine can be used to dispose of a received message
3094 * without retrieving its data.
3095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * @param rx_msg Address of the receive message descriptor.
3097 * @param buffer Address of the buffer to receive data, or NULL to discard
3098 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003099 *
3100 * @return N/A
3101 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003102extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003103
3104/**
3105 * @brief Retrieve mailbox message data into a memory pool block.
3106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003107 * This routine completes the processing of a received message by retrieving
3108 * its data into a memory pool block, then disposing of the message.
3109 * The memory pool block that results from successful retrieval must be
3110 * returned to the pool once the data has been processed, even in cases
3111 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003112 *
3113 * Alternatively, this routine can be used to dispose of a received message
3114 * without retrieving its data. In this case there is no need to return a
3115 * memory pool block to the pool.
3116 *
3117 * This routine allocates a new memory pool block for the data only if the
3118 * data is not already in one. If a new block cannot be allocated, the routine
3119 * returns a failure code and the received message is left unchanged. This
3120 * permits the caller to reattempt data retrieval at a later time or to dispose
3121 * of the received message without retrieving its data.
3122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @param rx_msg Address of a receive message descriptor.
3124 * @param pool Address of memory pool, or NULL to discard data.
3125 * @param block Address of the area to hold memory pool block info.
3126 * @param timeout Waiting period to wait for a memory pool block (in
3127 * milliseconds), or one of the special values K_NO_WAIT
3128 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003129 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003130 * @retval 0 Data retrieved.
3131 * @retval -ENOMEM Returned without waiting.
3132 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003133 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003134extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003135 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003136 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003137
Allan Stephensc98da842016-11-11 15:45:03 -05003138/**
3139 * @} end defgroup mailbox_apis
3140 */
3141
3142/**
3143 * @cond INTERNAL_HIDDEN
3144 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003145
3146struct k_pipe {
3147 unsigned char *buffer; /* Pipe buffer: may be NULL */
3148 size_t size; /* Buffer size */
3149 size_t bytes_used; /* # bytes used in buffer */
3150 size_t read_index; /* Where in buffer to read from */
3151 size_t write_index; /* Where in buffer to write */
3152
3153 struct {
3154 _wait_q_t readers; /* Reader wait queue */
3155 _wait_q_t writers; /* Writer wait queue */
3156 } wait_q;
3157
Anas Nashif2f203c22016-12-18 06:57:45 -05003158 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159};
3160
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003161#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162 { \
3163 .buffer = pipe_buffer, \
3164 .size = pipe_buffer_size, \
3165 .bytes_used = 0, \
3166 .read_index = 0, \
3167 .write_index = 0, \
3168 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3169 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003170 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003171 }
3172
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003173#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175/**
Allan Stephensc98da842016-11-11 15:45:03 -05003176 * INTERNAL_HIDDEN @endcond
3177 */
3178
3179/**
3180 * @defgroup pipe_apis Pipe APIs
3181 * @ingroup kernel_apis
3182 * @{
3183 */
3184
3185/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003189 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003190 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * @param name Name of the pipe.
3193 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3194 * or zero if no ring buffer is used.
3195 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003196 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003197#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3198 static unsigned char __noinit __aligned(pipe_align) \
3199 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003200 struct k_pipe name \
3201 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003202 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * This routine initializes a pipe object, prior to its first use.
3208 *
3209 * @param pipe Address of the pipe.
3210 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3211 * is used.
3212 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3213 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214 *
3215 * @return N/A
3216 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003217__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3218 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219
3220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @param pipe Address of the pipe.
3226 * @param data Address of data to write.
3227 * @param bytes_to_write Size of data (in bytes).
3228 * @param bytes_written Address of area to hold the number of bytes written.
3229 * @param min_xfer Minimum number of bytes to write.
3230 * @param timeout Waiting period to wait for the data to be written (in
3231 * milliseconds), or one of the special values K_NO_WAIT
3232 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003234 * @retval 0 At least @a min_xfer bytes of data were written.
3235 * @retval -EIO Returned without waiting; zero data bytes were written.
3236 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003239__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3240 size_t bytes_to_write, size_t *bytes_written,
3241 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242
3243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * @param pipe Address of the pipe.
3249 * @param data Address to place the data read from pipe.
3250 * @param bytes_to_read Maximum number of data bytes to read.
3251 * @param bytes_read Address of area to hold the number of bytes read.
3252 * @param min_xfer Minimum number of data bytes to read.
3253 * @param timeout Waiting period to wait for the data to be read (in
3254 * milliseconds), or one of the special values K_NO_WAIT
3255 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003257 * @retval 0 At least @a min_xfer bytes of data were read.
3258 * @retval -EIO Returned without waiting; zero data bytes were read.
3259 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003261 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003262__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3263 size_t bytes_to_read, size_t *bytes_read,
3264 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265
3266/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * This routine writes the data contained in a memory block to @a pipe.
3270 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 * @param block Memory block containing data to send
3275 * @param size Number of data bytes in memory block to send
3276 * @param sem Semaphore to signal upon completion (else NULL)
3277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279 */
3280extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3281 size_t size, struct k_sem *sem);
3282
3283/**
Allan Stephensc98da842016-11-11 15:45:03 -05003284 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 */
3286
Allan Stephensc98da842016-11-11 15:45:03 -05003287/**
3288 * @cond INTERNAL_HIDDEN
3289 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003291struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003293 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003294 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295 char *buffer;
3296 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003297 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298
Anas Nashif2f203c22016-12-18 06:57:45 -05003299 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300};
3301
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003302#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003303 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304 { \
3305 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003306 .num_blocks = slab_num_blocks, \
3307 .block_size = slab_block_size, \
3308 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309 .free_list = NULL, \
3310 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003311 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312 }
3313
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003314#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3315
3316
Peter Mitsis578f9112016-10-07 13:50:31 -04003317/**
Allan Stephensc98da842016-11-11 15:45:03 -05003318 * INTERNAL_HIDDEN @endcond
3319 */
3320
3321/**
3322 * @defgroup mem_slab_apis Memory Slab APIs
3323 * @ingroup kernel_apis
3324 * @{
3325 */
3326
3327/**
Allan Stephensda827222016-11-09 14:23:58 -06003328 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003329 *
Allan Stephensda827222016-11-09 14:23:58 -06003330 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003332 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3333 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003334 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003335 *
Allan Stephensda827222016-11-09 14:23:58 -06003336 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003338 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003339 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @param name Name of the memory slab.
3342 * @param slab_block_size Size of each memory block (in bytes).
3343 * @param slab_num_blocks Number memory blocks.
3344 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003345 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003346#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3347 char __noinit __aligned(slab_align) \
3348 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3349 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003350 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003351 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003352 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003354/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003355 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 *
Allan Stephensda827222016-11-09 14:23:58 -06003359 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3360 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3361 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3362 * To ensure that each memory block is similarly aligned to this boundary,
3363 * @a slab_block_size must also be a multiple of N.
3364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @param slab Address of the memory slab.
3366 * @param buffer Pointer to buffer used for the memory blocks.
3367 * @param block_size Size of each memory block (in bytes).
3368 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003369 *
3370 * @return N/A
3371 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003372extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003373 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003374
3375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * @param slab Address of the memory slab.
3381 * @param mem Pointer to block address area.
3382 * @param timeout Maximum time to wait for operation to complete
3383 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3384 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003385 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003386 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003388 * @retval -ENOMEM Returned without waiting.
3389 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003390 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003391extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003392 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003393
3394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * This routine releases a previously allocated memory block back to its
3398 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * @param slab Address of the memory slab.
3401 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003402 *
3403 * @return N/A
3404 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003405extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003406
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003407/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * This routine gets the number of memory blocks that are currently
3411 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003416 */
Kumar Galacc334c72017-04-21 10:55:34 -05003417static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003419 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420}
3421
Peter Mitsisc001aa82016-10-13 13:53:37 -04003422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * This routine gets the number of memory blocks that are currently
3426 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003431 */
Kumar Galacc334c72017-04-21 10:55:34 -05003432static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003433{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003434 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003435}
3436
Allan Stephensc98da842016-11-11 15:45:03 -05003437/**
3438 * @} end defgroup mem_slab_apis
3439 */
3440
3441/**
3442 * @cond INTERNAL_HIDDEN
3443 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444
Andy Ross73cb9582017-05-09 10:42:39 -07003445struct k_mem_pool_lvl {
3446 union {
3447 u32_t *bits_p;
3448 u32_t bits;
3449 };
3450 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003451};
3452
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003453struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003454 void *buf;
3455 size_t max_sz;
3456 u16_t n_max;
3457 u8_t n_levels;
3458 u8_t max_inline_level;
3459 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003460 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003461};
3462
Andy Ross73cb9582017-05-09 10:42:39 -07003463#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003464
Andy Ross73cb9582017-05-09 10:42:39 -07003465#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3466
3467#define _MPOOL_LVLS(maxsz, minsz) \
3468 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3469 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3470 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3471 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3472 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3473 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3474 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3475 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3476 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3477 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3478 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3479 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3480 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3481 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3482 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3483 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3484
3485/* Rounds the needed bits up to integer multiples of u32_t */
3486#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3487 ((((n_max) << (2*(l))) + 31) / 32)
3488
3489/* One word gets stored free unioned with the pointer, otherwise the
3490 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003491 */
Andy Ross73cb9582017-05-09 10:42:39 -07003492#define _MPOOL_LBIT_WORDS(n_max, l) \
3493 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3494 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003495
Andy Ross73cb9582017-05-09 10:42:39 -07003496/* How many bytes for the bitfields of a single level? */
3497#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3498 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3499 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003500
Andy Ross73cb9582017-05-09 10:42:39 -07003501/* Size of the bitmap array that follows the buffer in allocated memory */
3502#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3503 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3504 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3505 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3506 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3507 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3508 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3509 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3510 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3511 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3512 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3513 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3514 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3515 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3516 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3517 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3518 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003519
3520/**
Allan Stephensc98da842016-11-11 15:45:03 -05003521 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003522 */
3523
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003524/**
Allan Stephensc98da842016-11-11 15:45:03 -05003525 * @addtogroup mem_pool_apis
3526 * @{
3527 */
3528
3529/**
3530 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3533 * long. The memory pool allows blocks to be repeatedly partitioned into
3534 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003535 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003536 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003537 * If the pool is to be accessed outside the module where it is defined, it
3538 * can be declared via
3539 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003540 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003543 * @param minsz Size of the smallest blocks in the pool (in bytes).
3544 * @param maxsz Size of the largest blocks in the pool (in bytes).
3545 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003547 */
Andy Ross73cb9582017-05-09 10:42:39 -07003548#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3549 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3550 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3551 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3552 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3553 .buf = _mpool_buf_##name, \
3554 .max_sz = maxsz, \
3555 .n_max = nmax, \
3556 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3557 .levels = _mpool_lvls_##name, \
3558 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003559
Peter Mitsis937042c2016-10-13 13:18:26 -04003560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * @param pool Address of the memory pool.
3566 * @param block Pointer to block descriptor for the allocated memory.
3567 * @param size Amount of memory to allocate (in bytes).
3568 * @param timeout Maximum time to wait for operation to complete
3569 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3570 * or K_FOREVER to wait as long as necessary.
3571 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003572 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003574 * @retval -ENOMEM Returned without waiting.
3575 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003576 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003577extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003578 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003579
3580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003581 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * This routine releases a previously allocated memory block back to its
3584 * memory pool.
3585 *
3586 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003587 *
3588 * @return N/A
3589 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003591
3592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003593 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003594 *
Andy Ross73cb9582017-05-09 10:42:39 -07003595 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 *
Andy Ross73cb9582017-05-09 10:42:39 -07003597 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003598 *
3599 * @return N/A
3600 */
Andy Ross73cb9582017-05-09 10:42:39 -07003601static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003602
3603/**
Allan Stephensc98da842016-11-11 15:45:03 -05003604 * @} end addtogroup mem_pool_apis
3605 */
3606
3607/**
3608 * @defgroup heap_apis Heap Memory Pool APIs
3609 * @ingroup kernel_apis
3610 * @{
3611 */
3612
3613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003617 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003622 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003623extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003624
3625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003627 *
3628 * This routine provides traditional free() semantics. The memory being
3629 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003630 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003631 * If @a ptr is NULL, no operation is performed.
3632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003633 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003634 *
3635 * @return N/A
3636 */
3637extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003638
Allan Stephensc98da842016-11-11 15:45:03 -05003639/**
3640 * @} end defgroup heap_apis
3641 */
3642
Benjamin Walshacc68c12017-01-29 18:57:45 -05003643/* polling API - PRIVATE */
3644
Benjamin Walshb0179862017-02-02 16:39:57 -05003645#ifdef CONFIG_POLL
3646#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3647#else
3648#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3649#endif
3650
Benjamin Walshacc68c12017-01-29 18:57:45 -05003651/* private - implementation data created as needed, per-type */
3652struct _poller {
3653 struct k_thread *thread;
3654};
3655
3656/* private - types bit positions */
3657enum _poll_types_bits {
3658 /* can be used to ignore an event */
3659 _POLL_TYPE_IGNORE,
3660
3661 /* to be signaled by k_poll_signal() */
3662 _POLL_TYPE_SIGNAL,
3663
3664 /* semaphore availability */
3665 _POLL_TYPE_SEM_AVAILABLE,
3666
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003667 /* queue/fifo/lifo data availability */
3668 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003669
3670 _POLL_NUM_TYPES
3671};
3672
3673#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3674
3675/* private - states bit positions */
3676enum _poll_states_bits {
3677 /* default state when creating event */
3678 _POLL_STATE_NOT_READY,
3679
Benjamin Walshacc68c12017-01-29 18:57:45 -05003680 /* signaled by k_poll_signal() */
3681 _POLL_STATE_SIGNALED,
3682
3683 /* semaphore is available */
3684 _POLL_STATE_SEM_AVAILABLE,
3685
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003686 /* data is available to read on queue/fifo/lifo */
3687 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003688
3689 _POLL_NUM_STATES
3690};
3691
3692#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3693
3694#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003695 (32 - (0 \
3696 + 8 /* tag */ \
3697 + _POLL_NUM_TYPES \
3698 + _POLL_NUM_STATES \
3699 + 1 /* modes */ \
3700 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003701
3702#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3703#error overflow of 32-bit word in struct k_poll_event
3704#endif
3705
3706/* end of polling API - PRIVATE */
3707
3708
3709/**
3710 * @defgroup poll_apis Async polling APIs
3711 * @ingroup kernel_apis
3712 * @{
3713 */
3714
3715/* Public polling API */
3716
3717/* public - values for k_poll_event.type bitfield */
3718#define K_POLL_TYPE_IGNORE 0
3719#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3720#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003721#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3722#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003723
3724/* public - polling modes */
3725enum k_poll_modes {
3726 /* polling thread does not take ownership of objects when available */
3727 K_POLL_MODE_NOTIFY_ONLY = 0,
3728
3729 K_POLL_NUM_MODES
3730};
3731
3732/* public - values for k_poll_event.state bitfield */
3733#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003734#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3735#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003736#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3737#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003738
3739/* public - poll signal object */
3740struct k_poll_signal {
3741 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003742 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003743
3744 /*
3745 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3746 * user resets it to 0.
3747 */
3748 unsigned int signaled;
3749
3750 /* custom result value passed to k_poll_signal() if needed */
3751 int result;
3752};
3753
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003754#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003755 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003756 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003757 .signaled = 0, \
3758 .result = 0, \
3759 }
3760
3761struct k_poll_event {
3762 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003763 sys_dnode_t _node;
3764
3765 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003766 struct _poller *poller;
3767
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003768 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003769 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003770
Benjamin Walshacc68c12017-01-29 18:57:45 -05003771 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003772 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773
3774 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003775 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003776
3777 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003778 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003779
3780 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003781 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782
3783 /* per-type data */
3784 union {
3785 void *obj;
3786 struct k_poll_signal *signal;
3787 struct k_sem *sem;
3788 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003789 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003790 };
3791};
3792
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003793#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003794 { \
3795 .poller = NULL, \
3796 .type = event_type, \
3797 .state = K_POLL_STATE_NOT_READY, \
3798 .mode = event_mode, \
3799 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003800 { .obj = event_obj }, \
3801 }
3802
3803#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3804 event_tag) \
3805 { \
3806 .type = event_type, \
3807 .tag = event_tag, \
3808 .state = K_POLL_STATE_NOT_READY, \
3809 .mode = event_mode, \
3810 .unused = 0, \
3811 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003812 }
3813
3814/**
3815 * @brief Initialize one struct k_poll_event instance
3816 *
3817 * After this routine is called on a poll event, the event it ready to be
3818 * placed in an event array to be passed to k_poll().
3819 *
3820 * @param event The event to initialize.
3821 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3822 * values. Only values that apply to the same object being polled
3823 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3824 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003825 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003826 * @param obj Kernel object or poll signal.
3827 *
3828 * @return N/A
3829 */
3830
Kumar Galacc334c72017-04-21 10:55:34 -05003831extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003832 int mode, void *obj);
3833
3834/**
3835 * @brief Wait for one or many of multiple poll events to occur
3836 *
3837 * This routine allows a thread to wait concurrently for one or many of
3838 * multiple poll events to have occurred. Such events can be a kernel object
3839 * being available, like a semaphore, or a poll signal event.
3840 *
3841 * When an event notifies that a kernel object is available, the kernel object
3842 * is not "given" to the thread calling k_poll(): it merely signals the fact
3843 * that the object was available when the k_poll() call was in effect. Also,
3844 * all threads trying to acquire an object the regular way, i.e. by pending on
3845 * the object, have precedence over the thread polling on the object. This
3846 * means that the polling thread will never get the poll event on an object
3847 * until the object becomes available and its pend queue is empty. For this
3848 * reason, the k_poll() call is more effective when the objects being polled
3849 * only have one thread, the polling thread, trying to acquire them.
3850 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003851 * When k_poll() returns 0, the caller should loop on all the events that were
3852 * passed to k_poll() and check the state field for the values that were
3853 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854 *
3855 * Before being reused for another call to k_poll(), the user has to reset the
3856 * state field to K_POLL_STATE_NOT_READY.
3857 *
3858 * @param events An array of pointers to events to be polled for.
3859 * @param num_events The number of events in the array.
3860 * @param timeout Waiting period for an event to be ready (in milliseconds),
3861 * or one of the special values K_NO_WAIT and K_FOREVER.
3862 *
3863 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003864 * @retval -EAGAIN Waiting period timed out.
3865 */
3866
3867extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003868 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003869
3870/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003871 * @brief Initialize a poll signal object.
3872 *
3873 * Ready a poll signal object to be signaled via k_poll_signal().
3874 *
3875 * @param signal A poll signal.
3876 *
3877 * @return N/A
3878 */
3879
3880extern void k_poll_signal_init(struct k_poll_signal *signal);
3881
3882/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003883 * @brief Signal a poll signal object.
3884 *
3885 * This routine makes ready a poll signal, which is basically a poll event of
3886 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3887 * made ready to run. A @a result value can be specified.
3888 *
3889 * The poll signal contains a 'signaled' field that, when set by
3890 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3891 * be reset by the user before being passed again to k_poll() or k_poll() will
3892 * consider it being signaled, and will return immediately.
3893 *
3894 * @param signal A poll signal.
3895 * @param result The value to store in the result field of the signal.
3896 *
3897 * @retval 0 The signal was delivered successfully.
3898 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3899 */
3900
3901extern int k_poll_signal(struct k_poll_signal *signal, int result);
3902
3903/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003904extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003905
3906/**
3907 * @} end defgroup poll_apis
3908 */
3909
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003910/**
3911 * @brief Make the CPU idle.
3912 *
3913 * This function makes the CPU idle until an event wakes it up.
3914 *
3915 * In a regular system, the idle thread should be the only thread responsible
3916 * for making the CPU idle and triggering any type of power management.
3917 * However, in some more constrained systems, such as a single-threaded system,
3918 * the only thread would be responsible for this if needed.
3919 *
3920 * @return N/A
3921 */
3922extern void k_cpu_idle(void);
3923
3924/**
3925 * @brief Make the CPU idle in an atomic fashion.
3926 *
3927 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3928 * must be done atomically before making the CPU idle.
3929 *
3930 * @param key Interrupt locking key obtained from irq_lock().
3931 *
3932 * @return N/A
3933 */
3934extern void k_cpu_atomic_idle(unsigned int key);
3935
Kumar Galacc334c72017-04-21 10:55:34 -05003936extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003937
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938#include <arch/cpu.h>
3939
Andrew Boiecdb94d62017-04-18 15:22:05 -07003940#ifdef _ARCH_EXCEPT
3941/* This archtecture has direct support for triggering a CPU exception */
3942#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3943#else
3944
3945#include <misc/printk.h>
3946
3947/* NOTE: This is the implementation for arches that do not implement
3948 * _ARCH_EXCEPT() to generate a real CPU exception.
3949 *
3950 * We won't have a real exception frame to determine the PC value when
3951 * the oops occurred, so print file and line number before we jump into
3952 * the fatal error handler.
3953 */
3954#define _k_except_reason(reason) do { \
3955 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3956 _NanoFatalErrorHandler(reason, &_default_esf); \
3957 CODE_UNREACHABLE; \
3958 } while (0)
3959
3960#endif /* _ARCH__EXCEPT */
3961
3962/**
3963 * @brief Fatally terminate a thread
3964 *
3965 * This should be called when a thread has encountered an unrecoverable
3966 * runtime condition and needs to terminate. What this ultimately
3967 * means is determined by the _fatal_error_handler() implementation, which
3968 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3969 *
3970 * If this is called from ISR context, the default system fatal error handler
3971 * will treat it as an unrecoverable system error, just like k_panic().
3972 */
3973#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3974
3975/**
3976 * @brief Fatally terminate the system
3977 *
3978 * This should be called when the Zephyr kernel has encountered an
3979 * unrecoverable runtime condition and needs to terminate. What this ultimately
3980 * means is determined by the _fatal_error_handler() implementation, which
3981 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3982 */
3983#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3984
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003985/*
3986 * private APIs that are utilized by one or more public APIs
3987 */
3988
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003989#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003990extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003991#else
3992#define _init_static_threads() do { } while ((0))
3993#endif
3994
3995extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003996extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003997
Andrew Boiedc5d9352017-06-02 12:56:47 -07003998/* arch/cpu.h may declare an architecture or platform-specific macro
3999 * for properly declaring stacks, compatible with MMU/MPU constraints if
4000 * enabled
4001 */
4002#ifdef _ARCH_THREAD_STACK_DEFINE
4003#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4004#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4005 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4006#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4007#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004008static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4009{
4010 return _ARCH_THREAD_STACK_BUFFER(sym);
4011}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004012#else
4013/**
4014 * @brief Declare a toplevel thread stack memory region
4015 *
4016 * This declares a region of memory suitable for use as a thread's stack.
4017 *
4018 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4019 * 'noinit' section so that it isn't zeroed at boot
4020 *
Andrew Boie507852a2017-07-25 18:47:07 -07004021 * The declared symbol will always be a k_thread_stack_t which can be passed to
4022 * k_thread_create, but should otherwise not be manipulated. If the buffer
4023 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004024 *
4025 * It is legal to precede this definition with the 'static' keyword.
4026 *
4027 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4028 * parameter of k_thread_create(), it may not be the same as the
4029 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4030 *
4031 * @param sym Thread stack symbol name
4032 * @param size Size of the stack memory region
4033 */
4034#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004035 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004036
4037/**
4038 * @brief Declare a toplevel array of thread stack memory regions
4039 *
4040 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4041 * definition for additional details and constraints.
4042 *
4043 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4044 * 'noinit' section so that it isn't zeroed at boot
4045 *
4046 * @param sym Thread stack symbol name
4047 * @param nmemb Number of stacks to declare
4048 * @param size Size of the stack memory region
4049 */
4050
4051#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004052 struct _k_thread_stack_element __noinit \
4053 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004054
4055/**
4056 * @brief Declare an embedded stack memory region
4057 *
4058 * Used for stacks embedded within other data structures. Use is highly
4059 * discouraged but in some cases necessary. For memory protection scenarios,
4060 * it is very important that any RAM preceding this member not be writable
4061 * by threads else a stack overflow will lead to silent corruption. In other
4062 * words, the containing data structure should live in RAM owned by the kernel.
4063 *
4064 * @param sym Thread stack symbol name
4065 * @param size Size of the stack memory region
4066 */
4067#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004068 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004069
4070/**
4071 * @brief Return the size in bytes of a stack memory region
4072 *
4073 * Convenience macro for passing the desired stack size to k_thread_create()
4074 * since the underlying implementation may actually create something larger
4075 * (for instance a guard area).
4076 *
4077 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004078 * passed to K_THREAD_STACK_DEFINE.
4079 *
4080 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4081 * it is not guaranteed to return the original value since each array
4082 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004083 *
4084 * @param sym Stack memory symbol
4085 * @return Size of the stack
4086 */
4087#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4088
4089/**
4090 * @brief Get a pointer to the physical stack buffer
4091 *
4092 * Convenience macro to get at the real underlying stack buffer used by
4093 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4094 * This is really only intended for diagnostic tools which want to examine
4095 * stack memory contents.
4096 *
4097 * @param sym Declared stack symbol name
4098 * @return The buffer itself, a char *
4099 */
Andrew Boie507852a2017-07-25 18:47:07 -07004100static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4101{
4102 return (char *)sym;
4103}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004104
4105#endif /* _ARCH_DECLARE_STACK */
4106
Chunlin Hane9c97022017-07-07 20:29:30 +08004107/**
4108 * @defgroup mem_domain_apis Memory domain APIs
4109 * @ingroup kernel_apis
4110 * @{
4111 */
4112
4113/** @def MEM_PARTITION_ENTRY
4114 * @brief Used to declare a memory partition entry
4115 */
4116#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4117 {\
4118 .start = _start, \
4119 .size = _size, \
4120 .attr = _attr, \
4121 }
4122
4123/** @def K_MEM_PARTITION_DEFINE
4124 * @brief Used to declare a memory partition
4125 */
4126#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4127#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4128 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4129 struct k_mem_partition name = \
4130 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4131#else
4132#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4133 struct k_mem_partition name = \
4134 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4135#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4136
4137
4138/* memory partition */
4139struct k_mem_partition {
4140 /* start address of memory partition */
4141 u32_t start;
4142 /* size of memory partition */
4143 u32_t size;
4144 /* attribute of memory partition */
4145 u32_t attr;
4146};
4147
4148#if defined(CONFIG_USERSPACE)
4149/* memory domian */
4150struct k_mem_domain {
4151 /* number of partitions in the domain */
4152 u32_t num_partitions;
4153 /* partitions in the domain */
4154 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4155 /* domain q */
4156 sys_dlist_t mem_domain_q;
4157};
4158#endif /* CONFIG_USERSPACE */
4159
4160/**
4161 * @brief Initialize a memory domain.
4162 *
4163 * Initialize a memory domain with given name and memory partitions.
4164 *
4165 * @param domain The memory domain to be initialized.
4166 * @param num_parts The number of array items of "parts" parameter.
4167 * @param parts An array of pointers to the memory partitions. Can be NULL
4168 * if num_parts is zero.
4169 */
4170
4171extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4172 struct k_mem_partition *parts[]);
4173/**
4174 * @brief Destroy a memory domain.
4175 *
4176 * Destroy a memory domain.
4177 *
4178 * @param domain The memory domain to be destroyed.
4179 */
4180
4181extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4182
4183/**
4184 * @brief Add a memory partition into a memory domain.
4185 *
4186 * Add a memory partition into a memory domain.
4187 *
4188 * @param domain The memory domain to be added a memory partition.
4189 * @param part The memory partition to be added
4190 */
4191
4192extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4193 struct k_mem_partition *part);
4194
4195/**
4196 * @brief Remove a memory partition from a memory domain.
4197 *
4198 * Remove a memory partition from a memory domain.
4199 *
4200 * @param domain The memory domain to be removed a memory partition.
4201 * @param part The memory partition to be removed
4202 */
4203
4204extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4205 struct k_mem_partition *part);
4206
4207/**
4208 * @brief Add a thread into a memory domain.
4209 *
4210 * Add a thread into a memory domain.
4211 *
4212 * @param domain The memory domain that the thread is going to be added into.
4213 * @param thread ID of thread going to be added into the memory domain.
4214 */
4215
4216extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4217 k_tid_t thread);
4218
4219/**
4220 * @brief Remove a thread from its memory domain.
4221 *
4222 * Remove a thread from its memory domain.
4223 *
4224 * @param thread ID of thread going to be removed from its memory domain.
4225 */
4226
4227extern void k_mem_domain_remove_thread(k_tid_t thread);
4228
4229/**
4230 * @} end defgroup mem_domain_apis
4231 */
4232
Andrew Boie756f9072017-10-10 16:01:49 -07004233/**
4234 * @brief Emit a character buffer to the console device
4235 *
4236 * @param c String of characters to print
4237 * @param n The length of the string
4238 */
4239__syscall void k_str_out(char *c, size_t n);
4240
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004241#ifdef __cplusplus
4242}
4243#endif
4244
Andrew Boiee004dec2016-11-07 09:01:19 -08004245#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4246/*
4247 * Define new and delete operators.
4248 * At this moment, the operators do nothing since objects are supposed
4249 * to be statically allocated.
4250 */
4251inline void operator delete(void *ptr)
4252{
4253 (void)ptr;
4254}
4255
4256inline void operator delete[](void *ptr)
4257{
4258 (void)ptr;
4259}
4260
4261inline void *operator new(size_t size)
4262{
4263 (void)size;
4264 return NULL;
4265}
4266
4267inline void *operator new[](size_t size)
4268{
4269 (void)size;
4270 return NULL;
4271}
4272
4273/* Placement versions of operator new and delete */
4274inline void operator delete(void *ptr1, void *ptr2)
4275{
4276 (void)ptr1;
4277 (void)ptr2;
4278}
4279
4280inline void operator delete[](void *ptr1, void *ptr2)
4281{
4282 (void)ptr1;
4283 (void)ptr2;
4284}
4285
4286inline void *operator new(size_t size, void *ptr)
4287{
4288 (void)size;
4289 return ptr;
4290}
4291
4292inline void *operator new[](size_t size, void *ptr)
4293{
4294 (void)size;
4295 return ptr;
4296}
4297
4298#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4299
Andrew Boiefa94ee72017-09-28 16:54:35 -07004300#include <syscalls/kernel.h>
4301
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004302#endif /* !_ASMLANGUAGE */
4303
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004304#endif /* _kernel__h_ */