blob: 43addc98841bf76ac48649cd0b64323d2dc53f09 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070029#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070032#include <misc/printk.h>
33#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034
35#ifdef __cplusplus
36extern "C" {
37#endif
38
Anas Nashifbbb157d2017-01-15 08:46:31 -050039/**
40 * @brief Kernel APIs
41 * @defgroup kernel_apis Kernel APIs
42 * @{
43 * @}
44 */
45
Anas Nashif61f4b242016-11-18 10:53:59 -050046#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040047#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
48#else
49#define K_DEBUG(fmt, ...)
50#endif
51
Benjamin Walsh2f280412017-01-14 19:23:46 -050052#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
54#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
55#elif defined(CONFIG_COOP_ENABLED)
56#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
57#define _NUM_PREEMPT_PRIO (0)
58#elif defined(CONFIG_PREEMPT_ENABLED)
59#define _NUM_COOP_PRIO (0)
60#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
61#else
62#error "invalid configuration"
63#endif
64
65#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_PRIO_PREEMPT(x) (x)
67
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_ANY NULL
69#define K_END NULL
70
Benjamin Walshedb35702017-01-14 18:47:22 -050071#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050073#elif defined(CONFIG_COOP_ENABLED)
74#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
75#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050077#else
78#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#endif
80
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050081#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
83#else
84#define K_LOWEST_THREAD_PRIO -1
85#endif
86
Benjamin Walshfab8d922016-11-08 15:36:36 -050087#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
88
Benjamin Walsh456c6da2016-09-02 18:55:39 -040089#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
90#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
91
92typedef sys_dlist_t _wait_q_t;
93
Anas Nashif2f203c22016-12-18 06:57:45 -050094#ifdef CONFIG_OBJECT_TRACING
95#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
96#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#else
Anas Nashif2f203c22016-12-18 06:57:45 -050098#define _OBJECT_TRACING_INIT
99#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100#endif
101
Benjamin Walshacc68c12017-01-29 18:57:45 -0500102#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300103#define _POLL_EVENT_OBJ_INIT(obj) \
104 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
105#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500108#define _POLL_EVENT
109#endif
110
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500111struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112struct k_mutex;
113struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400114struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115struct k_msgq;
116struct k_mbox;
117struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200118struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_fifo;
120struct k_lifo;
121struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400122struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_mem_pool;
124struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500125struct k_poll_event;
126struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800127struct k_mem_domain;
128struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400129
Andrew Boie5bd891d2017-09-27 12:59:28 -0700130/* This enumeration needs to be kept in sync with the lists of kernel objects
131 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
132 * function in kernel/userspace.c
133 */
Andrew Boie945af952017-08-22 13:15:23 -0700134enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700135 K_OBJ_ANY,
136
Andrew Boie5bd891d2017-09-27 12:59:28 -0700137 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700138 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700139 K_OBJ_MSGQ,
140 K_OBJ_MUTEX,
141 K_OBJ_PIPE,
142 K_OBJ_SEM,
143 K_OBJ_STACK,
144 K_OBJ_THREAD,
145 K_OBJ_TIMER,
Andrew Boiebca15da2017-10-15 14:17:48 -0700146 K_OBJ__THREAD_STACK_ELEMENT,
Andrew Boie945af952017-08-22 13:15:23 -0700147
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148 /* Driver subsystems */
149 K_OBJ_DRIVER_ADC,
150 K_OBJ_DRIVER_AIO_CMP,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700151 K_OBJ_DRIVER_COUNTER,
152 K_OBJ_DRIVER_CRYPTO,
Andrew Boiece6c8f32018-02-09 13:58:37 -0800153 K_OBJ_DRIVER_DMA,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700154 K_OBJ_DRIVER_FLASH,
155 K_OBJ_DRIVER_GPIO,
156 K_OBJ_DRIVER_I2C,
157 K_OBJ_DRIVER_I2S,
158 K_OBJ_DRIVER_IPM,
159 K_OBJ_DRIVER_PINMUX,
160 K_OBJ_DRIVER_PWM,
Leandro Pereirada9b0dd2017-10-13 16:30:55 -0700161 K_OBJ_DRIVER_ENTROPY,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700162 K_OBJ_DRIVER_RTC,
163 K_OBJ_DRIVER_SENSOR,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700164 K_OBJ_DRIVER_SPI,
165 K_OBJ_DRIVER_UART,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700166
Andrew Boie945af952017-08-22 13:15:23 -0700167 K_OBJ_LAST
168};
169
170#ifdef CONFIG_USERSPACE
171/* Table generated by gperf, these objects are retrieved via
172 * _k_object_find() */
173struct _k_object {
174 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700175 u8_t perms[CONFIG_MAX_THREAD_BYTES];
176 u8_t type;
177 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700178 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700179} __packed;
180
Andrew Boie877f82e2017-10-17 11:20:22 -0700181struct _k_object_assignment {
182 struct k_thread *thread;
183 void * const *objects;
184};
185
186/**
187 * @brief Grant a static thread access to a list of kernel objects
188 *
189 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
190 * a set of kernel objects. These objects do not need to be in an initialized
191 * state. The permissions will be granted when the threads are initialized
192 * in the early boot sequence.
193 *
194 * All arguments beyond the first must be pointers to kernel objects.
195 *
196 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
197 */
198#define K_THREAD_ACCESS_GRANT(name_, ...) \
199 static void * const _CONCAT(_object_list_, name_)[] = \
200 { __VA_ARGS__, NULL }; \
201 static __used __in_section_unique(object_access) \
202 const struct _k_object_assignment \
203 _CONCAT(_object_access_, name_) = \
204 { (&_k_thread_obj_ ## name_), \
205 (_CONCAT(_object_list_, name_)) }
206
Andrew Boie945af952017-08-22 13:15:23 -0700207#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700208#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700209
210/**
211 * Lookup a kernel object and init its metadata if it exists
212 *
213 * Calling this on an object will make it usable from userspace.
214 * Intended to be called as the last statement in kernel object init
215 * functions.
216 *
217 * @param object Address of the kernel object
218 */
219void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700220#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700221
222#define K_THREAD_ACCESS_GRANT(thread, ...)
223
Andrew Boie743e4682017-10-04 12:25:50 -0700224static inline void _k_object_init(void *obj)
225{
226 ARG_UNUSED(obj);
227}
228
229static inline void _impl_k_object_access_grant(void *object,
230 struct k_thread *thread)
231{
232 ARG_UNUSED(object);
233 ARG_UNUSED(thread);
234}
235
Andrew Boiea89bf012017-10-09 14:47:55 -0700236static inline void _impl_k_object_access_revoke(void *object,
237 struct k_thread *thread)
238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Andrew Boie41bab6e2017-10-14 14:42:23 -0700243static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700244{
245 ARG_UNUSED(object);
246}
247#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700248
249/**
250 * grant a thread access to a kernel object
251 *
252 * The thread will be granted access to the object if the caller is from
253 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700254 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700255 *
256 * @param object Address of kernel object
257 * @param thread Thread to grant access to the object
258 */
Andrew Boie743e4682017-10-04 12:25:50 -0700259__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700260
Andrew Boiea89bf012017-10-09 14:47:55 -0700261/**
262 * grant a thread access to a kernel object
263 *
264 * The thread will lose access to the object if the caller is from
265 * supervisor mode, or the caller is from user mode AND has permissions
266 * on both the object and the thread whose access is being revoked.
267 *
268 * @param object Address of kernel object
269 * @param thread Thread to remove access to the object
270 */
271__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700272
273/**
274 * grant all present and future threads access to an object
275 *
276 * If the caller is from supervisor mode, or the caller is from user mode and
277 * have sufficient permissions on the object, then that object will have
278 * permissions granted to it for *all* current and future threads running in
279 * the system, effectively becoming a public kernel object.
280 *
281 * Use of this API should be avoided on systems that are running untrusted code
282 * as it is possible for such code to derive the addresses of kernel objects
283 * and perform unwanted operations on them.
284 *
Andrew Boie04caa672017-10-13 13:57:07 -0700285 * It is not possible to revoke permissions on public objects; once public,
286 * any thread may use it.
287 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700288 * @param object Address of kernel object
289 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700290void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700291
Andrew Boiebca15da2017-10-15 14:17:48 -0700292/* Using typedef deliberately here, this is quite intended to be an opaque
293 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
294 *
295 * The purpose of this data type is to clearly distinguish between the
296 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
297 * buffer which composes the stack data actually used by the underlying
298 * thread; they cannot be used interchangably as some arches precede the
299 * stack buffer region with guard areas that trigger a MPU or MMU fault
300 * if written to.
301 *
302 * APIs that want to work with the buffer inside should continue to use
303 * char *.
304 *
305 * Stacks should always be created with K_THREAD_STACK_DEFINE().
306 */
307struct __packed _k_thread_stack_element {
308 char data;
309};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700310typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700311
Andrew Boie73abd322017-04-04 13:19:13 -0700312/* timeouts */
313
314struct _timeout;
315typedef void (*_timeout_func_t)(struct _timeout *t);
316
317struct _timeout {
318 sys_dnode_t node;
319 struct k_thread *thread;
320 sys_dlist_t *wait_q;
321 s32_t delta_ticks_from_prev;
322 _timeout_func_t func;
323};
324
325extern s32_t _timeout_remaining_get(struct _timeout *timeout);
326
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700327/**
328 * @typedef k_thread_entry_t
329 * @brief Thread entry point function type.
330 *
331 * A thread's entry point function is invoked when the thread starts executing.
332 * Up to 3 argument values can be passed to the function.
333 *
334 * The thread terminates execution permanently if the entry point function
335 * returns. The thread is responsible for releasing any shared resources
336 * it may own (such as mutexes and dynamically allocated memory), prior to
337 * returning.
338 *
339 * @param p1 First argument.
340 * @param p2 Second argument.
341 * @param p3 Third argument.
342 *
343 * @return N/A
344 */
345typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700346
347#ifdef CONFIG_THREAD_MONITOR
348struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700349 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700350 void *parameter1;
351 void *parameter2;
352 void *parameter3;
353};
354#endif
355
356/* can be used for creating 'dummy' threads, e.g. for pending on objects */
357struct _thread_base {
358
359 /* this thread's entry in a ready/wait queue */
360 sys_dnode_t k_q_node;
361
362 /* user facing 'thread options'; values defined in include/kernel.h */
363 u8_t user_options;
364
365 /* thread state */
366 u8_t thread_state;
367
368 /*
369 * scheduler lock count and thread priority
370 *
371 * These two fields control the preemptibility of a thread.
372 *
373 * When the scheduler is locked, sched_locked is decremented, which
374 * means that the scheduler is locked for values from 0xff to 0x01. A
375 * thread is coop if its prio is negative, thus 0x80 to 0xff when
376 * looked at the value as unsigned.
377 *
378 * By putting them end-to-end, this means that a thread is
379 * non-preemptible if the bundled value is greater than or equal to
380 * 0x0080.
381 */
382 union {
383 struct {
384#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
385 u8_t sched_locked;
386 s8_t prio;
387#else /* LITTLE and PDP */
388 s8_t prio;
389 u8_t sched_locked;
390#endif
391 };
392 u16_t preempt;
393 };
394
Andy Ross2724fd12018-01-29 14:55:20 -0800395#ifdef CONFIG_SMP
396 /* True for the per-CPU idle threads */
397 u8_t is_idle;
398
399 /* Non-zero when actively running on a CPU */
400 u8_t active;
401
402 /* CPU index on which thread was last run */
403 u8_t cpu;
404#endif
405
Andrew Boie73abd322017-04-04 13:19:13 -0700406 /* data returned by APIs */
407 void *swap_data;
408
409#ifdef CONFIG_SYS_CLOCK_EXISTS
410 /* this thread's entry in a timeout queue */
411 struct _timeout timeout;
412#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700413};
414
415typedef struct _thread_base _thread_base_t;
416
417#if defined(CONFIG_THREAD_STACK_INFO)
418/* Contains the stack information of a thread */
419struct _thread_stack_info {
420 /* Stack Start */
421 u32_t start;
422 /* Stack Size */
423 u32_t size;
424};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700425
426typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700427#endif /* CONFIG_THREAD_STACK_INFO */
428
Chunlin Hane9c97022017-07-07 20:29:30 +0800429#if defined(CONFIG_USERSPACE)
430struct _mem_domain_info {
431 /* memory domain queue node */
432 sys_dnode_t mem_domain_q_node;
433 /* memory domain of the thread */
434 struct k_mem_domain *mem_domain;
435};
436
437#endif /* CONFIG_USERSPACE */
438
Andrew Boie73abd322017-04-04 13:19:13 -0700439struct k_thread {
440
441 struct _thread_base base;
442
443 /* defined by the architecture, but all archs need these */
444 struct _caller_saved caller_saved;
445 struct _callee_saved callee_saved;
446
447 /* static thread init data */
448 void *init_data;
449
450 /* abort function */
451 void (*fn_abort)(void);
452
453#if defined(CONFIG_THREAD_MONITOR)
454 /* thread entry and parameters description */
455 struct __thread_entry *entry;
456
457 /* next item in list of all threads */
458 struct k_thread *next_thread;
459#endif
460
461#ifdef CONFIG_THREAD_CUSTOM_DATA
462 /* crude thread-local storage */
463 void *custom_data;
464#endif
465
466#ifdef CONFIG_ERRNO
467 /* per-thread errno variable */
468 int errno_var;
469#endif
470
471#if defined(CONFIG_THREAD_STACK_INFO)
472 /* Stack Info */
473 struct _thread_stack_info stack_info;
474#endif /* CONFIG_THREAD_STACK_INFO */
475
Chunlin Hane9c97022017-07-07 20:29:30 +0800476#if defined(CONFIG_USERSPACE)
477 /* memory domain info of the thread */
478 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700479 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700480 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800481#endif /* CONFIG_USERSPACE */
482
Andy Ross042d8ec2017-12-09 08:37:20 -0800483#if defined(CONFIG_USE_SWITCH)
484 /* When using __switch() a few previously arch-specific items
485 * become part of the core OS
486 */
487
488 /* _Swap() return value */
489 int swap_retval;
490
491 /* Context handle returned via _arch_switch() */
492 void *switch_handle;
493#endif
494
Andrew Boie73abd322017-04-04 13:19:13 -0700495 /* arch-specifics: must always be at the end */
496 struct _thread_arch arch;
497};
498
499typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400500typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700501#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400502
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503enum execution_context_types {
504 K_ISR = 0,
505 K_COOP_THREAD,
506 K_PREEMPT_THREAD,
507};
508
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400509/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100510 * @defgroup profiling_apis Profiling APIs
511 * @ingroup kernel_apis
512 * @{
513 */
514
515/**
516 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
517 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700518 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100519 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
520 *
521 * CONFIG_MAIN_STACK_SIZE
522 * CONFIG_IDLE_STACK_SIZE
523 * CONFIG_ISR_STACK_SIZE
524 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
525 *
526 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
527 * produce output.
528 *
529 * @return N/A
530 */
531extern void k_call_stacks_analyze(void);
532
533/**
534 * @} end defgroup profiling_apis
535 */
536
537/**
Allan Stephensc98da842016-11-11 15:45:03 -0500538 * @defgroup thread_apis Thread APIs
539 * @ingroup kernel_apis
540 * @{
541 */
542
Benjamin Walshed240f22017-01-22 13:05:08 -0500543#endif /* !_ASMLANGUAGE */
544
545
546/*
547 * Thread user options. May be needed by assembly code. Common part uses low
548 * bits, arch-specific use high bits.
549 */
550
551/* system thread that must not abort */
552#define K_ESSENTIAL (1 << 0)
553
554#if defined(CONFIG_FP_SHARING)
555/* thread uses floating point registers */
556#define K_FP_REGS (1 << 1)
557#endif
558
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700559/* This thread has dropped from supervisor mode to user mode and consequently
560 * has additional restrictions
561 */
562#define K_USER (1 << 2)
563
Andrew Boie47f8fd12017-10-05 11:11:02 -0700564/* Indicates that the thread being created should inherit all kernel object
565 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
566 * is not enabled.
567 */
568#define K_INHERIT_PERMS (1 << 3)
569
Benjamin Walshed240f22017-01-22 13:05:08 -0500570#ifdef CONFIG_X86
571/* x86 Bitmask definitions for threads user options */
572
573#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
574/* thread uses SSEx (and also FP) registers */
575#define K_SSE_REGS (1 << 7)
576#endif
577#endif
578
579/* end - thread options */
580
581#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700583 * @brief Create a thread.
584 *
585 * This routine initializes a thread, then schedules it for execution.
586 *
587 * The new thread may be scheduled for immediate execution or a delayed start.
588 * If the newly spawned thread does not have a delayed start the kernel
589 * scheduler may preempt the current thread to allow the new thread to
590 * execute.
591 *
592 * Thread options are architecture-specific, and can include K_ESSENTIAL,
593 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
594 * them using "|" (the logical OR operator).
595 *
596 * Historically, users often would use the beginning of the stack memory region
597 * to store the struct k_thread data, although corruption will occur if the
598 * stack overflows this region and stack protection features may not detect this
599 * situation.
600 *
601 * @param new_thread Pointer to uninitialized struct k_thread
602 * @param stack Pointer to the stack space.
603 * @param stack_size Stack size in bytes.
604 * @param entry Thread entry function.
605 * @param p1 1st entry point parameter.
606 * @param p2 2nd entry point parameter.
607 * @param p3 3rd entry point parameter.
608 * @param prio Thread priority.
609 * @param options Thread options.
610 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
611 *
612 * @return ID of new thread.
613 */
Andrew Boie662c3452017-10-02 10:51:18 -0700614__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700615 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700616 size_t stack_size,
617 k_thread_entry_t entry,
618 void *p1, void *p2, void *p3,
619 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700620
Andrew Boie3f091b52017-08-30 14:34:14 -0700621/**
622 * @brief Drop a thread's privileges permanently to user mode
623 *
624 * @param entry Function to start executing from
625 * @param p1 1st entry point parameter
626 * @param p2 2nd entry point parameter
627 * @param p3 3rd entry point parameter
628 */
629extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
630 void *p1, void *p2,
631 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700632
Andrew Boied26cf2d2017-03-30 13:07:02 -0700633/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700634 * @brief Grant a thread access to a NULL-terminated set of kernel objects
635 *
636 * This is a convenience function. For the provided thread, grant access to
637 * the remaining arguments, which must be pointers to kernel objects.
638 * The final argument must be a NULL.
639 *
640 * The thread object must be initialized (i.e. running). The objects don't
641 * need to be.
642 *
643 * @param thread Thread to grant access to objects
644 * @param ... NULL-terminated list of kernel object pointers
645 */
646extern void __attribute__((sentinel))
647 k_thread_access_grant(struct k_thread *thread, ...);
648
649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500650 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651 *
Allan Stephensc98da842016-11-11 15:45:03 -0500652 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500653 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500655 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400656 *
657 * @return N/A
658 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700659__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660
661/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500662 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400663 *
664 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500665 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400667 * @return N/A
668 */
Kumar Galacc334c72017-04-21 10:55:34 -0500669extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400670
671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500674 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500676 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677 *
678 * @return N/A
679 */
Andrew Boie468190a2017-09-29 14:00:48 -0700680__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681
682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500687 * If @a thread is not currently sleeping, the routine has no effect.
688 *
689 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400690 *
691 * @return N/A
692 */
Andrew Boie468190a2017-09-29 14:00:48 -0700693__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694
695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700700__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701
702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500705 * This routine prevents @a thread from executing if it has not yet started
706 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500708 * @param thread ID of thread to cancel.
709 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700710 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500711 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400712 */
Andrew Boie468190a2017-09-29 14:00:48 -0700713__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400714
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400715/**
Allan Stephensc98da842016-11-11 15:45:03 -0500716 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500718 * This routine permanently stops execution of @a thread. The thread is taken
719 * off all kernel queues it is part of (i.e. the ready queue, the timeout
720 * queue, or a kernel object wait queue). However, any kernel resources the
721 * thread might currently own (such as mutexes or memory blocks) are not
722 * released. It is the responsibility of the caller of this routine to ensure
723 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400726 *
727 * @return N/A
728 */
Andrew Boie468190a2017-09-29 14:00:48 -0700729__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400730
Andrew Boie7d627c52017-08-30 11:01:56 -0700731
732/**
733 * @brief Start an inactive thread
734 *
735 * If a thread was created with K_FOREVER in the delay parameter, it will
736 * not be added to the scheduling queue until this function is called
737 * on it.
738 *
739 * @param thread thread to start
740 */
Andrew Boie468190a2017-09-29 14:00:48 -0700741__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700742
Allan Stephensc98da842016-11-11 15:45:03 -0500743/**
744 * @cond INTERNAL_HIDDEN
745 */
746
Benjamin Walshd211a522016-12-06 11:44:01 -0500747/* timeout has timed out and is not on _timeout_q anymore */
748#define _EXPIRED (-2)
749
750/* timeout is not in use */
751#define _INACTIVE (-1)
752
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400753struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700754 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700755 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400756 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700757 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500758 void *init_p1;
759 void *init_p2;
760 void *init_p3;
761 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500762 u32_t init_options;
763 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500764 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400765};
766
Andrew Boied26cf2d2017-03-30 13:07:02 -0700767#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400768 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500769 prio, options, delay, abort, groups) \
770 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700771 .init_thread = (thread), \
772 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500773 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700774 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400775 .init_p1 = (void *)p1, \
776 .init_p2 = (void *)p2, \
777 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500778 .init_prio = (prio), \
779 .init_options = (options), \
780 .init_delay = (delay), \
781 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400782 }
783
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400784/**
Allan Stephensc98da842016-11-11 15:45:03 -0500785 * INTERNAL_HIDDEN @endcond
786 */
787
788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500789 * @brief Statically define and initialize a thread.
790 *
791 * The thread may be scheduled for immediate execution or a delayed start.
792 *
793 * Thread options are architecture-specific, and can include K_ESSENTIAL,
794 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
795 * them using "|" (the logical OR operator).
796 *
797 * The ID of the thread can be accessed using:
798 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500799 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 *
801 * @param name Name of the thread.
802 * @param stack_size Stack size in bytes.
803 * @param entry Thread entry function.
804 * @param p1 1st entry point parameter.
805 * @param p2 2nd entry point parameter.
806 * @param p3 3rd entry point parameter.
807 * @param prio Thread priority.
808 * @param options Thread options.
809 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400810 *
811 * @internal It has been observed that the x86 compiler by default aligns
812 * these _static_thread_data structures to 32-byte boundaries, thereby
813 * wasting space. To work around this, force a 4-byte alignment.
814 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500815#define K_THREAD_DEFINE(name, stack_size, \
816 entry, p1, p2, p3, \
817 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700818 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700819 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500820 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500821 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700822 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
823 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500824 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700825 NULL, 0); \
826 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400827
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @param thread ID of thread whose priority is needed.
834 *
835 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700837__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838
839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 *
844 * Rescheduling can occur immediately depending on the priority @a thread is
845 * set to:
846 *
847 * - If its priority is raised above the priority of the caller of this
848 * function, and the caller is preemptible, @a thread will be scheduled in.
849 *
850 * - If the caller operates on itself, it lowers its priority below that of
851 * other threads in the system, and the caller is preemptible, the thread of
852 * highest priority will be scheduled in.
853 *
854 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
855 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
856 * highest priority.
857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 * @param prio New priority.
860 *
861 * @warning Changing the priority of a thread currently involved in mutex
862 * priority inheritance may result in undefined behavior.
863 *
864 * @return N/A
865 */
Andrew Boie468190a2017-09-29 14:00:48 -0700866__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400867
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * This routine prevents the kernel scheduler from making @a thread the
872 * current thread. All other internal operations on @a thread are still
873 * performed; for example, any timeout it is waiting on keeps ticking,
874 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * If @a thread is already suspended, the routine has no effect.
877 *
878 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
880 * @return N/A
881 */
Andrew Boie468190a2017-09-29 14:00:48 -0700882__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883
884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * This routine allows the kernel scheduler to make @a thread the current
888 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * If @a thread is not currently suspended, the routine has no effect.
891 *
892 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 *
894 * @return N/A
895 */
Andrew Boie468190a2017-09-29 14:00:48 -0700896__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400897
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * This routine specifies how the scheduler will perform time slicing of
902 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500904 * To enable time slicing, @a slice must be non-zero. The scheduler
905 * ensures that no thread runs for more than the specified time limit
906 * before other threads of that priority are given a chance to execute.
907 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700908 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500910 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400911 * execute. Once the scheduler selects a thread for execution, there is no
912 * minimum guaranteed time the thread will execute before threads of greater or
913 * equal priority are scheduled.
914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916 * for execution, this routine has no effect; the thread is immediately
917 * rescheduled after the slice period expires.
918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500919 * To disable timeslicing, set both @a slice and @a prio to zero.
920 *
921 * @param slice Maximum time slice length (in milliseconds).
922 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400923 *
924 * @return N/A
925 */
Kumar Galacc334c72017-04-21 10:55:34 -0500926extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400927
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928/**
Allan Stephensc98da842016-11-11 15:45:03 -0500929 * @} end defgroup thread_apis
930 */
931
932/**
933 * @addtogroup isr_apis
934 * @{
935 */
936
937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939 *
Allan Stephensc98da842016-11-11 15:45:03 -0500940 * This routine allows the caller to customize its actions, depending on
941 * whether it is a thread or an ISR.
942 *
943 * @note Can be called by ISRs.
944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500945 * @return 0 if invoked by a thread.
946 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400947 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500948extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949
Benjamin Walsh445830d2016-11-10 15:54:27 -0500950/**
951 * @brief Determine if code is running in a preemptible thread.
952 *
Allan Stephensc98da842016-11-11 15:45:03 -0500953 * This routine allows the caller to customize its actions, depending on
954 * whether it can be preempted by another thread. The routine returns a 'true'
955 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500956 *
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * - The code is running in a thread, not at ISR.
958 * - The thread's priority is in the preemptible range.
959 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500960 *
Allan Stephensc98da842016-11-11 15:45:03 -0500961 * @note Can be called by ISRs.
962 *
963 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500964 * @return Non-zero if invoked by a preemptible thread.
965 */
Andrew Boie468190a2017-09-29 14:00:48 -0700966__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500967
Allan Stephensc98da842016-11-11 15:45:03 -0500968/**
969 * @} end addtogroup isr_apis
970 */
971
972/**
973 * @addtogroup thread_apis
974 * @{
975 */
976
977/**
978 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500979 *
Allan Stephensc98da842016-11-11 15:45:03 -0500980 * This routine prevents the current thread from being preempted by another
981 * thread by instructing the scheduler to treat it as a cooperative thread.
982 * If the thread subsequently performs an operation that makes it unready,
983 * it will be context switched out in the normal manner. When the thread
984 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500985 *
Allan Stephensc98da842016-11-11 15:45:03 -0500986 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500987 *
Allan Stephensc98da842016-11-11 15:45:03 -0500988 * @note k_sched_lock() and k_sched_unlock() should normally be used
989 * when the operation being performed can be safely interrupted by ISRs.
990 * However, if the amount of processing involved is very small, better
991 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500992 *
993 * @return N/A
994 */
995extern void k_sched_lock(void);
996
Allan Stephensc98da842016-11-11 15:45:03 -0500997/**
998 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500999 *
Allan Stephensc98da842016-11-11 15:45:03 -05001000 * This routine reverses the effect of a previous call to k_sched_lock().
1001 * A thread must call the routine once for each time it called k_sched_lock()
1002 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001003 *
1004 * @return N/A
1005 */
1006extern void k_sched_unlock(void);
1007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * Custom data is not used by the kernel itself, and is freely available
1014 * for a thread to use as it sees fit. It can be used as a framework
1015 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 *
1019 * @return N/A
1020 */
Andrew Boie468190a2017-09-29 14:00:48 -07001021__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022
1023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 */
Andrew Boie468190a2017-09-29 14:00:48 -07001030__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031
1032/**
Allan Stephensc98da842016-11-11 15:45:03 -05001033 * @} end addtogroup thread_apis
1034 */
1035
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001036#include <sys_clock.h>
1037
Allan Stephensc2f15a42016-11-17 12:24:22 -05001038/**
1039 * @addtogroup clock_apis
1040 * @{
1041 */
1042
1043/**
1044 * @brief Generate null timeout delay.
1045 *
1046 * This macro generates a timeout delay that that instructs a kernel API
1047 * not to wait if the requested operation cannot be performed immediately.
1048 *
1049 * @return Timeout delay value.
1050 */
1051#define K_NO_WAIT 0
1052
1053/**
1054 * @brief Generate timeout delay from milliseconds.
1055 *
1056 * This macro generates a timeout delay that that instructs a kernel API
1057 * to wait up to @a ms milliseconds to perform the requested operation.
1058 *
1059 * @param ms Duration in milliseconds.
1060 *
1061 * @return Timeout delay value.
1062 */
Johan Hedberg14471692016-11-13 10:52:15 +02001063#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001064
1065/**
1066 * @brief Generate timeout delay from seconds.
1067 *
1068 * This macro generates a timeout delay that that instructs a kernel API
1069 * to wait up to @a s seconds to perform the requested operation.
1070 *
1071 * @param s Duration in seconds.
1072 *
1073 * @return Timeout delay value.
1074 */
Johan Hedberg14471692016-11-13 10:52:15 +02001075#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001076
1077/**
1078 * @brief Generate timeout delay from minutes.
1079 *
1080 * This macro generates a timeout delay that that instructs a kernel API
1081 * to wait up to @a m minutes to perform the requested operation.
1082 *
1083 * @param m Duration in minutes.
1084 *
1085 * @return Timeout delay value.
1086 */
Johan Hedberg14471692016-11-13 10:52:15 +02001087#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001088
1089/**
1090 * @brief Generate timeout delay from hours.
1091 *
1092 * This macro generates a timeout delay that that instructs a kernel API
1093 * to wait up to @a h hours to perform the requested operation.
1094 *
1095 * @param h Duration in hours.
1096 *
1097 * @return Timeout delay value.
1098 */
Johan Hedberg14471692016-11-13 10:52:15 +02001099#define K_HOURS(h) K_MINUTES((h) * 60)
1100
Allan Stephensc98da842016-11-11 15:45:03 -05001101/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001102 * @brief Generate infinite timeout delay.
1103 *
1104 * This macro generates a timeout delay that that instructs a kernel API
1105 * to wait as long as necessary to perform the requested operation.
1106 *
1107 * @return Timeout delay value.
1108 */
1109#define K_FOREVER (-1)
1110
1111/**
1112 * @} end addtogroup clock_apis
1113 */
1114
1115/**
Allan Stephensc98da842016-11-11 15:45:03 -05001116 * @cond INTERNAL_HIDDEN
1117 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001118
Benjamin Walsh62092182016-12-20 14:39:08 -05001119/* kernel clocks */
1120
1121#if (sys_clock_ticks_per_sec == 1000) || \
1122 (sys_clock_ticks_per_sec == 500) || \
1123 (sys_clock_ticks_per_sec == 250) || \
1124 (sys_clock_ticks_per_sec == 125) || \
1125 (sys_clock_ticks_per_sec == 100) || \
1126 (sys_clock_ticks_per_sec == 50) || \
1127 (sys_clock_ticks_per_sec == 25) || \
1128 (sys_clock_ticks_per_sec == 20) || \
1129 (sys_clock_ticks_per_sec == 10) || \
1130 (sys_clock_ticks_per_sec == 1)
1131
1132 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1133#else
1134 /* yields horrible 64-bit math on many architectures: try to avoid */
1135 #define _NON_OPTIMIZED_TICKS_PER_SEC
1136#endif
1137
1138#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001139extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001140#else
Kumar Galacc334c72017-04-21 10:55:34 -05001141static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001142{
Kumar Galacc334c72017-04-21 10:55:34 -05001143 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001144}
1145#endif
1146
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001147/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001148#ifdef CONFIG_TICKLESS_KERNEL
1149#define _TICK_ALIGN 0
1150#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001151#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001152#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001153
Kumar Galacc334c72017-04-21 10:55:34 -05001154static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001155{
Benjamin Walsh62092182016-12-20 14:39:08 -05001156#ifdef CONFIG_SYS_CLOCK_EXISTS
1157
1158#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001159 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001160#else
Kumar Galacc334c72017-04-21 10:55:34 -05001161 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001162#endif
1163
1164#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001165 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001166 return 0;
1167#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001168}
1169
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170struct k_timer {
1171 /*
1172 * _timeout structure must be first here if we want to use
1173 * dynamic timer allocation. timeout.node is used in the double-linked
1174 * list of free timers
1175 */
1176 struct _timeout timeout;
1177
Allan Stephens45bfa372016-10-12 12:39:42 -05001178 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179 _wait_q_t wait_q;
1180
1181 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001182 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001183
1184 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001185 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001186
1187 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001188 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189
Allan Stephens45bfa372016-10-12 12:39:42 -05001190 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001191 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001193 /* user-specific data, also used to support legacy features */
1194 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001195
Anas Nashif2f203c22016-12-18 06:57:45 -05001196 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197};
1198
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001199#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001201 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001202 .timeout.wait_q = NULL, \
1203 .timeout.thread = NULL, \
1204 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001206 .expiry_fn = expiry, \
1207 .stop_fn = stop, \
1208 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001209 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001210 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211 }
1212
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001213#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1214
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215/**
Allan Stephensc98da842016-11-11 15:45:03 -05001216 * INTERNAL_HIDDEN @endcond
1217 */
1218
1219/**
1220 * @defgroup timer_apis Timer APIs
1221 * @ingroup kernel_apis
1222 * @{
1223 */
1224
1225/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001226 * @typedef k_timer_expiry_t
1227 * @brief Timer expiry function type.
1228 *
1229 * A timer's expiry function is executed by the system clock interrupt handler
1230 * each time the timer expires. The expiry function is optional, and is only
1231 * invoked if the timer has been initialized with one.
1232 *
1233 * @param timer Address of timer.
1234 *
1235 * @return N/A
1236 */
1237typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1238
1239/**
1240 * @typedef k_timer_stop_t
1241 * @brief Timer stop function type.
1242 *
1243 * A timer's stop function is executed if the timer is stopped prematurely.
1244 * The function runs in the context of the thread that stops the timer.
1245 * The stop function is optional, and is only invoked if the timer has been
1246 * initialized with one.
1247 *
1248 * @param timer Address of timer.
1249 *
1250 * @return N/A
1251 */
1252typedef void (*k_timer_stop_t)(struct k_timer *timer);
1253
1254/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001255 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001257 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001259 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260 *
1261 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * @param expiry_fn Function to invoke each time the timer expires.
1263 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001265#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001266 struct k_timer name \
1267 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001268 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269
Allan Stephens45bfa372016-10-12 12:39:42 -05001270/**
1271 * @brief Initialize a timer.
1272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001274 *
1275 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * @param expiry_fn Function to invoke each time the timer expires.
1277 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001278 *
1279 * @return N/A
1280 */
1281extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001282 k_timer_expiry_t expiry_fn,
1283 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001284
Allan Stephens45bfa372016-10-12 12:39:42 -05001285/**
1286 * @brief Start a timer.
1287 *
1288 * This routine starts a timer, and resets its status to zero. The timer
1289 * begins counting down using the specified duration and period values.
1290 *
1291 * Attempting to start a timer that is already running is permitted.
1292 * The timer's status is reset to zero and the timer begins counting down
1293 * using the new duration and period values.
1294 *
1295 * @param timer Address of timer.
1296 * @param duration Initial timer duration (in milliseconds).
1297 * @param period Timer period (in milliseconds).
1298 *
1299 * @return N/A
1300 */
Andrew Boiea354d492017-09-29 16:22:28 -07001301__syscall void k_timer_start(struct k_timer *timer,
1302 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001303
1304/**
1305 * @brief Stop a timer.
1306 *
1307 * This routine stops a running timer prematurely. The timer's stop function,
1308 * if one exists, is invoked by the caller.
1309 *
1310 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001311 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001312 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001313 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1314 * if @a k_timer_stop is to be called from ISRs.
1315 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001316 * @param timer Address of timer.
1317 *
1318 * @return N/A
1319 */
Andrew Boiea354d492017-09-29 16:22:28 -07001320__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001321
1322/**
1323 * @brief Read timer status.
1324 *
1325 * This routine reads the timer's status, which indicates the number of times
1326 * it has expired since its status was last read.
1327 *
1328 * Calling this routine resets the timer's status to zero.
1329 *
1330 * @param timer Address of timer.
1331 *
1332 * @return Timer status.
1333 */
Andrew Boiea354d492017-09-29 16:22:28 -07001334__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001335
1336/**
1337 * @brief Synchronize thread to timer expiration.
1338 *
1339 * This routine blocks the calling thread until the timer's status is non-zero
1340 * (indicating that it has expired at least once since it was last examined)
1341 * or the timer is stopped. If the timer status is already non-zero,
1342 * or the timer is already stopped, the caller continues without waiting.
1343 *
1344 * Calling this routine resets the timer's status to zero.
1345 *
1346 * This routine must not be used by interrupt handlers, since they are not
1347 * allowed to block.
1348 *
1349 * @param timer Address of timer.
1350 *
1351 * @return Timer status.
1352 */
Andrew Boiea354d492017-09-29 16:22:28 -07001353__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001354
1355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001357 *
1358 * This routine computes the (approximate) time remaining before a running
1359 * timer next expires. If the timer is not running, it returns zero.
1360 *
1361 * @param timer Address of timer.
1362 *
1363 * @return Remaining time (in milliseconds).
1364 */
Andrew Boiea354d492017-09-29 16:22:28 -07001365__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1366
1367static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001368{
1369 return _timeout_remaining_get(&timer->timeout);
1370}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001371
Allan Stephensc98da842016-11-11 15:45:03 -05001372/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001373 * @brief Associate user-specific data with a timer.
1374 *
1375 * This routine records the @a user_data with the @a timer, to be retrieved
1376 * later.
1377 *
1378 * It can be used e.g. in a timer handler shared across multiple subsystems to
1379 * retrieve data specific to the subsystem this timer is associated with.
1380 *
1381 * @param timer Address of timer.
1382 * @param user_data User data to associate with the timer.
1383 *
1384 * @return N/A
1385 */
Andrew Boiea354d492017-09-29 16:22:28 -07001386__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1387
1388static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1389 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001390{
1391 timer->user_data = user_data;
1392}
1393
1394/**
1395 * @brief Retrieve the user-specific data from a timer.
1396 *
1397 * @param timer Address of timer.
1398 *
1399 * @return The user data.
1400 */
Andrew Boiea354d492017-09-29 16:22:28 -07001401__syscall void *k_timer_user_data_get(struct k_timer *timer);
1402
1403static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001404{
1405 return timer->user_data;
1406}
1407
1408/**
Allan Stephensc98da842016-11-11 15:45:03 -05001409 * @} end defgroup timer_apis
1410 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001411
Allan Stephensc98da842016-11-11 15:45:03 -05001412/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001413 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001414 * @{
1415 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001416
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001417/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001418 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001420 * This routine returns the elapsed time since the system booted,
1421 * in milliseconds.
1422 *
1423 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001424 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001425__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001426
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001427#ifdef CONFIG_TICKLESS_KERNEL
1428/**
1429 * @brief Enable clock always on in tickless kernel
1430 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001431 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001432 * there are no timer events programmed in tickless kernel
1433 * scheduling. This is necessary if the clock is used to track
1434 * passage of time.
1435 *
1436 * @retval prev_status Previous status of always on flag
1437 */
1438static inline int k_enable_sys_clock_always_on(void)
1439{
1440 int prev_status = _sys_clock_always_on;
1441
1442 _sys_clock_always_on = 1;
1443 _enable_sys_clock();
1444
1445 return prev_status;
1446}
1447
1448/**
1449 * @brief Disable clock always on in tickless kernel
1450 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001451 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001452 * there are no timer events programmed in tickless kernel
1453 * scheduling. To save power, this routine should be called
1454 * immediately when clock is not used to track time.
1455 */
1456static inline void k_disable_sys_clock_always_on(void)
1457{
1458 _sys_clock_always_on = 0;
1459}
1460#else
1461#define k_enable_sys_clock_always_on() do { } while ((0))
1462#define k_disable_sys_clock_always_on() do { } while ((0))
1463#endif
1464
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001465/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * This routine returns the lower 32-bits of the elapsed time since the system
1469 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001471 * This routine can be more efficient than k_uptime_get(), as it reduces the
1472 * need for interrupt locking and 64-bit math. However, the 32-bit result
1473 * cannot hold a system uptime time larger than approximately 50 days, so the
1474 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001477 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001478__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479
1480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * This routine computes the elapsed time between the current system uptime
1484 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001486 * @param reftime Pointer to a reference time, which is updated to the current
1487 * uptime upon return.
1488 *
1489 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001490 */
Kumar Galacc334c72017-04-21 10:55:34 -05001491extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001492
1493/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001494 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * This routine computes the elapsed time between the current system uptime
1497 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1500 * need for interrupt locking and 64-bit math. However, the 32-bit result
1501 * cannot hold an elapsed time larger than approximately 50 days, so the
1502 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * @param reftime Pointer to a reference time, which is updated to the current
1505 * uptime upon return.
1506 *
1507 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001508 */
Kumar Galacc334c72017-04-21 10:55:34 -05001509extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001510
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * This routine returns the current time, as measured by the system's hardware
1515 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001517 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001518 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001519#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001520
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001521/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001522 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001523 */
1524
Allan Stephensc98da842016-11-11 15:45:03 -05001525/**
1526 * @cond INTERNAL_HIDDEN
1527 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001528
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001529struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001530 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001531 union {
1532 _wait_q_t wait_q;
1533
1534 _POLL_EVENT;
1535 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001536
1537 _OBJECT_TRACING_NEXT_PTR(k_queue);
1538};
1539
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001540#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001541 { \
1542 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1543 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001544 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001545 _OBJECT_TRACING_INIT \
1546 }
1547
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001548#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1549
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001550/**
1551 * INTERNAL_HIDDEN @endcond
1552 */
1553
1554/**
1555 * @defgroup queue_apis Queue APIs
1556 * @ingroup kernel_apis
1557 * @{
1558 */
1559
1560/**
1561 * @brief Initialize a queue.
1562 *
1563 * This routine initializes a queue object, prior to its first use.
1564 *
1565 * @param queue Address of the queue.
1566 *
1567 * @return N/A
1568 */
1569extern void k_queue_init(struct k_queue *queue);
1570
1571/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001572 * @brief Cancel waiting on a queue.
1573 *
1574 * This routine causes first thread pending on @a queue, if any, to
1575 * return from k_queue_get() call with NULL value (as if timeout expired).
1576 *
1577 * @note Can be called by ISRs.
1578 *
1579 * @param queue Address of the queue.
1580 *
1581 * @return N/A
1582 */
1583extern void k_queue_cancel_wait(struct k_queue *queue);
1584
1585/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001586 * @brief Append an element to the end of a queue.
1587 *
1588 * This routine appends a data item to @a queue. A queue data item must be
1589 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1590 * reserved for the kernel's use.
1591 *
1592 * @note Can be called by ISRs.
1593 *
1594 * @param queue Address of the queue.
1595 * @param data Address of the data item.
1596 *
1597 * @return N/A
1598 */
1599extern void k_queue_append(struct k_queue *queue, void *data);
1600
1601/**
1602 * @brief Prepend an element to a queue.
1603 *
1604 * This routine prepends a data item to @a queue. A queue data item must be
1605 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1606 * reserved for the kernel's use.
1607 *
1608 * @note Can be called by ISRs.
1609 *
1610 * @param queue Address of the queue.
1611 * @param data Address of the data item.
1612 *
1613 * @return N/A
1614 */
1615extern void k_queue_prepend(struct k_queue *queue, void *data);
1616
1617/**
1618 * @brief Inserts an element to a queue.
1619 *
1620 * This routine inserts a data item to @a queue after previous item. A queue
1621 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1622 * item are reserved for the kernel's use.
1623 *
1624 * @note Can be called by ISRs.
1625 *
1626 * @param queue Address of the queue.
1627 * @param prev Address of the previous data item.
1628 * @param data Address of the data item.
1629 *
1630 * @return N/A
1631 */
1632extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1633
1634/**
1635 * @brief Atomically append a list of elements to a queue.
1636 *
1637 * This routine adds a list of data items to @a queue in one operation.
1638 * The data items must be in a singly-linked list, with the first 32 bits
1639 * in each data item pointing to the next data item; the list must be
1640 * NULL-terminated.
1641 *
1642 * @note Can be called by ISRs.
1643 *
1644 * @param queue Address of the queue.
1645 * @param head Pointer to first node in singly-linked list.
1646 * @param tail Pointer to last node in singly-linked list.
1647 *
1648 * @return N/A
1649 */
1650extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1651
1652/**
1653 * @brief Atomically add a list of elements to a queue.
1654 *
1655 * This routine adds a list of data items to @a queue in one operation.
1656 * The data items must be in a singly-linked list implemented using a
1657 * sys_slist_t object. Upon completion, the original list is empty.
1658 *
1659 * @note Can be called by ISRs.
1660 *
1661 * @param queue Address of the queue.
1662 * @param list Pointer to sys_slist_t object.
1663 *
1664 * @return N/A
1665 */
1666extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1667
1668/**
1669 * @brief Get an element from a queue.
1670 *
1671 * This routine removes first data item from @a queue. The first 32 bits of the
1672 * data item are reserved for the kernel's use.
1673 *
1674 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1675 *
1676 * @param queue Address of the queue.
1677 * @param timeout Waiting period to obtain a data item (in milliseconds),
1678 * or one of the special values K_NO_WAIT and K_FOREVER.
1679 *
1680 * @return Address of the data item if successful; NULL if returned
1681 * without waiting, or waiting period timed out.
1682 */
Kumar Galacc334c72017-04-21 10:55:34 -05001683extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001684
1685/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001686 * @brief Remove an element from a queue.
1687 *
1688 * This routine removes data item from @a queue. The first 32 bits of the
1689 * data item are reserved for the kernel's use. Removing elements from k_queue
1690 * rely on sys_slist_find_and_remove which is not a constant time operation.
1691 *
1692 * @note Can be called by ISRs
1693 *
1694 * @param queue Address of the queue.
1695 * @param data Address of the data item.
1696 *
1697 * @return true if data item was removed
1698 */
1699static inline bool k_queue_remove(struct k_queue *queue, void *data)
1700{
1701 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1702}
1703
1704/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001705 * @brief Query a queue to see if it has data available.
1706 *
1707 * Note that the data might be already gone by the time this function returns
1708 * if other threads are also trying to read from the queue.
1709 *
1710 * @note Can be called by ISRs.
1711 *
1712 * @param queue Address of the queue.
1713 *
1714 * @return Non-zero if the queue is empty.
1715 * @return 0 if data is available.
1716 */
1717static inline int k_queue_is_empty(struct k_queue *queue)
1718{
1719 return (int)sys_slist_is_empty(&queue->data_q);
1720}
1721
1722/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001723 * @brief Peek element at the head of queue.
1724 *
1725 * Return element from the head of queue without removing it.
1726 *
1727 * @param queue Address of the queue.
1728 *
1729 * @return Head element, or NULL if queue is empty.
1730 */
1731static inline void *k_queue_peek_head(struct k_queue *queue)
1732{
1733 return sys_slist_peek_head(&queue->data_q);
1734}
1735
1736/**
1737 * @brief Peek element at the tail of queue.
1738 *
1739 * Return element from the tail of queue without removing it.
1740 *
1741 * @param queue Address of the queue.
1742 *
1743 * @return Tail element, or NULL if queue is empty.
1744 */
1745static inline void *k_queue_peek_tail(struct k_queue *queue)
1746{
1747 return sys_slist_peek_tail(&queue->data_q);
1748}
1749
1750/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001751 * @brief Statically define and initialize a queue.
1752 *
1753 * The queue can be accessed outside the module where it is defined using:
1754 *
1755 * @code extern struct k_queue <name>; @endcode
1756 *
1757 * @param name Name of the queue.
1758 */
1759#define K_QUEUE_DEFINE(name) \
1760 struct k_queue name \
1761 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001762 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001763
1764/**
1765 * @} end defgroup queue_apis
1766 */
1767
1768/**
1769 * @cond INTERNAL_HIDDEN
1770 */
1771
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001772struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001773 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774};
1775
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001776#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001777 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001778 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001779 }
1780
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001781#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1782
Allan Stephensc98da842016-11-11 15:45:03 -05001783/**
1784 * INTERNAL_HIDDEN @endcond
1785 */
1786
1787/**
1788 * @defgroup fifo_apis Fifo APIs
1789 * @ingroup kernel_apis
1790 * @{
1791 */
1792
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 *
1800 * @return N/A
1801 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001802#define k_fifo_init(fifo) \
1803 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804
1805/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001806 * @brief Cancel waiting on a fifo.
1807 *
1808 * This routine causes first thread pending on @a fifo, if any, to
1809 * return from k_fifo_get() call with NULL value (as if timeout
1810 * expired).
1811 *
1812 * @note Can be called by ISRs.
1813 *
1814 * @param fifo Address of the fifo.
1815 *
1816 * @return N/A
1817 */
1818#define k_fifo_cancel_wait(fifo) \
1819 k_queue_cancel_wait((struct k_queue *) fifo)
1820
1821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * This routine adds a data item to @a fifo. A fifo data item must be
1825 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1826 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * @note Can be called by ISRs.
1829 *
1830 * @param fifo Address of the fifo.
1831 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832 *
1833 * @return N/A
1834 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001835#define k_fifo_put(fifo, data) \
1836 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837
1838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001839 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001841 * This routine adds a list of data items to @a fifo in one operation.
1842 * The data items must be in a singly-linked list, with the first 32 bits
1843 * each data item pointing to the next data item; the list must be
1844 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001846 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * @param fifo Address of the fifo.
1849 * @param head Pointer to first node in singly-linked list.
1850 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
1852 * @return N/A
1853 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001854#define k_fifo_put_list(fifo, head, tail) \
1855 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856
1857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * This routine adds a list of data items to @a fifo in one operation.
1861 * The data items must be in a singly-linked list implemented using a
1862 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 * and must be re-initialized via sys_slist_init().
1864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @note Can be called by ISRs.
1866 *
1867 * @param fifo Address of the fifo.
1868 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869 *
1870 * @return N/A
1871 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001872#define k_fifo_put_slist(fifo, list) \
1873 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001874
1875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * This routine removes a data item from @a fifo in a "first in, first out"
1879 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1882 *
1883 * @param fifo Address of the fifo.
1884 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 * or one of the special values K_NO_WAIT and K_FOREVER.
1886 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001887 * @return Address of the data item if successful; NULL if returned
1888 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001890#define k_fifo_get(fifo, timeout) \
1891 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001893/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001894 * @brief Query a fifo to see if it has data available.
1895 *
1896 * Note that the data might be already gone by the time this function returns
1897 * if other threads is also trying to read from the fifo.
1898 *
1899 * @note Can be called by ISRs.
1900 *
1901 * @param fifo Address of the fifo.
1902 *
1903 * @return Non-zero if the fifo is empty.
1904 * @return 0 if data is available.
1905 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001906#define k_fifo_is_empty(fifo) \
1907 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001908
1909/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001910 * @brief Peek element at the head of fifo.
1911 *
1912 * Return element from the head of fifo without removing it. A usecase
1913 * for this is if elements of the fifo are themselves containers. Then
1914 * on each iteration of processing, a head container will be peeked,
1915 * and some data processed out of it, and only if the container is empty,
1916 * it will be completely remove from the fifo.
1917 *
1918 * @param fifo Address of the fifo.
1919 *
1920 * @return Head element, or NULL if the fifo is empty.
1921 */
1922#define k_fifo_peek_head(fifo) \
1923 k_queue_peek_head((struct k_queue *) fifo)
1924
1925/**
1926 * @brief Peek element at the tail of fifo.
1927 *
1928 * Return element from the tail of fifo (without removing it). A usecase
1929 * for this is if elements of the fifo are themselves containers. Then
1930 * it may be useful to add more data to the last container in fifo.
1931 *
1932 * @param fifo Address of the fifo.
1933 *
1934 * @return Tail element, or NULL if fifo is empty.
1935 */
1936#define k_fifo_peek_tail(fifo) \
1937 k_queue_peek_tail((struct k_queue *) fifo)
1938
1939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001944 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001949 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001950 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001951 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952
Allan Stephensc98da842016-11-11 15:45:03 -05001953/**
1954 * @} end defgroup fifo_apis
1955 */
1956
1957/**
1958 * @cond INTERNAL_HIDDEN
1959 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960
1961struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001962 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963};
1964
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001965#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001966 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001967 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001968 }
1969
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1971
Allan Stephensc98da842016-11-11 15:45:03 -05001972/**
1973 * INTERNAL_HIDDEN @endcond
1974 */
1975
1976/**
1977 * @defgroup lifo_apis Lifo APIs
1978 * @ingroup kernel_apis
1979 * @{
1980 */
1981
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001985 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 *
1989 * @return N/A
1990 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001991#define k_lifo_init(lifo) \
1992 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001993
1994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001995 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001997 * This routine adds a data item to @a lifo. A lifo data item must be
1998 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1999 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * @note Can be called by ISRs.
2002 *
2003 * @param lifo Address of the lifo.
2004 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
2006 * @return N/A
2007 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002008#define k_lifo_put(lifo, data) \
2009 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010
2011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002014 * This routine removes a data item from @a lifo in a "last in, first out"
2015 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2018 *
2019 * @param lifo Address of the lifo.
2020 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 * or one of the special values K_NO_WAIT and K_FOREVER.
2022 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002023 * @return Address of the data item if successful; NULL if returned
2024 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002026#define k_lifo_get(lifo, timeout) \
2027 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002030 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002034 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002038#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002039 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002040 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002041 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042
Allan Stephensc98da842016-11-11 15:45:03 -05002043/**
2044 * @} end defgroup lifo_apis
2045 */
2046
2047/**
2048 * @cond INTERNAL_HIDDEN
2049 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050
2051struct k_stack {
2052 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002053 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002054
Anas Nashif2f203c22016-12-18 06:57:45 -05002055 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056};
2057
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002058#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002059 { \
2060 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2061 .base = stack_buffer, \
2062 .next = stack_buffer, \
2063 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002064 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002065 }
2066
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002067#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2068
Allan Stephensc98da842016-11-11 15:45:03 -05002069/**
2070 * INTERNAL_HIDDEN @endcond
2071 */
2072
2073/**
2074 * @defgroup stack_apis Stack APIs
2075 * @ingroup kernel_apis
2076 * @{
2077 */
2078
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002079/**
2080 * @brief Initialize a stack.
2081 *
2082 * This routine initializes a stack object, prior to its first use.
2083 *
2084 * @param stack Address of the stack.
2085 * @param buffer Address of array used to hold stacked values.
2086 * @param num_entries Maximum number of values that can be stacked.
2087 *
2088 * @return N/A
2089 */
Andrew Boiee8734462017-09-29 16:42:07 -07002090__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002091 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092
2093/**
2094 * @brief Push an element onto a stack.
2095 *
2096 * This routine adds a 32-bit value @a data to @a stack.
2097 *
2098 * @note Can be called by ISRs.
2099 *
2100 * @param stack Address of the stack.
2101 * @param data Value to push onto the stack.
2102 *
2103 * @return N/A
2104 */
Andrew Boiee8734462017-09-29 16:42:07 -07002105__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106
2107/**
2108 * @brief Pop an element from a stack.
2109 *
2110 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2111 * manner and stores the value in @a data.
2112 *
2113 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2114 *
2115 * @param stack Address of the stack.
2116 * @param data Address of area to hold the value popped from the stack.
2117 * @param timeout Waiting period to obtain a value (in milliseconds),
2118 * or one of the special values K_NO_WAIT and K_FOREVER.
2119 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002120 * @retval 0 Element popped from stack.
2121 * @retval -EBUSY Returned without waiting.
2122 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 */
Andrew Boiee8734462017-09-29 16:42:07 -07002124__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002131 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @param name Name of the stack.
2134 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002135 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002136#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002137 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002138 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002139 struct k_stack name \
2140 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002141 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002142 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144/**
Allan Stephensc98da842016-11-11 15:45:03 -05002145 * @} end defgroup stack_apis
2146 */
2147
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148struct k_work;
2149
Allan Stephensc98da842016-11-11 15:45:03 -05002150/**
2151 * @defgroup workqueue_apis Workqueue Thread APIs
2152 * @ingroup kernel_apis
2153 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154 */
2155
Allan Stephens6bba9b02016-11-16 14:56:54 -05002156/**
2157 * @typedef k_work_handler_t
2158 * @brief Work item handler function type.
2159 *
2160 * A work item's handler function is executed by a workqueue's thread
2161 * when the work item is processed by the workqueue.
2162 *
2163 * @param work Address of the work item.
2164 *
2165 * @return N/A
2166 */
2167typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002168
2169/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002170 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002172
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002174 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002175 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176};
2177
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002179 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180};
2181
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002183 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184 k_work_handler_t handler;
2185 atomic_t flags[1];
2186};
2187
Allan Stephens6bba9b02016-11-16 14:56:54 -05002188struct k_delayed_work {
2189 struct k_work work;
2190 struct _timeout timeout;
2191 struct k_work_q *work_q;
2192};
2193
2194extern struct k_work_q k_sys_work_q;
2195
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002197 * INTERNAL_HIDDEN @endcond
2198 */
2199
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002200#define _K_WORK_INITIALIZER(work_handler) \
2201 { \
2202 ._reserved = NULL, \
2203 .handler = work_handler, \
2204 .flags = { 0 } \
2205 }
2206
2207#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2208
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209/**
2210 * @brief Initialize a statically-defined work item.
2211 *
2212 * This macro can be used to initialize a statically-defined workqueue work
2213 * item, prior to its first use. For example,
2214 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002215 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002216 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002217 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002220#define K_WORK_DEFINE(work, work_handler) \
2221 struct k_work work \
2222 __in_section(_k_work, static, work) = \
2223 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224
2225/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002226 * @brief Initialize a work item.
2227 *
2228 * This routine initializes a workqueue work item, prior to its first use.
2229 *
2230 * @param work Address of work item.
2231 * @param handler Function to invoke each time work item is processed.
2232 *
2233 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234 */
2235static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2236{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002237 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002239 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240}
2241
2242/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002243 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002244 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * This routine submits work item @a work to be processed by workqueue
2246 * @a work_q. If the work item is already pending in the workqueue's queue
2247 * as a result of an earlier submission, this routine has no effect on the
2248 * work item. If the work item has already been processed, or is currently
2249 * being processed, its work is considered complete and the work item can be
2250 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002251 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002252 * @warning
2253 * A submitted work item must not be modified until it has been processed
2254 * by the workqueue.
2255 *
2256 * @note Can be called by ISRs.
2257 *
2258 * @param work_q Address of workqueue.
2259 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002260 *
2261 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262 */
2263static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2264 struct k_work *work)
2265{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002266 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002267 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268 }
2269}
2270
2271/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002272 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002274 * This routine indicates if work item @a work is pending in a workqueue's
2275 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002277 * @note Can be called by ISRs.
2278 *
2279 * @param work Address of work item.
2280 *
2281 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002282 */
2283static inline int k_work_pending(struct k_work *work)
2284{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002285 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002286}
2287
2288/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002289 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002291 * This routine starts workqueue @a work_q. The workqueue spawns its work
2292 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002294 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002295 * @param stack Pointer to work queue thread's stack space, as defined by
2296 * K_THREAD_STACK_DEFINE()
2297 * @param stack_size Size of the work queue thread's stack (in bytes), which
2298 * should either be the same constant passed to
2299 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 *
2302 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303 */
Andrew Boie507852a2017-07-25 18:47:07 -07002304extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002305 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002306 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002309 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002311 * This routine initializes a workqueue delayed work item, prior to
2312 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002314 * @param work Address of delayed work item.
2315 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316 *
2317 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002319extern void k_delayed_work_init(struct k_delayed_work *work,
2320 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321
2322/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002323 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * This routine schedules work item @a work to be processed by workqueue
2326 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002327 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002328 * Only when the countdown completes is the work item actually submitted to
2329 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002331 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002332 * counting down cancels the existing submission and restarts the
2333 * countdown using the new delay. Note that this behavior is
2334 * inherently subject to race conditions with the pre-existing
2335 * timeouts and work queue, so care must be taken to synchronize such
2336 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002338 * @warning
2339 * A delayed work item must not be modified until it has been processed
2340 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002342 * @note Can be called by ISRs.
2343 *
2344 * @param work_q Address of workqueue.
2345 * @param work Address of delayed work item.
2346 * @param delay Delay before submitting the work item (in milliseconds).
2347 *
2348 * @retval 0 Work item countdown started.
2349 * @retval -EINPROGRESS Work item is already pending.
2350 * @retval -EINVAL Work item is being processed or has completed its work.
2351 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002353extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2354 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002355 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356
2357/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002358 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002360 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002361 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002362 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002364 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002366 * @param work Address of delayed work item.
2367 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002368 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002369 * @retval -EINPROGRESS Work item is already pending.
2370 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002372extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375 * @brief Submit a work item to the system workqueue.
2376 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002377 * This routine submits work item @a work to be processed by the system
2378 * workqueue. If the work item is already pending in the workqueue's queue
2379 * as a result of an earlier submission, this routine has no effect on the
2380 * work item. If the work item has already been processed, or is currently
2381 * being processed, its work is considered complete and the work item can be
2382 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002383 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002384 * @warning
2385 * Work items submitted to the system workqueue should avoid using handlers
2386 * that block or yield since this may prevent the system workqueue from
2387 * processing other work items in a timely manner.
2388 *
2389 * @note Can be called by ISRs.
2390 *
2391 * @param work Address of work item.
2392 *
2393 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002394 */
2395static inline void k_work_submit(struct k_work *work)
2396{
2397 k_work_submit_to_queue(&k_sys_work_q, work);
2398}
2399
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002400/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002401 * @brief Submit a delayed work item to the system workqueue.
2402 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002403 * This routine schedules work item @a work to be processed by the system
2404 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002405 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002406 * Only when the countdown completes is the work item actually submitted to
2407 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002409 * Submitting a previously submitted delayed work item that is still
2410 * counting down cancels the existing submission and restarts the countdown
2411 * using the new delay. If the work item is currently pending on the
2412 * workqueue's queue because the countdown has completed it is too late to
2413 * resubmit the item, and resubmission fails without impacting the work item.
2414 * If the work item has already been processed, or is currently being processed,
2415 * its work is considered complete and the work item can be resubmitted.
2416 *
2417 * @warning
2418 * Work items submitted to the system workqueue should avoid using handlers
2419 * that block or yield since this may prevent the system workqueue from
2420 * processing other work items in a timely manner.
2421 *
2422 * @note Can be called by ISRs.
2423 *
2424 * @param work Address of delayed work item.
2425 * @param delay Delay before submitting the work item (in milliseconds).
2426 *
2427 * @retval 0 Work item countdown started.
2428 * @retval -EINPROGRESS Work item is already pending.
2429 * @retval -EINVAL Work item is being processed or has completed its work.
2430 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431 */
2432static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002433 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002435 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436}
2437
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002439 * @brief Get time remaining before a delayed work gets scheduled.
2440 *
2441 * This routine computes the (approximate) time remaining before a
2442 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002443 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002444 *
2445 * @param work Delayed work item.
2446 *
2447 * @return Remaining time (in milliseconds).
2448 */
Kumar Galacc334c72017-04-21 10:55:34 -05002449static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002450{
2451 return _timeout_remaining_get(&work->timeout);
2452}
2453
2454/**
Allan Stephensc98da842016-11-11 15:45:03 -05002455 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 */
2457
Allan Stephensc98da842016-11-11 15:45:03 -05002458/**
2459 * @cond INTERNAL_HIDDEN
2460 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461
2462struct k_mutex {
2463 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002464 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002465 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467
Anas Nashif2f203c22016-12-18 06:57:45 -05002468 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469};
2470
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002471#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472 { \
2473 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2474 .owner = NULL, \
2475 .lock_count = 0, \
2476 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002477 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478 }
2479
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002480#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2481
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482/**
Allan Stephensc98da842016-11-11 15:45:03 -05002483 * INTERNAL_HIDDEN @endcond
2484 */
2485
2486/**
2487 * @defgroup mutex_apis Mutex APIs
2488 * @ingroup kernel_apis
2489 * @{
2490 */
2491
2492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002497 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002502 struct k_mutex name \
2503 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002504 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * Upon completion, the mutex is available and does not have an owner.
2512 *
2513 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 *
2515 * @return N/A
2516 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002517__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002518
2519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * This routine locks @a mutex. If the mutex is locked by another thread,
2523 * the calling thread waits until the mutex becomes available or until
2524 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * A thread is permitted to lock a mutex it has already locked. The operation
2527 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @param mutex Address of the mutex.
2530 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 * or one of the special values K_NO_WAIT and K_FOREVER.
2532 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002533 * @retval 0 Mutex locked.
2534 * @retval -EBUSY Returned without waiting.
2535 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002537__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538
2539/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * This routine unlocks @a mutex. The mutex must already be locked by the
2543 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 *
2545 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 * thread.
2548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 *
2551 * @return N/A
2552 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002553__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
Allan Stephensc98da842016-11-11 15:45:03 -05002555/**
2556 * @} end defgroup mutex_apis
2557 */
2558
2559/**
2560 * @cond INTERNAL_HIDDEN
2561 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
2563struct k_sem {
2564 _wait_q_t wait_q;
2565 unsigned int count;
2566 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002567 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568
Anas Nashif2f203c22016-12-18 06:57:45 -05002569 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570};
2571
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002572#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002573 { \
2574 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2575 .count = initial_count, \
2576 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002577 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002578 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002579 }
2580
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002581#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2582
Allan Stephensc98da842016-11-11 15:45:03 -05002583/**
2584 * INTERNAL_HIDDEN @endcond
2585 */
2586
2587/**
2588 * @defgroup semaphore_apis Semaphore APIs
2589 * @ingroup kernel_apis
2590 * @{
2591 */
2592
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * @param sem Address of the semaphore.
2599 * @param initial_count Initial semaphore count.
2600 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601 *
2602 * @return N/A
2603 */
Andrew Boie99280232017-09-29 14:17:47 -07002604__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2605 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002606
2607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2613 *
2614 * @param sem Address of the semaphore.
2615 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616 * or one of the special values K_NO_WAIT and K_FOREVER.
2617 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002618 * @note When porting code from the nanokernel legacy API to the new API, be
2619 * careful with the return value of this function. The return value is the
2620 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2621 * non-zero means failure, while the nano_sem_take family returns 1 for success
2622 * and 0 for failure.
2623 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002624 * @retval 0 Semaphore taken.
2625 * @retval -EBUSY Returned without waiting.
2626 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 */
Andrew Boie99280232017-09-29 14:17:47 -07002628__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002629
2630/**
2631 * @brief Give a semaphore.
2632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * This routine gives @a sem, unless the semaphore is already at its maximum
2634 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002639 *
2640 * @return N/A
2641 */
Andrew Boie99280232017-09-29 14:17:47 -07002642__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002644/**
2645 * @brief Reset a semaphore's count to zero.
2646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002650 *
2651 * @return N/A
2652 */
Andrew Boie990bf162017-10-03 12:36:49 -07002653__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002654
2655static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656{
2657 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658}
2659
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002660/**
2661 * @brief Get a semaphore's count.
2662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002668 */
Andrew Boie990bf162017-10-03 12:36:49 -07002669__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002670
2671static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672{
2673 return sem->count;
2674}
2675
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002680 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002681 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @param name Name of the semaphore.
2684 * @param initial_count Initial semaphore count.
2685 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002686 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002688 struct k_sem name \
2689 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002690 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002691
Allan Stephensc98da842016-11-11 15:45:03 -05002692/**
2693 * @} end defgroup semaphore_apis
2694 */
2695
2696/**
2697 * @defgroup alert_apis Alert APIs
2698 * @ingroup kernel_apis
2699 * @{
2700 */
2701
Allan Stephens5eceb852016-11-16 10:16:30 -05002702/**
2703 * @typedef k_alert_handler_t
2704 * @brief Alert handler function type.
2705 *
2706 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002707 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002708 * and is only invoked if the alert has been initialized with one.
2709 *
2710 * @param alert Address of the alert.
2711 *
2712 * @return 0 if alert has been consumed; non-zero if alert should pend.
2713 */
2714typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002715
2716/**
2717 * @} end defgroup alert_apis
2718 */
2719
2720/**
2721 * @cond INTERNAL_HIDDEN
2722 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002724#define K_ALERT_DEFAULT NULL
2725#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002727struct k_alert {
2728 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 atomic_t send_count;
2730 struct k_work work_item;
2731 struct k_sem sem;
2732
Anas Nashif2f203c22016-12-18 06:57:45 -05002733 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734};
2735
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002736extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002738#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002740 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002742 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2743 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002744 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745 }
2746
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002747#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2748
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002749/**
Allan Stephensc98da842016-11-11 15:45:03 -05002750 * INTERNAL_HIDDEN @endcond
2751 */
2752
2753/**
2754 * @addtogroup alert_apis
2755 * @{
2756 */
2757
2758/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002761 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002762 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002763 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @param name Name of the alert.
2766 * @param alert_handler Action to take when alert is sent. Specify either
2767 * the address of a function to be invoked by the system workqueue
2768 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2769 * K_ALERT_DEFAULT (which causes the alert to pend).
2770 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002771 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002772#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002773 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002774 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002775 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002776 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002781 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * @param alert Address of the alert.
2784 * @param handler Action to take when alert is sent. Specify either the address
2785 * of a function to be invoked by the system workqueue thread,
2786 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2787 * K_ALERT_DEFAULT (which causes the alert to pend).
2788 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 *
2790 * @return N/A
2791 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002792extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2793 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794
2795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2801 *
2802 * @param alert Address of the alert.
2803 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804 * or one of the special values K_NO_WAIT and K_FOREVER.
2805 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002806 * @retval 0 Alert received.
2807 * @retval -EBUSY Returned without waiting.
2808 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002809 */
Andrew Boie310e9872017-09-29 04:41:15 -07002810__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002811
2812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * This routine signals @a alert. The action specified for @a alert will
2816 * be taken, which may trigger the execution of an alert handler function
2817 * and/or cause the alert to pend (assuming the alert has not reached its
2818 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * @note Can be called by ISRs.
2821 *
2822 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 *
2824 * @return N/A
2825 */
Andrew Boie310e9872017-09-29 04:41:15 -07002826__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827
2828/**
Allan Stephensc98da842016-11-11 15:45:03 -05002829 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 */
2831
Allan Stephensc98da842016-11-11 15:45:03 -05002832/**
2833 * @cond INTERNAL_HIDDEN
2834 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835
2836struct k_msgq {
2837 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002838 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002839 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 char *buffer_start;
2841 char *buffer_end;
2842 char *read_ptr;
2843 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002844 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845
Anas Nashif2f203c22016-12-18 06:57:45 -05002846 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847};
2848
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002849#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850 { \
2851 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002852 .max_msgs = q_max_msgs, \
2853 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002855 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856 .read_ptr = q_buffer, \
2857 .write_ptr = q_buffer, \
2858 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002859 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860 }
2861
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002862#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2863
Peter Mitsis1da807e2016-10-06 11:36:59 -04002864/**
Allan Stephensc98da842016-11-11 15:45:03 -05002865 * INTERNAL_HIDDEN @endcond
2866 */
2867
2868/**
2869 * @defgroup msgq_apis Message Queue APIs
2870 * @ingroup kernel_apis
2871 * @{
2872 */
2873
2874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2878 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002879 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2880 * message is similarly aligned to this boundary, @a q_msg_size must also be
2881 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * The message queue can be accessed outside the module where it is defined
2884 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002885 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002886 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param q_name Name of the message queue.
2889 * @param q_msg_size Message size (in bytes).
2890 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002891 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002892 */
2893#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2894 static char __noinit __aligned(q_align) \
2895 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002896 struct k_msgq q_name \
2897 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002898 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002899 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900
Peter Mitsisd7a37502016-10-13 11:37:40 -04002901/**
2902 * @brief Initialize a message queue.
2903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * This routine initializes a message queue object, prior to its first use.
2905 *
Allan Stephensda827222016-11-09 14:23:58 -06002906 * The message queue's ring buffer must contain space for @a max_msgs messages,
2907 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2908 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2909 * that each message is similarly aligned to this boundary, @a q_msg_size
2910 * must also be a multiple of N.
2911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @param q Address of the message queue.
2913 * @param buffer Pointer to ring buffer that holds queued messages.
2914 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002915 * @param max_msgs Maximum number of messages that can be queued.
2916 *
2917 * @return N/A
2918 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002919__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2920 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002921
2922/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002926 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002927 * @note Can be called by ISRs.
2928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @param q Address of the message queue.
2930 * @param data Pointer to the message.
2931 * @param timeout Waiting period to add the message (in milliseconds),
2932 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002933 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002934 * @retval 0 Message sent.
2935 * @retval -ENOMSG Returned without waiting or queue purged.
2936 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002937 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002938__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002939
2940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * This routine receives a message from message queue @a q in a "first in,
2944 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002945 *
Allan Stephensc98da842016-11-11 15:45:03 -05002946 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @param q Address of the message queue.
2949 * @param data Address of area to hold the received message.
2950 * @param timeout Waiting period to receive the message (in milliseconds),
2951 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002952 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002953 * @retval 0 Message received.
2954 * @retval -ENOMSG Returned without waiting.
2955 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002956 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002957__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002958
2959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * This routine discards all unreceived messages in a message queue's ring
2963 * buffer. Any threads that are blocked waiting to send a message to the
2964 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002967 *
2968 * @return N/A
2969 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002970__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002971
Peter Mitsis67be2492016-10-07 11:44:34 -04002972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * This routine returns the number of unused entries in a message queue's
2976 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @param q Address of the message queue.
2979 *
2980 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002981 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002982__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2983
2984static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002985{
2986 return q->max_msgs - q->used_msgs;
2987}
2988
Peter Mitsisd7a37502016-10-13 11:37:40 -04002989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * @param q Address of the message queue.
2995 *
2996 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002997 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002998__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2999
3000static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001{
3002 return q->used_msgs;
3003}
3004
Allan Stephensc98da842016-11-11 15:45:03 -05003005/**
3006 * @} end defgroup msgq_apis
3007 */
3008
3009/**
3010 * @defgroup mem_pool_apis Memory Pool APIs
3011 * @ingroup kernel_apis
3012 * @{
3013 */
3014
Andy Ross73cb9582017-05-09 10:42:39 -07003015/* Note on sizing: the use of a 20 bit field for block means that,
3016 * assuming a reasonable minimum block size of 16 bytes, we're limited
3017 * to 16M of memory managed by a single pool. Long term it would be
3018 * good to move to a variable bit size based on configuration.
3019 */
3020struct k_mem_block_id {
3021 u32_t pool : 8;
3022 u32_t level : 4;
3023 u32_t block : 20;
3024};
3025
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003028 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029};
3030
Allan Stephensc98da842016-11-11 15:45:03 -05003031/**
3032 * @} end defgroup mem_pool_apis
3033 */
3034
3035/**
3036 * @defgroup mailbox_apis Mailbox APIs
3037 * @ingroup kernel_apis
3038 * @{
3039 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040
3041struct k_mbox_msg {
3042 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003043 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003045 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003047 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048 /** sender's message data buffer */
3049 void *tx_data;
3050 /** internal use only - needed for legacy API support */
3051 void *_rx_data;
3052 /** message data block descriptor */
3053 struct k_mem_block tx_block;
3054 /** source thread id */
3055 k_tid_t rx_source_thread;
3056 /** target thread id */
3057 k_tid_t tx_target_thread;
3058 /** internal use only - thread waiting on send (may be a dummy) */
3059 k_tid_t _syncing_thread;
3060#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3061 /** internal use only - semaphore used during asynchronous send */
3062 struct k_sem *_async_sem;
3063#endif
3064};
3065
Allan Stephensc98da842016-11-11 15:45:03 -05003066/**
3067 * @cond INTERNAL_HIDDEN
3068 */
3069
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070struct k_mbox {
3071 _wait_q_t tx_msg_queue;
3072 _wait_q_t rx_msg_queue;
3073
Anas Nashif2f203c22016-12-18 06:57:45 -05003074 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075};
3076
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003077#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078 { \
3079 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3080 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003081 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 }
3083
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003084#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3085
Peter Mitsis12092702016-10-14 12:57:23 -04003086/**
Allan Stephensc98da842016-11-11 15:45:03 -05003087 * INTERNAL_HIDDEN @endcond
3088 */
3089
3090/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003091 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003093 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003094 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003095 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003100 struct k_mbox name \
3101 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003102 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103
Peter Mitsis12092702016-10-14 12:57:23 -04003104/**
3105 * @brief Initialize a mailbox.
3106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003107 * This routine initializes a mailbox object, prior to its first use.
3108 *
3109 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003110 *
3111 * @return N/A
3112 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113extern void k_mbox_init(struct k_mbox *mbox);
3114
Peter Mitsis12092702016-10-14 12:57:23 -04003115/**
3116 * @brief Send a mailbox message in a synchronous manner.
3117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * This routine sends a message to @a mbox and waits for a receiver to both
3119 * receive and process it. The message data may be in a buffer, in a memory
3120 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * @param mbox Address of the mailbox.
3123 * @param tx_msg Address of the transmit message descriptor.
3124 * @param timeout Waiting period for the message to be received (in
3125 * milliseconds), or one of the special values K_NO_WAIT
3126 * and K_FOREVER. Once the message has been received,
3127 * this routine waits as long as necessary for the message
3128 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003129 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003130 * @retval 0 Message sent.
3131 * @retval -ENOMSG Returned without waiting.
3132 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003133 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003134extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003135 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003136
Peter Mitsis12092702016-10-14 12:57:23 -04003137/**
3138 * @brief Send a mailbox message in an asynchronous manner.
3139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * This routine sends a message to @a mbox without waiting for a receiver
3141 * to process it. The message data may be in a buffer, in a memory pool block,
3142 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3143 * will be given when the message has been both received and completely
3144 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * @param mbox Address of the mailbox.
3147 * @param tx_msg Address of the transmit message descriptor.
3148 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003149 *
3150 * @return N/A
3151 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003152extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003153 struct k_sem *sem);
3154
Peter Mitsis12092702016-10-14 12:57:23 -04003155/**
3156 * @brief Receive a mailbox message.
3157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * This routine receives a message from @a mbox, then optionally retrieves
3159 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @param mbox Address of the mailbox.
3162 * @param rx_msg Address of the receive message descriptor.
3163 * @param buffer Address of the buffer to receive data, or NULL to defer data
3164 * retrieval and message disposal until later.
3165 * @param timeout Waiting period for a message to be received (in
3166 * milliseconds), or one of the special values K_NO_WAIT
3167 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003168 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003169 * @retval 0 Message received.
3170 * @retval -ENOMSG Returned without waiting.
3171 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003172 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003173extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003174 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003175
3176/**
3177 * @brief Retrieve mailbox message data into a buffer.
3178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * This routine completes the processing of a received message by retrieving
3180 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003181 *
3182 * Alternatively, this routine can be used to dispose of a received message
3183 * without retrieving its data.
3184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * @param rx_msg Address of the receive message descriptor.
3186 * @param buffer Address of the buffer to receive data, or NULL to discard
3187 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003188 *
3189 * @return N/A
3190 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003191extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003192
3193/**
3194 * @brief Retrieve mailbox message data into a memory pool block.
3195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003196 * This routine completes the processing of a received message by retrieving
3197 * its data into a memory pool block, then disposing of the message.
3198 * The memory pool block that results from successful retrieval must be
3199 * returned to the pool once the data has been processed, even in cases
3200 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003201 *
3202 * Alternatively, this routine can be used to dispose of a received message
3203 * without retrieving its data. In this case there is no need to return a
3204 * memory pool block to the pool.
3205 *
3206 * This routine allocates a new memory pool block for the data only if the
3207 * data is not already in one. If a new block cannot be allocated, the routine
3208 * returns a failure code and the received message is left unchanged. This
3209 * permits the caller to reattempt data retrieval at a later time or to dispose
3210 * of the received message without retrieving its data.
3211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * @param rx_msg Address of a receive message descriptor.
3213 * @param pool Address of memory pool, or NULL to discard data.
3214 * @param block Address of the area to hold memory pool block info.
3215 * @param timeout Waiting period to wait for a memory pool block (in
3216 * milliseconds), or one of the special values K_NO_WAIT
3217 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003218 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003219 * @retval 0 Data retrieved.
3220 * @retval -ENOMEM Returned without waiting.
3221 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003222 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003223extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003224 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003225 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226
Allan Stephensc98da842016-11-11 15:45:03 -05003227/**
3228 * @} end defgroup mailbox_apis
3229 */
3230
3231/**
3232 * @cond INTERNAL_HIDDEN
3233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234
3235struct k_pipe {
3236 unsigned char *buffer; /* Pipe buffer: may be NULL */
3237 size_t size; /* Buffer size */
3238 size_t bytes_used; /* # bytes used in buffer */
3239 size_t read_index; /* Where in buffer to read from */
3240 size_t write_index; /* Where in buffer to write */
3241
3242 struct {
3243 _wait_q_t readers; /* Reader wait queue */
3244 _wait_q_t writers; /* Writer wait queue */
3245 } wait_q;
3246
Anas Nashif2f203c22016-12-18 06:57:45 -05003247 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248};
3249
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003250#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 { \
3252 .buffer = pipe_buffer, \
3253 .size = pipe_buffer_size, \
3254 .bytes_used = 0, \
3255 .read_index = 0, \
3256 .write_index = 0, \
3257 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3258 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003259 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 }
3261
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003262#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3263
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003264/**
Allan Stephensc98da842016-11-11 15:45:03 -05003265 * INTERNAL_HIDDEN @endcond
3266 */
3267
3268/**
3269 * @defgroup pipe_apis Pipe APIs
3270 * @ingroup kernel_apis
3271 * @{
3272 */
3273
3274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003278 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003279 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * @param name Name of the pipe.
3282 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3283 * or zero if no ring buffer is used.
3284 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003285 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003286#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3287 static unsigned char __noinit __aligned(pipe_align) \
3288 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003289 struct k_pipe name \
3290 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003291 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003296 * This routine initializes a pipe object, prior to its first use.
3297 *
3298 * @param pipe Address of the pipe.
3299 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3300 * is used.
3301 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3302 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303 *
3304 * @return N/A
3305 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003306__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3307 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308
3309/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * @param pipe Address of the pipe.
3315 * @param data Address of data to write.
3316 * @param bytes_to_write Size of data (in bytes).
3317 * @param bytes_written Address of area to hold the number of bytes written.
3318 * @param min_xfer Minimum number of bytes to write.
3319 * @param timeout Waiting period to wait for the data to be written (in
3320 * milliseconds), or one of the special values K_NO_WAIT
3321 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003323 * @retval 0 At least @a min_xfer bytes of data were written.
3324 * @retval -EIO Returned without waiting; zero data bytes were written.
3325 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003326 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003328__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3329 size_t bytes_to_write, size_t *bytes_written,
3330 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331
3332/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @param pipe Address of the pipe.
3338 * @param data Address to place the data read from pipe.
3339 * @param bytes_to_read Maximum number of data bytes to read.
3340 * @param bytes_read Address of area to hold the number of bytes read.
3341 * @param min_xfer Minimum number of data bytes to read.
3342 * @param timeout Waiting period to wait for the data to be read (in
3343 * milliseconds), or one of the special values K_NO_WAIT
3344 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003346 * @retval 0 At least @a min_xfer bytes of data were read.
3347 * @retval -EIO Returned without waiting; zero data bytes were read.
3348 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003351__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3352 size_t bytes_to_read, size_t *bytes_read,
3353 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003354
3355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * This routine writes the data contained in a memory block to @a pipe.
3359 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363 * @param block Memory block containing data to send
3364 * @param size Number of data bytes in memory block to send
3365 * @param sem Semaphore to signal upon completion (else NULL)
3366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 */
3369extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3370 size_t size, struct k_sem *sem);
3371
3372/**
Allan Stephensc98da842016-11-11 15:45:03 -05003373 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374 */
3375
Allan Stephensc98da842016-11-11 15:45:03 -05003376/**
3377 * @cond INTERNAL_HIDDEN
3378 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003380struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003382 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003383 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384 char *buffer;
3385 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003386 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003387
Anas Nashif2f203c22016-12-18 06:57:45 -05003388 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003389};
3390
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003391#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003392 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003393 { \
3394 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003395 .num_blocks = slab_num_blocks, \
3396 .block_size = slab_block_size, \
3397 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003398 .free_list = NULL, \
3399 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003400 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003401 }
3402
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003403#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3404
3405
Peter Mitsis578f9112016-10-07 13:50:31 -04003406/**
Allan Stephensc98da842016-11-11 15:45:03 -05003407 * INTERNAL_HIDDEN @endcond
3408 */
3409
3410/**
3411 * @defgroup mem_slab_apis Memory Slab APIs
3412 * @ingroup kernel_apis
3413 * @{
3414 */
3415
3416/**
Allan Stephensda827222016-11-09 14:23:58 -06003417 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003418 *
Allan Stephensda827222016-11-09 14:23:58 -06003419 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003421 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3422 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003423 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003424 *
Allan Stephensda827222016-11-09 14:23:58 -06003425 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003427 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003428 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @param name Name of the memory slab.
3431 * @param slab_block_size Size of each memory block (in bytes).
3432 * @param slab_num_blocks Number memory blocks.
3433 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003434 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003435#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3436 char __noinit __aligned(slab_align) \
3437 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3438 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003439 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003440 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003441 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003443/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003444 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003447 *
Allan Stephensda827222016-11-09 14:23:58 -06003448 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3449 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3450 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3451 * To ensure that each memory block is similarly aligned to this boundary,
3452 * @a slab_block_size must also be a multiple of N.
3453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003454 * @param slab Address of the memory slab.
3455 * @param buffer Pointer to buffer used for the memory blocks.
3456 * @param block_size Size of each memory block (in bytes).
3457 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003458 *
3459 * @return N/A
3460 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003461extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003462 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003463
3464/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003465 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003467 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @param slab Address of the memory slab.
3470 * @param mem Pointer to block address area.
3471 * @param timeout Maximum time to wait for operation to complete
3472 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3473 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003474 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003475 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003477 * @retval -ENOMEM Returned without waiting.
3478 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003479 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003480extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003481 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003482
3483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * This routine releases a previously allocated memory block back to its
3487 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * @param slab Address of the memory slab.
3490 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003491 *
3492 * @return N/A
3493 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003494extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * This routine gets the number of memory blocks that are currently
3500 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003505 */
Kumar Galacc334c72017-04-21 10:55:34 -05003506static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003507{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003508 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003509}
3510
Peter Mitsisc001aa82016-10-13 13:53:37 -04003511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * This routine gets the number of memory blocks that are currently
3515 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003517 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003520 */
Kumar Galacc334c72017-04-21 10:55:34 -05003521static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003522{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003523 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003524}
3525
Allan Stephensc98da842016-11-11 15:45:03 -05003526/**
3527 * @} end defgroup mem_slab_apis
3528 */
3529
3530/**
3531 * @cond INTERNAL_HIDDEN
3532 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003533
Andy Ross73cb9582017-05-09 10:42:39 -07003534struct k_mem_pool_lvl {
3535 union {
3536 u32_t *bits_p;
3537 u32_t bits;
3538 };
3539 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003540};
3541
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003542struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003543 void *buf;
3544 size_t max_sz;
3545 u16_t n_max;
3546 u8_t n_levels;
3547 u8_t max_inline_level;
3548 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003549 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003550};
3551
Andy Ross73cb9582017-05-09 10:42:39 -07003552#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003553
Andy Ross73cb9582017-05-09 10:42:39 -07003554#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3555
Andy Ross8cf7ff52017-11-13 14:59:13 -08003556#define __MPOOL_LVLS(maxsz, minsz) \
3557 (_MPOOL_HAVE_LVL((maxsz), (minsz), 0) + \
3558 _MPOOL_HAVE_LVL((maxsz), (minsz), 1) + \
3559 _MPOOL_HAVE_LVL((maxsz), (minsz), 2) + \
3560 _MPOOL_HAVE_LVL((maxsz), (minsz), 3) + \
3561 _MPOOL_HAVE_LVL((maxsz), (minsz), 4) + \
3562 _MPOOL_HAVE_LVL((maxsz), (minsz), 5) + \
3563 _MPOOL_HAVE_LVL((maxsz), (minsz), 6) + \
3564 _MPOOL_HAVE_LVL((maxsz), (minsz), 7) + \
3565 _MPOOL_HAVE_LVL((maxsz), (minsz), 8) + \
3566 _MPOOL_HAVE_LVL((maxsz), (minsz), 9) + \
3567 _MPOOL_HAVE_LVL((maxsz), (minsz), 10) + \
3568 _MPOOL_HAVE_LVL((maxsz), (minsz), 11) + \
3569 _MPOOL_HAVE_LVL((maxsz), (minsz), 12) + \
3570 _MPOOL_HAVE_LVL((maxsz), (minsz), 13) + \
3571 _MPOOL_HAVE_LVL((maxsz), (minsz), 14) + \
3572 _MPOOL_HAVE_LVL((maxsz), (minsz), 15))
3573
3574#define _MPOOL_MINBLK sizeof(sys_dnode_t)
3575
3576#define _MPOOL_LVLS(max, min) \
3577 __MPOOL_LVLS((max), (min) >= _MPOOL_MINBLK ? (min) : _MPOOL_MINBLK)
Andy Ross73cb9582017-05-09 10:42:39 -07003578
3579/* Rounds the needed bits up to integer multiples of u32_t */
3580#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3581 ((((n_max) << (2*(l))) + 31) / 32)
3582
3583/* One word gets stored free unioned with the pointer, otherwise the
3584 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003585 */
Andy Ross73cb9582017-05-09 10:42:39 -07003586#define _MPOOL_LBIT_WORDS(n_max, l) \
3587 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3588 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003589
Andy Ross73cb9582017-05-09 10:42:39 -07003590/* How many bytes for the bitfields of a single level? */
3591#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3592 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3593 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003594
Andy Ross73cb9582017-05-09 10:42:39 -07003595/* Size of the bitmap array that follows the buffer in allocated memory */
3596#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3597 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3598 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3599 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3600 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3601 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3602 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3603 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3604 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3605 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3606 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3607 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3608 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3609 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3610 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3611 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3612 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003613
3614/**
Allan Stephensc98da842016-11-11 15:45:03 -05003615 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003616 */
3617
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003618/**
Allan Stephensc98da842016-11-11 15:45:03 -05003619 * @addtogroup mem_pool_apis
3620 * @{
3621 */
3622
3623/**
3624 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3627 * long. The memory pool allows blocks to be repeatedly partitioned into
3628 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003629 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003630 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003631 * If the pool is to be accessed outside the module where it is defined, it
3632 * can be declared via
3633 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003634 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003637 * @param minsz Size of the smallest blocks in the pool (in bytes).
3638 * @param maxsz Size of the largest blocks in the pool (in bytes).
3639 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003641 */
Andy Ross73cb9582017-05-09 10:42:39 -07003642#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3643 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3644 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3645 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3646 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3647 .buf = _mpool_buf_##name, \
3648 .max_sz = maxsz, \
3649 .n_max = nmax, \
3650 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3651 .levels = _mpool_lvls_##name, \
3652 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003653
Peter Mitsis937042c2016-10-13 13:18:26 -04003654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003659 * @param pool Address of the memory pool.
3660 * @param block Pointer to block descriptor for the allocated memory.
3661 * @param size Amount of memory to allocate (in bytes).
3662 * @param timeout Maximum time to wait for operation to complete
3663 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3664 * or K_FOREVER to wait as long as necessary.
3665 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003666 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003668 * @retval -ENOMEM Returned without waiting.
3669 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003670 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003671extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003672 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003673
3674/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003675 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * This routine releases a previously allocated memory block back to its
3678 * memory pool.
3679 *
3680 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003681 *
3682 * @return N/A
3683 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003685
3686/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003687 * @brief Free memory allocated from a memory pool.
3688 *
3689 * This routine releases a previously allocated memory block back to its
3690 * memory pool
3691 *
3692 * @param id Memory block identifier.
3693 *
3694 * @return N/A
3695 */
3696extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3697
3698/**
Allan Stephensc98da842016-11-11 15:45:03 -05003699 * @} end addtogroup mem_pool_apis
3700 */
3701
3702/**
3703 * @defgroup heap_apis Heap Memory Pool APIs
3704 * @ingroup kernel_apis
3705 * @{
3706 */
3707
3708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003709 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003711 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003712 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003714 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003717 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003718extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003719
3720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003722 *
3723 * This routine provides traditional free() semantics. The memory being
3724 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003725 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003726 * If @a ptr is NULL, no operation is performed.
3727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003728 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003729 *
3730 * @return N/A
3731 */
3732extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003733
Allan Stephensc98da842016-11-11 15:45:03 -05003734/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003735 * @brief Allocate memory from heap, array style
3736 *
3737 * This routine provides traditional calloc() semantics. Memory is
3738 * allocated from the heap memory pool and zeroed.
3739 *
3740 * @param nmemb Number of elements in the requested array
3741 * @param size Size of each array element (in bytes).
3742 *
3743 * @return Address of the allocated memory if successful; otherwise NULL.
3744 */
3745extern void *k_calloc(size_t nmemb, size_t size);
3746
3747/**
Allan Stephensc98da842016-11-11 15:45:03 -05003748 * @} end defgroup heap_apis
3749 */
3750
Benjamin Walshacc68c12017-01-29 18:57:45 -05003751/* polling API - PRIVATE */
3752
Benjamin Walshb0179862017-02-02 16:39:57 -05003753#ifdef CONFIG_POLL
3754#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3755#else
3756#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3757#endif
3758
Benjamin Walshacc68c12017-01-29 18:57:45 -05003759/* private - implementation data created as needed, per-type */
3760struct _poller {
3761 struct k_thread *thread;
3762};
3763
3764/* private - types bit positions */
3765enum _poll_types_bits {
3766 /* can be used to ignore an event */
3767 _POLL_TYPE_IGNORE,
3768
3769 /* to be signaled by k_poll_signal() */
3770 _POLL_TYPE_SIGNAL,
3771
3772 /* semaphore availability */
3773 _POLL_TYPE_SEM_AVAILABLE,
3774
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003775 /* queue/fifo/lifo data availability */
3776 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003777
3778 _POLL_NUM_TYPES
3779};
3780
3781#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3782
3783/* private - states bit positions */
3784enum _poll_states_bits {
3785 /* default state when creating event */
3786 _POLL_STATE_NOT_READY,
3787
Benjamin Walshacc68c12017-01-29 18:57:45 -05003788 /* signaled by k_poll_signal() */
3789 _POLL_STATE_SIGNALED,
3790
3791 /* semaphore is available */
3792 _POLL_STATE_SEM_AVAILABLE,
3793
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003794 /* data is available to read on queue/fifo/lifo */
3795 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796
3797 _POLL_NUM_STATES
3798};
3799
3800#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3801
3802#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003803 (32 - (0 \
3804 + 8 /* tag */ \
3805 + _POLL_NUM_TYPES \
3806 + _POLL_NUM_STATES \
3807 + 1 /* modes */ \
3808 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003809
Benjamin Walshacc68c12017-01-29 18:57:45 -05003810/* end of polling API - PRIVATE */
3811
3812
3813/**
3814 * @defgroup poll_apis Async polling APIs
3815 * @ingroup kernel_apis
3816 * @{
3817 */
3818
3819/* Public polling API */
3820
3821/* public - values for k_poll_event.type bitfield */
3822#define K_POLL_TYPE_IGNORE 0
3823#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3824#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003825#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3826#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003827
3828/* public - polling modes */
3829enum k_poll_modes {
3830 /* polling thread does not take ownership of objects when available */
3831 K_POLL_MODE_NOTIFY_ONLY = 0,
3832
3833 K_POLL_NUM_MODES
3834};
3835
3836/* public - values for k_poll_event.state bitfield */
3837#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3839#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003840#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3841#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003842
3843/* public - poll signal object */
3844struct k_poll_signal {
3845 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003846 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847
3848 /*
3849 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3850 * user resets it to 0.
3851 */
3852 unsigned int signaled;
3853
3854 /* custom result value passed to k_poll_signal() if needed */
3855 int result;
3856};
3857
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003858#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003859 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003860 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003861 .signaled = 0, \
3862 .result = 0, \
3863 }
3864
3865struct k_poll_event {
3866 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003867 sys_dnode_t _node;
3868
3869 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003870 struct _poller *poller;
3871
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003872 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003873 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003874
Benjamin Walshacc68c12017-01-29 18:57:45 -05003875 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003876 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003877
3878 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003879 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003880
3881 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003882 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003883
3884 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003885 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003886
3887 /* per-type data */
3888 union {
3889 void *obj;
3890 struct k_poll_signal *signal;
3891 struct k_sem *sem;
3892 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003893 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894 };
3895};
3896
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003897#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003898 { \
3899 .poller = NULL, \
3900 .type = event_type, \
3901 .state = K_POLL_STATE_NOT_READY, \
3902 .mode = event_mode, \
3903 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003904 { .obj = event_obj }, \
3905 }
3906
3907#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3908 event_tag) \
3909 { \
3910 .type = event_type, \
3911 .tag = event_tag, \
3912 .state = K_POLL_STATE_NOT_READY, \
3913 .mode = event_mode, \
3914 .unused = 0, \
3915 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003916 }
3917
3918/**
3919 * @brief Initialize one struct k_poll_event instance
3920 *
3921 * After this routine is called on a poll event, the event it ready to be
3922 * placed in an event array to be passed to k_poll().
3923 *
3924 * @param event The event to initialize.
3925 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3926 * values. Only values that apply to the same object being polled
3927 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3928 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003929 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003930 * @param obj Kernel object or poll signal.
3931 *
3932 * @return N/A
3933 */
3934
Kumar Galacc334c72017-04-21 10:55:34 -05003935extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003936 int mode, void *obj);
3937
3938/**
3939 * @brief Wait for one or many of multiple poll events to occur
3940 *
3941 * This routine allows a thread to wait concurrently for one or many of
3942 * multiple poll events to have occurred. Such events can be a kernel object
3943 * being available, like a semaphore, or a poll signal event.
3944 *
3945 * When an event notifies that a kernel object is available, the kernel object
3946 * is not "given" to the thread calling k_poll(): it merely signals the fact
3947 * that the object was available when the k_poll() call was in effect. Also,
3948 * all threads trying to acquire an object the regular way, i.e. by pending on
3949 * the object, have precedence over the thread polling on the object. This
3950 * means that the polling thread will never get the poll event on an object
3951 * until the object becomes available and its pend queue is empty. For this
3952 * reason, the k_poll() call is more effective when the objects being polled
3953 * only have one thread, the polling thread, trying to acquire them.
3954 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003955 * When k_poll() returns 0, the caller should loop on all the events that were
3956 * passed to k_poll() and check the state field for the values that were
3957 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003958 *
3959 * Before being reused for another call to k_poll(), the user has to reset the
3960 * state field to K_POLL_STATE_NOT_READY.
3961 *
3962 * @param events An array of pointers to events to be polled for.
3963 * @param num_events The number of events in the array.
3964 * @param timeout Waiting period for an event to be ready (in milliseconds),
3965 * or one of the special values K_NO_WAIT and K_FOREVER.
3966 *
3967 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003968 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003969 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003970 */
3971
3972extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003973 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003974
3975/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003976 * @brief Initialize a poll signal object.
3977 *
3978 * Ready a poll signal object to be signaled via k_poll_signal().
3979 *
3980 * @param signal A poll signal.
3981 *
3982 * @return N/A
3983 */
3984
3985extern void k_poll_signal_init(struct k_poll_signal *signal);
3986
3987/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003988 * @brief Signal a poll signal object.
3989 *
3990 * This routine makes ready a poll signal, which is basically a poll event of
3991 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3992 * made ready to run. A @a result value can be specified.
3993 *
3994 * The poll signal contains a 'signaled' field that, when set by
3995 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3996 * be reset by the user before being passed again to k_poll() or k_poll() will
3997 * consider it being signaled, and will return immediately.
3998 *
3999 * @param signal A poll signal.
4000 * @param result The value to store in the result field of the signal.
4001 *
4002 * @retval 0 The signal was delivered successfully.
4003 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4004 */
4005
4006extern int k_poll_signal(struct k_poll_signal *signal, int result);
4007
4008/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004009extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004010
4011/**
4012 * @} end defgroup poll_apis
4013 */
4014
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004015/**
4016 * @brief Make the CPU idle.
4017 *
4018 * This function makes the CPU idle until an event wakes it up.
4019 *
4020 * In a regular system, the idle thread should be the only thread responsible
4021 * for making the CPU idle and triggering any type of power management.
4022 * However, in some more constrained systems, such as a single-threaded system,
4023 * the only thread would be responsible for this if needed.
4024 *
4025 * @return N/A
4026 */
4027extern void k_cpu_idle(void);
4028
4029/**
4030 * @brief Make the CPU idle in an atomic fashion.
4031 *
4032 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4033 * must be done atomically before making the CPU idle.
4034 *
4035 * @param key Interrupt locking key obtained from irq_lock().
4036 *
4037 * @return N/A
4038 */
4039extern void k_cpu_atomic_idle(unsigned int key);
4040
Kumar Galacc334c72017-04-21 10:55:34 -05004041extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004042
Andrew Boiecdb94d62017-04-18 15:22:05 -07004043#ifdef _ARCH_EXCEPT
4044/* This archtecture has direct support for triggering a CPU exception */
4045#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4046#else
4047
Andrew Boiecdb94d62017-04-18 15:22:05 -07004048/* NOTE: This is the implementation for arches that do not implement
4049 * _ARCH_EXCEPT() to generate a real CPU exception.
4050 *
4051 * We won't have a real exception frame to determine the PC value when
4052 * the oops occurred, so print file and line number before we jump into
4053 * the fatal error handler.
4054 */
4055#define _k_except_reason(reason) do { \
4056 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4057 _NanoFatalErrorHandler(reason, &_default_esf); \
4058 CODE_UNREACHABLE; \
4059 } while (0)
4060
4061#endif /* _ARCH__EXCEPT */
4062
4063/**
4064 * @brief Fatally terminate a thread
4065 *
4066 * This should be called when a thread has encountered an unrecoverable
4067 * runtime condition and needs to terminate. What this ultimately
4068 * means is determined by the _fatal_error_handler() implementation, which
4069 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4070 *
4071 * If this is called from ISR context, the default system fatal error handler
4072 * will treat it as an unrecoverable system error, just like k_panic().
4073 */
4074#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4075
4076/**
4077 * @brief Fatally terminate the system
4078 *
4079 * This should be called when the Zephyr kernel has encountered an
4080 * unrecoverable runtime condition and needs to terminate. What this ultimately
4081 * means is determined by the _fatal_error_handler() implementation, which
4082 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4083 */
4084#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4085
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004086/*
4087 * private APIs that are utilized by one or more public APIs
4088 */
4089
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004090#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004091extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004092#else
4093#define _init_static_threads() do { } while ((0))
4094#endif
4095
4096extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004097extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004098
Andrew Boiedc5d9352017-06-02 12:56:47 -07004099/* arch/cpu.h may declare an architecture or platform-specific macro
4100 * for properly declaring stacks, compatible with MMU/MPU constraints if
4101 * enabled
4102 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004103
4104/**
4105 * @brief Obtain an extern reference to a stack
4106 *
4107 * This macro properly brings the symbol of a thread stack declared
4108 * elsewhere into scope.
4109 *
4110 * @param sym Thread stack symbol name
4111 */
4112#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4113
Andrew Boiedc5d9352017-06-02 12:56:47 -07004114#ifdef _ARCH_THREAD_STACK_DEFINE
4115#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4116#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4117 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4118#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4119#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004120static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004121{
4122 return _ARCH_THREAD_STACK_BUFFER(sym);
4123}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004124#else
4125/**
4126 * @brief Declare a toplevel thread stack memory region
4127 *
4128 * This declares a region of memory suitable for use as a thread's stack.
4129 *
4130 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4131 * 'noinit' section so that it isn't zeroed at boot
4132 *
Andrew Boie507852a2017-07-25 18:47:07 -07004133 * The declared symbol will always be a k_thread_stack_t which can be passed to
4134 * k_thread_create, but should otherwise not be manipulated. If the buffer
4135 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004136 *
4137 * It is legal to precede this definition with the 'static' keyword.
4138 *
4139 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4140 * parameter of k_thread_create(), it may not be the same as the
4141 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4142 *
4143 * @param sym Thread stack symbol name
4144 * @param size Size of the stack memory region
4145 */
4146#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004147 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004148
4149/**
4150 * @brief Declare a toplevel array of thread stack memory regions
4151 *
4152 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4153 * definition for additional details and constraints.
4154 *
4155 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4156 * 'noinit' section so that it isn't zeroed at boot
4157 *
4158 * @param sym Thread stack symbol name
4159 * @param nmemb Number of stacks to declare
4160 * @param size Size of the stack memory region
4161 */
4162
4163#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004164 struct _k_thread_stack_element __noinit \
4165 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004166
4167/**
4168 * @brief Declare an embedded stack memory region
4169 *
4170 * Used for stacks embedded within other data structures. Use is highly
4171 * discouraged but in some cases necessary. For memory protection scenarios,
4172 * it is very important that any RAM preceding this member not be writable
4173 * by threads else a stack overflow will lead to silent corruption. In other
4174 * words, the containing data structure should live in RAM owned by the kernel.
4175 *
4176 * @param sym Thread stack symbol name
4177 * @param size Size of the stack memory region
4178 */
4179#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004180 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004181
4182/**
4183 * @brief Return the size in bytes of a stack memory region
4184 *
4185 * Convenience macro for passing the desired stack size to k_thread_create()
4186 * since the underlying implementation may actually create something larger
4187 * (for instance a guard area).
4188 *
4189 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004190 * passed to K_THREAD_STACK_DEFINE.
4191 *
4192 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4193 * it is not guaranteed to return the original value since each array
4194 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004195 *
4196 * @param sym Stack memory symbol
4197 * @return Size of the stack
4198 */
4199#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4200
4201/**
4202 * @brief Get a pointer to the physical stack buffer
4203 *
4204 * Convenience macro to get at the real underlying stack buffer used by
4205 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4206 * This is really only intended for diagnostic tools which want to examine
4207 * stack memory contents.
4208 *
4209 * @param sym Declared stack symbol name
4210 * @return The buffer itself, a char *
4211 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004212static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004213{
4214 return (char *)sym;
4215}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004216
4217#endif /* _ARCH_DECLARE_STACK */
4218
Chunlin Hane9c97022017-07-07 20:29:30 +08004219/**
4220 * @defgroup mem_domain_apis Memory domain APIs
4221 * @ingroup kernel_apis
4222 * @{
4223 */
4224
4225/** @def MEM_PARTITION_ENTRY
4226 * @brief Used to declare a memory partition entry
4227 */
4228#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4229 {\
4230 .start = _start, \
4231 .size = _size, \
4232 .attr = _attr, \
4233 }
4234
4235/** @def K_MEM_PARTITION_DEFINE
4236 * @brief Used to declare a memory partition
4237 */
4238#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4239#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4240 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4241 struct k_mem_partition name = \
4242 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4243#else
4244#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4245 struct k_mem_partition name = \
4246 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4247#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4248
Chunlin Hane9c97022017-07-07 20:29:30 +08004249/* memory partition */
4250struct k_mem_partition {
4251 /* start address of memory partition */
4252 u32_t start;
4253 /* size of memory partition */
4254 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304255#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004256 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304257 k_mem_partition_attr_t attr;
4258#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004259};
4260
Chunlin Hane9c97022017-07-07 20:29:30 +08004261/* memory domian */
4262struct k_mem_domain {
4263 /* number of partitions in the domain */
4264 u32_t num_partitions;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304265#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004266 /* partitions in the domain */
4267 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304268#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004269 /* domain q */
4270 sys_dlist_t mem_domain_q;
4271};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304272
Chunlin Hane9c97022017-07-07 20:29:30 +08004273
4274/**
4275 * @brief Initialize a memory domain.
4276 *
4277 * Initialize a memory domain with given name and memory partitions.
4278 *
4279 * @param domain The memory domain to be initialized.
4280 * @param num_parts The number of array items of "parts" parameter.
4281 * @param parts An array of pointers to the memory partitions. Can be NULL
4282 * if num_parts is zero.
4283 */
4284
4285extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4286 struct k_mem_partition *parts[]);
4287/**
4288 * @brief Destroy a memory domain.
4289 *
4290 * Destroy a memory domain.
4291 *
4292 * @param domain The memory domain to be destroyed.
4293 */
4294
4295extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4296
4297/**
4298 * @brief Add a memory partition into a memory domain.
4299 *
4300 * Add a memory partition into a memory domain.
4301 *
4302 * @param domain The memory domain to be added a memory partition.
4303 * @param part The memory partition to be added
4304 */
4305
4306extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4307 struct k_mem_partition *part);
4308
4309/**
4310 * @brief Remove a memory partition from a memory domain.
4311 *
4312 * Remove a memory partition from a memory domain.
4313 *
4314 * @param domain The memory domain to be removed a memory partition.
4315 * @param part The memory partition to be removed
4316 */
4317
4318extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4319 struct k_mem_partition *part);
4320
4321/**
4322 * @brief Add a thread into a memory domain.
4323 *
4324 * Add a thread into a memory domain.
4325 *
4326 * @param domain The memory domain that the thread is going to be added into.
4327 * @param thread ID of thread going to be added into the memory domain.
4328 */
4329
4330extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4331 k_tid_t thread);
4332
4333/**
4334 * @brief Remove a thread from its memory domain.
4335 *
4336 * Remove a thread from its memory domain.
4337 *
4338 * @param thread ID of thread going to be removed from its memory domain.
4339 */
4340
4341extern void k_mem_domain_remove_thread(k_tid_t thread);
4342
4343/**
4344 * @} end defgroup mem_domain_apis
4345 */
4346
Andrew Boie756f9072017-10-10 16:01:49 -07004347/**
4348 * @brief Emit a character buffer to the console device
4349 *
4350 * @param c String of characters to print
4351 * @param n The length of the string
4352 */
4353__syscall void k_str_out(char *c, size_t n);
4354
Andy Rosse7172672018-01-24 15:48:32 -08004355/**
4356 * @brief Start a numbered CPU on a MP-capable system
4357
4358 * This starts and initializes a specific CPU. The main thread on
4359 * startup is running on CPU zero, other processors are numbered
4360 * sequentially. On return from this function, the CPU is known to
4361 * have begun operating and will enter the provided function. Its
4362 * interrupts will be initialied but disabled such that irq_unlock()
4363 * with the provided key will work to enable them.
4364 *
4365 * Normally, in SMP mode this function will be called by the kernel
4366 * initialization and should not be used as a user API. But it is
4367 * defined here for special-purpose apps which want Zephyr running on
4368 * one core and to use others for design-specific processing.
4369 *
4370 * @param cpu_num Integer number of the CPU
4371 * @param stack Stack memory for the CPU
4372 * @param sz Stack buffer size, in bytes
4373 * @param fn Function to begin running on the CPU. First argument is
4374 * an irq_unlock() key.
4375 * @param arg Untyped argument to be passed to "fn"
4376 */
4377extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4378 void (*fn)(int, void *), void *arg);
4379
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004380#ifdef __cplusplus
4381}
4382#endif
4383
Andrew Boiee004dec2016-11-07 09:01:19 -08004384#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4385/*
4386 * Define new and delete operators.
4387 * At this moment, the operators do nothing since objects are supposed
4388 * to be statically allocated.
4389 */
4390inline void operator delete(void *ptr)
4391{
4392 (void)ptr;
4393}
4394
4395inline void operator delete[](void *ptr)
4396{
4397 (void)ptr;
4398}
4399
4400inline void *operator new(size_t size)
4401{
4402 (void)size;
4403 return NULL;
4404}
4405
4406inline void *operator new[](size_t size)
4407{
4408 (void)size;
4409 return NULL;
4410}
4411
4412/* Placement versions of operator new and delete */
4413inline void operator delete(void *ptr1, void *ptr2)
4414{
4415 (void)ptr1;
4416 (void)ptr2;
4417}
4418
4419inline void operator delete[](void *ptr1, void *ptr2)
4420{
4421 (void)ptr1;
4422 (void)ptr2;
4423}
4424
4425inline void *operator new(size_t size, void *ptr)
4426{
4427 (void)size;
4428 return ptr;
4429}
4430
4431inline void *operator new[](size_t size, void *ptr)
4432{
4433 (void)size;
4434 return ptr;
4435}
4436
4437#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4438
Andrew Boiefa94ee72017-09-28 16:54:35 -07004439#include <syscalls/kernel.h>
4440
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004441#endif /* !_ASMLANGUAGE */
4442
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004443#endif /* _kernel__h_ */