blob: 9123e7c51b27972fa0b10b4e83c91989be1938c2 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
49#define K_FOREVER (-1)
50#define K_NO_WAIT 0
51
52#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
Benjamin Walshfab8d922016-11-08 15:36:36 -050067#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
74#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
75#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
77#else
78#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
79#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
80#endif
81
82#define k_thread tcs
83struct tcs;
84struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
99/* threads/scheduler/execution contexts */
100
101enum execution_context_types {
102 K_ISR = 0,
103 K_COOP_THREAD,
104 K_PREEMPT_THREAD,
105};
106
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400108
109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500110 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500112 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500114 * The new thread may be scheduled for immediate execution or a delayed start.
115 * If the newly spawned thread does not have a delayed start the kernel
116 * scheduler may preempt the current thread to allow the new thread to
117 * execute.
118 *
119 * Thread options are architecture-specific, and can include K_ESSENTIAL,
120 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
121 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400122 *
123 * @param stack Pointer to the stack space.
124 * @param stack_size Stack size in bytes.
125 * @param entry Thread entry function.
126 * @param p1 1st entry point parameter.
127 * @param p2 2nd entry point parameter.
128 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500129 * @param prio Thread priority.
130 * @param options Thread options.
131 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500133 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400135extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
136 void (*entry)(void *, void *, void*),
137 void *p1, void *p2, void *p3,
138 int32_t prio, uint32_t options, int32_t delay);
139
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500141 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500143 * This routine puts the currently thread to sleep for @a duration
144 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500146 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400147 *
148 * @return N/A
149 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400150extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400151
152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500153 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 *
155 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500156 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400157 *
158 * @warning This routine utilizes the system clock, so it must not be invoked
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500159 * until the system clock is operational or while interrupts are locked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400160 *
161 * @return N/A
162 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400163extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400164
165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500168 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400169 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500170 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400171 *
172 * @return N/A
173 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400174extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400175
176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500177 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500179 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500181 * If @a thread is not currently sleeping, the routine has no effect.
182 *
183 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 *
185 * @return N/A
186 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400187extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188
189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500190 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500192 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400193 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400194extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195
196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500197 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500199 * This routine prevents @a thread from executing if it has not yet started
200 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500202 * @param thread ID of thread to cancel.
203 *
204 * @retval 0 if successful.
205 * @retval -EINVAL if the thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400207extern int k_thread_cancel(k_tid_t thread);
208
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * @brief Abort thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500212 * This routine permanently stops execution of @a thread. The thread is taken
213 * off all kernel queues it is part of (i.e. the ready queue, the timeout
214 * queue, or a kernel object wait queue). However, any kernel resources the
215 * thread might currently own (such as mutexes or memory blocks) are not
216 * released. It is the responsibility of the caller of this routine to ensure
217 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
221 * @return N/A
222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400223extern void k_thread_abort(k_tid_t thread);
224
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400225#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226#define _THREAD_TIMEOUT_INIT(obj) \
227 (obj).nano_timeout = { \
228 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400229 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400230 .wait_q = NULL, \
231 .delta_ticks_from_prev = -1, \
232 },
233#else
234#define _THREAD_TIMEOUT_INIT(obj)
235#endif
236
237#ifdef CONFIG_ERRNO
238#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
239#else
240#define _THREAD_ERRNO_INIT(obj)
241#endif
242
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400243struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400244 union {
245 char *init_stack;
246 struct k_thread *thread;
247 };
248 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500249 void (*init_entry)(void *, void *, void *);
250 void *init_p1;
251 void *init_p2;
252 void *init_p3;
253 int init_prio;
254 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400255 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500256 void (*init_abort)(void);
257 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258};
259
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400260#define _THREAD_INITIALIZER(stack, stack_size, \
261 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500262 prio, options, delay, abort, groups) \
263 { \
264 .init_stack = (stack), \
265 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400266 .init_entry = (void (*)(void *, void *, void *))entry, \
267 .init_p1 = (void *)p1, \
268 .init_p2 = (void *)p2, \
269 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500270 .init_prio = (prio), \
271 .init_options = (options), \
272 .init_delay = (delay), \
273 .init_abort = (abort), \
274 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400275 }
276
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400277/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500278 * @brief Statically define and initialize a thread.
279 *
280 * The thread may be scheduled for immediate execution or a delayed start.
281 *
282 * Thread options are architecture-specific, and can include K_ESSENTIAL,
283 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
284 * them using "|" (the logical OR operator).
285 *
286 * The ID of the thread can be accessed using:
287 *
288 * extern const k_tid_t @a name;
289 *
290 * @param name Name of the thread.
291 * @param stack_size Stack size in bytes.
292 * @param entry Thread entry function.
293 * @param p1 1st entry point parameter.
294 * @param p2 2nd entry point parameter.
295 * @param p3 3rd entry point parameter.
296 * @param prio Thread priority.
297 * @param options Thread options.
298 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400299 *
300 * @internal It has been observed that the x86 compiler by default aligns
301 * these _static_thread_data structures to 32-byte boundaries, thereby
302 * wasting space. To work around this, force a 4-byte alignment.
303 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500304#define K_THREAD_DEFINE(name, stack_size, \
305 entry, p1, p2, p3, \
306 prio, options, delay) \
307 char __noinit __stack _k_thread_obj_##name[stack_size]; \
308 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500309 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500310 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
311 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500312 NULL, 0); \
313 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400314
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500316 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500318 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500320 * @param thread ID of thread whose priority is needed.
321 *
322 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400323 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500324extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400325
326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500327 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500329 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400330 *
331 * Rescheduling can occur immediately depending on the priority @a thread is
332 * set to:
333 *
334 * - If its priority is raised above the priority of the caller of this
335 * function, and the caller is preemptible, @a thread will be scheduled in.
336 *
337 * - If the caller operates on itself, it lowers its priority below that of
338 * other threads in the system, and the caller is preemptible, the thread of
339 * highest priority will be scheduled in.
340 *
341 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
342 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
343 * highest priority.
344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500345 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400346 * @param prio New priority.
347 *
348 * @warning Changing the priority of a thread currently involved in mutex
349 * priority inheritance may result in undefined behavior.
350 *
351 * @return N/A
352 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400353extern void k_thread_priority_set(k_tid_t thread, int prio);
354
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500356 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500358 * This routine prevents the kernel scheduler from making @a thread the
359 * current thread. All other internal operations on @a thread are still
360 * performed; for example, any timeout it is waiting on keeps ticking,
361 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500363 * If @a thread is already suspended, the routine has no effect.
364 *
365 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400366 *
367 * @return N/A
368 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400369extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400370
371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500372 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500374 * This routine allows the kernel scheduler to make @a thread the current
375 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500377 * If @a thread is not currently suspended, the routine has no effect.
378 *
379 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380 *
381 * @return N/A
382 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400383extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500386 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * This routine specifies how the scheduler will perform time slicing of
389 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500391 * To enable time slicing, @a slice must be non-zero. The scheduler
392 * ensures that no thread runs for more than the specified time limit
393 * before other threads of that priority are given a chance to execute.
394 * Any thread whose priority is higher than @a prio is exempted, and may
395 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 * execute. Once the scheduler selects a thread for execution, there is no
399 * minimum guaranteed time the thread will execute before threads of greater or
400 * equal priority are scheduled.
401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403 * for execution, this routine has no effect; the thread is immediately
404 * rescheduled after the slice period expires.
405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * To disable timeslicing, set both @a slice and @a prio to zero.
407 *
408 * @param slice Maximum time slice length (in milliseconds).
409 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410 *
411 * @return N/A
412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400413extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400414
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500416 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500418 * @return 0 if invoked by a thread.
419 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500421extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400422
Benjamin Walsh445830d2016-11-10 15:54:27 -0500423/**
424 * @brief Determine if code is running in a preemptible thread.
425 *
426 * Returns a 'true' value if these conditions are all met:
427 *
428 * - the code is not running in an ISR
429 * - the thread's priority is in the preemptible range
430 * - the thread has not locked the scheduler
431 *
432 * @return 0 if invoked by either an ISR or a cooperative thread.
433 * @return Non-zero if invoked by a preemptible thread.
434 */
435extern int k_is_preempt_thread(void);
436
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500437/*
438 * @brief Lock the scheduler
439 *
440 * Prevent another thread from preempting the current thread.
441 *
442 * @note If the thread does an operation that causes it to pend, it will still
443 * be context switched out.
444 *
445 * @note Similar to irq_lock, the scheduler lock state is tracked per-thread.
446 *
447 * This should be chosen over irq_lock when possible, basically when the data
448 * protected by it is not accessible from ISRs. However, the associated
449 * k_sched_unlock() is heavier to use than irq_unlock, so if the amount of
450 * processing is really small, irq_lock might be a better choice.
451 *
452 * Can be called recursively.
453 *
454 * @return N/A
455 */
456extern void k_sched_lock(void);
457
458/*
459 * @brief Unlock the scheduler
460 *
461 * Re-enable scheduling previously disabled by k_sched_lock(). Must be called
462 * an equal amount of times k_sched_lock() was called. Threads are rescheduled
463 * upon exit.
464 *
465 * @return N/A
466 */
467extern void k_sched_unlock(void);
468
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500470 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500472 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * Custom data is not used by the kernel itself, and is freely available
475 * for a thread to use as it sees fit. It can be used as a framework
476 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500478 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400479 *
480 * @return N/A
481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400482extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483
484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500485 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500487 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400490 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491extern void *k_thread_custom_data_get(void);
492
493/**
494 * kernel timing
495 */
496
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400497#include <sys_clock.h>
498
499/* private internal time manipulation (users should never play with ticks) */
500
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500501/* added tick needed to account for tick in progress */
502#define _TICK_ALIGN 1
503
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400504static int64_t __ticks_to_ms(int64_t ticks)
505{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400506#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400507 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400508#else
509 __ASSERT(ticks == 0, "");
510 return 0;
511#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400512}
513
514
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515/* timeouts */
516
517struct _timeout;
518typedef void (*_timeout_func_t)(struct _timeout *t);
519
520struct _timeout {
521 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400522 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400523 sys_dlist_t *wait_q;
524 int32_t delta_ticks_from_prev;
525 _timeout_func_t func;
526};
527
Allan Stephens45bfa372016-10-12 12:39:42 -0500528
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400529/* timers */
530
531struct k_timer {
532 /*
533 * _timeout structure must be first here if we want to use
534 * dynamic timer allocation. timeout.node is used in the double-linked
535 * list of free timers
536 */
537 struct _timeout timeout;
538
Allan Stephens45bfa372016-10-12 12:39:42 -0500539 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400540 _wait_q_t wait_q;
541
542 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500543 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400544
545 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500546 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400547
548 /* timer period */
549 int32_t period;
550
Allan Stephens45bfa372016-10-12 12:39:42 -0500551 /* timer status */
552 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400553
Allan Stephens45bfa372016-10-12 12:39:42 -0500554 /* used to support legacy timer APIs */
555 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400556
557 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
558};
559
Allan Stephens1342adb2016-11-03 13:54:53 -0500560#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400561 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500562 .timeout.delta_ticks_from_prev = -1, \
563 .timeout.wait_q = NULL, \
564 .timeout.thread = NULL, \
565 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400566 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500567 .expiry_fn = expiry, \
568 .stop_fn = stop, \
569 .status = 0, \
570 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400571 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
572 }
573
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578 *
579 * extern struct k_timer @a name;
580 *
581 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @param expiry_fn Function to invoke each time the timer expires.
583 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500585#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500586 struct k_timer name \
587 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500588 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400589
Allan Stephens45bfa372016-10-12 12:39:42 -0500590/**
591 * @brief Initialize a timer.
592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500594 *
595 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * @param expiry_fn Function to invoke each time the timer expires.
597 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500598 *
599 * @return N/A
600 */
601extern void k_timer_init(struct k_timer *timer,
602 void (*expiry_fn)(struct k_timer *),
603 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700604
Allan Stephens45bfa372016-10-12 12:39:42 -0500605/**
606 * @brief Start a timer.
607 *
608 * This routine starts a timer, and resets its status to zero. The timer
609 * begins counting down using the specified duration and period values.
610 *
611 * Attempting to start a timer that is already running is permitted.
612 * The timer's status is reset to zero and the timer begins counting down
613 * using the new duration and period values.
614 *
615 * @param timer Address of timer.
616 * @param duration Initial timer duration (in milliseconds).
617 * @param period Timer period (in milliseconds).
618 *
619 * @return N/A
620 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400621extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500622 int32_t duration, int32_t period);
623
624/**
625 * @brief Stop a timer.
626 *
627 * This routine stops a running timer prematurely. The timer's stop function,
628 * if one exists, is invoked by the caller.
629 *
630 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500631 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500632 *
633 * @param timer Address of timer.
634 *
635 * @return N/A
636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400637extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500638
639/**
640 * @brief Read timer status.
641 *
642 * This routine reads the timer's status, which indicates the number of times
643 * it has expired since its status was last read.
644 *
645 * Calling this routine resets the timer's status to zero.
646 *
647 * @param timer Address of timer.
648 *
649 * @return Timer status.
650 */
651extern uint32_t k_timer_status_get(struct k_timer *timer);
652
653/**
654 * @brief Synchronize thread to timer expiration.
655 *
656 * This routine blocks the calling thread until the timer's status is non-zero
657 * (indicating that it has expired at least once since it was last examined)
658 * or the timer is stopped. If the timer status is already non-zero,
659 * or the timer is already stopped, the caller continues without waiting.
660 *
661 * Calling this routine resets the timer's status to zero.
662 *
663 * This routine must not be used by interrupt handlers, since they are not
664 * allowed to block.
665 *
666 * @param timer Address of timer.
667 *
668 * @return Timer status.
669 */
670extern uint32_t k_timer_status_sync(struct k_timer *timer);
671
672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500673 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500674 *
675 * This routine computes the (approximate) time remaining before a running
676 * timer next expires. If the timer is not running, it returns zero.
677 *
678 * @param timer Address of timer.
679 *
680 * @return Remaining time (in milliseconds).
681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400682extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400683
684
Allan Stephens45bfa372016-10-12 12:39:42 -0500685/* kernel clocks */
686
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500688 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * This routine returns the elapsed time since the system booted,
691 * in milliseconds.
692 *
693 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400694 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400695extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400696
697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500700 * This routine returns the lower 32-bits of the elapsed time since the system
701 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * This routine can be more efficient than k_uptime_get(), as it reduces the
704 * need for interrupt locking and 64-bit math. However, the 32-bit result
705 * cannot hold a system uptime time larger than approximately 50 days, so the
706 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500708 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400709 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400710extern uint32_t k_uptime_get_32(void);
711
712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500715 * This routine computes the elapsed time between the current system uptime
716 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500718 * @param reftime Pointer to a reference time, which is updated to the current
719 * uptime upon return.
720 *
721 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400722 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400723extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400724
725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500726 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * This routine computes the elapsed time between the current system uptime
729 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500731 * This routine can be more efficient than k_uptime_delta(), as it reduces the
732 * need for interrupt locking and 64-bit math. However, the 32-bit result
733 * cannot hold an elapsed time larger than approximately 50 days, so the
734 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500736 * @param reftime Pointer to a reference time, which is updated to the current
737 * uptime upon return.
738 *
739 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400740 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400741extern uint32_t k_uptime_delta_32(int64_t *reftime);
742
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400743/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500744 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * This routine returns the current time, as measured by the system's hardware
747 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500749 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400751extern uint32_t k_cycle_get_32(void);
752
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753/**
754 * data transfers (basic)
755 */
756
757/* fifos */
758
759struct k_fifo {
760 _wait_q_t wait_q;
761 sys_slist_t data_q;
762
763 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
764};
765
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500767 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500769 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772 *
773 * @return N/A
774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776
777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * This routine adds a data item to @a fifo. A fifo data item must be
781 * aligned on a 4-byte boundary, and the first 32 bits of the item are
782 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * @note Can be called by ISRs.
785 *
786 * @param fifo Address of the fifo.
787 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
789 * @return N/A
790 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400791extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792
793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * This routine adds a list of data items to @a fifo in one operation.
797 * The data items must be in a singly-linked list, with the first 32 bits
798 * each data item pointing to the next data item; the list must be
799 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * @param fifo Address of the fifo.
804 * @param head Pointer to first node in singly-linked list.
805 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
807 * @return N/A
808 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400809extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810
811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * This routine adds a list of data items to @a fifo in one operation.
815 * The data items must be in a singly-linked list implemented using a
816 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 * and must be re-initialized via sys_slist_init().
818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * @note Can be called by ISRs.
820 *
821 * @param fifo Address of the fifo.
822 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
824 * @return N/A
825 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400826extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * This routine removes a data item from @a fifo in a "first in, first out"
832 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
835 *
836 * @param fifo Address of the fifo.
837 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 * or one of the special values K_NO_WAIT and K_FOREVER.
839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @return Address of the data item if successful.
841 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400843extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
844
845#define K_FIFO_INITIALIZER(obj) \
846 { \
847 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400848 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400849 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
850 }
851
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
857 * extern struct k_fifo @a name;
858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400861#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500862 struct k_fifo name \
863 __in_section(_k_fifo, static, name) = \
864 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400865
866/* lifos */
867
868struct k_lifo {
869 _wait_q_t wait_q;
870 void *list;
871
872 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
873};
874
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
882 * @return N/A
883 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885
886/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * This routine adds a data item to @a lifo. A lifo data item must be
890 * aligned on a 4-byte boundary, and the first 32 bits of the item are
891 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * @note Can be called by ISRs.
894 *
895 * @param lifo Address of the lifo.
896 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897 *
898 * @return N/A
899 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400900extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901
902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500903 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * This routine removes a data item from @a lifo in a "last in, first out"
906 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
909 *
910 * @param lifo Address of the lifo.
911 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912 * or one of the special values K_NO_WAIT and K_FOREVER.
913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @return Address of the data item if successful.
915 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400917extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
918
919#define K_LIFO_INITIALIZER(obj) \
920 { \
921 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
922 .list = NULL, \
923 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
924 }
925
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400930 *
931 * extern struct k_lifo @a name;
932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400935#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500936 struct k_lifo name \
937 __in_section(_k_lifo, static, name) = \
938 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400939
940/* stacks */
941
942struct k_stack {
943 _wait_q_t wait_q;
944 uint32_t *base, *next, *top;
945
946 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
947};
948
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949/**
950 * @brief Initialize a stack.
951 *
952 * This routine initializes a stack object, prior to its first use.
953 *
954 * @param stack Address of the stack.
955 * @param buffer Address of array used to hold stacked values.
956 * @param num_entries Maximum number of values that can be stacked.
957 *
958 * @return N/A
959 */
Allan Stephens018cd9a2016-10-07 15:13:24 -0500960extern void k_stack_init(struct k_stack *stack,
961 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962
963/**
964 * @brief Push an element onto a stack.
965 *
966 * This routine adds a 32-bit value @a data to @a stack.
967 *
968 * @note Can be called by ISRs.
969 *
970 * @param stack Address of the stack.
971 * @param data Value to push onto the stack.
972 *
973 * @return N/A
974 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400975extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500976
977/**
978 * @brief Pop an element from a stack.
979 *
980 * This routine removes a 32-bit value from @a stack in a "last in, first out"
981 * manner and stores the value in @a data.
982 *
983 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
984 *
985 * @param stack Address of the stack.
986 * @param data Address of area to hold the value popped from the stack.
987 * @param timeout Waiting period to obtain a value (in milliseconds),
988 * or one of the special values K_NO_WAIT and K_FOREVER.
989 *
990 * @retval 0 if successful.
991 * @retval -EBUSY if returned without waiting.
992 * @retval -EAGAIN if waiting period timed out.
993 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400994extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
995
Peter Mitsis602e6a82016-10-17 11:48:43 -0400996#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400997 { \
998 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
999 .base = stack_buffer, \
1000 .next = stack_buffer, \
1001 .top = stack_buffer + stack_num_entries, \
1002 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1003 }
1004
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
1010 * extern struct k_stack @a name;
1011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * @param name Name of the stack.
1013 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001015#define K_STACK_DEFINE(name, stack_num_entries) \
1016 uint32_t __noinit \
1017 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001018 struct k_stack name \
1019 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001020 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1021 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001022
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001023/**
1024 * workqueues
1025 */
1026
1027struct k_work;
1028
1029typedef void (*k_work_handler_t)(struct k_work *);
1030
1031/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033 * queued to it. This is useful for drivers which need to schedule
1034 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001036 * space.
1037 */
1038struct k_work_q {
1039 struct k_fifo fifo;
1040};
1041
1042/**
1043 * @brief Work flags.
1044 */
1045enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001046 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047};
1048
1049/**
1050 * @brief An item which can be scheduled on a @ref k_work_q.
1051 */
1052struct k_work {
1053 void *_reserved; /* Used by k_fifo implementation. */
1054 k_work_handler_t handler;
1055 atomic_t flags[1];
1056};
1057
1058/**
1059 * @brief Statically initialize work item
1060 */
1061#define K_WORK_INITIALIZER(work_handler) \
1062 { \
1063 ._reserved = NULL, \
1064 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001065 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066 }
1067
1068/**
1069 * @brief Dynamically initialize work item
1070 */
1071static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1072{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001073 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001074 work->handler = handler;
1075}
1076
1077/**
1078 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001079 *
1080 * This procedure schedules a work item to be processed.
1081 * In the case where the work item has already been submitted and is pending
1082 * execution, calling this function will result in a no-op. In this case, the
1083 * work item must not be modified externally (e.g. by the caller of this
1084 * function), since that could cause the work item to be processed in a
1085 * corrupted state.
1086 *
1087 * @param work_q to schedule the work item
1088 * @param work work item
1089 *
1090 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001091 */
1092static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1093 struct k_work *work)
1094{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001095 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096 k_fifo_put(&work_q->fifo, work);
1097 }
1098}
1099
1100/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001101 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
1103 * @param work Work item to query
1104 *
1105 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001106 */
1107static inline int k_work_pending(struct k_work *work)
1108{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001109 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001110}
1111
1112/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 * @brief Start a new workqueue.
1114 *
1115 * This routine must not be called from an ISR.
1116 *
1117 * @param work_q Pointer to Work queue
1118 * @param stack Pointer to work queue thread's stack
1119 * @param stack_size Size of the work queue thread's stack
1120 * @param prio Priority of the work queue's thread
1121 *
1122 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001123 */
Allan Stephens904cf972016-10-07 13:59:23 -05001124extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1125 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001126
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001127#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129/**
1130 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131 */
1132struct k_delayed_work {
1133 struct k_work work;
1134 struct _timeout timeout;
1135 struct k_work_q *work_q;
1136};
1137
1138/**
1139 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 *
1141 * Initialize a delayed work item.
1142 *
1143 * @param work Delayed work item
1144 * @param handler Routine invoked when processing delayed work item
1145 *
1146 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001148extern void k_delayed_work_init(struct k_delayed_work *work,
1149 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001150
1151/**
1152 * @brief Submit a delayed work item to a workqueue.
1153 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155 * Once the delay has passed, the work item is submitted to the work queue:
1156 * at this point, it is no longer possible to cancel it. Once the work item's
1157 * handler is about to be executed, the work is considered complete and can be
1158 * resubmitted.
1159 *
1160 * Care must be taken if the handler blocks or yield as there is no implicit
1161 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1162 * it should be explicitly done between the submitter and the handler.
1163 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001164 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001166 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001169 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001170extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1171 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001172 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173
1174/**
1175 * @brief Cancel a delayed work item
1176 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001178 * or is idle, this will do nothing. The only case where this can fail is when
1179 * the work has been submitted to the work queue, but the handler has not run
1180 * yet.
1181 *
1182 * @param work Delayed work item to be canceled
1183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001184 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001185 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001186extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001188#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001190extern struct k_work_q k_sys_work_q;
1191
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193 * @brief Submit a work item to the system workqueue.
1194 *
1195 * @ref k_work_submit_to_queue
1196 *
1197 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001199 * unexpected behavior.
1200 */
1201static inline void k_work_submit(struct k_work *work)
1202{
1203 k_work_submit_to_queue(&k_sys_work_q, work);
1204}
1205
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001206#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001208 * @brief Submit a delayed work item to the system workqueue.
1209 *
1210 * @ref k_delayed_work_submit_to_queue
1211 *
1212 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001214 * unexpected behavior.
1215 */
1216static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001217 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001218{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001219 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001220}
1221
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001222#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001223
1224/**
1225 * synchronization
1226 */
1227
1228/* mutexes */
1229
1230struct k_mutex {
1231 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001232 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001233 uint32_t lock_count;
1234 int owner_orig_prio;
1235#ifdef CONFIG_OBJECT_MONITOR
1236 int num_lock_state_changes;
1237 int num_conflicts;
1238#endif
1239
1240 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1241};
1242
1243#ifdef CONFIG_OBJECT_MONITOR
1244#define _MUTEX_INIT_OBJECT_MONITOR \
1245 .num_lock_state_changes = 0, .num_conflicts = 0,
1246#else
1247#define _MUTEX_INIT_OBJECT_MONITOR
1248#endif
1249
1250#define K_MUTEX_INITIALIZER(obj) \
1251 { \
1252 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1253 .owner = NULL, \
1254 .lock_count = 0, \
1255 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1256 _MUTEX_INIT_OBJECT_MONITOR \
1257 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1258 }
1259
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 *
1265 * extern struct k_mutex @a name;
1266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001270 struct k_mutex name \
1271 __in_section(_k_mutex, static, name) = \
1272 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * Upon completion, the mutex is available and does not have an owner.
1280 *
1281 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001282 *
1283 * @return N/A
1284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286
1287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine locks @a mutex. If the mutex is locked by another thread,
1291 * the calling thread waits until the mutex becomes available or until
1292 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * A thread is permitted to lock a mutex it has already locked. The operation
1295 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001297 * @param mutex Address of the mutex.
1298 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 * or one of the special values K_NO_WAIT and K_FOREVER.
1300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301 * @retval 0 if successful.
1302 * @retval -EBUSY if returned without waiting.
1303 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001305extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001306
1307/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001308 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001310 * This routine unlocks @a mutex. The mutex must already be locked by the
1311 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312 *
1313 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001314 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001315 * thread.
1316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 *
1319 * @return N/A
1320 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321extern void k_mutex_unlock(struct k_mutex *mutex);
1322
1323/* semaphores */
1324
1325struct k_sem {
1326 _wait_q_t wait_q;
1327 unsigned int count;
1328 unsigned int limit;
1329
1330 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1331};
1332
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001334 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * @param sem Address of the semaphore.
1339 * @param initial_count Initial semaphore count.
1340 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001341 *
1342 * @return N/A
1343 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1345 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001346
1347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001348 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001350 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1353 *
1354 * @param sem Address of the semaphore.
1355 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001356 * or one of the special values K_NO_WAIT and K_FOREVER.
1357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @retval 0 if successful.
1359 * @retval -EBUSY if returned without waiting.
1360 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001362extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001363
1364/**
1365 * @brief Give a semaphore.
1366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001367 * This routine gives @a sem, unless the semaphore is already at its maximum
1368 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001370 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001372 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001373 *
1374 * @return N/A
1375 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001376extern void k_sem_give(struct k_sem *sem);
1377
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001378/**
1379 * @brief Reset a semaphore's count to zero.
1380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001383 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001384 *
1385 * @return N/A
1386 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001387static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388{
1389 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390}
1391
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001392/**
1393 * @brief Get a semaphore's count.
1394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001395 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001400 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001401static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402{
1403 return sem->count;
1404}
1405
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1407 { \
1408 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1409 .count = initial_count, \
1410 .limit = count_limit, \
1411 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1412 }
1413
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001415 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001417 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001418 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001419 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001421 * @param name Name of the semaphore.
1422 * @param initial_count Initial semaphore count.
1423 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001424 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001426 struct k_sem name \
1427 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428 K_SEM_INITIALIZER(name, initial_count, count_limit)
1429
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001430/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001432#define K_ALERT_DEFAULT NULL
1433#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001435typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001436
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001437struct k_alert {
1438 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001439 atomic_t send_count;
1440 struct k_work work_item;
1441 struct k_sem sem;
1442
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001443 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444};
1445
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001446extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001447
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001448#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001449 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001450 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001451 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001452 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001453 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1455 }
1456
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001461 *
1462 * extern struct k_alert @a name;
1463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @param name Name of the alert.
1465 * @param alert_handler Action to take when alert is sent. Specify either
1466 * the address of a function to be invoked by the system workqueue
1467 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1468 * K_ALERT_DEFAULT (which causes the alert to pend).
1469 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001470 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001471#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001472 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001473 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001474 K_ALERT_INITIALIZER(name, alert_handler, \
1475 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001476
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001482 * @param alert Address of the alert.
1483 * @param handler Action to take when alert is sent. Specify either the address
1484 * of a function to be invoked by the system workqueue thread,
1485 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1486 * K_ALERT_DEFAULT (which causes the alert to pend).
1487 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001488 *
1489 * @return N/A
1490 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001491extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1492 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001493
1494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001497 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1500 *
1501 * @param alert Address of the alert.
1502 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001503 * or one of the special values K_NO_WAIT and K_FOREVER.
1504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @retval 0 if successful.
1506 * @retval -EBUSY if returned without waiting.
1507 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001509extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001510
1511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * This routine signals @a alert. The action specified for @a alert will
1515 * be taken, which may trigger the execution of an alert handler function
1516 * and/or cause the alert to pend (assuming the alert has not reached its
1517 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001519 * @note Can be called by ISRs.
1520 *
1521 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001522 *
1523 * @return N/A
1524 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001525extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001526
1527/**
1528 * data transfers (complex)
1529 */
1530
1531/* message queues */
1532
1533struct k_msgq {
1534 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001535 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536 uint32_t max_msgs;
1537 char *buffer_start;
1538 char *buffer_end;
1539 char *read_ptr;
1540 char *write_ptr;
1541 uint32_t used_msgs;
1542
1543 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1544};
1545
Peter Mitsis1da807e2016-10-06 11:36:59 -04001546#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001547 { \
1548 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001549 .max_msgs = q_max_msgs, \
1550 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001551 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001552 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001553 .read_ptr = q_buffer, \
1554 .write_ptr = q_buffer, \
1555 .used_msgs = 0, \
1556 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1557 }
1558
Peter Mitsis1da807e2016-10-06 11:36:59 -04001559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001560 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1563 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001564 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1565 * message is similarly aligned to this boundary, @a q_msg_size must also be
1566 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001568 * The message queue can be accessed outside the module where it is defined
1569 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001570 *
1571 * extern struct k_msgq @a name;
1572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * @param q_name Name of the message queue.
1574 * @param q_msg_size Message size (in bytes).
1575 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001576 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001577 */
1578#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1579 static char __noinit __aligned(q_align) \
1580 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001581 struct k_msgq q_name \
1582 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001583 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1584 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585
Peter Mitsisd7a37502016-10-13 11:37:40 -04001586/**
1587 * @brief Initialize a message queue.
1588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * This routine initializes a message queue object, prior to its first use.
1590 *
Allan Stephensda827222016-11-09 14:23:58 -06001591 * The message queue's ring buffer must contain space for @a max_msgs messages,
1592 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1593 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1594 * that each message is similarly aligned to this boundary, @a q_msg_size
1595 * must also be a multiple of N.
1596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001597 * @param q Address of the message queue.
1598 * @param buffer Pointer to ring buffer that holds queued messages.
1599 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001600 * @param max_msgs Maximum number of messages that can be queued.
1601 *
1602 * @return N/A
1603 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001604extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001605 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001606
1607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001610 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001611 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001612 * @note Can be called by ISRs.
1613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @param q Address of the message queue.
1615 * @param data Pointer to the message.
1616 * @param timeout Waiting period to add the message (in milliseconds),
1617 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * @retval 0 if successful.
1620 * @retval -ENOMSG if returned without waiting or after a queue purge.
1621 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001622 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001623extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001624
1625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001628 * This routine receives a message from message queue @a q in a "first in,
1629 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001630 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001631 * @note Can be called by ISRs.
1632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @param q Address of the message queue.
1634 * @param data Address of area to hold the received message.
1635 * @param timeout Waiting period to receive the message (in milliseconds),
1636 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001638 * @retval 0 if successful.
1639 * @retval -ENOMSG if returned without waiting.
1640 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001641 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001642extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001643
1644/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001645 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001647 * This routine discards all unreceived messages in a message queue's ring
1648 * buffer. Any threads that are blocked waiting to send a message to the
1649 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001652 *
1653 * @return N/A
1654 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001655extern void k_msgq_purge(struct k_msgq *q);
1656
Peter Mitsis67be2492016-10-07 11:44:34 -04001657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001658 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001660 * This routine returns the number of unused entries in a message queue's
1661 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001663 * @param q Address of the message queue.
1664 *
1665 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001666 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001667static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001668{
1669 return q->max_msgs - q->used_msgs;
1670}
1671
Peter Mitsisd7a37502016-10-13 11:37:40 -04001672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001675 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * @param q Address of the message queue.
1678 *
1679 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001680 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001681static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001682{
1683 return q->used_msgs;
1684}
1685
1686struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001687 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001688 void *addr_in_pool;
1689 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001690 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001691};
1692
1693/* mailboxes */
1694
1695struct k_mbox_msg {
1696 /** internal use only - needed for legacy API support */
1697 uint32_t _mailbox;
1698 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001699 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700 /** application-defined information value */
1701 uint32_t info;
1702 /** sender's message data buffer */
1703 void *tx_data;
1704 /** internal use only - needed for legacy API support */
1705 void *_rx_data;
1706 /** message data block descriptor */
1707 struct k_mem_block tx_block;
1708 /** source thread id */
1709 k_tid_t rx_source_thread;
1710 /** target thread id */
1711 k_tid_t tx_target_thread;
1712 /** internal use only - thread waiting on send (may be a dummy) */
1713 k_tid_t _syncing_thread;
1714#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1715 /** internal use only - semaphore used during asynchronous send */
1716 struct k_sem *_async_sem;
1717#endif
1718};
1719
1720struct k_mbox {
1721 _wait_q_t tx_msg_queue;
1722 _wait_q_t rx_msg_queue;
1723
1724 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1725};
1726
1727#define K_MBOX_INITIALIZER(obj) \
1728 { \
1729 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1730 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1731 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1732 }
1733
Peter Mitsis12092702016-10-14 12:57:23 -04001734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 *
1739 * extern struct k_mbox @a name;
1740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001744 struct k_mbox name \
1745 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746 K_MBOX_INITIALIZER(name) \
1747
Peter Mitsis12092702016-10-14 12:57:23 -04001748/**
1749 * @brief Initialize a mailbox.
1750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * This routine initializes a mailbox object, prior to its first use.
1752 *
1753 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001754 *
1755 * @return N/A
1756 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001757extern void k_mbox_init(struct k_mbox *mbox);
1758
Peter Mitsis12092702016-10-14 12:57:23 -04001759/**
1760 * @brief Send a mailbox message in a synchronous manner.
1761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * This routine sends a message to @a mbox and waits for a receiver to both
1763 * receive and process it. The message data may be in a buffer, in a memory
1764 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04001765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @param mbox Address of the mailbox.
1767 * @param tx_msg Address of the transmit message descriptor.
1768 * @param timeout Waiting period for the message to be received (in
1769 * milliseconds), or one of the special values K_NO_WAIT
1770 * and K_FOREVER. Once the message has been received,
1771 * this routine waits as long as necessary for the message
1772 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04001773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * @retval 0 if successful.
1775 * @retval -ENOMSG if returned without waiting.
1776 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001777 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001778extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001779 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001780
1781#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1782/**
1783 * @brief Send a mailbox message in an asynchronous manner.
1784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * This routine sends a message to @a mbox without waiting for a receiver
1786 * to process it. The message data may be in a buffer, in a memory pool block,
1787 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
1788 * will be given when the message has been both received and completely
1789 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04001790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * @param mbox Address of the mailbox.
1792 * @param tx_msg Address of the transmit message descriptor.
1793 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04001794 *
1795 * @return N/A
1796 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001797extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001799#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001800
Peter Mitsis12092702016-10-14 12:57:23 -04001801/**
1802 * @brief Receive a mailbox message.
1803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * This routine receives a message from @a mbox, then optionally retrieves
1805 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @param mbox Address of the mailbox.
1808 * @param rx_msg Address of the receive message descriptor.
1809 * @param buffer Address of the buffer to receive data, or NULL to defer data
1810 * retrieval and message disposal until later.
1811 * @param timeout Waiting period for a message to be received (in
1812 * milliseconds), or one of the special values K_NO_WAIT
1813 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @retval 0 if successful.
1816 * @retval -ENOMSG if returned without waiting.
1817 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001818 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001819extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001820 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001821
1822/**
1823 * @brief Retrieve mailbox message data into a buffer.
1824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * This routine completes the processing of a received message by retrieving
1826 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001827 *
1828 * Alternatively, this routine can be used to dispose of a received message
1829 * without retrieving its data.
1830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * @param rx_msg Address of the receive message descriptor.
1832 * @param buffer Address of the buffer to receive data, or NULL to discard
1833 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04001834 *
1835 * @return N/A
1836 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001837extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001838
1839/**
1840 * @brief Retrieve mailbox message data into a memory pool block.
1841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001842 * This routine completes the processing of a received message by retrieving
1843 * its data into a memory pool block, then disposing of the message.
1844 * The memory pool block that results from successful retrieval must be
1845 * returned to the pool once the data has been processed, even in cases
1846 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04001847 *
1848 * Alternatively, this routine can be used to dispose of a received message
1849 * without retrieving its data. In this case there is no need to return a
1850 * memory pool block to the pool.
1851 *
1852 * This routine allocates a new memory pool block for the data only if the
1853 * data is not already in one. If a new block cannot be allocated, the routine
1854 * returns a failure code and the received message is left unchanged. This
1855 * permits the caller to reattempt data retrieval at a later time or to dispose
1856 * of the received message without retrieving its data.
1857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @param rx_msg Address of a receive message descriptor.
1859 * @param pool Address of memory pool, or NULL to discard data.
1860 * @param block Address of the area to hold memory pool block info.
1861 * @param timeout Waiting period to wait for a memory pool block (in
1862 * milliseconds), or one of the special values K_NO_WAIT
1863 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @retval 0 if successful.
1866 * @retval -ENOMEM if returned without waiting.
1867 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001868 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001869extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001870 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001871 struct k_mem_block *block, int32_t timeout);
1872
1873/* pipes */
1874
1875struct k_pipe {
1876 unsigned char *buffer; /* Pipe buffer: may be NULL */
1877 size_t size; /* Buffer size */
1878 size_t bytes_used; /* # bytes used in buffer */
1879 size_t read_index; /* Where in buffer to read from */
1880 size_t write_index; /* Where in buffer to write */
1881
1882 struct {
1883 _wait_q_t readers; /* Reader wait queue */
1884 _wait_q_t writers; /* Writer wait queue */
1885 } wait_q;
1886
1887 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1888};
1889
Peter Mitsise5d9c582016-10-14 14:44:57 -04001890#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001891 { \
1892 .buffer = pipe_buffer, \
1893 .size = pipe_buffer_size, \
1894 .bytes_used = 0, \
1895 .read_index = 0, \
1896 .write_index = 0, \
1897 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1898 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1899 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1900 }
1901
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 *
1907 * extern struct k_pipe @a name;
1908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 * @param name Name of the pipe.
1910 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
1911 * or zero if no ring buffer is used.
1912 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001914#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1915 static unsigned char __noinit __aligned(pipe_align) \
1916 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001917 struct k_pipe name \
1918 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001919 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001920
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * This routine initializes a pipe object, prior to its first use.
1925 *
1926 * @param pipe Address of the pipe.
1927 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
1928 * is used.
1929 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
1930 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931 *
1932 * @return N/A
1933 */
1934extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1935 size_t size);
1936
1937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001938 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * @param pipe Address of the pipe.
1943 * @param data Address of data to write.
1944 * @param bytes_to_write Size of data (in bytes).
1945 * @param bytes_written Address of area to hold the number of bytes written.
1946 * @param min_xfer Minimum number of bytes to write.
1947 * @param timeout Waiting period to wait for the data to be written (in
1948 * milliseconds), or one of the special values K_NO_WAIT
1949 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001951 * @retval 0 if at least @a min_xfer data bytes were written.
1952 * @retval -EIO if returned without waiting; zero data bytes were written.
1953 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1954 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001956extern int k_pipe_put(struct k_pipe *pipe, void *data,
1957 size_t bytes_to_write, size_t *bytes_written,
1958 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959
1960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001965 * @param pipe Address of the pipe.
1966 * @param data Address to place the data read from pipe.
1967 * @param bytes_to_read Maximum number of data bytes to read.
1968 * @param bytes_read Address of area to hold the number of bytes read.
1969 * @param min_xfer Minimum number of data bytes to read.
1970 * @param timeout Waiting period to wait for the data to be read (in
1971 * milliseconds), or one of the special values K_NO_WAIT
1972 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974 * @retval 0 if at least @a min_xfer data bytes were read.
1975 * @retval -EIO if returned without waiting; zero data bytes were read.
1976 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1977 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001978 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001979extern int k_pipe_get(struct k_pipe *pipe, void *data,
1980 size_t bytes_to_read, size_t *bytes_read,
1981 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982
Peter Mitsis2fef0232016-10-14 14:53:44 -04001983#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001985 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * This routine writes the data contained in a memory block to @a pipe.
1988 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992 * @param block Memory block containing data to send
1993 * @param size Number of data bytes in memory block to send
1994 * @param sem Semaphore to signal upon completion (else NULL)
1995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001996 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997 */
1998extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1999 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04002000#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002001
2002/**
2003 * memory management
2004 */
2005
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002006/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002008struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002010 uint32_t num_blocks;
2011 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002012 char *buffer;
2013 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002014 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002015
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002016 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017};
2018
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002019#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2020 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021 { \
2022 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002023 .num_blocks = slab_num_blocks, \
2024 .block_size = slab_block_size, \
2025 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026 .free_list = NULL, \
2027 .num_used = 0, \
2028 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2029 }
2030
Peter Mitsis578f9112016-10-07 13:50:31 -04002031/**
Allan Stephensda827222016-11-09 14:23:58 -06002032 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002033 *
Allan Stephensda827222016-11-09 14:23:58 -06002034 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002036 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2037 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002038 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002039 *
Allan Stephensda827222016-11-09 14:23:58 -06002040 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
2043 * extern struct k_mem_slab @a name;
2044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * @param name Name of the memory slab.
2046 * @param slab_block_size Size of each memory block (in bytes).
2047 * @param slab_num_blocks Number memory blocks.
2048 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002049 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002050#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2051 char __noinit __aligned(slab_align) \
2052 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2053 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002054 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002055 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2056 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002058/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002059 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002062 *
Allan Stephensda827222016-11-09 14:23:58 -06002063 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2064 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2065 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2066 * To ensure that each memory block is similarly aligned to this boundary,
2067 * @a slab_block_size must also be a multiple of N.
2068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * @param slab Address of the memory slab.
2070 * @param buffer Pointer to buffer used for the memory blocks.
2071 * @param block_size Size of each memory block (in bytes).
2072 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002073 *
2074 * @return N/A
2075 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002076extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002077 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002078
2079/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002084 * @param slab Address of the memory slab.
2085 * @param mem Pointer to block address area.
2086 * @param timeout Maximum time to wait for operation to complete
2087 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2088 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @retval 0 if successful. The block address area pointed at by @a mem
2091 * is set to the starting address of the memory block.
2092 * @retval -ENOMEM if failed immediately.
2093 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002094 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002095extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2096 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002097
2098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * This routine releases a previously allocated memory block back to its
2102 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * @param slab Address of the memory slab.
2105 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002106 *
2107 * @return N/A
2108 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002109extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * This routine gets the number of memory blocks that are currently
2115 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002120 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002121static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002123 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124}
2125
Peter Mitsisc001aa82016-10-13 13:53:37 -04002126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 * This routine gets the number of memory blocks that are currently
2130 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002134 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002135 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002136static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002137{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002138 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002139}
2140
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002141/* memory pools */
2142
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002143/*
2144 * Memory pool requires a buffer and two arrays of structures for the
2145 * memory block accounting:
2146 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2147 * status of four blocks of memory.
2148 */
2149struct k_mem_pool_quad_block {
2150 char *mem_blocks; /* pointer to the first of four memory blocks */
2151 uint32_t mem_status; /* four bits. If bit is set, memory block is
2152 allocated */
2153};
2154/*
2155 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2156 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2157 * block size is 4 times less than the previous one and thus requires 4 times
2158 * bigger array of k_mem_pool_quad_block structures to keep track of the
2159 * memory blocks.
2160 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002162/*
2163 * The array of k_mem_pool_block_set keeps the information of each array of
2164 * k_mem_pool_quad_block structures
2165 */
2166struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002167 size_t block_size; /* memory block size */
2168 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002169 struct k_mem_pool_quad_block *quad_block;
2170 int count;
2171};
2172
2173/* Memory pool descriptor */
2174struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002175 size_t max_block_size;
2176 size_t min_block_size;
2177 uint32_t nr_of_maxblocks;
2178 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002179 struct k_mem_pool_block_set *block_set;
2180 char *bufblock;
2181 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2183};
2184
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002185#ifdef CONFIG_ARM
2186#define _SECTION_TYPE_SIGN "%"
2187#else
2188#define _SECTION_TYPE_SIGN "@"
2189#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002191/*
2192 * Static memory pool initialization
2193 */
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002194/**
2195 * @cond internal
2196 * Make Doxygen skip assembler macros
2197 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002198/*
2199 * Use .altmacro to be able to recalculate values and pass them as string
2200 * arguments when calling assembler macros resursively
2201 */
2202__asm__(".altmacro\n\t");
2203
2204/*
2205 * Recursively calls a macro
2206 * The followig global symbols need to be initialized:
2207 * __memory_pool_max_block_size - maximal size of the memory block
2208 * __memory_pool_min_block_size - minimal size of the memory block
2209 * Notes:
2210 * Global symbols are used due the fact that assembler macro allows only
2211 * one argument be passed with the % conversion
2212 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2213 * is used instead of / 4.
2214 * n_max argument needs to go first in the invoked macro, as some
2215 * assemblers concatenate \name and %(\n_max * 4) arguments
2216 * if \name goes first
2217 */
2218__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2219 ".ifge __memory_pool_max_block_size >> 2 -"
2220 " __memory_pool_min_block_size\n\t\t"
2221 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2222 "\\macro_name %(\\n_max * 4) \\name\n\t"
2223 ".endif\n\t"
2224 ".endm\n");
2225
2226/*
2227 * Build quad blocks
2228 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2229 * structures and recursively calls itself for the next array, 4 times
2230 * larger.
2231 * The followig global symbols need to be initialized:
2232 * __memory_pool_max_block_size - maximal size of the memory block
2233 * __memory_pool_min_block_size - minimal size of the memory block
2234 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2235 */
2236__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002237 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002238 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2239 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2240 ".if \\n_max % 4\n\t\t"
2241 ".skip __memory_pool_quad_block_size\n\t"
2242 ".endif\n\t"
2243 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2244 ".endm\n");
2245
2246/*
2247 * Build block sets and initialize them
2248 * Macro initializes the k_mem_pool_block_set structure and
2249 * recursively calls itself for the next one.
2250 * The followig global symbols need to be initialized:
2251 * __memory_pool_max_block_size - maximal size of the memory block
2252 * __memory_pool_min_block_size - minimal size of the memory block
2253 * __memory_pool_block_set_count, the number of the elements in the
2254 * block set array must be set to 0. Macro calculates it's real
2255 * value.
2256 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2257 * structures, _build_quad_blocks must be called prior it.
2258 */
2259__asm__(".macro _build_block_set n_max, name\n\t"
2260 ".int __memory_pool_max_block_size\n\t" /* block_size */
2261 ".if \\n_max % 4\n\t\t"
2262 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2263 ".else\n\t\t"
2264 ".int \\n_max >> 2\n\t"
2265 ".endif\n\t"
2266 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2267 ".int 0\n\t" /* count */
2268 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2269 "__do_recurse _build_block_set \\name \\n_max\n\t"
2270 ".endm\n");
2271
2272/*
2273 * Build a memory pool structure and initialize it
2274 * Macro uses __memory_pool_block_set_count global symbol,
2275 * block set addresses and buffer address, it may be called only after
2276 * _build_block_set
2277 */
2278__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002279 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002280 _SECTION_TYPE_SIGN "progbits\n\t"
2281 ".globl \\name\n\t"
2282 "\\name:\n\t"
2283 ".int \\max_size\n\t" /* max_block_size */
2284 ".int \\min_size\n\t" /* min_block_size */
2285 ".int \\n_max\n\t" /* nr_of_maxblocks */
2286 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2287 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2288 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2289 ".int 0\n\t" /* wait_q->head */
2290 ".int 0\n\t" /* wait_q->next */
2291 ".popsection\n\t"
2292 ".endm\n");
2293
2294#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2295 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2296 _SECTION_TYPE_SIGN "progbits\n\t"); \
2297 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2298 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2299 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2300 STRINGIFY(name) "\n\t"); \
2301 __asm__(".popsection\n\t")
2302
2303#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2304 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2305 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2306 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2307 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002308 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002309 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2310 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2311 STRINGIFY(name) "\n\t"); \
2312 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2313 __asm__(".int __memory_pool_block_set_count\n\t"); \
2314 __asm__(".popsection\n\t"); \
2315 extern uint32_t _mem_pool_block_set_count_##name; \
2316 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2317
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002318#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2319 char __noinit __aligned(align) \
2320 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002321
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002322/*
2323 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2324 * to __memory_pool_quad_block_size absolute symbol.
2325 * This function does not get called, but compiler calculates the value and
2326 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2327 */
2328static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2329{
2330 __asm__(".globl __memory_pool_quad_block_size\n\t"
2331#ifdef CONFIG_NIOS2
2332 "__memory_pool_quad_block_size = %0\n\t"
2333#else
2334 "__memory_pool_quad_block_size = %c0\n\t"
2335#endif
2336 :
2337 : "n"(sizeof(struct k_mem_pool_quad_block)));
2338}
2339
2340/**
2341 * @endcond
2342 * End of assembler macros that Doxygen has to skip
2343 */
2344
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002345/**
2346 * @brief Define a memory pool
2347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * This defines and initializes a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2351 * long. The memory pool allows blocks to be repeatedly partitioned into
2352 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2353 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002354 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002356 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 * If the pool is to be accessed outside the module where it is defined, it
2358 * can be declared via
2359 *
2360 * extern struct k_mem_pool @a name;
2361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param name Name of the memory pool.
2363 * @param min_size Size of the smallest blocks in the pool (in bytes).
2364 * @param max_size Size of the largest blocks in the pool (in bytes).
2365 * @param n_max Number of maximum sized blocks in the pool.
2366 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002367 */
2368#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002369 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2370 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002371 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002372 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2373 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2374 extern struct k_mem_pool name
2375
Peter Mitsis937042c2016-10-13 13:18:26 -04002376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * @param pool Address of the memory pool.
2382 * @param block Pointer to block descriptor for the allocated memory.
2383 * @param size Amount of memory to allocate (in bytes).
2384 * @param timeout Maximum time to wait for operation to complete
2385 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2386 * or K_FOREVER to wait as long as necessary.
2387 *
2388 * @retval 0 if successful. The @a data field of the block descriptor
2389 * is set to the starting address of the memory block.
2390 * @retval -ENOMEM if unable to allocate a memory block.
2391 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002392 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002393extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002394 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002395
2396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 * This routine releases a previously allocated memory block back to its
2400 * memory pool.
2401 *
2402 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002403 *
2404 * @return N/A
2405 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002407
2408/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * This routine instructs a memory pool to concatenate unused memory blocks
2412 * into larger blocks wherever possible. Manually defragmenting the memory
2413 * pool may speed up future allocations of memory blocks by eliminating the
2414 * need for the memory pool to perform an automatic partial defragmentation.
2415 *
2416 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002417 *
2418 * @return N/A
2419 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002420extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002421
2422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002426 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002431 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002432extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002433
2434/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002436 *
2437 * This routine provides traditional free() semantics. The memory being
2438 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002440 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002441 *
2442 * @return N/A
2443 */
2444extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
2446/*
2447 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2448 * hook into the device subsystem, which itself uses nanokernel semaphores,
2449 * and thus currently requires the definition of nano_sem.
2450 */
2451#include <legacy.h>
2452#include <arch/cpu.h>
2453
2454/*
2455 * private APIs that are utilized by one or more public APIs
2456 */
2457
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002460extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461
2462#ifdef __cplusplus
2463}
2464#endif
2465
Andrew Boiee004dec2016-11-07 09:01:19 -08002466#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2467/*
2468 * Define new and delete operators.
2469 * At this moment, the operators do nothing since objects are supposed
2470 * to be statically allocated.
2471 */
2472inline void operator delete(void *ptr)
2473{
2474 (void)ptr;
2475}
2476
2477inline void operator delete[](void *ptr)
2478{
2479 (void)ptr;
2480}
2481
2482inline void *operator new(size_t size)
2483{
2484 (void)size;
2485 return NULL;
2486}
2487
2488inline void *operator new[](size_t size)
2489{
2490 (void)size;
2491 return NULL;
2492}
2493
2494/* Placement versions of operator new and delete */
2495inline void operator delete(void *ptr1, void *ptr2)
2496{
2497 (void)ptr1;
2498 (void)ptr2;
2499}
2500
2501inline void operator delete[](void *ptr1, void *ptr2)
2502{
2503 (void)ptr1;
2504 (void)ptr2;
2505}
2506
2507inline void *operator new(size_t size, void *ptr)
2508{
2509 (void)size;
2510 return ptr;
2511}
2512
2513inline void *operator new[](size_t size, void *ptr)
2514{
2515 (void)size;
2516 return ptr;
2517}
2518
2519#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2520
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521#endif /* _kernel__h_ */