blob: bbc2aee3897202682d20f5f4ff8adb6e6df3c821 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050035#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050036#include <kernel_version.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Anas Nashifbbb157d2017-01-15 08:46:31 -050042/**
43 * @brief Kernel APIs
44 * @defgroup kernel_apis Kernel APIs
45 * @{
46 * @}
47 */
48
Anas Nashif61f4b242016-11-18 10:53:59 -050049#ifdef CONFIG_KERNEL_DEBUG
50#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
Benjamin Walsh2f280412017-01-14 19:23:46 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#elif defined(CONFIG_COOP_ENABLED)
60#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
61#define _NUM_PREEMPT_PRIO (0)
62#elif defined(CONFIG_PREEMPT_ENABLED)
63#define _NUM_COOP_PRIO (0)
64#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
65#else
66#error "invalid configuration"
67#endif
68
69#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_PRIO_PREEMPT(x) (x)
71
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_ANY NULL
73#define K_END NULL
74
Benjamin Walshedb35702017-01-14 18:47:22 -050075#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050077#elif defined(CONFIG_COOP_ENABLED)
78#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
79#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050081#else
82#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#endif
84
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050085#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
87#else
88#define K_LOWEST_THREAD_PRIO -1
89#endif
90
Benjamin Walshfab8d922016-11-08 15:36:36 -050091#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
92
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
94#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
95
96typedef sys_dlist_t _wait_q_t;
97
Anas Nashif2f203c22016-12-18 06:57:45 -050098#ifdef CONFIG_OBJECT_TRACING
99#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
100#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500102#define _OBJECT_TRACING_INIT
103#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104#endif
105
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500106#define tcs k_thread
107struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400108struct k_mutex;
109struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400110struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_msgq;
112struct k_mbox;
113struct k_pipe;
114struct k_fifo;
115struct k_lifo;
116struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400117struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_mem_pool;
119struct k_timer;
120
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400121typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123enum execution_context_types {
124 K_ISR = 0,
125 K_COOP_THREAD,
126 K_PREEMPT_THREAD,
127};
128
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400129/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100130 * @defgroup profiling_apis Profiling APIs
131 * @ingroup kernel_apis
132 * @{
133 */
134
135/**
136 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
137 *
138 * This routine calls @ref stack_analyze on the 4 call stacks declared and
139 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
140 *
141 * CONFIG_MAIN_STACK_SIZE
142 * CONFIG_IDLE_STACK_SIZE
143 * CONFIG_ISR_STACK_SIZE
144 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
145 *
146 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
147 * produce output.
148 *
149 * @return N/A
150 */
151extern void k_call_stacks_analyze(void);
152
153/**
154 * @} end defgroup profiling_apis
155 */
156
157/**
Allan Stephensc98da842016-11-11 15:45:03 -0500158 * @defgroup thread_apis Thread APIs
159 * @ingroup kernel_apis
160 * @{
161 */
162
163/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500164 * @typedef k_thread_entry_t
165 * @brief Thread entry point function type.
166 *
167 * A thread's entry point function is invoked when the thread starts executing.
168 * Up to 3 argument values can be passed to the function.
169 *
170 * The thread terminates execution permanently if the entry point function
171 * returns. The thread is responsible for releasing any shared resources
172 * it may own (such as mutexes and dynamically allocated memory), prior to
173 * returning.
174 *
175 * @param p1 First argument.
176 * @param p2 Second argument.
177 * @param p3 Third argument.
178 *
179 * @return N/A
180 */
181typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
182
183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500184 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500186 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500188 * The new thread may be scheduled for immediate execution or a delayed start.
189 * If the newly spawned thread does not have a delayed start the kernel
190 * scheduler may preempt the current thread to allow the new thread to
191 * execute.
192 *
193 * Thread options are architecture-specific, and can include K_ESSENTIAL,
194 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
195 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400196 *
197 * @param stack Pointer to the stack space.
198 * @param stack_size Stack size in bytes.
199 * @param entry Thread entry function.
200 * @param p1 1st entry point parameter.
201 * @param p2 2nd entry point parameter.
202 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500203 * @param prio Thread priority.
204 * @param options Thread options.
205 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500207 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500209extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500210 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400211 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500212 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400213
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500215 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400216 *
Allan Stephensc98da842016-11-11 15:45:03 -0500217 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500218 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
222 * @return N/A
223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400224extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225
226/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500227 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228 *
229 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500230 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400231 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400232 * @return N/A
233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400234extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235
236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500239 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400240 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500241 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242 *
243 * @return N/A
244 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400246
247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500248 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500250 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500252 * If @a thread is not currently sleeping, the routine has no effect.
253 *
254 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400255 *
256 * @return N/A
257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400259
260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500261 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500263 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400265extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400266
267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500268 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500270 * This routine prevents @a thread from executing if it has not yet started
271 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500273 * @param thread ID of thread to cancel.
274 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500275 * @retval 0 Thread spawning cancelled.
276 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400277 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400278extern int k_thread_cancel(k_tid_t thread);
279
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400280/**
Allan Stephensc98da842016-11-11 15:45:03 -0500281 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500283 * This routine permanently stops execution of @a thread. The thread is taken
284 * off all kernel queues it is part of (i.e. the ready queue, the timeout
285 * queue, or a kernel object wait queue). However, any kernel resources the
286 * thread might currently own (such as mutexes or memory blocks) are not
287 * released. It is the responsibility of the caller of this routine to ensure
288 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500290 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400291 *
292 * @return N/A
293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400294extern void k_thread_abort(k_tid_t thread);
295
Allan Stephensc98da842016-11-11 15:45:03 -0500296/**
297 * @cond INTERNAL_HIDDEN
298 */
299
Benjamin Walshd211a522016-12-06 11:44:01 -0500300/* timeout has timed out and is not on _timeout_q anymore */
301#define _EXPIRED (-2)
302
303/* timeout is not in use */
304#define _INACTIVE (-1)
305
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400306#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400307#define _THREAD_TIMEOUT_INIT(obj) \
308 (obj).nano_timeout = { \
309 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400310 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400311 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500312 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400313 },
314#else
315#define _THREAD_TIMEOUT_INIT(obj)
316#endif
317
318#ifdef CONFIG_ERRNO
319#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
320#else
321#define _THREAD_ERRNO_INIT(obj)
322#endif
323
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400324struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400325 union {
326 char *init_stack;
327 struct k_thread *thread;
328 };
329 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500330 void (*init_entry)(void *, void *, void *);
331 void *init_p1;
332 void *init_p2;
333 void *init_p3;
334 int init_prio;
335 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400336 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500337 void (*init_abort)(void);
338 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400339};
340
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400341#define _THREAD_INITIALIZER(stack, stack_size, \
342 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500343 prio, options, delay, abort, groups) \
344 { \
345 .init_stack = (stack), \
346 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400347 .init_entry = (void (*)(void *, void *, void *))entry, \
348 .init_p1 = (void *)p1, \
349 .init_p2 = (void *)p2, \
350 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500351 .init_prio = (prio), \
352 .init_options = (options), \
353 .init_delay = (delay), \
354 .init_abort = (abort), \
355 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400356 }
357
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400358/**
Allan Stephensc98da842016-11-11 15:45:03 -0500359 * INTERNAL_HIDDEN @endcond
360 */
361
362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500363 * @brief Statically define and initialize a thread.
364 *
365 * The thread may be scheduled for immediate execution or a delayed start.
366 *
367 * Thread options are architecture-specific, and can include K_ESSENTIAL,
368 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
369 * them using "|" (the logical OR operator).
370 *
371 * The ID of the thread can be accessed using:
372 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500373 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500374 *
375 * @param name Name of the thread.
376 * @param stack_size Stack size in bytes.
377 * @param entry Thread entry function.
378 * @param p1 1st entry point parameter.
379 * @param p2 2nd entry point parameter.
380 * @param p3 3rd entry point parameter.
381 * @param prio Thread priority.
382 * @param options Thread options.
383 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400384 *
385 * @internal It has been observed that the x86 compiler by default aligns
386 * these _static_thread_data structures to 32-byte boundaries, thereby
387 * wasting space. To work around this, force a 4-byte alignment.
388 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500389#define K_THREAD_DEFINE(name, stack_size, \
390 entry, p1, p2, p3, \
391 prio, options, delay) \
392 char __noinit __stack _k_thread_obj_##name[stack_size]; \
393 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500394 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500395 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
396 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500397 NULL, 0); \
398 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400399
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500401 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500403 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500405 * @param thread ID of thread whose priority is needed.
406 *
407 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400408 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500409extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410
411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500412 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500414 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415 *
416 * Rescheduling can occur immediately depending on the priority @a thread is
417 * set to:
418 *
419 * - If its priority is raised above the priority of the caller of this
420 * function, and the caller is preemptible, @a thread will be scheduled in.
421 *
422 * - If the caller operates on itself, it lowers its priority below that of
423 * other threads in the system, and the caller is preemptible, the thread of
424 * highest priority will be scheduled in.
425 *
426 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
427 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
428 * highest priority.
429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500430 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431 * @param prio New priority.
432 *
433 * @warning Changing the priority of a thread currently involved in mutex
434 * priority inheritance may result in undefined behavior.
435 *
436 * @return N/A
437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400438extern void k_thread_priority_set(k_tid_t thread, int prio);
439
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400440/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500441 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500443 * This routine prevents the kernel scheduler from making @a thread the
444 * current thread. All other internal operations on @a thread are still
445 * performed; for example, any timeout it is waiting on keeps ticking,
446 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500448 * If @a thread is already suspended, the routine has no effect.
449 *
450 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
452 * @return N/A
453 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400454extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400455
456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * This routine allows the kernel scheduler to make @a thread the current
460 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500462 * If @a thread is not currently suspended, the routine has no effect.
463 *
464 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
466 * @return N/A
467 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400468extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400469
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500471 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500473 * This routine specifies how the scheduler will perform time slicing of
474 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500476 * To enable time slicing, @a slice must be non-zero. The scheduler
477 * ensures that no thread runs for more than the specified time limit
478 * before other threads of that priority are given a chance to execute.
479 * Any thread whose priority is higher than @a prio is exempted, and may
480 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500482 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483 * execute. Once the scheduler selects a thread for execution, there is no
484 * minimum guaranteed time the thread will execute before threads of greater or
485 * equal priority are scheduled.
486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500487 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 * for execution, this routine has no effect; the thread is immediately
489 * rescheduled after the slice period expires.
490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500491 * To disable timeslicing, set both @a slice and @a prio to zero.
492 *
493 * @param slice Maximum time slice length (in milliseconds).
494 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400495 *
496 * @return N/A
497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400498extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400499
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400500/**
Allan Stephensc98da842016-11-11 15:45:03 -0500501 * @} end defgroup thread_apis
502 */
503
504/**
505 * @addtogroup isr_apis
506 * @{
507 */
508
509/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500510 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400511 *
Allan Stephensc98da842016-11-11 15:45:03 -0500512 * This routine allows the caller to customize its actions, depending on
513 * whether it is a thread or an ISR.
514 *
515 * @note Can be called by ISRs.
516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500517 * @return 0 if invoked by a thread.
518 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400519 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500520extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521
Benjamin Walsh445830d2016-11-10 15:54:27 -0500522/**
523 * @brief Determine if code is running in a preemptible thread.
524 *
Allan Stephensc98da842016-11-11 15:45:03 -0500525 * This routine allows the caller to customize its actions, depending on
526 * whether it can be preempted by another thread. The routine returns a 'true'
527 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500528 *
Allan Stephensc98da842016-11-11 15:45:03 -0500529 * - The code is running in a thread, not at ISR.
530 * - The thread's priority is in the preemptible range.
531 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500532 *
Allan Stephensc98da842016-11-11 15:45:03 -0500533 * @note Can be called by ISRs.
534 *
535 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500536 * @return Non-zero if invoked by a preemptible thread.
537 */
538extern int k_is_preempt_thread(void);
539
Allan Stephensc98da842016-11-11 15:45:03 -0500540/**
541 * @} end addtogroup isr_apis
542 */
543
544/**
545 * @addtogroup thread_apis
546 * @{
547 */
548
549/**
550 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500551 *
Allan Stephensc98da842016-11-11 15:45:03 -0500552 * This routine prevents the current thread from being preempted by another
553 * thread by instructing the scheduler to treat it as a cooperative thread.
554 * If the thread subsequently performs an operation that makes it unready,
555 * it will be context switched out in the normal manner. When the thread
556 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500557 *
Allan Stephensc98da842016-11-11 15:45:03 -0500558 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500559 *
Allan Stephensc98da842016-11-11 15:45:03 -0500560 * @note k_sched_lock() and k_sched_unlock() should normally be used
561 * when the operation being performed can be safely interrupted by ISRs.
562 * However, if the amount of processing involved is very small, better
563 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500564 *
565 * @return N/A
566 */
567extern void k_sched_lock(void);
568
Allan Stephensc98da842016-11-11 15:45:03 -0500569/**
570 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500571 *
Allan Stephensc98da842016-11-11 15:45:03 -0500572 * This routine reverses the effect of a previous call to k_sched_lock().
573 * A thread must call the routine once for each time it called k_sched_lock()
574 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500575 *
576 * @return N/A
577 */
578extern void k_sched_unlock(void);
579
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500585 * Custom data is not used by the kernel itself, and is freely available
586 * for a thread to use as it sees fit. It can be used as a framework
587 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500589 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
591 * @return N/A
592 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594
595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500598 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400602extern void *k_thread_custom_data_get(void);
603
604/**
Allan Stephensc98da842016-11-11 15:45:03 -0500605 * @} end addtogroup thread_apis
606 */
607
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400608#include <sys_clock.h>
609
Allan Stephensc2f15a42016-11-17 12:24:22 -0500610/**
611 * @addtogroup clock_apis
612 * @{
613 */
614
615/**
616 * @brief Generate null timeout delay.
617 *
618 * This macro generates a timeout delay that that instructs a kernel API
619 * not to wait if the requested operation cannot be performed immediately.
620 *
621 * @return Timeout delay value.
622 */
623#define K_NO_WAIT 0
624
625/**
626 * @brief Generate timeout delay from milliseconds.
627 *
628 * This macro generates a timeout delay that that instructs a kernel API
629 * to wait up to @a ms milliseconds to perform the requested operation.
630 *
631 * @param ms Duration in milliseconds.
632 *
633 * @return Timeout delay value.
634 */
Johan Hedberg14471692016-11-13 10:52:15 +0200635#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500636
637/**
638 * @brief Generate timeout delay from seconds.
639 *
640 * This macro generates a timeout delay that that instructs a kernel API
641 * to wait up to @a s seconds to perform the requested operation.
642 *
643 * @param s Duration in seconds.
644 *
645 * @return Timeout delay value.
646 */
Johan Hedberg14471692016-11-13 10:52:15 +0200647#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500648
649/**
650 * @brief Generate timeout delay from minutes.
651 *
652 * This macro generates a timeout delay that that instructs a kernel API
653 * to wait up to @a m minutes to perform the requested operation.
654 *
655 * @param m Duration in minutes.
656 *
657 * @return Timeout delay value.
658 */
Johan Hedberg14471692016-11-13 10:52:15 +0200659#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500660
661/**
662 * @brief Generate timeout delay from hours.
663 *
664 * This macro generates a timeout delay that that instructs a kernel API
665 * to wait up to @a h hours to perform the requested operation.
666 *
667 * @param h Duration in hours.
668 *
669 * @return Timeout delay value.
670 */
Johan Hedberg14471692016-11-13 10:52:15 +0200671#define K_HOURS(h) K_MINUTES((h) * 60)
672
Allan Stephensc98da842016-11-11 15:45:03 -0500673/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500674 * @brief Generate infinite timeout delay.
675 *
676 * This macro generates a timeout delay that that instructs a kernel API
677 * to wait as long as necessary to perform the requested operation.
678 *
679 * @return Timeout delay value.
680 */
681#define K_FOREVER (-1)
682
683/**
684 * @} end addtogroup clock_apis
685 */
686
687/**
Allan Stephensc98da842016-11-11 15:45:03 -0500688 * @cond INTERNAL_HIDDEN
689 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400690
Benjamin Walsh62092182016-12-20 14:39:08 -0500691/* kernel clocks */
692
693#if (sys_clock_ticks_per_sec == 1000) || \
694 (sys_clock_ticks_per_sec == 500) || \
695 (sys_clock_ticks_per_sec == 250) || \
696 (sys_clock_ticks_per_sec == 125) || \
697 (sys_clock_ticks_per_sec == 100) || \
698 (sys_clock_ticks_per_sec == 50) || \
699 (sys_clock_ticks_per_sec == 25) || \
700 (sys_clock_ticks_per_sec == 20) || \
701 (sys_clock_ticks_per_sec == 10) || \
702 (sys_clock_ticks_per_sec == 1)
703
704 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
705#else
706 /* yields horrible 64-bit math on many architectures: try to avoid */
707 #define _NON_OPTIMIZED_TICKS_PER_SEC
708#endif
709
710#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
711extern int32_t _ms_to_ticks(int32_t ms);
712#else
713static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
714{
715 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
716}
717#endif
718
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500719/* added tick needed to account for tick in progress */
720#define _TICK_ALIGN 1
721
Benjamin Walsh62092182016-12-20 14:39:08 -0500722static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400723{
Benjamin Walsh62092182016-12-20 14:39:08 -0500724#ifdef CONFIG_SYS_CLOCK_EXISTS
725
726#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400727 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400728#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500729 return (uint64_t)ticks * _ms_per_tick;
730#endif
731
732#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400733 __ASSERT(ticks == 0, "");
734 return 0;
735#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400736}
737
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400738/* timeouts */
739
740struct _timeout;
741typedef void (*_timeout_func_t)(struct _timeout *t);
742
743struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500744 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400745 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400746 sys_dlist_t *wait_q;
747 int32_t delta_ticks_from_prev;
748 _timeout_func_t func;
749};
750
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200751extern int32_t _timeout_remaining_get(struct _timeout *timeout);
752
Allan Stephensc98da842016-11-11 15:45:03 -0500753/**
754 * INTERNAL_HIDDEN @endcond
755 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500756
Allan Stephensc98da842016-11-11 15:45:03 -0500757/**
758 * @cond INTERNAL_HIDDEN
759 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400760
761struct k_timer {
762 /*
763 * _timeout structure must be first here if we want to use
764 * dynamic timer allocation. timeout.node is used in the double-linked
765 * list of free timers
766 */
767 struct _timeout timeout;
768
Allan Stephens45bfa372016-10-12 12:39:42 -0500769 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400770 _wait_q_t wait_q;
771
772 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500773 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400774
775 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500776 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400777
778 /* timer period */
779 int32_t period;
780
Allan Stephens45bfa372016-10-12 12:39:42 -0500781 /* timer status */
782 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400783
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500784 /* user-specific data, also used to support legacy features */
785 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400786
Anas Nashif2f203c22016-12-18 06:57:45 -0500787 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400788};
789
Allan Stephens1342adb2016-11-03 13:54:53 -0500790#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400791 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500792 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500793 .timeout.wait_q = NULL, \
794 .timeout.thread = NULL, \
795 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400796 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500797 .expiry_fn = expiry, \
798 .stop_fn = stop, \
799 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500800 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500801 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400802 }
803
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804/**
Allan Stephensc98da842016-11-11 15:45:03 -0500805 * INTERNAL_HIDDEN @endcond
806 */
807
808/**
809 * @defgroup timer_apis Timer APIs
810 * @ingroup kernel_apis
811 * @{
812 */
813
814/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500815 * @typedef k_timer_expiry_t
816 * @brief Timer expiry function type.
817 *
818 * A timer's expiry function is executed by the system clock interrupt handler
819 * each time the timer expires. The expiry function is optional, and is only
820 * invoked if the timer has been initialized with one.
821 *
822 * @param timer Address of timer.
823 *
824 * @return N/A
825 */
826typedef void (*k_timer_expiry_t)(struct k_timer *timer);
827
828/**
829 * @typedef k_timer_stop_t
830 * @brief Timer stop function type.
831 *
832 * A timer's stop function is executed if the timer is stopped prematurely.
833 * The function runs in the context of the thread that stops the timer.
834 * The stop function is optional, and is only invoked if the timer has been
835 * initialized with one.
836 *
837 * @param timer Address of timer.
838 *
839 * @return N/A
840 */
841typedef void (*k_timer_stop_t)(struct k_timer *timer);
842
843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500848 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
850 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @param expiry_fn Function to invoke each time the timer expires.
852 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500854#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500855 struct k_timer name \
856 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500857 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400858
Allan Stephens45bfa372016-10-12 12:39:42 -0500859/**
860 * @brief Initialize a timer.
861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500863 *
864 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @param expiry_fn Function to invoke each time the timer expires.
866 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500867 *
868 * @return N/A
869 */
870extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500871 k_timer_expiry_t expiry_fn,
872 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700873
Allan Stephens45bfa372016-10-12 12:39:42 -0500874/**
875 * @brief Start a timer.
876 *
877 * This routine starts a timer, and resets its status to zero. The timer
878 * begins counting down using the specified duration and period values.
879 *
880 * Attempting to start a timer that is already running is permitted.
881 * The timer's status is reset to zero and the timer begins counting down
882 * using the new duration and period values.
883 *
884 * @param timer Address of timer.
885 * @param duration Initial timer duration (in milliseconds).
886 * @param period Timer period (in milliseconds).
887 *
888 * @return N/A
889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400890extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500891 int32_t duration, int32_t period);
892
893/**
894 * @brief Stop a timer.
895 *
896 * This routine stops a running timer prematurely. The timer's stop function,
897 * if one exists, is invoked by the caller.
898 *
899 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500900 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500901 *
902 * @param timer Address of timer.
903 *
904 * @return N/A
905 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400906extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500907
908/**
909 * @brief Read timer status.
910 *
911 * This routine reads the timer's status, which indicates the number of times
912 * it has expired since its status was last read.
913 *
914 * Calling this routine resets the timer's status to zero.
915 *
916 * @param timer Address of timer.
917 *
918 * @return Timer status.
919 */
920extern uint32_t k_timer_status_get(struct k_timer *timer);
921
922/**
923 * @brief Synchronize thread to timer expiration.
924 *
925 * This routine blocks the calling thread until the timer's status is non-zero
926 * (indicating that it has expired at least once since it was last examined)
927 * or the timer is stopped. If the timer status is already non-zero,
928 * or the timer is already stopped, the caller continues without waiting.
929 *
930 * Calling this routine resets the timer's status to zero.
931 *
932 * This routine must not be used by interrupt handlers, since they are not
933 * allowed to block.
934 *
935 * @param timer Address of timer.
936 *
937 * @return Timer status.
938 */
939extern uint32_t k_timer_status_sync(struct k_timer *timer);
940
941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500942 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500943 *
944 * This routine computes the (approximate) time remaining before a running
945 * timer next expires. If the timer is not running, it returns zero.
946 *
947 * @param timer Address of timer.
948 *
949 * @return Remaining time (in milliseconds).
950 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200951static inline int32_t k_timer_remaining_get(struct k_timer *timer)
952{
953 return _timeout_remaining_get(&timer->timeout);
954}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400955
Allan Stephensc98da842016-11-11 15:45:03 -0500956/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500957 * @brief Associate user-specific data with a timer.
958 *
959 * This routine records the @a user_data with the @a timer, to be retrieved
960 * later.
961 *
962 * It can be used e.g. in a timer handler shared across multiple subsystems to
963 * retrieve data specific to the subsystem this timer is associated with.
964 *
965 * @param timer Address of timer.
966 * @param user_data User data to associate with the timer.
967 *
968 * @return N/A
969 */
970static inline void k_timer_user_data_set(struct k_timer *timer,
971 void *user_data)
972{
973 timer->user_data = user_data;
974}
975
976/**
977 * @brief Retrieve the user-specific data from a timer.
978 *
979 * @param timer Address of timer.
980 *
981 * @return The user data.
982 */
983static inline void *k_timer_user_data_get(struct k_timer *timer)
984{
985 return timer->user_data;
986}
987
988/**
Allan Stephensc98da842016-11-11 15:45:03 -0500989 * @} end defgroup timer_apis
990 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400991
Allan Stephensc98da842016-11-11 15:45:03 -0500992/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500993 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500994 * @{
995 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500996
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400997/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * This routine returns the elapsed time since the system booted,
1001 * in milliseconds.
1002 *
1003 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001004 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001005extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001006
1007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * This routine returns the lower 32-bits of the elapsed time since the system
1011 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * This routine can be more efficient than k_uptime_get(), as it reduces the
1014 * need for interrupt locking and 64-bit math. However, the 32-bit result
1015 * cannot hold a system uptime time larger than approximately 50 days, so the
1016 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001020extern uint32_t k_uptime_get_32(void);
1021
1022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * This routine computes the elapsed time between the current system uptime
1026 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @param reftime Pointer to a reference time, which is updated to the current
1029 * uptime upon return.
1030 *
1031 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001032 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001034
1035/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001036 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * This routine computes the elapsed time between the current system uptime
1039 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1042 * need for interrupt locking and 64-bit math. However, the 32-bit result
1043 * cannot hold an elapsed time larger than approximately 50 days, so the
1044 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @param reftime Pointer to a reference time, which is updated to the current
1047 * uptime upon return.
1048 *
1049 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001050 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001051extern uint32_t k_uptime_delta_32(int64_t *reftime);
1052
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001053/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * This routine returns the current time, as measured by the system's hardware
1057 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001059 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001060 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001061extern uint32_t k_cycle_get_32(void);
1062
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001064 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001065 */
1066
Allan Stephensc98da842016-11-11 15:45:03 -05001067/**
1068 * @cond INTERNAL_HIDDEN
1069 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070
1071struct k_fifo {
1072 _wait_q_t wait_q;
1073 sys_slist_t data_q;
1074
Anas Nashif2f203c22016-12-18 06:57:45 -05001075 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001076};
1077
Allan Stephensc98da842016-11-11 15:45:03 -05001078#define K_FIFO_INITIALIZER(obj) \
1079 { \
1080 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1081 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001082 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001083 }
1084
1085/**
1086 * INTERNAL_HIDDEN @endcond
1087 */
1088
1089/**
1090 * @defgroup fifo_apis Fifo APIs
1091 * @ingroup kernel_apis
1092 * @{
1093 */
1094
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001096 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001098 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
1102 * @return N/A
1103 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001104extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001105
1106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001109 * This routine adds a data item to @a fifo. A fifo data item must be
1110 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1111 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * @note Can be called by ISRs.
1114 *
1115 * @param fifo Address of the fifo.
1116 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
1118 * @return N/A
1119 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001120extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121
1122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * This routine adds a list of data items to @a fifo in one operation.
1126 * The data items must be in a singly-linked list, with the first 32 bits
1127 * each data item pointing to the next data item; the list must be
1128 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001130 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * @param fifo Address of the fifo.
1133 * @param head Pointer to first node in singly-linked list.
1134 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
1136 * @return N/A
1137 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001138extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139
1140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001141 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * This routine adds a list of data items to @a fifo in one operation.
1144 * The data items must be in a singly-linked list implemented using a
1145 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 * and must be re-initialized via sys_slist_init().
1147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001148 * @note Can be called by ISRs.
1149 *
1150 * @param fifo Address of the fifo.
1151 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
1153 * @return N/A
1154 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001156
1157/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001158 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001160 * This routine removes a data item from @a fifo in a "first in, first out"
1161 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1164 *
1165 * @param fifo Address of the fifo.
1166 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 * or one of the special values K_NO_WAIT and K_FOREVER.
1168 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001169 * @return Address of the data item if successful; NULL if returned
1170 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001171 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001172extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1173
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001175 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001179 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001183#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001184 struct k_fifo name \
1185 __in_section(_k_fifo, static, name) = \
1186 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187
Allan Stephensc98da842016-11-11 15:45:03 -05001188/**
1189 * @} end defgroup fifo_apis
1190 */
1191
1192/**
1193 * @cond INTERNAL_HIDDEN
1194 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001195
1196struct k_lifo {
1197 _wait_q_t wait_q;
1198 void *list;
1199
Anas Nashif2f203c22016-12-18 06:57:45 -05001200 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201};
1202
Allan Stephensc98da842016-11-11 15:45:03 -05001203#define K_LIFO_INITIALIZER(obj) \
1204 { \
1205 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1206 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001207 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001208 }
1209
1210/**
1211 * INTERNAL_HIDDEN @endcond
1212 */
1213
1214/**
1215 * @defgroup lifo_apis Lifo APIs
1216 * @ingroup kernel_apis
1217 * @{
1218 */
1219
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001226 *
1227 * @return N/A
1228 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230
1231/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001232 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * This routine adds a data item to @a lifo. A lifo data item must be
1235 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1236 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001238 * @note Can be called by ISRs.
1239 *
1240 * @param lifo Address of the lifo.
1241 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 *
1243 * @return N/A
1244 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246
1247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001248 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001250 * This routine removes a data item from @a lifo in a "last in, first out"
1251 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001253 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1254 *
1255 * @param lifo Address of the lifo.
1256 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001257 * or one of the special values K_NO_WAIT and K_FOREVER.
1258 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001259 * @return Address of the data item if successful; NULL if returned
1260 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001261 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001262extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1263
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001265 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001269 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001271 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001274 struct k_lifo name \
1275 __in_section(_k_lifo, static, name) = \
1276 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001277
Allan Stephensc98da842016-11-11 15:45:03 -05001278/**
1279 * @} end defgroup lifo_apis
1280 */
1281
1282/**
1283 * @cond INTERNAL_HIDDEN
1284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285
1286struct k_stack {
1287 _wait_q_t wait_q;
1288 uint32_t *base, *next, *top;
1289
Anas Nashif2f203c22016-12-18 06:57:45 -05001290 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291};
1292
Allan Stephensc98da842016-11-11 15:45:03 -05001293#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1294 { \
1295 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1296 .base = stack_buffer, \
1297 .next = stack_buffer, \
1298 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001299 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001300 }
1301
1302/**
1303 * INTERNAL_HIDDEN @endcond
1304 */
1305
1306/**
1307 * @defgroup stack_apis Stack APIs
1308 * @ingroup kernel_apis
1309 * @{
1310 */
1311
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001312/**
1313 * @brief Initialize a stack.
1314 *
1315 * This routine initializes a stack object, prior to its first use.
1316 *
1317 * @param stack Address of the stack.
1318 * @param buffer Address of array used to hold stacked values.
1319 * @param num_entries Maximum number of values that can be stacked.
1320 *
1321 * @return N/A
1322 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001323extern void k_stack_init(struct k_stack *stack,
1324 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001325
1326/**
1327 * @brief Push an element onto a stack.
1328 *
1329 * This routine adds a 32-bit value @a data to @a stack.
1330 *
1331 * @note Can be called by ISRs.
1332 *
1333 * @param stack Address of the stack.
1334 * @param data Value to push onto the stack.
1335 *
1336 * @return N/A
1337 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001338extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001339
1340/**
1341 * @brief Pop an element from a stack.
1342 *
1343 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1344 * manner and stores the value in @a data.
1345 *
1346 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1347 *
1348 * @param stack Address of the stack.
1349 * @param data Address of area to hold the value popped from the stack.
1350 * @param timeout Waiting period to obtain a value (in milliseconds),
1351 * or one of the special values K_NO_WAIT and K_FOREVER.
1352 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001353 * @retval 0 Element popped from stack.
1354 * @retval -EBUSY Returned without waiting.
1355 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001357extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1358
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001362 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001363 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001364 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001366 * @param name Name of the stack.
1367 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001369#define K_STACK_DEFINE(name, stack_num_entries) \
1370 uint32_t __noinit \
1371 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001372 struct k_stack name \
1373 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001374 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1375 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001376
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377/**
Allan Stephensc98da842016-11-11 15:45:03 -05001378 * @} end defgroup stack_apis
1379 */
1380
Allan Stephens6bba9b02016-11-16 14:56:54 -05001381struct k_work;
1382
Allan Stephensc98da842016-11-11 15:45:03 -05001383/**
1384 * @defgroup workqueue_apis Workqueue Thread APIs
1385 * @ingroup kernel_apis
1386 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387 */
1388
Allan Stephens6bba9b02016-11-16 14:56:54 -05001389/**
1390 * @typedef k_work_handler_t
1391 * @brief Work item handler function type.
1392 *
1393 * A work item's handler function is executed by a workqueue's thread
1394 * when the work item is processed by the workqueue.
1395 *
1396 * @param work Address of the work item.
1397 *
1398 * @return N/A
1399 */
1400typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401
1402/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001403 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001405
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406struct k_work_q {
1407 struct k_fifo fifo;
1408};
1409
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001411 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412};
1413
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414struct k_work {
1415 void *_reserved; /* Used by k_fifo implementation. */
1416 k_work_handler_t handler;
1417 atomic_t flags[1];
1418};
1419
Allan Stephens6bba9b02016-11-16 14:56:54 -05001420struct k_delayed_work {
1421 struct k_work work;
1422 struct _timeout timeout;
1423 struct k_work_q *work_q;
1424};
1425
1426extern struct k_work_q k_sys_work_q;
1427
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001429 * INTERNAL_HIDDEN @endcond
1430 */
1431
1432/**
1433 * @brief Initialize a statically-defined work item.
1434 *
1435 * This macro can be used to initialize a statically-defined workqueue work
1436 * item, prior to its first use. For example,
1437 *
1438 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1439 *
1440 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441 */
1442#define K_WORK_INITIALIZER(work_handler) \
1443 { \
1444 ._reserved = NULL, \
1445 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001446 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001447 }
1448
1449/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001450 * @brief Initialize a work item.
1451 *
1452 * This routine initializes a workqueue work item, prior to its first use.
1453 *
1454 * @param work Address of work item.
1455 * @param handler Function to invoke each time work item is processed.
1456 *
1457 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001458 */
1459static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1460{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001461 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462 work->handler = handler;
1463}
1464
1465/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001466 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001467 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001468 * This routine submits work item @a work to be processed by workqueue
1469 * @a work_q. If the work item is already pending in the workqueue's queue
1470 * as a result of an earlier submission, this routine has no effect on the
1471 * work item. If the work item has already been processed, or is currently
1472 * being processed, its work is considered complete and the work item can be
1473 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001474 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001475 * @warning
1476 * A submitted work item must not be modified until it has been processed
1477 * by the workqueue.
1478 *
1479 * @note Can be called by ISRs.
1480 *
1481 * @param work_q Address of workqueue.
1482 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001483 *
1484 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001485 */
1486static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1487 struct k_work *work)
1488{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001489 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001490 k_fifo_put(&work_q->fifo, work);
1491 }
1492}
1493
1494/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001495 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001497 * This routine indicates if work item @a work is pending in a workqueue's
1498 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001500 * @note Can be called by ISRs.
1501 *
1502 * @param work Address of work item.
1503 *
1504 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001505 */
1506static inline int k_work_pending(struct k_work *work)
1507{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001508 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001509}
1510
1511/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001512 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001514 * This routine starts workqueue @a work_q. The workqueue spawns its work
1515 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001516 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001517 * @param work_q Address of workqueue.
1518 * @param stack Pointer to work queue thread's stack space.
1519 * @param stack_size Size of the work queue thread's stack (in bytes).
1520 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001521 *
1522 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001523 */
Allan Stephens904cf972016-10-07 13:59:23 -05001524extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001525 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001526
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001528 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001530 * This routine initializes a workqueue delayed work item, prior to
1531 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001532 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001533 * @param work Address of delayed work item.
1534 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001535 *
1536 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001538extern void k_delayed_work_init(struct k_delayed_work *work,
1539 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001540
1541/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001542 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001543 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001544 * This routine schedules work item @a work to be processed by workqueue
1545 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1546 * an asychronous countdown for the work item and then returns to the caller.
1547 * Only when the countdown completes is the work item actually submitted to
1548 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001549 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001550 * Submitting a previously submitted delayed work item that is still
1551 * counting down cancels the existing submission and restarts the countdown
1552 * using the new delay. If the work item is currently pending on the
1553 * workqueue's queue because the countdown has completed it is too late to
1554 * resubmit the item, and resubmission fails without impacting the work item.
1555 * If the work item has already been processed, or is currently being processed,
1556 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001557 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001558 * @warning
1559 * A delayed work item must not be modified until it has been processed
1560 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001562 * @note Can be called by ISRs.
1563 *
1564 * @param work_q Address of workqueue.
1565 * @param work Address of delayed work item.
1566 * @param delay Delay before submitting the work item (in milliseconds).
1567 *
1568 * @retval 0 Work item countdown started.
1569 * @retval -EINPROGRESS Work item is already pending.
1570 * @retval -EINVAL Work item is being processed or has completed its work.
1571 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001573extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1574 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001575 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001576
1577/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001578 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001580 * This routine cancels the submission of delayed work item @a work.
1581 * A delayed work item can only be cancelled while its countdown is still
1582 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001583 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001584 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001586 * @param work Address of delayed work item.
1587 *
1588 * @retval 0 Work item countdown cancelled.
1589 * @retval -EINPROGRESS Work item is already pending.
1590 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001592extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001593
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001594/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001595 * @brief Submit a work item to the system workqueue.
1596 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001597 * This routine submits work item @a work to be processed by the system
1598 * workqueue. If the work item is already pending in the workqueue's queue
1599 * as a result of an earlier submission, this routine has no effect on the
1600 * work item. If the work item has already been processed, or is currently
1601 * being processed, its work is considered complete and the work item can be
1602 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001603 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001604 * @warning
1605 * Work items submitted to the system workqueue should avoid using handlers
1606 * that block or yield since this may prevent the system workqueue from
1607 * processing other work items in a timely manner.
1608 *
1609 * @note Can be called by ISRs.
1610 *
1611 * @param work Address of work item.
1612 *
1613 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001614 */
1615static inline void k_work_submit(struct k_work *work)
1616{
1617 k_work_submit_to_queue(&k_sys_work_q, work);
1618}
1619
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001621 * @brief Submit a delayed work item to the system workqueue.
1622 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001623 * This routine schedules work item @a work to be processed by the system
1624 * workqueue after a delay of @a delay milliseconds. The routine initiates
1625 * an asychronous countdown for the work item and then returns to the caller.
1626 * Only when the countdown completes is the work item actually submitted to
1627 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001628 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001629 * Submitting a previously submitted delayed work item that is still
1630 * counting down cancels the existing submission and restarts the countdown
1631 * using the new delay. If the work item is currently pending on the
1632 * workqueue's queue because the countdown has completed it is too late to
1633 * resubmit the item, and resubmission fails without impacting the work item.
1634 * If the work item has already been processed, or is currently being processed,
1635 * its work is considered complete and the work item can be resubmitted.
1636 *
1637 * @warning
1638 * Work items submitted to the system workqueue should avoid using handlers
1639 * that block or yield since this may prevent the system workqueue from
1640 * processing other work items in a timely manner.
1641 *
1642 * @note Can be called by ISRs.
1643 *
1644 * @param work Address of delayed work item.
1645 * @param delay Delay before submitting the work item (in milliseconds).
1646 *
1647 * @retval 0 Work item countdown started.
1648 * @retval -EINPROGRESS Work item is already pending.
1649 * @retval -EINVAL Work item is being processed or has completed its work.
1650 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651 */
1652static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001653 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001654{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001655 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656}
1657
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001659 * @brief Get time remaining before a delayed work gets scheduled.
1660 *
1661 * This routine computes the (approximate) time remaining before a
1662 * delayed work gets executed. If the delayed work is not waiting to be
1663 * schedules, it returns zero.
1664 *
1665 * @param work Delayed work item.
1666 *
1667 * @return Remaining time (in milliseconds).
1668 */
1669static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1670{
1671 return _timeout_remaining_get(&work->timeout);
1672}
1673
1674/**
Allan Stephensc98da842016-11-11 15:45:03 -05001675 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001676 */
1677
Allan Stephensc98da842016-11-11 15:45:03 -05001678/**
1679 * @cond INTERNAL_HIDDEN
1680 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001681
1682struct k_mutex {
1683 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001684 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001685 uint32_t lock_count;
1686 int owner_orig_prio;
1687#ifdef CONFIG_OBJECT_MONITOR
1688 int num_lock_state_changes;
1689 int num_conflicts;
1690#endif
1691
Anas Nashif2f203c22016-12-18 06:57:45 -05001692 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001693};
1694
1695#ifdef CONFIG_OBJECT_MONITOR
1696#define _MUTEX_INIT_OBJECT_MONITOR \
1697 .num_lock_state_changes = 0, .num_conflicts = 0,
1698#else
1699#define _MUTEX_INIT_OBJECT_MONITOR
1700#endif
1701
1702#define K_MUTEX_INITIALIZER(obj) \
1703 { \
1704 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1705 .owner = NULL, \
1706 .lock_count = 0, \
1707 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1708 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001709 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001710 }
1711
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001712/**
Allan Stephensc98da842016-11-11 15:45:03 -05001713 * INTERNAL_HIDDEN @endcond
1714 */
1715
1716/**
1717 * @defgroup mutex_apis Mutex APIs
1718 * @ingroup kernel_apis
1719 * @{
1720 */
1721
1722/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001727 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001731#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001732 struct k_mutex name \
1733 __in_section(_k_mutex, static, name) = \
1734 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001735
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * Upon completion, the mutex is available and does not have an owner.
1742 *
1743 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 *
1745 * @return N/A
1746 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748
1749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * This routine locks @a mutex. If the mutex is locked by another thread,
1753 * the calling thread waits until the mutex becomes available or until
1754 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001756 * A thread is permitted to lock a mutex it has already locked. The operation
1757 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * @param mutex Address of the mutex.
1760 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761 * or one of the special values K_NO_WAIT and K_FOREVER.
1762 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001763 * @retval 0 Mutex locked.
1764 * @retval -EBUSY Returned without waiting.
1765 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001767extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768
1769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001770 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * This routine unlocks @a mutex. The mutex must already be locked by the
1773 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 *
1775 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 * thread.
1778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
1781 * @return N/A
1782 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001783extern void k_mutex_unlock(struct k_mutex *mutex);
1784
Allan Stephensc98da842016-11-11 15:45:03 -05001785/**
1786 * @} end defgroup mutex_apis
1787 */
1788
1789/**
1790 * @cond INTERNAL_HIDDEN
1791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001792
1793struct k_sem {
1794 _wait_q_t wait_q;
1795 unsigned int count;
1796 unsigned int limit;
1797
Anas Nashif2f203c22016-12-18 06:57:45 -05001798 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001799};
1800
Allan Stephensc98da842016-11-11 15:45:03 -05001801#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1802 { \
1803 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1804 .count = initial_count, \
1805 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001806 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001807 }
1808
1809/**
1810 * INTERNAL_HIDDEN @endcond
1811 */
1812
1813/**
1814 * @defgroup semaphore_apis Semaphore APIs
1815 * @ingroup kernel_apis
1816 * @{
1817 */
1818
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * @param sem Address of the semaphore.
1825 * @param initial_count Initial semaphore count.
1826 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001827 *
1828 * @return N/A
1829 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1831 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001832
1833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001834 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1839 *
1840 * @param sem Address of the semaphore.
1841 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001842 * or one of the special values K_NO_WAIT and K_FOREVER.
1843 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001844 * @note When porting code from the nanokernel legacy API to the new API, be
1845 * careful with the return value of this function. The return value is the
1846 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1847 * non-zero means failure, while the nano_sem_take family returns 1 for success
1848 * and 0 for failure.
1849 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001850 * @retval 0 Semaphore taken.
1851 * @retval -EBUSY Returned without waiting.
1852 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001853 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001854extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001855
1856/**
1857 * @brief Give a semaphore.
1858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001859 * This routine gives @a sem, unless the semaphore is already at its maximum
1860 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001865 *
1866 * @return N/A
1867 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001868extern void k_sem_give(struct k_sem *sem);
1869
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001870/**
1871 * @brief Reset a semaphore's count to zero.
1872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001876 *
1877 * @return N/A
1878 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001879static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001880{
1881 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001882}
1883
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001884/**
1885 * @brief Get a semaphore's count.
1886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001892 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001893static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001894{
1895 return sem->count;
1896}
1897
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001899 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001901 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001902 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001903 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @param name Name of the semaphore.
1906 * @param initial_count Initial semaphore count.
1907 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001908 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001910 struct k_sem name \
1911 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001912 K_SEM_INITIALIZER(name, initial_count, count_limit)
1913
Allan Stephensc98da842016-11-11 15:45:03 -05001914/**
1915 * @} end defgroup semaphore_apis
1916 */
1917
1918/**
1919 * @defgroup alert_apis Alert APIs
1920 * @ingroup kernel_apis
1921 * @{
1922 */
1923
Allan Stephens5eceb852016-11-16 10:16:30 -05001924/**
1925 * @typedef k_alert_handler_t
1926 * @brief Alert handler function type.
1927 *
1928 * An alert's alert handler function is invoked by the system workqueue
1929 * when the alert is signalled. The alert handler function is optional,
1930 * and is only invoked if the alert has been initialized with one.
1931 *
1932 * @param alert Address of the alert.
1933 *
1934 * @return 0 if alert has been consumed; non-zero if alert should pend.
1935 */
1936typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001937
1938/**
1939 * @} end defgroup alert_apis
1940 */
1941
1942/**
1943 * @cond INTERNAL_HIDDEN
1944 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001946#define K_ALERT_DEFAULT NULL
1947#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001949struct k_alert {
1950 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951 atomic_t send_count;
1952 struct k_work work_item;
1953 struct k_sem sem;
1954
Anas Nashif2f203c22016-12-18 06:57:45 -05001955 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956};
1957
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001958extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001960#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001962 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001964 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001965 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001966 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967 }
1968
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969/**
Allan Stephensc98da842016-11-11 15:45:03 -05001970 * INTERNAL_HIDDEN @endcond
1971 */
1972
1973/**
1974 * @addtogroup alert_apis
1975 * @{
1976 */
1977
1978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001981 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001983 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001985 * @param name Name of the alert.
1986 * @param alert_handler Action to take when alert is sent. Specify either
1987 * the address of a function to be invoked by the system workqueue
1988 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1989 * K_ALERT_DEFAULT (which causes the alert to pend).
1990 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001991 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001992#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001993 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001994 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001995 K_ALERT_INITIALIZER(name, alert_handler, \
1996 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001999 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * @param alert Address of the alert.
2004 * @param handler Action to take when alert is sent. Specify either the address
2005 * of a function to be invoked by the system workqueue thread,
2006 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2007 * K_ALERT_DEFAULT (which causes the alert to pend).
2008 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 *
2010 * @return N/A
2011 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002012extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2013 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014
2015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002016 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2021 *
2022 * @param alert Address of the alert.
2023 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024 * or one of the special values K_NO_WAIT and K_FOREVER.
2025 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002026 * @retval 0 Alert received.
2027 * @retval -EBUSY Returned without waiting.
2028 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002030extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031
2032/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * This routine signals @a alert. The action specified for @a alert will
2036 * be taken, which may trigger the execution of an alert handler function
2037 * and/or cause the alert to pend (assuming the alert has not reached its
2038 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002040 * @note Can be called by ISRs.
2041 *
2042 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 *
2044 * @return N/A
2045 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002046extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047
2048/**
Allan Stephensc98da842016-11-11 15:45:03 -05002049 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050 */
2051
Allan Stephensc98da842016-11-11 15:45:03 -05002052/**
2053 * @cond INTERNAL_HIDDEN
2054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055
2056struct k_msgq {
2057 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002058 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059 uint32_t max_msgs;
2060 char *buffer_start;
2061 char *buffer_end;
2062 char *read_ptr;
2063 char *write_ptr;
2064 uint32_t used_msgs;
2065
Anas Nashif2f203c22016-12-18 06:57:45 -05002066 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002067};
2068
Peter Mitsis1da807e2016-10-06 11:36:59 -04002069#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002070 { \
2071 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002072 .max_msgs = q_max_msgs, \
2073 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002075 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076 .read_ptr = q_buffer, \
2077 .write_ptr = q_buffer, \
2078 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002079 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002080 }
2081
Peter Mitsis1da807e2016-10-06 11:36:59 -04002082/**
Allan Stephensc98da842016-11-11 15:45:03 -05002083 * INTERNAL_HIDDEN @endcond
2084 */
2085
2086/**
2087 * @defgroup msgq_apis Message Queue APIs
2088 * @ingroup kernel_apis
2089 * @{
2090 */
2091
2092/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2096 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002097 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2098 * message is similarly aligned to this boundary, @a q_msg_size must also be
2099 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * The message queue can be accessed outside the module where it is defined
2102 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002104 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * @param q_name Name of the message queue.
2107 * @param q_msg_size Message size (in bytes).
2108 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002109 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002110 */
2111#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2112 static char __noinit __aligned(q_align) \
2113 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002114 struct k_msgq q_name \
2115 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002116 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2117 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118
Peter Mitsisd7a37502016-10-13 11:37:40 -04002119/**
2120 * @brief Initialize a message queue.
2121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002122 * This routine initializes a message queue object, prior to its first use.
2123 *
Allan Stephensda827222016-11-09 14:23:58 -06002124 * The message queue's ring buffer must contain space for @a max_msgs messages,
2125 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2126 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2127 * that each message is similarly aligned to this boundary, @a q_msg_size
2128 * must also be a multiple of N.
2129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * @param q Address of the message queue.
2131 * @param buffer Pointer to ring buffer that holds queued messages.
2132 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002133 * @param max_msgs Maximum number of messages that can be queued.
2134 *
2135 * @return N/A
2136 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002137extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002138 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002139
2140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002143 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002144 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002145 * @note Can be called by ISRs.
2146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * @param q Address of the message queue.
2148 * @param data Pointer to the message.
2149 * @param timeout Waiting period to add the message (in milliseconds),
2150 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002151 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002152 * @retval 0 Message sent.
2153 * @retval -ENOMSG Returned without waiting or queue purged.
2154 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002155 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002157
2158/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 * This routine receives a message from message queue @a q in a "first in,
2162 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002163 *
Allan Stephensc98da842016-11-11 15:45:03 -05002164 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002166 * @param q Address of the message queue.
2167 * @param data Address of area to hold the received message.
2168 * @param timeout Waiting period to receive the message (in milliseconds),
2169 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002170 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002171 * @retval 0 Message received.
2172 * @retval -ENOMSG Returned without waiting.
2173 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002174 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002176
2177/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * This routine discards all unreceived messages in a message queue's ring
2181 * buffer. Any threads that are blocked waiting to send a message to the
2182 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002185 *
2186 * @return N/A
2187 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188extern void k_msgq_purge(struct k_msgq *q);
2189
Peter Mitsis67be2492016-10-07 11:44:34 -04002190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002191 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002193 * This routine returns the number of unused entries in a message queue's
2194 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * @param q Address of the message queue.
2197 *
2198 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002199 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002200static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002201{
2202 return q->max_msgs - q->used_msgs;
2203}
2204
Peter Mitsisd7a37502016-10-13 11:37:40 -04002205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002208 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002210 * @param q Address of the message queue.
2211 *
2212 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002213 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002214static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215{
2216 return q->used_msgs;
2217}
2218
Allan Stephensc98da842016-11-11 15:45:03 -05002219/**
2220 * @} end defgroup msgq_apis
2221 */
2222
2223/**
2224 * @defgroup mem_pool_apis Memory Pool APIs
2225 * @ingroup kernel_apis
2226 * @{
2227 */
2228
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002229struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002230 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231 void *addr_in_pool;
2232 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002233 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234};
2235
Allan Stephensc98da842016-11-11 15:45:03 -05002236/**
2237 * @} end defgroup mem_pool_apis
2238 */
2239
2240/**
2241 * @defgroup mailbox_apis Mailbox APIs
2242 * @ingroup kernel_apis
2243 * @{
2244 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002245
2246struct k_mbox_msg {
2247 /** internal use only - needed for legacy API support */
2248 uint32_t _mailbox;
2249 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002250 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251 /** application-defined information value */
2252 uint32_t info;
2253 /** sender's message data buffer */
2254 void *tx_data;
2255 /** internal use only - needed for legacy API support */
2256 void *_rx_data;
2257 /** message data block descriptor */
2258 struct k_mem_block tx_block;
2259 /** source thread id */
2260 k_tid_t rx_source_thread;
2261 /** target thread id */
2262 k_tid_t tx_target_thread;
2263 /** internal use only - thread waiting on send (may be a dummy) */
2264 k_tid_t _syncing_thread;
2265#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2266 /** internal use only - semaphore used during asynchronous send */
2267 struct k_sem *_async_sem;
2268#endif
2269};
2270
Allan Stephensc98da842016-11-11 15:45:03 -05002271/**
2272 * @cond INTERNAL_HIDDEN
2273 */
2274
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275struct k_mbox {
2276 _wait_q_t tx_msg_queue;
2277 _wait_q_t rx_msg_queue;
2278
Anas Nashif2f203c22016-12-18 06:57:45 -05002279 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280};
2281
2282#define K_MBOX_INITIALIZER(obj) \
2283 { \
2284 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2285 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002286 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287 }
2288
Peter Mitsis12092702016-10-14 12:57:23 -04002289/**
Allan Stephensc98da842016-11-11 15:45:03 -05002290 * INTERNAL_HIDDEN @endcond
2291 */
2292
2293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002296 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002298 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002300 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002301 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002303 struct k_mbox name \
2304 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 K_MBOX_INITIALIZER(name) \
2306
Peter Mitsis12092702016-10-14 12:57:23 -04002307/**
2308 * @brief Initialize a mailbox.
2309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002310 * This routine initializes a mailbox object, prior to its first use.
2311 *
2312 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002313 *
2314 * @return N/A
2315 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316extern void k_mbox_init(struct k_mbox *mbox);
2317
Peter Mitsis12092702016-10-14 12:57:23 -04002318/**
2319 * @brief Send a mailbox message in a synchronous manner.
2320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321 * This routine sends a message to @a mbox and waits for a receiver to both
2322 * receive and process it. The message data may be in a buffer, in a memory
2323 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @param mbox Address of the mailbox.
2326 * @param tx_msg Address of the transmit message descriptor.
2327 * @param timeout Waiting period for the message to be received (in
2328 * milliseconds), or one of the special values K_NO_WAIT
2329 * and K_FOREVER. Once the message has been received,
2330 * this routine waits as long as necessary for the message
2331 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002332 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002333 * @retval 0 Message sent.
2334 * @retval -ENOMSG Returned without waiting.
2335 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002336 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002337extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002339
Peter Mitsis12092702016-10-14 12:57:23 -04002340/**
2341 * @brief Send a mailbox message in an asynchronous manner.
2342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * This routine sends a message to @a mbox without waiting for a receiver
2344 * to process it. The message data may be in a buffer, in a memory pool block,
2345 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2346 * will be given when the message has been both received and completely
2347 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * @param mbox Address of the mailbox.
2350 * @param tx_msg Address of the transmit message descriptor.
2351 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002352 *
2353 * @return N/A
2354 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002355extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356 struct k_sem *sem);
2357
Peter Mitsis12092702016-10-14 12:57:23 -04002358/**
2359 * @brief Receive a mailbox message.
2360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * This routine receives a message from @a mbox, then optionally retrieves
2362 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002364 * @param mbox Address of the mailbox.
2365 * @param rx_msg Address of the receive message descriptor.
2366 * @param buffer Address of the buffer to receive data, or NULL to defer data
2367 * retrieval and message disposal until later.
2368 * @param timeout Waiting period for a message to be received (in
2369 * milliseconds), or one of the special values K_NO_WAIT
2370 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002371 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002372 * @retval 0 Message received.
2373 * @retval -ENOMSG Returned without waiting.
2374 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002375 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002376extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002378
2379/**
2380 * @brief Retrieve mailbox message data into a buffer.
2381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * This routine completes the processing of a received message by retrieving
2383 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002384 *
2385 * Alternatively, this routine can be used to dispose of a received message
2386 * without retrieving its data.
2387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002388 * @param rx_msg Address of the receive message descriptor.
2389 * @param buffer Address of the buffer to receive data, or NULL to discard
2390 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002391 *
2392 * @return N/A
2393 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002394extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002395
2396/**
2397 * @brief Retrieve mailbox message data into a memory pool block.
2398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 * This routine completes the processing of a received message by retrieving
2400 * its data into a memory pool block, then disposing of the message.
2401 * The memory pool block that results from successful retrieval must be
2402 * returned to the pool once the data has been processed, even in cases
2403 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002404 *
2405 * Alternatively, this routine can be used to dispose of a received message
2406 * without retrieving its data. In this case there is no need to return a
2407 * memory pool block to the pool.
2408 *
2409 * This routine allocates a new memory pool block for the data only if the
2410 * data is not already in one. If a new block cannot be allocated, the routine
2411 * returns a failure code and the received message is left unchanged. This
2412 * permits the caller to reattempt data retrieval at a later time or to dispose
2413 * of the received message without retrieving its data.
2414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @param rx_msg Address of a receive message descriptor.
2416 * @param pool Address of memory pool, or NULL to discard data.
2417 * @param block Address of the area to hold memory pool block info.
2418 * @param timeout Waiting period to wait for a memory pool block (in
2419 * milliseconds), or one of the special values K_NO_WAIT
2420 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002421 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002422 * @retval 0 Data retrieved.
2423 * @retval -ENOMEM Returned without waiting.
2424 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002425 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002426extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002427 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 struct k_mem_block *block, int32_t timeout);
2429
Allan Stephensc98da842016-11-11 15:45:03 -05002430/**
2431 * @} end defgroup mailbox_apis
2432 */
2433
2434/**
2435 * @cond INTERNAL_HIDDEN
2436 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437
2438struct k_pipe {
2439 unsigned char *buffer; /* Pipe buffer: may be NULL */
2440 size_t size; /* Buffer size */
2441 size_t bytes_used; /* # bytes used in buffer */
2442 size_t read_index; /* Where in buffer to read from */
2443 size_t write_index; /* Where in buffer to write */
2444
2445 struct {
2446 _wait_q_t readers; /* Reader wait queue */
2447 _wait_q_t writers; /* Writer wait queue */
2448 } wait_q;
2449
Anas Nashif2f203c22016-12-18 06:57:45 -05002450 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451};
2452
Peter Mitsise5d9c582016-10-14 14:44:57 -04002453#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454 { \
2455 .buffer = pipe_buffer, \
2456 .size = pipe_buffer_size, \
2457 .bytes_used = 0, \
2458 .read_index = 0, \
2459 .write_index = 0, \
2460 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2461 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002462 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002463 }
2464
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465/**
Allan Stephensc98da842016-11-11 15:45:03 -05002466 * INTERNAL_HIDDEN @endcond
2467 */
2468
2469/**
2470 * @defgroup pipe_apis Pipe APIs
2471 * @ingroup kernel_apis
2472 * @{
2473 */
2474
2475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002480 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @param name Name of the pipe.
2483 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2484 * or zero if no ring buffer is used.
2485 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002487#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2488 static unsigned char __noinit __aligned(pipe_align) \
2489 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002490 struct k_pipe name \
2491 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002492 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * This routine initializes a pipe object, prior to its first use.
2498 *
2499 * @param pipe Address of the pipe.
2500 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2501 * is used.
2502 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2503 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504 *
2505 * @return N/A
2506 */
2507extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2508 size_t size);
2509
2510/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * @param pipe Address of the pipe.
2516 * @param data Address of data to write.
2517 * @param bytes_to_write Size of data (in bytes).
2518 * @param bytes_written Address of area to hold the number of bytes written.
2519 * @param min_xfer Minimum number of bytes to write.
2520 * @param timeout Waiting period to wait for the data to be written (in
2521 * milliseconds), or one of the special values K_NO_WAIT
2522 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002524 * @retval 0 At least @a min_xfer bytes of data were written.
2525 * @retval -EIO Returned without waiting; zero data bytes were written.
2526 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002529extern int k_pipe_put(struct k_pipe *pipe, void *data,
2530 size_t bytes_to_write, size_t *bytes_written,
2531 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532
2533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * @param pipe Address of the pipe.
2539 * @param data Address to place the data read from pipe.
2540 * @param bytes_to_read Maximum number of data bytes to read.
2541 * @param bytes_read Address of area to hold the number of bytes read.
2542 * @param min_xfer Minimum number of data bytes to read.
2543 * @param timeout Waiting period to wait for the data to be read (in
2544 * milliseconds), or one of the special values K_NO_WAIT
2545 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002547 * @retval 0 At least @a min_xfer bytes of data were read.
2548 * @retval -EIO Returned without waiting; zero data bytes were read.
2549 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002552extern int k_pipe_get(struct k_pipe *pipe, void *data,
2553 size_t bytes_to_read, size_t *bytes_read,
2554 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555
2556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * This routine writes the data contained in a memory block to @a pipe.
2560 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564 * @param block Memory block containing data to send
2565 * @param size Number of data bytes in memory block to send
2566 * @param sem Semaphore to signal upon completion (else NULL)
2567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569 */
2570extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2571 size_t size, struct k_sem *sem);
2572
2573/**
Allan Stephensc98da842016-11-11 15:45:03 -05002574 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575 */
2576
Allan Stephensc98da842016-11-11 15:45:03 -05002577/**
2578 * @cond INTERNAL_HIDDEN
2579 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002581struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002583 uint32_t num_blocks;
2584 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585 char *buffer;
2586 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002587 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002588
Anas Nashif2f203c22016-12-18 06:57:45 -05002589 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590};
2591
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002592#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2593 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594 { \
2595 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002596 .num_blocks = slab_num_blocks, \
2597 .block_size = slab_block_size, \
2598 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599 .free_list = NULL, \
2600 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002601 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602 }
2603
Peter Mitsis578f9112016-10-07 13:50:31 -04002604/**
Allan Stephensc98da842016-11-11 15:45:03 -05002605 * INTERNAL_HIDDEN @endcond
2606 */
2607
2608/**
2609 * @defgroup mem_slab_apis Memory Slab APIs
2610 * @ingroup kernel_apis
2611 * @{
2612 */
2613
2614/**
Allan Stephensda827222016-11-09 14:23:58 -06002615 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002616 *
Allan Stephensda827222016-11-09 14:23:58 -06002617 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002619 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2620 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002621 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002622 *
Allan Stephensda827222016-11-09 14:23:58 -06002623 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002625 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002626 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * @param name Name of the memory slab.
2629 * @param slab_block_size Size of each memory block (in bytes).
2630 * @param slab_num_blocks Number memory blocks.
2631 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002632 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002633#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2634 char __noinit __aligned(slab_align) \
2635 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2636 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002637 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002638 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2639 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002641/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002642 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002645 *
Allan Stephensda827222016-11-09 14:23:58 -06002646 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2647 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2648 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2649 * To ensure that each memory block is similarly aligned to this boundary,
2650 * @a slab_block_size must also be a multiple of N.
2651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * @param slab Address of the memory slab.
2653 * @param buffer Pointer to buffer used for the memory blocks.
2654 * @param block_size Size of each memory block (in bytes).
2655 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002656 *
2657 * @return N/A
2658 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002659extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002660 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002661
2662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @param slab Address of the memory slab.
2668 * @param mem Pointer to block address area.
2669 * @param timeout Maximum time to wait for operation to complete
2670 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2671 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002672 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002673 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002675 * @retval -ENOMEM Returned without waiting.
2676 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002677 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002678extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2679 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002680
2681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * This routine releases a previously allocated memory block back to its
2685 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * @param slab Address of the memory slab.
2688 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002689 *
2690 * @return N/A
2691 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002692extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002694/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * This routine gets the number of memory blocks that are currently
2698 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002703 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002704static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002706 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707}
2708
Peter Mitsisc001aa82016-10-13 13:53:37 -04002709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002710 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002712 * This routine gets the number of memory blocks that are currently
2713 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002715 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002718 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002719static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002720{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002721 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002722}
2723
Allan Stephensc98da842016-11-11 15:45:03 -05002724/**
2725 * @} end defgroup mem_slab_apis
2726 */
2727
2728/**
2729 * @cond INTERNAL_HIDDEN
2730 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002732/*
2733 * Memory pool requires a buffer and two arrays of structures for the
2734 * memory block accounting:
2735 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2736 * status of four blocks of memory.
2737 */
2738struct k_mem_pool_quad_block {
2739 char *mem_blocks; /* pointer to the first of four memory blocks */
2740 uint32_t mem_status; /* four bits. If bit is set, memory block is
2741 allocated */
2742};
2743/*
2744 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2745 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2746 * block size is 4 times less than the previous one and thus requires 4 times
2747 * bigger array of k_mem_pool_quad_block structures to keep track of the
2748 * memory blocks.
2749 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002751/*
2752 * The array of k_mem_pool_block_set keeps the information of each array of
2753 * k_mem_pool_quad_block structures
2754 */
2755struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002756 size_t block_size; /* memory block size */
2757 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002758 struct k_mem_pool_quad_block *quad_block;
2759 int count;
2760};
2761
2762/* Memory pool descriptor */
2763struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002764 size_t max_block_size;
2765 size_t min_block_size;
2766 uint32_t nr_of_maxblocks;
2767 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002768 struct k_mem_pool_block_set *block_set;
2769 char *bufblock;
2770 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002771 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772};
2773
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002774#ifdef CONFIG_ARM
2775#define _SECTION_TYPE_SIGN "%"
2776#else
2777#define _SECTION_TYPE_SIGN "@"
2778#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002780/*
2781 * Static memory pool initialization
2782 */
Allan Stephensc98da842016-11-11 15:45:03 -05002783
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002784/*
2785 * Use .altmacro to be able to recalculate values and pass them as string
2786 * arguments when calling assembler macros resursively
2787 */
2788__asm__(".altmacro\n\t");
2789
2790/*
2791 * Recursively calls a macro
2792 * The followig global symbols need to be initialized:
2793 * __memory_pool_max_block_size - maximal size of the memory block
2794 * __memory_pool_min_block_size - minimal size of the memory block
2795 * Notes:
2796 * Global symbols are used due the fact that assembler macro allows only
2797 * one argument be passed with the % conversion
2798 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2799 * is used instead of / 4.
2800 * n_max argument needs to go first in the invoked macro, as some
2801 * assemblers concatenate \name and %(\n_max * 4) arguments
2802 * if \name goes first
2803 */
2804__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2805 ".ifge __memory_pool_max_block_size >> 2 -"
2806 " __memory_pool_min_block_size\n\t\t"
2807 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2808 "\\macro_name %(\\n_max * 4) \\name\n\t"
2809 ".endif\n\t"
2810 ".endm\n");
2811
2812/*
2813 * Build quad blocks
2814 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2815 * structures and recursively calls itself for the next array, 4 times
2816 * larger.
2817 * The followig global symbols need to be initialized:
2818 * __memory_pool_max_block_size - maximal size of the memory block
2819 * __memory_pool_min_block_size - minimal size of the memory block
2820 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2821 */
2822__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002823 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002824 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2825 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2826 ".if \\n_max % 4\n\t\t"
2827 ".skip __memory_pool_quad_block_size\n\t"
2828 ".endif\n\t"
2829 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2830 ".endm\n");
2831
2832/*
2833 * Build block sets and initialize them
2834 * Macro initializes the k_mem_pool_block_set structure and
2835 * recursively calls itself for the next one.
2836 * The followig global symbols need to be initialized:
2837 * __memory_pool_max_block_size - maximal size of the memory block
2838 * __memory_pool_min_block_size - minimal size of the memory block
2839 * __memory_pool_block_set_count, the number of the elements in the
2840 * block set array must be set to 0. Macro calculates it's real
2841 * value.
2842 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2843 * structures, _build_quad_blocks must be called prior it.
2844 */
2845__asm__(".macro _build_block_set n_max, name\n\t"
2846 ".int __memory_pool_max_block_size\n\t" /* block_size */
2847 ".if \\n_max % 4\n\t\t"
2848 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2849 ".else\n\t\t"
2850 ".int \\n_max >> 2\n\t"
2851 ".endif\n\t"
2852 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2853 ".int 0\n\t" /* count */
2854 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2855 "__do_recurse _build_block_set \\name \\n_max\n\t"
2856 ".endm\n");
2857
2858/*
2859 * Build a memory pool structure and initialize it
2860 * Macro uses __memory_pool_block_set_count global symbol,
2861 * block set addresses and buffer address, it may be called only after
2862 * _build_block_set
2863 */
2864__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002865 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002866 _SECTION_TYPE_SIGN "progbits\n\t"
2867 ".globl \\name\n\t"
2868 "\\name:\n\t"
2869 ".int \\max_size\n\t" /* max_block_size */
2870 ".int \\min_size\n\t" /* min_block_size */
2871 ".int \\n_max\n\t" /* nr_of_maxblocks */
2872 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2873 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2874 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2875 ".int 0\n\t" /* wait_q->head */
2876 ".int 0\n\t" /* wait_q->next */
2877 ".popsection\n\t"
2878 ".endm\n");
2879
2880#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2881 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2882 _SECTION_TYPE_SIGN "progbits\n\t"); \
2883 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2884 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2885 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2886 STRINGIFY(name) "\n\t"); \
2887 __asm__(".popsection\n\t")
2888
2889#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2890 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2891 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2892 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2893 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002894 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002895 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2896 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2897 STRINGIFY(name) "\n\t"); \
2898 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2899 __asm__(".int __memory_pool_block_set_count\n\t"); \
2900 __asm__(".popsection\n\t"); \
2901 extern uint32_t _mem_pool_block_set_count_##name; \
2902 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2903
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002904#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2905 char __noinit __aligned(align) \
2906 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002907
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002908/*
2909 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2910 * to __memory_pool_quad_block_size absolute symbol.
2911 * This function does not get called, but compiler calculates the value and
2912 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2913 */
2914static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2915{
2916 __asm__(".globl __memory_pool_quad_block_size\n\t"
2917#ifdef CONFIG_NIOS2
2918 "__memory_pool_quad_block_size = %0\n\t"
2919#else
2920 "__memory_pool_quad_block_size = %c0\n\t"
2921#endif
2922 :
2923 : "n"(sizeof(struct k_mem_pool_quad_block)));
2924}
2925
2926/**
Allan Stephensc98da842016-11-11 15:45:03 -05002927 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002928 */
2929
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002930/**
Allan Stephensc98da842016-11-11 15:45:03 -05002931 * @addtogroup mem_pool_apis
2932 * @{
2933 */
2934
2935/**
2936 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2939 * long. The memory pool allows blocks to be repeatedly partitioned into
2940 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2941 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002942 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002944 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002945 * If the pool is to be accessed outside the module where it is defined, it
2946 * can be declared via
2947 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002948 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @param name Name of the memory pool.
2951 * @param min_size Size of the smallest blocks in the pool (in bytes).
2952 * @param max_size Size of the largest blocks in the pool (in bytes).
2953 * @param n_max Number of maximum sized blocks in the pool.
2954 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002955 */
2956#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002957 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2958 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002959 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002960 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2961 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2962 extern struct k_mem_pool name
2963
Peter Mitsis937042c2016-10-13 13:18:26 -04002964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * @param pool Address of the memory pool.
2970 * @param block Pointer to block descriptor for the allocated memory.
2971 * @param size Amount of memory to allocate (in bytes).
2972 * @param timeout Maximum time to wait for operation to complete
2973 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2974 * or K_FOREVER to wait as long as necessary.
2975 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002976 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002978 * @retval -ENOMEM Returned without waiting.
2979 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002980 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002981extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002982 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002983
2984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * This routine releases a previously allocated memory block back to its
2988 * memory pool.
2989 *
2990 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002991 *
2992 * @return N/A
2993 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002994extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002995
2996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * This routine instructs a memory pool to concatenate unused memory blocks
3000 * into larger blocks wherever possible. Manually defragmenting the memory
3001 * pool may speed up future allocations of memory blocks by eliminating the
3002 * need for the memory pool to perform an automatic partial defragmentation.
3003 *
3004 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003005 *
3006 * @return N/A
3007 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003008extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003009
3010/**
Allan Stephensc98da842016-11-11 15:45:03 -05003011 * @} end addtogroup mem_pool_apis
3012 */
3013
3014/**
3015 * @defgroup heap_apis Heap Memory Pool APIs
3016 * @ingroup kernel_apis
3017 * @{
3018 */
3019
3020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003024 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003029 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003030extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003031
3032/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003034 *
3035 * This routine provides traditional free() semantics. The memory being
3036 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003037 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003038 * If @a ptr is NULL, no operation is performed.
3039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003040 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003041 *
3042 * @return N/A
3043 */
3044extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045
Allan Stephensc98da842016-11-11 15:45:03 -05003046/**
3047 * @} end defgroup heap_apis
3048 */
3049
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003050/**
3051 * @brief Make the CPU idle.
3052 *
3053 * This function makes the CPU idle until an event wakes it up.
3054 *
3055 * In a regular system, the idle thread should be the only thread responsible
3056 * for making the CPU idle and triggering any type of power management.
3057 * However, in some more constrained systems, such as a single-threaded system,
3058 * the only thread would be responsible for this if needed.
3059 *
3060 * @return N/A
3061 */
3062extern void k_cpu_idle(void);
3063
3064/**
3065 * @brief Make the CPU idle in an atomic fashion.
3066 *
3067 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3068 * must be done atomically before making the CPU idle.
3069 *
3070 * @param key Interrupt locking key obtained from irq_lock().
3071 *
3072 * @return N/A
3073 */
3074extern void k_cpu_atomic_idle(unsigned int key);
3075
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003076/*
3077 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
3078 * hook into the device subsystem, which itself uses nanokernel semaphores,
3079 * and thus currently requires the definition of nano_sem.
3080 */
3081#include <legacy.h>
3082#include <arch/cpu.h>
3083
3084/*
3085 * private APIs that are utilized by one or more public APIs
3086 */
3087
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003088#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003090#else
3091#define _init_static_threads() do { } while ((0))
3092#endif
3093
3094extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003095extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096
3097#ifdef __cplusplus
3098}
3099#endif
3100
Andrew Boiee004dec2016-11-07 09:01:19 -08003101#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3102/*
3103 * Define new and delete operators.
3104 * At this moment, the operators do nothing since objects are supposed
3105 * to be statically allocated.
3106 */
3107inline void operator delete(void *ptr)
3108{
3109 (void)ptr;
3110}
3111
3112inline void operator delete[](void *ptr)
3113{
3114 (void)ptr;
3115}
3116
3117inline void *operator new(size_t size)
3118{
3119 (void)size;
3120 return NULL;
3121}
3122
3123inline void *operator new[](size_t size)
3124{
3125 (void)size;
3126 return NULL;
3127}
3128
3129/* Placement versions of operator new and delete */
3130inline void operator delete(void *ptr1, void *ptr2)
3131{
3132 (void)ptr1;
3133 (void)ptr2;
3134}
3135
3136inline void operator delete[](void *ptr1, void *ptr2)
3137{
3138 (void)ptr1;
3139 (void)ptr2;
3140}
3141
3142inline void *operator new(size_t size, void *ptr)
3143{
3144 (void)size;
3145 return ptr;
3146}
3147
3148inline void *operator new[](size_t size, void *ptr)
3149{
3150 (void)size;
3151 return ptr;
3152}
3153
3154#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3155
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156#endif /* _kernel__h_ */