blob: 59c57a82642ff211e7bf0995507a455b1595dd6e [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
176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500177 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500179 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500181 * The new thread may be scheduled for immediate execution or a delayed start.
182 * If the newly spawned thread does not have a delayed start the kernel
183 * scheduler may preempt the current thread to allow the new thread to
184 * execute.
185 *
186 * Thread options are architecture-specific, and can include K_ESSENTIAL,
187 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
188 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400189 *
190 * @param stack Pointer to the stack space.
191 * @param stack_size Stack size in bytes.
192 * @param entry Thread entry function.
193 * @param p1 1st entry point parameter.
194 * @param p2 2nd entry point parameter.
195 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * @param prio Thread priority.
197 * @param options Thread options.
198 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500200 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500202extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500203 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500205 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500208 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209 *
Allan Stephensc98da842016-11-11 15:45:03 -0500210 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500211 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500213 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214 *
215 * @return N/A
216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400217extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218
219/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
222 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500223 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400224 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 * @return N/A
226 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400227extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228
229/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500230 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500232 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400233 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500234 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235 *
236 * @return N/A
237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400238extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400239
240/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500241 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500243 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500245 * If @a thread is not currently sleeping, the routine has no effect.
246 *
247 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400248 *
249 * @return N/A
250 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400251extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400252
253/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500254 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500256 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400259
260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500261 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500263 * This routine prevents @a thread from executing if it has not yet started
264 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500266 * @param thread ID of thread to cancel.
267 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500268 * @retval 0 Thread spawning cancelled.
269 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400270 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400271extern int k_thread_cancel(k_tid_t thread);
272
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400273/**
Allan Stephensc98da842016-11-11 15:45:03 -0500274 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500276 * This routine permanently stops execution of @a thread. The thread is taken
277 * off all kernel queues it is part of (i.e. the ready queue, the timeout
278 * queue, or a kernel object wait queue). However, any kernel resources the
279 * thread might currently own (such as mutexes or memory blocks) are not
280 * released. It is the responsibility of the caller of this routine to ensure
281 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500283 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400284 *
285 * @return N/A
286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287extern void k_thread_abort(k_tid_t thread);
288
Allan Stephensc98da842016-11-11 15:45:03 -0500289/**
290 * @cond INTERNAL_HIDDEN
291 */
292
Benjamin Walshd211a522016-12-06 11:44:01 -0500293/* timeout has timed out and is not on _timeout_q anymore */
294#define _EXPIRED (-2)
295
296/* timeout is not in use */
297#define _INACTIVE (-1)
298
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400299#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400300#define _THREAD_TIMEOUT_INIT(obj) \
301 (obj).nano_timeout = { \
302 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400303 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400304 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500305 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400306 },
307#else
308#define _THREAD_TIMEOUT_INIT(obj)
309#endif
310
311#ifdef CONFIG_ERRNO
312#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
313#else
314#define _THREAD_ERRNO_INIT(obj)
315#endif
316
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400317struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400318 union {
319 char *init_stack;
320 struct k_thread *thread;
321 };
322 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500323 void (*init_entry)(void *, void *, void *);
324 void *init_p1;
325 void *init_p2;
326 void *init_p3;
327 int init_prio;
328 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400329 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500330 void (*init_abort)(void);
331 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400332};
333
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400334#define _THREAD_INITIALIZER(stack, stack_size, \
335 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500336 prio, options, delay, abort, groups) \
337 { \
338 .init_stack = (stack), \
339 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400340 .init_entry = (void (*)(void *, void *, void *))entry, \
341 .init_p1 = (void *)p1, \
342 .init_p2 = (void *)p2, \
343 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500344 .init_prio = (prio), \
345 .init_options = (options), \
346 .init_delay = (delay), \
347 .init_abort = (abort), \
348 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400349 }
350
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400351/**
Allan Stephensc98da842016-11-11 15:45:03 -0500352 * INTERNAL_HIDDEN @endcond
353 */
354
355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500356 * @brief Statically define and initialize a thread.
357 *
358 * The thread may be scheduled for immediate execution or a delayed start.
359 *
360 * Thread options are architecture-specific, and can include K_ESSENTIAL,
361 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
362 * them using "|" (the logical OR operator).
363 *
364 * The ID of the thread can be accessed using:
365 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500366 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500367 *
368 * @param name Name of the thread.
369 * @param stack_size Stack size in bytes.
370 * @param entry Thread entry function.
371 * @param p1 1st entry point parameter.
372 * @param p2 2nd entry point parameter.
373 * @param p3 3rd entry point parameter.
374 * @param prio Thread priority.
375 * @param options Thread options.
376 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400377 *
378 * @internal It has been observed that the x86 compiler by default aligns
379 * these _static_thread_data structures to 32-byte boundaries, thereby
380 * wasting space. To work around this, force a 4-byte alignment.
381 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500382#define K_THREAD_DEFINE(name, stack_size, \
383 entry, p1, p2, p3, \
384 prio, options, delay) \
385 char __noinit __stack _k_thread_obj_##name[stack_size]; \
386 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500387 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500388 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
389 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500390 NULL, 0); \
391 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400392
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400393/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500394 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500396 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500398 * @param thread ID of thread whose priority is needed.
399 *
400 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400401 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500402extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403
404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500405 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500407 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400408 *
409 * Rescheduling can occur immediately depending on the priority @a thread is
410 * set to:
411 *
412 * - If its priority is raised above the priority of the caller of this
413 * function, and the caller is preemptible, @a thread will be scheduled in.
414 *
415 * - If the caller operates on itself, it lowers its priority below that of
416 * other threads in the system, and the caller is preemptible, the thread of
417 * highest priority will be scheduled in.
418 *
419 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
420 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
421 * highest priority.
422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500423 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400424 * @param prio New priority.
425 *
426 * @warning Changing the priority of a thread currently involved in mutex
427 * priority inheritance may result in undefined behavior.
428 *
429 * @return N/A
430 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400431extern void k_thread_priority_set(k_tid_t thread, int prio);
432
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * This routine prevents the kernel scheduler from making @a thread the
437 * current thread. All other internal operations on @a thread are still
438 * performed; for example, any timeout it is waiting on keeps ticking,
439 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500441 * If @a thread is already suspended, the routine has no effect.
442 *
443 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 *
445 * @return N/A
446 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400447extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448
449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500450 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * This routine allows the kernel scheduler to make @a thread the current
453 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * If @a thread is not currently suspended, the routine has no effect.
456 *
457 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
459 * @return N/A
460 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400461extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400462
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500466 * This routine specifies how the scheduler will perform time slicing of
467 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500469 * To enable time slicing, @a slice must be non-zero. The scheduler
470 * ensures that no thread runs for more than the specified time limit
471 * before other threads of that priority are given a chance to execute.
472 * Any thread whose priority is higher than @a prio is exempted, and may
473 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500475 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400476 * execute. Once the scheduler selects a thread for execution, there is no
477 * minimum guaranteed time the thread will execute before threads of greater or
478 * equal priority are scheduled.
479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500480 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400481 * for execution, this routine has no effect; the thread is immediately
482 * rescheduled after the slice period expires.
483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * To disable timeslicing, set both @a slice and @a prio to zero.
485 *
486 * @param slice Maximum time slice length (in milliseconds).
487 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
489 * @return N/A
490 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400492
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400493/**
Allan Stephensc98da842016-11-11 15:45:03 -0500494 * @} end defgroup thread_apis
495 */
496
497/**
498 * @addtogroup isr_apis
499 * @{
500 */
501
502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500503 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400504 *
Allan Stephensc98da842016-11-11 15:45:03 -0500505 * This routine allows the caller to customize its actions, depending on
506 * whether it is a thread or an ISR.
507 *
508 * @note Can be called by ISRs.
509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500510 * @return 0 if invoked by a thread.
511 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400512 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500513extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400514
Benjamin Walsh445830d2016-11-10 15:54:27 -0500515/**
516 * @brief Determine if code is running in a preemptible thread.
517 *
Allan Stephensc98da842016-11-11 15:45:03 -0500518 * This routine allows the caller to customize its actions, depending on
519 * whether it can be preempted by another thread. The routine returns a 'true'
520 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500521 *
Allan Stephensc98da842016-11-11 15:45:03 -0500522 * - The code is running in a thread, not at ISR.
523 * - The thread's priority is in the preemptible range.
524 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500525 *
Allan Stephensc98da842016-11-11 15:45:03 -0500526 * @note Can be called by ISRs.
527 *
528 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500529 * @return Non-zero if invoked by a preemptible thread.
530 */
531extern int k_is_preempt_thread(void);
532
Allan Stephensc98da842016-11-11 15:45:03 -0500533/**
534 * @} end addtogroup isr_apis
535 */
536
537/**
538 * @addtogroup thread_apis
539 * @{
540 */
541
542/**
543 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500544 *
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * This routine prevents the current thread from being preempted by another
546 * thread by instructing the scheduler to treat it as a cooperative thread.
547 * If the thread subsequently performs an operation that makes it unready,
548 * it will be context switched out in the normal manner. When the thread
549 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500550 *
Allan Stephensc98da842016-11-11 15:45:03 -0500551 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500552 *
Allan Stephensc98da842016-11-11 15:45:03 -0500553 * @note k_sched_lock() and k_sched_unlock() should normally be used
554 * when the operation being performed can be safely interrupted by ISRs.
555 * However, if the amount of processing involved is very small, better
556 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500557 *
558 * @return N/A
559 */
560extern void k_sched_lock(void);
561
Allan Stephensc98da842016-11-11 15:45:03 -0500562/**
563 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500564 *
Allan Stephensc98da842016-11-11 15:45:03 -0500565 * This routine reverses the effect of a previous call to k_sched_lock().
566 * A thread must call the routine once for each time it called k_sched_lock()
567 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500568 *
569 * @return N/A
570 */
571extern void k_sched_unlock(void);
572
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500574 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500576 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500578 * Custom data is not used by the kernel itself, and is freely available
579 * for a thread to use as it sees fit. It can be used as a framework
580 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
584 * @return N/A
585 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400586extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400587
588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500589 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500591 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595extern void *k_thread_custom_data_get(void);
596
597/**
Allan Stephensc98da842016-11-11 15:45:03 -0500598 * @} end addtogroup thread_apis
599 */
600
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400601#include <sys_clock.h>
602
Allan Stephensc2f15a42016-11-17 12:24:22 -0500603/**
604 * @addtogroup clock_apis
605 * @{
606 */
607
608/**
609 * @brief Generate null timeout delay.
610 *
611 * This macro generates a timeout delay that that instructs a kernel API
612 * not to wait if the requested operation cannot be performed immediately.
613 *
614 * @return Timeout delay value.
615 */
616#define K_NO_WAIT 0
617
618/**
619 * @brief Generate timeout delay from milliseconds.
620 *
621 * This macro generates a timeout delay that that instructs a kernel API
622 * to wait up to @a ms milliseconds to perform the requested operation.
623 *
624 * @param ms Duration in milliseconds.
625 *
626 * @return Timeout delay value.
627 */
Johan Hedberg14471692016-11-13 10:52:15 +0200628#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500629
630/**
631 * @brief Generate timeout delay from seconds.
632 *
633 * This macro generates a timeout delay that that instructs a kernel API
634 * to wait up to @a s seconds to perform the requested operation.
635 *
636 * @param s Duration in seconds.
637 *
638 * @return Timeout delay value.
639 */
Johan Hedberg14471692016-11-13 10:52:15 +0200640#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500641
642/**
643 * @brief Generate timeout delay from minutes.
644 *
645 * This macro generates a timeout delay that that instructs a kernel API
646 * to wait up to @a m minutes to perform the requested operation.
647 *
648 * @param m Duration in minutes.
649 *
650 * @return Timeout delay value.
651 */
Johan Hedberg14471692016-11-13 10:52:15 +0200652#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500653
654/**
655 * @brief Generate timeout delay from hours.
656 *
657 * This macro generates a timeout delay that that instructs a kernel API
658 * to wait up to @a h hours to perform the requested operation.
659 *
660 * @param h Duration in hours.
661 *
662 * @return Timeout delay value.
663 */
Johan Hedberg14471692016-11-13 10:52:15 +0200664#define K_HOURS(h) K_MINUTES((h) * 60)
665
Allan Stephensc98da842016-11-11 15:45:03 -0500666/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500667 * @brief Generate infinite timeout delay.
668 *
669 * This macro generates a timeout delay that that instructs a kernel API
670 * to wait as long as necessary to perform the requested operation.
671 *
672 * @return Timeout delay value.
673 */
674#define K_FOREVER (-1)
675
676/**
677 * @} end addtogroup clock_apis
678 */
679
680/**
Allan Stephensc98da842016-11-11 15:45:03 -0500681 * @cond INTERNAL_HIDDEN
682 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400683
Benjamin Walsh62092182016-12-20 14:39:08 -0500684/* kernel clocks */
685
686#if (sys_clock_ticks_per_sec == 1000) || \
687 (sys_clock_ticks_per_sec == 500) || \
688 (sys_clock_ticks_per_sec == 250) || \
689 (sys_clock_ticks_per_sec == 125) || \
690 (sys_clock_ticks_per_sec == 100) || \
691 (sys_clock_ticks_per_sec == 50) || \
692 (sys_clock_ticks_per_sec == 25) || \
693 (sys_clock_ticks_per_sec == 20) || \
694 (sys_clock_ticks_per_sec == 10) || \
695 (sys_clock_ticks_per_sec == 1)
696
697 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
698#else
699 /* yields horrible 64-bit math on many architectures: try to avoid */
700 #define _NON_OPTIMIZED_TICKS_PER_SEC
701#endif
702
703#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
704extern int32_t _ms_to_ticks(int32_t ms);
705#else
706static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
707{
708 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
709}
710#endif
711
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500712/* added tick needed to account for tick in progress */
713#define _TICK_ALIGN 1
714
Benjamin Walsh62092182016-12-20 14:39:08 -0500715static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400716{
Benjamin Walsh62092182016-12-20 14:39:08 -0500717#ifdef CONFIG_SYS_CLOCK_EXISTS
718
719#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400720 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400721#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500722 return (uint64_t)ticks * _ms_per_tick;
723#endif
724
725#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400726 __ASSERT(ticks == 0, "");
727 return 0;
728#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400729}
730
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400731/* timeouts */
732
733struct _timeout;
734typedef void (*_timeout_func_t)(struct _timeout *t);
735
736struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500737 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400738 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400739 sys_dlist_t *wait_q;
740 int32_t delta_ticks_from_prev;
741 _timeout_func_t func;
742};
743
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200744extern int32_t _timeout_remaining_get(struct _timeout *timeout);
745
Allan Stephensc98da842016-11-11 15:45:03 -0500746/**
747 * INTERNAL_HIDDEN @endcond
748 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500749
Allan Stephensc98da842016-11-11 15:45:03 -0500750/**
751 * @cond INTERNAL_HIDDEN
752 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753
754struct k_timer {
755 /*
756 * _timeout structure must be first here if we want to use
757 * dynamic timer allocation. timeout.node is used in the double-linked
758 * list of free timers
759 */
760 struct _timeout timeout;
761
Allan Stephens45bfa372016-10-12 12:39:42 -0500762 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763 _wait_q_t wait_q;
764
765 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500766 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400767
768 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500769 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400770
771 /* timer period */
772 int32_t period;
773
Allan Stephens45bfa372016-10-12 12:39:42 -0500774 /* timer status */
775 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400776
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500777 /* user-specific data, also used to support legacy features */
778 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400779
Anas Nashif2f203c22016-12-18 06:57:45 -0500780 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400781};
782
Allan Stephens1342adb2016-11-03 13:54:53 -0500783#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400784 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500785 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500786 .timeout.wait_q = NULL, \
787 .timeout.thread = NULL, \
788 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400789 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500790 .expiry_fn = expiry, \
791 .stop_fn = stop, \
792 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500793 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500794 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400795 }
796
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797/**
Allan Stephensc98da842016-11-11 15:45:03 -0500798 * INTERNAL_HIDDEN @endcond
799 */
800
801/**
802 * @defgroup timer_apis Timer APIs
803 * @ingroup kernel_apis
804 * @{
805 */
806
807/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500808 * @typedef k_timer_expiry_t
809 * @brief Timer expiry function type.
810 *
811 * A timer's expiry function is executed by the system clock interrupt handler
812 * each time the timer expires. The expiry function is optional, and is only
813 * invoked if the timer has been initialized with one.
814 *
815 * @param timer Address of timer.
816 *
817 * @return N/A
818 */
819typedef void (*k_timer_expiry_t)(struct k_timer *timer);
820
821/**
822 * @typedef k_timer_stop_t
823 * @brief Timer stop function type.
824 *
825 * A timer's stop function is executed if the timer is stopped prematurely.
826 * The function runs in the context of the thread that stops the timer.
827 * The stop function is optional, and is only invoked if the timer has been
828 * initialized with one.
829 *
830 * @param timer Address of timer.
831 *
832 * @return N/A
833 */
834typedef void (*k_timer_stop_t)(struct k_timer *timer);
835
836/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500841 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
843 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @param expiry_fn Function to invoke each time the timer expires.
845 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500847#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500848 struct k_timer name \
849 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500850 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400851
Allan Stephens45bfa372016-10-12 12:39:42 -0500852/**
853 * @brief Initialize a timer.
854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500856 *
857 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @param expiry_fn Function to invoke each time the timer expires.
859 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500860 *
861 * @return N/A
862 */
863extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500864 k_timer_expiry_t expiry_fn,
865 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700866
Allan Stephens45bfa372016-10-12 12:39:42 -0500867/**
868 * @brief Start a timer.
869 *
870 * This routine starts a timer, and resets its status to zero. The timer
871 * begins counting down using the specified duration and period values.
872 *
873 * Attempting to start a timer that is already running is permitted.
874 * The timer's status is reset to zero and the timer begins counting down
875 * using the new duration and period values.
876 *
877 * @param timer Address of timer.
878 * @param duration Initial timer duration (in milliseconds).
879 * @param period Timer period (in milliseconds).
880 *
881 * @return N/A
882 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400883extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500884 int32_t duration, int32_t period);
885
886/**
887 * @brief Stop a timer.
888 *
889 * This routine stops a running timer prematurely. The timer's stop function,
890 * if one exists, is invoked by the caller.
891 *
892 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500894 *
895 * @param timer Address of timer.
896 *
897 * @return N/A
898 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400899extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500900
901/**
902 * @brief Read timer status.
903 *
904 * This routine reads the timer's status, which indicates the number of times
905 * it has expired since its status was last read.
906 *
907 * Calling this routine resets the timer's status to zero.
908 *
909 * @param timer Address of timer.
910 *
911 * @return Timer status.
912 */
913extern uint32_t k_timer_status_get(struct k_timer *timer);
914
915/**
916 * @brief Synchronize thread to timer expiration.
917 *
918 * This routine blocks the calling thread until the timer's status is non-zero
919 * (indicating that it has expired at least once since it was last examined)
920 * or the timer is stopped. If the timer status is already non-zero,
921 * or the timer is already stopped, the caller continues without waiting.
922 *
923 * Calling this routine resets the timer's status to zero.
924 *
925 * This routine must not be used by interrupt handlers, since they are not
926 * allowed to block.
927 *
928 * @param timer Address of timer.
929 *
930 * @return Timer status.
931 */
932extern uint32_t k_timer_status_sync(struct k_timer *timer);
933
934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500936 *
937 * This routine computes the (approximate) time remaining before a running
938 * timer next expires. If the timer is not running, it returns zero.
939 *
940 * @param timer Address of timer.
941 *
942 * @return Remaining time (in milliseconds).
943 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200944static inline int32_t k_timer_remaining_get(struct k_timer *timer)
945{
946 return _timeout_remaining_get(&timer->timeout);
947}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400948
Allan Stephensc98da842016-11-11 15:45:03 -0500949/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500950 * @brief Associate user-specific data with a timer.
951 *
952 * This routine records the @a user_data with the @a timer, to be retrieved
953 * later.
954 *
955 * It can be used e.g. in a timer handler shared across multiple subsystems to
956 * retrieve data specific to the subsystem this timer is associated with.
957 *
958 * @param timer Address of timer.
959 * @param user_data User data to associate with the timer.
960 *
961 * @return N/A
962 */
963static inline void k_timer_user_data_set(struct k_timer *timer,
964 void *user_data)
965{
966 timer->user_data = user_data;
967}
968
969/**
970 * @brief Retrieve the user-specific data from a timer.
971 *
972 * @param timer Address of timer.
973 *
974 * @return The user data.
975 */
976static inline void *k_timer_user_data_get(struct k_timer *timer)
977{
978 return timer->user_data;
979}
980
981/**
Allan Stephensc98da842016-11-11 15:45:03 -0500982 * @} end defgroup timer_apis
983 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400984
Allan Stephensc98da842016-11-11 15:45:03 -0500985/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500986 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500987 * @{
988 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500989
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine returns the elapsed time since the system booted,
994 * in milliseconds.
995 *
996 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400997 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400998extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400999
1000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * This routine returns the lower 32-bits of the elapsed time since the system
1004 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * This routine can be more efficient than k_uptime_get(), as it reduces the
1007 * need for interrupt locking and 64-bit math. However, the 32-bit result
1008 * cannot hold a system uptime time larger than approximately 50 days, so the
1009 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001012 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001013extern uint32_t k_uptime_get_32(void);
1014
1015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * This routine computes the elapsed time between the current system uptime
1019 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * @param reftime Pointer to a reference time, which is updated to the current
1022 * uptime upon return.
1023 *
1024 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001025 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001026extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001027
1028/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001031 * This routine computes the elapsed time between the current system uptime
1032 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1035 * need for interrupt locking and 64-bit math. However, the 32-bit result
1036 * cannot hold an elapsed time larger than approximately 50 days, so the
1037 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * @param reftime Pointer to a reference time, which is updated to the current
1040 * uptime upon return.
1041 *
1042 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001043 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001044extern uint32_t k_uptime_delta_32(int64_t *reftime);
1045
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * This routine returns the current time, as measured by the system's hardware
1050 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001052 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001054extern uint32_t k_cycle_get_32(void);
1055
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001056/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001057 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001058 */
1059
Allan Stephensc98da842016-11-11 15:45:03 -05001060/**
1061 * @cond INTERNAL_HIDDEN
1062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063
1064struct k_fifo {
1065 _wait_q_t wait_q;
1066 sys_slist_t data_q;
1067
Anas Nashif2f203c22016-12-18 06:57:45 -05001068 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069};
1070
Allan Stephensc98da842016-11-11 15:45:03 -05001071#define K_FIFO_INITIALIZER(obj) \
1072 { \
1073 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1074 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001075 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001076 }
1077
1078/**
1079 * INTERNAL_HIDDEN @endcond
1080 */
1081
1082/**
1083 * @defgroup fifo_apis Fifo APIs
1084 * @ingroup kernel_apis
1085 * @{
1086 */
1087
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001091 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
1095 * @return N/A
1096 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098
1099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * This routine adds a data item to @a fifo. A fifo data item must be
1103 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1104 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * @note Can be called by ISRs.
1107 *
1108 * @param fifo Address of the fifo.
1109 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001110 *
1111 * @return N/A
1112 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001113extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114
1115/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * This routine adds a list of data items to @a fifo in one operation.
1119 * The data items must be in a singly-linked list, with the first 32 bits
1120 * each data item pointing to the next data item; the list must be
1121 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @param fifo Address of the fifo.
1126 * @param head Pointer to first node in singly-linked list.
1127 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001128 *
1129 * @return N/A
1130 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132
1133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * This routine adds a list of data items to @a fifo in one operation.
1137 * The data items must be in a singly-linked list implemented using a
1138 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 * and must be re-initialized via sys_slist_init().
1140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001141 * @note Can be called by ISRs.
1142 *
1143 * @param fifo Address of the fifo.
1144 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
1146 * @return N/A
1147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001148extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001149
1150/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * This routine removes a data item from @a fifo in a "first in, first out"
1154 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1157 *
1158 * @param fifo Address of the fifo.
1159 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 * or one of the special values K_NO_WAIT and K_FOREVER.
1161 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001162 * @return Address of the data item if successful; NULL if returned
1163 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1166
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001171 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001172 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001177 struct k_fifo name \
1178 __in_section(_k_fifo, static, name) = \
1179 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001180
Allan Stephensc98da842016-11-11 15:45:03 -05001181/**
1182 * @} end defgroup fifo_apis
1183 */
1184
1185/**
1186 * @cond INTERNAL_HIDDEN
1187 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188
1189struct k_lifo {
1190 _wait_q_t wait_q;
1191 void *list;
1192
Anas Nashif2f203c22016-12-18 06:57:45 -05001193 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001194};
1195
Allan Stephensc98da842016-11-11 15:45:03 -05001196#define K_LIFO_INITIALIZER(obj) \
1197 { \
1198 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1199 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001200 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001201 }
1202
1203/**
1204 * INTERNAL_HIDDEN @endcond
1205 */
1206
1207/**
1208 * @defgroup lifo_apis Lifo APIs
1209 * @ingroup kernel_apis
1210 * @{
1211 */
1212
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001214 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001216 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001219 *
1220 * @return N/A
1221 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001222extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223
1224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001227 * This routine adds a data item to @a lifo. A lifo data item must be
1228 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1229 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @note Can be called by ISRs.
1232 *
1233 * @param lifo Address of the lifo.
1234 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
1236 * @return N/A
1237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001238extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239
1240/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001243 * This routine removes a data item from @a lifo in a "last in, first out"
1244 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1247 *
1248 * @param lifo Address of the lifo.
1249 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 * or one of the special values K_NO_WAIT and K_FOREVER.
1251 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001252 * @return Address of the data item if successful; NULL if returned
1253 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001254 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001255extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1256
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001257/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001258 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001261 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001262 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001264 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001265 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001266#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001267 struct k_lifo name \
1268 __in_section(_k_lifo, static, name) = \
1269 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001270
Allan Stephensc98da842016-11-11 15:45:03 -05001271/**
1272 * @} end defgroup lifo_apis
1273 */
1274
1275/**
1276 * @cond INTERNAL_HIDDEN
1277 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278
1279struct k_stack {
1280 _wait_q_t wait_q;
1281 uint32_t *base, *next, *top;
1282
Anas Nashif2f203c22016-12-18 06:57:45 -05001283 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284};
1285
Allan Stephensc98da842016-11-11 15:45:03 -05001286#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1287 { \
1288 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1289 .base = stack_buffer, \
1290 .next = stack_buffer, \
1291 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001292 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001293 }
1294
1295/**
1296 * INTERNAL_HIDDEN @endcond
1297 */
1298
1299/**
1300 * @defgroup stack_apis Stack APIs
1301 * @ingroup kernel_apis
1302 * @{
1303 */
1304
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305/**
1306 * @brief Initialize a stack.
1307 *
1308 * This routine initializes a stack object, prior to its first use.
1309 *
1310 * @param stack Address of the stack.
1311 * @param buffer Address of array used to hold stacked values.
1312 * @param num_entries Maximum number of values that can be stacked.
1313 *
1314 * @return N/A
1315 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001316extern void k_stack_init(struct k_stack *stack,
1317 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001318
1319/**
1320 * @brief Push an element onto a stack.
1321 *
1322 * This routine adds a 32-bit value @a data to @a stack.
1323 *
1324 * @note Can be called by ISRs.
1325 *
1326 * @param stack Address of the stack.
1327 * @param data Value to push onto the stack.
1328 *
1329 * @return N/A
1330 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001332
1333/**
1334 * @brief Pop an element from a stack.
1335 *
1336 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1337 * manner and stores the value in @a data.
1338 *
1339 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1340 *
1341 * @param stack Address of the stack.
1342 * @param data Address of area to hold the value popped from the stack.
1343 * @param timeout Waiting period to obtain a value (in milliseconds),
1344 * or one of the special values K_NO_WAIT and K_FOREVER.
1345 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001346 * @retval 0 Element popped from stack.
1347 * @retval -EBUSY Returned without waiting.
1348 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001350extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1351
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001355 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001356 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001357 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * @param name Name of the stack.
1360 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001361 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001362#define K_STACK_DEFINE(name, stack_num_entries) \
1363 uint32_t __noinit \
1364 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001365 struct k_stack name \
1366 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001367 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1368 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001369
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370/**
Allan Stephensc98da842016-11-11 15:45:03 -05001371 * @} end defgroup stack_apis
1372 */
1373
Allan Stephens6bba9b02016-11-16 14:56:54 -05001374struct k_work;
1375
Allan Stephensc98da842016-11-11 15:45:03 -05001376/**
1377 * @defgroup workqueue_apis Workqueue Thread APIs
1378 * @ingroup kernel_apis
1379 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380 */
1381
Allan Stephens6bba9b02016-11-16 14:56:54 -05001382/**
1383 * @typedef k_work_handler_t
1384 * @brief Work item handler function type.
1385 *
1386 * A work item's handler function is executed by a workqueue's thread
1387 * when the work item is processed by the workqueue.
1388 *
1389 * @param work Address of the work item.
1390 *
1391 * @return N/A
1392 */
1393typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394
1395/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001396 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001398
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399struct k_work_q {
1400 struct k_fifo fifo;
1401};
1402
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001404 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405};
1406
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407struct k_work {
1408 void *_reserved; /* Used by k_fifo implementation. */
1409 k_work_handler_t handler;
1410 atomic_t flags[1];
1411};
1412
Allan Stephens6bba9b02016-11-16 14:56:54 -05001413struct k_delayed_work {
1414 struct k_work work;
1415 struct _timeout timeout;
1416 struct k_work_q *work_q;
1417};
1418
1419extern struct k_work_q k_sys_work_q;
1420
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001421/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001422 * INTERNAL_HIDDEN @endcond
1423 */
1424
1425/**
1426 * @brief Initialize a statically-defined work item.
1427 *
1428 * This macro can be used to initialize a statically-defined workqueue work
1429 * item, prior to its first use. For example,
1430 *
1431 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1432 *
1433 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 */
1435#define K_WORK_INITIALIZER(work_handler) \
1436 { \
1437 ._reserved = NULL, \
1438 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001439 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440 }
1441
1442/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001443 * @brief Initialize a work item.
1444 *
1445 * This routine initializes a workqueue work item, prior to its first use.
1446 *
1447 * @param work Address of work item.
1448 * @param handler Function to invoke each time work item is processed.
1449 *
1450 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001451 */
1452static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1453{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001454 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455 work->handler = handler;
1456}
1457
1458/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001459 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001460 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001461 * This routine submits work item @a work to be processed by workqueue
1462 * @a work_q. If the work item is already pending in the workqueue's queue
1463 * as a result of an earlier submission, this routine has no effect on the
1464 * work item. If the work item has already been processed, or is currently
1465 * being processed, its work is considered complete and the work item can be
1466 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001467 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001468 * @warning
1469 * A submitted work item must not be modified until it has been processed
1470 * by the workqueue.
1471 *
1472 * @note Can be called by ISRs.
1473 *
1474 * @param work_q Address of workqueue.
1475 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001476 *
1477 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001478 */
1479static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1480 struct k_work *work)
1481{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001482 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001483 k_fifo_put(&work_q->fifo, work);
1484 }
1485}
1486
1487/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001488 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001490 * This routine indicates if work item @a work is pending in a workqueue's
1491 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001493 * @note Can be called by ISRs.
1494 *
1495 * @param work Address of work item.
1496 *
1497 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001498 */
1499static inline int k_work_pending(struct k_work *work)
1500{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001501 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001502}
1503
1504/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001505 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001506 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001507 * This routine starts workqueue @a work_q. The workqueue spawns its work
1508 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001509 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001510 * @param work_q Address of workqueue.
1511 * @param stack Pointer to work queue thread's stack space.
1512 * @param stack_size Size of the work queue thread's stack (in bytes).
1513 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001514 *
1515 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001516 */
Allan Stephens904cf972016-10-07 13:59:23 -05001517extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001518 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001520/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001521 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001522 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001523 * This routine initializes a workqueue delayed work item, prior to
1524 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001526 * @param work Address of delayed work item.
1527 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001528 *
1529 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001531extern void k_delayed_work_init(struct k_delayed_work *work,
1532 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533
1534/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001535 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001537 * This routine schedules work item @a work to be processed by workqueue
1538 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1539 * an asychronous countdown for the work item and then returns to the caller.
1540 * Only when the countdown completes is the work item actually submitted to
1541 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001543 * Submitting a previously submitted delayed work item that is still
1544 * counting down cancels the existing submission and restarts the countdown
1545 * using the new delay. If the work item is currently pending on the
1546 * workqueue's queue because the countdown has completed it is too late to
1547 * resubmit the item, and resubmission fails without impacting the work item.
1548 * If the work item has already been processed, or is currently being processed,
1549 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001550 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001551 * @warning
1552 * A delayed work item must not be modified until it has been processed
1553 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001554 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001555 * @note Can be called by ISRs.
1556 *
1557 * @param work_q Address of workqueue.
1558 * @param work Address of delayed work item.
1559 * @param delay Delay before submitting the work item (in milliseconds).
1560 *
1561 * @retval 0 Work item countdown started.
1562 * @retval -EINPROGRESS Work item is already pending.
1563 * @retval -EINVAL Work item is being processed or has completed its work.
1564 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001565 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001566extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1567 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001568 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001569
1570/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001571 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001573 * This routine cancels the submission of delayed work item @a work.
1574 * A delayed work item can only be cancelled while its countdown is still
1575 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001576 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001577 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001578 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001579 * @param work Address of delayed work item.
1580 *
1581 * @retval 0 Work item countdown cancelled.
1582 * @retval -EINPROGRESS Work item is already pending.
1583 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001584 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001585extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001586
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001587/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001588 * @brief Submit a work item to the system workqueue.
1589 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001590 * This routine submits work item @a work to be processed by the system
1591 * workqueue. If the work item is already pending in the workqueue's queue
1592 * as a result of an earlier submission, this routine has no effect on the
1593 * work item. If the work item has already been processed, or is currently
1594 * being processed, its work is considered complete and the work item can be
1595 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001596 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001597 * @warning
1598 * Work items submitted to the system workqueue should avoid using handlers
1599 * that block or yield since this may prevent the system workqueue from
1600 * processing other work items in a timely manner.
1601 *
1602 * @note Can be called by ISRs.
1603 *
1604 * @param work Address of work item.
1605 *
1606 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001607 */
1608static inline void k_work_submit(struct k_work *work)
1609{
1610 k_work_submit_to_queue(&k_sys_work_q, work);
1611}
1612
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001613/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001614 * @brief Submit a delayed work item to the system workqueue.
1615 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001616 * This routine schedules work item @a work to be processed by the system
1617 * workqueue after a delay of @a delay milliseconds. The routine initiates
1618 * an asychronous countdown for the work item and then returns to the caller.
1619 * Only when the countdown completes is the work item actually submitted to
1620 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001622 * Submitting a previously submitted delayed work item that is still
1623 * counting down cancels the existing submission and restarts the countdown
1624 * using the new delay. If the work item is currently pending on the
1625 * workqueue's queue because the countdown has completed it is too late to
1626 * resubmit the item, and resubmission fails without impacting the work item.
1627 * If the work item has already been processed, or is currently being processed,
1628 * its work is considered complete and the work item can be resubmitted.
1629 *
1630 * @warning
1631 * Work items submitted to the system workqueue should avoid using handlers
1632 * that block or yield since this may prevent the system workqueue from
1633 * processing other work items in a timely manner.
1634 *
1635 * @note Can be called by ISRs.
1636 *
1637 * @param work Address of delayed work item.
1638 * @param delay Delay before submitting the work item (in milliseconds).
1639 *
1640 * @retval 0 Work item countdown started.
1641 * @retval -EINPROGRESS Work item is already pending.
1642 * @retval -EINVAL Work item is being processed or has completed its work.
1643 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001644 */
1645static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001646 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001648 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001649}
1650
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001652 * @brief Get time remaining before a delayed work gets scheduled.
1653 *
1654 * This routine computes the (approximate) time remaining before a
1655 * delayed work gets executed. If the delayed work is not waiting to be
1656 * schedules, it returns zero.
1657 *
1658 * @param work Delayed work item.
1659 *
1660 * @return Remaining time (in milliseconds).
1661 */
1662static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1663{
1664 return _timeout_remaining_get(&work->timeout);
1665}
1666
1667/**
Allan Stephensc98da842016-11-11 15:45:03 -05001668 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001669 */
1670
Allan Stephensc98da842016-11-11 15:45:03 -05001671/**
1672 * @cond INTERNAL_HIDDEN
1673 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001674
1675struct k_mutex {
1676 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001677 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001678 uint32_t lock_count;
1679 int owner_orig_prio;
1680#ifdef CONFIG_OBJECT_MONITOR
1681 int num_lock_state_changes;
1682 int num_conflicts;
1683#endif
1684
Anas Nashif2f203c22016-12-18 06:57:45 -05001685 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001686};
1687
1688#ifdef CONFIG_OBJECT_MONITOR
1689#define _MUTEX_INIT_OBJECT_MONITOR \
1690 .num_lock_state_changes = 0, .num_conflicts = 0,
1691#else
1692#define _MUTEX_INIT_OBJECT_MONITOR
1693#endif
1694
1695#define K_MUTEX_INITIALIZER(obj) \
1696 { \
1697 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1698 .owner = NULL, \
1699 .lock_count = 0, \
1700 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1701 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001702 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001703 }
1704
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705/**
Allan Stephensc98da842016-11-11 15:45:03 -05001706 * INTERNAL_HIDDEN @endcond
1707 */
1708
1709/**
1710 * @defgroup mutex_apis Mutex APIs
1711 * @ingroup kernel_apis
1712 * @{
1713 */
1714
1715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001720 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001725 struct k_mutex name \
1726 __in_section(_k_mutex, static, name) = \
1727 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * Upon completion, the mutex is available and does not have an owner.
1735 *
1736 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 *
1738 * @return N/A
1739 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741
1742/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001745 * This routine locks @a mutex. If the mutex is locked by another thread,
1746 * the calling thread waits until the mutex becomes available or until
1747 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * A thread is permitted to lock a mutex it has already locked. The operation
1750 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @param mutex Address of the mutex.
1753 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001754 * or one of the special values K_NO_WAIT and K_FOREVER.
1755 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001756 * @retval 0 Mutex locked.
1757 * @retval -EBUSY Returned without waiting.
1758 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761
1762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001765 * This routine unlocks @a mutex. The mutex must already be locked by the
1766 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 *
1768 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770 * thread.
1771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773 *
1774 * @return N/A
1775 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001776extern void k_mutex_unlock(struct k_mutex *mutex);
1777
Allan Stephensc98da842016-11-11 15:45:03 -05001778/**
1779 * @} end defgroup mutex_apis
1780 */
1781
1782/**
1783 * @cond INTERNAL_HIDDEN
1784 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785
1786struct k_sem {
1787 _wait_q_t wait_q;
1788 unsigned int count;
1789 unsigned int limit;
1790
Anas Nashif2f203c22016-12-18 06:57:45 -05001791 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001792};
1793
Allan Stephensc98da842016-11-11 15:45:03 -05001794#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1795 { \
1796 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1797 .count = initial_count, \
1798 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001799 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001800 }
1801
1802/**
1803 * INTERNAL_HIDDEN @endcond
1804 */
1805
1806/**
1807 * @defgroup semaphore_apis Semaphore APIs
1808 * @ingroup kernel_apis
1809 * @{
1810 */
1811
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001813 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * @param sem Address of the semaphore.
1818 * @param initial_count Initial semaphore count.
1819 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001820 *
1821 * @return N/A
1822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1824 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001825
1826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001827 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001829 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1832 *
1833 * @param sem Address of the semaphore.
1834 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001835 * or one of the special values K_NO_WAIT and K_FOREVER.
1836 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001837 * @note When porting code from the nanokernel legacy API to the new API, be
1838 * careful with the return value of this function. The return value is the
1839 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1840 * non-zero means failure, while the nano_sem_take family returns 1 for success
1841 * and 0 for failure.
1842 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001843 * @retval 0 Semaphore taken.
1844 * @retval -EBUSY Returned without waiting.
1845 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001846 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001847extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001848
1849/**
1850 * @brief Give a semaphore.
1851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * This routine gives @a sem, unless the semaphore is already at its maximum
1853 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001855 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001858 *
1859 * @return N/A
1860 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001861extern void k_sem_give(struct k_sem *sem);
1862
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001863/**
1864 * @brief Reset a semaphore's count to zero.
1865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001866 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001869 *
1870 * @return N/A
1871 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001872static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001873{
1874 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001875}
1876
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001877/**
1878 * @brief Get a semaphore's count.
1879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001885 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001886static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887{
1888 return sem->count;
1889}
1890
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001895 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001896 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001898 * @param name Name of the semaphore.
1899 * @param initial_count Initial semaphore count.
1900 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001901 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001903 struct k_sem name \
1904 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 K_SEM_INITIALIZER(name, initial_count, count_limit)
1906
Allan Stephensc98da842016-11-11 15:45:03 -05001907/**
1908 * @} end defgroup semaphore_apis
1909 */
1910
1911/**
1912 * @defgroup alert_apis Alert APIs
1913 * @ingroup kernel_apis
1914 * @{
1915 */
1916
Allan Stephens5eceb852016-11-16 10:16:30 -05001917/**
1918 * @typedef k_alert_handler_t
1919 * @brief Alert handler function type.
1920 *
1921 * An alert's alert handler function is invoked by the system workqueue
1922 * when the alert is signalled. The alert handler function is optional,
1923 * and is only invoked if the alert has been initialized with one.
1924 *
1925 * @param alert Address of the alert.
1926 *
1927 * @return 0 if alert has been consumed; non-zero if alert should pend.
1928 */
1929typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001930
1931/**
1932 * @} end defgroup alert_apis
1933 */
1934
1935/**
1936 * @cond INTERNAL_HIDDEN
1937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001939#define K_ALERT_DEFAULT NULL
1940#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001941
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001942struct k_alert {
1943 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944 atomic_t send_count;
1945 struct k_work work_item;
1946 struct k_sem sem;
1947
Anas Nashif2f203c22016-12-18 06:57:45 -05001948 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949};
1950
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001951extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001953#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001955 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001957 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001958 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001959 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960 }
1961
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001962/**
Allan Stephensc98da842016-11-11 15:45:03 -05001963 * INTERNAL_HIDDEN @endcond
1964 */
1965
1966/**
1967 * @addtogroup alert_apis
1968 * @{
1969 */
1970
1971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001974 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001976 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 * @param name Name of the alert.
1979 * @param alert_handler Action to take when alert is sent. Specify either
1980 * the address of a function to be invoked by the system workqueue
1981 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1982 * K_ALERT_DEFAULT (which causes the alert to pend).
1983 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001985#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001986 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001987 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001988 K_ALERT_INITIALIZER(name, alert_handler, \
1989 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001992 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001994 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001996 * @param alert Address of the alert.
1997 * @param handler Action to take when alert is sent. Specify either the address
1998 * of a function to be invoked by the system workqueue thread,
1999 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2000 * K_ALERT_DEFAULT (which causes the alert to pend).
2001 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
2003 * @return N/A
2004 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002005extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2006 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007
2008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002011 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002013 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2014 *
2015 * @param alert Address of the alert.
2016 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 * or one of the special values K_NO_WAIT and K_FOREVER.
2018 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002019 * @retval 0 Alert received.
2020 * @retval -EBUSY Returned without waiting.
2021 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002023extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024
2025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002028 * This routine signals @a alert. The action specified for @a alert will
2029 * be taken, which may trigger the execution of an alert handler function
2030 * and/or cause the alert to pend (assuming the alert has not reached its
2031 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 * @note Can be called by ISRs.
2034 *
2035 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
2037 * @return N/A
2038 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002039extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002040
2041/**
Allan Stephensc98da842016-11-11 15:45:03 -05002042 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043 */
2044
Allan Stephensc98da842016-11-11 15:45:03 -05002045/**
2046 * @cond INTERNAL_HIDDEN
2047 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048
2049struct k_msgq {
2050 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002051 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002052 uint32_t max_msgs;
2053 char *buffer_start;
2054 char *buffer_end;
2055 char *read_ptr;
2056 char *write_ptr;
2057 uint32_t used_msgs;
2058
Anas Nashif2f203c22016-12-18 06:57:45 -05002059 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060};
2061
Peter Mitsis1da807e2016-10-06 11:36:59 -04002062#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063 { \
2064 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002065 .max_msgs = q_max_msgs, \
2066 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002067 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002068 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069 .read_ptr = q_buffer, \
2070 .write_ptr = q_buffer, \
2071 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002072 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002073 }
2074
Peter Mitsis1da807e2016-10-06 11:36:59 -04002075/**
Allan Stephensc98da842016-11-11 15:45:03 -05002076 * INTERNAL_HIDDEN @endcond
2077 */
2078
2079/**
2080 * @defgroup msgq_apis Message Queue APIs
2081 * @ingroup kernel_apis
2082 * @{
2083 */
2084
2085/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002086 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2089 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002090 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2091 * message is similarly aligned to this boundary, @a q_msg_size must also be
2092 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * The message queue can be accessed outside the module where it is defined
2095 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002096 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002097 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @param q_name Name of the message queue.
2100 * @param q_msg_size Message size (in bytes).
2101 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002102 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002103 */
2104#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2105 static char __noinit __aligned(q_align) \
2106 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002107 struct k_msgq q_name \
2108 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002109 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2110 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002111
Peter Mitsisd7a37502016-10-13 11:37:40 -04002112/**
2113 * @brief Initialize a message queue.
2114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * This routine initializes a message queue object, prior to its first use.
2116 *
Allan Stephensda827222016-11-09 14:23:58 -06002117 * The message queue's ring buffer must contain space for @a max_msgs messages,
2118 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2119 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2120 * that each message is similarly aligned to this boundary, @a q_msg_size
2121 * must also be a multiple of N.
2122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * @param q Address of the message queue.
2124 * @param buffer Pointer to ring buffer that holds queued messages.
2125 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002126 * @param max_msgs Maximum number of messages that can be queued.
2127 *
2128 * @return N/A
2129 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002130extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002131 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002132
2133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002134 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002136 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002137 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002138 * @note Can be called by ISRs.
2139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * @param q Address of the message queue.
2141 * @param data Pointer to the message.
2142 * @param timeout Waiting period to add the message (in milliseconds),
2143 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002144 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002145 * @retval 0 Message sent.
2146 * @retval -ENOMSG Returned without waiting or queue purged.
2147 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002148 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002150
2151/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002152 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002154 * This routine receives a message from message queue @a q in a "first in,
2155 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002156 *
Allan Stephensc98da842016-11-11 15:45:03 -05002157 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * @param q Address of the message queue.
2160 * @param data Address of area to hold the received message.
2161 * @param timeout Waiting period to receive the message (in milliseconds),
2162 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002163 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002164 * @retval 0 Message received.
2165 * @retval -ENOMSG Returned without waiting.
2166 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002167 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002168extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002169
2170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002171 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002173 * This routine discards all unreceived messages in a message queue's ring
2174 * buffer. Any threads that are blocked waiting to send a message to the
2175 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002177 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002178 *
2179 * @return N/A
2180 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181extern void k_msgq_purge(struct k_msgq *q);
2182
Peter Mitsis67be2492016-10-07 11:44:34 -04002183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * This routine returns the number of unused entries in a message queue's
2187 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002189 * @param q Address of the message queue.
2190 *
2191 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002192 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002193static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002194{
2195 return q->max_msgs - q->used_msgs;
2196}
2197
Peter Mitsisd7a37502016-10-13 11:37:40 -04002198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002201 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002203 * @param q Address of the message queue.
2204 *
2205 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002206 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002207static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208{
2209 return q->used_msgs;
2210}
2211
Allan Stephensc98da842016-11-11 15:45:03 -05002212/**
2213 * @} end defgroup msgq_apis
2214 */
2215
2216/**
2217 * @defgroup mem_pool_apis Memory Pool APIs
2218 * @ingroup kernel_apis
2219 * @{
2220 */
2221
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002223 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224 void *addr_in_pool;
2225 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002226 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227};
2228
Allan Stephensc98da842016-11-11 15:45:03 -05002229/**
2230 * @} end defgroup mem_pool_apis
2231 */
2232
2233/**
2234 * @defgroup mailbox_apis Mailbox APIs
2235 * @ingroup kernel_apis
2236 * @{
2237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238
2239struct k_mbox_msg {
2240 /** internal use only - needed for legacy API support */
2241 uint32_t _mailbox;
2242 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002243 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 /** application-defined information value */
2245 uint32_t info;
2246 /** sender's message data buffer */
2247 void *tx_data;
2248 /** internal use only - needed for legacy API support */
2249 void *_rx_data;
2250 /** message data block descriptor */
2251 struct k_mem_block tx_block;
2252 /** source thread id */
2253 k_tid_t rx_source_thread;
2254 /** target thread id */
2255 k_tid_t tx_target_thread;
2256 /** internal use only - thread waiting on send (may be a dummy) */
2257 k_tid_t _syncing_thread;
2258#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2259 /** internal use only - semaphore used during asynchronous send */
2260 struct k_sem *_async_sem;
2261#endif
2262};
2263
Allan Stephensc98da842016-11-11 15:45:03 -05002264/**
2265 * @cond INTERNAL_HIDDEN
2266 */
2267
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268struct k_mbox {
2269 _wait_q_t tx_msg_queue;
2270 _wait_q_t rx_msg_queue;
2271
Anas Nashif2f203c22016-12-18 06:57:45 -05002272 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273};
2274
2275#define K_MBOX_INITIALIZER(obj) \
2276 { \
2277 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2278 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002279 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280 }
2281
Peter Mitsis12092702016-10-14 12:57:23 -04002282/**
Allan Stephensc98da842016-11-11 15:45:03 -05002283 * INTERNAL_HIDDEN @endcond
2284 */
2285
2286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002289 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002291 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002294 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002296 struct k_mbox name \
2297 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298 K_MBOX_INITIALIZER(name) \
2299
Peter Mitsis12092702016-10-14 12:57:23 -04002300/**
2301 * @brief Initialize a mailbox.
2302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303 * This routine initializes a mailbox object, prior to its first use.
2304 *
2305 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002306 *
2307 * @return N/A
2308 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309extern void k_mbox_init(struct k_mbox *mbox);
2310
Peter Mitsis12092702016-10-14 12:57:23 -04002311/**
2312 * @brief Send a mailbox message in a synchronous manner.
2313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * This routine sends a message to @a mbox and waits for a receiver to both
2315 * receive and process it. The message data may be in a buffer, in a memory
2316 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * @param mbox Address of the mailbox.
2319 * @param tx_msg Address of the transmit message descriptor.
2320 * @param timeout Waiting period for the message to be received (in
2321 * milliseconds), or one of the special values K_NO_WAIT
2322 * and K_FOREVER. Once the message has been received,
2323 * this routine waits as long as necessary for the message
2324 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002325 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002326 * @retval 0 Message sent.
2327 * @retval -ENOMSG Returned without waiting.
2328 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002329 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002330extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002332
Peter Mitsis12092702016-10-14 12:57:23 -04002333/**
2334 * @brief Send a mailbox message in an asynchronous manner.
2335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * This routine sends a message to @a mbox without waiting for a receiver
2337 * to process it. The message data may be in a buffer, in a memory pool block,
2338 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2339 * will be given when the message has been both received and completely
2340 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @param mbox Address of the mailbox.
2343 * @param tx_msg Address of the transmit message descriptor.
2344 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002345 *
2346 * @return N/A
2347 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002348extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 struct k_sem *sem);
2350
Peter Mitsis12092702016-10-14 12:57:23 -04002351/**
2352 * @brief Receive a mailbox message.
2353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * This routine receives a message from @a mbox, then optionally retrieves
2355 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002357 * @param mbox Address of the mailbox.
2358 * @param rx_msg Address of the receive message descriptor.
2359 * @param buffer Address of the buffer to receive data, or NULL to defer data
2360 * retrieval and message disposal until later.
2361 * @param timeout Waiting period for a message to be received (in
2362 * milliseconds), or one of the special values K_NO_WAIT
2363 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002364 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002365 * @retval 0 Message received.
2366 * @retval -ENOMSG Returned without waiting.
2367 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002368 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002369extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002371
2372/**
2373 * @brief Retrieve mailbox message data into a buffer.
2374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * This routine completes the processing of a received message by retrieving
2376 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002377 *
2378 * Alternatively, this routine can be used to dispose of a received message
2379 * without retrieving its data.
2380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * @param rx_msg Address of the receive message descriptor.
2382 * @param buffer Address of the buffer to receive data, or NULL to discard
2383 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002384 *
2385 * @return N/A
2386 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002387extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002388
2389/**
2390 * @brief Retrieve mailbox message data into a memory pool block.
2391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 * This routine completes the processing of a received message by retrieving
2393 * its data into a memory pool block, then disposing of the message.
2394 * The memory pool block that results from successful retrieval must be
2395 * returned to the pool once the data has been processed, even in cases
2396 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002397 *
2398 * Alternatively, this routine can be used to dispose of a received message
2399 * without retrieving its data. In this case there is no need to return a
2400 * memory pool block to the pool.
2401 *
2402 * This routine allocates a new memory pool block for the data only if the
2403 * data is not already in one. If a new block cannot be allocated, the routine
2404 * returns a failure code and the received message is left unchanged. This
2405 * permits the caller to reattempt data retrieval at a later time or to dispose
2406 * of the received message without retrieving its data.
2407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * @param rx_msg Address of a receive message descriptor.
2409 * @param pool Address of memory pool, or NULL to discard data.
2410 * @param block Address of the area to hold memory pool block info.
2411 * @param timeout Waiting period to wait for a memory pool block (in
2412 * milliseconds), or one of the special values K_NO_WAIT
2413 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002414 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002415 * @retval 0 Data retrieved.
2416 * @retval -ENOMEM Returned without waiting.
2417 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002418 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002419extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002420 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 struct k_mem_block *block, int32_t timeout);
2422
Allan Stephensc98da842016-11-11 15:45:03 -05002423/**
2424 * @} end defgroup mailbox_apis
2425 */
2426
2427/**
2428 * @cond INTERNAL_HIDDEN
2429 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430
2431struct k_pipe {
2432 unsigned char *buffer; /* Pipe buffer: may be NULL */
2433 size_t size; /* Buffer size */
2434 size_t bytes_used; /* # bytes used in buffer */
2435 size_t read_index; /* Where in buffer to read from */
2436 size_t write_index; /* Where in buffer to write */
2437
2438 struct {
2439 _wait_q_t readers; /* Reader wait queue */
2440 _wait_q_t writers; /* Writer wait queue */
2441 } wait_q;
2442
Anas Nashif2f203c22016-12-18 06:57:45 -05002443 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444};
2445
Peter Mitsise5d9c582016-10-14 14:44:57 -04002446#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447 { \
2448 .buffer = pipe_buffer, \
2449 .size = pipe_buffer_size, \
2450 .bytes_used = 0, \
2451 .read_index = 0, \
2452 .write_index = 0, \
2453 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2454 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002455 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 }
2457
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458/**
Allan Stephensc98da842016-11-11 15:45:03 -05002459 * INTERNAL_HIDDEN @endcond
2460 */
2461
2462/**
2463 * @defgroup pipe_apis Pipe APIs
2464 * @ingroup kernel_apis
2465 * @{
2466 */
2467
2468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002473 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * @param name Name of the pipe.
2476 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2477 * or zero if no ring buffer is used.
2478 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002480#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2481 static unsigned char __noinit __aligned(pipe_align) \
2482 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002483 struct k_pipe name \
2484 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002485 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002488 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * This routine initializes a pipe object, prior to its first use.
2491 *
2492 * @param pipe Address of the pipe.
2493 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2494 * is used.
2495 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2496 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497 *
2498 * @return N/A
2499 */
2500extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2501 size_t size);
2502
2503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * @param pipe Address of the pipe.
2509 * @param data Address of data to write.
2510 * @param bytes_to_write Size of data (in bytes).
2511 * @param bytes_written Address of area to hold the number of bytes written.
2512 * @param min_xfer Minimum number of bytes to write.
2513 * @param timeout Waiting period to wait for the data to be written (in
2514 * milliseconds), or one of the special values K_NO_WAIT
2515 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002517 * @retval 0 At least @a min_xfer bytes of data were written.
2518 * @retval -EIO Returned without waiting; zero data bytes were written.
2519 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002522extern int k_pipe_put(struct k_pipe *pipe, void *data,
2523 size_t bytes_to_write, size_t *bytes_written,
2524 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525
2526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * @param pipe Address of the pipe.
2532 * @param data Address to place the data read from pipe.
2533 * @param bytes_to_read Maximum number of data bytes to read.
2534 * @param bytes_read Address of area to hold the number of bytes read.
2535 * @param min_xfer Minimum number of data bytes to read.
2536 * @param timeout Waiting period to wait for the data to be read (in
2537 * milliseconds), or one of the special values K_NO_WAIT
2538 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002540 * @retval 0 At least @a min_xfer bytes of data were read.
2541 * @retval -EIO Returned without waiting; zero data bytes were read.
2542 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002545extern int k_pipe_get(struct k_pipe *pipe, void *data,
2546 size_t bytes_to_read, size_t *bytes_read,
2547 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548
2549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002552 * This routine writes the data contained in a memory block to @a pipe.
2553 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557 * @param block Memory block containing data to send
2558 * @param size Number of data bytes in memory block to send
2559 * @param sem Semaphore to signal upon completion (else NULL)
2560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 */
2563extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2564 size_t size, struct k_sem *sem);
2565
2566/**
Allan Stephensc98da842016-11-11 15:45:03 -05002567 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 */
2569
Allan Stephensc98da842016-11-11 15:45:03 -05002570/**
2571 * @cond INTERNAL_HIDDEN
2572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002574struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002576 uint32_t num_blocks;
2577 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 char *buffer;
2579 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002580 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581
Anas Nashif2f203c22016-12-18 06:57:45 -05002582 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583};
2584
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002585#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2586 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587 { \
2588 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002589 .num_blocks = slab_num_blocks, \
2590 .block_size = slab_block_size, \
2591 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 .free_list = NULL, \
2593 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002594 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595 }
2596
Peter Mitsis578f9112016-10-07 13:50:31 -04002597/**
Allan Stephensc98da842016-11-11 15:45:03 -05002598 * INTERNAL_HIDDEN @endcond
2599 */
2600
2601/**
2602 * @defgroup mem_slab_apis Memory Slab APIs
2603 * @ingroup kernel_apis
2604 * @{
2605 */
2606
2607/**
Allan Stephensda827222016-11-09 14:23:58 -06002608 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002609 *
Allan Stephensda827222016-11-09 14:23:58 -06002610 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002612 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2613 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002614 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002615 *
Allan Stephensda827222016-11-09 14:23:58 -06002616 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002619 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * @param name Name of the memory slab.
2622 * @param slab_block_size Size of each memory block (in bytes).
2623 * @param slab_num_blocks Number memory blocks.
2624 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002625 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002626#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2627 char __noinit __aligned(slab_align) \
2628 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2629 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002630 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002631 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2632 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002634/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002635 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002638 *
Allan Stephensda827222016-11-09 14:23:58 -06002639 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2640 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2641 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2642 * To ensure that each memory block is similarly aligned to this boundary,
2643 * @a slab_block_size must also be a multiple of N.
2644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * @param slab Address of the memory slab.
2646 * @param buffer Pointer to buffer used for the memory blocks.
2647 * @param block_size Size of each memory block (in bytes).
2648 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002649 *
2650 * @return N/A
2651 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002652extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002653 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002654
2655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @param slab Address of the memory slab.
2661 * @param mem Pointer to block address area.
2662 * @param timeout Maximum time to wait for operation to complete
2663 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2664 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002665 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002666 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002668 * @retval -ENOMEM Returned without waiting.
2669 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002670 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002671extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2672 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002673
2674/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * This routine releases a previously allocated memory block back to its
2678 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * @param slab Address of the memory slab.
2681 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002682 *
2683 * @return N/A
2684 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002685extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * This routine gets the number of memory blocks that are currently
2691 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002693 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002696 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002697static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002699 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700}
2701
Peter Mitsisc001aa82016-10-13 13:53:37 -04002702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * This routine gets the number of memory blocks that are currently
2706 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002710 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002711 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002712static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002713{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002714 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002715}
2716
Allan Stephensc98da842016-11-11 15:45:03 -05002717/**
2718 * @} end defgroup mem_slab_apis
2719 */
2720
2721/**
2722 * @cond INTERNAL_HIDDEN
2723 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002725/*
2726 * Memory pool requires a buffer and two arrays of structures for the
2727 * memory block accounting:
2728 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2729 * status of four blocks of memory.
2730 */
2731struct k_mem_pool_quad_block {
2732 char *mem_blocks; /* pointer to the first of four memory blocks */
2733 uint32_t mem_status; /* four bits. If bit is set, memory block is
2734 allocated */
2735};
2736/*
2737 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2738 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2739 * block size is 4 times less than the previous one and thus requires 4 times
2740 * bigger array of k_mem_pool_quad_block structures to keep track of the
2741 * memory blocks.
2742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002744/*
2745 * The array of k_mem_pool_block_set keeps the information of each array of
2746 * k_mem_pool_quad_block structures
2747 */
2748struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002749 size_t block_size; /* memory block size */
2750 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002751 struct k_mem_pool_quad_block *quad_block;
2752 int count;
2753};
2754
2755/* Memory pool descriptor */
2756struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002757 size_t max_block_size;
2758 size_t min_block_size;
2759 uint32_t nr_of_maxblocks;
2760 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002761 struct k_mem_pool_block_set *block_set;
2762 char *bufblock;
2763 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002764 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765};
2766
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002767#ifdef CONFIG_ARM
2768#define _SECTION_TYPE_SIGN "%"
2769#else
2770#define _SECTION_TYPE_SIGN "@"
2771#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002773/*
2774 * Static memory pool initialization
2775 */
Allan Stephensc98da842016-11-11 15:45:03 -05002776
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002777/*
2778 * Use .altmacro to be able to recalculate values and pass them as string
2779 * arguments when calling assembler macros resursively
2780 */
2781__asm__(".altmacro\n\t");
2782
2783/*
2784 * Recursively calls a macro
2785 * The followig global symbols need to be initialized:
2786 * __memory_pool_max_block_size - maximal size of the memory block
2787 * __memory_pool_min_block_size - minimal size of the memory block
2788 * Notes:
2789 * Global symbols are used due the fact that assembler macro allows only
2790 * one argument be passed with the % conversion
2791 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2792 * is used instead of / 4.
2793 * n_max argument needs to go first in the invoked macro, as some
2794 * assemblers concatenate \name and %(\n_max * 4) arguments
2795 * if \name goes first
2796 */
2797__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2798 ".ifge __memory_pool_max_block_size >> 2 -"
2799 " __memory_pool_min_block_size\n\t\t"
2800 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2801 "\\macro_name %(\\n_max * 4) \\name\n\t"
2802 ".endif\n\t"
2803 ".endm\n");
2804
2805/*
2806 * Build quad blocks
2807 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2808 * structures and recursively calls itself for the next array, 4 times
2809 * larger.
2810 * The followig global symbols need to be initialized:
2811 * __memory_pool_max_block_size - maximal size of the memory block
2812 * __memory_pool_min_block_size - minimal size of the memory block
2813 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2814 */
2815__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002816 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002817 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2818 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2819 ".if \\n_max % 4\n\t\t"
2820 ".skip __memory_pool_quad_block_size\n\t"
2821 ".endif\n\t"
2822 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2823 ".endm\n");
2824
2825/*
2826 * Build block sets and initialize them
2827 * Macro initializes the k_mem_pool_block_set structure and
2828 * recursively calls itself for the next one.
2829 * The followig global symbols need to be initialized:
2830 * __memory_pool_max_block_size - maximal size of the memory block
2831 * __memory_pool_min_block_size - minimal size of the memory block
2832 * __memory_pool_block_set_count, the number of the elements in the
2833 * block set array must be set to 0. Macro calculates it's real
2834 * value.
2835 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2836 * structures, _build_quad_blocks must be called prior it.
2837 */
2838__asm__(".macro _build_block_set n_max, name\n\t"
2839 ".int __memory_pool_max_block_size\n\t" /* block_size */
2840 ".if \\n_max % 4\n\t\t"
2841 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2842 ".else\n\t\t"
2843 ".int \\n_max >> 2\n\t"
2844 ".endif\n\t"
2845 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2846 ".int 0\n\t" /* count */
2847 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2848 "__do_recurse _build_block_set \\name \\n_max\n\t"
2849 ".endm\n");
2850
2851/*
2852 * Build a memory pool structure and initialize it
2853 * Macro uses __memory_pool_block_set_count global symbol,
2854 * block set addresses and buffer address, it may be called only after
2855 * _build_block_set
2856 */
2857__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002858 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002859 _SECTION_TYPE_SIGN "progbits\n\t"
2860 ".globl \\name\n\t"
2861 "\\name:\n\t"
2862 ".int \\max_size\n\t" /* max_block_size */
2863 ".int \\min_size\n\t" /* min_block_size */
2864 ".int \\n_max\n\t" /* nr_of_maxblocks */
2865 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2866 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2867 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2868 ".int 0\n\t" /* wait_q->head */
2869 ".int 0\n\t" /* wait_q->next */
2870 ".popsection\n\t"
2871 ".endm\n");
2872
2873#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2874 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2875 _SECTION_TYPE_SIGN "progbits\n\t"); \
2876 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2877 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2878 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2879 STRINGIFY(name) "\n\t"); \
2880 __asm__(".popsection\n\t")
2881
2882#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2883 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2884 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2885 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2886 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002887 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002888 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2889 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2890 STRINGIFY(name) "\n\t"); \
2891 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2892 __asm__(".int __memory_pool_block_set_count\n\t"); \
2893 __asm__(".popsection\n\t"); \
2894 extern uint32_t _mem_pool_block_set_count_##name; \
2895 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2896
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002897#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2898 char __noinit __aligned(align) \
2899 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002900
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002901/*
2902 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2903 * to __memory_pool_quad_block_size absolute symbol.
2904 * This function does not get called, but compiler calculates the value and
2905 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2906 */
2907static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2908{
2909 __asm__(".globl __memory_pool_quad_block_size\n\t"
2910#ifdef CONFIG_NIOS2
2911 "__memory_pool_quad_block_size = %0\n\t"
2912#else
2913 "__memory_pool_quad_block_size = %c0\n\t"
2914#endif
2915 :
2916 : "n"(sizeof(struct k_mem_pool_quad_block)));
2917}
2918
2919/**
Allan Stephensc98da842016-11-11 15:45:03 -05002920 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002921 */
2922
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002923/**
Allan Stephensc98da842016-11-11 15:45:03 -05002924 * @addtogroup mem_pool_apis
2925 * @{
2926 */
2927
2928/**
2929 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2932 * long. The memory pool allows blocks to be repeatedly partitioned into
2933 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2934 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002935 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002937 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002938 * If the pool is to be accessed outside the module where it is defined, it
2939 * can be declared via
2940 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002941 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @param name Name of the memory pool.
2944 * @param min_size Size of the smallest blocks in the pool (in bytes).
2945 * @param max_size Size of the largest blocks in the pool (in bytes).
2946 * @param n_max Number of maximum sized blocks in the pool.
2947 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002948 */
2949#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002950 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2951 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002952 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002953 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2954 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2955 extern struct k_mem_pool name
2956
Peter Mitsis937042c2016-10-13 13:18:26 -04002957/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * @param pool Address of the memory pool.
2963 * @param block Pointer to block descriptor for the allocated memory.
2964 * @param size Amount of memory to allocate (in bytes).
2965 * @param timeout Maximum time to wait for operation to complete
2966 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2967 * or K_FOREVER to wait as long as necessary.
2968 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002969 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002971 * @retval -ENOMEM Returned without waiting.
2972 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002973 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002974extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002975 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002976
2977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * This routine releases a previously allocated memory block back to its
2981 * memory pool.
2982 *
2983 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002984 *
2985 * @return N/A
2986 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002988
2989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * This routine instructs a memory pool to concatenate unused memory blocks
2993 * into larger blocks wherever possible. Manually defragmenting the memory
2994 * pool may speed up future allocations of memory blocks by eliminating the
2995 * need for the memory pool to perform an automatic partial defragmentation.
2996 *
2997 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002998 *
2999 * @return N/A
3000 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003001extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003002
3003/**
Allan Stephensc98da842016-11-11 15:45:03 -05003004 * @} end addtogroup mem_pool_apis
3005 */
3006
3007/**
3008 * @defgroup heap_apis Heap Memory Pool APIs
3009 * @ingroup kernel_apis
3010 * @{
3011 */
3012
3013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003017 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003022 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003023extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003024
3025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003027 *
3028 * This routine provides traditional free() semantics. The memory being
3029 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003030 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003031 * If @a ptr is NULL, no operation is performed.
3032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003034 *
3035 * @return N/A
3036 */
3037extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038
Allan Stephensc98da842016-11-11 15:45:03 -05003039/**
3040 * @} end defgroup heap_apis
3041 */
3042
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003043/**
3044 * @brief Make the CPU idle.
3045 *
3046 * This function makes the CPU idle until an event wakes it up.
3047 *
3048 * In a regular system, the idle thread should be the only thread responsible
3049 * for making the CPU idle and triggering any type of power management.
3050 * However, in some more constrained systems, such as a single-threaded system,
3051 * the only thread would be responsible for this if needed.
3052 *
3053 * @return N/A
3054 */
3055extern void k_cpu_idle(void);
3056
3057/**
3058 * @brief Make the CPU idle in an atomic fashion.
3059 *
3060 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3061 * must be done atomically before making the CPU idle.
3062 *
3063 * @param key Interrupt locking key obtained from irq_lock().
3064 *
3065 * @return N/A
3066 */
3067extern void k_cpu_atomic_idle(unsigned int key);
3068
Andrew Boie350f88d2017-01-18 13:13:45 -08003069extern void _sys_power_save_idle_exit(int32_t ticks);
3070
Anas Nashifa6149502017-01-17 07:47:31 -05003071/* Include legacy APIs */
3072#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003074#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075#include <arch/cpu.h>
3076
3077/*
3078 * private APIs that are utilized by one or more public APIs
3079 */
3080
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003081#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003083#else
3084#define _init_static_threads() do { } while ((0))
3085#endif
3086
3087extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003088extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089
3090#ifdef __cplusplus
3091}
3092#endif
3093
Andrew Boiee004dec2016-11-07 09:01:19 -08003094#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3095/*
3096 * Define new and delete operators.
3097 * At this moment, the operators do nothing since objects are supposed
3098 * to be statically allocated.
3099 */
3100inline void operator delete(void *ptr)
3101{
3102 (void)ptr;
3103}
3104
3105inline void operator delete[](void *ptr)
3106{
3107 (void)ptr;
3108}
3109
3110inline void *operator new(size_t size)
3111{
3112 (void)size;
3113 return NULL;
3114}
3115
3116inline void *operator new[](size_t size)
3117{
3118 (void)size;
3119 return NULL;
3120}
3121
3122/* Placement versions of operator new and delete */
3123inline void operator delete(void *ptr1, void *ptr2)
3124{
3125 (void)ptr1;
3126 (void)ptr2;
3127}
3128
3129inline void operator delete[](void *ptr1, void *ptr2)
3130{
3131 (void)ptr1;
3132 (void)ptr2;
3133}
3134
3135inline void *operator new(size_t size, void *ptr)
3136{
3137 (void)size;
3138 return ptr;
3139}
3140
3141inline void *operator new[](size_t size, void *ptr)
3142{
3143 (void)size;
3144 return ptr;
3145}
3146
3147#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3148
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003149#endif /* !_ASMLANGUAGE */
3150
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003151#endif /* _kernel__h_ */