blob: c68f86cdc5b81da57989e3d661fbb7e5269e123e [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Anas Nashifbbb157d2017-01-15 08:46:31 -050037/**
38 * @brief Kernel APIs
39 * @defgroup kernel_apis Kernel APIs
40 * @{
41 * @}
42 */
43
Anas Nashif61f4b242016-11-18 10:53:59 -050044#ifdef CONFIG_KERNEL_DEBUG
45#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040046#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
47#else
48#define K_DEBUG(fmt, ...)
49#endif
50
Benjamin Walsh2f280412017-01-14 19:23:46 -050051#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
53#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
54#elif defined(CONFIG_COOP_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
56#define _NUM_PREEMPT_PRIO (0)
57#elif defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (0)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#else
61#error "invalid configuration"
62#endif
63
64#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_PRIO_PREEMPT(x) (x)
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_ANY NULL
68#define K_END NULL
69
Benjamin Walshedb35702017-01-14 18:47:22 -050070#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050072#elif defined(CONFIG_COOP_ENABLED)
73#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
74#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050076#else
77#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050080#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
82#else
83#define K_LOWEST_THREAD_PRIO -1
84#endif
85
Benjamin Walshfab8d922016-11-08 15:36:36 -050086#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
87
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
89#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
90
91typedef sys_dlist_t _wait_q_t;
92
Anas Nashif2f203c22016-12-18 06:57:45 -050093#ifdef CONFIG_OBJECT_TRACING
94#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
95#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#else
Anas Nashif2f203c22016-12-18 06:57:45 -050097#define _OBJECT_TRACING_INIT
98#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#endif
100
Benjamin Walshacc68c12017-01-29 18:57:45 -0500101#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300102#define _POLL_EVENT_OBJ_INIT(obj) \
103 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
104#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#define _POLL_EVENT
108#endif
109
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800126struct k_mem_domain;
127struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128
Andrew Boie5bd891d2017-09-27 12:59:28 -0700129/* This enumeration needs to be kept in sync with the lists of kernel objects
130 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
131 * function in kernel/userspace.c
132 */
Andrew Boie945af952017-08-22 13:15:23 -0700133enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700134 K_OBJ_ANY,
135
Andrew Boie5bd891d2017-09-27 12:59:28 -0700136 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700137 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700138 K_OBJ_MSGQ,
139 K_OBJ_MUTEX,
140 K_OBJ_PIPE,
141 K_OBJ_SEM,
142 K_OBJ_STACK,
143 K_OBJ_THREAD,
144 K_OBJ_TIMER,
Andrew Boiebca15da2017-10-15 14:17:48 -0700145 K_OBJ__THREAD_STACK_ELEMENT,
Andrew Boie945af952017-08-22 13:15:23 -0700146
Andrew Boie5bd891d2017-09-27 12:59:28 -0700147 /* Driver subsystems */
148 K_OBJ_DRIVER_ADC,
149 K_OBJ_DRIVER_AIO_CMP,
150 K_OBJ_DRIVER_CLOCK_CONTROL,
151 K_OBJ_DRIVER_COUNTER,
152 K_OBJ_DRIVER_CRYPTO,
153 K_OBJ_DRIVER_DMA,
154 K_OBJ_DRIVER_ETH,
155 K_OBJ_DRIVER_FLASH,
156 K_OBJ_DRIVER_GPIO,
157 K_OBJ_DRIVER_I2C,
158 K_OBJ_DRIVER_I2S,
159 K_OBJ_DRIVER_IPM,
160 K_OBJ_DRIVER_PINMUX,
161 K_OBJ_DRIVER_PWM,
162 K_OBJ_DRIVER_RANDOM,
163 K_OBJ_DRIVER_RTC,
164 K_OBJ_DRIVER_SENSOR,
165 K_OBJ_DRIVER_SHARED_IRQ,
166 K_OBJ_DRIVER_SPI,
167 K_OBJ_DRIVER_UART,
168 K_OBJ_DRIVER_WDT,
169
Andrew Boie945af952017-08-22 13:15:23 -0700170 K_OBJ_LAST
171};
172
173#ifdef CONFIG_USERSPACE
174/* Table generated by gperf, these objects are retrieved via
175 * _k_object_find() */
176struct _k_object {
177 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700178 u8_t perms[CONFIG_MAX_THREAD_BYTES];
179 u8_t type;
180 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700181 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700182} __packed;
183
184#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700185#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700186
187/**
188 * Lookup a kernel object and init its metadata if it exists
189 *
190 * Calling this on an object will make it usable from userspace.
191 * Intended to be called as the last statement in kernel object init
192 * functions.
193 *
194 * @param object Address of the kernel object
195 */
196void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700197#else
Andrew Boie743e4682017-10-04 12:25:50 -0700198static inline void _k_object_init(void *obj)
199{
200 ARG_UNUSED(obj);
201}
202
203static inline void _impl_k_object_access_grant(void *object,
204 struct k_thread *thread)
205{
206 ARG_UNUSED(object);
207 ARG_UNUSED(thread);
208}
209
Andrew Boiea89bf012017-10-09 14:47:55 -0700210static inline void _impl_k_object_access_revoke(void *object,
211 struct k_thread *thread)
212{
213 ARG_UNUSED(object);
214 ARG_UNUSED(thread);
215}
216
Andrew Boie41bab6e2017-10-14 14:42:23 -0700217static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700218{
219 ARG_UNUSED(object);
220}
221#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700222
223/**
224 * grant a thread access to a kernel object
225 *
226 * The thread will be granted access to the object if the caller is from
227 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700228 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700229 *
230 * @param object Address of kernel object
231 * @param thread Thread to grant access to the object
232 */
Andrew Boie743e4682017-10-04 12:25:50 -0700233__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700234
Andrew Boiea89bf012017-10-09 14:47:55 -0700235/**
236 * grant a thread access to a kernel object
237 *
238 * The thread will lose access to the object if the caller is from
239 * supervisor mode, or the caller is from user mode AND has permissions
240 * on both the object and the thread whose access is being revoked.
241 *
242 * @param object Address of kernel object
243 * @param thread Thread to remove access to the object
244 */
245__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700246
247/**
248 * grant all present and future threads access to an object
249 *
250 * If the caller is from supervisor mode, or the caller is from user mode and
251 * have sufficient permissions on the object, then that object will have
252 * permissions granted to it for *all* current and future threads running in
253 * the system, effectively becoming a public kernel object.
254 *
255 * Use of this API should be avoided on systems that are running untrusted code
256 * as it is possible for such code to derive the addresses of kernel objects
257 * and perform unwanted operations on them.
258 *
Andrew Boie04caa672017-10-13 13:57:07 -0700259 * It is not possible to revoke permissions on public objects; once public,
260 * any thread may use it.
261 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700262 * @param object Address of kernel object
263 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700264void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700265
Andrew Boiebca15da2017-10-15 14:17:48 -0700266/* Using typedef deliberately here, this is quite intended to be an opaque
267 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
268 *
269 * The purpose of this data type is to clearly distinguish between the
270 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
271 * buffer which composes the stack data actually used by the underlying
272 * thread; they cannot be used interchangably as some arches precede the
273 * stack buffer region with guard areas that trigger a MPU or MMU fault
274 * if written to.
275 *
276 * APIs that want to work with the buffer inside should continue to use
277 * char *.
278 *
279 * Stacks should always be created with K_THREAD_STACK_DEFINE().
280 */
281struct __packed _k_thread_stack_element {
282 char data;
283};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700284typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700285
Andrew Boie73abd322017-04-04 13:19:13 -0700286/* timeouts */
287
288struct _timeout;
289typedef void (*_timeout_func_t)(struct _timeout *t);
290
291struct _timeout {
292 sys_dnode_t node;
293 struct k_thread *thread;
294 sys_dlist_t *wait_q;
295 s32_t delta_ticks_from_prev;
296 _timeout_func_t func;
297};
298
299extern s32_t _timeout_remaining_get(struct _timeout *timeout);
300
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700301/**
302 * @typedef k_thread_entry_t
303 * @brief Thread entry point function type.
304 *
305 * A thread's entry point function is invoked when the thread starts executing.
306 * Up to 3 argument values can be passed to the function.
307 *
308 * The thread terminates execution permanently if the entry point function
309 * returns. The thread is responsible for releasing any shared resources
310 * it may own (such as mutexes and dynamically allocated memory), prior to
311 * returning.
312 *
313 * @param p1 First argument.
314 * @param p2 Second argument.
315 * @param p3 Third argument.
316 *
317 * @return N/A
318 */
319typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700320
321#ifdef CONFIG_THREAD_MONITOR
322struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700323 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700324 void *parameter1;
325 void *parameter2;
326 void *parameter3;
327};
328#endif
329
330/* can be used for creating 'dummy' threads, e.g. for pending on objects */
331struct _thread_base {
332
333 /* this thread's entry in a ready/wait queue */
334 sys_dnode_t k_q_node;
335
336 /* user facing 'thread options'; values defined in include/kernel.h */
337 u8_t user_options;
338
339 /* thread state */
340 u8_t thread_state;
341
342 /*
343 * scheduler lock count and thread priority
344 *
345 * These two fields control the preemptibility of a thread.
346 *
347 * When the scheduler is locked, sched_locked is decremented, which
348 * means that the scheduler is locked for values from 0xff to 0x01. A
349 * thread is coop if its prio is negative, thus 0x80 to 0xff when
350 * looked at the value as unsigned.
351 *
352 * By putting them end-to-end, this means that a thread is
353 * non-preemptible if the bundled value is greater than or equal to
354 * 0x0080.
355 */
356 union {
357 struct {
358#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
359 u8_t sched_locked;
360 s8_t prio;
361#else /* LITTLE and PDP */
362 s8_t prio;
363 u8_t sched_locked;
364#endif
365 };
366 u16_t preempt;
367 };
368
369 /* data returned by APIs */
370 void *swap_data;
371
372#ifdef CONFIG_SYS_CLOCK_EXISTS
373 /* this thread's entry in a timeout queue */
374 struct _timeout timeout;
375#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700376
377#ifdef CONFIG_USERSPACE
378 /* Bit position in kernel object permissions bitfield for this thread */
379 unsigned int perm_index;
380#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700381};
382
383typedef struct _thread_base _thread_base_t;
384
385#if defined(CONFIG_THREAD_STACK_INFO)
386/* Contains the stack information of a thread */
387struct _thread_stack_info {
388 /* Stack Start */
389 u32_t start;
390 /* Stack Size */
391 u32_t size;
392};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700393
394typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700395#endif /* CONFIG_THREAD_STACK_INFO */
396
Chunlin Hane9c97022017-07-07 20:29:30 +0800397#if defined(CONFIG_USERSPACE)
398struct _mem_domain_info {
399 /* memory domain queue node */
400 sys_dnode_t mem_domain_q_node;
401 /* memory domain of the thread */
402 struct k_mem_domain *mem_domain;
403};
404
405#endif /* CONFIG_USERSPACE */
406
Andrew Boie73abd322017-04-04 13:19:13 -0700407struct k_thread {
408
409 struct _thread_base base;
410
411 /* defined by the architecture, but all archs need these */
412 struct _caller_saved caller_saved;
413 struct _callee_saved callee_saved;
414
415 /* static thread init data */
416 void *init_data;
417
418 /* abort function */
419 void (*fn_abort)(void);
420
421#if defined(CONFIG_THREAD_MONITOR)
422 /* thread entry and parameters description */
423 struct __thread_entry *entry;
424
425 /* next item in list of all threads */
426 struct k_thread *next_thread;
427#endif
428
429#ifdef CONFIG_THREAD_CUSTOM_DATA
430 /* crude thread-local storage */
431 void *custom_data;
432#endif
433
434#ifdef CONFIG_ERRNO
435 /* per-thread errno variable */
436 int errno_var;
437#endif
438
439#if defined(CONFIG_THREAD_STACK_INFO)
440 /* Stack Info */
441 struct _thread_stack_info stack_info;
442#endif /* CONFIG_THREAD_STACK_INFO */
443
Chunlin Hane9c97022017-07-07 20:29:30 +0800444#if defined(CONFIG_USERSPACE)
445 /* memory domain info of the thread */
446 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700447 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700448 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800449#endif /* CONFIG_USERSPACE */
450
Andrew Boie73abd322017-04-04 13:19:13 -0700451 /* arch-specifics: must always be at the end */
452 struct _thread_arch arch;
453};
454
455typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400456typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700457#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400458
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400459enum execution_context_types {
460 K_ISR = 0,
461 K_COOP_THREAD,
462 K_PREEMPT_THREAD,
463};
464
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100466 * @defgroup profiling_apis Profiling APIs
467 * @ingroup kernel_apis
468 * @{
469 */
470
471/**
472 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
473 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700474 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100475 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
476 *
477 * CONFIG_MAIN_STACK_SIZE
478 * CONFIG_IDLE_STACK_SIZE
479 * CONFIG_ISR_STACK_SIZE
480 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
481 *
482 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
483 * produce output.
484 *
485 * @return N/A
486 */
487extern void k_call_stacks_analyze(void);
488
489/**
490 * @} end defgroup profiling_apis
491 */
492
493/**
Allan Stephensc98da842016-11-11 15:45:03 -0500494 * @defgroup thread_apis Thread APIs
495 * @ingroup kernel_apis
496 * @{
497 */
498
Benjamin Walshed240f22017-01-22 13:05:08 -0500499#endif /* !_ASMLANGUAGE */
500
501
502/*
503 * Thread user options. May be needed by assembly code. Common part uses low
504 * bits, arch-specific use high bits.
505 */
506
507/* system thread that must not abort */
508#define K_ESSENTIAL (1 << 0)
509
510#if defined(CONFIG_FP_SHARING)
511/* thread uses floating point registers */
512#define K_FP_REGS (1 << 1)
513#endif
514
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700515/* This thread has dropped from supervisor mode to user mode and consequently
516 * has additional restrictions
517 */
518#define K_USER (1 << 2)
519
Andrew Boie47f8fd12017-10-05 11:11:02 -0700520/* Indicates that the thread being created should inherit all kernel object
521 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
522 * is not enabled.
523 */
524#define K_INHERIT_PERMS (1 << 3)
525
Benjamin Walshed240f22017-01-22 13:05:08 -0500526#ifdef CONFIG_X86
527/* x86 Bitmask definitions for threads user options */
528
529#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
530/* thread uses SSEx (and also FP) registers */
531#define K_SSE_REGS (1 << 7)
532#endif
533#endif
534
535/* end - thread options */
536
537#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700539 * @brief Create a thread.
540 *
541 * This routine initializes a thread, then schedules it for execution.
542 *
543 * The new thread may be scheduled for immediate execution or a delayed start.
544 * If the newly spawned thread does not have a delayed start the kernel
545 * scheduler may preempt the current thread to allow the new thread to
546 * execute.
547 *
548 * Thread options are architecture-specific, and can include K_ESSENTIAL,
549 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
550 * them using "|" (the logical OR operator).
551 *
552 * Historically, users often would use the beginning of the stack memory region
553 * to store the struct k_thread data, although corruption will occur if the
554 * stack overflows this region and stack protection features may not detect this
555 * situation.
556 *
557 * @param new_thread Pointer to uninitialized struct k_thread
558 * @param stack Pointer to the stack space.
559 * @param stack_size Stack size in bytes.
560 * @param entry Thread entry function.
561 * @param p1 1st entry point parameter.
562 * @param p2 2nd entry point parameter.
563 * @param p3 3rd entry point parameter.
564 * @param prio Thread priority.
565 * @param options Thread options.
566 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
567 *
568 * @return ID of new thread.
569 */
Andrew Boie662c3452017-10-02 10:51:18 -0700570__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700571 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700572 size_t stack_size,
573 k_thread_entry_t entry,
574 void *p1, void *p2, void *p3,
575 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700576
Andrew Boie3f091b52017-08-30 14:34:14 -0700577/**
578 * @brief Drop a thread's privileges permanently to user mode
579 *
580 * @param entry Function to start executing from
581 * @param p1 1st entry point parameter
582 * @param p2 2nd entry point parameter
583 * @param p3 3rd entry point parameter
584 */
585extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
586 void *p1, void *p2,
587 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700588
Andrew Boied26cf2d2017-03-30 13:07:02 -0700589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500590 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591 *
Allan Stephensc98da842016-11-11 15:45:03 -0500592 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596 *
597 * @return N/A
598 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700599__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600
601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 *
604 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 * @return N/A
608 */
Kumar Galacc334c72017-04-21 10:55:34 -0500609extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610
611/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500612 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500614 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500616 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400617 *
618 * @return N/A
619 */
Andrew Boie468190a2017-09-29 14:00:48 -0700620__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400621
622/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500623 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500625 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500627 * If @a thread is not currently sleeping, the routine has no effect.
628 *
629 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 *
631 * @return N/A
632 */
Andrew Boie468190a2017-09-29 14:00:48 -0700633__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634
635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500636 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500638 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700640__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641
642/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500643 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500645 * This routine prevents @a thread from executing if it has not yet started
646 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500648 * @param thread ID of thread to cancel.
649 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700650 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500651 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400652 */
Andrew Boie468190a2017-09-29 14:00:48 -0700653__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400654
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655/**
Allan Stephensc98da842016-11-11 15:45:03 -0500656 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500658 * This routine permanently stops execution of @a thread. The thread is taken
659 * off all kernel queues it is part of (i.e. the ready queue, the timeout
660 * queue, or a kernel object wait queue). However, any kernel resources the
661 * thread might currently own (such as mutexes or memory blocks) are not
662 * released. It is the responsibility of the caller of this routine to ensure
663 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500665 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 *
667 * @return N/A
668 */
Andrew Boie468190a2017-09-29 14:00:48 -0700669__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400670
Andrew Boie7d627c52017-08-30 11:01:56 -0700671
672/**
673 * @brief Start an inactive thread
674 *
675 * If a thread was created with K_FOREVER in the delay parameter, it will
676 * not be added to the scheduling queue until this function is called
677 * on it.
678 *
679 * @param thread thread to start
680 */
Andrew Boie468190a2017-09-29 14:00:48 -0700681__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700682
Allan Stephensc98da842016-11-11 15:45:03 -0500683/**
684 * @cond INTERNAL_HIDDEN
685 */
686
Benjamin Walshd211a522016-12-06 11:44:01 -0500687/* timeout has timed out and is not on _timeout_q anymore */
688#define _EXPIRED (-2)
689
690/* timeout is not in use */
691#define _INACTIVE (-1)
692
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400693struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700694 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700695 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400696 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700697 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500698 void *init_p1;
699 void *init_p2;
700 void *init_p3;
701 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500702 u32_t init_options;
703 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500704 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500705 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400706};
707
Andrew Boied26cf2d2017-03-30 13:07:02 -0700708#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400709 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500710 prio, options, delay, abort, groups) \
711 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700712 .init_thread = (thread), \
713 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500714 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700715 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400716 .init_p1 = (void *)p1, \
717 .init_p2 = (void *)p2, \
718 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500719 .init_prio = (prio), \
720 .init_options = (options), \
721 .init_delay = (delay), \
722 .init_abort = (abort), \
723 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400724 }
725
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400726/**
Allan Stephensc98da842016-11-11 15:45:03 -0500727 * INTERNAL_HIDDEN @endcond
728 */
729
730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500731 * @brief Statically define and initialize a thread.
732 *
733 * The thread may be scheduled for immediate execution or a delayed start.
734 *
735 * Thread options are architecture-specific, and can include K_ESSENTIAL,
736 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
737 * them using "|" (the logical OR operator).
738 *
739 * The ID of the thread can be accessed using:
740 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500741 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500742 *
743 * @param name Name of the thread.
744 * @param stack_size Stack size in bytes.
745 * @param entry Thread entry function.
746 * @param p1 1st entry point parameter.
747 * @param p2 2nd entry point parameter.
748 * @param p3 3rd entry point parameter.
749 * @param prio Thread priority.
750 * @param options Thread options.
751 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400752 *
753 * @internal It has been observed that the x86 compiler by default aligns
754 * these _static_thread_data structures to 32-byte boundaries, thereby
755 * wasting space. To work around this, force a 4-byte alignment.
756 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500757#define K_THREAD_DEFINE(name, stack_size, \
758 entry, p1, p2, p3, \
759 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700760 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700761 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500762 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500763 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700764 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
765 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500766 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700767 NULL, 0); \
768 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * @param thread ID of thread whose priority is needed.
776 *
777 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700779__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780
781/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 *
786 * Rescheduling can occur immediately depending on the priority @a thread is
787 * set to:
788 *
789 * - If its priority is raised above the priority of the caller of this
790 * function, and the caller is preemptible, @a thread will be scheduled in.
791 *
792 * - If the caller operates on itself, it lowers its priority below that of
793 * other threads in the system, and the caller is preemptible, the thread of
794 * highest priority will be scheduled in.
795 *
796 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
797 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
798 * highest priority.
799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 * @param prio New priority.
802 *
803 * @warning Changing the priority of a thread currently involved in mutex
804 * priority inheritance may result in undefined behavior.
805 *
806 * @return N/A
807 */
Andrew Boie468190a2017-09-29 14:00:48 -0700808__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400809
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine prevents the kernel scheduler from making @a thread the
814 * current thread. All other internal operations on @a thread are still
815 * performed; for example, any timeout it is waiting on keeps ticking,
816 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * If @a thread is already suspended, the routine has no effect.
819 *
820 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
822 * @return N/A
823 */
Andrew Boie468190a2017-09-29 14:00:48 -0700824__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825
826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * This routine allows the kernel scheduler to make @a thread the current
830 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * If @a thread is not currently suspended, the routine has no effect.
833 *
834 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
836 * @return N/A
837 */
Andrew Boie468190a2017-09-29 14:00:48 -0700838__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400839
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * This routine specifies how the scheduler will perform time slicing of
844 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * To enable time slicing, @a slice must be non-zero. The scheduler
847 * ensures that no thread runs for more than the specified time limit
848 * before other threads of that priority are given a chance to execute.
849 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700850 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 * execute. Once the scheduler selects a thread for execution, there is no
854 * minimum guaranteed time the thread will execute before threads of greater or
855 * equal priority are scheduled.
856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 * for execution, this routine has no effect; the thread is immediately
859 * rescheduled after the slice period expires.
860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * To disable timeslicing, set both @a slice and @a prio to zero.
862 *
863 * @param slice Maximum time slice length (in milliseconds).
864 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
866 * @return N/A
867 */
Kumar Galacc334c72017-04-21 10:55:34 -0500868extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400869
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870/**
Allan Stephensc98da842016-11-11 15:45:03 -0500871 * @} end defgroup thread_apis
872 */
873
874/**
875 * @addtogroup isr_apis
876 * @{
877 */
878
879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephensc98da842016-11-11 15:45:03 -0500882 * This routine allows the caller to customize its actions, depending on
883 * whether it is a thread or an ISR.
884 *
885 * @note Can be called by ISRs.
886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @return 0 if invoked by a thread.
888 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500890extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891
Benjamin Walsh445830d2016-11-10 15:54:27 -0500892/**
893 * @brief Determine if code is running in a preemptible thread.
894 *
Allan Stephensc98da842016-11-11 15:45:03 -0500895 * This routine allows the caller to customize its actions, depending on
896 * whether it can be preempted by another thread. The routine returns a 'true'
897 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500898 *
Allan Stephensc98da842016-11-11 15:45:03 -0500899 * - The code is running in a thread, not at ISR.
900 * - The thread's priority is in the preemptible range.
901 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500902 *
Allan Stephensc98da842016-11-11 15:45:03 -0500903 * @note Can be called by ISRs.
904 *
905 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500906 * @return Non-zero if invoked by a preemptible thread.
907 */
Andrew Boie468190a2017-09-29 14:00:48 -0700908__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500909
Allan Stephensc98da842016-11-11 15:45:03 -0500910/**
911 * @} end addtogroup isr_apis
912 */
913
914/**
915 * @addtogroup thread_apis
916 * @{
917 */
918
919/**
920 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500921 *
Allan Stephensc98da842016-11-11 15:45:03 -0500922 * This routine prevents the current thread from being preempted by another
923 * thread by instructing the scheduler to treat it as a cooperative thread.
924 * If the thread subsequently performs an operation that makes it unready,
925 * it will be context switched out in the normal manner. When the thread
926 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500927 *
Allan Stephensc98da842016-11-11 15:45:03 -0500928 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500929 *
Allan Stephensc98da842016-11-11 15:45:03 -0500930 * @note k_sched_lock() and k_sched_unlock() should normally be used
931 * when the operation being performed can be safely interrupted by ISRs.
932 * However, if the amount of processing involved is very small, better
933 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500934 *
935 * @return N/A
936 */
937extern void k_sched_lock(void);
938
Allan Stephensc98da842016-11-11 15:45:03 -0500939/**
940 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500941 *
Allan Stephensc98da842016-11-11 15:45:03 -0500942 * This routine reverses the effect of a previous call to k_sched_lock().
943 * A thread must call the routine once for each time it called k_sched_lock()
944 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500945 *
946 * @return N/A
947 */
948extern void k_sched_unlock(void);
949
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500951 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500955 * Custom data is not used by the kernel itself, and is freely available
956 * for a thread to use as it sees fit. It can be used as a framework
957 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500959 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400960 *
961 * @return N/A
962 */
Andrew Boie468190a2017-09-29 14:00:48 -0700963__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964
965/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500966 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500970 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 */
Andrew Boie468190a2017-09-29 14:00:48 -0700972__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400973
974/**
Allan Stephensc98da842016-11-11 15:45:03 -0500975 * @} end addtogroup thread_apis
976 */
977
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400978#include <sys_clock.h>
979
Allan Stephensc2f15a42016-11-17 12:24:22 -0500980/**
981 * @addtogroup clock_apis
982 * @{
983 */
984
985/**
986 * @brief Generate null timeout delay.
987 *
988 * This macro generates a timeout delay that that instructs a kernel API
989 * not to wait if the requested operation cannot be performed immediately.
990 *
991 * @return Timeout delay value.
992 */
993#define K_NO_WAIT 0
994
995/**
996 * @brief Generate timeout delay from milliseconds.
997 *
998 * This macro generates a timeout delay that that instructs a kernel API
999 * to wait up to @a ms milliseconds to perform the requested operation.
1000 *
1001 * @param ms Duration in milliseconds.
1002 *
1003 * @return Timeout delay value.
1004 */
Johan Hedberg14471692016-11-13 10:52:15 +02001005#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001006
1007/**
1008 * @brief Generate timeout delay from seconds.
1009 *
1010 * This macro generates a timeout delay that that instructs a kernel API
1011 * to wait up to @a s seconds to perform the requested operation.
1012 *
1013 * @param s Duration in seconds.
1014 *
1015 * @return Timeout delay value.
1016 */
Johan Hedberg14471692016-11-13 10:52:15 +02001017#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001018
1019/**
1020 * @brief Generate timeout delay from minutes.
1021 *
1022 * This macro generates a timeout delay that that instructs a kernel API
1023 * to wait up to @a m minutes to perform the requested operation.
1024 *
1025 * @param m Duration in minutes.
1026 *
1027 * @return Timeout delay value.
1028 */
Johan Hedberg14471692016-11-13 10:52:15 +02001029#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001030
1031/**
1032 * @brief Generate timeout delay from hours.
1033 *
1034 * This macro generates a timeout delay that that instructs a kernel API
1035 * to wait up to @a h hours to perform the requested operation.
1036 *
1037 * @param h Duration in hours.
1038 *
1039 * @return Timeout delay value.
1040 */
Johan Hedberg14471692016-11-13 10:52:15 +02001041#define K_HOURS(h) K_MINUTES((h) * 60)
1042
Allan Stephensc98da842016-11-11 15:45:03 -05001043/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001044 * @brief Generate infinite timeout delay.
1045 *
1046 * This macro generates a timeout delay that that instructs a kernel API
1047 * to wait as long as necessary to perform the requested operation.
1048 *
1049 * @return Timeout delay value.
1050 */
1051#define K_FOREVER (-1)
1052
1053/**
1054 * @} end addtogroup clock_apis
1055 */
1056
1057/**
Allan Stephensc98da842016-11-11 15:45:03 -05001058 * @cond INTERNAL_HIDDEN
1059 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001060
Benjamin Walsh62092182016-12-20 14:39:08 -05001061/* kernel clocks */
1062
1063#if (sys_clock_ticks_per_sec == 1000) || \
1064 (sys_clock_ticks_per_sec == 500) || \
1065 (sys_clock_ticks_per_sec == 250) || \
1066 (sys_clock_ticks_per_sec == 125) || \
1067 (sys_clock_ticks_per_sec == 100) || \
1068 (sys_clock_ticks_per_sec == 50) || \
1069 (sys_clock_ticks_per_sec == 25) || \
1070 (sys_clock_ticks_per_sec == 20) || \
1071 (sys_clock_ticks_per_sec == 10) || \
1072 (sys_clock_ticks_per_sec == 1)
1073
1074 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1075#else
1076 /* yields horrible 64-bit math on many architectures: try to avoid */
1077 #define _NON_OPTIMIZED_TICKS_PER_SEC
1078#endif
1079
1080#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001081extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001082#else
Kumar Galacc334c72017-04-21 10:55:34 -05001083static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001084{
Kumar Galacc334c72017-04-21 10:55:34 -05001085 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001086}
1087#endif
1088
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001089/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001090#ifdef CONFIG_TICKLESS_KERNEL
1091#define _TICK_ALIGN 0
1092#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001093#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001094#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001095
Kumar Galacc334c72017-04-21 10:55:34 -05001096static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001097{
Benjamin Walsh62092182016-12-20 14:39:08 -05001098#ifdef CONFIG_SYS_CLOCK_EXISTS
1099
1100#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001101 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001102#else
Kumar Galacc334c72017-04-21 10:55:34 -05001103 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001104#endif
1105
1106#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001107 __ASSERT(ticks == 0, "");
1108 return 0;
1109#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001110}
1111
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112struct k_timer {
1113 /*
1114 * _timeout structure must be first here if we want to use
1115 * dynamic timer allocation. timeout.node is used in the double-linked
1116 * list of free timers
1117 */
1118 struct _timeout timeout;
1119
Allan Stephens45bfa372016-10-12 12:39:42 -05001120 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121 _wait_q_t wait_q;
1122
1123 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001124 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001125
1126 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001127 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128
1129 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001130 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131
Allan Stephens45bfa372016-10-12 12:39:42 -05001132 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001133 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001134
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001135 /* user-specific data, also used to support legacy features */
1136 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001137
Anas Nashif2f203c22016-12-18 06:57:45 -05001138 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001139};
1140
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001141#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001143 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001144 .timeout.wait_q = NULL, \
1145 .timeout.thread = NULL, \
1146 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001148 .expiry_fn = expiry, \
1149 .stop_fn = stop, \
1150 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001151 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001152 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001153 }
1154
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001155#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1156
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157/**
Allan Stephensc98da842016-11-11 15:45:03 -05001158 * INTERNAL_HIDDEN @endcond
1159 */
1160
1161/**
1162 * @defgroup timer_apis Timer APIs
1163 * @ingroup kernel_apis
1164 * @{
1165 */
1166
1167/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001168 * @typedef k_timer_expiry_t
1169 * @brief Timer expiry function type.
1170 *
1171 * A timer's expiry function is executed by the system clock interrupt handler
1172 * each time the timer expires. The expiry function is optional, and is only
1173 * invoked if the timer has been initialized with one.
1174 *
1175 * @param timer Address of timer.
1176 *
1177 * @return N/A
1178 */
1179typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1180
1181/**
1182 * @typedef k_timer_stop_t
1183 * @brief Timer stop function type.
1184 *
1185 * A timer's stop function is executed if the timer is stopped prematurely.
1186 * The function runs in the context of the thread that stops the timer.
1187 * The stop function is optional, and is only invoked if the timer has been
1188 * initialized with one.
1189 *
1190 * @param timer Address of timer.
1191 *
1192 * @return N/A
1193 */
1194typedef void (*k_timer_stop_t)(struct k_timer *timer);
1195
1196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001199 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001201 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 *
1203 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001204 * @param expiry_fn Function to invoke each time the timer expires.
1205 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001207#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001208 struct k_timer name \
1209 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001210 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211
Allan Stephens45bfa372016-10-12 12:39:42 -05001212/**
1213 * @brief Initialize a timer.
1214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001216 *
1217 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * @param expiry_fn Function to invoke each time the timer expires.
1219 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001220 *
1221 * @return N/A
1222 */
1223extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001224 k_timer_expiry_t expiry_fn,
1225 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001226
Allan Stephens45bfa372016-10-12 12:39:42 -05001227/**
1228 * @brief Start a timer.
1229 *
1230 * This routine starts a timer, and resets its status to zero. The timer
1231 * begins counting down using the specified duration and period values.
1232 *
1233 * Attempting to start a timer that is already running is permitted.
1234 * The timer's status is reset to zero and the timer begins counting down
1235 * using the new duration and period values.
1236 *
1237 * @param timer Address of timer.
1238 * @param duration Initial timer duration (in milliseconds).
1239 * @param period Timer period (in milliseconds).
1240 *
1241 * @return N/A
1242 */
Andrew Boiea354d492017-09-29 16:22:28 -07001243__syscall void k_timer_start(struct k_timer *timer,
1244 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001245
1246/**
1247 * @brief Stop a timer.
1248 *
1249 * This routine stops a running timer prematurely. The timer's stop function,
1250 * if one exists, is invoked by the caller.
1251 *
1252 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001253 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001254 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001255 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1256 * if @a k_timer_stop is to be called from ISRs.
1257 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001258 * @param timer Address of timer.
1259 *
1260 * @return N/A
1261 */
Andrew Boiea354d492017-09-29 16:22:28 -07001262__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001263
1264/**
1265 * @brief Read timer status.
1266 *
1267 * This routine reads the timer's status, which indicates the number of times
1268 * it has expired since its status was last read.
1269 *
1270 * Calling this routine resets the timer's status to zero.
1271 *
1272 * @param timer Address of timer.
1273 *
1274 * @return Timer status.
1275 */
Andrew Boiea354d492017-09-29 16:22:28 -07001276__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001277
1278/**
1279 * @brief Synchronize thread to timer expiration.
1280 *
1281 * This routine blocks the calling thread until the timer's status is non-zero
1282 * (indicating that it has expired at least once since it was last examined)
1283 * or the timer is stopped. If the timer status is already non-zero,
1284 * or the timer is already stopped, the caller continues without waiting.
1285 *
1286 * Calling this routine resets the timer's status to zero.
1287 *
1288 * This routine must not be used by interrupt handlers, since they are not
1289 * allowed to block.
1290 *
1291 * @param timer Address of timer.
1292 *
1293 * @return Timer status.
1294 */
Andrew Boiea354d492017-09-29 16:22:28 -07001295__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001296
1297/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001299 *
1300 * This routine computes the (approximate) time remaining before a running
1301 * timer next expires. If the timer is not running, it returns zero.
1302 *
1303 * @param timer Address of timer.
1304 *
1305 * @return Remaining time (in milliseconds).
1306 */
Andrew Boiea354d492017-09-29 16:22:28 -07001307__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1308
1309static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001310{
1311 return _timeout_remaining_get(&timer->timeout);
1312}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001313
Allan Stephensc98da842016-11-11 15:45:03 -05001314/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001315 * @brief Associate user-specific data with a timer.
1316 *
1317 * This routine records the @a user_data with the @a timer, to be retrieved
1318 * later.
1319 *
1320 * It can be used e.g. in a timer handler shared across multiple subsystems to
1321 * retrieve data specific to the subsystem this timer is associated with.
1322 *
1323 * @param timer Address of timer.
1324 * @param user_data User data to associate with the timer.
1325 *
1326 * @return N/A
1327 */
Andrew Boiea354d492017-09-29 16:22:28 -07001328__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1329
1330static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1331 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001332{
1333 timer->user_data = user_data;
1334}
1335
1336/**
1337 * @brief Retrieve the user-specific data from a timer.
1338 *
1339 * @param timer Address of timer.
1340 *
1341 * @return The user data.
1342 */
Andrew Boiea354d492017-09-29 16:22:28 -07001343__syscall void *k_timer_user_data_get(struct k_timer *timer);
1344
1345static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001346{
1347 return timer->user_data;
1348}
1349
1350/**
Allan Stephensc98da842016-11-11 15:45:03 -05001351 * @} end defgroup timer_apis
1352 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001353
Allan Stephensc98da842016-11-11 15:45:03 -05001354/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001355 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001356 * @{
1357 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001358
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001362 * This routine returns the elapsed time since the system booted,
1363 * in milliseconds.
1364 *
1365 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001366 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001367__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001368
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001369#ifdef CONFIG_TICKLESS_KERNEL
1370/**
1371 * @brief Enable clock always on in tickless kernel
1372 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001373 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001374 * there are no timer events programmed in tickless kernel
1375 * scheduling. This is necessary if the clock is used to track
1376 * passage of time.
1377 *
1378 * @retval prev_status Previous status of always on flag
1379 */
1380static inline int k_enable_sys_clock_always_on(void)
1381{
1382 int prev_status = _sys_clock_always_on;
1383
1384 _sys_clock_always_on = 1;
1385 _enable_sys_clock();
1386
1387 return prev_status;
1388}
1389
1390/**
1391 * @brief Disable clock always on in tickless kernel
1392 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001393 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001394 * there are no timer events programmed in tickless kernel
1395 * scheduling. To save power, this routine should be called
1396 * immediately when clock is not used to track time.
1397 */
1398static inline void k_disable_sys_clock_always_on(void)
1399{
1400 _sys_clock_always_on = 0;
1401}
1402#else
1403#define k_enable_sys_clock_always_on() do { } while ((0))
1404#define k_disable_sys_clock_always_on() do { } while ((0))
1405#endif
1406
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001407/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001408 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001410 * This routine returns the lower 32-bits of the elapsed time since the system
1411 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001413 * This routine can be more efficient than k_uptime_get(), as it reduces the
1414 * need for interrupt locking and 64-bit math. However, the 32-bit result
1415 * cannot hold a system uptime time larger than approximately 50 days, so the
1416 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001418 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001419 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001420__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001421
1422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001423 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001425 * This routine computes the elapsed time between the current system uptime
1426 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001428 * @param reftime Pointer to a reference time, which is updated to the current
1429 * uptime upon return.
1430 *
1431 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001432 */
Kumar Galacc334c72017-04-21 10:55:34 -05001433extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001434
1435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001436 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001438 * This routine computes the elapsed time between the current system uptime
1439 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001441 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1442 * need for interrupt locking and 64-bit math. However, the 32-bit result
1443 * cannot hold an elapsed time larger than approximately 50 days, so the
1444 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * @param reftime Pointer to a reference time, which is updated to the current
1447 * uptime upon return.
1448 *
1449 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001450 */
Kumar Galacc334c72017-04-21 10:55:34 -05001451extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001452
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001454 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001456 * This routine returns the current time, as measured by the system's hardware
1457 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001460 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001461#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001464 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465 */
1466
Allan Stephensc98da842016-11-11 15:45:03 -05001467/**
1468 * @cond INTERNAL_HIDDEN
1469 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001470
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001471struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001472 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001473 union {
1474 _wait_q_t wait_q;
1475
1476 _POLL_EVENT;
1477 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001478
1479 _OBJECT_TRACING_NEXT_PTR(k_queue);
1480};
1481
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001482#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001483 { \
1484 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1485 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001486 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001487 _OBJECT_TRACING_INIT \
1488 }
1489
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001490#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1491
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001492/**
1493 * INTERNAL_HIDDEN @endcond
1494 */
1495
1496/**
1497 * @defgroup queue_apis Queue APIs
1498 * @ingroup kernel_apis
1499 * @{
1500 */
1501
1502/**
1503 * @brief Initialize a queue.
1504 *
1505 * This routine initializes a queue object, prior to its first use.
1506 *
1507 * @param queue Address of the queue.
1508 *
1509 * @return N/A
1510 */
1511extern void k_queue_init(struct k_queue *queue);
1512
1513/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001514 * @brief Cancel waiting on a queue.
1515 *
1516 * This routine causes first thread pending on @a queue, if any, to
1517 * return from k_queue_get() call with NULL value (as if timeout expired).
1518 *
1519 * @note Can be called by ISRs.
1520 *
1521 * @param queue Address of the queue.
1522 *
1523 * @return N/A
1524 */
1525extern void k_queue_cancel_wait(struct k_queue *queue);
1526
1527/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001528 * @brief Append an element to the end of a queue.
1529 *
1530 * This routine appends a data item to @a queue. A queue data item must be
1531 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1532 * reserved for the kernel's use.
1533 *
1534 * @note Can be called by ISRs.
1535 *
1536 * @param queue Address of the queue.
1537 * @param data Address of the data item.
1538 *
1539 * @return N/A
1540 */
1541extern void k_queue_append(struct k_queue *queue, void *data);
1542
1543/**
1544 * @brief Prepend an element to a queue.
1545 *
1546 * This routine prepends a data item to @a queue. A queue data item must be
1547 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1548 * reserved for the kernel's use.
1549 *
1550 * @note Can be called by ISRs.
1551 *
1552 * @param queue Address of the queue.
1553 * @param data Address of the data item.
1554 *
1555 * @return N/A
1556 */
1557extern void k_queue_prepend(struct k_queue *queue, void *data);
1558
1559/**
1560 * @brief Inserts an element to a queue.
1561 *
1562 * This routine inserts a data item to @a queue after previous item. A queue
1563 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1564 * item are reserved for the kernel's use.
1565 *
1566 * @note Can be called by ISRs.
1567 *
1568 * @param queue Address of the queue.
1569 * @param prev Address of the previous data item.
1570 * @param data Address of the data item.
1571 *
1572 * @return N/A
1573 */
1574extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1575
1576/**
1577 * @brief Atomically append a list of elements to a queue.
1578 *
1579 * This routine adds a list of data items to @a queue in one operation.
1580 * The data items must be in a singly-linked list, with the first 32 bits
1581 * in each data item pointing to the next data item; the list must be
1582 * NULL-terminated.
1583 *
1584 * @note Can be called by ISRs.
1585 *
1586 * @param queue Address of the queue.
1587 * @param head Pointer to first node in singly-linked list.
1588 * @param tail Pointer to last node in singly-linked list.
1589 *
1590 * @return N/A
1591 */
1592extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1593
1594/**
1595 * @brief Atomically add a list of elements to a queue.
1596 *
1597 * This routine adds a list of data items to @a queue in one operation.
1598 * The data items must be in a singly-linked list implemented using a
1599 * sys_slist_t object. Upon completion, the original list is empty.
1600 *
1601 * @note Can be called by ISRs.
1602 *
1603 * @param queue Address of the queue.
1604 * @param list Pointer to sys_slist_t object.
1605 *
1606 * @return N/A
1607 */
1608extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1609
1610/**
1611 * @brief Get an element from a queue.
1612 *
1613 * This routine removes first data item from @a queue. The first 32 bits of the
1614 * data item are reserved for the kernel's use.
1615 *
1616 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1617 *
1618 * @param queue Address of the queue.
1619 * @param timeout Waiting period to obtain a data item (in milliseconds),
1620 * or one of the special values K_NO_WAIT and K_FOREVER.
1621 *
1622 * @return Address of the data item if successful; NULL if returned
1623 * without waiting, or waiting period timed out.
1624 */
Kumar Galacc334c72017-04-21 10:55:34 -05001625extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001626
1627/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001628 * @brief Remove an element from a queue.
1629 *
1630 * This routine removes data item from @a queue. The first 32 bits of the
1631 * data item are reserved for the kernel's use. Removing elements from k_queue
1632 * rely on sys_slist_find_and_remove which is not a constant time operation.
1633 *
1634 * @note Can be called by ISRs
1635 *
1636 * @param queue Address of the queue.
1637 * @param data Address of the data item.
1638 *
1639 * @return true if data item was removed
1640 */
1641static inline bool k_queue_remove(struct k_queue *queue, void *data)
1642{
1643 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1644}
1645
1646/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001647 * @brief Query a queue to see if it has data available.
1648 *
1649 * Note that the data might be already gone by the time this function returns
1650 * if other threads are also trying to read from the queue.
1651 *
1652 * @note Can be called by ISRs.
1653 *
1654 * @param queue Address of the queue.
1655 *
1656 * @return Non-zero if the queue is empty.
1657 * @return 0 if data is available.
1658 */
1659static inline int k_queue_is_empty(struct k_queue *queue)
1660{
1661 return (int)sys_slist_is_empty(&queue->data_q);
1662}
1663
1664/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001665 * @brief Peek element at the head of queue.
1666 *
1667 * Return element from the head of queue without removing it.
1668 *
1669 * @param queue Address of the queue.
1670 *
1671 * @return Head element, or NULL if queue is empty.
1672 */
1673static inline void *k_queue_peek_head(struct k_queue *queue)
1674{
1675 return sys_slist_peek_head(&queue->data_q);
1676}
1677
1678/**
1679 * @brief Peek element at the tail of queue.
1680 *
1681 * Return element from the tail of queue without removing it.
1682 *
1683 * @param queue Address of the queue.
1684 *
1685 * @return Tail element, or NULL if queue is empty.
1686 */
1687static inline void *k_queue_peek_tail(struct k_queue *queue)
1688{
1689 return sys_slist_peek_tail(&queue->data_q);
1690}
1691
1692/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001693 * @brief Statically define and initialize a queue.
1694 *
1695 * The queue can be accessed outside the module where it is defined using:
1696 *
1697 * @code extern struct k_queue <name>; @endcode
1698 *
1699 * @param name Name of the queue.
1700 */
1701#define K_QUEUE_DEFINE(name) \
1702 struct k_queue name \
1703 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001704 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001705
1706/**
1707 * @} end defgroup queue_apis
1708 */
1709
1710/**
1711 * @cond INTERNAL_HIDDEN
1712 */
1713
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001715 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001716};
1717
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001718#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001719 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001720 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001721 }
1722
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001723#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1724
Allan Stephensc98da842016-11-11 15:45:03 -05001725/**
1726 * INTERNAL_HIDDEN @endcond
1727 */
1728
1729/**
1730 * @defgroup fifo_apis Fifo APIs
1731 * @ingroup kernel_apis
1732 * @{
1733 */
1734
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
1742 * @return N/A
1743 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001744#define k_fifo_init(fifo) \
1745 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746
1747/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001748 * @brief Cancel waiting on a fifo.
1749 *
1750 * This routine causes first thread pending on @a fifo, if any, to
1751 * return from k_fifo_get() call with NULL value (as if timeout
1752 * expired).
1753 *
1754 * @note Can be called by ISRs.
1755 *
1756 * @param fifo Address of the fifo.
1757 *
1758 * @return N/A
1759 */
1760#define k_fifo_cancel_wait(fifo) \
1761 k_queue_cancel_wait((struct k_queue *) fifo)
1762
1763/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * This routine adds a data item to @a fifo. A fifo data item must be
1767 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1768 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001770 * @note Can be called by ISRs.
1771 *
1772 * @param fifo Address of the fifo.
1773 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 *
1775 * @return N/A
1776 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001777#define k_fifo_put(fifo, data) \
1778 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779
1780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001783 * This routine adds a list of data items to @a fifo in one operation.
1784 * The data items must be in a singly-linked list, with the first 32 bits
1785 * each data item pointing to the next data item; the list must be
1786 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @param fifo Address of the fifo.
1791 * @param head Pointer to first node in singly-linked list.
1792 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793 *
1794 * @return N/A
1795 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001796#define k_fifo_put_list(fifo, head, tail) \
1797 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798
1799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * This routine adds a list of data items to @a fifo in one operation.
1803 * The data items must be in a singly-linked list implemented using a
1804 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 * and must be re-initialized via sys_slist_init().
1806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @note Can be called by ISRs.
1808 *
1809 * @param fifo Address of the fifo.
1810 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
1812 * @return N/A
1813 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001814#define k_fifo_put_slist(fifo, list) \
1815 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816
1817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * This routine removes a data item from @a fifo in a "first in, first out"
1821 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1824 *
1825 * @param fifo Address of the fifo.
1826 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 * or one of the special values K_NO_WAIT and K_FOREVER.
1828 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001829 * @return Address of the data item if successful; NULL if returned
1830 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001831 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001832#define k_fifo_get(fifo, timeout) \
1833 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001834
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001835/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001836 * @brief Query a fifo to see if it has data available.
1837 *
1838 * Note that the data might be already gone by the time this function returns
1839 * if other threads is also trying to read from the fifo.
1840 *
1841 * @note Can be called by ISRs.
1842 *
1843 * @param fifo Address of the fifo.
1844 *
1845 * @return Non-zero if the fifo is empty.
1846 * @return 0 if data is available.
1847 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001848#define k_fifo_is_empty(fifo) \
1849 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001850
1851/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001852 * @brief Peek element at the head of fifo.
1853 *
1854 * Return element from the head of fifo without removing it. A usecase
1855 * for this is if elements of the fifo are themselves containers. Then
1856 * on each iteration of processing, a head container will be peeked,
1857 * and some data processed out of it, and only if the container is empty,
1858 * it will be completely remove from the fifo.
1859 *
1860 * @param fifo Address of the fifo.
1861 *
1862 * @return Head element, or NULL if the fifo is empty.
1863 */
1864#define k_fifo_peek_head(fifo) \
1865 k_queue_peek_head((struct k_queue *) fifo)
1866
1867/**
1868 * @brief Peek element at the tail of fifo.
1869 *
1870 * Return element from the tail of fifo (without removing it). A usecase
1871 * for this is if elements of the fifo are themselves containers. Then
1872 * it may be useful to add more data to the last container in fifo.
1873 *
1874 * @param fifo Address of the fifo.
1875 *
1876 * @return Tail element, or NULL if fifo is empty.
1877 */
1878#define k_fifo_peek_tail(fifo) \
1879 k_queue_peek_tail((struct k_queue *) fifo)
1880
1881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001886 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001891 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001892 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001893 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001894
Allan Stephensc98da842016-11-11 15:45:03 -05001895/**
1896 * @} end defgroup fifo_apis
1897 */
1898
1899/**
1900 * @cond INTERNAL_HIDDEN
1901 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
1903struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001904 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905};
1906
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001907#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001908 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001909 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001910 }
1911
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001912#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1913
Allan Stephensc98da842016-11-11 15:45:03 -05001914/**
1915 * INTERNAL_HIDDEN @endcond
1916 */
1917
1918/**
1919 * @defgroup lifo_apis Lifo APIs
1920 * @ingroup kernel_apis
1921 * @{
1922 */
1923
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001925 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930 *
1931 * @return N/A
1932 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001933#define k_lifo_init(lifo) \
1934 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935
1936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001937 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939 * This routine adds a data item to @a lifo. A lifo data item must be
1940 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1941 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001943 * @note Can be called by ISRs.
1944 *
1945 * @param lifo Address of the lifo.
1946 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 *
1948 * @return N/A
1949 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001950#define k_lifo_put(lifo, data) \
1951 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952
1953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * This routine removes a data item from @a lifo in a "last in, first out"
1957 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1960 *
1961 * @param lifo Address of the lifo.
1962 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001963 * or one of the special values K_NO_WAIT and K_FOREVER.
1964 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001965 * @return Address of the data item if successful; NULL if returned
1966 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001968#define k_lifo_get(lifo, timeout) \
1969 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001976 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001980#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001981 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001982 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001983 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984
Allan Stephensc98da842016-11-11 15:45:03 -05001985/**
1986 * @} end defgroup lifo_apis
1987 */
1988
1989/**
1990 * @cond INTERNAL_HIDDEN
1991 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992
1993struct k_stack {
1994 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001995 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996
Anas Nashif2f203c22016-12-18 06:57:45 -05001997 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998};
1999
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002000#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002001 { \
2002 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2003 .base = stack_buffer, \
2004 .next = stack_buffer, \
2005 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002006 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002007 }
2008
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002009#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2010
Allan Stephensc98da842016-11-11 15:45:03 -05002011/**
2012 * INTERNAL_HIDDEN @endcond
2013 */
2014
2015/**
2016 * @defgroup stack_apis Stack APIs
2017 * @ingroup kernel_apis
2018 * @{
2019 */
2020
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021/**
2022 * @brief Initialize a stack.
2023 *
2024 * This routine initializes a stack object, prior to its first use.
2025 *
2026 * @param stack Address of the stack.
2027 * @param buffer Address of array used to hold stacked values.
2028 * @param num_entries Maximum number of values that can be stacked.
2029 *
2030 * @return N/A
2031 */
Andrew Boiee8734462017-09-29 16:42:07 -07002032__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002033 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034
2035/**
2036 * @brief Push an element onto a stack.
2037 *
2038 * This routine adds a 32-bit value @a data to @a stack.
2039 *
2040 * @note Can be called by ISRs.
2041 *
2042 * @param stack Address of the stack.
2043 * @param data Value to push onto the stack.
2044 *
2045 * @return N/A
2046 */
Andrew Boiee8734462017-09-29 16:42:07 -07002047__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002048
2049/**
2050 * @brief Pop an element from a stack.
2051 *
2052 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2053 * manner and stores the value in @a data.
2054 *
2055 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2056 *
2057 * @param stack Address of the stack.
2058 * @param data Address of area to hold the value popped from the stack.
2059 * @param timeout Waiting period to obtain a value (in milliseconds),
2060 * or one of the special values K_NO_WAIT and K_FOREVER.
2061 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002062 * @retval 0 Element popped from stack.
2063 * @retval -EBUSY Returned without waiting.
2064 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 */
Andrew Boiee8734462017-09-29 16:42:07 -07002066__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002067
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002073 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * @param name Name of the stack.
2076 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002077 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002078#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002079 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002080 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002081 struct k_stack name \
2082 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002083 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002084 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002086/**
Allan Stephensc98da842016-11-11 15:45:03 -05002087 * @} end defgroup stack_apis
2088 */
2089
Allan Stephens6bba9b02016-11-16 14:56:54 -05002090struct k_work;
2091
Allan Stephensc98da842016-11-11 15:45:03 -05002092/**
2093 * @defgroup workqueue_apis Workqueue Thread APIs
2094 * @ingroup kernel_apis
2095 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096 */
2097
Allan Stephens6bba9b02016-11-16 14:56:54 -05002098/**
2099 * @typedef k_work_handler_t
2100 * @brief Work item handler function type.
2101 *
2102 * A work item's handler function is executed by a workqueue's thread
2103 * when the work item is processed by the workqueue.
2104 *
2105 * @param work Address of the work item.
2106 *
2107 * @return N/A
2108 */
2109typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110
2111/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002112 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002114
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002116 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002117 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118};
2119
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002121 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122};
2123
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002125 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002126 k_work_handler_t handler;
2127 atomic_t flags[1];
2128};
2129
Allan Stephens6bba9b02016-11-16 14:56:54 -05002130struct k_delayed_work {
2131 struct k_work work;
2132 struct _timeout timeout;
2133 struct k_work_q *work_q;
2134};
2135
2136extern struct k_work_q k_sys_work_q;
2137
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002138/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002139 * INTERNAL_HIDDEN @endcond
2140 */
2141
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002142#define _K_WORK_INITIALIZER(work_handler) \
2143 { \
2144 ._reserved = NULL, \
2145 .handler = work_handler, \
2146 .flags = { 0 } \
2147 }
2148
2149#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2150
Allan Stephens6bba9b02016-11-16 14:56:54 -05002151/**
2152 * @brief Initialize a statically-defined work item.
2153 *
2154 * This macro can be used to initialize a statically-defined workqueue work
2155 * item, prior to its first use. For example,
2156 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002157 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002158 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002159 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002160 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002162#define K_WORK_DEFINE(work, work_handler) \
2163 struct k_work work \
2164 __in_section(_k_work, static, work) = \
2165 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002166
2167/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002168 * @brief Initialize a work item.
2169 *
2170 * This routine initializes a workqueue work item, prior to its first use.
2171 *
2172 * @param work Address of work item.
2173 * @param handler Function to invoke each time work item is processed.
2174 *
2175 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176 */
2177static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2178{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002179 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002181 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182}
2183
2184/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002186 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002187 * This routine submits work item @a work to be processed by workqueue
2188 * @a work_q. If the work item is already pending in the workqueue's queue
2189 * as a result of an earlier submission, this routine has no effect on the
2190 * work item. If the work item has already been processed, or is currently
2191 * being processed, its work is considered complete and the work item can be
2192 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002193 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002194 * @warning
2195 * A submitted work item must not be modified until it has been processed
2196 * by the workqueue.
2197 *
2198 * @note Can be called by ISRs.
2199 *
2200 * @param work_q Address of workqueue.
2201 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002202 *
2203 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204 */
2205static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2206 struct k_work *work)
2207{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002208 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002209 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210 }
2211}
2212
2213/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002216 * This routine indicates if work item @a work is pending in a workqueue's
2217 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002219 * @note Can be called by ISRs.
2220 *
2221 * @param work Address of work item.
2222 *
2223 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002224 */
2225static inline int k_work_pending(struct k_work *work)
2226{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002227 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002228}
2229
2230/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002233 * This routine starts workqueue @a work_q. The workqueue spawns its work
2234 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002235 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002236 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002237 * @param stack Pointer to work queue thread's stack space, as defined by
2238 * K_THREAD_STACK_DEFINE()
2239 * @param stack_size Size of the work queue thread's stack (in bytes), which
2240 * should either be the same constant passed to
2241 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002242 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243 *
2244 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002245 */
Andrew Boie507852a2017-07-25 18:47:07 -07002246extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002247 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002248 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002251 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 * This routine initializes a workqueue delayed work item, prior to
2254 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002256 * @param work Address of delayed work item.
2257 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
2259 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002261extern void k_delayed_work_init(struct k_delayed_work *work,
2262 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263
2264/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * This routine schedules work item @a work to be processed by workqueue
2268 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002269 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002270 * Only when the countdown completes is the work item actually submitted to
2271 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * Submitting a previously submitted delayed work item that is still
2274 * counting down cancels the existing submission and restarts the countdown
2275 * using the new delay. If the work item is currently pending on the
2276 * workqueue's queue because the countdown has completed it is too late to
2277 * resubmit the item, and resubmission fails without impacting the work item.
2278 * If the work item has already been processed, or is currently being processed,
2279 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * @warning
2282 * A delayed work item must not be modified until it has been processed
2283 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @note Can be called by ISRs.
2286 *
2287 * @param work_q Address of workqueue.
2288 * @param work Address of delayed work item.
2289 * @param delay Delay before submitting the work item (in milliseconds).
2290 *
2291 * @retval 0 Work item countdown started.
2292 * @retval -EINPROGRESS Work item is already pending.
2293 * @retval -EINVAL Work item is being processed or has completed its work.
2294 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002296extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2297 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002298 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299
2300/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002303 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002304 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002309 * @param work Address of delayed work item.
2310 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002311 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002312 * @retval -EINPROGRESS Work item is already pending.
2313 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002315extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318 * @brief Submit a work item to the system workqueue.
2319 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002320 * This routine submits work item @a work to be processed by the system
2321 * workqueue. If the work item is already pending in the workqueue's queue
2322 * as a result of an earlier submission, this routine has no effect on the
2323 * work item. If the work item has already been processed, or is currently
2324 * being processed, its work is considered complete and the work item can be
2325 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002326 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * @warning
2328 * Work items submitted to the system workqueue should avoid using handlers
2329 * that block or yield since this may prevent the system workqueue from
2330 * processing other work items in a timely manner.
2331 *
2332 * @note Can be called by ISRs.
2333 *
2334 * @param work Address of work item.
2335 *
2336 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337 */
2338static inline void k_work_submit(struct k_work *work)
2339{
2340 k_work_submit_to_queue(&k_sys_work_q, work);
2341}
2342
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344 * @brief Submit a delayed work item to the system workqueue.
2345 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * This routine schedules work item @a work to be processed by the system
2347 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002348 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002349 * Only when the countdown completes is the work item actually submitted to
2350 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002352 * Submitting a previously submitted delayed work item that is still
2353 * counting down cancels the existing submission and restarts the countdown
2354 * using the new delay. If the work item is currently pending on the
2355 * workqueue's queue because the countdown has completed it is too late to
2356 * resubmit the item, and resubmission fails without impacting the work item.
2357 * If the work item has already been processed, or is currently being processed,
2358 * its work is considered complete and the work item can be resubmitted.
2359 *
2360 * @warning
2361 * Work items submitted to the system workqueue should avoid using handlers
2362 * that block or yield since this may prevent the system workqueue from
2363 * processing other work items in a timely manner.
2364 *
2365 * @note Can be called by ISRs.
2366 *
2367 * @param work Address of delayed work item.
2368 * @param delay Delay before submitting the work item (in milliseconds).
2369 *
2370 * @retval 0 Work item countdown started.
2371 * @retval -EINPROGRESS Work item is already pending.
2372 * @retval -EINVAL Work item is being processed or has completed its work.
2373 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374 */
2375static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002376 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002378 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379}
2380
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002382 * @brief Get time remaining before a delayed work gets scheduled.
2383 *
2384 * This routine computes the (approximate) time remaining before a
2385 * delayed work gets executed. If the delayed work is not waiting to be
2386 * schedules, it returns zero.
2387 *
2388 * @param work Delayed work item.
2389 *
2390 * @return Remaining time (in milliseconds).
2391 */
Kumar Galacc334c72017-04-21 10:55:34 -05002392static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002393{
2394 return _timeout_remaining_get(&work->timeout);
2395}
2396
2397/**
Allan Stephensc98da842016-11-11 15:45:03 -05002398 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399 */
2400
Allan Stephensc98da842016-11-11 15:45:03 -05002401/**
2402 * @cond INTERNAL_HIDDEN
2403 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404
2405struct k_mutex {
2406 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002407 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002408 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410
Anas Nashif2f203c22016-12-18 06:57:45 -05002411 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412};
2413
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002414#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 { \
2416 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2417 .owner = NULL, \
2418 .lock_count = 0, \
2419 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002420 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 }
2422
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002423#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2424
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425/**
Allan Stephensc98da842016-11-11 15:45:03 -05002426 * INTERNAL_HIDDEN @endcond
2427 */
2428
2429/**
2430 * @defgroup mutex_apis Mutex APIs
2431 * @ingroup kernel_apis
2432 * @{
2433 */
2434
2435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002439 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002440 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002443 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002445 struct k_mutex name \
2446 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002448
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454 * Upon completion, the mutex is available and does not have an owner.
2455 *
2456 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
2458 * @return N/A
2459 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002460__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461
2462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * This routine locks @a mutex. If the mutex is locked by another thread,
2466 * the calling thread waits until the mutex becomes available or until
2467 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * A thread is permitted to lock a mutex it has already locked. The operation
2470 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 * @param mutex Address of the mutex.
2473 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 * or one of the special values K_NO_WAIT and K_FOREVER.
2475 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002476 * @retval 0 Mutex locked.
2477 * @retval -EBUSY Returned without waiting.
2478 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002480__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481
2482/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * This routine unlocks @a mutex. The mutex must already be locked by the
2486 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 *
2488 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490 * thread.
2491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
2494 * @return N/A
2495 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002496__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497
Allan Stephensc98da842016-11-11 15:45:03 -05002498/**
2499 * @} end defgroup mutex_apis
2500 */
2501
2502/**
2503 * @cond INTERNAL_HIDDEN
2504 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
2506struct k_sem {
2507 _wait_q_t wait_q;
2508 unsigned int count;
2509 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002510 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511
Anas Nashif2f203c22016-12-18 06:57:45 -05002512 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513};
2514
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002515#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002516 { \
2517 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2518 .count = initial_count, \
2519 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002520 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002521 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002522 }
2523
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002524#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2525
Allan Stephensc98da842016-11-11 15:45:03 -05002526/**
2527 * INTERNAL_HIDDEN @endcond
2528 */
2529
2530/**
2531 * @defgroup semaphore_apis Semaphore APIs
2532 * @ingroup kernel_apis
2533 * @{
2534 */
2535
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @param sem Address of the semaphore.
2542 * @param initial_count Initial semaphore count.
2543 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002544 *
2545 * @return N/A
2546 */
Andrew Boie99280232017-09-29 14:17:47 -07002547__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2548 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002549
2550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2556 *
2557 * @param sem Address of the semaphore.
2558 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002559 * or one of the special values K_NO_WAIT and K_FOREVER.
2560 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002561 * @note When porting code from the nanokernel legacy API to the new API, be
2562 * careful with the return value of this function. The return value is the
2563 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2564 * non-zero means failure, while the nano_sem_take family returns 1 for success
2565 * and 0 for failure.
2566 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002567 * @retval 0 Semaphore taken.
2568 * @retval -EBUSY Returned without waiting.
2569 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002570 */
Andrew Boie99280232017-09-29 14:17:47 -07002571__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572
2573/**
2574 * @brief Give a semaphore.
2575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * This routine gives @a sem, unless the semaphore is already at its maximum
2577 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002582 *
2583 * @return N/A
2584 */
Andrew Boie99280232017-09-29 14:17:47 -07002585__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587/**
2588 * @brief Reset a semaphore's count to zero.
2589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593 *
2594 * @return N/A
2595 */
Andrew Boie990bf162017-10-03 12:36:49 -07002596__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002597
2598static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599{
2600 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601}
2602
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002603/**
2604 * @brief Get a semaphore's count.
2605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 */
Andrew Boie990bf162017-10-03 12:36:49 -07002612__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002613
2614static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002615{
2616 return sem->count;
2617}
2618
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002624 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @param name Name of the semaphore.
2627 * @param initial_count Initial semaphore count.
2628 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002629 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002631 struct k_sem name \
2632 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002633 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634
Allan Stephensc98da842016-11-11 15:45:03 -05002635/**
2636 * @} end defgroup semaphore_apis
2637 */
2638
2639/**
2640 * @defgroup alert_apis Alert APIs
2641 * @ingroup kernel_apis
2642 * @{
2643 */
2644
Allan Stephens5eceb852016-11-16 10:16:30 -05002645/**
2646 * @typedef k_alert_handler_t
2647 * @brief Alert handler function type.
2648 *
2649 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002650 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002651 * and is only invoked if the alert has been initialized with one.
2652 *
2653 * @param alert Address of the alert.
2654 *
2655 * @return 0 if alert has been consumed; non-zero if alert should pend.
2656 */
2657typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002658
2659/**
2660 * @} end defgroup alert_apis
2661 */
2662
2663/**
2664 * @cond INTERNAL_HIDDEN
2665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002667#define K_ALERT_DEFAULT NULL
2668#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002670struct k_alert {
2671 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672 atomic_t send_count;
2673 struct k_work work_item;
2674 struct k_sem sem;
2675
Anas Nashif2f203c22016-12-18 06:57:45 -05002676 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677};
2678
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002679extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002681#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002683 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002685 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2686 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002687 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 }
2689
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002690#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2691
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002692/**
Allan Stephensc98da842016-11-11 15:45:03 -05002693 * INTERNAL_HIDDEN @endcond
2694 */
2695
2696/**
2697 * @addtogroup alert_apis
2698 * @{
2699 */
2700
2701/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002704 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002706 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * @param name Name of the alert.
2709 * @param alert_handler Action to take when alert is sent. Specify either
2710 * the address of a function to be invoked by the system workqueue
2711 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2712 * K_ALERT_DEFAULT (which causes the alert to pend).
2713 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002714 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002715#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002716 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002717 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002718 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002719 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002721/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @param alert Address of the alert.
2727 * @param handler Action to take when alert is sent. Specify either the address
2728 * of a function to be invoked by the system workqueue thread,
2729 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2730 * K_ALERT_DEFAULT (which causes the alert to pend).
2731 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
2733 * @return N/A
2734 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002735extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2736 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737
2738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002743 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2744 *
2745 * @param alert Address of the alert.
2746 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002747 * or one of the special values K_NO_WAIT and K_FOREVER.
2748 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002749 * @retval 0 Alert received.
2750 * @retval -EBUSY Returned without waiting.
2751 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752 */
Andrew Boie310e9872017-09-29 04:41:15 -07002753__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754
2755/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * This routine signals @a alert. The action specified for @a alert will
2759 * be taken, which may trigger the execution of an alert handler function
2760 * and/or cause the alert to pend (assuming the alert has not reached its
2761 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763 * @note Can be called by ISRs.
2764 *
2765 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766 *
2767 * @return N/A
2768 */
Andrew Boie310e9872017-09-29 04:41:15 -07002769__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770
2771/**
Allan Stephensc98da842016-11-11 15:45:03 -05002772 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773 */
2774
Allan Stephensc98da842016-11-11 15:45:03 -05002775/**
2776 * @cond INTERNAL_HIDDEN
2777 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778
2779struct k_msgq {
2780 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002781 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002782 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 char *buffer_start;
2784 char *buffer_end;
2785 char *read_ptr;
2786 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002787 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788
Anas Nashif2f203c22016-12-18 06:57:45 -05002789 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790};
2791
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002792#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793 { \
2794 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002795 .max_msgs = q_max_msgs, \
2796 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002798 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799 .read_ptr = q_buffer, \
2800 .write_ptr = q_buffer, \
2801 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002802 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803 }
2804
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002805#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2806
Peter Mitsis1da807e2016-10-06 11:36:59 -04002807/**
Allan Stephensc98da842016-11-11 15:45:03 -05002808 * INTERNAL_HIDDEN @endcond
2809 */
2810
2811/**
2812 * @defgroup msgq_apis Message Queue APIs
2813 * @ingroup kernel_apis
2814 * @{
2815 */
2816
2817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2821 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002822 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2823 * message is similarly aligned to this boundary, @a q_msg_size must also be
2824 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002826 * The message queue can be accessed outside the module where it is defined
2827 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002829 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @param q_name Name of the message queue.
2832 * @param q_msg_size Message size (in bytes).
2833 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002834 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002835 */
2836#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2837 static char __noinit __aligned(q_align) \
2838 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002839 struct k_msgq q_name \
2840 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002841 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002842 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844/**
2845 * @brief Initialize a message queue.
2846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * This routine initializes a message queue object, prior to its first use.
2848 *
Allan Stephensda827222016-11-09 14:23:58 -06002849 * The message queue's ring buffer must contain space for @a max_msgs messages,
2850 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2851 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2852 * that each message is similarly aligned to this boundary, @a q_msg_size
2853 * must also be a multiple of N.
2854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * @param q Address of the message queue.
2856 * @param buffer Pointer to ring buffer that holds queued messages.
2857 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002858 * @param max_msgs Maximum number of messages that can be queued.
2859 *
2860 * @return N/A
2861 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002862__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2863 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002864
2865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002869 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002870 * @note Can be called by ISRs.
2871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * @param q Address of the message queue.
2873 * @param data Pointer to the message.
2874 * @param timeout Waiting period to add the message (in milliseconds),
2875 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002876 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002877 * @retval 0 Message sent.
2878 * @retval -ENOMSG Returned without waiting or queue purged.
2879 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002880 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002881__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002882
2883/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * This routine receives a message from message queue @a q in a "first in,
2887 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002888 *
Allan Stephensc98da842016-11-11 15:45:03 -05002889 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param q Address of the message queue.
2892 * @param data Address of area to hold the received message.
2893 * @param timeout Waiting period to receive the message (in milliseconds),
2894 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002895 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002896 * @retval 0 Message received.
2897 * @retval -ENOMSG Returned without waiting.
2898 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002899 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002900__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002901
2902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * This routine discards all unreceived messages in a message queue's ring
2906 * buffer. Any threads that are blocked waiting to send a message to the
2907 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002910 *
2911 * @return N/A
2912 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002913__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914
Peter Mitsis67be2492016-10-07 11:44:34 -04002915/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * This routine returns the number of unused entries in a message queue's
2919 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * @param q Address of the message queue.
2922 *
2923 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002924 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002925__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2926
2927static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002928{
2929 return q->max_msgs - q->used_msgs;
2930}
2931
Peter Mitsisd7a37502016-10-13 11:37:40 -04002932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @param q Address of the message queue.
2938 *
2939 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002940 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002941__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2942
2943static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944{
2945 return q->used_msgs;
2946}
2947
Allan Stephensc98da842016-11-11 15:45:03 -05002948/**
2949 * @} end defgroup msgq_apis
2950 */
2951
2952/**
2953 * @defgroup mem_pool_apis Memory Pool APIs
2954 * @ingroup kernel_apis
2955 * @{
2956 */
2957
Andy Ross73cb9582017-05-09 10:42:39 -07002958/* Note on sizing: the use of a 20 bit field for block means that,
2959 * assuming a reasonable minimum block size of 16 bytes, we're limited
2960 * to 16M of memory managed by a single pool. Long term it would be
2961 * good to move to a variable bit size based on configuration.
2962 */
2963struct k_mem_block_id {
2964 u32_t pool : 8;
2965 u32_t level : 4;
2966 u32_t block : 20;
2967};
2968
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002971 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972};
2973
Allan Stephensc98da842016-11-11 15:45:03 -05002974/**
2975 * @} end defgroup mem_pool_apis
2976 */
2977
2978/**
2979 * @defgroup mailbox_apis Mailbox APIs
2980 * @ingroup kernel_apis
2981 * @{
2982 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983
2984struct k_mbox_msg {
2985 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002986 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002988 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002990 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991 /** sender's message data buffer */
2992 void *tx_data;
2993 /** internal use only - needed for legacy API support */
2994 void *_rx_data;
2995 /** message data block descriptor */
2996 struct k_mem_block tx_block;
2997 /** source thread id */
2998 k_tid_t rx_source_thread;
2999 /** target thread id */
3000 k_tid_t tx_target_thread;
3001 /** internal use only - thread waiting on send (may be a dummy) */
3002 k_tid_t _syncing_thread;
3003#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3004 /** internal use only - semaphore used during asynchronous send */
3005 struct k_sem *_async_sem;
3006#endif
3007};
3008
Allan Stephensc98da842016-11-11 15:45:03 -05003009/**
3010 * @cond INTERNAL_HIDDEN
3011 */
3012
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013struct k_mbox {
3014 _wait_q_t tx_msg_queue;
3015 _wait_q_t rx_msg_queue;
3016
Anas Nashif2f203c22016-12-18 06:57:45 -05003017 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018};
3019
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003020#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021 { \
3022 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3023 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003024 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025 }
3026
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003027#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3028
Peter Mitsis12092702016-10-14 12:57:23 -04003029/**
Allan Stephensc98da842016-11-11 15:45:03 -05003030 * INTERNAL_HIDDEN @endcond
3031 */
3032
3033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003037 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003038 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003040 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003043 struct k_mbox name \
3044 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003045 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046
Peter Mitsis12092702016-10-14 12:57:23 -04003047/**
3048 * @brief Initialize a mailbox.
3049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * This routine initializes a mailbox object, prior to its first use.
3051 *
3052 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003053 *
3054 * @return N/A
3055 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056extern void k_mbox_init(struct k_mbox *mbox);
3057
Peter Mitsis12092702016-10-14 12:57:23 -04003058/**
3059 * @brief Send a mailbox message in a synchronous manner.
3060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * This routine sends a message to @a mbox and waits for a receiver to both
3062 * receive and process it. The message data may be in a buffer, in a memory
3063 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * @param mbox Address of the mailbox.
3066 * @param tx_msg Address of the transmit message descriptor.
3067 * @param timeout Waiting period for the message to be received (in
3068 * milliseconds), or one of the special values K_NO_WAIT
3069 * and K_FOREVER. Once the message has been received,
3070 * this routine waits as long as necessary for the message
3071 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003072 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003073 * @retval 0 Message sent.
3074 * @retval -ENOMSG Returned without waiting.
3075 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003076 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003077extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003078 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003079
Peter Mitsis12092702016-10-14 12:57:23 -04003080/**
3081 * @brief Send a mailbox message in an asynchronous manner.
3082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * This routine sends a message to @a mbox without waiting for a receiver
3084 * to process it. The message data may be in a buffer, in a memory pool block,
3085 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3086 * will be given when the message has been both received and completely
3087 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @param mbox Address of the mailbox.
3090 * @param tx_msg Address of the transmit message descriptor.
3091 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003092 *
3093 * @return N/A
3094 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003095extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096 struct k_sem *sem);
3097
Peter Mitsis12092702016-10-14 12:57:23 -04003098/**
3099 * @brief Receive a mailbox message.
3100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * This routine receives a message from @a mbox, then optionally retrieves
3102 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * @param mbox Address of the mailbox.
3105 * @param rx_msg Address of the receive message descriptor.
3106 * @param buffer Address of the buffer to receive data, or NULL to defer data
3107 * retrieval and message disposal until later.
3108 * @param timeout Waiting period for a message to be received (in
3109 * milliseconds), or one of the special values K_NO_WAIT
3110 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003111 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003112 * @retval 0 Message received.
3113 * @retval -ENOMSG Returned without waiting.
3114 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003115 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003116extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003117 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003118
3119/**
3120 * @brief Retrieve mailbox message data into a buffer.
3121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * This routine completes the processing of a received message by retrieving
3123 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003124 *
3125 * Alternatively, this routine can be used to dispose of a received message
3126 * without retrieving its data.
3127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * @param rx_msg Address of the receive message descriptor.
3129 * @param buffer Address of the buffer to receive data, or NULL to discard
3130 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003131 *
3132 * @return N/A
3133 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003134extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003135
3136/**
3137 * @brief Retrieve mailbox message data into a memory pool block.
3138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * This routine completes the processing of a received message by retrieving
3140 * its data into a memory pool block, then disposing of the message.
3141 * The memory pool block that results from successful retrieval must be
3142 * returned to the pool once the data has been processed, even in cases
3143 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003144 *
3145 * Alternatively, this routine can be used to dispose of a received message
3146 * without retrieving its data. In this case there is no need to return a
3147 * memory pool block to the pool.
3148 *
3149 * This routine allocates a new memory pool block for the data only if the
3150 * data is not already in one. If a new block cannot be allocated, the routine
3151 * returns a failure code and the received message is left unchanged. This
3152 * permits the caller to reattempt data retrieval at a later time or to dispose
3153 * of the received message without retrieving its data.
3154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @param rx_msg Address of a receive message descriptor.
3156 * @param pool Address of memory pool, or NULL to discard data.
3157 * @param block Address of the area to hold memory pool block info.
3158 * @param timeout Waiting period to wait for a memory pool block (in
3159 * milliseconds), or one of the special values K_NO_WAIT
3160 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003161 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003162 * @retval 0 Data retrieved.
3163 * @retval -ENOMEM Returned without waiting.
3164 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003165 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003166extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003167 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003168 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169
Allan Stephensc98da842016-11-11 15:45:03 -05003170/**
3171 * @} end defgroup mailbox_apis
3172 */
3173
3174/**
3175 * @cond INTERNAL_HIDDEN
3176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177
3178struct k_pipe {
3179 unsigned char *buffer; /* Pipe buffer: may be NULL */
3180 size_t size; /* Buffer size */
3181 size_t bytes_used; /* # bytes used in buffer */
3182 size_t read_index; /* Where in buffer to read from */
3183 size_t write_index; /* Where in buffer to write */
3184
3185 struct {
3186 _wait_q_t readers; /* Reader wait queue */
3187 _wait_q_t writers; /* Writer wait queue */
3188 } wait_q;
3189
Anas Nashif2f203c22016-12-18 06:57:45 -05003190 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191};
3192
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003193#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194 { \
3195 .buffer = pipe_buffer, \
3196 .size = pipe_buffer_size, \
3197 .bytes_used = 0, \
3198 .read_index = 0, \
3199 .write_index = 0, \
3200 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3201 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003202 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 }
3204
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003205#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3206
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003207/**
Allan Stephensc98da842016-11-11 15:45:03 -05003208 * INTERNAL_HIDDEN @endcond
3209 */
3210
3211/**
3212 * @defgroup pipe_apis Pipe APIs
3213 * @ingroup kernel_apis
3214 * @{
3215 */
3216
3217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003221 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003222 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * @param name Name of the pipe.
3225 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3226 * or zero if no ring buffer is used.
3227 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003228 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003229#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3230 static unsigned char __noinit __aligned(pipe_align) \
3231 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003232 struct k_pipe name \
3233 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003234 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * This routine initializes a pipe object, prior to its first use.
3240 *
3241 * @param pipe Address of the pipe.
3242 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3243 * is used.
3244 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3245 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 *
3247 * @return N/A
3248 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003249__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3250 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251
3252/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * @param pipe Address of the pipe.
3258 * @param data Address of data to write.
3259 * @param bytes_to_write Size of data (in bytes).
3260 * @param bytes_written Address of area to hold the number of bytes written.
3261 * @param min_xfer Minimum number of bytes to write.
3262 * @param timeout Waiting period to wait for the data to be written (in
3263 * milliseconds), or one of the special values K_NO_WAIT
3264 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003266 * @retval 0 At least @a min_xfer bytes of data were written.
3267 * @retval -EIO Returned without waiting; zero data bytes were written.
3268 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003271__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3272 size_t bytes_to_write, size_t *bytes_written,
3273 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274
3275/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @param pipe Address of the pipe.
3281 * @param data Address to place the data read from pipe.
3282 * @param bytes_to_read Maximum number of data bytes to read.
3283 * @param bytes_read Address of area to hold the number of bytes read.
3284 * @param min_xfer Minimum number of data bytes to read.
3285 * @param timeout Waiting period to wait for the data to be read (in
3286 * milliseconds), or one of the special values K_NO_WAIT
3287 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003289 * @retval 0 At least @a min_xfer bytes of data were read.
3290 * @retval -EIO Returned without waiting; zero data bytes were read.
3291 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003294__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3295 size_t bytes_to_read, size_t *bytes_read,
3296 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297
3298/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003301 * This routine writes the data contained in a memory block to @a pipe.
3302 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306 * @param block Memory block containing data to send
3307 * @param size Number of data bytes in memory block to send
3308 * @param sem Semaphore to signal upon completion (else NULL)
3309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311 */
3312extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3313 size_t size, struct k_sem *sem);
3314
3315/**
Allan Stephensc98da842016-11-11 15:45:03 -05003316 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317 */
3318
Allan Stephensc98da842016-11-11 15:45:03 -05003319/**
3320 * @cond INTERNAL_HIDDEN
3321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003323struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003325 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003326 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327 char *buffer;
3328 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003329 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330
Anas Nashif2f203c22016-12-18 06:57:45 -05003331 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332};
3333
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003334#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003335 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336 { \
3337 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003338 .num_blocks = slab_num_blocks, \
3339 .block_size = slab_block_size, \
3340 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003341 .free_list = NULL, \
3342 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003343 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344 }
3345
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003346#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3347
3348
Peter Mitsis578f9112016-10-07 13:50:31 -04003349/**
Allan Stephensc98da842016-11-11 15:45:03 -05003350 * INTERNAL_HIDDEN @endcond
3351 */
3352
3353/**
3354 * @defgroup mem_slab_apis Memory Slab APIs
3355 * @ingroup kernel_apis
3356 * @{
3357 */
3358
3359/**
Allan Stephensda827222016-11-09 14:23:58 -06003360 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003361 *
Allan Stephensda827222016-11-09 14:23:58 -06003362 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003364 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3365 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003366 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003367 *
Allan Stephensda827222016-11-09 14:23:58 -06003368 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003370 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003371 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003373 * @param name Name of the memory slab.
3374 * @param slab_block_size Size of each memory block (in bytes).
3375 * @param slab_num_blocks Number memory blocks.
3376 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003377 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003378#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3379 char __noinit __aligned(slab_align) \
3380 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3381 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003382 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003383 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003384 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003386/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003387 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003390 *
Allan Stephensda827222016-11-09 14:23:58 -06003391 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3392 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3393 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3394 * To ensure that each memory block is similarly aligned to this boundary,
3395 * @a slab_block_size must also be a multiple of N.
3396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * @param slab Address of the memory slab.
3398 * @param buffer Pointer to buffer used for the memory blocks.
3399 * @param block_size Size of each memory block (in bytes).
3400 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003401 *
3402 * @return N/A
3403 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003404extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003405 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003406
3407/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @param slab Address of the memory slab.
3413 * @param mem Pointer to block address area.
3414 * @param timeout Maximum time to wait for operation to complete
3415 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3416 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003417 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003418 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003420 * @retval -ENOMEM Returned without waiting.
3421 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003422 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003423extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003424 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003425
3426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * This routine releases a previously allocated memory block back to its
3430 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @param slab Address of the memory slab.
3433 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003434 *
3435 * @return N/A
3436 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003437extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003438
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003439/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * This routine gets the number of memory blocks that are currently
3443 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003445 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003447 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003448 */
Kumar Galacc334c72017-04-21 10:55:34 -05003449static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003451 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003452}
3453
Peter Mitsisc001aa82016-10-13 13:53:37 -04003454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * This routine gets the number of memory blocks that are currently
3458 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003463 */
Kumar Galacc334c72017-04-21 10:55:34 -05003464static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003465{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003466 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003467}
3468
Allan Stephensc98da842016-11-11 15:45:03 -05003469/**
3470 * @} end defgroup mem_slab_apis
3471 */
3472
3473/**
3474 * @cond INTERNAL_HIDDEN
3475 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003476
Andy Ross73cb9582017-05-09 10:42:39 -07003477struct k_mem_pool_lvl {
3478 union {
3479 u32_t *bits_p;
3480 u32_t bits;
3481 };
3482 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003483};
3484
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003485struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003486 void *buf;
3487 size_t max_sz;
3488 u16_t n_max;
3489 u8_t n_levels;
3490 u8_t max_inline_level;
3491 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003492 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493};
3494
Andy Ross73cb9582017-05-09 10:42:39 -07003495#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003496
Andy Ross73cb9582017-05-09 10:42:39 -07003497#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3498
3499#define _MPOOL_LVLS(maxsz, minsz) \
3500 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3501 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3502 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3503 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3504 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3505 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3506 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3507 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3508 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3509 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3510 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3511 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3512 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3513 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3514 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3515 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3516
3517/* Rounds the needed bits up to integer multiples of u32_t */
3518#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3519 ((((n_max) << (2*(l))) + 31) / 32)
3520
3521/* One word gets stored free unioned with the pointer, otherwise the
3522 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003523 */
Andy Ross73cb9582017-05-09 10:42:39 -07003524#define _MPOOL_LBIT_WORDS(n_max, l) \
3525 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3526 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003527
Andy Ross73cb9582017-05-09 10:42:39 -07003528/* How many bytes for the bitfields of a single level? */
3529#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3530 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3531 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003532
Andy Ross73cb9582017-05-09 10:42:39 -07003533/* Size of the bitmap array that follows the buffer in allocated memory */
3534#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3535 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3536 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3537 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3538 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3539 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3540 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3541 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3542 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3543 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3544 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3545 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3546 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3547 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3548 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3549 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3550 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003551
3552/**
Allan Stephensc98da842016-11-11 15:45:03 -05003553 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003554 */
3555
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003556/**
Allan Stephensc98da842016-11-11 15:45:03 -05003557 * @addtogroup mem_pool_apis
3558 * @{
3559 */
3560
3561/**
3562 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003564 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3565 * long. The memory pool allows blocks to be repeatedly partitioned into
3566 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003567 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003568 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003569 * If the pool is to be accessed outside the module where it is defined, it
3570 * can be declared via
3571 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003572 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003574 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003575 * @param minsz Size of the smallest blocks in the pool (in bytes).
3576 * @param maxsz Size of the largest blocks in the pool (in bytes).
3577 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003579 */
Andy Ross73cb9582017-05-09 10:42:39 -07003580#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3581 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3582 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3583 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3584 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3585 .buf = _mpool_buf_##name, \
3586 .max_sz = maxsz, \
3587 .n_max = nmax, \
3588 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3589 .levels = _mpool_lvls_##name, \
3590 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003591
Peter Mitsis937042c2016-10-13 13:18:26 -04003592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003593 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * @param pool Address of the memory pool.
3598 * @param block Pointer to block descriptor for the allocated memory.
3599 * @param size Amount of memory to allocate (in bytes).
3600 * @param timeout Maximum time to wait for operation to complete
3601 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3602 * or K_FOREVER to wait as long as necessary.
3603 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003604 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003606 * @retval -ENOMEM Returned without waiting.
3607 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003608 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003609extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003610 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003611
3612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * This routine releases a previously allocated memory block back to its
3616 * memory pool.
3617 *
3618 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003619 *
3620 * @return N/A
3621 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003622extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003623
3624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003626 *
Andy Ross73cb9582017-05-09 10:42:39 -07003627 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003628 *
Andy Ross73cb9582017-05-09 10:42:39 -07003629 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003630 *
3631 * @return N/A
3632 */
Andy Ross73cb9582017-05-09 10:42:39 -07003633static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003634
3635/**
Allan Stephensc98da842016-11-11 15:45:03 -05003636 * @} end addtogroup mem_pool_apis
3637 */
3638
3639/**
3640 * @defgroup heap_apis Heap Memory Pool APIs
3641 * @ingroup kernel_apis
3642 * @{
3643 */
3644
3645/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003646 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003648 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003649 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003654 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003655extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003656
3657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003659 *
3660 * This routine provides traditional free() semantics. The memory being
3661 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003662 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003663 * If @a ptr is NULL, no operation is performed.
3664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003665 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003666 *
3667 * @return N/A
3668 */
3669extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670
Allan Stephensc98da842016-11-11 15:45:03 -05003671/**
3672 * @} end defgroup heap_apis
3673 */
3674
Benjamin Walshacc68c12017-01-29 18:57:45 -05003675/* polling API - PRIVATE */
3676
Benjamin Walshb0179862017-02-02 16:39:57 -05003677#ifdef CONFIG_POLL
3678#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3679#else
3680#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3681#endif
3682
Benjamin Walshacc68c12017-01-29 18:57:45 -05003683/* private - implementation data created as needed, per-type */
3684struct _poller {
3685 struct k_thread *thread;
3686};
3687
3688/* private - types bit positions */
3689enum _poll_types_bits {
3690 /* can be used to ignore an event */
3691 _POLL_TYPE_IGNORE,
3692
3693 /* to be signaled by k_poll_signal() */
3694 _POLL_TYPE_SIGNAL,
3695
3696 /* semaphore availability */
3697 _POLL_TYPE_SEM_AVAILABLE,
3698
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003699 /* queue/fifo/lifo data availability */
3700 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003701
3702 _POLL_NUM_TYPES
3703};
3704
3705#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3706
3707/* private - states bit positions */
3708enum _poll_states_bits {
3709 /* default state when creating event */
3710 _POLL_STATE_NOT_READY,
3711
Benjamin Walshacc68c12017-01-29 18:57:45 -05003712 /* signaled by k_poll_signal() */
3713 _POLL_STATE_SIGNALED,
3714
3715 /* semaphore is available */
3716 _POLL_STATE_SEM_AVAILABLE,
3717
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003718 /* data is available to read on queue/fifo/lifo */
3719 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003720
3721 _POLL_NUM_STATES
3722};
3723
3724#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3725
3726#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003727 (32 - (0 \
3728 + 8 /* tag */ \
3729 + _POLL_NUM_TYPES \
3730 + _POLL_NUM_STATES \
3731 + 1 /* modes */ \
3732 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003733
3734#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3735#error overflow of 32-bit word in struct k_poll_event
3736#endif
3737
3738/* end of polling API - PRIVATE */
3739
3740
3741/**
3742 * @defgroup poll_apis Async polling APIs
3743 * @ingroup kernel_apis
3744 * @{
3745 */
3746
3747/* Public polling API */
3748
3749/* public - values for k_poll_event.type bitfield */
3750#define K_POLL_TYPE_IGNORE 0
3751#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3752#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003753#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3754#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003755
3756/* public - polling modes */
3757enum k_poll_modes {
3758 /* polling thread does not take ownership of objects when available */
3759 K_POLL_MODE_NOTIFY_ONLY = 0,
3760
3761 K_POLL_NUM_MODES
3762};
3763
3764/* public - values for k_poll_event.state bitfield */
3765#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003766#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3767#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003768#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3769#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003770
3771/* public - poll signal object */
3772struct k_poll_signal {
3773 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003774 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003775
3776 /*
3777 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3778 * user resets it to 0.
3779 */
3780 unsigned int signaled;
3781
3782 /* custom result value passed to k_poll_signal() if needed */
3783 int result;
3784};
3785
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003786#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003788 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003789 .signaled = 0, \
3790 .result = 0, \
3791 }
3792
3793struct k_poll_event {
3794 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003795 sys_dnode_t _node;
3796
3797 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003798 struct _poller *poller;
3799
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003800 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003801 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003802
Benjamin Walshacc68c12017-01-29 18:57:45 -05003803 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003804 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003805
3806 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003807 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003808
3809 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003810 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811
3812 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003813 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003814
3815 /* per-type data */
3816 union {
3817 void *obj;
3818 struct k_poll_signal *signal;
3819 struct k_sem *sem;
3820 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003821 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003822 };
3823};
3824
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003825#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003826 { \
3827 .poller = NULL, \
3828 .type = event_type, \
3829 .state = K_POLL_STATE_NOT_READY, \
3830 .mode = event_mode, \
3831 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003832 { .obj = event_obj }, \
3833 }
3834
3835#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3836 event_tag) \
3837 { \
3838 .type = event_type, \
3839 .tag = event_tag, \
3840 .state = K_POLL_STATE_NOT_READY, \
3841 .mode = event_mode, \
3842 .unused = 0, \
3843 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003844 }
3845
3846/**
3847 * @brief Initialize one struct k_poll_event instance
3848 *
3849 * After this routine is called on a poll event, the event it ready to be
3850 * placed in an event array to be passed to k_poll().
3851 *
3852 * @param event The event to initialize.
3853 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3854 * values. Only values that apply to the same object being polled
3855 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3856 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003857 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858 * @param obj Kernel object or poll signal.
3859 *
3860 * @return N/A
3861 */
3862
Kumar Galacc334c72017-04-21 10:55:34 -05003863extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003864 int mode, void *obj);
3865
3866/**
3867 * @brief Wait for one or many of multiple poll events to occur
3868 *
3869 * This routine allows a thread to wait concurrently for one or many of
3870 * multiple poll events to have occurred. Such events can be a kernel object
3871 * being available, like a semaphore, or a poll signal event.
3872 *
3873 * When an event notifies that a kernel object is available, the kernel object
3874 * is not "given" to the thread calling k_poll(): it merely signals the fact
3875 * that the object was available when the k_poll() call was in effect. Also,
3876 * all threads trying to acquire an object the regular way, i.e. by pending on
3877 * the object, have precedence over the thread polling on the object. This
3878 * means that the polling thread will never get the poll event on an object
3879 * until the object becomes available and its pend queue is empty. For this
3880 * reason, the k_poll() call is more effective when the objects being polled
3881 * only have one thread, the polling thread, trying to acquire them.
3882 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003883 * When k_poll() returns 0, the caller should loop on all the events that were
3884 * passed to k_poll() and check the state field for the values that were
3885 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003886 *
3887 * Before being reused for another call to k_poll(), the user has to reset the
3888 * state field to K_POLL_STATE_NOT_READY.
3889 *
3890 * @param events An array of pointers to events to be polled for.
3891 * @param num_events The number of events in the array.
3892 * @param timeout Waiting period for an event to be ready (in milliseconds),
3893 * or one of the special values K_NO_WAIT and K_FOREVER.
3894 *
3895 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003896 * @retval -EAGAIN Waiting period timed out.
3897 */
3898
3899extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003900 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003901
3902/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003903 * @brief Initialize a poll signal object.
3904 *
3905 * Ready a poll signal object to be signaled via k_poll_signal().
3906 *
3907 * @param signal A poll signal.
3908 *
3909 * @return N/A
3910 */
3911
3912extern void k_poll_signal_init(struct k_poll_signal *signal);
3913
3914/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003915 * @brief Signal a poll signal object.
3916 *
3917 * This routine makes ready a poll signal, which is basically a poll event of
3918 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3919 * made ready to run. A @a result value can be specified.
3920 *
3921 * The poll signal contains a 'signaled' field that, when set by
3922 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3923 * be reset by the user before being passed again to k_poll() or k_poll() will
3924 * consider it being signaled, and will return immediately.
3925 *
3926 * @param signal A poll signal.
3927 * @param result The value to store in the result field of the signal.
3928 *
3929 * @retval 0 The signal was delivered successfully.
3930 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3931 */
3932
3933extern int k_poll_signal(struct k_poll_signal *signal, int result);
3934
3935/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003936extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003937
3938/**
3939 * @} end defgroup poll_apis
3940 */
3941
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003942/**
3943 * @brief Make the CPU idle.
3944 *
3945 * This function makes the CPU idle until an event wakes it up.
3946 *
3947 * In a regular system, the idle thread should be the only thread responsible
3948 * for making the CPU idle and triggering any type of power management.
3949 * However, in some more constrained systems, such as a single-threaded system,
3950 * the only thread would be responsible for this if needed.
3951 *
3952 * @return N/A
3953 */
3954extern void k_cpu_idle(void);
3955
3956/**
3957 * @brief Make the CPU idle in an atomic fashion.
3958 *
3959 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3960 * must be done atomically before making the CPU idle.
3961 *
3962 * @param key Interrupt locking key obtained from irq_lock().
3963 *
3964 * @return N/A
3965 */
3966extern void k_cpu_atomic_idle(unsigned int key);
3967
Kumar Galacc334c72017-04-21 10:55:34 -05003968extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003969
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003970#include <arch/cpu.h>
3971
Andrew Boiecdb94d62017-04-18 15:22:05 -07003972#ifdef _ARCH_EXCEPT
3973/* This archtecture has direct support for triggering a CPU exception */
3974#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3975#else
3976
3977#include <misc/printk.h>
3978
3979/* NOTE: This is the implementation for arches that do not implement
3980 * _ARCH_EXCEPT() to generate a real CPU exception.
3981 *
3982 * We won't have a real exception frame to determine the PC value when
3983 * the oops occurred, so print file and line number before we jump into
3984 * the fatal error handler.
3985 */
3986#define _k_except_reason(reason) do { \
3987 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3988 _NanoFatalErrorHandler(reason, &_default_esf); \
3989 CODE_UNREACHABLE; \
3990 } while (0)
3991
3992#endif /* _ARCH__EXCEPT */
3993
3994/**
3995 * @brief Fatally terminate a thread
3996 *
3997 * This should be called when a thread has encountered an unrecoverable
3998 * runtime condition and needs to terminate. What this ultimately
3999 * means is determined by the _fatal_error_handler() implementation, which
4000 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4001 *
4002 * If this is called from ISR context, the default system fatal error handler
4003 * will treat it as an unrecoverable system error, just like k_panic().
4004 */
4005#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4006
4007/**
4008 * @brief Fatally terminate the system
4009 *
4010 * This should be called when the Zephyr kernel has encountered an
4011 * unrecoverable runtime condition and needs to terminate. What this ultimately
4012 * means is determined by the _fatal_error_handler() implementation, which
4013 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4014 */
4015#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4016
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004017/*
4018 * private APIs that are utilized by one or more public APIs
4019 */
4020
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004021#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004022extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004023#else
4024#define _init_static_threads() do { } while ((0))
4025#endif
4026
4027extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004028extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004029
Andrew Boiedc5d9352017-06-02 12:56:47 -07004030/* arch/cpu.h may declare an architecture or platform-specific macro
4031 * for properly declaring stacks, compatible with MMU/MPU constraints if
4032 * enabled
4033 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004034
4035/**
4036 * @brief Obtain an extern reference to a stack
4037 *
4038 * This macro properly brings the symbol of a thread stack declared
4039 * elsewhere into scope.
4040 *
4041 * @param sym Thread stack symbol name
4042 */
4043#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4044
Andrew Boiedc5d9352017-06-02 12:56:47 -07004045#ifdef _ARCH_THREAD_STACK_DEFINE
4046#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4047#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4048 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4049#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4050#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004051static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004052{
4053 return _ARCH_THREAD_STACK_BUFFER(sym);
4054}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004055#else
4056/**
4057 * @brief Declare a toplevel thread stack memory region
4058 *
4059 * This declares a region of memory suitable for use as a thread's stack.
4060 *
4061 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4062 * 'noinit' section so that it isn't zeroed at boot
4063 *
Andrew Boie507852a2017-07-25 18:47:07 -07004064 * The declared symbol will always be a k_thread_stack_t which can be passed to
4065 * k_thread_create, but should otherwise not be manipulated. If the buffer
4066 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004067 *
4068 * It is legal to precede this definition with the 'static' keyword.
4069 *
4070 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4071 * parameter of k_thread_create(), it may not be the same as the
4072 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4073 *
4074 * @param sym Thread stack symbol name
4075 * @param size Size of the stack memory region
4076 */
4077#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004078 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004079
4080/**
4081 * @brief Declare a toplevel array of thread stack memory regions
4082 *
4083 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4084 * definition for additional details and constraints.
4085 *
4086 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4087 * 'noinit' section so that it isn't zeroed at boot
4088 *
4089 * @param sym Thread stack symbol name
4090 * @param nmemb Number of stacks to declare
4091 * @param size Size of the stack memory region
4092 */
4093
4094#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004095 struct _k_thread_stack_element __noinit \
4096 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004097
4098/**
4099 * @brief Declare an embedded stack memory region
4100 *
4101 * Used for stacks embedded within other data structures. Use is highly
4102 * discouraged but in some cases necessary. For memory protection scenarios,
4103 * it is very important that any RAM preceding this member not be writable
4104 * by threads else a stack overflow will lead to silent corruption. In other
4105 * words, the containing data structure should live in RAM owned by the kernel.
4106 *
4107 * @param sym Thread stack symbol name
4108 * @param size Size of the stack memory region
4109 */
4110#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004111 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004112
4113/**
4114 * @brief Return the size in bytes of a stack memory region
4115 *
4116 * Convenience macro for passing the desired stack size to k_thread_create()
4117 * since the underlying implementation may actually create something larger
4118 * (for instance a guard area).
4119 *
4120 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004121 * passed to K_THREAD_STACK_DEFINE.
4122 *
4123 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4124 * it is not guaranteed to return the original value since each array
4125 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004126 *
4127 * @param sym Stack memory symbol
4128 * @return Size of the stack
4129 */
4130#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4131
4132/**
4133 * @brief Get a pointer to the physical stack buffer
4134 *
4135 * Convenience macro to get at the real underlying stack buffer used by
4136 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4137 * This is really only intended for diagnostic tools which want to examine
4138 * stack memory contents.
4139 *
4140 * @param sym Declared stack symbol name
4141 * @return The buffer itself, a char *
4142 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004143static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004144{
4145 return (char *)sym;
4146}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004147
4148#endif /* _ARCH_DECLARE_STACK */
4149
Chunlin Hane9c97022017-07-07 20:29:30 +08004150/**
4151 * @defgroup mem_domain_apis Memory domain APIs
4152 * @ingroup kernel_apis
4153 * @{
4154 */
4155
4156/** @def MEM_PARTITION_ENTRY
4157 * @brief Used to declare a memory partition entry
4158 */
4159#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4160 {\
4161 .start = _start, \
4162 .size = _size, \
4163 .attr = _attr, \
4164 }
4165
4166/** @def K_MEM_PARTITION_DEFINE
4167 * @brief Used to declare a memory partition
4168 */
4169#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4170#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4171 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4172 struct k_mem_partition name = \
4173 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4174#else
4175#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4176 struct k_mem_partition name = \
4177 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4178#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4179
4180
4181/* memory partition */
4182struct k_mem_partition {
4183 /* start address of memory partition */
4184 u32_t start;
4185 /* size of memory partition */
4186 u32_t size;
4187 /* attribute of memory partition */
4188 u32_t attr;
4189};
4190
4191#if defined(CONFIG_USERSPACE)
4192/* memory domian */
4193struct k_mem_domain {
4194 /* number of partitions in the domain */
4195 u32_t num_partitions;
4196 /* partitions in the domain */
4197 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4198 /* domain q */
4199 sys_dlist_t mem_domain_q;
4200};
4201#endif /* CONFIG_USERSPACE */
4202
4203/**
4204 * @brief Initialize a memory domain.
4205 *
4206 * Initialize a memory domain with given name and memory partitions.
4207 *
4208 * @param domain The memory domain to be initialized.
4209 * @param num_parts The number of array items of "parts" parameter.
4210 * @param parts An array of pointers to the memory partitions. Can be NULL
4211 * if num_parts is zero.
4212 */
4213
4214extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4215 struct k_mem_partition *parts[]);
4216/**
4217 * @brief Destroy a memory domain.
4218 *
4219 * Destroy a memory domain.
4220 *
4221 * @param domain The memory domain to be destroyed.
4222 */
4223
4224extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4225
4226/**
4227 * @brief Add a memory partition into a memory domain.
4228 *
4229 * Add a memory partition into a memory domain.
4230 *
4231 * @param domain The memory domain to be added a memory partition.
4232 * @param part The memory partition to be added
4233 */
4234
4235extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4236 struct k_mem_partition *part);
4237
4238/**
4239 * @brief Remove a memory partition from a memory domain.
4240 *
4241 * Remove a memory partition from a memory domain.
4242 *
4243 * @param domain The memory domain to be removed a memory partition.
4244 * @param part The memory partition to be removed
4245 */
4246
4247extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4248 struct k_mem_partition *part);
4249
4250/**
4251 * @brief Add a thread into a memory domain.
4252 *
4253 * Add a thread into a memory domain.
4254 *
4255 * @param domain The memory domain that the thread is going to be added into.
4256 * @param thread ID of thread going to be added into the memory domain.
4257 */
4258
4259extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4260 k_tid_t thread);
4261
4262/**
4263 * @brief Remove a thread from its memory domain.
4264 *
4265 * Remove a thread from its memory domain.
4266 *
4267 * @param thread ID of thread going to be removed from its memory domain.
4268 */
4269
4270extern void k_mem_domain_remove_thread(k_tid_t thread);
4271
4272/**
4273 * @} end defgroup mem_domain_apis
4274 */
4275
Andrew Boie756f9072017-10-10 16:01:49 -07004276/**
4277 * @brief Emit a character buffer to the console device
4278 *
4279 * @param c String of characters to print
4280 * @param n The length of the string
4281 */
4282__syscall void k_str_out(char *c, size_t n);
4283
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004284#ifdef __cplusplus
4285}
4286#endif
4287
Andrew Boiee004dec2016-11-07 09:01:19 -08004288#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4289/*
4290 * Define new and delete operators.
4291 * At this moment, the operators do nothing since objects are supposed
4292 * to be statically allocated.
4293 */
4294inline void operator delete(void *ptr)
4295{
4296 (void)ptr;
4297}
4298
4299inline void operator delete[](void *ptr)
4300{
4301 (void)ptr;
4302}
4303
4304inline void *operator new(size_t size)
4305{
4306 (void)size;
4307 return NULL;
4308}
4309
4310inline void *operator new[](size_t size)
4311{
4312 (void)size;
4313 return NULL;
4314}
4315
4316/* Placement versions of operator new and delete */
4317inline void operator delete(void *ptr1, void *ptr2)
4318{
4319 (void)ptr1;
4320 (void)ptr2;
4321}
4322
4323inline void operator delete[](void *ptr1, void *ptr2)
4324{
4325 (void)ptr1;
4326 (void)ptr2;
4327}
4328
4329inline void *operator new(size_t size, void *ptr)
4330{
4331 (void)size;
4332 return ptr;
4333}
4334
4335inline void *operator new[](size_t size, void *ptr)
4336{
4337 (void)size;
4338 return ptr;
4339}
4340
4341#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4342
Andrew Boiefa94ee72017-09-28 16:54:35 -07004343#include <syscalls/kernel.h>
4344
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004345#endif /* !_ASMLANGUAGE */
4346
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004347#endif /* _kernel__h_ */