blob: b8c1c3e11b6c1d8a847f14255d2543559e6d196b [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050035#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050036#include <kernel_version.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Anas Nashifbbb157d2017-01-15 08:46:31 -050042/**
43 * @brief Kernel APIs
44 * @defgroup kernel_apis Kernel APIs
45 * @{
46 * @}
47 */
48
Anas Nashif61f4b242016-11-18 10:53:59 -050049#ifdef CONFIG_KERNEL_DEBUG
50#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
56#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
57#define K_PRIO_PREEMPT(x) (x)
58
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_ANY NULL
60#define K_END NULL
61
Benjamin Walshedb35702017-01-14 18:47:22 -050062#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050064#elif defined(CONFIG_COOP_ENABLED)
65#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
66#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050068#else
69#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#endif
71
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050072#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
74#else
75#define K_LOWEST_THREAD_PRIO -1
76#endif
77
Benjamin Walshfab8d922016-11-08 15:36:36 -050078#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
79
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
81#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
82
83typedef sys_dlist_t _wait_q_t;
84
Anas Nashif2f203c22016-12-18 06:57:45 -050085#ifdef CONFIG_OBJECT_TRACING
86#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
87#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#else
Anas Nashif2f203c22016-12-18 06:57:45 -050089#define _OBJECT_TRACING_INIT
90#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040091#endif
92
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050093#define tcs k_thread
94struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095struct k_mutex;
96struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040097struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098struct k_msgq;
99struct k_mbox;
100struct k_pipe;
101struct k_fifo;
102struct k_lifo;
103struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400104struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105struct k_mem_pool;
106struct k_timer;
107
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400108typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110enum execution_context_types {
111 K_ISR = 0,
112 K_COOP_THREAD,
113 K_PREEMPT_THREAD,
114};
115
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400116/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100117 * @defgroup profiling_apis Profiling APIs
118 * @ingroup kernel_apis
119 * @{
120 */
121
122/**
123 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
124 *
125 * This routine calls @ref stack_analyze on the 4 call stacks declared and
126 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
127 *
128 * CONFIG_MAIN_STACK_SIZE
129 * CONFIG_IDLE_STACK_SIZE
130 * CONFIG_ISR_STACK_SIZE
131 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
132 *
133 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
134 * produce output.
135 *
136 * @return N/A
137 */
138extern void k_call_stacks_analyze(void);
139
140/**
141 * @} end defgroup profiling_apis
142 */
143
144/**
Allan Stephensc98da842016-11-11 15:45:03 -0500145 * @defgroup thread_apis Thread APIs
146 * @ingroup kernel_apis
147 * @{
148 */
149
150/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500151 * @typedef k_thread_entry_t
152 * @brief Thread entry point function type.
153 *
154 * A thread's entry point function is invoked when the thread starts executing.
155 * Up to 3 argument values can be passed to the function.
156 *
157 * The thread terminates execution permanently if the entry point function
158 * returns. The thread is responsible for releasing any shared resources
159 * it may own (such as mutexes and dynamically allocated memory), prior to
160 * returning.
161 *
162 * @param p1 First argument.
163 * @param p2 Second argument.
164 * @param p3 Third argument.
165 *
166 * @return N/A
167 */
168typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
169
170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500171 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500173 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500175 * The new thread may be scheduled for immediate execution or a delayed start.
176 * If the newly spawned thread does not have a delayed start the kernel
177 * scheduler may preempt the current thread to allow the new thread to
178 * execute.
179 *
180 * Thread options are architecture-specific, and can include K_ESSENTIAL,
181 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
182 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400183 *
184 * @param stack Pointer to the stack space.
185 * @param stack_size Stack size in bytes.
186 * @param entry Thread entry function.
187 * @param p1 1st entry point parameter.
188 * @param p2 2nd entry point parameter.
189 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500190 * @param prio Thread priority.
191 * @param options Thread options.
192 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500194 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500196extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500197 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400198 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500199 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400200
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500202 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400203 *
Allan Stephensc98da842016-11-11 15:45:03 -0500204 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500205 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500207 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 *
209 * @return N/A
210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400211extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212
213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500214 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400215 *
216 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500217 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400219 * @return N/A
220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400221extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400222
223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500224 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500226 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500228 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400229 *
230 * @return N/A
231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400232extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400233
234/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500235 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500239 * If @a thread is not currently sleeping, the routine has no effect.
240 *
241 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242 *
243 * @return N/A
244 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400246
247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500248 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500250 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400251 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400252extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253
254/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500255 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500257 * This routine prevents @a thread from executing if it has not yet started
258 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500260 * @param thread ID of thread to cancel.
261 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500262 * @retval 0 Thread spawning cancelled.
263 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400265extern int k_thread_cancel(k_tid_t thread);
266
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267/**
Allan Stephensc98da842016-11-11 15:45:03 -0500268 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500270 * This routine permanently stops execution of @a thread. The thread is taken
271 * off all kernel queues it is part of (i.e. the ready queue, the timeout
272 * queue, or a kernel object wait queue). However, any kernel resources the
273 * thread might currently own (such as mutexes or memory blocks) are not
274 * released. It is the responsibility of the caller of this routine to ensure
275 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500277 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400278 *
279 * @return N/A
280 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400281extern void k_thread_abort(k_tid_t thread);
282
Allan Stephensc98da842016-11-11 15:45:03 -0500283/**
284 * @cond INTERNAL_HIDDEN
285 */
286
Benjamin Walshd211a522016-12-06 11:44:01 -0500287/* timeout has timed out and is not on _timeout_q anymore */
288#define _EXPIRED (-2)
289
290/* timeout is not in use */
291#define _INACTIVE (-1)
292
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400293#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400294#define _THREAD_TIMEOUT_INIT(obj) \
295 (obj).nano_timeout = { \
296 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400297 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400298 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500299 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400300 },
301#else
302#define _THREAD_TIMEOUT_INIT(obj)
303#endif
304
305#ifdef CONFIG_ERRNO
306#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
307#else
308#define _THREAD_ERRNO_INIT(obj)
309#endif
310
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400311struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400312 union {
313 char *init_stack;
314 struct k_thread *thread;
315 };
316 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500317 void (*init_entry)(void *, void *, void *);
318 void *init_p1;
319 void *init_p2;
320 void *init_p3;
321 int init_prio;
322 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400323 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500324 void (*init_abort)(void);
325 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400326};
327
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400328#define _THREAD_INITIALIZER(stack, stack_size, \
329 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500330 prio, options, delay, abort, groups) \
331 { \
332 .init_stack = (stack), \
333 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400334 .init_entry = (void (*)(void *, void *, void *))entry, \
335 .init_p1 = (void *)p1, \
336 .init_p2 = (void *)p2, \
337 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500338 .init_prio = (prio), \
339 .init_options = (options), \
340 .init_delay = (delay), \
341 .init_abort = (abort), \
342 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400343 }
344
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400345/**
Allan Stephensc98da842016-11-11 15:45:03 -0500346 * INTERNAL_HIDDEN @endcond
347 */
348
349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500350 * @brief Statically define and initialize a thread.
351 *
352 * The thread may be scheduled for immediate execution or a delayed start.
353 *
354 * Thread options are architecture-specific, and can include K_ESSENTIAL,
355 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
356 * them using "|" (the logical OR operator).
357 *
358 * The ID of the thread can be accessed using:
359 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500360 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500361 *
362 * @param name Name of the thread.
363 * @param stack_size Stack size in bytes.
364 * @param entry Thread entry function.
365 * @param p1 1st entry point parameter.
366 * @param p2 2nd entry point parameter.
367 * @param p3 3rd entry point parameter.
368 * @param prio Thread priority.
369 * @param options Thread options.
370 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400371 *
372 * @internal It has been observed that the x86 compiler by default aligns
373 * these _static_thread_data structures to 32-byte boundaries, thereby
374 * wasting space. To work around this, force a 4-byte alignment.
375 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500376#define K_THREAD_DEFINE(name, stack_size, \
377 entry, p1, p2, p3, \
378 prio, options, delay) \
379 char __noinit __stack _k_thread_obj_##name[stack_size]; \
380 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500381 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500382 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
383 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500384 NULL, 0); \
385 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400386
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500390 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500392 * @param thread ID of thread whose priority is needed.
393 *
394 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400395 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500396extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400397
398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500399 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500401 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400402 *
403 * Rescheduling can occur immediately depending on the priority @a thread is
404 * set to:
405 *
406 * - If its priority is raised above the priority of the caller of this
407 * function, and the caller is preemptible, @a thread will be scheduled in.
408 *
409 * - If the caller operates on itself, it lowers its priority below that of
410 * other threads in the system, and the caller is preemptible, the thread of
411 * highest priority will be scheduled in.
412 *
413 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
414 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
415 * highest priority.
416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500417 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400418 * @param prio New priority.
419 *
420 * @warning Changing the priority of a thread currently involved in mutex
421 * priority inheritance may result in undefined behavior.
422 *
423 * @return N/A
424 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400425extern void k_thread_priority_set(k_tid_t thread, int prio);
426
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500428 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500430 * This routine prevents the kernel scheduler from making @a thread the
431 * current thread. All other internal operations on @a thread are still
432 * performed; for example, any timeout it is waiting on keeps ticking,
433 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * If @a thread is already suspended, the routine has no effect.
436 *
437 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400438 *
439 * @return N/A
440 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400441extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442
443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500444 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * This routine allows the kernel scheduler to make @a thread the current
447 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500449 * If @a thread is not currently suspended, the routine has no effect.
450 *
451 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400452 *
453 * @return N/A
454 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400455extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400456
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500460 * This routine specifies how the scheduler will perform time slicing of
461 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * To enable time slicing, @a slice must be non-zero. The scheduler
464 * ensures that no thread runs for more than the specified time limit
465 * before other threads of that priority are given a chance to execute.
466 * Any thread whose priority is higher than @a prio is exempted, and may
467 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500469 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400470 * execute. Once the scheduler selects a thread for execution, there is no
471 * minimum guaranteed time the thread will execute before threads of greater or
472 * equal priority are scheduled.
473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 * for execution, this routine has no effect; the thread is immediately
476 * rescheduled after the slice period expires.
477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500478 * To disable timeslicing, set both @a slice and @a prio to zero.
479 *
480 * @param slice Maximum time slice length (in milliseconds).
481 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400482 *
483 * @return N/A
484 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400485extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400486
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487/**
Allan Stephensc98da842016-11-11 15:45:03 -0500488 * @} end defgroup thread_apis
489 */
490
491/**
492 * @addtogroup isr_apis
493 * @{
494 */
495
496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500497 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
Allan Stephensc98da842016-11-11 15:45:03 -0500499 * This routine allows the caller to customize its actions, depending on
500 * whether it is a thread or an ISR.
501 *
502 * @note Can be called by ISRs.
503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500504 * @return 0 if invoked by a thread.
505 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400506 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500507extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400508
Benjamin Walsh445830d2016-11-10 15:54:27 -0500509/**
510 * @brief Determine if code is running in a preemptible thread.
511 *
Allan Stephensc98da842016-11-11 15:45:03 -0500512 * This routine allows the caller to customize its actions, depending on
513 * whether it can be preempted by another thread. The routine returns a 'true'
514 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500515 *
Allan Stephensc98da842016-11-11 15:45:03 -0500516 * - The code is running in a thread, not at ISR.
517 * - The thread's priority is in the preemptible range.
518 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500519 *
Allan Stephensc98da842016-11-11 15:45:03 -0500520 * @note Can be called by ISRs.
521 *
522 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500523 * @return Non-zero if invoked by a preemptible thread.
524 */
525extern int k_is_preempt_thread(void);
526
Allan Stephensc98da842016-11-11 15:45:03 -0500527/**
528 * @} end addtogroup isr_apis
529 */
530
531/**
532 * @addtogroup thread_apis
533 * @{
534 */
535
536/**
537 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500538 *
Allan Stephensc98da842016-11-11 15:45:03 -0500539 * This routine prevents the current thread from being preempted by another
540 * thread by instructing the scheduler to treat it as a cooperative thread.
541 * If the thread subsequently performs an operation that makes it unready,
542 * it will be context switched out in the normal manner. When the thread
543 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500544 *
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500546 *
Allan Stephensc98da842016-11-11 15:45:03 -0500547 * @note k_sched_lock() and k_sched_unlock() should normally be used
548 * when the operation being performed can be safely interrupted by ISRs.
549 * However, if the amount of processing involved is very small, better
550 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500551 *
552 * @return N/A
553 */
554extern void k_sched_lock(void);
555
Allan Stephensc98da842016-11-11 15:45:03 -0500556/**
557 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500558 *
Allan Stephensc98da842016-11-11 15:45:03 -0500559 * This routine reverses the effect of a previous call to k_sched_lock().
560 * A thread must call the routine once for each time it called k_sched_lock()
561 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500562 *
563 * @return N/A
564 */
565extern void k_sched_unlock(void);
566
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400567/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500568 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500570 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * Custom data is not used by the kernel itself, and is freely available
573 * for a thread to use as it sees fit. It can be used as a framework
574 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500576 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577 *
578 * @return N/A
579 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400580extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400581
582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500585 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500587 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400589extern void *k_thread_custom_data_get(void);
590
591/**
Allan Stephensc98da842016-11-11 15:45:03 -0500592 * @} end addtogroup thread_apis
593 */
594
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400595#include <sys_clock.h>
596
Allan Stephensc2f15a42016-11-17 12:24:22 -0500597/**
598 * @addtogroup clock_apis
599 * @{
600 */
601
602/**
603 * @brief Generate null timeout delay.
604 *
605 * This macro generates a timeout delay that that instructs a kernel API
606 * not to wait if the requested operation cannot be performed immediately.
607 *
608 * @return Timeout delay value.
609 */
610#define K_NO_WAIT 0
611
612/**
613 * @brief Generate timeout delay from milliseconds.
614 *
615 * This macro generates a timeout delay that that instructs a kernel API
616 * to wait up to @a ms milliseconds to perform the requested operation.
617 *
618 * @param ms Duration in milliseconds.
619 *
620 * @return Timeout delay value.
621 */
Johan Hedberg14471692016-11-13 10:52:15 +0200622#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500623
624/**
625 * @brief Generate timeout delay from seconds.
626 *
627 * This macro generates a timeout delay that that instructs a kernel API
628 * to wait up to @a s seconds to perform the requested operation.
629 *
630 * @param s Duration in seconds.
631 *
632 * @return Timeout delay value.
633 */
Johan Hedberg14471692016-11-13 10:52:15 +0200634#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500635
636/**
637 * @brief Generate timeout delay from minutes.
638 *
639 * This macro generates a timeout delay that that instructs a kernel API
640 * to wait up to @a m minutes to perform the requested operation.
641 *
642 * @param m Duration in minutes.
643 *
644 * @return Timeout delay value.
645 */
Johan Hedberg14471692016-11-13 10:52:15 +0200646#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500647
648/**
649 * @brief Generate timeout delay from hours.
650 *
651 * This macro generates a timeout delay that that instructs a kernel API
652 * to wait up to @a h hours to perform the requested operation.
653 *
654 * @param h Duration in hours.
655 *
656 * @return Timeout delay value.
657 */
Johan Hedberg14471692016-11-13 10:52:15 +0200658#define K_HOURS(h) K_MINUTES((h) * 60)
659
Allan Stephensc98da842016-11-11 15:45:03 -0500660/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500661 * @brief Generate infinite timeout delay.
662 *
663 * This macro generates a timeout delay that that instructs a kernel API
664 * to wait as long as necessary to perform the requested operation.
665 *
666 * @return Timeout delay value.
667 */
668#define K_FOREVER (-1)
669
670/**
671 * @} end addtogroup clock_apis
672 */
673
674/**
Allan Stephensc98da842016-11-11 15:45:03 -0500675 * @cond INTERNAL_HIDDEN
676 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400677
Benjamin Walsh62092182016-12-20 14:39:08 -0500678/* kernel clocks */
679
680#if (sys_clock_ticks_per_sec == 1000) || \
681 (sys_clock_ticks_per_sec == 500) || \
682 (sys_clock_ticks_per_sec == 250) || \
683 (sys_clock_ticks_per_sec == 125) || \
684 (sys_clock_ticks_per_sec == 100) || \
685 (sys_clock_ticks_per_sec == 50) || \
686 (sys_clock_ticks_per_sec == 25) || \
687 (sys_clock_ticks_per_sec == 20) || \
688 (sys_clock_ticks_per_sec == 10) || \
689 (sys_clock_ticks_per_sec == 1)
690
691 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
692#else
693 /* yields horrible 64-bit math on many architectures: try to avoid */
694 #define _NON_OPTIMIZED_TICKS_PER_SEC
695#endif
696
697#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
698extern int32_t _ms_to_ticks(int32_t ms);
699#else
700static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
701{
702 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
703}
704#endif
705
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500706/* added tick needed to account for tick in progress */
707#define _TICK_ALIGN 1
708
Benjamin Walsh62092182016-12-20 14:39:08 -0500709static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400710{
Benjamin Walsh62092182016-12-20 14:39:08 -0500711#ifdef CONFIG_SYS_CLOCK_EXISTS
712
713#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400714 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400715#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500716 return (uint64_t)ticks * _ms_per_tick;
717#endif
718
719#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400720 __ASSERT(ticks == 0, "");
721 return 0;
722#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400723}
724
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400725/* timeouts */
726
727struct _timeout;
728typedef void (*_timeout_func_t)(struct _timeout *t);
729
730struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500731 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400732 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400733 sys_dlist_t *wait_q;
734 int32_t delta_ticks_from_prev;
735 _timeout_func_t func;
736};
737
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200738extern int32_t _timeout_remaining_get(struct _timeout *timeout);
739
Allan Stephensc98da842016-11-11 15:45:03 -0500740/**
741 * INTERNAL_HIDDEN @endcond
742 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500743
Allan Stephensc98da842016-11-11 15:45:03 -0500744/**
745 * @cond INTERNAL_HIDDEN
746 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400747
748struct k_timer {
749 /*
750 * _timeout structure must be first here if we want to use
751 * dynamic timer allocation. timeout.node is used in the double-linked
752 * list of free timers
753 */
754 struct _timeout timeout;
755
Allan Stephens45bfa372016-10-12 12:39:42 -0500756 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400757 _wait_q_t wait_q;
758
759 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500760 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400761
762 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500763 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764
765 /* timer period */
766 int32_t period;
767
Allan Stephens45bfa372016-10-12 12:39:42 -0500768 /* timer status */
769 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400770
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500771 /* user-specific data, also used to support legacy features */
772 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400773
Anas Nashif2f203c22016-12-18 06:57:45 -0500774 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775};
776
Allan Stephens1342adb2016-11-03 13:54:53 -0500777#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500779 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500780 .timeout.wait_q = NULL, \
781 .timeout.thread = NULL, \
782 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400783 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500784 .expiry_fn = expiry, \
785 .stop_fn = stop, \
786 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500787 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500788 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400789 }
790
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791/**
Allan Stephensc98da842016-11-11 15:45:03 -0500792 * INTERNAL_HIDDEN @endcond
793 */
794
795/**
796 * @defgroup timer_apis Timer APIs
797 * @ingroup kernel_apis
798 * @{
799 */
800
801/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500802 * @typedef k_timer_expiry_t
803 * @brief Timer expiry function type.
804 *
805 * A timer's expiry function is executed by the system clock interrupt handler
806 * each time the timer expires. The expiry function is optional, and is only
807 * invoked if the timer has been initialized with one.
808 *
809 * @param timer Address of timer.
810 *
811 * @return N/A
812 */
813typedef void (*k_timer_expiry_t)(struct k_timer *timer);
814
815/**
816 * @typedef k_timer_stop_t
817 * @brief Timer stop function type.
818 *
819 * A timer's stop function is executed if the timer is stopped prematurely.
820 * The function runs in the context of the thread that stops the timer.
821 * The stop function is optional, and is only invoked if the timer has been
822 * initialized with one.
823 *
824 * @param timer Address of timer.
825 *
826 * @return N/A
827 */
828typedef void (*k_timer_stop_t)(struct k_timer *timer);
829
830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500835 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
837 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * @param expiry_fn Function to invoke each time the timer expires.
839 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500841#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500842 struct k_timer name \
843 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500844 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400845
Allan Stephens45bfa372016-10-12 12:39:42 -0500846/**
847 * @brief Initialize a timer.
848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500850 *
851 * @param timer Address of timer.
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.
Allan Stephens45bfa372016-10-12 12:39:42 -0500854 *
855 * @return N/A
856 */
857extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500858 k_timer_expiry_t expiry_fn,
859 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700860
Allan Stephens45bfa372016-10-12 12:39:42 -0500861/**
862 * @brief Start a timer.
863 *
864 * This routine starts a timer, and resets its status to zero. The timer
865 * begins counting down using the specified duration and period values.
866 *
867 * Attempting to start a timer that is already running is permitted.
868 * The timer's status is reset to zero and the timer begins counting down
869 * using the new duration and period values.
870 *
871 * @param timer Address of timer.
872 * @param duration Initial timer duration (in milliseconds).
873 * @param period Timer period (in milliseconds).
874 *
875 * @return N/A
876 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400877extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500878 int32_t duration, int32_t period);
879
880/**
881 * @brief Stop a timer.
882 *
883 * This routine stops a running timer prematurely. The timer's stop function,
884 * if one exists, is invoked by the caller.
885 *
886 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500888 *
889 * @param timer Address of timer.
890 *
891 * @return N/A
892 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400893extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500894
895/**
896 * @brief Read timer status.
897 *
898 * This routine reads the timer's status, which indicates the number of times
899 * it has expired since its status was last read.
900 *
901 * Calling this routine resets the timer's status to zero.
902 *
903 * @param timer Address of timer.
904 *
905 * @return Timer status.
906 */
907extern uint32_t k_timer_status_get(struct k_timer *timer);
908
909/**
910 * @brief Synchronize thread to timer expiration.
911 *
912 * This routine blocks the calling thread until the timer's status is non-zero
913 * (indicating that it has expired at least once since it was last examined)
914 * or the timer is stopped. If the timer status is already non-zero,
915 * or the timer is already stopped, the caller continues without waiting.
916 *
917 * Calling this routine resets the timer's status to zero.
918 *
919 * This routine must not be used by interrupt handlers, since they are not
920 * allowed to block.
921 *
922 * @param timer Address of timer.
923 *
924 * @return Timer status.
925 */
926extern uint32_t k_timer_status_sync(struct k_timer *timer);
927
928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500930 *
931 * This routine computes the (approximate) time remaining before a running
932 * timer next expires. If the timer is not running, it returns zero.
933 *
934 * @param timer Address of timer.
935 *
936 * @return Remaining time (in milliseconds).
937 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200938static inline int32_t k_timer_remaining_get(struct k_timer *timer)
939{
940 return _timeout_remaining_get(&timer->timeout);
941}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400942
Allan Stephensc98da842016-11-11 15:45:03 -0500943/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500944 * @brief Associate user-specific data with a timer.
945 *
946 * This routine records the @a user_data with the @a timer, to be retrieved
947 * later.
948 *
949 * It can be used e.g. in a timer handler shared across multiple subsystems to
950 * retrieve data specific to the subsystem this timer is associated with.
951 *
952 * @param timer Address of timer.
953 * @param user_data User data to associate with the timer.
954 *
955 * @return N/A
956 */
957static inline void k_timer_user_data_set(struct k_timer *timer,
958 void *user_data)
959{
960 timer->user_data = user_data;
961}
962
963/**
964 * @brief Retrieve the user-specific data from a timer.
965 *
966 * @param timer Address of timer.
967 *
968 * @return The user data.
969 */
970static inline void *k_timer_user_data_get(struct k_timer *timer)
971{
972 return timer->user_data;
973}
974
975/**
Allan Stephensc98da842016-11-11 15:45:03 -0500976 * @} end defgroup timer_apis
977 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400978
Allan Stephensc98da842016-11-11 15:45:03 -0500979/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500980 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500981 * @{
982 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500983
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * This routine returns the elapsed time since the system booted,
988 * in milliseconds.
989 *
990 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400991 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400993
994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * This routine returns the lower 32-bits of the elapsed time since the system
998 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * This routine can be more efficient than k_uptime_get(), as it reduces the
1001 * need for interrupt locking and 64-bit math. However, the 32-bit result
1002 * cannot hold a system uptime time larger than approximately 50 days, so the
1003 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001006 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001007extern uint32_t k_uptime_get_32(void);
1008
1009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * This routine computes the elapsed time between the current system uptime
1013 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @param reftime Pointer to a reference time, which is updated to the current
1016 * uptime upon return.
1017 *
1018 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001021
1022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * This routine computes the elapsed time between the current system uptime
1026 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1029 * need for interrupt locking and 64-bit math. However, the 32-bit result
1030 * cannot hold an elapsed time larger than approximately 50 days, so the
1031 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * @param reftime Pointer to a reference time, which is updated to the current
1034 * uptime upon return.
1035 *
1036 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001037 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001038extern uint32_t k_uptime_delta_32(int64_t *reftime);
1039
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001040/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * This routine returns the current time, as measured by the system's hardware
1044 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048extern uint32_t k_cycle_get_32(void);
1049
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001050/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001051 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052 */
1053
Allan Stephensc98da842016-11-11 15:45:03 -05001054/**
1055 * @cond INTERNAL_HIDDEN
1056 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001057
1058struct k_fifo {
1059 _wait_q_t wait_q;
1060 sys_slist_t data_q;
1061
Anas Nashif2f203c22016-12-18 06:57:45 -05001062 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063};
1064
Allan Stephensc98da842016-11-11 15:45:03 -05001065#define K_FIFO_INITIALIZER(obj) \
1066 { \
1067 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1068 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001069 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001070 }
1071
1072/**
1073 * INTERNAL_HIDDEN @endcond
1074 */
1075
1076/**
1077 * @defgroup fifo_apis Fifo APIs
1078 * @ingroup kernel_apis
1079 * @{
1080 */
1081
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001085 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
1089 * @return N/A
1090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001091extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092
1093/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001094 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001096 * This routine adds a data item to @a fifo. A fifo data item must be
1097 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1098 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * @note Can be called by ISRs.
1101 *
1102 * @param fifo Address of the fifo.
1103 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 *
1105 * @return N/A
1106 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108
1109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * This routine adds a list of data items to @a fifo in one operation.
1113 * The data items must be in a singly-linked list, with the first 32 bits
1114 * each data item pointing to the next data item; the list must be
1115 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * @param fifo Address of the fifo.
1120 * @param head Pointer to first node in singly-linked list.
1121 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
1123 * @return N/A
1124 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001125extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126
1127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001130 * This routine adds a list of data items to @a fifo in one operation.
1131 * The data items must be in a singly-linked list implemented using a
1132 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133 * and must be re-initialized via sys_slist_init().
1134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @note Can be called by ISRs.
1136 *
1137 * @param fifo Address of the fifo.
1138 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
1140 * @return N/A
1141 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143
1144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001147 * This routine removes a data item from @a fifo in a "first in, first out"
1148 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1151 *
1152 * @param fifo Address of the fifo.
1153 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 * or one of the special values K_NO_WAIT and K_FOREVER.
1155 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001156 * @return Address of the data item if successful; NULL if returned
1157 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1160
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001162 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001166 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001171 struct k_fifo name \
1172 __in_section(_k_fifo, static, name) = \
1173 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174
Allan Stephensc98da842016-11-11 15:45:03 -05001175/**
1176 * @} end defgroup fifo_apis
1177 */
1178
1179/**
1180 * @cond INTERNAL_HIDDEN
1181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182
1183struct k_lifo {
1184 _wait_q_t wait_q;
1185 void *list;
1186
Anas Nashif2f203c22016-12-18 06:57:45 -05001187 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188};
1189
Allan Stephensc98da842016-11-11 15:45:03 -05001190#define K_LIFO_INITIALIZER(obj) \
1191 { \
1192 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1193 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001194 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001195 }
1196
1197/**
1198 * INTERNAL_HIDDEN @endcond
1199 */
1200
1201/**
1202 * @defgroup lifo_apis Lifo APIs
1203 * @ingroup kernel_apis
1204 * @{
1205 */
1206
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001208 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001210 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001212 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213 *
1214 * @return N/A
1215 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001216extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001217
1218/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001219 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * This routine adds a data item to @a lifo. A lifo data item must be
1222 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1223 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * @note Can be called by ISRs.
1226 *
1227 * @param lifo Address of the lifo.
1228 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 *
1230 * @return N/A
1231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233
1234/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * This routine removes a data item from @a lifo in a "last in, first out"
1238 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1241 *
1242 * @param lifo Address of the lifo.
1243 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 * or one of the special values K_NO_WAIT and K_FOREVER.
1245 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001246 * @return Address of the data item if successful; NULL if returned
1247 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001249extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1250
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001256 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001258 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001259 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001260#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001261 struct k_lifo name \
1262 __in_section(_k_lifo, static, name) = \
1263 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001264
Allan Stephensc98da842016-11-11 15:45:03 -05001265/**
1266 * @} end defgroup lifo_apis
1267 */
1268
1269/**
1270 * @cond INTERNAL_HIDDEN
1271 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001272
1273struct k_stack {
1274 _wait_q_t wait_q;
1275 uint32_t *base, *next, *top;
1276
Anas Nashif2f203c22016-12-18 06:57:45 -05001277 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278};
1279
Allan Stephensc98da842016-11-11 15:45:03 -05001280#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1281 { \
1282 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1283 .base = stack_buffer, \
1284 .next = stack_buffer, \
1285 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001286 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001287 }
1288
1289/**
1290 * INTERNAL_HIDDEN @endcond
1291 */
1292
1293/**
1294 * @defgroup stack_apis Stack APIs
1295 * @ingroup kernel_apis
1296 * @{
1297 */
1298
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001299/**
1300 * @brief Initialize a stack.
1301 *
1302 * This routine initializes a stack object, prior to its first use.
1303 *
1304 * @param stack Address of the stack.
1305 * @param buffer Address of array used to hold stacked values.
1306 * @param num_entries Maximum number of values that can be stacked.
1307 *
1308 * @return N/A
1309 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001310extern void k_stack_init(struct k_stack *stack,
1311 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001312
1313/**
1314 * @brief Push an element onto a stack.
1315 *
1316 * This routine adds a 32-bit value @a data to @a stack.
1317 *
1318 * @note Can be called by ISRs.
1319 *
1320 * @param stack Address of the stack.
1321 * @param data Value to push onto the stack.
1322 *
1323 * @return N/A
1324 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326
1327/**
1328 * @brief Pop an element from a stack.
1329 *
1330 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1331 * manner and stores the value in @a data.
1332 *
1333 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1334 *
1335 * @param stack Address of the stack.
1336 * @param data Address of area to hold the value popped from the stack.
1337 * @param timeout Waiting period to obtain a value (in milliseconds),
1338 * or one of the special values K_NO_WAIT and K_FOREVER.
1339 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001340 * @retval 0 Element popped from stack.
1341 * @retval -EBUSY Returned without waiting.
1342 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1345
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001347 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001350 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001351 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * @param name Name of the stack.
1354 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001355 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001356#define K_STACK_DEFINE(name, stack_num_entries) \
1357 uint32_t __noinit \
1358 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001359 struct k_stack name \
1360 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001361 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1362 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001363
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364/**
Allan Stephensc98da842016-11-11 15:45:03 -05001365 * @} end defgroup stack_apis
1366 */
1367
Allan Stephens6bba9b02016-11-16 14:56:54 -05001368struct k_work;
1369
Allan Stephensc98da842016-11-11 15:45:03 -05001370/**
1371 * @defgroup workqueue_apis Workqueue Thread APIs
1372 * @ingroup kernel_apis
1373 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374 */
1375
Allan Stephens6bba9b02016-11-16 14:56:54 -05001376/**
1377 * @typedef k_work_handler_t
1378 * @brief Work item handler function type.
1379 *
1380 * A work item's handler function is executed by a workqueue's thread
1381 * when the work item is processed by the workqueue.
1382 *
1383 * @param work Address of the work item.
1384 *
1385 * @return N/A
1386 */
1387typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388
1389/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001390 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001392
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393struct k_work_q {
1394 struct k_fifo fifo;
1395};
1396
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001398 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399};
1400
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401struct k_work {
1402 void *_reserved; /* Used by k_fifo implementation. */
1403 k_work_handler_t handler;
1404 atomic_t flags[1];
1405};
1406
Allan Stephens6bba9b02016-11-16 14:56:54 -05001407struct k_delayed_work {
1408 struct k_work work;
1409 struct _timeout timeout;
1410 struct k_work_q *work_q;
1411};
1412
1413extern struct k_work_q k_sys_work_q;
1414
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001416 * INTERNAL_HIDDEN @endcond
1417 */
1418
1419/**
1420 * @brief Initialize a statically-defined work item.
1421 *
1422 * This macro can be used to initialize a statically-defined workqueue work
1423 * item, prior to its first use. For example,
1424 *
1425 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1426 *
1427 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428 */
1429#define K_WORK_INITIALIZER(work_handler) \
1430 { \
1431 ._reserved = NULL, \
1432 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001433 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 }
1435
1436/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001437 * @brief Initialize a work item.
1438 *
1439 * This routine initializes a workqueue work item, prior to its first use.
1440 *
1441 * @param work Address of work item.
1442 * @param handler Function to invoke each time work item is processed.
1443 *
1444 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445 */
1446static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1447{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001448 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001449 work->handler = handler;
1450}
1451
1452/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001453 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001454 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001455 * This routine submits work item @a work to be processed by workqueue
1456 * @a work_q. If the work item is already pending in the workqueue's queue
1457 * as a result of an earlier submission, this routine has no effect on the
1458 * work item. If the work item has already been processed, or is currently
1459 * being processed, its work is considered complete and the work item can be
1460 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001461 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001462 * @warning
1463 * A submitted work item must not be modified until it has been processed
1464 * by the workqueue.
1465 *
1466 * @note Can be called by ISRs.
1467 *
1468 * @param work_q Address of workqueue.
1469 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001470 *
1471 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001472 */
1473static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1474 struct k_work *work)
1475{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001476 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001477 k_fifo_put(&work_q->fifo, work);
1478 }
1479}
1480
1481/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001482 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001484 * This routine indicates if work item @a work is pending in a workqueue's
1485 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001486 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001487 * @note Can be called by ISRs.
1488 *
1489 * @param work Address of work item.
1490 *
1491 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001492 */
1493static inline int k_work_pending(struct k_work *work)
1494{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001495 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001496}
1497
1498/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001499 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001501 * This routine starts workqueue @a work_q. The workqueue spawns its work
1502 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001503 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001504 * @param work_q Address of workqueue.
1505 * @param stack Pointer to work queue thread's stack space.
1506 * @param stack_size Size of the work queue thread's stack (in bytes).
1507 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 *
1509 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001510 */
Allan Stephens904cf972016-10-07 13:59:23 -05001511extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001512 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001513
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001514/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001515 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001516 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001517 * This routine initializes a workqueue delayed work item, prior to
1518 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001519 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001520 * @param work Address of delayed work item.
1521 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001522 *
1523 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001524 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001525extern void k_delayed_work_init(struct k_delayed_work *work,
1526 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001527
1528/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001529 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001531 * This routine schedules work item @a work to be processed by workqueue
1532 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1533 * an asychronous countdown for the work item and then returns to the caller.
1534 * Only when the countdown completes is the work item actually submitted to
1535 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001537 * Submitting a previously submitted delayed work item that is still
1538 * counting down cancels the existing submission and restarts the countdown
1539 * using the new delay. If the work item is currently pending on the
1540 * workqueue's queue because the countdown has completed it is too late to
1541 * resubmit the item, and resubmission fails without impacting the work item.
1542 * If the work item has already been processed, or is currently being processed,
1543 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001545 * @warning
1546 * A delayed work item must not be modified until it has been processed
1547 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001548 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001549 * @note Can be called by ISRs.
1550 *
1551 * @param work_q Address of workqueue.
1552 * @param work Address of delayed work item.
1553 * @param delay Delay before submitting the work item (in milliseconds).
1554 *
1555 * @retval 0 Work item countdown started.
1556 * @retval -EINPROGRESS Work item is already pending.
1557 * @retval -EINVAL Work item is being processed or has completed its work.
1558 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001560extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1561 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001562 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001563
1564/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001565 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001566 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001567 * This routine cancels the submission of delayed work item @a work.
1568 * A delayed work item can only be cancelled while its countdown is still
1569 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001571 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001573 * @param work Address of delayed work item.
1574 *
1575 * @retval 0 Work item countdown cancelled.
1576 * @retval -EINPROGRESS Work item is already pending.
1577 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001578 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001579extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001580
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001581/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001582 * @brief Submit a work item to the system workqueue.
1583 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001584 * This routine submits work item @a work to be processed by the system
1585 * workqueue. If the work item is already pending in the workqueue's queue
1586 * as a result of an earlier submission, this routine has no effect on the
1587 * work item. If the work item has already been processed, or is currently
1588 * being processed, its work is considered complete and the work item can be
1589 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001590 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001591 * @warning
1592 * Work items submitted to the system workqueue should avoid using handlers
1593 * that block or yield since this may prevent the system workqueue from
1594 * processing other work items in a timely manner.
1595 *
1596 * @note Can be called by ISRs.
1597 *
1598 * @param work Address of work item.
1599 *
1600 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601 */
1602static inline void k_work_submit(struct k_work *work)
1603{
1604 k_work_submit_to_queue(&k_sys_work_q, work);
1605}
1606
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001607/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001608 * @brief Submit a delayed work item to the system workqueue.
1609 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001610 * This routine schedules work item @a work to be processed by the system
1611 * workqueue after a delay of @a delay milliseconds. The routine initiates
1612 * an asychronous countdown for the work item and then returns to the caller.
1613 * Only when the countdown completes is the work item actually submitted to
1614 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001616 * Submitting a previously submitted delayed work item that is still
1617 * counting down cancels the existing submission and restarts the countdown
1618 * using the new delay. If the work item is currently pending on the
1619 * workqueue's queue because the countdown has completed it is too late to
1620 * resubmit the item, and resubmission fails without impacting the work item.
1621 * If the work item has already been processed, or is currently being processed,
1622 * its work is considered complete and the work item can be resubmitted.
1623 *
1624 * @warning
1625 * Work items submitted to the system workqueue should avoid using handlers
1626 * that block or yield since this may prevent the system workqueue from
1627 * processing other work items in a timely manner.
1628 *
1629 * @note Can be called by ISRs.
1630 *
1631 * @param work Address of delayed work item.
1632 * @param delay Delay before submitting the work item (in milliseconds).
1633 *
1634 * @retval 0 Work item countdown started.
1635 * @retval -EINPROGRESS Work item is already pending.
1636 * @retval -EINVAL Work item is being processed or has completed its work.
1637 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001638 */
1639static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001640 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001641{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001642 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643}
1644
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001645/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001646 * @brief Get time remaining before a delayed work gets scheduled.
1647 *
1648 * This routine computes the (approximate) time remaining before a
1649 * delayed work gets executed. If the delayed work is not waiting to be
1650 * schedules, it returns zero.
1651 *
1652 * @param work Delayed work item.
1653 *
1654 * @return Remaining time (in milliseconds).
1655 */
1656static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1657{
1658 return _timeout_remaining_get(&work->timeout);
1659}
1660
1661/**
Allan Stephensc98da842016-11-11 15:45:03 -05001662 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663 */
1664
Allan Stephensc98da842016-11-11 15:45:03 -05001665/**
1666 * @cond INTERNAL_HIDDEN
1667 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001668
1669struct k_mutex {
1670 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001671 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001672 uint32_t lock_count;
1673 int owner_orig_prio;
1674#ifdef CONFIG_OBJECT_MONITOR
1675 int num_lock_state_changes;
1676 int num_conflicts;
1677#endif
1678
Anas Nashif2f203c22016-12-18 06:57:45 -05001679 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001680};
1681
1682#ifdef CONFIG_OBJECT_MONITOR
1683#define _MUTEX_INIT_OBJECT_MONITOR \
1684 .num_lock_state_changes = 0, .num_conflicts = 0,
1685#else
1686#define _MUTEX_INIT_OBJECT_MONITOR
1687#endif
1688
1689#define K_MUTEX_INITIALIZER(obj) \
1690 { \
1691 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1692 .owner = NULL, \
1693 .lock_count = 0, \
1694 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1695 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001696 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001697 }
1698
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001699/**
Allan Stephensc98da842016-11-11 15:45:03 -05001700 * INTERNAL_HIDDEN @endcond
1701 */
1702
1703/**
1704 * @defgroup mutex_apis Mutex APIs
1705 * @ingroup kernel_apis
1706 * @{
1707 */
1708
1709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001714 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001719 struct k_mutex name \
1720 __in_section(_k_mutex, static, name) = \
1721 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001722
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001724 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * Upon completion, the mutex is available and does not have an owner.
1729 *
1730 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
1732 * @return N/A
1733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735
1736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * This routine locks @a mutex. If the mutex is locked by another thread,
1740 * the calling thread waits until the mutex becomes available or until
1741 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * A thread is permitted to lock a mutex it has already locked. The operation
1744 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 * @param mutex Address of the mutex.
1747 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 * or one of the special values K_NO_WAIT and K_FOREVER.
1749 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001750 * @retval 0 Mutex locked.
1751 * @retval -EBUSY Returned without waiting.
1752 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755
1756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * This routine unlocks @a mutex. The mutex must already be locked by the
1760 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761 *
1762 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 * thread.
1765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 *
1768 * @return N/A
1769 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001770extern void k_mutex_unlock(struct k_mutex *mutex);
1771
Allan Stephensc98da842016-11-11 15:45:03 -05001772/**
1773 * @} end defgroup mutex_apis
1774 */
1775
1776/**
1777 * @cond INTERNAL_HIDDEN
1778 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001779
1780struct k_sem {
1781 _wait_q_t wait_q;
1782 unsigned int count;
1783 unsigned int limit;
1784
Anas Nashif2f203c22016-12-18 06:57:45 -05001785 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001786};
1787
Allan Stephensc98da842016-11-11 15:45:03 -05001788#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1789 { \
1790 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1791 .count = initial_count, \
1792 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001793 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001794 }
1795
1796/**
1797 * INTERNAL_HIDDEN @endcond
1798 */
1799
1800/**
1801 * @defgroup semaphore_apis Semaphore APIs
1802 * @ingroup kernel_apis
1803 * @{
1804 */
1805
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001809 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001811 * @param sem Address of the semaphore.
1812 * @param initial_count Initial semaphore count.
1813 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001814 *
1815 * @return N/A
1816 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001817extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1818 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001819
1820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1826 *
1827 * @param sem Address of the semaphore.
1828 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001829 * or one of the special values K_NO_WAIT and K_FOREVER.
1830 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001831 * @note When porting code from the nanokernel legacy API to the new API, be
1832 * careful with the return value of this function. The return value is the
1833 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1834 * non-zero means failure, while the nano_sem_take family returns 1 for success
1835 * and 0 for failure.
1836 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001837 * @retval 0 Semaphore taken.
1838 * @retval -EBUSY Returned without waiting.
1839 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001841extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001842
1843/**
1844 * @brief Give a semaphore.
1845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001846 * This routine gives @a sem, unless the semaphore is already at its maximum
1847 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001851 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001852 *
1853 * @return N/A
1854 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855extern void k_sem_give(struct k_sem *sem);
1856
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001857/**
1858 * @brief Reset a semaphore's count to zero.
1859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001863 *
1864 * @return N/A
1865 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001866static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001867{
1868 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001869}
1870
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001871/**
1872 * @brief Get a semaphore's count.
1873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001874 * This routine returns the current count of @a sem.
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 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001879 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001880static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001881{
1882 return sem->count;
1883}
1884
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001889 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001890 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @param name Name of the semaphore.
1893 * @param initial_count Initial semaphore count.
1894 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001895 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001897 struct k_sem name \
1898 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001899 K_SEM_INITIALIZER(name, initial_count, count_limit)
1900
Allan Stephensc98da842016-11-11 15:45:03 -05001901/**
1902 * @} end defgroup semaphore_apis
1903 */
1904
1905/**
1906 * @defgroup alert_apis Alert APIs
1907 * @ingroup kernel_apis
1908 * @{
1909 */
1910
Allan Stephens5eceb852016-11-16 10:16:30 -05001911/**
1912 * @typedef k_alert_handler_t
1913 * @brief Alert handler function type.
1914 *
1915 * An alert's alert handler function is invoked by the system workqueue
1916 * when the alert is signalled. The alert handler function is optional,
1917 * and is only invoked if the alert has been initialized with one.
1918 *
1919 * @param alert Address of the alert.
1920 *
1921 * @return 0 if alert has been consumed; non-zero if alert should pend.
1922 */
1923typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001924
1925/**
1926 * @} end defgroup alert_apis
1927 */
1928
1929/**
1930 * @cond INTERNAL_HIDDEN
1931 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001933#define K_ALERT_DEFAULT NULL
1934#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001936struct k_alert {
1937 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938 atomic_t send_count;
1939 struct k_work work_item;
1940 struct k_sem sem;
1941
Anas Nashif2f203c22016-12-18 06:57:45 -05001942 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001943};
1944
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001945extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001947#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001949 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001951 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001952 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001953 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954 }
1955
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956/**
Allan Stephensc98da842016-11-11 15:45:03 -05001957 * INTERNAL_HIDDEN @endcond
1958 */
1959
1960/**
1961 * @addtogroup alert_apis
1962 * @{
1963 */
1964
1965/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001966 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001968 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001970 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * @param name Name of the alert.
1973 * @param alert_handler Action to take when alert is sent. Specify either
1974 * the address of a function to be invoked by the system workqueue
1975 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1976 * K_ALERT_DEFAULT (which causes the alert to pend).
1977 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001979#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001980 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001981 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001982 K_ALERT_INITIALIZER(name, alert_handler, \
1983 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001990 * @param alert Address of the alert.
1991 * @param handler Action to take when alert is sent. Specify either the address
1992 * of a function to be invoked by the system workqueue thread,
1993 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1994 * K_ALERT_DEFAULT (which causes the alert to pend).
1995 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996 *
1997 * @return N/A
1998 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001999extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2000 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001
2002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002005 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2008 *
2009 * @param alert Address of the alert.
2010 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 * or one of the special values K_NO_WAIT and K_FOREVER.
2012 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002013 * @retval 0 Alert received.
2014 * @retval -EBUSY Returned without waiting.
2015 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002017extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018
2019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * This routine signals @a alert. The action specified for @a alert will
2023 * be taken, which may trigger the execution of an alert handler function
2024 * and/or cause the alert to pend (assuming the alert has not reached its
2025 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027 * @note Can be called by ISRs.
2028 *
2029 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 *
2031 * @return N/A
2032 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002033extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034
2035/**
Allan Stephensc98da842016-11-11 15:45:03 -05002036 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037 */
2038
Allan Stephensc98da842016-11-11 15:45:03 -05002039/**
2040 * @cond INTERNAL_HIDDEN
2041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042
2043struct k_msgq {
2044 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002045 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046 uint32_t max_msgs;
2047 char *buffer_start;
2048 char *buffer_end;
2049 char *read_ptr;
2050 char *write_ptr;
2051 uint32_t used_msgs;
2052
Anas Nashif2f203c22016-12-18 06:57:45 -05002053 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002054};
2055
Peter Mitsis1da807e2016-10-06 11:36:59 -04002056#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057 { \
2058 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002059 .max_msgs = q_max_msgs, \
2060 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002062 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063 .read_ptr = q_buffer, \
2064 .write_ptr = q_buffer, \
2065 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002066 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002067 }
2068
Peter Mitsis1da807e2016-10-06 11:36:59 -04002069/**
Allan Stephensc98da842016-11-11 15:45:03 -05002070 * INTERNAL_HIDDEN @endcond
2071 */
2072
2073/**
2074 * @defgroup msgq_apis Message Queue APIs
2075 * @ingroup kernel_apis
2076 * @{
2077 */
2078
2079/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2083 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002084 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2085 * message is similarly aligned to this boundary, @a q_msg_size must also be
2086 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * The message queue can be accessed outside the module where it is defined
2089 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002091 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * @param q_name Name of the message queue.
2094 * @param q_msg_size Message size (in bytes).
2095 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002096 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002097 */
2098#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2099 static char __noinit __aligned(q_align) \
2100 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002101 struct k_msgq q_name \
2102 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002103 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2104 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002105
Peter Mitsisd7a37502016-10-13 11:37:40 -04002106/**
2107 * @brief Initialize a message queue.
2108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * This routine initializes a message queue object, prior to its first use.
2110 *
Allan Stephensda827222016-11-09 14:23:58 -06002111 * The message queue's ring buffer must contain space for @a max_msgs messages,
2112 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2113 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2114 * that each message is similarly aligned to this boundary, @a q_msg_size
2115 * must also be a multiple of N.
2116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * @param q Address of the message queue.
2118 * @param buffer Pointer to ring buffer that holds queued messages.
2119 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002120 * @param max_msgs Maximum number of messages that can be queued.
2121 *
2122 * @return N/A
2123 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002124extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002125 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002126
2127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002131 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002132 * @note Can be called by ISRs.
2133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002134 * @param q Address of the message queue.
2135 * @param data Pointer to the message.
2136 * @param timeout Waiting period to add the message (in milliseconds),
2137 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002138 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002139 * @retval 0 Message sent.
2140 * @retval -ENOMSG Returned without waiting or queue purged.
2141 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002142 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002144
2145/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * This routine receives a message from message queue @a q in a "first in,
2149 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002150 *
Allan Stephensc98da842016-11-11 15:45:03 -05002151 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @param q Address of the message queue.
2154 * @param data Address of area to hold the received message.
2155 * @param timeout Waiting period to receive the message (in milliseconds),
2156 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002157 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002158 * @retval 0 Message received.
2159 * @retval -ENOMSG Returned without waiting.
2160 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002161 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002163
2164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * This routine discards all unreceived messages in a message queue's ring
2168 * buffer. Any threads that are blocked waiting to send a message to the
2169 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002171 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002172 *
2173 * @return N/A
2174 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175extern void k_msgq_purge(struct k_msgq *q);
2176
Peter Mitsis67be2492016-10-07 11:44:34 -04002177/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * This routine returns the number of unused entries in a message queue's
2181 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * @param q Address of the message queue.
2184 *
2185 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002186 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002187static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002188{
2189 return q->max_msgs - q->used_msgs;
2190}
2191
Peter Mitsisd7a37502016-10-13 11:37:40 -04002192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002193 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002197 * @param q Address of the message queue.
2198 *
2199 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002200 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002201static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202{
2203 return q->used_msgs;
2204}
2205
Allan Stephensc98da842016-11-11 15:45:03 -05002206/**
2207 * @} end defgroup msgq_apis
2208 */
2209
2210/**
2211 * @defgroup mem_pool_apis Memory Pool APIs
2212 * @ingroup kernel_apis
2213 * @{
2214 */
2215
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002217 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218 void *addr_in_pool;
2219 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002220 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221};
2222
Allan Stephensc98da842016-11-11 15:45:03 -05002223/**
2224 * @} end defgroup mem_pool_apis
2225 */
2226
2227/**
2228 * @defgroup mailbox_apis Mailbox APIs
2229 * @ingroup kernel_apis
2230 * @{
2231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232
2233struct k_mbox_msg {
2234 /** internal use only - needed for legacy API support */
2235 uint32_t _mailbox;
2236 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002237 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 /** application-defined information value */
2239 uint32_t info;
2240 /** sender's message data buffer */
2241 void *tx_data;
2242 /** internal use only - needed for legacy API support */
2243 void *_rx_data;
2244 /** message data block descriptor */
2245 struct k_mem_block tx_block;
2246 /** source thread id */
2247 k_tid_t rx_source_thread;
2248 /** target thread id */
2249 k_tid_t tx_target_thread;
2250 /** internal use only - thread waiting on send (may be a dummy) */
2251 k_tid_t _syncing_thread;
2252#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2253 /** internal use only - semaphore used during asynchronous send */
2254 struct k_sem *_async_sem;
2255#endif
2256};
2257
Allan Stephensc98da842016-11-11 15:45:03 -05002258/**
2259 * @cond INTERNAL_HIDDEN
2260 */
2261
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262struct k_mbox {
2263 _wait_q_t tx_msg_queue;
2264 _wait_q_t rx_msg_queue;
2265
Anas Nashif2f203c22016-12-18 06:57:45 -05002266 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267};
2268
2269#define K_MBOX_INITIALIZER(obj) \
2270 { \
2271 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2272 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002273 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274 }
2275
Peter Mitsis12092702016-10-14 12:57:23 -04002276/**
Allan Stephensc98da842016-11-11 15:45:03 -05002277 * INTERNAL_HIDDEN @endcond
2278 */
2279
2280/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002281 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002285 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002288 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002290 struct k_mbox name \
2291 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292 K_MBOX_INITIALIZER(name) \
2293
Peter Mitsis12092702016-10-14 12:57:23 -04002294/**
2295 * @brief Initialize a mailbox.
2296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * This routine initializes a mailbox object, prior to its first use.
2298 *
2299 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002300 *
2301 * @return N/A
2302 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303extern void k_mbox_init(struct k_mbox *mbox);
2304
Peter Mitsis12092702016-10-14 12:57:23 -04002305/**
2306 * @brief Send a mailbox message in a synchronous manner.
2307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002308 * This routine sends a message to @a mbox and waits for a receiver to both
2309 * receive and process it. The message data may be in a buffer, in a memory
2310 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * @param mbox Address of the mailbox.
2313 * @param tx_msg Address of the transmit message descriptor.
2314 * @param timeout Waiting period for the message to be received (in
2315 * milliseconds), or one of the special values K_NO_WAIT
2316 * and K_FOREVER. Once the message has been received,
2317 * this routine waits as long as necessary for the message
2318 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002319 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002320 * @retval 0 Message sent.
2321 * @retval -ENOMSG Returned without waiting.
2322 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002323 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002324extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002325 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002326
Peter Mitsis12092702016-10-14 12:57:23 -04002327/**
2328 * @brief Send a mailbox message in an asynchronous manner.
2329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * This routine sends a message to @a mbox without waiting for a receiver
2331 * to process it. The message data may be in a buffer, in a memory pool block,
2332 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2333 * will be given when the message has been both received and completely
2334 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * @param mbox Address of the mailbox.
2337 * @param tx_msg Address of the transmit message descriptor.
2338 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002339 *
2340 * @return N/A
2341 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002342extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 struct k_sem *sem);
2344
Peter Mitsis12092702016-10-14 12:57:23 -04002345/**
2346 * @brief Receive a mailbox message.
2347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * This routine receives a message from @a mbox, then optionally retrieves
2349 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * @param mbox Address of the mailbox.
2352 * @param rx_msg Address of the receive message descriptor.
2353 * @param buffer Address of the buffer to receive data, or NULL to defer data
2354 * retrieval and message disposal until later.
2355 * @param timeout Waiting period for a message to be received (in
2356 * milliseconds), or one of the special values K_NO_WAIT
2357 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002358 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002359 * @retval 0 Message received.
2360 * @retval -ENOMSG Returned without waiting.
2361 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002362 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002363extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002365
2366/**
2367 * @brief Retrieve mailbox message data into a buffer.
2368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * This routine completes the processing of a received message by retrieving
2370 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002371 *
2372 * Alternatively, this routine can be used to dispose of a received message
2373 * without retrieving its data.
2374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * @param rx_msg Address of the receive message descriptor.
2376 * @param buffer Address of the buffer to receive data, or NULL to discard
2377 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002378 *
2379 * @return N/A
2380 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002381extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002382
2383/**
2384 * @brief Retrieve mailbox message data into a memory pool block.
2385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002386 * This routine completes the processing of a received message by retrieving
2387 * its data into a memory pool block, then disposing of the message.
2388 * The memory pool block that results from successful retrieval must be
2389 * returned to the pool once the data has been processed, even in cases
2390 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002391 *
2392 * Alternatively, this routine can be used to dispose of a received message
2393 * without retrieving its data. In this case there is no need to return a
2394 * memory pool block to the pool.
2395 *
2396 * This routine allocates a new memory pool block for the data only if the
2397 * data is not already in one. If a new block cannot be allocated, the routine
2398 * returns a failure code and the received message is left unchanged. This
2399 * permits the caller to reattempt data retrieval at a later time or to dispose
2400 * of the received message without retrieving its data.
2401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * @param rx_msg Address of a receive message descriptor.
2403 * @param pool Address of memory pool, or NULL to discard data.
2404 * @param block Address of the area to hold memory pool block info.
2405 * @param timeout Waiting period to wait for a memory pool block (in
2406 * milliseconds), or one of the special values K_NO_WAIT
2407 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002408 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002409 * @retval 0 Data retrieved.
2410 * @retval -ENOMEM Returned without waiting.
2411 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002412 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002413extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002414 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 struct k_mem_block *block, int32_t timeout);
2416
Allan Stephensc98da842016-11-11 15:45:03 -05002417/**
2418 * @} end defgroup mailbox_apis
2419 */
2420
2421/**
2422 * @cond INTERNAL_HIDDEN
2423 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424
2425struct k_pipe {
2426 unsigned char *buffer; /* Pipe buffer: may be NULL */
2427 size_t size; /* Buffer size */
2428 size_t bytes_used; /* # bytes used in buffer */
2429 size_t read_index; /* Where in buffer to read from */
2430 size_t write_index; /* Where in buffer to write */
2431
2432 struct {
2433 _wait_q_t readers; /* Reader wait queue */
2434 _wait_q_t writers; /* Writer wait queue */
2435 } wait_q;
2436
Anas Nashif2f203c22016-12-18 06:57:45 -05002437 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438};
2439
Peter Mitsise5d9c582016-10-14 14:44:57 -04002440#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441 { \
2442 .buffer = pipe_buffer, \
2443 .size = pipe_buffer_size, \
2444 .bytes_used = 0, \
2445 .read_index = 0, \
2446 .write_index = 0, \
2447 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2448 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002449 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 }
2451
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002452/**
Allan Stephensc98da842016-11-11 15:45:03 -05002453 * INTERNAL_HIDDEN @endcond
2454 */
2455
2456/**
2457 * @defgroup pipe_apis Pipe APIs
2458 * @ingroup kernel_apis
2459 * @{
2460 */
2461
2462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002467 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @param name Name of the pipe.
2470 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2471 * or zero if no ring buffer is used.
2472 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002474#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2475 static unsigned char __noinit __aligned(pipe_align) \
2476 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002477 struct k_pipe name \
2478 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002479 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * This routine initializes a pipe object, prior to its first use.
2485 *
2486 * @param pipe Address of the pipe.
2487 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2488 * is used.
2489 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2490 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491 *
2492 * @return N/A
2493 */
2494extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2495 size_t size);
2496
2497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002502 * @param pipe Address of the pipe.
2503 * @param data Address of data to write.
2504 * @param bytes_to_write Size of data (in bytes).
2505 * @param bytes_written Address of area to hold the number of bytes written.
2506 * @param min_xfer Minimum number of bytes to write.
2507 * @param timeout Waiting period to wait for the data to be written (in
2508 * milliseconds), or one of the special values K_NO_WAIT
2509 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002511 * @retval 0 At least @a min_xfer bytes of data were written.
2512 * @retval -EIO Returned without waiting; zero data bytes were written.
2513 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002516extern int k_pipe_put(struct k_pipe *pipe, void *data,
2517 size_t bytes_to_write, size_t *bytes_written,
2518 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519
2520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * @param pipe Address of the pipe.
2526 * @param data Address to place the data read from pipe.
2527 * @param bytes_to_read Maximum number of data bytes to read.
2528 * @param bytes_read Address of area to hold the number of bytes read.
2529 * @param min_xfer Minimum number of data bytes to read.
2530 * @param timeout Waiting period to wait for the data to be read (in
2531 * milliseconds), or one of the special values K_NO_WAIT
2532 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002534 * @retval 0 At least @a min_xfer bytes of data were read.
2535 * @retval -EIO Returned without waiting; zero data bytes were read.
2536 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002539extern int k_pipe_get(struct k_pipe *pipe, void *data,
2540 size_t bytes_to_read, size_t *bytes_read,
2541 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542
2543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * This routine writes the data contained in a memory block to @a pipe.
2547 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551 * @param block Memory block containing data to send
2552 * @param size Number of data bytes in memory block to send
2553 * @param sem Semaphore to signal upon completion (else NULL)
2554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 */
2557extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2558 size_t size, struct k_sem *sem);
2559
2560/**
Allan Stephensc98da842016-11-11 15:45:03 -05002561 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 */
2563
Allan Stephensc98da842016-11-11 15:45:03 -05002564/**
2565 * @cond INTERNAL_HIDDEN
2566 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002568struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002570 uint32_t num_blocks;
2571 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572 char *buffer;
2573 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002574 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575
Anas Nashif2f203c22016-12-18 06:57:45 -05002576 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577};
2578
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002579#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2580 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581 { \
2582 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002583 .num_blocks = slab_num_blocks, \
2584 .block_size = slab_block_size, \
2585 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 .free_list = NULL, \
2587 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002588 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589 }
2590
Peter Mitsis578f9112016-10-07 13:50:31 -04002591/**
Allan Stephensc98da842016-11-11 15:45:03 -05002592 * INTERNAL_HIDDEN @endcond
2593 */
2594
2595/**
2596 * @defgroup mem_slab_apis Memory Slab APIs
2597 * @ingroup kernel_apis
2598 * @{
2599 */
2600
2601/**
Allan Stephensda827222016-11-09 14:23:58 -06002602 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002603 *
Allan Stephensda827222016-11-09 14:23:58 -06002604 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002606 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2607 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002608 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002609 *
Allan Stephensda827222016-11-09 14:23:58 -06002610 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002612 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002613 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @param name Name of the memory slab.
2616 * @param slab_block_size Size of each memory block (in bytes).
2617 * @param slab_num_blocks Number memory blocks.
2618 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002619 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002620#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2621 char __noinit __aligned(slab_align) \
2622 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2623 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002624 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002625 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2626 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002628/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002629 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002632 *
Allan Stephensda827222016-11-09 14:23:58 -06002633 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2634 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2635 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2636 * To ensure that each memory block is similarly aligned to this boundary,
2637 * @a slab_block_size must also be a multiple of N.
2638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639 * @param slab Address of the memory slab.
2640 * @param buffer Pointer to buffer used for the memory blocks.
2641 * @param block_size Size of each memory block (in bytes).
2642 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002643 *
2644 * @return N/A
2645 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002646extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002647 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002648
2649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * @param slab Address of the memory slab.
2655 * @param mem Pointer to block address area.
2656 * @param timeout Maximum time to wait for operation to complete
2657 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2658 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002659 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002660 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002662 * @retval -ENOMEM Returned without waiting.
2663 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002664 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002665extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2666 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002667
2668/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * This routine releases a previously allocated memory block back to its
2672 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * @param slab Address of the memory slab.
2675 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002676 *
2677 * @return N/A
2678 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002679extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * This routine gets the number of memory blocks that are currently
2685 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002690 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002691static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002693 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694}
2695
Peter Mitsisc001aa82016-10-13 13:53:37 -04002696/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * This routine gets the number of memory blocks that are currently
2700 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002705 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002706static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002707{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002708 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002709}
2710
Allan Stephensc98da842016-11-11 15:45:03 -05002711/**
2712 * @} end defgroup mem_slab_apis
2713 */
2714
2715/**
2716 * @cond INTERNAL_HIDDEN
2717 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002719/*
2720 * Memory pool requires a buffer and two arrays of structures for the
2721 * memory block accounting:
2722 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2723 * status of four blocks of memory.
2724 */
2725struct k_mem_pool_quad_block {
2726 char *mem_blocks; /* pointer to the first of four memory blocks */
2727 uint32_t mem_status; /* four bits. If bit is set, memory block is
2728 allocated */
2729};
2730/*
2731 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2732 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2733 * block size is 4 times less than the previous one and thus requires 4 times
2734 * bigger array of k_mem_pool_quad_block structures to keep track of the
2735 * memory blocks.
2736 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002738/*
2739 * The array of k_mem_pool_block_set keeps the information of each array of
2740 * k_mem_pool_quad_block structures
2741 */
2742struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002743 size_t block_size; /* memory block size */
2744 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002745 struct k_mem_pool_quad_block *quad_block;
2746 int count;
2747};
2748
2749/* Memory pool descriptor */
2750struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002751 size_t max_block_size;
2752 size_t min_block_size;
2753 uint32_t nr_of_maxblocks;
2754 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002755 struct k_mem_pool_block_set *block_set;
2756 char *bufblock;
2757 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002758 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759};
2760
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002761#ifdef CONFIG_ARM
2762#define _SECTION_TYPE_SIGN "%"
2763#else
2764#define _SECTION_TYPE_SIGN "@"
2765#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002767/*
2768 * Static memory pool initialization
2769 */
Allan Stephensc98da842016-11-11 15:45:03 -05002770
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002771/*
2772 * Use .altmacro to be able to recalculate values and pass them as string
2773 * arguments when calling assembler macros resursively
2774 */
2775__asm__(".altmacro\n\t");
2776
2777/*
2778 * Recursively calls a macro
2779 * The followig global symbols need to be initialized:
2780 * __memory_pool_max_block_size - maximal size of the memory block
2781 * __memory_pool_min_block_size - minimal size of the memory block
2782 * Notes:
2783 * Global symbols are used due the fact that assembler macro allows only
2784 * one argument be passed with the % conversion
2785 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2786 * is used instead of / 4.
2787 * n_max argument needs to go first in the invoked macro, as some
2788 * assemblers concatenate \name and %(\n_max * 4) arguments
2789 * if \name goes first
2790 */
2791__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2792 ".ifge __memory_pool_max_block_size >> 2 -"
2793 " __memory_pool_min_block_size\n\t\t"
2794 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2795 "\\macro_name %(\\n_max * 4) \\name\n\t"
2796 ".endif\n\t"
2797 ".endm\n");
2798
2799/*
2800 * Build quad blocks
2801 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2802 * structures and recursively calls itself for the next array, 4 times
2803 * larger.
2804 * The followig global symbols need to be initialized:
2805 * __memory_pool_max_block_size - maximal size of the memory block
2806 * __memory_pool_min_block_size - minimal size of the memory block
2807 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2808 */
2809__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002810 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002811 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2812 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2813 ".if \\n_max % 4\n\t\t"
2814 ".skip __memory_pool_quad_block_size\n\t"
2815 ".endif\n\t"
2816 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2817 ".endm\n");
2818
2819/*
2820 * Build block sets and initialize them
2821 * Macro initializes the k_mem_pool_block_set structure and
2822 * recursively calls itself for the next one.
2823 * The followig global symbols need to be initialized:
2824 * __memory_pool_max_block_size - maximal size of the memory block
2825 * __memory_pool_min_block_size - minimal size of the memory block
2826 * __memory_pool_block_set_count, the number of the elements in the
2827 * block set array must be set to 0. Macro calculates it's real
2828 * value.
2829 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2830 * structures, _build_quad_blocks must be called prior it.
2831 */
2832__asm__(".macro _build_block_set n_max, name\n\t"
2833 ".int __memory_pool_max_block_size\n\t" /* block_size */
2834 ".if \\n_max % 4\n\t\t"
2835 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2836 ".else\n\t\t"
2837 ".int \\n_max >> 2\n\t"
2838 ".endif\n\t"
2839 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2840 ".int 0\n\t" /* count */
2841 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2842 "__do_recurse _build_block_set \\name \\n_max\n\t"
2843 ".endm\n");
2844
2845/*
2846 * Build a memory pool structure and initialize it
2847 * Macro uses __memory_pool_block_set_count global symbol,
2848 * block set addresses and buffer address, it may be called only after
2849 * _build_block_set
2850 */
2851__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002852 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002853 _SECTION_TYPE_SIGN "progbits\n\t"
2854 ".globl \\name\n\t"
2855 "\\name:\n\t"
2856 ".int \\max_size\n\t" /* max_block_size */
2857 ".int \\min_size\n\t" /* min_block_size */
2858 ".int \\n_max\n\t" /* nr_of_maxblocks */
2859 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2860 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2861 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2862 ".int 0\n\t" /* wait_q->head */
2863 ".int 0\n\t" /* wait_q->next */
2864 ".popsection\n\t"
2865 ".endm\n");
2866
2867#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2868 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2869 _SECTION_TYPE_SIGN "progbits\n\t"); \
2870 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2871 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2872 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2873 STRINGIFY(name) "\n\t"); \
2874 __asm__(".popsection\n\t")
2875
2876#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2877 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2878 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2879 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2880 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002881 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002882 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2883 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2884 STRINGIFY(name) "\n\t"); \
2885 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2886 __asm__(".int __memory_pool_block_set_count\n\t"); \
2887 __asm__(".popsection\n\t"); \
2888 extern uint32_t _mem_pool_block_set_count_##name; \
2889 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2890
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002891#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2892 char __noinit __aligned(align) \
2893 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002894
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002895/*
2896 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2897 * to __memory_pool_quad_block_size absolute symbol.
2898 * This function does not get called, but compiler calculates the value and
2899 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2900 */
2901static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2902{
2903 __asm__(".globl __memory_pool_quad_block_size\n\t"
2904#ifdef CONFIG_NIOS2
2905 "__memory_pool_quad_block_size = %0\n\t"
2906#else
2907 "__memory_pool_quad_block_size = %c0\n\t"
2908#endif
2909 :
2910 : "n"(sizeof(struct k_mem_pool_quad_block)));
2911}
2912
2913/**
Allan Stephensc98da842016-11-11 15:45:03 -05002914 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002915 */
2916
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002917/**
Allan Stephensc98da842016-11-11 15:45:03 -05002918 * @addtogroup mem_pool_apis
2919 * @{
2920 */
2921
2922/**
2923 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2926 * long. The memory pool allows blocks to be repeatedly partitioned into
2927 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2928 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002929 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002931 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 * If the pool is to be accessed outside the module where it is defined, it
2933 * can be declared via
2934 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002935 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @param name Name of the memory pool.
2938 * @param min_size Size of the smallest blocks in the pool (in bytes).
2939 * @param max_size Size of the largest blocks in the pool (in bytes).
2940 * @param n_max Number of maximum sized blocks in the pool.
2941 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002942 */
2943#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002944 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2945 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002946 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002947 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2948 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2949 extern struct k_mem_pool name
2950
Peter Mitsis937042c2016-10-13 13:18:26 -04002951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param pool Address of the memory pool.
2957 * @param block Pointer to block descriptor for the allocated memory.
2958 * @param size Amount of memory to allocate (in bytes).
2959 * @param timeout Maximum time to wait for operation to complete
2960 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2961 * or K_FOREVER to wait as long as necessary.
2962 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002963 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002965 * @retval -ENOMEM Returned without waiting.
2966 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002967 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002968extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002969 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002970
2971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * This routine releases a previously allocated memory block back to its
2975 * memory pool.
2976 *
2977 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002978 *
2979 * @return N/A
2980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002982
2983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine instructs a memory pool to concatenate unused memory blocks
2987 * into larger blocks wherever possible. Manually defragmenting the memory
2988 * pool may speed up future allocations of memory blocks by eliminating the
2989 * need for the memory pool to perform an automatic partial defragmentation.
2990 *
2991 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002992 *
2993 * @return N/A
2994 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002995extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002996
2997/**
Allan Stephensc98da842016-11-11 15:45:03 -05002998 * @} end addtogroup mem_pool_apis
2999 */
3000
3001/**
3002 * @defgroup heap_apis Heap Memory Pool APIs
3003 * @ingroup kernel_apis
3004 * @{
3005 */
3006
3007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003011 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003016 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003017extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003018
3019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003021 *
3022 * This routine provides traditional free() semantics. The memory being
3023 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003024 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003025 * If @a ptr is NULL, no operation is performed.
3026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003028 *
3029 * @return N/A
3030 */
3031extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003032
Allan Stephensc98da842016-11-11 15:45:03 -05003033/**
3034 * @} end defgroup heap_apis
3035 */
3036
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003037/**
3038 * @brief Make the CPU idle.
3039 *
3040 * This function makes the CPU idle until an event wakes it up.
3041 *
3042 * In a regular system, the idle thread should be the only thread responsible
3043 * for making the CPU idle and triggering any type of power management.
3044 * However, in some more constrained systems, such as a single-threaded system,
3045 * the only thread would be responsible for this if needed.
3046 *
3047 * @return N/A
3048 */
3049extern void k_cpu_idle(void);
3050
3051/**
3052 * @brief Make the CPU idle in an atomic fashion.
3053 *
3054 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3055 * must be done atomically before making the CPU idle.
3056 *
3057 * @param key Interrupt locking key obtained from irq_lock().
3058 *
3059 * @return N/A
3060 */
3061extern void k_cpu_atomic_idle(unsigned int key);
3062
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063/*
3064 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
3065 * hook into the device subsystem, which itself uses nanokernel semaphores,
3066 * and thus currently requires the definition of nano_sem.
3067 */
3068#include <legacy.h>
3069#include <arch/cpu.h>
3070
3071/*
3072 * private APIs that are utilized by one or more public APIs
3073 */
3074
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003075#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003076extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003077#else
3078#define _init_static_threads() do { } while ((0))
3079#endif
3080
3081extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003082extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083
3084#ifdef __cplusplus
3085}
3086#endif
3087
Andrew Boiee004dec2016-11-07 09:01:19 -08003088#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3089/*
3090 * Define new and delete operators.
3091 * At this moment, the operators do nothing since objects are supposed
3092 * to be statically allocated.
3093 */
3094inline void operator delete(void *ptr)
3095{
3096 (void)ptr;
3097}
3098
3099inline void operator delete[](void *ptr)
3100{
3101 (void)ptr;
3102}
3103
3104inline void *operator new(size_t size)
3105{
3106 (void)size;
3107 return NULL;
3108}
3109
3110inline void *operator new[](size_t size)
3111{
3112 (void)size;
3113 return NULL;
3114}
3115
3116/* Placement versions of operator new and delete */
3117inline void operator delete(void *ptr1, void *ptr2)
3118{
3119 (void)ptr1;
3120 (void)ptr2;
3121}
3122
3123inline void operator delete[](void *ptr1, void *ptr2)
3124{
3125 (void)ptr1;
3126 (void)ptr2;
3127}
3128
3129inline void *operator new(size_t size, void *ptr)
3130{
3131 (void)size;
3132 return ptr;
3133}
3134
3135inline void *operator new[](size_t size, void *ptr)
3136{
3137 (void)size;
3138 return ptr;
3139}
3140
3141#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3142
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143#endif /* _kernel__h_ */