blob: 0f969820b80d00fceb7b05a0ab1cf07c1ea7ba1a [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>
18#include <stdint.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
21#include <sections.h>
22#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Anas Nashifbbb157d2017-01-15 08:46:31 -050035/**
36 * @brief Kernel APIs
37 * @defgroup kernel_apis Kernel APIs
38 * @{
39 * @}
40 */
41
Anas Nashif61f4b242016-11-18 10:53:59 -050042#ifdef CONFIG_KERNEL_DEBUG
43#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040044#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
45#else
46#define K_DEBUG(fmt, ...)
47#endif
48
Benjamin Walsh2f280412017-01-14 19:23:46 -050049#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
50#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
51#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
52#elif defined(CONFIG_COOP_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
54#define _NUM_PREEMPT_PRIO (0)
55#elif defined(CONFIG_PREEMPT_ENABLED)
56#define _NUM_COOP_PRIO (0)
57#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
58#else
59#error "invalid configuration"
60#endif
61
62#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_PRIO_PREEMPT(x) (x)
64
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_ANY NULL
66#define K_END NULL
67
Benjamin Walshedb35702017-01-14 18:47:22 -050068#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050070#elif defined(CONFIG_COOP_ENABLED)
71#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
72#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050074#else
75#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#endif
77
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050078#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
80#else
81#define K_LOWEST_THREAD_PRIO -1
82#endif
83
Benjamin Walshfab8d922016-11-08 15:36:36 -050084#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
85
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
87#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
88
89typedef sys_dlist_t _wait_q_t;
90
Anas Nashif2f203c22016-12-18 06:57:45 -050091#ifdef CONFIG_OBJECT_TRACING
92#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
93#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094#else
Anas Nashif2f203c22016-12-18 06:57:45 -050095#define _OBJECT_TRACING_INIT
96#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#endif
98
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050099#define tcs k_thread
100struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101struct k_mutex;
102struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400103struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104struct k_msgq;
105struct k_mbox;
106struct k_pipe;
107struct k_fifo;
108struct k_lifo;
109struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400110struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mem_pool;
112struct k_timer;
113
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400114typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116enum execution_context_types {
117 K_ISR = 0,
118 K_COOP_THREAD,
119 K_PREEMPT_THREAD,
120};
121
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400122/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100123 * @defgroup profiling_apis Profiling APIs
124 * @ingroup kernel_apis
125 * @{
126 */
127
128/**
129 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
130 *
131 * This routine calls @ref stack_analyze on the 4 call stacks declared and
132 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
133 *
134 * CONFIG_MAIN_STACK_SIZE
135 * CONFIG_IDLE_STACK_SIZE
136 * CONFIG_ISR_STACK_SIZE
137 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
138 *
139 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
140 * produce output.
141 *
142 * @return N/A
143 */
144extern void k_call_stacks_analyze(void);
145
146/**
147 * @} end defgroup profiling_apis
148 */
149
150/**
Allan Stephensc98da842016-11-11 15:45:03 -0500151 * @defgroup thread_apis Thread APIs
152 * @ingroup kernel_apis
153 * @{
154 */
155
156/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500157 * @typedef k_thread_entry_t
158 * @brief Thread entry point function type.
159 *
160 * A thread's entry point function is invoked when the thread starts executing.
161 * Up to 3 argument values can be passed to the function.
162 *
163 * The thread terminates execution permanently if the entry point function
164 * returns. The thread is responsible for releasing any shared resources
165 * it may own (such as mutexes and dynamically allocated memory), prior to
166 * returning.
167 *
168 * @param p1 First argument.
169 * @param p2 Second argument.
170 * @param p3 Third argument.
171 *
172 * @return N/A
173 */
174typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
175
Benjamin Walshed240f22017-01-22 13:05:08 -0500176#endif /* !_ASMLANGUAGE */
177
178
179/*
180 * Thread user options. May be needed by assembly code. Common part uses low
181 * bits, arch-specific use high bits.
182 */
183
184/* system thread that must not abort */
185#define K_ESSENTIAL (1 << 0)
186
187#if defined(CONFIG_FP_SHARING)
188/* thread uses floating point registers */
189#define K_FP_REGS (1 << 1)
190#endif
191
192#ifdef CONFIG_X86
193/* x86 Bitmask definitions for threads user options */
194
195#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
196/* thread uses SSEx (and also FP) registers */
197#define K_SSE_REGS (1 << 7)
198#endif
199#endif
200
201/* end - thread options */
202
203#if !defined(_ASMLANGUAGE)
204
Allan Stephens5eceb852016-11-16 10:16:30 -0500205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500206 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500208 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * The new thread may be scheduled for immediate execution or a delayed start.
211 * If the newly spawned thread does not have a delayed start the kernel
212 * scheduler may preempt the current thread to allow the new thread to
213 * execute.
214 *
215 * Thread options are architecture-specific, and can include K_ESSENTIAL,
216 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
217 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
219 * @param stack Pointer to the stack space.
220 * @param stack_size Stack size in bytes.
221 * @param entry Thread entry function.
222 * @param p1 1st entry point parameter.
223 * @param p2 2nd entry point parameter.
224 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500225 * @param prio Thread priority.
226 * @param options Thread options.
227 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500231extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500232 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400233 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500234 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400235
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
Allan Stephensc98da842016-11-11 15:45:03 -0500239 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500240 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500242 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400243 *
244 * @return N/A
245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400247
248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400250 *
251 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500252 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400254 * @return N/A
255 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400256extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400257
258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500259 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500261 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400262 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500263 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 *
265 * @return N/A
266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400267extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400268
269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500270 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500272 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500274 * If @a thread is not currently sleeping, the routine has no effect.
275 *
276 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400277 *
278 * @return N/A
279 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400280extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400281
282/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500283 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500285 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400288
289/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500290 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500292 * This routine prevents @a thread from executing if it has not yet started
293 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500295 * @param thread ID of thread to cancel.
296 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500297 * @retval 0 Thread spawning cancelled.
298 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400300extern int k_thread_cancel(k_tid_t thread);
301
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400302/**
Allan Stephensc98da842016-11-11 15:45:03 -0500303 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500305 * This routine permanently stops execution of @a thread. The thread is taken
306 * off all kernel queues it is part of (i.e. the ready queue, the timeout
307 * queue, or a kernel object wait queue). However, any kernel resources the
308 * thread might currently own (such as mutexes or memory blocks) are not
309 * released. It is the responsibility of the caller of this routine to ensure
310 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500312 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400313 *
314 * @return N/A
315 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400316extern void k_thread_abort(k_tid_t thread);
317
Allan Stephensc98da842016-11-11 15:45:03 -0500318/**
319 * @cond INTERNAL_HIDDEN
320 */
321
Benjamin Walshd211a522016-12-06 11:44:01 -0500322/* timeout has timed out and is not on _timeout_q anymore */
323#define _EXPIRED (-2)
324
325/* timeout is not in use */
326#define _INACTIVE (-1)
327
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400328#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400329#define _THREAD_TIMEOUT_INIT(obj) \
330 (obj).nano_timeout = { \
331 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400332 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400333 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500334 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400335 },
336#else
337#define _THREAD_TIMEOUT_INIT(obj)
338#endif
339
340#ifdef CONFIG_ERRNO
341#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
342#else
343#define _THREAD_ERRNO_INIT(obj)
344#endif
345
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400346struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400347 union {
348 char *init_stack;
349 struct k_thread *thread;
350 };
351 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500352 void (*init_entry)(void *, void *, void *);
353 void *init_p1;
354 void *init_p2;
355 void *init_p3;
356 int init_prio;
357 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400358 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500359 void (*init_abort)(void);
360 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400361};
362
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400363#define _THREAD_INITIALIZER(stack, stack_size, \
364 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500365 prio, options, delay, abort, groups) \
366 { \
367 .init_stack = (stack), \
368 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400369 .init_entry = (void (*)(void *, void *, void *))entry, \
370 .init_p1 = (void *)p1, \
371 .init_p2 = (void *)p2, \
372 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500373 .init_prio = (prio), \
374 .init_options = (options), \
375 .init_delay = (delay), \
376 .init_abort = (abort), \
377 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378 }
379
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400380/**
Allan Stephensc98da842016-11-11 15:45:03 -0500381 * INTERNAL_HIDDEN @endcond
382 */
383
384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * @brief Statically define and initialize a thread.
386 *
387 * The thread may be scheduled for immediate execution or a delayed start.
388 *
389 * Thread options are architecture-specific, and can include K_ESSENTIAL,
390 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
391 * them using "|" (the logical OR operator).
392 *
393 * The ID of the thread can be accessed using:
394 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500395 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500396 *
397 * @param name Name of the thread.
398 * @param stack_size Stack size in bytes.
399 * @param entry Thread entry function.
400 * @param p1 1st entry point parameter.
401 * @param p2 2nd entry point parameter.
402 * @param p3 3rd entry point parameter.
403 * @param prio Thread priority.
404 * @param options Thread options.
405 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400406 *
407 * @internal It has been observed that the x86 compiler by default aligns
408 * these _static_thread_data structures to 32-byte boundaries, thereby
409 * wasting space. To work around this, force a 4-byte alignment.
410 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500411#define K_THREAD_DEFINE(name, stack_size, \
412 entry, p1, p2, p3, \
413 prio, options, delay) \
414 char __noinit __stack _k_thread_obj_##name[stack_size]; \
415 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500416 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500417 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
418 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500419 NULL, 0); \
420 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400421
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500423 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500425 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500427 * @param thread ID of thread whose priority is needed.
428 *
429 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500431extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432
433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * Rescheduling can occur immediately depending on the priority @a thread is
439 * set to:
440 *
441 * - If its priority is raised above the priority of the caller of this
442 * function, and the caller is preemptible, @a thread will be scheduled in.
443 *
444 * - If the caller operates on itself, it lowers its priority below that of
445 * other threads in the system, and the caller is preemptible, the thread of
446 * highest priority will be scheduled in.
447 *
448 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
449 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
450 * highest priority.
451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 * @param prio New priority.
454 *
455 * @warning Changing the priority of a thread currently involved in mutex
456 * priority inheritance may result in undefined behavior.
457 *
458 * @return N/A
459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400460extern void k_thread_priority_set(k_tid_t thread, int prio);
461
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500465 * This routine prevents the kernel scheduler from making @a thread the
466 * current thread. All other internal operations on @a thread are still
467 * performed; for example, any timeout it is waiting on keeps ticking,
468 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500470 * If @a thread is already suspended, the routine has no effect.
471 *
472 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
474 * @return N/A
475 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400476extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477
478/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500481 * This routine allows the kernel scheduler to make @a thread the current
482 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * If @a thread is not currently suspended, the routine has no effect.
485 *
486 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487 *
488 * @return N/A
489 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400490extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500495 * This routine specifies how the scheduler will perform time slicing of
496 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500498 * To enable time slicing, @a slice must be non-zero. The scheduler
499 * ensures that no thread runs for more than the specified time limit
500 * before other threads of that priority are given a chance to execute.
501 * Any thread whose priority is higher than @a prio is exempted, and may
502 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500504 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 * execute. Once the scheduler selects a thread for execution, there is no
506 * minimum guaranteed time the thread will execute before threads of greater or
507 * equal priority are scheduled.
508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400510 * for execution, this routine has no effect; the thread is immediately
511 * rescheduled after the slice period expires.
512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500513 * To disable timeslicing, set both @a slice and @a prio to zero.
514 *
515 * @param slice Maximum time slice length (in milliseconds).
516 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517 *
518 * @return N/A
519 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400520extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522/**
Allan Stephensc98da842016-11-11 15:45:03 -0500523 * @} end defgroup thread_apis
524 */
525
526/**
527 * @addtogroup isr_apis
528 * @{
529 */
530
531/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500532 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400533 *
Allan Stephensc98da842016-11-11 15:45:03 -0500534 * This routine allows the caller to customize its actions, depending on
535 * whether it is a thread or an ISR.
536 *
537 * @note Can be called by ISRs.
538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500539 * @return 0 if invoked by a thread.
540 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500542extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400543
Benjamin Walsh445830d2016-11-10 15:54:27 -0500544/**
545 * @brief Determine if code is running in a preemptible thread.
546 *
Allan Stephensc98da842016-11-11 15:45:03 -0500547 * This routine allows the caller to customize its actions, depending on
548 * whether it can be preempted by another thread. The routine returns a 'true'
549 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500550 *
Allan Stephensc98da842016-11-11 15:45:03 -0500551 * - The code is running in a thread, not at ISR.
552 * - The thread's priority is in the preemptible range.
553 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500554 *
Allan Stephensc98da842016-11-11 15:45:03 -0500555 * @note Can be called by ISRs.
556 *
557 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500558 * @return Non-zero if invoked by a preemptible thread.
559 */
560extern int k_is_preempt_thread(void);
561
Allan Stephensc98da842016-11-11 15:45:03 -0500562/**
563 * @} end addtogroup isr_apis
564 */
565
566/**
567 * @addtogroup thread_apis
568 * @{
569 */
570
571/**
572 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500573 *
Allan Stephensc98da842016-11-11 15:45:03 -0500574 * This routine prevents the current thread from being preempted by another
575 * thread by instructing the scheduler to treat it as a cooperative thread.
576 * If the thread subsequently performs an operation that makes it unready,
577 * it will be context switched out in the normal manner. When the thread
578 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500579 *
Allan Stephensc98da842016-11-11 15:45:03 -0500580 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500581 *
Allan Stephensc98da842016-11-11 15:45:03 -0500582 * @note k_sched_lock() and k_sched_unlock() should normally be used
583 * when the operation being performed can be safely interrupted by ISRs.
584 * However, if the amount of processing involved is very small, better
585 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500586 *
587 * @return N/A
588 */
589extern void k_sched_lock(void);
590
Allan Stephensc98da842016-11-11 15:45:03 -0500591/**
592 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500593 *
Allan Stephensc98da842016-11-11 15:45:03 -0500594 * This routine reverses the effect of a previous call to k_sched_lock().
595 * A thread must call the routine once for each time it called k_sched_lock()
596 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500597 *
598 * @return N/A
599 */
600extern void k_sched_unlock(void);
601
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500603 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500607 * Custom data is not used by the kernel itself, and is freely available
608 * for a thread to use as it sees fit. It can be used as a framework
609 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
613 * @return N/A
614 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400615extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616
617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500618 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500620 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400624extern void *k_thread_custom_data_get(void);
625
626/**
Allan Stephensc98da842016-11-11 15:45:03 -0500627 * @} end addtogroup thread_apis
628 */
629
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400630#include <sys_clock.h>
631
Allan Stephensc2f15a42016-11-17 12:24:22 -0500632/**
633 * @addtogroup clock_apis
634 * @{
635 */
636
637/**
638 * @brief Generate null timeout delay.
639 *
640 * This macro generates a timeout delay that that instructs a kernel API
641 * not to wait if the requested operation cannot be performed immediately.
642 *
643 * @return Timeout delay value.
644 */
645#define K_NO_WAIT 0
646
647/**
648 * @brief Generate timeout delay from milliseconds.
649 *
650 * This macro generates a timeout delay that that instructs a kernel API
651 * to wait up to @a ms milliseconds to perform the requested operation.
652 *
653 * @param ms Duration in milliseconds.
654 *
655 * @return Timeout delay value.
656 */
Johan Hedberg14471692016-11-13 10:52:15 +0200657#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500658
659/**
660 * @brief Generate timeout delay from seconds.
661 *
662 * This macro generates a timeout delay that that instructs a kernel API
663 * to wait up to @a s seconds to perform the requested operation.
664 *
665 * @param s Duration in seconds.
666 *
667 * @return Timeout delay value.
668 */
Johan Hedberg14471692016-11-13 10:52:15 +0200669#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500670
671/**
672 * @brief Generate timeout delay from minutes.
673 *
674 * This macro generates a timeout delay that that instructs a kernel API
675 * to wait up to @a m minutes to perform the requested operation.
676 *
677 * @param m Duration in minutes.
678 *
679 * @return Timeout delay value.
680 */
Johan Hedberg14471692016-11-13 10:52:15 +0200681#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500682
683/**
684 * @brief Generate timeout delay from hours.
685 *
686 * This macro generates a timeout delay that that instructs a kernel API
687 * to wait up to @a h hours to perform the requested operation.
688 *
689 * @param h Duration in hours.
690 *
691 * @return Timeout delay value.
692 */
Johan Hedberg14471692016-11-13 10:52:15 +0200693#define K_HOURS(h) K_MINUTES((h) * 60)
694
Allan Stephensc98da842016-11-11 15:45:03 -0500695/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500696 * @brief Generate infinite timeout delay.
697 *
698 * This macro generates a timeout delay that that instructs a kernel API
699 * to wait as long as necessary to perform the requested operation.
700 *
701 * @return Timeout delay value.
702 */
703#define K_FOREVER (-1)
704
705/**
706 * @} end addtogroup clock_apis
707 */
708
709/**
Allan Stephensc98da842016-11-11 15:45:03 -0500710 * @cond INTERNAL_HIDDEN
711 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400712
Benjamin Walsh62092182016-12-20 14:39:08 -0500713/* kernel clocks */
714
715#if (sys_clock_ticks_per_sec == 1000) || \
716 (sys_clock_ticks_per_sec == 500) || \
717 (sys_clock_ticks_per_sec == 250) || \
718 (sys_clock_ticks_per_sec == 125) || \
719 (sys_clock_ticks_per_sec == 100) || \
720 (sys_clock_ticks_per_sec == 50) || \
721 (sys_clock_ticks_per_sec == 25) || \
722 (sys_clock_ticks_per_sec == 20) || \
723 (sys_clock_ticks_per_sec == 10) || \
724 (sys_clock_ticks_per_sec == 1)
725
726 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
727#else
728 /* yields horrible 64-bit math on many architectures: try to avoid */
729 #define _NON_OPTIMIZED_TICKS_PER_SEC
730#endif
731
732#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
733extern int32_t _ms_to_ticks(int32_t ms);
734#else
735static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
736{
737 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
738}
739#endif
740
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500741/* added tick needed to account for tick in progress */
742#define _TICK_ALIGN 1
743
Benjamin Walsh62092182016-12-20 14:39:08 -0500744static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400745{
Benjamin Walsh62092182016-12-20 14:39:08 -0500746#ifdef CONFIG_SYS_CLOCK_EXISTS
747
748#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400749 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400750#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500751 return (uint64_t)ticks * _ms_per_tick;
752#endif
753
754#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400755 __ASSERT(ticks == 0, "");
756 return 0;
757#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400758}
759
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400760/* timeouts */
761
762struct _timeout;
763typedef void (*_timeout_func_t)(struct _timeout *t);
764
765struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500766 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400767 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400768 sys_dlist_t *wait_q;
769 int32_t delta_ticks_from_prev;
770 _timeout_func_t func;
771};
772
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200773extern int32_t _timeout_remaining_get(struct _timeout *timeout);
774
Allan Stephensc98da842016-11-11 15:45:03 -0500775/**
776 * INTERNAL_HIDDEN @endcond
777 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500778
Allan Stephensc98da842016-11-11 15:45:03 -0500779/**
780 * @cond INTERNAL_HIDDEN
781 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400782
783struct k_timer {
784 /*
785 * _timeout structure must be first here if we want to use
786 * dynamic timer allocation. timeout.node is used in the double-linked
787 * list of free timers
788 */
789 struct _timeout timeout;
790
Allan Stephens45bfa372016-10-12 12:39:42 -0500791 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792 _wait_q_t wait_q;
793
794 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500795 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400796
797 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500798 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400799
800 /* timer period */
801 int32_t period;
802
Allan Stephens45bfa372016-10-12 12:39:42 -0500803 /* timer status */
804 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400805
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500806 /* user-specific data, also used to support legacy features */
807 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808
Anas Nashif2f203c22016-12-18 06:57:45 -0500809 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810};
811
Allan Stephens1342adb2016-11-03 13:54:53 -0500812#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400813 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500814 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500815 .timeout.wait_q = NULL, \
816 .timeout.thread = NULL, \
817 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400818 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500819 .expiry_fn = expiry, \
820 .stop_fn = stop, \
821 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500822 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500823 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400824 }
825
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826/**
Allan Stephensc98da842016-11-11 15:45:03 -0500827 * INTERNAL_HIDDEN @endcond
828 */
829
830/**
831 * @defgroup timer_apis Timer APIs
832 * @ingroup kernel_apis
833 * @{
834 */
835
836/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500837 * @typedef k_timer_expiry_t
838 * @brief Timer expiry function type.
839 *
840 * A timer's expiry function is executed by the system clock interrupt handler
841 * each time the timer expires. The expiry function is optional, and is only
842 * invoked if the timer has been initialized with one.
843 *
844 * @param timer Address of timer.
845 *
846 * @return N/A
847 */
848typedef void (*k_timer_expiry_t)(struct k_timer *timer);
849
850/**
851 * @typedef k_timer_stop_t
852 * @brief Timer stop function type.
853 *
854 * A timer's stop function is executed if the timer is stopped prematurely.
855 * The function runs in the context of the thread that stops the timer.
856 * The stop function is optional, and is only invoked if the timer has been
857 * initialized with one.
858 *
859 * @param timer Address of timer.
860 *
861 * @return N/A
862 */
863typedef void (*k_timer_stop_t)(struct k_timer *timer);
864
865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500870 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
872 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @param expiry_fn Function to invoke each time the timer expires.
874 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500876#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500877 struct k_timer name \
878 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500879 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400880
Allan Stephens45bfa372016-10-12 12:39:42 -0500881/**
882 * @brief Initialize a timer.
883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500885 *
886 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @param expiry_fn Function to invoke each time the timer expires.
888 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500889 *
890 * @return N/A
891 */
892extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500893 k_timer_expiry_t expiry_fn,
894 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700895
Allan Stephens45bfa372016-10-12 12:39:42 -0500896/**
897 * @brief Start a timer.
898 *
899 * This routine starts a timer, and resets its status to zero. The timer
900 * begins counting down using the specified duration and period values.
901 *
902 * Attempting to start a timer that is already running is permitted.
903 * The timer's status is reset to zero and the timer begins counting down
904 * using the new duration and period values.
905 *
906 * @param timer Address of timer.
907 * @param duration Initial timer duration (in milliseconds).
908 * @param period Timer period (in milliseconds).
909 *
910 * @return N/A
911 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500913 int32_t duration, int32_t period);
914
915/**
916 * @brief Stop a timer.
917 *
918 * This routine stops a running timer prematurely. The timer's stop function,
919 * if one exists, is invoked by the caller.
920 *
921 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500923 *
924 * @param timer Address of timer.
925 *
926 * @return N/A
927 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400928extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500929
930/**
931 * @brief Read timer status.
932 *
933 * This routine reads the timer's status, which indicates the number of times
934 * it has expired since its status was last read.
935 *
936 * Calling this routine resets the timer's status to zero.
937 *
938 * @param timer Address of timer.
939 *
940 * @return Timer status.
941 */
942extern uint32_t k_timer_status_get(struct k_timer *timer);
943
944/**
945 * @brief Synchronize thread to timer expiration.
946 *
947 * This routine blocks the calling thread until the timer's status is non-zero
948 * (indicating that it has expired at least once since it was last examined)
949 * or the timer is stopped. If the timer status is already non-zero,
950 * or the timer is already stopped, the caller continues without waiting.
951 *
952 * Calling this routine resets the timer's status to zero.
953 *
954 * This routine must not be used by interrupt handlers, since they are not
955 * allowed to block.
956 *
957 * @param timer Address of timer.
958 *
959 * @return Timer status.
960 */
961extern uint32_t k_timer_status_sync(struct k_timer *timer);
962
963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500965 *
966 * This routine computes the (approximate) time remaining before a running
967 * timer next expires. If the timer is not running, it returns zero.
968 *
969 * @param timer Address of timer.
970 *
971 * @return Remaining time (in milliseconds).
972 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200973static inline int32_t k_timer_remaining_get(struct k_timer *timer)
974{
975 return _timeout_remaining_get(&timer->timeout);
976}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400977
Allan Stephensc98da842016-11-11 15:45:03 -0500978/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500979 * @brief Associate user-specific data with a timer.
980 *
981 * This routine records the @a user_data with the @a timer, to be retrieved
982 * later.
983 *
984 * It can be used e.g. in a timer handler shared across multiple subsystems to
985 * retrieve data specific to the subsystem this timer is associated with.
986 *
987 * @param timer Address of timer.
988 * @param user_data User data to associate with the timer.
989 *
990 * @return N/A
991 */
992static inline void k_timer_user_data_set(struct k_timer *timer,
993 void *user_data)
994{
995 timer->user_data = user_data;
996}
997
998/**
999 * @brief Retrieve the user-specific data from a timer.
1000 *
1001 * @param timer Address of timer.
1002 *
1003 * @return The user data.
1004 */
1005static inline void *k_timer_user_data_get(struct k_timer *timer)
1006{
1007 return timer->user_data;
1008}
1009
1010/**
Allan Stephensc98da842016-11-11 15:45:03 -05001011 * @} end defgroup timer_apis
1012 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001013
Allan Stephensc98da842016-11-11 15:45:03 -05001014/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001015 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001016 * @{
1017 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001018
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * This routine returns the elapsed time since the system booted,
1023 * in milliseconds.
1024 *
1025 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001026 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001027extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001028
1029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * This routine returns the lower 32-bits of the elapsed time since the system
1033 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * This routine can be more efficient than k_uptime_get(), as it reduces the
1036 * need for interrupt locking and 64-bit math. However, the 32-bit result
1037 * cannot hold a system uptime time larger than approximately 50 days, so the
1038 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001041 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001042extern uint32_t k_uptime_get_32(void);
1043
1044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * This routine computes the elapsed time between the current system uptime
1048 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * @param reftime Pointer to a reference time, which is updated to the current
1051 * uptime upon return.
1052 *
1053 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001056
1057/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * This routine computes the elapsed time between the current system uptime
1061 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001063 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1064 * need for interrupt locking and 64-bit math. However, the 32-bit result
1065 * cannot hold an elapsed time larger than approximately 50 days, so the
1066 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001068 * @param reftime Pointer to a reference time, which is updated to the current
1069 * uptime upon return.
1070 *
1071 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001072 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001073extern uint32_t k_uptime_delta_32(int64_t *reftime);
1074
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * This routine returns the current time, as measured by the system's hardware
1079 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001083extern uint32_t k_cycle_get_32(void);
1084
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001086 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001087 */
1088
Allan Stephensc98da842016-11-11 15:45:03 -05001089/**
1090 * @cond INTERNAL_HIDDEN
1091 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001092
1093struct k_fifo {
1094 _wait_q_t wait_q;
1095 sys_slist_t data_q;
1096
Anas Nashif2f203c22016-12-18 06:57:45 -05001097 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098};
1099
Allan Stephensc98da842016-11-11 15:45:03 -05001100#define K_FIFO_INITIALIZER(obj) \
1101 { \
1102 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1103 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001104 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001105 }
1106
1107/**
1108 * INTERNAL_HIDDEN @endcond
1109 */
1110
1111/**
1112 * @defgroup fifo_apis Fifo APIs
1113 * @ingroup kernel_apis
1114 * @{
1115 */
1116
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
1124 * @return N/A
1125 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001126extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001127
1128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * This routine adds a data item to @a fifo. A fifo data item must be
1132 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1133 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @note Can be called by ISRs.
1136 *
1137 * @param fifo Address of the fifo.
1138 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
1140 * @return N/A
1141 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143
1144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001147 * This routine adds a list of data items to @a fifo in one operation.
1148 * The data items must be in a singly-linked list, with the first 32 bits
1149 * each data item pointing to the next data item; the list must be
1150 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @param fifo Address of the fifo.
1155 * @param head Pointer to first node in singly-linked list.
1156 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
1158 * @return N/A
1159 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161
1162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * This routine adds a list of data items to @a fifo in one operation.
1166 * The data items must be in a singly-linked list implemented using a
1167 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 * and must be re-initialized via sys_slist_init().
1169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * @note Can be called by ISRs.
1171 *
1172 * @param fifo Address of the fifo.
1173 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 *
1175 * @return N/A
1176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178
1179/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * This routine removes a data item from @a fifo in a "first in, first out"
1183 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1186 *
1187 * @param fifo Address of the fifo.
1188 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001189 * or one of the special values K_NO_WAIT and K_FOREVER.
1190 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001191 * @return Address of the data item if successful; NULL if returned
1192 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001194extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1195
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001199 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001201 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001203 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001204 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001206 struct k_fifo name \
1207 __in_section(_k_fifo, static, name) = \
1208 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
Allan Stephensc98da842016-11-11 15:45:03 -05001210/**
1211 * @} end defgroup fifo_apis
1212 */
1213
1214/**
1215 * @cond INTERNAL_HIDDEN
1216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217
1218struct k_lifo {
1219 _wait_q_t wait_q;
1220 void *list;
1221
Anas Nashif2f203c22016-12-18 06:57:45 -05001222 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001223};
1224
Allan Stephensc98da842016-11-11 15:45:03 -05001225#define K_LIFO_INITIALIZER(obj) \
1226 { \
1227 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1228 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001229 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001230 }
1231
1232/**
1233 * INTERNAL_HIDDEN @endcond
1234 */
1235
1236/**
1237 * @defgroup lifo_apis Lifo APIs
1238 * @ingroup kernel_apis
1239 * @{
1240 */
1241
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001243 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001245 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 *
1249 * @return N/A
1250 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001251extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252
1253/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001256 * This routine adds a data item to @a lifo. A lifo data item must be
1257 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1258 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * @note Can be called by ISRs.
1261 *
1262 * @param lifo Address of the lifo.
1263 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 *
1265 * @return N/A
1266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001267extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268
1269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001270 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * This routine removes a data item from @a lifo in a "last in, first out"
1273 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1276 *
1277 * @param lifo Address of the lifo.
1278 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 * or one of the special values K_NO_WAIT and K_FOREVER.
1280 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001281 * @return Address of the data item if successful; NULL if returned
1282 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1285
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001287 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001289 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001291 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001294 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001295#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001296 struct k_lifo name \
1297 __in_section(_k_lifo, static, name) = \
1298 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001299
Allan Stephensc98da842016-11-11 15:45:03 -05001300/**
1301 * @} end defgroup lifo_apis
1302 */
1303
1304/**
1305 * @cond INTERNAL_HIDDEN
1306 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307
1308struct k_stack {
1309 _wait_q_t wait_q;
1310 uint32_t *base, *next, *top;
1311
Anas Nashif2f203c22016-12-18 06:57:45 -05001312 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001313};
1314
Allan Stephensc98da842016-11-11 15:45:03 -05001315#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1316 { \
1317 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1318 .base = stack_buffer, \
1319 .next = stack_buffer, \
1320 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001321 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001322 }
1323
1324/**
1325 * INTERNAL_HIDDEN @endcond
1326 */
1327
1328/**
1329 * @defgroup stack_apis Stack APIs
1330 * @ingroup kernel_apis
1331 * @{
1332 */
1333
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001334/**
1335 * @brief Initialize a stack.
1336 *
1337 * This routine initializes a stack object, prior to its first use.
1338 *
1339 * @param stack Address of the stack.
1340 * @param buffer Address of array used to hold stacked values.
1341 * @param num_entries Maximum number of values that can be stacked.
1342 *
1343 * @return N/A
1344 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001345extern void k_stack_init(struct k_stack *stack,
1346 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001347
1348/**
1349 * @brief Push an element onto a stack.
1350 *
1351 * This routine adds a 32-bit value @a data to @a stack.
1352 *
1353 * @note Can be called by ISRs.
1354 *
1355 * @param stack Address of the stack.
1356 * @param data Value to push onto the stack.
1357 *
1358 * @return N/A
1359 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001360extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361
1362/**
1363 * @brief Pop an element from a stack.
1364 *
1365 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1366 * manner and stores the value in @a data.
1367 *
1368 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1369 *
1370 * @param stack Address of the stack.
1371 * @param data Address of area to hold the value popped from the stack.
1372 * @param timeout Waiting period to obtain a value (in milliseconds),
1373 * or one of the special values K_NO_WAIT and K_FOREVER.
1374 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001375 * @retval 0 Element popped from stack.
1376 * @retval -EBUSY Returned without waiting.
1377 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1380
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001382 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001384 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001385 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001386 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * @param name Name of the stack.
1389 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001391#define K_STACK_DEFINE(name, stack_num_entries) \
1392 uint32_t __noinit \
1393 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001394 struct k_stack name \
1395 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001396 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1397 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399/**
Allan Stephensc98da842016-11-11 15:45:03 -05001400 * @} end defgroup stack_apis
1401 */
1402
Allan Stephens6bba9b02016-11-16 14:56:54 -05001403struct k_work;
1404
Allan Stephensc98da842016-11-11 15:45:03 -05001405/**
1406 * @defgroup workqueue_apis Workqueue Thread APIs
1407 * @ingroup kernel_apis
1408 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001409 */
1410
Allan Stephens6bba9b02016-11-16 14:56:54 -05001411/**
1412 * @typedef k_work_handler_t
1413 * @brief Work item handler function type.
1414 *
1415 * A work item's handler function is executed by a workqueue's thread
1416 * when the work item is processed by the workqueue.
1417 *
1418 * @param work Address of the work item.
1419 *
1420 * @return N/A
1421 */
1422typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423
1424/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001425 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001427
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428struct k_work_q {
1429 struct k_fifo fifo;
1430};
1431
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001432enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001433 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434};
1435
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001436struct k_work {
1437 void *_reserved; /* Used by k_fifo implementation. */
1438 k_work_handler_t handler;
1439 atomic_t flags[1];
1440};
1441
Allan Stephens6bba9b02016-11-16 14:56:54 -05001442struct k_delayed_work {
1443 struct k_work work;
1444 struct _timeout timeout;
1445 struct k_work_q *work_q;
1446};
1447
1448extern struct k_work_q k_sys_work_q;
1449
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001451 * INTERNAL_HIDDEN @endcond
1452 */
1453
1454/**
1455 * @brief Initialize a statically-defined work item.
1456 *
1457 * This macro can be used to initialize a statically-defined workqueue work
1458 * item, prior to its first use. For example,
1459 *
1460 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1461 *
1462 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463 */
1464#define K_WORK_INITIALIZER(work_handler) \
1465 { \
1466 ._reserved = NULL, \
1467 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001468 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001469 }
1470
1471/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001472 * @brief Initialize a work item.
1473 *
1474 * This routine initializes a workqueue work item, prior to its first use.
1475 *
1476 * @param work Address of work item.
1477 * @param handler Function to invoke each time work item is processed.
1478 *
1479 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480 */
1481static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1482{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001483 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001484 work->handler = handler;
1485}
1486
1487/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001488 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001489 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001490 * This routine submits work item @a work to be processed by workqueue
1491 * @a work_q. If the work item is already pending in the workqueue's queue
1492 * as a result of an earlier submission, this routine has no effect on the
1493 * work item. If the work item has already been processed, or is currently
1494 * being processed, its work is considered complete and the work item can be
1495 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001496 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001497 * @warning
1498 * A submitted work item must not be modified until it has been processed
1499 * by the workqueue.
1500 *
1501 * @note Can be called by ISRs.
1502 *
1503 * @param work_q Address of workqueue.
1504 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001505 *
1506 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507 */
1508static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1509 struct k_work *work)
1510{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001511 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001512 k_fifo_put(&work_q->fifo, work);
1513 }
1514}
1515
1516/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001517 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001518 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001519 * This routine indicates if work item @a work is pending in a workqueue's
1520 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001521 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001522 * @note Can be called by ISRs.
1523 *
1524 * @param work Address of work item.
1525 *
1526 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001527 */
1528static inline int k_work_pending(struct k_work *work)
1529{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001530 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001531}
1532
1533/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001534 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001535 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001536 * This routine starts workqueue @a work_q. The workqueue spawns its work
1537 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001538 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001539 * @param work_q Address of workqueue.
1540 * @param stack Pointer to work queue thread's stack space.
1541 * @param stack_size Size of the work queue thread's stack (in bytes).
1542 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001543 *
1544 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545 */
Allan Stephens904cf972016-10-07 13:59:23 -05001546extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001547 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001548
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001549/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001550 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001551 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001552 * This routine initializes a workqueue delayed work item, prior to
1553 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001554 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001555 * @param work Address of delayed work item.
1556 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001557 *
1558 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001560extern void k_delayed_work_init(struct k_delayed_work *work,
1561 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001562
1563/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001564 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001565 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001566 * This routine schedules work item @a work to be processed by workqueue
1567 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1568 * an asychronous countdown for the work item and then returns to the caller.
1569 * Only when the countdown completes is the work item actually submitted to
1570 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001571 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001572 * Submitting a previously submitted delayed work item that is still
1573 * counting down cancels the existing submission and restarts the countdown
1574 * using the new delay. If the work item is currently pending on the
1575 * workqueue's queue because the countdown has completed it is too late to
1576 * resubmit the item, and resubmission fails without impacting the work item.
1577 * If the work item has already been processed, or is currently being processed,
1578 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001580 * @warning
1581 * A delayed work item must not be modified until it has been processed
1582 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001583 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001584 * @note Can be called by ISRs.
1585 *
1586 * @param work_q Address of workqueue.
1587 * @param work Address of delayed work item.
1588 * @param delay Delay before submitting the work item (in milliseconds).
1589 *
1590 * @retval 0 Work item countdown started.
1591 * @retval -EINPROGRESS Work item is already pending.
1592 * @retval -EINVAL Work item is being processed or has completed its work.
1593 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001594 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001595extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1596 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001597 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001598
1599/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001600 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001602 * This routine cancels the submission of delayed work item @a work.
1603 * A delayed work item can only be cancelled while its countdown is still
1604 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001605 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001606 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001607 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001608 * @param work Address of delayed work item.
1609 *
1610 * @retval 0 Work item countdown cancelled.
1611 * @retval -EINPROGRESS Work item is already pending.
1612 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001613 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001614extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001616/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617 * @brief Submit a work item to the system workqueue.
1618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001619 * This routine submits work item @a work to be processed by the system
1620 * workqueue. If the work item is already pending in the workqueue's queue
1621 * as a result of an earlier submission, this routine has no effect on the
1622 * work item. If the work item has already been processed, or is currently
1623 * being processed, its work is considered complete and the work item can be
1624 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001626 * @warning
1627 * Work items submitted to the system workqueue should avoid using handlers
1628 * that block or yield since this may prevent the system workqueue from
1629 * processing other work items in a timely manner.
1630 *
1631 * @note Can be called by ISRs.
1632 *
1633 * @param work Address of work item.
1634 *
1635 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001636 */
1637static inline void k_work_submit(struct k_work *work)
1638{
1639 k_work_submit_to_queue(&k_sys_work_q, work);
1640}
1641
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643 * @brief Submit a delayed work item to the system workqueue.
1644 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001645 * This routine schedules work item @a work to be processed by the system
1646 * workqueue after a delay of @a delay milliseconds. The routine initiates
1647 * an asychronous countdown for the work item and then returns to the caller.
1648 * Only when the countdown completes is the work item actually submitted to
1649 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001650 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001651 * Submitting a previously submitted delayed work item that is still
1652 * counting down cancels the existing submission and restarts the countdown
1653 * using the new delay. If the work item is currently pending on the
1654 * workqueue's queue because the countdown has completed it is too late to
1655 * resubmit the item, and resubmission fails without impacting the work item.
1656 * If the work item has already been processed, or is currently being processed,
1657 * its work is considered complete and the work item can be resubmitted.
1658 *
1659 * @warning
1660 * Work items submitted to the system workqueue should avoid using handlers
1661 * that block or yield since this may prevent the system workqueue from
1662 * processing other work items in a timely manner.
1663 *
1664 * @note Can be called by ISRs.
1665 *
1666 * @param work Address of delayed work item.
1667 * @param delay Delay before submitting the work item (in milliseconds).
1668 *
1669 * @retval 0 Work item countdown started.
1670 * @retval -EINPROGRESS Work item is already pending.
1671 * @retval -EINVAL Work item is being processed or has completed its work.
1672 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001673 */
1674static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001675 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001676{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001677 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001678}
1679
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001680/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001681 * @brief Get time remaining before a delayed work gets scheduled.
1682 *
1683 * This routine computes the (approximate) time remaining before a
1684 * delayed work gets executed. If the delayed work is not waiting to be
1685 * schedules, it returns zero.
1686 *
1687 * @param work Delayed work item.
1688 *
1689 * @return Remaining time (in milliseconds).
1690 */
1691static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1692{
1693 return _timeout_remaining_get(&work->timeout);
1694}
1695
1696/**
Allan Stephensc98da842016-11-11 15:45:03 -05001697 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001698 */
1699
Allan Stephensc98da842016-11-11 15:45:03 -05001700/**
1701 * @cond INTERNAL_HIDDEN
1702 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001703
1704struct k_mutex {
1705 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001706 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707 uint32_t lock_count;
1708 int owner_orig_prio;
1709#ifdef CONFIG_OBJECT_MONITOR
1710 int num_lock_state_changes;
1711 int num_conflicts;
1712#endif
1713
Anas Nashif2f203c22016-12-18 06:57:45 -05001714 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001715};
1716
1717#ifdef CONFIG_OBJECT_MONITOR
1718#define _MUTEX_INIT_OBJECT_MONITOR \
1719 .num_lock_state_changes = 0, .num_conflicts = 0,
1720#else
1721#define _MUTEX_INIT_OBJECT_MONITOR
1722#endif
1723
1724#define K_MUTEX_INITIALIZER(obj) \
1725 { \
1726 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1727 .owner = NULL, \
1728 .lock_count = 0, \
1729 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1730 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001731 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732 }
1733
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734/**
Allan Stephensc98da842016-11-11 15:45:03 -05001735 * INTERNAL_HIDDEN @endcond
1736 */
1737
1738/**
1739 * @defgroup mutex_apis Mutex APIs
1740 * @ingroup kernel_apis
1741 * @{
1742 */
1743
1744/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001745 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001749 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001753#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001754 struct k_mutex name \
1755 __in_section(_k_mutex, static, name) = \
1756 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001757
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * Upon completion, the mutex is available and does not have an owner.
1764 *
1765 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
1767 * @return N/A
1768 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001769extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770
1771/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * This routine locks @a mutex. If the mutex is locked by another thread,
1775 * the calling thread waits until the mutex becomes available or until
1776 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * A thread is permitted to lock a mutex it has already locked. The operation
1779 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @param mutex Address of the mutex.
1782 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 * or one of the special values K_NO_WAIT and K_FOREVER.
1784 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001785 * @retval 0 Mutex locked.
1786 * @retval -EBUSY Returned without waiting.
1787 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001788 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001789extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001790
1791/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001792 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * This routine unlocks @a mutex. The mutex must already be locked by the
1795 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796 *
1797 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 * thread.
1800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 *
1803 * @return N/A
1804 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805extern void k_mutex_unlock(struct k_mutex *mutex);
1806
Allan Stephensc98da842016-11-11 15:45:03 -05001807/**
1808 * @} end defgroup mutex_apis
1809 */
1810
1811/**
1812 * @cond INTERNAL_HIDDEN
1813 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814
1815struct k_sem {
1816 _wait_q_t wait_q;
1817 unsigned int count;
1818 unsigned int limit;
1819
Anas Nashif2f203c22016-12-18 06:57:45 -05001820 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821};
1822
Allan Stephensc98da842016-11-11 15:45:03 -05001823#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1824 { \
1825 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1826 .count = initial_count, \
1827 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001828 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001829 }
1830
1831/**
1832 * INTERNAL_HIDDEN @endcond
1833 */
1834
1835/**
1836 * @defgroup semaphore_apis Semaphore APIs
1837 * @ingroup kernel_apis
1838 * @{
1839 */
1840
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001841/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001842 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001844 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001846 * @param sem Address of the semaphore.
1847 * @param initial_count Initial semaphore count.
1848 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001849 *
1850 * @return N/A
1851 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001852extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1853 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001854
1855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1861 *
1862 * @param sem Address of the semaphore.
1863 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001864 * or one of the special values K_NO_WAIT and K_FOREVER.
1865 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001866 * @note When porting code from the nanokernel legacy API to the new API, be
1867 * careful with the return value of this function. The return value is the
1868 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1869 * non-zero means failure, while the nano_sem_take family returns 1 for success
1870 * and 0 for failure.
1871 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001872 * @retval 0 Semaphore taken.
1873 * @retval -EBUSY Returned without waiting.
1874 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001875 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001876extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001877
1878/**
1879 * @brief Give a semaphore.
1880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * This routine gives @a sem, unless the semaphore is already at its maximum
1882 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001887 *
1888 * @return N/A
1889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890extern void k_sem_give(struct k_sem *sem);
1891
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001892/**
1893 * @brief Reset a semaphore's count to zero.
1894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001895 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001898 *
1899 * @return N/A
1900 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001901static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902{
1903 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001904}
1905
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001906/**
1907 * @brief Get a semaphore's count.
1908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001911 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001914 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001915static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916{
1917 return sem->count;
1918}
1919
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001924 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001925 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * @param name Name of the semaphore.
1928 * @param initial_count Initial semaphore count.
1929 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001930 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001932 struct k_sem name \
1933 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934 K_SEM_INITIALIZER(name, initial_count, count_limit)
1935
Allan Stephensc98da842016-11-11 15:45:03 -05001936/**
1937 * @} end defgroup semaphore_apis
1938 */
1939
1940/**
1941 * @defgroup alert_apis Alert APIs
1942 * @ingroup kernel_apis
1943 * @{
1944 */
1945
Allan Stephens5eceb852016-11-16 10:16:30 -05001946/**
1947 * @typedef k_alert_handler_t
1948 * @brief Alert handler function type.
1949 *
1950 * An alert's alert handler function is invoked by the system workqueue
1951 * when the alert is signalled. The alert handler function is optional,
1952 * and is only invoked if the alert has been initialized with one.
1953 *
1954 * @param alert Address of the alert.
1955 *
1956 * @return 0 if alert has been consumed; non-zero if alert should pend.
1957 */
1958typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001959
1960/**
1961 * @} end defgroup alert_apis
1962 */
1963
1964/**
1965 * @cond INTERNAL_HIDDEN
1966 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001968#define K_ALERT_DEFAULT NULL
1969#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001971struct k_alert {
1972 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973 atomic_t send_count;
1974 struct k_work work_item;
1975 struct k_sem sem;
1976
Anas Nashif2f203c22016-12-18 06:57:45 -05001977 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001978};
1979
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001980extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001982#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001984 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001986 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001987 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001988 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989 }
1990
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001991/**
Allan Stephensc98da842016-11-11 15:45:03 -05001992 * INTERNAL_HIDDEN @endcond
1993 */
1994
1995/**
1996 * @addtogroup alert_apis
1997 * @{
1998 */
1999
2000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002003 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002005 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @param name Name of the alert.
2008 * @param alert_handler Action to take when alert is sent. Specify either
2009 * the address of a function to be invoked by the system workqueue
2010 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2011 * K_ALERT_DEFAULT (which causes the alert to pend).
2012 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002014#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002015 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002016 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002017 K_ALERT_INITIALIZER(name, alert_handler, \
2018 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002019
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @param alert Address of the alert.
2026 * @param handler Action to take when alert is sent. Specify either the address
2027 * of a function to be invoked by the system workqueue thread,
2028 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2029 * K_ALERT_DEFAULT (which causes the alert to pend).
2030 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
2032 * @return N/A
2033 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002034extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2035 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036
2037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002040 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2043 *
2044 * @param alert Address of the alert.
2045 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 * or one of the special values K_NO_WAIT and K_FOREVER.
2047 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002048 * @retval 0 Alert received.
2049 * @retval -EBUSY Returned without waiting.
2050 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002052extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053
2054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002055 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057 * This routine signals @a alert. The action specified for @a alert will
2058 * be taken, which may trigger the execution of an alert handler function
2059 * and/or cause the alert to pend (assuming the alert has not reached its
2060 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002062 * @note Can be called by ISRs.
2063 *
2064 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002065 *
2066 * @return N/A
2067 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002068extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069
2070/**
Allan Stephensc98da842016-11-11 15:45:03 -05002071 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072 */
2073
Allan Stephensc98da842016-11-11 15:45:03 -05002074/**
2075 * @cond INTERNAL_HIDDEN
2076 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002077
2078struct k_msgq {
2079 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002080 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081 uint32_t max_msgs;
2082 char *buffer_start;
2083 char *buffer_end;
2084 char *read_ptr;
2085 char *write_ptr;
2086 uint32_t used_msgs;
2087
Anas Nashif2f203c22016-12-18 06:57:45 -05002088 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089};
2090
Peter Mitsis1da807e2016-10-06 11:36:59 -04002091#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092 { \
2093 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002094 .max_msgs = q_max_msgs, \
2095 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002097 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098 .read_ptr = q_buffer, \
2099 .write_ptr = q_buffer, \
2100 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002101 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102 }
2103
Peter Mitsis1da807e2016-10-06 11:36:59 -04002104/**
Allan Stephensc98da842016-11-11 15:45:03 -05002105 * INTERNAL_HIDDEN @endcond
2106 */
2107
2108/**
2109 * @defgroup msgq_apis Message Queue APIs
2110 * @ingroup kernel_apis
2111 * @{
2112 */
2113
2114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2118 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002119 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2120 * message is similarly aligned to this boundary, @a q_msg_size must also be
2121 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * The message queue can be accessed outside the module where it is defined
2124 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002125 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002126 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * @param q_name Name of the message queue.
2129 * @param q_msg_size Message size (in bytes).
2130 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002131 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002132 */
2133#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2134 static char __noinit __aligned(q_align) \
2135 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002136 struct k_msgq q_name \
2137 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002138 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2139 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002140
Peter Mitsisd7a37502016-10-13 11:37:40 -04002141/**
2142 * @brief Initialize a message queue.
2143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * This routine initializes a message queue object, prior to its first use.
2145 *
Allan Stephensda827222016-11-09 14:23:58 -06002146 * The message queue's ring buffer must contain space for @a max_msgs messages,
2147 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2148 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2149 * that each message is similarly aligned to this boundary, @a q_msg_size
2150 * must also be a multiple of N.
2151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002152 * @param q Address of the message queue.
2153 * @param buffer Pointer to ring buffer that holds queued messages.
2154 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002155 * @param max_msgs Maximum number of messages that can be queued.
2156 *
2157 * @return N/A
2158 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002159extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002160 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002161
2162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002166 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002167 * @note Can be called by ISRs.
2168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002169 * @param q Address of the message queue.
2170 * @param data Pointer to the message.
2171 * @param timeout Waiting period to add the message (in milliseconds),
2172 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002173 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002174 * @retval 0 Message sent.
2175 * @retval -ENOMSG Returned without waiting or queue purged.
2176 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002177 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002179
2180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * This routine receives a message from message queue @a q in a "first in,
2184 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002185 *
Allan Stephensc98da842016-11-11 15:45:03 -05002186 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002188 * @param q Address of the message queue.
2189 * @param data Address of area to hold the received message.
2190 * @param timeout Waiting period to receive the message (in milliseconds),
2191 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002192 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002193 * @retval 0 Message received.
2194 * @retval -ENOMSG Returned without waiting.
2195 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002196 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002198
2199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002200 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * This routine discards all unreceived messages in a message queue's ring
2203 * buffer. Any threads that are blocked waiting to send a message to the
2204 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002207 *
2208 * @return N/A
2209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210extern void k_msgq_purge(struct k_msgq *q);
2211
Peter Mitsis67be2492016-10-07 11:44:34 -04002212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002213 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002215 * This routine returns the number of unused entries in a message queue's
2216 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * @param q Address of the message queue.
2219 *
2220 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002221 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002222static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002223{
2224 return q->max_msgs - q->used_msgs;
2225}
2226
Peter Mitsisd7a37502016-10-13 11:37:40 -04002227/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002228 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002230 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002232 * @param q Address of the message queue.
2233 *
2234 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002235 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002236static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237{
2238 return q->used_msgs;
2239}
2240
Allan Stephensc98da842016-11-11 15:45:03 -05002241/**
2242 * @} end defgroup msgq_apis
2243 */
2244
2245/**
2246 * @defgroup mem_pool_apis Memory Pool APIs
2247 * @ingroup kernel_apis
2248 * @{
2249 */
2250
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002252 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253 void *addr_in_pool;
2254 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002255 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256};
2257
Allan Stephensc98da842016-11-11 15:45:03 -05002258/**
2259 * @} end defgroup mem_pool_apis
2260 */
2261
2262/**
2263 * @defgroup mailbox_apis Mailbox APIs
2264 * @ingroup kernel_apis
2265 * @{
2266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267
2268struct k_mbox_msg {
2269 /** internal use only - needed for legacy API support */
2270 uint32_t _mailbox;
2271 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002272 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273 /** application-defined information value */
2274 uint32_t info;
2275 /** sender's message data buffer */
2276 void *tx_data;
2277 /** internal use only - needed for legacy API support */
2278 void *_rx_data;
2279 /** message data block descriptor */
2280 struct k_mem_block tx_block;
2281 /** source thread id */
2282 k_tid_t rx_source_thread;
2283 /** target thread id */
2284 k_tid_t tx_target_thread;
2285 /** internal use only - thread waiting on send (may be a dummy) */
2286 k_tid_t _syncing_thread;
2287#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2288 /** internal use only - semaphore used during asynchronous send */
2289 struct k_sem *_async_sem;
2290#endif
2291};
2292
Allan Stephensc98da842016-11-11 15:45:03 -05002293/**
2294 * @cond INTERNAL_HIDDEN
2295 */
2296
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297struct k_mbox {
2298 _wait_q_t tx_msg_queue;
2299 _wait_q_t rx_msg_queue;
2300
Anas Nashif2f203c22016-12-18 06:57:45 -05002301 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302};
2303
2304#define K_MBOX_INITIALIZER(obj) \
2305 { \
2306 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2307 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002308 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309 }
2310
Peter Mitsis12092702016-10-14 12:57:23 -04002311/**
Allan Stephensc98da842016-11-11 15:45:03 -05002312 * INTERNAL_HIDDEN @endcond
2313 */
2314
2315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002320 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002323 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002325 struct k_mbox name \
2326 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327 K_MBOX_INITIALIZER(name) \
2328
Peter Mitsis12092702016-10-14 12:57:23 -04002329/**
2330 * @brief Initialize a mailbox.
2331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * This routine initializes a mailbox object, prior to its first use.
2333 *
2334 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002335 *
2336 * @return N/A
2337 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338extern void k_mbox_init(struct k_mbox *mbox);
2339
Peter Mitsis12092702016-10-14 12:57:23 -04002340/**
2341 * @brief Send a mailbox message in a synchronous manner.
2342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * This routine sends a message to @a mbox and waits for a receiver to both
2344 * receive and process it. The message data may be in a buffer, in a memory
2345 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @param mbox Address of the mailbox.
2348 * @param tx_msg Address of the transmit message descriptor.
2349 * @param timeout Waiting period for the message to be received (in
2350 * milliseconds), or one of the special values K_NO_WAIT
2351 * and K_FOREVER. Once the message has been received,
2352 * this routine waits as long as necessary for the message
2353 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002354 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002355 * @retval 0 Message sent.
2356 * @retval -ENOMSG Returned without waiting.
2357 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002358 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002359extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002361
Peter Mitsis12092702016-10-14 12:57:23 -04002362/**
2363 * @brief Send a mailbox message in an asynchronous manner.
2364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * This routine sends a message to @a mbox without waiting for a receiver
2366 * to process it. The message data may be in a buffer, in a memory pool block,
2367 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2368 * will be given when the message has been both received and completely
2369 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * @param mbox Address of the mailbox.
2372 * @param tx_msg Address of the transmit message descriptor.
2373 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002374 *
2375 * @return N/A
2376 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002377extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 struct k_sem *sem);
2379
Peter Mitsis12092702016-10-14 12:57:23 -04002380/**
2381 * @brief Receive a mailbox message.
2382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * This routine receives a message from @a mbox, then optionally retrieves
2384 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002386 * @param mbox Address of the mailbox.
2387 * @param rx_msg Address of the receive message descriptor.
2388 * @param buffer Address of the buffer to receive data, or NULL to defer data
2389 * retrieval and message disposal until later.
2390 * @param timeout Waiting period for a message to be received (in
2391 * milliseconds), or one of the special values K_NO_WAIT
2392 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002393 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002394 * @retval 0 Message received.
2395 * @retval -ENOMSG Returned without waiting.
2396 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002397 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002398extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002400
2401/**
2402 * @brief Retrieve mailbox message data into a buffer.
2403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * This routine completes the processing of a received message by retrieving
2405 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002406 *
2407 * Alternatively, this routine can be used to dispose of a received message
2408 * without retrieving its data.
2409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @param rx_msg Address of the receive message descriptor.
2411 * @param buffer Address of the buffer to receive data, or NULL to discard
2412 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002413 *
2414 * @return N/A
2415 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002416extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002417
2418/**
2419 * @brief Retrieve mailbox message data into a memory pool block.
2420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * This routine completes the processing of a received message by retrieving
2422 * its data into a memory pool block, then disposing of the message.
2423 * The memory pool block that results from successful retrieval must be
2424 * returned to the pool once the data has been processed, even in cases
2425 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002426 *
2427 * Alternatively, this routine can be used to dispose of a received message
2428 * without retrieving its data. In this case there is no need to return a
2429 * memory pool block to the pool.
2430 *
2431 * This routine allocates a new memory pool block for the data only if the
2432 * data is not already in one. If a new block cannot be allocated, the routine
2433 * returns a failure code and the received message is left unchanged. This
2434 * permits the caller to reattempt data retrieval at a later time or to dispose
2435 * of the received message without retrieving its data.
2436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * @param rx_msg Address of a receive message descriptor.
2438 * @param pool Address of memory pool, or NULL to discard data.
2439 * @param block Address of the area to hold memory pool block info.
2440 * @param timeout Waiting period to wait for a memory pool block (in
2441 * milliseconds), or one of the special values K_NO_WAIT
2442 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002443 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002444 * @retval 0 Data retrieved.
2445 * @retval -ENOMEM Returned without waiting.
2446 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002447 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002448extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002449 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 struct k_mem_block *block, int32_t timeout);
2451
Allan Stephensc98da842016-11-11 15:45:03 -05002452/**
2453 * @} end defgroup mailbox_apis
2454 */
2455
2456/**
2457 * @cond INTERNAL_HIDDEN
2458 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459
2460struct k_pipe {
2461 unsigned char *buffer; /* Pipe buffer: may be NULL */
2462 size_t size; /* Buffer size */
2463 size_t bytes_used; /* # bytes used in buffer */
2464 size_t read_index; /* Where in buffer to read from */
2465 size_t write_index; /* Where in buffer to write */
2466
2467 struct {
2468 _wait_q_t readers; /* Reader wait queue */
2469 _wait_q_t writers; /* Writer wait queue */
2470 } wait_q;
2471
Anas Nashif2f203c22016-12-18 06:57:45 -05002472 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473};
2474
Peter Mitsise5d9c582016-10-14 14:44:57 -04002475#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476 { \
2477 .buffer = pipe_buffer, \
2478 .size = pipe_buffer_size, \
2479 .bytes_used = 0, \
2480 .read_index = 0, \
2481 .write_index = 0, \
2482 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2483 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002484 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485 }
2486
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487/**
Allan Stephensc98da842016-11-11 15:45:03 -05002488 * INTERNAL_HIDDEN @endcond
2489 */
2490
2491/**
2492 * @defgroup pipe_apis Pipe APIs
2493 * @ingroup kernel_apis
2494 * @{
2495 */
2496
2497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002502 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @param name Name of the pipe.
2505 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2506 * or zero if no ring buffer is used.
2507 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002509#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2510 static unsigned char __noinit __aligned(pipe_align) \
2511 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002512 struct k_pipe name \
2513 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002514 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * This routine initializes a pipe object, prior to its first use.
2520 *
2521 * @param pipe Address of the pipe.
2522 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2523 * is used.
2524 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2525 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526 *
2527 * @return N/A
2528 */
2529extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2530 size_t size);
2531
2532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @param pipe Address of the pipe.
2538 * @param data Address of data to write.
2539 * @param bytes_to_write Size of data (in bytes).
2540 * @param bytes_written Address of area to hold the number of bytes written.
2541 * @param min_xfer Minimum number of bytes to write.
2542 * @param timeout Waiting period to wait for the data to be written (in
2543 * milliseconds), or one of the special values K_NO_WAIT
2544 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002546 * @retval 0 At least @a min_xfer bytes of data were written.
2547 * @retval -EIO Returned without waiting; zero data bytes were written.
2548 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002551extern int k_pipe_put(struct k_pipe *pipe, void *data,
2552 size_t bytes_to_write, size_t *bytes_written,
2553 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
2555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @param pipe Address of the pipe.
2561 * @param data Address to place the data read from pipe.
2562 * @param bytes_to_read Maximum number of data bytes to read.
2563 * @param bytes_read Address of area to hold the number of bytes read.
2564 * @param min_xfer Minimum number of data bytes to read.
2565 * @param timeout Waiting period to wait for the data to be read (in
2566 * milliseconds), or one of the special values K_NO_WAIT
2567 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002569 * @retval 0 At least @a min_xfer bytes of data were read.
2570 * @retval -EIO Returned without waiting; zero data bytes were read.
2571 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002572 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002574extern int k_pipe_get(struct k_pipe *pipe, void *data,
2575 size_t bytes_to_read, size_t *bytes_read,
2576 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577
2578/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * This routine writes the data contained in a memory block to @a pipe.
2582 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 * @param block Memory block containing data to send
2587 * @param size Number of data bytes in memory block to send
2588 * @param sem Semaphore to signal upon completion (else NULL)
2589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 */
2592extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2593 size_t size, struct k_sem *sem);
2594
2595/**
Allan Stephensc98da842016-11-11 15:45:03 -05002596 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597 */
2598
Allan Stephensc98da842016-11-11 15:45:03 -05002599/**
2600 * @cond INTERNAL_HIDDEN
2601 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002603struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002605 uint32_t num_blocks;
2606 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607 char *buffer;
2608 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002609 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610
Anas Nashif2f203c22016-12-18 06:57:45 -05002611 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612};
2613
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002614#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2615 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 { \
2617 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002618 .num_blocks = slab_num_blocks, \
2619 .block_size = slab_block_size, \
2620 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621 .free_list = NULL, \
2622 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002623 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624 }
2625
Peter Mitsis578f9112016-10-07 13:50:31 -04002626/**
Allan Stephensc98da842016-11-11 15:45:03 -05002627 * INTERNAL_HIDDEN @endcond
2628 */
2629
2630/**
2631 * @defgroup mem_slab_apis Memory Slab APIs
2632 * @ingroup kernel_apis
2633 * @{
2634 */
2635
2636/**
Allan Stephensda827222016-11-09 14:23:58 -06002637 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002638 *
Allan Stephensda827222016-11-09 14:23:58 -06002639 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002641 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2642 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002643 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002644 *
Allan Stephensda827222016-11-09 14:23:58 -06002645 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002648 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @param name Name of the memory slab.
2651 * @param slab_block_size Size of each memory block (in bytes).
2652 * @param slab_num_blocks Number memory blocks.
2653 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002654 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002655#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2656 char __noinit __aligned(slab_align) \
2657 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2658 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002659 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002660 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2661 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002663/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002664 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002667 *
Allan Stephensda827222016-11-09 14:23:58 -06002668 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2669 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2670 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2671 * To ensure that each memory block is similarly aligned to this boundary,
2672 * @a slab_block_size must also be a multiple of N.
2673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * @param slab Address of the memory slab.
2675 * @param buffer Pointer to buffer used for the memory blocks.
2676 * @param block_size Size of each memory block (in bytes).
2677 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002678 *
2679 * @return N/A
2680 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002681extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002682 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002683
2684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * @param slab Address of the memory slab.
2690 * @param mem Pointer to block address area.
2691 * @param timeout Maximum time to wait for operation to complete
2692 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2693 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002694 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002695 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002697 * @retval -ENOMEM Returned without waiting.
2698 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002699 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002700extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2701 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002702
2703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002706 * This routine releases a previously allocated memory block back to its
2707 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002709 * @param slab Address of the memory slab.
2710 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002711 *
2712 * @return N/A
2713 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002714extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * This routine gets the number of memory blocks that are currently
2720 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002725 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002726static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002728 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729}
2730
Peter Mitsisc001aa82016-10-13 13:53:37 -04002731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * This routine gets the number of memory blocks that are currently
2735 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002740 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002741static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002742{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002743 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002744}
2745
Allan Stephensc98da842016-11-11 15:45:03 -05002746/**
2747 * @} end defgroup mem_slab_apis
2748 */
2749
2750/**
2751 * @cond INTERNAL_HIDDEN
2752 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002754/*
2755 * Memory pool requires a buffer and two arrays of structures for the
2756 * memory block accounting:
2757 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2758 * status of four blocks of memory.
2759 */
2760struct k_mem_pool_quad_block {
2761 char *mem_blocks; /* pointer to the first of four memory blocks */
2762 uint32_t mem_status; /* four bits. If bit is set, memory block is
2763 allocated */
2764};
2765/*
2766 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2767 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2768 * block size is 4 times less than the previous one and thus requires 4 times
2769 * bigger array of k_mem_pool_quad_block structures to keep track of the
2770 * memory blocks.
2771 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002773/*
2774 * The array of k_mem_pool_block_set keeps the information of each array of
2775 * k_mem_pool_quad_block structures
2776 */
2777struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002778 size_t block_size; /* memory block size */
2779 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002780 struct k_mem_pool_quad_block *quad_block;
2781 int count;
2782};
2783
2784/* Memory pool descriptor */
2785struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002786 size_t max_block_size;
2787 size_t min_block_size;
2788 uint32_t nr_of_maxblocks;
2789 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002790 struct k_mem_pool_block_set *block_set;
2791 char *bufblock;
2792 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002793 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794};
2795
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002796#ifdef CONFIG_ARM
2797#define _SECTION_TYPE_SIGN "%"
2798#else
2799#define _SECTION_TYPE_SIGN "@"
2800#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002802/*
2803 * Static memory pool initialization
2804 */
Allan Stephensc98da842016-11-11 15:45:03 -05002805
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002806/*
2807 * Use .altmacro to be able to recalculate values and pass them as string
2808 * arguments when calling assembler macros resursively
2809 */
2810__asm__(".altmacro\n\t");
2811
2812/*
2813 * Recursively calls a macro
2814 * The followig global symbols need to be initialized:
2815 * __memory_pool_max_block_size - maximal size of the memory block
2816 * __memory_pool_min_block_size - minimal size of the memory block
2817 * Notes:
2818 * Global symbols are used due the fact that assembler macro allows only
2819 * one argument be passed with the % conversion
2820 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2821 * is used instead of / 4.
2822 * n_max argument needs to go first in the invoked macro, as some
2823 * assemblers concatenate \name and %(\n_max * 4) arguments
2824 * if \name goes first
2825 */
2826__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2827 ".ifge __memory_pool_max_block_size >> 2 -"
2828 " __memory_pool_min_block_size\n\t\t"
2829 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2830 "\\macro_name %(\\n_max * 4) \\name\n\t"
2831 ".endif\n\t"
2832 ".endm\n");
2833
2834/*
2835 * Build quad blocks
2836 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2837 * structures and recursively calls itself for the next array, 4 times
2838 * larger.
2839 * The followig global symbols need to be initialized:
2840 * __memory_pool_max_block_size - maximal size of the memory block
2841 * __memory_pool_min_block_size - minimal size of the memory block
2842 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2843 */
2844__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002845 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002846 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2847 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2848 ".if \\n_max % 4\n\t\t"
2849 ".skip __memory_pool_quad_block_size\n\t"
2850 ".endif\n\t"
2851 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2852 ".endm\n");
2853
2854/*
2855 * Build block sets and initialize them
2856 * Macro initializes the k_mem_pool_block_set structure and
2857 * recursively calls itself for the next one.
2858 * The followig global symbols need to be initialized:
2859 * __memory_pool_max_block_size - maximal size of the memory block
2860 * __memory_pool_min_block_size - minimal size of the memory block
2861 * __memory_pool_block_set_count, the number of the elements in the
2862 * block set array must be set to 0. Macro calculates it's real
2863 * value.
2864 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2865 * structures, _build_quad_blocks must be called prior it.
2866 */
2867__asm__(".macro _build_block_set n_max, name\n\t"
2868 ".int __memory_pool_max_block_size\n\t" /* block_size */
2869 ".if \\n_max % 4\n\t\t"
2870 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2871 ".else\n\t\t"
2872 ".int \\n_max >> 2\n\t"
2873 ".endif\n\t"
2874 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2875 ".int 0\n\t" /* count */
2876 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2877 "__do_recurse _build_block_set \\name \\n_max\n\t"
2878 ".endm\n");
2879
2880/*
2881 * Build a memory pool structure and initialize it
2882 * Macro uses __memory_pool_block_set_count global symbol,
2883 * block set addresses and buffer address, it may be called only after
2884 * _build_block_set
2885 */
2886__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002887 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002888 _SECTION_TYPE_SIGN "progbits\n\t"
2889 ".globl \\name\n\t"
2890 "\\name:\n\t"
2891 ".int \\max_size\n\t" /* max_block_size */
2892 ".int \\min_size\n\t" /* min_block_size */
2893 ".int \\n_max\n\t" /* nr_of_maxblocks */
2894 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2895 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2896 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2897 ".int 0\n\t" /* wait_q->head */
2898 ".int 0\n\t" /* wait_q->next */
2899 ".popsection\n\t"
2900 ".endm\n");
2901
2902#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2903 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2904 _SECTION_TYPE_SIGN "progbits\n\t"); \
2905 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2906 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2907 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2908 STRINGIFY(name) "\n\t"); \
2909 __asm__(".popsection\n\t")
2910
2911#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2912 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2913 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2914 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2915 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002916 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002917 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2918 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2919 STRINGIFY(name) "\n\t"); \
2920 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2921 __asm__(".int __memory_pool_block_set_count\n\t"); \
2922 __asm__(".popsection\n\t"); \
2923 extern uint32_t _mem_pool_block_set_count_##name; \
2924 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2925
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002926#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2927 char __noinit __aligned(align) \
2928 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002929
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002930/*
2931 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2932 * to __memory_pool_quad_block_size absolute symbol.
2933 * This function does not get called, but compiler calculates the value and
2934 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2935 */
2936static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2937{
2938 __asm__(".globl __memory_pool_quad_block_size\n\t"
2939#ifdef CONFIG_NIOS2
2940 "__memory_pool_quad_block_size = %0\n\t"
2941#else
2942 "__memory_pool_quad_block_size = %c0\n\t"
2943#endif
2944 :
2945 : "n"(sizeof(struct k_mem_pool_quad_block)));
2946}
2947
2948/**
Allan Stephensc98da842016-11-11 15:45:03 -05002949 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002950 */
2951
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002952/**
Allan Stephensc98da842016-11-11 15:45:03 -05002953 * @addtogroup mem_pool_apis
2954 * @{
2955 */
2956
2957/**
2958 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2961 * long. The memory pool allows blocks to be repeatedly partitioned into
2962 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2963 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002964 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002966 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002967 * If the pool is to be accessed outside the module where it is defined, it
2968 * can be declared via
2969 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002970 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @param name Name of the memory pool.
2973 * @param min_size Size of the smallest blocks in the pool (in bytes).
2974 * @param max_size Size of the largest blocks in the pool (in bytes).
2975 * @param n_max Number of maximum sized blocks in the pool.
2976 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002977 */
2978#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002979 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2980 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002981 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002982 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2983 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2984 extern struct k_mem_pool name
2985
Peter Mitsis937042c2016-10-13 13:18:26 -04002986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * @param pool Address of the memory pool.
2992 * @param block Pointer to block descriptor for the allocated memory.
2993 * @param size Amount of memory to allocate (in bytes).
2994 * @param timeout Maximum time to wait for operation to complete
2995 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2996 * or K_FOREVER to wait as long as necessary.
2997 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002998 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003000 * @retval -ENOMEM Returned without waiting.
3001 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003002 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003003extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003004 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003005
3006/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * This routine releases a previously allocated memory block back to its
3010 * memory pool.
3011 *
3012 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003013 *
3014 * @return N/A
3015 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003017
3018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * This routine instructs a memory pool to concatenate unused memory blocks
3022 * into larger blocks wherever possible. Manually defragmenting the memory
3023 * pool may speed up future allocations of memory blocks by eliminating the
3024 * need for the memory pool to perform an automatic partial defragmentation.
3025 *
3026 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003027 *
3028 * @return N/A
3029 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003030extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003031
3032/**
Allan Stephensc98da842016-11-11 15:45:03 -05003033 * @} end addtogroup mem_pool_apis
3034 */
3035
3036/**
3037 * @defgroup heap_apis Heap Memory Pool APIs
3038 * @ingroup kernel_apis
3039 * @{
3040 */
3041
3042/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003043 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003046 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003051 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003052extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003053
3054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003056 *
3057 * This routine provides traditional free() semantics. The memory being
3058 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003059 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003060 * If @a ptr is NULL, no operation is performed.
3061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003063 *
3064 * @return N/A
3065 */
3066extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067
Allan Stephensc98da842016-11-11 15:45:03 -05003068/**
3069 * @} end defgroup heap_apis
3070 */
3071
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003072/**
3073 * @brief Make the CPU idle.
3074 *
3075 * This function makes the CPU idle until an event wakes it up.
3076 *
3077 * In a regular system, the idle thread should be the only thread responsible
3078 * for making the CPU idle and triggering any type of power management.
3079 * However, in some more constrained systems, such as a single-threaded system,
3080 * the only thread would be responsible for this if needed.
3081 *
3082 * @return N/A
3083 */
3084extern void k_cpu_idle(void);
3085
3086/**
3087 * @brief Make the CPU idle in an atomic fashion.
3088 *
3089 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3090 * must be done atomically before making the CPU idle.
3091 *
3092 * @param key Interrupt locking key obtained from irq_lock().
3093 *
3094 * @return N/A
3095 */
3096extern void k_cpu_atomic_idle(unsigned int key);
3097
Andrew Boie350f88d2017-01-18 13:13:45 -08003098extern void _sys_power_save_idle_exit(int32_t ticks);
3099
Anas Nashifa6149502017-01-17 07:47:31 -05003100/* Include legacy APIs */
3101#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003103#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003104#include <arch/cpu.h>
3105
3106/*
3107 * private APIs that are utilized by one or more public APIs
3108 */
3109
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003110#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003112#else
3113#define _init_static_threads() do { } while ((0))
3114#endif
3115
3116extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003117extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118
3119#ifdef __cplusplus
3120}
3121#endif
3122
Andrew Boiee004dec2016-11-07 09:01:19 -08003123#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3124/*
3125 * Define new and delete operators.
3126 * At this moment, the operators do nothing since objects are supposed
3127 * to be statically allocated.
3128 */
3129inline void operator delete(void *ptr)
3130{
3131 (void)ptr;
3132}
3133
3134inline void operator delete[](void *ptr)
3135{
3136 (void)ptr;
3137}
3138
3139inline void *operator new(size_t size)
3140{
3141 (void)size;
3142 return NULL;
3143}
3144
3145inline void *operator new[](size_t size)
3146{
3147 (void)size;
3148 return NULL;
3149}
3150
3151/* Placement versions of operator new and delete */
3152inline void operator delete(void *ptr1, void *ptr2)
3153{
3154 (void)ptr1;
3155 (void)ptr2;
3156}
3157
3158inline void operator delete[](void *ptr1, void *ptr2)
3159{
3160 (void)ptr1;
3161 (void)ptr2;
3162}
3163
3164inline void *operator new(size_t size, void *ptr)
3165{
3166 (void)size;
3167 return ptr;
3168}
3169
3170inline void *operator new[](size_t size, void *ptr)
3171{
3172 (void)size;
3173 return ptr;
3174}
3175
3176#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3177
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003178#endif /* !_ASMLANGUAGE */
3179
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180#endif /* _kernel__h_ */