blob: 3b4a29e6201532a7b5bb6e770c55c7820f938e33 [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>
Anas Nashif173902f2017-01-17 07:08:56 -050028#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040029#include <toolchain.h>
30#include <sections.h>
31#include <atomic.h>
32#include <errno.h>
33#include <misc/__assert.h>
34#include <misc/dlist.h>
35#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050036#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050037#include <kernel_version.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Anas Nashifbbb157d2017-01-15 08:46:31 -050043/**
44 * @brief Kernel APIs
45 * @defgroup kernel_apis Kernel APIs
46 * @{
47 * @}
48 */
49
Anas Nashif61f4b242016-11-18 10:53:59 -050050#ifdef CONFIG_KERNEL_DEBUG
51#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
53#else
54#define K_DEBUG(fmt, ...)
55#endif
56
Benjamin Walsh2f280412017-01-14 19:23:46 -050057#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#elif defined(CONFIG_COOP_ENABLED)
61#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
62#define _NUM_PREEMPT_PRIO (0)
63#elif defined(CONFIG_PREEMPT_ENABLED)
64#define _NUM_COOP_PRIO (0)
65#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
66#else
67#error "invalid configuration"
68#endif
69
70#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_PRIO_PREEMPT(x) (x)
72
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_ANY NULL
74#define K_END NULL
75
Benjamin Walshedb35702017-01-14 18:47:22 -050076#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050078#elif defined(CONFIG_COOP_ENABLED)
79#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
80#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050082#else
83#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084#endif
85
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050086#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
88#else
89#define K_LOWEST_THREAD_PRIO -1
90#endif
91
Benjamin Walshfab8d922016-11-08 15:36:36 -050092#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
93
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
95#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
96
97typedef sys_dlist_t _wait_q_t;
98
Anas Nashif2f203c22016-12-18 06:57:45 -050099#ifdef CONFIG_OBJECT_TRACING
100#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
101#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400102#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500103#define _OBJECT_TRACING_INIT
104#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#endif
106
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500107#define tcs k_thread
108struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109struct k_mutex;
110struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400111struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112struct k_msgq;
113struct k_mbox;
114struct k_pipe;
115struct k_fifo;
116struct k_lifo;
117struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400118struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_mem_pool;
120struct k_timer;
121
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400122typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124enum execution_context_types {
125 K_ISR = 0,
126 K_COOP_THREAD,
127 K_PREEMPT_THREAD,
128};
129
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400130/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100131 * @defgroup profiling_apis Profiling APIs
132 * @ingroup kernel_apis
133 * @{
134 */
135
136/**
137 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
138 *
139 * This routine calls @ref stack_analyze on the 4 call stacks declared and
140 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
141 *
142 * CONFIG_MAIN_STACK_SIZE
143 * CONFIG_IDLE_STACK_SIZE
144 * CONFIG_ISR_STACK_SIZE
145 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
146 *
147 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
148 * produce output.
149 *
150 * @return N/A
151 */
152extern void k_call_stacks_analyze(void);
153
154/**
155 * @} end defgroup profiling_apis
156 */
157
158/**
Allan Stephensc98da842016-11-11 15:45:03 -0500159 * @defgroup thread_apis Thread APIs
160 * @ingroup kernel_apis
161 * @{
162 */
163
164/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500165 * @typedef k_thread_entry_t
166 * @brief Thread entry point function type.
167 *
168 * A thread's entry point function is invoked when the thread starts executing.
169 * Up to 3 argument values can be passed to the function.
170 *
171 * The thread terminates execution permanently if the entry point function
172 * returns. The thread is responsible for releasing any shared resources
173 * it may own (such as mutexes and dynamically allocated memory), prior to
174 * returning.
175 *
176 * @param p1 First argument.
177 * @param p2 Second argument.
178 * @param p3 Third argument.
179 *
180 * @return N/A
181 */
182typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
183
184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500185 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500187 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500189 * The new thread may be scheduled for immediate execution or a delayed start.
190 * If the newly spawned thread does not have a delayed start the kernel
191 * scheduler may preempt the current thread to allow the new thread to
192 * execute.
193 *
194 * Thread options are architecture-specific, and can include K_ESSENTIAL,
195 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
196 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
198 * @param stack Pointer to the stack space.
199 * @param stack_size Stack size in bytes.
200 * @param entry Thread entry function.
201 * @param p1 1st entry point parameter.
202 * @param p2 2nd entry point parameter.
203 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500204 * @param prio Thread priority.
205 * @param options Thread options.
206 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500208 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500210extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500211 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500213 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400214
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500216 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400217 *
Allan Stephensc98da842016-11-11 15:45:03 -0500218 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500221 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400222 *
223 * @return N/A
224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400225extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400226
227/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500228 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400229 *
230 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500231 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400232 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400233 * @return N/A
234 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400235extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400236
237/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500238 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500240 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400241 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500242 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400243 *
244 * @return N/A
245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400247
248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500251 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500253 * If @a thread is not currently sleeping, the routine has no effect.
254 *
255 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400256 *
257 * @return N/A
258 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400260
261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500262 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500264 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400266extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267
268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500269 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500271 * This routine prevents @a thread from executing if it has not yet started
272 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500274 * @param thread ID of thread to cancel.
275 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500276 * @retval 0 Thread spawning cancelled.
277 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400278 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400279extern int k_thread_cancel(k_tid_t thread);
280
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400281/**
Allan Stephensc98da842016-11-11 15:45:03 -0500282 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500284 * This routine permanently stops execution of @a thread. The thread is taken
285 * off all kernel queues it is part of (i.e. the ready queue, the timeout
286 * queue, or a kernel object wait queue). However, any kernel resources the
287 * thread might currently own (such as mutexes or memory blocks) are not
288 * released. It is the responsibility of the caller of this routine to ensure
289 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500291 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400292 *
293 * @return N/A
294 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400295extern void k_thread_abort(k_tid_t thread);
296
Allan Stephensc98da842016-11-11 15:45:03 -0500297/**
298 * @cond INTERNAL_HIDDEN
299 */
300
Benjamin Walshd211a522016-12-06 11:44:01 -0500301/* timeout has timed out and is not on _timeout_q anymore */
302#define _EXPIRED (-2)
303
304/* timeout is not in use */
305#define _INACTIVE (-1)
306
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400307#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400308#define _THREAD_TIMEOUT_INIT(obj) \
309 (obj).nano_timeout = { \
310 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400311 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400312 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500313 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400314 },
315#else
316#define _THREAD_TIMEOUT_INIT(obj)
317#endif
318
319#ifdef CONFIG_ERRNO
320#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
321#else
322#define _THREAD_ERRNO_INIT(obj)
323#endif
324
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400325struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400326 union {
327 char *init_stack;
328 struct k_thread *thread;
329 };
330 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500331 void (*init_entry)(void *, void *, void *);
332 void *init_p1;
333 void *init_p2;
334 void *init_p3;
335 int init_prio;
336 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400337 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500338 void (*init_abort)(void);
339 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400340};
341
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400342#define _THREAD_INITIALIZER(stack, stack_size, \
343 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500344 prio, options, delay, abort, groups) \
345 { \
346 .init_stack = (stack), \
347 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400348 .init_entry = (void (*)(void *, void *, void *))entry, \
349 .init_p1 = (void *)p1, \
350 .init_p2 = (void *)p2, \
351 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500352 .init_prio = (prio), \
353 .init_options = (options), \
354 .init_delay = (delay), \
355 .init_abort = (abort), \
356 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400357 }
358
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400359/**
Allan Stephensc98da842016-11-11 15:45:03 -0500360 * INTERNAL_HIDDEN @endcond
361 */
362
363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500364 * @brief Statically define and initialize a thread.
365 *
366 * The thread may be scheduled for immediate execution or a delayed start.
367 *
368 * Thread options are architecture-specific, and can include K_ESSENTIAL,
369 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
370 * them using "|" (the logical OR operator).
371 *
372 * The ID of the thread can be accessed using:
373 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500374 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500375 *
376 * @param name Name of the thread.
377 * @param stack_size Stack size in bytes.
378 * @param entry Thread entry function.
379 * @param p1 1st entry point parameter.
380 * @param p2 2nd entry point parameter.
381 * @param p3 3rd entry point parameter.
382 * @param prio Thread priority.
383 * @param options Thread options.
384 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400385 *
386 * @internal It has been observed that the x86 compiler by default aligns
387 * these _static_thread_data structures to 32-byte boundaries, thereby
388 * wasting space. To work around this, force a 4-byte alignment.
389 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500390#define K_THREAD_DEFINE(name, stack_size, \
391 entry, p1, p2, p3, \
392 prio, options, delay) \
393 char __noinit __stack _k_thread_obj_##name[stack_size]; \
394 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500395 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500396 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
397 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500398 NULL, 0); \
399 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400400
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500404 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * @param thread ID of thread whose priority is needed.
407 *
408 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400409 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500410extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400411
412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500413 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500415 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400416 *
417 * Rescheduling can occur immediately depending on the priority @a thread is
418 * set to:
419 *
420 * - If its priority is raised above the priority of the caller of this
421 * function, and the caller is preemptible, @a thread will be scheduled in.
422 *
423 * - If the caller operates on itself, it lowers its priority below that of
424 * other threads in the system, and the caller is preemptible, the thread of
425 * highest priority will be scheduled in.
426 *
427 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
428 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
429 * highest priority.
430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432 * @param prio New priority.
433 *
434 * @warning Changing the priority of a thread currently involved in mutex
435 * priority inheritance may result in undefined behavior.
436 *
437 * @return N/A
438 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400439extern void k_thread_priority_set(k_tid_t thread, int prio);
440
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500442 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500444 * This routine prevents the kernel scheduler from making @a thread the
445 * current thread. All other internal operations on @a thread are still
446 * performed; for example, any timeout it is waiting on keeps ticking,
447 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500449 * If @a thread is already suspended, the routine has no effect.
450 *
451 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400452 *
453 * @return N/A
454 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400455extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456
457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500460 * This routine allows the kernel scheduler to make @a thread the current
461 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * If @a thread is not currently suspended, the routine has no effect.
464 *
465 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400466 *
467 * @return N/A
468 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400469extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400470
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500472 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * This routine specifies how the scheduler will perform time slicing of
475 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500477 * To enable time slicing, @a slice must be non-zero. The scheduler
478 * ensures that no thread runs for more than the specified time limit
479 * before other threads of that priority are given a chance to execute.
480 * Any thread whose priority is higher than @a prio is exempted, and may
481 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500483 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400484 * execute. Once the scheduler selects a thread for execution, there is no
485 * minimum guaranteed time the thread will execute before threads of greater or
486 * equal priority are scheduled.
487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500488 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400489 * for execution, this routine has no effect; the thread is immediately
490 * rescheduled after the slice period expires.
491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500492 * To disable timeslicing, set both @a slice and @a prio to zero.
493 *
494 * @param slice Maximum time slice length (in milliseconds).
495 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400496 *
497 * @return N/A
498 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400499extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400500
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400501/**
Allan Stephensc98da842016-11-11 15:45:03 -0500502 * @} end defgroup thread_apis
503 */
504
505/**
506 * @addtogroup isr_apis
507 * @{
508 */
509
510/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500511 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400512 *
Allan Stephensc98da842016-11-11 15:45:03 -0500513 * This routine allows the caller to customize its actions, depending on
514 * whether it is a thread or an ISR.
515 *
516 * @note Can be called by ISRs.
517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500518 * @return 0 if invoked by a thread.
519 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500521extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400522
Benjamin Walsh445830d2016-11-10 15:54:27 -0500523/**
524 * @brief Determine if code is running in a preemptible thread.
525 *
Allan Stephensc98da842016-11-11 15:45:03 -0500526 * This routine allows the caller to customize its actions, depending on
527 * whether it can be preempted by another thread. The routine returns a 'true'
528 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500529 *
Allan Stephensc98da842016-11-11 15:45:03 -0500530 * - The code is running in a thread, not at ISR.
531 * - The thread's priority is in the preemptible range.
532 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500533 *
Allan Stephensc98da842016-11-11 15:45:03 -0500534 * @note Can be called by ISRs.
535 *
536 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500537 * @return Non-zero if invoked by a preemptible thread.
538 */
539extern int k_is_preempt_thread(void);
540
Allan Stephensc98da842016-11-11 15:45:03 -0500541/**
542 * @} end addtogroup isr_apis
543 */
544
545/**
546 * @addtogroup thread_apis
547 * @{
548 */
549
550/**
551 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500552 *
Allan Stephensc98da842016-11-11 15:45:03 -0500553 * This routine prevents the current thread from being preempted by another
554 * thread by instructing the scheduler to treat it as a cooperative thread.
555 * If the thread subsequently performs an operation that makes it unready,
556 * it will be context switched out in the normal manner. When the thread
557 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500558 *
Allan Stephensc98da842016-11-11 15:45:03 -0500559 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500560 *
Allan Stephensc98da842016-11-11 15:45:03 -0500561 * @note k_sched_lock() and k_sched_unlock() should normally be used
562 * when the operation being performed can be safely interrupted by ISRs.
563 * However, if the amount of processing involved is very small, better
564 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500565 *
566 * @return N/A
567 */
568extern void k_sched_lock(void);
569
Allan Stephensc98da842016-11-11 15:45:03 -0500570/**
571 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500572 *
Allan Stephensc98da842016-11-11 15:45:03 -0500573 * This routine reverses the effect of a previous call to k_sched_lock().
574 * A thread must call the routine once for each time it called k_sched_lock()
575 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500576 *
577 * @return N/A
578 */
579extern void k_sched_unlock(void);
580
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500586 * Custom data is not used by the kernel itself, and is freely available
587 * for a thread to use as it sees fit. It can be used as a framework
588 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500590 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591 *
592 * @return N/A
593 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400595
596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500599 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500601 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400603extern void *k_thread_custom_data_get(void);
604
605/**
Allan Stephensc98da842016-11-11 15:45:03 -0500606 * @} end addtogroup thread_apis
607 */
608
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400609#include <sys_clock.h>
610
Allan Stephensc2f15a42016-11-17 12:24:22 -0500611/**
612 * @addtogroup clock_apis
613 * @{
614 */
615
616/**
617 * @brief Generate null timeout delay.
618 *
619 * This macro generates a timeout delay that that instructs a kernel API
620 * not to wait if the requested operation cannot be performed immediately.
621 *
622 * @return Timeout delay value.
623 */
624#define K_NO_WAIT 0
625
626/**
627 * @brief Generate timeout delay from milliseconds.
628 *
629 * This macro generates a timeout delay that that instructs a kernel API
630 * to wait up to @a ms milliseconds to perform the requested operation.
631 *
632 * @param ms Duration in milliseconds.
633 *
634 * @return Timeout delay value.
635 */
Johan Hedberg14471692016-11-13 10:52:15 +0200636#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500637
638/**
639 * @brief Generate timeout delay from seconds.
640 *
641 * This macro generates a timeout delay that that instructs a kernel API
642 * to wait up to @a s seconds to perform the requested operation.
643 *
644 * @param s Duration in seconds.
645 *
646 * @return Timeout delay value.
647 */
Johan Hedberg14471692016-11-13 10:52:15 +0200648#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500649
650/**
651 * @brief Generate timeout delay from minutes.
652 *
653 * This macro generates a timeout delay that that instructs a kernel API
654 * to wait up to @a m minutes to perform the requested operation.
655 *
656 * @param m Duration in minutes.
657 *
658 * @return Timeout delay value.
659 */
Johan Hedberg14471692016-11-13 10:52:15 +0200660#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500661
662/**
663 * @brief Generate timeout delay from hours.
664 *
665 * This macro generates a timeout delay that that instructs a kernel API
666 * to wait up to @a h hours to perform the requested operation.
667 *
668 * @param h Duration in hours.
669 *
670 * @return Timeout delay value.
671 */
Johan Hedberg14471692016-11-13 10:52:15 +0200672#define K_HOURS(h) K_MINUTES((h) * 60)
673
Allan Stephensc98da842016-11-11 15:45:03 -0500674/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500675 * @brief Generate infinite timeout delay.
676 *
677 * This macro generates a timeout delay that that instructs a kernel API
678 * to wait as long as necessary to perform the requested operation.
679 *
680 * @return Timeout delay value.
681 */
682#define K_FOREVER (-1)
683
684/**
685 * @} end addtogroup clock_apis
686 */
687
688/**
Allan Stephensc98da842016-11-11 15:45:03 -0500689 * @cond INTERNAL_HIDDEN
690 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400691
Benjamin Walsh62092182016-12-20 14:39:08 -0500692/* kernel clocks */
693
694#if (sys_clock_ticks_per_sec == 1000) || \
695 (sys_clock_ticks_per_sec == 500) || \
696 (sys_clock_ticks_per_sec == 250) || \
697 (sys_clock_ticks_per_sec == 125) || \
698 (sys_clock_ticks_per_sec == 100) || \
699 (sys_clock_ticks_per_sec == 50) || \
700 (sys_clock_ticks_per_sec == 25) || \
701 (sys_clock_ticks_per_sec == 20) || \
702 (sys_clock_ticks_per_sec == 10) || \
703 (sys_clock_ticks_per_sec == 1)
704
705 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
706#else
707 /* yields horrible 64-bit math on many architectures: try to avoid */
708 #define _NON_OPTIMIZED_TICKS_PER_SEC
709#endif
710
711#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
712extern int32_t _ms_to_ticks(int32_t ms);
713#else
714static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
715{
716 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
717}
718#endif
719
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500720/* added tick needed to account for tick in progress */
721#define _TICK_ALIGN 1
722
Benjamin Walsh62092182016-12-20 14:39:08 -0500723static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400724{
Benjamin Walsh62092182016-12-20 14:39:08 -0500725#ifdef CONFIG_SYS_CLOCK_EXISTS
726
727#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400728 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400729#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500730 return (uint64_t)ticks * _ms_per_tick;
731#endif
732
733#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400734 __ASSERT(ticks == 0, "");
735 return 0;
736#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400737}
738
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400739/* timeouts */
740
741struct _timeout;
742typedef void (*_timeout_func_t)(struct _timeout *t);
743
744struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500745 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400746 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400747 sys_dlist_t *wait_q;
748 int32_t delta_ticks_from_prev;
749 _timeout_func_t func;
750};
751
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200752extern int32_t _timeout_remaining_get(struct _timeout *timeout);
753
Allan Stephensc98da842016-11-11 15:45:03 -0500754/**
755 * INTERNAL_HIDDEN @endcond
756 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500757
Allan Stephensc98da842016-11-11 15:45:03 -0500758/**
759 * @cond INTERNAL_HIDDEN
760 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400761
762struct k_timer {
763 /*
764 * _timeout structure must be first here if we want to use
765 * dynamic timer allocation. timeout.node is used in the double-linked
766 * list of free timers
767 */
768 struct _timeout timeout;
769
Allan Stephens45bfa372016-10-12 12:39:42 -0500770 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400771 _wait_q_t wait_q;
772
773 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500774 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775
776 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500777 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778
779 /* timer period */
780 int32_t period;
781
Allan Stephens45bfa372016-10-12 12:39:42 -0500782 /* timer status */
783 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400784
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500785 /* user-specific data, also used to support legacy features */
786 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400787
Anas Nashif2f203c22016-12-18 06:57:45 -0500788 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400789};
790
Allan Stephens1342adb2016-11-03 13:54:53 -0500791#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500793 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500794 .timeout.wait_q = NULL, \
795 .timeout.thread = NULL, \
796 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400797 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500798 .expiry_fn = expiry, \
799 .stop_fn = stop, \
800 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500801 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500802 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400803 }
804
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805/**
Allan Stephensc98da842016-11-11 15:45:03 -0500806 * INTERNAL_HIDDEN @endcond
807 */
808
809/**
810 * @defgroup timer_apis Timer APIs
811 * @ingroup kernel_apis
812 * @{
813 */
814
815/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500816 * @typedef k_timer_expiry_t
817 * @brief Timer expiry function type.
818 *
819 * A timer's expiry function is executed by the system clock interrupt handler
820 * each time the timer expires. The expiry function is optional, and is only
821 * invoked if the timer has been initialized with one.
822 *
823 * @param timer Address of timer.
824 *
825 * @return N/A
826 */
827typedef void (*k_timer_expiry_t)(struct k_timer *timer);
828
829/**
830 * @typedef k_timer_stop_t
831 * @brief Timer stop function type.
832 *
833 * A timer's stop function is executed if the timer is stopped prematurely.
834 * The function runs in the context of the thread that stops the timer.
835 * The stop function is optional, and is only invoked if the timer has been
836 * initialized with one.
837 *
838 * @param timer Address of timer.
839 *
840 * @return N/A
841 */
842typedef void (*k_timer_stop_t)(struct k_timer *timer);
843
844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500849 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * @param expiry_fn Function to invoke each time the timer expires.
853 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500855#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500856 struct k_timer name \
857 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500858 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400859
Allan Stephens45bfa372016-10-12 12:39:42 -0500860/**
861 * @brief Initialize a timer.
862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500864 *
865 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @param expiry_fn Function to invoke each time the timer expires.
867 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500868 *
869 * @return N/A
870 */
871extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500872 k_timer_expiry_t expiry_fn,
873 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700874
Allan Stephens45bfa372016-10-12 12:39:42 -0500875/**
876 * @brief Start a timer.
877 *
878 * This routine starts a timer, and resets its status to zero. The timer
879 * begins counting down using the specified duration and period values.
880 *
881 * Attempting to start a timer that is already running is permitted.
882 * The timer's status is reset to zero and the timer begins counting down
883 * using the new duration and period values.
884 *
885 * @param timer Address of timer.
886 * @param duration Initial timer duration (in milliseconds).
887 * @param period Timer period (in milliseconds).
888 *
889 * @return N/A
890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500892 int32_t duration, int32_t period);
893
894/**
895 * @brief Stop a timer.
896 *
897 * This routine stops a running timer prematurely. The timer's stop function,
898 * if one exists, is invoked by the caller.
899 *
900 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500902 *
903 * @param timer Address of timer.
904 *
905 * @return N/A
906 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500908
909/**
910 * @brief Read timer status.
911 *
912 * This routine reads the timer's status, which indicates the number of times
913 * it has expired since its status was last read.
914 *
915 * Calling this routine resets the timer's status to zero.
916 *
917 * @param timer Address of timer.
918 *
919 * @return Timer status.
920 */
921extern uint32_t k_timer_status_get(struct k_timer *timer);
922
923/**
924 * @brief Synchronize thread to timer expiration.
925 *
926 * This routine blocks the calling thread until the timer's status is non-zero
927 * (indicating that it has expired at least once since it was last examined)
928 * or the timer is stopped. If the timer status is already non-zero,
929 * or the timer is already stopped, the caller continues without waiting.
930 *
931 * Calling this routine resets the timer's status to zero.
932 *
933 * This routine must not be used by interrupt handlers, since they are not
934 * allowed to block.
935 *
936 * @param timer Address of timer.
937 *
938 * @return Timer status.
939 */
940extern uint32_t k_timer_status_sync(struct k_timer *timer);
941
942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500944 *
945 * This routine computes the (approximate) time remaining before a running
946 * timer next expires. If the timer is not running, it returns zero.
947 *
948 * @param timer Address of timer.
949 *
950 * @return Remaining time (in milliseconds).
951 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200952static inline int32_t k_timer_remaining_get(struct k_timer *timer)
953{
954 return _timeout_remaining_get(&timer->timeout);
955}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400956
Allan Stephensc98da842016-11-11 15:45:03 -0500957/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500958 * @brief Associate user-specific data with a timer.
959 *
960 * This routine records the @a user_data with the @a timer, to be retrieved
961 * later.
962 *
963 * It can be used e.g. in a timer handler shared across multiple subsystems to
964 * retrieve data specific to the subsystem this timer is associated with.
965 *
966 * @param timer Address of timer.
967 * @param user_data User data to associate with the timer.
968 *
969 * @return N/A
970 */
971static inline void k_timer_user_data_set(struct k_timer *timer,
972 void *user_data)
973{
974 timer->user_data = user_data;
975}
976
977/**
978 * @brief Retrieve the user-specific data from a timer.
979 *
980 * @param timer Address of timer.
981 *
982 * @return The user data.
983 */
984static inline void *k_timer_user_data_get(struct k_timer *timer)
985{
986 return timer->user_data;
987}
988
989/**
Allan Stephensc98da842016-11-11 15:45:03 -0500990 * @} end defgroup timer_apis
991 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400992
Allan Stephensc98da842016-11-11 15:45:03 -0500993/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500994 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500995 * @{
996 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500997
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * This routine returns the elapsed time since the system booted,
1002 * in milliseconds.
1003 *
1004 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001005 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001006extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001007
1008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * This routine returns the lower 32-bits of the elapsed time since the system
1012 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * This routine can be more efficient than k_uptime_get(), as it reduces the
1015 * need for interrupt locking and 64-bit math. However, the 32-bit result
1016 * cannot hold a system uptime time larger than approximately 50 days, so the
1017 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001020 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001021extern uint32_t k_uptime_get_32(void);
1022
1023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * This routine computes the elapsed time between the current system uptime
1027 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @param reftime Pointer to a reference time, which is updated to the current
1030 * uptime upon return.
1031 *
1032 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001033 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001034extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001035
1036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * This routine computes the elapsed time between the current system uptime
1040 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1043 * need for interrupt locking and 64-bit math. However, the 32-bit result
1044 * cannot hold an elapsed time larger than approximately 50 days, so the
1045 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * @param reftime Pointer to a reference time, which is updated to the current
1048 * uptime upon return.
1049 *
1050 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001051 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001052extern uint32_t k_uptime_delta_32(int64_t *reftime);
1053
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001057 * This routine returns the current time, as measured by the system's hardware
1058 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001062extern uint32_t k_cycle_get_32(void);
1063
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001064/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001065 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066 */
1067
Allan Stephensc98da842016-11-11 15:45:03 -05001068/**
1069 * @cond INTERNAL_HIDDEN
1070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071
1072struct k_fifo {
1073 _wait_q_t wait_q;
1074 sys_slist_t data_q;
1075
Anas Nashif2f203c22016-12-18 06:57:45 -05001076 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077};
1078
Allan Stephensc98da842016-11-11 15:45:03 -05001079#define K_FIFO_INITIALIZER(obj) \
1080 { \
1081 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1082 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001083 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001084 }
1085
1086/**
1087 * INTERNAL_HIDDEN @endcond
1088 */
1089
1090/**
1091 * @defgroup fifo_apis Fifo APIs
1092 * @ingroup kernel_apis
1093 * @{
1094 */
1095
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
1103 * @return N/A
1104 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106
1107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * This routine adds a data item to @a fifo. A fifo data item must be
1111 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1112 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * @note Can be called by ISRs.
1115 *
1116 * @param fifo Address of the fifo.
1117 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
1119 * @return N/A
1120 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122
1123/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001126 * This routine adds a list of data items to @a fifo in one operation.
1127 * The data items must be in a singly-linked list, with the first 32 bits
1128 * each data item pointing to the next data item; the list must be
1129 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * @param fifo Address of the fifo.
1134 * @param head Pointer to first node in singly-linked list.
1135 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
1137 * @return N/A
1138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001139extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140
1141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001142 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * This routine adds a list of data items to @a fifo in one operation.
1145 * The data items must be in a singly-linked list implemented using a
1146 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 * and must be re-initialized via sys_slist_init().
1148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001149 * @note Can be called by ISRs.
1150 *
1151 * @param fifo Address of the fifo.
1152 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 *
1154 * @return N/A
1155 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001156extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157
1158/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001159 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001161 * This routine removes a data item from @a fifo in a "first in, first out"
1162 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1165 *
1166 * @param fifo Address of the fifo.
1167 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 * or one of the special values K_NO_WAIT and K_FOREVER.
1169 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001170 * @return Address of the data item if successful; NULL if returned
1171 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001180 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001183 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001184#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001185 struct k_fifo name \
1186 __in_section(_k_fifo, static, name) = \
1187 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188
Allan Stephensc98da842016-11-11 15:45:03 -05001189/**
1190 * @} end defgroup fifo_apis
1191 */
1192
1193/**
1194 * @cond INTERNAL_HIDDEN
1195 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001196
1197struct k_lifo {
1198 _wait_q_t wait_q;
1199 void *list;
1200
Anas Nashif2f203c22016-12-18 06:57:45 -05001201 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001202};
1203
Allan Stephensc98da842016-11-11 15:45:03 -05001204#define K_LIFO_INITIALIZER(obj) \
1205 { \
1206 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1207 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001208 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001209 }
1210
1211/**
1212 * INTERNAL_HIDDEN @endcond
1213 */
1214
1215/**
1216 * @defgroup lifo_apis Lifo APIs
1217 * @ingroup kernel_apis
1218 * @{
1219 */
1220
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001221/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001222 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
1228 * @return N/A
1229 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001230extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231
1232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * This routine adds a data item to @a lifo. A lifo data item must be
1236 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1237 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * @note Can be called by ISRs.
1240 *
1241 * @param lifo Address of the lifo.
1242 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 *
1244 * @return N/A
1245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247
1248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * This routine removes a data item from @a lifo in a "last in, first out"
1252 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1255 *
1256 * @param lifo Address of the lifo.
1257 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258 * or one of the special values K_NO_WAIT and K_FOREVER.
1259 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001260 * @return Address of the data item if successful; NULL if returned
1261 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001262 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001263extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1264
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001266 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001270 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001274#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001275 struct k_lifo name \
1276 __in_section(_k_lifo, static, name) = \
1277 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278
Allan Stephensc98da842016-11-11 15:45:03 -05001279/**
1280 * @} end defgroup lifo_apis
1281 */
1282
1283/**
1284 * @cond INTERNAL_HIDDEN
1285 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286
1287struct k_stack {
1288 _wait_q_t wait_q;
1289 uint32_t *base, *next, *top;
1290
Anas Nashif2f203c22016-12-18 06:57:45 -05001291 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001292};
1293
Allan Stephensc98da842016-11-11 15:45:03 -05001294#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1295 { \
1296 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1297 .base = stack_buffer, \
1298 .next = stack_buffer, \
1299 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001300 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001301 }
1302
1303/**
1304 * INTERNAL_HIDDEN @endcond
1305 */
1306
1307/**
1308 * @defgroup stack_apis Stack APIs
1309 * @ingroup kernel_apis
1310 * @{
1311 */
1312
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001313/**
1314 * @brief Initialize a stack.
1315 *
1316 * This routine initializes a stack object, prior to its first use.
1317 *
1318 * @param stack Address of the stack.
1319 * @param buffer Address of array used to hold stacked values.
1320 * @param num_entries Maximum number of values that can be stacked.
1321 *
1322 * @return N/A
1323 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001324extern void k_stack_init(struct k_stack *stack,
1325 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326
1327/**
1328 * @brief Push an element onto a stack.
1329 *
1330 * This routine adds a 32-bit value @a data to @a stack.
1331 *
1332 * @note Can be called by ISRs.
1333 *
1334 * @param stack Address of the stack.
1335 * @param data Value to push onto the stack.
1336 *
1337 * @return N/A
1338 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001339extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001340
1341/**
1342 * @brief Pop an element from a stack.
1343 *
1344 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1345 * manner and stores the value in @a data.
1346 *
1347 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1348 *
1349 * @param stack Address of the stack.
1350 * @param data Address of area to hold the value popped from the stack.
1351 * @param timeout Waiting period to obtain a value (in milliseconds),
1352 * or one of the special values K_NO_WAIT and K_FOREVER.
1353 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001354 * @retval 0 Element popped from stack.
1355 * @retval -EBUSY Returned without waiting.
1356 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001358extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1359
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001365 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001367 * @param name Name of the stack.
1368 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001369 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001370#define K_STACK_DEFINE(name, stack_num_entries) \
1371 uint32_t __noinit \
1372 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001373 struct k_stack name \
1374 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001375 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1376 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378/**
Allan Stephensc98da842016-11-11 15:45:03 -05001379 * @} end defgroup stack_apis
1380 */
1381
Allan Stephens6bba9b02016-11-16 14:56:54 -05001382struct k_work;
1383
Allan Stephensc98da842016-11-11 15:45:03 -05001384/**
1385 * @defgroup workqueue_apis Workqueue Thread APIs
1386 * @ingroup kernel_apis
1387 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388 */
1389
Allan Stephens6bba9b02016-11-16 14:56:54 -05001390/**
1391 * @typedef k_work_handler_t
1392 * @brief Work item handler function type.
1393 *
1394 * A work item's handler function is executed by a workqueue's thread
1395 * when the work item is processed by the workqueue.
1396 *
1397 * @param work Address of the work item.
1398 *
1399 * @return N/A
1400 */
1401typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402
1403/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001404 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001406
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407struct k_work_q {
1408 struct k_fifo fifo;
1409};
1410
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001412 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413};
1414
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415struct k_work {
1416 void *_reserved; /* Used by k_fifo implementation. */
1417 k_work_handler_t handler;
1418 atomic_t flags[1];
1419};
1420
Allan Stephens6bba9b02016-11-16 14:56:54 -05001421struct k_delayed_work {
1422 struct k_work work;
1423 struct _timeout timeout;
1424 struct k_work_q *work_q;
1425};
1426
1427extern struct k_work_q k_sys_work_q;
1428
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001430 * INTERNAL_HIDDEN @endcond
1431 */
1432
1433/**
1434 * @brief Initialize a statically-defined work item.
1435 *
1436 * This macro can be used to initialize a statically-defined workqueue work
1437 * item, prior to its first use. For example,
1438 *
1439 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1440 *
1441 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001442 */
1443#define K_WORK_INITIALIZER(work_handler) \
1444 { \
1445 ._reserved = NULL, \
1446 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001447 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001448 }
1449
1450/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001451 * @brief Initialize a work item.
1452 *
1453 * This routine initializes a workqueue work item, prior to its first use.
1454 *
1455 * @param work Address of work item.
1456 * @param handler Function to invoke each time work item is processed.
1457 *
1458 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 */
1460static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1461{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001462 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463 work->handler = handler;
1464}
1465
1466/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001467 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001468 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001469 * This routine submits work item @a work to be processed by workqueue
1470 * @a work_q. If the work item is already pending in the workqueue's queue
1471 * as a result of an earlier submission, this routine has no effect on the
1472 * work item. If the work item has already been processed, or is currently
1473 * being processed, its work is considered complete and the work item can be
1474 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001475 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001476 * @warning
1477 * A submitted work item must not be modified until it has been processed
1478 * by the workqueue.
1479 *
1480 * @note Can be called by ISRs.
1481 *
1482 * @param work_q Address of workqueue.
1483 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001484 *
1485 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001486 */
1487static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1488 struct k_work *work)
1489{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001490 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491 k_fifo_put(&work_q->fifo, work);
1492 }
1493}
1494
1495/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001496 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001498 * This routine indicates if work item @a work is pending in a workqueue's
1499 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001501 * @note Can be called by ISRs.
1502 *
1503 * @param work Address of work item.
1504 *
1505 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001506 */
1507static inline int k_work_pending(struct k_work *work)
1508{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001509 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001510}
1511
1512/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001513 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001514 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001515 * This routine starts workqueue @a work_q. The workqueue spawns its work
1516 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001517 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001518 * @param work_q Address of workqueue.
1519 * @param stack Pointer to work queue thread's stack space.
1520 * @param stack_size Size of the work queue thread's stack (in bytes).
1521 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001522 *
1523 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001524 */
Allan Stephens904cf972016-10-07 13:59:23 -05001525extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001526 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001527
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001528/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001529 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001530 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001531 * This routine initializes a workqueue delayed work item, prior to
1532 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001533 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001534 * @param work Address of delayed work item.
1535 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536 *
1537 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001539extern void k_delayed_work_init(struct k_delayed_work *work,
1540 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001541
1542/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001543 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001545 * This routine schedules work item @a work to be processed by workqueue
1546 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1547 * an asychronous countdown for the work item and then returns to the caller.
1548 * Only when the countdown completes is the work item actually submitted to
1549 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001550 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001551 * Submitting a previously submitted delayed work item that is still
1552 * counting down cancels the existing submission and restarts the countdown
1553 * using the new delay. If the work item is currently pending on the
1554 * workqueue's queue because the countdown has completed it is too late to
1555 * resubmit the item, and resubmission fails without impacting the work item.
1556 * If the work item has already been processed, or is currently being processed,
1557 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001558 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001559 * @warning
1560 * A delayed work item must not be modified until it has been processed
1561 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001562 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001563 * @note Can be called by ISRs.
1564 *
1565 * @param work_q Address of workqueue.
1566 * @param work Address of delayed work item.
1567 * @param delay Delay before submitting the work item (in milliseconds).
1568 *
1569 * @retval 0 Work item countdown started.
1570 * @retval -EINPROGRESS Work item is already pending.
1571 * @retval -EINVAL Work item is being processed or has completed its work.
1572 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001574extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1575 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001576 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001577
1578/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001579 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001580 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001581 * This routine cancels the submission of delayed work item @a work.
1582 * A delayed work item can only be cancelled while its countdown is still
1583 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001584 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001585 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001586 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001587 * @param work Address of delayed work item.
1588 *
1589 * @retval 0 Work item countdown cancelled.
1590 * @retval -EINPROGRESS Work item is already pending.
1591 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001592 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001593extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001594
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001595/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001596 * @brief Submit a work item to the system workqueue.
1597 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001598 * This routine submits work item @a work to be processed by the system
1599 * workqueue. If the work item is already pending in the workqueue's queue
1600 * as a result of an earlier submission, this routine has no effect on the
1601 * work item. If the work item has already been processed, or is currently
1602 * being processed, its work is considered complete and the work item can be
1603 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001604 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001605 * @warning
1606 * Work items submitted to the system workqueue should avoid using handlers
1607 * that block or yield since this may prevent the system workqueue from
1608 * processing other work items in a timely manner.
1609 *
1610 * @note Can be called by ISRs.
1611 *
1612 * @param work Address of work item.
1613 *
1614 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615 */
1616static inline void k_work_submit(struct k_work *work)
1617{
1618 k_work_submit_to_queue(&k_sys_work_q, work);
1619}
1620
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001621/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001622 * @brief Submit a delayed work item to the system workqueue.
1623 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001624 * This routine schedules work item @a work to be processed by the system
1625 * workqueue after a delay of @a delay milliseconds. The routine initiates
1626 * an asychronous countdown for the work item and then returns to the caller.
1627 * Only when the countdown completes is the work item actually submitted to
1628 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001630 * Submitting a previously submitted delayed work item that is still
1631 * counting down cancels the existing submission and restarts the countdown
1632 * using the new delay. If the work item is currently pending on the
1633 * workqueue's queue because the countdown has completed it is too late to
1634 * resubmit the item, and resubmission fails without impacting the work item.
1635 * If the work item has already been processed, or is currently being processed,
1636 * its work is considered complete and the work item can be resubmitted.
1637 *
1638 * @warning
1639 * Work items submitted to the system workqueue should avoid using handlers
1640 * that block or yield since this may prevent the system workqueue from
1641 * processing other work items in a timely manner.
1642 *
1643 * @note Can be called by ISRs.
1644 *
1645 * @param work Address of delayed work item.
1646 * @param delay Delay before submitting the work item (in milliseconds).
1647 *
1648 * @retval 0 Work item countdown started.
1649 * @retval -EINPROGRESS Work item is already pending.
1650 * @retval -EINVAL Work item is being processed or has completed its work.
1651 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001652 */
1653static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001654 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001655{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001656 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657}
1658
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001659/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001660 * @brief Get time remaining before a delayed work gets scheduled.
1661 *
1662 * This routine computes the (approximate) time remaining before a
1663 * delayed work gets executed. If the delayed work is not waiting to be
1664 * schedules, it returns zero.
1665 *
1666 * @param work Delayed work item.
1667 *
1668 * @return Remaining time (in milliseconds).
1669 */
1670static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1671{
1672 return _timeout_remaining_get(&work->timeout);
1673}
1674
1675/**
Allan Stephensc98da842016-11-11 15:45:03 -05001676 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001677 */
1678
Allan Stephensc98da842016-11-11 15:45:03 -05001679/**
1680 * @cond INTERNAL_HIDDEN
1681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001682
1683struct k_mutex {
1684 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001685 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001686 uint32_t lock_count;
1687 int owner_orig_prio;
1688#ifdef CONFIG_OBJECT_MONITOR
1689 int num_lock_state_changes;
1690 int num_conflicts;
1691#endif
1692
Anas Nashif2f203c22016-12-18 06:57:45 -05001693 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694};
1695
1696#ifdef CONFIG_OBJECT_MONITOR
1697#define _MUTEX_INIT_OBJECT_MONITOR \
1698 .num_lock_state_changes = 0, .num_conflicts = 0,
1699#else
1700#define _MUTEX_INIT_OBJECT_MONITOR
1701#endif
1702
1703#define K_MUTEX_INITIALIZER(obj) \
1704 { \
1705 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1706 .owner = NULL, \
1707 .lock_count = 0, \
1708 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1709 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001710 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001711 }
1712
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713/**
Allan Stephensc98da842016-11-11 15:45:03 -05001714 * INTERNAL_HIDDEN @endcond
1715 */
1716
1717/**
1718 * @defgroup mutex_apis Mutex APIs
1719 * @ingroup kernel_apis
1720 * @{
1721 */
1722
1723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001724 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001728 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001733 struct k_mutex name \
1734 __in_section(_k_mutex, static, name) = \
1735 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001736
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * Upon completion, the mutex is available and does not have an owner.
1743 *
1744 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745 *
1746 * @return N/A
1747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749
1750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * This routine locks @a mutex. If the mutex is locked by another thread,
1754 * the calling thread waits until the mutex becomes available or until
1755 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * A thread is permitted to lock a mutex it has already locked. The operation
1758 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * @param mutex Address of the mutex.
1761 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 * or one of the special values K_NO_WAIT and K_FOREVER.
1763 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001764 * @retval 0 Mutex locked.
1765 * @retval -EBUSY Returned without waiting.
1766 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769
1770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * This routine unlocks @a mutex. The mutex must already be locked by the
1774 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775 *
1776 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001778 * thread.
1779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781 *
1782 * @return N/A
1783 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001784extern void k_mutex_unlock(struct k_mutex *mutex);
1785
Allan Stephensc98da842016-11-11 15:45:03 -05001786/**
1787 * @} end defgroup mutex_apis
1788 */
1789
1790/**
1791 * @cond INTERNAL_HIDDEN
1792 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001793
1794struct k_sem {
1795 _wait_q_t wait_q;
1796 unsigned int count;
1797 unsigned int limit;
1798
Anas Nashif2f203c22016-12-18 06:57:45 -05001799 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001800};
1801
Allan Stephensc98da842016-11-11 15:45:03 -05001802#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1803 { \
1804 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1805 .count = initial_count, \
1806 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001807 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001808 }
1809
1810/**
1811 * INTERNAL_HIDDEN @endcond
1812 */
1813
1814/**
1815 * @defgroup semaphore_apis Semaphore APIs
1816 * @ingroup kernel_apis
1817 * @{
1818 */
1819
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * @param sem Address of the semaphore.
1826 * @param initial_count Initial semaphore count.
1827 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001828 *
1829 * @return N/A
1830 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1832 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001833
1834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001837 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001839 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1840 *
1841 * @param sem Address of the semaphore.
1842 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001843 * or one of the special values K_NO_WAIT and K_FOREVER.
1844 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001845 * @note When porting code from the nanokernel legacy API to the new API, be
1846 * careful with the return value of this function. The return value is the
1847 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1848 * non-zero means failure, while the nano_sem_take family returns 1 for success
1849 * and 0 for failure.
1850 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001851 * @retval 0 Semaphore taken.
1852 * @retval -EBUSY Returned without waiting.
1853 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001854 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001856
1857/**
1858 * @brief Give a semaphore.
1859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * This routine gives @a sem, unless the semaphore is already at its maximum
1861 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001863 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001866 *
1867 * @return N/A
1868 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001869extern void k_sem_give(struct k_sem *sem);
1870
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001871/**
1872 * @brief Reset a semaphore's count to zero.
1873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001874 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001877 *
1878 * @return N/A
1879 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001880static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001881{
1882 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883}
1884
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001885/**
1886 * @brief Get a semaphore's count.
1887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001890 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001893 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001894static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895{
1896 return sem->count;
1897}
1898
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001902 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001903 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001904 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001906 * @param name Name of the semaphore.
1907 * @param initial_count Initial semaphore count.
1908 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001910#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001911 struct k_sem name \
1912 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913 K_SEM_INITIALIZER(name, initial_count, count_limit)
1914
Allan Stephensc98da842016-11-11 15:45:03 -05001915/**
1916 * @} end defgroup semaphore_apis
1917 */
1918
1919/**
1920 * @defgroup alert_apis Alert APIs
1921 * @ingroup kernel_apis
1922 * @{
1923 */
1924
Allan Stephens5eceb852016-11-16 10:16:30 -05001925/**
1926 * @typedef k_alert_handler_t
1927 * @brief Alert handler function type.
1928 *
1929 * An alert's alert handler function is invoked by the system workqueue
1930 * when the alert is signalled. The alert handler function is optional,
1931 * and is only invoked if the alert has been initialized with one.
1932 *
1933 * @param alert Address of the alert.
1934 *
1935 * @return 0 if alert has been consumed; non-zero if alert should pend.
1936 */
1937typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001938
1939/**
1940 * @} end defgroup alert_apis
1941 */
1942
1943/**
1944 * @cond INTERNAL_HIDDEN
1945 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001947#define K_ALERT_DEFAULT NULL
1948#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001950struct k_alert {
1951 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952 atomic_t send_count;
1953 struct k_work work_item;
1954 struct k_sem sem;
1955
Anas Nashif2f203c22016-12-18 06:57:45 -05001956 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957};
1958
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001959extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001961#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001963 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001965 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001966 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001967 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968 }
1969
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970/**
Allan Stephensc98da842016-11-11 15:45:03 -05001971 * INTERNAL_HIDDEN @endcond
1972 */
1973
1974/**
1975 * @addtogroup alert_apis
1976 * @{
1977 */
1978
1979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001982 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001984 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * @param name Name of the alert.
1987 * @param alert_handler Action to take when alert is sent. Specify either
1988 * the address of a function to be invoked by the system workqueue
1989 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1990 * K_ALERT_DEFAULT (which causes the alert to pend).
1991 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001993#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001994 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001995 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001996 K_ALERT_INITIALIZER(name, alert_handler, \
1997 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002000 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * @param alert Address of the alert.
2005 * @param handler Action to take when alert is sent. Specify either the address
2006 * of a function to be invoked by the system workqueue thread,
2007 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2008 * K_ALERT_DEFAULT (which causes the alert to pend).
2009 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
2011 * @return N/A
2012 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002013extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2014 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015
2016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2022 *
2023 * @param alert Address of the alert.
2024 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 * or one of the special values K_NO_WAIT and K_FOREVER.
2026 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002027 * @retval 0 Alert received.
2028 * @retval -EBUSY Returned without waiting.
2029 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002031extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002032
2033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * This routine signals @a alert. The action specified for @a alert will
2037 * be taken, which may trigger the execution of an alert handler function
2038 * and/or cause the alert to pend (assuming the alert has not reached its
2039 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @note Can be called by ISRs.
2042 *
2043 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044 *
2045 * @return N/A
2046 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002047extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048
2049/**
Allan Stephensc98da842016-11-11 15:45:03 -05002050 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051 */
2052
Allan Stephensc98da842016-11-11 15:45:03 -05002053/**
2054 * @cond INTERNAL_HIDDEN
2055 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056
2057struct k_msgq {
2058 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002059 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060 uint32_t max_msgs;
2061 char *buffer_start;
2062 char *buffer_end;
2063 char *read_ptr;
2064 char *write_ptr;
2065 uint32_t used_msgs;
2066
Anas Nashif2f203c22016-12-18 06:57:45 -05002067 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068};
2069
Peter Mitsis1da807e2016-10-06 11:36:59 -04002070#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071 { \
2072 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002073 .max_msgs = q_max_msgs, \
2074 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002075 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002076 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002077 .read_ptr = q_buffer, \
2078 .write_ptr = q_buffer, \
2079 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002080 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081 }
2082
Peter Mitsis1da807e2016-10-06 11:36:59 -04002083/**
Allan Stephensc98da842016-11-11 15:45:03 -05002084 * INTERNAL_HIDDEN @endcond
2085 */
2086
2087/**
2088 * @defgroup msgq_apis Message Queue APIs
2089 * @ingroup kernel_apis
2090 * @{
2091 */
2092
2093/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2097 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002098 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2099 * message is similarly aligned to this boundary, @a q_msg_size must also be
2100 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002102 * The message queue can be accessed outside the module where it is defined
2103 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002105 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @param q_name Name of the message queue.
2108 * @param q_msg_size Message size (in bytes).
2109 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002110 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002111 */
2112#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2113 static char __noinit __aligned(q_align) \
2114 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002115 struct k_msgq q_name \
2116 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002117 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2118 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119
Peter Mitsisd7a37502016-10-13 11:37:40 -04002120/**
2121 * @brief Initialize a message queue.
2122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * This routine initializes a message queue object, prior to its first use.
2124 *
Allan Stephensda827222016-11-09 14:23:58 -06002125 * The message queue's ring buffer must contain space for @a max_msgs messages,
2126 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2127 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2128 * that each message is similarly aligned to this boundary, @a q_msg_size
2129 * must also be a multiple of N.
2130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131 * @param q Address of the message queue.
2132 * @param buffer Pointer to ring buffer that holds queued messages.
2133 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002134 * @param max_msgs Maximum number of messages that can be queued.
2135 *
2136 * @return N/A
2137 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002138extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002139 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002140
2141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002142 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002145 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002146 * @note Can be called by ISRs.
2147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * @param q Address of the message queue.
2149 * @param data Pointer to the message.
2150 * @param timeout Waiting period to add the message (in milliseconds),
2151 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002152 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002153 * @retval 0 Message sent.
2154 * @retval -ENOMSG Returned without waiting or queue purged.
2155 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002158
2159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002160 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 * This routine receives a message from message queue @a q in a "first in,
2163 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002164 *
Allan Stephensc98da842016-11-11 15:45:03 -05002165 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * @param q Address of the message queue.
2168 * @param data Address of area to hold the received message.
2169 * @param timeout Waiting period to receive the message (in milliseconds),
2170 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002171 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002172 * @retval 0 Message received.
2173 * @retval -ENOMSG Returned without waiting.
2174 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002177
2178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * This routine discards all unreceived messages in a message queue's ring
2182 * buffer. Any threads that are blocked waiting to send a message to the
2183 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002186 *
2187 * @return N/A
2188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189extern void k_msgq_purge(struct k_msgq *q);
2190
Peter Mitsis67be2492016-10-07 11:44:34 -04002191/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002192 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002194 * This routine returns the number of unused entries in a message queue's
2195 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002197 * @param q Address of the message queue.
2198 *
2199 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002200 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002201static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002202{
2203 return q->max_msgs - q->used_msgs;
2204}
2205
Peter Mitsisd7a37502016-10-13 11:37:40 -04002206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002207 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002209 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002211 * @param q Address of the message queue.
2212 *
2213 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002214 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002215static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216{
2217 return q->used_msgs;
2218}
2219
Allan Stephensc98da842016-11-11 15:45:03 -05002220/**
2221 * @} end defgroup msgq_apis
2222 */
2223
2224/**
2225 * @defgroup mem_pool_apis Memory Pool APIs
2226 * @ingroup kernel_apis
2227 * @{
2228 */
2229
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002231 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232 void *addr_in_pool;
2233 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002234 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235};
2236
Allan Stephensc98da842016-11-11 15:45:03 -05002237/**
2238 * @} end defgroup mem_pool_apis
2239 */
2240
2241/**
2242 * @defgroup mailbox_apis Mailbox APIs
2243 * @ingroup kernel_apis
2244 * @{
2245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246
2247struct k_mbox_msg {
2248 /** internal use only - needed for legacy API support */
2249 uint32_t _mailbox;
2250 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002251 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252 /** application-defined information value */
2253 uint32_t info;
2254 /** sender's message data buffer */
2255 void *tx_data;
2256 /** internal use only - needed for legacy API support */
2257 void *_rx_data;
2258 /** message data block descriptor */
2259 struct k_mem_block tx_block;
2260 /** source thread id */
2261 k_tid_t rx_source_thread;
2262 /** target thread id */
2263 k_tid_t tx_target_thread;
2264 /** internal use only - thread waiting on send (may be a dummy) */
2265 k_tid_t _syncing_thread;
2266#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2267 /** internal use only - semaphore used during asynchronous send */
2268 struct k_sem *_async_sem;
2269#endif
2270};
2271
Allan Stephensc98da842016-11-11 15:45:03 -05002272/**
2273 * @cond INTERNAL_HIDDEN
2274 */
2275
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276struct k_mbox {
2277 _wait_q_t tx_msg_queue;
2278 _wait_q_t rx_msg_queue;
2279
Anas Nashif2f203c22016-12-18 06:57:45 -05002280 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281};
2282
2283#define K_MBOX_INITIALIZER(obj) \
2284 { \
2285 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2286 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002287 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288 }
2289
Peter Mitsis12092702016-10-14 12:57:23 -04002290/**
Allan Stephensc98da842016-11-11 15:45:03 -05002291 * INTERNAL_HIDDEN @endcond
2292 */
2293
2294/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002295 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002299 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002302 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002304 struct k_mbox name \
2305 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 K_MBOX_INITIALIZER(name) \
2307
Peter Mitsis12092702016-10-14 12:57:23 -04002308/**
2309 * @brief Initialize a mailbox.
2310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002311 * This routine initializes a mailbox object, prior to its first use.
2312 *
2313 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002314 *
2315 * @return N/A
2316 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317extern void k_mbox_init(struct k_mbox *mbox);
2318
Peter Mitsis12092702016-10-14 12:57:23 -04002319/**
2320 * @brief Send a mailbox message in a synchronous manner.
2321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * This routine sends a message to @a mbox and waits for a receiver to both
2323 * receive and process it. The message data may be in a buffer, in a memory
2324 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * @param mbox Address of the mailbox.
2327 * @param tx_msg Address of the transmit message descriptor.
2328 * @param timeout Waiting period for the message to be received (in
2329 * milliseconds), or one of the special values K_NO_WAIT
2330 * and K_FOREVER. Once the message has been received,
2331 * this routine waits as long as necessary for the message
2332 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002333 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002334 * @retval 0 Message sent.
2335 * @retval -ENOMSG Returned without waiting.
2336 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002337 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002338extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002340
Peter Mitsis12092702016-10-14 12:57:23 -04002341/**
2342 * @brief Send a mailbox message in an asynchronous manner.
2343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * This routine sends a message to @a mbox without waiting for a receiver
2345 * to process it. The message data may be in a buffer, in a memory pool block,
2346 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2347 * will be given when the message has been both received and completely
2348 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @param mbox Address of the mailbox.
2351 * @param tx_msg Address of the transmit message descriptor.
2352 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002353 *
2354 * @return N/A
2355 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002356extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357 struct k_sem *sem);
2358
Peter Mitsis12092702016-10-14 12:57:23 -04002359/**
2360 * @brief Receive a mailbox message.
2361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * This routine receives a message from @a mbox, then optionally retrieves
2363 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * @param mbox Address of the mailbox.
2366 * @param rx_msg Address of the receive message descriptor.
2367 * @param buffer Address of the buffer to receive data, or NULL to defer data
2368 * retrieval and message disposal until later.
2369 * @param timeout Waiting period for a message to be received (in
2370 * milliseconds), or one of the special values K_NO_WAIT
2371 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002372 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002373 * @retval 0 Message received.
2374 * @retval -ENOMSG Returned without waiting.
2375 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002376 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002377extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002379
2380/**
2381 * @brief Retrieve mailbox message data into a buffer.
2382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * This routine completes the processing of a received message by retrieving
2384 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002385 *
2386 * Alternatively, this routine can be used to dispose of a received message
2387 * without retrieving its data.
2388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * @param rx_msg Address of the receive message descriptor.
2390 * @param buffer Address of the buffer to receive data, or NULL to discard
2391 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002392 *
2393 * @return N/A
2394 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002395extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002396
2397/**
2398 * @brief Retrieve mailbox message data into a memory pool block.
2399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * This routine completes the processing of a received message by retrieving
2401 * its data into a memory pool block, then disposing of the message.
2402 * The memory pool block that results from successful retrieval must be
2403 * returned to the pool once the data has been processed, even in cases
2404 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002405 *
2406 * Alternatively, this routine can be used to dispose of a received message
2407 * without retrieving its data. In this case there is no need to return a
2408 * memory pool block to the pool.
2409 *
2410 * This routine allocates a new memory pool block for the data only if the
2411 * data is not already in one. If a new block cannot be allocated, the routine
2412 * returns a failure code and the received message is left unchanged. This
2413 * permits the caller to reattempt data retrieval at a later time or to dispose
2414 * of the received message without retrieving its data.
2415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @param rx_msg Address of a receive message descriptor.
2417 * @param pool Address of memory pool, or NULL to discard data.
2418 * @param block Address of the area to hold memory pool block info.
2419 * @param timeout Waiting period to wait for a memory pool block (in
2420 * milliseconds), or one of the special values K_NO_WAIT
2421 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002422 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002423 * @retval 0 Data retrieved.
2424 * @retval -ENOMEM Returned without waiting.
2425 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002426 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002427extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002428 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429 struct k_mem_block *block, int32_t timeout);
2430
Allan Stephensc98da842016-11-11 15:45:03 -05002431/**
2432 * @} end defgroup mailbox_apis
2433 */
2434
2435/**
2436 * @cond INTERNAL_HIDDEN
2437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438
2439struct k_pipe {
2440 unsigned char *buffer; /* Pipe buffer: may be NULL */
2441 size_t size; /* Buffer size */
2442 size_t bytes_used; /* # bytes used in buffer */
2443 size_t read_index; /* Where in buffer to read from */
2444 size_t write_index; /* Where in buffer to write */
2445
2446 struct {
2447 _wait_q_t readers; /* Reader wait queue */
2448 _wait_q_t writers; /* Writer wait queue */
2449 } wait_q;
2450
Anas Nashif2f203c22016-12-18 06:57:45 -05002451 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452};
2453
Peter Mitsise5d9c582016-10-14 14:44:57 -04002454#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455 { \
2456 .buffer = pipe_buffer, \
2457 .size = pipe_buffer_size, \
2458 .bytes_used = 0, \
2459 .read_index = 0, \
2460 .write_index = 0, \
2461 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2462 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002463 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464 }
2465
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466/**
Allan Stephensc98da842016-11-11 15:45:03 -05002467 * INTERNAL_HIDDEN @endcond
2468 */
2469
2470/**
2471 * @defgroup pipe_apis Pipe APIs
2472 * @ingroup kernel_apis
2473 * @{
2474 */
2475
2476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002481 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @param name Name of the pipe.
2484 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2485 * or zero if no ring buffer is used.
2486 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002488#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2489 static unsigned char __noinit __aligned(pipe_align) \
2490 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002491 struct k_pipe name \
2492 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002493 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002496 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * This routine initializes a pipe object, prior to its first use.
2499 *
2500 * @param pipe Address of the pipe.
2501 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2502 * is used.
2503 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2504 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 *
2506 * @return N/A
2507 */
2508extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2509 size_t size);
2510
2511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @param pipe Address of the pipe.
2517 * @param data Address of data to write.
2518 * @param bytes_to_write Size of data (in bytes).
2519 * @param bytes_written Address of area to hold the number of bytes written.
2520 * @param min_xfer Minimum number of bytes to write.
2521 * @param timeout Waiting period to wait for the data to be written (in
2522 * milliseconds), or one of the special values K_NO_WAIT
2523 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002525 * @retval 0 At least @a min_xfer bytes of data were written.
2526 * @retval -EIO Returned without waiting; zero data bytes were written.
2527 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002530extern int k_pipe_put(struct k_pipe *pipe, void *data,
2531 size_t bytes_to_write, size_t *bytes_written,
2532 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533
2534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @param pipe Address of the pipe.
2540 * @param data Address to place the data read from pipe.
2541 * @param bytes_to_read Maximum number of data bytes to read.
2542 * @param bytes_read Address of area to hold the number of bytes read.
2543 * @param min_xfer Minimum number of data bytes to read.
2544 * @param timeout Waiting period to wait for the data to be read (in
2545 * milliseconds), or one of the special values K_NO_WAIT
2546 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002548 * @retval 0 At least @a min_xfer bytes of data were read.
2549 * @retval -EIO Returned without waiting; zero data bytes were read.
2550 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002553extern int k_pipe_get(struct k_pipe *pipe, void *data,
2554 size_t bytes_to_read, size_t *bytes_read,
2555 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
2557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * This routine writes the data contained in a memory block to @a pipe.
2561 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565 * @param block Memory block containing data to send
2566 * @param size Number of data bytes in memory block to send
2567 * @param sem Semaphore to signal upon completion (else NULL)
2568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570 */
2571extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2572 size_t size, struct k_sem *sem);
2573
2574/**
Allan Stephensc98da842016-11-11 15:45:03 -05002575 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576 */
2577
Allan Stephensc98da842016-11-11 15:45:03 -05002578/**
2579 * @cond INTERNAL_HIDDEN
2580 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002582struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002584 uint32_t num_blocks;
2585 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 char *buffer;
2587 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002588 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589
Anas Nashif2f203c22016-12-18 06:57:45 -05002590 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591};
2592
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002593#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2594 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595 { \
2596 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002597 .num_blocks = slab_num_blocks, \
2598 .block_size = slab_block_size, \
2599 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600 .free_list = NULL, \
2601 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002602 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603 }
2604
Peter Mitsis578f9112016-10-07 13:50:31 -04002605/**
Allan Stephensc98da842016-11-11 15:45:03 -05002606 * INTERNAL_HIDDEN @endcond
2607 */
2608
2609/**
2610 * @defgroup mem_slab_apis Memory Slab APIs
2611 * @ingroup kernel_apis
2612 * @{
2613 */
2614
2615/**
Allan Stephensda827222016-11-09 14:23:58 -06002616 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002617 *
Allan Stephensda827222016-11-09 14:23:58 -06002618 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002620 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2621 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002622 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002623 *
Allan Stephensda827222016-11-09 14:23:58 -06002624 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002625 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002627 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * @param name Name of the memory slab.
2630 * @param slab_block_size Size of each memory block (in bytes).
2631 * @param slab_num_blocks Number memory blocks.
2632 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002633 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002634#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2635 char __noinit __aligned(slab_align) \
2636 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2637 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002638 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002639 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2640 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002642/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002643 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002646 *
Allan Stephensda827222016-11-09 14:23:58 -06002647 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2648 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2649 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2650 * To ensure that each memory block is similarly aligned to this boundary,
2651 * @a slab_block_size must also be a multiple of N.
2652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 * @param slab Address of the memory slab.
2654 * @param buffer Pointer to buffer used for the memory blocks.
2655 * @param block_size Size of each memory block (in bytes).
2656 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002657 *
2658 * @return N/A
2659 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002660extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002661 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002662
2663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * @param slab Address of the memory slab.
2669 * @param mem Pointer to block address area.
2670 * @param timeout Maximum time to wait for operation to complete
2671 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2672 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002673 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002674 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002676 * @retval -ENOMEM Returned without waiting.
2677 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002678 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002679extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2680 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002681
2682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * This routine releases a previously allocated memory block back to its
2686 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * @param slab Address of the memory slab.
2689 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002690 *
2691 * @return N/A
2692 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002693extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002698 * This routine gets the number of memory blocks that are currently
2699 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002704 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002705static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002707 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708}
2709
Peter Mitsisc001aa82016-10-13 13:53:37 -04002710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002713 * This routine gets the number of memory blocks that are currently
2714 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002719 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002720static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002721{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002722 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002723}
2724
Allan Stephensc98da842016-11-11 15:45:03 -05002725/**
2726 * @} end defgroup mem_slab_apis
2727 */
2728
2729/**
2730 * @cond INTERNAL_HIDDEN
2731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002733/*
2734 * Memory pool requires a buffer and two arrays of structures for the
2735 * memory block accounting:
2736 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2737 * status of four blocks of memory.
2738 */
2739struct k_mem_pool_quad_block {
2740 char *mem_blocks; /* pointer to the first of four memory blocks */
2741 uint32_t mem_status; /* four bits. If bit is set, memory block is
2742 allocated */
2743};
2744/*
2745 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2746 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2747 * block size is 4 times less than the previous one and thus requires 4 times
2748 * bigger array of k_mem_pool_quad_block structures to keep track of the
2749 * memory blocks.
2750 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002752/*
2753 * The array of k_mem_pool_block_set keeps the information of each array of
2754 * k_mem_pool_quad_block structures
2755 */
2756struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002757 size_t block_size; /* memory block size */
2758 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002759 struct k_mem_pool_quad_block *quad_block;
2760 int count;
2761};
2762
2763/* Memory pool descriptor */
2764struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002765 size_t max_block_size;
2766 size_t min_block_size;
2767 uint32_t nr_of_maxblocks;
2768 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002769 struct k_mem_pool_block_set *block_set;
2770 char *bufblock;
2771 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002772 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773};
2774
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002775#ifdef CONFIG_ARM
2776#define _SECTION_TYPE_SIGN "%"
2777#else
2778#define _SECTION_TYPE_SIGN "@"
2779#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002781/*
2782 * Static memory pool initialization
2783 */
Allan Stephensc98da842016-11-11 15:45:03 -05002784
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002785/*
2786 * Use .altmacro to be able to recalculate values and pass them as string
2787 * arguments when calling assembler macros resursively
2788 */
2789__asm__(".altmacro\n\t");
2790
2791/*
2792 * Recursively calls a macro
2793 * The followig global symbols need to be initialized:
2794 * __memory_pool_max_block_size - maximal size of the memory block
2795 * __memory_pool_min_block_size - minimal size of the memory block
2796 * Notes:
2797 * Global symbols are used due the fact that assembler macro allows only
2798 * one argument be passed with the % conversion
2799 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2800 * is used instead of / 4.
2801 * n_max argument needs to go first in the invoked macro, as some
2802 * assemblers concatenate \name and %(\n_max * 4) arguments
2803 * if \name goes first
2804 */
2805__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2806 ".ifge __memory_pool_max_block_size >> 2 -"
2807 " __memory_pool_min_block_size\n\t\t"
2808 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2809 "\\macro_name %(\\n_max * 4) \\name\n\t"
2810 ".endif\n\t"
2811 ".endm\n");
2812
2813/*
2814 * Build quad blocks
2815 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2816 * structures and recursively calls itself for the next array, 4 times
2817 * larger.
2818 * The followig global symbols need to be initialized:
2819 * __memory_pool_max_block_size - maximal size of the memory block
2820 * __memory_pool_min_block_size - minimal size of the memory block
2821 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2822 */
2823__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002824 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002825 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2826 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2827 ".if \\n_max % 4\n\t\t"
2828 ".skip __memory_pool_quad_block_size\n\t"
2829 ".endif\n\t"
2830 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2831 ".endm\n");
2832
2833/*
2834 * Build block sets and initialize them
2835 * Macro initializes the k_mem_pool_block_set structure and
2836 * recursively calls itself for the next one.
2837 * The followig global symbols need to be initialized:
2838 * __memory_pool_max_block_size - maximal size of the memory block
2839 * __memory_pool_min_block_size - minimal size of the memory block
2840 * __memory_pool_block_set_count, the number of the elements in the
2841 * block set array must be set to 0. Macro calculates it's real
2842 * value.
2843 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2844 * structures, _build_quad_blocks must be called prior it.
2845 */
2846__asm__(".macro _build_block_set n_max, name\n\t"
2847 ".int __memory_pool_max_block_size\n\t" /* block_size */
2848 ".if \\n_max % 4\n\t\t"
2849 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2850 ".else\n\t\t"
2851 ".int \\n_max >> 2\n\t"
2852 ".endif\n\t"
2853 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2854 ".int 0\n\t" /* count */
2855 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2856 "__do_recurse _build_block_set \\name \\n_max\n\t"
2857 ".endm\n");
2858
2859/*
2860 * Build a memory pool structure and initialize it
2861 * Macro uses __memory_pool_block_set_count global symbol,
2862 * block set addresses and buffer address, it may be called only after
2863 * _build_block_set
2864 */
2865__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002866 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002867 _SECTION_TYPE_SIGN "progbits\n\t"
2868 ".globl \\name\n\t"
2869 "\\name:\n\t"
2870 ".int \\max_size\n\t" /* max_block_size */
2871 ".int \\min_size\n\t" /* min_block_size */
2872 ".int \\n_max\n\t" /* nr_of_maxblocks */
2873 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2874 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2875 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2876 ".int 0\n\t" /* wait_q->head */
2877 ".int 0\n\t" /* wait_q->next */
2878 ".popsection\n\t"
2879 ".endm\n");
2880
2881#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2882 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2883 _SECTION_TYPE_SIGN "progbits\n\t"); \
2884 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2885 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2886 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2887 STRINGIFY(name) "\n\t"); \
2888 __asm__(".popsection\n\t")
2889
2890#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2891 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2892 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2893 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2894 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002895 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002896 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2897 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2898 STRINGIFY(name) "\n\t"); \
2899 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2900 __asm__(".int __memory_pool_block_set_count\n\t"); \
2901 __asm__(".popsection\n\t"); \
2902 extern uint32_t _mem_pool_block_set_count_##name; \
2903 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2904
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002905#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2906 char __noinit __aligned(align) \
2907 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002908
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002909/*
2910 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2911 * to __memory_pool_quad_block_size absolute symbol.
2912 * This function does not get called, but compiler calculates the value and
2913 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2914 */
2915static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2916{
2917 __asm__(".globl __memory_pool_quad_block_size\n\t"
2918#ifdef CONFIG_NIOS2
2919 "__memory_pool_quad_block_size = %0\n\t"
2920#else
2921 "__memory_pool_quad_block_size = %c0\n\t"
2922#endif
2923 :
2924 : "n"(sizeof(struct k_mem_pool_quad_block)));
2925}
2926
2927/**
Allan Stephensc98da842016-11-11 15:45:03 -05002928 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002929 */
2930
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002931/**
Allan Stephensc98da842016-11-11 15:45:03 -05002932 * @addtogroup mem_pool_apis
2933 * @{
2934 */
2935
2936/**
2937 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2940 * long. The memory pool allows blocks to be repeatedly partitioned into
2941 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2942 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002943 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002945 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002946 * If the pool is to be accessed outside the module where it is defined, it
2947 * can be declared via
2948 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002949 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002951 * @param name Name of the memory pool.
2952 * @param min_size Size of the smallest blocks in the pool (in bytes).
2953 * @param max_size Size of the largest blocks in the pool (in bytes).
2954 * @param n_max Number of maximum sized blocks in the pool.
2955 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002956 */
2957#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002958 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2959 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002960 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002961 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2962 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2963 extern struct k_mem_pool name
2964
Peter Mitsis937042c2016-10-13 13:18:26 -04002965/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @param pool Address of the memory pool.
2971 * @param block Pointer to block descriptor for the allocated memory.
2972 * @param size Amount of memory to allocate (in bytes).
2973 * @param timeout Maximum time to wait for operation to complete
2974 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2975 * or K_FOREVER to wait as long as necessary.
2976 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002977 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002979 * @retval -ENOMEM Returned without waiting.
2980 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002981 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002982extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002983 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002984
2985/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002988 * This routine releases a previously allocated memory block back to its
2989 * memory pool.
2990 *
2991 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002992 *
2993 * @return N/A
2994 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002996
2997/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003000 * This routine instructs a memory pool to concatenate unused memory blocks
3001 * into larger blocks wherever possible. Manually defragmenting the memory
3002 * pool may speed up future allocations of memory blocks by eliminating the
3003 * need for the memory pool to perform an automatic partial defragmentation.
3004 *
3005 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003006 *
3007 * @return N/A
3008 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003009extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003010
3011/**
Allan Stephensc98da842016-11-11 15:45:03 -05003012 * @} end addtogroup mem_pool_apis
3013 */
3014
3015/**
3016 * @defgroup heap_apis Heap Memory Pool APIs
3017 * @ingroup kernel_apis
3018 * @{
3019 */
3020
3021/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003025 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003030 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003031extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003032
3033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003035 *
3036 * This routine provides traditional free() semantics. The memory being
3037 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003038 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003039 * If @a ptr is NULL, no operation is performed.
3040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003041 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003042 *
3043 * @return N/A
3044 */
3045extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046
Allan Stephensc98da842016-11-11 15:45:03 -05003047/**
3048 * @} end defgroup heap_apis
3049 */
3050
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003051/**
3052 * @brief Make the CPU idle.
3053 *
3054 * This function makes the CPU idle until an event wakes it up.
3055 *
3056 * In a regular system, the idle thread should be the only thread responsible
3057 * for making the CPU idle and triggering any type of power management.
3058 * However, in some more constrained systems, such as a single-threaded system,
3059 * the only thread would be responsible for this if needed.
3060 *
3061 * @return N/A
3062 */
3063extern void k_cpu_idle(void);
3064
3065/**
3066 * @brief Make the CPU idle in an atomic fashion.
3067 *
3068 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3069 * must be done atomically before making the CPU idle.
3070 *
3071 * @param key Interrupt locking key obtained from irq_lock().
3072 *
3073 * @return N/A
3074 */
3075extern void k_cpu_atomic_idle(unsigned int key);
3076
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077/*
3078 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
3079 * hook into the device subsystem, which itself uses nanokernel semaphores,
3080 * and thus currently requires the definition of nano_sem.
3081 */
3082#include <legacy.h>
3083#include <arch/cpu.h>
3084
3085/*
3086 * private APIs that are utilized by one or more public APIs
3087 */
3088
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003089#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003091#else
3092#define _init_static_threads() do { } while ((0))
3093#endif
3094
3095extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003096extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097
3098#ifdef __cplusplus
3099}
3100#endif
3101
Andrew Boiee004dec2016-11-07 09:01:19 -08003102#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3103/*
3104 * Define new and delete operators.
3105 * At this moment, the operators do nothing since objects are supposed
3106 * to be statically allocated.
3107 */
3108inline void operator delete(void *ptr)
3109{
3110 (void)ptr;
3111}
3112
3113inline void operator delete[](void *ptr)
3114{
3115 (void)ptr;
3116}
3117
3118inline void *operator new(size_t size)
3119{
3120 (void)size;
3121 return NULL;
3122}
3123
3124inline void *operator new[](size_t size)
3125{
3126 (void)size;
3127 return NULL;
3128}
3129
3130/* Placement versions of operator new and delete */
3131inline void operator delete(void *ptr1, void *ptr2)
3132{
3133 (void)ptr1;
3134 (void)ptr2;
3135}
3136
3137inline void operator delete[](void *ptr1, void *ptr2)
3138{
3139 (void)ptr1;
3140 (void)ptr2;
3141}
3142
3143inline void *operator new(size_t size, void *ptr)
3144{
3145 (void)size;
3146 return ptr;
3147}
3148
3149inline void *operator new[](size_t size, void *ptr)
3150{
3151 (void)size;
3152 return ptr;
3153}
3154
3155#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3156
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157#endif /* _kernel__h_ */