blob: 912f2e2cf2f30e27810fd459c51fc2a1cf34a02e [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050035#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050036#include <kernel_version.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Anas Nashifbbb157d2017-01-15 08:46:31 -050042/**
43 * @brief Kernel APIs
44 * @defgroup kernel_apis Kernel APIs
45 * @{
46 * @}
47 */
48
Anas Nashif61f4b242016-11-18 10:53:59 -050049#ifdef CONFIG_KERNEL_DEBUG
50#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
56#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
57#define K_PRIO_PREEMPT(x) (x)
58
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_ANY NULL
60#define K_END NULL
61
Benjamin Walsh456c6da2016-09-02 18:55:39 -040062#if CONFIG_NUM_COOP_PRIORITIES > 0
63#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
64#else
65#define K_HIGHEST_THREAD_PRIO 0
66#endif
67
68#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
69#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
70#else
71#define K_LOWEST_THREAD_PRIO -1
72#endif
73
Benjamin Walshfab8d922016-11-08 15:36:36 -050074#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
75
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
77#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
78
79typedef sys_dlist_t _wait_q_t;
80
Anas Nashif2f203c22016-12-18 06:57:45 -050081#ifdef CONFIG_OBJECT_TRACING
82#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
83#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084#else
Anas Nashif2f203c22016-12-18 06:57:45 -050085#define _OBJECT_TRACING_INIT
86#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#endif
88
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050089#define tcs k_thread
90struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040091struct k_mutex;
92struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040093struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_msgq;
95struct k_mbox;
96struct k_pipe;
97struct k_fifo;
98struct k_lifo;
99struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400100struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101struct k_mem_pool;
102struct k_timer;
103
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400104typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400106enum execution_context_types {
107 K_ISR = 0,
108 K_COOP_THREAD,
109 K_PREEMPT_THREAD,
110};
111
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400112/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100113 * @defgroup profiling_apis Profiling APIs
114 * @ingroup kernel_apis
115 * @{
116 */
117
118/**
119 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
120 *
121 * This routine calls @ref stack_analyze on the 4 call stacks declared and
122 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
123 *
124 * CONFIG_MAIN_STACK_SIZE
125 * CONFIG_IDLE_STACK_SIZE
126 * CONFIG_ISR_STACK_SIZE
127 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
128 *
129 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
130 * produce output.
131 *
132 * @return N/A
133 */
134extern void k_call_stacks_analyze(void);
135
136/**
137 * @} end defgroup profiling_apis
138 */
139
140/**
Allan Stephensc98da842016-11-11 15:45:03 -0500141 * @defgroup thread_apis Thread APIs
142 * @ingroup kernel_apis
143 * @{
144 */
145
146/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500147 * @typedef k_thread_entry_t
148 * @brief Thread entry point function type.
149 *
150 * A thread's entry point function is invoked when the thread starts executing.
151 * Up to 3 argument values can be passed to the function.
152 *
153 * The thread terminates execution permanently if the entry point function
154 * returns. The thread is responsible for releasing any shared resources
155 * it may own (such as mutexes and dynamically allocated memory), prior to
156 * returning.
157 *
158 * @param p1 First argument.
159 * @param p2 Second argument.
160 * @param p3 Third argument.
161 *
162 * @return N/A
163 */
164typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
165
166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500167 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500169 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500171 * The new thread may be scheduled for immediate execution or a delayed start.
172 * If the newly spawned thread does not have a delayed start the kernel
173 * scheduler may preempt the current thread to allow the new thread to
174 * execute.
175 *
176 * Thread options are architecture-specific, and can include K_ESSENTIAL,
177 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
178 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400179 *
180 * @param stack Pointer to the stack space.
181 * @param stack_size Stack size in bytes.
182 * @param entry Thread entry function.
183 * @param p1 1st entry point parameter.
184 * @param p2 2nd entry point parameter.
185 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500186 * @param prio Thread priority.
187 * @param options Thread options.
188 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500190 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400191 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500192extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500193 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400194 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500195 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400196
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400199 *
Allan Stephensc98da842016-11-11 15:45:03 -0500200 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500201 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500203 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400204 *
205 * @return N/A
206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400207extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208
209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211 *
212 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500213 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400215 * @return N/A
216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400217extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218
219/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500222 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400223 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500224 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 *
226 * @return N/A
227 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400228extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400229
230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500231 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500233 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500235 * If @a thread is not currently sleeping, the routine has no effect.
236 *
237 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
239 * @return N/A
240 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400241extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242
243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500244 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500246 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400247 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400248extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400249
250/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500251 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500253 * This routine prevents @a thread from executing if it has not yet started
254 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500256 * @param thread ID of thread to cancel.
257 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500258 * @retval 0 Thread spawning cancelled.
259 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400260 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261extern int k_thread_cancel(k_tid_t thread);
262
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400263/**
Allan Stephensc98da842016-11-11 15:45:03 -0500264 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500266 * This routine permanently stops execution of @a thread. The thread is taken
267 * off all kernel queues it is part of (i.e. the ready queue, the timeout
268 * queue, or a kernel object wait queue). However, any kernel resources the
269 * thread might currently own (such as mutexes or memory blocks) are not
270 * released. It is the responsibility of the caller of this routine to ensure
271 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500273 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400274 *
275 * @return N/A
276 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400277extern void k_thread_abort(k_tid_t thread);
278
Allan Stephensc98da842016-11-11 15:45:03 -0500279/**
280 * @cond INTERNAL_HIDDEN
281 */
282
Benjamin Walshd211a522016-12-06 11:44:01 -0500283/* timeout has timed out and is not on _timeout_q anymore */
284#define _EXPIRED (-2)
285
286/* timeout is not in use */
287#define _INACTIVE (-1)
288
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400289#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400290#define _THREAD_TIMEOUT_INIT(obj) \
291 (obj).nano_timeout = { \
292 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400293 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400294 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500295 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400296 },
297#else
298#define _THREAD_TIMEOUT_INIT(obj)
299#endif
300
301#ifdef CONFIG_ERRNO
302#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
303#else
304#define _THREAD_ERRNO_INIT(obj)
305#endif
306
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400307struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400308 union {
309 char *init_stack;
310 struct k_thread *thread;
311 };
312 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500313 void (*init_entry)(void *, void *, void *);
314 void *init_p1;
315 void *init_p2;
316 void *init_p3;
317 int init_prio;
318 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400319 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500320 void (*init_abort)(void);
321 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400322};
323
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400324#define _THREAD_INITIALIZER(stack, stack_size, \
325 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500326 prio, options, delay, abort, groups) \
327 { \
328 .init_stack = (stack), \
329 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400330 .init_entry = (void (*)(void *, void *, void *))entry, \
331 .init_p1 = (void *)p1, \
332 .init_p2 = (void *)p2, \
333 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500334 .init_prio = (prio), \
335 .init_options = (options), \
336 .init_delay = (delay), \
337 .init_abort = (abort), \
338 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400339 }
340
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400341/**
Allan Stephensc98da842016-11-11 15:45:03 -0500342 * INTERNAL_HIDDEN @endcond
343 */
344
345/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500346 * @brief Statically define and initialize a thread.
347 *
348 * The thread may be scheduled for immediate execution or a delayed start.
349 *
350 * Thread options are architecture-specific, and can include K_ESSENTIAL,
351 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
352 * them using "|" (the logical OR operator).
353 *
354 * The ID of the thread can be accessed using:
355 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500356 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500357 *
358 * @param name Name of the thread.
359 * @param stack_size Stack size in bytes.
360 * @param entry Thread entry function.
361 * @param p1 1st entry point parameter.
362 * @param p2 2nd entry point parameter.
363 * @param p3 3rd entry point parameter.
364 * @param prio Thread priority.
365 * @param options Thread options.
366 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400367 *
368 * @internal It has been observed that the x86 compiler by default aligns
369 * these _static_thread_data structures to 32-byte boundaries, thereby
370 * wasting space. To work around this, force a 4-byte alignment.
371 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500372#define K_THREAD_DEFINE(name, stack_size, \
373 entry, p1, p2, p3, \
374 prio, options, delay) \
375 char __noinit __stack _k_thread_obj_##name[stack_size]; \
376 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500377 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500378 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
379 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500380 NULL, 0); \
381 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400382
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500384 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500386 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * @param thread ID of thread whose priority is needed.
389 *
390 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400391 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500392extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400393
394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500395 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 *
399 * Rescheduling can occur immediately depending on the priority @a thread is
400 * set to:
401 *
402 * - If its priority is raised above the priority of the caller of this
403 * function, and the caller is preemptible, @a thread will be scheduled in.
404 *
405 * - If the caller operates on itself, it lowers its priority below that of
406 * other threads in the system, and the caller is preemptible, the thread of
407 * highest priority will be scheduled in.
408 *
409 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
410 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
411 * highest priority.
412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500413 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400414 * @param prio New priority.
415 *
416 * @warning Changing the priority of a thread currently involved in mutex
417 * priority inheritance may result in undefined behavior.
418 *
419 * @return N/A
420 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400421extern void k_thread_priority_set(k_tid_t thread, int prio);
422
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500426 * This routine prevents the kernel scheduler from making @a thread the
427 * current thread. All other internal operations on @a thread are still
428 * performed; for example, any timeout it is waiting on keeps ticking,
429 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * If @a thread is already suspended, the routine has no effect.
432 *
433 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
435 * @return N/A
436 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400437extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400438
439/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500440 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500442 * This routine allows the kernel scheduler to make @a thread the current
443 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500445 * If @a thread is not currently suspended, the routine has no effect.
446 *
447 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 *
449 * @return N/A
450 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400451extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400452
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500454 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500456 * This routine specifies how the scheduler will perform time slicing of
457 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * To enable time slicing, @a slice must be non-zero. The scheduler
460 * ensures that no thread runs for more than the specified time limit
461 * before other threads of that priority are given a chance to execute.
462 * Any thread whose priority is higher than @a prio is exempted, and may
463 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500465 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400466 * execute. Once the scheduler selects a thread for execution, there is no
467 * minimum guaranteed time the thread will execute before threads of greater or
468 * equal priority are scheduled.
469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500470 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 * for execution, this routine has no effect; the thread is immediately
472 * rescheduled after the slice period expires.
473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * To disable timeslicing, set both @a slice and @a prio to zero.
475 *
476 * @param slice Maximum time slice length (in milliseconds).
477 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400478 *
479 * @return N/A
480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400481extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400482
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483/**
Allan Stephensc98da842016-11-11 15:45:03 -0500484 * @} end defgroup thread_apis
485 */
486
487/**
488 * @addtogroup isr_apis
489 * @{
490 */
491
492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400494 *
Allan Stephensc98da842016-11-11 15:45:03 -0500495 * This routine allows the caller to customize its actions, depending on
496 * whether it is a thread or an ISR.
497 *
498 * @note Can be called by ISRs.
499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500500 * @return 0 if invoked by a thread.
501 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400502 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500503extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400504
Benjamin Walsh445830d2016-11-10 15:54:27 -0500505/**
506 * @brief Determine if code is running in a preemptible thread.
507 *
Allan Stephensc98da842016-11-11 15:45:03 -0500508 * This routine allows the caller to customize its actions, depending on
509 * whether it can be preempted by another thread. The routine returns a 'true'
510 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500511 *
Allan Stephensc98da842016-11-11 15:45:03 -0500512 * - The code is running in a thread, not at ISR.
513 * - The thread's priority is in the preemptible range.
514 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500515 *
Allan Stephensc98da842016-11-11 15:45:03 -0500516 * @note Can be called by ISRs.
517 *
518 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500519 * @return Non-zero if invoked by a preemptible thread.
520 */
521extern int k_is_preempt_thread(void);
522
Allan Stephensc98da842016-11-11 15:45:03 -0500523/**
524 * @} end addtogroup isr_apis
525 */
526
527/**
528 * @addtogroup thread_apis
529 * @{
530 */
531
532/**
533 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500534 *
Allan Stephensc98da842016-11-11 15:45:03 -0500535 * This routine prevents the current thread from being preempted by another
536 * thread by instructing the scheduler to treat it as a cooperative thread.
537 * If the thread subsequently performs an operation that makes it unready,
538 * it will be context switched out in the normal manner. When the thread
539 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500540 *
Allan Stephensc98da842016-11-11 15:45:03 -0500541 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500542 *
Allan Stephensc98da842016-11-11 15:45:03 -0500543 * @note k_sched_lock() and k_sched_unlock() should normally be used
544 * when the operation being performed can be safely interrupted by ISRs.
545 * However, if the amount of processing involved is very small, better
546 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500547 *
548 * @return N/A
549 */
550extern void k_sched_lock(void);
551
Allan Stephensc98da842016-11-11 15:45:03 -0500552/**
553 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500554 *
Allan Stephensc98da842016-11-11 15:45:03 -0500555 * This routine reverses the effect of a previous call to k_sched_lock().
556 * A thread must call the routine once for each time it called k_sched_lock()
557 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500558 *
559 * @return N/A
560 */
561extern void k_sched_unlock(void);
562
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400563/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500564 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500566 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500568 * Custom data is not used by the kernel itself, and is freely available
569 * for a thread to use as it sees fit. It can be used as a framework
570 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 *
574 * @return N/A
575 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400576extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577
578/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500579 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400585extern void *k_thread_custom_data_get(void);
586
587/**
Allan Stephensc98da842016-11-11 15:45:03 -0500588 * @} end addtogroup thread_apis
589 */
590
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400591#include <sys_clock.h>
592
Allan Stephensc2f15a42016-11-17 12:24:22 -0500593/**
594 * @addtogroup clock_apis
595 * @{
596 */
597
598/**
599 * @brief Generate null timeout delay.
600 *
601 * This macro generates a timeout delay that that instructs a kernel API
602 * not to wait if the requested operation cannot be performed immediately.
603 *
604 * @return Timeout delay value.
605 */
606#define K_NO_WAIT 0
607
608/**
609 * @brief Generate timeout delay from milliseconds.
610 *
611 * This macro generates a timeout delay that that instructs a kernel API
612 * to wait up to @a ms milliseconds to perform the requested operation.
613 *
614 * @param ms Duration in milliseconds.
615 *
616 * @return Timeout delay value.
617 */
Johan Hedberg14471692016-11-13 10:52:15 +0200618#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500619
620/**
621 * @brief Generate timeout delay from seconds.
622 *
623 * This macro generates a timeout delay that that instructs a kernel API
624 * to wait up to @a s seconds to perform the requested operation.
625 *
626 * @param s Duration in seconds.
627 *
628 * @return Timeout delay value.
629 */
Johan Hedberg14471692016-11-13 10:52:15 +0200630#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500631
632/**
633 * @brief Generate timeout delay from minutes.
634 *
635 * This macro generates a timeout delay that that instructs a kernel API
636 * to wait up to @a m minutes to perform the requested operation.
637 *
638 * @param m Duration in minutes.
639 *
640 * @return Timeout delay value.
641 */
Johan Hedberg14471692016-11-13 10:52:15 +0200642#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500643
644/**
645 * @brief Generate timeout delay from hours.
646 *
647 * This macro generates a timeout delay that that instructs a kernel API
648 * to wait up to @a h hours to perform the requested operation.
649 *
650 * @param h Duration in hours.
651 *
652 * @return Timeout delay value.
653 */
Johan Hedberg14471692016-11-13 10:52:15 +0200654#define K_HOURS(h) K_MINUTES((h) * 60)
655
Allan Stephensc98da842016-11-11 15:45:03 -0500656/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500657 * @brief Generate infinite timeout delay.
658 *
659 * This macro generates a timeout delay that that instructs a kernel API
660 * to wait as long as necessary to perform the requested operation.
661 *
662 * @return Timeout delay value.
663 */
664#define K_FOREVER (-1)
665
666/**
667 * @} end addtogroup clock_apis
668 */
669
670/**
Allan Stephensc98da842016-11-11 15:45:03 -0500671 * @cond INTERNAL_HIDDEN
672 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400673
Benjamin Walsh62092182016-12-20 14:39:08 -0500674/* kernel clocks */
675
676#if (sys_clock_ticks_per_sec == 1000) || \
677 (sys_clock_ticks_per_sec == 500) || \
678 (sys_clock_ticks_per_sec == 250) || \
679 (sys_clock_ticks_per_sec == 125) || \
680 (sys_clock_ticks_per_sec == 100) || \
681 (sys_clock_ticks_per_sec == 50) || \
682 (sys_clock_ticks_per_sec == 25) || \
683 (sys_clock_ticks_per_sec == 20) || \
684 (sys_clock_ticks_per_sec == 10) || \
685 (sys_clock_ticks_per_sec == 1)
686
687 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
688#else
689 /* yields horrible 64-bit math on many architectures: try to avoid */
690 #define _NON_OPTIMIZED_TICKS_PER_SEC
691#endif
692
693#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
694extern int32_t _ms_to_ticks(int32_t ms);
695#else
696static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
697{
698 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
699}
700#endif
701
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500702/* added tick needed to account for tick in progress */
703#define _TICK_ALIGN 1
704
Benjamin Walsh62092182016-12-20 14:39:08 -0500705static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400706{
Benjamin Walsh62092182016-12-20 14:39:08 -0500707#ifdef CONFIG_SYS_CLOCK_EXISTS
708
709#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400710 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400711#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500712 return (uint64_t)ticks * _ms_per_tick;
713#endif
714
715#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400716 __ASSERT(ticks == 0, "");
717 return 0;
718#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400719}
720
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400721/* timeouts */
722
723struct _timeout;
724typedef void (*_timeout_func_t)(struct _timeout *t);
725
726struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500727 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400728 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400729 sys_dlist_t *wait_q;
730 int32_t delta_ticks_from_prev;
731 _timeout_func_t func;
732};
733
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200734extern int32_t _timeout_remaining_get(struct _timeout *timeout);
735
Allan Stephensc98da842016-11-11 15:45:03 -0500736/**
737 * INTERNAL_HIDDEN @endcond
738 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500739
Allan Stephensc98da842016-11-11 15:45:03 -0500740/**
741 * @cond INTERNAL_HIDDEN
742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400743
744struct k_timer {
745 /*
746 * _timeout structure must be first here if we want to use
747 * dynamic timer allocation. timeout.node is used in the double-linked
748 * list of free timers
749 */
750 struct _timeout timeout;
751
Allan Stephens45bfa372016-10-12 12:39:42 -0500752 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753 _wait_q_t wait_q;
754
755 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500756 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400757
758 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500759 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400760
761 /* timer period */
762 int32_t period;
763
Allan Stephens45bfa372016-10-12 12:39:42 -0500764 /* timer status */
765 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400766
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500767 /* user-specific data, also used to support legacy features */
768 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769
Anas Nashif2f203c22016-12-18 06:57:45 -0500770 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400771};
772
Allan Stephens1342adb2016-11-03 13:54:53 -0500773#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400774 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500775 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500776 .timeout.wait_q = NULL, \
777 .timeout.thread = NULL, \
778 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400779 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500780 .expiry_fn = expiry, \
781 .stop_fn = stop, \
782 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500783 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500784 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400785 }
786
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787/**
Allan Stephensc98da842016-11-11 15:45:03 -0500788 * INTERNAL_HIDDEN @endcond
789 */
790
791/**
792 * @defgroup timer_apis Timer APIs
793 * @ingroup kernel_apis
794 * @{
795 */
796
797/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500798 * @typedef k_timer_expiry_t
799 * @brief Timer expiry function type.
800 *
801 * A timer's expiry function is executed by the system clock interrupt handler
802 * each time the timer expires. The expiry function is optional, and is only
803 * invoked if the timer has been initialized with one.
804 *
805 * @param timer Address of timer.
806 *
807 * @return N/A
808 */
809typedef void (*k_timer_expiry_t)(struct k_timer *timer);
810
811/**
812 * @typedef k_timer_stop_t
813 * @brief Timer stop function type.
814 *
815 * A timer's stop function is executed if the timer is stopped prematurely.
816 * The function runs in the context of the thread that stops the timer.
817 * The stop function is optional, and is only invoked if the timer has been
818 * initialized with one.
819 *
820 * @param timer Address of timer.
821 *
822 * @return N/A
823 */
824typedef void (*k_timer_stop_t)(struct k_timer *timer);
825
826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500831 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
833 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @param expiry_fn Function to invoke each time the timer expires.
835 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500837#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500838 struct k_timer name \
839 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500840 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400841
Allan Stephens45bfa372016-10-12 12:39:42 -0500842/**
843 * @brief Initialize a timer.
844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500846 *
847 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @param expiry_fn Function to invoke each time the timer expires.
849 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500850 *
851 * @return N/A
852 */
853extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500854 k_timer_expiry_t expiry_fn,
855 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700856
Allan Stephens45bfa372016-10-12 12:39:42 -0500857/**
858 * @brief Start a timer.
859 *
860 * This routine starts a timer, and resets its status to zero. The timer
861 * begins counting down using the specified duration and period values.
862 *
863 * Attempting to start a timer that is already running is permitted.
864 * The timer's status is reset to zero and the timer begins counting down
865 * using the new duration and period values.
866 *
867 * @param timer Address of timer.
868 * @param duration Initial timer duration (in milliseconds).
869 * @param period Timer period (in milliseconds).
870 *
871 * @return N/A
872 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400873extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500874 int32_t duration, int32_t period);
875
876/**
877 * @brief Stop a timer.
878 *
879 * This routine stops a running timer prematurely. The timer's stop function,
880 * if one exists, is invoked by the caller.
881 *
882 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500884 *
885 * @param timer Address of timer.
886 *
887 * @return N/A
888 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500890
891/**
892 * @brief Read timer status.
893 *
894 * This routine reads the timer's status, which indicates the number of times
895 * it has expired since its status was last read.
896 *
897 * Calling this routine resets the timer's status to zero.
898 *
899 * @param timer Address of timer.
900 *
901 * @return Timer status.
902 */
903extern uint32_t k_timer_status_get(struct k_timer *timer);
904
905/**
906 * @brief Synchronize thread to timer expiration.
907 *
908 * This routine blocks the calling thread until the timer's status is non-zero
909 * (indicating that it has expired at least once since it was last examined)
910 * or the timer is stopped. If the timer status is already non-zero,
911 * or the timer is already stopped, the caller continues without waiting.
912 *
913 * Calling this routine resets the timer's status to zero.
914 *
915 * This routine must not be used by interrupt handlers, since they are not
916 * allowed to block.
917 *
918 * @param timer Address of timer.
919 *
920 * @return Timer status.
921 */
922extern uint32_t k_timer_status_sync(struct k_timer *timer);
923
924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500926 *
927 * This routine computes the (approximate) time remaining before a running
928 * timer next expires. If the timer is not running, it returns zero.
929 *
930 * @param timer Address of timer.
931 *
932 * @return Remaining time (in milliseconds).
933 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200934static inline int32_t k_timer_remaining_get(struct k_timer *timer)
935{
936 return _timeout_remaining_get(&timer->timeout);
937}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400938
Allan Stephensc98da842016-11-11 15:45:03 -0500939/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500940 * @brief Associate user-specific data with a timer.
941 *
942 * This routine records the @a user_data with the @a timer, to be retrieved
943 * later.
944 *
945 * It can be used e.g. in a timer handler shared across multiple subsystems to
946 * retrieve data specific to the subsystem this timer is associated with.
947 *
948 * @param timer Address of timer.
949 * @param user_data User data to associate with the timer.
950 *
951 * @return N/A
952 */
953static inline void k_timer_user_data_set(struct k_timer *timer,
954 void *user_data)
955{
956 timer->user_data = user_data;
957}
958
959/**
960 * @brief Retrieve the user-specific data from a timer.
961 *
962 * @param timer Address of timer.
963 *
964 * @return The user data.
965 */
966static inline void *k_timer_user_data_get(struct k_timer *timer)
967{
968 return timer->user_data;
969}
970
971/**
Allan Stephensc98da842016-11-11 15:45:03 -0500972 * @} end defgroup timer_apis
973 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400974
Allan Stephensc98da842016-11-11 15:45:03 -0500975/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500976 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500977 * @{
978 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500979
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * This routine returns the elapsed time since the system booted,
984 * in milliseconds.
985 *
986 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400987 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400988extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400989
990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine returns the lower 32-bits of the elapsed time since the system
994 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * This routine can be more efficient than k_uptime_get(), as it reduces the
997 * need for interrupt locking and 64-bit math. However, the 32-bit result
998 * cannot hold a system uptime time larger than approximately 50 days, so the
999 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001002 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001003extern uint32_t k_uptime_get_32(void);
1004
1005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * This routine computes the elapsed time between the current system uptime
1009 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @param reftime Pointer to a reference time, which is updated to the current
1012 * uptime upon return.
1013 *
1014 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001015 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001016extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001017
1018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * This routine computes the elapsed time between the current system uptime
1022 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1025 * need for interrupt locking and 64-bit math. However, the 32-bit result
1026 * cannot hold an elapsed time larger than approximately 50 days, so the
1027 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @param reftime Pointer to a reference time, which is updated to the current
1030 * uptime upon return.
1031 *
1032 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001033 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001034extern uint32_t k_uptime_delta_32(int64_t *reftime);
1035
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * This routine returns the current time, as measured by the system's hardware
1040 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001044extern uint32_t k_cycle_get_32(void);
1045
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001046/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001047 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048 */
1049
Allan Stephensc98da842016-11-11 15:45:03 -05001050/**
1051 * @cond INTERNAL_HIDDEN
1052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053
1054struct k_fifo {
1055 _wait_q_t wait_q;
1056 sys_slist_t data_q;
1057
Anas Nashif2f203c22016-12-18 06:57:45 -05001058 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001059};
1060
Allan Stephensc98da842016-11-11 15:45:03 -05001061#define K_FIFO_INITIALIZER(obj) \
1062 { \
1063 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1064 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001065 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001066 }
1067
1068/**
1069 * INTERNAL_HIDDEN @endcond
1070 */
1071
1072/**
1073 * @defgroup fifo_apis Fifo APIs
1074 * @ingroup kernel_apis
1075 * @{
1076 */
1077
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001084 *
1085 * @return N/A
1086 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001087extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088
1089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * This routine adds a data item to @a fifo. A fifo data item must be
1093 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1094 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001096 * @note Can be called by ISRs.
1097 *
1098 * @param fifo Address of the fifo.
1099 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
1101 * @return N/A
1102 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001103extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104
1105/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * This routine adds a list of data items to @a fifo in one operation.
1109 * The data items must be in a singly-linked list, with the first 32 bits
1110 * each data item pointing to the next data item; the list must be
1111 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * @param fifo Address of the fifo.
1116 * @param head Pointer to first node in singly-linked list.
1117 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
1119 * @return N/A
1120 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122
1123/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001126 * This routine adds a list of data items to @a fifo in one operation.
1127 * The data items must be in a singly-linked list implemented using a
1128 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 * and must be re-initialized via sys_slist_init().
1130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * @note Can be called by ISRs.
1132 *
1133 * @param fifo Address of the fifo.
1134 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
1136 * @return N/A
1137 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001138extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139
1140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001141 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * This routine removes a data item from @a fifo in a "first in, first out"
1144 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001146 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1147 *
1148 * @param fifo Address of the fifo.
1149 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 * or one of the special values K_NO_WAIT and K_FOREVER.
1151 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001152 * @return Address of the data item if successful; NULL if returned
1153 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1156
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001158 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001160 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001162 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001167 struct k_fifo name \
1168 __in_section(_k_fifo, static, name) = \
1169 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170
Allan Stephensc98da842016-11-11 15:45:03 -05001171/**
1172 * @} end defgroup fifo_apis
1173 */
1174
1175/**
1176 * @cond INTERNAL_HIDDEN
1177 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001178
1179struct k_lifo {
1180 _wait_q_t wait_q;
1181 void *list;
1182
Anas Nashif2f203c22016-12-18 06:57:45 -05001183 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001184};
1185
Allan Stephensc98da842016-11-11 15:45:03 -05001186#define K_LIFO_INITIALIZER(obj) \
1187 { \
1188 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1189 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001190 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001191 }
1192
1193/**
1194 * INTERNAL_HIDDEN @endcond
1195 */
1196
1197/**
1198 * @defgroup lifo_apis Lifo APIs
1199 * @ingroup kernel_apis
1200 * @{
1201 */
1202
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001204 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001208 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
1210 * @return N/A
1211 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001212extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213
1214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * This routine adds a data item to @a lifo. A lifo data item must be
1218 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1219 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @note Can be called by ISRs.
1222 *
1223 * @param lifo Address of the lifo.
1224 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
1226 * @return N/A
1227 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001228extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229
1230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * This routine removes a data item from @a lifo in a "last in, first out"
1234 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1237 *
1238 * @param lifo Address of the lifo.
1239 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 * or one of the special values K_NO_WAIT and K_FOREVER.
1241 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001242 * @return Address of the data item if successful; NULL if returned
1243 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1246
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001248 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001250 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001251 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001252 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001256#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001257 struct k_lifo name \
1258 __in_section(_k_lifo, static, name) = \
1259 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001260
Allan Stephensc98da842016-11-11 15:45:03 -05001261/**
1262 * @} end defgroup lifo_apis
1263 */
1264
1265/**
1266 * @cond INTERNAL_HIDDEN
1267 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001268
1269struct k_stack {
1270 _wait_q_t wait_q;
1271 uint32_t *base, *next, *top;
1272
Anas Nashif2f203c22016-12-18 06:57:45 -05001273 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001274};
1275
Allan Stephensc98da842016-11-11 15:45:03 -05001276#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1277 { \
1278 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1279 .base = stack_buffer, \
1280 .next = stack_buffer, \
1281 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001282 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001283 }
1284
1285/**
1286 * INTERNAL_HIDDEN @endcond
1287 */
1288
1289/**
1290 * @defgroup stack_apis Stack APIs
1291 * @ingroup kernel_apis
1292 * @{
1293 */
1294
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001295/**
1296 * @brief Initialize a stack.
1297 *
1298 * This routine initializes a stack object, prior to its first use.
1299 *
1300 * @param stack Address of the stack.
1301 * @param buffer Address of array used to hold stacked values.
1302 * @param num_entries Maximum number of values that can be stacked.
1303 *
1304 * @return N/A
1305 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001306extern void k_stack_init(struct k_stack *stack,
1307 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001308
1309/**
1310 * @brief Push an element onto a stack.
1311 *
1312 * This routine adds a 32-bit value @a data to @a stack.
1313 *
1314 * @note Can be called by ISRs.
1315 *
1316 * @param stack Address of the stack.
1317 * @param data Value to push onto the stack.
1318 *
1319 * @return N/A
1320 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001322
1323/**
1324 * @brief Pop an element from a stack.
1325 *
1326 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1327 * manner and stores the value in @a data.
1328 *
1329 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1330 *
1331 * @param stack Address of the stack.
1332 * @param data Address of area to hold the value popped from the stack.
1333 * @param timeout Waiting period to obtain a value (in milliseconds),
1334 * or one of the special values K_NO_WAIT and K_FOREVER.
1335 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001336 * @retval 0 Element popped from stack.
1337 * @retval -EBUSY Returned without waiting.
1338 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001339 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001340extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1341
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001345 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001347 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * @param name Name of the stack.
1350 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001351 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001352#define K_STACK_DEFINE(name, stack_num_entries) \
1353 uint32_t __noinit \
1354 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001355 struct k_stack name \
1356 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001357 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1358 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001359
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001360/**
Allan Stephensc98da842016-11-11 15:45:03 -05001361 * @} end defgroup stack_apis
1362 */
1363
Allan Stephens6bba9b02016-11-16 14:56:54 -05001364struct k_work;
1365
Allan Stephensc98da842016-11-11 15:45:03 -05001366/**
1367 * @defgroup workqueue_apis Workqueue Thread APIs
1368 * @ingroup kernel_apis
1369 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370 */
1371
Allan Stephens6bba9b02016-11-16 14:56:54 -05001372/**
1373 * @typedef k_work_handler_t
1374 * @brief Work item handler function type.
1375 *
1376 * A work item's handler function is executed by a workqueue's thread
1377 * when the work item is processed by the workqueue.
1378 *
1379 * @param work Address of the work item.
1380 *
1381 * @return N/A
1382 */
1383typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384
1385/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001386 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001388
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001389struct k_work_q {
1390 struct k_fifo fifo;
1391};
1392
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001394 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395};
1396
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397struct k_work {
1398 void *_reserved; /* Used by k_fifo implementation. */
1399 k_work_handler_t handler;
1400 atomic_t flags[1];
1401};
1402
Allan Stephens6bba9b02016-11-16 14:56:54 -05001403struct k_delayed_work {
1404 struct k_work work;
1405 struct _timeout timeout;
1406 struct k_work_q *work_q;
1407};
1408
1409extern struct k_work_q k_sys_work_q;
1410
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001412 * INTERNAL_HIDDEN @endcond
1413 */
1414
1415/**
1416 * @brief Initialize a statically-defined work item.
1417 *
1418 * This macro can be used to initialize a statically-defined workqueue work
1419 * item, prior to its first use. For example,
1420 *
1421 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1422 *
1423 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001424 */
1425#define K_WORK_INITIALIZER(work_handler) \
1426 { \
1427 ._reserved = NULL, \
1428 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001429 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430 }
1431
1432/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001433 * @brief Initialize a work item.
1434 *
1435 * This routine initializes a workqueue work item, prior to its first use.
1436 *
1437 * @param work Address of work item.
1438 * @param handler Function to invoke each time work item is processed.
1439 *
1440 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441 */
1442static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1443{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001444 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445 work->handler = handler;
1446}
1447
1448/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001449 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001450 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001451 * This routine submits work item @a work to be processed by workqueue
1452 * @a work_q. If the work item is already pending in the workqueue's queue
1453 * as a result of an earlier submission, this routine has no effect on the
1454 * work item. If the work item has already been processed, or is currently
1455 * being processed, its work is considered complete and the work item can be
1456 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001457 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 * @warning
1459 * A submitted work item must not be modified until it has been processed
1460 * by the workqueue.
1461 *
1462 * @note Can be called by ISRs.
1463 *
1464 * @param work_q Address of workqueue.
1465 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001466 *
1467 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001468 */
1469static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1470 struct k_work *work)
1471{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001472 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001473 k_fifo_put(&work_q->fifo, work);
1474 }
1475}
1476
1477/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001478 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001480 * This routine indicates if work item @a work is pending in a workqueue's
1481 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001483 * @note Can be called by ISRs.
1484 *
1485 * @param work Address of work item.
1486 *
1487 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001488 */
1489static inline int k_work_pending(struct k_work *work)
1490{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001491 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001492}
1493
1494/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001495 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001497 * This routine starts workqueue @a work_q. The workqueue spawns its work
1498 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001500 * @param work_q Address of workqueue.
1501 * @param stack Pointer to work queue thread's stack space.
1502 * @param stack_size Size of the work queue thread's stack (in bytes).
1503 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
1505 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001506 */
Allan Stephens904cf972016-10-07 13:59:23 -05001507extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001508 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001509
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001510/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001511 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001512 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001513 * This routine initializes a workqueue delayed work item, prior to
1514 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001515 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001516 * @param work Address of delayed work item.
1517 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001518 *
1519 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001520 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001521extern void k_delayed_work_init(struct k_delayed_work *work,
1522 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001523
1524/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001525 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001526 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001527 * This routine schedules work item @a work to be processed by workqueue
1528 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1529 * an asychronous countdown for the work item and then returns to the caller.
1530 * Only when the countdown completes is the work item actually submitted to
1531 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001532 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001533 * Submitting a previously submitted delayed work item that is still
1534 * counting down cancels the existing submission and restarts the countdown
1535 * using the new delay. If the work item is currently pending on the
1536 * workqueue's queue because the countdown has completed it is too late to
1537 * resubmit the item, and resubmission fails without impacting the work item.
1538 * If the work item has already been processed, or is currently being processed,
1539 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001540 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001541 * @warning
1542 * A delayed work item must not be modified until it has been processed
1543 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001545 * @note Can be called by ISRs.
1546 *
1547 * @param work_q Address of workqueue.
1548 * @param work Address of delayed work item.
1549 * @param delay Delay before submitting the work item (in milliseconds).
1550 *
1551 * @retval 0 Work item countdown started.
1552 * @retval -EINPROGRESS Work item is already pending.
1553 * @retval -EINVAL Work item is being processed or has completed its work.
1554 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001555 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001556extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1557 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001558 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559
1560/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001561 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001562 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001563 * This routine cancels the submission of delayed work item @a work.
1564 * A delayed work item can only be cancelled while its countdown is still
1565 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001566 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001567 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001569 * @param work Address of delayed work item.
1570 *
1571 * @retval 0 Work item countdown cancelled.
1572 * @retval -EINPROGRESS Work item is already pending.
1573 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001574 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001575extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001576
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001577/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001578 * @brief Submit a work item to the system workqueue.
1579 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001580 * This routine submits work item @a work to be processed by the system
1581 * workqueue. If the work item is already pending in the workqueue's queue
1582 * as a result of an earlier submission, this routine has no effect on the
1583 * work item. If the work item has already been processed, or is currently
1584 * being processed, its work is considered complete and the work item can be
1585 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001586 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001587 * @warning
1588 * Work items submitted to the system workqueue should avoid using handlers
1589 * that block or yield since this may prevent the system workqueue from
1590 * processing other work items in a timely manner.
1591 *
1592 * @note Can be called by ISRs.
1593 *
1594 * @param work Address of work item.
1595 *
1596 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001597 */
1598static inline void k_work_submit(struct k_work *work)
1599{
1600 k_work_submit_to_queue(&k_sys_work_q, work);
1601}
1602
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001603/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001604 * @brief Submit a delayed work item to the system workqueue.
1605 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001606 * This routine schedules work item @a work to be processed by the system
1607 * workqueue after a delay of @a delay milliseconds. The routine initiates
1608 * an asychronous countdown for the work item and then returns to the caller.
1609 * Only when the countdown completes is the work item actually submitted to
1610 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001611 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001612 * Submitting a previously submitted delayed work item that is still
1613 * counting down cancels the existing submission and restarts the countdown
1614 * using the new delay. If the work item is currently pending on the
1615 * workqueue's queue because the countdown has completed it is too late to
1616 * resubmit the item, and resubmission fails without impacting the work item.
1617 * If the work item has already been processed, or is currently being processed,
1618 * its work is considered complete and the work item can be resubmitted.
1619 *
1620 * @warning
1621 * Work items submitted to the system workqueue should avoid using handlers
1622 * that block or yield since this may prevent the system workqueue from
1623 * processing other work items in a timely manner.
1624 *
1625 * @note Can be called by ISRs.
1626 *
1627 * @param work Address of delayed work item.
1628 * @param delay Delay before submitting the work item (in milliseconds).
1629 *
1630 * @retval 0 Work item countdown started.
1631 * @retval -EINPROGRESS Work item is already pending.
1632 * @retval -EINVAL Work item is being processed or has completed its work.
1633 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634 */
1635static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001636 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001637{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001638 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001639}
1640
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001641/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001642 * @brief Get time remaining before a delayed work gets scheduled.
1643 *
1644 * This routine computes the (approximate) time remaining before a
1645 * delayed work gets executed. If the delayed work is not waiting to be
1646 * schedules, it returns zero.
1647 *
1648 * @param work Delayed work item.
1649 *
1650 * @return Remaining time (in milliseconds).
1651 */
1652static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1653{
1654 return _timeout_remaining_get(&work->timeout);
1655}
1656
1657/**
Allan Stephensc98da842016-11-11 15:45:03 -05001658 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001659 */
1660
Allan Stephensc98da842016-11-11 15:45:03 -05001661/**
1662 * @cond INTERNAL_HIDDEN
1663 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001664
1665struct k_mutex {
1666 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001667 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001668 uint32_t lock_count;
1669 int owner_orig_prio;
1670#ifdef CONFIG_OBJECT_MONITOR
1671 int num_lock_state_changes;
1672 int num_conflicts;
1673#endif
1674
Anas Nashif2f203c22016-12-18 06:57:45 -05001675 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001676};
1677
1678#ifdef CONFIG_OBJECT_MONITOR
1679#define _MUTEX_INIT_OBJECT_MONITOR \
1680 .num_lock_state_changes = 0, .num_conflicts = 0,
1681#else
1682#define _MUTEX_INIT_OBJECT_MONITOR
1683#endif
1684
1685#define K_MUTEX_INITIALIZER(obj) \
1686 { \
1687 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1688 .owner = NULL, \
1689 .lock_count = 0, \
1690 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1691 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001692 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001693 }
1694
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695/**
Allan Stephensc98da842016-11-11 15:45:03 -05001696 * INTERNAL_HIDDEN @endcond
1697 */
1698
1699/**
1700 * @defgroup mutex_apis Mutex APIs
1701 * @ingroup kernel_apis
1702 * @{
1703 */
1704
1705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001710 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001715 struct k_mutex name \
1716 __in_section(_k_mutex, static, name) = \
1717 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001724 * Upon completion, the mutex is available and does not have an owner.
1725 *
1726 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
1728 * @return N/A
1729 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001730extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731
1732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * This routine locks @a mutex. If the mutex is locked by another thread,
1736 * the calling thread waits until the mutex becomes available or until
1737 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * A thread is permitted to lock a mutex it has already locked. The operation
1740 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @param mutex Address of the mutex.
1743 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 * or one of the special values K_NO_WAIT and K_FOREVER.
1745 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001746 * @retval 0 Mutex locked.
1747 * @retval -EBUSY Returned without waiting.
1748 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751
1752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * This routine unlocks @a mutex. The mutex must already be locked by the
1756 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757 *
1758 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 * thread.
1761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
1764 * @return N/A
1765 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766extern void k_mutex_unlock(struct k_mutex *mutex);
1767
Allan Stephensc98da842016-11-11 15:45:03 -05001768/**
1769 * @} end defgroup mutex_apis
1770 */
1771
1772/**
1773 * @cond INTERNAL_HIDDEN
1774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001775
1776struct k_sem {
1777 _wait_q_t wait_q;
1778 unsigned int count;
1779 unsigned int limit;
1780
Anas Nashif2f203c22016-12-18 06:57:45 -05001781 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001782};
1783
Allan Stephensc98da842016-11-11 15:45:03 -05001784#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1785 { \
1786 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1787 .count = initial_count, \
1788 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001789 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001790 }
1791
1792/**
1793 * INTERNAL_HIDDEN @endcond
1794 */
1795
1796/**
1797 * @defgroup semaphore_apis Semaphore APIs
1798 * @ingroup kernel_apis
1799 * @{
1800 */
1801
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001805 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @param sem Address of the semaphore.
1808 * @param initial_count Initial semaphore count.
1809 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001810 *
1811 * @return N/A
1812 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1814 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001815
1816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001819 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1822 *
1823 * @param sem Address of the semaphore.
1824 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001825 * or one of the special values K_NO_WAIT and K_FOREVER.
1826 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001827 * @note When porting code from the nanokernel legacy API to the new API, be
1828 * careful with the return value of this function. The return value is the
1829 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1830 * non-zero means failure, while the nano_sem_take family returns 1 for success
1831 * and 0 for failure.
1832 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001833 * @retval 0 Semaphore taken.
1834 * @retval -EBUSY Returned without waiting.
1835 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001836 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001838
1839/**
1840 * @brief Give a semaphore.
1841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001842 * This routine gives @a sem, unless the semaphore is already at its maximum
1843 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001848 *
1849 * @return N/A
1850 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001851extern void k_sem_give(struct k_sem *sem);
1852
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001853/**
1854 * @brief Reset a semaphore's count to zero.
1855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001859 *
1860 * @return N/A
1861 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001862static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001863{
1864 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001865}
1866
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001867/**
1868 * @brief Get a semaphore's count.
1869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001874 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001875 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001876static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877{
1878 return sem->count;
1879}
1880
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001885 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001886 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * @param name Name of the semaphore.
1889 * @param initial_count Initial semaphore count.
1890 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001891 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001893 struct k_sem name \
1894 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895 K_SEM_INITIALIZER(name, initial_count, count_limit)
1896
Allan Stephensc98da842016-11-11 15:45:03 -05001897/**
1898 * @} end defgroup semaphore_apis
1899 */
1900
1901/**
1902 * @defgroup alert_apis Alert APIs
1903 * @ingroup kernel_apis
1904 * @{
1905 */
1906
Allan Stephens5eceb852016-11-16 10:16:30 -05001907/**
1908 * @typedef k_alert_handler_t
1909 * @brief Alert handler function type.
1910 *
1911 * An alert's alert handler function is invoked by the system workqueue
1912 * when the alert is signalled. The alert handler function is optional,
1913 * and is only invoked if the alert has been initialized with one.
1914 *
1915 * @param alert Address of the alert.
1916 *
1917 * @return 0 if alert has been consumed; non-zero if alert should pend.
1918 */
1919typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001920
1921/**
1922 * @} end defgroup alert_apis
1923 */
1924
1925/**
1926 * @cond INTERNAL_HIDDEN
1927 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001928
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001929#define K_ALERT_DEFAULT NULL
1930#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001932struct k_alert {
1933 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934 atomic_t send_count;
1935 struct k_work work_item;
1936 struct k_sem sem;
1937
Anas Nashif2f203c22016-12-18 06:57:45 -05001938 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939};
1940
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001941extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001942
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001943#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001945 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001947 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001948 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001949 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950 }
1951
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952/**
Allan Stephensc98da842016-11-11 15:45:03 -05001953 * INTERNAL_HIDDEN @endcond
1954 */
1955
1956/**
1957 * @addtogroup alert_apis
1958 * @{
1959 */
1960
1961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001962 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001963 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001964 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001966 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001968 * @param name Name of the alert.
1969 * @param alert_handler Action to take when alert is sent. Specify either
1970 * the address of a function to be invoked by the system workqueue
1971 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1972 * K_ALERT_DEFAULT (which causes the alert to pend).
1973 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001975#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001976 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001977 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001978 K_ALERT_INITIALIZER(name, alert_handler, \
1979 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001980
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001982 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * @param alert Address of the alert.
1987 * @param handler Action to take when alert is sent. Specify either the address
1988 * of a function to be invoked by the system workqueue thread,
1989 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1990 * K_ALERT_DEFAULT (which causes the alert to pend).
1991 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
1993 * @return N/A
1994 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001995extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1996 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001997
1998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001999 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2004 *
2005 * @param alert Address of the alert.
2006 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 * or one of the special values K_NO_WAIT and K_FOREVER.
2008 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002009 * @retval 0 Alert received.
2010 * @retval -EBUSY Returned without waiting.
2011 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002012 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002013extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014
2015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002016 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 * This routine signals @a alert. The action specified for @a alert will
2019 * be taken, which may trigger the execution of an alert handler function
2020 * and/or cause the alert to pend (assuming the alert has not reached its
2021 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * @note Can be called by ISRs.
2024 *
2025 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026 *
2027 * @return N/A
2028 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002029extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030
2031/**
Allan Stephensc98da842016-11-11 15:45:03 -05002032 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033 */
2034
Allan Stephensc98da842016-11-11 15:45:03 -05002035/**
2036 * @cond INTERNAL_HIDDEN
2037 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002038
2039struct k_msgq {
2040 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002041 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042 uint32_t max_msgs;
2043 char *buffer_start;
2044 char *buffer_end;
2045 char *read_ptr;
2046 char *write_ptr;
2047 uint32_t used_msgs;
2048
Anas Nashif2f203c22016-12-18 06:57:45 -05002049 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050};
2051
Peter Mitsis1da807e2016-10-06 11:36:59 -04002052#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053 { \
2054 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002055 .max_msgs = q_max_msgs, \
2056 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002058 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059 .read_ptr = q_buffer, \
2060 .write_ptr = q_buffer, \
2061 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002062 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063 }
2064
Peter Mitsis1da807e2016-10-06 11:36:59 -04002065/**
Allan Stephensc98da842016-11-11 15:45:03 -05002066 * INTERNAL_HIDDEN @endcond
2067 */
2068
2069/**
2070 * @defgroup msgq_apis Message Queue APIs
2071 * @ingroup kernel_apis
2072 * @{
2073 */
2074
2075/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002078 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2079 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002080 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2081 * message is similarly aligned to this boundary, @a q_msg_size must also be
2082 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002084 * The message queue can be accessed outside the module where it is defined
2085 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002087 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * @param q_name Name of the message queue.
2090 * @param q_msg_size Message size (in bytes).
2091 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002092 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002093 */
2094#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2095 static char __noinit __aligned(q_align) \
2096 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002097 struct k_msgq q_name \
2098 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002099 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2100 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002101
Peter Mitsisd7a37502016-10-13 11:37:40 -04002102/**
2103 * @brief Initialize a message queue.
2104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * This routine initializes a message queue object, prior to its first use.
2106 *
Allan Stephensda827222016-11-09 14:23:58 -06002107 * The message queue's ring buffer must contain space for @a max_msgs messages,
2108 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2109 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2110 * that each message is similarly aligned to this boundary, @a q_msg_size
2111 * must also be a multiple of N.
2112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002113 * @param q Address of the message queue.
2114 * @param buffer Pointer to ring buffer that holds queued messages.
2115 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002116 * @param max_msgs Maximum number of messages that can be queued.
2117 *
2118 * @return N/A
2119 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002120extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002121 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002122
2123/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002127 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002128 * @note Can be called by ISRs.
2129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * @param q Address of the message queue.
2131 * @param data Pointer to the message.
2132 * @param timeout Waiting period to add the message (in milliseconds),
2133 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002134 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002135 * @retval 0 Message sent.
2136 * @retval -ENOMSG Returned without waiting or queue purged.
2137 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002140
2141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002142 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * This routine receives a message from message queue @a q in a "first in,
2145 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002146 *
Allan Stephensc98da842016-11-11 15:45:03 -05002147 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * @param q Address of the message queue.
2150 * @param data Address of area to hold the received message.
2151 * @param timeout Waiting period to receive the message (in milliseconds),
2152 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002153 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002154 * @retval 0 Message received.
2155 * @retval -ENOMSG Returned without waiting.
2156 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002157 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002159
2160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * This routine discards all unreceived messages in a message queue's ring
2164 * buffer. Any threads that are blocked waiting to send a message to the
2165 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002168 *
2169 * @return N/A
2170 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171extern void k_msgq_purge(struct k_msgq *q);
2172
Peter Mitsis67be2492016-10-07 11:44:34 -04002173/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002176 * This routine returns the number of unused entries in a message queue's
2177 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * @param q Address of the message queue.
2180 *
2181 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002182 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002183static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002184{
2185 return q->max_msgs - q->used_msgs;
2186}
2187
Peter Mitsisd7a37502016-10-13 11:37:40 -04002188/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002189 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002191 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002193 * @param q Address of the message queue.
2194 *
2195 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002196 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002197static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198{
2199 return q->used_msgs;
2200}
2201
Allan Stephensc98da842016-11-11 15:45:03 -05002202/**
2203 * @} end defgroup msgq_apis
2204 */
2205
2206/**
2207 * @defgroup mem_pool_apis Memory Pool APIs
2208 * @ingroup kernel_apis
2209 * @{
2210 */
2211
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002213 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214 void *addr_in_pool;
2215 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002216 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217};
2218
Allan Stephensc98da842016-11-11 15:45:03 -05002219/**
2220 * @} end defgroup mem_pool_apis
2221 */
2222
2223/**
2224 * @defgroup mailbox_apis Mailbox APIs
2225 * @ingroup kernel_apis
2226 * @{
2227 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228
2229struct k_mbox_msg {
2230 /** internal use only - needed for legacy API support */
2231 uint32_t _mailbox;
2232 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002233 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234 /** application-defined information value */
2235 uint32_t info;
2236 /** sender's message data buffer */
2237 void *tx_data;
2238 /** internal use only - needed for legacy API support */
2239 void *_rx_data;
2240 /** message data block descriptor */
2241 struct k_mem_block tx_block;
2242 /** source thread id */
2243 k_tid_t rx_source_thread;
2244 /** target thread id */
2245 k_tid_t tx_target_thread;
2246 /** internal use only - thread waiting on send (may be a dummy) */
2247 k_tid_t _syncing_thread;
2248#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2249 /** internal use only - semaphore used during asynchronous send */
2250 struct k_sem *_async_sem;
2251#endif
2252};
2253
Allan Stephensc98da842016-11-11 15:45:03 -05002254/**
2255 * @cond INTERNAL_HIDDEN
2256 */
2257
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258struct k_mbox {
2259 _wait_q_t tx_msg_queue;
2260 _wait_q_t rx_msg_queue;
2261
Anas Nashif2f203c22016-12-18 06:57:45 -05002262 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263};
2264
2265#define K_MBOX_INITIALIZER(obj) \
2266 { \
2267 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2268 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002269 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 }
2271
Peter Mitsis12092702016-10-14 12:57:23 -04002272/**
Allan Stephensc98da842016-11-11 15:45:03 -05002273 * INTERNAL_HIDDEN @endcond
2274 */
2275
2276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002277 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002279 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002281 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002286 struct k_mbox name \
2287 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288 K_MBOX_INITIALIZER(name) \
2289
Peter Mitsis12092702016-10-14 12:57:23 -04002290/**
2291 * @brief Initialize a mailbox.
2292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * This routine initializes a mailbox object, prior to its first use.
2294 *
2295 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002296 *
2297 * @return N/A
2298 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299extern void k_mbox_init(struct k_mbox *mbox);
2300
Peter Mitsis12092702016-10-14 12:57:23 -04002301/**
2302 * @brief Send a mailbox message in a synchronous manner.
2303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002304 * This routine sends a message to @a mbox and waits for a receiver to both
2305 * receive and process it. The message data may be in a buffer, in a memory
2306 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002308 * @param mbox Address of the mailbox.
2309 * @param tx_msg Address of the transmit message descriptor.
2310 * @param timeout Waiting period for the message to be received (in
2311 * milliseconds), or one of the special values K_NO_WAIT
2312 * and K_FOREVER. Once the message has been received,
2313 * this routine waits as long as necessary for the message
2314 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002315 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002316 * @retval 0 Message sent.
2317 * @retval -ENOMSG Returned without waiting.
2318 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002319 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002320extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002322
Peter Mitsis12092702016-10-14 12:57:23 -04002323/**
2324 * @brief Send a mailbox message in an asynchronous manner.
2325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * This routine sends a message to @a mbox without waiting for a receiver
2327 * to process it. The message data may be in a buffer, in a memory pool block,
2328 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2329 * will be given when the message has been both received and completely
2330 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @param mbox Address of the mailbox.
2333 * @param tx_msg Address of the transmit message descriptor.
2334 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002335 *
2336 * @return N/A
2337 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002338extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339 struct k_sem *sem);
2340
Peter Mitsis12092702016-10-14 12:57:23 -04002341/**
2342 * @brief Receive a mailbox message.
2343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * This routine receives a message from @a mbox, then optionally retrieves
2345 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @param mbox Address of the mailbox.
2348 * @param rx_msg Address of the receive message descriptor.
2349 * @param buffer Address of the buffer to receive data, or NULL to defer data
2350 * retrieval and message disposal until later.
2351 * @param timeout Waiting period for a message to be received (in
2352 * milliseconds), or one of the special values K_NO_WAIT
2353 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002354 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002355 * @retval 0 Message received.
2356 * @retval -ENOMSG Returned without waiting.
2357 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002358 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002359extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002361
2362/**
2363 * @brief Retrieve mailbox message data into a buffer.
2364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * This routine completes the processing of a received message by retrieving
2366 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002367 *
2368 * Alternatively, this routine can be used to dispose of a received message
2369 * without retrieving its data.
2370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * @param rx_msg Address of the receive message descriptor.
2372 * @param buffer Address of the buffer to receive data, or NULL to discard
2373 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002374 *
2375 * @return N/A
2376 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002377extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002378
2379/**
2380 * @brief Retrieve mailbox message data into a memory pool block.
2381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * This routine completes the processing of a received message by retrieving
2383 * its data into a memory pool block, then disposing of the message.
2384 * The memory pool block that results from successful retrieval must be
2385 * returned to the pool once the data has been processed, even in cases
2386 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002387 *
2388 * Alternatively, this routine can be used to dispose of a received message
2389 * without retrieving its data. In this case there is no need to return a
2390 * memory pool block to the pool.
2391 *
2392 * This routine allocates a new memory pool block for the data only if the
2393 * data is not already in one. If a new block cannot be allocated, the routine
2394 * returns a failure code and the received message is left unchanged. This
2395 * permits the caller to reattempt data retrieval at a later time or to dispose
2396 * of the received message without retrieving its data.
2397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * @param rx_msg Address of a receive message descriptor.
2399 * @param pool Address of memory pool, or NULL to discard data.
2400 * @param block Address of the area to hold memory pool block info.
2401 * @param timeout Waiting period to wait for a memory pool block (in
2402 * milliseconds), or one of the special values K_NO_WAIT
2403 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002404 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002405 * @retval 0 Data retrieved.
2406 * @retval -ENOMEM Returned without waiting.
2407 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002408 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002409extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002410 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411 struct k_mem_block *block, int32_t timeout);
2412
Allan Stephensc98da842016-11-11 15:45:03 -05002413/**
2414 * @} end defgroup mailbox_apis
2415 */
2416
2417/**
2418 * @cond INTERNAL_HIDDEN
2419 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420
2421struct k_pipe {
2422 unsigned char *buffer; /* Pipe buffer: may be NULL */
2423 size_t size; /* Buffer size */
2424 size_t bytes_used; /* # bytes used in buffer */
2425 size_t read_index; /* Where in buffer to read from */
2426 size_t write_index; /* Where in buffer to write */
2427
2428 struct {
2429 _wait_q_t readers; /* Reader wait queue */
2430 _wait_q_t writers; /* Writer wait queue */
2431 } wait_q;
2432
Anas Nashif2f203c22016-12-18 06:57:45 -05002433 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434};
2435
Peter Mitsise5d9c582016-10-14 14:44:57 -04002436#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437 { \
2438 .buffer = pipe_buffer, \
2439 .size = pipe_buffer_size, \
2440 .bytes_used = 0, \
2441 .read_index = 0, \
2442 .write_index = 0, \
2443 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2444 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002445 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446 }
2447
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002448/**
Allan Stephensc98da842016-11-11 15:45:03 -05002449 * INTERNAL_HIDDEN @endcond
2450 */
2451
2452/**
2453 * @defgroup pipe_apis Pipe APIs
2454 * @ingroup kernel_apis
2455 * @{
2456 */
2457
2458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002463 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * @param name Name of the pipe.
2466 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2467 * or zero if no ring buffer is used.
2468 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002470#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2471 static unsigned char __noinit __aligned(pipe_align) \
2472 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002473 struct k_pipe name \
2474 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002475 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * This routine initializes a pipe object, prior to its first use.
2481 *
2482 * @param pipe Address of the pipe.
2483 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2484 * is used.
2485 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2486 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487 *
2488 * @return N/A
2489 */
2490extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2491 size_t size);
2492
2493/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002496 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @param pipe Address of the pipe.
2499 * @param data Address of data to write.
2500 * @param bytes_to_write Size of data (in bytes).
2501 * @param bytes_written Address of area to hold the number of bytes written.
2502 * @param min_xfer Minimum number of bytes to write.
2503 * @param timeout Waiting period to wait for the data to be written (in
2504 * milliseconds), or one of the special values K_NO_WAIT
2505 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002507 * @retval 0 At least @a min_xfer bytes of data were written.
2508 * @retval -EIO Returned without waiting; zero data bytes were written.
2509 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002512extern int k_pipe_put(struct k_pipe *pipe, void *data,
2513 size_t bytes_to_write, size_t *bytes_written,
2514 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515
2516/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @param pipe Address of the pipe.
2522 * @param data Address to place the data read from pipe.
2523 * @param bytes_to_read Maximum number of data bytes to read.
2524 * @param bytes_read Address of area to hold the number of bytes read.
2525 * @param min_xfer Minimum number of data bytes to read.
2526 * @param timeout Waiting period to wait for the data to be read (in
2527 * milliseconds), or one of the special values K_NO_WAIT
2528 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002530 * @retval 0 At least @a min_xfer bytes of data were read.
2531 * @retval -EIO Returned without waiting; zero data bytes were read.
2532 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002535extern int k_pipe_get(struct k_pipe *pipe, void *data,
2536 size_t bytes_to_read, size_t *bytes_read,
2537 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538
2539/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * This routine writes the data contained in a memory block to @a pipe.
2543 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547 * @param block Memory block containing data to send
2548 * @param size Number of data bytes in memory block to send
2549 * @param sem Semaphore to signal upon completion (else NULL)
2550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552 */
2553extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2554 size_t size, struct k_sem *sem);
2555
2556/**
Allan Stephensc98da842016-11-11 15:45:03 -05002557 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558 */
2559
Allan Stephensc98da842016-11-11 15:45:03 -05002560/**
2561 * @cond INTERNAL_HIDDEN
2562 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002564struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002566 uint32_t num_blocks;
2567 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 char *buffer;
2569 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002570 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002571
Anas Nashif2f203c22016-12-18 06:57:45 -05002572 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573};
2574
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002575#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2576 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577 { \
2578 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002579 .num_blocks = slab_num_blocks, \
2580 .block_size = slab_block_size, \
2581 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582 .free_list = NULL, \
2583 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002584 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585 }
2586
Peter Mitsis578f9112016-10-07 13:50:31 -04002587/**
Allan Stephensc98da842016-11-11 15:45:03 -05002588 * INTERNAL_HIDDEN @endcond
2589 */
2590
2591/**
2592 * @defgroup mem_slab_apis Memory Slab APIs
2593 * @ingroup kernel_apis
2594 * @{
2595 */
2596
2597/**
Allan Stephensda827222016-11-09 14:23:58 -06002598 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002599 *
Allan Stephensda827222016-11-09 14:23:58 -06002600 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002601 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002602 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2603 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002604 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002605 *
Allan Stephensda827222016-11-09 14:23:58 -06002606 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002607 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002608 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002609 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * @param name Name of the memory slab.
2612 * @param slab_block_size Size of each memory block (in bytes).
2613 * @param slab_num_blocks Number memory blocks.
2614 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002615 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002616#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2617 char __noinit __aligned(slab_align) \
2618 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2619 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002620 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002621 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2622 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002623
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002624/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002625 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002628 *
Allan Stephensda827222016-11-09 14:23:58 -06002629 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2630 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2631 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2632 * To ensure that each memory block is similarly aligned to this boundary,
2633 * @a slab_block_size must also be a multiple of N.
2634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @param slab Address of the memory slab.
2636 * @param buffer Pointer to buffer used for the memory blocks.
2637 * @param block_size Size of each memory block (in bytes).
2638 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002639 *
2640 * @return N/A
2641 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002642extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002643 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002644
2645/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @param slab Address of the memory slab.
2651 * @param mem Pointer to block address area.
2652 * @param timeout Maximum time to wait for operation to complete
2653 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2654 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002655 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002656 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002658 * @retval -ENOMEM Returned without waiting.
2659 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002660 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002661extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2662 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002663
2664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * This routine releases a previously allocated memory block back to its
2668 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @param slab Address of the memory slab.
2671 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002672 *
2673 * @return N/A
2674 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002675extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * This routine gets the number of memory blocks that are currently
2681 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002686 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002687static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002689 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690}
2691
Peter Mitsisc001aa82016-10-13 13:53:37 -04002692/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002693 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 * This routine gets the number of memory blocks that are currently
2696 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002698 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002701 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002702static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002703{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002704 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002705}
2706
Allan Stephensc98da842016-11-11 15:45:03 -05002707/**
2708 * @} end defgroup mem_slab_apis
2709 */
2710
2711/**
2712 * @cond INTERNAL_HIDDEN
2713 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002715/*
2716 * Memory pool requires a buffer and two arrays of structures for the
2717 * memory block accounting:
2718 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2719 * status of four blocks of memory.
2720 */
2721struct k_mem_pool_quad_block {
2722 char *mem_blocks; /* pointer to the first of four memory blocks */
2723 uint32_t mem_status; /* four bits. If bit is set, memory block is
2724 allocated */
2725};
2726/*
2727 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2728 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2729 * block size is 4 times less than the previous one and thus requires 4 times
2730 * bigger array of k_mem_pool_quad_block structures to keep track of the
2731 * memory blocks.
2732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002734/*
2735 * The array of k_mem_pool_block_set keeps the information of each array of
2736 * k_mem_pool_quad_block structures
2737 */
2738struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002739 size_t block_size; /* memory block size */
2740 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002741 struct k_mem_pool_quad_block *quad_block;
2742 int count;
2743};
2744
2745/* Memory pool descriptor */
2746struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002747 size_t max_block_size;
2748 size_t min_block_size;
2749 uint32_t nr_of_maxblocks;
2750 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002751 struct k_mem_pool_block_set *block_set;
2752 char *bufblock;
2753 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002754 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755};
2756
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002757#ifdef CONFIG_ARM
2758#define _SECTION_TYPE_SIGN "%"
2759#else
2760#define _SECTION_TYPE_SIGN "@"
2761#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002763/*
2764 * Static memory pool initialization
2765 */
Allan Stephensc98da842016-11-11 15:45:03 -05002766
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002767/*
2768 * Use .altmacro to be able to recalculate values and pass them as string
2769 * arguments when calling assembler macros resursively
2770 */
2771__asm__(".altmacro\n\t");
2772
2773/*
2774 * Recursively calls a macro
2775 * The followig global symbols need to be initialized:
2776 * __memory_pool_max_block_size - maximal size of the memory block
2777 * __memory_pool_min_block_size - minimal size of the memory block
2778 * Notes:
2779 * Global symbols are used due the fact that assembler macro allows only
2780 * one argument be passed with the % conversion
2781 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2782 * is used instead of / 4.
2783 * n_max argument needs to go first in the invoked macro, as some
2784 * assemblers concatenate \name and %(\n_max * 4) arguments
2785 * if \name goes first
2786 */
2787__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2788 ".ifge __memory_pool_max_block_size >> 2 -"
2789 " __memory_pool_min_block_size\n\t\t"
2790 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2791 "\\macro_name %(\\n_max * 4) \\name\n\t"
2792 ".endif\n\t"
2793 ".endm\n");
2794
2795/*
2796 * Build quad blocks
2797 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2798 * structures and recursively calls itself for the next array, 4 times
2799 * larger.
2800 * The followig global symbols need to be initialized:
2801 * __memory_pool_max_block_size - maximal size of the memory block
2802 * __memory_pool_min_block_size - minimal size of the memory block
2803 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2804 */
2805__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002806 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002807 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2808 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2809 ".if \\n_max % 4\n\t\t"
2810 ".skip __memory_pool_quad_block_size\n\t"
2811 ".endif\n\t"
2812 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2813 ".endm\n");
2814
2815/*
2816 * Build block sets and initialize them
2817 * Macro initializes the k_mem_pool_block_set structure and
2818 * recursively calls itself for the next one.
2819 * The followig global symbols need to be initialized:
2820 * __memory_pool_max_block_size - maximal size of the memory block
2821 * __memory_pool_min_block_size - minimal size of the memory block
2822 * __memory_pool_block_set_count, the number of the elements in the
2823 * block set array must be set to 0. Macro calculates it's real
2824 * value.
2825 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2826 * structures, _build_quad_blocks must be called prior it.
2827 */
2828__asm__(".macro _build_block_set n_max, name\n\t"
2829 ".int __memory_pool_max_block_size\n\t" /* block_size */
2830 ".if \\n_max % 4\n\t\t"
2831 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2832 ".else\n\t\t"
2833 ".int \\n_max >> 2\n\t"
2834 ".endif\n\t"
2835 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2836 ".int 0\n\t" /* count */
2837 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2838 "__do_recurse _build_block_set \\name \\n_max\n\t"
2839 ".endm\n");
2840
2841/*
2842 * Build a memory pool structure and initialize it
2843 * Macro uses __memory_pool_block_set_count global symbol,
2844 * block set addresses and buffer address, it may be called only after
2845 * _build_block_set
2846 */
2847__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002848 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002849 _SECTION_TYPE_SIGN "progbits\n\t"
2850 ".globl \\name\n\t"
2851 "\\name:\n\t"
2852 ".int \\max_size\n\t" /* max_block_size */
2853 ".int \\min_size\n\t" /* min_block_size */
2854 ".int \\n_max\n\t" /* nr_of_maxblocks */
2855 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2856 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2857 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2858 ".int 0\n\t" /* wait_q->head */
2859 ".int 0\n\t" /* wait_q->next */
2860 ".popsection\n\t"
2861 ".endm\n");
2862
2863#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2864 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2865 _SECTION_TYPE_SIGN "progbits\n\t"); \
2866 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2867 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2868 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2869 STRINGIFY(name) "\n\t"); \
2870 __asm__(".popsection\n\t")
2871
2872#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2873 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2874 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2875 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2876 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002877 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002878 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2879 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2880 STRINGIFY(name) "\n\t"); \
2881 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2882 __asm__(".int __memory_pool_block_set_count\n\t"); \
2883 __asm__(".popsection\n\t"); \
2884 extern uint32_t _mem_pool_block_set_count_##name; \
2885 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2886
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002887#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2888 char __noinit __aligned(align) \
2889 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002890
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002891/*
2892 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2893 * to __memory_pool_quad_block_size absolute symbol.
2894 * This function does not get called, but compiler calculates the value and
2895 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2896 */
2897static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2898{
2899 __asm__(".globl __memory_pool_quad_block_size\n\t"
2900#ifdef CONFIG_NIOS2
2901 "__memory_pool_quad_block_size = %0\n\t"
2902#else
2903 "__memory_pool_quad_block_size = %c0\n\t"
2904#endif
2905 :
2906 : "n"(sizeof(struct k_mem_pool_quad_block)));
2907}
2908
2909/**
Allan Stephensc98da842016-11-11 15:45:03 -05002910 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002911 */
2912
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002913/**
Allan Stephensc98da842016-11-11 15:45:03 -05002914 * @addtogroup mem_pool_apis
2915 * @{
2916 */
2917
2918/**
2919 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2922 * long. The memory pool allows blocks to be repeatedly partitioned into
2923 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2924 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002925 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002927 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002928 * If the pool is to be accessed outside the module where it is defined, it
2929 * can be declared via
2930 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002931 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * @param name Name of the memory pool.
2934 * @param min_size Size of the smallest blocks in the pool (in bytes).
2935 * @param max_size Size of the largest blocks in the pool (in bytes).
2936 * @param n_max Number of maximum sized blocks in the pool.
2937 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002938 */
2939#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002940 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2941 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002942 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002943 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2944 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2945 extern struct k_mem_pool name
2946
Peter Mitsis937042c2016-10-13 13:18:26 -04002947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @param pool Address of the memory pool.
2953 * @param block Pointer to block descriptor for the allocated memory.
2954 * @param size Amount of memory to allocate (in bytes).
2955 * @param timeout Maximum time to wait for operation to complete
2956 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2957 * or K_FOREVER to wait as long as necessary.
2958 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002959 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002961 * @retval -ENOMEM Returned without waiting.
2962 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002963 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002964extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002965 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002966
2967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * This routine releases a previously allocated memory block back to its
2971 * memory pool.
2972 *
2973 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002974 *
2975 * @return N/A
2976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002977extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002978
2979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002982 * This routine instructs a memory pool to concatenate unused memory blocks
2983 * into larger blocks wherever possible. Manually defragmenting the memory
2984 * pool may speed up future allocations of memory blocks by eliminating the
2985 * need for the memory pool to perform an automatic partial defragmentation.
2986 *
2987 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002988 *
2989 * @return N/A
2990 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002991extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002992
2993/**
Allan Stephensc98da842016-11-11 15:45:03 -05002994 * @} end addtogroup mem_pool_apis
2995 */
2996
2997/**
2998 * @defgroup heap_apis Heap Memory Pool APIs
2999 * @ingroup kernel_apis
3000 * @{
3001 */
3002
3003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003007 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003012 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003013extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003014
3015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003017 *
3018 * This routine provides traditional free() semantics. The memory being
3019 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003020 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003021 * If @a ptr is NULL, no operation is performed.
3022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003024 *
3025 * @return N/A
3026 */
3027extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028
Allan Stephensc98da842016-11-11 15:45:03 -05003029/**
3030 * @} end defgroup heap_apis
3031 */
3032
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003033/**
3034 * @brief Make the CPU idle.
3035 *
3036 * This function makes the CPU idle until an event wakes it up.
3037 *
3038 * In a regular system, the idle thread should be the only thread responsible
3039 * for making the CPU idle and triggering any type of power management.
3040 * However, in some more constrained systems, such as a single-threaded system,
3041 * the only thread would be responsible for this if needed.
3042 *
3043 * @return N/A
3044 */
3045extern void k_cpu_idle(void);
3046
3047/**
3048 * @brief Make the CPU idle in an atomic fashion.
3049 *
3050 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3051 * must be done atomically before making the CPU idle.
3052 *
3053 * @param key Interrupt locking key obtained from irq_lock().
3054 *
3055 * @return N/A
3056 */
3057extern void k_cpu_atomic_idle(unsigned int key);
3058
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059/*
3060 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
3061 * hook into the device subsystem, which itself uses nanokernel semaphores,
3062 * and thus currently requires the definition of nano_sem.
3063 */
3064#include <legacy.h>
3065#include <arch/cpu.h>
3066
3067/*
3068 * private APIs that are utilized by one or more public APIs
3069 */
3070
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003071#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003073#else
3074#define _init_static_threads() do { } while ((0))
3075#endif
3076
3077extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003078extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079
3080#ifdef __cplusplus
3081}
3082#endif
3083
Andrew Boiee004dec2016-11-07 09:01:19 -08003084#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3085/*
3086 * Define new and delete operators.
3087 * At this moment, the operators do nothing since objects are supposed
3088 * to be statically allocated.
3089 */
3090inline void operator delete(void *ptr)
3091{
3092 (void)ptr;
3093}
3094
3095inline void operator delete[](void *ptr)
3096{
3097 (void)ptr;
3098}
3099
3100inline void *operator new(size_t size)
3101{
3102 (void)size;
3103 return NULL;
3104}
3105
3106inline void *operator new[](size_t size)
3107{
3108 (void)size;
3109 return NULL;
3110}
3111
3112/* Placement versions of operator new and delete */
3113inline void operator delete(void *ptr1, void *ptr2)
3114{
3115 (void)ptr1;
3116 (void)ptr2;
3117}
3118
3119inline void operator delete[](void *ptr1, void *ptr2)
3120{
3121 (void)ptr1;
3122 (void)ptr2;
3123}
3124
3125inline void *operator new(size_t size, void *ptr)
3126{
3127 (void)size;
3128 return ptr;
3129}
3130
3131inline void *operator new[](size_t size, void *ptr)
3132{
3133 (void)size;
3134 return ptr;
3135}
3136
3137#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3138
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139#endif /* _kernel__h_ */