blob: 0e4b7b5e6c3e606551496e93cea3f4e149848b76 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Anas Nashifbbb157d2017-01-15 08:46:31 -050037/**
38 * @brief Kernel APIs
39 * @defgroup kernel_apis Kernel APIs
40 * @{
41 * @}
42 */
43
Anas Nashif61f4b242016-11-18 10:53:59 -050044#ifdef CONFIG_KERNEL_DEBUG
45#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040046#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
47#else
48#define K_DEBUG(fmt, ...)
49#endif
50
Benjamin Walsh2f280412017-01-14 19:23:46 -050051#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
53#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
54#elif defined(CONFIG_COOP_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
56#define _NUM_PREEMPT_PRIO (0)
57#elif defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (0)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#else
61#error "invalid configuration"
62#endif
63
64#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_PRIO_PREEMPT(x) (x)
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_ANY NULL
68#define K_END NULL
69
Benjamin Walshedb35702017-01-14 18:47:22 -050070#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050072#elif defined(CONFIG_COOP_ENABLED)
73#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
74#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050076#else
77#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050080#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
82#else
83#define K_LOWEST_THREAD_PRIO -1
84#endif
85
Benjamin Walshfab8d922016-11-08 15:36:36 -050086#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
87
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
89#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
90
91typedef sys_dlist_t _wait_q_t;
92
Anas Nashif2f203c22016-12-18 06:57:45 -050093#ifdef CONFIG_OBJECT_TRACING
94#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
95#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#else
Anas Nashif2f203c22016-12-18 06:57:45 -050097#define _OBJECT_TRACING_INIT
98#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#endif
100
Benjamin Walshacc68c12017-01-29 18:57:45 -0500101#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300102#define _POLL_EVENT_OBJ_INIT(obj) \
103 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
104#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#define _POLL_EVENT
108#endif
109
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800126struct k_mem_domain;
127struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128
Andrew Boie5bd891d2017-09-27 12:59:28 -0700129/* This enumeration needs to be kept in sync with the lists of kernel objects
130 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
131 * function in kernel/userspace.c
132 */
Andrew Boie945af952017-08-22 13:15:23 -0700133enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700134 K_OBJ_ANY,
135
Andrew Boie5bd891d2017-09-27 12:59:28 -0700136 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700137 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700138 K_OBJ_MSGQ,
139 K_OBJ_MUTEX,
140 K_OBJ_PIPE,
141 K_OBJ_SEM,
142 K_OBJ_STACK,
143 K_OBJ_THREAD,
144 K_OBJ_TIMER,
Andrew Boie945af952017-08-22 13:15:23 -0700145
Andrew Boie5bd891d2017-09-27 12:59:28 -0700146 /* Driver subsystems */
147 K_OBJ_DRIVER_ADC,
148 K_OBJ_DRIVER_AIO_CMP,
149 K_OBJ_DRIVER_CLOCK_CONTROL,
150 K_OBJ_DRIVER_COUNTER,
151 K_OBJ_DRIVER_CRYPTO,
152 K_OBJ_DRIVER_DMA,
153 K_OBJ_DRIVER_ETH,
154 K_OBJ_DRIVER_FLASH,
155 K_OBJ_DRIVER_GPIO,
156 K_OBJ_DRIVER_I2C,
157 K_OBJ_DRIVER_I2S,
158 K_OBJ_DRIVER_IPM,
159 K_OBJ_DRIVER_PINMUX,
160 K_OBJ_DRIVER_PWM,
161 K_OBJ_DRIVER_RANDOM,
162 K_OBJ_DRIVER_RTC,
163 K_OBJ_DRIVER_SENSOR,
164 K_OBJ_DRIVER_SHARED_IRQ,
165 K_OBJ_DRIVER_SPI,
166 K_OBJ_DRIVER_UART,
167 K_OBJ_DRIVER_WDT,
168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700177 u8_t perms[CONFIG_MAX_THREAD_BYTES];
178 u8_t type;
179 u8_t flags;
Andrew Boie945af952017-08-22 13:15:23 -0700180} __packed;
181
182#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700183#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700184
185/**
186 * Lookup a kernel object and init its metadata if it exists
187 *
188 * Calling this on an object will make it usable from userspace.
189 * Intended to be called as the last statement in kernel object init
190 * functions.
191 *
192 * @param object Address of the kernel object
193 */
194void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700195#else
Andrew Boie743e4682017-10-04 12:25:50 -0700196static inline void _k_object_init(void *obj)
197{
198 ARG_UNUSED(obj);
199}
200
201static inline void _impl_k_object_access_grant(void *object,
202 struct k_thread *thread)
203{
204 ARG_UNUSED(object);
205 ARG_UNUSED(thread);
206}
207
Andrew Boiea89bf012017-10-09 14:47:55 -0700208static inline void _impl_k_object_access_revoke(void *object,
209 struct k_thread *thread)
210{
211 ARG_UNUSED(object);
212 ARG_UNUSED(thread);
213}
214
Andrew Boie743e4682017-10-04 12:25:50 -0700215static inline void _impl_k_object_access_all_grant(void *object)
216{
217 ARG_UNUSED(object);
218}
219#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700220
221/**
222 * grant a thread access to a kernel object
223 *
224 * The thread will be granted access to the object if the caller is from
225 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700226 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700227 *
228 * @param object Address of kernel object
229 * @param thread Thread to grant access to the object
230 */
Andrew Boie743e4682017-10-04 12:25:50 -0700231__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700232
Andrew Boiea89bf012017-10-09 14:47:55 -0700233/**
234 * grant a thread access to a kernel object
235 *
236 * The thread will lose access to the object if the caller is from
237 * supervisor mode, or the caller is from user mode AND has permissions
238 * on both the object and the thread whose access is being revoked.
239 *
240 * @param object Address of kernel object
241 * @param thread Thread to remove access to the object
242 */
243__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700244
245/**
246 * grant all present and future threads access to an object
247 *
248 * If the caller is from supervisor mode, or the caller is from user mode and
249 * have sufficient permissions on the object, then that object will have
250 * permissions granted to it for *all* current and future threads running in
251 * the system, effectively becoming a public kernel object.
252 *
253 * Use of this API should be avoided on systems that are running untrusted code
254 * as it is possible for such code to derive the addresses of kernel objects
255 * and perform unwanted operations on them.
256 *
Andrew Boie04caa672017-10-13 13:57:07 -0700257 * It is not possible to revoke permissions on public objects; once public,
258 * any thread may use it.
259 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700260 * @param object Address of kernel object
261 */
Andrew Boie743e4682017-10-04 12:25:50 -0700262__syscall void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700263
Andrew Boie73abd322017-04-04 13:19:13 -0700264/* timeouts */
265
266struct _timeout;
267typedef void (*_timeout_func_t)(struct _timeout *t);
268
269struct _timeout {
270 sys_dnode_t node;
271 struct k_thread *thread;
272 sys_dlist_t *wait_q;
273 s32_t delta_ticks_from_prev;
274 _timeout_func_t func;
275};
276
277extern s32_t _timeout_remaining_get(struct _timeout *timeout);
278
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700279/**
280 * @typedef k_thread_entry_t
281 * @brief Thread entry point function type.
282 *
283 * A thread's entry point function is invoked when the thread starts executing.
284 * Up to 3 argument values can be passed to the function.
285 *
286 * The thread terminates execution permanently if the entry point function
287 * returns. The thread is responsible for releasing any shared resources
288 * it may own (such as mutexes and dynamically allocated memory), prior to
289 * returning.
290 *
291 * @param p1 First argument.
292 * @param p2 Second argument.
293 * @param p3 Third argument.
294 *
295 * @return N/A
296 */
297typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700298
299#ifdef CONFIG_THREAD_MONITOR
300struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700301 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700302 void *parameter1;
303 void *parameter2;
304 void *parameter3;
305};
306#endif
307
308/* can be used for creating 'dummy' threads, e.g. for pending on objects */
309struct _thread_base {
310
311 /* this thread's entry in a ready/wait queue */
312 sys_dnode_t k_q_node;
313
314 /* user facing 'thread options'; values defined in include/kernel.h */
315 u8_t user_options;
316
317 /* thread state */
318 u8_t thread_state;
319
320 /*
321 * scheduler lock count and thread priority
322 *
323 * These two fields control the preemptibility of a thread.
324 *
325 * When the scheduler is locked, sched_locked is decremented, which
326 * means that the scheduler is locked for values from 0xff to 0x01. A
327 * thread is coop if its prio is negative, thus 0x80 to 0xff when
328 * looked at the value as unsigned.
329 *
330 * By putting them end-to-end, this means that a thread is
331 * non-preemptible if the bundled value is greater than or equal to
332 * 0x0080.
333 */
334 union {
335 struct {
336#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
337 u8_t sched_locked;
338 s8_t prio;
339#else /* LITTLE and PDP */
340 s8_t prio;
341 u8_t sched_locked;
342#endif
343 };
344 u16_t preempt;
345 };
346
347 /* data returned by APIs */
348 void *swap_data;
349
350#ifdef CONFIG_SYS_CLOCK_EXISTS
351 /* this thread's entry in a timeout queue */
352 struct _timeout timeout;
353#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700354
355#ifdef CONFIG_USERSPACE
356 /* Bit position in kernel object permissions bitfield for this thread */
357 unsigned int perm_index;
358#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700359};
360
361typedef struct _thread_base _thread_base_t;
362
363#if defined(CONFIG_THREAD_STACK_INFO)
364/* Contains the stack information of a thread */
365struct _thread_stack_info {
366 /* Stack Start */
367 u32_t start;
368 /* Stack Size */
369 u32_t size;
370};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700371
372typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700373#endif /* CONFIG_THREAD_STACK_INFO */
374
Chunlin Hane9c97022017-07-07 20:29:30 +0800375#if defined(CONFIG_USERSPACE)
376struct _mem_domain_info {
377 /* memory domain queue node */
378 sys_dnode_t mem_domain_q_node;
379 /* memory domain of the thread */
380 struct k_mem_domain *mem_domain;
381};
382
383#endif /* CONFIG_USERSPACE */
384
Andrew Boie73abd322017-04-04 13:19:13 -0700385struct k_thread {
386
387 struct _thread_base base;
388
389 /* defined by the architecture, but all archs need these */
390 struct _caller_saved caller_saved;
391 struct _callee_saved callee_saved;
392
393 /* static thread init data */
394 void *init_data;
395
396 /* abort function */
397 void (*fn_abort)(void);
398
399#if defined(CONFIG_THREAD_MONITOR)
400 /* thread entry and parameters description */
401 struct __thread_entry *entry;
402
403 /* next item in list of all threads */
404 struct k_thread *next_thread;
405#endif
406
407#ifdef CONFIG_THREAD_CUSTOM_DATA
408 /* crude thread-local storage */
409 void *custom_data;
410#endif
411
412#ifdef CONFIG_ERRNO
413 /* per-thread errno variable */
414 int errno_var;
415#endif
416
417#if defined(CONFIG_THREAD_STACK_INFO)
418 /* Stack Info */
419 struct _thread_stack_info stack_info;
420#endif /* CONFIG_THREAD_STACK_INFO */
421
Chunlin Hane9c97022017-07-07 20:29:30 +0800422#if defined(CONFIG_USERSPACE)
423 /* memory domain info of the thread */
424 struct _mem_domain_info mem_domain_info;
425#endif /* CONFIG_USERSPACE */
426
Andrew Boie73abd322017-04-04 13:19:13 -0700427 /* arch-specifics: must always be at the end */
428 struct _thread_arch arch;
429};
430
431typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400432typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700433#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400434
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400435enum execution_context_types {
436 K_ISR = 0,
437 K_COOP_THREAD,
438 K_PREEMPT_THREAD,
439};
440
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100442 * @defgroup profiling_apis Profiling APIs
443 * @ingroup kernel_apis
444 * @{
445 */
446
447/**
448 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
449 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700450 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100451 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
452 *
453 * CONFIG_MAIN_STACK_SIZE
454 * CONFIG_IDLE_STACK_SIZE
455 * CONFIG_ISR_STACK_SIZE
456 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
457 *
458 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
459 * produce output.
460 *
461 * @return N/A
462 */
463extern void k_call_stacks_analyze(void);
464
465/**
466 * @} end defgroup profiling_apis
467 */
468
469/**
Allan Stephensc98da842016-11-11 15:45:03 -0500470 * @defgroup thread_apis Thread APIs
471 * @ingroup kernel_apis
472 * @{
473 */
474
Benjamin Walshed240f22017-01-22 13:05:08 -0500475#endif /* !_ASMLANGUAGE */
476
477
478/*
479 * Thread user options. May be needed by assembly code. Common part uses low
480 * bits, arch-specific use high bits.
481 */
482
483/* system thread that must not abort */
484#define K_ESSENTIAL (1 << 0)
485
486#if defined(CONFIG_FP_SHARING)
487/* thread uses floating point registers */
488#define K_FP_REGS (1 << 1)
489#endif
490
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700491/* This thread has dropped from supervisor mode to user mode and consequently
492 * has additional restrictions
493 */
494#define K_USER (1 << 2)
495
Andrew Boie47f8fd12017-10-05 11:11:02 -0700496/* Indicates that the thread being created should inherit all kernel object
497 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
498 * is not enabled.
499 */
500#define K_INHERIT_PERMS (1 << 3)
501
Benjamin Walshed240f22017-01-22 13:05:08 -0500502#ifdef CONFIG_X86
503/* x86 Bitmask definitions for threads user options */
504
505#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
506/* thread uses SSEx (and also FP) registers */
507#define K_SSE_REGS (1 << 7)
508#endif
509#endif
510
511/* end - thread options */
512
513#if !defined(_ASMLANGUAGE)
514
Andrew Boie507852a2017-07-25 18:47:07 -0700515/* Using typedef deliberately here, this is quite intended to be an opaque
516 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
517 *
518 * The purpose of this data type is to clearly distinguish between the
519 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
520 * buffer which composes the stack data actually used by the underlying
521 * thread; they cannot be used interchangably as some arches precede the
522 * stack buffer region with guard areas that trigger a MPU or MMU fault
523 * if written to.
524 *
525 * APIs that want to work with the buffer inside should continue to use
526 * char *.
527 *
528 * Stacks should always be created with K_THREAD_STACK_DEFINE().
529 */
530struct __packed _k_thread_stack_element {
531 char data;
532};
533typedef struct _k_thread_stack_element *k_thread_stack_t;
534
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700537 * @brief Create a thread.
538 *
539 * This routine initializes a thread, then schedules it for execution.
540 *
541 * The new thread may be scheduled for immediate execution or a delayed start.
542 * If the newly spawned thread does not have a delayed start the kernel
543 * scheduler may preempt the current thread to allow the new thread to
544 * execute.
545 *
546 * Thread options are architecture-specific, and can include K_ESSENTIAL,
547 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
548 * them using "|" (the logical OR operator).
549 *
550 * Historically, users often would use the beginning of the stack memory region
551 * to store the struct k_thread data, although corruption will occur if the
552 * stack overflows this region and stack protection features may not detect this
553 * situation.
554 *
555 * @param new_thread Pointer to uninitialized struct k_thread
556 * @param stack Pointer to the stack space.
557 * @param stack_size Stack size in bytes.
558 * @param entry Thread entry function.
559 * @param p1 1st entry point parameter.
560 * @param p2 2nd entry point parameter.
561 * @param p3 3rd entry point parameter.
562 * @param prio Thread priority.
563 * @param options Thread options.
564 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
565 *
566 * @return ID of new thread.
567 */
Andrew Boie507852a2017-07-25 18:47:07 -0700568extern k_tid_t k_thread_create(struct k_thread *new_thread,
569 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700570 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700571 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700572 void *p1, void *p2, void *p3,
573 int prio, u32_t options, s32_t delay);
574
Andrew Boie3f091b52017-08-30 14:34:14 -0700575/**
576 * @brief Drop a thread's privileges permanently to user mode
577 *
578 * @param entry Function to start executing from
579 * @param p1 1st entry point parameter
580 * @param p2 2nd entry point parameter
581 * @param p3 3rd entry point parameter
582 */
583extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
584 void *p1, void *p2,
585 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700586
Andrew Boied26cf2d2017-03-30 13:07:02 -0700587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500588 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589 *
Allan Stephensc98da842016-11-11 15:45:03 -0500590 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500591 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
595 * @return N/A
596 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700597__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598
599/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
602 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500603 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 * @return N/A
606 */
Kumar Galacc334c72017-04-21 10:55:34 -0500607extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400608
609/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500610 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500612 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400613 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500614 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615 *
616 * @return N/A
617 */
Andrew Boie468190a2017-09-29 14:00:48 -0700618__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619
620/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500621 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500623 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500625 * If @a thread is not currently sleeping, the routine has no effect.
626 *
627 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628 *
629 * @return N/A
630 */
Andrew Boie468190a2017-09-29 14:00:48 -0700631__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632
633/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500634 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500636 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700638__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639
640/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500641 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500643 * This routine prevents @a thread from executing if it has not yet started
644 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500646 * @param thread ID of thread to cancel.
647 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700648 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500649 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 */
Andrew Boie468190a2017-09-29 14:00:48 -0700651__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400652
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400653/**
Allan Stephensc98da842016-11-11 15:45:03 -0500654 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * This routine permanently stops execution of @a thread. The thread is taken
657 * off all kernel queues it is part of (i.e. the ready queue, the timeout
658 * queue, or a kernel object wait queue). However, any kernel resources the
659 * thread might currently own (such as mutexes or memory blocks) are not
660 * released. It is the responsibility of the caller of this routine to ensure
661 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
665 * @return N/A
666 */
Andrew Boie468190a2017-09-29 14:00:48 -0700667__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668
Andrew Boie7d627c52017-08-30 11:01:56 -0700669
670/**
671 * @brief Start an inactive thread
672 *
673 * If a thread was created with K_FOREVER in the delay parameter, it will
674 * not be added to the scheduling queue until this function is called
675 * on it.
676 *
677 * @param thread thread to start
678 */
Andrew Boie468190a2017-09-29 14:00:48 -0700679__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700680
Allan Stephensc98da842016-11-11 15:45:03 -0500681/**
682 * @cond INTERNAL_HIDDEN
683 */
684
Benjamin Walshd211a522016-12-06 11:44:01 -0500685/* timeout has timed out and is not on _timeout_q anymore */
686#define _EXPIRED (-2)
687
688/* timeout is not in use */
689#define _INACTIVE (-1)
690
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400691struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700692 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700693 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700695 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500696 void *init_p1;
697 void *init_p2;
698 void *init_p3;
699 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500700 u32_t init_options;
701 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500702 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500703 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400704};
705
Andrew Boied26cf2d2017-03-30 13:07:02 -0700706#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400707 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500708 prio, options, delay, abort, groups) \
709 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700710 .init_thread = (thread), \
711 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500712 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700713 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400714 .init_p1 = (void *)p1, \
715 .init_p2 = (void *)p2, \
716 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500717 .init_prio = (prio), \
718 .init_options = (options), \
719 .init_delay = (delay), \
720 .init_abort = (abort), \
721 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722 }
723
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400724/**
Allan Stephensc98da842016-11-11 15:45:03 -0500725 * INTERNAL_HIDDEN @endcond
726 */
727
728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500729 * @brief Statically define and initialize a thread.
730 *
731 * The thread may be scheduled for immediate execution or a delayed start.
732 *
733 * Thread options are architecture-specific, and can include K_ESSENTIAL,
734 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
735 * them using "|" (the logical OR operator).
736 *
737 * The ID of the thread can be accessed using:
738 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500739 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500740 *
741 * @param name Name of the thread.
742 * @param stack_size Stack size in bytes.
743 * @param entry Thread entry function.
744 * @param p1 1st entry point parameter.
745 * @param p2 2nd entry point parameter.
746 * @param p3 3rd entry point parameter.
747 * @param prio Thread priority.
748 * @param options Thread options.
749 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400750 *
751 * @internal It has been observed that the x86 compiler by default aligns
752 * these _static_thread_data structures to 32-byte boundaries, thereby
753 * wasting space. To work around this, force a 4-byte alignment.
754 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500755#define K_THREAD_DEFINE(name, stack_size, \
756 entry, p1, p2, p3, \
757 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700758 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700759 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500760 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500761 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700762 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
763 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500764 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700765 NULL, 0); \
766 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400767
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500769 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @param thread ID of thread whose priority is needed.
774 *
775 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700777__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778
779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
784 * Rescheduling can occur immediately depending on the priority @a thread is
785 * set to:
786 *
787 * - If its priority is raised above the priority of the caller of this
788 * function, and the caller is preemptible, @a thread will be scheduled in.
789 *
790 * - If the caller operates on itself, it lowers its priority below that of
791 * other threads in the system, and the caller is preemptible, the thread of
792 * highest priority will be scheduled in.
793 *
794 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
795 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
796 * highest priority.
797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 * @param prio New priority.
800 *
801 * @warning Changing the priority of a thread currently involved in mutex
802 * priority inheritance may result in undefined behavior.
803 *
804 * @return N/A
805 */
Andrew Boie468190a2017-09-29 14:00:48 -0700806__syscall void k_thread_priority_set(k_tid_t thread, int prio);
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 Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * This routine prevents the kernel scheduler from making @a thread the
812 * current thread. All other internal operations on @a thread are still
813 * performed; for example, any timeout it is waiting on keeps ticking,
814 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * If @a thread is already suspended, the routine has no effect.
817 *
818 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
820 * @return N/A
821 */
Andrew Boie468190a2017-09-29 14:00:48 -0700822__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823
824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * This routine allows the kernel scheduler to make @a thread the current
828 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * If @a thread is not currently suspended, the routine has no effect.
831 *
832 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
834 * @return N/A
835 */
Andrew Boie468190a2017-09-29 14:00:48 -0700836__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400837
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * This routine specifies how the scheduler will perform time slicing of
842 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * To enable time slicing, @a slice must be non-zero. The scheduler
845 * ensures that no thread runs for more than the specified time limit
846 * before other threads of that priority are given a chance to execute.
847 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700848 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 * execute. Once the scheduler selects a thread for execution, there is no
852 * minimum guaranteed time the thread will execute before threads of greater or
853 * equal priority are scheduled.
854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 * for execution, this routine has no effect; the thread is immediately
857 * rescheduled after the slice period expires.
858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * To disable timeslicing, set both @a slice and @a prio to zero.
860 *
861 * @param slice Maximum time slice length (in milliseconds).
862 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
864 * @return N/A
865 */
Kumar Galacc334c72017-04-21 10:55:34 -0500866extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400867
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868/**
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * @} end defgroup thread_apis
870 */
871
872/**
873 * @addtogroup isr_apis
874 * @{
875 */
876
877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephensc98da842016-11-11 15:45:03 -0500880 * This routine allows the caller to customize its actions, depending on
881 * whether it is a thread or an ISR.
882 *
883 * @note Can be called by ISRs.
884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * @return 0 if invoked by a thread.
886 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500888extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889
Benjamin Walsh445830d2016-11-10 15:54:27 -0500890/**
891 * @brief Determine if code is running in a preemptible thread.
892 *
Allan Stephensc98da842016-11-11 15:45:03 -0500893 * This routine allows the caller to customize its actions, depending on
894 * whether it can be preempted by another thread. The routine returns a 'true'
895 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500896 *
Allan Stephensc98da842016-11-11 15:45:03 -0500897 * - The code is running in a thread, not at ISR.
898 * - The thread's priority is in the preemptible range.
899 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500900 *
Allan Stephensc98da842016-11-11 15:45:03 -0500901 * @note Can be called by ISRs.
902 *
903 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500904 * @return Non-zero if invoked by a preemptible thread.
905 */
Andrew Boie468190a2017-09-29 14:00:48 -0700906__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500907
Allan Stephensc98da842016-11-11 15:45:03 -0500908/**
909 * @} end addtogroup isr_apis
910 */
911
912/**
913 * @addtogroup thread_apis
914 * @{
915 */
916
917/**
918 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500919 *
Allan Stephensc98da842016-11-11 15:45:03 -0500920 * This routine prevents the current thread from being preempted by another
921 * thread by instructing the scheduler to treat it as a cooperative thread.
922 * If the thread subsequently performs an operation that makes it unready,
923 * it will be context switched out in the normal manner. When the thread
924 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500925 *
Allan Stephensc98da842016-11-11 15:45:03 -0500926 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500927 *
Allan Stephensc98da842016-11-11 15:45:03 -0500928 * @note k_sched_lock() and k_sched_unlock() should normally be used
929 * when the operation being performed can be safely interrupted by ISRs.
930 * However, if the amount of processing involved is very small, better
931 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500932 *
933 * @return N/A
934 */
935extern void k_sched_lock(void);
936
Allan Stephensc98da842016-11-11 15:45:03 -0500937/**
938 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500939 *
Allan Stephensc98da842016-11-11 15:45:03 -0500940 * This routine reverses the effect of a previous call to k_sched_lock().
941 * A thread must call the routine once for each time it called k_sched_lock()
942 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500943 *
944 * @return N/A
945 */
946extern void k_sched_unlock(void);
947
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500951 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * Custom data is not used by the kernel itself, and is freely available
954 * for a thread to use as it sees fit. It can be used as a framework
955 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500957 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400958 *
959 * @return N/A
960 */
Andrew Boie468190a2017-09-29 14:00:48 -0700961__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400962
963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500966 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 */
Andrew Boie468190a2017-09-29 14:00:48 -0700970__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971
972/**
Allan Stephensc98da842016-11-11 15:45:03 -0500973 * @} end addtogroup thread_apis
974 */
975
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400976#include <sys_clock.h>
977
Allan Stephensc2f15a42016-11-17 12:24:22 -0500978/**
979 * @addtogroup clock_apis
980 * @{
981 */
982
983/**
984 * @brief Generate null timeout delay.
985 *
986 * This macro generates a timeout delay that that instructs a kernel API
987 * not to wait if the requested operation cannot be performed immediately.
988 *
989 * @return Timeout delay value.
990 */
991#define K_NO_WAIT 0
992
993/**
994 * @brief Generate timeout delay from milliseconds.
995 *
996 * This macro generates a timeout delay that that instructs a kernel API
997 * to wait up to @a ms milliseconds to perform the requested operation.
998 *
999 * @param ms Duration in milliseconds.
1000 *
1001 * @return Timeout delay value.
1002 */
Johan Hedberg14471692016-11-13 10:52:15 +02001003#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001004
1005/**
1006 * @brief Generate timeout delay from seconds.
1007 *
1008 * This macro generates a timeout delay that that instructs a kernel API
1009 * to wait up to @a s seconds to perform the requested operation.
1010 *
1011 * @param s Duration in seconds.
1012 *
1013 * @return Timeout delay value.
1014 */
Johan Hedberg14471692016-11-13 10:52:15 +02001015#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001016
1017/**
1018 * @brief Generate timeout delay from minutes.
1019 *
1020 * This macro generates a timeout delay that that instructs a kernel API
1021 * to wait up to @a m minutes to perform the requested operation.
1022 *
1023 * @param m Duration in minutes.
1024 *
1025 * @return Timeout delay value.
1026 */
Johan Hedberg14471692016-11-13 10:52:15 +02001027#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001028
1029/**
1030 * @brief Generate timeout delay from hours.
1031 *
1032 * This macro generates a timeout delay that that instructs a kernel API
1033 * to wait up to @a h hours to perform the requested operation.
1034 *
1035 * @param h Duration in hours.
1036 *
1037 * @return Timeout delay value.
1038 */
Johan Hedberg14471692016-11-13 10:52:15 +02001039#define K_HOURS(h) K_MINUTES((h) * 60)
1040
Allan Stephensc98da842016-11-11 15:45:03 -05001041/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001042 * @brief Generate infinite timeout delay.
1043 *
1044 * This macro generates a timeout delay that that instructs a kernel API
1045 * to wait as long as necessary to perform the requested operation.
1046 *
1047 * @return Timeout delay value.
1048 */
1049#define K_FOREVER (-1)
1050
1051/**
1052 * @} end addtogroup clock_apis
1053 */
1054
1055/**
Allan Stephensc98da842016-11-11 15:45:03 -05001056 * @cond INTERNAL_HIDDEN
1057 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001058
Benjamin Walsh62092182016-12-20 14:39:08 -05001059/* kernel clocks */
1060
1061#if (sys_clock_ticks_per_sec == 1000) || \
1062 (sys_clock_ticks_per_sec == 500) || \
1063 (sys_clock_ticks_per_sec == 250) || \
1064 (sys_clock_ticks_per_sec == 125) || \
1065 (sys_clock_ticks_per_sec == 100) || \
1066 (sys_clock_ticks_per_sec == 50) || \
1067 (sys_clock_ticks_per_sec == 25) || \
1068 (sys_clock_ticks_per_sec == 20) || \
1069 (sys_clock_ticks_per_sec == 10) || \
1070 (sys_clock_ticks_per_sec == 1)
1071
1072 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1073#else
1074 /* yields horrible 64-bit math on many architectures: try to avoid */
1075 #define _NON_OPTIMIZED_TICKS_PER_SEC
1076#endif
1077
1078#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001079extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001080#else
Kumar Galacc334c72017-04-21 10:55:34 -05001081static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001082{
Kumar Galacc334c72017-04-21 10:55:34 -05001083 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001084}
1085#endif
1086
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001087/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001088#ifdef CONFIG_TICKLESS_KERNEL
1089#define _TICK_ALIGN 0
1090#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001091#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001092#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001093
Kumar Galacc334c72017-04-21 10:55:34 -05001094static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001095{
Benjamin Walsh62092182016-12-20 14:39:08 -05001096#ifdef CONFIG_SYS_CLOCK_EXISTS
1097
1098#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001099 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001100#else
Kumar Galacc334c72017-04-21 10:55:34 -05001101 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001102#endif
1103
1104#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001105 __ASSERT(ticks == 0, "");
1106 return 0;
1107#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001108}
1109
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001110struct k_timer {
1111 /*
1112 * _timeout structure must be first here if we want to use
1113 * dynamic timer allocation. timeout.node is used in the double-linked
1114 * list of free timers
1115 */
1116 struct _timeout timeout;
1117
Allan Stephens45bfa372016-10-12 12:39:42 -05001118 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119 _wait_q_t wait_q;
1120
1121 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001122 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001123
1124 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001125 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001126
1127 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001128 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001129
Allan Stephens45bfa372016-10-12 12:39:42 -05001130 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001131 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001132
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001133 /* user-specific data, also used to support legacy features */
1134 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135
Anas Nashif2f203c22016-12-18 06:57:45 -05001136 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001137};
1138
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001139#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001140 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001141 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001142 .timeout.wait_q = NULL, \
1143 .timeout.thread = NULL, \
1144 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001145 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001146 .expiry_fn = expiry, \
1147 .stop_fn = stop, \
1148 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001149 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001150 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001151 }
1152
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001153#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1154
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155/**
Allan Stephensc98da842016-11-11 15:45:03 -05001156 * INTERNAL_HIDDEN @endcond
1157 */
1158
1159/**
1160 * @defgroup timer_apis Timer APIs
1161 * @ingroup kernel_apis
1162 * @{
1163 */
1164
1165/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001166 * @typedef k_timer_expiry_t
1167 * @brief Timer expiry function type.
1168 *
1169 * A timer's expiry function is executed by the system clock interrupt handler
1170 * each time the timer expires. The expiry function is optional, and is only
1171 * invoked if the timer has been initialized with one.
1172 *
1173 * @param timer Address of timer.
1174 *
1175 * @return N/A
1176 */
1177typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1178
1179/**
1180 * @typedef k_timer_stop_t
1181 * @brief Timer stop function type.
1182 *
1183 * A timer's stop function is executed if the timer is stopped prematurely.
1184 * The function runs in the context of the thread that stops the timer.
1185 * The stop function is optional, and is only invoked if the timer has been
1186 * initialized with one.
1187 *
1188 * @param timer Address of timer.
1189 *
1190 * @return N/A
1191 */
1192typedef void (*k_timer_stop_t)(struct k_timer *timer);
1193
1194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001199 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 *
1201 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001202 * @param expiry_fn Function to invoke each time the timer expires.
1203 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001204 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001205#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001206 struct k_timer name \
1207 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001208 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
Allan Stephens45bfa372016-10-12 12:39:42 -05001210/**
1211 * @brief Initialize a timer.
1212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001213 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001214 *
1215 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001216 * @param expiry_fn Function to invoke each time the timer expires.
1217 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001218 *
1219 * @return N/A
1220 */
1221extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001222 k_timer_expiry_t expiry_fn,
1223 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001224
Allan Stephens45bfa372016-10-12 12:39:42 -05001225/**
1226 * @brief Start a timer.
1227 *
1228 * This routine starts a timer, and resets its status to zero. The timer
1229 * begins counting down using the specified duration and period values.
1230 *
1231 * Attempting to start a timer that is already running is permitted.
1232 * The timer's status is reset to zero and the timer begins counting down
1233 * using the new duration and period values.
1234 *
1235 * @param timer Address of timer.
1236 * @param duration Initial timer duration (in milliseconds).
1237 * @param period Timer period (in milliseconds).
1238 *
1239 * @return N/A
1240 */
Andrew Boiea354d492017-09-29 16:22:28 -07001241__syscall void k_timer_start(struct k_timer *timer,
1242 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001243
1244/**
1245 * @brief Stop a timer.
1246 *
1247 * This routine stops a running timer prematurely. The timer's stop function,
1248 * if one exists, is invoked by the caller.
1249 *
1250 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001252 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001253 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1254 * if @a k_timer_stop is to be called from ISRs.
1255 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001256 * @param timer Address of timer.
1257 *
1258 * @return N/A
1259 */
Andrew Boiea354d492017-09-29 16:22:28 -07001260__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001261
1262/**
1263 * @brief Read timer status.
1264 *
1265 * This routine reads the timer's status, which indicates the number of times
1266 * it has expired since its status was last read.
1267 *
1268 * Calling this routine resets the timer's status to zero.
1269 *
1270 * @param timer Address of timer.
1271 *
1272 * @return Timer status.
1273 */
Andrew Boiea354d492017-09-29 16:22:28 -07001274__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001275
1276/**
1277 * @brief Synchronize thread to timer expiration.
1278 *
1279 * This routine blocks the calling thread until the timer's status is non-zero
1280 * (indicating that it has expired at least once since it was last examined)
1281 * or the timer is stopped. If the timer status is already non-zero,
1282 * or the timer is already stopped, the caller continues without waiting.
1283 *
1284 * Calling this routine resets the timer's status to zero.
1285 *
1286 * This routine must not be used by interrupt handlers, since they are not
1287 * allowed to block.
1288 *
1289 * @param timer Address of timer.
1290 *
1291 * @return Timer status.
1292 */
Andrew Boiea354d492017-09-29 16:22:28 -07001293__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001294
1295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001297 *
1298 * This routine computes the (approximate) time remaining before a running
1299 * timer next expires. If the timer is not running, it returns zero.
1300 *
1301 * @param timer Address of timer.
1302 *
1303 * @return Remaining time (in milliseconds).
1304 */
Andrew Boiea354d492017-09-29 16:22:28 -07001305__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1306
1307static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001308{
1309 return _timeout_remaining_get(&timer->timeout);
1310}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001311
Allan Stephensc98da842016-11-11 15:45:03 -05001312/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001313 * @brief Associate user-specific data with a timer.
1314 *
1315 * This routine records the @a user_data with the @a timer, to be retrieved
1316 * later.
1317 *
1318 * It can be used e.g. in a timer handler shared across multiple subsystems to
1319 * retrieve data specific to the subsystem this timer is associated with.
1320 *
1321 * @param timer Address of timer.
1322 * @param user_data User data to associate with the timer.
1323 *
1324 * @return N/A
1325 */
Andrew Boiea354d492017-09-29 16:22:28 -07001326__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1327
1328static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1329 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001330{
1331 timer->user_data = user_data;
1332}
1333
1334/**
1335 * @brief Retrieve the user-specific data from a timer.
1336 *
1337 * @param timer Address of timer.
1338 *
1339 * @return The user data.
1340 */
Andrew Boiea354d492017-09-29 16:22:28 -07001341__syscall void *k_timer_user_data_get(struct k_timer *timer);
1342
1343static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001344{
1345 return timer->user_data;
1346}
1347
1348/**
Allan Stephensc98da842016-11-11 15:45:03 -05001349 * @} end defgroup timer_apis
1350 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001351
Allan Stephensc98da842016-11-11 15:45:03 -05001352/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001353 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001354 * @{
1355 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001356
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * This routine returns the elapsed time since the system booted,
1361 * in milliseconds.
1362 *
1363 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001364 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001365__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001366
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001367#ifdef CONFIG_TICKLESS_KERNEL
1368/**
1369 * @brief Enable clock always on in tickless kernel
1370 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001371 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001372 * there are no timer events programmed in tickless kernel
1373 * scheduling. This is necessary if the clock is used to track
1374 * passage of time.
1375 *
1376 * @retval prev_status Previous status of always on flag
1377 */
1378static inline int k_enable_sys_clock_always_on(void)
1379{
1380 int prev_status = _sys_clock_always_on;
1381
1382 _sys_clock_always_on = 1;
1383 _enable_sys_clock();
1384
1385 return prev_status;
1386}
1387
1388/**
1389 * @brief Disable clock always on in tickless kernel
1390 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001391 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001392 * there are no timer events programmed in tickless kernel
1393 * scheduling. To save power, this routine should be called
1394 * immediately when clock is not used to track time.
1395 */
1396static inline void k_disable_sys_clock_always_on(void)
1397{
1398 _sys_clock_always_on = 0;
1399}
1400#else
1401#define k_enable_sys_clock_always_on() do { } while ((0))
1402#define k_disable_sys_clock_always_on() do { } while ((0))
1403#endif
1404
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001408 * This routine returns the lower 32-bits of the elapsed time since the system
1409 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001411 * This routine can be more efficient than k_uptime_get(), as it reduces the
1412 * need for interrupt locking and 64-bit math. However, the 32-bit result
1413 * cannot hold a system uptime time larger than approximately 50 days, so the
1414 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001416 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001417 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001418__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001419
1420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001421 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001423 * This routine computes the elapsed time between the current system uptime
1424 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001426 * @param reftime Pointer to a reference time, which is updated to the current
1427 * uptime upon return.
1428 *
1429 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001430 */
Kumar Galacc334c72017-04-21 10:55:34 -05001431extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001432
1433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001434 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001436 * This routine computes the elapsed time between the current system uptime
1437 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001439 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1440 * need for interrupt locking and 64-bit math. However, the 32-bit result
1441 * cannot hold an elapsed time larger than approximately 50 days, so the
1442 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * @param reftime Pointer to a reference time, which is updated to the current
1445 * uptime upon return.
1446 *
1447 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001448 */
Kumar Galacc334c72017-04-21 10:55:34 -05001449extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001450
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001452 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001454 * This routine returns the current time, as measured by the system's hardware
1455 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001458 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001459#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001461/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001462 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463 */
1464
Allan Stephensc98da842016-11-11 15:45:03 -05001465/**
1466 * @cond INTERNAL_HIDDEN
1467 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001468
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001469struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001470 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001471 union {
1472 _wait_q_t wait_q;
1473
1474 _POLL_EVENT;
1475 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001476
1477 _OBJECT_TRACING_NEXT_PTR(k_queue);
1478};
1479
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001480#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001481 { \
1482 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1483 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001484 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001485 _OBJECT_TRACING_INIT \
1486 }
1487
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001488#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1489
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001490/**
1491 * INTERNAL_HIDDEN @endcond
1492 */
1493
1494/**
1495 * @defgroup queue_apis Queue APIs
1496 * @ingroup kernel_apis
1497 * @{
1498 */
1499
1500/**
1501 * @brief Initialize a queue.
1502 *
1503 * This routine initializes a queue object, prior to its first use.
1504 *
1505 * @param queue Address of the queue.
1506 *
1507 * @return N/A
1508 */
1509extern void k_queue_init(struct k_queue *queue);
1510
1511/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001512 * @brief Cancel waiting on a queue.
1513 *
1514 * This routine causes first thread pending on @a queue, if any, to
1515 * return from k_queue_get() call with NULL value (as if timeout expired).
1516 *
1517 * @note Can be called by ISRs.
1518 *
1519 * @param queue Address of the queue.
1520 *
1521 * @return N/A
1522 */
1523extern void k_queue_cancel_wait(struct k_queue *queue);
1524
1525/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001526 * @brief Append an element to the end of a queue.
1527 *
1528 * This routine appends a data item to @a queue. A queue data item must be
1529 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1530 * reserved for the kernel's use.
1531 *
1532 * @note Can be called by ISRs.
1533 *
1534 * @param queue Address of the queue.
1535 * @param data Address of the data item.
1536 *
1537 * @return N/A
1538 */
1539extern void k_queue_append(struct k_queue *queue, void *data);
1540
1541/**
1542 * @brief Prepend an element to a queue.
1543 *
1544 * This routine prepends a data item to @a queue. A queue data item must be
1545 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1546 * reserved for the kernel's use.
1547 *
1548 * @note Can be called by ISRs.
1549 *
1550 * @param queue Address of the queue.
1551 * @param data Address of the data item.
1552 *
1553 * @return N/A
1554 */
1555extern void k_queue_prepend(struct k_queue *queue, void *data);
1556
1557/**
1558 * @brief Inserts an element to a queue.
1559 *
1560 * This routine inserts a data item to @a queue after previous item. A queue
1561 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1562 * item are reserved for the kernel's use.
1563 *
1564 * @note Can be called by ISRs.
1565 *
1566 * @param queue Address of the queue.
1567 * @param prev Address of the previous data item.
1568 * @param data Address of the data item.
1569 *
1570 * @return N/A
1571 */
1572extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1573
1574/**
1575 * @brief Atomically append a list of elements to a queue.
1576 *
1577 * This routine adds a list of data items to @a queue in one operation.
1578 * The data items must be in a singly-linked list, with the first 32 bits
1579 * in each data item pointing to the next data item; the list must be
1580 * NULL-terminated.
1581 *
1582 * @note Can be called by ISRs.
1583 *
1584 * @param queue Address of the queue.
1585 * @param head Pointer to first node in singly-linked list.
1586 * @param tail Pointer to last node in singly-linked list.
1587 *
1588 * @return N/A
1589 */
1590extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1591
1592/**
1593 * @brief Atomically add a list of elements to a queue.
1594 *
1595 * This routine adds a list of data items to @a queue in one operation.
1596 * The data items must be in a singly-linked list implemented using a
1597 * sys_slist_t object. Upon completion, the original list is empty.
1598 *
1599 * @note Can be called by ISRs.
1600 *
1601 * @param queue Address of the queue.
1602 * @param list Pointer to sys_slist_t object.
1603 *
1604 * @return N/A
1605 */
1606extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1607
1608/**
1609 * @brief Get an element from a queue.
1610 *
1611 * This routine removes first data item from @a queue. The first 32 bits of the
1612 * data item are reserved for the kernel's use.
1613 *
1614 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1615 *
1616 * @param queue Address of the queue.
1617 * @param timeout Waiting period to obtain a data item (in milliseconds),
1618 * or one of the special values K_NO_WAIT and K_FOREVER.
1619 *
1620 * @return Address of the data item if successful; NULL if returned
1621 * without waiting, or waiting period timed out.
1622 */
Kumar Galacc334c72017-04-21 10:55:34 -05001623extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001624
1625/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001626 * @brief Remove an element from a queue.
1627 *
1628 * This routine removes data item from @a queue. The first 32 bits of the
1629 * data item are reserved for the kernel's use. Removing elements from k_queue
1630 * rely on sys_slist_find_and_remove which is not a constant time operation.
1631 *
1632 * @note Can be called by ISRs
1633 *
1634 * @param queue Address of the queue.
1635 * @param data Address of the data item.
1636 *
1637 * @return true if data item was removed
1638 */
1639static inline bool k_queue_remove(struct k_queue *queue, void *data)
1640{
1641 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1642}
1643
1644/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001645 * @brief Query a queue to see if it has data available.
1646 *
1647 * Note that the data might be already gone by the time this function returns
1648 * if other threads are also trying to read from the queue.
1649 *
1650 * @note Can be called by ISRs.
1651 *
1652 * @param queue Address of the queue.
1653 *
1654 * @return Non-zero if the queue is empty.
1655 * @return 0 if data is available.
1656 */
1657static inline int k_queue_is_empty(struct k_queue *queue)
1658{
1659 return (int)sys_slist_is_empty(&queue->data_q);
1660}
1661
1662/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001663 * @brief Peek element at the head of queue.
1664 *
1665 * Return element from the head of queue without removing it.
1666 *
1667 * @param queue Address of the queue.
1668 *
1669 * @return Head element, or NULL if queue is empty.
1670 */
1671static inline void *k_queue_peek_head(struct k_queue *queue)
1672{
1673 return sys_slist_peek_head(&queue->data_q);
1674}
1675
1676/**
1677 * @brief Peek element at the tail of queue.
1678 *
1679 * Return element from the tail of queue without removing it.
1680 *
1681 * @param queue Address of the queue.
1682 *
1683 * @return Tail element, or NULL if queue is empty.
1684 */
1685static inline void *k_queue_peek_tail(struct k_queue *queue)
1686{
1687 return sys_slist_peek_tail(&queue->data_q);
1688}
1689
1690/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001691 * @brief Statically define and initialize a queue.
1692 *
1693 * The queue can be accessed outside the module where it is defined using:
1694 *
1695 * @code extern struct k_queue <name>; @endcode
1696 *
1697 * @param name Name of the queue.
1698 */
1699#define K_QUEUE_DEFINE(name) \
1700 struct k_queue name \
1701 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001702 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001703
1704/**
1705 * @} end defgroup queue_apis
1706 */
1707
1708/**
1709 * @cond INTERNAL_HIDDEN
1710 */
1711
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001712struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001713 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714};
1715
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001716#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001717 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001718 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001719 }
1720
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001721#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1722
Allan Stephensc98da842016-11-11 15:45:03 -05001723/**
1724 * INTERNAL_HIDDEN @endcond
1725 */
1726
1727/**
1728 * @defgroup fifo_apis Fifo APIs
1729 * @ingroup kernel_apis
1730 * @{
1731 */
1732
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
1740 * @return N/A
1741 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001742#define k_fifo_init(fifo) \
1743 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744
1745/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001746 * @brief Cancel waiting on a fifo.
1747 *
1748 * This routine causes first thread pending on @a fifo, if any, to
1749 * return from k_fifo_get() call with NULL value (as if timeout
1750 * expired).
1751 *
1752 * @note Can be called by ISRs.
1753 *
1754 * @param fifo Address of the fifo.
1755 *
1756 * @return N/A
1757 */
1758#define k_fifo_cancel_wait(fifo) \
1759 k_queue_cancel_wait((struct k_queue *) fifo)
1760
1761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * This routine adds a data item to @a fifo. A fifo data item must be
1765 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1766 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * @note Can be called by ISRs.
1769 *
1770 * @param fifo Address of the fifo.
1771 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
1773 * @return N/A
1774 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001775#define k_fifo_put(fifo, data) \
1776 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777
1778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * This routine adds a list of data items to @a fifo in one operation.
1782 * The data items must be in a singly-linked list, with the first 32 bits
1783 * each data item pointing to the next data item; the list must be
1784 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001786 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * @param fifo Address of the fifo.
1789 * @param head Pointer to first node in singly-linked list.
1790 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791 *
1792 * @return N/A
1793 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001794#define k_fifo_put_list(fifo, head, tail) \
1795 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796
1797/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * This routine adds a list of data items to @a fifo in one operation.
1801 * The data items must be in a singly-linked list implemented using a
1802 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 * and must be re-initialized via sys_slist_init().
1804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001805 * @note Can be called by ISRs.
1806 *
1807 * @param fifo Address of the fifo.
1808 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809 *
1810 * @return N/A
1811 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001812#define k_fifo_put_slist(fifo, list) \
1813 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814
1815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * This routine removes a data item from @a fifo in a "first in, first out"
1819 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1822 *
1823 * @param fifo Address of the fifo.
1824 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001825 * or one of the special values K_NO_WAIT and K_FOREVER.
1826 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001827 * @return Address of the data item if successful; NULL if returned
1828 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001830#define k_fifo_get(fifo, timeout) \
1831 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001832
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001834 * @brief Query a fifo to see if it has data available.
1835 *
1836 * Note that the data might be already gone by the time this function returns
1837 * if other threads is also trying to read from the fifo.
1838 *
1839 * @note Can be called by ISRs.
1840 *
1841 * @param fifo Address of the fifo.
1842 *
1843 * @return Non-zero if the fifo is empty.
1844 * @return 0 if data is available.
1845 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001846#define k_fifo_is_empty(fifo) \
1847 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001848
1849/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001850 * @brief Peek element at the head of fifo.
1851 *
1852 * Return element from the head of fifo without removing it. A usecase
1853 * for this is if elements of the fifo are themselves containers. Then
1854 * on each iteration of processing, a head container will be peeked,
1855 * and some data processed out of it, and only if the container is empty,
1856 * it will be completely remove from the fifo.
1857 *
1858 * @param fifo Address of the fifo.
1859 *
1860 * @return Head element, or NULL if the fifo is empty.
1861 */
1862#define k_fifo_peek_head(fifo) \
1863 k_queue_peek_head((struct k_queue *) fifo)
1864
1865/**
1866 * @brief Peek element at the tail of fifo.
1867 *
1868 * Return element from the tail of fifo (without removing it). A usecase
1869 * for this is if elements of the fifo are themselves containers. Then
1870 * it may be useful to add more data to the last container in fifo.
1871 *
1872 * @param fifo Address of the fifo.
1873 *
1874 * @return Tail element, or NULL if fifo is empty.
1875 */
1876#define k_fifo_peek_tail(fifo) \
1877 k_queue_peek_tail((struct k_queue *) fifo)
1878
1879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001884 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001889 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001890 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001891 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892
Allan Stephensc98da842016-11-11 15:45:03 -05001893/**
1894 * @} end defgroup fifo_apis
1895 */
1896
1897/**
1898 * @cond INTERNAL_HIDDEN
1899 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900
1901struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001902 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903};
1904
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001905#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001906 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001907 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001908 }
1909
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001910#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1911
Allan Stephensc98da842016-11-11 15:45:03 -05001912/**
1913 * INTERNAL_HIDDEN @endcond
1914 */
1915
1916/**
1917 * @defgroup lifo_apis Lifo APIs
1918 * @ingroup kernel_apis
1919 * @{
1920 */
1921
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001925 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
1929 * @return N/A
1930 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001931#define k_lifo_init(lifo) \
1932 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001933
1934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001937 * This routine adds a data item to @a lifo. A lifo data item must be
1938 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1939 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001941 * @note Can be called by ISRs.
1942 *
1943 * @param lifo Address of the lifo.
1944 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 *
1946 * @return N/A
1947 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001948#define k_lifo_put(lifo, data) \
1949 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950
1951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * This routine removes a data item from @a lifo in a "last in, first out"
1955 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1958 *
1959 * @param lifo Address of the lifo.
1960 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961 * or one of the special values K_NO_WAIT and K_FOREVER.
1962 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001963 * @return Address of the data item if successful; NULL if returned
1964 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001966#define k_lifo_get(lifo, timeout) \
1967 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001970 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001974 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001976 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001978#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001979 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001980 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001981 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982
Allan Stephensc98da842016-11-11 15:45:03 -05001983/**
1984 * @} end defgroup lifo_apis
1985 */
1986
1987/**
1988 * @cond INTERNAL_HIDDEN
1989 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990
1991struct k_stack {
1992 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001993 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001994
Anas Nashif2f203c22016-12-18 06:57:45 -05001995 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996};
1997
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001998#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001999 { \
2000 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2001 .base = stack_buffer, \
2002 .next = stack_buffer, \
2003 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002004 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002005 }
2006
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002007#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2008
Allan Stephensc98da842016-11-11 15:45:03 -05002009/**
2010 * INTERNAL_HIDDEN @endcond
2011 */
2012
2013/**
2014 * @defgroup stack_apis Stack APIs
2015 * @ingroup kernel_apis
2016 * @{
2017 */
2018
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019/**
2020 * @brief Initialize a stack.
2021 *
2022 * This routine initializes a stack object, prior to its first use.
2023 *
2024 * @param stack Address of the stack.
2025 * @param buffer Address of array used to hold stacked values.
2026 * @param num_entries Maximum number of values that can be stacked.
2027 *
2028 * @return N/A
2029 */
Andrew Boiee8734462017-09-29 16:42:07 -07002030__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002031 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032
2033/**
2034 * @brief Push an element onto a stack.
2035 *
2036 * This routine adds a 32-bit value @a data to @a stack.
2037 *
2038 * @note Can be called by ISRs.
2039 *
2040 * @param stack Address of the stack.
2041 * @param data Value to push onto the stack.
2042 *
2043 * @return N/A
2044 */
Andrew Boiee8734462017-09-29 16:42:07 -07002045__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002046
2047/**
2048 * @brief Pop an element from a stack.
2049 *
2050 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2051 * manner and stores the value in @a data.
2052 *
2053 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2054 *
2055 * @param stack Address of the stack.
2056 * @param data Address of area to hold the value popped from the stack.
2057 * @param timeout Waiting period to obtain a value (in milliseconds),
2058 * or one of the special values K_NO_WAIT and K_FOREVER.
2059 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002060 * @retval 0 Element popped from stack.
2061 * @retval -EBUSY Returned without waiting.
2062 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 */
Andrew Boiee8734462017-09-29 16:42:07 -07002064__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002067 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002071 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @param name Name of the stack.
2074 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002076#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002077 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002078 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002079 struct k_stack name \
2080 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002081 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002082 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002084/**
Allan Stephensc98da842016-11-11 15:45:03 -05002085 * @} end defgroup stack_apis
2086 */
2087
Allan Stephens6bba9b02016-11-16 14:56:54 -05002088struct k_work;
2089
Allan Stephensc98da842016-11-11 15:45:03 -05002090/**
2091 * @defgroup workqueue_apis Workqueue Thread APIs
2092 * @ingroup kernel_apis
2093 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094 */
2095
Allan Stephens6bba9b02016-11-16 14:56:54 -05002096/**
2097 * @typedef k_work_handler_t
2098 * @brief Work item handler function type.
2099 *
2100 * A work item's handler function is executed by a workqueue's thread
2101 * when the work item is processed by the workqueue.
2102 *
2103 * @param work Address of the work item.
2104 *
2105 * @return N/A
2106 */
2107typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108
2109/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002110 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002111 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002112
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002114 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002115 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116};
2117
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002119 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120};
2121
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002123 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124 k_work_handler_t handler;
2125 atomic_t flags[1];
2126};
2127
Allan Stephens6bba9b02016-11-16 14:56:54 -05002128struct k_delayed_work {
2129 struct k_work work;
2130 struct _timeout timeout;
2131 struct k_work_q *work_q;
2132};
2133
2134extern struct k_work_q k_sys_work_q;
2135
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002136/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002137 * INTERNAL_HIDDEN @endcond
2138 */
2139
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002140#define _K_WORK_INITIALIZER(work_handler) \
2141 { \
2142 ._reserved = NULL, \
2143 .handler = work_handler, \
2144 .flags = { 0 } \
2145 }
2146
2147#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2148
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149/**
2150 * @brief Initialize a statically-defined work item.
2151 *
2152 * This macro can be used to initialize a statically-defined workqueue work
2153 * item, prior to its first use. For example,
2154 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002155 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002156 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002157 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002158 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002160#define K_WORK_DEFINE(work, work_handler) \
2161 struct k_work work \
2162 __in_section(_k_work, static, work) = \
2163 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164
2165/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002166 * @brief Initialize a work item.
2167 *
2168 * This routine initializes a workqueue work item, prior to its first use.
2169 *
2170 * @param work Address of work item.
2171 * @param handler Function to invoke each time work item is processed.
2172 *
2173 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174 */
2175static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2176{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002177 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002179 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180}
2181
2182/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002183 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002184 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185 * This routine submits work item @a work to be processed by workqueue
2186 * @a work_q. If the work item is already pending in the workqueue's queue
2187 * as a result of an earlier submission, this routine has no effect on the
2188 * work item. If the work item has already been processed, or is currently
2189 * being processed, its work is considered complete and the work item can be
2190 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002191 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * @warning
2193 * A submitted work item must not be modified until it has been processed
2194 * by the workqueue.
2195 *
2196 * @note Can be called by ISRs.
2197 *
2198 * @param work_q Address of workqueue.
2199 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002200 *
2201 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202 */
2203static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2204 struct k_work *work)
2205{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002206 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002207 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208 }
2209}
2210
2211/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * This routine indicates if work item @a work is pending in a workqueue's
2215 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002217 * @note Can be called by ISRs.
2218 *
2219 * @param work Address of work item.
2220 *
2221 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002222 */
2223static inline int k_work_pending(struct k_work *work)
2224{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002225 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002226}
2227
2228/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * This routine starts workqueue @a work_q. The workqueue spawns its work
2232 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002235 * @param stack Pointer to work queue thread's stack space, as defined by
2236 * K_THREAD_STACK_DEFINE()
2237 * @param stack_size Size of the work queue thread's stack (in bytes), which
2238 * should either be the same constant passed to
2239 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002240 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 *
2242 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243 */
Andrew Boie507852a2017-07-25 18:47:07 -07002244extern void k_work_q_start(struct k_work_q *work_q,
2245 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002246 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002251 * This routine initializes a workqueue delayed work item, prior to
2252 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002254 * @param work Address of delayed work item.
2255 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 *
2257 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002259extern void k_delayed_work_init(struct k_delayed_work *work,
2260 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002261
2262/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002263 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * This routine schedules work item @a work to be processed by workqueue
2266 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002267 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002268 * Only when the countdown completes is the work item actually submitted to
2269 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002271 * Submitting a previously submitted delayed work item that is still
2272 * counting down cancels the existing submission and restarts the countdown
2273 * using the new delay. If the work item is currently pending on the
2274 * workqueue's queue because the countdown has completed it is too late to
2275 * resubmit the item, and resubmission fails without impacting the work item.
2276 * If the work item has already been processed, or is currently being processed,
2277 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * @warning
2280 * A delayed work item must not be modified until it has been processed
2281 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002283 * @note Can be called by ISRs.
2284 *
2285 * @param work_q Address of workqueue.
2286 * @param work Address of delayed work item.
2287 * @param delay Delay before submitting the work item (in milliseconds).
2288 *
2289 * @retval 0 Work item countdown started.
2290 * @retval -EINPROGRESS Work item is already pending.
2291 * @retval -EINVAL Work item is being processed or has completed its work.
2292 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002294extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2295 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002296 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297
2298/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002299 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002302 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002303 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * @param work Address of delayed work item.
2308 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002309 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * @retval -EINPROGRESS Work item is already pending.
2311 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002313extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316 * @brief Submit a work item to the system workqueue.
2317 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002318 * This routine submits work item @a work to be processed by the system
2319 * workqueue. If the work item is already pending in the workqueue's queue
2320 * as a result of an earlier submission, this routine has no effect on the
2321 * work item. If the work item has already been processed, or is currently
2322 * being processed, its work is considered complete and the work item can be
2323 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * @warning
2326 * Work items submitted to the system workqueue should avoid using handlers
2327 * that block or yield since this may prevent the system workqueue from
2328 * processing other work items in a timely manner.
2329 *
2330 * @note Can be called by ISRs.
2331 *
2332 * @param work Address of work item.
2333 *
2334 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335 */
2336static inline void k_work_submit(struct k_work *work)
2337{
2338 k_work_submit_to_queue(&k_sys_work_q, work);
2339}
2340
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 * @brief Submit a delayed work item to the system workqueue.
2343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * This routine schedules work item @a work to be processed by the system
2345 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002346 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002347 * Only when the countdown completes is the work item actually submitted to
2348 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002350 * Submitting a previously submitted delayed work item that is still
2351 * counting down cancels the existing submission and restarts the countdown
2352 * using the new delay. If the work item is currently pending on the
2353 * workqueue's queue because the countdown has completed it is too late to
2354 * resubmit the item, and resubmission fails without impacting the work item.
2355 * If the work item has already been processed, or is currently being processed,
2356 * its work is considered complete and the work item can be resubmitted.
2357 *
2358 * @warning
2359 * Work items submitted to the system workqueue should avoid using handlers
2360 * that block or yield since this may prevent the system workqueue from
2361 * processing other work items in a timely manner.
2362 *
2363 * @note Can be called by ISRs.
2364 *
2365 * @param work Address of delayed work item.
2366 * @param delay Delay before submitting the work item (in milliseconds).
2367 *
2368 * @retval 0 Work item countdown started.
2369 * @retval -EINPROGRESS Work item is already pending.
2370 * @retval -EINVAL Work item is being processed or has completed its work.
2371 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372 */
2373static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002374 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002376 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377}
2378
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002380 * @brief Get time remaining before a delayed work gets scheduled.
2381 *
2382 * This routine computes the (approximate) time remaining before a
2383 * delayed work gets executed. If the delayed work is not waiting to be
2384 * schedules, it returns zero.
2385 *
2386 * @param work Delayed work item.
2387 *
2388 * @return Remaining time (in milliseconds).
2389 */
Kumar Galacc334c72017-04-21 10:55:34 -05002390static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002391{
2392 return _timeout_remaining_get(&work->timeout);
2393}
2394
2395/**
Allan Stephensc98da842016-11-11 15:45:03 -05002396 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397 */
2398
Allan Stephensc98da842016-11-11 15:45:03 -05002399/**
2400 * @cond INTERNAL_HIDDEN
2401 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402
2403struct k_mutex {
2404 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002405 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002406 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408
Anas Nashif2f203c22016-12-18 06:57:45 -05002409 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410};
2411
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002412#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413 { \
2414 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2415 .owner = NULL, \
2416 .lock_count = 0, \
2417 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002418 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419 }
2420
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002421#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2422
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423/**
Allan Stephensc98da842016-11-11 15:45:03 -05002424 * INTERNAL_HIDDEN @endcond
2425 */
2426
2427/**
2428 * @defgroup mutex_apis Mutex APIs
2429 * @ingroup kernel_apis
2430 * @{
2431 */
2432
2433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002434 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002438 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002440 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002443 struct k_mutex name \
2444 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002445 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * Upon completion, the mutex is available and does not have an owner.
2453 *
2454 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 *
2456 * @return N/A
2457 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002458__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459
2460/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * This routine locks @a mutex. If the mutex is locked by another thread,
2464 * the calling thread waits until the mutex becomes available or until
2465 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467 * A thread is permitted to lock a mutex it has already locked. The operation
2468 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @param mutex Address of the mutex.
2471 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 * or one of the special values K_NO_WAIT and K_FOREVER.
2473 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002474 * @retval 0 Mutex locked.
2475 * @retval -EBUSY Returned without waiting.
2476 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002478__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479
2480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * This routine unlocks @a mutex. The mutex must already be locked by the
2484 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485 *
2486 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 * thread.
2489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 *
2492 * @return N/A
2493 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002494__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495
Allan Stephensc98da842016-11-11 15:45:03 -05002496/**
2497 * @} end defgroup mutex_apis
2498 */
2499
2500/**
2501 * @cond INTERNAL_HIDDEN
2502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503
2504struct k_sem {
2505 _wait_q_t wait_q;
2506 unsigned int count;
2507 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002508 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509
Anas Nashif2f203c22016-12-18 06:57:45 -05002510 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511};
2512
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002513#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002514 { \
2515 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2516 .count = initial_count, \
2517 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002518 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002519 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002520 }
2521
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002522#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2523
Allan Stephensc98da842016-11-11 15:45:03 -05002524/**
2525 * INTERNAL_HIDDEN @endcond
2526 */
2527
2528/**
2529 * @defgroup semaphore_apis Semaphore APIs
2530 * @ingroup kernel_apis
2531 * @{
2532 */
2533
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @param sem Address of the semaphore.
2540 * @param initial_count Initial semaphore count.
2541 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002542 *
2543 * @return N/A
2544 */
Andrew Boie99280232017-09-29 14:17:47 -07002545__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2546 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002547
2548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2554 *
2555 * @param sem Address of the semaphore.
2556 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002557 * or one of the special values K_NO_WAIT and K_FOREVER.
2558 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002559 * @note When porting code from the nanokernel legacy API to the new API, be
2560 * careful with the return value of this function. The return value is the
2561 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2562 * non-zero means failure, while the nano_sem_take family returns 1 for success
2563 * and 0 for failure.
2564 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002565 * @retval 0 Semaphore taken.
2566 * @retval -EBUSY Returned without waiting.
2567 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002568 */
Andrew Boie99280232017-09-29 14:17:47 -07002569__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002570
2571/**
2572 * @brief Give a semaphore.
2573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * This routine gives @a sem, unless the semaphore is already at its maximum
2575 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580 *
2581 * @return N/A
2582 */
Andrew Boie99280232017-09-29 14:17:47 -07002583__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585/**
2586 * @brief Reset a semaphore's count to zero.
2587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002591 *
2592 * @return N/A
2593 */
Andrew Boie990bf162017-10-03 12:36:49 -07002594__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002595
2596static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597{
2598 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599}
2600
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601/**
2602 * @brief Get a semaphore's count.
2603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002609 */
Andrew Boie990bf162017-10-03 12:36:49 -07002610__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002611
2612static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613{
2614 return sem->count;
2615}
2616
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002621 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002622 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @param name Name of the semaphore.
2625 * @param initial_count Initial semaphore count.
2626 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002629 struct k_sem name \
2630 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002631 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632
Allan Stephensc98da842016-11-11 15:45:03 -05002633/**
2634 * @} end defgroup semaphore_apis
2635 */
2636
2637/**
2638 * @defgroup alert_apis Alert APIs
2639 * @ingroup kernel_apis
2640 * @{
2641 */
2642
Allan Stephens5eceb852016-11-16 10:16:30 -05002643/**
2644 * @typedef k_alert_handler_t
2645 * @brief Alert handler function type.
2646 *
2647 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002648 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002649 * and is only invoked if the alert has been initialized with one.
2650 *
2651 * @param alert Address of the alert.
2652 *
2653 * @return 0 if alert has been consumed; non-zero if alert should pend.
2654 */
2655typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002656
2657/**
2658 * @} end defgroup alert_apis
2659 */
2660
2661/**
2662 * @cond INTERNAL_HIDDEN
2663 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002665#define K_ALERT_DEFAULT NULL
2666#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002668struct k_alert {
2669 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670 atomic_t send_count;
2671 struct k_work work_item;
2672 struct k_sem sem;
2673
Anas Nashif2f203c22016-12-18 06:57:45 -05002674 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675};
2676
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002677extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002679#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002681 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002683 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2684 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002685 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686 }
2687
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002688#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2689
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690/**
Allan Stephensc98da842016-11-11 15:45:03 -05002691 * INTERNAL_HIDDEN @endcond
2692 */
2693
2694/**
2695 * @addtogroup alert_apis
2696 * @{
2697 */
2698
2699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002702 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002704 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002706 * @param name Name of the alert.
2707 * @param alert_handler Action to take when alert is sent. Specify either
2708 * the address of a function to be invoked by the system workqueue
2709 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2710 * K_ALERT_DEFAULT (which causes the alert to pend).
2711 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002712 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002713#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002714 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002715 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002716 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002717 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * @param alert Address of the alert.
2725 * @param handler Action to take when alert is sent. Specify either the address
2726 * of a function to be invoked by the system workqueue thread,
2727 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2728 * K_ALERT_DEFAULT (which causes the alert to pend).
2729 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730 *
2731 * @return N/A
2732 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002733extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2734 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002735
2736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2742 *
2743 * @param alert Address of the alert.
2744 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002745 * or one of the special values K_NO_WAIT and K_FOREVER.
2746 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002747 * @retval 0 Alert received.
2748 * @retval -EBUSY Returned without waiting.
2749 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 */
Andrew Boie310e9872017-09-29 04:41:15 -07002751__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752
2753/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * This routine signals @a alert. The action specified for @a alert will
2757 * be taken, which may trigger the execution of an alert handler function
2758 * and/or cause the alert to pend (assuming the alert has not reached its
2759 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * @note Can be called by ISRs.
2762 *
2763 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
2765 * @return N/A
2766 */
Andrew Boie310e9872017-09-29 04:41:15 -07002767__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768
2769/**
Allan Stephensc98da842016-11-11 15:45:03 -05002770 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 */
2772
Allan Stephensc98da842016-11-11 15:45:03 -05002773/**
2774 * @cond INTERNAL_HIDDEN
2775 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776
2777struct k_msgq {
2778 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002779 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002780 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781 char *buffer_start;
2782 char *buffer_end;
2783 char *read_ptr;
2784 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002785 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786
Anas Nashif2f203c22016-12-18 06:57:45 -05002787 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788};
2789
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002790#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 { \
2792 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002793 .max_msgs = q_max_msgs, \
2794 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002796 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797 .read_ptr = q_buffer, \
2798 .write_ptr = q_buffer, \
2799 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002800 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 }
2802
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002803#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2804
Peter Mitsis1da807e2016-10-06 11:36:59 -04002805/**
Allan Stephensc98da842016-11-11 15:45:03 -05002806 * INTERNAL_HIDDEN @endcond
2807 */
2808
2809/**
2810 * @defgroup msgq_apis Message Queue APIs
2811 * @ingroup kernel_apis
2812 * @{
2813 */
2814
2815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2819 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002820 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2821 * message is similarly aligned to this boundary, @a q_msg_size must also be
2822 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * The message queue can be accessed outside the module where it is defined
2825 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002827 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002829 * @param q_name Name of the message queue.
2830 * @param q_msg_size Message size (in bytes).
2831 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002832 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002833 */
2834#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2835 static char __noinit __aligned(q_align) \
2836 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002837 struct k_msgq q_name \
2838 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002839 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002840 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841
Peter Mitsisd7a37502016-10-13 11:37:40 -04002842/**
2843 * @brief Initialize a message queue.
2844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * This routine initializes a message queue object, prior to its first use.
2846 *
Allan Stephensda827222016-11-09 14:23:58 -06002847 * The message queue's ring buffer must contain space for @a max_msgs messages,
2848 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2849 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2850 * that each message is similarly aligned to this boundary, @a q_msg_size
2851 * must also be a multiple of N.
2852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @param q Address of the message queue.
2854 * @param buffer Pointer to ring buffer that holds queued messages.
2855 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002856 * @param max_msgs Maximum number of messages that can be queued.
2857 *
2858 * @return N/A
2859 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002860__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2861 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002862
2863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002867 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002868 * @note Can be called by ISRs.
2869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @param q Address of the message queue.
2871 * @param data Pointer to the message.
2872 * @param timeout Waiting period to add the message (in milliseconds),
2873 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002874 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002875 * @retval 0 Message sent.
2876 * @retval -ENOMSG Returned without waiting or queue purged.
2877 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002878 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002879__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002880
2881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * This routine receives a message from message queue @a q in a "first in,
2885 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002886 *
Allan Stephensc98da842016-11-11 15:45:03 -05002887 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * @param q Address of the message queue.
2890 * @param data Address of area to hold the received message.
2891 * @param timeout Waiting period to receive the message (in milliseconds),
2892 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002893 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002894 * @retval 0 Message received.
2895 * @retval -ENOMSG Returned without waiting.
2896 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002897 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002898__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002899
2900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * This routine discards all unreceived messages in a message queue's ring
2904 * buffer. Any threads that are blocked waiting to send a message to the
2905 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002908 *
2909 * @return N/A
2910 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002911__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912
Peter Mitsis67be2492016-10-07 11:44:34 -04002913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * This routine returns the number of unused entries in a message queue's
2917 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @param q Address of the message queue.
2920 *
2921 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002922 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002923__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2924
2925static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002926{
2927 return q->max_msgs - q->used_msgs;
2928}
2929
Peter Mitsisd7a37502016-10-13 11:37:40 -04002930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * @param q Address of the message queue.
2936 *
2937 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002938 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002939__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2940
2941static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942{
2943 return q->used_msgs;
2944}
2945
Allan Stephensc98da842016-11-11 15:45:03 -05002946/**
2947 * @} end defgroup msgq_apis
2948 */
2949
2950/**
2951 * @defgroup mem_pool_apis Memory Pool APIs
2952 * @ingroup kernel_apis
2953 * @{
2954 */
2955
Andy Ross73cb9582017-05-09 10:42:39 -07002956/* Note on sizing: the use of a 20 bit field for block means that,
2957 * assuming a reasonable minimum block size of 16 bytes, we're limited
2958 * to 16M of memory managed by a single pool. Long term it would be
2959 * good to move to a variable bit size based on configuration.
2960 */
2961struct k_mem_block_id {
2962 u32_t pool : 8;
2963 u32_t level : 4;
2964 u32_t block : 20;
2965};
2966
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002967struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002968 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002969 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970};
2971
Allan Stephensc98da842016-11-11 15:45:03 -05002972/**
2973 * @} end defgroup mem_pool_apis
2974 */
2975
2976/**
2977 * @defgroup mailbox_apis Mailbox APIs
2978 * @ingroup kernel_apis
2979 * @{
2980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981
2982struct k_mbox_msg {
2983 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002984 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002986 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002988 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989 /** sender's message data buffer */
2990 void *tx_data;
2991 /** internal use only - needed for legacy API support */
2992 void *_rx_data;
2993 /** message data block descriptor */
2994 struct k_mem_block tx_block;
2995 /** source thread id */
2996 k_tid_t rx_source_thread;
2997 /** target thread id */
2998 k_tid_t tx_target_thread;
2999 /** internal use only - thread waiting on send (may be a dummy) */
3000 k_tid_t _syncing_thread;
3001#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3002 /** internal use only - semaphore used during asynchronous send */
3003 struct k_sem *_async_sem;
3004#endif
3005};
3006
Allan Stephensc98da842016-11-11 15:45:03 -05003007/**
3008 * @cond INTERNAL_HIDDEN
3009 */
3010
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011struct k_mbox {
3012 _wait_q_t tx_msg_queue;
3013 _wait_q_t rx_msg_queue;
3014
Anas Nashif2f203c22016-12-18 06:57:45 -05003015 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016};
3017
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003018#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019 { \
3020 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3021 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003022 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023 }
3024
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003025#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3026
Peter Mitsis12092702016-10-14 12:57:23 -04003027/**
Allan Stephensc98da842016-11-11 15:45:03 -05003028 * INTERNAL_HIDDEN @endcond
3029 */
3030
3031/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003035 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003036 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003039 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003041 struct k_mbox name \
3042 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003043 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044
Peter Mitsis12092702016-10-14 12:57:23 -04003045/**
3046 * @brief Initialize a mailbox.
3047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * This routine initializes a mailbox object, prior to its first use.
3049 *
3050 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003051 *
3052 * @return N/A
3053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054extern void k_mbox_init(struct k_mbox *mbox);
3055
Peter Mitsis12092702016-10-14 12:57:23 -04003056/**
3057 * @brief Send a mailbox message in a synchronous manner.
3058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * This routine sends a message to @a mbox and waits for a receiver to both
3060 * receive and process it. The message data may be in a buffer, in a memory
3061 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @param mbox Address of the mailbox.
3064 * @param tx_msg Address of the transmit message descriptor.
3065 * @param timeout Waiting period for the message to be received (in
3066 * milliseconds), or one of the special values K_NO_WAIT
3067 * and K_FOREVER. Once the message has been received,
3068 * this routine waits as long as necessary for the message
3069 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003070 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003071 * @retval 0 Message sent.
3072 * @retval -ENOMSG Returned without waiting.
3073 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003074 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003075extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003076 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003077
Peter Mitsis12092702016-10-14 12:57:23 -04003078/**
3079 * @brief Send a mailbox message in an asynchronous manner.
3080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * This routine sends a message to @a mbox without waiting for a receiver
3082 * to process it. The message data may be in a buffer, in a memory pool block,
3083 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3084 * will be given when the message has been both received and completely
3085 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003087 * @param mbox Address of the mailbox.
3088 * @param tx_msg Address of the transmit message descriptor.
3089 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003090 *
3091 * @return N/A
3092 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003093extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094 struct k_sem *sem);
3095
Peter Mitsis12092702016-10-14 12:57:23 -04003096/**
3097 * @brief Receive a mailbox message.
3098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003099 * This routine receives a message from @a mbox, then optionally retrieves
3100 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * @param mbox Address of the mailbox.
3103 * @param rx_msg Address of the receive message descriptor.
3104 * @param buffer Address of the buffer to receive data, or NULL to defer data
3105 * retrieval and message disposal until later.
3106 * @param timeout Waiting period for a message to be received (in
3107 * milliseconds), or one of the special values K_NO_WAIT
3108 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003109 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003110 * @retval 0 Message received.
3111 * @retval -ENOMSG Returned without waiting.
3112 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003113 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003114extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003115 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003116
3117/**
3118 * @brief Retrieve mailbox message data into a buffer.
3119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003120 * This routine completes the processing of a received message by retrieving
3121 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003122 *
3123 * Alternatively, this routine can be used to dispose of a received message
3124 * without retrieving its data.
3125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003126 * @param rx_msg Address of the receive message descriptor.
3127 * @param buffer Address of the buffer to receive data, or NULL to discard
3128 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003129 *
3130 * @return N/A
3131 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003132extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003133
3134/**
3135 * @brief Retrieve mailbox message data into a memory pool block.
3136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * This routine completes the processing of a received message by retrieving
3138 * its data into a memory pool block, then disposing of the message.
3139 * The memory pool block that results from successful retrieval must be
3140 * returned to the pool once the data has been processed, even in cases
3141 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003142 *
3143 * Alternatively, this routine can be used to dispose of a received message
3144 * without retrieving its data. In this case there is no need to return a
3145 * memory pool block to the pool.
3146 *
3147 * This routine allocates a new memory pool block for the data only if the
3148 * data is not already in one. If a new block cannot be allocated, the routine
3149 * returns a failure code and the received message is left unchanged. This
3150 * permits the caller to reattempt data retrieval at a later time or to dispose
3151 * of the received message without retrieving its data.
3152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003153 * @param rx_msg Address of a receive message descriptor.
3154 * @param pool Address of memory pool, or NULL to discard data.
3155 * @param block Address of the area to hold memory pool block info.
3156 * @param timeout Waiting period to wait for a memory pool block (in
3157 * milliseconds), or one of the special values K_NO_WAIT
3158 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003159 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003160 * @retval 0 Data retrieved.
3161 * @retval -ENOMEM Returned without waiting.
3162 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003163 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003164extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003165 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003166 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003167
Allan Stephensc98da842016-11-11 15:45:03 -05003168/**
3169 * @} end defgroup mailbox_apis
3170 */
3171
3172/**
3173 * @cond INTERNAL_HIDDEN
3174 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175
3176struct k_pipe {
3177 unsigned char *buffer; /* Pipe buffer: may be NULL */
3178 size_t size; /* Buffer size */
3179 size_t bytes_used; /* # bytes used in buffer */
3180 size_t read_index; /* Where in buffer to read from */
3181 size_t write_index; /* Where in buffer to write */
3182
3183 struct {
3184 _wait_q_t readers; /* Reader wait queue */
3185 _wait_q_t writers; /* Writer wait queue */
3186 } wait_q;
3187
Anas Nashif2f203c22016-12-18 06:57:45 -05003188 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003189};
3190
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003191#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003192 { \
3193 .buffer = pipe_buffer, \
3194 .size = pipe_buffer_size, \
3195 .bytes_used = 0, \
3196 .read_index = 0, \
3197 .write_index = 0, \
3198 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3199 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003200 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201 }
3202
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003203#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3204
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003205/**
Allan Stephensc98da842016-11-11 15:45:03 -05003206 * INTERNAL_HIDDEN @endcond
3207 */
3208
3209/**
3210 * @defgroup pipe_apis Pipe APIs
3211 * @ingroup kernel_apis
3212 * @{
3213 */
3214
3215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003219 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003220 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * @param name Name of the pipe.
3223 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3224 * or zero if no ring buffer is used.
3225 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003226 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003227#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3228 static unsigned char __noinit __aligned(pipe_align) \
3229 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003230 struct k_pipe name \
3231 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003232 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003235 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * This routine initializes a pipe object, prior to its first use.
3238 *
3239 * @param pipe Address of the pipe.
3240 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3241 * is used.
3242 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3243 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244 *
3245 * @return N/A
3246 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003247__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3248 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249
3250/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param pipe Address of the pipe.
3256 * @param data Address of data to write.
3257 * @param bytes_to_write Size of data (in bytes).
3258 * @param bytes_written Address of area to hold the number of bytes written.
3259 * @param min_xfer Minimum number of bytes to write.
3260 * @param timeout Waiting period to wait for the data to be written (in
3261 * milliseconds), or one of the special values K_NO_WAIT
3262 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003264 * @retval 0 At least @a min_xfer bytes of data were written.
3265 * @retval -EIO Returned without waiting; zero data bytes were written.
3266 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003269__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3270 size_t bytes_to_write, size_t *bytes_written,
3271 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272
3273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * @param pipe Address of the pipe.
3279 * @param data Address to place the data read from pipe.
3280 * @param bytes_to_read Maximum number of data bytes to read.
3281 * @param bytes_read Address of area to hold the number of bytes read.
3282 * @param min_xfer Minimum number of data bytes to read.
3283 * @param timeout Waiting period to wait for the data to be read (in
3284 * milliseconds), or one of the special values K_NO_WAIT
3285 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003287 * @retval 0 At least @a min_xfer bytes of data were read.
3288 * @retval -EIO Returned without waiting; zero data bytes were read.
3289 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003290 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003292__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3293 size_t bytes_to_read, size_t *bytes_read,
3294 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295
3296/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * This routine writes the data contained in a memory block to @a pipe.
3300 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304 * @param block Memory block containing data to send
3305 * @param size Number of data bytes in memory block to send
3306 * @param sem Semaphore to signal upon completion (else NULL)
3307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309 */
3310extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3311 size_t size, struct k_sem *sem);
3312
3313/**
Allan Stephensc98da842016-11-11 15:45:03 -05003314 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315 */
3316
Allan Stephensc98da842016-11-11 15:45:03 -05003317/**
3318 * @cond INTERNAL_HIDDEN
3319 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003321struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003323 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003324 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325 char *buffer;
3326 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003327 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328
Anas Nashif2f203c22016-12-18 06:57:45 -05003329 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330};
3331
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003332#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003333 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 { \
3335 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003336 .num_blocks = slab_num_blocks, \
3337 .block_size = slab_block_size, \
3338 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339 .free_list = NULL, \
3340 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003341 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342 }
3343
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003344#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3345
3346
Peter Mitsis578f9112016-10-07 13:50:31 -04003347/**
Allan Stephensc98da842016-11-11 15:45:03 -05003348 * INTERNAL_HIDDEN @endcond
3349 */
3350
3351/**
3352 * @defgroup mem_slab_apis Memory Slab APIs
3353 * @ingroup kernel_apis
3354 * @{
3355 */
3356
3357/**
Allan Stephensda827222016-11-09 14:23:58 -06003358 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003359 *
Allan Stephensda827222016-11-09 14:23:58 -06003360 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003362 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3363 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003364 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003365 *
Allan Stephensda827222016-11-09 14:23:58 -06003366 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003368 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003369 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @param name Name of the memory slab.
3372 * @param slab_block_size Size of each memory block (in bytes).
3373 * @param slab_num_blocks Number memory blocks.
3374 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003375 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003376#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3377 char __noinit __aligned(slab_align) \
3378 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3379 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003380 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003381 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003382 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003383
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003384/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003385 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003388 *
Allan Stephensda827222016-11-09 14:23:58 -06003389 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3390 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3391 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3392 * To ensure that each memory block is similarly aligned to this boundary,
3393 * @a slab_block_size must also be a multiple of N.
3394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @param slab Address of the memory slab.
3396 * @param buffer Pointer to buffer used for the memory blocks.
3397 * @param block_size Size of each memory block (in bytes).
3398 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003399 *
3400 * @return N/A
3401 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003402extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003403 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003404
3405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * @param slab Address of the memory slab.
3411 * @param mem Pointer to block address area.
3412 * @param timeout Maximum time to wait for operation to complete
3413 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3414 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003415 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003416 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003418 * @retval -ENOMEM Returned without waiting.
3419 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003420 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003421extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003422 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003423
3424/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * This routine releases a previously allocated memory block back to its
3428 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @param slab Address of the memory slab.
3431 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003432 *
3433 * @return N/A
3434 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003435extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * This routine gets the number of memory blocks that are currently
3441 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003443 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003445 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446 */
Kumar Galacc334c72017-04-21 10:55:34 -05003447static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003449 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450}
3451
Peter Mitsisc001aa82016-10-13 13:53:37 -04003452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * This routine gets the number of memory blocks that are currently
3456 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003461 */
Kumar Galacc334c72017-04-21 10:55:34 -05003462static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003463{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003464 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003465}
3466
Allan Stephensc98da842016-11-11 15:45:03 -05003467/**
3468 * @} end defgroup mem_slab_apis
3469 */
3470
3471/**
3472 * @cond INTERNAL_HIDDEN
3473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474
Andy Ross73cb9582017-05-09 10:42:39 -07003475struct k_mem_pool_lvl {
3476 union {
3477 u32_t *bits_p;
3478 u32_t bits;
3479 };
3480 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003481};
3482
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003483struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003484 void *buf;
3485 size_t max_sz;
3486 u16_t n_max;
3487 u8_t n_levels;
3488 u8_t max_inline_level;
3489 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003490 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491};
3492
Andy Ross73cb9582017-05-09 10:42:39 -07003493#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003494
Andy Ross73cb9582017-05-09 10:42:39 -07003495#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3496
3497#define _MPOOL_LVLS(maxsz, minsz) \
3498 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3499 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3500 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3501 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3502 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3503 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3504 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3505 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3506 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3507 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3508 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3509 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3510 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3511 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3512 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3513 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3514
3515/* Rounds the needed bits up to integer multiples of u32_t */
3516#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3517 ((((n_max) << (2*(l))) + 31) / 32)
3518
3519/* One word gets stored free unioned with the pointer, otherwise the
3520 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003521 */
Andy Ross73cb9582017-05-09 10:42:39 -07003522#define _MPOOL_LBIT_WORDS(n_max, l) \
3523 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3524 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003525
Andy Ross73cb9582017-05-09 10:42:39 -07003526/* How many bytes for the bitfields of a single level? */
3527#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3528 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3529 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003530
Andy Ross73cb9582017-05-09 10:42:39 -07003531/* Size of the bitmap array that follows the buffer in allocated memory */
3532#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3533 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3534 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3535 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3536 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3537 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3538 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3539 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3540 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3541 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3542 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3543 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3544 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3545 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3546 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3547 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3548 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003549
3550/**
Allan Stephensc98da842016-11-11 15:45:03 -05003551 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003552 */
3553
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003554/**
Allan Stephensc98da842016-11-11 15:45:03 -05003555 * @addtogroup mem_pool_apis
3556 * @{
3557 */
3558
3559/**
3560 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3563 * long. The memory pool allows blocks to be repeatedly partitioned into
3564 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003565 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003566 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003567 * If the pool is to be accessed outside the module where it is defined, it
3568 * can be declared via
3569 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003570 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003573 * @param minsz Size of the smallest blocks in the pool (in bytes).
3574 * @param maxsz Size of the largest blocks in the pool (in bytes).
3575 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003576 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003577 */
Andy Ross73cb9582017-05-09 10:42:39 -07003578#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3579 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3580 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3581 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3582 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3583 .buf = _mpool_buf_##name, \
3584 .max_sz = maxsz, \
3585 .n_max = nmax, \
3586 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3587 .levels = _mpool_lvls_##name, \
3588 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003589
Peter Mitsis937042c2016-10-13 13:18:26 -04003590/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003593 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * @param pool Address of the memory pool.
3596 * @param block Pointer to block descriptor for the allocated memory.
3597 * @param size Amount of memory to allocate (in bytes).
3598 * @param timeout Maximum time to wait for operation to complete
3599 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3600 * or K_FOREVER to wait as long as necessary.
3601 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003602 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003604 * @retval -ENOMEM Returned without waiting.
3605 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003606 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003607extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003608 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003609
3610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003611 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * This routine releases a previously allocated memory block back to its
3614 * memory pool.
3615 *
3616 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003617 *
3618 * @return N/A
3619 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003620extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003621
3622/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003624 *
Andy Ross73cb9582017-05-09 10:42:39 -07003625 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 *
Andy Ross73cb9582017-05-09 10:42:39 -07003627 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003628 *
3629 * @return N/A
3630 */
Andy Ross73cb9582017-05-09 10:42:39 -07003631static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003632
3633/**
Allan Stephensc98da842016-11-11 15:45:03 -05003634 * @} end addtogroup mem_pool_apis
3635 */
3636
3637/**
3638 * @defgroup heap_apis Heap Memory Pool APIs
3639 * @ingroup kernel_apis
3640 * @{
3641 */
3642
3643/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003646 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003647 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003652 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003653extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003654
3655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003657 *
3658 * This routine provides traditional free() semantics. The memory being
3659 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003660 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003661 * If @a ptr is NULL, no operation is performed.
3662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003663 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003664 *
3665 * @return N/A
3666 */
3667extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003668
Allan Stephensc98da842016-11-11 15:45:03 -05003669/**
3670 * @} end defgroup heap_apis
3671 */
3672
Benjamin Walshacc68c12017-01-29 18:57:45 -05003673/* polling API - PRIVATE */
3674
Benjamin Walshb0179862017-02-02 16:39:57 -05003675#ifdef CONFIG_POLL
3676#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3677#else
3678#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3679#endif
3680
Benjamin Walshacc68c12017-01-29 18:57:45 -05003681/* private - implementation data created as needed, per-type */
3682struct _poller {
3683 struct k_thread *thread;
3684};
3685
3686/* private - types bit positions */
3687enum _poll_types_bits {
3688 /* can be used to ignore an event */
3689 _POLL_TYPE_IGNORE,
3690
3691 /* to be signaled by k_poll_signal() */
3692 _POLL_TYPE_SIGNAL,
3693
3694 /* semaphore availability */
3695 _POLL_TYPE_SEM_AVAILABLE,
3696
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003697 /* queue/fifo/lifo data availability */
3698 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003699
3700 _POLL_NUM_TYPES
3701};
3702
3703#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3704
3705/* private - states bit positions */
3706enum _poll_states_bits {
3707 /* default state when creating event */
3708 _POLL_STATE_NOT_READY,
3709
Benjamin Walshacc68c12017-01-29 18:57:45 -05003710 /* signaled by k_poll_signal() */
3711 _POLL_STATE_SIGNALED,
3712
3713 /* semaphore is available */
3714 _POLL_STATE_SEM_AVAILABLE,
3715
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003716 /* data is available to read on queue/fifo/lifo */
3717 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003718
3719 _POLL_NUM_STATES
3720};
3721
3722#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3723
3724#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003725 (32 - (0 \
3726 + 8 /* tag */ \
3727 + _POLL_NUM_TYPES \
3728 + _POLL_NUM_STATES \
3729 + 1 /* modes */ \
3730 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003731
3732#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3733#error overflow of 32-bit word in struct k_poll_event
3734#endif
3735
3736/* end of polling API - PRIVATE */
3737
3738
3739/**
3740 * @defgroup poll_apis Async polling APIs
3741 * @ingroup kernel_apis
3742 * @{
3743 */
3744
3745/* Public polling API */
3746
3747/* public - values for k_poll_event.type bitfield */
3748#define K_POLL_TYPE_IGNORE 0
3749#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3750#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003751#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3752#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003753
3754/* public - polling modes */
3755enum k_poll_modes {
3756 /* polling thread does not take ownership of objects when available */
3757 K_POLL_MODE_NOTIFY_ONLY = 0,
3758
3759 K_POLL_NUM_MODES
3760};
3761
3762/* public - values for k_poll_event.state bitfield */
3763#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003764#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3765#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003766#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3767#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768
3769/* public - poll signal object */
3770struct k_poll_signal {
3771 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003772 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773
3774 /*
3775 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3776 * user resets it to 0.
3777 */
3778 unsigned int signaled;
3779
3780 /* custom result value passed to k_poll_signal() if needed */
3781 int result;
3782};
3783
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003784#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003785 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003786 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787 .signaled = 0, \
3788 .result = 0, \
3789 }
3790
3791struct k_poll_event {
3792 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003793 sys_dnode_t _node;
3794
3795 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 struct _poller *poller;
3797
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003798 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003799 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003800
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003802 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003803
3804 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003805 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003806
3807 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003808 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003809
3810 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003811 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003812
3813 /* per-type data */
3814 union {
3815 void *obj;
3816 struct k_poll_signal *signal;
3817 struct k_sem *sem;
3818 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003819 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820 };
3821};
3822
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003823#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003824 { \
3825 .poller = NULL, \
3826 .type = event_type, \
3827 .state = K_POLL_STATE_NOT_READY, \
3828 .mode = event_mode, \
3829 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003830 { .obj = event_obj }, \
3831 }
3832
3833#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3834 event_tag) \
3835 { \
3836 .type = event_type, \
3837 .tag = event_tag, \
3838 .state = K_POLL_STATE_NOT_READY, \
3839 .mode = event_mode, \
3840 .unused = 0, \
3841 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003842 }
3843
3844/**
3845 * @brief Initialize one struct k_poll_event instance
3846 *
3847 * After this routine is called on a poll event, the event it ready to be
3848 * placed in an event array to be passed to k_poll().
3849 *
3850 * @param event The event to initialize.
3851 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3852 * values. Only values that apply to the same object being polled
3853 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3854 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003855 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003856 * @param obj Kernel object or poll signal.
3857 *
3858 * @return N/A
3859 */
3860
Kumar Galacc334c72017-04-21 10:55:34 -05003861extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003862 int mode, void *obj);
3863
3864/**
3865 * @brief Wait for one or many of multiple poll events to occur
3866 *
3867 * This routine allows a thread to wait concurrently for one or many of
3868 * multiple poll events to have occurred. Such events can be a kernel object
3869 * being available, like a semaphore, or a poll signal event.
3870 *
3871 * When an event notifies that a kernel object is available, the kernel object
3872 * is not "given" to the thread calling k_poll(): it merely signals the fact
3873 * that the object was available when the k_poll() call was in effect. Also,
3874 * all threads trying to acquire an object the regular way, i.e. by pending on
3875 * the object, have precedence over the thread polling on the object. This
3876 * means that the polling thread will never get the poll event on an object
3877 * until the object becomes available and its pend queue is empty. For this
3878 * reason, the k_poll() call is more effective when the objects being polled
3879 * only have one thread, the polling thread, trying to acquire them.
3880 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003881 * When k_poll() returns 0, the caller should loop on all the events that were
3882 * passed to k_poll() and check the state field for the values that were
3883 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003884 *
3885 * Before being reused for another call to k_poll(), the user has to reset the
3886 * state field to K_POLL_STATE_NOT_READY.
3887 *
3888 * @param events An array of pointers to events to be polled for.
3889 * @param num_events The number of events in the array.
3890 * @param timeout Waiting period for an event to be ready (in milliseconds),
3891 * or one of the special values K_NO_WAIT and K_FOREVER.
3892 *
3893 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894 * @retval -EAGAIN Waiting period timed out.
3895 */
3896
3897extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003898 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003899
3900/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003901 * @brief Initialize a poll signal object.
3902 *
3903 * Ready a poll signal object to be signaled via k_poll_signal().
3904 *
3905 * @param signal A poll signal.
3906 *
3907 * @return N/A
3908 */
3909
3910extern void k_poll_signal_init(struct k_poll_signal *signal);
3911
3912/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003913 * @brief Signal a poll signal object.
3914 *
3915 * This routine makes ready a poll signal, which is basically a poll event of
3916 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3917 * made ready to run. A @a result value can be specified.
3918 *
3919 * The poll signal contains a 'signaled' field that, when set by
3920 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3921 * be reset by the user before being passed again to k_poll() or k_poll() will
3922 * consider it being signaled, and will return immediately.
3923 *
3924 * @param signal A poll signal.
3925 * @param result The value to store in the result field of the signal.
3926 *
3927 * @retval 0 The signal was delivered successfully.
3928 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3929 */
3930
3931extern int k_poll_signal(struct k_poll_signal *signal, int result);
3932
3933/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003934extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003935
3936/**
3937 * @} end defgroup poll_apis
3938 */
3939
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003940/**
3941 * @brief Make the CPU idle.
3942 *
3943 * This function makes the CPU idle until an event wakes it up.
3944 *
3945 * In a regular system, the idle thread should be the only thread responsible
3946 * for making the CPU idle and triggering any type of power management.
3947 * However, in some more constrained systems, such as a single-threaded system,
3948 * the only thread would be responsible for this if needed.
3949 *
3950 * @return N/A
3951 */
3952extern void k_cpu_idle(void);
3953
3954/**
3955 * @brief Make the CPU idle in an atomic fashion.
3956 *
3957 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3958 * must be done atomically before making the CPU idle.
3959 *
3960 * @param key Interrupt locking key obtained from irq_lock().
3961 *
3962 * @return N/A
3963 */
3964extern void k_cpu_atomic_idle(unsigned int key);
3965
Kumar Galacc334c72017-04-21 10:55:34 -05003966extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003967
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003968#include <arch/cpu.h>
3969
Andrew Boiecdb94d62017-04-18 15:22:05 -07003970#ifdef _ARCH_EXCEPT
3971/* This archtecture has direct support for triggering a CPU exception */
3972#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3973#else
3974
3975#include <misc/printk.h>
3976
3977/* NOTE: This is the implementation for arches that do not implement
3978 * _ARCH_EXCEPT() to generate a real CPU exception.
3979 *
3980 * We won't have a real exception frame to determine the PC value when
3981 * the oops occurred, so print file and line number before we jump into
3982 * the fatal error handler.
3983 */
3984#define _k_except_reason(reason) do { \
3985 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3986 _NanoFatalErrorHandler(reason, &_default_esf); \
3987 CODE_UNREACHABLE; \
3988 } while (0)
3989
3990#endif /* _ARCH__EXCEPT */
3991
3992/**
3993 * @brief Fatally terminate a thread
3994 *
3995 * This should be called when a thread has encountered an unrecoverable
3996 * runtime condition and needs to terminate. What this ultimately
3997 * means is determined by the _fatal_error_handler() implementation, which
3998 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3999 *
4000 * If this is called from ISR context, the default system fatal error handler
4001 * will treat it as an unrecoverable system error, just like k_panic().
4002 */
4003#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4004
4005/**
4006 * @brief Fatally terminate the system
4007 *
4008 * This should be called when the Zephyr kernel has encountered an
4009 * unrecoverable runtime condition and needs to terminate. What this ultimately
4010 * means is determined by the _fatal_error_handler() implementation, which
4011 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4012 */
4013#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4014
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004015/*
4016 * private APIs that are utilized by one or more public APIs
4017 */
4018
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004019#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004020extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004021#else
4022#define _init_static_threads() do { } while ((0))
4023#endif
4024
4025extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004026extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004027
Andrew Boiedc5d9352017-06-02 12:56:47 -07004028/* arch/cpu.h may declare an architecture or platform-specific macro
4029 * for properly declaring stacks, compatible with MMU/MPU constraints if
4030 * enabled
4031 */
4032#ifdef _ARCH_THREAD_STACK_DEFINE
4033#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4034#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4035 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4036#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4037#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004038static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4039{
4040 return _ARCH_THREAD_STACK_BUFFER(sym);
4041}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004042#else
4043/**
4044 * @brief Declare a toplevel thread stack memory region
4045 *
4046 * This declares a region of memory suitable for use as a thread's stack.
4047 *
4048 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4049 * 'noinit' section so that it isn't zeroed at boot
4050 *
Andrew Boie507852a2017-07-25 18:47:07 -07004051 * The declared symbol will always be a k_thread_stack_t which can be passed to
4052 * k_thread_create, but should otherwise not be manipulated. If the buffer
4053 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004054 *
4055 * It is legal to precede this definition with the 'static' keyword.
4056 *
4057 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4058 * parameter of k_thread_create(), it may not be the same as the
4059 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4060 *
4061 * @param sym Thread stack symbol name
4062 * @param size Size of the stack memory region
4063 */
4064#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004065 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004066
4067/**
4068 * @brief Declare a toplevel array of thread stack memory regions
4069 *
4070 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4071 * definition for additional details and constraints.
4072 *
4073 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4074 * 'noinit' section so that it isn't zeroed at boot
4075 *
4076 * @param sym Thread stack symbol name
4077 * @param nmemb Number of stacks to declare
4078 * @param size Size of the stack memory region
4079 */
4080
4081#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004082 struct _k_thread_stack_element __noinit \
4083 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004084
4085/**
4086 * @brief Declare an embedded stack memory region
4087 *
4088 * Used for stacks embedded within other data structures. Use is highly
4089 * discouraged but in some cases necessary. For memory protection scenarios,
4090 * it is very important that any RAM preceding this member not be writable
4091 * by threads else a stack overflow will lead to silent corruption. In other
4092 * words, the containing data structure should live in RAM owned by the kernel.
4093 *
4094 * @param sym Thread stack symbol name
4095 * @param size Size of the stack memory region
4096 */
4097#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004098 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004099
4100/**
4101 * @brief Return the size in bytes of a stack memory region
4102 *
4103 * Convenience macro for passing the desired stack size to k_thread_create()
4104 * since the underlying implementation may actually create something larger
4105 * (for instance a guard area).
4106 *
4107 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004108 * passed to K_THREAD_STACK_DEFINE.
4109 *
4110 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4111 * it is not guaranteed to return the original value since each array
4112 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004113 *
4114 * @param sym Stack memory symbol
4115 * @return Size of the stack
4116 */
4117#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4118
4119/**
4120 * @brief Get a pointer to the physical stack buffer
4121 *
4122 * Convenience macro to get at the real underlying stack buffer used by
4123 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4124 * This is really only intended for diagnostic tools which want to examine
4125 * stack memory contents.
4126 *
4127 * @param sym Declared stack symbol name
4128 * @return The buffer itself, a char *
4129 */
Andrew Boie507852a2017-07-25 18:47:07 -07004130static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4131{
4132 return (char *)sym;
4133}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004134
4135#endif /* _ARCH_DECLARE_STACK */
4136
Chunlin Hane9c97022017-07-07 20:29:30 +08004137/**
4138 * @defgroup mem_domain_apis Memory domain APIs
4139 * @ingroup kernel_apis
4140 * @{
4141 */
4142
4143/** @def MEM_PARTITION_ENTRY
4144 * @brief Used to declare a memory partition entry
4145 */
4146#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4147 {\
4148 .start = _start, \
4149 .size = _size, \
4150 .attr = _attr, \
4151 }
4152
4153/** @def K_MEM_PARTITION_DEFINE
4154 * @brief Used to declare a memory partition
4155 */
4156#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4157#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4158 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4159 struct k_mem_partition name = \
4160 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4161#else
4162#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4163 struct k_mem_partition name = \
4164 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4165#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4166
4167
4168/* memory partition */
4169struct k_mem_partition {
4170 /* start address of memory partition */
4171 u32_t start;
4172 /* size of memory partition */
4173 u32_t size;
4174 /* attribute of memory partition */
4175 u32_t attr;
4176};
4177
4178#if defined(CONFIG_USERSPACE)
4179/* memory domian */
4180struct k_mem_domain {
4181 /* number of partitions in the domain */
4182 u32_t num_partitions;
4183 /* partitions in the domain */
4184 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4185 /* domain q */
4186 sys_dlist_t mem_domain_q;
4187};
4188#endif /* CONFIG_USERSPACE */
4189
4190/**
4191 * @brief Initialize a memory domain.
4192 *
4193 * Initialize a memory domain with given name and memory partitions.
4194 *
4195 * @param domain The memory domain to be initialized.
4196 * @param num_parts The number of array items of "parts" parameter.
4197 * @param parts An array of pointers to the memory partitions. Can be NULL
4198 * if num_parts is zero.
4199 */
4200
4201extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4202 struct k_mem_partition *parts[]);
4203/**
4204 * @brief Destroy a memory domain.
4205 *
4206 * Destroy a memory domain.
4207 *
4208 * @param domain The memory domain to be destroyed.
4209 */
4210
4211extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4212
4213/**
4214 * @brief Add a memory partition into a memory domain.
4215 *
4216 * Add a memory partition into a memory domain.
4217 *
4218 * @param domain The memory domain to be added a memory partition.
4219 * @param part The memory partition to be added
4220 */
4221
4222extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4223 struct k_mem_partition *part);
4224
4225/**
4226 * @brief Remove a memory partition from a memory domain.
4227 *
4228 * Remove a memory partition from a memory domain.
4229 *
4230 * @param domain The memory domain to be removed a memory partition.
4231 * @param part The memory partition to be removed
4232 */
4233
4234extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4235 struct k_mem_partition *part);
4236
4237/**
4238 * @brief Add a thread into a memory domain.
4239 *
4240 * Add a thread into a memory domain.
4241 *
4242 * @param domain The memory domain that the thread is going to be added into.
4243 * @param thread ID of thread going to be added into the memory domain.
4244 */
4245
4246extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4247 k_tid_t thread);
4248
4249/**
4250 * @brief Remove a thread from its memory domain.
4251 *
4252 * Remove a thread from its memory domain.
4253 *
4254 * @param thread ID of thread going to be removed from its memory domain.
4255 */
4256
4257extern void k_mem_domain_remove_thread(k_tid_t thread);
4258
4259/**
4260 * @} end defgroup mem_domain_apis
4261 */
4262
Andrew Boie756f9072017-10-10 16:01:49 -07004263/**
4264 * @brief Emit a character buffer to the console device
4265 *
4266 * @param c String of characters to print
4267 * @param n The length of the string
4268 */
4269__syscall void k_str_out(char *c, size_t n);
4270
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004271#ifdef __cplusplus
4272}
4273#endif
4274
Andrew Boiee004dec2016-11-07 09:01:19 -08004275#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4276/*
4277 * Define new and delete operators.
4278 * At this moment, the operators do nothing since objects are supposed
4279 * to be statically allocated.
4280 */
4281inline void operator delete(void *ptr)
4282{
4283 (void)ptr;
4284}
4285
4286inline void operator delete[](void *ptr)
4287{
4288 (void)ptr;
4289}
4290
4291inline void *operator new(size_t size)
4292{
4293 (void)size;
4294 return NULL;
4295}
4296
4297inline void *operator new[](size_t size)
4298{
4299 (void)size;
4300 return NULL;
4301}
4302
4303/* Placement versions of operator new and delete */
4304inline void operator delete(void *ptr1, void *ptr2)
4305{
4306 (void)ptr1;
4307 (void)ptr2;
4308}
4309
4310inline void operator delete[](void *ptr1, void *ptr2)
4311{
4312 (void)ptr1;
4313 (void)ptr2;
4314}
4315
4316inline void *operator new(size_t size, void *ptr)
4317{
4318 (void)size;
4319 return ptr;
4320}
4321
4322inline void *operator new[](size_t size, void *ptr)
4323{
4324 (void)size;
4325 return ptr;
4326}
4327
4328#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4329
Andrew Boiefa94ee72017-09-28 16:54:35 -07004330#include <syscalls/kernel.h>
4331
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004332#endif /* !_ASMLANGUAGE */
4333
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004334#endif /* _kernel__h_ */