blob: c5c459b3517fd041e26b53eecf6a494bf9ff0074 [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 Nashif61f4b242016-11-18 10:53:59 -050042#ifdef CONFIG_KERNEL_DEBUG
43#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040044#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
45#else
46#define K_DEBUG(fmt, ...)
47#endif
48
49#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
50#define K_PRIO_PREEMPT(x) (x)
51
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
Benjamin Walshfab8d922016-11-08 15:36:36 -050067#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
Anas Nashif2f203c22016-12-18 06:57:45 -050074#ifdef CONFIG_OBJECT_TRACING
75#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
76#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#else
Anas Nashif2f203c22016-12-18 06:57:45 -050078#define _OBJECT_TRACING_INIT
79#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050082#define tcs k_thread
83struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400105/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100106 * @defgroup profiling_apis Profiling APIs
107 * @ingroup kernel_apis
108 * @{
109 */
110
111/**
112 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
113 *
114 * This routine calls @ref stack_analyze on the 4 call stacks declared and
115 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
116 *
117 * CONFIG_MAIN_STACK_SIZE
118 * CONFIG_IDLE_STACK_SIZE
119 * CONFIG_ISR_STACK_SIZE
120 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
121 *
122 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
123 * produce output.
124 *
125 * @return N/A
126 */
127extern void k_call_stacks_analyze(void);
128
129/**
130 * @} end defgroup profiling_apis
131 */
132
133/**
Allan Stephensc98da842016-11-11 15:45:03 -0500134 * @defgroup thread_apis Thread APIs
135 * @ingroup kernel_apis
136 * @{
137 */
138
139/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500140 * @typedef k_thread_entry_t
141 * @brief Thread entry point function type.
142 *
143 * A thread's entry point function is invoked when the thread starts executing.
144 * Up to 3 argument values can be passed to the function.
145 *
146 * The thread terminates execution permanently if the entry point function
147 * returns. The thread is responsible for releasing any shared resources
148 * it may own (such as mutexes and dynamically allocated memory), prior to
149 * returning.
150 *
151 * @param p1 First argument.
152 * @param p2 Second argument.
153 * @param p3 Third argument.
154 *
155 * @return N/A
156 */
157typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
158
159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500160 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500162 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500164 * The new thread may be scheduled for immediate execution or a delayed start.
165 * If the newly spawned thread does not have a delayed start the kernel
166 * scheduler may preempt the current thread to allow the new thread to
167 * execute.
168 *
169 * Thread options are architecture-specific, and can include K_ESSENTIAL,
170 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
171 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400172 *
173 * @param stack Pointer to the stack space.
174 * @param stack_size Stack size in bytes.
175 * @param entry Thread entry function.
176 * @param p1 1st entry point parameter.
177 * @param p2 2nd entry point parameter.
178 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500179 * @param prio Thread priority.
180 * @param options Thread options.
181 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500183 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500185extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500186 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400187 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500188 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400189
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500191 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400192 *
Allan Stephensc98da842016-11-11 15:45:03 -0500193 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500194 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
198 * @return N/A
199 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400200extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201
202/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500203 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400204 *
205 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500206 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 * @return N/A
209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400210extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211
212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500213 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500215 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400216 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500217 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
219 * @return N/A
220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400221extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400222
223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500224 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500226 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500228 * If @a thread is not currently sleeping, the routine has no effect.
229 *
230 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400231 *
232 * @return N/A
233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400234extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235
236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500239 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400240 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400241extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242
243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500244 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500246 * This routine prevents @a thread from executing if it has not yet started
247 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @param thread ID of thread to cancel.
250 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500251 * @retval 0 Thread spawning cancelled.
252 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400254extern int k_thread_cancel(k_tid_t thread);
255
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400256/**
Allan Stephensc98da842016-11-11 15:45:03 -0500257 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500259 * This routine permanently stops execution of @a thread. The thread is taken
260 * off all kernel queues it is part of (i.e. the ready queue, the timeout
261 * queue, or a kernel object wait queue). However, any kernel resources the
262 * thread might currently own (such as mutexes or memory blocks) are not
263 * released. It is the responsibility of the caller of this routine to ensure
264 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500266 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267 *
268 * @return N/A
269 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400270extern void k_thread_abort(k_tid_t thread);
271
Allan Stephensc98da842016-11-11 15:45:03 -0500272/**
273 * @cond INTERNAL_HIDDEN
274 */
275
Benjamin Walshd211a522016-12-06 11:44:01 -0500276/* timeout has timed out and is not on _timeout_q anymore */
277#define _EXPIRED (-2)
278
279/* timeout is not in use */
280#define _INACTIVE (-1)
281
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400282#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400283#define _THREAD_TIMEOUT_INIT(obj) \
284 (obj).nano_timeout = { \
285 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400286 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500288 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400289 },
290#else
291#define _THREAD_TIMEOUT_INIT(obj)
292#endif
293
294#ifdef CONFIG_ERRNO
295#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
296#else
297#define _THREAD_ERRNO_INIT(obj)
298#endif
299
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400300struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400301 union {
302 char *init_stack;
303 struct k_thread *thread;
304 };
305 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500306 void (*init_entry)(void *, void *, void *);
307 void *init_p1;
308 void *init_p2;
309 void *init_p3;
310 int init_prio;
311 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400312 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500313 void (*init_abort)(void);
314 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400315};
316
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400317#define _THREAD_INITIALIZER(stack, stack_size, \
318 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500319 prio, options, delay, abort, groups) \
320 { \
321 .init_stack = (stack), \
322 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400323 .init_entry = (void (*)(void *, void *, void *))entry, \
324 .init_p1 = (void *)p1, \
325 .init_p2 = (void *)p2, \
326 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500327 .init_prio = (prio), \
328 .init_options = (options), \
329 .init_delay = (delay), \
330 .init_abort = (abort), \
331 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400332 }
333
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400334/**
Allan Stephensc98da842016-11-11 15:45:03 -0500335 * INTERNAL_HIDDEN @endcond
336 */
337
338/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500339 * @brief Statically define and initialize a thread.
340 *
341 * The thread may be scheduled for immediate execution or a delayed start.
342 *
343 * Thread options are architecture-specific, and can include K_ESSENTIAL,
344 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
345 * them using "|" (the logical OR operator).
346 *
347 * The ID of the thread can be accessed using:
348 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500349 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500350 *
351 * @param name Name of the thread.
352 * @param stack_size Stack size in bytes.
353 * @param entry Thread entry function.
354 * @param p1 1st entry point parameter.
355 * @param p2 2nd entry point parameter.
356 * @param p3 3rd entry point parameter.
357 * @param prio Thread priority.
358 * @param options Thread options.
359 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400360 *
361 * @internal It has been observed that the x86 compiler by default aligns
362 * these _static_thread_data structures to 32-byte boundaries, thereby
363 * wasting space. To work around this, force a 4-byte alignment.
364 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500365#define K_THREAD_DEFINE(name, stack_size, \
366 entry, p1, p2, p3, \
367 prio, options, delay) \
368 char __noinit __stack _k_thread_obj_##name[stack_size]; \
369 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500370 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500371 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
372 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500373 NULL, 0); \
374 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400375
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500377 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500379 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @param thread ID of thread whose priority is needed.
382 *
383 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500385extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386
387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500390 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400391 *
392 * Rescheduling can occur immediately depending on the priority @a thread is
393 * set to:
394 *
395 * - If its priority is raised above the priority of the caller of this
396 * function, and the caller is preemptible, @a thread will be scheduled in.
397 *
398 * - If the caller operates on itself, it lowers its priority below that of
399 * other threads in the system, and the caller is preemptible, the thread of
400 * highest priority will be scheduled in.
401 *
402 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
403 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
404 * highest priority.
405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 * @param prio New priority.
408 *
409 * @warning Changing the priority of a thread currently involved in mutex
410 * priority inheritance may result in undefined behavior.
411 *
412 * @return N/A
413 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400414extern void k_thread_priority_set(k_tid_t thread, int prio);
415
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500417 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500419 * This routine prevents the kernel scheduler from making @a thread the
420 * current thread. All other internal operations on @a thread are still
421 * performed; for example, any timeout it is waiting on keeps ticking,
422 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * If @a thread is already suspended, the routine has no effect.
425 *
426 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 *
428 * @return N/A
429 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400430extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431
432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500433 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * This routine allows the kernel scheduler to make @a thread the current
436 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500438 * If @a thread is not currently suspended, the routine has no effect.
439 *
440 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441 *
442 * @return N/A
443 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400444extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500447 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500449 * This routine specifies how the scheduler will perform time slicing of
450 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * To enable time slicing, @a slice must be non-zero. The scheduler
453 * ensures that no thread runs for more than the specified time limit
454 * before other threads of that priority are given a chance to execute.
455 * Any thread whose priority is higher than @a prio is exempted, and may
456 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 * execute. Once the scheduler selects a thread for execution, there is no
460 * minimum guaranteed time the thread will execute before threads of greater or
461 * equal priority are scheduled.
462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 * for execution, this routine has no effect; the thread is immediately
465 * rescheduled after the slice period expires.
466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500467 * To disable timeslicing, set both @a slice and @a prio to zero.
468 *
469 * @param slice Maximum time slice length (in milliseconds).
470 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
472 * @return N/A
473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400475
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400476/**
Allan Stephensc98da842016-11-11 15:45:03 -0500477 * @} end defgroup thread_apis
478 */
479
480/**
481 * @addtogroup isr_apis
482 * @{
483 */
484
485/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500486 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487 *
Allan Stephensc98da842016-11-11 15:45:03 -0500488 * This routine allows the caller to customize its actions, depending on
489 * whether it is a thread or an ISR.
490 *
491 * @note Can be called by ISRs.
492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * @return 0 if invoked by a thread.
494 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400495 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500496extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400497
Benjamin Walsh445830d2016-11-10 15:54:27 -0500498/**
499 * @brief Determine if code is running in a preemptible thread.
500 *
Allan Stephensc98da842016-11-11 15:45:03 -0500501 * This routine allows the caller to customize its actions, depending on
502 * whether it can be preempted by another thread. The routine returns a 'true'
503 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500504 *
Allan Stephensc98da842016-11-11 15:45:03 -0500505 * - The code is running in a thread, not at ISR.
506 * - The thread's priority is in the preemptible range.
507 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500508 *
Allan Stephensc98da842016-11-11 15:45:03 -0500509 * @note Can be called by ISRs.
510 *
511 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500512 * @return Non-zero if invoked by a preemptible thread.
513 */
514extern int k_is_preempt_thread(void);
515
Allan Stephensc98da842016-11-11 15:45:03 -0500516/**
517 * @} end addtogroup isr_apis
518 */
519
520/**
521 * @addtogroup thread_apis
522 * @{
523 */
524
525/**
526 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500527 *
Allan Stephensc98da842016-11-11 15:45:03 -0500528 * This routine prevents the current thread from being preempted by another
529 * thread by instructing the scheduler to treat it as a cooperative thread.
530 * If the thread subsequently performs an operation that makes it unready,
531 * it will be context switched out in the normal manner. When the thread
532 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500533 *
Allan Stephensc98da842016-11-11 15:45:03 -0500534 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500535 *
Allan Stephensc98da842016-11-11 15:45:03 -0500536 * @note k_sched_lock() and k_sched_unlock() should normally be used
537 * when the operation being performed can be safely interrupted by ISRs.
538 * However, if the amount of processing involved is very small, better
539 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500540 *
541 * @return N/A
542 */
543extern void k_sched_lock(void);
544
Allan Stephensc98da842016-11-11 15:45:03 -0500545/**
546 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500547 *
Allan Stephensc98da842016-11-11 15:45:03 -0500548 * This routine reverses the effect of a previous call to k_sched_lock().
549 * A thread must call the routine once for each time it called k_sched_lock()
550 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500551 *
552 * @return N/A
553 */
554extern void k_sched_unlock(void);
555
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500557 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500559 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * Custom data is not used by the kernel itself, and is freely available
562 * for a thread to use as it sees fit. It can be used as a framework
563 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500565 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400566 *
567 * @return N/A
568 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400569extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400570
571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500574 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500576 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578extern void *k_thread_custom_data_get(void);
579
580/**
Allan Stephensc98da842016-11-11 15:45:03 -0500581 * @} end addtogroup thread_apis
582 */
583
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400584#include <sys_clock.h>
585
Allan Stephensc2f15a42016-11-17 12:24:22 -0500586/**
587 * @addtogroup clock_apis
588 * @{
589 */
590
591/**
592 * @brief Generate null timeout delay.
593 *
594 * This macro generates a timeout delay that that instructs a kernel API
595 * not to wait if the requested operation cannot be performed immediately.
596 *
597 * @return Timeout delay value.
598 */
599#define K_NO_WAIT 0
600
601/**
602 * @brief Generate timeout delay from milliseconds.
603 *
604 * This macro generates a timeout delay that that instructs a kernel API
605 * to wait up to @a ms milliseconds to perform the requested operation.
606 *
607 * @param ms Duration in milliseconds.
608 *
609 * @return Timeout delay value.
610 */
Johan Hedberg14471692016-11-13 10:52:15 +0200611#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500612
613/**
614 * @brief Generate timeout delay from seconds.
615 *
616 * This macro generates a timeout delay that that instructs a kernel API
617 * to wait up to @a s seconds to perform the requested operation.
618 *
619 * @param s Duration in seconds.
620 *
621 * @return Timeout delay value.
622 */
Johan Hedberg14471692016-11-13 10:52:15 +0200623#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500624
625/**
626 * @brief Generate timeout delay from minutes.
627 *
628 * This macro generates a timeout delay that that instructs a kernel API
629 * to wait up to @a m minutes to perform the requested operation.
630 *
631 * @param m Duration in minutes.
632 *
633 * @return Timeout delay value.
634 */
Johan Hedberg14471692016-11-13 10:52:15 +0200635#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500636
637/**
638 * @brief Generate timeout delay from hours.
639 *
640 * This macro generates a timeout delay that that instructs a kernel API
641 * to wait up to @a h hours to perform the requested operation.
642 *
643 * @param h Duration in hours.
644 *
645 * @return Timeout delay value.
646 */
Johan Hedberg14471692016-11-13 10:52:15 +0200647#define K_HOURS(h) K_MINUTES((h) * 60)
648
Allan Stephensc98da842016-11-11 15:45:03 -0500649/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500650 * @brief Generate infinite timeout delay.
651 *
652 * This macro generates a timeout delay that that instructs a kernel API
653 * to wait as long as necessary to perform the requested operation.
654 *
655 * @return Timeout delay value.
656 */
657#define K_FOREVER (-1)
658
659/**
660 * @} end addtogroup clock_apis
661 */
662
663/**
Allan Stephensc98da842016-11-11 15:45:03 -0500664 * @cond INTERNAL_HIDDEN
665 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400666
Benjamin Walsh62092182016-12-20 14:39:08 -0500667/* kernel clocks */
668
669#if (sys_clock_ticks_per_sec == 1000) || \
670 (sys_clock_ticks_per_sec == 500) || \
671 (sys_clock_ticks_per_sec == 250) || \
672 (sys_clock_ticks_per_sec == 125) || \
673 (sys_clock_ticks_per_sec == 100) || \
674 (sys_clock_ticks_per_sec == 50) || \
675 (sys_clock_ticks_per_sec == 25) || \
676 (sys_clock_ticks_per_sec == 20) || \
677 (sys_clock_ticks_per_sec == 10) || \
678 (sys_clock_ticks_per_sec == 1)
679
680 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
681#else
682 /* yields horrible 64-bit math on many architectures: try to avoid */
683 #define _NON_OPTIMIZED_TICKS_PER_SEC
684#endif
685
686#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
687extern int32_t _ms_to_ticks(int32_t ms);
688#else
689static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
690{
691 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
692}
693#endif
694
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500695/* added tick needed to account for tick in progress */
696#define _TICK_ALIGN 1
697
Benjamin Walsh62092182016-12-20 14:39:08 -0500698static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400699{
Benjamin Walsh62092182016-12-20 14:39:08 -0500700#ifdef CONFIG_SYS_CLOCK_EXISTS
701
702#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400703 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400704#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500705 return (uint64_t)ticks * _ms_per_tick;
706#endif
707
708#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400709 __ASSERT(ticks == 0, "");
710 return 0;
711#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400712}
713
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400714/* timeouts */
715
716struct _timeout;
717typedef void (*_timeout_func_t)(struct _timeout *t);
718
719struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500720 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400721 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722 sys_dlist_t *wait_q;
723 int32_t delta_ticks_from_prev;
724 _timeout_func_t func;
725};
726
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200727extern int32_t _timeout_remaining_get(struct _timeout *timeout);
728
Allan Stephensc98da842016-11-11 15:45:03 -0500729/**
730 * INTERNAL_HIDDEN @endcond
731 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500732
Allan Stephensc98da842016-11-11 15:45:03 -0500733/**
734 * @cond INTERNAL_HIDDEN
735 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400736
737struct k_timer {
738 /*
739 * _timeout structure must be first here if we want to use
740 * dynamic timer allocation. timeout.node is used in the double-linked
741 * list of free timers
742 */
743 struct _timeout timeout;
744
Allan Stephens45bfa372016-10-12 12:39:42 -0500745 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400746 _wait_q_t wait_q;
747
748 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500749 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400750
751 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500752 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753
754 /* timer period */
755 int32_t period;
756
Allan Stephens45bfa372016-10-12 12:39:42 -0500757 /* timer status */
758 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500760 /* user-specific data, also used to support legacy features */
761 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400762
Anas Nashif2f203c22016-12-18 06:57:45 -0500763 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764};
765
Allan Stephens1342adb2016-11-03 13:54:53 -0500766#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400767 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500768 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500769 .timeout.wait_q = NULL, \
770 .timeout.thread = NULL, \
771 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400772 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500773 .expiry_fn = expiry, \
774 .stop_fn = stop, \
775 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500776 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500777 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778 }
779
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780/**
Allan Stephensc98da842016-11-11 15:45:03 -0500781 * INTERNAL_HIDDEN @endcond
782 */
783
784/**
785 * @defgroup timer_apis Timer APIs
786 * @ingroup kernel_apis
787 * @{
788 */
789
790/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500791 * @typedef k_timer_expiry_t
792 * @brief Timer expiry function type.
793 *
794 * A timer's expiry function is executed by the system clock interrupt handler
795 * each time the timer expires. The expiry function is optional, and is only
796 * invoked if the timer has been initialized with one.
797 *
798 * @param timer Address of timer.
799 *
800 * @return N/A
801 */
802typedef void (*k_timer_expiry_t)(struct k_timer *timer);
803
804/**
805 * @typedef k_timer_stop_t
806 * @brief Timer stop function type.
807 *
808 * A timer's stop function is executed if the timer is stopped prematurely.
809 * The function runs in the context of the thread that stops the timer.
810 * The stop function is optional, and is only invoked if the timer has been
811 * initialized with one.
812 *
813 * @param timer Address of timer.
814 *
815 * @return N/A
816 */
817typedef void (*k_timer_stop_t)(struct k_timer *timer);
818
819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500824 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @param expiry_fn Function to invoke each time the timer expires.
828 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500830#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500831 struct k_timer name \
832 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500833 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400834
Allan Stephens45bfa372016-10-12 12:39:42 -0500835/**
836 * @brief Initialize a timer.
837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500839 *
840 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @param expiry_fn Function to invoke each time the timer expires.
842 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500843 *
844 * @return N/A
845 */
846extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500847 k_timer_expiry_t expiry_fn,
848 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700849
Allan Stephens45bfa372016-10-12 12:39:42 -0500850/**
851 * @brief Start a timer.
852 *
853 * This routine starts a timer, and resets its status to zero. The timer
854 * begins counting down using the specified duration and period values.
855 *
856 * Attempting to start a timer that is already running is permitted.
857 * The timer's status is reset to zero and the timer begins counting down
858 * using the new duration and period values.
859 *
860 * @param timer Address of timer.
861 * @param duration Initial timer duration (in milliseconds).
862 * @param period Timer period (in milliseconds).
863 *
864 * @return N/A
865 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500867 int32_t duration, int32_t period);
868
869/**
870 * @brief Stop a timer.
871 *
872 * This routine stops a running timer prematurely. The timer's stop function,
873 * if one exists, is invoked by the caller.
874 *
875 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500877 *
878 * @param timer Address of timer.
879 *
880 * @return N/A
881 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500883
884/**
885 * @brief Read timer status.
886 *
887 * This routine reads the timer's status, which indicates the number of times
888 * it has expired since its status was last read.
889 *
890 * Calling this routine resets the timer's status to zero.
891 *
892 * @param timer Address of timer.
893 *
894 * @return Timer status.
895 */
896extern uint32_t k_timer_status_get(struct k_timer *timer);
897
898/**
899 * @brief Synchronize thread to timer expiration.
900 *
901 * This routine blocks the calling thread until the timer's status is non-zero
902 * (indicating that it has expired at least once since it was last examined)
903 * or the timer is stopped. If the timer status is already non-zero,
904 * or the timer is already stopped, the caller continues without waiting.
905 *
906 * Calling this routine resets the timer's status to zero.
907 *
908 * This routine must not be used by interrupt handlers, since they are not
909 * allowed to block.
910 *
911 * @param timer Address of timer.
912 *
913 * @return Timer status.
914 */
915extern uint32_t k_timer_status_sync(struct k_timer *timer);
916
917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500919 *
920 * This routine computes the (approximate) time remaining before a running
921 * timer next expires. If the timer is not running, it returns zero.
922 *
923 * @param timer Address of timer.
924 *
925 * @return Remaining time (in milliseconds).
926 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200927static inline int32_t k_timer_remaining_get(struct k_timer *timer)
928{
929 return _timeout_remaining_get(&timer->timeout);
930}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400931
Allan Stephensc98da842016-11-11 15:45:03 -0500932/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500933 * @brief Associate user-specific data with a timer.
934 *
935 * This routine records the @a user_data with the @a timer, to be retrieved
936 * later.
937 *
938 * It can be used e.g. in a timer handler shared across multiple subsystems to
939 * retrieve data specific to the subsystem this timer is associated with.
940 *
941 * @param timer Address of timer.
942 * @param user_data User data to associate with the timer.
943 *
944 * @return N/A
945 */
946static inline void k_timer_user_data_set(struct k_timer *timer,
947 void *user_data)
948{
949 timer->user_data = user_data;
950}
951
952/**
953 * @brief Retrieve the user-specific data from a timer.
954 *
955 * @param timer Address of timer.
956 *
957 * @return The user data.
958 */
959static inline void *k_timer_user_data_get(struct k_timer *timer)
960{
961 return timer->user_data;
962}
963
964/**
Allan Stephensc98da842016-11-11 15:45:03 -0500965 * @} end defgroup timer_apis
966 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400967
Allan Stephensc98da842016-11-11 15:45:03 -0500968/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500969 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500970 * @{
971 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500972
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400973/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500976 * This routine returns the elapsed time since the system booted,
977 * in milliseconds.
978 *
979 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400981extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400982
983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500984 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * This routine returns the lower 32-bits of the elapsed time since the system
987 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * This routine can be more efficient than k_uptime_get(), as it reduces the
990 * need for interrupt locking and 64-bit math. However, the 32-bit result
991 * cannot hold a system uptime time larger than approximately 50 days, so the
992 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400995 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400996extern uint32_t k_uptime_get_32(void);
997
998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * This routine computes the elapsed time between the current system uptime
1002 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @param reftime Pointer to a reference time, which is updated to the current
1005 * uptime upon return.
1006 *
1007 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001008 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001009extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001010
1011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * This routine computes the elapsed time between the current system uptime
1015 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1018 * need for interrupt locking and 64-bit math. However, the 32-bit result
1019 * cannot hold an elapsed time larger than approximately 50 days, so the
1020 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * @param reftime Pointer to a reference time, which is updated to the current
1023 * uptime upon return.
1024 *
1025 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001026 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001027extern uint32_t k_uptime_delta_32(int64_t *reftime);
1028
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * This routine returns the current time, as measured by the system's hardware
1033 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037extern uint32_t k_cycle_get_32(void);
1038
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001040 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001041 */
1042
Allan Stephensc98da842016-11-11 15:45:03 -05001043/**
1044 * @cond INTERNAL_HIDDEN
1045 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001046
1047struct k_fifo {
1048 _wait_q_t wait_q;
1049 sys_slist_t data_q;
1050
Anas Nashif2f203c22016-12-18 06:57:45 -05001051 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052};
1053
Allan Stephensc98da842016-11-11 15:45:03 -05001054#define K_FIFO_INITIALIZER(obj) \
1055 { \
1056 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1057 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001058 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001059 }
1060
1061/**
1062 * INTERNAL_HIDDEN @endcond
1063 */
1064
1065/**
1066 * @defgroup fifo_apis Fifo APIs
1067 * @ingroup kernel_apis
1068 * @{
1069 */
1070
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001072 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
1078 * @return N/A
1079 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081
1082/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001085 * This routine adds a data item to @a fifo. A fifo data item must be
1086 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1087 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * @note Can be called by ISRs.
1090 *
1091 * @param fifo Address of the fifo.
1092 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
1094 * @return N/A
1095 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097
1098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * This routine adds a list of data items to @a fifo in one operation.
1102 * The data items must be in a singly-linked list, with the first 32 bits
1103 * each data item pointing to the next data item; the list must be
1104 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * @param fifo Address of the fifo.
1109 * @param head Pointer to first node in singly-linked list.
1110 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
1112 * @return N/A
1113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001114extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115
1116/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * This routine adds a list of data items to @a fifo in one operation.
1120 * The data items must be in a singly-linked list implemented using a
1121 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 * and must be re-initialized via sys_slist_init().
1123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * @note Can be called by ISRs.
1125 *
1126 * @param fifo Address of the fifo.
1127 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001128 *
1129 * @return N/A
1130 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132
1133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * This routine removes a data item from @a fifo in a "first in, first out"
1137 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1140 *
1141 * @param fifo Address of the fifo.
1142 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 * or one of the special values K_NO_WAIT and K_FOREVER.
1144 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001145 * @return Address of the data item if successful; NULL if returned
1146 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001148extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1149
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001155 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001157 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001160 struct k_fifo name \
1161 __in_section(_k_fifo, static, name) = \
1162 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001163
Allan Stephensc98da842016-11-11 15:45:03 -05001164/**
1165 * @} end defgroup fifo_apis
1166 */
1167
1168/**
1169 * @cond INTERNAL_HIDDEN
1170 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171
1172struct k_lifo {
1173 _wait_q_t wait_q;
1174 void *list;
1175
Anas Nashif2f203c22016-12-18 06:57:45 -05001176 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177};
1178
Allan Stephensc98da842016-11-11 15:45:03 -05001179#define K_LIFO_INITIALIZER(obj) \
1180 { \
1181 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1182 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001183 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001184 }
1185
1186/**
1187 * INTERNAL_HIDDEN @endcond
1188 */
1189
1190/**
1191 * @defgroup lifo_apis Lifo APIs
1192 * @ingroup kernel_apis
1193 * @{
1194 */
1195
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001199 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001201 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 *
1203 * @return N/A
1204 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206
1207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001208 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001210 * This routine adds a data item to @a lifo. A lifo data item must be
1211 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1212 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001214 * @note Can be called by ISRs.
1215 *
1216 * @param lifo Address of the lifo.
1217 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
1219 * @return N/A
1220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001221extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222
1223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * This routine removes a data item from @a lifo in a "last in, first out"
1227 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1230 *
1231 * @param lifo Address of the lifo.
1232 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 * or one of the special values K_NO_WAIT and K_FOREVER.
1234 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001235 * @return Address of the data item if successful; NULL if returned
1236 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001238extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1239
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001243 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001245 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001249#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001250 struct k_lifo name \
1251 __in_section(_k_lifo, static, name) = \
1252 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001253
Allan Stephensc98da842016-11-11 15:45:03 -05001254/**
1255 * @} end defgroup lifo_apis
1256 */
1257
1258/**
1259 * @cond INTERNAL_HIDDEN
1260 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001261
1262struct k_stack {
1263 _wait_q_t wait_q;
1264 uint32_t *base, *next, *top;
1265
Anas Nashif2f203c22016-12-18 06:57:45 -05001266 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001267};
1268
Allan Stephensc98da842016-11-11 15:45:03 -05001269#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1270 { \
1271 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1272 .base = stack_buffer, \
1273 .next = stack_buffer, \
1274 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001275 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001276 }
1277
1278/**
1279 * INTERNAL_HIDDEN @endcond
1280 */
1281
1282/**
1283 * @defgroup stack_apis Stack APIs
1284 * @ingroup kernel_apis
1285 * @{
1286 */
1287
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288/**
1289 * @brief Initialize a stack.
1290 *
1291 * This routine initializes a stack object, prior to its first use.
1292 *
1293 * @param stack Address of the stack.
1294 * @param buffer Address of array used to hold stacked values.
1295 * @param num_entries Maximum number of values that can be stacked.
1296 *
1297 * @return N/A
1298 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001299extern void k_stack_init(struct k_stack *stack,
1300 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301
1302/**
1303 * @brief Push an element onto a stack.
1304 *
1305 * This routine adds a 32-bit value @a data to @a stack.
1306 *
1307 * @note Can be called by ISRs.
1308 *
1309 * @param stack Address of the stack.
1310 * @param data Value to push onto the stack.
1311 *
1312 * @return N/A
1313 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001314extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001315
1316/**
1317 * @brief Pop an element from a stack.
1318 *
1319 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1320 * manner and stores the value in @a data.
1321 *
1322 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1323 *
1324 * @param stack Address of the stack.
1325 * @param data Address of area to hold the value popped from the stack.
1326 * @param timeout Waiting period to obtain a value (in milliseconds),
1327 * or one of the special values K_NO_WAIT and K_FOREVER.
1328 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001329 * @retval 0 Element popped from stack.
1330 * @retval -EBUSY Returned without waiting.
1331 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001332 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1334
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001339 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001340 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001342 * @param name Name of the stack.
1343 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001344 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001345#define K_STACK_DEFINE(name, stack_num_entries) \
1346 uint32_t __noinit \
1347 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001348 struct k_stack name \
1349 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001350 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1351 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001352
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001353/**
Allan Stephensc98da842016-11-11 15:45:03 -05001354 * @} end defgroup stack_apis
1355 */
1356
Allan Stephens6bba9b02016-11-16 14:56:54 -05001357struct k_work;
1358
Allan Stephensc98da842016-11-11 15:45:03 -05001359/**
1360 * @defgroup workqueue_apis Workqueue Thread APIs
1361 * @ingroup kernel_apis
1362 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001363 */
1364
Allan Stephens6bba9b02016-11-16 14:56:54 -05001365/**
1366 * @typedef k_work_handler_t
1367 * @brief Work item handler function type.
1368 *
1369 * A work item's handler function is executed by a workqueue's thread
1370 * when the work item is processed by the workqueue.
1371 *
1372 * @param work Address of the work item.
1373 *
1374 * @return N/A
1375 */
1376typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377
1378/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001379 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001381
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382struct k_work_q {
1383 struct k_fifo fifo;
1384};
1385
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001386enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001387 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388};
1389
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390struct k_work {
1391 void *_reserved; /* Used by k_fifo implementation. */
1392 k_work_handler_t handler;
1393 atomic_t flags[1];
1394};
1395
Allan Stephens6bba9b02016-11-16 14:56:54 -05001396struct k_delayed_work {
1397 struct k_work work;
1398 struct _timeout timeout;
1399 struct k_work_q *work_q;
1400};
1401
1402extern struct k_work_q k_sys_work_q;
1403
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001405 * INTERNAL_HIDDEN @endcond
1406 */
1407
1408/**
1409 * @brief Initialize a statically-defined work item.
1410 *
1411 * This macro can be used to initialize a statically-defined workqueue work
1412 * item, prior to its first use. For example,
1413 *
1414 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1415 *
1416 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417 */
1418#define K_WORK_INITIALIZER(work_handler) \
1419 { \
1420 ._reserved = NULL, \
1421 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001422 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423 }
1424
1425/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001426 * @brief Initialize a work item.
1427 *
1428 * This routine initializes a workqueue work item, prior to its first use.
1429 *
1430 * @param work Address of work item.
1431 * @param handler Function to invoke each time work item is processed.
1432 *
1433 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 */
1435static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1436{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001437 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438 work->handler = handler;
1439}
1440
1441/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001442 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001443 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001444 * This routine submits work item @a work to be processed by workqueue
1445 * @a work_q. If the work item is already pending in the workqueue's queue
1446 * as a result of an earlier submission, this routine has no effect on the
1447 * work item. If the work item has already been processed, or is currently
1448 * being processed, its work is considered complete and the work item can be
1449 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001450 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001451 * @warning
1452 * A submitted work item must not be modified until it has been processed
1453 * by the workqueue.
1454 *
1455 * @note Can be called by ISRs.
1456 *
1457 * @param work_q Address of workqueue.
1458 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001459 *
1460 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001461 */
1462static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1463 struct k_work *work)
1464{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001465 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001466 k_fifo_put(&work_q->fifo, work);
1467 }
1468}
1469
1470/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001471 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001472 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001473 * This routine indicates if work item @a work is pending in a workqueue's
1474 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001475 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001476 * @note Can be called by ISRs.
1477 *
1478 * @param work Address of work item.
1479 *
1480 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001481 */
1482static inline int k_work_pending(struct k_work *work)
1483{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001484 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001485}
1486
1487/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001488 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001490 * This routine starts workqueue @a work_q. The workqueue spawns its work
1491 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001493 * @param work_q Address of workqueue.
1494 * @param stack Pointer to work queue thread's stack space.
1495 * @param stack_size Size of the work queue thread's stack (in bytes).
1496 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497 *
1498 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499 */
Allan Stephens904cf972016-10-07 13:59:23 -05001500extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001501 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001503/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001504 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001505 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001506 * This routine initializes a workqueue delayed work item, prior to
1507 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001509 * @param work Address of delayed work item.
1510 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001511 *
1512 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001513 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001514extern void k_delayed_work_init(struct k_delayed_work *work,
1515 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001516
1517/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001518 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001520 * This routine schedules work item @a work to be processed by workqueue
1521 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1522 * an asychronous countdown for the work item and then returns to the caller.
1523 * Only when the countdown completes is the work item actually submitted to
1524 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001526 * Submitting a previously submitted delayed work item that is still
1527 * counting down cancels the existing submission and restarts the countdown
1528 * using the new delay. If the work item is currently pending on the
1529 * workqueue's queue because the countdown has completed it is too late to
1530 * resubmit the item, and resubmission fails without impacting the work item.
1531 * If the work item has already been processed, or is currently being processed,
1532 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001534 * @warning
1535 * A delayed work item must not be modified until it has been processed
1536 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001538 * @note Can be called by ISRs.
1539 *
1540 * @param work_q Address of workqueue.
1541 * @param work Address of delayed work item.
1542 * @param delay Delay before submitting the work item (in milliseconds).
1543 *
1544 * @retval 0 Work item countdown started.
1545 * @retval -EINPROGRESS Work item is already pending.
1546 * @retval -EINVAL Work item is being processed or has completed its work.
1547 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001548 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001549extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1550 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001551 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001552
1553/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001554 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001555 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001556 * This routine cancels the submission of delayed work item @a work.
1557 * A delayed work item can only be cancelled while its countdown is still
1558 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001560 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001562 * @param work Address of delayed work item.
1563 *
1564 * @retval 0 Work item countdown cancelled.
1565 * @retval -EINPROGRESS Work item is already pending.
1566 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001567 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001568extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001569
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001570/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001571 * @brief Submit a work item to the system workqueue.
1572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001573 * This routine submits work item @a work to be processed by the system
1574 * workqueue. If the work item is already pending in the workqueue's queue
1575 * as a result of an earlier submission, this routine has no effect on the
1576 * work item. If the work item has already been processed, or is currently
1577 * being processed, its work is considered complete and the work item can be
1578 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001580 * @warning
1581 * Work items submitted to the system workqueue should avoid using handlers
1582 * that block or yield since this may prevent the system workqueue from
1583 * processing other work items in a timely manner.
1584 *
1585 * @note Can be called by ISRs.
1586 *
1587 * @param work Address of work item.
1588 *
1589 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001590 */
1591static inline void k_work_submit(struct k_work *work)
1592{
1593 k_work_submit_to_queue(&k_sys_work_q, work);
1594}
1595
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001597 * @brief Submit a delayed work item to the system workqueue.
1598 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001599 * This routine schedules work item @a work to be processed by the system
1600 * workqueue after a delay of @a delay milliseconds. The routine initiates
1601 * an asychronous countdown for the work item and then returns to the caller.
1602 * Only when the countdown completes is the work item actually submitted to
1603 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001604 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001605 * Submitting a previously submitted delayed work item that is still
1606 * counting down cancels the existing submission and restarts the countdown
1607 * using the new delay. If the work item is currently pending on the
1608 * workqueue's queue because the countdown has completed it is too late to
1609 * resubmit the item, and resubmission fails without impacting the work item.
1610 * If the work item has already been processed, or is currently being processed,
1611 * its work is considered complete and the work item can be resubmitted.
1612 *
1613 * @warning
1614 * Work items submitted to the system workqueue should avoid using handlers
1615 * that block or yield since this may prevent the system workqueue from
1616 * processing other work items in a timely manner.
1617 *
1618 * @note Can be called by ISRs.
1619 *
1620 * @param work Address of delayed work item.
1621 * @param delay Delay before submitting the work item (in milliseconds).
1622 *
1623 * @retval 0 Work item countdown started.
1624 * @retval -EINPROGRESS Work item is already pending.
1625 * @retval -EINVAL Work item is being processed or has completed its work.
1626 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001627 */
1628static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001629 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001630{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001631 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632}
1633
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001635 * @brief Get time remaining before a delayed work gets scheduled.
1636 *
1637 * This routine computes the (approximate) time remaining before a
1638 * delayed work gets executed. If the delayed work is not waiting to be
1639 * schedules, it returns zero.
1640 *
1641 * @param work Delayed work item.
1642 *
1643 * @return Remaining time (in milliseconds).
1644 */
1645static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1646{
1647 return _timeout_remaining_get(&work->timeout);
1648}
1649
1650/**
Allan Stephensc98da842016-11-11 15:45:03 -05001651 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001652 */
1653
Allan Stephensc98da842016-11-11 15:45:03 -05001654/**
1655 * @cond INTERNAL_HIDDEN
1656 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657
1658struct k_mutex {
1659 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001660 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001661 uint32_t lock_count;
1662 int owner_orig_prio;
1663#ifdef CONFIG_OBJECT_MONITOR
1664 int num_lock_state_changes;
1665 int num_conflicts;
1666#endif
1667
Anas Nashif2f203c22016-12-18 06:57:45 -05001668 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001669};
1670
1671#ifdef CONFIG_OBJECT_MONITOR
1672#define _MUTEX_INIT_OBJECT_MONITOR \
1673 .num_lock_state_changes = 0, .num_conflicts = 0,
1674#else
1675#define _MUTEX_INIT_OBJECT_MONITOR
1676#endif
1677
1678#define K_MUTEX_INITIALIZER(obj) \
1679 { \
1680 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1681 .owner = NULL, \
1682 .lock_count = 0, \
1683 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1684 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001685 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001686 }
1687
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001688/**
Allan Stephensc98da842016-11-11 15:45:03 -05001689 * INTERNAL_HIDDEN @endcond
1690 */
1691
1692/**
1693 * @defgroup mutex_apis Mutex APIs
1694 * @ingroup kernel_apis
1695 * @{
1696 */
1697
1698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001702 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001703 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001706 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001708 struct k_mutex name \
1709 __in_section(_k_mutex, static, name) = \
1710 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001711
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * Upon completion, the mutex is available and does not have an owner.
1718 *
1719 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001720 *
1721 * @return N/A
1722 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001723extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724
1725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * This routine locks @a mutex. If the mutex is locked by another thread,
1729 * the calling thread waits until the mutex becomes available or until
1730 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * A thread is permitted to lock a mutex it has already locked. The operation
1733 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @param mutex Address of the mutex.
1736 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 * or one of the special values K_NO_WAIT and K_FOREVER.
1738 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001739 * @retval 0 Mutex locked.
1740 * @retval -EBUSY Returned without waiting.
1741 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744
1745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001748 * This routine unlocks @a mutex. The mutex must already be locked by the
1749 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
1751 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 * thread.
1754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756 *
1757 * @return N/A
1758 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759extern void k_mutex_unlock(struct k_mutex *mutex);
1760
Allan Stephensc98da842016-11-11 15:45:03 -05001761/**
1762 * @} end defgroup mutex_apis
1763 */
1764
1765/**
1766 * @cond INTERNAL_HIDDEN
1767 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768
1769struct k_sem {
1770 _wait_q_t wait_q;
1771 unsigned int count;
1772 unsigned int limit;
1773
Anas Nashif2f203c22016-12-18 06:57:45 -05001774 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001775};
1776
Allan Stephensc98da842016-11-11 15:45:03 -05001777#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1778 { \
1779 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1780 .count = initial_count, \
1781 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001782 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001783 }
1784
1785/**
1786 * INTERNAL_HIDDEN @endcond
1787 */
1788
1789/**
1790 * @defgroup semaphore_apis Semaphore APIs
1791 * @ingroup kernel_apis
1792 * @{
1793 */
1794
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @param sem Address of the semaphore.
1801 * @param initial_count Initial semaphore count.
1802 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001803 *
1804 * @return N/A
1805 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1807 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001808
1809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1815 *
1816 * @param sem Address of the semaphore.
1817 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001818 * or one of the special values K_NO_WAIT and K_FOREVER.
1819 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001820 * @note When porting code from the nanokernel legacy API to the new API, be
1821 * careful with the return value of this function. The return value is the
1822 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1823 * non-zero means failure, while the nano_sem_take family returns 1 for success
1824 * and 0 for failure.
1825 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001826 * @retval 0 Semaphore taken.
1827 * @retval -EBUSY Returned without waiting.
1828 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001829 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001831
1832/**
1833 * @brief Give a semaphore.
1834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * This routine gives @a sem, unless the semaphore is already at its maximum
1836 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001841 *
1842 * @return N/A
1843 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001844extern void k_sem_give(struct k_sem *sem);
1845
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001846/**
1847 * @brief Reset a semaphore's count to zero.
1848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * This routine sets the count of @a sem to zero.
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 Walsh70c68b92016-09-21 10:37:34 -04001855static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001856{
1857 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858}
1859
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001860/**
1861 * @brief Get a semaphore's count.
1862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001863 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001868 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001869static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870{
1871 return sem->count;
1872}
1873
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001878 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001879 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * @param name Name of the semaphore.
1882 * @param initial_count Initial semaphore count.
1883 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001884 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001885#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001886 struct k_sem name \
1887 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888 K_SEM_INITIALIZER(name, initial_count, count_limit)
1889
Allan Stephensc98da842016-11-11 15:45:03 -05001890/**
1891 * @} end defgroup semaphore_apis
1892 */
1893
1894/**
1895 * @defgroup alert_apis Alert APIs
1896 * @ingroup kernel_apis
1897 * @{
1898 */
1899
Allan Stephens5eceb852016-11-16 10:16:30 -05001900/**
1901 * @typedef k_alert_handler_t
1902 * @brief Alert handler function type.
1903 *
1904 * An alert's alert handler function is invoked by the system workqueue
1905 * when the alert is signalled. The alert handler function is optional,
1906 * and is only invoked if the alert has been initialized with one.
1907 *
1908 * @param alert Address of the alert.
1909 *
1910 * @return 0 if alert has been consumed; non-zero if alert should pend.
1911 */
1912typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001913
1914/**
1915 * @} end defgroup alert_apis
1916 */
1917
1918/**
1919 * @cond INTERNAL_HIDDEN
1920 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001922#define K_ALERT_DEFAULT NULL
1923#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001925struct k_alert {
1926 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927 atomic_t send_count;
1928 struct k_work work_item;
1929 struct k_sem sem;
1930
Anas Nashif2f203c22016-12-18 06:57:45 -05001931 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932};
1933
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001934extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001936#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001938 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001940 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001941 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001942 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001943 }
1944
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945/**
Allan Stephensc98da842016-11-11 15:45:03 -05001946 * INTERNAL_HIDDEN @endcond
1947 */
1948
1949/**
1950 * @addtogroup alert_apis
1951 * @{
1952 */
1953
1954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001955 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001957 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001959 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961 * @param name Name of the alert.
1962 * @param alert_handler Action to take when alert is sent. Specify either
1963 * the address of a function to be invoked by the system workqueue
1964 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1965 * K_ALERT_DEFAULT (which causes the alert to pend).
1966 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001968#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001969 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001970 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001971 K_ALERT_INITIALIZER(name, alert_handler, \
1972 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * @param alert Address of the alert.
1980 * @param handler Action to take when alert is sent. Specify either the address
1981 * of a function to be invoked by the system workqueue thread,
1982 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1983 * K_ALERT_DEFAULT (which causes the alert to pend).
1984 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
1986 * @return N/A
1987 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001988extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1989 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990
1991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001992 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001994 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001996 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1997 *
1998 * @param alert Address of the alert.
1999 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 * or one of the special values K_NO_WAIT and K_FOREVER.
2001 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002002 * @retval 0 Alert received.
2003 * @retval -EBUSY Returned without waiting.
2004 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002006extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007
2008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002011 * This routine signals @a alert. The action specified for @a alert will
2012 * be taken, which may trigger the execution of an alert handler function
2013 * and/or cause the alert to pend (assuming the alert has not reached its
2014 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002016 * @note Can be called by ISRs.
2017 *
2018 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 *
2020 * @return N/A
2021 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002022extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002023
2024/**
Allan Stephensc98da842016-11-11 15:45:03 -05002025 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026 */
2027
Allan Stephensc98da842016-11-11 15:45:03 -05002028/**
2029 * @cond INTERNAL_HIDDEN
2030 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031
2032struct k_msgq {
2033 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002034 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002035 uint32_t max_msgs;
2036 char *buffer_start;
2037 char *buffer_end;
2038 char *read_ptr;
2039 char *write_ptr;
2040 uint32_t used_msgs;
2041
Anas Nashif2f203c22016-12-18 06:57:45 -05002042 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043};
2044
Peter Mitsis1da807e2016-10-06 11:36:59 -04002045#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046 { \
2047 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002048 .max_msgs = q_max_msgs, \
2049 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002051 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002052 .read_ptr = q_buffer, \
2053 .write_ptr = q_buffer, \
2054 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002055 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056 }
2057
Peter Mitsis1da807e2016-10-06 11:36:59 -04002058/**
Allan Stephensc98da842016-11-11 15:45:03 -05002059 * INTERNAL_HIDDEN @endcond
2060 */
2061
2062/**
2063 * @defgroup msgq_apis Message Queue APIs
2064 * @ingroup kernel_apis
2065 * @{
2066 */
2067
2068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2072 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002073 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2074 * message is similarly aligned to this boundary, @a q_msg_size must also be
2075 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * The message queue can be accessed outside the module where it is defined
2078 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002080 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082 * @param q_name Name of the message queue.
2083 * @param q_msg_size Message size (in bytes).
2084 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002085 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002086 */
2087#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2088 static char __noinit __aligned(q_align) \
2089 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002090 struct k_msgq q_name \
2091 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002092 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2093 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094
Peter Mitsisd7a37502016-10-13 11:37:40 -04002095/**
2096 * @brief Initialize a message queue.
2097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * This routine initializes a message queue object, prior to its first use.
2099 *
Allan Stephensda827222016-11-09 14:23:58 -06002100 * The message queue's ring buffer must contain space for @a max_msgs messages,
2101 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2102 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2103 * that each message is similarly aligned to this boundary, @a q_msg_size
2104 * must also be a multiple of N.
2105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * @param q Address of the message queue.
2107 * @param buffer Pointer to ring buffer that holds queued messages.
2108 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002109 * @param max_msgs Maximum number of messages that can be queued.
2110 *
2111 * @return N/A
2112 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002113extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002114 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002115
2116/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002120 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002121 * @note Can be called by ISRs.
2122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * @param q Address of the message queue.
2124 * @param data Pointer to the message.
2125 * @param timeout Waiting period to add the message (in milliseconds),
2126 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002127 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002128 * @retval 0 Message sent.
2129 * @retval -ENOMSG Returned without waiting or queue purged.
2130 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002131 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002133
2134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * This routine receives a message from message queue @a q in a "first in,
2138 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002139 *
Allan Stephensc98da842016-11-11 15:45:03 -05002140 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002142 * @param q Address of the message queue.
2143 * @param data Address of area to hold the received message.
2144 * @param timeout Waiting period to receive the message (in milliseconds),
2145 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002146 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002147 * @retval 0 Message received.
2148 * @retval -ENOMSG Returned without waiting.
2149 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002150 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002152
2153/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002154 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002156 * This routine discards all unreceived messages in a message queue's ring
2157 * buffer. Any threads that are blocked waiting to send a message to the
2158 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002160 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002161 *
2162 * @return N/A
2163 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164extern void k_msgq_purge(struct k_msgq *q);
2165
Peter Mitsis67be2492016-10-07 11:44:34 -04002166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002169 * This routine returns the number of unused entries in a message queue's
2170 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * @param q Address of the message queue.
2173 *
2174 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002175 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002176static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002177{
2178 return q->max_msgs - q->used_msgs;
2179}
2180
Peter Mitsisd7a37502016-10-13 11:37:40 -04002181/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * @param q Address of the message queue.
2187 *
2188 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002189 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002190static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191{
2192 return q->used_msgs;
2193}
2194
Allan Stephensc98da842016-11-11 15:45:03 -05002195/**
2196 * @} end defgroup msgq_apis
2197 */
2198
2199/**
2200 * @defgroup mem_pool_apis Memory Pool APIs
2201 * @ingroup kernel_apis
2202 * @{
2203 */
2204
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002206 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207 void *addr_in_pool;
2208 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002209 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210};
2211
Allan Stephensc98da842016-11-11 15:45:03 -05002212/**
2213 * @} end defgroup mem_pool_apis
2214 */
2215
2216/**
2217 * @defgroup mailbox_apis Mailbox APIs
2218 * @ingroup kernel_apis
2219 * @{
2220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221
2222struct k_mbox_msg {
2223 /** internal use only - needed for legacy API support */
2224 uint32_t _mailbox;
2225 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002226 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227 /** application-defined information value */
2228 uint32_t info;
2229 /** sender's message data buffer */
2230 void *tx_data;
2231 /** internal use only - needed for legacy API support */
2232 void *_rx_data;
2233 /** message data block descriptor */
2234 struct k_mem_block tx_block;
2235 /** source thread id */
2236 k_tid_t rx_source_thread;
2237 /** target thread id */
2238 k_tid_t tx_target_thread;
2239 /** internal use only - thread waiting on send (may be a dummy) */
2240 k_tid_t _syncing_thread;
2241#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2242 /** internal use only - semaphore used during asynchronous send */
2243 struct k_sem *_async_sem;
2244#endif
2245};
2246
Allan Stephensc98da842016-11-11 15:45:03 -05002247/**
2248 * @cond INTERNAL_HIDDEN
2249 */
2250
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251struct k_mbox {
2252 _wait_q_t tx_msg_queue;
2253 _wait_q_t rx_msg_queue;
2254
Anas Nashif2f203c22016-12-18 06:57:45 -05002255 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256};
2257
2258#define K_MBOX_INITIALIZER(obj) \
2259 { \
2260 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2261 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002262 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263 }
2264
Peter Mitsis12092702016-10-14 12:57:23 -04002265/**
Allan Stephensc98da842016-11-11 15:45:03 -05002266 * INTERNAL_HIDDEN @endcond
2267 */
2268
2269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002272 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002274 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002277 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002279 struct k_mbox name \
2280 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 K_MBOX_INITIALIZER(name) \
2282
Peter Mitsis12092702016-10-14 12:57:23 -04002283/**
2284 * @brief Initialize a mailbox.
2285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002286 * This routine initializes a mailbox object, prior to its first use.
2287 *
2288 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002289 *
2290 * @return N/A
2291 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292extern void k_mbox_init(struct k_mbox *mbox);
2293
Peter Mitsis12092702016-10-14 12:57:23 -04002294/**
2295 * @brief Send a mailbox message in a synchronous manner.
2296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * This routine sends a message to @a mbox and waits for a receiver to both
2298 * receive and process it. The message data may be in a buffer, in a memory
2299 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * @param mbox Address of the mailbox.
2302 * @param tx_msg Address of the transmit message descriptor.
2303 * @param timeout Waiting period for the message to be received (in
2304 * milliseconds), or one of the special values K_NO_WAIT
2305 * and K_FOREVER. Once the message has been received,
2306 * this routine waits as long as necessary for the message
2307 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002308 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002309 * @retval 0 Message sent.
2310 * @retval -ENOMSG Returned without waiting.
2311 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002312 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002313extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002315
Peter Mitsis12092702016-10-14 12:57:23 -04002316/**
2317 * @brief Send a mailbox message in an asynchronous manner.
2318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * This routine sends a message to @a mbox without waiting for a receiver
2320 * to process it. The message data may be in a buffer, in a memory pool block,
2321 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2322 * will be given when the message has been both received and completely
2323 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @param mbox Address of the mailbox.
2326 * @param tx_msg Address of the transmit message descriptor.
2327 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002328 *
2329 * @return N/A
2330 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002331extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332 struct k_sem *sem);
2333
Peter Mitsis12092702016-10-14 12:57:23 -04002334/**
2335 * @brief Receive a mailbox message.
2336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * This routine receives a message from @a mbox, then optionally retrieves
2338 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @param mbox Address of the mailbox.
2341 * @param rx_msg Address of the receive message descriptor.
2342 * @param buffer Address of the buffer to receive data, or NULL to defer data
2343 * retrieval and message disposal until later.
2344 * @param timeout Waiting period for a message to be received (in
2345 * milliseconds), or one of the special values K_NO_WAIT
2346 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002347 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002348 * @retval 0 Message received.
2349 * @retval -ENOMSG Returned without waiting.
2350 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002351 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002352extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002354
2355/**
2356 * @brief Retrieve mailbox message data into a buffer.
2357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * This routine completes the processing of a received message by retrieving
2359 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002360 *
2361 * Alternatively, this routine can be used to dispose of a received message
2362 * without retrieving its data.
2363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002364 * @param rx_msg Address of the receive message descriptor.
2365 * @param buffer Address of the buffer to receive data, or NULL to discard
2366 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002367 *
2368 * @return N/A
2369 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002370extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002371
2372/**
2373 * @brief Retrieve mailbox message data into a memory pool block.
2374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * This routine completes the processing of a received message by retrieving
2376 * its data into a memory pool block, then disposing of the message.
2377 * The memory pool block that results from successful retrieval must be
2378 * returned to the pool once the data has been processed, even in cases
2379 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002380 *
2381 * Alternatively, this routine can be used to dispose of a received message
2382 * without retrieving its data. In this case there is no need to return a
2383 * memory pool block to the pool.
2384 *
2385 * This routine allocates a new memory pool block for the data only if the
2386 * data is not already in one. If a new block cannot be allocated, the routine
2387 * returns a failure code and the received message is left unchanged. This
2388 * permits the caller to reattempt data retrieval at a later time or to dispose
2389 * of the received message without retrieving its data.
2390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @param rx_msg Address of a receive message descriptor.
2392 * @param pool Address of memory pool, or NULL to discard data.
2393 * @param block Address of the area to hold memory pool block info.
2394 * @param timeout Waiting period to wait for a memory pool block (in
2395 * milliseconds), or one of the special values K_NO_WAIT
2396 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002397 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002398 * @retval 0 Data retrieved.
2399 * @retval -ENOMEM Returned without waiting.
2400 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002401 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002402extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002403 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404 struct k_mem_block *block, int32_t timeout);
2405
Allan Stephensc98da842016-11-11 15:45:03 -05002406/**
2407 * @} end defgroup mailbox_apis
2408 */
2409
2410/**
2411 * @cond INTERNAL_HIDDEN
2412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413
2414struct k_pipe {
2415 unsigned char *buffer; /* Pipe buffer: may be NULL */
2416 size_t size; /* Buffer size */
2417 size_t bytes_used; /* # bytes used in buffer */
2418 size_t read_index; /* Where in buffer to read from */
2419 size_t write_index; /* Where in buffer to write */
2420
2421 struct {
2422 _wait_q_t readers; /* Reader wait queue */
2423 _wait_q_t writers; /* Writer wait queue */
2424 } wait_q;
2425
Anas Nashif2f203c22016-12-18 06:57:45 -05002426 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427};
2428
Peter Mitsise5d9c582016-10-14 14:44:57 -04002429#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430 { \
2431 .buffer = pipe_buffer, \
2432 .size = pipe_buffer_size, \
2433 .bytes_used = 0, \
2434 .read_index = 0, \
2435 .write_index = 0, \
2436 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2437 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002438 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439 }
2440
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441/**
Allan Stephensc98da842016-11-11 15:45:03 -05002442 * INTERNAL_HIDDEN @endcond
2443 */
2444
2445/**
2446 * @defgroup pipe_apis Pipe APIs
2447 * @ingroup kernel_apis
2448 * @{
2449 */
2450
2451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002456 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002458 * @param name Name of the pipe.
2459 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2460 * or zero if no ring buffer is used.
2461 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002463#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2464 static unsigned char __noinit __aligned(pipe_align) \
2465 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002466 struct k_pipe name \
2467 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002468 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * This routine initializes a pipe object, prior to its first use.
2474 *
2475 * @param pipe Address of the pipe.
2476 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2477 * is used.
2478 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2479 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 *
2481 * @return N/A
2482 */
2483extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2484 size_t size);
2485
2486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 * @param pipe Address of the pipe.
2492 * @param data Address of data to write.
2493 * @param bytes_to_write Size of data (in bytes).
2494 * @param bytes_written Address of area to hold the number of bytes written.
2495 * @param min_xfer Minimum number of bytes to write.
2496 * @param timeout Waiting period to wait for the data to be written (in
2497 * milliseconds), or one of the special values K_NO_WAIT
2498 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002500 * @retval 0 At least @a min_xfer bytes of data were written.
2501 * @retval -EIO Returned without waiting; zero data bytes were written.
2502 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002505extern int k_pipe_put(struct k_pipe *pipe, void *data,
2506 size_t bytes_to_write, size_t *bytes_written,
2507 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508
2509/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * @param pipe Address of the pipe.
2515 * @param data Address to place the data read from pipe.
2516 * @param bytes_to_read Maximum number of data bytes to read.
2517 * @param bytes_read Address of area to hold the number of bytes read.
2518 * @param min_xfer Minimum number of data bytes to read.
2519 * @param timeout Waiting period to wait for the data to be read (in
2520 * milliseconds), or one of the special values K_NO_WAIT
2521 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002523 * @retval 0 At least @a min_xfer bytes of data were read.
2524 * @retval -EIO Returned without waiting; zero data bytes were read.
2525 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002528extern int k_pipe_get(struct k_pipe *pipe, void *data,
2529 size_t bytes_to_read, size_t *bytes_read,
2530 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531
2532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * This routine writes the data contained in a memory block to @a pipe.
2536 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540 * @param block Memory block containing data to send
2541 * @param size Number of data bytes in memory block to send
2542 * @param sem Semaphore to signal upon completion (else NULL)
2543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545 */
2546extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2547 size_t size, struct k_sem *sem);
2548
2549/**
Allan Stephensc98da842016-11-11 15:45:03 -05002550 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551 */
2552
Allan Stephensc98da842016-11-11 15:45:03 -05002553/**
2554 * @cond INTERNAL_HIDDEN
2555 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002557struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002559 uint32_t num_blocks;
2560 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 char *buffer;
2562 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002563 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564
Anas Nashif2f203c22016-12-18 06:57:45 -05002565 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566};
2567
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002568#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2569 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570 { \
2571 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002572 .num_blocks = slab_num_blocks, \
2573 .block_size = slab_block_size, \
2574 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575 .free_list = NULL, \
2576 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002577 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 }
2579
Peter Mitsis578f9112016-10-07 13:50:31 -04002580/**
Allan Stephensc98da842016-11-11 15:45:03 -05002581 * INTERNAL_HIDDEN @endcond
2582 */
2583
2584/**
2585 * @defgroup mem_slab_apis Memory Slab APIs
2586 * @ingroup kernel_apis
2587 * @{
2588 */
2589
2590/**
Allan Stephensda827222016-11-09 14:23:58 -06002591 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002592 *
Allan Stephensda827222016-11-09 14:23:58 -06002593 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002595 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2596 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002597 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002598 *
Allan Stephensda827222016-11-09 14:23:58 -06002599 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002602 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * @param name Name of the memory slab.
2605 * @param slab_block_size Size of each memory block (in bytes).
2606 * @param slab_num_blocks Number memory blocks.
2607 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002608 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002609#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2610 char __noinit __aligned(slab_align) \
2611 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2612 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002613 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002614 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2615 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002617/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002618 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002621 *
Allan Stephensda827222016-11-09 14:23:58 -06002622 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2623 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2624 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2625 * To ensure that each memory block is similarly aligned to this boundary,
2626 * @a slab_block_size must also be a multiple of N.
2627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * @param slab Address of the memory slab.
2629 * @param buffer Pointer to buffer used for the memory blocks.
2630 * @param block_size Size of each memory block (in bytes).
2631 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002632 *
2633 * @return N/A
2634 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002635extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002636 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002637
2638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * @param slab Address of the memory slab.
2644 * @param mem Pointer to block address area.
2645 * @param timeout Maximum time to wait for operation to complete
2646 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2647 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002648 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002649 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002651 * @retval -ENOMEM Returned without waiting.
2652 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002653 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002654extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2655 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002656
2657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * This routine releases a previously allocated memory block back to its
2661 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * @param slab Address of the memory slab.
2664 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002665 *
2666 * @return N/A
2667 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002668extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002670/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * This routine gets the number of memory blocks that are currently
2674 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002679 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002680static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002682 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683}
2684
Peter Mitsisc001aa82016-10-13 13:53:37 -04002685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * This routine gets the number of memory blocks that are currently
2689 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002691 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002693 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002694 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002695static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002696{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002697 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002698}
2699
Allan Stephensc98da842016-11-11 15:45:03 -05002700/**
2701 * @} end defgroup mem_slab_apis
2702 */
2703
2704/**
2705 * @cond INTERNAL_HIDDEN
2706 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002708/*
2709 * Memory pool requires a buffer and two arrays of structures for the
2710 * memory block accounting:
2711 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2712 * status of four blocks of memory.
2713 */
2714struct k_mem_pool_quad_block {
2715 char *mem_blocks; /* pointer to the first of four memory blocks */
2716 uint32_t mem_status; /* four bits. If bit is set, memory block is
2717 allocated */
2718};
2719/*
2720 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2721 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2722 * block size is 4 times less than the previous one and thus requires 4 times
2723 * bigger array of k_mem_pool_quad_block structures to keep track of the
2724 * memory blocks.
2725 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002727/*
2728 * The array of k_mem_pool_block_set keeps the information of each array of
2729 * k_mem_pool_quad_block structures
2730 */
2731struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002732 size_t block_size; /* memory block size */
2733 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002734 struct k_mem_pool_quad_block *quad_block;
2735 int count;
2736};
2737
2738/* Memory pool descriptor */
2739struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002740 size_t max_block_size;
2741 size_t min_block_size;
2742 uint32_t nr_of_maxblocks;
2743 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002744 struct k_mem_pool_block_set *block_set;
2745 char *bufblock;
2746 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002747 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748};
2749
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002750#ifdef CONFIG_ARM
2751#define _SECTION_TYPE_SIGN "%"
2752#else
2753#define _SECTION_TYPE_SIGN "@"
2754#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002756/*
2757 * Static memory pool initialization
2758 */
Allan Stephensc98da842016-11-11 15:45:03 -05002759
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002760/*
2761 * Use .altmacro to be able to recalculate values and pass them as string
2762 * arguments when calling assembler macros resursively
2763 */
2764__asm__(".altmacro\n\t");
2765
2766/*
2767 * Recursively calls a macro
2768 * The followig global symbols need to be initialized:
2769 * __memory_pool_max_block_size - maximal size of the memory block
2770 * __memory_pool_min_block_size - minimal size of the memory block
2771 * Notes:
2772 * Global symbols are used due the fact that assembler macro allows only
2773 * one argument be passed with the % conversion
2774 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2775 * is used instead of / 4.
2776 * n_max argument needs to go first in the invoked macro, as some
2777 * assemblers concatenate \name and %(\n_max * 4) arguments
2778 * if \name goes first
2779 */
2780__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2781 ".ifge __memory_pool_max_block_size >> 2 -"
2782 " __memory_pool_min_block_size\n\t\t"
2783 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2784 "\\macro_name %(\\n_max * 4) \\name\n\t"
2785 ".endif\n\t"
2786 ".endm\n");
2787
2788/*
2789 * Build quad blocks
2790 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2791 * structures and recursively calls itself for the next array, 4 times
2792 * larger.
2793 * The followig global symbols need to be initialized:
2794 * __memory_pool_max_block_size - maximal size of the memory block
2795 * __memory_pool_min_block_size - minimal size of the memory block
2796 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2797 */
2798__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002799 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002800 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2801 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2802 ".if \\n_max % 4\n\t\t"
2803 ".skip __memory_pool_quad_block_size\n\t"
2804 ".endif\n\t"
2805 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2806 ".endm\n");
2807
2808/*
2809 * Build block sets and initialize them
2810 * Macro initializes the k_mem_pool_block_set structure and
2811 * recursively calls itself for the next one.
2812 * The followig global symbols need to be initialized:
2813 * __memory_pool_max_block_size - maximal size of the memory block
2814 * __memory_pool_min_block_size - minimal size of the memory block
2815 * __memory_pool_block_set_count, the number of the elements in the
2816 * block set array must be set to 0. Macro calculates it's real
2817 * value.
2818 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2819 * structures, _build_quad_blocks must be called prior it.
2820 */
2821__asm__(".macro _build_block_set n_max, name\n\t"
2822 ".int __memory_pool_max_block_size\n\t" /* block_size */
2823 ".if \\n_max % 4\n\t\t"
2824 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2825 ".else\n\t\t"
2826 ".int \\n_max >> 2\n\t"
2827 ".endif\n\t"
2828 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2829 ".int 0\n\t" /* count */
2830 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2831 "__do_recurse _build_block_set \\name \\n_max\n\t"
2832 ".endm\n");
2833
2834/*
2835 * Build a memory pool structure and initialize it
2836 * Macro uses __memory_pool_block_set_count global symbol,
2837 * block set addresses and buffer address, it may be called only after
2838 * _build_block_set
2839 */
2840__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002841 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002842 _SECTION_TYPE_SIGN "progbits\n\t"
2843 ".globl \\name\n\t"
2844 "\\name:\n\t"
2845 ".int \\max_size\n\t" /* max_block_size */
2846 ".int \\min_size\n\t" /* min_block_size */
2847 ".int \\n_max\n\t" /* nr_of_maxblocks */
2848 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2849 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2850 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2851 ".int 0\n\t" /* wait_q->head */
2852 ".int 0\n\t" /* wait_q->next */
2853 ".popsection\n\t"
2854 ".endm\n");
2855
2856#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2857 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2858 _SECTION_TYPE_SIGN "progbits\n\t"); \
2859 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2860 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2861 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2862 STRINGIFY(name) "\n\t"); \
2863 __asm__(".popsection\n\t")
2864
2865#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2866 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2867 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2868 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2869 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002870 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002871 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2872 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2873 STRINGIFY(name) "\n\t"); \
2874 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2875 __asm__(".int __memory_pool_block_set_count\n\t"); \
2876 __asm__(".popsection\n\t"); \
2877 extern uint32_t _mem_pool_block_set_count_##name; \
2878 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2879
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002880#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2881 char __noinit __aligned(align) \
2882 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002883
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002884/*
2885 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2886 * to __memory_pool_quad_block_size absolute symbol.
2887 * This function does not get called, but compiler calculates the value and
2888 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2889 */
2890static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2891{
2892 __asm__(".globl __memory_pool_quad_block_size\n\t"
2893#ifdef CONFIG_NIOS2
2894 "__memory_pool_quad_block_size = %0\n\t"
2895#else
2896 "__memory_pool_quad_block_size = %c0\n\t"
2897#endif
2898 :
2899 : "n"(sizeof(struct k_mem_pool_quad_block)));
2900}
2901
2902/**
Allan Stephensc98da842016-11-11 15:45:03 -05002903 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002904 */
2905
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002906/**
Allan Stephensc98da842016-11-11 15:45:03 -05002907 * @addtogroup mem_pool_apis
2908 * @{
2909 */
2910
2911/**
2912 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2915 * long. The memory pool allows blocks to be repeatedly partitioned into
2916 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2917 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002918 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002920 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 * If the pool is to be accessed outside the module where it is defined, it
2922 * can be declared via
2923 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002924 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * @param name Name of the memory pool.
2927 * @param min_size Size of the smallest blocks in the pool (in bytes).
2928 * @param max_size Size of the largest blocks in the pool (in bytes).
2929 * @param n_max Number of maximum sized blocks in the pool.
2930 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002931 */
2932#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002933 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2934 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002935 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002936 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2937 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2938 extern struct k_mem_pool name
2939
Peter Mitsis937042c2016-10-13 13:18:26 -04002940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * @param pool Address of the memory pool.
2946 * @param block Pointer to block descriptor for the allocated memory.
2947 * @param size Amount of memory to allocate (in bytes).
2948 * @param timeout Maximum time to wait for operation to complete
2949 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2950 * or K_FOREVER to wait as long as necessary.
2951 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002952 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002954 * @retval -ENOMEM Returned without waiting.
2955 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002956 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002957extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002958 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002959
2960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * This routine releases a previously allocated memory block back to its
2964 * memory pool.
2965 *
2966 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002967 *
2968 * @return N/A
2969 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002971
2972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * This routine instructs a memory pool to concatenate unused memory blocks
2976 * into larger blocks wherever possible. Manually defragmenting the memory
2977 * pool may speed up future allocations of memory blocks by eliminating the
2978 * need for the memory pool to perform an automatic partial defragmentation.
2979 *
2980 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002981 *
2982 * @return N/A
2983 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002984extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002985
2986/**
Allan Stephensc98da842016-11-11 15:45:03 -05002987 * @} end addtogroup mem_pool_apis
2988 */
2989
2990/**
2991 * @defgroup heap_apis Heap Memory Pool APIs
2992 * @ingroup kernel_apis
2993 * @{
2994 */
2995
2996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003000 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003005 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003006extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003007
3008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003010 *
3011 * This routine provides traditional free() semantics. The memory being
3012 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003013 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003014 * If @a ptr is NULL, no operation is performed.
3015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003017 *
3018 * @return N/A
3019 */
3020extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021
Allan Stephensc98da842016-11-11 15:45:03 -05003022/**
3023 * @} end defgroup heap_apis
3024 */
3025
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003026/**
3027 * @brief Make the CPU idle.
3028 *
3029 * This function makes the CPU idle until an event wakes it up.
3030 *
3031 * In a regular system, the idle thread should be the only thread responsible
3032 * for making the CPU idle and triggering any type of power management.
3033 * However, in some more constrained systems, such as a single-threaded system,
3034 * the only thread would be responsible for this if needed.
3035 *
3036 * @return N/A
3037 */
3038extern void k_cpu_idle(void);
3039
3040/**
3041 * @brief Make the CPU idle in an atomic fashion.
3042 *
3043 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3044 * must be done atomically before making the CPU idle.
3045 *
3046 * @param key Interrupt locking key obtained from irq_lock().
3047 *
3048 * @return N/A
3049 */
3050extern void k_cpu_atomic_idle(unsigned int key);
3051
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003052/*
3053 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
3054 * hook into the device subsystem, which itself uses nanokernel semaphores,
3055 * and thus currently requires the definition of nano_sem.
3056 */
3057#include <legacy.h>
3058#include <arch/cpu.h>
3059
3060/*
3061 * private APIs that are utilized by one or more public APIs
3062 */
3063
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003064#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003066#else
3067#define _init_static_threads() do { } while ((0))
3068#endif
3069
3070extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003071extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072
3073#ifdef __cplusplus
3074}
3075#endif
3076
Andrew Boiee004dec2016-11-07 09:01:19 -08003077#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3078/*
3079 * Define new and delete operators.
3080 * At this moment, the operators do nothing since objects are supposed
3081 * to be statically allocated.
3082 */
3083inline void operator delete(void *ptr)
3084{
3085 (void)ptr;
3086}
3087
3088inline void operator delete[](void *ptr)
3089{
3090 (void)ptr;
3091}
3092
3093inline void *operator new(size_t size)
3094{
3095 (void)size;
3096 return NULL;
3097}
3098
3099inline void *operator new[](size_t size)
3100{
3101 (void)size;
3102 return NULL;
3103}
3104
3105/* Placement versions of operator new and delete */
3106inline void operator delete(void *ptr1, void *ptr2)
3107{
3108 (void)ptr1;
3109 (void)ptr2;
3110}
3111
3112inline void operator delete[](void *ptr1, void *ptr2)
3113{
3114 (void)ptr1;
3115 (void)ptr2;
3116}
3117
3118inline void *operator new(size_t size, void *ptr)
3119{
3120 (void)size;
3121 return ptr;
3122}
3123
3124inline void *operator new[](size_t size, void *ptr)
3125{
3126 (void)size;
3127 return ptr;
3128}
3129
3130#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3131
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132#endif /* _kernel__h_ */