blob: 3e14ef2bfa1de3bd7df1d26633456442007f4beb [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
18#include <stdint.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
21#include <sections.h>
22#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Anas Nashifbbb157d2017-01-15 08:46:31 -050035/**
36 * @brief Kernel APIs
37 * @defgroup kernel_apis Kernel APIs
38 * @{
39 * @}
40 */
41
Anas Nashif61f4b242016-11-18 10:53:59 -050042#ifdef CONFIG_KERNEL_DEBUG
43#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040044#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
45#else
46#define K_DEBUG(fmt, ...)
47#endif
48
Benjamin Walsh2f280412017-01-14 19:23:46 -050049#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
50#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
51#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
52#elif defined(CONFIG_COOP_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
54#define _NUM_PREEMPT_PRIO (0)
55#elif defined(CONFIG_PREEMPT_ENABLED)
56#define _NUM_COOP_PRIO (0)
57#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
58#else
59#error "invalid configuration"
60#endif
61
62#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_PRIO_PREEMPT(x) (x)
64
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_ANY NULL
66#define K_END NULL
67
Benjamin Walshedb35702017-01-14 18:47:22 -050068#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050070#elif defined(CONFIG_COOP_ENABLED)
71#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
72#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050074#else
75#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#endif
77
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050078#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
80#else
81#define K_LOWEST_THREAD_PRIO -1
82#endif
83
Benjamin Walshfab8d922016-11-08 15:36:36 -050084#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
85
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
87#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
88
89typedef sys_dlist_t _wait_q_t;
90
Anas Nashif2f203c22016-12-18 06:57:45 -050091#ifdef CONFIG_OBJECT_TRACING
92#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
93#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094#else
Anas Nashif2f203c22016-12-18 06:57:45 -050095#define _OBJECT_TRACING_INIT
96#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#endif
98
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050099#define tcs k_thread
100struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101struct k_mutex;
102struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400103struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104struct k_msgq;
105struct k_mbox;
106struct k_pipe;
107struct k_fifo;
108struct k_lifo;
109struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400110struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mem_pool;
112struct k_timer;
113
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400114typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116enum execution_context_types {
117 K_ISR = 0,
118 K_COOP_THREAD,
119 K_PREEMPT_THREAD,
120};
121
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400122/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100123 * @defgroup profiling_apis Profiling APIs
124 * @ingroup kernel_apis
125 * @{
126 */
127
128/**
129 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
130 *
131 * This routine calls @ref stack_analyze on the 4 call stacks declared and
132 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
133 *
134 * CONFIG_MAIN_STACK_SIZE
135 * CONFIG_IDLE_STACK_SIZE
136 * CONFIG_ISR_STACK_SIZE
137 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
138 *
139 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
140 * produce output.
141 *
142 * @return N/A
143 */
144extern void k_call_stacks_analyze(void);
145
146/**
147 * @} end defgroup profiling_apis
148 */
149
150/**
Allan Stephensc98da842016-11-11 15:45:03 -0500151 * @defgroup thread_apis Thread APIs
152 * @ingroup kernel_apis
153 * @{
154 */
155
156/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500157 * @typedef k_thread_entry_t
158 * @brief Thread entry point function type.
159 *
160 * A thread's entry point function is invoked when the thread starts executing.
161 * Up to 3 argument values can be passed to the function.
162 *
163 * The thread terminates execution permanently if the entry point function
164 * returns. The thread is responsible for releasing any shared resources
165 * it may own (such as mutexes and dynamically allocated memory), prior to
166 * returning.
167 *
168 * @param p1 First argument.
169 * @param p2 Second argument.
170 * @param p3 Third argument.
171 *
172 * @return N/A
173 */
174typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
175
Benjamin Walshed240f22017-01-22 13:05:08 -0500176#endif /* !_ASMLANGUAGE */
177
178
179/*
180 * Thread user options. May be needed by assembly code. Common part uses low
181 * bits, arch-specific use high bits.
182 */
183
184/* system thread that must not abort */
185#define K_ESSENTIAL (1 << 0)
186
187#if defined(CONFIG_FP_SHARING)
188/* thread uses floating point registers */
189#define K_FP_REGS (1 << 1)
190#endif
191
192#ifdef CONFIG_X86
193/* x86 Bitmask definitions for threads user options */
194
195#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
196/* thread uses SSEx (and also FP) registers */
197#define K_SSE_REGS (1 << 7)
198#endif
199#endif
200
201/* end - thread options */
202
203#if !defined(_ASMLANGUAGE)
204
Allan Stephens5eceb852016-11-16 10:16:30 -0500205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500206 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500208 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * The new thread may be scheduled for immediate execution or a delayed start.
211 * If the newly spawned thread does not have a delayed start the kernel
212 * scheduler may preempt the current thread to allow the new thread to
213 * execute.
214 *
215 * Thread options are architecture-specific, and can include K_ESSENTIAL,
216 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
217 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
219 * @param stack Pointer to the stack space.
220 * @param stack_size Stack size in bytes.
221 * @param entry Thread entry function.
222 * @param p1 1st entry point parameter.
223 * @param p2 2nd entry point parameter.
224 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500225 * @param prio Thread priority.
226 * @param options Thread options.
227 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500231extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500232 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400233 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500234 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400235
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
Allan Stephensc98da842016-11-11 15:45:03 -0500239 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500240 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500242 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400243 *
244 * @return N/A
245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400247
248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400250 *
251 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500252 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400254 * @return N/A
255 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400256extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400257
258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500259 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500261 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400262 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500263 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 *
265 * @return N/A
266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400267extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400268
269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500270 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500272 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500274 * If @a thread is not currently sleeping, the routine has no effect.
275 *
276 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400277 *
278 * @return N/A
279 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400280extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400281
282/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500283 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500285 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400288
289/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500290 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500292 * This routine prevents @a thread from executing if it has not yet started
293 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500295 * @param thread ID of thread to cancel.
296 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500297 * @retval 0 Thread spawning cancelled.
298 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400300extern int k_thread_cancel(k_tid_t thread);
301
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400302/**
Allan Stephensc98da842016-11-11 15:45:03 -0500303 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500305 * This routine permanently stops execution of @a thread. The thread is taken
306 * off all kernel queues it is part of (i.e. the ready queue, the timeout
307 * queue, or a kernel object wait queue). However, any kernel resources the
308 * thread might currently own (such as mutexes or memory blocks) are not
309 * released. It is the responsibility of the caller of this routine to ensure
310 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500312 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400313 *
314 * @return N/A
315 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400316extern void k_thread_abort(k_tid_t thread);
317
Allan Stephensc98da842016-11-11 15:45:03 -0500318/**
319 * @cond INTERNAL_HIDDEN
320 */
321
Benjamin Walshd211a522016-12-06 11:44:01 -0500322/* timeout has timed out and is not on _timeout_q anymore */
323#define _EXPIRED (-2)
324
325/* timeout is not in use */
326#define _INACTIVE (-1)
327
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400328#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400329#define _THREAD_TIMEOUT_INIT(obj) \
330 (obj).nano_timeout = { \
331 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400332 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400333 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500334 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400335 },
336#else
337#define _THREAD_TIMEOUT_INIT(obj)
338#endif
339
340#ifdef CONFIG_ERRNO
341#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
342#else
343#define _THREAD_ERRNO_INIT(obj)
344#endif
345
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400346struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400347 union {
348 char *init_stack;
349 struct k_thread *thread;
350 };
351 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500352 void (*init_entry)(void *, void *, void *);
353 void *init_p1;
354 void *init_p2;
355 void *init_p3;
356 int init_prio;
357 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400358 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500359 void (*init_abort)(void);
360 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400361};
362
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400363#define _THREAD_INITIALIZER(stack, stack_size, \
364 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500365 prio, options, delay, abort, groups) \
366 { \
367 .init_stack = (stack), \
368 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400369 .init_entry = (void (*)(void *, void *, void *))entry, \
370 .init_p1 = (void *)p1, \
371 .init_p2 = (void *)p2, \
372 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500373 .init_prio = (prio), \
374 .init_options = (options), \
375 .init_delay = (delay), \
376 .init_abort = (abort), \
377 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378 }
379
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400380/**
Allan Stephensc98da842016-11-11 15:45:03 -0500381 * INTERNAL_HIDDEN @endcond
382 */
383
384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * @brief Statically define and initialize a thread.
386 *
387 * The thread may be scheduled for immediate execution or a delayed start.
388 *
389 * Thread options are architecture-specific, and can include K_ESSENTIAL,
390 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
391 * them using "|" (the logical OR operator).
392 *
393 * The ID of the thread can be accessed using:
394 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500395 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500396 *
397 * @param name Name of the thread.
398 * @param stack_size Stack size in bytes.
399 * @param entry Thread entry function.
400 * @param p1 1st entry point parameter.
401 * @param p2 2nd entry point parameter.
402 * @param p3 3rd entry point parameter.
403 * @param prio Thread priority.
404 * @param options Thread options.
405 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400406 *
407 * @internal It has been observed that the x86 compiler by default aligns
408 * these _static_thread_data structures to 32-byte boundaries, thereby
409 * wasting space. To work around this, force a 4-byte alignment.
410 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500411#define K_THREAD_DEFINE(name, stack_size, \
412 entry, p1, p2, p3, \
413 prio, options, delay) \
414 char __noinit __stack _k_thread_obj_##name[stack_size]; \
415 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500416 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500417 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
418 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500419 NULL, 0); \
420 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400421
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500423 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500425 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500427 * @param thread ID of thread whose priority is needed.
428 *
429 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500431extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432
433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * Rescheduling can occur immediately depending on the priority @a thread is
439 * set to:
440 *
441 * - If its priority is raised above the priority of the caller of this
442 * function, and the caller is preemptible, @a thread will be scheduled in.
443 *
444 * - If the caller operates on itself, it lowers its priority below that of
445 * other threads in the system, and the caller is preemptible, the thread of
446 * highest priority will be scheduled in.
447 *
448 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
449 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
450 * highest priority.
451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 * @param prio New priority.
454 *
455 * @warning Changing the priority of a thread currently involved in mutex
456 * priority inheritance may result in undefined behavior.
457 *
458 * @return N/A
459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400460extern void k_thread_priority_set(k_tid_t thread, int prio);
461
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500465 * This routine prevents the kernel scheduler from making @a thread the
466 * current thread. All other internal operations on @a thread are still
467 * performed; for example, any timeout it is waiting on keeps ticking,
468 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500470 * If @a thread is already suspended, the routine has no effect.
471 *
472 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
474 * @return N/A
475 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400476extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477
478/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500481 * This routine allows the kernel scheduler to make @a thread the current
482 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * If @a thread is not currently suspended, the routine has no effect.
485 *
486 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487 *
488 * @return N/A
489 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400490extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500495 * This routine specifies how the scheduler will perform time slicing of
496 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500498 * To enable time slicing, @a slice must be non-zero. The scheduler
499 * ensures that no thread runs for more than the specified time limit
500 * before other threads of that priority are given a chance to execute.
501 * Any thread whose priority is higher than @a prio is exempted, and may
502 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500504 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 * execute. Once the scheduler selects a thread for execution, there is no
506 * minimum guaranteed time the thread will execute before threads of greater or
507 * equal priority are scheduled.
508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400510 * for execution, this routine has no effect; the thread is immediately
511 * rescheduled after the slice period expires.
512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500513 * To disable timeslicing, set both @a slice and @a prio to zero.
514 *
515 * @param slice Maximum time slice length (in milliseconds).
516 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517 *
518 * @return N/A
519 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400520extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522/**
Allan Stephensc98da842016-11-11 15:45:03 -0500523 * @} end defgroup thread_apis
524 */
525
526/**
527 * @addtogroup isr_apis
528 * @{
529 */
530
531/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500532 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400533 *
Allan Stephensc98da842016-11-11 15:45:03 -0500534 * This routine allows the caller to customize its actions, depending on
535 * whether it is a thread or an ISR.
536 *
537 * @note Can be called by ISRs.
538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500539 * @return 0 if invoked by a thread.
540 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500542extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400543
Benjamin Walsh445830d2016-11-10 15:54:27 -0500544/**
545 * @brief Determine if code is running in a preemptible thread.
546 *
Allan Stephensc98da842016-11-11 15:45:03 -0500547 * This routine allows the caller to customize its actions, depending on
548 * whether it can be preempted by another thread. The routine returns a 'true'
549 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500550 *
Allan Stephensc98da842016-11-11 15:45:03 -0500551 * - The code is running in a thread, not at ISR.
552 * - The thread's priority is in the preemptible range.
553 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500554 *
Allan Stephensc98da842016-11-11 15:45:03 -0500555 * @note Can be called by ISRs.
556 *
557 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500558 * @return Non-zero if invoked by a preemptible thread.
559 */
560extern int k_is_preempt_thread(void);
561
Allan Stephensc98da842016-11-11 15:45:03 -0500562/**
563 * @} end addtogroup isr_apis
564 */
565
566/**
567 * @addtogroup thread_apis
568 * @{
569 */
570
571/**
572 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500573 *
Allan Stephensc98da842016-11-11 15:45:03 -0500574 * This routine prevents the current thread from being preempted by another
575 * thread by instructing the scheduler to treat it as a cooperative thread.
576 * If the thread subsequently performs an operation that makes it unready,
577 * it will be context switched out in the normal manner. When the thread
578 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500579 *
Allan Stephensc98da842016-11-11 15:45:03 -0500580 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500581 *
Allan Stephensc98da842016-11-11 15:45:03 -0500582 * @note k_sched_lock() and k_sched_unlock() should normally be used
583 * when the operation being performed can be safely interrupted by ISRs.
584 * However, if the amount of processing involved is very small, better
585 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500586 *
587 * @return N/A
588 */
589extern void k_sched_lock(void);
590
Allan Stephensc98da842016-11-11 15:45:03 -0500591/**
592 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500593 *
Allan Stephensc98da842016-11-11 15:45:03 -0500594 * This routine reverses the effect of a previous call to k_sched_lock().
595 * A thread must call the routine once for each time it called k_sched_lock()
596 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500597 *
598 * @return N/A
599 */
600extern void k_sched_unlock(void);
601
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500603 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500607 * Custom data is not used by the kernel itself, and is freely available
608 * for a thread to use as it sees fit. It can be used as a framework
609 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
613 * @return N/A
614 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400615extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616
617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500618 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500620 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400624extern void *k_thread_custom_data_get(void);
625
626/**
Allan Stephensc98da842016-11-11 15:45:03 -0500627 * @} end addtogroup thread_apis
628 */
629
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400630#include <sys_clock.h>
631
Allan Stephensc2f15a42016-11-17 12:24:22 -0500632/**
633 * @addtogroup clock_apis
634 * @{
635 */
636
637/**
638 * @brief Generate null timeout delay.
639 *
640 * This macro generates a timeout delay that that instructs a kernel API
641 * not to wait if the requested operation cannot be performed immediately.
642 *
643 * @return Timeout delay value.
644 */
645#define K_NO_WAIT 0
646
647/**
648 * @brief Generate timeout delay from milliseconds.
649 *
650 * This macro generates a timeout delay that that instructs a kernel API
651 * to wait up to @a ms milliseconds to perform the requested operation.
652 *
653 * @param ms Duration in milliseconds.
654 *
655 * @return Timeout delay value.
656 */
Johan Hedberg14471692016-11-13 10:52:15 +0200657#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500658
659/**
660 * @brief Generate timeout delay from seconds.
661 *
662 * This macro generates a timeout delay that that instructs a kernel API
663 * to wait up to @a s seconds to perform the requested operation.
664 *
665 * @param s Duration in seconds.
666 *
667 * @return Timeout delay value.
668 */
Johan Hedberg14471692016-11-13 10:52:15 +0200669#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500670
671/**
672 * @brief Generate timeout delay from minutes.
673 *
674 * This macro generates a timeout delay that that instructs a kernel API
675 * to wait up to @a m minutes to perform the requested operation.
676 *
677 * @param m Duration in minutes.
678 *
679 * @return Timeout delay value.
680 */
Johan Hedberg14471692016-11-13 10:52:15 +0200681#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500682
683/**
684 * @brief Generate timeout delay from hours.
685 *
686 * This macro generates a timeout delay that that instructs a kernel API
687 * to wait up to @a h hours to perform the requested operation.
688 *
689 * @param h Duration in hours.
690 *
691 * @return Timeout delay value.
692 */
Johan Hedberg14471692016-11-13 10:52:15 +0200693#define K_HOURS(h) K_MINUTES((h) * 60)
694
Allan Stephensc98da842016-11-11 15:45:03 -0500695/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500696 * @brief Generate infinite timeout delay.
697 *
698 * This macro generates a timeout delay that that instructs a kernel API
699 * to wait as long as necessary to perform the requested operation.
700 *
701 * @return Timeout delay value.
702 */
703#define K_FOREVER (-1)
704
705/**
706 * @} end addtogroup clock_apis
707 */
708
709/**
Allan Stephensc98da842016-11-11 15:45:03 -0500710 * @cond INTERNAL_HIDDEN
711 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400712
Benjamin Walsh62092182016-12-20 14:39:08 -0500713/* kernel clocks */
714
715#if (sys_clock_ticks_per_sec == 1000) || \
716 (sys_clock_ticks_per_sec == 500) || \
717 (sys_clock_ticks_per_sec == 250) || \
718 (sys_clock_ticks_per_sec == 125) || \
719 (sys_clock_ticks_per_sec == 100) || \
720 (sys_clock_ticks_per_sec == 50) || \
721 (sys_clock_ticks_per_sec == 25) || \
722 (sys_clock_ticks_per_sec == 20) || \
723 (sys_clock_ticks_per_sec == 10) || \
724 (sys_clock_ticks_per_sec == 1)
725
726 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
727#else
728 /* yields horrible 64-bit math on many architectures: try to avoid */
729 #define _NON_OPTIMIZED_TICKS_PER_SEC
730#endif
731
732#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
733extern int32_t _ms_to_ticks(int32_t ms);
734#else
735static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
736{
737 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
738}
739#endif
740
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500741/* added tick needed to account for tick in progress */
742#define _TICK_ALIGN 1
743
Benjamin Walsh62092182016-12-20 14:39:08 -0500744static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400745{
Benjamin Walsh62092182016-12-20 14:39:08 -0500746#ifdef CONFIG_SYS_CLOCK_EXISTS
747
748#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400749 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400750#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500751 return (uint64_t)ticks * _ms_per_tick;
752#endif
753
754#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400755 __ASSERT(ticks == 0, "");
756 return 0;
757#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400758}
759
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400760/* timeouts */
761
762struct _timeout;
763typedef void (*_timeout_func_t)(struct _timeout *t);
764
765struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500766 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400767 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400768 sys_dlist_t *wait_q;
769 int32_t delta_ticks_from_prev;
770 _timeout_func_t func;
771};
772
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200773extern int32_t _timeout_remaining_get(struct _timeout *timeout);
774
Allan Stephensc98da842016-11-11 15:45:03 -0500775/**
776 * INTERNAL_HIDDEN @endcond
777 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500778
Allan Stephensc98da842016-11-11 15:45:03 -0500779/**
780 * @cond INTERNAL_HIDDEN
781 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400782
783struct k_timer {
784 /*
785 * _timeout structure must be first here if we want to use
786 * dynamic timer allocation. timeout.node is used in the double-linked
787 * list of free timers
788 */
789 struct _timeout timeout;
790
Allan Stephens45bfa372016-10-12 12:39:42 -0500791 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792 _wait_q_t wait_q;
793
794 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500795 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400796
797 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500798 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400799
800 /* timer period */
801 int32_t period;
802
Allan Stephens45bfa372016-10-12 12:39:42 -0500803 /* timer status */
804 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400805
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500806 /* user-specific data, also used to support legacy features */
807 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808
Anas Nashif2f203c22016-12-18 06:57:45 -0500809 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810};
811
Allan Stephens1342adb2016-11-03 13:54:53 -0500812#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400813 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500814 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500815 .timeout.wait_q = NULL, \
816 .timeout.thread = NULL, \
817 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400818 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500819 .expiry_fn = expiry, \
820 .stop_fn = stop, \
821 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500822 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500823 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400824 }
825
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826/**
Allan Stephensc98da842016-11-11 15:45:03 -0500827 * INTERNAL_HIDDEN @endcond
828 */
829
830/**
831 * @defgroup timer_apis Timer APIs
832 * @ingroup kernel_apis
833 * @{
834 */
835
836/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500837 * @typedef k_timer_expiry_t
838 * @brief Timer expiry function type.
839 *
840 * A timer's expiry function is executed by the system clock interrupt handler
841 * each time the timer expires. The expiry function is optional, and is only
842 * invoked if the timer has been initialized with one.
843 *
844 * @param timer Address of timer.
845 *
846 * @return N/A
847 */
848typedef void (*k_timer_expiry_t)(struct k_timer *timer);
849
850/**
851 * @typedef k_timer_stop_t
852 * @brief Timer stop function type.
853 *
854 * A timer's stop function is executed if the timer is stopped prematurely.
855 * The function runs in the context of the thread that stops the timer.
856 * The stop function is optional, and is only invoked if the timer has been
857 * initialized with one.
858 *
859 * @param timer Address of timer.
860 *
861 * @return N/A
862 */
863typedef void (*k_timer_stop_t)(struct k_timer *timer);
864
865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500870 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
872 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @param expiry_fn Function to invoke each time the timer expires.
874 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500876#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500877 struct k_timer name \
878 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500879 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400880
Allan Stephens45bfa372016-10-12 12:39:42 -0500881/**
882 * @brief Initialize a timer.
883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500885 *
886 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @param expiry_fn Function to invoke each time the timer expires.
888 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500889 *
890 * @return N/A
891 */
892extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500893 k_timer_expiry_t expiry_fn,
894 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700895
Allan Stephens45bfa372016-10-12 12:39:42 -0500896/**
897 * @brief Start a timer.
898 *
899 * This routine starts a timer, and resets its status to zero. The timer
900 * begins counting down using the specified duration and period values.
901 *
902 * Attempting to start a timer that is already running is permitted.
903 * The timer's status is reset to zero and the timer begins counting down
904 * using the new duration and period values.
905 *
906 * @param timer Address of timer.
907 * @param duration Initial timer duration (in milliseconds).
908 * @param period Timer period (in milliseconds).
909 *
910 * @return N/A
911 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500913 int32_t duration, int32_t period);
914
915/**
916 * @brief Stop a timer.
917 *
918 * This routine stops a running timer prematurely. The timer's stop function,
919 * if one exists, is invoked by the caller.
920 *
921 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500923 *
924 * @param timer Address of timer.
925 *
926 * @return N/A
927 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400928extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500929
930/**
931 * @brief Read timer status.
932 *
933 * This routine reads the timer's status, which indicates the number of times
934 * it has expired since its status was last read.
935 *
936 * Calling this routine resets the timer's status to zero.
937 *
938 * @param timer Address of timer.
939 *
940 * @return Timer status.
941 */
942extern uint32_t k_timer_status_get(struct k_timer *timer);
943
944/**
945 * @brief Synchronize thread to timer expiration.
946 *
947 * This routine blocks the calling thread until the timer's status is non-zero
948 * (indicating that it has expired at least once since it was last examined)
949 * or the timer is stopped. If the timer status is already non-zero,
950 * or the timer is already stopped, the caller continues without waiting.
951 *
952 * Calling this routine resets the timer's status to zero.
953 *
954 * This routine must not be used by interrupt handlers, since they are not
955 * allowed to block.
956 *
957 * @param timer Address of timer.
958 *
959 * @return Timer status.
960 */
961extern uint32_t k_timer_status_sync(struct k_timer *timer);
962
963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500965 *
966 * This routine computes the (approximate) time remaining before a running
967 * timer next expires. If the timer is not running, it returns zero.
968 *
969 * @param timer Address of timer.
970 *
971 * @return Remaining time (in milliseconds).
972 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200973static inline int32_t k_timer_remaining_get(struct k_timer *timer)
974{
975 return _timeout_remaining_get(&timer->timeout);
976}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400977
Allan Stephensc98da842016-11-11 15:45:03 -0500978/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500979 * @brief Associate user-specific data with a timer.
980 *
981 * This routine records the @a user_data with the @a timer, to be retrieved
982 * later.
983 *
984 * It can be used e.g. in a timer handler shared across multiple subsystems to
985 * retrieve data specific to the subsystem this timer is associated with.
986 *
987 * @param timer Address of timer.
988 * @param user_data User data to associate with the timer.
989 *
990 * @return N/A
991 */
992static inline void k_timer_user_data_set(struct k_timer *timer,
993 void *user_data)
994{
995 timer->user_data = user_data;
996}
997
998/**
999 * @brief Retrieve the user-specific data from a timer.
1000 *
1001 * @param timer Address of timer.
1002 *
1003 * @return The user data.
1004 */
1005static inline void *k_timer_user_data_get(struct k_timer *timer)
1006{
1007 return timer->user_data;
1008}
1009
1010/**
Allan Stephensc98da842016-11-11 15:45:03 -05001011 * @} end defgroup timer_apis
1012 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001013
Allan Stephensc98da842016-11-11 15:45:03 -05001014/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001015 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001016 * @{
1017 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001018
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * This routine returns the elapsed time since the system booted,
1023 * in milliseconds.
1024 *
1025 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001026 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001027extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001028
1029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * This routine returns the lower 32-bits of the elapsed time since the system
1033 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * This routine can be more efficient than k_uptime_get(), as it reduces the
1036 * need for interrupt locking and 64-bit math. However, the 32-bit result
1037 * cannot hold a system uptime time larger than approximately 50 days, so the
1038 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001041 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001042extern uint32_t k_uptime_get_32(void);
1043
1044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * This routine computes the elapsed time between the current system uptime
1048 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * @param reftime Pointer to a reference time, which is updated to the current
1051 * uptime upon return.
1052 *
1053 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001056
1057/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * This routine computes the elapsed time between the current system uptime
1061 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001063 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1064 * need for interrupt locking and 64-bit math. However, the 32-bit result
1065 * cannot hold an elapsed time larger than approximately 50 days, so the
1066 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001068 * @param reftime Pointer to a reference time, which is updated to the current
1069 * uptime upon return.
1070 *
1071 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001072 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001073extern uint32_t k_uptime_delta_32(int64_t *reftime);
1074
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * This routine returns the current time, as measured by the system's hardware
1079 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001083extern uint32_t k_cycle_get_32(void);
1084
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001086 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001087 */
1088
Allan Stephensc98da842016-11-11 15:45:03 -05001089/**
1090 * @cond INTERNAL_HIDDEN
1091 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001092
1093struct k_fifo {
1094 _wait_q_t wait_q;
1095 sys_slist_t data_q;
1096
Anas Nashif2f203c22016-12-18 06:57:45 -05001097 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098};
1099
Allan Stephensc98da842016-11-11 15:45:03 -05001100#define K_FIFO_INITIALIZER(obj) \
1101 { \
1102 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1103 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001104 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001105 }
1106
1107/**
1108 * INTERNAL_HIDDEN @endcond
1109 */
1110
1111/**
1112 * @defgroup fifo_apis Fifo APIs
1113 * @ingroup kernel_apis
1114 * @{
1115 */
1116
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
1124 * @return N/A
1125 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001126extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001127
1128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * This routine adds a data item to @a fifo. A fifo data item must be
1132 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1133 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @note Can be called by ISRs.
1136 *
1137 * @param fifo Address of the fifo.
1138 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
1140 * @return N/A
1141 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143
1144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001147 * This routine adds a list of data items to @a fifo in one operation.
1148 * The data items must be in a singly-linked list, with the first 32 bits
1149 * each data item pointing to the next data item; the list must be
1150 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @param fifo Address of the fifo.
1155 * @param head Pointer to first node in singly-linked list.
1156 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
1158 * @return N/A
1159 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161
1162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * This routine adds a list of data items to @a fifo in one operation.
1166 * The data items must be in a singly-linked list implemented using a
1167 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 * and must be re-initialized via sys_slist_init().
1169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * @note Can be called by ISRs.
1171 *
1172 * @param fifo Address of the fifo.
1173 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 *
1175 * @return N/A
1176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178
1179/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * This routine removes a data item from @a fifo in a "first in, first out"
1183 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1186 *
1187 * @param fifo Address of the fifo.
1188 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001189 * or one of the special values K_NO_WAIT and K_FOREVER.
1190 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001191 * @return Address of the data item if successful; NULL if returned
1192 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001194extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1195
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001197 * @brief Query a fifo to see if it has data available.
1198 *
1199 * Note that the data might be already gone by the time this function returns
1200 * if other threads is also trying to read from the fifo.
1201 *
1202 * @note Can be called by ISRs.
1203 *
1204 * @param fifo Address of the fifo.
1205 *
1206 * @return Non-zero if the fifo is empty.
1207 * @return 0 if data is available.
1208 */
1209static inline int k_fifo_is_empty(struct k_fifo *fifo)
1210{
1211 return (int)sys_slist_is_empty(&fifo->data_q);
1212}
1213
1214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001219 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001223#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001224 struct k_fifo name \
1225 __in_section(_k_fifo, static, name) = \
1226 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001227
Allan Stephensc98da842016-11-11 15:45:03 -05001228/**
1229 * @} end defgroup fifo_apis
1230 */
1231
1232/**
1233 * @cond INTERNAL_HIDDEN
1234 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001235
1236struct k_lifo {
1237 _wait_q_t wait_q;
1238 void *list;
1239
Anas Nashif2f203c22016-12-18 06:57:45 -05001240 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001241};
1242
Allan Stephensc98da842016-11-11 15:45:03 -05001243#define K_LIFO_INITIALIZER(obj) \
1244 { \
1245 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1246 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001247 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001248 }
1249
1250/**
1251 * INTERNAL_HIDDEN @endcond
1252 */
1253
1254/**
1255 * @defgroup lifo_apis Lifo APIs
1256 * @ingroup kernel_apis
1257 * @{
1258 */
1259
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001265 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 *
1267 * @return N/A
1268 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001270
1271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001274 * This routine adds a data item to @a lifo. A lifo data item must be
1275 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1276 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001278 * @note Can be called by ISRs.
1279 *
1280 * @param lifo Address of the lifo.
1281 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001282 *
1283 * @return N/A
1284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286
1287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine removes a data item from @a lifo in a "last in, first out"
1291 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1294 *
1295 * @param lifo Address of the lifo.
1296 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 * or one of the special values K_NO_WAIT and K_FOREVER.
1298 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001299 * @return Address of the data item if successful; NULL if returned
1300 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1303
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001307 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001308 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001309 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001311 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001313#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001314 struct k_lifo name \
1315 __in_section(_k_lifo, static, name) = \
1316 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001317
Allan Stephensc98da842016-11-11 15:45:03 -05001318/**
1319 * @} end defgroup lifo_apis
1320 */
1321
1322/**
1323 * @cond INTERNAL_HIDDEN
1324 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325
1326struct k_stack {
1327 _wait_q_t wait_q;
1328 uint32_t *base, *next, *top;
1329
Anas Nashif2f203c22016-12-18 06:57:45 -05001330 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331};
1332
Allan Stephensc98da842016-11-11 15:45:03 -05001333#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1334 { \
1335 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1336 .base = stack_buffer, \
1337 .next = stack_buffer, \
1338 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001339 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001340 }
1341
1342/**
1343 * INTERNAL_HIDDEN @endcond
1344 */
1345
1346/**
1347 * @defgroup stack_apis Stack APIs
1348 * @ingroup kernel_apis
1349 * @{
1350 */
1351
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352/**
1353 * @brief Initialize a stack.
1354 *
1355 * This routine initializes a stack object, prior to its first use.
1356 *
1357 * @param stack Address of the stack.
1358 * @param buffer Address of array used to hold stacked values.
1359 * @param num_entries Maximum number of values that can be stacked.
1360 *
1361 * @return N/A
1362 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001363extern void k_stack_init(struct k_stack *stack,
1364 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365
1366/**
1367 * @brief Push an element onto a stack.
1368 *
1369 * This routine adds a 32-bit value @a data to @a stack.
1370 *
1371 * @note Can be called by ISRs.
1372 *
1373 * @param stack Address of the stack.
1374 * @param data Value to push onto the stack.
1375 *
1376 * @return N/A
1377 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001379
1380/**
1381 * @brief Pop an element from a stack.
1382 *
1383 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1384 * manner and stores the value in @a data.
1385 *
1386 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1387 *
1388 * @param stack Address of the stack.
1389 * @param data Address of area to hold the value popped from the stack.
1390 * @param timeout Waiting period to obtain a value (in milliseconds),
1391 * or one of the special values K_NO_WAIT and K_FOREVER.
1392 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001393 * @retval 0 Element popped from stack.
1394 * @retval -EBUSY Returned without waiting.
1395 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1398
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001399/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001400 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001402 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001403 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001404 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * @param name Name of the stack.
1407 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001409#define K_STACK_DEFINE(name, stack_num_entries) \
1410 uint32_t __noinit \
1411 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001412 struct k_stack name \
1413 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001414 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1415 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001416
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417/**
Allan Stephensc98da842016-11-11 15:45:03 -05001418 * @} end defgroup stack_apis
1419 */
1420
Allan Stephens6bba9b02016-11-16 14:56:54 -05001421struct k_work;
1422
Allan Stephensc98da842016-11-11 15:45:03 -05001423/**
1424 * @defgroup workqueue_apis Workqueue Thread APIs
1425 * @ingroup kernel_apis
1426 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001427 */
1428
Allan Stephens6bba9b02016-11-16 14:56:54 -05001429/**
1430 * @typedef k_work_handler_t
1431 * @brief Work item handler function type.
1432 *
1433 * A work item's handler function is executed by a workqueue's thread
1434 * when the work item is processed by the workqueue.
1435 *
1436 * @param work Address of the work item.
1437 *
1438 * @return N/A
1439 */
1440typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441
1442/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001443 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001445
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446struct k_work_q {
1447 struct k_fifo fifo;
1448};
1449
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001451 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001452};
1453
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454struct k_work {
1455 void *_reserved; /* Used by k_fifo implementation. */
1456 k_work_handler_t handler;
1457 atomic_t flags[1];
1458};
1459
Allan Stephens6bba9b02016-11-16 14:56:54 -05001460struct k_delayed_work {
1461 struct k_work work;
1462 struct _timeout timeout;
1463 struct k_work_q *work_q;
1464};
1465
1466extern struct k_work_q k_sys_work_q;
1467
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001468/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001469 * INTERNAL_HIDDEN @endcond
1470 */
1471
1472/**
1473 * @brief Initialize a statically-defined work item.
1474 *
1475 * This macro can be used to initialize a statically-defined workqueue work
1476 * item, prior to its first use. For example,
1477 *
1478 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1479 *
1480 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001481 */
1482#define K_WORK_INITIALIZER(work_handler) \
1483 { \
1484 ._reserved = NULL, \
1485 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001486 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001487 }
1488
1489/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001490 * @brief Initialize a work item.
1491 *
1492 * This routine initializes a workqueue work item, prior to its first use.
1493 *
1494 * @param work Address of work item.
1495 * @param handler Function to invoke each time work item is processed.
1496 *
1497 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001498 */
1499static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1500{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001501 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502 work->handler = handler;
1503}
1504
1505/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001506 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001507 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001508 * This routine submits work item @a work to be processed by workqueue
1509 * @a work_q. If the work item is already pending in the workqueue's queue
1510 * as a result of an earlier submission, this routine has no effect on the
1511 * work item. If the work item has already been processed, or is currently
1512 * being processed, its work is considered complete and the work item can be
1513 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001514 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001515 * @warning
1516 * A submitted work item must not be modified until it has been processed
1517 * by the workqueue.
1518 *
1519 * @note Can be called by ISRs.
1520 *
1521 * @param work_q Address of workqueue.
1522 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001523 *
1524 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525 */
1526static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1527 struct k_work *work)
1528{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001529 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530 k_fifo_put(&work_q->fifo, work);
1531 }
1532}
1533
1534/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001535 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001537 * This routine indicates if work item @a work is pending in a workqueue's
1538 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001539 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001540 * @note Can be called by ISRs.
1541 *
1542 * @param work Address of work item.
1543 *
1544 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001545 */
1546static inline int k_work_pending(struct k_work *work)
1547{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001548 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001549}
1550
1551/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001552 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001553 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001554 * This routine starts workqueue @a work_q. The workqueue spawns its work
1555 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001556 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001557 * @param work_q Address of workqueue.
1558 * @param stack Pointer to work queue thread's stack space.
1559 * @param stack_size Size of the work queue thread's stack (in bytes).
1560 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001561 *
1562 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001563 */
Allan Stephens904cf972016-10-07 13:59:23 -05001564extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001565 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001566
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001567/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001568 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001570 * This routine initializes a workqueue delayed work item, prior to
1571 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001573 * @param work Address of delayed work item.
1574 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001575 *
1576 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001577 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001578extern void k_delayed_work_init(struct k_delayed_work *work,
1579 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001580
1581/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001582 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001583 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001584 * This routine schedules work item @a work to be processed by workqueue
1585 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1586 * an asychronous countdown for the work item and then returns to the caller.
1587 * Only when the countdown completes is the work item actually submitted to
1588 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001589 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001590 * Submitting a previously submitted delayed work item that is still
1591 * counting down cancels the existing submission and restarts the countdown
1592 * using the new delay. If the work item is currently pending on the
1593 * workqueue's queue because the countdown has completed it is too late to
1594 * resubmit the item, and resubmission fails without impacting the work item.
1595 * If the work item has already been processed, or is currently being processed,
1596 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001597 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001598 * @warning
1599 * A delayed work item must not be modified until it has been processed
1600 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001602 * @note Can be called by ISRs.
1603 *
1604 * @param work_q Address of workqueue.
1605 * @param work Address of delayed work item.
1606 * @param delay Delay before submitting the work item (in milliseconds).
1607 *
1608 * @retval 0 Work item countdown started.
1609 * @retval -EINPROGRESS Work item is already pending.
1610 * @retval -EINVAL Work item is being processed or has completed its work.
1611 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001612 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001613extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1614 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001615 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001616
1617/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001618 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001619 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001620 * This routine cancels the submission of delayed work item @a work.
1621 * A delayed work item can only be cancelled while its countdown is still
1622 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001623 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001624 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001626 * @param work Address of delayed work item.
1627 *
1628 * @retval 0 Work item countdown cancelled.
1629 * @retval -EINPROGRESS Work item is already pending.
1630 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001632extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001633
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001634/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635 * @brief Submit a work item to the system workqueue.
1636 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001637 * This routine submits work item @a work to be processed by the system
1638 * workqueue. If the work item is already pending in the workqueue's queue
1639 * as a result of an earlier submission, this routine has no effect on the
1640 * work item. If the work item has already been processed, or is currently
1641 * being processed, its work is considered complete and the work item can be
1642 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001644 * @warning
1645 * Work items submitted to the system workqueue should avoid using handlers
1646 * that block or yield since this may prevent the system workqueue from
1647 * processing other work items in a timely manner.
1648 *
1649 * @note Can be called by ISRs.
1650 *
1651 * @param work Address of work item.
1652 *
1653 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001654 */
1655static inline void k_work_submit(struct k_work *work)
1656{
1657 k_work_submit_to_queue(&k_sys_work_q, work);
1658}
1659
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001661 * @brief Submit a delayed work item to the system workqueue.
1662 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001663 * This routine schedules work item @a work to be processed by the system
1664 * workqueue after a delay of @a delay milliseconds. The routine initiates
1665 * an asychronous countdown for the work item and then returns to the caller.
1666 * Only when the countdown completes is the work item actually submitted to
1667 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001668 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001669 * Submitting a previously submitted delayed work item that is still
1670 * counting down cancels the existing submission and restarts the countdown
1671 * using the new delay. If the work item is currently pending on the
1672 * workqueue's queue because the countdown has completed it is too late to
1673 * resubmit the item, and resubmission fails without impacting the work item.
1674 * If the work item has already been processed, or is currently being processed,
1675 * its work is considered complete and the work item can be resubmitted.
1676 *
1677 * @warning
1678 * Work items submitted to the system workqueue should avoid using handlers
1679 * that block or yield since this may prevent the system workqueue from
1680 * processing other work items in a timely manner.
1681 *
1682 * @note Can be called by ISRs.
1683 *
1684 * @param work Address of delayed work item.
1685 * @param delay Delay before submitting the work item (in milliseconds).
1686 *
1687 * @retval 0 Work item countdown started.
1688 * @retval -EINPROGRESS Work item is already pending.
1689 * @retval -EINVAL Work item is being processed or has completed its work.
1690 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001691 */
1692static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001693 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001695 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001696}
1697
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001698/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001699 * @brief Get time remaining before a delayed work gets scheduled.
1700 *
1701 * This routine computes the (approximate) time remaining before a
1702 * delayed work gets executed. If the delayed work is not waiting to be
1703 * schedules, it returns zero.
1704 *
1705 * @param work Delayed work item.
1706 *
1707 * @return Remaining time (in milliseconds).
1708 */
1709static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1710{
1711 return _timeout_remaining_get(&work->timeout);
1712}
1713
1714/**
Allan Stephensc98da842016-11-11 15:45:03 -05001715 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001716 */
1717
Allan Stephensc98da842016-11-11 15:45:03 -05001718/**
1719 * @cond INTERNAL_HIDDEN
1720 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001721
1722struct k_mutex {
1723 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001724 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001725 uint32_t lock_count;
1726 int owner_orig_prio;
1727#ifdef CONFIG_OBJECT_MONITOR
1728 int num_lock_state_changes;
1729 int num_conflicts;
1730#endif
1731
Anas Nashif2f203c22016-12-18 06:57:45 -05001732 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001733};
1734
1735#ifdef CONFIG_OBJECT_MONITOR
1736#define _MUTEX_INIT_OBJECT_MONITOR \
1737 .num_lock_state_changes = 0, .num_conflicts = 0,
1738#else
1739#define _MUTEX_INIT_OBJECT_MONITOR
1740#endif
1741
1742#define K_MUTEX_INITIALIZER(obj) \
1743 { \
1744 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1745 .owner = NULL, \
1746 .lock_count = 0, \
1747 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1748 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001749 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750 }
1751
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752/**
Allan Stephensc98da842016-11-11 15:45:03 -05001753 * INTERNAL_HIDDEN @endcond
1754 */
1755
1756/**
1757 * @defgroup mutex_apis Mutex APIs
1758 * @ingroup kernel_apis
1759 * @{
1760 */
1761
1762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001765 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001767 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001771#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001772 struct k_mutex name \
1773 __in_section(_k_mutex, static, name) = \
1774 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001775
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * Upon completion, the mutex is available and does not have an owner.
1782 *
1783 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001784 *
1785 * @return N/A
1786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001787extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001788
1789/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001792 * This routine locks @a mutex. If the mutex is locked by another thread,
1793 * the calling thread waits until the mutex becomes available or until
1794 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * A thread is permitted to lock a mutex it has already locked. The operation
1797 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @param mutex Address of the mutex.
1800 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 * or one of the special values K_NO_WAIT and K_FOREVER.
1802 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001803 * @retval 0 Mutex locked.
1804 * @retval -EBUSY Returned without waiting.
1805 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001806 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001807extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808
1809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * This routine unlocks @a mutex. The mutex must already be locked by the
1813 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
1815 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 * thread.
1818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001819 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001820 *
1821 * @return N/A
1822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823extern void k_mutex_unlock(struct k_mutex *mutex);
1824
Allan Stephensc98da842016-11-11 15:45:03 -05001825/**
1826 * @} end defgroup mutex_apis
1827 */
1828
1829/**
1830 * @cond INTERNAL_HIDDEN
1831 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001832
1833struct k_sem {
1834 _wait_q_t wait_q;
1835 unsigned int count;
1836 unsigned int limit;
1837
Anas Nashif2f203c22016-12-18 06:57:45 -05001838 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001839};
1840
Allan Stephensc98da842016-11-11 15:45:03 -05001841#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1842 { \
1843 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1844 .count = initial_count, \
1845 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001846 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001847 }
1848
1849/**
1850 * INTERNAL_HIDDEN @endcond
1851 */
1852
1853/**
1854 * @defgroup semaphore_apis Semaphore APIs
1855 * @ingroup kernel_apis
1856 * @{
1857 */
1858
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * @param sem Address of the semaphore.
1865 * @param initial_count Initial semaphore count.
1866 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001867 *
1868 * @return N/A
1869 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1871 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001872
1873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001874 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1879 *
1880 * @param sem Address of the semaphore.
1881 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001882 * or one of the special values K_NO_WAIT and K_FOREVER.
1883 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001884 * @note When porting code from the nanokernel legacy API to the new API, be
1885 * careful with the return value of this function. The return value is the
1886 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1887 * non-zero means failure, while the nano_sem_take family returns 1 for success
1888 * and 0 for failure.
1889 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001890 * @retval 0 Semaphore taken.
1891 * @retval -EBUSY Returned without waiting.
1892 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001893 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001894extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001895
1896/**
1897 * @brief Give a semaphore.
1898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001899 * This routine gives @a sem, unless the semaphore is already at its maximum
1900 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001902 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001904 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001905 *
1906 * @return N/A
1907 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001908extern void k_sem_give(struct k_sem *sem);
1909
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001910/**
1911 * @brief Reset a semaphore's count to zero.
1912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001916 *
1917 * @return N/A
1918 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001919static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001920{
1921 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001922}
1923
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001924/**
1925 * @brief Get a semaphore's count.
1926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001932 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001933static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934{
1935 return sem->count;
1936}
1937
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001941 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001942 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001943 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001945 * @param name Name of the semaphore.
1946 * @param initial_count Initial semaphore count.
1947 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001948 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001950 struct k_sem name \
1951 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952 K_SEM_INITIALIZER(name, initial_count, count_limit)
1953
Allan Stephensc98da842016-11-11 15:45:03 -05001954/**
1955 * @} end defgroup semaphore_apis
1956 */
1957
1958/**
1959 * @defgroup alert_apis Alert APIs
1960 * @ingroup kernel_apis
1961 * @{
1962 */
1963
Allan Stephens5eceb852016-11-16 10:16:30 -05001964/**
1965 * @typedef k_alert_handler_t
1966 * @brief Alert handler function type.
1967 *
1968 * An alert's alert handler function is invoked by the system workqueue
1969 * when the alert is signalled. The alert handler function is optional,
1970 * and is only invoked if the alert has been initialized with one.
1971 *
1972 * @param alert Address of the alert.
1973 *
1974 * @return 0 if alert has been consumed; non-zero if alert should pend.
1975 */
1976typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001977
1978/**
1979 * @} end defgroup alert_apis
1980 */
1981
1982/**
1983 * @cond INTERNAL_HIDDEN
1984 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001986#define K_ALERT_DEFAULT NULL
1987#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001988
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001989struct k_alert {
1990 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991 atomic_t send_count;
1992 struct k_work work_item;
1993 struct k_sem sem;
1994
Anas Nashif2f203c22016-12-18 06:57:45 -05001995 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996};
1997
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001998extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002000#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002001 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002002 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002004 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002005 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002006 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007 }
2008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009/**
Allan Stephensc98da842016-11-11 15:45:03 -05002010 * INTERNAL_HIDDEN @endcond
2011 */
2012
2013/**
2014 * @addtogroup alert_apis
2015 * @{
2016 */
2017
2018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002021 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002023 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @param name Name of the alert.
2026 * @param alert_handler Action to take when alert is sent. Specify either
2027 * the address of a function to be invoked by the system workqueue
2028 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2029 * K_ALERT_DEFAULT (which causes the alert to pend).
2030 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002032#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002033 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002034 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002035 K_ALERT_INITIALIZER(name, alert_handler, \
2036 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002043 * @param alert Address of the alert.
2044 * @param handler Action to take when alert is sent. Specify either the address
2045 * of a function to be invoked by the system workqueue thread,
2046 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2047 * K_ALERT_DEFAULT (which causes the alert to pend).
2048 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049 *
2050 * @return N/A
2051 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002052extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2053 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054
2055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002060 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2061 *
2062 * @param alert Address of the alert.
2063 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 * or one of the special values K_NO_WAIT and K_FOREVER.
2065 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002066 * @retval 0 Alert received.
2067 * @retval -EBUSY Returned without waiting.
2068 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002070extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071
2072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * This routine signals @a alert. The action specified for @a alert will
2076 * be taken, which may trigger the execution of an alert handler function
2077 * and/or cause the alert to pend (assuming the alert has not reached its
2078 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080 * @note Can be called by ISRs.
2081 *
2082 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083 *
2084 * @return N/A
2085 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002086extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087
2088/**
Allan Stephensc98da842016-11-11 15:45:03 -05002089 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090 */
2091
Allan Stephensc98da842016-11-11 15:45:03 -05002092/**
2093 * @cond INTERNAL_HIDDEN
2094 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095
2096struct k_msgq {
2097 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002098 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002099 uint32_t max_msgs;
2100 char *buffer_start;
2101 char *buffer_end;
2102 char *read_ptr;
2103 char *write_ptr;
2104 uint32_t used_msgs;
2105
Anas Nashif2f203c22016-12-18 06:57:45 -05002106 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107};
2108
Peter Mitsis1da807e2016-10-06 11:36:59 -04002109#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110 { \
2111 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002112 .max_msgs = q_max_msgs, \
2113 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002115 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116 .read_ptr = q_buffer, \
2117 .write_ptr = q_buffer, \
2118 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002119 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120 }
2121
Peter Mitsis1da807e2016-10-06 11:36:59 -04002122/**
Allan Stephensc98da842016-11-11 15:45:03 -05002123 * INTERNAL_HIDDEN @endcond
2124 */
2125
2126/**
2127 * @defgroup msgq_apis Message Queue APIs
2128 * @ingroup kernel_apis
2129 * @{
2130 */
2131
2132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2136 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002137 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2138 * message is similarly aligned to this boundary, @a q_msg_size must also be
2139 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 * The message queue can be accessed outside the module where it is defined
2142 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002144 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @param q_name Name of the message queue.
2147 * @param q_msg_size Message size (in bytes).
2148 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002149 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002150 */
2151#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2152 static char __noinit __aligned(q_align) \
2153 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002154 struct k_msgq q_name \
2155 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002156 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2157 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158
Peter Mitsisd7a37502016-10-13 11:37:40 -04002159/**
2160 * @brief Initialize a message queue.
2161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 * This routine initializes a message queue object, prior to its first use.
2163 *
Allan Stephensda827222016-11-09 14:23:58 -06002164 * The message queue's ring buffer must contain space for @a max_msgs messages,
2165 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2166 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2167 * that each message is similarly aligned to this boundary, @a q_msg_size
2168 * must also be a multiple of N.
2169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @param q Address of the message queue.
2171 * @param buffer Pointer to ring buffer that holds queued messages.
2172 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002173 * @param max_msgs Maximum number of messages that can be queued.
2174 *
2175 * @return N/A
2176 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002177extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002178 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002179
2180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002184 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002185 * @note Can be called by ISRs.
2186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002187 * @param q Address of the message queue.
2188 * @param data Pointer to the message.
2189 * @param timeout Waiting period to add the message (in milliseconds),
2190 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002191 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002192 * @retval 0 Message sent.
2193 * @retval -ENOMSG Returned without waiting or queue purged.
2194 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002195 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002197
2198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002201 * This routine receives a message from message queue @a q in a "first in,
2202 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002203 *
Allan Stephensc98da842016-11-11 15:45:03 -05002204 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * @param q Address of the message queue.
2207 * @param data Address of area to hold the received message.
2208 * @param timeout Waiting period to receive the message (in milliseconds),
2209 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002210 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002211 * @retval 0 Message received.
2212 * @retval -ENOMSG Returned without waiting.
2213 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002214 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002216
2217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002220 * This routine discards all unreceived messages in a message queue's ring
2221 * buffer. Any threads that are blocked waiting to send a message to the
2222 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002224 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002225 *
2226 * @return N/A
2227 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228extern void k_msgq_purge(struct k_msgq *q);
2229
Peter Mitsis67be2492016-10-07 11:44:34 -04002230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * This routine returns the number of unused entries in a message queue's
2234 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002236 * @param q Address of the message queue.
2237 *
2238 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002239 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002240static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002241{
2242 return q->max_msgs - q->used_msgs;
2243}
2244
Peter Mitsisd7a37502016-10-13 11:37:40 -04002245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002246 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002250 * @param q Address of the message queue.
2251 *
2252 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002253 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002254static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255{
2256 return q->used_msgs;
2257}
2258
Allan Stephensc98da842016-11-11 15:45:03 -05002259/**
2260 * @} end defgroup msgq_apis
2261 */
2262
2263/**
2264 * @defgroup mem_pool_apis Memory Pool APIs
2265 * @ingroup kernel_apis
2266 * @{
2267 */
2268
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002270 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 void *addr_in_pool;
2272 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002273 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274};
2275
Allan Stephensc98da842016-11-11 15:45:03 -05002276/**
2277 * @} end defgroup mem_pool_apis
2278 */
2279
2280/**
2281 * @defgroup mailbox_apis Mailbox APIs
2282 * @ingroup kernel_apis
2283 * @{
2284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285
2286struct k_mbox_msg {
2287 /** internal use only - needed for legacy API support */
2288 uint32_t _mailbox;
2289 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002290 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002291 /** application-defined information value */
2292 uint32_t info;
2293 /** sender's message data buffer */
2294 void *tx_data;
2295 /** internal use only - needed for legacy API support */
2296 void *_rx_data;
2297 /** message data block descriptor */
2298 struct k_mem_block tx_block;
2299 /** source thread id */
2300 k_tid_t rx_source_thread;
2301 /** target thread id */
2302 k_tid_t tx_target_thread;
2303 /** internal use only - thread waiting on send (may be a dummy) */
2304 k_tid_t _syncing_thread;
2305#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2306 /** internal use only - semaphore used during asynchronous send */
2307 struct k_sem *_async_sem;
2308#endif
2309};
2310
Allan Stephensc98da842016-11-11 15:45:03 -05002311/**
2312 * @cond INTERNAL_HIDDEN
2313 */
2314
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315struct k_mbox {
2316 _wait_q_t tx_msg_queue;
2317 _wait_q_t rx_msg_queue;
2318
Anas Nashif2f203c22016-12-18 06:57:45 -05002319 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320};
2321
2322#define K_MBOX_INITIALIZER(obj) \
2323 { \
2324 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2325 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002326 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327 }
2328
Peter Mitsis12092702016-10-14 12:57:23 -04002329/**
Allan Stephensc98da842016-11-11 15:45:03 -05002330 * INTERNAL_HIDDEN @endcond
2331 */
2332
2333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002338 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002341 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002343 struct k_mbox name \
2344 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345 K_MBOX_INITIALIZER(name) \
2346
Peter Mitsis12092702016-10-14 12:57:23 -04002347/**
2348 * @brief Initialize a mailbox.
2349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * This routine initializes a mailbox object, prior to its first use.
2351 *
2352 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002353 *
2354 * @return N/A
2355 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356extern void k_mbox_init(struct k_mbox *mbox);
2357
Peter Mitsis12092702016-10-14 12:57:23 -04002358/**
2359 * @brief Send a mailbox message in a synchronous manner.
2360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * This routine sends a message to @a mbox and waits for a receiver to both
2362 * receive and process it. The message data may be in a buffer, in a memory
2363 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * @param mbox Address of the mailbox.
2366 * @param tx_msg Address of the transmit message descriptor.
2367 * @param timeout Waiting period for the message to be received (in
2368 * milliseconds), or one of the special values K_NO_WAIT
2369 * and K_FOREVER. Once the message has been received,
2370 * this routine waits as long as necessary for the message
2371 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002372 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002373 * @retval 0 Message sent.
2374 * @retval -ENOMSG Returned without waiting.
2375 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002376 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002377extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002379
Peter Mitsis12092702016-10-14 12:57:23 -04002380/**
2381 * @brief Send a mailbox message in an asynchronous manner.
2382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * This routine sends a message to @a mbox without waiting for a receiver
2384 * to process it. The message data may be in a buffer, in a memory pool block,
2385 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2386 * will be given when the message has been both received and completely
2387 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * @param mbox Address of the mailbox.
2390 * @param tx_msg Address of the transmit message descriptor.
2391 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002392 *
2393 * @return N/A
2394 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002395extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 struct k_sem *sem);
2397
Peter Mitsis12092702016-10-14 12:57:23 -04002398/**
2399 * @brief Receive a mailbox message.
2400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * This routine receives a message from @a mbox, then optionally retrieves
2402 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @param mbox Address of the mailbox.
2405 * @param rx_msg Address of the receive message descriptor.
2406 * @param buffer Address of the buffer to receive data, or NULL to defer data
2407 * retrieval and message disposal until later.
2408 * @param timeout Waiting period for a message to be received (in
2409 * milliseconds), or one of the special values K_NO_WAIT
2410 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002411 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002412 * @retval 0 Message received.
2413 * @retval -ENOMSG Returned without waiting.
2414 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002415 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002416extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002418
2419/**
2420 * @brief Retrieve mailbox message data into a buffer.
2421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * This routine completes the processing of a received message by retrieving
2423 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002424 *
2425 * Alternatively, this routine can be used to dispose of a received message
2426 * without retrieving its data.
2427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * @param rx_msg Address of the receive message descriptor.
2429 * @param buffer Address of the buffer to receive data, or NULL to discard
2430 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002431 *
2432 * @return N/A
2433 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002434extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002435
2436/**
2437 * @brief Retrieve mailbox message data into a memory pool block.
2438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * This routine completes the processing of a received message by retrieving
2440 * its data into a memory pool block, then disposing of the message.
2441 * The memory pool block that results from successful retrieval must be
2442 * returned to the pool once the data has been processed, even in cases
2443 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002444 *
2445 * Alternatively, this routine can be used to dispose of a received message
2446 * without retrieving its data. In this case there is no need to return a
2447 * memory pool block to the pool.
2448 *
2449 * This routine allocates a new memory pool block for the data only if the
2450 * data is not already in one. If a new block cannot be allocated, the routine
2451 * returns a failure code and the received message is left unchanged. This
2452 * permits the caller to reattempt data retrieval at a later time or to dispose
2453 * of the received message without retrieving its data.
2454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002455 * @param rx_msg Address of a receive message descriptor.
2456 * @param pool Address of memory pool, or NULL to discard data.
2457 * @param block Address of the area to hold memory pool block info.
2458 * @param timeout Waiting period to wait for a memory pool block (in
2459 * milliseconds), or one of the special values K_NO_WAIT
2460 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002461 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002462 * @retval 0 Data retrieved.
2463 * @retval -ENOMEM Returned without waiting.
2464 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002465 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002466extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002467 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468 struct k_mem_block *block, int32_t timeout);
2469
Allan Stephensc98da842016-11-11 15:45:03 -05002470/**
2471 * @} end defgroup mailbox_apis
2472 */
2473
2474/**
2475 * @cond INTERNAL_HIDDEN
2476 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477
2478struct k_pipe {
2479 unsigned char *buffer; /* Pipe buffer: may be NULL */
2480 size_t size; /* Buffer size */
2481 size_t bytes_used; /* # bytes used in buffer */
2482 size_t read_index; /* Where in buffer to read from */
2483 size_t write_index; /* Where in buffer to write */
2484
2485 struct {
2486 _wait_q_t readers; /* Reader wait queue */
2487 _wait_q_t writers; /* Writer wait queue */
2488 } wait_q;
2489
Anas Nashif2f203c22016-12-18 06:57:45 -05002490 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491};
2492
Peter Mitsise5d9c582016-10-14 14:44:57 -04002493#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494 { \
2495 .buffer = pipe_buffer, \
2496 .size = pipe_buffer_size, \
2497 .bytes_used = 0, \
2498 .read_index = 0, \
2499 .write_index = 0, \
2500 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2501 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002502 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503 }
2504
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505/**
Allan Stephensc98da842016-11-11 15:45:03 -05002506 * INTERNAL_HIDDEN @endcond
2507 */
2508
2509/**
2510 * @defgroup pipe_apis Pipe APIs
2511 * @ingroup kernel_apis
2512 * @{
2513 */
2514
2515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002520 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * @param name Name of the pipe.
2523 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2524 * or zero if no ring buffer is used.
2525 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002526 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002527#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2528 static unsigned char __noinit __aligned(pipe_align) \
2529 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002530 struct k_pipe name \
2531 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002532 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * This routine initializes a pipe object, prior to its first use.
2538 *
2539 * @param pipe Address of the pipe.
2540 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2541 * is used.
2542 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2543 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 *
2545 * @return N/A
2546 */
2547extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2548 size_t size);
2549
2550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @param pipe Address of the pipe.
2556 * @param data Address of data to write.
2557 * @param bytes_to_write Size of data (in bytes).
2558 * @param bytes_written Address of area to hold the number of bytes written.
2559 * @param min_xfer Minimum number of bytes to write.
2560 * @param timeout Waiting period to wait for the data to be written (in
2561 * milliseconds), or one of the special values K_NO_WAIT
2562 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002564 * @retval 0 At least @a min_xfer bytes of data were written.
2565 * @retval -EIO Returned without waiting; zero data bytes were written.
2566 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002567 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002569extern int k_pipe_put(struct k_pipe *pipe, void *data,
2570 size_t bytes_to_write, size_t *bytes_written,
2571 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572
2573/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @param pipe Address of the pipe.
2579 * @param data Address to place the data read from pipe.
2580 * @param bytes_to_read Maximum number of data bytes to read.
2581 * @param bytes_read Address of area to hold the number of bytes read.
2582 * @param min_xfer Minimum number of data bytes to read.
2583 * @param timeout Waiting period to wait for the data to be read (in
2584 * milliseconds), or one of the special values K_NO_WAIT
2585 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002587 * @retval 0 At least @a min_xfer bytes of data were read.
2588 * @retval -EIO Returned without waiting; zero data bytes were read.
2589 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002592extern int k_pipe_get(struct k_pipe *pipe, void *data,
2593 size_t bytes_to_read, size_t *bytes_read,
2594 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595
2596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * This routine writes the data contained in a memory block to @a pipe.
2600 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002603 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 * @param block Memory block containing data to send
2605 * @param size Number of data bytes in memory block to send
2606 * @param sem Semaphore to signal upon completion (else NULL)
2607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 */
2610extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2611 size_t size, struct k_sem *sem);
2612
2613/**
Allan Stephensc98da842016-11-11 15:45:03 -05002614 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002615 */
2616
Allan Stephensc98da842016-11-11 15:45:03 -05002617/**
2618 * @cond INTERNAL_HIDDEN
2619 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002621struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002622 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002623 uint32_t num_blocks;
2624 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625 char *buffer;
2626 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002627 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628
Anas Nashif2f203c22016-12-18 06:57:45 -05002629 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630};
2631
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002632#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2633 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634 { \
2635 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002636 .num_blocks = slab_num_blocks, \
2637 .block_size = slab_block_size, \
2638 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 .free_list = NULL, \
2640 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002641 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642 }
2643
Peter Mitsis578f9112016-10-07 13:50:31 -04002644/**
Allan Stephensc98da842016-11-11 15:45:03 -05002645 * INTERNAL_HIDDEN @endcond
2646 */
2647
2648/**
2649 * @defgroup mem_slab_apis Memory Slab APIs
2650 * @ingroup kernel_apis
2651 * @{
2652 */
2653
2654/**
Allan Stephensda827222016-11-09 14:23:58 -06002655 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002656 *
Allan Stephensda827222016-11-09 14:23:58 -06002657 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002659 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2660 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002661 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002662 *
Allan Stephensda827222016-11-09 14:23:58 -06002663 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002666 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * @param name Name of the memory slab.
2669 * @param slab_block_size Size of each memory block (in bytes).
2670 * @param slab_num_blocks Number memory blocks.
2671 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002672 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002673#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2674 char __noinit __aligned(slab_align) \
2675 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2676 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002677 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002678 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2679 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002681/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002682 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002685 *
Allan Stephensda827222016-11-09 14:23:58 -06002686 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2687 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2688 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2689 * To ensure that each memory block is similarly aligned to this boundary,
2690 * @a slab_block_size must also be a multiple of N.
2691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * @param slab Address of the memory slab.
2693 * @param buffer Pointer to buffer used for the memory blocks.
2694 * @param block_size Size of each memory block (in bytes).
2695 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002696 *
2697 * @return N/A
2698 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002699extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002700 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002701
2702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @param slab Address of the memory slab.
2708 * @param mem Pointer to block address area.
2709 * @param timeout Maximum time to wait for operation to complete
2710 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2711 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002712 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002713 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002715 * @retval -ENOMEM Returned without waiting.
2716 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002717 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002718extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2719 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002720
2721/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * This routine releases a previously allocated memory block back to its
2725 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * @param slab Address of the memory slab.
2728 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002729 *
2730 * @return N/A
2731 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002732extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * This routine gets the number of memory blocks that are currently
2738 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002743 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002744static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002746 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747}
2748
Peter Mitsisc001aa82016-10-13 13:53:37 -04002749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * This routine gets the number of memory blocks that are currently
2753 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002758 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002759static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002760{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002761 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002762}
2763
Allan Stephensc98da842016-11-11 15:45:03 -05002764/**
2765 * @} end defgroup mem_slab_apis
2766 */
2767
2768/**
2769 * @cond INTERNAL_HIDDEN
2770 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002772/*
2773 * Memory pool requires a buffer and two arrays of structures for the
2774 * memory block accounting:
2775 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2776 * status of four blocks of memory.
2777 */
2778struct k_mem_pool_quad_block {
2779 char *mem_blocks; /* pointer to the first of four memory blocks */
2780 uint32_t mem_status; /* four bits. If bit is set, memory block is
2781 allocated */
2782};
2783/*
2784 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2785 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2786 * block size is 4 times less than the previous one and thus requires 4 times
2787 * bigger array of k_mem_pool_quad_block structures to keep track of the
2788 * memory blocks.
2789 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002791/*
2792 * The array of k_mem_pool_block_set keeps the information of each array of
2793 * k_mem_pool_quad_block structures
2794 */
2795struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002796 size_t block_size; /* memory block size */
2797 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002798 struct k_mem_pool_quad_block *quad_block;
2799 int count;
2800};
2801
2802/* Memory pool descriptor */
2803struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002804 size_t max_block_size;
2805 size_t min_block_size;
2806 uint32_t nr_of_maxblocks;
2807 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002808 struct k_mem_pool_block_set *block_set;
2809 char *bufblock;
2810 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002811 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812};
2813
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002814#ifdef CONFIG_ARM
2815#define _SECTION_TYPE_SIGN "%"
2816#else
2817#define _SECTION_TYPE_SIGN "@"
2818#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002820/*
2821 * Static memory pool initialization
2822 */
Allan Stephensc98da842016-11-11 15:45:03 -05002823
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002824/*
2825 * Use .altmacro to be able to recalculate values and pass them as string
2826 * arguments when calling assembler macros resursively
2827 */
2828__asm__(".altmacro\n\t");
2829
2830/*
2831 * Recursively calls a macro
2832 * The followig global symbols need to be initialized:
2833 * __memory_pool_max_block_size - maximal size of the memory block
2834 * __memory_pool_min_block_size - minimal size of the memory block
2835 * Notes:
2836 * Global symbols are used due the fact that assembler macro allows only
2837 * one argument be passed with the % conversion
2838 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2839 * is used instead of / 4.
2840 * n_max argument needs to go first in the invoked macro, as some
2841 * assemblers concatenate \name and %(\n_max * 4) arguments
2842 * if \name goes first
2843 */
2844__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2845 ".ifge __memory_pool_max_block_size >> 2 -"
2846 " __memory_pool_min_block_size\n\t\t"
2847 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2848 "\\macro_name %(\\n_max * 4) \\name\n\t"
2849 ".endif\n\t"
2850 ".endm\n");
2851
2852/*
2853 * Build quad blocks
2854 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2855 * structures and recursively calls itself for the next array, 4 times
2856 * larger.
2857 * The followig global symbols need to be initialized:
2858 * __memory_pool_max_block_size - maximal size of the memory block
2859 * __memory_pool_min_block_size - minimal size of the memory block
2860 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2861 */
2862__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002863 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002864 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2865 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2866 ".if \\n_max % 4\n\t\t"
2867 ".skip __memory_pool_quad_block_size\n\t"
2868 ".endif\n\t"
2869 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2870 ".endm\n");
2871
2872/*
2873 * Build block sets and initialize them
2874 * Macro initializes the k_mem_pool_block_set structure and
2875 * recursively calls itself for the next one.
2876 * The followig global symbols need to be initialized:
2877 * __memory_pool_max_block_size - maximal size of the memory block
2878 * __memory_pool_min_block_size - minimal size of the memory block
2879 * __memory_pool_block_set_count, the number of the elements in the
2880 * block set array must be set to 0. Macro calculates it's real
2881 * value.
2882 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2883 * structures, _build_quad_blocks must be called prior it.
2884 */
2885__asm__(".macro _build_block_set n_max, name\n\t"
2886 ".int __memory_pool_max_block_size\n\t" /* block_size */
2887 ".if \\n_max % 4\n\t\t"
2888 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2889 ".else\n\t\t"
2890 ".int \\n_max >> 2\n\t"
2891 ".endif\n\t"
2892 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2893 ".int 0\n\t" /* count */
2894 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2895 "__do_recurse _build_block_set \\name \\n_max\n\t"
2896 ".endm\n");
2897
2898/*
2899 * Build a memory pool structure and initialize it
2900 * Macro uses __memory_pool_block_set_count global symbol,
2901 * block set addresses and buffer address, it may be called only after
2902 * _build_block_set
2903 */
2904__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002905 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002906 _SECTION_TYPE_SIGN "progbits\n\t"
2907 ".globl \\name\n\t"
2908 "\\name:\n\t"
2909 ".int \\max_size\n\t" /* max_block_size */
2910 ".int \\min_size\n\t" /* min_block_size */
2911 ".int \\n_max\n\t" /* nr_of_maxblocks */
2912 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2913 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2914 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2915 ".int 0\n\t" /* wait_q->head */
2916 ".int 0\n\t" /* wait_q->next */
2917 ".popsection\n\t"
2918 ".endm\n");
2919
2920#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2921 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2922 _SECTION_TYPE_SIGN "progbits\n\t"); \
2923 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2924 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2925 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2926 STRINGIFY(name) "\n\t"); \
2927 __asm__(".popsection\n\t")
2928
2929#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2930 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2931 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2932 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2933 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002934 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002935 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2936 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2937 STRINGIFY(name) "\n\t"); \
2938 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2939 __asm__(".int __memory_pool_block_set_count\n\t"); \
2940 __asm__(".popsection\n\t"); \
2941 extern uint32_t _mem_pool_block_set_count_##name; \
2942 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2943
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002944#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2945 char __noinit __aligned(align) \
2946 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002947
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002948/*
2949 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2950 * to __memory_pool_quad_block_size absolute symbol.
2951 * This function does not get called, but compiler calculates the value and
2952 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2953 */
2954static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2955{
2956 __asm__(".globl __memory_pool_quad_block_size\n\t"
2957#ifdef CONFIG_NIOS2
2958 "__memory_pool_quad_block_size = %0\n\t"
2959#else
2960 "__memory_pool_quad_block_size = %c0\n\t"
2961#endif
2962 :
2963 : "n"(sizeof(struct k_mem_pool_quad_block)));
2964}
2965
2966/**
Allan Stephensc98da842016-11-11 15:45:03 -05002967 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002968 */
2969
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002970/**
Allan Stephensc98da842016-11-11 15:45:03 -05002971 * @addtogroup mem_pool_apis
2972 * @{
2973 */
2974
2975/**
2976 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2979 * long. The memory pool allows blocks to be repeatedly partitioned into
2980 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2981 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002982 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002984 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002985 * If the pool is to be accessed outside the module where it is defined, it
2986 * can be declared via
2987 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002988 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @param name Name of the memory pool.
2991 * @param min_size Size of the smallest blocks in the pool (in bytes).
2992 * @param max_size Size of the largest blocks in the pool (in bytes).
2993 * @param n_max Number of maximum sized blocks in the pool.
2994 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002995 */
2996#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002997 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2998 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002999 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003000 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3001 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3002 extern struct k_mem_pool name
3003
Peter Mitsis937042c2016-10-13 13:18:26 -04003004/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @param pool Address of the memory pool.
3010 * @param block Pointer to block descriptor for the allocated memory.
3011 * @param size Amount of memory to allocate (in bytes).
3012 * @param timeout Maximum time to wait for operation to complete
3013 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3014 * or K_FOREVER to wait as long as necessary.
3015 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003016 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003018 * @retval -ENOMEM Returned without waiting.
3019 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003020 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003021extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003022 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003023
3024/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * This routine releases a previously allocated memory block back to its
3028 * memory pool.
3029 *
3030 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003031 *
3032 * @return N/A
3033 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003034extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003035
3036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * This routine instructs a memory pool to concatenate unused memory blocks
3040 * into larger blocks wherever possible. Manually defragmenting the memory
3041 * pool may speed up future allocations of memory blocks by eliminating the
3042 * need for the memory pool to perform an automatic partial defragmentation.
3043 *
3044 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003045 *
3046 * @return N/A
3047 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003048extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003049
3050/**
Allan Stephensc98da842016-11-11 15:45:03 -05003051 * @} end addtogroup mem_pool_apis
3052 */
3053
3054/**
3055 * @defgroup heap_apis Heap Memory Pool APIs
3056 * @ingroup kernel_apis
3057 * @{
3058 */
3059
3060/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003064 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003066 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003068 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003069 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003070extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003071
3072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003073 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003074 *
3075 * This routine provides traditional free() semantics. The memory being
3076 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003077 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003078 * If @a ptr is NULL, no operation is performed.
3079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003080 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003081 *
3082 * @return N/A
3083 */
3084extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085
Allan Stephensc98da842016-11-11 15:45:03 -05003086/**
3087 * @} end defgroup heap_apis
3088 */
3089
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003090/**
3091 * @brief Make the CPU idle.
3092 *
3093 * This function makes the CPU idle until an event wakes it up.
3094 *
3095 * In a regular system, the idle thread should be the only thread responsible
3096 * for making the CPU idle and triggering any type of power management.
3097 * However, in some more constrained systems, such as a single-threaded system,
3098 * the only thread would be responsible for this if needed.
3099 *
3100 * @return N/A
3101 */
3102extern void k_cpu_idle(void);
3103
3104/**
3105 * @brief Make the CPU idle in an atomic fashion.
3106 *
3107 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3108 * must be done atomically before making the CPU idle.
3109 *
3110 * @param key Interrupt locking key obtained from irq_lock().
3111 *
3112 * @return N/A
3113 */
3114extern void k_cpu_atomic_idle(unsigned int key);
3115
Andrew Boie350f88d2017-01-18 13:13:45 -08003116extern void _sys_power_save_idle_exit(int32_t ticks);
3117
Anas Nashifa6149502017-01-17 07:47:31 -05003118/* Include legacy APIs */
3119#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003121#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122#include <arch/cpu.h>
3123
3124/*
3125 * private APIs that are utilized by one or more public APIs
3126 */
3127
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003128#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003130#else
3131#define _init_static_threads() do { } while ((0))
3132#endif
3133
3134extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003135extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136
3137#ifdef __cplusplus
3138}
3139#endif
3140
Andrew Boiee004dec2016-11-07 09:01:19 -08003141#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3142/*
3143 * Define new and delete operators.
3144 * At this moment, the operators do nothing since objects are supposed
3145 * to be statically allocated.
3146 */
3147inline void operator delete(void *ptr)
3148{
3149 (void)ptr;
3150}
3151
3152inline void operator delete[](void *ptr)
3153{
3154 (void)ptr;
3155}
3156
3157inline void *operator new(size_t size)
3158{
3159 (void)size;
3160 return NULL;
3161}
3162
3163inline void *operator new[](size_t size)
3164{
3165 (void)size;
3166 return NULL;
3167}
3168
3169/* Placement versions of operator new and delete */
3170inline void operator delete(void *ptr1, void *ptr2)
3171{
3172 (void)ptr1;
3173 (void)ptr2;
3174}
3175
3176inline void operator delete[](void *ptr1, void *ptr2)
3177{
3178 (void)ptr1;
3179 (void)ptr2;
3180}
3181
3182inline void *operator new(size_t size, void *ptr)
3183{
3184 (void)size;
3185 return ptr;
3186}
3187
3188inline void *operator new[](size_t size, void *ptr)
3189{
3190 (void)size;
3191 return ptr;
3192}
3193
3194#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3195
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003196#endif /* !_ASMLANGUAGE */
3197
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003198#endif /* _kernel__h_ */