blob: ba4111a6617d9c262e4eb57b7d4992e3356b751a [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
Anas Nashif954d5502018-02-25 08:37:28 -0600224/**
225 * @internal
226 */
Andrew Boie743e4682017-10-04 12:25:50 -0700227static inline void _k_object_init(void *obj)
228{
229 ARG_UNUSED(obj);
230}
231
Anas Nashif954d5502018-02-25 08:37:28 -0600232/**
233 * @internal
234 */
Andrew Boie743e4682017-10-04 12:25:50 -0700235static inline void _impl_k_object_access_grant(void *object,
236 struct k_thread *thread)
237{
238 ARG_UNUSED(object);
239 ARG_UNUSED(thread);
240}
241
Anas Nashif954d5502018-02-25 08:37:28 -0600242/**
243 * @internal
244 */
Andrew Boiea89bf012017-10-09 14:47:55 -0700245static inline void _impl_k_object_access_revoke(void *object,
246 struct k_thread *thread)
247{
248 ARG_UNUSED(object);
249 ARG_UNUSED(thread);
250}
251
Andrew Boie41bab6e2017-10-14 14:42:23 -0700252static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700253{
254 ARG_UNUSED(object);
255}
256#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700257
258/**
259 * grant a thread access to a kernel object
260 *
261 * The thread will be granted access to the object if the caller is from
262 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700263 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700264 *
265 * @param object Address of kernel object
266 * @param thread Thread to grant access to the object
267 */
Andrew Boie743e4682017-10-04 12:25:50 -0700268__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700269
Andrew Boiea89bf012017-10-09 14:47:55 -0700270/**
271 * grant a thread access to a kernel object
272 *
273 * The thread will lose access to the object if the caller is from
274 * supervisor mode, or the caller is from user mode AND has permissions
275 * on both the object and the thread whose access is being revoked.
276 *
277 * @param object Address of kernel object
278 * @param thread Thread to remove access to the object
279 */
280__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700281
282/**
283 * grant all present and future threads access to an object
284 *
285 * If the caller is from supervisor mode, or the caller is from user mode and
286 * have sufficient permissions on the object, then that object will have
287 * permissions granted to it for *all* current and future threads running in
288 * the system, effectively becoming a public kernel object.
289 *
290 * Use of this API should be avoided on systems that are running untrusted code
291 * as it is possible for such code to derive the addresses of kernel objects
292 * and perform unwanted operations on them.
293 *
Andrew Boie04caa672017-10-13 13:57:07 -0700294 * It is not possible to revoke permissions on public objects; once public,
295 * any thread may use it.
296 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700297 * @param object Address of kernel object
298 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700299void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700300
Andrew Boiebca15da2017-10-15 14:17:48 -0700301/* Using typedef deliberately here, this is quite intended to be an opaque
302 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
303 *
304 * The purpose of this data type is to clearly distinguish between the
305 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
306 * buffer which composes the stack data actually used by the underlying
307 * thread; they cannot be used interchangably as some arches precede the
308 * stack buffer region with guard areas that trigger a MPU or MMU fault
309 * if written to.
310 *
311 * APIs that want to work with the buffer inside should continue to use
312 * char *.
313 *
314 * Stacks should always be created with K_THREAD_STACK_DEFINE().
315 */
316struct __packed _k_thread_stack_element {
317 char data;
318};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700319typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700320
Andrew Boie73abd322017-04-04 13:19:13 -0700321/* timeouts */
322
323struct _timeout;
324typedef void (*_timeout_func_t)(struct _timeout *t);
325
326struct _timeout {
327 sys_dnode_t node;
328 struct k_thread *thread;
329 sys_dlist_t *wait_q;
330 s32_t delta_ticks_from_prev;
331 _timeout_func_t func;
332};
333
334extern s32_t _timeout_remaining_get(struct _timeout *timeout);
335
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700336/**
337 * @typedef k_thread_entry_t
338 * @brief Thread entry point function type.
339 *
340 * A thread's entry point function is invoked when the thread starts executing.
341 * Up to 3 argument values can be passed to the function.
342 *
343 * The thread terminates execution permanently if the entry point function
344 * returns. The thread is responsible for releasing any shared resources
345 * it may own (such as mutexes and dynamically allocated memory), prior to
346 * returning.
347 *
348 * @param p1 First argument.
349 * @param p2 Second argument.
350 * @param p3 Third argument.
351 *
352 * @return N/A
353 */
354typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700355
356#ifdef CONFIG_THREAD_MONITOR
357struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700358 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700359 void *parameter1;
360 void *parameter2;
361 void *parameter3;
362};
363#endif
364
365/* can be used for creating 'dummy' threads, e.g. for pending on objects */
366struct _thread_base {
367
368 /* this thread's entry in a ready/wait queue */
369 sys_dnode_t k_q_node;
370
371 /* user facing 'thread options'; values defined in include/kernel.h */
372 u8_t user_options;
373
374 /* thread state */
375 u8_t thread_state;
376
377 /*
378 * scheduler lock count and thread priority
379 *
380 * These two fields control the preemptibility of a thread.
381 *
382 * When the scheduler is locked, sched_locked is decremented, which
383 * means that the scheduler is locked for values from 0xff to 0x01. A
384 * thread is coop if its prio is negative, thus 0x80 to 0xff when
385 * looked at the value as unsigned.
386 *
387 * By putting them end-to-end, this means that a thread is
388 * non-preemptible if the bundled value is greater than or equal to
389 * 0x0080.
390 */
391 union {
392 struct {
393#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
394 u8_t sched_locked;
395 s8_t prio;
396#else /* LITTLE and PDP */
397 s8_t prio;
398 u8_t sched_locked;
399#endif
400 };
401 u16_t preempt;
402 };
403
Andy Ross2724fd12018-01-29 14:55:20 -0800404#ifdef CONFIG_SMP
405 /* True for the per-CPU idle threads */
406 u8_t is_idle;
407
408 /* Non-zero when actively running on a CPU */
409 u8_t active;
410
411 /* CPU index on which thread was last run */
412 u8_t cpu;
413#endif
414
Andrew Boie73abd322017-04-04 13:19:13 -0700415 /* data returned by APIs */
416 void *swap_data;
417
418#ifdef CONFIG_SYS_CLOCK_EXISTS
419 /* this thread's entry in a timeout queue */
420 struct _timeout timeout;
421#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700422};
423
424typedef struct _thread_base _thread_base_t;
425
426#if defined(CONFIG_THREAD_STACK_INFO)
427/* Contains the stack information of a thread */
428struct _thread_stack_info {
429 /* Stack Start */
430 u32_t start;
431 /* Stack Size */
432 u32_t size;
433};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700434
435typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700436#endif /* CONFIG_THREAD_STACK_INFO */
437
Chunlin Hane9c97022017-07-07 20:29:30 +0800438#if defined(CONFIG_USERSPACE)
439struct _mem_domain_info {
440 /* memory domain queue node */
441 sys_dnode_t mem_domain_q_node;
442 /* memory domain of the thread */
443 struct k_mem_domain *mem_domain;
444};
445
446#endif /* CONFIG_USERSPACE */
447
Andrew Boie73abd322017-04-04 13:19:13 -0700448struct k_thread {
449
450 struct _thread_base base;
451
452 /* defined by the architecture, but all archs need these */
453 struct _caller_saved caller_saved;
454 struct _callee_saved callee_saved;
455
456 /* static thread init data */
457 void *init_data;
458
459 /* abort function */
460 void (*fn_abort)(void);
461
462#if defined(CONFIG_THREAD_MONITOR)
463 /* thread entry and parameters description */
464 struct __thread_entry *entry;
465
466 /* next item in list of all threads */
467 struct k_thread *next_thread;
468#endif
469
470#ifdef CONFIG_THREAD_CUSTOM_DATA
471 /* crude thread-local storage */
472 void *custom_data;
473#endif
474
475#ifdef CONFIG_ERRNO
476 /* per-thread errno variable */
477 int errno_var;
478#endif
479
480#if defined(CONFIG_THREAD_STACK_INFO)
481 /* Stack Info */
482 struct _thread_stack_info stack_info;
483#endif /* CONFIG_THREAD_STACK_INFO */
484
Chunlin Hane9c97022017-07-07 20:29:30 +0800485#if defined(CONFIG_USERSPACE)
486 /* memory domain info of the thread */
487 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700488 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700489 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800490#endif /* CONFIG_USERSPACE */
491
Andy Ross042d8ec2017-12-09 08:37:20 -0800492#if defined(CONFIG_USE_SWITCH)
493 /* When using __switch() a few previously arch-specific items
494 * become part of the core OS
495 */
496
497 /* _Swap() return value */
498 int swap_retval;
499
500 /* Context handle returned via _arch_switch() */
501 void *switch_handle;
502#endif
503
Andrew Boie73abd322017-04-04 13:19:13 -0700504 /* arch-specifics: must always be at the end */
505 struct _thread_arch arch;
506};
507
508typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400509typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700510#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400511
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400512enum execution_context_types {
513 K_ISR = 0,
514 K_COOP_THREAD,
515 K_PREEMPT_THREAD,
516};
517
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400518/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100519 * @defgroup profiling_apis Profiling APIs
520 * @ingroup kernel_apis
521 * @{
522 */
523
524/**
525 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
526 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700527 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100528 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
529 *
530 * CONFIG_MAIN_STACK_SIZE
531 * CONFIG_IDLE_STACK_SIZE
532 * CONFIG_ISR_STACK_SIZE
533 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
534 *
535 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
536 * produce output.
537 *
538 * @return N/A
539 */
540extern void k_call_stacks_analyze(void);
541
Anas Nashif166f5192018-02-25 08:02:36 -0600542/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100543
544/**
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * @defgroup thread_apis Thread APIs
546 * @ingroup kernel_apis
547 * @{
548 */
549
Benjamin Walshed240f22017-01-22 13:05:08 -0500550#endif /* !_ASMLANGUAGE */
551
552
553/*
554 * Thread user options. May be needed by assembly code. Common part uses low
555 * bits, arch-specific use high bits.
556 */
557
558/* system thread that must not abort */
559#define K_ESSENTIAL (1 << 0)
560
561#if defined(CONFIG_FP_SHARING)
562/* thread uses floating point registers */
563#define K_FP_REGS (1 << 1)
564#endif
565
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700566/* This thread has dropped from supervisor mode to user mode and consequently
567 * has additional restrictions
568 */
569#define K_USER (1 << 2)
570
Andrew Boie47f8fd12017-10-05 11:11:02 -0700571/* Indicates that the thread being created should inherit all kernel object
572 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
573 * is not enabled.
574 */
575#define K_INHERIT_PERMS (1 << 3)
576
Benjamin Walshed240f22017-01-22 13:05:08 -0500577#ifdef CONFIG_X86
578/* x86 Bitmask definitions for threads user options */
579
580#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
581/* thread uses SSEx (and also FP) registers */
582#define K_SSE_REGS (1 << 7)
583#endif
584#endif
585
586/* end - thread options */
587
588#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700590 * @brief Create a thread.
591 *
592 * This routine initializes a thread, then schedules it for execution.
593 *
594 * The new thread may be scheduled for immediate execution or a delayed start.
595 * If the newly spawned thread does not have a delayed start the kernel
596 * scheduler may preempt the current thread to allow the new thread to
597 * execute.
598 *
599 * Thread options are architecture-specific, and can include K_ESSENTIAL,
600 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
601 * them using "|" (the logical OR operator).
602 *
603 * Historically, users often would use the beginning of the stack memory region
604 * to store the struct k_thread data, although corruption will occur if the
605 * stack overflows this region and stack protection features may not detect this
606 * situation.
607 *
608 * @param new_thread Pointer to uninitialized struct k_thread
609 * @param stack Pointer to the stack space.
610 * @param stack_size Stack size in bytes.
611 * @param entry Thread entry function.
612 * @param p1 1st entry point parameter.
613 * @param p2 2nd entry point parameter.
614 * @param p3 3rd entry point parameter.
615 * @param prio Thread priority.
616 * @param options Thread options.
617 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
618 *
619 * @return ID of new thread.
620 */
Andrew Boie662c3452017-10-02 10:51:18 -0700621__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700622 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700623 size_t stack_size,
624 k_thread_entry_t entry,
625 void *p1, void *p2, void *p3,
626 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700627
Andrew Boie3f091b52017-08-30 14:34:14 -0700628/**
629 * @brief Drop a thread's privileges permanently to user mode
630 *
631 * @param entry Function to start executing from
632 * @param p1 1st entry point parameter
633 * @param p2 2nd entry point parameter
634 * @param p3 3rd entry point parameter
635 */
636extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
637 void *p1, void *p2,
638 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700639
Andrew Boied26cf2d2017-03-30 13:07:02 -0700640/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700641 * @brief Grant a thread access to a NULL-terminated set of kernel objects
642 *
643 * This is a convenience function. For the provided thread, grant access to
644 * the remaining arguments, which must be pointers to kernel objects.
645 * The final argument must be a NULL.
646 *
647 * The thread object must be initialized (i.e. running). The objects don't
648 * need to be.
649 *
650 * @param thread Thread to grant access to objects
651 * @param ... NULL-terminated list of kernel object pointers
652 */
653extern void __attribute__((sentinel))
654 k_thread_access_grant(struct k_thread *thread, ...);
655
656/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500657 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658 *
Allan Stephensc98da842016-11-11 15:45:03 -0500659 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500660 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500662 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400663 *
664 * @return N/A
665 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700666__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400667
668/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500669 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400670 *
671 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 * @return N/A
675 */
Kumar Galacc334c72017-04-21 10:55:34 -0500676extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677
678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500679 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 *
685 * @return N/A
686 */
Andrew Boie468190a2017-09-29 14:00:48 -0700687__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400688
689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500692 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * If @a thread is not currently sleeping, the routine has no effect.
695 *
696 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
698 * @return N/A
699 */
Andrew Boie468190a2017-09-29 14:00:48 -0700700__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701
702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500705 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400706 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700707__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708
709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500710 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500712 * This routine prevents @a thread from executing if it has not yet started
713 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500715 * @param thread ID of thread to cancel.
716 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700717 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500718 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400719 */
Andrew Boie468190a2017-09-29 14:00:48 -0700720__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400721
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400722/**
Allan Stephensc98da842016-11-11 15:45:03 -0500723 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * This routine permanently stops execution of @a thread. The thread is taken
726 * off all kernel queues it is part of (i.e. the ready queue, the timeout
727 * queue, or a kernel object wait queue). However, any kernel resources the
728 * thread might currently own (such as mutexes or memory blocks) are not
729 * released. It is the responsibility of the caller of this routine to ensure
730 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400733 *
734 * @return N/A
735 */
Andrew Boie468190a2017-09-29 14:00:48 -0700736__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400737
Andrew Boie7d627c52017-08-30 11:01:56 -0700738
739/**
740 * @brief Start an inactive thread
741 *
742 * If a thread was created with K_FOREVER in the delay parameter, it will
743 * not be added to the scheduling queue until this function is called
744 * on it.
745 *
746 * @param thread thread to start
747 */
Andrew Boie468190a2017-09-29 14:00:48 -0700748__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700749
Allan Stephensc98da842016-11-11 15:45:03 -0500750/**
751 * @cond INTERNAL_HIDDEN
752 */
753
Benjamin Walshd211a522016-12-06 11:44:01 -0500754/* timeout has timed out and is not on _timeout_q anymore */
755#define _EXPIRED (-2)
756
757/* timeout is not in use */
758#define _INACTIVE (-1)
759
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400760struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700761 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700762 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700764 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500765 void *init_p1;
766 void *init_p2;
767 void *init_p3;
768 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500769 u32_t init_options;
770 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500771 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400772};
773
Andrew Boied26cf2d2017-03-30 13:07:02 -0700774#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400775 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500776 prio, options, delay, abort, groups) \
777 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700778 .init_thread = (thread), \
779 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500780 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700781 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400782 .init_p1 = (void *)p1, \
783 .init_p2 = (void *)p2, \
784 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500785 .init_prio = (prio), \
786 .init_options = (options), \
787 .init_delay = (delay), \
788 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400789 }
790
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400791/**
Allan Stephensc98da842016-11-11 15:45:03 -0500792 * INTERNAL_HIDDEN @endcond
793 */
794
795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * @brief Statically define and initialize a thread.
797 *
798 * The thread may be scheduled for immediate execution or a delayed start.
799 *
800 * Thread options are architecture-specific, and can include K_ESSENTIAL,
801 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
802 * them using "|" (the logical OR operator).
803 *
804 * The ID of the thread can be accessed using:
805 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500806 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 *
808 * @param name Name of the thread.
809 * @param stack_size Stack size in bytes.
810 * @param entry Thread entry function.
811 * @param p1 1st entry point parameter.
812 * @param p2 2nd entry point parameter.
813 * @param p3 3rd entry point parameter.
814 * @param prio Thread priority.
815 * @param options Thread options.
816 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400817 *
818 * @internal It has been observed that the x86 compiler by default aligns
819 * these _static_thread_data structures to 32-byte boundaries, thereby
820 * wasting space. To work around this, force a 4-byte alignment.
821 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500822#define K_THREAD_DEFINE(name, stack_size, \
823 entry, p1, p2, p3, \
824 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700825 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700826 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500827 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500828 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700829 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
830 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500831 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700832 NULL, 0); \
833 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400834
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @param thread ID of thread whose priority is needed.
841 *
842 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700844__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845
846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * Rescheduling can occur immediately depending on the priority @a thread is
852 * set to:
853 *
854 * - If its priority is raised above the priority of the caller of this
855 * function, and the caller is preemptible, @a thread will be scheduled in.
856 *
857 * - If the caller operates on itself, it lowers its priority below that of
858 * other threads in the system, and the caller is preemptible, the thread of
859 * highest priority will be scheduled in.
860 *
861 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
862 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
863 * highest priority.
864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 * @param prio New priority.
867 *
868 * @warning Changing the priority of a thread currently involved in mutex
869 * priority inheritance may result in undefined behavior.
870 *
871 * @return N/A
872 */
Andrew Boie468190a2017-09-29 14:00:48 -0700873__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * This routine prevents the kernel scheduler from making @a thread the
879 * current thread. All other internal operations on @a thread are still
880 * performed; for example, any timeout it is waiting on keeps ticking,
881 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * If @a thread is already suspended, the routine has no effect.
884 *
885 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
887 * @return N/A
888 */
Andrew Boie468190a2017-09-29 14:00:48 -0700889__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890
891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500894 * This routine allows the kernel scheduler to make @a thread the current
895 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * If @a thread is not currently suspended, the routine has no effect.
898 *
899 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900 *
901 * @return N/A
902 */
Andrew Boie468190a2017-09-29 14:00:48 -0700903__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * This routine specifies how the scheduler will perform time slicing of
909 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500911 * To enable time slicing, @a slice must be non-zero. The scheduler
912 * ensures that no thread runs for more than the specified time limit
913 * before other threads of that priority are given a chance to execute.
914 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700915 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500917 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400918 * execute. Once the scheduler selects a thread for execution, there is no
919 * minimum guaranteed time the thread will execute before threads of greater or
920 * equal priority are scheduled.
921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400923 * for execution, this routine has no effect; the thread is immediately
924 * rescheduled after the slice period expires.
925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * To disable timeslicing, set both @a slice and @a prio to zero.
927 *
928 * @param slice Maximum time slice length (in milliseconds).
929 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400930 *
931 * @return N/A
932 */
Kumar Galacc334c72017-04-21 10:55:34 -0500933extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400934
Anas Nashif166f5192018-02-25 08:02:36 -0600935/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500936
937/**
938 * @addtogroup isr_apis
939 * @{
940 */
941
942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944 *
Allan Stephensc98da842016-11-11 15:45:03 -0500945 * This routine allows the caller to customize its actions, depending on
946 * whether it is a thread or an ISR.
947 *
948 * @note Can be called by ISRs.
949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500950 * @return 0 if invoked by a thread.
951 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400952 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500953extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400954
Benjamin Walsh445830d2016-11-10 15:54:27 -0500955/**
956 * @brief Determine if code is running in a preemptible thread.
957 *
Allan Stephensc98da842016-11-11 15:45:03 -0500958 * This routine allows the caller to customize its actions, depending on
959 * whether it can be preempted by another thread. The routine returns a 'true'
960 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500961 *
Allan Stephensc98da842016-11-11 15:45:03 -0500962 * - The code is running in a thread, not at ISR.
963 * - The thread's priority is in the preemptible range.
964 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500965 *
Allan Stephensc98da842016-11-11 15:45:03 -0500966 * @note Can be called by ISRs.
967 *
968 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500969 * @return Non-zero if invoked by a preemptible thread.
970 */
Andrew Boie468190a2017-09-29 14:00:48 -0700971__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500972
Allan Stephensc98da842016-11-11 15:45:03 -0500973/**
Anas Nashif166f5192018-02-25 08:02:36 -0600974 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500975 */
976
977/**
978 * @addtogroup thread_apis
979 * @{
980 */
981
982/**
983 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500984 *
Allan Stephensc98da842016-11-11 15:45:03 -0500985 * This routine prevents the current thread from being preempted by another
986 * thread by instructing the scheduler to treat it as a cooperative thread.
987 * If the thread subsequently performs an operation that makes it unready,
988 * it will be context switched out in the normal manner. When the thread
989 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500990 *
Allan Stephensc98da842016-11-11 15:45:03 -0500991 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500992 *
Allan Stephensc98da842016-11-11 15:45:03 -0500993 * @note k_sched_lock() and k_sched_unlock() should normally be used
994 * when the operation being performed can be safely interrupted by ISRs.
995 * However, if the amount of processing involved is very small, better
996 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500997 *
998 * @return N/A
999 */
1000extern void k_sched_lock(void);
1001
Allan Stephensc98da842016-11-11 15:45:03 -05001002/**
1003 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001004 *
Allan Stephensc98da842016-11-11 15:45:03 -05001005 * This routine reverses the effect of a previous call to k_sched_lock().
1006 * A thread must call the routine once for each time it called k_sched_lock()
1007 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001008 *
1009 * @return N/A
1010 */
1011extern void k_sched_unlock(void);
1012
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * Custom data is not used by the kernel itself, and is freely available
1019 * for a thread to use as it sees fit. It can be used as a framework
1020 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 *
1024 * @return N/A
1025 */
Andrew Boie468190a2017-09-29 14:00:48 -07001026__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027
1028/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001031 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001034 */
Andrew Boie468190a2017-09-29 14:00:48 -07001035__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001036
1037/**
Anas Nashif166f5192018-02-25 08:02:36 -06001038 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001039 */
1040
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001041#include <sys_clock.h>
1042
Allan Stephensc2f15a42016-11-17 12:24:22 -05001043/**
1044 * @addtogroup clock_apis
1045 * @{
1046 */
1047
1048/**
1049 * @brief Generate null timeout delay.
1050 *
1051 * This macro generates a timeout delay that that instructs a kernel API
1052 * not to wait if the requested operation cannot be performed immediately.
1053 *
1054 * @return Timeout delay value.
1055 */
1056#define K_NO_WAIT 0
1057
1058/**
1059 * @brief Generate timeout delay from milliseconds.
1060 *
1061 * This macro generates a timeout delay that that instructs a kernel API
1062 * to wait up to @a ms milliseconds to perform the requested operation.
1063 *
1064 * @param ms Duration in milliseconds.
1065 *
1066 * @return Timeout delay value.
1067 */
Johan Hedberg14471692016-11-13 10:52:15 +02001068#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001069
1070/**
1071 * @brief Generate timeout delay from seconds.
1072 *
1073 * This macro generates a timeout delay that that instructs a kernel API
1074 * to wait up to @a s seconds to perform the requested operation.
1075 *
1076 * @param s Duration in seconds.
1077 *
1078 * @return Timeout delay value.
1079 */
Johan Hedberg14471692016-11-13 10:52:15 +02001080#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001081
1082/**
1083 * @brief Generate timeout delay from minutes.
1084 *
1085 * This macro generates a timeout delay that that instructs a kernel API
1086 * to wait up to @a m minutes to perform the requested operation.
1087 *
1088 * @param m Duration in minutes.
1089 *
1090 * @return Timeout delay value.
1091 */
Johan Hedberg14471692016-11-13 10:52:15 +02001092#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001093
1094/**
1095 * @brief Generate timeout delay from hours.
1096 *
1097 * This macro generates a timeout delay that that instructs a kernel API
1098 * to wait up to @a h hours to perform the requested operation.
1099 *
1100 * @param h Duration in hours.
1101 *
1102 * @return Timeout delay value.
1103 */
Johan Hedberg14471692016-11-13 10:52:15 +02001104#define K_HOURS(h) K_MINUTES((h) * 60)
1105
Allan Stephensc98da842016-11-11 15:45:03 -05001106/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001107 * @brief Generate infinite timeout delay.
1108 *
1109 * This macro generates a timeout delay that that instructs a kernel API
1110 * to wait as long as necessary to perform the requested operation.
1111 *
1112 * @return Timeout delay value.
1113 */
1114#define K_FOREVER (-1)
1115
1116/**
Anas Nashif166f5192018-02-25 08:02:36 -06001117 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001118 */
1119
1120/**
Allan Stephensc98da842016-11-11 15:45:03 -05001121 * @cond INTERNAL_HIDDEN
1122 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001123
Benjamin Walsh62092182016-12-20 14:39:08 -05001124/* kernel clocks */
1125
1126#if (sys_clock_ticks_per_sec == 1000) || \
1127 (sys_clock_ticks_per_sec == 500) || \
1128 (sys_clock_ticks_per_sec == 250) || \
1129 (sys_clock_ticks_per_sec == 125) || \
1130 (sys_clock_ticks_per_sec == 100) || \
1131 (sys_clock_ticks_per_sec == 50) || \
1132 (sys_clock_ticks_per_sec == 25) || \
1133 (sys_clock_ticks_per_sec == 20) || \
1134 (sys_clock_ticks_per_sec == 10) || \
1135 (sys_clock_ticks_per_sec == 1)
1136
1137 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1138#else
1139 /* yields horrible 64-bit math on many architectures: try to avoid */
1140 #define _NON_OPTIMIZED_TICKS_PER_SEC
1141#endif
1142
1143#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001144extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001145#else
Kumar Galacc334c72017-04-21 10:55:34 -05001146static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001147{
Kumar Galacc334c72017-04-21 10:55:34 -05001148 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001149}
1150#endif
1151
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001152/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001153#ifdef CONFIG_TICKLESS_KERNEL
1154#define _TICK_ALIGN 0
1155#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001156#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001157#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001158
Kumar Galacc334c72017-04-21 10:55:34 -05001159static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001160{
Benjamin Walsh62092182016-12-20 14:39:08 -05001161#ifdef CONFIG_SYS_CLOCK_EXISTS
1162
1163#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001164 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001165#else
Kumar Galacc334c72017-04-21 10:55:34 -05001166 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001167#endif
1168
1169#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001170 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001171 return 0;
1172#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001173}
1174
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001175struct k_timer {
1176 /*
1177 * _timeout structure must be first here if we want to use
1178 * dynamic timer allocation. timeout.node is used in the double-linked
1179 * list of free timers
1180 */
1181 struct _timeout timeout;
1182
Allan Stephens45bfa372016-10-12 12:39:42 -05001183 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001184 _wait_q_t wait_q;
1185
1186 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001187 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188
1189 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001190 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191
1192 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001193 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001194
Allan Stephens45bfa372016-10-12 12:39:42 -05001195 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001196 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001198 /* user-specific data, also used to support legacy features */
1199 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200
Anas Nashif2f203c22016-12-18 06:57:45 -05001201 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001202};
1203
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001204#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001206 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001207 .timeout.wait_q = NULL, \
1208 .timeout.thread = NULL, \
1209 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001210 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001211 .expiry_fn = expiry, \
1212 .stop_fn = stop, \
1213 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001214 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001215 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001216 }
1217
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001218#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1219
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220/**
Allan Stephensc98da842016-11-11 15:45:03 -05001221 * INTERNAL_HIDDEN @endcond
1222 */
1223
1224/**
1225 * @defgroup timer_apis Timer APIs
1226 * @ingroup kernel_apis
1227 * @{
1228 */
1229
1230/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001231 * @typedef k_timer_expiry_t
1232 * @brief Timer expiry function type.
1233 *
1234 * A timer's expiry function is executed by the system clock interrupt handler
1235 * each time the timer expires. The expiry function is optional, and is only
1236 * invoked if the timer has been initialized with one.
1237 *
1238 * @param timer Address of timer.
1239 *
1240 * @return N/A
1241 */
1242typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1243
1244/**
1245 * @typedef k_timer_stop_t
1246 * @brief Timer stop function type.
1247 *
1248 * A timer's stop function is executed if the timer is stopped prematurely.
1249 * The function runs in the context of the thread that stops the timer.
1250 * The stop function is optional, and is only invoked if the timer has been
1251 * initialized with one.
1252 *
1253 * @param timer Address of timer.
1254 *
1255 * @return N/A
1256 */
1257typedef void (*k_timer_stop_t)(struct k_timer *timer);
1258
1259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001263 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001264 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001265 *
1266 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * @param expiry_fn Function to invoke each time the timer expires.
1268 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001270#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001271 struct k_timer name \
1272 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001273 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001274
Allan Stephens45bfa372016-10-12 12:39:42 -05001275/**
1276 * @brief Initialize a timer.
1277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001278 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001279 *
1280 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * @param expiry_fn Function to invoke each time the timer expires.
1282 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001283 *
1284 * @return N/A
1285 */
1286extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001287 k_timer_expiry_t expiry_fn,
1288 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001289
Allan Stephens45bfa372016-10-12 12:39:42 -05001290/**
1291 * @brief Start a timer.
1292 *
1293 * This routine starts a timer, and resets its status to zero. The timer
1294 * begins counting down using the specified duration and period values.
1295 *
1296 * Attempting to start a timer that is already running is permitted.
1297 * The timer's status is reset to zero and the timer begins counting down
1298 * using the new duration and period values.
1299 *
1300 * @param timer Address of timer.
1301 * @param duration Initial timer duration (in milliseconds).
1302 * @param period Timer period (in milliseconds).
1303 *
1304 * @return N/A
1305 */
Andrew Boiea354d492017-09-29 16:22:28 -07001306__syscall void k_timer_start(struct k_timer *timer,
1307 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001308
1309/**
1310 * @brief Stop a timer.
1311 *
1312 * This routine stops a running timer prematurely. The timer's stop function,
1313 * if one exists, is invoked by the caller.
1314 *
1315 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001317 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001318 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1319 * if @a k_timer_stop is to be called from ISRs.
1320 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001321 * @param timer Address of timer.
1322 *
1323 * @return N/A
1324 */
Andrew Boiea354d492017-09-29 16:22:28 -07001325__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001326
1327/**
1328 * @brief Read timer status.
1329 *
1330 * This routine reads the timer's status, which indicates the number of times
1331 * it has expired since its status was last read.
1332 *
1333 * Calling this routine resets the timer's status to zero.
1334 *
1335 * @param timer Address of timer.
1336 *
1337 * @return Timer status.
1338 */
Andrew Boiea354d492017-09-29 16:22:28 -07001339__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001340
1341/**
1342 * @brief Synchronize thread to timer expiration.
1343 *
1344 * This routine blocks the calling thread until the timer's status is non-zero
1345 * (indicating that it has expired at least once since it was last examined)
1346 * or the timer is stopped. If the timer status is already non-zero,
1347 * or the timer is already stopped, the caller continues without waiting.
1348 *
1349 * Calling this routine resets the timer's status to zero.
1350 *
1351 * This routine must not be used by interrupt handlers, since they are not
1352 * allowed to block.
1353 *
1354 * @param timer Address of timer.
1355 *
1356 * @return Timer status.
1357 */
Andrew Boiea354d492017-09-29 16:22:28 -07001358__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001359
1360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001362 *
1363 * This routine computes the (approximate) time remaining before a running
1364 * timer next expires. If the timer is not running, it returns zero.
1365 *
1366 * @param timer Address of timer.
1367 *
1368 * @return Remaining time (in milliseconds).
1369 */
Andrew Boiea354d492017-09-29 16:22:28 -07001370__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1371
1372static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001373{
1374 return _timeout_remaining_get(&timer->timeout);
1375}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001376
Allan Stephensc98da842016-11-11 15:45:03 -05001377/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001378 * @brief Associate user-specific data with a timer.
1379 *
1380 * This routine records the @a user_data with the @a timer, to be retrieved
1381 * later.
1382 *
1383 * It can be used e.g. in a timer handler shared across multiple subsystems to
1384 * retrieve data specific to the subsystem this timer is associated with.
1385 *
1386 * @param timer Address of timer.
1387 * @param user_data User data to associate with the timer.
1388 *
1389 * @return N/A
1390 */
Andrew Boiea354d492017-09-29 16:22:28 -07001391__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1392
Anas Nashif954d5502018-02-25 08:37:28 -06001393/**
1394 * @internal
1395 */
Andrew Boiea354d492017-09-29 16:22:28 -07001396static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1397 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001398{
1399 timer->user_data = user_data;
1400}
1401
1402/**
1403 * @brief Retrieve the user-specific data from a timer.
1404 *
1405 * @param timer Address of timer.
1406 *
1407 * @return The user data.
1408 */
Andrew Boiea354d492017-09-29 16:22:28 -07001409__syscall void *k_timer_user_data_get(struct k_timer *timer);
1410
1411static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001412{
1413 return timer->user_data;
1414}
1415
Anas Nashif166f5192018-02-25 08:02:36 -06001416/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001417
Allan Stephensc98da842016-11-11 15:45:03 -05001418/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001419 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001420 * @{
1421 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001422
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001426 * This routine returns the elapsed time since the system booted,
1427 * in milliseconds.
1428 *
1429 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001430 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001431__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001432
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001433/**
1434 * @brief Enable clock always on in tickless kernel
1435 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001436 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001437 * there are no timer events programmed in tickless kernel
1438 * scheduling. This is necessary if the clock is used to track
1439 * passage of time.
1440 *
1441 * @retval prev_status Previous status of always on flag
1442 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301443#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001444static inline int k_enable_sys_clock_always_on(void)
1445{
1446 int prev_status = _sys_clock_always_on;
1447
1448 _sys_clock_always_on = 1;
1449 _enable_sys_clock();
1450
1451 return prev_status;
1452}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301453#else
1454#define k_enable_sys_clock_always_on() do { } while ((0))
1455#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001456
1457/**
1458 * @brief Disable clock always on in tickless kernel
1459 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001460 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001461 * there are no timer events programmed in tickless kernel
1462 * scheduling. To save power, this routine should be called
1463 * immediately when clock is not used to track time.
1464 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301465#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001466static inline void k_disable_sys_clock_always_on(void)
1467{
1468 _sys_clock_always_on = 0;
1469}
1470#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001471#define k_disable_sys_clock_always_on() do { } while ((0))
1472#endif
1473
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * This routine returns the lower 32-bits of the elapsed time since the system
1478 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * This routine can be more efficient than k_uptime_get(), as it reduces the
1481 * need for interrupt locking and 64-bit math. However, the 32-bit result
1482 * cannot hold a system uptime time larger than approximately 50 days, so the
1483 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001485 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001486 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001487__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001488
1489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * This routine computes the elapsed time between the current system uptime
1493 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @param reftime Pointer to a reference time, which is updated to the current
1496 * uptime upon return.
1497 *
1498 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001499 */
Kumar Galacc334c72017-04-21 10:55:34 -05001500extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001501
1502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001503 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * This routine computes the elapsed time between the current system uptime
1506 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001508 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1509 * need for interrupt locking and 64-bit math. However, the 32-bit result
1510 * cannot hold an elapsed time larger than approximately 50 days, so the
1511 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001513 * @param reftime Pointer to a reference time, which is updated to the current
1514 * uptime upon return.
1515 *
1516 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001517 */
Kumar Galacc334c72017-04-21 10:55:34 -05001518extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001519
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001521 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001523 * This routine returns the current time, as measured by the system's hardware
1524 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001528#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001529
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530/**
Anas Nashif166f5192018-02-25 08:02:36 -06001531 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001532 */
1533
Allan Stephensc98da842016-11-11 15:45:03 -05001534/**
1535 * @cond INTERNAL_HIDDEN
1536 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001538struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001539 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001540 union {
1541 _wait_q_t wait_q;
1542
1543 _POLL_EVENT;
1544 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001545
1546 _OBJECT_TRACING_NEXT_PTR(k_queue);
1547};
1548
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001549#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001550 { \
1551 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1552 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001553 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001554 _OBJECT_TRACING_INIT \
1555 }
1556
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001557#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1558
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001559/**
1560 * INTERNAL_HIDDEN @endcond
1561 */
1562
1563/**
1564 * @defgroup queue_apis Queue APIs
1565 * @ingroup kernel_apis
1566 * @{
1567 */
1568
1569/**
1570 * @brief Initialize a queue.
1571 *
1572 * This routine initializes a queue object, prior to its first use.
1573 *
1574 * @param queue Address of the queue.
1575 *
1576 * @return N/A
1577 */
1578extern void k_queue_init(struct k_queue *queue);
1579
1580/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001581 * @brief Cancel waiting on a queue.
1582 *
1583 * This routine causes first thread pending on @a queue, if any, to
1584 * return from k_queue_get() call with NULL value (as if timeout expired).
1585 *
1586 * @note Can be called by ISRs.
1587 *
1588 * @param queue Address of the queue.
1589 *
1590 * @return N/A
1591 */
1592extern void k_queue_cancel_wait(struct k_queue *queue);
1593
1594/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001595 * @brief Append an element to the end of a queue.
1596 *
1597 * This routine appends a data item to @a queue. A queue data item must be
1598 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1599 * reserved for the kernel's use.
1600 *
1601 * @note Can be called by ISRs.
1602 *
1603 * @param queue Address of the queue.
1604 * @param data Address of the data item.
1605 *
1606 * @return N/A
1607 */
1608extern void k_queue_append(struct k_queue *queue, void *data);
1609
1610/**
1611 * @brief Prepend an element to a queue.
1612 *
1613 * This routine prepends a data item to @a queue. A queue data item must be
1614 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1615 * reserved for the kernel's use.
1616 *
1617 * @note Can be called by ISRs.
1618 *
1619 * @param queue Address of the queue.
1620 * @param data Address of the data item.
1621 *
1622 * @return N/A
1623 */
1624extern void k_queue_prepend(struct k_queue *queue, void *data);
1625
1626/**
1627 * @brief Inserts an element to a queue.
1628 *
1629 * This routine inserts a data item to @a queue after previous item. A queue
1630 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1631 * item are reserved for the kernel's use.
1632 *
1633 * @note Can be called by ISRs.
1634 *
1635 * @param queue Address of the queue.
1636 * @param prev Address of the previous data item.
1637 * @param data Address of the data item.
1638 *
1639 * @return N/A
1640 */
1641extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1642
1643/**
1644 * @brief Atomically append a list of elements to a queue.
1645 *
1646 * This routine adds a list of data items to @a queue in one operation.
1647 * The data items must be in a singly-linked list, with the first 32 bits
1648 * in each data item pointing to the next data item; the list must be
1649 * NULL-terminated.
1650 *
1651 * @note Can be called by ISRs.
1652 *
1653 * @param queue Address of the queue.
1654 * @param head Pointer to first node in singly-linked list.
1655 * @param tail Pointer to last node in singly-linked list.
1656 *
1657 * @return N/A
1658 */
1659extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1660
1661/**
1662 * @brief Atomically add a list of elements to a queue.
1663 *
1664 * This routine adds a list of data items to @a queue in one operation.
1665 * The data items must be in a singly-linked list implemented using a
1666 * sys_slist_t object. Upon completion, the original list is empty.
1667 *
1668 * @note Can be called by ISRs.
1669 *
1670 * @param queue Address of the queue.
1671 * @param list Pointer to sys_slist_t object.
1672 *
1673 * @return N/A
1674 */
1675extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1676
1677/**
1678 * @brief Get an element from a queue.
1679 *
1680 * This routine removes first data item from @a queue. The first 32 bits of the
1681 * data item are reserved for the kernel's use.
1682 *
1683 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1684 *
1685 * @param queue Address of the queue.
1686 * @param timeout Waiting period to obtain a data item (in milliseconds),
1687 * or one of the special values K_NO_WAIT and K_FOREVER.
1688 *
1689 * @return Address of the data item if successful; NULL if returned
1690 * without waiting, or waiting period timed out.
1691 */
Kumar Galacc334c72017-04-21 10:55:34 -05001692extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001693
1694/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001695 * @brief Remove an element from a queue.
1696 *
1697 * This routine removes data item from @a queue. The first 32 bits of the
1698 * data item are reserved for the kernel's use. Removing elements from k_queue
1699 * rely on sys_slist_find_and_remove which is not a constant time operation.
1700 *
1701 * @note Can be called by ISRs
1702 *
1703 * @param queue Address of the queue.
1704 * @param data Address of the data item.
1705 *
1706 * @return true if data item was removed
1707 */
1708static inline bool k_queue_remove(struct k_queue *queue, void *data)
1709{
1710 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1711}
1712
1713/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001714 * @brief Query a queue to see if it has data available.
1715 *
1716 * Note that the data might be already gone by the time this function returns
1717 * if other threads are also trying to read from the queue.
1718 *
1719 * @note Can be called by ISRs.
1720 *
1721 * @param queue Address of the queue.
1722 *
1723 * @return Non-zero if the queue is empty.
1724 * @return 0 if data is available.
1725 */
1726static inline int k_queue_is_empty(struct k_queue *queue)
1727{
1728 return (int)sys_slist_is_empty(&queue->data_q);
1729}
1730
1731/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001732 * @brief Peek element at the head of queue.
1733 *
1734 * Return element from the head of queue without removing it.
1735 *
1736 * @param queue Address of the queue.
1737 *
1738 * @return Head element, or NULL if queue is empty.
1739 */
1740static inline void *k_queue_peek_head(struct k_queue *queue)
1741{
1742 return sys_slist_peek_head(&queue->data_q);
1743}
1744
1745/**
1746 * @brief Peek element at the tail of queue.
1747 *
1748 * Return element from the tail of queue without removing it.
1749 *
1750 * @param queue Address of the queue.
1751 *
1752 * @return Tail element, or NULL if queue is empty.
1753 */
1754static inline void *k_queue_peek_tail(struct k_queue *queue)
1755{
1756 return sys_slist_peek_tail(&queue->data_q);
1757}
1758
1759/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001760 * @brief Statically define and initialize a queue.
1761 *
1762 * The queue can be accessed outside the module where it is defined using:
1763 *
1764 * @code extern struct k_queue <name>; @endcode
1765 *
1766 * @param name Name of the queue.
1767 */
1768#define K_QUEUE_DEFINE(name) \
1769 struct k_queue name \
1770 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001771 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001772
Anas Nashif166f5192018-02-25 08:02:36 -06001773/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001774
1775/**
1776 * @cond INTERNAL_HIDDEN
1777 */
1778
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001779struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001780 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001781};
1782
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001783#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001784 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001785 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001786 }
1787
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001788#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1789
Allan Stephensc98da842016-11-11 15:45:03 -05001790/**
1791 * INTERNAL_HIDDEN @endcond
1792 */
1793
1794/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001795 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001796 * @ingroup kernel_apis
1797 * @{
1798 */
1799
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001801 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001803 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001805 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001806 *
1807 * @return N/A
1808 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001809#define k_fifo_init(fifo) \
1810 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811
1812/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001813 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001814 *
1815 * This routine causes first thread pending on @a fifo, if any, to
1816 * return from k_fifo_get() call with NULL value (as if timeout
1817 * expired).
1818 *
1819 * @note Can be called by ISRs.
1820 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001821 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001822 *
1823 * @return N/A
1824 */
1825#define k_fifo_cancel_wait(fifo) \
1826 k_queue_cancel_wait((struct k_queue *) fifo)
1827
1828/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001829 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001830 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001831 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1833 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @note Can be called by ISRs.
1836 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001837 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839 *
1840 * @return N/A
1841 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001842#define k_fifo_put(fifo, data) \
1843 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844
1845/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001846 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * This routine adds a list of data items to @a fifo in one operation.
1849 * The data items must be in a singly-linked list, with the first 32 bits
1850 * each data item pointing to the next data item; the list must be
1851 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001853 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001854 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001855 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @param head Pointer to first node in singly-linked list.
1857 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001858 *
1859 * @return N/A
1860 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001861#define k_fifo_put_list(fifo, head, tail) \
1862 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863
1864/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001865 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * This routine adds a list of data items to @a fifo in one operation.
1868 * The data items must be in a singly-linked list implemented using a
1869 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 * and must be re-initialized via sys_slist_init().
1871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * @note Can be called by ISRs.
1873 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001874 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 *
1877 * @return N/A
1878 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001879#define k_fifo_put_slist(fifo, list) \
1880 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881
1882/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001883 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001885 * This routine removes a data item from @a fifo in a "first in, first out"
1886 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1889 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001890 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 * or one of the special values K_NO_WAIT and K_FOREVER.
1893 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001894 * @return Address of the data item if successful; NULL if returned
1895 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001897#define k_fifo_get(fifo, timeout) \
1898 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001899
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001901 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001902 *
1903 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06001904 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001905 *
1906 * @note Can be called by ISRs.
1907 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001908 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001909 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001910 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001911 * @return 0 if data is available.
1912 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001913#define k_fifo_is_empty(fifo) \
1914 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001915
1916/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001917 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001918 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001919 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05301920 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001921 * on each iteration of processing, a head container will be peeked,
1922 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06001923 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001924 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001925 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001926 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001927 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001928 */
1929#define k_fifo_peek_head(fifo) \
1930 k_queue_peek_head((struct k_queue *) fifo)
1931
1932/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001933 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001934 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001935 * Return element from the tail of FIFO queue (without removing it). A usecase
1936 * for this is if elements of the FIFO queue are themselves containers. Then
1937 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001938 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001939 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001940 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001941 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001942 */
1943#define k_fifo_peek_tail(fifo) \
1944 k_queue_peek_tail((struct k_queue *) fifo)
1945
1946/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001947 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001948 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001949 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001951 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001953 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001956 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001957 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001958 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959
Anas Nashif166f5192018-02-25 08:02:36 -06001960/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001961
1962/**
1963 * @cond INTERNAL_HIDDEN
1964 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965
1966struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001967 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968};
1969
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001971 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001972 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001973 }
1974
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001975#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1976
Allan Stephensc98da842016-11-11 15:45:03 -05001977/**
1978 * INTERNAL_HIDDEN @endcond
1979 */
1980
1981/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001982 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001983 * @ingroup kernel_apis
1984 * @{
1985 */
1986
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001988 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001990 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001991 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001992 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001993 *
1994 * @return N/A
1995 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001996#define k_lifo_init(lifo) \
1997 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998
1999/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002000 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2004 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * @note Can be called by ISRs.
2007 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002008 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
2011 * @return N/A
2012 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002013#define k_lifo_put(lifo, data) \
2014 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015
2016/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002017 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * This routine removes a data item from @a lifo in a "last in, first out"
2020 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2023 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002024 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026 * or one of the special values K_NO_WAIT and K_FOREVER.
2027 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002028 * @return Address of the data item if successful; NULL if returned
2029 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002031#define k_lifo_get(lifo, timeout) \
2032 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002034/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002035 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002039 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002044 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002045 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002046 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047
Anas Nashif166f5192018-02-25 08:02:36 -06002048/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002049
2050/**
2051 * @cond INTERNAL_HIDDEN
2052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053
2054struct k_stack {
2055 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002056 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057
Anas Nashif2f203c22016-12-18 06:57:45 -05002058 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059};
2060
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002061#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002062 { \
2063 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2064 .base = stack_buffer, \
2065 .next = stack_buffer, \
2066 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002067 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002068 }
2069
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002070#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2071
Allan Stephensc98da842016-11-11 15:45:03 -05002072/**
2073 * INTERNAL_HIDDEN @endcond
2074 */
2075
2076/**
2077 * @defgroup stack_apis Stack APIs
2078 * @ingroup kernel_apis
2079 * @{
2080 */
2081
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082/**
2083 * @brief Initialize a stack.
2084 *
2085 * This routine initializes a stack object, prior to its first use.
2086 *
2087 * @param stack Address of the stack.
2088 * @param buffer Address of array used to hold stacked values.
2089 * @param num_entries Maximum number of values that can be stacked.
2090 *
2091 * @return N/A
2092 */
Andrew Boiee8734462017-09-29 16:42:07 -07002093__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002094 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095
2096/**
2097 * @brief Push an element onto a stack.
2098 *
2099 * This routine adds a 32-bit value @a data to @a stack.
2100 *
2101 * @note Can be called by ISRs.
2102 *
2103 * @param stack Address of the stack.
2104 * @param data Value to push onto the stack.
2105 *
2106 * @return N/A
2107 */
Andrew Boiee8734462017-09-29 16:42:07 -07002108__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109
2110/**
2111 * @brief Pop an element from a stack.
2112 *
2113 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2114 * manner and stores the value in @a data.
2115 *
2116 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2117 *
2118 * @param stack Address of the stack.
2119 * @param data Address of area to hold the value popped from the stack.
2120 * @param timeout Waiting period to obtain a value (in milliseconds),
2121 * or one of the special values K_NO_WAIT and K_FOREVER.
2122 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002123 * @retval 0 Element popped from stack.
2124 * @retval -EBUSY Returned without waiting.
2125 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 */
Andrew Boiee8734462017-09-29 16:42:07 -07002127__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002129/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002133 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002134 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002136 * @param name Name of the stack.
2137 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002139#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002140 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002141 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002142 struct k_stack name \
2143 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002144 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002145 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146
Anas Nashif166f5192018-02-25 08:02:36 -06002147/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002148
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149struct k_work;
2150
Allan Stephensc98da842016-11-11 15:45:03 -05002151/**
2152 * @defgroup workqueue_apis Workqueue Thread APIs
2153 * @ingroup kernel_apis
2154 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155 */
2156
Allan Stephens6bba9b02016-11-16 14:56:54 -05002157/**
2158 * @typedef k_work_handler_t
2159 * @brief Work item handler function type.
2160 *
2161 * A work item's handler function is executed by a workqueue's thread
2162 * when the work item is processed by the workqueue.
2163 *
2164 * @param work Address of the work item.
2165 *
2166 * @return N/A
2167 */
2168typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169
2170/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002171 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002172 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002173
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002175 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002176 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177};
2178
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002180 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181};
2182
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002184 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185 k_work_handler_t handler;
2186 atomic_t flags[1];
2187};
2188
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189struct k_delayed_work {
2190 struct k_work work;
2191 struct _timeout timeout;
2192 struct k_work_q *work_q;
2193};
2194
2195extern struct k_work_q k_sys_work_q;
2196
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002198 * INTERNAL_HIDDEN @endcond
2199 */
2200
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002201#define _K_WORK_INITIALIZER(work_handler) \
2202 { \
2203 ._reserved = NULL, \
2204 .handler = work_handler, \
2205 .flags = { 0 } \
2206 }
2207
2208#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2209
Allan Stephens6bba9b02016-11-16 14:56:54 -05002210/**
2211 * @brief Initialize a statically-defined work item.
2212 *
2213 * This macro can be used to initialize a statically-defined workqueue work
2214 * item, prior to its first use. For example,
2215 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002216 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002217 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002218 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002219 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002221#define K_WORK_DEFINE(work, work_handler) \
2222 struct k_work work \
2223 __in_section(_k_work, static, work) = \
2224 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225
2226/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002227 * @brief Initialize a work item.
2228 *
2229 * This routine initializes a workqueue work item, prior to its first use.
2230 *
2231 * @param work Address of work item.
2232 * @param handler Function to invoke each time work item is processed.
2233 *
2234 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235 */
2236static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2237{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002238 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002240 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241}
2242
2243/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002244 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002245 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002246 * This routine submits work item @a work to be processed by workqueue
2247 * @a work_q. If the work item is already pending in the workqueue's queue
2248 * as a result of an earlier submission, this routine has no effect on the
2249 * work item. If the work item has already been processed, or is currently
2250 * being processed, its work is considered complete and the work item can be
2251 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002252 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 * @warning
2254 * A submitted work item must not be modified until it has been processed
2255 * by the workqueue.
2256 *
2257 * @note Can be called by ISRs.
2258 *
2259 * @param work_q Address of workqueue.
2260 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002261 *
2262 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263 */
2264static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2265 struct k_work *work)
2266{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002267 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002268 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 }
2270}
2271
2272/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002274 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002275 * This routine indicates if work item @a work is pending in a workqueue's
2276 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * @note Can be called by ISRs.
2279 *
2280 * @param work Address of work item.
2281 *
2282 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002283 */
2284static inline int k_work_pending(struct k_work *work)
2285{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002286 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002287}
2288
2289/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002290 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002292 * This routine starts workqueue @a work_q. The workqueue spawns its work
2293 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002295 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002296 * @param stack Pointer to work queue thread's stack space, as defined by
2297 * K_THREAD_STACK_DEFINE()
2298 * @param stack_size Size of the work queue thread's stack (in bytes), which
2299 * should either be the same constant passed to
2300 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002302 *
2303 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304 */
Andrew Boie507852a2017-07-25 18:47:07 -07002305extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002306 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002307 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002312 * This routine initializes a workqueue delayed work item, prior to
2313 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002315 * @param work Address of delayed work item.
2316 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
2318 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002320extern void k_delayed_work_init(struct k_delayed_work *work,
2321 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322
2323/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002325 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002326 * This routine schedules work item @a work to be processed by workqueue
2327 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002328 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002329 * Only when the countdown completes is the work item actually submitted to
2330 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002332 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002333 * counting down cancels the existing submission and restarts the
2334 * countdown using the new delay. Note that this behavior is
2335 * inherently subject to race conditions with the pre-existing
2336 * timeouts and work queue, so care must be taken to synchronize such
2337 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * @warning
2340 * A delayed work item must not be modified until it has been processed
2341 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002343 * @note Can be called by ISRs.
2344 *
2345 * @param work_q Address of workqueue.
2346 * @param work Address of delayed work item.
2347 * @param delay Delay before submitting the work item (in milliseconds).
2348 *
2349 * @retval 0 Work item countdown started.
2350 * @retval -EINPROGRESS Work item is already pending.
2351 * @retval -EINVAL Work item is being processed or has completed its work.
2352 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002354extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2355 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002356 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357
2358/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002359 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002361 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002362 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002363 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002365 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002367 * @param work Address of delayed work item.
2368 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002369 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002370 * @retval -EINPROGRESS Work item is already pending.
2371 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002373extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376 * @brief Submit a work item to the system workqueue.
2377 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002378 * This routine submits work item @a work to be processed by the system
2379 * workqueue. If the work item is already pending in the workqueue's queue
2380 * as a result of an earlier submission, this routine has no effect on the
2381 * work item. If the work item has already been processed, or is currently
2382 * being processed, its work is considered complete and the work item can be
2383 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002385 * @warning
2386 * Work items submitted to the system workqueue should avoid using handlers
2387 * that block or yield since this may prevent the system workqueue from
2388 * processing other work items in a timely manner.
2389 *
2390 * @note Can be called by ISRs.
2391 *
2392 * @param work Address of work item.
2393 *
2394 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395 */
2396static inline void k_work_submit(struct k_work *work)
2397{
2398 k_work_submit_to_queue(&k_sys_work_q, work);
2399}
2400
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402 * @brief Submit a delayed work item to the system workqueue.
2403 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002404 * This routine schedules work item @a work to be processed by the system
2405 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002406 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002407 * Only when the countdown completes is the work item actually submitted to
2408 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002410 * Submitting a previously submitted delayed work item that is still
2411 * counting down cancels the existing submission and restarts the countdown
2412 * using the new delay. If the work item is currently pending on the
2413 * workqueue's queue because the countdown has completed it is too late to
2414 * resubmit the item, and resubmission fails without impacting the work item.
2415 * If the work item has already been processed, or is currently being processed,
2416 * its work is considered complete and the work item can be resubmitted.
2417 *
2418 * @warning
2419 * Work items submitted to the system workqueue should avoid using handlers
2420 * that block or yield since this may prevent the system workqueue from
2421 * processing other work items in a timely manner.
2422 *
2423 * @note Can be called by ISRs.
2424 *
2425 * @param work Address of delayed work item.
2426 * @param delay Delay before submitting the work item (in milliseconds).
2427 *
2428 * @retval 0 Work item countdown started.
2429 * @retval -EINPROGRESS Work item is already pending.
2430 * @retval -EINVAL Work item is being processed or has completed its work.
2431 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432 */
2433static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002434 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002436 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437}
2438
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002440 * @brief Get time remaining before a delayed work gets scheduled.
2441 *
2442 * This routine computes the (approximate) time remaining before a
2443 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002444 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002445 *
2446 * @param work Delayed work item.
2447 *
2448 * @return Remaining time (in milliseconds).
2449 */
Kumar Galacc334c72017-04-21 10:55:34 -05002450static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002451{
2452 return _timeout_remaining_get(&work->timeout);
2453}
2454
Anas Nashif166f5192018-02-25 08:02:36 -06002455/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456
Allan Stephensc98da842016-11-11 15:45:03 -05002457/**
2458 * @cond INTERNAL_HIDDEN
2459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460
2461struct k_mutex {
2462 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002463 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002464 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466
Anas Nashif2f203c22016-12-18 06:57:45 -05002467 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468};
2469
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002470#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471 { \
2472 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2473 .owner = NULL, \
2474 .lock_count = 0, \
2475 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002476 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477 }
2478
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002479#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2480
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481/**
Allan Stephensc98da842016-11-11 15:45:03 -05002482 * INTERNAL_HIDDEN @endcond
2483 */
2484
2485/**
2486 * @defgroup mutex_apis Mutex APIs
2487 * @ingroup kernel_apis
2488 * @{
2489 */
2490
2491/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002495 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002496 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002501 struct k_mutex name \
2502 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002503 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * Upon completion, the mutex is available and does not have an owner.
2511 *
2512 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 *
2514 * @return N/A
2515 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002516__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517
2518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * This routine locks @a mutex. If the mutex is locked by another thread,
2522 * the calling thread waits until the mutex becomes available or until
2523 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * A thread is permitted to lock a mutex it has already locked. The operation
2526 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * @param mutex Address of the mutex.
2529 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 * or one of the special values K_NO_WAIT and K_FOREVER.
2531 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002532 * @retval 0 Mutex locked.
2533 * @retval -EBUSY Returned without waiting.
2534 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002536__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537
2538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * This routine unlocks @a mutex. The mutex must already be locked by the
2542 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 *
2544 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002545 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546 * thread.
2547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 *
2550 * @return N/A
2551 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002552__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553
Allan Stephensc98da842016-11-11 15:45:03 -05002554/**
Anas Nashif166f5192018-02-25 08:02:36 -06002555 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002556 */
2557
2558/**
2559 * @cond INTERNAL_HIDDEN
2560 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561
2562struct k_sem {
2563 _wait_q_t wait_q;
2564 unsigned int count;
2565 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002566 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567
Anas Nashif2f203c22016-12-18 06:57:45 -05002568 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569};
2570
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002571#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002572 { \
2573 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2574 .count = initial_count, \
2575 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002576 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002577 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002578 }
2579
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002580#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2581
Allan Stephensc98da842016-11-11 15:45:03 -05002582/**
2583 * INTERNAL_HIDDEN @endcond
2584 */
2585
2586/**
2587 * @defgroup semaphore_apis Semaphore APIs
2588 * @ingroup kernel_apis
2589 * @{
2590 */
2591
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * @param sem Address of the semaphore.
2598 * @param initial_count Initial semaphore count.
2599 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002600 *
2601 * @return N/A
2602 */
Andrew Boie99280232017-09-29 14:17:47 -07002603__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2604 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002605
2606/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002607 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002609 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2612 *
2613 * @param sem Address of the semaphore.
2614 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002615 * or one of the special values K_NO_WAIT and K_FOREVER.
2616 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002617 * @note When porting code from the nanokernel legacy API to the new API, be
2618 * careful with the return value of this function. The return value is the
2619 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2620 * non-zero means failure, while the nano_sem_take family returns 1 for success
2621 * and 0 for failure.
2622 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002623 * @retval 0 Semaphore taken.
2624 * @retval -EBUSY Returned without waiting.
2625 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002626 */
Andrew Boie99280232017-09-29 14:17:47 -07002627__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002628
2629/**
2630 * @brief Give a semaphore.
2631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 * This routine gives @a sem, unless the semaphore is already at its maximum
2633 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002638 *
2639 * @return N/A
2640 */
Andrew Boie99280232017-09-29 14:17:47 -07002641__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643/**
2644 * @brief Reset a semaphore's count to zero.
2645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002649 *
2650 * @return N/A
2651 */
Andrew Boie990bf162017-10-03 12:36:49 -07002652__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002653
Anas Nashif954d5502018-02-25 08:37:28 -06002654/**
2655 * @internal
2656 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002657static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658{
2659 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660}
2661
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002662/**
2663 * @brief Get a semaphore's count.
2664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002670 */
Andrew Boie990bf162017-10-03 12:36:49 -07002671__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002672
Anas Nashif954d5502018-02-25 08:37:28 -06002673/**
2674 * @internal
2675 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002676static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677{
2678 return sem->count;
2679}
2680
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002685 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002686 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * @param name Name of the semaphore.
2689 * @param initial_count Initial semaphore count.
2690 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002691 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002693 struct k_sem name \
2694 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002695 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696
Anas Nashif166f5192018-02-25 08:02:36 -06002697/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002698
2699/**
2700 * @defgroup alert_apis Alert APIs
2701 * @ingroup kernel_apis
2702 * @{
2703 */
2704
Allan Stephens5eceb852016-11-16 10:16:30 -05002705/**
2706 * @typedef k_alert_handler_t
2707 * @brief Alert handler function type.
2708 *
2709 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002710 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002711 * and is only invoked if the alert has been initialized with one.
2712 *
2713 * @param alert Address of the alert.
2714 *
2715 * @return 0 if alert has been consumed; non-zero if alert should pend.
2716 */
2717typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002718
Anas Nashif166f5192018-02-25 08:02:36 -06002719/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002720
2721/**
2722 * @cond INTERNAL_HIDDEN
2723 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002725#define K_ALERT_DEFAULT NULL
2726#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002728struct k_alert {
2729 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730 atomic_t send_count;
2731 struct k_work work_item;
2732 struct k_sem sem;
2733
Anas Nashif2f203c22016-12-18 06:57:45 -05002734 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735};
2736
Anas Nashif954d5502018-02-25 08:37:28 -06002737/**
2738 * @internal
2739 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002740extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002742#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002744 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002746 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2747 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002748 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749 }
2750
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002751#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2752
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002753/**
Allan Stephensc98da842016-11-11 15:45:03 -05002754 * INTERNAL_HIDDEN @endcond
2755 */
2756
2757/**
2758 * @addtogroup alert_apis
2759 * @{
2760 */
2761
2762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002765 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002767 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * @param name Name of the alert.
2770 * @param alert_handler Action to take when alert is sent. Specify either
2771 * the address of a function to be invoked by the system workqueue
2772 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2773 * K_ALERT_DEFAULT (which causes the alert to pend).
2774 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002775 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002776#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002777 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002778 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002779 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002780 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002785 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002787 * @param alert Address of the alert.
2788 * @param handler Action to take when alert is sent. Specify either the address
2789 * of a function to be invoked by the system workqueue thread,
2790 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2791 * K_ALERT_DEFAULT (which causes the alert to pend).
2792 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002793 *
2794 * @return N/A
2795 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002796extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2797 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798
2799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002802 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2805 *
2806 * @param alert Address of the alert.
2807 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808 * or one of the special values K_NO_WAIT and K_FOREVER.
2809 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002810 * @retval 0 Alert received.
2811 * @retval -EBUSY Returned without waiting.
2812 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002813 */
Andrew Boie310e9872017-09-29 04:41:15 -07002814__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815
2816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * This routine signals @a alert. The action specified for @a alert will
2820 * be taken, which may trigger the execution of an alert handler function
2821 * and/or cause the alert to pend (assuming the alert has not reached its
2822 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * @note Can be called by ISRs.
2825 *
2826 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
2828 * @return N/A
2829 */
Andrew Boie310e9872017-09-29 04:41:15 -07002830__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831
2832/**
Anas Nashif166f5192018-02-25 08:02:36 -06002833 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 */
2835
Allan Stephensc98da842016-11-11 15:45:03 -05002836/**
2837 * @cond INTERNAL_HIDDEN
2838 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839
2840struct k_msgq {
2841 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002842 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002843 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844 char *buffer_start;
2845 char *buffer_end;
2846 char *read_ptr;
2847 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002848 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849
Anas Nashif2f203c22016-12-18 06:57:45 -05002850 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851};
2852
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002853#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854 { \
2855 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002856 .max_msgs = q_max_msgs, \
2857 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002858 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002859 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860 .read_ptr = q_buffer, \
2861 .write_ptr = q_buffer, \
2862 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002863 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864 }
2865
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002866#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2867
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302868struct k_msgq_attrs {
2869 size_t msg_size;
2870 u32_t max_msgs;
2871 u32_t used_msgs;
2872};
2873
Peter Mitsis1da807e2016-10-06 11:36:59 -04002874/**
Allan Stephensc98da842016-11-11 15:45:03 -05002875 * INTERNAL_HIDDEN @endcond
2876 */
2877
2878/**
2879 * @defgroup msgq_apis Message Queue APIs
2880 * @ingroup kernel_apis
2881 * @{
2882 */
2883
2884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2888 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002889 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2890 * message is similarly aligned to this boundary, @a q_msg_size must also be
2891 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * The message queue can be accessed outside the module where it is defined
2894 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002896 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * @param q_name Name of the message queue.
2899 * @param q_msg_size Message size (in bytes).
2900 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002901 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002902 */
2903#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2904 static char __noinit __aligned(q_align) \
2905 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002906 struct k_msgq q_name \
2907 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002908 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002909 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910
Peter Mitsisd7a37502016-10-13 11:37:40 -04002911/**
2912 * @brief Initialize a message queue.
2913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * This routine initializes a message queue object, prior to its first use.
2915 *
Allan Stephensda827222016-11-09 14:23:58 -06002916 * The message queue's ring buffer must contain space for @a max_msgs messages,
2917 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2918 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2919 * that each message is similarly aligned to this boundary, @a q_msg_size
2920 * must also be a multiple of N.
2921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002922 * @param q Address of the message queue.
2923 * @param buffer Pointer to ring buffer that holds queued messages.
2924 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002925 * @param max_msgs Maximum number of messages that can be queued.
2926 *
2927 * @return N/A
2928 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002929__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2930 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002931
2932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002936 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002937 * @note Can be called by ISRs.
2938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * @param q Address of the message queue.
2940 * @param data Pointer to the message.
2941 * @param timeout Waiting period to add the message (in milliseconds),
2942 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002943 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002944 * @retval 0 Message sent.
2945 * @retval -ENOMSG Returned without waiting or queue purged.
2946 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002947 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002948__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002949
2950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002951 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * This routine receives a message from message queue @a q in a "first in,
2954 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002955 *
Allan Stephensc98da842016-11-11 15:45:03 -05002956 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * @param q Address of the message queue.
2959 * @param data Address of area to hold the received message.
2960 * @param timeout Waiting period to receive the message (in milliseconds),
2961 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002962 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002963 * @retval 0 Message received.
2964 * @retval -ENOMSG Returned without waiting.
2965 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002966 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002967__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002968
2969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * This routine discards all unreceived messages in a message queue's ring
2973 * buffer. Any threads that are blocked waiting to send a message to the
2974 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002976 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002977 *
2978 * @return N/A
2979 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002980__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981
Peter Mitsis67be2492016-10-07 11:44:34 -04002982/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * This routine returns the number of unused entries in a message queue's
2986 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002988 * @param q Address of the message queue.
2989 *
2990 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002991 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002992__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2993
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05302994/**
2995 * @brief Get basic attributes of a message queue.
2996 *
2997 * This routine fetches basic attributes of message queue into attr argument.
2998 *
2999 * @param q Address of the message queue.
3000 * @param attrs pointer to message queue attribute structure.
3001 *
3002 * @return N/A
3003 */
3004__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3005
3006
Andrew Boie82edb6e2017-10-02 10:53:06 -07003007static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003008{
3009 return q->max_msgs - q->used_msgs;
3010}
3011
Peter Mitsisd7a37502016-10-13 11:37:40 -04003012/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * @param q Address of the message queue.
3018 *
3019 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003020 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003021__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3022
3023static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024{
3025 return q->used_msgs;
3026}
3027
Anas Nashif166f5192018-02-25 08:02:36 -06003028/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003029
3030/**
3031 * @defgroup mem_pool_apis Memory Pool APIs
3032 * @ingroup kernel_apis
3033 * @{
3034 */
3035
Andy Ross73cb9582017-05-09 10:42:39 -07003036/* Note on sizing: the use of a 20 bit field for block means that,
3037 * assuming a reasonable minimum block size of 16 bytes, we're limited
3038 * to 16M of memory managed by a single pool. Long term it would be
3039 * good to move to a variable bit size based on configuration.
3040 */
3041struct k_mem_block_id {
3042 u32_t pool : 8;
3043 u32_t level : 4;
3044 u32_t block : 20;
3045};
3046
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003049 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003050};
3051
Anas Nashif166f5192018-02-25 08:02:36 -06003052/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003053
3054/**
3055 * @defgroup mailbox_apis Mailbox APIs
3056 * @ingroup kernel_apis
3057 * @{
3058 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059
3060struct k_mbox_msg {
3061 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003062 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003064 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003066 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 /** sender's message data buffer */
3068 void *tx_data;
3069 /** internal use only - needed for legacy API support */
3070 void *_rx_data;
3071 /** message data block descriptor */
3072 struct k_mem_block tx_block;
3073 /** source thread id */
3074 k_tid_t rx_source_thread;
3075 /** target thread id */
3076 k_tid_t tx_target_thread;
3077 /** internal use only - thread waiting on send (may be a dummy) */
3078 k_tid_t _syncing_thread;
3079#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3080 /** internal use only - semaphore used during asynchronous send */
3081 struct k_sem *_async_sem;
3082#endif
3083};
3084
Allan Stephensc98da842016-11-11 15:45:03 -05003085/**
3086 * @cond INTERNAL_HIDDEN
3087 */
3088
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089struct k_mbox {
3090 _wait_q_t tx_msg_queue;
3091 _wait_q_t rx_msg_queue;
3092
Anas Nashif2f203c22016-12-18 06:57:45 -05003093 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094};
3095
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003096#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 { \
3098 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3099 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003100 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 }
3102
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003103#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3104
Peter Mitsis12092702016-10-14 12:57:23 -04003105/**
Allan Stephensc98da842016-11-11 15:45:03 -05003106 * INTERNAL_HIDDEN @endcond
3107 */
3108
3109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003113 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003114 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003116 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003117 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003119 struct k_mbox name \
3120 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003121 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122
Peter Mitsis12092702016-10-14 12:57:23 -04003123/**
3124 * @brief Initialize a mailbox.
3125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003126 * This routine initializes a mailbox object, prior to its first use.
3127 *
3128 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003129 *
3130 * @return N/A
3131 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132extern void k_mbox_init(struct k_mbox *mbox);
3133
Peter Mitsis12092702016-10-14 12:57:23 -04003134/**
3135 * @brief Send a mailbox message in a synchronous manner.
3136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * This routine sends a message to @a mbox and waits for a receiver to both
3138 * receive and process it. The message data may be in a buffer, in a memory
3139 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * @param mbox Address of the mailbox.
3142 * @param tx_msg Address of the transmit message descriptor.
3143 * @param timeout Waiting period for the message to be received (in
3144 * milliseconds), or one of the special values K_NO_WAIT
3145 * and K_FOREVER. Once the message has been received,
3146 * this routine waits as long as necessary for the message
3147 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003148 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003149 * @retval 0 Message sent.
3150 * @retval -ENOMSG Returned without waiting.
3151 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003152 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003153extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003154 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003155
Peter Mitsis12092702016-10-14 12:57:23 -04003156/**
3157 * @brief Send a mailbox message in an asynchronous manner.
3158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003159 * This routine sends a message to @a mbox without waiting for a receiver
3160 * to process it. The message data may be in a buffer, in a memory pool block,
3161 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3162 * will be given when the message has been both received and completely
3163 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003165 * @param mbox Address of the mailbox.
3166 * @param tx_msg Address of the transmit message descriptor.
3167 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003168 *
3169 * @return N/A
3170 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003171extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003172 struct k_sem *sem);
3173
Peter Mitsis12092702016-10-14 12:57:23 -04003174/**
3175 * @brief Receive a mailbox message.
3176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * This routine receives a message from @a mbox, then optionally retrieves
3178 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * @param mbox Address of the mailbox.
3181 * @param rx_msg Address of the receive message descriptor.
3182 * @param buffer Address of the buffer to receive data, or NULL to defer data
3183 * retrieval and message disposal until later.
3184 * @param timeout Waiting period for a message to be received (in
3185 * milliseconds), or one of the special values K_NO_WAIT
3186 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003187 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003188 * @retval 0 Message received.
3189 * @retval -ENOMSG Returned without waiting.
3190 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003191 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003192extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003193 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003194
3195/**
3196 * @brief Retrieve mailbox message data into a buffer.
3197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * This routine completes the processing of a received message by retrieving
3199 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003200 *
3201 * Alternatively, this routine can be used to dispose of a received message
3202 * without retrieving its data.
3203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * @param rx_msg Address of the receive message descriptor.
3205 * @param buffer Address of the buffer to receive data, or NULL to discard
3206 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003207 *
3208 * @return N/A
3209 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003210extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003211
3212/**
3213 * @brief Retrieve mailbox message data into a memory pool block.
3214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * This routine completes the processing of a received message by retrieving
3216 * its data into a memory pool block, then disposing of the message.
3217 * The memory pool block that results from successful retrieval must be
3218 * returned to the pool once the data has been processed, even in cases
3219 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003220 *
3221 * Alternatively, this routine can be used to dispose of a received message
3222 * without retrieving its data. In this case there is no need to return a
3223 * memory pool block to the pool.
3224 *
3225 * This routine allocates a new memory pool block for the data only if the
3226 * data is not already in one. If a new block cannot be allocated, the routine
3227 * returns a failure code and the received message is left unchanged. This
3228 * permits the caller to reattempt data retrieval at a later time or to dispose
3229 * of the received message without retrieving its data.
3230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @param rx_msg Address of a receive message descriptor.
3232 * @param pool Address of memory pool, or NULL to discard data.
3233 * @param block Address of the area to hold memory pool block info.
3234 * @param timeout Waiting period to wait for a memory pool block (in
3235 * milliseconds), or one of the special values K_NO_WAIT
3236 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003237 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003238 * @retval 0 Data retrieved.
3239 * @retval -ENOMEM Returned without waiting.
3240 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003241 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003242extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003243 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003244 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245
Anas Nashif166f5192018-02-25 08:02:36 -06003246/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003247
3248/**
3249 * @cond INTERNAL_HIDDEN
3250 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251
3252struct k_pipe {
3253 unsigned char *buffer; /* Pipe buffer: may be NULL */
3254 size_t size; /* Buffer size */
3255 size_t bytes_used; /* # bytes used in buffer */
3256 size_t read_index; /* Where in buffer to read from */
3257 size_t write_index; /* Where in buffer to write */
3258
3259 struct {
3260 _wait_q_t readers; /* Reader wait queue */
3261 _wait_q_t writers; /* Writer wait queue */
3262 } wait_q;
3263
Anas Nashif2f203c22016-12-18 06:57:45 -05003264 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265};
3266
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003267#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 { \
3269 .buffer = pipe_buffer, \
3270 .size = pipe_buffer_size, \
3271 .bytes_used = 0, \
3272 .read_index = 0, \
3273 .write_index = 0, \
3274 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3275 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003276 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277 }
3278
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003279#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3280
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003281/**
Allan Stephensc98da842016-11-11 15:45:03 -05003282 * INTERNAL_HIDDEN @endcond
3283 */
3284
3285/**
3286 * @defgroup pipe_apis Pipe APIs
3287 * @ingroup kernel_apis
3288 * @{
3289 */
3290
3291/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003296 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * @param name Name of the pipe.
3299 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3300 * or zero if no ring buffer is used.
3301 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003302 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003303#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3304 static unsigned char __noinit __aligned(pipe_align) \
3305 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003306 struct k_pipe name \
3307 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003308 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * This routine initializes a pipe object, prior to its first use.
3314 *
3315 * @param pipe Address of the pipe.
3316 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3317 * is used.
3318 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3319 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320 *
3321 * @return N/A
3322 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003323__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3324 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325
3326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * @param pipe Address of the pipe.
3332 * @param data Address of data to write.
3333 * @param bytes_to_write Size of data (in bytes).
3334 * @param bytes_written Address of area to hold the number of bytes written.
3335 * @param min_xfer Minimum number of bytes to write.
3336 * @param timeout Waiting period to wait for the data to be written (in
3337 * milliseconds), or one of the special values K_NO_WAIT
3338 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003340 * @retval 0 At least @a min_xfer bytes of data were written.
3341 * @retval -EIO Returned without waiting; zero data bytes were written.
3342 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003345__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3346 size_t bytes_to_write, size_t *bytes_written,
3347 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348
3349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * @param pipe Address of the pipe.
3355 * @param data Address to place the data read from pipe.
3356 * @param bytes_to_read Maximum number of data bytes to read.
3357 * @param bytes_read Address of area to hold the number of bytes read.
3358 * @param min_xfer Minimum number of data bytes to read.
3359 * @param timeout Waiting period to wait for the data to be read (in
3360 * milliseconds), or one of the special values K_NO_WAIT
3361 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003363 * @retval 0 At least @a min_xfer bytes of data were read.
3364 * @retval -EIO Returned without waiting; zero data bytes were read.
3365 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003368__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3369 size_t bytes_to_read, size_t *bytes_read,
3370 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003371
3372/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003373 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * This routine writes the data contained in a memory block to @a pipe.
3376 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003377 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003380 * @param block Memory block containing data to send
3381 * @param size Number of data bytes in memory block to send
3382 * @param sem Semaphore to signal upon completion (else NULL)
3383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385 */
3386extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3387 size_t size, struct k_sem *sem);
3388
Anas Nashif166f5192018-02-25 08:02:36 -06003389/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003390
Allan Stephensc98da842016-11-11 15:45:03 -05003391/**
3392 * @cond INTERNAL_HIDDEN
3393 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003395struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003397 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003398 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399 char *buffer;
3400 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003401 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402
Anas Nashif2f203c22016-12-18 06:57:45 -05003403 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003404};
3405
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003406#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003407 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408 { \
3409 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003410 .num_blocks = slab_num_blocks, \
3411 .block_size = slab_block_size, \
3412 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413 .free_list = NULL, \
3414 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003415 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416 }
3417
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003418#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3419
3420
Peter Mitsis578f9112016-10-07 13:50:31 -04003421/**
Allan Stephensc98da842016-11-11 15:45:03 -05003422 * INTERNAL_HIDDEN @endcond
3423 */
3424
3425/**
3426 * @defgroup mem_slab_apis Memory Slab APIs
3427 * @ingroup kernel_apis
3428 * @{
3429 */
3430
3431/**
Allan Stephensda827222016-11-09 14:23:58 -06003432 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003433 *
Allan Stephensda827222016-11-09 14:23:58 -06003434 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003435 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003436 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3437 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003438 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003439 *
Allan Stephensda827222016-11-09 14:23:58 -06003440 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003442 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003443 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003445 * @param name Name of the memory slab.
3446 * @param slab_block_size Size of each memory block (in bytes).
3447 * @param slab_num_blocks Number memory blocks.
3448 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003449 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003450#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3451 char __noinit __aligned(slab_align) \
3452 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3453 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003454 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003455 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003456 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003457
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003458/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003459 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003462 *
Allan Stephensda827222016-11-09 14:23:58 -06003463 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3464 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3465 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3466 * To ensure that each memory block is similarly aligned to this boundary,
3467 * @a slab_block_size must also be a multiple of N.
3468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @param slab Address of the memory slab.
3470 * @param buffer Pointer to buffer used for the memory blocks.
3471 * @param block_size Size of each memory block (in bytes).
3472 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003473 *
3474 * @return N/A
3475 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003476extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003477 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003478
3479/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @param slab Address of the memory slab.
3485 * @param mem Pointer to block address area.
3486 * @param timeout Maximum time to wait for operation to complete
3487 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3488 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003489 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003490 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003492 * @retval -ENOMEM Returned without waiting.
3493 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003494 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003495extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003496 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003497
3498/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003501 * This routine releases a previously allocated memory block back to its
3502 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @param slab Address of the memory slab.
3505 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003506 *
3507 * @return N/A
3508 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003509extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * This routine gets the number of memory blocks that are currently
3515 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003517 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003520 */
Kumar Galacc334c72017-04-21 10:55:34 -05003521static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003523 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003524}
3525
Peter Mitsisc001aa82016-10-13 13:53:37 -04003526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * This routine gets the number of memory blocks that are currently
3530 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003535 */
Kumar Galacc334c72017-04-21 10:55:34 -05003536static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003537{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003538 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003539}
3540
Anas Nashif166f5192018-02-25 08:02:36 -06003541/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003542
3543/**
3544 * @cond INTERNAL_HIDDEN
3545 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546
Andy Ross73cb9582017-05-09 10:42:39 -07003547struct k_mem_pool_lvl {
3548 union {
3549 u32_t *bits_p;
3550 u32_t bits;
3551 };
3552 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003553};
3554
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003555struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003556 void *buf;
3557 size_t max_sz;
3558 u16_t n_max;
3559 u8_t n_levels;
3560 u8_t max_inline_level;
3561 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003562 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563};
3564
Andy Ross73cb9582017-05-09 10:42:39 -07003565#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003566
Andy Ross73cb9582017-05-09 10:42:39 -07003567#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3568
Andy Ross8cf7ff52017-11-13 14:59:13 -08003569#define __MPOOL_LVLS(maxsz, minsz) \
3570 (_MPOOL_HAVE_LVL((maxsz), (minsz), 0) + \
3571 _MPOOL_HAVE_LVL((maxsz), (minsz), 1) + \
3572 _MPOOL_HAVE_LVL((maxsz), (minsz), 2) + \
3573 _MPOOL_HAVE_LVL((maxsz), (minsz), 3) + \
3574 _MPOOL_HAVE_LVL((maxsz), (minsz), 4) + \
3575 _MPOOL_HAVE_LVL((maxsz), (minsz), 5) + \
3576 _MPOOL_HAVE_LVL((maxsz), (minsz), 6) + \
3577 _MPOOL_HAVE_LVL((maxsz), (minsz), 7) + \
3578 _MPOOL_HAVE_LVL((maxsz), (minsz), 8) + \
3579 _MPOOL_HAVE_LVL((maxsz), (minsz), 9) + \
3580 _MPOOL_HAVE_LVL((maxsz), (minsz), 10) + \
3581 _MPOOL_HAVE_LVL((maxsz), (minsz), 11) + \
3582 _MPOOL_HAVE_LVL((maxsz), (minsz), 12) + \
3583 _MPOOL_HAVE_LVL((maxsz), (minsz), 13) + \
3584 _MPOOL_HAVE_LVL((maxsz), (minsz), 14) + \
3585 _MPOOL_HAVE_LVL((maxsz), (minsz), 15))
3586
3587#define _MPOOL_MINBLK sizeof(sys_dnode_t)
3588
3589#define _MPOOL_LVLS(max, min) \
3590 __MPOOL_LVLS((max), (min) >= _MPOOL_MINBLK ? (min) : _MPOOL_MINBLK)
Andy Ross73cb9582017-05-09 10:42:39 -07003591
3592/* Rounds the needed bits up to integer multiples of u32_t */
3593#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3594 ((((n_max) << (2*(l))) + 31) / 32)
3595
3596/* One word gets stored free unioned with the pointer, otherwise the
3597 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003598 */
Andy Ross73cb9582017-05-09 10:42:39 -07003599#define _MPOOL_LBIT_WORDS(n_max, l) \
3600 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3601 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003602
Andy Ross73cb9582017-05-09 10:42:39 -07003603/* How many bytes for the bitfields of a single level? */
3604#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3605 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3606 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003607
Andy Ross73cb9582017-05-09 10:42:39 -07003608/* Size of the bitmap array that follows the buffer in allocated memory */
3609#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3610 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3611 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3612 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3613 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3614 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3615 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3616 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3617 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3618 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3619 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3620 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3621 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3622 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3623 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3624 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3625 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003626
3627/**
Allan Stephensc98da842016-11-11 15:45:03 -05003628 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003629 */
3630
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003631/**
Allan Stephensc98da842016-11-11 15:45:03 -05003632 * @addtogroup mem_pool_apis
3633 * @{
3634 */
3635
3636/**
3637 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003639 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3640 * long. The memory pool allows blocks to be repeatedly partitioned into
3641 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003642 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003643 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003644 * If the pool is to be accessed outside the module where it is defined, it
3645 * can be declared via
3646 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003647 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003650 * @param minsz Size of the smallest blocks in the pool (in bytes).
3651 * @param maxsz Size of the largest blocks in the pool (in bytes).
3652 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003654 */
Andy Ross73cb9582017-05-09 10:42:39 -07003655#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3656 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3657 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3658 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3659 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3660 .buf = _mpool_buf_##name, \
3661 .max_sz = maxsz, \
3662 .n_max = nmax, \
3663 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3664 .levels = _mpool_lvls_##name, \
3665 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003666
Peter Mitsis937042c2016-10-13 13:18:26 -04003667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003672 * @param pool Address of the memory pool.
3673 * @param block Pointer to block descriptor for the allocated memory.
3674 * @param size Amount of memory to allocate (in bytes).
3675 * @param timeout Maximum time to wait for operation to complete
3676 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3677 * or K_FOREVER to wait as long as necessary.
3678 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003679 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003680 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003681 * @retval -ENOMEM Returned without waiting.
3682 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003683 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003684extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003685 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003686
3687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * This routine releases a previously allocated memory block back to its
3691 * memory pool.
3692 *
3693 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003694 *
3695 * @return N/A
3696 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003697extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003698
3699/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003700 * @brief Free memory allocated from a memory pool.
3701 *
3702 * This routine releases a previously allocated memory block back to its
3703 * memory pool
3704 *
3705 * @param id Memory block identifier.
3706 *
3707 * @return N/A
3708 */
3709extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3710
3711/**
Anas Nashif166f5192018-02-25 08:02:36 -06003712 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003713 */
3714
3715/**
3716 * @defgroup heap_apis Heap Memory Pool APIs
3717 * @ingroup kernel_apis
3718 * @{
3719 */
3720
3721/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003724 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003725 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003727 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003729 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003730 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003731extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003732
3733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003734 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003735 *
3736 * This routine provides traditional free() semantics. The memory being
3737 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003738 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003739 * If @a ptr is NULL, no operation is performed.
3740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003742 *
3743 * @return N/A
3744 */
3745extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746
Allan Stephensc98da842016-11-11 15:45:03 -05003747/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003748 * @brief Allocate memory from heap, array style
3749 *
3750 * This routine provides traditional calloc() semantics. Memory is
3751 * allocated from the heap memory pool and zeroed.
3752 *
3753 * @param nmemb Number of elements in the requested array
3754 * @param size Size of each array element (in bytes).
3755 *
3756 * @return Address of the allocated memory if successful; otherwise NULL.
3757 */
3758extern void *k_calloc(size_t nmemb, size_t size);
3759
Anas Nashif166f5192018-02-25 08:02:36 -06003760/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003761
Benjamin Walshacc68c12017-01-29 18:57:45 -05003762/* polling API - PRIVATE */
3763
Benjamin Walshb0179862017-02-02 16:39:57 -05003764#ifdef CONFIG_POLL
3765#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3766#else
3767#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3768#endif
3769
Benjamin Walshacc68c12017-01-29 18:57:45 -05003770/* private - implementation data created as needed, per-type */
3771struct _poller {
3772 struct k_thread *thread;
3773};
3774
3775/* private - types bit positions */
3776enum _poll_types_bits {
3777 /* can be used to ignore an event */
3778 _POLL_TYPE_IGNORE,
3779
3780 /* to be signaled by k_poll_signal() */
3781 _POLL_TYPE_SIGNAL,
3782
3783 /* semaphore availability */
3784 _POLL_TYPE_SEM_AVAILABLE,
3785
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003786 /* queue/fifo/lifo data availability */
3787 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003788
3789 _POLL_NUM_TYPES
3790};
3791
3792#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3793
3794/* private - states bit positions */
3795enum _poll_states_bits {
3796 /* default state when creating event */
3797 _POLL_STATE_NOT_READY,
3798
Benjamin Walshacc68c12017-01-29 18:57:45 -05003799 /* signaled by k_poll_signal() */
3800 _POLL_STATE_SIGNALED,
3801
3802 /* semaphore is available */
3803 _POLL_STATE_SEM_AVAILABLE,
3804
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003805 /* data is available to read on queue/fifo/lifo */
3806 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003807
3808 _POLL_NUM_STATES
3809};
3810
3811#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3812
3813#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003814 (32 - (0 \
3815 + 8 /* tag */ \
3816 + _POLL_NUM_TYPES \
3817 + _POLL_NUM_STATES \
3818 + 1 /* modes */ \
3819 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820
Benjamin Walshacc68c12017-01-29 18:57:45 -05003821/* end of polling API - PRIVATE */
3822
3823
3824/**
3825 * @defgroup poll_apis Async polling APIs
3826 * @ingroup kernel_apis
3827 * @{
3828 */
3829
3830/* Public polling API */
3831
3832/* public - values for k_poll_event.type bitfield */
3833#define K_POLL_TYPE_IGNORE 0
3834#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3835#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003836#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3837#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838
3839/* public - polling modes */
3840enum k_poll_modes {
3841 /* polling thread does not take ownership of objects when available */
3842 K_POLL_MODE_NOTIFY_ONLY = 0,
3843
3844 K_POLL_NUM_MODES
3845};
3846
3847/* public - values for k_poll_event.state bitfield */
3848#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003849#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3850#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003851#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3852#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003853
3854/* public - poll signal object */
3855struct k_poll_signal {
3856 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003857 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858
3859 /*
3860 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3861 * user resets it to 0.
3862 */
3863 unsigned int signaled;
3864
3865 /* custom result value passed to k_poll_signal() if needed */
3866 int result;
3867};
3868
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003869#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003870 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003871 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003872 .signaled = 0, \
3873 .result = 0, \
3874 }
3875
3876struct k_poll_event {
3877 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003878 sys_dnode_t _node;
3879
3880 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003881 struct _poller *poller;
3882
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003883 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003884 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003885
Benjamin Walshacc68c12017-01-29 18:57:45 -05003886 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003887 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003888
3889 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003890 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003891
3892 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003893 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894
3895 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003896 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003897
3898 /* per-type data */
3899 union {
3900 void *obj;
3901 struct k_poll_signal *signal;
3902 struct k_sem *sem;
3903 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003904 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003905 };
3906};
3907
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003908#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003909 { \
3910 .poller = NULL, \
3911 .type = event_type, \
3912 .state = K_POLL_STATE_NOT_READY, \
3913 .mode = event_mode, \
3914 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003915 { .obj = event_obj }, \
3916 }
3917
3918#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3919 event_tag) \
3920 { \
3921 .type = event_type, \
3922 .tag = event_tag, \
3923 .state = K_POLL_STATE_NOT_READY, \
3924 .mode = event_mode, \
3925 .unused = 0, \
3926 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003927 }
3928
3929/**
3930 * @brief Initialize one struct k_poll_event instance
3931 *
3932 * After this routine is called on a poll event, the event it ready to be
3933 * placed in an event array to be passed to k_poll().
3934 *
3935 * @param event The event to initialize.
3936 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3937 * values. Only values that apply to the same object being polled
3938 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3939 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003940 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003941 * @param obj Kernel object or poll signal.
3942 *
3943 * @return N/A
3944 */
3945
Kumar Galacc334c72017-04-21 10:55:34 -05003946extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003947 int mode, void *obj);
3948
3949/**
3950 * @brief Wait for one or many of multiple poll events to occur
3951 *
3952 * This routine allows a thread to wait concurrently for one or many of
3953 * multiple poll events to have occurred. Such events can be a kernel object
3954 * being available, like a semaphore, or a poll signal event.
3955 *
3956 * When an event notifies that a kernel object is available, the kernel object
3957 * is not "given" to the thread calling k_poll(): it merely signals the fact
3958 * that the object was available when the k_poll() call was in effect. Also,
3959 * all threads trying to acquire an object the regular way, i.e. by pending on
3960 * the object, have precedence over the thread polling on the object. This
3961 * means that the polling thread will never get the poll event on an object
3962 * until the object becomes available and its pend queue is empty. For this
3963 * reason, the k_poll() call is more effective when the objects being polled
3964 * only have one thread, the polling thread, trying to acquire them.
3965 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003966 * When k_poll() returns 0, the caller should loop on all the events that were
3967 * passed to k_poll() and check the state field for the values that were
3968 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003969 *
3970 * Before being reused for another call to k_poll(), the user has to reset the
3971 * state field to K_POLL_STATE_NOT_READY.
3972 *
3973 * @param events An array of pointers to events to be polled for.
3974 * @param num_events The number of events in the array.
3975 * @param timeout Waiting period for an event to be ready (in milliseconds),
3976 * or one of the special values K_NO_WAIT and K_FOREVER.
3977 *
3978 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003979 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003980 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003981 */
3982
3983extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003984 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003985
3986/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003987 * @brief Initialize a poll signal object.
3988 *
3989 * Ready a poll signal object to be signaled via k_poll_signal().
3990 *
3991 * @param signal A poll signal.
3992 *
3993 * @return N/A
3994 */
3995
3996extern void k_poll_signal_init(struct k_poll_signal *signal);
3997
3998/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003999 * @brief Signal a poll signal object.
4000 *
4001 * This routine makes ready a poll signal, which is basically a poll event of
4002 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4003 * made ready to run. A @a result value can be specified.
4004 *
4005 * The poll signal contains a 'signaled' field that, when set by
4006 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
4007 * be reset by the user before being passed again to k_poll() or k_poll() will
4008 * consider it being signaled, and will return immediately.
4009 *
4010 * @param signal A poll signal.
4011 * @param result The value to store in the result field of the signal.
4012 *
4013 * @retval 0 The signal was delivered successfully.
4014 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4015 */
4016
4017extern int k_poll_signal(struct k_poll_signal *signal, int result);
4018
Anas Nashif954d5502018-02-25 08:37:28 -06004019/**
4020 * @internal
4021 */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004022extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004023
Anas Nashif166f5192018-02-25 08:02:36 -06004024/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004025
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004026/**
4027 * @brief Make the CPU idle.
4028 *
4029 * This function makes the CPU idle until an event wakes it up.
4030 *
4031 * In a regular system, the idle thread should be the only thread responsible
4032 * for making the CPU idle and triggering any type of power management.
4033 * However, in some more constrained systems, such as a single-threaded system,
4034 * the only thread would be responsible for this if needed.
4035 *
4036 * @return N/A
4037 */
4038extern void k_cpu_idle(void);
4039
4040/**
4041 * @brief Make the CPU idle in an atomic fashion.
4042 *
4043 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4044 * must be done atomically before making the CPU idle.
4045 *
4046 * @param key Interrupt locking key obtained from irq_lock().
4047 *
4048 * @return N/A
4049 */
4050extern void k_cpu_atomic_idle(unsigned int key);
4051
Anas Nashif954d5502018-02-25 08:37:28 -06004052
4053/**
4054 * @internal
4055 */
Kumar Galacc334c72017-04-21 10:55:34 -05004056extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004057
Andrew Boiecdb94d62017-04-18 15:22:05 -07004058#ifdef _ARCH_EXCEPT
4059/* This archtecture has direct support for triggering a CPU exception */
4060#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4061#else
4062
Andrew Boiecdb94d62017-04-18 15:22:05 -07004063/* NOTE: This is the implementation for arches that do not implement
4064 * _ARCH_EXCEPT() to generate a real CPU exception.
4065 *
4066 * We won't have a real exception frame to determine the PC value when
4067 * the oops occurred, so print file and line number before we jump into
4068 * the fatal error handler.
4069 */
4070#define _k_except_reason(reason) do { \
4071 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4072 _NanoFatalErrorHandler(reason, &_default_esf); \
4073 CODE_UNREACHABLE; \
4074 } while (0)
4075
4076#endif /* _ARCH__EXCEPT */
4077
4078/**
4079 * @brief Fatally terminate a thread
4080 *
4081 * This should be called when a thread has encountered an unrecoverable
4082 * runtime condition and needs to terminate. What this ultimately
4083 * means is determined by the _fatal_error_handler() implementation, which
4084 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4085 *
4086 * If this is called from ISR context, the default system fatal error handler
4087 * will treat it as an unrecoverable system error, just like k_panic().
4088 */
4089#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4090
4091/**
4092 * @brief Fatally terminate the system
4093 *
4094 * This should be called when the Zephyr kernel has encountered an
4095 * unrecoverable runtime condition and needs to terminate. What this ultimately
4096 * means is determined by the _fatal_error_handler() implementation, which
4097 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4098 */
4099#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4100
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004101/*
4102 * private APIs that are utilized by one or more public APIs
4103 */
4104
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004105#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004106/**
4107 * @internal
4108 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004109extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004110#else
Anas Nashif954d5502018-02-25 08:37:28 -06004111/**
4112 * @internal
4113 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004114#define _init_static_threads() do { } while ((0))
4115#endif
4116
Anas Nashif954d5502018-02-25 08:37:28 -06004117/**
4118 * @internal
4119 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004120extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004121/**
4122 * @internal
4123 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004124extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004125
Andrew Boiedc5d9352017-06-02 12:56:47 -07004126/* arch/cpu.h may declare an architecture or platform-specific macro
4127 * for properly declaring stacks, compatible with MMU/MPU constraints if
4128 * enabled
4129 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004130
4131/**
4132 * @brief Obtain an extern reference to a stack
4133 *
4134 * This macro properly brings the symbol of a thread stack declared
4135 * elsewhere into scope.
4136 *
4137 * @param sym Thread stack symbol name
4138 */
4139#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4140
Andrew Boiedc5d9352017-06-02 12:56:47 -07004141#ifdef _ARCH_THREAD_STACK_DEFINE
4142#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4143#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4144 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4145#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4146#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004147static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004148{
4149 return _ARCH_THREAD_STACK_BUFFER(sym);
4150}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004151#else
4152/**
4153 * @brief Declare a toplevel thread stack memory region
4154 *
4155 * This declares a region of memory suitable for use as a thread's stack.
4156 *
4157 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4158 * 'noinit' section so that it isn't zeroed at boot
4159 *
Andrew Boie507852a2017-07-25 18:47:07 -07004160 * The declared symbol will always be a k_thread_stack_t which can be passed to
4161 * k_thread_create, but should otherwise not be manipulated. If the buffer
4162 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004163 *
4164 * It is legal to precede this definition with the 'static' keyword.
4165 *
4166 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4167 * parameter of k_thread_create(), it may not be the same as the
4168 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4169 *
4170 * @param sym Thread stack symbol name
4171 * @param size Size of the stack memory region
4172 */
4173#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004174 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004175
4176/**
4177 * @brief Declare a toplevel array of thread stack memory regions
4178 *
4179 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4180 * definition for additional details and constraints.
4181 *
4182 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4183 * 'noinit' section so that it isn't zeroed at boot
4184 *
4185 * @param sym Thread stack symbol name
4186 * @param nmemb Number of stacks to declare
4187 * @param size Size of the stack memory region
4188 */
4189
4190#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004191 struct _k_thread_stack_element __noinit \
4192 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004193
4194/**
4195 * @brief Declare an embedded stack memory region
4196 *
4197 * Used for stacks embedded within other data structures. Use is highly
4198 * discouraged but in some cases necessary. For memory protection scenarios,
4199 * it is very important that any RAM preceding this member not be writable
4200 * by threads else a stack overflow will lead to silent corruption. In other
4201 * words, the containing data structure should live in RAM owned by the kernel.
4202 *
4203 * @param sym Thread stack symbol name
4204 * @param size Size of the stack memory region
4205 */
4206#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004207 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004208
4209/**
4210 * @brief Return the size in bytes of a stack memory region
4211 *
4212 * Convenience macro for passing the desired stack size to k_thread_create()
4213 * since the underlying implementation may actually create something larger
4214 * (for instance a guard area).
4215 *
4216 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004217 * passed to K_THREAD_STACK_DEFINE.
4218 *
4219 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4220 * it is not guaranteed to return the original value since each array
4221 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004222 *
4223 * @param sym Stack memory symbol
4224 * @return Size of the stack
4225 */
4226#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4227
4228/**
4229 * @brief Get a pointer to the physical stack buffer
4230 *
4231 * Convenience macro to get at the real underlying stack buffer used by
4232 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4233 * This is really only intended for diagnostic tools which want to examine
4234 * stack memory contents.
4235 *
4236 * @param sym Declared stack symbol name
4237 * @return The buffer itself, a char *
4238 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004239static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004240{
4241 return (char *)sym;
4242}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004243
4244#endif /* _ARCH_DECLARE_STACK */
4245
Chunlin Hane9c97022017-07-07 20:29:30 +08004246/**
4247 * @defgroup mem_domain_apis Memory domain APIs
4248 * @ingroup kernel_apis
4249 * @{
4250 */
4251
4252/** @def MEM_PARTITION_ENTRY
4253 * @brief Used to declare a memory partition entry
4254 */
4255#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4256 {\
4257 .start = _start, \
4258 .size = _size, \
4259 .attr = _attr, \
4260 }
4261
4262/** @def K_MEM_PARTITION_DEFINE
4263 * @brief Used to declare a memory partition
4264 */
4265#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4266#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4267 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304268 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004269 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4270#else
4271#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304272 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004273 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4274#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4275
Chunlin Hane9c97022017-07-07 20:29:30 +08004276/* memory partition */
4277struct k_mem_partition {
4278 /* start address of memory partition */
4279 u32_t start;
4280 /* size of memory partition */
4281 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304282#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004283 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304284 k_mem_partition_attr_t attr;
4285#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004286};
4287
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304288/* memory domian
4289 * Note: Always declare this structure with __kernel prefix
4290 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004291struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304292#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004293 /* partitions in the domain */
4294 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304295#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004296 /* domain q */
4297 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004298 /* number of partitions in the domain */
4299 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004300};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304301
Chunlin Hane9c97022017-07-07 20:29:30 +08004302
4303/**
4304 * @brief Initialize a memory domain.
4305 *
4306 * Initialize a memory domain with given name and memory partitions.
4307 *
4308 * @param domain The memory domain to be initialized.
4309 * @param num_parts The number of array items of "parts" parameter.
4310 * @param parts An array of pointers to the memory partitions. Can be NULL
4311 * if num_parts is zero.
4312 */
4313
Leandro Pereira08de6582018-02-28 14:22:57 -08004314extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004315 struct k_mem_partition *parts[]);
4316/**
4317 * @brief Destroy a memory domain.
4318 *
4319 * Destroy a memory domain.
4320 *
4321 * @param domain The memory domain to be destroyed.
4322 */
4323
4324extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4325
4326/**
4327 * @brief Add a memory partition into a memory domain.
4328 *
4329 * Add a memory partition into a memory domain.
4330 *
4331 * @param domain The memory domain to be added a memory partition.
4332 * @param part The memory partition to be added
4333 */
4334
4335extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4336 struct k_mem_partition *part);
4337
4338/**
4339 * @brief Remove a memory partition from a memory domain.
4340 *
4341 * Remove a memory partition from a memory domain.
4342 *
4343 * @param domain The memory domain to be removed a memory partition.
4344 * @param part The memory partition to be removed
4345 */
4346
4347extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4348 struct k_mem_partition *part);
4349
4350/**
4351 * @brief Add a thread into a memory domain.
4352 *
4353 * Add a thread into a memory domain.
4354 *
4355 * @param domain The memory domain that the thread is going to be added into.
4356 * @param thread ID of thread going to be added into the memory domain.
4357 */
4358
4359extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4360 k_tid_t thread);
4361
4362/**
4363 * @brief Remove a thread from its memory domain.
4364 *
4365 * Remove a thread from its memory domain.
4366 *
4367 * @param thread ID of thread going to be removed from its memory domain.
4368 */
4369
4370extern void k_mem_domain_remove_thread(k_tid_t thread);
4371
Anas Nashif166f5192018-02-25 08:02:36 -06004372/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004373
Andrew Boie756f9072017-10-10 16:01:49 -07004374/**
4375 * @brief Emit a character buffer to the console device
4376 *
4377 * @param c String of characters to print
4378 * @param n The length of the string
4379 */
4380__syscall void k_str_out(char *c, size_t n);
4381
Andy Rosse7172672018-01-24 15:48:32 -08004382/**
4383 * @brief Start a numbered CPU on a MP-capable system
4384
4385 * This starts and initializes a specific CPU. The main thread on
4386 * startup is running on CPU zero, other processors are numbered
4387 * sequentially. On return from this function, the CPU is known to
4388 * have begun operating and will enter the provided function. Its
4389 * interrupts will be initialied but disabled such that irq_unlock()
4390 * with the provided key will work to enable them.
4391 *
4392 * Normally, in SMP mode this function will be called by the kernel
4393 * initialization and should not be used as a user API. But it is
4394 * defined here for special-purpose apps which want Zephyr running on
4395 * one core and to use others for design-specific processing.
4396 *
4397 * @param cpu_num Integer number of the CPU
4398 * @param stack Stack memory for the CPU
4399 * @param sz Stack buffer size, in bytes
4400 * @param fn Function to begin running on the CPU. First argument is
4401 * an irq_unlock() key.
4402 * @param arg Untyped argument to be passed to "fn"
4403 */
4404extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4405 void (*fn)(int, void *), void *arg);
4406
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004407#ifdef __cplusplus
4408}
4409#endif
4410
Andrew Boiee004dec2016-11-07 09:01:19 -08004411#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4412/*
4413 * Define new and delete operators.
4414 * At this moment, the operators do nothing since objects are supposed
4415 * to be statically allocated.
4416 */
4417inline void operator delete(void *ptr)
4418{
4419 (void)ptr;
4420}
4421
4422inline void operator delete[](void *ptr)
4423{
4424 (void)ptr;
4425}
4426
4427inline void *operator new(size_t size)
4428{
4429 (void)size;
4430 return NULL;
4431}
4432
4433inline void *operator new[](size_t size)
4434{
4435 (void)size;
4436 return NULL;
4437}
4438
4439/* Placement versions of operator new and delete */
4440inline void operator delete(void *ptr1, void *ptr2)
4441{
4442 (void)ptr1;
4443 (void)ptr2;
4444}
4445
4446inline void operator delete[](void *ptr1, void *ptr2)
4447{
4448 (void)ptr1;
4449 (void)ptr2;
4450}
4451
4452inline void *operator new(size_t size, void *ptr)
4453{
4454 (void)size;
4455 return ptr;
4456}
4457
4458inline void *operator new[](size_t size, void *ptr)
4459{
4460 (void)size;
4461 return ptr;
4462}
4463
4464#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4465
Andrew Boiefa94ee72017-09-28 16:54:35 -07004466#include <syscalls/kernel.h>
4467
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004468#endif /* !_ASMLANGUAGE */
4469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004470#endif /* _kernel__h_ */