blob: 7cdad5cf46c687a0d19d0305bd28802ef294ded3 [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>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070029#include <random/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>
Andrew Boie43263fc2017-11-02 11:07:31 -070032#include <misc/printk.h>
33#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034
35#ifdef __cplusplus
36extern "C" {
37#endif
38
Anas Nashifbbb157d2017-01-15 08:46:31 -050039/**
40 * @brief Kernel APIs
41 * @defgroup kernel_apis Kernel APIs
42 * @{
43 * @}
44 */
45
Anas Nashif61f4b242016-11-18 10:53:59 -050046#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040047#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
48#else
49#define K_DEBUG(fmt, ...)
50#endif
51
Benjamin Walsh2f280412017-01-14 19:23:46 -050052#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
54#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
55#elif defined(CONFIG_COOP_ENABLED)
56#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
57#define _NUM_PREEMPT_PRIO (0)
58#elif defined(CONFIG_PREEMPT_ENABLED)
59#define _NUM_COOP_PRIO (0)
60#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
61#else
62#error "invalid configuration"
63#endif
64
65#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_PRIO_PREEMPT(x) (x)
67
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_ANY NULL
69#define K_END NULL
70
Benjamin Walshedb35702017-01-14 18:47:22 -050071#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050073#elif defined(CONFIG_COOP_ENABLED)
74#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
75#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050077#else
78#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#endif
80
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050081#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
83#else
84#define K_LOWEST_THREAD_PRIO -1
85#endif
86
Benjamin Walshfab8d922016-11-08 15:36:36 -050087#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
88
Benjamin Walsh456c6da2016-09-02 18:55:39 -040089#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
90#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
91
92typedef sys_dlist_t _wait_q_t;
93
Anas Nashif2f203c22016-12-18 06:57:45 -050094#ifdef CONFIG_OBJECT_TRACING
95#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
96#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#else
Anas Nashif2f203c22016-12-18 06:57:45 -050098#define _OBJECT_TRACING_INIT
99#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100#endif
101
Benjamin Walshacc68c12017-01-29 18:57:45 -0500102#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300103#define _POLL_EVENT_OBJ_INIT(obj) \
104 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
105#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500108#define _POLL_EVENT
109#endif
110
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500111struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112struct k_mutex;
113struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400114struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115struct k_msgq;
116struct k_mbox;
117struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200118struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_fifo;
120struct k_lifo;
121struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400122struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_mem_pool;
124struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500125struct k_poll_event;
126struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800127struct k_mem_domain;
128struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400129
Andrew Boie5bd891d2017-09-27 12:59:28 -0700130/* This enumeration needs to be kept in sync with the lists of kernel objects
131 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
132 * function in kernel/userspace.c
133 */
Andrew Boie945af952017-08-22 13:15:23 -0700134enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700135 K_OBJ_ANY,
136
Andrew Boie5bd891d2017-09-27 12:59:28 -0700137 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700138 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700139 K_OBJ_MSGQ,
140 K_OBJ_MUTEX,
141 K_OBJ_PIPE,
142 K_OBJ_SEM,
143 K_OBJ_STACK,
144 K_OBJ_THREAD,
145 K_OBJ_TIMER,
Andrew Boiebca15da2017-10-15 14:17:48 -0700146 K_OBJ__THREAD_STACK_ELEMENT,
Andrew Boie945af952017-08-22 13:15:23 -0700147
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148 /* Driver subsystems */
149 K_OBJ_DRIVER_ADC,
150 K_OBJ_DRIVER_AIO_CMP,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700151 K_OBJ_DRIVER_COUNTER,
152 K_OBJ_DRIVER_CRYPTO,
Andrew Boiece6c8f32018-02-09 13:58:37 -0800153 K_OBJ_DRIVER_DMA,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700154 K_OBJ_DRIVER_FLASH,
155 K_OBJ_DRIVER_GPIO,
156 K_OBJ_DRIVER_I2C,
157 K_OBJ_DRIVER_I2S,
158 K_OBJ_DRIVER_IPM,
159 K_OBJ_DRIVER_PINMUX,
160 K_OBJ_DRIVER_PWM,
Leandro Pereirada9b0dd2017-10-13 16:30:55 -0700161 K_OBJ_DRIVER_ENTROPY,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700162 K_OBJ_DRIVER_RTC,
163 K_OBJ_DRIVER_SENSOR,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700164 K_OBJ_DRIVER_SPI,
165 K_OBJ_DRIVER_UART,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700166
Andrew Boie945af952017-08-22 13:15:23 -0700167 K_OBJ_LAST
168};
169
170#ifdef CONFIG_USERSPACE
171/* Table generated by gperf, these objects are retrieved via
172 * _k_object_find() */
173struct _k_object {
174 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700175 u8_t perms[CONFIG_MAX_THREAD_BYTES];
176 u8_t type;
177 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700178 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700179} __packed;
180
Andrew Boie877f82e2017-10-17 11:20:22 -0700181struct _k_object_assignment {
182 struct k_thread *thread;
183 void * const *objects;
184};
185
186/**
187 * @brief Grant a static thread access to a list of kernel objects
188 *
189 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
190 * a set of kernel objects. These objects do not need to be in an initialized
191 * state. The permissions will be granted when the threads are initialized
192 * in the early boot sequence.
193 *
194 * All arguments beyond the first must be pointers to kernel objects.
195 *
196 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
197 */
198#define K_THREAD_ACCESS_GRANT(name_, ...) \
199 static void * const _CONCAT(_object_list_, name_)[] = \
200 { __VA_ARGS__, NULL }; \
201 static __used __in_section_unique(object_access) \
202 const struct _k_object_assignment \
203 _CONCAT(_object_access_, name_) = \
204 { (&_k_thread_obj_ ## name_), \
205 (_CONCAT(_object_list_, name_)) }
206
Andrew Boie945af952017-08-22 13:15:23 -0700207#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700208#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700209
210/**
211 * Lookup a kernel object and init its metadata if it exists
212 *
213 * Calling this on an object will make it usable from userspace.
214 * Intended to be called as the last statement in kernel object init
215 * functions.
216 *
217 * @param object Address of the kernel object
218 */
219void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700220#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700221
222#define K_THREAD_ACCESS_GRANT(thread, ...)
223
Andrew Boie743e4682017-10-04 12:25:50 -0700224static inline void _k_object_init(void *obj)
225{
226 ARG_UNUSED(obj);
227}
228
229static inline void _impl_k_object_access_grant(void *object,
230 struct k_thread *thread)
231{
232 ARG_UNUSED(object);
233 ARG_UNUSED(thread);
234}
235
Andrew Boiea89bf012017-10-09 14:47:55 -0700236static inline void _impl_k_object_access_revoke(void *object,
237 struct k_thread *thread)
238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Andrew Boie41bab6e2017-10-14 14:42:23 -0700243static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700244{
245 ARG_UNUSED(object);
246}
247#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700248
249/**
250 * grant a thread access to a kernel object
251 *
252 * The thread will be granted access to the object if the caller is from
253 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700254 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700255 *
256 * @param object Address of kernel object
257 * @param thread Thread to grant access to the object
258 */
Andrew Boie743e4682017-10-04 12:25:50 -0700259__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700260
Andrew Boiea89bf012017-10-09 14:47:55 -0700261/**
262 * grant a thread access to a kernel object
263 *
264 * The thread will lose access to the object if the caller is from
265 * supervisor mode, or the caller is from user mode AND has permissions
266 * on both the object and the thread whose access is being revoked.
267 *
268 * @param object Address of kernel object
269 * @param thread Thread to remove access to the object
270 */
271__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700272
273/**
274 * grant all present and future threads access to an object
275 *
276 * If the caller is from supervisor mode, or the caller is from user mode and
277 * have sufficient permissions on the object, then that object will have
278 * permissions granted to it for *all* current and future threads running in
279 * the system, effectively becoming a public kernel object.
280 *
281 * Use of this API should be avoided on systems that are running untrusted code
282 * as it is possible for such code to derive the addresses of kernel objects
283 * and perform unwanted operations on them.
284 *
Andrew Boie04caa672017-10-13 13:57:07 -0700285 * It is not possible to revoke permissions on public objects; once public,
286 * any thread may use it.
287 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700288 * @param object Address of kernel object
289 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700290void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700291
Andrew Boiebca15da2017-10-15 14:17:48 -0700292/* Using typedef deliberately here, this is quite intended to be an opaque
293 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
294 *
295 * The purpose of this data type is to clearly distinguish between the
296 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
297 * buffer which composes the stack data actually used by the underlying
298 * thread; they cannot be used interchangably as some arches precede the
299 * stack buffer region with guard areas that trigger a MPU or MMU fault
300 * if written to.
301 *
302 * APIs that want to work with the buffer inside should continue to use
303 * char *.
304 *
305 * Stacks should always be created with K_THREAD_STACK_DEFINE().
306 */
307struct __packed _k_thread_stack_element {
308 char data;
309};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700310typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700311
Andrew Boie73abd322017-04-04 13:19:13 -0700312/* timeouts */
313
314struct _timeout;
315typedef void (*_timeout_func_t)(struct _timeout *t);
316
317struct _timeout {
318 sys_dnode_t node;
319 struct k_thread *thread;
320 sys_dlist_t *wait_q;
321 s32_t delta_ticks_from_prev;
322 _timeout_func_t func;
323};
324
325extern s32_t _timeout_remaining_get(struct _timeout *timeout);
326
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700327/**
328 * @typedef k_thread_entry_t
329 * @brief Thread entry point function type.
330 *
331 * A thread's entry point function is invoked when the thread starts executing.
332 * Up to 3 argument values can be passed to the function.
333 *
334 * The thread terminates execution permanently if the entry point function
335 * returns. The thread is responsible for releasing any shared resources
336 * it may own (such as mutexes and dynamically allocated memory), prior to
337 * returning.
338 *
339 * @param p1 First argument.
340 * @param p2 Second argument.
341 * @param p3 Third argument.
342 *
343 * @return N/A
344 */
345typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700346
347#ifdef CONFIG_THREAD_MONITOR
348struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700349 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700350 void *parameter1;
351 void *parameter2;
352 void *parameter3;
353};
354#endif
355
356/* can be used for creating 'dummy' threads, e.g. for pending on objects */
357struct _thread_base {
358
359 /* this thread's entry in a ready/wait queue */
360 sys_dnode_t k_q_node;
361
362 /* user facing 'thread options'; values defined in include/kernel.h */
363 u8_t user_options;
364
365 /* thread state */
366 u8_t thread_state;
367
368 /*
369 * scheduler lock count and thread priority
370 *
371 * These two fields control the preemptibility of a thread.
372 *
373 * When the scheduler is locked, sched_locked is decremented, which
374 * means that the scheduler is locked for values from 0xff to 0x01. A
375 * thread is coop if its prio is negative, thus 0x80 to 0xff when
376 * looked at the value as unsigned.
377 *
378 * By putting them end-to-end, this means that a thread is
379 * non-preemptible if the bundled value is greater than or equal to
380 * 0x0080.
381 */
382 union {
383 struct {
384#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
385 u8_t sched_locked;
386 s8_t prio;
387#else /* LITTLE and PDP */
388 s8_t prio;
389 u8_t sched_locked;
390#endif
391 };
392 u16_t preempt;
393 };
394
Andy Ross2724fd12018-01-29 14:55:20 -0800395#ifdef CONFIG_SMP
396 /* True for the per-CPU idle threads */
397 u8_t is_idle;
398
399 /* Non-zero when actively running on a CPU */
400 u8_t active;
401
402 /* CPU index on which thread was last run */
403 u8_t cpu;
404#endif
405
Andrew Boie73abd322017-04-04 13:19:13 -0700406 /* data returned by APIs */
407 void *swap_data;
408
409#ifdef CONFIG_SYS_CLOCK_EXISTS
410 /* this thread's entry in a timeout queue */
411 struct _timeout timeout;
412#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700413};
414
415typedef struct _thread_base _thread_base_t;
416
417#if defined(CONFIG_THREAD_STACK_INFO)
418/* Contains the stack information of a thread */
419struct _thread_stack_info {
420 /* Stack Start */
421 u32_t start;
422 /* Stack Size */
423 u32_t size;
424};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700425
426typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700427#endif /* CONFIG_THREAD_STACK_INFO */
428
Chunlin Hane9c97022017-07-07 20:29:30 +0800429#if defined(CONFIG_USERSPACE)
430struct _mem_domain_info {
431 /* memory domain queue node */
432 sys_dnode_t mem_domain_q_node;
433 /* memory domain of the thread */
434 struct k_mem_domain *mem_domain;
435};
436
437#endif /* CONFIG_USERSPACE */
438
Andrew Boie73abd322017-04-04 13:19:13 -0700439struct k_thread {
440
441 struct _thread_base base;
442
443 /* defined by the architecture, but all archs need these */
444 struct _caller_saved caller_saved;
445 struct _callee_saved callee_saved;
446
447 /* static thread init data */
448 void *init_data;
449
450 /* abort function */
451 void (*fn_abort)(void);
452
453#if defined(CONFIG_THREAD_MONITOR)
454 /* thread entry and parameters description */
455 struct __thread_entry *entry;
456
457 /* next item in list of all threads */
458 struct k_thread *next_thread;
459#endif
460
461#ifdef CONFIG_THREAD_CUSTOM_DATA
462 /* crude thread-local storage */
463 void *custom_data;
464#endif
465
466#ifdef CONFIG_ERRNO
467 /* per-thread errno variable */
468 int errno_var;
469#endif
470
471#if defined(CONFIG_THREAD_STACK_INFO)
472 /* Stack Info */
473 struct _thread_stack_info stack_info;
474#endif /* CONFIG_THREAD_STACK_INFO */
475
Chunlin Hane9c97022017-07-07 20:29:30 +0800476#if defined(CONFIG_USERSPACE)
477 /* memory domain info of the thread */
478 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700479 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700480 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800481#endif /* CONFIG_USERSPACE */
482
Andy Ross042d8ec2017-12-09 08:37:20 -0800483#if defined(CONFIG_USE_SWITCH)
484 /* When using __switch() a few previously arch-specific items
485 * become part of the core OS
486 */
487
488 /* _Swap() return value */
489 int swap_retval;
490
491 /* Context handle returned via _arch_switch() */
492 void *switch_handle;
493#endif
494
Andrew Boie73abd322017-04-04 13:19:13 -0700495 /* arch-specifics: must always be at the end */
496 struct _thread_arch arch;
497};
498
499typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400500typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700501#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400502
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503enum execution_context_types {
504 K_ISR = 0,
505 K_COOP_THREAD,
506 K_PREEMPT_THREAD,
507};
508
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400509/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100510 * @defgroup profiling_apis Profiling APIs
511 * @ingroup kernel_apis
512 * @{
513 */
514
515/**
516 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
517 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700518 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100519 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
520 *
521 * CONFIG_MAIN_STACK_SIZE
522 * CONFIG_IDLE_STACK_SIZE
523 * CONFIG_ISR_STACK_SIZE
524 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
525 *
526 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
527 * produce output.
528 *
529 * @return N/A
530 */
531extern void k_call_stacks_analyze(void);
532
Anas Nashif166f5192018-02-25 08:02:36 -0600533/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100534
535/**
Allan Stephensc98da842016-11-11 15:45:03 -0500536 * @defgroup thread_apis Thread APIs
537 * @ingroup kernel_apis
538 * @{
539 */
540
Benjamin Walshed240f22017-01-22 13:05:08 -0500541#endif /* !_ASMLANGUAGE */
542
543
544/*
545 * Thread user options. May be needed by assembly code. Common part uses low
546 * bits, arch-specific use high bits.
547 */
548
549/* system thread that must not abort */
550#define K_ESSENTIAL (1 << 0)
551
552#if defined(CONFIG_FP_SHARING)
553/* thread uses floating point registers */
554#define K_FP_REGS (1 << 1)
555#endif
556
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700557/* This thread has dropped from supervisor mode to user mode and consequently
558 * has additional restrictions
559 */
560#define K_USER (1 << 2)
561
Andrew Boie47f8fd12017-10-05 11:11:02 -0700562/* Indicates that the thread being created should inherit all kernel object
563 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
564 * is not enabled.
565 */
566#define K_INHERIT_PERMS (1 << 3)
567
Benjamin Walshed240f22017-01-22 13:05:08 -0500568#ifdef CONFIG_X86
569/* x86 Bitmask definitions for threads user options */
570
571#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
572/* thread uses SSEx (and also FP) registers */
573#define K_SSE_REGS (1 << 7)
574#endif
575#endif
576
577/* end - thread options */
578
579#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700581 * @brief Create a thread.
582 *
583 * This routine initializes a thread, then schedules it for execution.
584 *
585 * The new thread may be scheduled for immediate execution or a delayed start.
586 * If the newly spawned thread does not have a delayed start the kernel
587 * scheduler may preempt the current thread to allow the new thread to
588 * execute.
589 *
590 * Thread options are architecture-specific, and can include K_ESSENTIAL,
591 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
592 * them using "|" (the logical OR operator).
593 *
594 * Historically, users often would use the beginning of the stack memory region
595 * to store the struct k_thread data, although corruption will occur if the
596 * stack overflows this region and stack protection features may not detect this
597 * situation.
598 *
599 * @param new_thread Pointer to uninitialized struct k_thread
600 * @param stack Pointer to the stack space.
601 * @param stack_size Stack size in bytes.
602 * @param entry Thread entry function.
603 * @param p1 1st entry point parameter.
604 * @param p2 2nd entry point parameter.
605 * @param p3 3rd entry point parameter.
606 * @param prio Thread priority.
607 * @param options Thread options.
608 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
609 *
610 * @return ID of new thread.
611 */
Andrew Boie662c3452017-10-02 10:51:18 -0700612__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700613 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700614 size_t stack_size,
615 k_thread_entry_t entry,
616 void *p1, void *p2, void *p3,
617 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700618
Andrew Boie3f091b52017-08-30 14:34:14 -0700619/**
620 * @brief Drop a thread's privileges permanently to user mode
621 *
622 * @param entry Function to start executing from
623 * @param p1 1st entry point parameter
624 * @param p2 2nd entry point parameter
625 * @param p3 3rd entry point parameter
626 */
627extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
628 void *p1, void *p2,
629 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700630
Andrew Boied26cf2d2017-03-30 13:07:02 -0700631/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700632 * @brief Grant a thread access to a NULL-terminated set of kernel objects
633 *
634 * This is a convenience function. For the provided thread, grant access to
635 * the remaining arguments, which must be pointers to kernel objects.
636 * The final argument must be a NULL.
637 *
638 * The thread object must be initialized (i.e. running). The objects don't
639 * need to be.
640 *
641 * @param thread Thread to grant access to objects
642 * @param ... NULL-terminated list of kernel object pointers
643 */
644extern void __attribute__((sentinel))
645 k_thread_access_grant(struct k_thread *thread, ...);
646
647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500648 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400649 *
Allan Stephensc98da842016-11-11 15:45:03 -0500650 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500651 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500653 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654 *
655 * @return N/A
656 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700657__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658
659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500660 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400661 *
662 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400665 * @return N/A
666 */
Kumar Galacc334c72017-04-21 10:55:34 -0500667extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400668
669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500674 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 *
676 * @return N/A
677 */
Andrew Boie468190a2017-09-29 14:00:48 -0700678__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400679
680/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * If @a thread is not currently sleeping, the routine has no effect.
686 *
687 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400688 *
689 * @return N/A
690 */
Andrew Boie468190a2017-09-29 14:00:48 -0700691__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692
693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700698__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699
700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * This routine prevents @a thread from executing if it has not yet started
704 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500706 * @param thread ID of thread to cancel.
707 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700708 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500709 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 */
Andrew Boie468190a2017-09-29 14:00:48 -0700711__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400712
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400713/**
Allan Stephensc98da842016-11-11 15:45:03 -0500714 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * This routine permanently stops execution of @a thread. The thread is taken
717 * off all kernel queues it is part of (i.e. the ready queue, the timeout
718 * queue, or a kernel object wait queue). However, any kernel resources the
719 * thread might currently own (such as mutexes or memory blocks) are not
720 * released. It is the responsibility of the caller of this routine to ensure
721 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500723 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
725 * @return N/A
726 */
Andrew Boie468190a2017-09-29 14:00:48 -0700727__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400728
Andrew Boie7d627c52017-08-30 11:01:56 -0700729
730/**
731 * @brief Start an inactive thread
732 *
733 * If a thread was created with K_FOREVER in the delay parameter, it will
734 * not be added to the scheduling queue until this function is called
735 * on it.
736 *
737 * @param thread thread to start
738 */
Andrew Boie468190a2017-09-29 14:00:48 -0700739__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700740
Allan Stephensc98da842016-11-11 15:45:03 -0500741/**
742 * @cond INTERNAL_HIDDEN
743 */
744
Benjamin Walshd211a522016-12-06 11:44:01 -0500745/* timeout has timed out and is not on _timeout_q anymore */
746#define _EXPIRED (-2)
747
748/* timeout is not in use */
749#define _INACTIVE (-1)
750
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400751struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700752 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700753 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400754 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700755 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500756 void *init_p1;
757 void *init_p2;
758 void *init_p3;
759 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500760 u32_t init_options;
761 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500762 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763};
764
Andrew Boied26cf2d2017-03-30 13:07:02 -0700765#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400766 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500767 prio, options, delay, abort, groups) \
768 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700769 .init_thread = (thread), \
770 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500771 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700772 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400773 .init_p1 = (void *)p1, \
774 .init_p2 = (void *)p2, \
775 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500776 .init_prio = (prio), \
777 .init_options = (options), \
778 .init_delay = (delay), \
779 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400780 }
781
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400782/**
Allan Stephensc98da842016-11-11 15:45:03 -0500783 * INTERNAL_HIDDEN @endcond
784 */
785
786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @brief Statically define and initialize a thread.
788 *
789 * The thread may be scheduled for immediate execution or a delayed start.
790 *
791 * Thread options are architecture-specific, and can include K_ESSENTIAL,
792 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
793 * them using "|" (the logical OR operator).
794 *
795 * The ID of the thread can be accessed using:
796 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500797 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 *
799 * @param name Name of the thread.
800 * @param stack_size Stack size in bytes.
801 * @param entry Thread entry function.
802 * @param p1 1st entry point parameter.
803 * @param p2 2nd entry point parameter.
804 * @param p3 3rd entry point parameter.
805 * @param prio Thread priority.
806 * @param options Thread options.
807 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400808 *
809 * @internal It has been observed that the x86 compiler by default aligns
810 * these _static_thread_data structures to 32-byte boundaries, thereby
811 * wasting space. To work around this, force a 4-byte alignment.
812 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500813#define K_THREAD_DEFINE(name, stack_size, \
814 entry, p1, p2, p3, \
815 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700816 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700817 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500818 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500819 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700820 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
821 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500822 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700823 NULL, 0); \
824 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400825
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @param thread ID of thread whose priority is needed.
832 *
833 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700835__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836
837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
842 * Rescheduling can occur immediately depending on the priority @a thread is
843 * set to:
844 *
845 * - If its priority is raised above the priority of the caller of this
846 * function, and the caller is preemptible, @a thread will be scheduled in.
847 *
848 * - If the caller operates on itself, it lowers its priority below that of
849 * other threads in the system, and the caller is preemptible, the thread of
850 * highest priority will be scheduled in.
851 *
852 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
853 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
854 * highest priority.
855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 * @param prio New priority.
858 *
859 * @warning Changing the priority of a thread currently involved in mutex
860 * priority inheritance may result in undefined behavior.
861 *
862 * @return N/A
863 */
Andrew Boie468190a2017-09-29 14:00:48 -0700864__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400865
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * This routine prevents the kernel scheduler from making @a thread the
870 * current thread. All other internal operations on @a thread are still
871 * performed; for example, any timeout it is waiting on keeps ticking,
872 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * If @a thread is already suspended, the routine has no effect.
875 *
876 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
878 * @return N/A
879 */
Andrew Boie468190a2017-09-29 14:00:48 -0700880__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * This routine allows the kernel scheduler to make @a thread the current
886 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * If @a thread is not currently suspended, the routine has no effect.
889 *
890 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 *
892 * @return N/A
893 */
Andrew Boie468190a2017-09-29 14:00:48 -0700894__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400895
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * This routine specifies how the scheduler will perform time slicing of
900 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500902 * To enable time slicing, @a slice must be non-zero. The scheduler
903 * ensures that no thread runs for more than the specified time limit
904 * before other threads of that priority are given a chance to execute.
905 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700906 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909 * execute. Once the scheduler selects a thread for execution, there is no
910 * minimum guaranteed time the thread will execute before threads of greater or
911 * equal priority are scheduled.
912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500913 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400914 * for execution, this routine has no effect; the thread is immediately
915 * rescheduled after the slice period expires.
916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500917 * To disable timeslicing, set both @a slice and @a prio to zero.
918 *
919 * @param slice Maximum time slice length (in milliseconds).
920 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400921 *
922 * @return N/A
923 */
Kumar Galacc334c72017-04-21 10:55:34 -0500924extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925
Anas Nashif166f5192018-02-25 08:02:36 -0600926/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500927
928/**
929 * @addtogroup isr_apis
930 * @{
931 */
932
933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
Allan Stephensc98da842016-11-11 15:45:03 -0500936 * This routine allows the caller to customize its actions, depending on
937 * whether it is a thread or an ISR.
938 *
939 * @note Can be called by ISRs.
940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * @return 0 if invoked by a thread.
942 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400943 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500944extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945
Benjamin Walsh445830d2016-11-10 15:54:27 -0500946/**
947 * @brief Determine if code is running in a preemptible thread.
948 *
Allan Stephensc98da842016-11-11 15:45:03 -0500949 * This routine allows the caller to customize its actions, depending on
950 * whether it can be preempted by another thread. The routine returns a 'true'
951 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500952 *
Allan Stephensc98da842016-11-11 15:45:03 -0500953 * - The code is running in a thread, not at ISR.
954 * - The thread's priority is in the preemptible range.
955 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500956 *
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * @note Can be called by ISRs.
958 *
959 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500960 * @return Non-zero if invoked by a preemptible thread.
961 */
Andrew Boie468190a2017-09-29 14:00:48 -0700962__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500963
Allan Stephensc98da842016-11-11 15:45:03 -0500964/**
Anas Nashif166f5192018-02-25 08:02:36 -0600965 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500966 */
967
968/**
969 * @addtogroup thread_apis
970 * @{
971 */
972
973/**
974 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500975 *
Allan Stephensc98da842016-11-11 15:45:03 -0500976 * This routine prevents the current thread from being preempted by another
977 * thread by instructing the scheduler to treat it as a cooperative thread.
978 * If the thread subsequently performs an operation that makes it unready,
979 * it will be context switched out in the normal manner. When the thread
980 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500981 *
Allan Stephensc98da842016-11-11 15:45:03 -0500982 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500983 *
Allan Stephensc98da842016-11-11 15:45:03 -0500984 * @note k_sched_lock() and k_sched_unlock() should normally be used
985 * when the operation being performed can be safely interrupted by ISRs.
986 * However, if the amount of processing involved is very small, better
987 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500988 *
989 * @return N/A
990 */
991extern void k_sched_lock(void);
992
Allan Stephensc98da842016-11-11 15:45:03 -0500993/**
994 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500995 *
Allan Stephensc98da842016-11-11 15:45:03 -0500996 * This routine reverses the effect of a previous call to k_sched_lock().
997 * A thread must call the routine once for each time it called k_sched_lock()
998 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500999 *
1000 * @return N/A
1001 */
1002extern void k_sched_unlock(void);
1003
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001007 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * Custom data is not used by the kernel itself, and is freely available
1010 * for a thread to use as it sees fit. It can be used as a framework
1011 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
1015 * @return N/A
1016 */
Andrew Boie468190a2017-09-29 14:00:48 -07001017__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018
1019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025 */
Andrew Boie468190a2017-09-29 14:00:48 -07001026__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001027
1028/**
Anas Nashif166f5192018-02-25 08:02:36 -06001029 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001030 */
1031
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001032#include <sys_clock.h>
1033
Allan Stephensc2f15a42016-11-17 12:24:22 -05001034/**
1035 * @addtogroup clock_apis
1036 * @{
1037 */
1038
1039/**
1040 * @brief Generate null timeout delay.
1041 *
1042 * This macro generates a timeout delay that that instructs a kernel API
1043 * not to wait if the requested operation cannot be performed immediately.
1044 *
1045 * @return Timeout delay value.
1046 */
1047#define K_NO_WAIT 0
1048
1049/**
1050 * @brief Generate timeout delay from milliseconds.
1051 *
1052 * This macro generates a timeout delay that that instructs a kernel API
1053 * to wait up to @a ms milliseconds to perform the requested operation.
1054 *
1055 * @param ms Duration in milliseconds.
1056 *
1057 * @return Timeout delay value.
1058 */
Johan Hedberg14471692016-11-13 10:52:15 +02001059#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001060
1061/**
1062 * @brief Generate timeout delay from seconds.
1063 *
1064 * This macro generates a timeout delay that that instructs a kernel API
1065 * to wait up to @a s seconds to perform the requested operation.
1066 *
1067 * @param s Duration in seconds.
1068 *
1069 * @return Timeout delay value.
1070 */
Johan Hedberg14471692016-11-13 10:52:15 +02001071#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001072
1073/**
1074 * @brief Generate timeout delay from minutes.
1075 *
1076 * This macro generates a timeout delay that that instructs a kernel API
1077 * to wait up to @a m minutes to perform the requested operation.
1078 *
1079 * @param m Duration in minutes.
1080 *
1081 * @return Timeout delay value.
1082 */
Johan Hedberg14471692016-11-13 10:52:15 +02001083#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001084
1085/**
1086 * @brief Generate timeout delay from hours.
1087 *
1088 * This macro generates a timeout delay that that instructs a kernel API
1089 * to wait up to @a h hours to perform the requested operation.
1090 *
1091 * @param h Duration in hours.
1092 *
1093 * @return Timeout delay value.
1094 */
Johan Hedberg14471692016-11-13 10:52:15 +02001095#define K_HOURS(h) K_MINUTES((h) * 60)
1096
Allan Stephensc98da842016-11-11 15:45:03 -05001097/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001098 * @brief Generate infinite timeout delay.
1099 *
1100 * This macro generates a timeout delay that that instructs a kernel API
1101 * to wait as long as necessary to perform the requested operation.
1102 *
1103 * @return Timeout delay value.
1104 */
1105#define K_FOREVER (-1)
1106
1107/**
Anas Nashif166f5192018-02-25 08:02:36 -06001108 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001109 */
1110
1111/**
Allan Stephensc98da842016-11-11 15:45:03 -05001112 * @cond INTERNAL_HIDDEN
1113 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001114
Benjamin Walsh62092182016-12-20 14:39:08 -05001115/* kernel clocks */
1116
1117#if (sys_clock_ticks_per_sec == 1000) || \
1118 (sys_clock_ticks_per_sec == 500) || \
1119 (sys_clock_ticks_per_sec == 250) || \
1120 (sys_clock_ticks_per_sec == 125) || \
1121 (sys_clock_ticks_per_sec == 100) || \
1122 (sys_clock_ticks_per_sec == 50) || \
1123 (sys_clock_ticks_per_sec == 25) || \
1124 (sys_clock_ticks_per_sec == 20) || \
1125 (sys_clock_ticks_per_sec == 10) || \
1126 (sys_clock_ticks_per_sec == 1)
1127
1128 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1129#else
1130 /* yields horrible 64-bit math on many architectures: try to avoid */
1131 #define _NON_OPTIMIZED_TICKS_PER_SEC
1132#endif
1133
1134#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001135extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001136#else
Kumar Galacc334c72017-04-21 10:55:34 -05001137static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001138{
Kumar Galacc334c72017-04-21 10:55:34 -05001139 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001140}
1141#endif
1142
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001143/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001144#ifdef CONFIG_TICKLESS_KERNEL
1145#define _TICK_ALIGN 0
1146#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001147#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001148#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001149
Kumar Galacc334c72017-04-21 10:55:34 -05001150static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001151{
Benjamin Walsh62092182016-12-20 14:39:08 -05001152#ifdef CONFIG_SYS_CLOCK_EXISTS
1153
1154#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001155 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001156#else
Kumar Galacc334c72017-04-21 10:55:34 -05001157 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001158#endif
1159
1160#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001161 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001162 return 0;
1163#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001164}
1165
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166struct k_timer {
1167 /*
1168 * _timeout structure must be first here if we want to use
1169 * dynamic timer allocation. timeout.node is used in the double-linked
1170 * list of free timers
1171 */
1172 struct _timeout timeout;
1173
Allan Stephens45bfa372016-10-12 12:39:42 -05001174 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001175 _wait_q_t wait_q;
1176
1177 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001178 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179
1180 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001181 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182
1183 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001184 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001185
Allan Stephens45bfa372016-10-12 12:39:42 -05001186 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001187 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001189 /* user-specific data, also used to support legacy features */
1190 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191
Anas Nashif2f203c22016-12-18 06:57:45 -05001192 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193};
1194
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001195#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001196 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001197 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001198 .timeout.wait_q = NULL, \
1199 .timeout.thread = NULL, \
1200 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001202 .expiry_fn = expiry, \
1203 .stop_fn = stop, \
1204 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001205 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001206 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001207 }
1208
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001209#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1210
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001211/**
Allan Stephensc98da842016-11-11 15:45:03 -05001212 * INTERNAL_HIDDEN @endcond
1213 */
1214
1215/**
1216 * @defgroup timer_apis Timer APIs
1217 * @ingroup kernel_apis
1218 * @{
1219 */
1220
1221/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001222 * @typedef k_timer_expiry_t
1223 * @brief Timer expiry function type.
1224 *
1225 * A timer's expiry function is executed by the system clock interrupt handler
1226 * each time the timer expires. The expiry function is optional, and is only
1227 * invoked if the timer has been initialized with one.
1228 *
1229 * @param timer Address of timer.
1230 *
1231 * @return N/A
1232 */
1233typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1234
1235/**
1236 * @typedef k_timer_stop_t
1237 * @brief Timer stop function type.
1238 *
1239 * A timer's stop function is executed if the timer is stopped prematurely.
1240 * The function runs in the context of the thread that stops the timer.
1241 * The stop function is optional, and is only invoked if the timer has been
1242 * initialized with one.
1243 *
1244 * @param timer Address of timer.
1245 *
1246 * @return N/A
1247 */
1248typedef void (*k_timer_stop_t)(struct k_timer *timer);
1249
1250/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001253 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001254 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001255 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001256 *
1257 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001258 * @param expiry_fn Function to invoke each time the timer expires.
1259 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001261#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001262 struct k_timer name \
1263 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001264 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001265
Allan Stephens45bfa372016-10-12 12:39:42 -05001266/**
1267 * @brief Initialize a timer.
1268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001269 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001270 *
1271 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @param expiry_fn Function to invoke each time the timer expires.
1273 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001274 *
1275 * @return N/A
1276 */
1277extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001278 k_timer_expiry_t expiry_fn,
1279 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001280
Allan Stephens45bfa372016-10-12 12:39:42 -05001281/**
1282 * @brief Start a timer.
1283 *
1284 * This routine starts a timer, and resets its status to zero. The timer
1285 * begins counting down using the specified duration and period values.
1286 *
1287 * Attempting to start a timer that is already running is permitted.
1288 * The timer's status is reset to zero and the timer begins counting down
1289 * using the new duration and period values.
1290 *
1291 * @param timer Address of timer.
1292 * @param duration Initial timer duration (in milliseconds).
1293 * @param period Timer period (in milliseconds).
1294 *
1295 * @return N/A
1296 */
Andrew Boiea354d492017-09-29 16:22:28 -07001297__syscall void k_timer_start(struct k_timer *timer,
1298 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001299
1300/**
1301 * @brief Stop a timer.
1302 *
1303 * This routine stops a running timer prematurely. The timer's stop function,
1304 * if one exists, is invoked by the caller.
1305 *
1306 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001307 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001308 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001309 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1310 * if @a k_timer_stop is to be called from ISRs.
1311 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001312 * @param timer Address of timer.
1313 *
1314 * @return N/A
1315 */
Andrew Boiea354d492017-09-29 16:22:28 -07001316__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001317
1318/**
1319 * @brief Read timer status.
1320 *
1321 * This routine reads the timer's status, which indicates the number of times
1322 * it has expired since its status was last read.
1323 *
1324 * Calling this routine resets the timer's status to zero.
1325 *
1326 * @param timer Address of timer.
1327 *
1328 * @return Timer status.
1329 */
Andrew Boiea354d492017-09-29 16:22:28 -07001330__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001331
1332/**
1333 * @brief Synchronize thread to timer expiration.
1334 *
1335 * This routine blocks the calling thread until the timer's status is non-zero
1336 * (indicating that it has expired at least once since it was last examined)
1337 * or the timer is stopped. If the timer status is already non-zero,
1338 * or the timer is already stopped, the caller continues without waiting.
1339 *
1340 * Calling this routine resets the timer's status to zero.
1341 *
1342 * This routine must not be used by interrupt handlers, since they are not
1343 * allowed to block.
1344 *
1345 * @param timer Address of timer.
1346 *
1347 * @return Timer status.
1348 */
Andrew Boiea354d492017-09-29 16:22:28 -07001349__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001350
1351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001353 *
1354 * This routine computes the (approximate) time remaining before a running
1355 * timer next expires. If the timer is not running, it returns zero.
1356 *
1357 * @param timer Address of timer.
1358 *
1359 * @return Remaining time (in milliseconds).
1360 */
Andrew Boiea354d492017-09-29 16:22:28 -07001361__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1362
1363static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001364{
1365 return _timeout_remaining_get(&timer->timeout);
1366}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001367
Allan Stephensc98da842016-11-11 15:45:03 -05001368/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001369 * @brief Associate user-specific data with a timer.
1370 *
1371 * This routine records the @a user_data with the @a timer, to be retrieved
1372 * later.
1373 *
1374 * It can be used e.g. in a timer handler shared across multiple subsystems to
1375 * retrieve data specific to the subsystem this timer is associated with.
1376 *
1377 * @param timer Address of timer.
1378 * @param user_data User data to associate with the timer.
1379 *
1380 * @return N/A
1381 */
Andrew Boiea354d492017-09-29 16:22:28 -07001382__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1383
1384static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1385 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001386{
1387 timer->user_data = user_data;
1388}
1389
1390/**
1391 * @brief Retrieve the user-specific data from a timer.
1392 *
1393 * @param timer Address of timer.
1394 *
1395 * @return The user data.
1396 */
Andrew Boiea354d492017-09-29 16:22:28 -07001397__syscall void *k_timer_user_data_get(struct k_timer *timer);
1398
1399static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001400{
1401 return timer->user_data;
1402}
1403
Anas Nashif166f5192018-02-25 08:02:36 -06001404/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001405
Allan Stephensc98da842016-11-11 15:45:03 -05001406/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001407 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001408 * @{
1409 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001410
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001414 * This routine returns the elapsed time since the system booted,
1415 * in milliseconds.
1416 *
1417 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001418 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001419__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001420
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001421#ifdef CONFIG_TICKLESS_KERNEL
1422/**
1423 * @brief Enable clock always on in tickless kernel
1424 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001425 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001426 * there are no timer events programmed in tickless kernel
1427 * scheduling. This is necessary if the clock is used to track
1428 * passage of time.
1429 *
1430 * @retval prev_status Previous status of always on flag
1431 */
1432static inline int k_enable_sys_clock_always_on(void)
1433{
1434 int prev_status = _sys_clock_always_on;
1435
1436 _sys_clock_always_on = 1;
1437 _enable_sys_clock();
1438
1439 return prev_status;
1440}
1441
1442/**
1443 * @brief Disable clock always on in tickless kernel
1444 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001445 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001446 * there are no timer events programmed in tickless kernel
1447 * scheduling. To save power, this routine should be called
1448 * immediately when clock is not used to track time.
1449 */
1450static inline void k_disable_sys_clock_always_on(void)
1451{
1452 _sys_clock_always_on = 0;
1453}
1454#else
1455#define k_enable_sys_clock_always_on() do { } while ((0))
1456#define k_disable_sys_clock_always_on() do { } while ((0))
1457#endif
1458
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * This routine returns the lower 32-bits of the elapsed time since the system
1463 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001465 * This routine can be more efficient than k_uptime_get(), as it reduces the
1466 * need for interrupt locking and 64-bit math. However, the 32-bit result
1467 * cannot hold a system uptime time larger than approximately 50 days, so the
1468 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001470 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001471 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001472__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001473
1474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * This routine computes the elapsed time between the current system uptime
1478 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * @param reftime Pointer to a reference time, which is updated to the current
1481 * uptime upon return.
1482 *
1483 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001484 */
Kumar Galacc334c72017-04-21 10:55:34 -05001485extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001486
1487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * This routine computes the elapsed time between the current system uptime
1491 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1494 * need for interrupt locking and 64-bit math. However, the 32-bit result
1495 * cannot hold an elapsed time larger than approximately 50 days, so the
1496 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @param reftime Pointer to a reference time, which is updated to the current
1499 * uptime upon return.
1500 *
1501 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001502 */
Kumar Galacc334c72017-04-21 10:55:34 -05001503extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001504
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001505/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001508 * This routine returns the current time, as measured by the system's hardware
1509 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001511 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001512 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001513#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001514
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515/**
Anas Nashif166f5192018-02-25 08:02:36 -06001516 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001517 */
1518
Allan Stephensc98da842016-11-11 15:45:03 -05001519/**
1520 * @cond INTERNAL_HIDDEN
1521 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001522
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001523struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001524 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001525 union {
1526 _wait_q_t wait_q;
1527
1528 _POLL_EVENT;
1529 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001530
1531 _OBJECT_TRACING_NEXT_PTR(k_queue);
1532};
1533
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001534#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001535 { \
1536 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1537 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001538 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001539 _OBJECT_TRACING_INIT \
1540 }
1541
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001542#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1543
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001544/**
1545 * INTERNAL_HIDDEN @endcond
1546 */
1547
1548/**
1549 * @defgroup queue_apis Queue APIs
1550 * @ingroup kernel_apis
1551 * @{
1552 */
1553
1554/**
1555 * @brief Initialize a queue.
1556 *
1557 * This routine initializes a queue object, prior to its first use.
1558 *
1559 * @param queue Address of the queue.
1560 *
1561 * @return N/A
1562 */
1563extern void k_queue_init(struct k_queue *queue);
1564
1565/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001566 * @brief Cancel waiting on a queue.
1567 *
1568 * This routine causes first thread pending on @a queue, if any, to
1569 * return from k_queue_get() call with NULL value (as if timeout expired).
1570 *
1571 * @note Can be called by ISRs.
1572 *
1573 * @param queue Address of the queue.
1574 *
1575 * @return N/A
1576 */
1577extern void k_queue_cancel_wait(struct k_queue *queue);
1578
1579/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001580 * @brief Append an element to the end of a queue.
1581 *
1582 * This routine appends a data item to @a queue. A queue data item must be
1583 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1584 * reserved for the kernel's use.
1585 *
1586 * @note Can be called by ISRs.
1587 *
1588 * @param queue Address of the queue.
1589 * @param data Address of the data item.
1590 *
1591 * @return N/A
1592 */
1593extern void k_queue_append(struct k_queue *queue, void *data);
1594
1595/**
1596 * @brief Prepend an element to a queue.
1597 *
1598 * This routine prepends a data item to @a queue. A queue data item must be
1599 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1600 * reserved for the kernel's use.
1601 *
1602 * @note Can be called by ISRs.
1603 *
1604 * @param queue Address of the queue.
1605 * @param data Address of the data item.
1606 *
1607 * @return N/A
1608 */
1609extern void k_queue_prepend(struct k_queue *queue, void *data);
1610
1611/**
1612 * @brief Inserts an element to a queue.
1613 *
1614 * This routine inserts a data item to @a queue after previous item. A queue
1615 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1616 * item are reserved for the kernel's use.
1617 *
1618 * @note Can be called by ISRs.
1619 *
1620 * @param queue Address of the queue.
1621 * @param prev Address of the previous data item.
1622 * @param data Address of the data item.
1623 *
1624 * @return N/A
1625 */
1626extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1627
1628/**
1629 * @brief Atomically append a list of elements to a queue.
1630 *
1631 * This routine adds a list of data items to @a queue in one operation.
1632 * The data items must be in a singly-linked list, with the first 32 bits
1633 * in each data item pointing to the next data item; the list must be
1634 * NULL-terminated.
1635 *
1636 * @note Can be called by ISRs.
1637 *
1638 * @param queue Address of the queue.
1639 * @param head Pointer to first node in singly-linked list.
1640 * @param tail Pointer to last node in singly-linked list.
1641 *
1642 * @return N/A
1643 */
1644extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1645
1646/**
1647 * @brief Atomically add a list of elements to a queue.
1648 *
1649 * This routine adds a list of data items to @a queue in one operation.
1650 * The data items must be in a singly-linked list implemented using a
1651 * sys_slist_t object. Upon completion, the original list is empty.
1652 *
1653 * @note Can be called by ISRs.
1654 *
1655 * @param queue Address of the queue.
1656 * @param list Pointer to sys_slist_t object.
1657 *
1658 * @return N/A
1659 */
1660extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1661
1662/**
1663 * @brief Get an element from a queue.
1664 *
1665 * This routine removes first data item from @a queue. The first 32 bits of the
1666 * data item are reserved for the kernel's use.
1667 *
1668 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1669 *
1670 * @param queue Address of the queue.
1671 * @param timeout Waiting period to obtain a data item (in milliseconds),
1672 * or one of the special values K_NO_WAIT and K_FOREVER.
1673 *
1674 * @return Address of the data item if successful; NULL if returned
1675 * without waiting, or waiting period timed out.
1676 */
Kumar Galacc334c72017-04-21 10:55:34 -05001677extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001678
1679/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001680 * @brief Remove an element from a queue.
1681 *
1682 * This routine removes data item from @a queue. The first 32 bits of the
1683 * data item are reserved for the kernel's use. Removing elements from k_queue
1684 * rely on sys_slist_find_and_remove which is not a constant time operation.
1685 *
1686 * @note Can be called by ISRs
1687 *
1688 * @param queue Address of the queue.
1689 * @param data Address of the data item.
1690 *
1691 * @return true if data item was removed
1692 */
1693static inline bool k_queue_remove(struct k_queue *queue, void *data)
1694{
1695 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1696}
1697
1698/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001699 * @brief Query a queue to see if it has data available.
1700 *
1701 * Note that the data might be already gone by the time this function returns
1702 * if other threads are also trying to read from the queue.
1703 *
1704 * @note Can be called by ISRs.
1705 *
1706 * @param queue Address of the queue.
1707 *
1708 * @return Non-zero if the queue is empty.
1709 * @return 0 if data is available.
1710 */
1711static inline int k_queue_is_empty(struct k_queue *queue)
1712{
1713 return (int)sys_slist_is_empty(&queue->data_q);
1714}
1715
1716/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001717 * @brief Peek element at the head of queue.
1718 *
1719 * Return element from the head of queue without removing it.
1720 *
1721 * @param queue Address of the queue.
1722 *
1723 * @return Head element, or NULL if queue is empty.
1724 */
1725static inline void *k_queue_peek_head(struct k_queue *queue)
1726{
1727 return sys_slist_peek_head(&queue->data_q);
1728}
1729
1730/**
1731 * @brief Peek element at the tail of queue.
1732 *
1733 * Return element from the tail of queue without removing it.
1734 *
1735 * @param queue Address of the queue.
1736 *
1737 * @return Tail element, or NULL if queue is empty.
1738 */
1739static inline void *k_queue_peek_tail(struct k_queue *queue)
1740{
1741 return sys_slist_peek_tail(&queue->data_q);
1742}
1743
1744/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001745 * @brief Statically define and initialize a queue.
1746 *
1747 * The queue can be accessed outside the module where it is defined using:
1748 *
1749 * @code extern struct k_queue <name>; @endcode
1750 *
1751 * @param name Name of the queue.
1752 */
1753#define K_QUEUE_DEFINE(name) \
1754 struct k_queue name \
1755 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001756 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001757
Anas Nashif166f5192018-02-25 08:02:36 -06001758/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001759
1760/**
1761 * @cond INTERNAL_HIDDEN
1762 */
1763
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001764struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001765 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766};
1767
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001768#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001769 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001770 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001771 }
1772
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001773#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1774
Allan Stephensc98da842016-11-11 15:45:03 -05001775/**
1776 * INTERNAL_HIDDEN @endcond
1777 */
1778
1779/**
1780 * @defgroup fifo_apis Fifo APIs
1781 * @ingroup kernel_apis
1782 * @{
1783 */
1784
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001786 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791 *
1792 * @return N/A
1793 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001794#define k_fifo_init(fifo) \
1795 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796
1797/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001798 * @brief Cancel waiting on a fifo.
1799 *
1800 * This routine causes first thread pending on @a fifo, if any, to
1801 * return from k_fifo_get() call with NULL value (as if timeout
1802 * expired).
1803 *
1804 * @note Can be called by ISRs.
1805 *
1806 * @param fifo Address of the fifo.
1807 *
1808 * @return N/A
1809 */
1810#define k_fifo_cancel_wait(fifo) \
1811 k_queue_cancel_wait((struct k_queue *) fifo)
1812
1813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * This routine adds a data item to @a fifo. A fifo data item must be
1817 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1818 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @note Can be called by ISRs.
1821 *
1822 * @param fifo Address of the fifo.
1823 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 *
1825 * @return N/A
1826 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001827#define k_fifo_put(fifo, data) \
1828 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829
1830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001833 * This routine adds a list of data items to @a fifo in one operation.
1834 * The data items must be in a singly-linked list, with the first 32 bits
1835 * each data item pointing to the next data item; the list must be
1836 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * @param fifo Address of the fifo.
1841 * @param head Pointer to first node in singly-linked list.
1842 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001843 *
1844 * @return N/A
1845 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001846#define k_fifo_put_list(fifo, head, tail) \
1847 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848
1849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * This routine adds a list of data items to @a fifo in one operation.
1853 * The data items must be in a singly-linked list implemented using a
1854 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 * and must be re-initialized via sys_slist_init().
1856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * @note Can be called by ISRs.
1858 *
1859 * @param fifo Address of the fifo.
1860 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 *
1862 * @return N/A
1863 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001864#define k_fifo_put_slist(fifo, list) \
1865 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866
1867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * This routine removes a data item from @a fifo in a "first in, first out"
1871 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1874 *
1875 * @param fifo Address of the fifo.
1876 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 * or one of the special values K_NO_WAIT and K_FOREVER.
1878 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001879 * @return Address of the data item if successful; NULL if returned
1880 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001882#define k_fifo_get(fifo, timeout) \
1883 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001886 * @brief Query a fifo to see if it has data available.
1887 *
1888 * Note that the data might be already gone by the time this function returns
1889 * if other threads is also trying to read from the fifo.
1890 *
1891 * @note Can be called by ISRs.
1892 *
1893 * @param fifo Address of the fifo.
1894 *
1895 * @return Non-zero if the fifo is empty.
1896 * @return 0 if data is available.
1897 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001898#define k_fifo_is_empty(fifo) \
1899 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001900
1901/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001902 * @brief Peek element at the head of fifo.
1903 *
1904 * Return element from the head of fifo without removing it. A usecase
1905 * for this is if elements of the fifo are themselves containers. Then
1906 * on each iteration of processing, a head container will be peeked,
1907 * and some data processed out of it, and only if the container is empty,
1908 * it will be completely remove from the fifo.
1909 *
1910 * @param fifo Address of the fifo.
1911 *
1912 * @return Head element, or NULL if the fifo is empty.
1913 */
1914#define k_fifo_peek_head(fifo) \
1915 k_queue_peek_head((struct k_queue *) fifo)
1916
1917/**
1918 * @brief Peek element at the tail of fifo.
1919 *
1920 * Return element from the tail of fifo (without removing it). A usecase
1921 * for this is if elements of the fifo are themselves containers. Then
1922 * it may be useful to add more data to the last container in fifo.
1923 *
1924 * @param fifo Address of the fifo.
1925 *
1926 * @return Tail element, or NULL if fifo is empty.
1927 */
1928#define k_fifo_peek_tail(fifo) \
1929 k_queue_peek_tail((struct k_queue *) fifo)
1930
1931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001932 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001936 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001938 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001941 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001942 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001943 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944
Anas Nashif166f5192018-02-25 08:02:36 -06001945/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001946
1947/**
1948 * @cond INTERNAL_HIDDEN
1949 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950
1951struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001952 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001953};
1954
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001955#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001956 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001957 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001958 }
1959
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001960#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1961
Allan Stephensc98da842016-11-11 15:45:03 -05001962/**
1963 * INTERNAL_HIDDEN @endcond
1964 */
1965
1966/**
1967 * @defgroup lifo_apis Lifo APIs
1968 * @ingroup kernel_apis
1969 * @{
1970 */
1971
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
1979 * @return N/A
1980 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001981#define k_lifo_init(lifo) \
1982 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983
1984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001985 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * This routine adds a data item to @a lifo. A lifo data item must be
1988 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1989 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991 * @note Can be called by ISRs.
1992 *
1993 * @param lifo Address of the lifo.
1994 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995 *
1996 * @return N/A
1997 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001998#define k_lifo_put(lifo, data) \
1999 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000
2001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * This routine removes a data item from @a lifo in a "last in, first out"
2005 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2008 *
2009 * @param lifo Address of the lifo.
2010 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 * or one of the special values K_NO_WAIT and K_FOREVER.
2012 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002013 * @return Address of the data item if successful; NULL if returned
2014 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002016#define k_lifo_get(lifo, timeout) \
2017 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002023 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002024 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002029 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002030 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002031 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032
Anas Nashif166f5192018-02-25 08:02:36 -06002033/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002034
2035/**
2036 * @cond INTERNAL_HIDDEN
2037 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002038
2039struct k_stack {
2040 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002041 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042
Anas Nashif2f203c22016-12-18 06:57:45 -05002043 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044};
2045
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002046#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002047 { \
2048 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2049 .base = stack_buffer, \
2050 .next = stack_buffer, \
2051 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002052 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002053 }
2054
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002055#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2056
Allan Stephensc98da842016-11-11 15:45:03 -05002057/**
2058 * INTERNAL_HIDDEN @endcond
2059 */
2060
2061/**
2062 * @defgroup stack_apis Stack APIs
2063 * @ingroup kernel_apis
2064 * @{
2065 */
2066
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002067/**
2068 * @brief Initialize a stack.
2069 *
2070 * This routine initializes a stack object, prior to its first use.
2071 *
2072 * @param stack Address of the stack.
2073 * @param buffer Address of array used to hold stacked values.
2074 * @param num_entries Maximum number of values that can be stacked.
2075 *
2076 * @return N/A
2077 */
Andrew Boiee8734462017-09-29 16:42:07 -07002078__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002079 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080
2081/**
2082 * @brief Push an element onto a stack.
2083 *
2084 * This routine adds a 32-bit value @a data to @a stack.
2085 *
2086 * @note Can be called by ISRs.
2087 *
2088 * @param stack Address of the stack.
2089 * @param data Value to push onto the stack.
2090 *
2091 * @return N/A
2092 */
Andrew Boiee8734462017-09-29 16:42:07 -07002093__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094
2095/**
2096 * @brief Pop an element from a stack.
2097 *
2098 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2099 * manner and stores the value in @a data.
2100 *
2101 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2102 *
2103 * @param stack Address of the stack.
2104 * @param data Address of area to hold the value popped from the stack.
2105 * @param timeout Waiting period to obtain a value (in milliseconds),
2106 * or one of the special values K_NO_WAIT and K_FOREVER.
2107 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002108 * @retval 0 Element popped from stack.
2109 * @retval -EBUSY Returned without waiting.
2110 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 */
Andrew Boiee8734462017-09-29 16:42:07 -07002112__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002118 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002119 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * @param name Name of the stack.
2122 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002124#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002125 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002126 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002127 struct k_stack name \
2128 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002129 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002130 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131
Anas Nashif166f5192018-02-25 08:02:36 -06002132/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002133
Allan Stephens6bba9b02016-11-16 14:56:54 -05002134struct k_work;
2135
Allan Stephensc98da842016-11-11 15:45:03 -05002136/**
2137 * @defgroup workqueue_apis Workqueue Thread APIs
2138 * @ingroup kernel_apis
2139 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002140 */
2141
Allan Stephens6bba9b02016-11-16 14:56:54 -05002142/**
2143 * @typedef k_work_handler_t
2144 * @brief Work item handler function type.
2145 *
2146 * A work item's handler function is executed by a workqueue's thread
2147 * when the work item is processed by the workqueue.
2148 *
2149 * @param work Address of the work item.
2150 *
2151 * @return N/A
2152 */
2153typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154
2155/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002156 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002158
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002160 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002161 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162};
2163
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002165 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002166};
2167
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002168struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002169 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002170 k_work_handler_t handler;
2171 atomic_t flags[1];
2172};
2173
Allan Stephens6bba9b02016-11-16 14:56:54 -05002174struct k_delayed_work {
2175 struct k_work work;
2176 struct _timeout timeout;
2177 struct k_work_q *work_q;
2178};
2179
2180extern struct k_work_q k_sys_work_q;
2181
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002183 * INTERNAL_HIDDEN @endcond
2184 */
2185
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002186#define _K_WORK_INITIALIZER(work_handler) \
2187 { \
2188 ._reserved = NULL, \
2189 .handler = work_handler, \
2190 .flags = { 0 } \
2191 }
2192
2193#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2194
Allan Stephens6bba9b02016-11-16 14:56:54 -05002195/**
2196 * @brief Initialize a statically-defined work item.
2197 *
2198 * This macro can be used to initialize a statically-defined workqueue work
2199 * item, prior to its first use. For example,
2200 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002201 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002202 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002203 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002204 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002206#define K_WORK_DEFINE(work, work_handler) \
2207 struct k_work work \
2208 __in_section(_k_work, static, work) = \
2209 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210
2211/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @brief Initialize a work item.
2213 *
2214 * This routine initializes a workqueue work item, prior to its first use.
2215 *
2216 * @param work Address of work item.
2217 * @param handler Function to invoke each time work item is processed.
2218 *
2219 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 */
2221static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2222{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002223 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002225 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226}
2227
2228/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002230 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * This routine submits work item @a work to be processed by workqueue
2232 * @a work_q. If the work item is already pending in the workqueue's queue
2233 * as a result of an earlier submission, this routine has no effect on the
2234 * work item. If the work item has already been processed, or is currently
2235 * being processed, its work is considered complete and the work item can be
2236 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002237 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238 * @warning
2239 * A submitted work item must not be modified until it has been processed
2240 * by the workqueue.
2241 *
2242 * @note Can be called by ISRs.
2243 *
2244 * @param work_q Address of workqueue.
2245 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002246 *
2247 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248 */
2249static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2250 struct k_work *work)
2251{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002252 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002253 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254 }
2255}
2256
2257/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002258 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002259 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * This routine indicates if work item @a work is pending in a workqueue's
2261 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002263 * @note Can be called by ISRs.
2264 *
2265 * @param work Address of work item.
2266 *
2267 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002268 */
2269static inline int k_work_pending(struct k_work *work)
2270{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002271 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002272}
2273
2274/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002275 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002277 * This routine starts workqueue @a work_q. The workqueue spawns its work
2278 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002280 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002281 * @param stack Pointer to work queue thread's stack space, as defined by
2282 * K_THREAD_STACK_DEFINE()
2283 * @param stack_size Size of the work queue thread's stack (in bytes), which
2284 * should either be the same constant passed to
2285 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002286 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
2288 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289 */
Andrew Boie507852a2017-07-25 18:47:07 -07002290extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002291 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002292 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002297 * This routine initializes a workqueue delayed work item, prior to
2298 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300 * @param work Address of delayed work item.
2301 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002302 *
2303 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002305extern void k_delayed_work_init(struct k_delayed_work *work,
2306 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307
2308/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002309 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002311 * This routine schedules work item @a work to be processed by workqueue
2312 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002313 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002314 * Only when the countdown completes is the work item actually submitted to
2315 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002317 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002318 * counting down cancels the existing submission and restarts the
2319 * countdown using the new delay. Note that this behavior is
2320 * inherently subject to race conditions with the pre-existing
2321 * timeouts and work queue, so care must be taken to synchronize such
2322 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * @warning
2325 * A delayed work item must not be modified until it has been processed
2326 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002328 * @note Can be called by ISRs.
2329 *
2330 * @param work_q Address of workqueue.
2331 * @param work Address of delayed work item.
2332 * @param delay Delay before submitting the work item (in milliseconds).
2333 *
2334 * @retval 0 Work item countdown started.
2335 * @retval -EINPROGRESS Work item is already pending.
2336 * @retval -EINVAL Work item is being processed or has completed its work.
2337 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002339extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2340 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002341 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342
2343/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002347 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002348 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002350 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002352 * @param work Address of delayed work item.
2353 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002354 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002355 * @retval -EINPROGRESS Work item is already pending.
2356 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002358extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361 * @brief Submit a work item to the system workqueue.
2362 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002363 * This routine submits work item @a work to be processed by the system
2364 * workqueue. If the work item is already pending in the workqueue's queue
2365 * as a result of an earlier submission, this routine has no effect on the
2366 * work item. If the work item has already been processed, or is currently
2367 * being processed, its work is considered complete and the work item can be
2368 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002370 * @warning
2371 * Work items submitted to the system workqueue should avoid using handlers
2372 * that block or yield since this may prevent the system workqueue from
2373 * processing other work items in a timely manner.
2374 *
2375 * @note Can be called by ISRs.
2376 *
2377 * @param work Address of work item.
2378 *
2379 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380 */
2381static inline void k_work_submit(struct k_work *work)
2382{
2383 k_work_submit_to_queue(&k_sys_work_q, work);
2384}
2385
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002386/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 * @brief Submit a delayed work item to the system workqueue.
2388 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002389 * This routine schedules work item @a work to be processed by the system
2390 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002391 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002392 * Only when the countdown completes is the work item actually submitted to
2393 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002394 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002395 * Submitting a previously submitted delayed work item that is still
2396 * counting down cancels the existing submission and restarts the countdown
2397 * using the new delay. If the work item is currently pending on the
2398 * workqueue's queue because the countdown has completed it is too late to
2399 * resubmit the item, and resubmission fails without impacting the work item.
2400 * If the work item has already been processed, or is currently being processed,
2401 * its work is considered complete and the work item can be resubmitted.
2402 *
2403 * @warning
2404 * Work items submitted to the system workqueue should avoid using handlers
2405 * that block or yield since this may prevent the system workqueue from
2406 * processing other work items in a timely manner.
2407 *
2408 * @note Can be called by ISRs.
2409 *
2410 * @param work Address of delayed work item.
2411 * @param delay Delay before submitting the work item (in milliseconds).
2412 *
2413 * @retval 0 Work item countdown started.
2414 * @retval -EINPROGRESS Work item is already pending.
2415 * @retval -EINVAL Work item is being processed or has completed its work.
2416 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417 */
2418static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002419 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002421 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422}
2423
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002425 * @brief Get time remaining before a delayed work gets scheduled.
2426 *
2427 * This routine computes the (approximate) time remaining before a
2428 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002429 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002430 *
2431 * @param work Delayed work item.
2432 *
2433 * @return Remaining time (in milliseconds).
2434 */
Kumar Galacc334c72017-04-21 10:55:34 -05002435static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002436{
2437 return _timeout_remaining_get(&work->timeout);
2438}
2439
Anas Nashif166f5192018-02-25 08:02:36 -06002440/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441
Allan Stephensc98da842016-11-11 15:45:03 -05002442/**
2443 * @cond INTERNAL_HIDDEN
2444 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
2446struct k_mutex {
2447 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002448 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002449 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451
Anas Nashif2f203c22016-12-18 06:57:45 -05002452 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453};
2454
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002455#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 { \
2457 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2458 .owner = NULL, \
2459 .lock_count = 0, \
2460 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002461 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462 }
2463
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002464#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2465
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466/**
Allan Stephensc98da842016-11-11 15:45:03 -05002467 * INTERNAL_HIDDEN @endcond
2468 */
2469
2470/**
2471 * @defgroup mutex_apis Mutex APIs
2472 * @ingroup kernel_apis
2473 * @{
2474 */
2475
2476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002481 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002486 struct k_mutex name \
2487 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002488 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * Upon completion, the mutex is available and does not have an owner.
2496 *
2497 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
2499 * @return N/A
2500 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002501__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502
2503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * This routine locks @a mutex. If the mutex is locked by another thread,
2507 * the calling thread waits until the mutex becomes available or until
2508 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * A thread is permitted to lock a mutex it has already locked. The operation
2511 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @param mutex Address of the mutex.
2514 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002515 * or one of the special values K_NO_WAIT and K_FOREVER.
2516 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002517 * @retval 0 Mutex locked.
2518 * @retval -EBUSY Returned without waiting.
2519 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002521__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522
2523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * This routine unlocks @a mutex. The mutex must already be locked by the
2527 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
2529 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 * thread.
2532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002534 *
2535 * @return N/A
2536 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002537__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538
Allan Stephensc98da842016-11-11 15:45:03 -05002539/**
Anas Nashif166f5192018-02-25 08:02:36 -06002540 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002541 */
2542
2543/**
2544 * @cond INTERNAL_HIDDEN
2545 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
2547struct k_sem {
2548 _wait_q_t wait_q;
2549 unsigned int count;
2550 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002551 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552
Anas Nashif2f203c22016-12-18 06:57:45 -05002553 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554};
2555
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002556#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002557 { \
2558 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2559 .count = initial_count, \
2560 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002561 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002562 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002563 }
2564
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002565#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2566
Allan Stephensc98da842016-11-11 15:45:03 -05002567/**
2568 * INTERNAL_HIDDEN @endcond
2569 */
2570
2571/**
2572 * @defgroup semaphore_apis Semaphore APIs
2573 * @ingroup kernel_apis
2574 * @{
2575 */
2576
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * @param sem Address of the semaphore.
2583 * @param initial_count Initial semaphore count.
2584 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585 *
2586 * @return N/A
2587 */
Andrew Boie99280232017-09-29 14:17:47 -07002588__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2589 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002590
2591/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2597 *
2598 * @param sem Address of the semaphore.
2599 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002600 * or one of the special values K_NO_WAIT and K_FOREVER.
2601 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002602 * @note When porting code from the nanokernel legacy API to the new API, be
2603 * careful with the return value of this function. The return value is the
2604 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2605 * non-zero means failure, while the nano_sem_take family returns 1 for success
2606 * and 0 for failure.
2607 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002608 * @retval 0 Semaphore taken.
2609 * @retval -EBUSY Returned without waiting.
2610 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 */
Andrew Boie99280232017-09-29 14:17:47 -07002612__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002613
2614/**
2615 * @brief Give a semaphore.
2616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * This routine gives @a sem, unless the semaphore is already at its maximum
2618 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623 *
2624 * @return N/A
2625 */
Andrew Boie99280232017-09-29 14:17:47 -07002626__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002628/**
2629 * @brief Reset a semaphore's count to zero.
2630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002634 *
2635 * @return N/A
2636 */
Andrew Boie990bf162017-10-03 12:36:49 -07002637__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002638
2639static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640{
2641 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642}
2643
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002644/**
2645 * @brief Get a semaphore's count.
2646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002652 */
Andrew Boie990bf162017-10-03 12:36:49 -07002653__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002654
2655static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656{
2657 return sem->count;
2658}
2659
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002660/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002665 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @param name Name of the semaphore.
2668 * @param initial_count Initial semaphore count.
2669 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002670 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002672 struct k_sem name \
2673 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002674 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675
Anas Nashif166f5192018-02-25 08:02:36 -06002676/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002677
2678/**
2679 * @defgroup alert_apis Alert APIs
2680 * @ingroup kernel_apis
2681 * @{
2682 */
2683
Allan Stephens5eceb852016-11-16 10:16:30 -05002684/**
2685 * @typedef k_alert_handler_t
2686 * @brief Alert handler function type.
2687 *
2688 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002689 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002690 * and is only invoked if the alert has been initialized with one.
2691 *
2692 * @param alert Address of the alert.
2693 *
2694 * @return 0 if alert has been consumed; non-zero if alert should pend.
2695 */
2696typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002697
Anas Nashif166f5192018-02-25 08:02:36 -06002698/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002699
2700/**
2701 * @cond INTERNAL_HIDDEN
2702 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002704#define K_ALERT_DEFAULT NULL
2705#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002707struct k_alert {
2708 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709 atomic_t send_count;
2710 struct k_work work_item;
2711 struct k_sem sem;
2712
Anas Nashif2f203c22016-12-18 06:57:45 -05002713 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714};
2715
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002716extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002718#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002720 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002722 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2723 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002724 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725 }
2726
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002727#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2728
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002729/**
Allan Stephensc98da842016-11-11 15:45:03 -05002730 * INTERNAL_HIDDEN @endcond
2731 */
2732
2733/**
2734 * @addtogroup alert_apis
2735 * @{
2736 */
2737
2738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002741 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002743 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * @param name Name of the alert.
2746 * @param alert_handler Action to take when alert is sent. Specify either
2747 * the address of a function to be invoked by the system workqueue
2748 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2749 * K_ALERT_DEFAULT (which causes the alert to pend).
2750 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002751 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002752#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002753 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002754 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002755 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002756 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763 * @param alert Address of the alert.
2764 * @param handler Action to take when alert is sent. Specify either the address
2765 * of a function to be invoked by the system workqueue thread,
2766 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2767 * K_ALERT_DEFAULT (which causes the alert to pend).
2768 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002769 *
2770 * @return N/A
2771 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002772extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2773 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002774
2775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002776 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002778 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2781 *
2782 * @param alert Address of the alert.
2783 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002784 * or one of the special values K_NO_WAIT and K_FOREVER.
2785 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002786 * @retval 0 Alert received.
2787 * @retval -EBUSY Returned without waiting.
2788 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 */
Andrew Boie310e9872017-09-29 04:41:15 -07002790__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791
2792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * This routine signals @a alert. The action specified for @a alert will
2796 * be taken, which may trigger the execution of an alert handler function
2797 * and/or cause the alert to pend (assuming the alert has not reached its
2798 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @note Can be called by ISRs.
2801 *
2802 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803 *
2804 * @return N/A
2805 */
Andrew Boie310e9872017-09-29 04:41:15 -07002806__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807
2808/**
Anas Nashif166f5192018-02-25 08:02:36 -06002809 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810 */
2811
Allan Stephensc98da842016-11-11 15:45:03 -05002812/**
2813 * @cond INTERNAL_HIDDEN
2814 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815
2816struct k_msgq {
2817 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002818 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002819 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820 char *buffer_start;
2821 char *buffer_end;
2822 char *read_ptr;
2823 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002824 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002825
Anas Nashif2f203c22016-12-18 06:57:45 -05002826 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827};
2828
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002829#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 { \
2831 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002832 .max_msgs = q_max_msgs, \
2833 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002835 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 .read_ptr = q_buffer, \
2837 .write_ptr = q_buffer, \
2838 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002839 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 }
2841
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002842#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2843
Peter Mitsis1da807e2016-10-06 11:36:59 -04002844/**
Allan Stephensc98da842016-11-11 15:45:03 -05002845 * INTERNAL_HIDDEN @endcond
2846 */
2847
2848/**
2849 * @defgroup msgq_apis Message Queue APIs
2850 * @ingroup kernel_apis
2851 * @{
2852 */
2853
2854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2858 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002859 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2860 * message is similarly aligned to this boundary, @a q_msg_size must also be
2861 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * The message queue can be accessed outside the module where it is defined
2864 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002866 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * @param q_name Name of the message queue.
2869 * @param q_msg_size Message size (in bytes).
2870 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002871 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002872 */
2873#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2874 static char __noinit __aligned(q_align) \
2875 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002876 struct k_msgq q_name \
2877 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002878 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002879 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002880
Peter Mitsisd7a37502016-10-13 11:37:40 -04002881/**
2882 * @brief Initialize a message queue.
2883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * This routine initializes a message queue object, prior to its first use.
2885 *
Allan Stephensda827222016-11-09 14:23:58 -06002886 * The message queue's ring buffer must contain space for @a max_msgs messages,
2887 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2888 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2889 * that each message is similarly aligned to this boundary, @a q_msg_size
2890 * must also be a multiple of N.
2891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * @param q Address of the message queue.
2893 * @param buffer Pointer to ring buffer that holds queued messages.
2894 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002895 * @param max_msgs Maximum number of messages that can be queued.
2896 *
2897 * @return N/A
2898 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002899__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2900 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002901
2902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002906 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002907 * @note Can be called by ISRs.
2908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @param q Address of the message queue.
2910 * @param data Pointer to the message.
2911 * @param timeout Waiting period to add the message (in milliseconds),
2912 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002913 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002914 * @retval 0 Message sent.
2915 * @retval -ENOMSG Returned without waiting or queue purged.
2916 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002917 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002918__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002919
2920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * This routine receives a message from message queue @a q in a "first in,
2924 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002925 *
Allan Stephensc98da842016-11-11 15:45:03 -05002926 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002928 * @param q Address of the message queue.
2929 * @param data Address of area to hold the received message.
2930 * @param timeout Waiting period to receive the message (in milliseconds),
2931 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002932 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002933 * @retval 0 Message received.
2934 * @retval -ENOMSG Returned without waiting.
2935 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002936 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002937__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002938
2939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * This routine discards all unreceived messages in a message queue's ring
2943 * buffer. Any threads that are blocked waiting to send a message to the
2944 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002947 *
2948 * @return N/A
2949 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002950__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951
Peter Mitsis67be2492016-10-07 11:44:34 -04002952/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * This routine returns the number of unused entries in a message queue's
2956 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * @param q Address of the message queue.
2959 *
2960 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002961 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002962__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2963
2964static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002965{
2966 return q->max_msgs - q->used_msgs;
2967}
2968
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * @param q Address of the message queue.
2975 *
2976 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002977 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002978__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2979
2980static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981{
2982 return q->used_msgs;
2983}
2984
Anas Nashif166f5192018-02-25 08:02:36 -06002985/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002986
2987/**
2988 * @defgroup mem_pool_apis Memory Pool APIs
2989 * @ingroup kernel_apis
2990 * @{
2991 */
2992
Andy Ross73cb9582017-05-09 10:42:39 -07002993/* Note on sizing: the use of a 20 bit field for block means that,
2994 * assuming a reasonable minimum block size of 16 bytes, we're limited
2995 * to 16M of memory managed by a single pool. Long term it would be
2996 * good to move to a variable bit size based on configuration.
2997 */
2998struct k_mem_block_id {
2999 u32_t pool : 8;
3000 u32_t level : 4;
3001 u32_t block : 20;
3002};
3003
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003006 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007};
3008
Anas Nashif166f5192018-02-25 08:02:36 -06003009/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003010
3011/**
3012 * @defgroup mailbox_apis Mailbox APIs
3013 * @ingroup kernel_apis
3014 * @{
3015 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016
3017struct k_mbox_msg {
3018 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003019 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003021 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003023 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024 /** sender's message data buffer */
3025 void *tx_data;
3026 /** internal use only - needed for legacy API support */
3027 void *_rx_data;
3028 /** message data block descriptor */
3029 struct k_mem_block tx_block;
3030 /** source thread id */
3031 k_tid_t rx_source_thread;
3032 /** target thread id */
3033 k_tid_t tx_target_thread;
3034 /** internal use only - thread waiting on send (may be a dummy) */
3035 k_tid_t _syncing_thread;
3036#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3037 /** internal use only - semaphore used during asynchronous send */
3038 struct k_sem *_async_sem;
3039#endif
3040};
3041
Allan Stephensc98da842016-11-11 15:45:03 -05003042/**
3043 * @cond INTERNAL_HIDDEN
3044 */
3045
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046struct k_mbox {
3047 _wait_q_t tx_msg_queue;
3048 _wait_q_t rx_msg_queue;
3049
Anas Nashif2f203c22016-12-18 06:57:45 -05003050 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051};
3052
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003053#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054 { \
3055 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3056 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003057 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058 }
3059
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003060#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3061
Peter Mitsis12092702016-10-14 12:57:23 -04003062/**
Allan Stephensc98da842016-11-11 15:45:03 -05003063 * INTERNAL_HIDDEN @endcond
3064 */
3065
3066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003067 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003070 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003071 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003073 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003076 struct k_mbox name \
3077 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003078 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079
Peter Mitsis12092702016-10-14 12:57:23 -04003080/**
3081 * @brief Initialize a mailbox.
3082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * This routine initializes a mailbox object, prior to its first use.
3084 *
3085 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003086 *
3087 * @return N/A
3088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089extern void k_mbox_init(struct k_mbox *mbox);
3090
Peter Mitsis12092702016-10-14 12:57:23 -04003091/**
3092 * @brief Send a mailbox message in a synchronous manner.
3093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * This routine sends a message to @a mbox and waits for a receiver to both
3095 * receive and process it. The message data may be in a buffer, in a memory
3096 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @param mbox Address of the mailbox.
3099 * @param tx_msg Address of the transmit message descriptor.
3100 * @param timeout Waiting period for the message to be received (in
3101 * milliseconds), or one of the special values K_NO_WAIT
3102 * and K_FOREVER. Once the message has been received,
3103 * this routine waits as long as necessary for the message
3104 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003105 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003106 * @retval 0 Message sent.
3107 * @retval -ENOMSG Returned without waiting.
3108 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003109 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003110extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003111 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003112
Peter Mitsis12092702016-10-14 12:57:23 -04003113/**
3114 * @brief Send a mailbox message in an asynchronous manner.
3115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003116 * This routine sends a message to @a mbox without waiting for a receiver
3117 * to process it. The message data may be in a buffer, in a memory pool block,
3118 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3119 * will be given when the message has been both received and completely
3120 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * @param mbox Address of the mailbox.
3123 * @param tx_msg Address of the transmit message descriptor.
3124 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003125 *
3126 * @return N/A
3127 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003128extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129 struct k_sem *sem);
3130
Peter Mitsis12092702016-10-14 12:57:23 -04003131/**
3132 * @brief Receive a mailbox message.
3133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * This routine receives a message from @a mbox, then optionally retrieves
3135 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * @param mbox Address of the mailbox.
3138 * @param rx_msg Address of the receive message descriptor.
3139 * @param buffer Address of the buffer to receive data, or NULL to defer data
3140 * retrieval and message disposal until later.
3141 * @param timeout Waiting period for a message to be received (in
3142 * milliseconds), or one of the special values K_NO_WAIT
3143 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003144 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003145 * @retval 0 Message received.
3146 * @retval -ENOMSG Returned without waiting.
3147 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003148 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003149extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003150 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003151
3152/**
3153 * @brief Retrieve mailbox message data into a buffer.
3154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * This routine completes the processing of a received message by retrieving
3156 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003157 *
3158 * Alternatively, this routine can be used to dispose of a received message
3159 * without retrieving its data.
3160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @param rx_msg Address of the receive message descriptor.
3162 * @param buffer Address of the buffer to receive data, or NULL to discard
3163 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003164 *
3165 * @return N/A
3166 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003167extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003168
3169/**
3170 * @brief Retrieve mailbox message data into a memory pool block.
3171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * This routine completes the processing of a received message by retrieving
3173 * its data into a memory pool block, then disposing of the message.
3174 * The memory pool block that results from successful retrieval must be
3175 * returned to the pool once the data has been processed, even in cases
3176 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003177 *
3178 * Alternatively, this routine can be used to dispose of a received message
3179 * without retrieving its data. In this case there is no need to return a
3180 * memory pool block to the pool.
3181 *
3182 * This routine allocates a new memory pool block for the data only if the
3183 * data is not already in one. If a new block cannot be allocated, the routine
3184 * returns a failure code and the received message is left unchanged. This
3185 * permits the caller to reattempt data retrieval at a later time or to dispose
3186 * of the received message without retrieving its data.
3187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * @param rx_msg Address of a receive message descriptor.
3189 * @param pool Address of memory pool, or NULL to discard data.
3190 * @param block Address of the area to hold memory pool block info.
3191 * @param timeout Waiting period to wait for a memory pool block (in
3192 * milliseconds), or one of the special values K_NO_WAIT
3193 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003194 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003195 * @retval 0 Data retrieved.
3196 * @retval -ENOMEM Returned without waiting.
3197 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003198 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003199extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003200 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003201 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003202
Anas Nashif166f5192018-02-25 08:02:36 -06003203/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003204
3205/**
3206 * @cond INTERNAL_HIDDEN
3207 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208
3209struct k_pipe {
3210 unsigned char *buffer; /* Pipe buffer: may be NULL */
3211 size_t size; /* Buffer size */
3212 size_t bytes_used; /* # bytes used in buffer */
3213 size_t read_index; /* Where in buffer to read from */
3214 size_t write_index; /* Where in buffer to write */
3215
3216 struct {
3217 _wait_q_t readers; /* Reader wait queue */
3218 _wait_q_t writers; /* Writer wait queue */
3219 } wait_q;
3220
Anas Nashif2f203c22016-12-18 06:57:45 -05003221 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222};
3223
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003224#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225 { \
3226 .buffer = pipe_buffer, \
3227 .size = pipe_buffer_size, \
3228 .bytes_used = 0, \
3229 .read_index = 0, \
3230 .write_index = 0, \
3231 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3232 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003233 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 }
3235
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003236#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3237
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003238/**
Allan Stephensc98da842016-11-11 15:45:03 -05003239 * INTERNAL_HIDDEN @endcond
3240 */
3241
3242/**
3243 * @defgroup pipe_apis Pipe APIs
3244 * @ingroup kernel_apis
3245 * @{
3246 */
3247
3248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003252 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003253 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param name Name of the pipe.
3256 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3257 * or zero if no ring buffer is used.
3258 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003260#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3261 static unsigned char __noinit __aligned(pipe_align) \
3262 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003263 struct k_pipe name \
3264 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003265 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * This routine initializes a pipe object, prior to its first use.
3271 *
3272 * @param pipe Address of the pipe.
3273 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3274 * is used.
3275 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3276 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277 *
3278 * @return N/A
3279 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003280__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3281 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282
3283/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * @param pipe Address of the pipe.
3289 * @param data Address of data to write.
3290 * @param bytes_to_write Size of data (in bytes).
3291 * @param bytes_written Address of area to hold the number of bytes written.
3292 * @param min_xfer Minimum number of bytes to write.
3293 * @param timeout Waiting period to wait for the data to be written (in
3294 * milliseconds), or one of the special values K_NO_WAIT
3295 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003297 * @retval 0 At least @a min_xfer bytes of data were written.
3298 * @retval -EIO Returned without waiting; zero data bytes were written.
3299 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003300 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003302__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3303 size_t bytes_to_write, size_t *bytes_written,
3304 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305
3306/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * @param pipe Address of the pipe.
3312 * @param data Address to place the data read from pipe.
3313 * @param bytes_to_read Maximum number of data bytes to read.
3314 * @param bytes_read Address of area to hold the number of bytes read.
3315 * @param min_xfer Minimum number of data bytes to read.
3316 * @param timeout Waiting period to wait for the data to be read (in
3317 * milliseconds), or one of the special values K_NO_WAIT
3318 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003320 * @retval 0 At least @a min_xfer bytes of data were read.
3321 * @retval -EIO Returned without waiting; zero data bytes were read.
3322 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003323 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003325__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3326 size_t bytes_to_read, size_t *bytes_read,
3327 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328
3329/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * This routine writes the data contained in a memory block to @a pipe.
3333 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337 * @param block Memory block containing data to send
3338 * @param size Number of data bytes in memory block to send
3339 * @param sem Semaphore to signal upon completion (else NULL)
3340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342 */
3343extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3344 size_t size, struct k_sem *sem);
3345
Anas Nashif166f5192018-02-25 08:02:36 -06003346/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347
Allan Stephensc98da842016-11-11 15:45:03 -05003348/**
3349 * @cond INTERNAL_HIDDEN
3350 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003352struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003354 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003355 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 char *buffer;
3357 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003358 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359
Anas Nashif2f203c22016-12-18 06:57:45 -05003360 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361};
3362
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003363#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003364 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365 { \
3366 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003367 .num_blocks = slab_num_blocks, \
3368 .block_size = slab_block_size, \
3369 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370 .free_list = NULL, \
3371 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003372 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003373 }
3374
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003375#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3376
3377
Peter Mitsis578f9112016-10-07 13:50:31 -04003378/**
Allan Stephensc98da842016-11-11 15:45:03 -05003379 * INTERNAL_HIDDEN @endcond
3380 */
3381
3382/**
3383 * @defgroup mem_slab_apis Memory Slab APIs
3384 * @ingroup kernel_apis
3385 * @{
3386 */
3387
3388/**
Allan Stephensda827222016-11-09 14:23:58 -06003389 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003390 *
Allan Stephensda827222016-11-09 14:23:58 -06003391 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003393 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3394 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003395 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003396 *
Allan Stephensda827222016-11-09 14:23:58 -06003397 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003399 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003400 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @param name Name of the memory slab.
3403 * @param slab_block_size Size of each memory block (in bytes).
3404 * @param slab_num_blocks Number memory blocks.
3405 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003406 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003407#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3408 char __noinit __aligned(slab_align) \
3409 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3410 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003411 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003412 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003413 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003415/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003416 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003419 *
Allan Stephensda827222016-11-09 14:23:58 -06003420 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3421 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3422 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3423 * To ensure that each memory block is similarly aligned to this boundary,
3424 * @a slab_block_size must also be a multiple of N.
3425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @param slab Address of the memory slab.
3427 * @param buffer Pointer to buffer used for the memory blocks.
3428 * @param block_size Size of each memory block (in bytes).
3429 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003430 *
3431 * @return N/A
3432 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003433extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003434 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003435
3436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003439 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 * @param slab Address of the memory slab.
3442 * @param mem Pointer to block address area.
3443 * @param timeout Maximum time to wait for operation to complete
3444 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3445 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003447 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003449 * @retval -ENOMEM Returned without waiting.
3450 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003451 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003452extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003453 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003454
3455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * This routine releases a previously allocated memory block back to its
3459 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * @param slab Address of the memory slab.
3462 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003463 *
3464 * @return N/A
3465 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003466extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * This routine gets the number of memory blocks that are currently
3472 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003477 */
Kumar Galacc334c72017-04-21 10:55:34 -05003478static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003480 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481}
3482
Peter Mitsisc001aa82016-10-13 13:53:37 -04003483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * This routine gets the number of memory blocks that are currently
3487 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003492 */
Kumar Galacc334c72017-04-21 10:55:34 -05003493static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003494{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003495 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003496}
3497
Anas Nashif166f5192018-02-25 08:02:36 -06003498/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003499
3500/**
3501 * @cond INTERNAL_HIDDEN
3502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003503
Andy Ross73cb9582017-05-09 10:42:39 -07003504struct k_mem_pool_lvl {
3505 union {
3506 u32_t *bits_p;
3507 u32_t bits;
3508 };
3509 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003510};
3511
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003512struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003513 void *buf;
3514 size_t max_sz;
3515 u16_t n_max;
3516 u8_t n_levels;
3517 u8_t max_inline_level;
3518 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003519 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003520};
3521
Andy Ross73cb9582017-05-09 10:42:39 -07003522#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003523
Andy Ross73cb9582017-05-09 10:42:39 -07003524#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3525
Andy Ross8cf7ff52017-11-13 14:59:13 -08003526#define __MPOOL_LVLS(maxsz, minsz) \
3527 (_MPOOL_HAVE_LVL((maxsz), (minsz), 0) + \
3528 _MPOOL_HAVE_LVL((maxsz), (minsz), 1) + \
3529 _MPOOL_HAVE_LVL((maxsz), (minsz), 2) + \
3530 _MPOOL_HAVE_LVL((maxsz), (minsz), 3) + \
3531 _MPOOL_HAVE_LVL((maxsz), (minsz), 4) + \
3532 _MPOOL_HAVE_LVL((maxsz), (minsz), 5) + \
3533 _MPOOL_HAVE_LVL((maxsz), (minsz), 6) + \
3534 _MPOOL_HAVE_LVL((maxsz), (minsz), 7) + \
3535 _MPOOL_HAVE_LVL((maxsz), (minsz), 8) + \
3536 _MPOOL_HAVE_LVL((maxsz), (minsz), 9) + \
3537 _MPOOL_HAVE_LVL((maxsz), (minsz), 10) + \
3538 _MPOOL_HAVE_LVL((maxsz), (minsz), 11) + \
3539 _MPOOL_HAVE_LVL((maxsz), (minsz), 12) + \
3540 _MPOOL_HAVE_LVL((maxsz), (minsz), 13) + \
3541 _MPOOL_HAVE_LVL((maxsz), (minsz), 14) + \
3542 _MPOOL_HAVE_LVL((maxsz), (minsz), 15))
3543
3544#define _MPOOL_MINBLK sizeof(sys_dnode_t)
3545
3546#define _MPOOL_LVLS(max, min) \
3547 __MPOOL_LVLS((max), (min) >= _MPOOL_MINBLK ? (min) : _MPOOL_MINBLK)
Andy Ross73cb9582017-05-09 10:42:39 -07003548
3549/* Rounds the needed bits up to integer multiples of u32_t */
3550#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3551 ((((n_max) << (2*(l))) + 31) / 32)
3552
3553/* One word gets stored free unioned with the pointer, otherwise the
3554 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003555 */
Andy Ross73cb9582017-05-09 10:42:39 -07003556#define _MPOOL_LBIT_WORDS(n_max, l) \
3557 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3558 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003559
Andy Ross73cb9582017-05-09 10:42:39 -07003560/* How many bytes for the bitfields of a single level? */
3561#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3562 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3563 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003564
Andy Ross73cb9582017-05-09 10:42:39 -07003565/* Size of the bitmap array that follows the buffer in allocated memory */
3566#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3567 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3568 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3569 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3570 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3571 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3572 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3573 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3574 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3575 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3576 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3577 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3578 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3579 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3580 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3581 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3582 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003583
3584/**
Allan Stephensc98da842016-11-11 15:45:03 -05003585 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003586 */
3587
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003588/**
Allan Stephensc98da842016-11-11 15:45:03 -05003589 * @addtogroup mem_pool_apis
3590 * @{
3591 */
3592
3593/**
3594 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3597 * long. The memory pool allows blocks to be repeatedly partitioned into
3598 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003599 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003600 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003601 * If the pool is to be accessed outside the module where it is defined, it
3602 * can be declared via
3603 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003604 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003607 * @param minsz Size of the smallest blocks in the pool (in bytes).
3608 * @param maxsz Size of the largest blocks in the pool (in bytes).
3609 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003611 */
Andy Ross73cb9582017-05-09 10:42:39 -07003612#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3613 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3614 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3615 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3616 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3617 .buf = _mpool_buf_##name, \
3618 .max_sz = maxsz, \
3619 .n_max = nmax, \
3620 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3621 .levels = _mpool_lvls_##name, \
3622 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003623
Peter Mitsis937042c2016-10-13 13:18:26 -04003624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003627 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003629 * @param pool Address of the memory pool.
3630 * @param block Pointer to block descriptor for the allocated memory.
3631 * @param size Amount of memory to allocate (in bytes).
3632 * @param timeout Maximum time to wait for operation to complete
3633 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3634 * or K_FOREVER to wait as long as necessary.
3635 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003636 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003637 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003638 * @retval -ENOMEM Returned without waiting.
3639 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003640 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003641extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003642 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003643
3644/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * This routine releases a previously allocated memory block back to its
3648 * memory pool.
3649 *
3650 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003651 *
3652 * @return N/A
3653 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003654extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003655
3656/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003657 * @brief Free memory allocated from a memory pool.
3658 *
3659 * This routine releases a previously allocated memory block back to its
3660 * memory pool
3661 *
3662 * @param id Memory block identifier.
3663 *
3664 * @return N/A
3665 */
3666extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3667
3668/**
Anas Nashif166f5192018-02-25 08:02:36 -06003669 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003670 */
3671
3672/**
3673 * @defgroup heap_apis Heap Memory Pool APIs
3674 * @ingroup kernel_apis
3675 * @{
3676 */
3677
3678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003679 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003682 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003687 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003688extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003689
3690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003692 *
3693 * This routine provides traditional free() semantics. The memory being
3694 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003695 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003696 * If @a ptr is NULL, no operation is performed.
3697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003699 *
3700 * @return N/A
3701 */
3702extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003703
Allan Stephensc98da842016-11-11 15:45:03 -05003704/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003705 * @brief Allocate memory from heap, array style
3706 *
3707 * This routine provides traditional calloc() semantics. Memory is
3708 * allocated from the heap memory pool and zeroed.
3709 *
3710 * @param nmemb Number of elements in the requested array
3711 * @param size Size of each array element (in bytes).
3712 *
3713 * @return Address of the allocated memory if successful; otherwise NULL.
3714 */
3715extern void *k_calloc(size_t nmemb, size_t size);
3716
Anas Nashif166f5192018-02-25 08:02:36 -06003717/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003718
Benjamin Walshacc68c12017-01-29 18:57:45 -05003719/* polling API - PRIVATE */
3720
Benjamin Walshb0179862017-02-02 16:39:57 -05003721#ifdef CONFIG_POLL
3722#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3723#else
3724#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3725#endif
3726
Benjamin Walshacc68c12017-01-29 18:57:45 -05003727/* private - implementation data created as needed, per-type */
3728struct _poller {
3729 struct k_thread *thread;
3730};
3731
3732/* private - types bit positions */
3733enum _poll_types_bits {
3734 /* can be used to ignore an event */
3735 _POLL_TYPE_IGNORE,
3736
3737 /* to be signaled by k_poll_signal() */
3738 _POLL_TYPE_SIGNAL,
3739
3740 /* semaphore availability */
3741 _POLL_TYPE_SEM_AVAILABLE,
3742
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003743 /* queue/fifo/lifo data availability */
3744 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003745
3746 _POLL_NUM_TYPES
3747};
3748
3749#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3750
3751/* private - states bit positions */
3752enum _poll_states_bits {
3753 /* default state when creating event */
3754 _POLL_STATE_NOT_READY,
3755
Benjamin Walshacc68c12017-01-29 18:57:45 -05003756 /* signaled by k_poll_signal() */
3757 _POLL_STATE_SIGNALED,
3758
3759 /* semaphore is available */
3760 _POLL_STATE_SEM_AVAILABLE,
3761
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003762 /* data is available to read on queue/fifo/lifo */
3763 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003764
3765 _POLL_NUM_STATES
3766};
3767
3768#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3769
3770#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003771 (32 - (0 \
3772 + 8 /* tag */ \
3773 + _POLL_NUM_TYPES \
3774 + _POLL_NUM_STATES \
3775 + 1 /* modes */ \
3776 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003777
Benjamin Walshacc68c12017-01-29 18:57:45 -05003778/* end of polling API - PRIVATE */
3779
3780
3781/**
3782 * @defgroup poll_apis Async polling APIs
3783 * @ingroup kernel_apis
3784 * @{
3785 */
3786
3787/* Public polling API */
3788
3789/* public - values for k_poll_event.type bitfield */
3790#define K_POLL_TYPE_IGNORE 0
3791#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3792#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003793#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3794#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003795
3796/* public - polling modes */
3797enum k_poll_modes {
3798 /* polling thread does not take ownership of objects when available */
3799 K_POLL_MODE_NOTIFY_ONLY = 0,
3800
3801 K_POLL_NUM_MODES
3802};
3803
3804/* public - values for k_poll_event.state bitfield */
3805#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003806#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3807#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003808#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3809#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003810
3811/* public - poll signal object */
3812struct k_poll_signal {
3813 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003814 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815
3816 /*
3817 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3818 * user resets it to 0.
3819 */
3820 unsigned int signaled;
3821
3822 /* custom result value passed to k_poll_signal() if needed */
3823 int result;
3824};
3825
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003826#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003827 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003828 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003829 .signaled = 0, \
3830 .result = 0, \
3831 }
3832
3833struct k_poll_event {
3834 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003835 sys_dnode_t _node;
3836
3837 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 struct _poller *poller;
3839
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003840 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003841 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003842
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003844 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003845
3846 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003847 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003848
3849 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003850 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003851
3852 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003853 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854
3855 /* per-type data */
3856 union {
3857 void *obj;
3858 struct k_poll_signal *signal;
3859 struct k_sem *sem;
3860 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003861 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003862 };
3863};
3864
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003865#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003866 { \
3867 .poller = NULL, \
3868 .type = event_type, \
3869 .state = K_POLL_STATE_NOT_READY, \
3870 .mode = event_mode, \
3871 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003872 { .obj = event_obj }, \
3873 }
3874
3875#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3876 event_tag) \
3877 { \
3878 .type = event_type, \
3879 .tag = event_tag, \
3880 .state = K_POLL_STATE_NOT_READY, \
3881 .mode = event_mode, \
3882 .unused = 0, \
3883 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003884 }
3885
3886/**
3887 * @brief Initialize one struct k_poll_event instance
3888 *
3889 * After this routine is called on a poll event, the event it ready to be
3890 * placed in an event array to be passed to k_poll().
3891 *
3892 * @param event The event to initialize.
3893 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3894 * values. Only values that apply to the same object being polled
3895 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3896 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003897 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003898 * @param obj Kernel object or poll signal.
3899 *
3900 * @return N/A
3901 */
3902
Kumar Galacc334c72017-04-21 10:55:34 -05003903extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003904 int mode, void *obj);
3905
3906/**
3907 * @brief Wait for one or many of multiple poll events to occur
3908 *
3909 * This routine allows a thread to wait concurrently for one or many of
3910 * multiple poll events to have occurred. Such events can be a kernel object
3911 * being available, like a semaphore, or a poll signal event.
3912 *
3913 * When an event notifies that a kernel object is available, the kernel object
3914 * is not "given" to the thread calling k_poll(): it merely signals the fact
3915 * that the object was available when the k_poll() call was in effect. Also,
3916 * all threads trying to acquire an object the regular way, i.e. by pending on
3917 * the object, have precedence over the thread polling on the object. This
3918 * means that the polling thread will never get the poll event on an object
3919 * until the object becomes available and its pend queue is empty. For this
3920 * reason, the k_poll() call is more effective when the objects being polled
3921 * only have one thread, the polling thread, trying to acquire them.
3922 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003923 * When k_poll() returns 0, the caller should loop on all the events that were
3924 * passed to k_poll() and check the state field for the values that were
3925 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003926 *
3927 * Before being reused for another call to k_poll(), the user has to reset the
3928 * state field to K_POLL_STATE_NOT_READY.
3929 *
3930 * @param events An array of pointers to events to be polled for.
3931 * @param num_events The number of events in the array.
3932 * @param timeout Waiting period for an event to be ready (in milliseconds),
3933 * or one of the special values K_NO_WAIT and K_FOREVER.
3934 *
3935 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003936 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003937 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003938 */
3939
3940extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003941 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003942
3943/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003944 * @brief Initialize a poll signal object.
3945 *
3946 * Ready a poll signal object to be signaled via k_poll_signal().
3947 *
3948 * @param signal A poll signal.
3949 *
3950 * @return N/A
3951 */
3952
3953extern void k_poll_signal_init(struct k_poll_signal *signal);
3954
3955/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003956 * @brief Signal a poll signal object.
3957 *
3958 * This routine makes ready a poll signal, which is basically a poll event of
3959 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3960 * made ready to run. A @a result value can be specified.
3961 *
3962 * The poll signal contains a 'signaled' field that, when set by
3963 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3964 * be reset by the user before being passed again to k_poll() or k_poll() will
3965 * consider it being signaled, and will return immediately.
3966 *
3967 * @param signal A poll signal.
3968 * @param result The value to store in the result field of the signal.
3969 *
3970 * @retval 0 The signal was delivered successfully.
3971 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3972 */
3973
3974extern int k_poll_signal(struct k_poll_signal *signal, int result);
3975
3976/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003977extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003978
Anas Nashif166f5192018-02-25 08:02:36 -06003979/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003980
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003981/**
3982 * @brief Make the CPU idle.
3983 *
3984 * This function makes the CPU idle until an event wakes it up.
3985 *
3986 * In a regular system, the idle thread should be the only thread responsible
3987 * for making the CPU idle and triggering any type of power management.
3988 * However, in some more constrained systems, such as a single-threaded system,
3989 * the only thread would be responsible for this if needed.
3990 *
3991 * @return N/A
3992 */
3993extern void k_cpu_idle(void);
3994
3995/**
3996 * @brief Make the CPU idle in an atomic fashion.
3997 *
3998 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3999 * must be done atomically before making the CPU idle.
4000 *
4001 * @param key Interrupt locking key obtained from irq_lock().
4002 *
4003 * @return N/A
4004 */
4005extern void k_cpu_atomic_idle(unsigned int key);
4006
Kumar Galacc334c72017-04-21 10:55:34 -05004007extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004008
Andrew Boiecdb94d62017-04-18 15:22:05 -07004009#ifdef _ARCH_EXCEPT
4010/* This archtecture has direct support for triggering a CPU exception */
4011#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4012#else
4013
Andrew Boiecdb94d62017-04-18 15:22:05 -07004014/* NOTE: This is the implementation for arches that do not implement
4015 * _ARCH_EXCEPT() to generate a real CPU exception.
4016 *
4017 * We won't have a real exception frame to determine the PC value when
4018 * the oops occurred, so print file and line number before we jump into
4019 * the fatal error handler.
4020 */
4021#define _k_except_reason(reason) do { \
4022 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4023 _NanoFatalErrorHandler(reason, &_default_esf); \
4024 CODE_UNREACHABLE; \
4025 } while (0)
4026
4027#endif /* _ARCH__EXCEPT */
4028
4029/**
4030 * @brief Fatally terminate a thread
4031 *
4032 * This should be called when a thread has encountered an unrecoverable
4033 * runtime condition and needs to terminate. What this ultimately
4034 * means is determined by the _fatal_error_handler() implementation, which
4035 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4036 *
4037 * If this is called from ISR context, the default system fatal error handler
4038 * will treat it as an unrecoverable system error, just like k_panic().
4039 */
4040#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4041
4042/**
4043 * @brief Fatally terminate the system
4044 *
4045 * This should be called when the Zephyr kernel has encountered an
4046 * unrecoverable runtime condition and needs to terminate. What this ultimately
4047 * means is determined by the _fatal_error_handler() implementation, which
4048 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4049 */
4050#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4051
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004052/*
4053 * private APIs that are utilized by one or more public APIs
4054 */
4055
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004056#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004057extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004058#else
4059#define _init_static_threads() do { } while ((0))
4060#endif
4061
4062extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004063extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004064
Andrew Boiedc5d9352017-06-02 12:56:47 -07004065/* arch/cpu.h may declare an architecture or platform-specific macro
4066 * for properly declaring stacks, compatible with MMU/MPU constraints if
4067 * enabled
4068 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004069
4070/**
4071 * @brief Obtain an extern reference to a stack
4072 *
4073 * This macro properly brings the symbol of a thread stack declared
4074 * elsewhere into scope.
4075 *
4076 * @param sym Thread stack symbol name
4077 */
4078#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4079
Andrew Boiedc5d9352017-06-02 12:56:47 -07004080#ifdef _ARCH_THREAD_STACK_DEFINE
4081#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4082#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4083 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4084#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4085#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004086static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004087{
4088 return _ARCH_THREAD_STACK_BUFFER(sym);
4089}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004090#else
4091/**
4092 * @brief Declare a toplevel thread stack memory region
4093 *
4094 * This declares a region of memory suitable for use as a thread's stack.
4095 *
4096 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4097 * 'noinit' section so that it isn't zeroed at boot
4098 *
Andrew Boie507852a2017-07-25 18:47:07 -07004099 * The declared symbol will always be a k_thread_stack_t which can be passed to
4100 * k_thread_create, but should otherwise not be manipulated. If the buffer
4101 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004102 *
4103 * It is legal to precede this definition with the 'static' keyword.
4104 *
4105 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4106 * parameter of k_thread_create(), it may not be the same as the
4107 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4108 *
4109 * @param sym Thread stack symbol name
4110 * @param size Size of the stack memory region
4111 */
4112#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004113 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004114
4115/**
4116 * @brief Declare a toplevel array of thread stack memory regions
4117 *
4118 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4119 * definition for additional details and constraints.
4120 *
4121 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4122 * 'noinit' section so that it isn't zeroed at boot
4123 *
4124 * @param sym Thread stack symbol name
4125 * @param nmemb Number of stacks to declare
4126 * @param size Size of the stack memory region
4127 */
4128
4129#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004130 struct _k_thread_stack_element __noinit \
4131 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004132
4133/**
4134 * @brief Declare an embedded stack memory region
4135 *
4136 * Used for stacks embedded within other data structures. Use is highly
4137 * discouraged but in some cases necessary. For memory protection scenarios,
4138 * it is very important that any RAM preceding this member not be writable
4139 * by threads else a stack overflow will lead to silent corruption. In other
4140 * words, the containing data structure should live in RAM owned by the kernel.
4141 *
4142 * @param sym Thread stack symbol name
4143 * @param size Size of the stack memory region
4144 */
4145#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004146 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004147
4148/**
4149 * @brief Return the size in bytes of a stack memory region
4150 *
4151 * Convenience macro for passing the desired stack size to k_thread_create()
4152 * since the underlying implementation may actually create something larger
4153 * (for instance a guard area).
4154 *
4155 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004156 * passed to K_THREAD_STACK_DEFINE.
4157 *
4158 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4159 * it is not guaranteed to return the original value since each array
4160 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004161 *
4162 * @param sym Stack memory symbol
4163 * @return Size of the stack
4164 */
4165#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4166
4167/**
4168 * @brief Get a pointer to the physical stack buffer
4169 *
4170 * Convenience macro to get at the real underlying stack buffer used by
4171 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4172 * This is really only intended for diagnostic tools which want to examine
4173 * stack memory contents.
4174 *
4175 * @param sym Declared stack symbol name
4176 * @return The buffer itself, a char *
4177 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004178static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004179{
4180 return (char *)sym;
4181}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004182
4183#endif /* _ARCH_DECLARE_STACK */
4184
Chunlin Hane9c97022017-07-07 20:29:30 +08004185/**
4186 * @defgroup mem_domain_apis Memory domain APIs
4187 * @ingroup kernel_apis
4188 * @{
4189 */
4190
4191/** @def MEM_PARTITION_ENTRY
4192 * @brief Used to declare a memory partition entry
4193 */
4194#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4195 {\
4196 .start = _start, \
4197 .size = _size, \
4198 .attr = _attr, \
4199 }
4200
4201/** @def K_MEM_PARTITION_DEFINE
4202 * @brief Used to declare a memory partition
4203 */
4204#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4205#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4206 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304207 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004208 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4209#else
4210#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304211 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004212 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4213#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4214
Chunlin Hane9c97022017-07-07 20:29:30 +08004215/* memory partition */
4216struct k_mem_partition {
4217 /* start address of memory partition */
4218 u32_t start;
4219 /* size of memory partition */
4220 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304221#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004222 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304223 k_mem_partition_attr_t attr;
4224#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004225};
4226
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304227/* memory domian
4228 * Note: Always declare this structure with __kernel prefix
4229 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004230struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304231#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004232 /* partitions in the domain */
4233 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304234#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004235 /* domain q */
4236 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004237 /* number of partitions in the domain */
4238 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004239};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304240
Chunlin Hane9c97022017-07-07 20:29:30 +08004241
4242/**
4243 * @brief Initialize a memory domain.
4244 *
4245 * Initialize a memory domain with given name and memory partitions.
4246 *
4247 * @param domain The memory domain to be initialized.
4248 * @param num_parts The number of array items of "parts" parameter.
4249 * @param parts An array of pointers to the memory partitions. Can be NULL
4250 * if num_parts is zero.
4251 */
4252
Leandro Pereira08de6582018-02-28 14:22:57 -08004253extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004254 struct k_mem_partition *parts[]);
4255/**
4256 * @brief Destroy a memory domain.
4257 *
4258 * Destroy a memory domain.
4259 *
4260 * @param domain The memory domain to be destroyed.
4261 */
4262
4263extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4264
4265/**
4266 * @brief Add a memory partition into a memory domain.
4267 *
4268 * Add a memory partition into a memory domain.
4269 *
4270 * @param domain The memory domain to be added a memory partition.
4271 * @param part The memory partition to be added
4272 */
4273
4274extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4275 struct k_mem_partition *part);
4276
4277/**
4278 * @brief Remove a memory partition from a memory domain.
4279 *
4280 * Remove a memory partition from a memory domain.
4281 *
4282 * @param domain The memory domain to be removed a memory partition.
4283 * @param part The memory partition to be removed
4284 */
4285
4286extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4287 struct k_mem_partition *part);
4288
4289/**
4290 * @brief Add a thread into a memory domain.
4291 *
4292 * Add a thread into a memory domain.
4293 *
4294 * @param domain The memory domain that the thread is going to be added into.
4295 * @param thread ID of thread going to be added into the memory domain.
4296 */
4297
4298extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4299 k_tid_t thread);
4300
4301/**
4302 * @brief Remove a thread from its memory domain.
4303 *
4304 * Remove a thread from its memory domain.
4305 *
4306 * @param thread ID of thread going to be removed from its memory domain.
4307 */
4308
4309extern void k_mem_domain_remove_thread(k_tid_t thread);
4310
Anas Nashif166f5192018-02-25 08:02:36 -06004311/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004312
Andrew Boie756f9072017-10-10 16:01:49 -07004313/**
4314 * @brief Emit a character buffer to the console device
4315 *
4316 * @param c String of characters to print
4317 * @param n The length of the string
4318 */
4319__syscall void k_str_out(char *c, size_t n);
4320
Andy Rosse7172672018-01-24 15:48:32 -08004321/**
4322 * @brief Start a numbered CPU on a MP-capable system
4323
4324 * This starts and initializes a specific CPU. The main thread on
4325 * startup is running on CPU zero, other processors are numbered
4326 * sequentially. On return from this function, the CPU is known to
4327 * have begun operating and will enter the provided function. Its
4328 * interrupts will be initialied but disabled such that irq_unlock()
4329 * with the provided key will work to enable them.
4330 *
4331 * Normally, in SMP mode this function will be called by the kernel
4332 * initialization and should not be used as a user API. But it is
4333 * defined here for special-purpose apps which want Zephyr running on
4334 * one core and to use others for design-specific processing.
4335 *
4336 * @param cpu_num Integer number of the CPU
4337 * @param stack Stack memory for the CPU
4338 * @param sz Stack buffer size, in bytes
4339 * @param fn Function to begin running on the CPU. First argument is
4340 * an irq_unlock() key.
4341 * @param arg Untyped argument to be passed to "fn"
4342 */
4343extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4344 void (*fn)(int, void *), void *arg);
4345
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004346#ifdef __cplusplus
4347}
4348#endif
4349
Andrew Boiee004dec2016-11-07 09:01:19 -08004350#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4351/*
4352 * Define new and delete operators.
4353 * At this moment, the operators do nothing since objects are supposed
4354 * to be statically allocated.
4355 */
4356inline void operator delete(void *ptr)
4357{
4358 (void)ptr;
4359}
4360
4361inline void operator delete[](void *ptr)
4362{
4363 (void)ptr;
4364}
4365
4366inline void *operator new(size_t size)
4367{
4368 (void)size;
4369 return NULL;
4370}
4371
4372inline void *operator new[](size_t size)
4373{
4374 (void)size;
4375 return NULL;
4376}
4377
4378/* Placement versions of operator new and delete */
4379inline void operator delete(void *ptr1, void *ptr2)
4380{
4381 (void)ptr1;
4382 (void)ptr2;
4383}
4384
4385inline void operator delete[](void *ptr1, void *ptr2)
4386{
4387 (void)ptr1;
4388 (void)ptr2;
4389}
4390
4391inline void *operator new(size_t size, void *ptr)
4392{
4393 (void)size;
4394 return ptr;
4395}
4396
4397inline void *operator new[](size_t size, void *ptr)
4398{
4399 (void)size;
4400 return ptr;
4401}
4402
4403#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4404
Andrew Boiefa94ee72017-09-28 16:54:35 -07004405#include <syscalls/kernel.h>
4406
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004407#endif /* !_ASMLANGUAGE */
4408
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004409#endif /* _kernel__h_ */