blob: 652c3c3178837b8ccaad633183382b71193404ec [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
1564 * @a q_align -byte boundary. To ensure that each message is aligned to a
1565 * @a q_align -byte boundary, @a q_msg_size must be a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * The message queue can be accessed outside the module where it is defined
1568 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
1570 * extern struct k_msgq @a name;
1571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001572 * @param q_name Name of the message queue.
1573 * @param q_msg_size Message size (in bytes).
1574 * @param q_max_msgs Maximum number of messages that can be queued.
1575 * @param q_align Alignment of the message queue's ring buffer (power of 2).
Peter Mitsis1da807e2016-10-06 11:36:59 -04001576 */
1577#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1578 static char __noinit __aligned(q_align) \
1579 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001580 struct k_msgq q_name \
1581 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001582 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1583 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001584
Peter Mitsisd7a37502016-10-13 11:37:40 -04001585/**
1586 * @brief Initialize a message queue.
1587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * This routine initializes a message queue object, prior to its first use.
1589 *
1590 * @param q Address of the message queue.
1591 * @param buffer Pointer to ring buffer that holds queued messages.
1592 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001593 * @param max_msgs Maximum number of messages that can be queued.
1594 *
1595 * @return N/A
1596 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001597extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001598 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001599
1600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001604 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001605 * @note Can be called by ISRs.
1606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * @param q Address of the message queue.
1608 * @param data Pointer to the message.
1609 * @param timeout Waiting period to add the message (in milliseconds),
1610 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @retval 0 if successful.
1613 * @retval -ENOMSG if returned without waiting or after a queue purge.
1614 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001615 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001616extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001617
1618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001621 * This routine receives a message from message queue @a q in a "first in,
1622 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001623 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001624 * @note Can be called by ISRs.
1625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @param q Address of the message queue.
1627 * @param data Address of area to hold the received message.
1628 * @param timeout Waiting period to receive the message (in milliseconds),
1629 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * @retval 0 if successful.
1632 * @retval -ENOMSG if returned without waiting.
1633 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001636
1637/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001638 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001640 * This routine discards all unreceived messages in a message queue's ring
1641 * buffer. Any threads that are blocked waiting to send a message to the
1642 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001645 *
1646 * @return N/A
1647 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648extern void k_msgq_purge(struct k_msgq *q);
1649
Peter Mitsis67be2492016-10-07 11:44:34 -04001650/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * This routine returns the number of unused entries in a message queue's
1654 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * @param q Address of the message queue.
1657 *
1658 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001659 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001660static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001661{
1662 return q->max_msgs - q->used_msgs;
1663}
1664
Peter Mitsisd7a37502016-10-13 11:37:40 -04001665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001666 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001670 * @param q Address of the message queue.
1671 *
1672 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001673 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001674static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675{
1676 return q->used_msgs;
1677}
1678
1679struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001680 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001681 void *addr_in_pool;
1682 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001683 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001684};
1685
1686/* mailboxes */
1687
1688struct k_mbox_msg {
1689 /** internal use only - needed for legacy API support */
1690 uint32_t _mailbox;
1691 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001692 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001693 /** application-defined information value */
1694 uint32_t info;
1695 /** sender's message data buffer */
1696 void *tx_data;
1697 /** internal use only - needed for legacy API support */
1698 void *_rx_data;
1699 /** message data block descriptor */
1700 struct k_mem_block tx_block;
1701 /** source thread id */
1702 k_tid_t rx_source_thread;
1703 /** target thread id */
1704 k_tid_t tx_target_thread;
1705 /** internal use only - thread waiting on send (may be a dummy) */
1706 k_tid_t _syncing_thread;
1707#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1708 /** internal use only - semaphore used during asynchronous send */
1709 struct k_sem *_async_sem;
1710#endif
1711};
1712
1713struct k_mbox {
1714 _wait_q_t tx_msg_queue;
1715 _wait_q_t rx_msg_queue;
1716
1717 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1718};
1719
1720#define K_MBOX_INITIALIZER(obj) \
1721 { \
1722 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1723 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1724 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1725 }
1726
Peter Mitsis12092702016-10-14 12:57:23 -04001727/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
1732 * extern struct k_mbox @a name;
1733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001735 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001736#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001737 struct k_mbox name \
1738 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001739 K_MBOX_INITIALIZER(name) \
1740
Peter Mitsis12092702016-10-14 12:57:23 -04001741/**
1742 * @brief Initialize a mailbox.
1743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * This routine initializes a mailbox object, prior to its first use.
1745 *
1746 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001747 *
1748 * @return N/A
1749 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750extern void k_mbox_init(struct k_mbox *mbox);
1751
Peter Mitsis12092702016-10-14 12:57:23 -04001752/**
1753 * @brief Send a mailbox message in a synchronous manner.
1754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * This routine sends a message to @a mbox and waits for a receiver to both
1756 * receive and process it. The message data may be in a buffer, in a memory
1757 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04001758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * @param mbox Address of the mailbox.
1760 * @param tx_msg Address of the transmit message descriptor.
1761 * @param timeout Waiting period for the message to be received (in
1762 * milliseconds), or one of the special values K_NO_WAIT
1763 * and K_FOREVER. Once the message has been received,
1764 * this routine waits as long as necessary for the message
1765 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04001766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * @retval 0 if successful.
1768 * @retval -ENOMSG if returned without waiting.
1769 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001770 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001771extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001772 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001773
1774#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1775/**
1776 * @brief Send a mailbox message in an asynchronous manner.
1777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * This routine sends a message to @a mbox without waiting for a receiver
1779 * to process it. The message data may be in a buffer, in a memory pool block,
1780 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
1781 * will be given when the message has been both received and completely
1782 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04001783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001784 * @param mbox Address of the mailbox.
1785 * @param tx_msg Address of the transmit message descriptor.
1786 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04001787 *
1788 * @return N/A
1789 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001790extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001792#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001793
Peter Mitsis12092702016-10-14 12:57:23 -04001794/**
1795 * @brief Receive a mailbox message.
1796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * This routine receives a message from @a mbox, then optionally retrieves
1798 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @param mbox Address of the mailbox.
1801 * @param rx_msg Address of the receive message descriptor.
1802 * @param buffer Address of the buffer to receive data, or NULL to defer data
1803 * retrieval and message disposal until later.
1804 * @param timeout Waiting period for a message to be received (in
1805 * milliseconds), or one of the special values K_NO_WAIT
1806 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001808 * @retval 0 if successful.
1809 * @retval -ENOMSG if returned without waiting.
1810 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001811 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001812extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001814
1815/**
1816 * @brief Retrieve mailbox message data into a buffer.
1817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * This routine completes the processing of a received message by retrieving
1819 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001820 *
1821 * Alternatively, this routine can be used to dispose of a received message
1822 * without retrieving its data.
1823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * @param rx_msg Address of the receive message descriptor.
1825 * @param buffer Address of the buffer to receive data, or NULL to discard
1826 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04001827 *
1828 * @return N/A
1829 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001830extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001831
1832/**
1833 * @brief Retrieve mailbox message data into a memory pool block.
1834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * This routine completes the processing of a received message by retrieving
1836 * its data into a memory pool block, then disposing of the message.
1837 * The memory pool block that results from successful retrieval must be
1838 * returned to the pool once the data has been processed, even in cases
1839 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04001840 *
1841 * Alternatively, this routine can be used to dispose of a received message
1842 * without retrieving its data. In this case there is no need to return a
1843 * memory pool block to the pool.
1844 *
1845 * This routine allocates a new memory pool block for the data only if the
1846 * data is not already in one. If a new block cannot be allocated, the routine
1847 * returns a failure code and the received message is left unchanged. This
1848 * permits the caller to reattempt data retrieval at a later time or to dispose
1849 * of the received message without retrieving its data.
1850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001851 * @param rx_msg Address of a receive message descriptor.
1852 * @param pool Address of memory pool, or NULL to discard data.
1853 * @param block Address of the area to hold memory pool block info.
1854 * @param timeout Waiting period to wait for a memory pool block (in
1855 * milliseconds), or one of the special values K_NO_WAIT
1856 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @retval 0 if successful.
1859 * @retval -ENOMEM if returned without waiting.
1860 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001861 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001862extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001863 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864 struct k_mem_block *block, int32_t timeout);
1865
1866/* pipes */
1867
1868struct k_pipe {
1869 unsigned char *buffer; /* Pipe buffer: may be NULL */
1870 size_t size; /* Buffer size */
1871 size_t bytes_used; /* # bytes used in buffer */
1872 size_t read_index; /* Where in buffer to read from */
1873 size_t write_index; /* Where in buffer to write */
1874
1875 struct {
1876 _wait_q_t readers; /* Reader wait queue */
1877 _wait_q_t writers; /* Writer wait queue */
1878 } wait_q;
1879
1880 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1881};
1882
Peter Mitsise5d9c582016-10-14 14:44:57 -04001883#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884 { \
1885 .buffer = pipe_buffer, \
1886 .size = pipe_buffer_size, \
1887 .bytes_used = 0, \
1888 .read_index = 0, \
1889 .write_index = 0, \
1890 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1891 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1892 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1893 }
1894
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001898 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 *
1900 * extern struct k_pipe @a name;
1901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001902 * @param name Name of the pipe.
1903 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
1904 * or zero if no ring buffer is used.
1905 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001907#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1908 static unsigned char __noinit __aligned(pipe_align) \
1909 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001910 struct k_pipe name \
1911 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001912 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine initializes a pipe object, prior to its first use.
1918 *
1919 * @param pipe Address of the pipe.
1920 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
1921 * is used.
1922 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
1923 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924 *
1925 * @return N/A
1926 */
1927extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1928 size_t size);
1929
1930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * @param pipe Address of the pipe.
1936 * @param data Address of data to write.
1937 * @param bytes_to_write Size of data (in bytes).
1938 * @param bytes_written Address of area to hold the number of bytes written.
1939 * @param min_xfer Minimum number of bytes to write.
1940 * @param timeout Waiting period to wait for the data to be written (in
1941 * milliseconds), or one of the special values K_NO_WAIT
1942 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * @retval 0 if at least @a min_xfer data bytes were written.
1945 * @retval -EIO if returned without waiting; zero data bytes were written.
1946 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1947 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001949extern int k_pipe_put(struct k_pipe *pipe, void *data,
1950 size_t bytes_to_write, size_t *bytes_written,
1951 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952
1953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001958 * @param pipe Address of the pipe.
1959 * @param data Address to place the data read from pipe.
1960 * @param bytes_to_read Maximum number of data bytes to read.
1961 * @param bytes_read Address of area to hold the number of bytes read.
1962 * @param min_xfer Minimum number of data bytes to read.
1963 * @param timeout Waiting period to wait for the data to be read (in
1964 * milliseconds), or one of the special values K_NO_WAIT
1965 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * @retval 0 if at least @a min_xfer data bytes were read.
1968 * @retval -EIO if returned without waiting; zero data bytes were read.
1969 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1970 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001972extern int k_pipe_get(struct k_pipe *pipe, void *data,
1973 size_t bytes_to_read, size_t *bytes_read,
1974 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975
Peter Mitsis2fef0232016-10-14 14:53:44 -04001976#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * This routine writes the data contained in a memory block to @a pipe.
1981 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985 * @param block Memory block containing data to send
1986 * @param size Number of data bytes in memory block to send
1987 * @param sem Semaphore to signal upon completion (else NULL)
1988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990 */
1991extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1992 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001993#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001994
1995/**
1996 * memory management
1997 */
1998
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001999/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002000
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002001struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002002 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002003 uint32_t num_blocks;
2004 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002005 char *buffer;
2006 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002007 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002008
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002009 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002010};
2011
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002012#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2013 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014 { \
2015 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002016 .num_blocks = slab_num_blocks, \
2017 .block_size = slab_block_size, \
2018 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002019 .free_list = NULL, \
2020 .num_used = 0, \
2021 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2022 }
2023
Peter Mitsis578f9112016-10-07 13:50:31 -04002024/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @brief Statically define and initialize a memory slab allocator.
Peter Mitsis578f9112016-10-07 13:50:31 -04002026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027 * The slab allocator's buffer contains @a slab_num_blocks memory blocks
2028 * that are @a slab_block_size bytes long. The buffer is aligned to a
2029 * @a slab_align -byte boundary. To ensure that each memory block is aligned
2030 * to a @a slab_align -byte boundary, @a slab_block_size must be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002031 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 * The slab allocator can be accessed outside the module where it is defined
2034 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
2036 * extern struct k_mem_slab @a name;
2037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @param name Name of the memory slab.
2039 * @param slab_block_size Size of each memory block (in bytes).
2040 * @param slab_num_blocks Number memory blocks.
2041 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002042 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002043#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2044 char __noinit __aligned(slab_align) \
2045 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2046 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002047 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002048 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2049 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002051/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002052 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @param slab Address of the memory slab.
2057 * @param buffer Pointer to buffer used for the memory blocks.
2058 * @param block_size Size of each memory block (in bytes).
2059 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002060 *
2061 * @return N/A
2062 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002063extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002064 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002065
2066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002067 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * @param slab Address of the memory slab.
2072 * @param mem Pointer to block address area.
2073 * @param timeout Maximum time to wait for operation to complete
2074 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2075 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @retval 0 if successful. The block address area pointed at by @a mem
2078 * is set to the starting address of the memory block.
2079 * @retval -ENOMEM if failed immediately.
2080 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002081 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002082extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2083 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002084
2085/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002086 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * This routine releases a previously allocated memory block back to its
2089 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * @param slab Address of the memory slab.
2092 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002093 *
2094 * @return N/A
2095 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002096extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002097
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * This routine gets the number of memory blocks that are currently
2102 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002107 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002108static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002110 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002111}
2112
Peter Mitsisc001aa82016-10-13 13:53:37 -04002113/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * This routine gets the number of memory blocks that are currently
2117 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002122 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002123static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002124{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002125 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002126}
2127
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128/* memory pools */
2129
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002130/*
2131 * Memory pool requires a buffer and two arrays of structures for the
2132 * memory block accounting:
2133 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2134 * status of four blocks of memory.
2135 */
2136struct k_mem_pool_quad_block {
2137 char *mem_blocks; /* pointer to the first of four memory blocks */
2138 uint32_t mem_status; /* four bits. If bit is set, memory block is
2139 allocated */
2140};
2141/*
2142 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2143 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2144 * block size is 4 times less than the previous one and thus requires 4 times
2145 * bigger array of k_mem_pool_quad_block structures to keep track of the
2146 * memory blocks.
2147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002149/*
2150 * The array of k_mem_pool_block_set keeps the information of each array of
2151 * k_mem_pool_quad_block structures
2152 */
2153struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002154 size_t block_size; /* memory block size */
2155 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002156 struct k_mem_pool_quad_block *quad_block;
2157 int count;
2158};
2159
2160/* Memory pool descriptor */
2161struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002162 size_t max_block_size;
2163 size_t min_block_size;
2164 uint32_t nr_of_maxblocks;
2165 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002166 struct k_mem_pool_block_set *block_set;
2167 char *bufblock;
2168 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2170};
2171
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002172#ifdef CONFIG_ARM
2173#define _SECTION_TYPE_SIGN "%"
2174#else
2175#define _SECTION_TYPE_SIGN "@"
2176#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002178/*
2179 * Static memory pool initialization
2180 */
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002181/**
2182 * @cond internal
2183 * Make Doxygen skip assembler macros
2184 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002185/*
2186 * Use .altmacro to be able to recalculate values and pass them as string
2187 * arguments when calling assembler macros resursively
2188 */
2189__asm__(".altmacro\n\t");
2190
2191/*
2192 * Recursively calls a macro
2193 * The followig global symbols need to be initialized:
2194 * __memory_pool_max_block_size - maximal size of the memory block
2195 * __memory_pool_min_block_size - minimal size of the memory block
2196 * Notes:
2197 * Global symbols are used due the fact that assembler macro allows only
2198 * one argument be passed with the % conversion
2199 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2200 * is used instead of / 4.
2201 * n_max argument needs to go first in the invoked macro, as some
2202 * assemblers concatenate \name and %(\n_max * 4) arguments
2203 * if \name goes first
2204 */
2205__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2206 ".ifge __memory_pool_max_block_size >> 2 -"
2207 " __memory_pool_min_block_size\n\t\t"
2208 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2209 "\\macro_name %(\\n_max * 4) \\name\n\t"
2210 ".endif\n\t"
2211 ".endm\n");
2212
2213/*
2214 * Build quad blocks
2215 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2216 * structures and recursively calls itself for the next array, 4 times
2217 * larger.
2218 * The followig global symbols need to be initialized:
2219 * __memory_pool_max_block_size - maximal size of the memory block
2220 * __memory_pool_min_block_size - minimal size of the memory block
2221 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2222 */
2223__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002224 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002225 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2226 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2227 ".if \\n_max % 4\n\t\t"
2228 ".skip __memory_pool_quad_block_size\n\t"
2229 ".endif\n\t"
2230 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2231 ".endm\n");
2232
2233/*
2234 * Build block sets and initialize them
2235 * Macro initializes the k_mem_pool_block_set structure and
2236 * recursively calls itself for the next one.
2237 * The followig global symbols need to be initialized:
2238 * __memory_pool_max_block_size - maximal size of the memory block
2239 * __memory_pool_min_block_size - minimal size of the memory block
2240 * __memory_pool_block_set_count, the number of the elements in the
2241 * block set array must be set to 0. Macro calculates it's real
2242 * value.
2243 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2244 * structures, _build_quad_blocks must be called prior it.
2245 */
2246__asm__(".macro _build_block_set n_max, name\n\t"
2247 ".int __memory_pool_max_block_size\n\t" /* block_size */
2248 ".if \\n_max % 4\n\t\t"
2249 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2250 ".else\n\t\t"
2251 ".int \\n_max >> 2\n\t"
2252 ".endif\n\t"
2253 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2254 ".int 0\n\t" /* count */
2255 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2256 "__do_recurse _build_block_set \\name \\n_max\n\t"
2257 ".endm\n");
2258
2259/*
2260 * Build a memory pool structure and initialize it
2261 * Macro uses __memory_pool_block_set_count global symbol,
2262 * block set addresses and buffer address, it may be called only after
2263 * _build_block_set
2264 */
2265__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002266 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002267 _SECTION_TYPE_SIGN "progbits\n\t"
2268 ".globl \\name\n\t"
2269 "\\name:\n\t"
2270 ".int \\max_size\n\t" /* max_block_size */
2271 ".int \\min_size\n\t" /* min_block_size */
2272 ".int \\n_max\n\t" /* nr_of_maxblocks */
2273 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2274 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2275 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2276 ".int 0\n\t" /* wait_q->head */
2277 ".int 0\n\t" /* wait_q->next */
2278 ".popsection\n\t"
2279 ".endm\n");
2280
2281#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2282 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2283 _SECTION_TYPE_SIGN "progbits\n\t"); \
2284 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2285 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2286 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2287 STRINGIFY(name) "\n\t"); \
2288 __asm__(".popsection\n\t")
2289
2290#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2291 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2292 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2293 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2294 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002295 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002296 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2297 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2298 STRINGIFY(name) "\n\t"); \
2299 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2300 __asm__(".int __memory_pool_block_set_count\n\t"); \
2301 __asm__(".popsection\n\t"); \
2302 extern uint32_t _mem_pool_block_set_count_##name; \
2303 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2304
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002305#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2306 char __noinit __aligned(align) \
2307 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002308
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002309/*
2310 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2311 * to __memory_pool_quad_block_size absolute symbol.
2312 * This function does not get called, but compiler calculates the value and
2313 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2314 */
2315static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2316{
2317 __asm__(".globl __memory_pool_quad_block_size\n\t"
2318#ifdef CONFIG_NIOS2
2319 "__memory_pool_quad_block_size = %0\n\t"
2320#else
2321 "__memory_pool_quad_block_size = %c0\n\t"
2322#endif
2323 :
2324 : "n"(sizeof(struct k_mem_pool_quad_block)));
2325}
2326
2327/**
2328 * @endcond
2329 * End of assembler macros that Doxygen has to skip
2330 */
2331
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002332/**
2333 * @brief Define a memory pool
2334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * This defines and initializes a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2338 * long. The memory pool allows blocks to be repeatedly partitioned into
2339 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2340 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
2341 * aligned to @a align -byte boundary, @a min_size must be a multiple of
2342 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002343 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344 * If the pool is to be accessed outside the module where it is defined, it
2345 * can be declared via
2346 *
2347 * extern struct k_mem_pool @a name;
2348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * @param name Name of the memory pool.
2350 * @param min_size Size of the smallest blocks in the pool (in bytes).
2351 * @param max_size Size of the largest blocks in the pool (in bytes).
2352 * @param n_max Number of maximum sized blocks in the pool.
2353 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002354 */
2355#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002356 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2357 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002358 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002359 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2360 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2361 extern struct k_mem_pool name
2362
Peter Mitsis937042c2016-10-13 13:18:26 -04002363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002364 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * @param pool Address of the memory pool.
2369 * @param block Pointer to block descriptor for the allocated memory.
2370 * @param size Amount of memory to allocate (in bytes).
2371 * @param timeout Maximum time to wait for operation to complete
2372 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2373 * or K_FOREVER to wait as long as necessary.
2374 *
2375 * @retval 0 if successful. The @a data field of the block descriptor
2376 * is set to the starting address of the memory block.
2377 * @retval -ENOMEM if unable to allocate a memory block.
2378 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002379 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002380extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002381 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002382
2383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002386 * This routine releases a previously allocated memory block back to its
2387 * memory pool.
2388 *
2389 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002390 *
2391 * @return N/A
2392 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002394
2395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * This routine instructs a memory pool to concatenate unused memory blocks
2399 * into larger blocks wherever possible. Manually defragmenting the memory
2400 * pool may speed up future allocations of memory blocks by eliminating the
2401 * need for the memory pool to perform an automatic partial defragmentation.
2402 *
2403 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002404 *
2405 * @return N/A
2406 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002407extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002408
2409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002413 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002418 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002419extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002420
2421/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002423 *
2424 * This routine provides traditional free() semantics. The memory being
2425 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002428 *
2429 * @return N/A
2430 */
2431extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432
2433/*
2434 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2435 * hook into the device subsystem, which itself uses nanokernel semaphores,
2436 * and thus currently requires the definition of nano_sem.
2437 */
2438#include <legacy.h>
2439#include <arch/cpu.h>
2440
2441/*
2442 * private APIs that are utilized by one or more public APIs
2443 */
2444
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002447extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002448
2449#ifdef __cplusplus
2450}
2451#endif
2452
Andrew Boiee004dec2016-11-07 09:01:19 -08002453#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2454/*
2455 * Define new and delete operators.
2456 * At this moment, the operators do nothing since objects are supposed
2457 * to be statically allocated.
2458 */
2459inline void operator delete(void *ptr)
2460{
2461 (void)ptr;
2462}
2463
2464inline void operator delete[](void *ptr)
2465{
2466 (void)ptr;
2467}
2468
2469inline void *operator new(size_t size)
2470{
2471 (void)size;
2472 return NULL;
2473}
2474
2475inline void *operator new[](size_t size)
2476{
2477 (void)size;
2478 return NULL;
2479}
2480
2481/* Placement versions of operator new and delete */
2482inline void operator delete(void *ptr1, void *ptr2)
2483{
2484 (void)ptr1;
2485 (void)ptr2;
2486}
2487
2488inline void operator delete[](void *ptr1, void *ptr2)
2489{
2490 (void)ptr1;
2491 (void)ptr2;
2492}
2493
2494inline void *operator new(size_t size, void *ptr)
2495{
2496 (void)size;
2497 return ptr;
2498}
2499
2500inline void *operator new[](size_t size, void *ptr)
2501{
2502 (void)size;
2503 return ptr;
2504}
2505
2506#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2507
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508#endif /* _kernel__h_ */