blob: 46a31db501ae928d1a79098156643447be5bbdb7 [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
16#include <stddef.h>
17#include <stdint.h>
Anas Nashif173902f2017-01-17 07:08:56 -050018#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040019#include <toolchain.h>
20#include <sections.h>
21#include <atomic.h>
22#include <errno.h>
23#include <misc/__assert.h>
24#include <misc/dlist.h>
25#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050026#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050027#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050028#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040029
30#ifdef __cplusplus
31extern "C" {
32#endif
33
Anas Nashifbbb157d2017-01-15 08:46:31 -050034/**
35 * @brief Kernel APIs
36 * @defgroup kernel_apis Kernel APIs
37 * @{
38 * @}
39 */
40
Anas Nashif61f4b242016-11-18 10:53:59 -050041#ifdef CONFIG_KERNEL_DEBUG
42#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040043#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
44#else
45#define K_DEBUG(fmt, ...)
46#endif
47
Benjamin Walsh2f280412017-01-14 19:23:46 -050048#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
49#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
50#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
51#elif defined(CONFIG_COOP_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
53#define _NUM_PREEMPT_PRIO (0)
54#elif defined(CONFIG_PREEMPT_ENABLED)
55#define _NUM_COOP_PRIO (0)
56#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
57#else
58#error "invalid configuration"
59#endif
60
61#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040062#define K_PRIO_PREEMPT(x) (x)
63
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_ANY NULL
65#define K_END NULL
66
Benjamin Walshedb35702017-01-14 18:47:22 -050067#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050069#elif defined(CONFIG_COOP_ENABLED)
70#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
71#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050073#else
74#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#endif
76
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050077#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
79#else
80#define K_LOWEST_THREAD_PRIO -1
81#endif
82
Benjamin Walshfab8d922016-11-08 15:36:36 -050083#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
84
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
86#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
87
88typedef sys_dlist_t _wait_q_t;
89
Anas Nashif2f203c22016-12-18 06:57:45 -050090#ifdef CONFIG_OBJECT_TRACING
91#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
92#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093#else
Anas Nashif2f203c22016-12-18 06:57:45 -050094#define _OBJECT_TRACING_INIT
95#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#endif
97
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050098#define tcs k_thread
99struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100struct k_mutex;
101struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400102struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103struct k_msgq;
104struct k_mbox;
105struct k_pipe;
106struct k_fifo;
107struct k_lifo;
108struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400109struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mem_pool;
111struct k_timer;
112
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400113typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115enum execution_context_types {
116 K_ISR = 0,
117 K_COOP_THREAD,
118 K_PREEMPT_THREAD,
119};
120
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400121/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100122 * @defgroup profiling_apis Profiling APIs
123 * @ingroup kernel_apis
124 * @{
125 */
126
127/**
128 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
129 *
130 * This routine calls @ref stack_analyze on the 4 call stacks declared and
131 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
132 *
133 * CONFIG_MAIN_STACK_SIZE
134 * CONFIG_IDLE_STACK_SIZE
135 * CONFIG_ISR_STACK_SIZE
136 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
137 *
138 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
139 * produce output.
140 *
141 * @return N/A
142 */
143extern void k_call_stacks_analyze(void);
144
145/**
146 * @} end defgroup profiling_apis
147 */
148
149/**
Allan Stephensc98da842016-11-11 15:45:03 -0500150 * @defgroup thread_apis Thread APIs
151 * @ingroup kernel_apis
152 * @{
153 */
154
155/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500156 * @typedef k_thread_entry_t
157 * @brief Thread entry point function type.
158 *
159 * A thread's entry point function is invoked when the thread starts executing.
160 * Up to 3 argument values can be passed to the function.
161 *
162 * The thread terminates execution permanently if the entry point function
163 * returns. The thread is responsible for releasing any shared resources
164 * it may own (such as mutexes and dynamically allocated memory), prior to
165 * returning.
166 *
167 * @param p1 First argument.
168 * @param p2 Second argument.
169 * @param p3 Third argument.
170 *
171 * @return N/A
172 */
173typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
174
175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500176 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500178 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500180 * The new thread may be scheduled for immediate execution or a delayed start.
181 * If the newly spawned thread does not have a delayed start the kernel
182 * scheduler may preempt the current thread to allow the new thread to
183 * execute.
184 *
185 * Thread options are architecture-specific, and can include K_ESSENTIAL,
186 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
187 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 *
189 * @param stack Pointer to the stack space.
190 * @param stack_size Stack size in bytes.
191 * @param entry Thread entry function.
192 * @param p1 1st entry point parameter.
193 * @param p2 2nd entry point parameter.
194 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500195 * @param prio Thread priority.
196 * @param options Thread options.
197 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500199 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400200 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500201extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500202 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400203 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500204 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400205
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500207 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 *
Allan Stephensc98da842016-11-11 15:45:03 -0500209 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500212 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400213 *
214 * @return N/A
215 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400216extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400217
218/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
221 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500222 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400223 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400224 * @return N/A
225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227
228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500231 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400232 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500233 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400234 *
235 * @return N/A
236 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400237extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238
239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500240 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500242 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500244 * If @a thread is not currently sleeping, the routine has no effect.
245 *
246 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400247 *
248 * @return N/A
249 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400250extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400251
252/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500253 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500255 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400256 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400257extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400258
259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500260 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500262 * This routine prevents @a thread from executing if it has not yet started
263 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500265 * @param thread ID of thread to cancel.
266 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500267 * @retval 0 Thread spawning cancelled.
268 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400269 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400270extern int k_thread_cancel(k_tid_t thread);
271
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400272/**
Allan Stephensc98da842016-11-11 15:45:03 -0500273 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500275 * This routine permanently stops execution of @a thread. The thread is taken
276 * off all kernel queues it is part of (i.e. the ready queue, the timeout
277 * queue, or a kernel object wait queue). However, any kernel resources the
278 * thread might currently own (such as mutexes or memory blocks) are not
279 * released. It is the responsibility of the caller of this routine to ensure
280 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500282 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400283 *
284 * @return N/A
285 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400286extern void k_thread_abort(k_tid_t thread);
287
Allan Stephensc98da842016-11-11 15:45:03 -0500288/**
289 * @cond INTERNAL_HIDDEN
290 */
291
Benjamin Walshd211a522016-12-06 11:44:01 -0500292/* timeout has timed out and is not on _timeout_q anymore */
293#define _EXPIRED (-2)
294
295/* timeout is not in use */
296#define _INACTIVE (-1)
297
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400298#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400299#define _THREAD_TIMEOUT_INIT(obj) \
300 (obj).nano_timeout = { \
301 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400302 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400303 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500304 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400305 },
306#else
307#define _THREAD_TIMEOUT_INIT(obj)
308#endif
309
310#ifdef CONFIG_ERRNO
311#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
312#else
313#define _THREAD_ERRNO_INIT(obj)
314#endif
315
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400316struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400317 union {
318 char *init_stack;
319 struct k_thread *thread;
320 };
321 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500322 void (*init_entry)(void *, void *, void *);
323 void *init_p1;
324 void *init_p2;
325 void *init_p3;
326 int init_prio;
327 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400328 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500329 void (*init_abort)(void);
330 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400331};
332
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400333#define _THREAD_INITIALIZER(stack, stack_size, \
334 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500335 prio, options, delay, abort, groups) \
336 { \
337 .init_stack = (stack), \
338 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400339 .init_entry = (void (*)(void *, void *, void *))entry, \
340 .init_p1 = (void *)p1, \
341 .init_p2 = (void *)p2, \
342 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500343 .init_prio = (prio), \
344 .init_options = (options), \
345 .init_delay = (delay), \
346 .init_abort = (abort), \
347 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400348 }
349
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400350/**
Allan Stephensc98da842016-11-11 15:45:03 -0500351 * INTERNAL_HIDDEN @endcond
352 */
353
354/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500355 * @brief Statically define and initialize a thread.
356 *
357 * The thread may be scheduled for immediate execution or a delayed start.
358 *
359 * Thread options are architecture-specific, and can include K_ESSENTIAL,
360 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
361 * them using "|" (the logical OR operator).
362 *
363 * The ID of the thread can be accessed using:
364 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500365 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500366 *
367 * @param name Name of the thread.
368 * @param stack_size Stack size in bytes.
369 * @param entry Thread entry function.
370 * @param p1 1st entry point parameter.
371 * @param p2 2nd entry point parameter.
372 * @param p3 3rd entry point parameter.
373 * @param prio Thread priority.
374 * @param options Thread options.
375 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400376 *
377 * @internal It has been observed that the x86 compiler by default aligns
378 * these _static_thread_data structures to 32-byte boundaries, thereby
379 * wasting space. To work around this, force a 4-byte alignment.
380 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500381#define K_THREAD_DEFINE(name, stack_size, \
382 entry, p1, p2, p3, \
383 prio, options, delay) \
384 char __noinit __stack _k_thread_obj_##name[stack_size]; \
385 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500386 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500387 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
388 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500389 NULL, 0); \
390 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400391
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500393 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500395 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * @param thread ID of thread whose priority is needed.
398 *
399 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500401extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400402
403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500404 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 *
408 * Rescheduling can occur immediately depending on the priority @a thread is
409 * set to:
410 *
411 * - If its priority is raised above the priority of the caller of this
412 * function, and the caller is preemptible, @a thread will be scheduled in.
413 *
414 * - If the caller operates on itself, it lowers its priority below that of
415 * other threads in the system, and the caller is preemptible, the thread of
416 * highest priority will be scheduled in.
417 *
418 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
419 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
420 * highest priority.
421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500422 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 * @param prio New priority.
424 *
425 * @warning Changing the priority of a thread currently involved in mutex
426 * priority inheritance may result in undefined behavior.
427 *
428 * @return N/A
429 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400430extern void k_thread_priority_set(k_tid_t thread, int prio);
431
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500433 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * This routine prevents the kernel scheduler from making @a thread the
436 * current thread. All other internal operations on @a thread are still
437 * performed; for example, any timeout it is waiting on keeps ticking,
438 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500440 * If @a thread is already suspended, the routine has no effect.
441 *
442 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400443 *
444 * @return N/A
445 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400446extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447
448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500449 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500451 * This routine allows the kernel scheduler to make @a thread the current
452 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500454 * If @a thread is not currently suspended, the routine has no effect.
455 *
456 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457 *
458 * @return N/A
459 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400460extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500465 * This routine specifies how the scheduler will perform time slicing of
466 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500468 * To enable time slicing, @a slice must be non-zero. The scheduler
469 * ensures that no thread runs for more than the specified time limit
470 * before other threads of that priority are given a chance to execute.
471 * Any thread whose priority is higher than @a prio is exempted, and may
472 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 * execute. Once the scheduler selects a thread for execution, there is no
476 * minimum guaranteed time the thread will execute before threads of greater or
477 * equal priority are scheduled.
478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 * for execution, this routine has no effect; the thread is immediately
481 * rescheduled after the slice period expires.
482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500483 * To disable timeslicing, set both @a slice and @a prio to zero.
484 *
485 * @param slice Maximum time slice length (in milliseconds).
486 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487 *
488 * @return N/A
489 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400490extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400492/**
Allan Stephensc98da842016-11-11 15:45:03 -0500493 * @} end defgroup thread_apis
494 */
495
496/**
497 * @addtogroup isr_apis
498 * @{
499 */
500
501/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500502 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503 *
Allan Stephensc98da842016-11-11 15:45:03 -0500504 * This routine allows the caller to customize its actions, depending on
505 * whether it is a thread or an ISR.
506 *
507 * @note Can be called by ISRs.
508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * @return 0 if invoked by a thread.
510 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400511 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500512extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400513
Benjamin Walsh445830d2016-11-10 15:54:27 -0500514/**
515 * @brief Determine if code is running in a preemptible thread.
516 *
Allan Stephensc98da842016-11-11 15:45:03 -0500517 * This routine allows the caller to customize its actions, depending on
518 * whether it can be preempted by another thread. The routine returns a 'true'
519 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500520 *
Allan Stephensc98da842016-11-11 15:45:03 -0500521 * - The code is running in a thread, not at ISR.
522 * - The thread's priority is in the preemptible range.
523 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500524 *
Allan Stephensc98da842016-11-11 15:45:03 -0500525 * @note Can be called by ISRs.
526 *
527 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500528 * @return Non-zero if invoked by a preemptible thread.
529 */
530extern int k_is_preempt_thread(void);
531
Allan Stephensc98da842016-11-11 15:45:03 -0500532/**
533 * @} end addtogroup isr_apis
534 */
535
536/**
537 * @addtogroup thread_apis
538 * @{
539 */
540
541/**
542 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500543 *
Allan Stephensc98da842016-11-11 15:45:03 -0500544 * This routine prevents the current thread from being preempted by another
545 * thread by instructing the scheduler to treat it as a cooperative thread.
546 * If the thread subsequently performs an operation that makes it unready,
547 * it will be context switched out in the normal manner. When the thread
548 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500549 *
Allan Stephensc98da842016-11-11 15:45:03 -0500550 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500551 *
Allan Stephensc98da842016-11-11 15:45:03 -0500552 * @note k_sched_lock() and k_sched_unlock() should normally be used
553 * when the operation being performed can be safely interrupted by ISRs.
554 * However, if the amount of processing involved is very small, better
555 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500556 *
557 * @return N/A
558 */
559extern void k_sched_lock(void);
560
Allan Stephensc98da842016-11-11 15:45:03 -0500561/**
562 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500563 *
Allan Stephensc98da842016-11-11 15:45:03 -0500564 * This routine reverses the effect of a previous call to k_sched_lock().
565 * A thread must call the routine once for each time it called k_sched_lock()
566 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500567 *
568 * @return N/A
569 */
570extern void k_sched_unlock(void);
571
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400572/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500573 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * Custom data is not used by the kernel itself, and is freely available
578 * for a thread to use as it sees fit. It can be used as a framework
579 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 *
583 * @return N/A
584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400585extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400586
587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500588 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500590 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500592 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594extern void *k_thread_custom_data_get(void);
595
596/**
Allan Stephensc98da842016-11-11 15:45:03 -0500597 * @} end addtogroup thread_apis
598 */
599
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400600#include <sys_clock.h>
601
Allan Stephensc2f15a42016-11-17 12:24:22 -0500602/**
603 * @addtogroup clock_apis
604 * @{
605 */
606
607/**
608 * @brief Generate null timeout delay.
609 *
610 * This macro generates a timeout delay that that instructs a kernel API
611 * not to wait if the requested operation cannot be performed immediately.
612 *
613 * @return Timeout delay value.
614 */
615#define K_NO_WAIT 0
616
617/**
618 * @brief Generate timeout delay from milliseconds.
619 *
620 * This macro generates a timeout delay that that instructs a kernel API
621 * to wait up to @a ms milliseconds to perform the requested operation.
622 *
623 * @param ms Duration in milliseconds.
624 *
625 * @return Timeout delay value.
626 */
Johan Hedberg14471692016-11-13 10:52:15 +0200627#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500628
629/**
630 * @brief Generate timeout delay from seconds.
631 *
632 * This macro generates a timeout delay that that instructs a kernel API
633 * to wait up to @a s seconds to perform the requested operation.
634 *
635 * @param s Duration in seconds.
636 *
637 * @return Timeout delay value.
638 */
Johan Hedberg14471692016-11-13 10:52:15 +0200639#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500640
641/**
642 * @brief Generate timeout delay from minutes.
643 *
644 * This macro generates a timeout delay that that instructs a kernel API
645 * to wait up to @a m minutes to perform the requested operation.
646 *
647 * @param m Duration in minutes.
648 *
649 * @return Timeout delay value.
650 */
Johan Hedberg14471692016-11-13 10:52:15 +0200651#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500652
653/**
654 * @brief Generate timeout delay from hours.
655 *
656 * This macro generates a timeout delay that that instructs a kernel API
657 * to wait up to @a h hours to perform the requested operation.
658 *
659 * @param h Duration in hours.
660 *
661 * @return Timeout delay value.
662 */
Johan Hedberg14471692016-11-13 10:52:15 +0200663#define K_HOURS(h) K_MINUTES((h) * 60)
664
Allan Stephensc98da842016-11-11 15:45:03 -0500665/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500666 * @brief Generate infinite timeout delay.
667 *
668 * This macro generates a timeout delay that that instructs a kernel API
669 * to wait as long as necessary to perform the requested operation.
670 *
671 * @return Timeout delay value.
672 */
673#define K_FOREVER (-1)
674
675/**
676 * @} end addtogroup clock_apis
677 */
678
679/**
Allan Stephensc98da842016-11-11 15:45:03 -0500680 * @cond INTERNAL_HIDDEN
681 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400682
Benjamin Walsh62092182016-12-20 14:39:08 -0500683/* kernel clocks */
684
685#if (sys_clock_ticks_per_sec == 1000) || \
686 (sys_clock_ticks_per_sec == 500) || \
687 (sys_clock_ticks_per_sec == 250) || \
688 (sys_clock_ticks_per_sec == 125) || \
689 (sys_clock_ticks_per_sec == 100) || \
690 (sys_clock_ticks_per_sec == 50) || \
691 (sys_clock_ticks_per_sec == 25) || \
692 (sys_clock_ticks_per_sec == 20) || \
693 (sys_clock_ticks_per_sec == 10) || \
694 (sys_clock_ticks_per_sec == 1)
695
696 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
697#else
698 /* yields horrible 64-bit math on many architectures: try to avoid */
699 #define _NON_OPTIMIZED_TICKS_PER_SEC
700#endif
701
702#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
703extern int32_t _ms_to_ticks(int32_t ms);
704#else
705static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
706{
707 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
708}
709#endif
710
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500711/* added tick needed to account for tick in progress */
712#define _TICK_ALIGN 1
713
Benjamin Walsh62092182016-12-20 14:39:08 -0500714static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400715{
Benjamin Walsh62092182016-12-20 14:39:08 -0500716#ifdef CONFIG_SYS_CLOCK_EXISTS
717
718#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400719 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400720#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500721 return (uint64_t)ticks * _ms_per_tick;
722#endif
723
724#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400725 __ASSERT(ticks == 0, "");
726 return 0;
727#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400728}
729
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400730/* timeouts */
731
732struct _timeout;
733typedef void (*_timeout_func_t)(struct _timeout *t);
734
735struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500736 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400737 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400738 sys_dlist_t *wait_q;
739 int32_t delta_ticks_from_prev;
740 _timeout_func_t func;
741};
742
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200743extern int32_t _timeout_remaining_get(struct _timeout *timeout);
744
Allan Stephensc98da842016-11-11 15:45:03 -0500745/**
746 * INTERNAL_HIDDEN @endcond
747 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500748
Allan Stephensc98da842016-11-11 15:45:03 -0500749/**
750 * @cond INTERNAL_HIDDEN
751 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400752
753struct k_timer {
754 /*
755 * _timeout structure must be first here if we want to use
756 * dynamic timer allocation. timeout.node is used in the double-linked
757 * list of free timers
758 */
759 struct _timeout timeout;
760
Allan Stephens45bfa372016-10-12 12:39:42 -0500761 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400762 _wait_q_t wait_q;
763
764 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500765 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400766
767 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500768 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769
770 /* timer period */
771 int32_t period;
772
Allan Stephens45bfa372016-10-12 12:39:42 -0500773 /* timer status */
774 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500776 /* user-specific data, also used to support legacy features */
777 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778
Anas Nashif2f203c22016-12-18 06:57:45 -0500779 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400780};
781
Allan Stephens1342adb2016-11-03 13:54:53 -0500782#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400783 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500784 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500785 .timeout.wait_q = NULL, \
786 .timeout.thread = NULL, \
787 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400788 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500789 .expiry_fn = expiry, \
790 .stop_fn = stop, \
791 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500792 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500793 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400794 }
795
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796/**
Allan Stephensc98da842016-11-11 15:45:03 -0500797 * INTERNAL_HIDDEN @endcond
798 */
799
800/**
801 * @defgroup timer_apis Timer APIs
802 * @ingroup kernel_apis
803 * @{
804 */
805
806/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500807 * @typedef k_timer_expiry_t
808 * @brief Timer expiry function type.
809 *
810 * A timer's expiry function is executed by the system clock interrupt handler
811 * each time the timer expires. The expiry function is optional, and is only
812 * invoked if the timer has been initialized with one.
813 *
814 * @param timer Address of timer.
815 *
816 * @return N/A
817 */
818typedef void (*k_timer_expiry_t)(struct k_timer *timer);
819
820/**
821 * @typedef k_timer_stop_t
822 * @brief Timer stop function type.
823 *
824 * A timer's stop function is executed if the timer is stopped prematurely.
825 * The function runs in the context of the thread that stops the timer.
826 * The stop function is optional, and is only invoked if the timer has been
827 * initialized with one.
828 *
829 * @param timer Address of timer.
830 *
831 * @return N/A
832 */
833typedef void (*k_timer_stop_t)(struct k_timer *timer);
834
835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500840 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
842 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @param expiry_fn Function to invoke each time the timer expires.
844 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500846#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500847 struct k_timer name \
848 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500849 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400850
Allan Stephens45bfa372016-10-12 12:39:42 -0500851/**
852 * @brief Initialize a timer.
853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500855 *
856 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @param expiry_fn Function to invoke each time the timer expires.
858 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500859 *
860 * @return N/A
861 */
862extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500863 k_timer_expiry_t expiry_fn,
864 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700865
Allan Stephens45bfa372016-10-12 12:39:42 -0500866/**
867 * @brief Start a timer.
868 *
869 * This routine starts a timer, and resets its status to zero. The timer
870 * begins counting down using the specified duration and period values.
871 *
872 * Attempting to start a timer that is already running is permitted.
873 * The timer's status is reset to zero and the timer begins counting down
874 * using the new duration and period values.
875 *
876 * @param timer Address of timer.
877 * @param duration Initial timer duration (in milliseconds).
878 * @param period Timer period (in milliseconds).
879 *
880 * @return N/A
881 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500883 int32_t duration, int32_t period);
884
885/**
886 * @brief Stop a timer.
887 *
888 * This routine stops a running timer prematurely. The timer's stop function,
889 * if one exists, is invoked by the caller.
890 *
891 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500893 *
894 * @param timer Address of timer.
895 *
896 * @return N/A
897 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400898extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500899
900/**
901 * @brief Read timer status.
902 *
903 * This routine reads the timer's status, which indicates the number of times
904 * it has expired since its status was last read.
905 *
906 * Calling this routine resets the timer's status to zero.
907 *
908 * @param timer Address of timer.
909 *
910 * @return Timer status.
911 */
912extern uint32_t k_timer_status_get(struct k_timer *timer);
913
914/**
915 * @brief Synchronize thread to timer expiration.
916 *
917 * This routine blocks the calling thread until the timer's status is non-zero
918 * (indicating that it has expired at least once since it was last examined)
919 * or the timer is stopped. If the timer status is already non-zero,
920 * or the timer is already stopped, the caller continues without waiting.
921 *
922 * Calling this routine resets the timer's status to zero.
923 *
924 * This routine must not be used by interrupt handlers, since they are not
925 * allowed to block.
926 *
927 * @param timer Address of timer.
928 *
929 * @return Timer status.
930 */
931extern uint32_t k_timer_status_sync(struct k_timer *timer);
932
933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500935 *
936 * This routine computes the (approximate) time remaining before a running
937 * timer next expires. If the timer is not running, it returns zero.
938 *
939 * @param timer Address of timer.
940 *
941 * @return Remaining time (in milliseconds).
942 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200943static inline int32_t k_timer_remaining_get(struct k_timer *timer)
944{
945 return _timeout_remaining_get(&timer->timeout);
946}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400947
Allan Stephensc98da842016-11-11 15:45:03 -0500948/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500949 * @brief Associate user-specific data with a timer.
950 *
951 * This routine records the @a user_data with the @a timer, to be retrieved
952 * later.
953 *
954 * It can be used e.g. in a timer handler shared across multiple subsystems to
955 * retrieve data specific to the subsystem this timer is associated with.
956 *
957 * @param timer Address of timer.
958 * @param user_data User data to associate with the timer.
959 *
960 * @return N/A
961 */
962static inline void k_timer_user_data_set(struct k_timer *timer,
963 void *user_data)
964{
965 timer->user_data = user_data;
966}
967
968/**
969 * @brief Retrieve the user-specific data from a timer.
970 *
971 * @param timer Address of timer.
972 *
973 * @return The user data.
974 */
975static inline void *k_timer_user_data_get(struct k_timer *timer)
976{
977 return timer->user_data;
978}
979
980/**
Allan Stephensc98da842016-11-11 15:45:03 -0500981 * @} end defgroup timer_apis
982 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400983
Allan Stephensc98da842016-11-11 15:45:03 -0500984/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500985 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500986 * @{
987 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500988
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * This routine returns the elapsed time since the system booted,
993 * in milliseconds.
994 *
995 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400996 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400997extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400998
999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * This routine returns the lower 32-bits of the elapsed time since the system
1003 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * This routine can be more efficient than k_uptime_get(), as it reduces the
1006 * need for interrupt locking and 64-bit math. However, the 32-bit result
1007 * cannot hold a system uptime time larger than approximately 50 days, so the
1008 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001011 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001012extern uint32_t k_uptime_get_32(void);
1013
1014/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * This routine computes the elapsed time between the current system uptime
1018 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @param reftime Pointer to a reference time, which is updated to the current
1021 * uptime upon return.
1022 *
1023 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001024 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001025extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001026
1027/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * This routine computes the elapsed time between the current system uptime
1031 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1034 * need for interrupt locking and 64-bit math. However, the 32-bit result
1035 * cannot hold an elapsed time larger than approximately 50 days, so the
1036 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * @param reftime Pointer to a reference time, which is updated to the current
1039 * uptime upon return.
1040 *
1041 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001042 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001043extern uint32_t k_uptime_delta_32(int64_t *reftime);
1044
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * This routine returns the current time, as measured by the system's hardware
1049 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001051 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053extern uint32_t k_cycle_get_32(void);
1054
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001056 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001057 */
1058
Allan Stephensc98da842016-11-11 15:45:03 -05001059/**
1060 * @cond INTERNAL_HIDDEN
1061 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001062
1063struct k_fifo {
1064 _wait_q_t wait_q;
1065 sys_slist_t data_q;
1066
Anas Nashif2f203c22016-12-18 06:57:45 -05001067 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001068};
1069
Allan Stephensc98da842016-11-11 15:45:03 -05001070#define K_FIFO_INITIALIZER(obj) \
1071 { \
1072 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1073 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001074 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001075 }
1076
1077/**
1078 * INTERNAL_HIDDEN @endcond
1079 */
1080
1081/**
1082 * @defgroup fifo_apis Fifo APIs
1083 * @ingroup kernel_apis
1084 * @{
1085 */
1086
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001087/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001088 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
1094 * @return N/A
1095 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097
1098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * This routine adds a data item to @a fifo. A fifo data item must be
1102 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1103 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001105 * @note Can be called by ISRs.
1106 *
1107 * @param fifo Address of the fifo.
1108 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
1110 * @return N/A
1111 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113
1114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * This routine adds a list of data items to @a fifo in one operation.
1118 * The data items must be in a singly-linked list, with the first 32 bits
1119 * each data item pointing to the next data item; the list must be
1120 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * @param fifo Address of the fifo.
1125 * @param head Pointer to first node in singly-linked list.
1126 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001127 *
1128 * @return N/A
1129 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001130extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131
1132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * This routine adds a list of data items to @a fifo in one operation.
1136 * The data items must be in a singly-linked list implemented using a
1137 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 * and must be re-initialized via sys_slist_init().
1139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001140 * @note Can be called by ISRs.
1141 *
1142 * @param fifo Address of the fifo.
1143 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144 *
1145 * @return N/A
1146 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148
1149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * This routine removes a data item from @a fifo in a "first in, first out"
1153 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001155 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1156 *
1157 * @param fifo Address of the fifo.
1158 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 * or one of the special values K_NO_WAIT and K_FOREVER.
1160 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001161 * @return Address of the data item if successful; NULL if returned
1162 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001164extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1165
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001171 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001173 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001175#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001176 struct k_fifo name \
1177 __in_section(_k_fifo, static, name) = \
1178 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179
Allan Stephensc98da842016-11-11 15:45:03 -05001180/**
1181 * @} end defgroup fifo_apis
1182 */
1183
1184/**
1185 * @cond INTERNAL_HIDDEN
1186 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187
1188struct k_lifo {
1189 _wait_q_t wait_q;
1190 void *list;
1191
Anas Nashif2f203c22016-12-18 06:57:45 -05001192 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193};
1194
Allan Stephensc98da842016-11-11 15:45:03 -05001195#define K_LIFO_INITIALIZER(obj) \
1196 { \
1197 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1198 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001199 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001200 }
1201
1202/**
1203 * INTERNAL_HIDDEN @endcond
1204 */
1205
1206/**
1207 * @defgroup lifo_apis Lifo APIs
1208 * @ingroup kernel_apis
1209 * @{
1210 */
1211
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001213 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
1219 * @return N/A
1220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001221extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222
1223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * This routine adds a data item to @a lifo. A lifo data item must be
1227 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1228 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001230 * @note Can be called by ISRs.
1231 *
1232 * @param lifo Address of the lifo.
1233 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
1235 * @return N/A
1236 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001237extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238
1239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * This routine removes a data item from @a lifo in a "last in, first out"
1243 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001245 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1246 *
1247 * @param lifo Address of the lifo.
1248 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001249 * or one of the special values K_NO_WAIT and K_FOREVER.
1250 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001251 * @return Address of the data item if successful; NULL if returned
1252 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001254extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1255
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001257 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001259 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001261 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001265#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001266 struct k_lifo name \
1267 __in_section(_k_lifo, static, name) = \
1268 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269
Allan Stephensc98da842016-11-11 15:45:03 -05001270/**
1271 * @} end defgroup lifo_apis
1272 */
1273
1274/**
1275 * @cond INTERNAL_HIDDEN
1276 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001277
1278struct k_stack {
1279 _wait_q_t wait_q;
1280 uint32_t *base, *next, *top;
1281
Anas Nashif2f203c22016-12-18 06:57:45 -05001282 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001283};
1284
Allan Stephensc98da842016-11-11 15:45:03 -05001285#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1286 { \
1287 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1288 .base = stack_buffer, \
1289 .next = stack_buffer, \
1290 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001291 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001292 }
1293
1294/**
1295 * INTERNAL_HIDDEN @endcond
1296 */
1297
1298/**
1299 * @defgroup stack_apis Stack APIs
1300 * @ingroup kernel_apis
1301 * @{
1302 */
1303
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001304/**
1305 * @brief Initialize a stack.
1306 *
1307 * This routine initializes a stack object, prior to its first use.
1308 *
1309 * @param stack Address of the stack.
1310 * @param buffer Address of array used to hold stacked values.
1311 * @param num_entries Maximum number of values that can be stacked.
1312 *
1313 * @return N/A
1314 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001315extern void k_stack_init(struct k_stack *stack,
1316 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317
1318/**
1319 * @brief Push an element onto a stack.
1320 *
1321 * This routine adds a 32-bit value @a data to @a stack.
1322 *
1323 * @note Can be called by ISRs.
1324 *
1325 * @param stack Address of the stack.
1326 * @param data Value to push onto the stack.
1327 *
1328 * @return N/A
1329 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001331
1332/**
1333 * @brief Pop an element from a stack.
1334 *
1335 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1336 * manner and stores the value in @a data.
1337 *
1338 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1339 *
1340 * @param stack Address of the stack.
1341 * @param data Address of area to hold the value popped from the stack.
1342 * @param timeout Waiting period to obtain a value (in milliseconds),
1343 * or one of the special values K_NO_WAIT and K_FOREVER.
1344 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001345 * @retval 0 Element popped from stack.
1346 * @retval -EBUSY Returned without waiting.
1347 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001348 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001349extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1350
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001355 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001356 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @param name Name of the stack.
1359 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001360 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001361#define K_STACK_DEFINE(name, stack_num_entries) \
1362 uint32_t __noinit \
1363 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001364 struct k_stack name \
1365 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001366 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1367 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001368
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001369/**
Allan Stephensc98da842016-11-11 15:45:03 -05001370 * @} end defgroup stack_apis
1371 */
1372
Allan Stephens6bba9b02016-11-16 14:56:54 -05001373struct k_work;
1374
Allan Stephensc98da842016-11-11 15:45:03 -05001375/**
1376 * @defgroup workqueue_apis Workqueue Thread APIs
1377 * @ingroup kernel_apis
1378 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379 */
1380
Allan Stephens6bba9b02016-11-16 14:56:54 -05001381/**
1382 * @typedef k_work_handler_t
1383 * @brief Work item handler function type.
1384 *
1385 * A work item's handler function is executed by a workqueue's thread
1386 * when the work item is processed by the workqueue.
1387 *
1388 * @param work Address of the work item.
1389 *
1390 * @return N/A
1391 */
1392typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393
1394/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001395 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001397
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398struct k_work_q {
1399 struct k_fifo fifo;
1400};
1401
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001403 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404};
1405
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406struct k_work {
1407 void *_reserved; /* Used by k_fifo implementation. */
1408 k_work_handler_t handler;
1409 atomic_t flags[1];
1410};
1411
Allan Stephens6bba9b02016-11-16 14:56:54 -05001412struct k_delayed_work {
1413 struct k_work work;
1414 struct _timeout timeout;
1415 struct k_work_q *work_q;
1416};
1417
1418extern struct k_work_q k_sys_work_q;
1419
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001421 * INTERNAL_HIDDEN @endcond
1422 */
1423
1424/**
1425 * @brief Initialize a statically-defined work item.
1426 *
1427 * This macro can be used to initialize a statically-defined workqueue work
1428 * item, prior to its first use. For example,
1429 *
1430 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1431 *
1432 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433 */
1434#define K_WORK_INITIALIZER(work_handler) \
1435 { \
1436 ._reserved = NULL, \
1437 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001438 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001439 }
1440
1441/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001442 * @brief Initialize a work item.
1443 *
1444 * This routine initializes a workqueue work item, prior to its first use.
1445 *
1446 * @param work Address of work item.
1447 * @param handler Function to invoke each time work item is processed.
1448 *
1449 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450 */
1451static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1452{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001453 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454 work->handler = handler;
1455}
1456
1457/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001459 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001460 * This routine submits work item @a work to be processed by workqueue
1461 * @a work_q. If the work item is already pending in the workqueue's queue
1462 * as a result of an earlier submission, this routine has no effect on the
1463 * work item. If the work item has already been processed, or is currently
1464 * being processed, its work is considered complete and the work item can be
1465 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001466 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001467 * @warning
1468 * A submitted work item must not be modified until it has been processed
1469 * by the workqueue.
1470 *
1471 * @note Can be called by ISRs.
1472 *
1473 * @param work_q Address of workqueue.
1474 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001475 *
1476 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001477 */
1478static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1479 struct k_work *work)
1480{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001481 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001482 k_fifo_put(&work_q->fifo, work);
1483 }
1484}
1485
1486/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001487 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001488 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001489 * This routine indicates if work item @a work is pending in a workqueue's
1490 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001491 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001492 * @note Can be called by ISRs.
1493 *
1494 * @param work Address of work item.
1495 *
1496 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001497 */
1498static inline int k_work_pending(struct k_work *work)
1499{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001500 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001501}
1502
1503/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001504 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001505 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001506 * This routine starts workqueue @a work_q. The workqueue spawns its work
1507 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001509 * @param work_q Address of workqueue.
1510 * @param stack Pointer to work queue thread's stack space.
1511 * @param stack_size Size of the work queue thread's stack (in bytes).
1512 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513 *
1514 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515 */
Allan Stephens904cf972016-10-07 13:59:23 -05001516extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001517 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001518
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001519/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001520 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001521 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001522 * This routine initializes a workqueue delayed work item, prior to
1523 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001524 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001525 * @param work Address of delayed work item.
1526 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 *
1528 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001529 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001530extern void k_delayed_work_init(struct k_delayed_work *work,
1531 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001532
1533/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001534 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001535 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001536 * This routine schedules work item @a work to be processed by workqueue
1537 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1538 * an asychronous countdown for the work item and then returns to the caller.
1539 * Only when the countdown completes is the work item actually submitted to
1540 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001541 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001542 * Submitting a previously submitted delayed work item that is still
1543 * counting down cancels the existing submission and restarts the countdown
1544 * using the new delay. If the work item is currently pending on the
1545 * workqueue's queue because the countdown has completed it is too late to
1546 * resubmit the item, and resubmission fails without impacting the work item.
1547 * If the work item has already been processed, or is currently being processed,
1548 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001549 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001550 * @warning
1551 * A delayed work item must not be modified until it has been processed
1552 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001553 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001554 * @note Can be called by ISRs.
1555 *
1556 * @param work_q Address of workqueue.
1557 * @param work Address of delayed work item.
1558 * @param delay Delay before submitting the work item (in milliseconds).
1559 *
1560 * @retval 0 Work item countdown started.
1561 * @retval -EINPROGRESS Work item is already pending.
1562 * @retval -EINVAL Work item is being processed or has completed its work.
1563 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001564 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001565extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1566 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001567 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568
1569/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001570 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001571 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001572 * This routine cancels the submission of delayed work item @a work.
1573 * A delayed work item can only be cancelled while its countdown is still
1574 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001575 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001576 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001577 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001578 * @param work Address of delayed work item.
1579 *
1580 * @retval 0 Work item countdown cancelled.
1581 * @retval -EINPROGRESS Work item is already pending.
1582 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001583 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001584extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001586/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587 * @brief Submit a work item to the system workqueue.
1588 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001589 * This routine submits work item @a work to be processed by the system
1590 * workqueue. If the work item is already pending in the workqueue's queue
1591 * as a result of an earlier submission, this routine has no effect on the
1592 * work item. If the work item has already been processed, or is currently
1593 * being processed, its work is considered complete and the work item can be
1594 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001595 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001596 * @warning
1597 * Work items submitted to the system workqueue should avoid using handlers
1598 * that block or yield since this may prevent the system workqueue from
1599 * processing other work items in a timely manner.
1600 *
1601 * @note Can be called by ISRs.
1602 *
1603 * @param work Address of work item.
1604 *
1605 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001606 */
1607static inline void k_work_submit(struct k_work *work)
1608{
1609 k_work_submit_to_queue(&k_sys_work_q, work);
1610}
1611
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001612/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001613 * @brief Submit a delayed work item to the system workqueue.
1614 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001615 * This routine schedules work item @a work to be processed by the system
1616 * workqueue after a delay of @a delay milliseconds. The routine initiates
1617 * an asychronous countdown for the work item and then returns to the caller.
1618 * Only when the countdown completes is the work item actually submitted to
1619 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001620 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001621 * Submitting a previously submitted delayed work item that is still
1622 * counting down cancels the existing submission and restarts the countdown
1623 * using the new delay. If the work item is currently pending on the
1624 * workqueue's queue because the countdown has completed it is too late to
1625 * resubmit the item, and resubmission fails without impacting the work item.
1626 * If the work item has already been processed, or is currently being processed,
1627 * its work is considered complete and the work item can be resubmitted.
1628 *
1629 * @warning
1630 * Work items submitted to the system workqueue should avoid using handlers
1631 * that block or yield since this may prevent the system workqueue from
1632 * processing other work items in a timely manner.
1633 *
1634 * @note Can be called by ISRs.
1635 *
1636 * @param work Address of delayed work item.
1637 * @param delay Delay before submitting the work item (in milliseconds).
1638 *
1639 * @retval 0 Work item countdown started.
1640 * @retval -EINPROGRESS Work item is already pending.
1641 * @retval -EINVAL Work item is being processed or has completed its work.
1642 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643 */
1644static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001645 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001646{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001647 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648}
1649
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001650/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001651 * @brief Get time remaining before a delayed work gets scheduled.
1652 *
1653 * This routine computes the (approximate) time remaining before a
1654 * delayed work gets executed. If the delayed work is not waiting to be
1655 * schedules, it returns zero.
1656 *
1657 * @param work Delayed work item.
1658 *
1659 * @return Remaining time (in milliseconds).
1660 */
1661static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1662{
1663 return _timeout_remaining_get(&work->timeout);
1664}
1665
1666/**
Allan Stephensc98da842016-11-11 15:45:03 -05001667 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001668 */
1669
Allan Stephensc98da842016-11-11 15:45:03 -05001670/**
1671 * @cond INTERNAL_HIDDEN
1672 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001673
1674struct k_mutex {
1675 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001676 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001677 uint32_t lock_count;
1678 int owner_orig_prio;
1679#ifdef CONFIG_OBJECT_MONITOR
1680 int num_lock_state_changes;
1681 int num_conflicts;
1682#endif
1683
Anas Nashif2f203c22016-12-18 06:57:45 -05001684 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001685};
1686
1687#ifdef CONFIG_OBJECT_MONITOR
1688#define _MUTEX_INIT_OBJECT_MONITOR \
1689 .num_lock_state_changes = 0, .num_conflicts = 0,
1690#else
1691#define _MUTEX_INIT_OBJECT_MONITOR
1692#endif
1693
1694#define K_MUTEX_INITIALIZER(obj) \
1695 { \
1696 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1697 .owner = NULL, \
1698 .lock_count = 0, \
1699 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1700 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001701 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001702 }
1703
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001704/**
Allan Stephensc98da842016-11-11 15:45:03 -05001705 * INTERNAL_HIDDEN @endcond
1706 */
1707
1708/**
1709 * @defgroup mutex_apis Mutex APIs
1710 * @ingroup kernel_apis
1711 * @{
1712 */
1713
1714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001718 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001719 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001723#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001724 struct k_mutex name \
1725 __in_section(_k_mutex, static, name) = \
1726 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * Upon completion, the mutex is available and does not have an owner.
1734 *
1735 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736 *
1737 * @return N/A
1738 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001739extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740
1741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * This routine locks @a mutex. If the mutex is locked by another thread,
1745 * the calling thread waits until the mutex becomes available or until
1746 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001748 * A thread is permitted to lock a mutex it has already locked. The operation
1749 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @param mutex Address of the mutex.
1752 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 * or one of the special values K_NO_WAIT and K_FOREVER.
1754 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001755 * @retval 0 Mutex locked.
1756 * @retval -EBUSY Returned without waiting.
1757 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760
1761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * This routine unlocks @a mutex. The mutex must already be locked by the
1765 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
1767 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 * thread.
1770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
1773 * @return N/A
1774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001775extern void k_mutex_unlock(struct k_mutex *mutex);
1776
Allan Stephensc98da842016-11-11 15:45:03 -05001777/**
1778 * @} end defgroup mutex_apis
1779 */
1780
1781/**
1782 * @cond INTERNAL_HIDDEN
1783 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001784
1785struct k_sem {
1786 _wait_q_t wait_q;
1787 unsigned int count;
1788 unsigned int limit;
1789
Anas Nashif2f203c22016-12-18 06:57:45 -05001790 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791};
1792
Allan Stephensc98da842016-11-11 15:45:03 -05001793#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1794 { \
1795 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1796 .count = initial_count, \
1797 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001798 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001799 }
1800
1801/**
1802 * INTERNAL_HIDDEN @endcond
1803 */
1804
1805/**
1806 * @defgroup semaphore_apis Semaphore APIs
1807 * @ingroup kernel_apis
1808 * @{
1809 */
1810
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @param sem Address of the semaphore.
1817 * @param initial_count Initial semaphore count.
1818 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001819 *
1820 * @return N/A
1821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001822extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1823 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001824
1825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001826 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001830 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1831 *
1832 * @param sem Address of the semaphore.
1833 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001834 * or one of the special values K_NO_WAIT and K_FOREVER.
1835 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001836 * @note When porting code from the nanokernel legacy API to the new API, be
1837 * careful with the return value of this function. The return value is the
1838 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1839 * non-zero means failure, while the nano_sem_take family returns 1 for success
1840 * and 0 for failure.
1841 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001842 * @retval 0 Semaphore taken.
1843 * @retval -EBUSY Returned without waiting.
1844 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001845 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001846extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001847
1848/**
1849 * @brief Give a semaphore.
1850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001851 * This routine gives @a sem, unless the semaphore is already at its maximum
1852 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001857 *
1858 * @return N/A
1859 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001860extern void k_sem_give(struct k_sem *sem);
1861
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001862/**
1863 * @brief Reset a semaphore's count to zero.
1864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001868 *
1869 * @return N/A
1870 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001871static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001872{
1873 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001874}
1875
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001876/**
1877 * @brief Get a semaphore's count.
1878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001883 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001884 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001885static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001886{
1887 return sem->count;
1888}
1889
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001894 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001895 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * @param name Name of the semaphore.
1898 * @param initial_count Initial semaphore count.
1899 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001900 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001902 struct k_sem name \
1903 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001904 K_SEM_INITIALIZER(name, initial_count, count_limit)
1905
Allan Stephensc98da842016-11-11 15:45:03 -05001906/**
1907 * @} end defgroup semaphore_apis
1908 */
1909
1910/**
1911 * @defgroup alert_apis Alert APIs
1912 * @ingroup kernel_apis
1913 * @{
1914 */
1915
Allan Stephens5eceb852016-11-16 10:16:30 -05001916/**
1917 * @typedef k_alert_handler_t
1918 * @brief Alert handler function type.
1919 *
1920 * An alert's alert handler function is invoked by the system workqueue
1921 * when the alert is signalled. The alert handler function is optional,
1922 * and is only invoked if the alert has been initialized with one.
1923 *
1924 * @param alert Address of the alert.
1925 *
1926 * @return 0 if alert has been consumed; non-zero if alert should pend.
1927 */
1928typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001929
1930/**
1931 * @} end defgroup alert_apis
1932 */
1933
1934/**
1935 * @cond INTERNAL_HIDDEN
1936 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001938#define K_ALERT_DEFAULT NULL
1939#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001941struct k_alert {
1942 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001943 atomic_t send_count;
1944 struct k_work work_item;
1945 struct k_sem sem;
1946
Anas Nashif2f203c22016-12-18 06:57:45 -05001947 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948};
1949
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001950extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001952#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001953 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001954 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001956 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001957 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001958 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959 }
1960
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961/**
Allan Stephensc98da842016-11-11 15:45:03 -05001962 * INTERNAL_HIDDEN @endcond
1963 */
1964
1965/**
1966 * @addtogroup alert_apis
1967 * @{
1968 */
1969
1970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001973 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001975 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @param name Name of the alert.
1978 * @param alert_handler Action to take when alert is sent. Specify either
1979 * the address of a function to be invoked by the system workqueue
1980 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1981 * K_ALERT_DEFAULT (which causes the alert to pend).
1982 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001984#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001985 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001986 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001987 K_ALERT_INITIALIZER(name, alert_handler, \
1988 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001995 * @param alert Address of the alert.
1996 * @param handler Action to take when alert is sent. Specify either the address
1997 * of a function to be invoked by the system workqueue thread,
1998 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1999 * K_ALERT_DEFAULT (which causes the alert to pend).
2000 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
2002 * @return N/A
2003 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002004extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2005 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006
2007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2013 *
2014 * @param alert Address of the alert.
2015 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016 * or one of the special values K_NO_WAIT and K_FOREVER.
2017 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002018 * @retval 0 Alert received.
2019 * @retval -EBUSY Returned without waiting.
2020 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002022extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002023
2024/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027 * This routine signals @a alert. The action specified for @a alert will
2028 * be taken, which may trigger the execution of an alert handler function
2029 * and/or cause the alert to pend (assuming the alert has not reached its
2030 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * @note Can be called by ISRs.
2033 *
2034 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
2036 * @return N/A
2037 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002038extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002039
2040/**
Allan Stephensc98da842016-11-11 15:45:03 -05002041 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042 */
2043
Allan Stephensc98da842016-11-11 15:45:03 -05002044/**
2045 * @cond INTERNAL_HIDDEN
2046 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047
2048struct k_msgq {
2049 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002050 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051 uint32_t max_msgs;
2052 char *buffer_start;
2053 char *buffer_end;
2054 char *read_ptr;
2055 char *write_ptr;
2056 uint32_t used_msgs;
2057
Anas Nashif2f203c22016-12-18 06:57:45 -05002058 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059};
2060
Peter Mitsis1da807e2016-10-06 11:36:59 -04002061#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062 { \
2063 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002064 .max_msgs = q_max_msgs, \
2065 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002067 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068 .read_ptr = q_buffer, \
2069 .write_ptr = q_buffer, \
2070 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002071 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072 }
2073
Peter Mitsis1da807e2016-10-06 11:36:59 -04002074/**
Allan Stephensc98da842016-11-11 15:45:03 -05002075 * INTERNAL_HIDDEN @endcond
2076 */
2077
2078/**
2079 * @defgroup msgq_apis Message Queue APIs
2080 * @ingroup kernel_apis
2081 * @{
2082 */
2083
2084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002087 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2088 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002089 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2090 * message is similarly aligned to this boundary, @a q_msg_size must also be
2091 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * The message queue can be accessed outside the module where it is defined
2094 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002096 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @param q_name Name of the message queue.
2099 * @param q_msg_size Message size (in bytes).
2100 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002101 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002102 */
2103#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2104 static char __noinit __aligned(q_align) \
2105 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002106 struct k_msgq q_name \
2107 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002108 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2109 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110
Peter Mitsisd7a37502016-10-13 11:37:40 -04002111/**
2112 * @brief Initialize a message queue.
2113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * This routine initializes a message queue object, prior to its first use.
2115 *
Allan Stephensda827222016-11-09 14:23:58 -06002116 * The message queue's ring buffer must contain space for @a max_msgs messages,
2117 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2118 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2119 * that each message is similarly aligned to this boundary, @a q_msg_size
2120 * must also be a multiple of N.
2121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002122 * @param q Address of the message queue.
2123 * @param buffer Pointer to ring buffer that holds queued messages.
2124 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002125 * @param max_msgs Maximum number of messages that can be queued.
2126 *
2127 * @return N/A
2128 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002129extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002130 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002131
2132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002136 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002137 * @note Can be called by ISRs.
2138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002139 * @param q Address of the message queue.
2140 * @param data Pointer to the message.
2141 * @param timeout Waiting period to add the message (in milliseconds),
2142 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002143 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002144 * @retval 0 Message sent.
2145 * @retval -ENOMSG Returned without waiting or queue purged.
2146 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002149
2150/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002151 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * This routine receives a message from message queue @a q in a "first in,
2154 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002155 *
Allan Stephensc98da842016-11-11 15:45:03 -05002156 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002158 * @param q Address of the message queue.
2159 * @param data Address of area to hold the received message.
2160 * @param timeout Waiting period to receive the message (in milliseconds),
2161 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002162 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002163 * @retval 0 Message received.
2164 * @retval -ENOMSG Returned without waiting.
2165 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002166 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002168
2169/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * This routine discards all unreceived messages in a message queue's ring
2173 * buffer. Any threads that are blocked waiting to send a message to the
2174 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002176 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002177 *
2178 * @return N/A
2179 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180extern void k_msgq_purge(struct k_msgq *q);
2181
Peter Mitsis67be2492016-10-07 11:44:34 -04002182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * This routine returns the number of unused entries in a message queue's
2186 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002188 * @param q Address of the message queue.
2189 *
2190 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002191 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002192static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002193{
2194 return q->max_msgs - q->used_msgs;
2195}
2196
Peter Mitsisd7a37502016-10-13 11:37:40 -04002197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002198 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002200 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * @param q Address of the message queue.
2203 *
2204 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002205 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002206static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207{
2208 return q->used_msgs;
2209}
2210
Allan Stephensc98da842016-11-11 15:45:03 -05002211/**
2212 * @} end defgroup msgq_apis
2213 */
2214
2215/**
2216 * @defgroup mem_pool_apis Memory Pool APIs
2217 * @ingroup kernel_apis
2218 * @{
2219 */
2220
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002222 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223 void *addr_in_pool;
2224 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002225 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226};
2227
Allan Stephensc98da842016-11-11 15:45:03 -05002228/**
2229 * @} end defgroup mem_pool_apis
2230 */
2231
2232/**
2233 * @defgroup mailbox_apis Mailbox APIs
2234 * @ingroup kernel_apis
2235 * @{
2236 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237
2238struct k_mbox_msg {
2239 /** internal use only - needed for legacy API support */
2240 uint32_t _mailbox;
2241 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002242 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243 /** application-defined information value */
2244 uint32_t info;
2245 /** sender's message data buffer */
2246 void *tx_data;
2247 /** internal use only - needed for legacy API support */
2248 void *_rx_data;
2249 /** message data block descriptor */
2250 struct k_mem_block tx_block;
2251 /** source thread id */
2252 k_tid_t rx_source_thread;
2253 /** target thread id */
2254 k_tid_t tx_target_thread;
2255 /** internal use only - thread waiting on send (may be a dummy) */
2256 k_tid_t _syncing_thread;
2257#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2258 /** internal use only - semaphore used during asynchronous send */
2259 struct k_sem *_async_sem;
2260#endif
2261};
2262
Allan Stephensc98da842016-11-11 15:45:03 -05002263/**
2264 * @cond INTERNAL_HIDDEN
2265 */
2266
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267struct k_mbox {
2268 _wait_q_t tx_msg_queue;
2269 _wait_q_t rx_msg_queue;
2270
Anas Nashif2f203c22016-12-18 06:57:45 -05002271 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272};
2273
2274#define K_MBOX_INITIALIZER(obj) \
2275 { \
2276 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2277 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002278 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279 }
2280
Peter Mitsis12092702016-10-14 12:57:23 -04002281/**
Allan Stephensc98da842016-11-11 15:45:03 -05002282 * INTERNAL_HIDDEN @endcond
2283 */
2284
2285/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002286 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002288 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002290 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002292 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002295 struct k_mbox name \
2296 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297 K_MBOX_INITIALIZER(name) \
2298
Peter Mitsis12092702016-10-14 12:57:23 -04002299/**
2300 * @brief Initialize a mailbox.
2301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002302 * This routine initializes a mailbox object, prior to its first use.
2303 *
2304 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002305 *
2306 * @return N/A
2307 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308extern void k_mbox_init(struct k_mbox *mbox);
2309
Peter Mitsis12092702016-10-14 12:57:23 -04002310/**
2311 * @brief Send a mailbox message in a synchronous manner.
2312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * This routine sends a message to @a mbox and waits for a receiver to both
2314 * receive and process it. The message data may be in a buffer, in a memory
2315 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * @param mbox Address of the mailbox.
2318 * @param tx_msg Address of the transmit message descriptor.
2319 * @param timeout Waiting period for the message to be received (in
2320 * milliseconds), or one of the special values K_NO_WAIT
2321 * and K_FOREVER. Once the message has been received,
2322 * this routine waits as long as necessary for the message
2323 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002324 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002325 * @retval 0 Message sent.
2326 * @retval -ENOMSG Returned without waiting.
2327 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002328 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002329extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002331
Peter Mitsis12092702016-10-14 12:57:23 -04002332/**
2333 * @brief Send a mailbox message in an asynchronous manner.
2334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * This routine sends a message to @a mbox without waiting for a receiver
2336 * to process it. The message data may be in a buffer, in a memory pool block,
2337 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2338 * will be given when the message has been both received and completely
2339 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341 * @param mbox Address of the mailbox.
2342 * @param tx_msg Address of the transmit message descriptor.
2343 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002344 *
2345 * @return N/A
2346 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002347extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 struct k_sem *sem);
2349
Peter Mitsis12092702016-10-14 12:57:23 -04002350/**
2351 * @brief Receive a mailbox message.
2352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * This routine receives a message from @a mbox, then optionally retrieves
2354 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @param mbox Address of the mailbox.
2357 * @param rx_msg Address of the receive message descriptor.
2358 * @param buffer Address of the buffer to receive data, or NULL to defer data
2359 * retrieval and message disposal until later.
2360 * @param timeout Waiting period for a message to be received (in
2361 * milliseconds), or one of the special values K_NO_WAIT
2362 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002363 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002364 * @retval 0 Message received.
2365 * @retval -ENOMSG Returned without waiting.
2366 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002367 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002368extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002370
2371/**
2372 * @brief Retrieve mailbox message data into a buffer.
2373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * This routine completes the processing of a received message by retrieving
2375 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002376 *
2377 * Alternatively, this routine can be used to dispose of a received message
2378 * without retrieving its data.
2379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @param rx_msg Address of the receive message descriptor.
2381 * @param buffer Address of the buffer to receive data, or NULL to discard
2382 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002383 *
2384 * @return N/A
2385 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002386extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002387
2388/**
2389 * @brief Retrieve mailbox message data into a memory pool block.
2390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * This routine completes the processing of a received message by retrieving
2392 * its data into a memory pool block, then disposing of the message.
2393 * The memory pool block that results from successful retrieval must be
2394 * returned to the pool once the data has been processed, even in cases
2395 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002396 *
2397 * Alternatively, this routine can be used to dispose of a received message
2398 * without retrieving its data. In this case there is no need to return a
2399 * memory pool block to the pool.
2400 *
2401 * This routine allocates a new memory pool block for the data only if the
2402 * data is not already in one. If a new block cannot be allocated, the routine
2403 * returns a failure code and the received message is left unchanged. This
2404 * permits the caller to reattempt data retrieval at a later time or to dispose
2405 * of the received message without retrieving its data.
2406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @param rx_msg Address of a receive message descriptor.
2408 * @param pool Address of memory pool, or NULL to discard data.
2409 * @param block Address of the area to hold memory pool block info.
2410 * @param timeout Waiting period to wait for a memory pool block (in
2411 * milliseconds), or one of the special values K_NO_WAIT
2412 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002413 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002414 * @retval 0 Data retrieved.
2415 * @retval -ENOMEM Returned without waiting.
2416 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002417 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002418extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002419 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420 struct k_mem_block *block, int32_t timeout);
2421
Allan Stephensc98da842016-11-11 15:45:03 -05002422/**
2423 * @} end defgroup mailbox_apis
2424 */
2425
2426/**
2427 * @cond INTERNAL_HIDDEN
2428 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429
2430struct k_pipe {
2431 unsigned char *buffer; /* Pipe buffer: may be NULL */
2432 size_t size; /* Buffer size */
2433 size_t bytes_used; /* # bytes used in buffer */
2434 size_t read_index; /* Where in buffer to read from */
2435 size_t write_index; /* Where in buffer to write */
2436
2437 struct {
2438 _wait_q_t readers; /* Reader wait queue */
2439 _wait_q_t writers; /* Writer wait queue */
2440 } wait_q;
2441
Anas Nashif2f203c22016-12-18 06:57:45 -05002442 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443};
2444
Peter Mitsise5d9c582016-10-14 14:44:57 -04002445#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446 { \
2447 .buffer = pipe_buffer, \
2448 .size = pipe_buffer_size, \
2449 .bytes_used = 0, \
2450 .read_index = 0, \
2451 .write_index = 0, \
2452 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2453 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002454 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455 }
2456
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457/**
Allan Stephensc98da842016-11-11 15:45:03 -05002458 * INTERNAL_HIDDEN @endcond
2459 */
2460
2461/**
2462 * @defgroup pipe_apis Pipe APIs
2463 * @ingroup kernel_apis
2464 * @{
2465 */
2466
2467/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002468 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002472 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * @param name Name of the pipe.
2475 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2476 * or zero if no ring buffer is used.
2477 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002479#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2480 static unsigned char __noinit __aligned(pipe_align) \
2481 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002482 struct k_pipe name \
2483 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002484 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * This routine initializes a pipe object, prior to its first use.
2490 *
2491 * @param pipe Address of the pipe.
2492 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2493 * is used.
2494 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2495 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496 *
2497 * @return N/A
2498 */
2499extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2500 size_t size);
2501
2502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @param pipe Address of the pipe.
2508 * @param data Address of data to write.
2509 * @param bytes_to_write Size of data (in bytes).
2510 * @param bytes_written Address of area to hold the number of bytes written.
2511 * @param min_xfer Minimum number of bytes to write.
2512 * @param timeout Waiting period to wait for the data to be written (in
2513 * milliseconds), or one of the special values K_NO_WAIT
2514 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002516 * @retval 0 At least @a min_xfer bytes of data were written.
2517 * @retval -EIO Returned without waiting; zero data bytes were written.
2518 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002521extern int k_pipe_put(struct k_pipe *pipe, void *data,
2522 size_t bytes_to_write, size_t *bytes_written,
2523 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524
2525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @param pipe Address of the pipe.
2531 * @param data Address to place the data read from pipe.
2532 * @param bytes_to_read Maximum number of data bytes to read.
2533 * @param bytes_read Address of area to hold the number of bytes read.
2534 * @param min_xfer Minimum number of data bytes to read.
2535 * @param timeout Waiting period to wait for the data to be read (in
2536 * milliseconds), or one of the special values K_NO_WAIT
2537 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002539 * @retval 0 At least @a min_xfer bytes of data were read.
2540 * @retval -EIO Returned without waiting; zero data bytes were read.
2541 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002544extern int k_pipe_get(struct k_pipe *pipe, void *data,
2545 size_t bytes_to_read, size_t *bytes_read,
2546 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547
2548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * This routine writes the data contained in a memory block to @a pipe.
2552 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 * @param block Memory block containing data to send
2557 * @param size Number of data bytes in memory block to send
2558 * @param sem Semaphore to signal upon completion (else NULL)
2559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 */
2562extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2563 size_t size, struct k_sem *sem);
2564
2565/**
Allan Stephensc98da842016-11-11 15:45:03 -05002566 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567 */
2568
Allan Stephensc98da842016-11-11 15:45:03 -05002569/**
2570 * @cond INTERNAL_HIDDEN
2571 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002573struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002574 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002575 uint32_t num_blocks;
2576 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577 char *buffer;
2578 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002579 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580
Anas Nashif2f203c22016-12-18 06:57:45 -05002581 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582};
2583
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002584#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2585 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 { \
2587 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002588 .num_blocks = slab_num_blocks, \
2589 .block_size = slab_block_size, \
2590 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 .free_list = NULL, \
2592 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002593 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594 }
2595
Peter Mitsis578f9112016-10-07 13:50:31 -04002596/**
Allan Stephensc98da842016-11-11 15:45:03 -05002597 * INTERNAL_HIDDEN @endcond
2598 */
2599
2600/**
2601 * @defgroup mem_slab_apis Memory Slab APIs
2602 * @ingroup kernel_apis
2603 * @{
2604 */
2605
2606/**
Allan Stephensda827222016-11-09 14:23:58 -06002607 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002608 *
Allan Stephensda827222016-11-09 14:23:58 -06002609 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002611 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2612 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002613 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002614 *
Allan Stephensda827222016-11-09 14:23:58 -06002615 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002617 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002618 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * @param name Name of the memory slab.
2621 * @param slab_block_size Size of each memory block (in bytes).
2622 * @param slab_num_blocks Number memory blocks.
2623 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002624 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002625#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2626 char __noinit __aligned(slab_align) \
2627 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2628 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002629 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002630 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2631 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002633/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002634 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002637 *
Allan Stephensda827222016-11-09 14:23:58 -06002638 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2639 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2640 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2641 * To ensure that each memory block is similarly aligned to this boundary,
2642 * @a slab_block_size must also be a multiple of N.
2643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @param slab Address of the memory slab.
2645 * @param buffer Pointer to buffer used for the memory blocks.
2646 * @param block_size Size of each memory block (in bytes).
2647 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002648 *
2649 * @return N/A
2650 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002651extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002652 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002653
2654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002659 * @param slab Address of the memory slab.
2660 * @param mem Pointer to block address area.
2661 * @param timeout Maximum time to wait for operation to complete
2662 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2663 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002664 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002665 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002667 * @retval -ENOMEM Returned without waiting.
2668 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002669 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002670extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2671 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002672
2673/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * This routine releases a previously allocated memory block back to its
2677 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * @param slab Address of the memory slab.
2680 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002681 *
2682 * @return N/A
2683 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002684extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * This routine gets the number of memory blocks that are currently
2690 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002695 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002696static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002698 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699}
2700
Peter Mitsisc001aa82016-10-13 13:53:37 -04002701/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * This routine gets the number of memory blocks that are currently
2705 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002709 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002710 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002711static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002712{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002713 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002714}
2715
Allan Stephensc98da842016-11-11 15:45:03 -05002716/**
2717 * @} end defgroup mem_slab_apis
2718 */
2719
2720/**
2721 * @cond INTERNAL_HIDDEN
2722 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002724/*
2725 * Memory pool requires a buffer and two arrays of structures for the
2726 * memory block accounting:
2727 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2728 * status of four blocks of memory.
2729 */
2730struct k_mem_pool_quad_block {
2731 char *mem_blocks; /* pointer to the first of four memory blocks */
2732 uint32_t mem_status; /* four bits. If bit is set, memory block is
2733 allocated */
2734};
2735/*
2736 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2737 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2738 * block size is 4 times less than the previous one and thus requires 4 times
2739 * bigger array of k_mem_pool_quad_block structures to keep track of the
2740 * memory blocks.
2741 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002743/*
2744 * The array of k_mem_pool_block_set keeps the information of each array of
2745 * k_mem_pool_quad_block structures
2746 */
2747struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002748 size_t block_size; /* memory block size */
2749 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002750 struct k_mem_pool_quad_block *quad_block;
2751 int count;
2752};
2753
2754/* Memory pool descriptor */
2755struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002756 size_t max_block_size;
2757 size_t min_block_size;
2758 uint32_t nr_of_maxblocks;
2759 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002760 struct k_mem_pool_block_set *block_set;
2761 char *bufblock;
2762 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002763 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764};
2765
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002766#ifdef CONFIG_ARM
2767#define _SECTION_TYPE_SIGN "%"
2768#else
2769#define _SECTION_TYPE_SIGN "@"
2770#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002772/*
2773 * Static memory pool initialization
2774 */
Allan Stephensc98da842016-11-11 15:45:03 -05002775
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002776/*
2777 * Use .altmacro to be able to recalculate values and pass them as string
2778 * arguments when calling assembler macros resursively
2779 */
2780__asm__(".altmacro\n\t");
2781
2782/*
2783 * Recursively calls a macro
2784 * The followig global symbols need to be initialized:
2785 * __memory_pool_max_block_size - maximal size of the memory block
2786 * __memory_pool_min_block_size - minimal size of the memory block
2787 * Notes:
2788 * Global symbols are used due the fact that assembler macro allows only
2789 * one argument be passed with the % conversion
2790 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2791 * is used instead of / 4.
2792 * n_max argument needs to go first in the invoked macro, as some
2793 * assemblers concatenate \name and %(\n_max * 4) arguments
2794 * if \name goes first
2795 */
2796__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2797 ".ifge __memory_pool_max_block_size >> 2 -"
2798 " __memory_pool_min_block_size\n\t\t"
2799 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2800 "\\macro_name %(\\n_max * 4) \\name\n\t"
2801 ".endif\n\t"
2802 ".endm\n");
2803
2804/*
2805 * Build quad blocks
2806 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2807 * structures and recursively calls itself for the next array, 4 times
2808 * larger.
2809 * The followig global symbols need to be initialized:
2810 * __memory_pool_max_block_size - maximal size of the memory block
2811 * __memory_pool_min_block_size - minimal size of the memory block
2812 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2813 */
2814__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002815 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002816 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2817 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2818 ".if \\n_max % 4\n\t\t"
2819 ".skip __memory_pool_quad_block_size\n\t"
2820 ".endif\n\t"
2821 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2822 ".endm\n");
2823
2824/*
2825 * Build block sets and initialize them
2826 * Macro initializes the k_mem_pool_block_set structure and
2827 * recursively calls itself for the next one.
2828 * The followig global symbols need to be initialized:
2829 * __memory_pool_max_block_size - maximal size of the memory block
2830 * __memory_pool_min_block_size - minimal size of the memory block
2831 * __memory_pool_block_set_count, the number of the elements in the
2832 * block set array must be set to 0. Macro calculates it's real
2833 * value.
2834 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2835 * structures, _build_quad_blocks must be called prior it.
2836 */
2837__asm__(".macro _build_block_set n_max, name\n\t"
2838 ".int __memory_pool_max_block_size\n\t" /* block_size */
2839 ".if \\n_max % 4\n\t\t"
2840 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2841 ".else\n\t\t"
2842 ".int \\n_max >> 2\n\t"
2843 ".endif\n\t"
2844 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2845 ".int 0\n\t" /* count */
2846 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2847 "__do_recurse _build_block_set \\name \\n_max\n\t"
2848 ".endm\n");
2849
2850/*
2851 * Build a memory pool structure and initialize it
2852 * Macro uses __memory_pool_block_set_count global symbol,
2853 * block set addresses and buffer address, it may be called only after
2854 * _build_block_set
2855 */
2856__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002857 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002858 _SECTION_TYPE_SIGN "progbits\n\t"
2859 ".globl \\name\n\t"
2860 "\\name:\n\t"
2861 ".int \\max_size\n\t" /* max_block_size */
2862 ".int \\min_size\n\t" /* min_block_size */
2863 ".int \\n_max\n\t" /* nr_of_maxblocks */
2864 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2865 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2866 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2867 ".int 0\n\t" /* wait_q->head */
2868 ".int 0\n\t" /* wait_q->next */
2869 ".popsection\n\t"
2870 ".endm\n");
2871
2872#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2873 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2874 _SECTION_TYPE_SIGN "progbits\n\t"); \
2875 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2876 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2877 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2878 STRINGIFY(name) "\n\t"); \
2879 __asm__(".popsection\n\t")
2880
2881#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2882 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2883 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2884 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2885 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002886 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002887 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2888 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2889 STRINGIFY(name) "\n\t"); \
2890 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2891 __asm__(".int __memory_pool_block_set_count\n\t"); \
2892 __asm__(".popsection\n\t"); \
2893 extern uint32_t _mem_pool_block_set_count_##name; \
2894 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2895
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002896#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2897 char __noinit __aligned(align) \
2898 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002899
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002900/*
2901 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2902 * to __memory_pool_quad_block_size absolute symbol.
2903 * This function does not get called, but compiler calculates the value and
2904 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2905 */
2906static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2907{
2908 __asm__(".globl __memory_pool_quad_block_size\n\t"
2909#ifdef CONFIG_NIOS2
2910 "__memory_pool_quad_block_size = %0\n\t"
2911#else
2912 "__memory_pool_quad_block_size = %c0\n\t"
2913#endif
2914 :
2915 : "n"(sizeof(struct k_mem_pool_quad_block)));
2916}
2917
2918/**
Allan Stephensc98da842016-11-11 15:45:03 -05002919 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002920 */
2921
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002922/**
Allan Stephensc98da842016-11-11 15:45:03 -05002923 * @addtogroup mem_pool_apis
2924 * @{
2925 */
2926
2927/**
2928 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2931 * long. The memory pool allows blocks to be repeatedly partitioned into
2932 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2933 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002934 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002936 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002937 * If the pool is to be accessed outside the module where it is defined, it
2938 * can be declared via
2939 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002940 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @param name Name of the memory pool.
2943 * @param min_size Size of the smallest blocks in the pool (in bytes).
2944 * @param max_size Size of the largest blocks in the pool (in bytes).
2945 * @param n_max Number of maximum sized blocks in the pool.
2946 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002947 */
2948#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002949 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2950 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002951 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002952 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2953 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2954 extern struct k_mem_pool name
2955
Peter Mitsis937042c2016-10-13 13:18:26 -04002956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param pool Address of the memory pool.
2962 * @param block Pointer to block descriptor for the allocated memory.
2963 * @param size Amount of memory to allocate (in bytes).
2964 * @param timeout Maximum time to wait for operation to complete
2965 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2966 * or K_FOREVER to wait as long as necessary.
2967 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002968 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002970 * @retval -ENOMEM Returned without waiting.
2971 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002972 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002973extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002974 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002975
2976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * This routine releases a previously allocated memory block back to its
2980 * memory pool.
2981 *
2982 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002983 *
2984 * @return N/A
2985 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002987
2988/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * This routine instructs a memory pool to concatenate unused memory blocks
2992 * into larger blocks wherever possible. Manually defragmenting the memory
2993 * pool may speed up future allocations of memory blocks by eliminating the
2994 * need for the memory pool to perform an automatic partial defragmentation.
2995 *
2996 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002997 *
2998 * @return N/A
2999 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003000extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003001
3002/**
Allan Stephensc98da842016-11-11 15:45:03 -05003003 * @} end addtogroup mem_pool_apis
3004 */
3005
3006/**
3007 * @defgroup heap_apis Heap Memory Pool APIs
3008 * @ingroup kernel_apis
3009 * @{
3010 */
3011
3012/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003016 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003021 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003022extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003023
3024/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003026 *
3027 * This routine provides traditional free() semantics. The memory being
3028 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003029 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003030 * If @a ptr is NULL, no operation is performed.
3031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003033 *
3034 * @return N/A
3035 */
3036extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037
Allan Stephensc98da842016-11-11 15:45:03 -05003038/**
3039 * @} end defgroup heap_apis
3040 */
3041
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003042/**
3043 * @brief Make the CPU idle.
3044 *
3045 * This function makes the CPU idle until an event wakes it up.
3046 *
3047 * In a regular system, the idle thread should be the only thread responsible
3048 * for making the CPU idle and triggering any type of power management.
3049 * However, in some more constrained systems, such as a single-threaded system,
3050 * the only thread would be responsible for this if needed.
3051 *
3052 * @return N/A
3053 */
3054extern void k_cpu_idle(void);
3055
3056/**
3057 * @brief Make the CPU idle in an atomic fashion.
3058 *
3059 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3060 * must be done atomically before making the CPU idle.
3061 *
3062 * @param key Interrupt locking key obtained from irq_lock().
3063 *
3064 * @return N/A
3065 */
3066extern void k_cpu_atomic_idle(unsigned int key);
3067
Andrew Boie350f88d2017-01-18 13:13:45 -08003068extern void _sys_power_save_idle_exit(int32_t ticks);
3069
Anas Nashifa6149502017-01-17 07:47:31 -05003070/* Include legacy APIs */
3071#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003073#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074#include <arch/cpu.h>
3075
3076/*
3077 * private APIs that are utilized by one or more public APIs
3078 */
3079
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003080#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003082#else
3083#define _init_static_threads() do { } while ((0))
3084#endif
3085
3086extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003087extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088
3089#ifdef __cplusplus
3090}
3091#endif
3092
Andrew Boiee004dec2016-11-07 09:01:19 -08003093#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3094/*
3095 * Define new and delete operators.
3096 * At this moment, the operators do nothing since objects are supposed
3097 * to be statically allocated.
3098 */
3099inline void operator delete(void *ptr)
3100{
3101 (void)ptr;
3102}
3103
3104inline void operator delete[](void *ptr)
3105{
3106 (void)ptr;
3107}
3108
3109inline void *operator new(size_t size)
3110{
3111 (void)size;
3112 return NULL;
3113}
3114
3115inline void *operator new[](size_t size)
3116{
3117 (void)size;
3118 return NULL;
3119}
3120
3121/* Placement versions of operator new and delete */
3122inline void operator delete(void *ptr1, void *ptr2)
3123{
3124 (void)ptr1;
3125 (void)ptr2;
3126}
3127
3128inline void operator delete[](void *ptr1, void *ptr2)
3129{
3130 (void)ptr1;
3131 (void)ptr2;
3132}
3133
3134inline void *operator new(size_t size, void *ptr)
3135{
3136 (void)size;
3137 return ptr;
3138}
3139
3140inline void *operator new[](size_t size, void *ptr)
3141{
3142 (void)size;
3143 return ptr;
3144}
3145
3146#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3147
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148#endif /* _kernel__h_ */