blob: 1a0cecced92c93320416ef2eebb83ce119e587b0 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
Anas Nashif173902f2017-01-17 07:08:56 -050028#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040029#include <toolchain.h>
30#include <sections.h>
31#include <atomic.h>
32#include <errno.h>
33#include <misc/__assert.h>
34#include <misc/dlist.h>
35#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050036#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050037#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050038#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
Anas Nashifbbb157d2017-01-15 08:46:31 -050044/**
45 * @brief Kernel APIs
46 * @defgroup kernel_apis Kernel APIs
47 * @{
48 * @}
49 */
50
Anas Nashif61f4b242016-11-18 10:53:59 -050051#ifdef CONFIG_KERNEL_DEBUG
52#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
54#else
55#define K_DEBUG(fmt, ...)
56#endif
57
Benjamin Walsh2f280412017-01-14 19:23:46 -050058#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
59#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
60#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
61#elif defined(CONFIG_COOP_ENABLED)
62#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
63#define _NUM_PREEMPT_PRIO (0)
64#elif defined(CONFIG_PREEMPT_ENABLED)
65#define _NUM_COOP_PRIO (0)
66#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
67#else
68#error "invalid configuration"
69#endif
70
71#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_PRIO_PREEMPT(x) (x)
73
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_ANY NULL
75#define K_END NULL
76
Benjamin Walshedb35702017-01-14 18:47:22 -050077#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050079#elif defined(CONFIG_COOP_ENABLED)
80#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
81#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050083#else
84#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085#endif
86
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050087#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
89#else
90#define K_LOWEST_THREAD_PRIO -1
91#endif
92
Benjamin Walshfab8d922016-11-08 15:36:36 -050093#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
94
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
96#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
97
98typedef sys_dlist_t _wait_q_t;
99
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#ifdef CONFIG_OBJECT_TRACING
101#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
102#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500104#define _OBJECT_TRACING_INIT
105#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400106#endif
107
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500108#define tcs k_thread
109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
116struct k_fifo;
117struct k_lifo;
118struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400119struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400120struct k_mem_pool;
121struct k_timer;
122
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400123typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125enum execution_context_types {
126 K_ISR = 0,
127 K_COOP_THREAD,
128 K_PREEMPT_THREAD,
129};
130
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400131/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100132 * @defgroup profiling_apis Profiling APIs
133 * @ingroup kernel_apis
134 * @{
135 */
136
137/**
138 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
139 *
140 * This routine calls @ref stack_analyze on the 4 call stacks declared and
141 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
142 *
143 * CONFIG_MAIN_STACK_SIZE
144 * CONFIG_IDLE_STACK_SIZE
145 * CONFIG_ISR_STACK_SIZE
146 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
147 *
148 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
149 * produce output.
150 *
151 * @return N/A
152 */
153extern void k_call_stacks_analyze(void);
154
155/**
156 * @} end defgroup profiling_apis
157 */
158
159/**
Allan Stephensc98da842016-11-11 15:45:03 -0500160 * @defgroup thread_apis Thread APIs
161 * @ingroup kernel_apis
162 * @{
163 */
164
165/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500166 * @typedef k_thread_entry_t
167 * @brief Thread entry point function type.
168 *
169 * A thread's entry point function is invoked when the thread starts executing.
170 * Up to 3 argument values can be passed to the function.
171 *
172 * The thread terminates execution permanently if the entry point function
173 * returns. The thread is responsible for releasing any shared resources
174 * it may own (such as mutexes and dynamically allocated memory), prior to
175 * returning.
176 *
177 * @param p1 First argument.
178 * @param p2 Second argument.
179 * @param p3 Third argument.
180 *
181 * @return N/A
182 */
183typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
184
185/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500186 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500188 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500190 * The new thread may be scheduled for immediate execution or a delayed start.
191 * If the newly spawned thread does not have a delayed start the kernel
192 * scheduler may preempt the current thread to allow the new thread to
193 * execute.
194 *
195 * Thread options are architecture-specific, and can include K_ESSENTIAL,
196 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
197 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400198 *
199 * @param stack Pointer to the stack space.
200 * @param stack_size Stack size in bytes.
201 * @param entry Thread entry function.
202 * @param p1 1st entry point parameter.
203 * @param p2 2nd entry point parameter.
204 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500205 * @param prio Thread priority.
206 * @param options Thread options.
207 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500209 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500211extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500212 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400213 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500214 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400215
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500217 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephensc98da842016-11-11 15:45:03 -0500219 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500222 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400223 *
224 * @return N/A
225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227
228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 *
231 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500232 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400233 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400234 * @return N/A
235 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400236extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400237
238/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500239 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500241 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500243 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400244 *
245 * @return N/A
246 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400247extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400248
249/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500250 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500252 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500254 * If @a thread is not currently sleeping, the routine has no effect.
255 *
256 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400257 *
258 * @return N/A
259 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400261
262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500263 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500265 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400267extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400268
269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500270 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500272 * This routine prevents @a thread from executing if it has not yet started
273 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500275 * @param thread ID of thread to cancel.
276 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500277 * @retval 0 Thread spawning cancelled.
278 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400279 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400280extern int k_thread_cancel(k_tid_t thread);
281
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400282/**
Allan Stephensc98da842016-11-11 15:45:03 -0500283 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500285 * This routine permanently stops execution of @a thread. The thread is taken
286 * off all kernel queues it is part of (i.e. the ready queue, the timeout
287 * queue, or a kernel object wait queue). However, any kernel resources the
288 * thread might currently own (such as mutexes or memory blocks) are not
289 * released. It is the responsibility of the caller of this routine to ensure
290 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500292 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400293 *
294 * @return N/A
295 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400296extern void k_thread_abort(k_tid_t thread);
297
Allan Stephensc98da842016-11-11 15:45:03 -0500298/**
299 * @cond INTERNAL_HIDDEN
300 */
301
Benjamin Walshd211a522016-12-06 11:44:01 -0500302/* timeout has timed out and is not on _timeout_q anymore */
303#define _EXPIRED (-2)
304
305/* timeout is not in use */
306#define _INACTIVE (-1)
307
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400308#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400309#define _THREAD_TIMEOUT_INIT(obj) \
310 (obj).nano_timeout = { \
311 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400312 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400313 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500314 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400315 },
316#else
317#define _THREAD_TIMEOUT_INIT(obj)
318#endif
319
320#ifdef CONFIG_ERRNO
321#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
322#else
323#define _THREAD_ERRNO_INIT(obj)
324#endif
325
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400326struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400327 union {
328 char *init_stack;
329 struct k_thread *thread;
330 };
331 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500332 void (*init_entry)(void *, void *, void *);
333 void *init_p1;
334 void *init_p2;
335 void *init_p3;
336 int init_prio;
337 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400338 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500339 void (*init_abort)(void);
340 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400341};
342
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400343#define _THREAD_INITIALIZER(stack, stack_size, \
344 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500345 prio, options, delay, abort, groups) \
346 { \
347 .init_stack = (stack), \
348 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400349 .init_entry = (void (*)(void *, void *, void *))entry, \
350 .init_p1 = (void *)p1, \
351 .init_p2 = (void *)p2, \
352 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500353 .init_prio = (prio), \
354 .init_options = (options), \
355 .init_delay = (delay), \
356 .init_abort = (abort), \
357 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400358 }
359
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400360/**
Allan Stephensc98da842016-11-11 15:45:03 -0500361 * INTERNAL_HIDDEN @endcond
362 */
363
364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500365 * @brief Statically define and initialize a thread.
366 *
367 * The thread may be scheduled for immediate execution or a delayed start.
368 *
369 * Thread options are architecture-specific, and can include K_ESSENTIAL,
370 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
371 * them using "|" (the logical OR operator).
372 *
373 * The ID of the thread can be accessed using:
374 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500375 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500376 *
377 * @param name Name of the thread.
378 * @param stack_size Stack size in bytes.
379 * @param entry Thread entry function.
380 * @param p1 1st entry point parameter.
381 * @param p2 2nd entry point parameter.
382 * @param p3 3rd entry point parameter.
383 * @param prio Thread priority.
384 * @param options Thread options.
385 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400386 *
387 * @internal It has been observed that the x86 compiler by default aligns
388 * these _static_thread_data structures to 32-byte boundaries, thereby
389 * wasting space. To work around this, force a 4-byte alignment.
390 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500391#define K_THREAD_DEFINE(name, stack_size, \
392 entry, p1, p2, p3, \
393 prio, options, delay) \
394 char __noinit __stack _k_thread_obj_##name[stack_size]; \
395 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500396 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500397 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
398 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500399 NULL, 0); \
400 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400401
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500403 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500405 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500407 * @param thread ID of thread whose priority is needed.
408 *
409 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500411extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412
413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500414 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500416 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417 *
418 * Rescheduling can occur immediately depending on the priority @a thread is
419 * set to:
420 *
421 * - If its priority is raised above the priority of the caller of this
422 * function, and the caller is preemptible, @a thread will be scheduled in.
423 *
424 * - If the caller operates on itself, it lowers its priority below that of
425 * other threads in the system, and the caller is preemptible, the thread of
426 * highest priority will be scheduled in.
427 *
428 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
429 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
430 * highest priority.
431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500432 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400433 * @param prio New priority.
434 *
435 * @warning Changing the priority of a thread currently involved in mutex
436 * priority inheritance may result in undefined behavior.
437 *
438 * @return N/A
439 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400440extern void k_thread_priority_set(k_tid_t thread, int prio);
441
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500443 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500445 * This routine prevents the kernel scheduler from making @a thread the
446 * current thread. All other internal operations on @a thread are still
447 * performed; for example, any timeout it is waiting on keeps ticking,
448 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500450 * If @a thread is already suspended, the routine has no effect.
451 *
452 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 *
454 * @return N/A
455 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400456extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457
458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500461 * This routine allows the kernel scheduler to make @a thread the current
462 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * If @a thread is not currently suspended, the routine has no effect.
465 *
466 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 *
468 * @return N/A
469 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400470extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400471
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500473 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500475 * This routine specifies how the scheduler will perform time slicing of
476 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500478 * To enable time slicing, @a slice must be non-zero. The scheduler
479 * ensures that no thread runs for more than the specified time limit
480 * before other threads of that priority are given a chance to execute.
481 * Any thread whose priority is higher than @a prio is exempted, and may
482 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485 * execute. Once the scheduler selects a thread for execution, there is no
486 * minimum guaranteed time the thread will execute before threads of greater or
487 * equal priority are scheduled.
488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400490 * for execution, this routine has no effect; the thread is immediately
491 * rescheduled after the slice period expires.
492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * To disable timeslicing, set both @a slice and @a prio to zero.
494 *
495 * @param slice Maximum time slice length (in milliseconds).
496 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400497 *
498 * @return N/A
499 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400500extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400501
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400502/**
Allan Stephensc98da842016-11-11 15:45:03 -0500503 * @} end defgroup thread_apis
504 */
505
506/**
507 * @addtogroup isr_apis
508 * @{
509 */
510
511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500512 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400513 *
Allan Stephensc98da842016-11-11 15:45:03 -0500514 * This routine allows the caller to customize its actions, depending on
515 * whether it is a thread or an ISR.
516 *
517 * @note Can be called by ISRs.
518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500519 * @return 0 if invoked by a thread.
520 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400521 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500522extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400523
Benjamin Walsh445830d2016-11-10 15:54:27 -0500524/**
525 * @brief Determine if code is running in a preemptible thread.
526 *
Allan Stephensc98da842016-11-11 15:45:03 -0500527 * This routine allows the caller to customize its actions, depending on
528 * whether it can be preempted by another thread. The routine returns a 'true'
529 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500530 *
Allan Stephensc98da842016-11-11 15:45:03 -0500531 * - The code is running in a thread, not at ISR.
532 * - The thread's priority is in the preemptible range.
533 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500534 *
Allan Stephensc98da842016-11-11 15:45:03 -0500535 * @note Can be called by ISRs.
536 *
537 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500538 * @return Non-zero if invoked by a preemptible thread.
539 */
540extern int k_is_preempt_thread(void);
541
Allan Stephensc98da842016-11-11 15:45:03 -0500542/**
543 * @} end addtogroup isr_apis
544 */
545
546/**
547 * @addtogroup thread_apis
548 * @{
549 */
550
551/**
552 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500553 *
Allan Stephensc98da842016-11-11 15:45:03 -0500554 * This routine prevents the current thread from being preempted by another
555 * thread by instructing the scheduler to treat it as a cooperative thread.
556 * If the thread subsequently performs an operation that makes it unready,
557 * it will be context switched out in the normal manner. When the thread
558 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500559 *
Allan Stephensc98da842016-11-11 15:45:03 -0500560 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500561 *
Allan Stephensc98da842016-11-11 15:45:03 -0500562 * @note k_sched_lock() and k_sched_unlock() should normally be used
563 * when the operation being performed can be safely interrupted by ISRs.
564 * However, if the amount of processing involved is very small, better
565 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500566 *
567 * @return N/A
568 */
569extern void k_sched_lock(void);
570
Allan Stephensc98da842016-11-11 15:45:03 -0500571/**
572 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500573 *
Allan Stephensc98da842016-11-11 15:45:03 -0500574 * This routine reverses the effect of a previous call to k_sched_lock().
575 * A thread must call the routine once for each time it called k_sched_lock()
576 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500577 *
578 * @return N/A
579 */
580extern void k_sched_unlock(void);
581
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500585 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500587 * Custom data is not used by the kernel itself, and is freely available
588 * for a thread to use as it sees fit. It can be used as a framework
589 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500591 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592 *
593 * @return N/A
594 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596
597/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500598 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400604extern void *k_thread_custom_data_get(void);
605
606/**
Allan Stephensc98da842016-11-11 15:45:03 -0500607 * @} end addtogroup thread_apis
608 */
609
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400610#include <sys_clock.h>
611
Allan Stephensc2f15a42016-11-17 12:24:22 -0500612/**
613 * @addtogroup clock_apis
614 * @{
615 */
616
617/**
618 * @brief Generate null timeout delay.
619 *
620 * This macro generates a timeout delay that that instructs a kernel API
621 * not to wait if the requested operation cannot be performed immediately.
622 *
623 * @return Timeout delay value.
624 */
625#define K_NO_WAIT 0
626
627/**
628 * @brief Generate timeout delay from milliseconds.
629 *
630 * This macro generates a timeout delay that that instructs a kernel API
631 * to wait up to @a ms milliseconds to perform the requested operation.
632 *
633 * @param ms Duration in milliseconds.
634 *
635 * @return Timeout delay value.
636 */
Johan Hedberg14471692016-11-13 10:52:15 +0200637#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500638
639/**
640 * @brief Generate timeout delay from seconds.
641 *
642 * This macro generates a timeout delay that that instructs a kernel API
643 * to wait up to @a s seconds to perform the requested operation.
644 *
645 * @param s Duration in seconds.
646 *
647 * @return Timeout delay value.
648 */
Johan Hedberg14471692016-11-13 10:52:15 +0200649#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500650
651/**
652 * @brief Generate timeout delay from minutes.
653 *
654 * This macro generates a timeout delay that that instructs a kernel API
655 * to wait up to @a m minutes to perform the requested operation.
656 *
657 * @param m Duration in minutes.
658 *
659 * @return Timeout delay value.
660 */
Johan Hedberg14471692016-11-13 10:52:15 +0200661#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500662
663/**
664 * @brief Generate timeout delay from hours.
665 *
666 * This macro generates a timeout delay that that instructs a kernel API
667 * to wait up to @a h hours to perform the requested operation.
668 *
669 * @param h Duration in hours.
670 *
671 * @return Timeout delay value.
672 */
Johan Hedberg14471692016-11-13 10:52:15 +0200673#define K_HOURS(h) K_MINUTES((h) * 60)
674
Allan Stephensc98da842016-11-11 15:45:03 -0500675/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500676 * @brief Generate infinite timeout delay.
677 *
678 * This macro generates a timeout delay that that instructs a kernel API
679 * to wait as long as necessary to perform the requested operation.
680 *
681 * @return Timeout delay value.
682 */
683#define K_FOREVER (-1)
684
685/**
686 * @} end addtogroup clock_apis
687 */
688
689/**
Allan Stephensc98da842016-11-11 15:45:03 -0500690 * @cond INTERNAL_HIDDEN
691 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400692
Benjamin Walsh62092182016-12-20 14:39:08 -0500693/* kernel clocks */
694
695#if (sys_clock_ticks_per_sec == 1000) || \
696 (sys_clock_ticks_per_sec == 500) || \
697 (sys_clock_ticks_per_sec == 250) || \
698 (sys_clock_ticks_per_sec == 125) || \
699 (sys_clock_ticks_per_sec == 100) || \
700 (sys_clock_ticks_per_sec == 50) || \
701 (sys_clock_ticks_per_sec == 25) || \
702 (sys_clock_ticks_per_sec == 20) || \
703 (sys_clock_ticks_per_sec == 10) || \
704 (sys_clock_ticks_per_sec == 1)
705
706 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
707#else
708 /* yields horrible 64-bit math on many architectures: try to avoid */
709 #define _NON_OPTIMIZED_TICKS_PER_SEC
710#endif
711
712#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
713extern int32_t _ms_to_ticks(int32_t ms);
714#else
715static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
716{
717 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
718}
719#endif
720
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500721/* added tick needed to account for tick in progress */
722#define _TICK_ALIGN 1
723
Benjamin Walsh62092182016-12-20 14:39:08 -0500724static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400725{
Benjamin Walsh62092182016-12-20 14:39:08 -0500726#ifdef CONFIG_SYS_CLOCK_EXISTS
727
728#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400729 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400730#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500731 return (uint64_t)ticks * _ms_per_tick;
732#endif
733
734#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400735 __ASSERT(ticks == 0, "");
736 return 0;
737#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400738}
739
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400740/* timeouts */
741
742struct _timeout;
743typedef void (*_timeout_func_t)(struct _timeout *t);
744
745struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500746 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400747 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400748 sys_dlist_t *wait_q;
749 int32_t delta_ticks_from_prev;
750 _timeout_func_t func;
751};
752
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200753extern int32_t _timeout_remaining_get(struct _timeout *timeout);
754
Allan Stephensc98da842016-11-11 15:45:03 -0500755/**
756 * INTERNAL_HIDDEN @endcond
757 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500758
Allan Stephensc98da842016-11-11 15:45:03 -0500759/**
760 * @cond INTERNAL_HIDDEN
761 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400762
763struct k_timer {
764 /*
765 * _timeout structure must be first here if we want to use
766 * dynamic timer allocation. timeout.node is used in the double-linked
767 * list of free timers
768 */
769 struct _timeout timeout;
770
Allan Stephens45bfa372016-10-12 12:39:42 -0500771 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400772 _wait_q_t wait_q;
773
774 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500775 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400776
777 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500778 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400779
780 /* timer period */
781 int32_t period;
782
Allan Stephens45bfa372016-10-12 12:39:42 -0500783 /* timer status */
784 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400785
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500786 /* user-specific data, also used to support legacy features */
787 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400788
Anas Nashif2f203c22016-12-18 06:57:45 -0500789 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400790};
791
Allan Stephens1342adb2016-11-03 13:54:53 -0500792#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400793 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500794 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500795 .timeout.wait_q = NULL, \
796 .timeout.thread = NULL, \
797 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400798 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500799 .expiry_fn = expiry, \
800 .stop_fn = stop, \
801 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500802 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500803 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400804 }
805
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806/**
Allan Stephensc98da842016-11-11 15:45:03 -0500807 * INTERNAL_HIDDEN @endcond
808 */
809
810/**
811 * @defgroup timer_apis Timer APIs
812 * @ingroup kernel_apis
813 * @{
814 */
815
816/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500817 * @typedef k_timer_expiry_t
818 * @brief Timer expiry function type.
819 *
820 * A timer's expiry function is executed by the system clock interrupt handler
821 * each time the timer expires. The expiry function is optional, and is only
822 * invoked if the timer has been initialized with one.
823 *
824 * @param timer Address of timer.
825 *
826 * @return N/A
827 */
828typedef void (*k_timer_expiry_t)(struct k_timer *timer);
829
830/**
831 * @typedef k_timer_stop_t
832 * @brief Timer stop function type.
833 *
834 * A timer's stop function is executed if the timer is stopped prematurely.
835 * The function runs in the context of the thread that stops the timer.
836 * The stop function is optional, and is only invoked if the timer has been
837 * initialized with one.
838 *
839 * @param timer Address of timer.
840 *
841 * @return N/A
842 */
843typedef void (*k_timer_stop_t)(struct k_timer *timer);
844
845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500850 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
852 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * @param expiry_fn Function to invoke each time the timer expires.
854 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500856#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500857 struct k_timer name \
858 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500859 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400860
Allan Stephens45bfa372016-10-12 12:39:42 -0500861/**
862 * @brief Initialize a timer.
863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500865 *
866 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @param expiry_fn Function to invoke each time the timer expires.
868 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500869 *
870 * @return N/A
871 */
872extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500873 k_timer_expiry_t expiry_fn,
874 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700875
Allan Stephens45bfa372016-10-12 12:39:42 -0500876/**
877 * @brief Start a timer.
878 *
879 * This routine starts a timer, and resets its status to zero. The timer
880 * begins counting down using the specified duration and period values.
881 *
882 * Attempting to start a timer that is already running is permitted.
883 * The timer's status is reset to zero and the timer begins counting down
884 * using the new duration and period values.
885 *
886 * @param timer Address of timer.
887 * @param duration Initial timer duration (in milliseconds).
888 * @param period Timer period (in milliseconds).
889 *
890 * @return N/A
891 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400892extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500893 int32_t duration, int32_t period);
894
895/**
896 * @brief Stop a timer.
897 *
898 * This routine stops a running timer prematurely. The timer's stop function,
899 * if one exists, is invoked by the caller.
900 *
901 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500902 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500903 *
904 * @param timer Address of timer.
905 *
906 * @return N/A
907 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500909
910/**
911 * @brief Read timer status.
912 *
913 * This routine reads the timer's status, which indicates the number of times
914 * it has expired since its status was last read.
915 *
916 * Calling this routine resets the timer's status to zero.
917 *
918 * @param timer Address of timer.
919 *
920 * @return Timer status.
921 */
922extern uint32_t k_timer_status_get(struct k_timer *timer);
923
924/**
925 * @brief Synchronize thread to timer expiration.
926 *
927 * This routine blocks the calling thread until the timer's status is non-zero
928 * (indicating that it has expired at least once since it was last examined)
929 * or the timer is stopped. If the timer status is already non-zero,
930 * or the timer is already stopped, the caller continues without waiting.
931 *
932 * Calling this routine resets the timer's status to zero.
933 *
934 * This routine must not be used by interrupt handlers, since they are not
935 * allowed to block.
936 *
937 * @param timer Address of timer.
938 *
939 * @return Timer status.
940 */
941extern uint32_t k_timer_status_sync(struct k_timer *timer);
942
943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500945 *
946 * This routine computes the (approximate) time remaining before a running
947 * timer next expires. If the timer is not running, it returns zero.
948 *
949 * @param timer Address of timer.
950 *
951 * @return Remaining time (in milliseconds).
952 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200953static inline int32_t k_timer_remaining_get(struct k_timer *timer)
954{
955 return _timeout_remaining_get(&timer->timeout);
956}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400957
Allan Stephensc98da842016-11-11 15:45:03 -0500958/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500959 * @brief Associate user-specific data with a timer.
960 *
961 * This routine records the @a user_data with the @a timer, to be retrieved
962 * later.
963 *
964 * It can be used e.g. in a timer handler shared across multiple subsystems to
965 * retrieve data specific to the subsystem this timer is associated with.
966 *
967 * @param timer Address of timer.
968 * @param user_data User data to associate with the timer.
969 *
970 * @return N/A
971 */
972static inline void k_timer_user_data_set(struct k_timer *timer,
973 void *user_data)
974{
975 timer->user_data = user_data;
976}
977
978/**
979 * @brief Retrieve the user-specific data from a timer.
980 *
981 * @param timer Address of timer.
982 *
983 * @return The user data.
984 */
985static inline void *k_timer_user_data_get(struct k_timer *timer)
986{
987 return timer->user_data;
988}
989
990/**
Allan Stephensc98da842016-11-11 15:45:03 -0500991 * @} end defgroup timer_apis
992 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400993
Allan Stephensc98da842016-11-11 15:45:03 -0500994/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500995 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500996 * @{
997 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500998
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * This routine returns the elapsed time since the system booted,
1003 * in milliseconds.
1004 *
1005 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001006 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001007extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001008
1009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * This routine returns the lower 32-bits of the elapsed time since the system
1013 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * This routine can be more efficient than k_uptime_get(), as it reduces the
1016 * need for interrupt locking and 64-bit math. However, the 32-bit result
1017 * cannot hold a system uptime time larger than approximately 50 days, so the
1018 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001021 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001022extern uint32_t k_uptime_get_32(void);
1023
1024/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001027 * This routine computes the elapsed time between the current system uptime
1028 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @param reftime Pointer to a reference time, which is updated to the current
1031 * uptime upon return.
1032 *
1033 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001034 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001035extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001036
1037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * This routine computes the elapsed time between the current system uptime
1041 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1044 * need for interrupt locking and 64-bit math. However, the 32-bit result
1045 * cannot hold an elapsed time larger than approximately 50 days, so the
1046 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @param reftime Pointer to a reference time, which is updated to the current
1049 * uptime upon return.
1050 *
1051 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001052 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001053extern uint32_t k_uptime_delta_32(int64_t *reftime);
1054
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine returns the current time, as measured by the system's hardware
1059 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063extern uint32_t k_cycle_get_32(void);
1064
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001065/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001066 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067 */
1068
Allan Stephensc98da842016-11-11 15:45:03 -05001069/**
1070 * @cond INTERNAL_HIDDEN
1071 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072
1073struct k_fifo {
1074 _wait_q_t wait_q;
1075 sys_slist_t data_q;
1076
Anas Nashif2f203c22016-12-18 06:57:45 -05001077 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001078};
1079
Allan Stephensc98da842016-11-11 15:45:03 -05001080#define K_FIFO_INITIALIZER(obj) \
1081 { \
1082 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1083 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001084 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001085 }
1086
1087/**
1088 * INTERNAL_HIDDEN @endcond
1089 */
1090
1091/**
1092 * @defgroup fifo_apis Fifo APIs
1093 * @ingroup kernel_apis
1094 * @{
1095 */
1096
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001098 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
1104 * @return N/A
1105 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107
1108/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001109 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001111 * This routine adds a data item to @a fifo. A fifo data item must be
1112 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1113 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * @note Can be called by ISRs.
1116 *
1117 * @param fifo Address of the fifo.
1118 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
1120 * @return N/A
1121 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001122extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123
1124/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * This routine adds a list of data items to @a fifo in one operation.
1128 * The data items must be in a singly-linked list, with the first 32 bits
1129 * each data item pointing to the next data item; the list must be
1130 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * @param fifo Address of the fifo.
1135 * @param head Pointer to first node in singly-linked list.
1136 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
1138 * @return N/A
1139 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001140extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141
1142/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * This routine adds a list of data items to @a fifo in one operation.
1146 * The data items must be in a singly-linked list implemented using a
1147 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148 * and must be re-initialized via sys_slist_init().
1149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @note Can be called by ISRs.
1151 *
1152 * @param fifo Address of the fifo.
1153 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 *
1155 * @return N/A
1156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158
1159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001160 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001162 * This routine removes a data item from @a fifo in a "first in, first out"
1163 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1166 *
1167 * @param fifo Address of the fifo.
1168 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 * or one of the special values K_NO_WAIT and K_FOREVER.
1170 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001171 * @return Address of the data item if successful; NULL if returned
1172 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1175
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001181 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001185#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001186 struct k_fifo name \
1187 __in_section(_k_fifo, static, name) = \
1188 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189
Allan Stephensc98da842016-11-11 15:45:03 -05001190/**
1191 * @} end defgroup fifo_apis
1192 */
1193
1194/**
1195 * @cond INTERNAL_HIDDEN
1196 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
1198struct k_lifo {
1199 _wait_q_t wait_q;
1200 void *list;
1201
Anas Nashif2f203c22016-12-18 06:57:45 -05001202 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001203};
1204
Allan Stephensc98da842016-11-11 15:45:03 -05001205#define K_LIFO_INITIALIZER(obj) \
1206 { \
1207 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1208 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001209 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001210 }
1211
1212/**
1213 * INTERNAL_HIDDEN @endcond
1214 */
1215
1216/**
1217 * @defgroup lifo_apis Lifo APIs
1218 * @ingroup kernel_apis
1219 * @{
1220 */
1221
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001227 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 *
1229 * @return N/A
1230 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001231extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232
1233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * This routine adds a data item to @a lifo. A lifo data item must be
1237 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1238 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * @note Can be called by ISRs.
1241 *
1242 * @param lifo Address of the lifo.
1243 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 *
1245 * @return N/A
1246 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001247extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248
1249/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001250 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * This routine removes a data item from @a lifo in a "last in, first out"
1253 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001255 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1256 *
1257 * @param lifo Address of the lifo.
1258 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001259 * or one of the special values K_NO_WAIT and K_FOREVER.
1260 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001261 * @return Address of the data item if successful; NULL if returned
1262 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001263 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001264extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1265
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001269 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001270 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001271 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001276 struct k_lifo name \
1277 __in_section(_k_lifo, static, name) = \
1278 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279
Allan Stephensc98da842016-11-11 15:45:03 -05001280/**
1281 * @} end defgroup lifo_apis
1282 */
1283
1284/**
1285 * @cond INTERNAL_HIDDEN
1286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287
1288struct k_stack {
1289 _wait_q_t wait_q;
1290 uint32_t *base, *next, *top;
1291
Anas Nashif2f203c22016-12-18 06:57:45 -05001292 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001293};
1294
Allan Stephensc98da842016-11-11 15:45:03 -05001295#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1296 { \
1297 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1298 .base = stack_buffer, \
1299 .next = stack_buffer, \
1300 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001301 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001302 }
1303
1304/**
1305 * INTERNAL_HIDDEN @endcond
1306 */
1307
1308/**
1309 * @defgroup stack_apis Stack APIs
1310 * @ingroup kernel_apis
1311 * @{
1312 */
1313
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001314/**
1315 * @brief Initialize a stack.
1316 *
1317 * This routine initializes a stack object, prior to its first use.
1318 *
1319 * @param stack Address of the stack.
1320 * @param buffer Address of array used to hold stacked values.
1321 * @param num_entries Maximum number of values that can be stacked.
1322 *
1323 * @return N/A
1324 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001325extern void k_stack_init(struct k_stack *stack,
1326 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001327
1328/**
1329 * @brief Push an element onto a stack.
1330 *
1331 * This routine adds a 32-bit value @a data to @a stack.
1332 *
1333 * @note Can be called by ISRs.
1334 *
1335 * @param stack Address of the stack.
1336 * @param data Value to push onto the stack.
1337 *
1338 * @return N/A
1339 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001340extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341
1342/**
1343 * @brief Pop an element from a stack.
1344 *
1345 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1346 * manner and stores the value in @a data.
1347 *
1348 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1349 *
1350 * @param stack Address of the stack.
1351 * @param data Address of area to hold the value popped from the stack.
1352 * @param timeout Waiting period to obtain a value (in milliseconds),
1353 * or one of the special values K_NO_WAIT and K_FOREVER.
1354 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001355 * @retval 0 Element popped from stack.
1356 * @retval -EBUSY Returned without waiting.
1357 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001359extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1360
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001362 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001364 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001365 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001366 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001368 * @param name Name of the stack.
1369 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001370 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001371#define K_STACK_DEFINE(name, stack_num_entries) \
1372 uint32_t __noinit \
1373 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001374 struct k_stack name \
1375 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001376 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1377 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379/**
Allan Stephensc98da842016-11-11 15:45:03 -05001380 * @} end defgroup stack_apis
1381 */
1382
Allan Stephens6bba9b02016-11-16 14:56:54 -05001383struct k_work;
1384
Allan Stephensc98da842016-11-11 15:45:03 -05001385/**
1386 * @defgroup workqueue_apis Workqueue Thread APIs
1387 * @ingroup kernel_apis
1388 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001389 */
1390
Allan Stephens6bba9b02016-11-16 14:56:54 -05001391/**
1392 * @typedef k_work_handler_t
1393 * @brief Work item handler function type.
1394 *
1395 * A work item's handler function is executed by a workqueue's thread
1396 * when the work item is processed by the workqueue.
1397 *
1398 * @param work Address of the work item.
1399 *
1400 * @return N/A
1401 */
1402typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
1404/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001405 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001407
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408struct k_work_q {
1409 struct k_fifo fifo;
1410};
1411
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001413 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414};
1415
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001416struct k_work {
1417 void *_reserved; /* Used by k_fifo implementation. */
1418 k_work_handler_t handler;
1419 atomic_t flags[1];
1420};
1421
Allan Stephens6bba9b02016-11-16 14:56:54 -05001422struct k_delayed_work {
1423 struct k_work work;
1424 struct _timeout timeout;
1425 struct k_work_q *work_q;
1426};
1427
1428extern struct k_work_q k_sys_work_q;
1429
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001431 * INTERNAL_HIDDEN @endcond
1432 */
1433
1434/**
1435 * @brief Initialize a statically-defined work item.
1436 *
1437 * This macro can be used to initialize a statically-defined workqueue work
1438 * item, prior to its first use. For example,
1439 *
1440 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1441 *
1442 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001443 */
1444#define K_WORK_INITIALIZER(work_handler) \
1445 { \
1446 ._reserved = NULL, \
1447 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001448 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001449 }
1450
1451/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001452 * @brief Initialize a work item.
1453 *
1454 * This routine initializes a workqueue work item, prior to its first use.
1455 *
1456 * @param work Address of work item.
1457 * @param handler Function to invoke each time work item is processed.
1458 *
1459 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460 */
1461static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1462{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001463 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464 work->handler = handler;
1465}
1466
1467/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001468 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001469 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001470 * This routine submits work item @a work to be processed by workqueue
1471 * @a work_q. If the work item is already pending in the workqueue's queue
1472 * as a result of an earlier submission, this routine has no effect on the
1473 * work item. If the work item has already been processed, or is currently
1474 * being processed, its work is considered complete and the work item can be
1475 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001476 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001477 * @warning
1478 * A submitted work item must not be modified until it has been processed
1479 * by the workqueue.
1480 *
1481 * @note Can be called by ISRs.
1482 *
1483 * @param work_q Address of workqueue.
1484 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001485 *
1486 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001487 */
1488static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1489 struct k_work *work)
1490{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001491 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001492 k_fifo_put(&work_q->fifo, work);
1493 }
1494}
1495
1496/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001497 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001499 * This routine indicates if work item @a work is pending in a workqueue's
1500 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001501 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001502 * @note Can be called by ISRs.
1503 *
1504 * @param work Address of work item.
1505 *
1506 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001507 */
1508static inline int k_work_pending(struct k_work *work)
1509{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001510 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001511}
1512
1513/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001514 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001515 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001516 * This routine starts workqueue @a work_q. The workqueue spawns its work
1517 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001518 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001519 * @param work_q Address of workqueue.
1520 * @param stack Pointer to work queue thread's stack space.
1521 * @param stack_size Size of the work queue thread's stack (in bytes).
1522 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001523 *
1524 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525 */
Allan Stephens904cf972016-10-07 13:59:23 -05001526extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001527 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001528
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001530 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001531 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001532 * This routine initializes a workqueue delayed work item, prior to
1533 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001535 * @param work Address of delayed work item.
1536 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537 *
1538 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001539 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001540extern void k_delayed_work_init(struct k_delayed_work *work,
1541 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542
1543/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001544 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001546 * This routine schedules work item @a work to be processed by workqueue
1547 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1548 * an asychronous countdown for the work item and then returns to the caller.
1549 * Only when the countdown completes is the work item actually submitted to
1550 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001551 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001552 * Submitting a previously submitted delayed work item that is still
1553 * counting down cancels the existing submission and restarts the countdown
1554 * using the new delay. If the work item is currently pending on the
1555 * workqueue's queue because the countdown has completed it is too late to
1556 * resubmit the item, and resubmission fails without impacting the work item.
1557 * If the work item has already been processed, or is currently being processed,
1558 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001560 * @warning
1561 * A delayed work item must not be modified until it has been processed
1562 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001563 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001564 * @note Can be called by ISRs.
1565 *
1566 * @param work_q Address of workqueue.
1567 * @param work Address of delayed work item.
1568 * @param delay Delay before submitting the work item (in milliseconds).
1569 *
1570 * @retval 0 Work item countdown started.
1571 * @retval -EINPROGRESS Work item is already pending.
1572 * @retval -EINVAL Work item is being processed or has completed its work.
1573 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001574 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001575extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1576 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001577 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001578
1579/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001580 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001581 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001582 * This routine cancels the submission of delayed work item @a work.
1583 * A delayed work item can only be cancelled while its countdown is still
1584 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001586 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001588 * @param work Address of delayed work item.
1589 *
1590 * @retval 0 Work item countdown cancelled.
1591 * @retval -EINPROGRESS Work item is already pending.
1592 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001593 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001594extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001595
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001597 * @brief Submit a work item to the system workqueue.
1598 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001599 * This routine submits work item @a work to be processed by the system
1600 * workqueue. If the work item is already pending in the workqueue's queue
1601 * as a result of an earlier submission, this routine has no effect on the
1602 * work item. If the work item has already been processed, or is currently
1603 * being processed, its work is considered complete and the work item can be
1604 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001605 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001606 * @warning
1607 * Work items submitted to the system workqueue should avoid using handlers
1608 * that block or yield since this may prevent the system workqueue from
1609 * processing other work items in a timely manner.
1610 *
1611 * @note Can be called by ISRs.
1612 *
1613 * @param work Address of work item.
1614 *
1615 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001616 */
1617static inline void k_work_submit(struct k_work *work)
1618{
1619 k_work_submit_to_queue(&k_sys_work_q, work);
1620}
1621
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001622/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001623 * @brief Submit a delayed work item to the system workqueue.
1624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001625 * This routine schedules work item @a work to be processed by the system
1626 * workqueue after a delay of @a delay milliseconds. The routine initiates
1627 * an asychronous countdown for the work item and then returns to the caller.
1628 * Only when the countdown completes is the work item actually submitted to
1629 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001630 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001631 * Submitting a previously submitted delayed work item that is still
1632 * counting down cancels the existing submission and restarts the countdown
1633 * using the new delay. If the work item is currently pending on the
1634 * workqueue's queue because the countdown has completed it is too late to
1635 * resubmit the item, and resubmission fails without impacting the work item.
1636 * If the work item has already been processed, or is currently being processed,
1637 * its work is considered complete and the work item can be resubmitted.
1638 *
1639 * @warning
1640 * Work items submitted to the system workqueue should avoid using handlers
1641 * that block or yield since this may prevent the system workqueue from
1642 * processing other work items in a timely manner.
1643 *
1644 * @note Can be called by ISRs.
1645 *
1646 * @param work Address of delayed work item.
1647 * @param delay Delay before submitting the work item (in milliseconds).
1648 *
1649 * @retval 0 Work item countdown started.
1650 * @retval -EINPROGRESS Work item is already pending.
1651 * @retval -EINVAL Work item is being processed or has completed its work.
1652 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001653 */
1654static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001655 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001657 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658}
1659
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001660/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001661 * @brief Get time remaining before a delayed work gets scheduled.
1662 *
1663 * This routine computes the (approximate) time remaining before a
1664 * delayed work gets executed. If the delayed work is not waiting to be
1665 * schedules, it returns zero.
1666 *
1667 * @param work Delayed work item.
1668 *
1669 * @return Remaining time (in milliseconds).
1670 */
1671static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1672{
1673 return _timeout_remaining_get(&work->timeout);
1674}
1675
1676/**
Allan Stephensc98da842016-11-11 15:45:03 -05001677 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001678 */
1679
Allan Stephensc98da842016-11-11 15:45:03 -05001680/**
1681 * @cond INTERNAL_HIDDEN
1682 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001683
1684struct k_mutex {
1685 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001686 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001687 uint32_t lock_count;
1688 int owner_orig_prio;
1689#ifdef CONFIG_OBJECT_MONITOR
1690 int num_lock_state_changes;
1691 int num_conflicts;
1692#endif
1693
Anas Nashif2f203c22016-12-18 06:57:45 -05001694 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001695};
1696
1697#ifdef CONFIG_OBJECT_MONITOR
1698#define _MUTEX_INIT_OBJECT_MONITOR \
1699 .num_lock_state_changes = 0, .num_conflicts = 0,
1700#else
1701#define _MUTEX_INIT_OBJECT_MONITOR
1702#endif
1703
1704#define K_MUTEX_INITIALIZER(obj) \
1705 { \
1706 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1707 .owner = NULL, \
1708 .lock_count = 0, \
1709 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1710 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001711 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001712 }
1713
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714/**
Allan Stephensc98da842016-11-11 15:45:03 -05001715 * INTERNAL_HIDDEN @endcond
1716 */
1717
1718/**
1719 * @defgroup mutex_apis Mutex APIs
1720 * @ingroup kernel_apis
1721 * @{
1722 */
1723
1724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001727 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001729 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001733#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001734 struct k_mutex name \
1735 __in_section(_k_mutex, static, name) = \
1736 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * Upon completion, the mutex is available and does not have an owner.
1744 *
1745 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746 *
1747 * @return N/A
1748 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750
1751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * This routine locks @a mutex. If the mutex is locked by another thread,
1755 * the calling thread waits until the mutex becomes available or until
1756 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * A thread is permitted to lock a mutex it has already locked. The operation
1759 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * @param mutex Address of the mutex.
1762 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 * or one of the special values K_NO_WAIT and K_FOREVER.
1764 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001765 * @retval 0 Mutex locked.
1766 * @retval -EBUSY Returned without waiting.
1767 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001769extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770
1771/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * This routine unlocks @a mutex. The mutex must already be locked by the
1775 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776 *
1777 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 * thread.
1780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782 *
1783 * @return N/A
1784 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785extern void k_mutex_unlock(struct k_mutex *mutex);
1786
Allan Stephensc98da842016-11-11 15:45:03 -05001787/**
1788 * @} end defgroup mutex_apis
1789 */
1790
1791/**
1792 * @cond INTERNAL_HIDDEN
1793 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001794
1795struct k_sem {
1796 _wait_q_t wait_q;
1797 unsigned int count;
1798 unsigned int limit;
1799
Anas Nashif2f203c22016-12-18 06:57:45 -05001800 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001801};
1802
Allan Stephensc98da842016-11-11 15:45:03 -05001803#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1804 { \
1805 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1806 .count = initial_count, \
1807 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001808 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001809 }
1810
1811/**
1812 * INTERNAL_HIDDEN @endcond
1813 */
1814
1815/**
1816 * @defgroup semaphore_apis Semaphore APIs
1817 * @ingroup kernel_apis
1818 * @{
1819 */
1820
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001826 * @param sem Address of the semaphore.
1827 * @param initial_count Initial semaphore count.
1828 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001829 *
1830 * @return N/A
1831 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001832extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1833 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001834
1835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1841 *
1842 * @param sem Address of the semaphore.
1843 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001844 * or one of the special values K_NO_WAIT and K_FOREVER.
1845 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001846 * @note When porting code from the nanokernel legacy API to the new API, be
1847 * careful with the return value of this function. The return value is the
1848 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1849 * non-zero means failure, while the nano_sem_take family returns 1 for success
1850 * and 0 for failure.
1851 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001852 * @retval 0 Semaphore taken.
1853 * @retval -EBUSY Returned without waiting.
1854 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001855 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001856extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001857
1858/**
1859 * @brief Give a semaphore.
1860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001861 * This routine gives @a sem, unless the semaphore is already at its maximum
1862 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001866 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001867 *
1868 * @return N/A
1869 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870extern void k_sem_give(struct k_sem *sem);
1871
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001872/**
1873 * @brief Reset a semaphore's count to zero.
1874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001878 *
1879 * @return N/A
1880 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001881static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001882{
1883 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884}
1885
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001886/**
1887 * @brief Get a semaphore's count.
1888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001894 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001895static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896{
1897 return sem->count;
1898}
1899
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001901 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001904 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001905 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * @param name Name of the semaphore.
1908 * @param initial_count Initial semaphore count.
1909 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001910 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001912 struct k_sem name \
1913 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914 K_SEM_INITIALIZER(name, initial_count, count_limit)
1915
Allan Stephensc98da842016-11-11 15:45:03 -05001916/**
1917 * @} end defgroup semaphore_apis
1918 */
1919
1920/**
1921 * @defgroup alert_apis Alert APIs
1922 * @ingroup kernel_apis
1923 * @{
1924 */
1925
Allan Stephens5eceb852016-11-16 10:16:30 -05001926/**
1927 * @typedef k_alert_handler_t
1928 * @brief Alert handler function type.
1929 *
1930 * An alert's alert handler function is invoked by the system workqueue
1931 * when the alert is signalled. The alert handler function is optional,
1932 * and is only invoked if the alert has been initialized with one.
1933 *
1934 * @param alert Address of the alert.
1935 *
1936 * @return 0 if alert has been consumed; non-zero if alert should pend.
1937 */
1938typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001939
1940/**
1941 * @} end defgroup alert_apis
1942 */
1943
1944/**
1945 * @cond INTERNAL_HIDDEN
1946 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001947
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001948#define K_ALERT_DEFAULT NULL
1949#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001951struct k_alert {
1952 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001953 atomic_t send_count;
1954 struct k_work work_item;
1955 struct k_sem sem;
1956
Anas Nashif2f203c22016-12-18 06:57:45 -05001957 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001958};
1959
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001960extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001962#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001964 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001966 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001967 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001968 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969 }
1970
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971/**
Allan Stephensc98da842016-11-11 15:45:03 -05001972 * INTERNAL_HIDDEN @endcond
1973 */
1974
1975/**
1976 * @addtogroup alert_apis
1977 * @{
1978 */
1979
1980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001981 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001983 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001985 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * @param name Name of the alert.
1988 * @param alert_handler Action to take when alert is sent. Specify either
1989 * the address of a function to be invoked by the system workqueue
1990 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1991 * K_ALERT_DEFAULT (which causes the alert to pend).
1992 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001993 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001994#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001995 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001996 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001997 K_ALERT_INITIALIZER(name, alert_handler, \
1998 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002005 * @param alert Address of the alert.
2006 * @param handler Action to take when alert is sent. Specify either the address
2007 * of a function to be invoked by the system workqueue thread,
2008 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2009 * K_ALERT_DEFAULT (which causes the alert to pend).
2010 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
2012 * @return N/A
2013 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002014extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2015 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016
2017/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002018 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2023 *
2024 * @param alert Address of the alert.
2025 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026 * or one of the special values K_NO_WAIT and K_FOREVER.
2027 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002028 * @retval 0 Alert received.
2029 * @retval -EBUSY Returned without waiting.
2030 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002032extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033
2034/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * This routine signals @a alert. The action specified for @a alert will
2038 * be taken, which may trigger the execution of an alert handler function
2039 * and/or cause the alert to pend (assuming the alert has not reached its
2040 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @note Can be called by ISRs.
2043 *
2044 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045 *
2046 * @return N/A
2047 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002048extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049
2050/**
Allan Stephensc98da842016-11-11 15:45:03 -05002051 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002052 */
2053
Allan Stephensc98da842016-11-11 15:45:03 -05002054/**
2055 * @cond INTERNAL_HIDDEN
2056 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057
2058struct k_msgq {
2059 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002060 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061 uint32_t max_msgs;
2062 char *buffer_start;
2063 char *buffer_end;
2064 char *read_ptr;
2065 char *write_ptr;
2066 uint32_t used_msgs;
2067
Anas Nashif2f203c22016-12-18 06:57:45 -05002068 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069};
2070
Peter Mitsis1da807e2016-10-06 11:36:59 -04002071#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072 { \
2073 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002074 .max_msgs = q_max_msgs, \
2075 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002077 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078 .read_ptr = q_buffer, \
2079 .write_ptr = q_buffer, \
2080 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002081 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002082 }
2083
Peter Mitsis1da807e2016-10-06 11:36:59 -04002084/**
Allan Stephensc98da842016-11-11 15:45:03 -05002085 * INTERNAL_HIDDEN @endcond
2086 */
2087
2088/**
2089 * @defgroup msgq_apis Message Queue APIs
2090 * @ingroup kernel_apis
2091 * @{
2092 */
2093
2094/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002097 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2098 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002099 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2100 * message is similarly aligned to this boundary, @a q_msg_size must also be
2101 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * The message queue can be accessed outside the module where it is defined
2104 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002106 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * @param q_name Name of the message queue.
2109 * @param q_msg_size Message size (in bytes).
2110 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002111 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002112 */
2113#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2114 static char __noinit __aligned(q_align) \
2115 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002116 struct k_msgq q_name \
2117 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002118 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2119 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120
Peter Mitsisd7a37502016-10-13 11:37:40 -04002121/**
2122 * @brief Initialize a message queue.
2123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * This routine initializes a message queue object, prior to its first use.
2125 *
Allan Stephensda827222016-11-09 14:23:58 -06002126 * The message queue's ring buffer must contain space for @a max_msgs messages,
2127 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2128 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2129 * that each message is similarly aligned to this boundary, @a q_msg_size
2130 * must also be a multiple of N.
2131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @param q Address of the message queue.
2133 * @param buffer Pointer to ring buffer that holds queued messages.
2134 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002135 * @param max_msgs Maximum number of messages that can be queued.
2136 *
2137 * @return N/A
2138 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002139extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002140 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002141
2142/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002143 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002146 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002147 * @note Can be called by ISRs.
2148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * @param q Address of the message queue.
2150 * @param data Pointer to the message.
2151 * @param timeout Waiting period to add 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 sent.
2155 * @retval -ENOMSG Returned without waiting or queue purged.
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_put(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 Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * This routine receives a message from message queue @a q in a "first in,
2164 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002165 *
Allan Stephensc98da842016-11-11 15:45:03 -05002166 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * @param q Address of the message queue.
2169 * @param data Address of area to hold the received message.
2170 * @param timeout Waiting period to receive the message (in milliseconds),
2171 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002172 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002173 * @retval 0 Message received.
2174 * @retval -ENOMSG Returned without waiting.
2175 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002178
2179/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182 * This routine discards all unreceived messages in a message queue's ring
2183 * buffer. Any threads that are blocked waiting to send a message to the
2184 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002187 *
2188 * @return N/A
2189 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190extern void k_msgq_purge(struct k_msgq *q);
2191
Peter Mitsis67be2492016-10-07 11:44:34 -04002192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002193 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * This routine returns the number of unused entries in a message queue's
2196 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002198 * @param q Address of the message queue.
2199 *
2200 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002201 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002202static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002203{
2204 return q->max_msgs - q->used_msgs;
2205}
2206
Peter Mitsisd7a37502016-10-13 11:37:40 -04002207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002208 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002210 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002212 * @param q Address of the message queue.
2213 *
2214 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002215 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002216static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217{
2218 return q->used_msgs;
2219}
2220
Allan Stephensc98da842016-11-11 15:45:03 -05002221/**
2222 * @} end defgroup msgq_apis
2223 */
2224
2225/**
2226 * @defgroup mem_pool_apis Memory Pool APIs
2227 * @ingroup kernel_apis
2228 * @{
2229 */
2230
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002232 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233 void *addr_in_pool;
2234 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002235 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236};
2237
Allan Stephensc98da842016-11-11 15:45:03 -05002238/**
2239 * @} end defgroup mem_pool_apis
2240 */
2241
2242/**
2243 * @defgroup mailbox_apis Mailbox APIs
2244 * @ingroup kernel_apis
2245 * @{
2246 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247
2248struct k_mbox_msg {
2249 /** internal use only - needed for legacy API support */
2250 uint32_t _mailbox;
2251 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002252 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253 /** application-defined information value */
2254 uint32_t info;
2255 /** sender's message data buffer */
2256 void *tx_data;
2257 /** internal use only - needed for legacy API support */
2258 void *_rx_data;
2259 /** message data block descriptor */
2260 struct k_mem_block tx_block;
2261 /** source thread id */
2262 k_tid_t rx_source_thread;
2263 /** target thread id */
2264 k_tid_t tx_target_thread;
2265 /** internal use only - thread waiting on send (may be a dummy) */
2266 k_tid_t _syncing_thread;
2267#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2268 /** internal use only - semaphore used during asynchronous send */
2269 struct k_sem *_async_sem;
2270#endif
2271};
2272
Allan Stephensc98da842016-11-11 15:45:03 -05002273/**
2274 * @cond INTERNAL_HIDDEN
2275 */
2276
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277struct k_mbox {
2278 _wait_q_t tx_msg_queue;
2279 _wait_q_t rx_msg_queue;
2280
Anas Nashif2f203c22016-12-18 06:57:45 -05002281 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282};
2283
2284#define K_MBOX_INITIALIZER(obj) \
2285 { \
2286 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2287 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002288 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289 }
2290
Peter Mitsis12092702016-10-14 12:57:23 -04002291/**
Allan Stephensc98da842016-11-11 15:45:03 -05002292 * INTERNAL_HIDDEN @endcond
2293 */
2294
2295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002296 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002298 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002300 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002302 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002303 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002305 struct k_mbox name \
2306 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 K_MBOX_INITIALIZER(name) \
2308
Peter Mitsis12092702016-10-14 12:57:23 -04002309/**
2310 * @brief Initialize a mailbox.
2311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * This routine initializes a mailbox object, prior to its first use.
2313 *
2314 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002315 *
2316 * @return N/A
2317 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318extern void k_mbox_init(struct k_mbox *mbox);
2319
Peter Mitsis12092702016-10-14 12:57:23 -04002320/**
2321 * @brief Send a mailbox message in a synchronous manner.
2322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * This routine sends a message to @a mbox and waits for a receiver to both
2324 * receive and process it. The message data may be in a buffer, in a memory
2325 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * @param mbox Address of the mailbox.
2328 * @param tx_msg Address of the transmit message descriptor.
2329 * @param timeout Waiting period for the message to be received (in
2330 * milliseconds), or one of the special values K_NO_WAIT
2331 * and K_FOREVER. Once the message has been received,
2332 * this routine waits as long as necessary for the message
2333 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002334 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002335 * @retval 0 Message sent.
2336 * @retval -ENOMSG Returned without waiting.
2337 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002338 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002339extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002341
Peter Mitsis12092702016-10-14 12:57:23 -04002342/**
2343 * @brief Send a mailbox message in an asynchronous manner.
2344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 * This routine sends a message to @a mbox without waiting for a receiver
2346 * to process it. The message data may be in a buffer, in a memory pool block,
2347 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2348 * will be given when the message has been both received and completely
2349 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * @param mbox Address of the mailbox.
2352 * @param tx_msg Address of the transmit message descriptor.
2353 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002354 *
2355 * @return N/A
2356 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002357extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358 struct k_sem *sem);
2359
Peter Mitsis12092702016-10-14 12:57:23 -04002360/**
2361 * @brief Receive a mailbox message.
2362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002363 * This routine receives a message from @a mbox, then optionally retrieves
2364 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * @param mbox Address of the mailbox.
2367 * @param rx_msg Address of the receive message descriptor.
2368 * @param buffer Address of the buffer to receive data, or NULL to defer data
2369 * retrieval and message disposal until later.
2370 * @param timeout Waiting period for a message to be received (in
2371 * milliseconds), or one of the special values K_NO_WAIT
2372 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002373 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002374 * @retval 0 Message received.
2375 * @retval -ENOMSG Returned without waiting.
2376 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002377 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002378extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002380
2381/**
2382 * @brief Retrieve mailbox message data into a buffer.
2383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * This routine completes the processing of a received message by retrieving
2385 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002386 *
2387 * Alternatively, this routine can be used to dispose of a received message
2388 * without retrieving its data.
2389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002390 * @param rx_msg Address of the receive message descriptor.
2391 * @param buffer Address of the buffer to receive data, or NULL to discard
2392 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002393 *
2394 * @return N/A
2395 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002396extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002397
2398/**
2399 * @brief Retrieve mailbox message data into a memory pool block.
2400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * This routine completes the processing of a received message by retrieving
2402 * its data into a memory pool block, then disposing of the message.
2403 * The memory pool block that results from successful retrieval must be
2404 * returned to the pool once the data has been processed, even in cases
2405 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002406 *
2407 * Alternatively, this routine can be used to dispose of a received message
2408 * without retrieving its data. In this case there is no need to return a
2409 * memory pool block to the pool.
2410 *
2411 * This routine allocates a new memory pool block for the data only if the
2412 * data is not already in one. If a new block cannot be allocated, the routine
2413 * returns a failure code and the received message is left unchanged. This
2414 * permits the caller to reattempt data retrieval at a later time or to dispose
2415 * of the received message without retrieving its data.
2416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 * @param rx_msg Address of a receive message descriptor.
2418 * @param pool Address of memory pool, or NULL to discard data.
2419 * @param block Address of the area to hold memory pool block info.
2420 * @param timeout Waiting period to wait for a memory pool block (in
2421 * milliseconds), or one of the special values K_NO_WAIT
2422 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002423 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002424 * @retval 0 Data retrieved.
2425 * @retval -ENOMEM Returned without waiting.
2426 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002427 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002428extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002429 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430 struct k_mem_block *block, int32_t timeout);
2431
Allan Stephensc98da842016-11-11 15:45:03 -05002432/**
2433 * @} end defgroup mailbox_apis
2434 */
2435
2436/**
2437 * @cond INTERNAL_HIDDEN
2438 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439
2440struct k_pipe {
2441 unsigned char *buffer; /* Pipe buffer: may be NULL */
2442 size_t size; /* Buffer size */
2443 size_t bytes_used; /* # bytes used in buffer */
2444 size_t read_index; /* Where in buffer to read from */
2445 size_t write_index; /* Where in buffer to write */
2446
2447 struct {
2448 _wait_q_t readers; /* Reader wait queue */
2449 _wait_q_t writers; /* Writer wait queue */
2450 } wait_q;
2451
Anas Nashif2f203c22016-12-18 06:57:45 -05002452 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453};
2454
Peter Mitsise5d9c582016-10-14 14:44:57 -04002455#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 { \
2457 .buffer = pipe_buffer, \
2458 .size = pipe_buffer_size, \
2459 .bytes_used = 0, \
2460 .read_index = 0, \
2461 .write_index = 0, \
2462 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2463 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002464 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465 }
2466
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467/**
Allan Stephensc98da842016-11-11 15:45:03 -05002468 * INTERNAL_HIDDEN @endcond
2469 */
2470
2471/**
2472 * @defgroup pipe_apis Pipe APIs
2473 * @ingroup kernel_apis
2474 * @{
2475 */
2476
2477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002482 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * @param name Name of the pipe.
2485 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2486 * or zero if no ring buffer is used.
2487 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002489#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2490 static unsigned char __noinit __aligned(pipe_align) \
2491 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002492 struct k_pipe name \
2493 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002494 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * This routine initializes a pipe object, prior to its first use.
2500 *
2501 * @param pipe Address of the pipe.
2502 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2503 * is used.
2504 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2505 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 *
2507 * @return N/A
2508 */
2509extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2510 size_t size);
2511
2512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @param pipe Address of the pipe.
2518 * @param data Address of data to write.
2519 * @param bytes_to_write Size of data (in bytes).
2520 * @param bytes_written Address of area to hold the number of bytes written.
2521 * @param min_xfer Minimum number of bytes to write.
2522 * @param timeout Waiting period to wait for the data to be written (in
2523 * milliseconds), or one of the special values K_NO_WAIT
2524 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002526 * @retval 0 At least @a min_xfer bytes of data were written.
2527 * @retval -EIO Returned without waiting; zero data bytes were written.
2528 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002531extern int k_pipe_put(struct k_pipe *pipe, void *data,
2532 size_t bytes_to_write, size_t *bytes_written,
2533 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534
2535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @param pipe Address of the pipe.
2541 * @param data Address to place the data read from pipe.
2542 * @param bytes_to_read Maximum number of data bytes to read.
2543 * @param bytes_read Address of area to hold the number of bytes read.
2544 * @param min_xfer Minimum number of data bytes to read.
2545 * @param timeout Waiting period to wait for the data to be read (in
2546 * milliseconds), or one of the special values K_NO_WAIT
2547 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002549 * @retval 0 At least @a min_xfer bytes of data were read.
2550 * @retval -EIO Returned without waiting; zero data bytes were read.
2551 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002552 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002554extern int k_pipe_get(struct k_pipe *pipe, void *data,
2555 size_t bytes_to_read, size_t *bytes_read,
2556 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557
2558/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * This routine writes the data contained in a memory block to @a pipe.
2562 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002565 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566 * @param block Memory block containing data to send
2567 * @param size Number of data bytes in memory block to send
2568 * @param sem Semaphore to signal upon completion (else NULL)
2569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002571 */
2572extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2573 size_t size, struct k_sem *sem);
2574
2575/**
Allan Stephensc98da842016-11-11 15:45:03 -05002576 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577 */
2578
Allan Stephensc98da842016-11-11 15:45:03 -05002579/**
2580 * @cond INTERNAL_HIDDEN
2581 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002583struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002585 uint32_t num_blocks;
2586 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587 char *buffer;
2588 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002589 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590
Anas Nashif2f203c22016-12-18 06:57:45 -05002591 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592};
2593
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002594#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2595 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596 { \
2597 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002598 .num_blocks = slab_num_blocks, \
2599 .block_size = slab_block_size, \
2600 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601 .free_list = NULL, \
2602 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002603 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 }
2605
Peter Mitsis578f9112016-10-07 13:50:31 -04002606/**
Allan Stephensc98da842016-11-11 15:45:03 -05002607 * INTERNAL_HIDDEN @endcond
2608 */
2609
2610/**
2611 * @defgroup mem_slab_apis Memory Slab APIs
2612 * @ingroup kernel_apis
2613 * @{
2614 */
2615
2616/**
Allan Stephensda827222016-11-09 14:23:58 -06002617 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002618 *
Allan Stephensda827222016-11-09 14:23:58 -06002619 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002621 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2622 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002623 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002624 *
Allan Stephensda827222016-11-09 14:23:58 -06002625 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002627 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002628 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * @param name Name of the memory slab.
2631 * @param slab_block_size Size of each memory block (in bytes).
2632 * @param slab_num_blocks Number memory blocks.
2633 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002634 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002635#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2636 char __noinit __aligned(slab_align) \
2637 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2638 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002639 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002640 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2641 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002643/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002644 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002647 *
Allan Stephensda827222016-11-09 14:23:58 -06002648 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2649 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2650 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2651 * To ensure that each memory block is similarly aligned to this boundary,
2652 * @a slab_block_size must also be a multiple of N.
2653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * @param slab Address of the memory slab.
2655 * @param buffer Pointer to buffer used for the memory blocks.
2656 * @param block_size Size of each memory block (in bytes).
2657 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002658 *
2659 * @return N/A
2660 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002661extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002662 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002663
2664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * @param slab Address of the memory slab.
2670 * @param mem Pointer to block address area.
2671 * @param timeout Maximum time to wait for operation to complete
2672 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2673 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002674 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002675 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002677 * @retval -ENOMEM Returned without waiting.
2678 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002679 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002680extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2681 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002682
2683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * This routine releases a previously allocated memory block back to its
2687 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * @param slab Address of the memory slab.
2690 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002691 *
2692 * @return N/A
2693 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002694extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002696/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * This routine gets the number of memory blocks that are currently
2700 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002705 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002706static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002708 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709}
2710
Peter Mitsisc001aa82016-10-13 13:53:37 -04002711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002712 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * This routine gets the number of memory blocks that are currently
2715 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002720 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002721static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002722{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002723 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002724}
2725
Allan Stephensc98da842016-11-11 15:45:03 -05002726/**
2727 * @} end defgroup mem_slab_apis
2728 */
2729
2730/**
2731 * @cond INTERNAL_HIDDEN
2732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002734/*
2735 * Memory pool requires a buffer and two arrays of structures for the
2736 * memory block accounting:
2737 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2738 * status of four blocks of memory.
2739 */
2740struct k_mem_pool_quad_block {
2741 char *mem_blocks; /* pointer to the first of four memory blocks */
2742 uint32_t mem_status; /* four bits. If bit is set, memory block is
2743 allocated */
2744};
2745/*
2746 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2747 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2748 * block size is 4 times less than the previous one and thus requires 4 times
2749 * bigger array of k_mem_pool_quad_block structures to keep track of the
2750 * memory blocks.
2751 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002753/*
2754 * The array of k_mem_pool_block_set keeps the information of each array of
2755 * k_mem_pool_quad_block structures
2756 */
2757struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002758 size_t block_size; /* memory block size */
2759 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002760 struct k_mem_pool_quad_block *quad_block;
2761 int count;
2762};
2763
2764/* Memory pool descriptor */
2765struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002766 size_t max_block_size;
2767 size_t min_block_size;
2768 uint32_t nr_of_maxblocks;
2769 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002770 struct k_mem_pool_block_set *block_set;
2771 char *bufblock;
2772 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002773 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002774};
2775
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002776#ifdef CONFIG_ARM
2777#define _SECTION_TYPE_SIGN "%"
2778#else
2779#define _SECTION_TYPE_SIGN "@"
2780#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002782/*
2783 * Static memory pool initialization
2784 */
Allan Stephensc98da842016-11-11 15:45:03 -05002785
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002786/*
2787 * Use .altmacro to be able to recalculate values and pass them as string
2788 * arguments when calling assembler macros resursively
2789 */
2790__asm__(".altmacro\n\t");
2791
2792/*
2793 * Recursively calls a macro
2794 * The followig global symbols need to be initialized:
2795 * __memory_pool_max_block_size - maximal size of the memory block
2796 * __memory_pool_min_block_size - minimal size of the memory block
2797 * Notes:
2798 * Global symbols are used due the fact that assembler macro allows only
2799 * one argument be passed with the % conversion
2800 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2801 * is used instead of / 4.
2802 * n_max argument needs to go first in the invoked macro, as some
2803 * assemblers concatenate \name and %(\n_max * 4) arguments
2804 * if \name goes first
2805 */
2806__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2807 ".ifge __memory_pool_max_block_size >> 2 -"
2808 " __memory_pool_min_block_size\n\t\t"
2809 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2810 "\\macro_name %(\\n_max * 4) \\name\n\t"
2811 ".endif\n\t"
2812 ".endm\n");
2813
2814/*
2815 * Build quad blocks
2816 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2817 * structures and recursively calls itself for the next array, 4 times
2818 * larger.
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_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2823 */
2824__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002825 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002826 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2827 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2828 ".if \\n_max % 4\n\t\t"
2829 ".skip __memory_pool_quad_block_size\n\t"
2830 ".endif\n\t"
2831 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2832 ".endm\n");
2833
2834/*
2835 * Build block sets and initialize them
2836 * Macro initializes the k_mem_pool_block_set structure and
2837 * recursively calls itself for the next one.
2838 * The followig global symbols need to be initialized:
2839 * __memory_pool_max_block_size - maximal size of the memory block
2840 * __memory_pool_min_block_size - minimal size of the memory block
2841 * __memory_pool_block_set_count, the number of the elements in the
2842 * block set array must be set to 0. Macro calculates it's real
2843 * value.
2844 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2845 * structures, _build_quad_blocks must be called prior it.
2846 */
2847__asm__(".macro _build_block_set n_max, name\n\t"
2848 ".int __memory_pool_max_block_size\n\t" /* block_size */
2849 ".if \\n_max % 4\n\t\t"
2850 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2851 ".else\n\t\t"
2852 ".int \\n_max >> 2\n\t"
2853 ".endif\n\t"
2854 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2855 ".int 0\n\t" /* count */
2856 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2857 "__do_recurse _build_block_set \\name \\n_max\n\t"
2858 ".endm\n");
2859
2860/*
2861 * Build a memory pool structure and initialize it
2862 * Macro uses __memory_pool_block_set_count global symbol,
2863 * block set addresses and buffer address, it may be called only after
2864 * _build_block_set
2865 */
2866__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002867 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002868 _SECTION_TYPE_SIGN "progbits\n\t"
2869 ".globl \\name\n\t"
2870 "\\name:\n\t"
2871 ".int \\max_size\n\t" /* max_block_size */
2872 ".int \\min_size\n\t" /* min_block_size */
2873 ".int \\n_max\n\t" /* nr_of_maxblocks */
2874 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2875 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2876 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2877 ".int 0\n\t" /* wait_q->head */
2878 ".int 0\n\t" /* wait_q->next */
2879 ".popsection\n\t"
2880 ".endm\n");
2881
2882#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2883 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2884 _SECTION_TYPE_SIGN "progbits\n\t"); \
2885 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2886 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2887 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2888 STRINGIFY(name) "\n\t"); \
2889 __asm__(".popsection\n\t")
2890
2891#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2892 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2893 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2894 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2895 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002896 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002897 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2898 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2899 STRINGIFY(name) "\n\t"); \
2900 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2901 __asm__(".int __memory_pool_block_set_count\n\t"); \
2902 __asm__(".popsection\n\t"); \
2903 extern uint32_t _mem_pool_block_set_count_##name; \
2904 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2905
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002906#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2907 char __noinit __aligned(align) \
2908 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002909
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002910/*
2911 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2912 * to __memory_pool_quad_block_size absolute symbol.
2913 * This function does not get called, but compiler calculates the value and
2914 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2915 */
2916static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2917{
2918 __asm__(".globl __memory_pool_quad_block_size\n\t"
2919#ifdef CONFIG_NIOS2
2920 "__memory_pool_quad_block_size = %0\n\t"
2921#else
2922 "__memory_pool_quad_block_size = %c0\n\t"
2923#endif
2924 :
2925 : "n"(sizeof(struct k_mem_pool_quad_block)));
2926}
2927
2928/**
Allan Stephensc98da842016-11-11 15:45:03 -05002929 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002930 */
2931
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002932/**
Allan Stephensc98da842016-11-11 15:45:03 -05002933 * @addtogroup mem_pool_apis
2934 * @{
2935 */
2936
2937/**
2938 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2941 * long. The memory pool allows blocks to be repeatedly partitioned into
2942 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2943 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002944 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002946 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002947 * If the pool is to be accessed outside the module where it is defined, it
2948 * can be declared via
2949 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002950 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @param name Name of the memory pool.
2953 * @param min_size Size of the smallest blocks in the pool (in bytes).
2954 * @param max_size Size of the largest blocks in the pool (in bytes).
2955 * @param n_max Number of maximum sized blocks in the pool.
2956 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002957 */
2958#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002959 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2960 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002961 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002962 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2963 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2964 extern struct k_mem_pool name
2965
Peter Mitsis937042c2016-10-13 13:18:26 -04002966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @param pool Address of the memory pool.
2972 * @param block Pointer to block descriptor for the allocated memory.
2973 * @param size Amount of memory to allocate (in bytes).
2974 * @param timeout Maximum time to wait for operation to complete
2975 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2976 * or K_FOREVER to wait as long as necessary.
2977 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002978 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002980 * @retval -ENOMEM Returned without waiting.
2981 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002982 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002983extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002984 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002985
2986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * This routine releases a previously allocated memory block back to its
2990 * memory pool.
2991 *
2992 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002993 *
2994 * @return N/A
2995 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002997
2998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * This routine instructs a memory pool to concatenate unused memory blocks
3002 * into larger blocks wherever possible. Manually defragmenting the memory
3003 * pool may speed up future allocations of memory blocks by eliminating the
3004 * need for the memory pool to perform an automatic partial defragmentation.
3005 *
3006 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003007 *
3008 * @return N/A
3009 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003010extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003011
3012/**
Allan Stephensc98da842016-11-11 15:45:03 -05003013 * @} end addtogroup mem_pool_apis
3014 */
3015
3016/**
3017 * @defgroup heap_apis Heap Memory Pool APIs
3018 * @ingroup kernel_apis
3019 * @{
3020 */
3021
3022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003026 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003031 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003032extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003033
3034/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003036 *
3037 * This routine provides traditional free() semantics. The memory being
3038 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003039 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003040 * If @a ptr is NULL, no operation is performed.
3041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003042 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003043 *
3044 * @return N/A
3045 */
3046extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047
Allan Stephensc98da842016-11-11 15:45:03 -05003048/**
3049 * @} end defgroup heap_apis
3050 */
3051
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003052/**
3053 * @brief Make the CPU idle.
3054 *
3055 * This function makes the CPU idle until an event wakes it up.
3056 *
3057 * In a regular system, the idle thread should be the only thread responsible
3058 * for making the CPU idle and triggering any type of power management.
3059 * However, in some more constrained systems, such as a single-threaded system,
3060 * the only thread would be responsible for this if needed.
3061 *
3062 * @return N/A
3063 */
3064extern void k_cpu_idle(void);
3065
3066/**
3067 * @brief Make the CPU idle in an atomic fashion.
3068 *
3069 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3070 * must be done atomically before making the CPU idle.
3071 *
3072 * @param key Interrupt locking key obtained from irq_lock().
3073 *
3074 * @return N/A
3075 */
3076extern void k_cpu_atomic_idle(unsigned int key);
3077
Anas Nashifa6149502017-01-17 07:47:31 -05003078/* Include legacy APIs */
3079#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003081#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082#include <arch/cpu.h>
3083
3084/*
3085 * private APIs that are utilized by one or more public APIs
3086 */
3087
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003088#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003090#else
3091#define _init_static_threads() do { } while ((0))
3092#endif
3093
3094extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003095extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096
3097#ifdef __cplusplus
3098}
3099#endif
3100
Andrew Boiee004dec2016-11-07 09:01:19 -08003101#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3102/*
3103 * Define new and delete operators.
3104 * At this moment, the operators do nothing since objects are supposed
3105 * to be statically allocated.
3106 */
3107inline void operator delete(void *ptr)
3108{
3109 (void)ptr;
3110}
3111
3112inline void operator delete[](void *ptr)
3113{
3114 (void)ptr;
3115}
3116
3117inline void *operator new(size_t size)
3118{
3119 (void)size;
3120 return NULL;
3121}
3122
3123inline void *operator new[](size_t size)
3124{
3125 (void)size;
3126 return NULL;
3127}
3128
3129/* Placement versions of operator new and delete */
3130inline void operator delete(void *ptr1, void *ptr2)
3131{
3132 (void)ptr1;
3133 (void)ptr2;
3134}
3135
3136inline void operator delete[](void *ptr1, void *ptr2)
3137{
3138 (void)ptr1;
3139 (void)ptr2;
3140}
3141
3142inline void *operator new(size_t size, void *ptr)
3143{
3144 (void)size;
3145 return ptr;
3146}
3147
3148inline void *operator new[](size_t size, void *ptr)
3149{
3150 (void)size;
3151 return ptr;
3152}
3153
3154#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3155
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156#endif /* _kernel__h_ */