blob: 3b940b94d6a49766907a69e72030ef447334e9a5 [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
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500426 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500428 * Custom data is not used by the kernel itself, and is freely available
429 * for a thread to use as it sees fit. It can be used as a framework
430 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500432 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400433 *
434 * @return N/A
435 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400436extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437
438/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500439 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500441 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500443 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445extern void *k_thread_custom_data_get(void);
446
447/**
448 * kernel timing
449 */
450
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400451#include <sys_clock.h>
452
453/* private internal time manipulation (users should never play with ticks) */
454
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500455/* added tick needed to account for tick in progress */
456#define _TICK_ALIGN 1
457
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400458static int64_t __ticks_to_ms(int64_t ticks)
459{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400460#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400461 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400462#else
463 __ASSERT(ticks == 0, "");
464 return 0;
465#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400466}
467
468
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400469/* timeouts */
470
471struct _timeout;
472typedef void (*_timeout_func_t)(struct _timeout *t);
473
474struct _timeout {
475 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400476 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400477 sys_dlist_t *wait_q;
478 int32_t delta_ticks_from_prev;
479 _timeout_func_t func;
480};
481
Allan Stephens45bfa372016-10-12 12:39:42 -0500482
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400483/* timers */
484
485struct k_timer {
486 /*
487 * _timeout structure must be first here if we want to use
488 * dynamic timer allocation. timeout.node is used in the double-linked
489 * list of free timers
490 */
491 struct _timeout timeout;
492
Allan Stephens45bfa372016-10-12 12:39:42 -0500493 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400494 _wait_q_t wait_q;
495
496 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500497 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400498
499 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500500 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400501
502 /* timer period */
503 int32_t period;
504
Allan Stephens45bfa372016-10-12 12:39:42 -0500505 /* timer status */
506 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400507
Allan Stephens45bfa372016-10-12 12:39:42 -0500508 /* used to support legacy timer APIs */
509 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400510
511 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
512};
513
Allan Stephens1342adb2016-11-03 13:54:53 -0500514#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500516 .timeout.delta_ticks_from_prev = -1, \
517 .timeout.wait_q = NULL, \
518 .timeout.thread = NULL, \
519 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400520 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500521 .expiry_fn = expiry, \
522 .stop_fn = stop, \
523 .status = 0, \
524 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400525 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
526 }
527
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532 *
533 * extern struct k_timer @a name;
534 *
535 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500536 * @param expiry_fn Function to invoke each time the timer expires.
537 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500539#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500540 struct k_timer name \
541 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500542 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400543
Allan Stephens45bfa372016-10-12 12:39:42 -0500544/**
545 * @brief Initialize a timer.
546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500547 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500548 *
549 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500550 * @param expiry_fn Function to invoke each time the timer expires.
551 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500552 *
553 * @return N/A
554 */
555extern void k_timer_init(struct k_timer *timer,
556 void (*expiry_fn)(struct k_timer *),
557 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700558
Allan Stephens45bfa372016-10-12 12:39:42 -0500559/**
560 * @brief Start a timer.
561 *
562 * This routine starts a timer, and resets its status to zero. The timer
563 * begins counting down using the specified duration and period values.
564 *
565 * Attempting to start a timer that is already running is permitted.
566 * The timer's status is reset to zero and the timer begins counting down
567 * using the new duration and period values.
568 *
569 * @param timer Address of timer.
570 * @param duration Initial timer duration (in milliseconds).
571 * @param period Timer period (in milliseconds).
572 *
573 * @return N/A
574 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400575extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500576 int32_t duration, int32_t period);
577
578/**
579 * @brief Stop a timer.
580 *
581 * This routine stops a running timer prematurely. The timer's stop function,
582 * if one exists, is invoked by the caller.
583 *
584 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500585 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500586 *
587 * @param timer Address of timer.
588 *
589 * @return N/A
590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500592
593/**
594 * @brief Read timer status.
595 *
596 * This routine reads the timer's status, which indicates the number of times
597 * it has expired since its status was last read.
598 *
599 * Calling this routine resets the timer's status to zero.
600 *
601 * @param timer Address of timer.
602 *
603 * @return Timer status.
604 */
605extern uint32_t k_timer_status_get(struct k_timer *timer);
606
607/**
608 * @brief Synchronize thread to timer expiration.
609 *
610 * This routine blocks the calling thread until the timer's status is non-zero
611 * (indicating that it has expired at least once since it was last examined)
612 * or the timer is stopped. If the timer status is already non-zero,
613 * or the timer is already stopped, the caller continues without waiting.
614 *
615 * Calling this routine resets the timer's status to zero.
616 *
617 * This routine must not be used by interrupt handlers, since they are not
618 * allowed to block.
619 *
620 * @param timer Address of timer.
621 *
622 * @return Timer status.
623 */
624extern uint32_t k_timer_status_sync(struct k_timer *timer);
625
626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500627 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500628 *
629 * This routine computes the (approximate) time remaining before a running
630 * timer next expires. If the timer is not running, it returns zero.
631 *
632 * @param timer Address of timer.
633 *
634 * @return Remaining time (in milliseconds).
635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400636extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400637
638
Allan Stephens45bfa372016-10-12 12:39:42 -0500639/* kernel clocks */
640
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400641/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500644 * This routine returns the elapsed time since the system booted,
645 * in milliseconds.
646 *
647 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400648 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400649extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400650
651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500652 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500654 * This routine returns the lower 32-bits of the elapsed time since the system
655 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500657 * This routine can be more efficient than k_uptime_get(), as it reduces the
658 * need for interrupt locking and 64-bit math. However, the 32-bit result
659 * cannot hold a system uptime time larger than approximately 50 days, so the
660 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500662 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400663 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400664extern uint32_t k_uptime_get_32(void);
665
666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500667 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500669 * This routine computes the elapsed time between the current system uptime
670 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * @param reftime Pointer to a reference time, which is updated to the current
673 * uptime upon return.
674 *
675 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400676 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400677extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400678
679/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500680 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500682 * This routine computes the elapsed time between the current system uptime
683 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * This routine can be more efficient than k_uptime_delta(), as it reduces the
686 * need for interrupt locking and 64-bit math. However, the 32-bit result
687 * cannot hold an elapsed time larger than approximately 50 days, so the
688 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * @param reftime Pointer to a reference time, which is updated to the current
691 * uptime upon return.
692 *
693 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400694 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400695extern uint32_t k_uptime_delta_32(int64_t *reftime);
696
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500700 * This routine returns the current time, as measured by the system's hardware
701 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500703 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400705extern uint32_t k_cycle_get_32(void);
706
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400707/**
708 * data transfers (basic)
709 */
710
711/* fifos */
712
713struct k_fifo {
714 _wait_q_t wait_q;
715 sys_slist_t data_q;
716
717 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
718};
719
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500721 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500723 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400726 *
727 * @return N/A
728 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400729extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400730
731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500734 * This routine adds a data item to @a fifo. A fifo data item must be
735 * aligned on a 4-byte boundary, and the first 32 bits of the item are
736 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500738 * @note Can be called by ISRs.
739 *
740 * @param fifo Address of the fifo.
741 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742 *
743 * @return N/A
744 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400745extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400746
747/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500748 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * This routine adds a list of data items to @a fifo in one operation.
751 * The data items must be in a singly-linked list, with the first 32 bits
752 * each data item pointing to the next data item; the list must be
753 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * @param fifo Address of the fifo.
758 * @param head Pointer to first node in singly-linked list.
759 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760 *
761 * @return N/A
762 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764
765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * This routine adds a list of data items to @a fifo in one operation.
769 * The data items must be in a singly-linked list implemented using a
770 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 * and must be re-initialized via sys_slist_init().
772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @note Can be called by ISRs.
774 *
775 * @param fifo Address of the fifo.
776 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 *
778 * @return N/A
779 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400780extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781
782/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500785 * This routine removes a data item from @a fifo in a "first in, first out"
786 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
789 *
790 * @param fifo Address of the fifo.
791 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 * or one of the special values K_NO_WAIT and K_FOREVER.
793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @return Address of the data item if successful.
795 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400797extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
798
799#define K_FIFO_INITIALIZER(obj) \
800 { \
801 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400802 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400803 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
804 }
805
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
811 * extern struct k_fifo @a name;
812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400815#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500816 struct k_fifo name \
817 __in_section(_k_fifo, static, name) = \
818 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400819
820/* lifos */
821
822struct k_lifo {
823 _wait_q_t wait_q;
824 void *list;
825
826 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
827};
828
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
836 * @return N/A
837 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400838extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839
840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * This routine adds a data item to @a lifo. A lifo data item must be
844 * aligned on a 4-byte boundary, and the first 32 bits of the item are
845 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * @note Can be called by ISRs.
848 *
849 * @param lifo Address of the lifo.
850 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
852 * @return N/A
853 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400854extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855
856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * This routine removes a data item from @a lifo in a "last in, first out"
860 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
863 *
864 * @param lifo Address of the lifo.
865 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 * or one of the special values K_NO_WAIT and K_FOREVER.
867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * @return Address of the data item if successful.
869 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400871extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
872
873#define K_LIFO_INITIALIZER(obj) \
874 { \
875 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
876 .list = NULL, \
877 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
878 }
879
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
885 * extern struct k_lifo @a name;
886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500890 struct k_lifo name \
891 __in_section(_k_lifo, static, name) = \
892 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400893
894/* stacks */
895
896struct k_stack {
897 _wait_q_t wait_q;
898 uint32_t *base, *next, *top;
899
900 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
901};
902
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500903/**
904 * @brief Initialize a stack.
905 *
906 * This routine initializes a stack object, prior to its first use.
907 *
908 * @param stack Address of the stack.
909 * @param buffer Address of array used to hold stacked values.
910 * @param num_entries Maximum number of values that can be stacked.
911 *
912 * @return N/A
913 */
Allan Stephens018cd9a2016-10-07 15:13:24 -0500914extern void k_stack_init(struct k_stack *stack,
915 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916
917/**
918 * @brief Push an element onto a stack.
919 *
920 * This routine adds a 32-bit value @a data to @a stack.
921 *
922 * @note Can be called by ISRs.
923 *
924 * @param stack Address of the stack.
925 * @param data Value to push onto the stack.
926 *
927 * @return N/A
928 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400929extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930
931/**
932 * @brief Pop an element from a stack.
933 *
934 * This routine removes a 32-bit value from @a stack in a "last in, first out"
935 * manner and stores the value in @a data.
936 *
937 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
938 *
939 * @param stack Address of the stack.
940 * @param data Address of area to hold the value popped from the stack.
941 * @param timeout Waiting period to obtain a value (in milliseconds),
942 * or one of the special values K_NO_WAIT and K_FOREVER.
943 *
944 * @retval 0 if successful.
945 * @retval -EBUSY if returned without waiting.
946 * @retval -EAGAIN if waiting period timed out.
947 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400948extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
949
Peter Mitsis602e6a82016-10-17 11:48:43 -0400950#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400951 { \
952 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
953 .base = stack_buffer, \
954 .next = stack_buffer, \
955 .top = stack_buffer + stack_num_entries, \
956 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
957 }
958
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963 *
964 * extern struct k_stack @a name;
965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500966 * @param name Name of the stack.
967 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968 */
Peter Mitsis602e6a82016-10-17 11:48:43 -0400969#define K_STACK_DEFINE(name, stack_num_entries) \
970 uint32_t __noinit \
971 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500972 struct k_stack name \
973 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -0400974 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
975 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400976
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977/**
978 * workqueues
979 */
980
981struct k_work;
982
983typedef void (*k_work_handler_t)(struct k_work *);
984
985/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400987 * queued to it. This is useful for drivers which need to schedule
988 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400990 * space.
991 */
992struct k_work_q {
993 struct k_fifo fifo;
994};
995
996/**
997 * @brief Work flags.
998 */
999enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001000 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001001};
1002
1003/**
1004 * @brief An item which can be scheduled on a @ref k_work_q.
1005 */
1006struct k_work {
1007 void *_reserved; /* Used by k_fifo implementation. */
1008 k_work_handler_t handler;
1009 atomic_t flags[1];
1010};
1011
1012/**
1013 * @brief Statically initialize work item
1014 */
1015#define K_WORK_INITIALIZER(work_handler) \
1016 { \
1017 ._reserved = NULL, \
1018 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001019 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020 }
1021
1022/**
1023 * @brief Dynamically initialize work item
1024 */
1025static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1026{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001027 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001028 work->handler = handler;
1029}
1030
1031/**
1032 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001033 *
1034 * This procedure schedules a work item to be processed.
1035 * In the case where the work item has already been submitted and is pending
1036 * execution, calling this function will result in a no-op. In this case, the
1037 * work item must not be modified externally (e.g. by the caller of this
1038 * function), since that could cause the work item to be processed in a
1039 * corrupted state.
1040 *
1041 * @param work_q to schedule the work item
1042 * @param work work item
1043 *
1044 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001045 */
1046static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1047 struct k_work *work)
1048{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001049 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001050 k_fifo_put(&work_q->fifo, work);
1051 }
1052}
1053
1054/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001055 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001056 *
1057 * @param work Work item to query
1058 *
1059 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001060 */
1061static inline int k_work_pending(struct k_work *work)
1062{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001063 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001064}
1065
1066/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001067 * @brief Start a new workqueue.
1068 *
1069 * This routine must not be called from an ISR.
1070 *
1071 * @param work_q Pointer to Work queue
1072 * @param stack Pointer to work queue thread's stack
1073 * @param stack_size Size of the work queue thread's stack
1074 * @param prio Priority of the work queue's thread
1075 *
1076 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077 */
Allan Stephens904cf972016-10-07 13:59:23 -05001078extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1079 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001081#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083/**
1084 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085 */
1086struct k_delayed_work {
1087 struct k_work work;
1088 struct _timeout timeout;
1089 struct k_work_q *work_q;
1090};
1091
1092/**
1093 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
1095 * Initialize a delayed work item.
1096 *
1097 * @param work Delayed work item
1098 * @param handler Routine invoked when processing delayed work item
1099 *
1100 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001101 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001102extern void k_delayed_work_init(struct k_delayed_work *work,
1103 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001104
1105/**
1106 * @brief Submit a delayed work item to a workqueue.
1107 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109 * Once the delay has passed, the work item is submitted to the work queue:
1110 * at this point, it is no longer possible to cancel it. Once the work item's
1111 * handler is about to be executed, the work is considered complete and can be
1112 * resubmitted.
1113 *
1114 * Care must be taken if the handler blocks or yield as there is no implicit
1115 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1116 * it should be explicitly done between the submitter and the handler.
1117 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001118 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001120 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001123 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001124extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1125 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001126 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
1128/**
1129 * @brief Cancel a delayed work item
1130 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001132 * or is idle, this will do nothing. The only case where this can fail is when
1133 * the work has been submitted to the work queue, but the handler has not run
1134 * yet.
1135 *
1136 * @param work Delayed work item to be canceled
1137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001139 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001140extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001142#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001143
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001144extern struct k_work_q k_sys_work_q;
1145
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147 * @brief Submit a work item to the system workqueue.
1148 *
1149 * @ref k_work_submit_to_queue
1150 *
1151 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001153 * unexpected behavior.
1154 */
1155static inline void k_work_submit(struct k_work *work)
1156{
1157 k_work_submit_to_queue(&k_sys_work_q, work);
1158}
1159
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001160#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001162 * @brief Submit a delayed work item to the system workqueue.
1163 *
1164 * @ref k_delayed_work_submit_to_queue
1165 *
1166 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001168 * unexpected behavior.
1169 */
1170static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001171 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001172{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001173 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174}
1175
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001176#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177
1178/**
1179 * synchronization
1180 */
1181
1182/* mutexes */
1183
1184struct k_mutex {
1185 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001186 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187 uint32_t lock_count;
1188 int owner_orig_prio;
1189#ifdef CONFIG_OBJECT_MONITOR
1190 int num_lock_state_changes;
1191 int num_conflicts;
1192#endif
1193
1194 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1195};
1196
1197#ifdef CONFIG_OBJECT_MONITOR
1198#define _MUTEX_INIT_OBJECT_MONITOR \
1199 .num_lock_state_changes = 0, .num_conflicts = 0,
1200#else
1201#define _MUTEX_INIT_OBJECT_MONITOR
1202#endif
1203
1204#define K_MUTEX_INITIALIZER(obj) \
1205 { \
1206 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1207 .owner = NULL, \
1208 .lock_count = 0, \
1209 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1210 _MUTEX_INIT_OBJECT_MONITOR \
1211 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1212 }
1213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
1219 * extern struct k_mutex @a name;
1220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001223#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001224 struct k_mutex name \
1225 __in_section(_k_mutex, static, name) = \
1226 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * Upon completion, the mutex is available and does not have an owner.
1234 *
1235 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
1237 * @return N/A
1238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001239extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240
1241/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * This routine locks @a mutex. If the mutex is locked by another thread,
1245 * the calling thread waits until the mutex becomes available or until
1246 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001248 * A thread is permitted to lock a mutex it has already locked. The operation
1249 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * @param mutex Address of the mutex.
1252 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001253 * or one of the special values K_NO_WAIT and K_FOREVER.
1254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001255 * @retval 0 if successful.
1256 * @retval -EBUSY if returned without waiting.
1257 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001259extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260
1261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001264 * This routine unlocks @a mutex. The mutex must already be locked by the
1265 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 *
1267 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 * thread.
1270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001271 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 *
1273 * @return N/A
1274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275extern void k_mutex_unlock(struct k_mutex *mutex);
1276
1277/* semaphores */
1278
1279struct k_sem {
1280 _wait_q_t wait_q;
1281 unsigned int count;
1282 unsigned int limit;
1283
1284 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1285};
1286
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @param sem Address of the semaphore.
1293 * @param initial_count Initial semaphore count.
1294 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001295 *
1296 * @return N/A
1297 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001298extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1299 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001300
1301/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001302 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001304 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001306 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1307 *
1308 * @param sem Address of the semaphore.
1309 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001310 * or one of the special values K_NO_WAIT and K_FOREVER.
1311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001312 * @retval 0 if successful.
1313 * @retval -EBUSY if returned without waiting.
1314 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001315 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001317
1318/**
1319 * @brief Give a semaphore.
1320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * This routine gives @a sem, unless the semaphore is already at its maximum
1322 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001324 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001327 *
1328 * @return N/A
1329 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330extern void k_sem_give(struct k_sem *sem);
1331
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001332/**
1333 * @brief Reset a semaphore's count to zero.
1334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001335 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001337 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001338 *
1339 * @return N/A
1340 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001341static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001342{
1343 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344}
1345
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001346/**
1347 * @brief Get a semaphore's count.
1348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001351 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001354 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001355static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001356{
1357 return sem->count;
1358}
1359
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001360#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1361 { \
1362 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1363 .count = initial_count, \
1364 .limit = count_limit, \
1365 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1366 }
1367
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001369 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001371 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001372 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001373 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001375 * @param name Name of the semaphore.
1376 * @param initial_count Initial semaphore count.
1377 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001378 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001380 struct k_sem name \
1381 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382 K_SEM_INITIALIZER(name, initial_count, count_limit)
1383
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001384/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001385
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001386#define K_ALERT_DEFAULT NULL
1387#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001389typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001391struct k_alert {
1392 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393 atomic_t send_count;
1394 struct k_work work_item;
1395 struct k_sem sem;
1396
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001397 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398};
1399
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001400extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001402#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001404 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001406 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001407 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1409 }
1410
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001414 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001415 *
1416 * extern struct k_alert @a name;
1417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001418 * @param name Name of the alert.
1419 * @param alert_handler Action to take when alert is sent. Specify either
1420 * the address of a function to be invoked by the system workqueue
1421 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1422 * K_ALERT_DEFAULT (which causes the alert to pend).
1423 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001424 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001425#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001426 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001427 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001428 K_ALERT_INITIALIZER(name, alert_handler, \
1429 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001431/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001432 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001434 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001436 * @param alert Address of the alert.
1437 * @param handler Action to take when alert is sent. Specify either the address
1438 * of a function to be invoked by the system workqueue thread,
1439 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1440 * K_ALERT_DEFAULT (which causes the alert to pend).
1441 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442 *
1443 * @return N/A
1444 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001445extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1446 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001447
1448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001449 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1454 *
1455 * @param alert Address of the alert.
1456 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457 * or one of the special values K_NO_WAIT and K_FOREVER.
1458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * @retval 0 if successful.
1460 * @retval -EBUSY if returned without waiting.
1461 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001462 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001463extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001464
1465/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * This routine signals @a alert. The action specified for @a alert will
1469 * be taken, which may trigger the execution of an alert handler function
1470 * and/or cause the alert to pend (assuming the alert has not reached its
1471 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001473 * @note Can be called by ISRs.
1474 *
1475 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 *
1477 * @return N/A
1478 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001479extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480
1481/**
1482 * data transfers (complex)
1483 */
1484
1485/* message queues */
1486
1487struct k_msgq {
1488 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001489 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001490 uint32_t max_msgs;
1491 char *buffer_start;
1492 char *buffer_end;
1493 char *read_ptr;
1494 char *write_ptr;
1495 uint32_t used_msgs;
1496
1497 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1498};
1499
Peter Mitsis1da807e2016-10-06 11:36:59 -04001500#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501 { \
1502 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001503 .max_msgs = q_max_msgs, \
1504 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001506 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507 .read_ptr = q_buffer, \
1508 .write_ptr = q_buffer, \
1509 .used_msgs = 0, \
1510 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1511 }
1512
Peter Mitsis1da807e2016-10-06 11:36:59 -04001513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001516 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1517 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
1518 * @a q_align -byte boundary. To ensure that each message is aligned to a
1519 * @a q_align -byte boundary, @a q_msg_size must be a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001521 * The message queue can be accessed outside the module where it is defined
1522 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001523 *
1524 * extern struct k_msgq @a name;
1525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * @param q_name Name of the message queue.
1527 * @param q_msg_size Message size (in bytes).
1528 * @param q_max_msgs Maximum number of messages that can be queued.
1529 * @param q_align Alignment of the message queue's ring buffer (power of 2).
Peter Mitsis1da807e2016-10-06 11:36:59 -04001530 */
1531#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1532 static char __noinit __aligned(q_align) \
1533 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001534 struct k_msgq q_name \
1535 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001536 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1537 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538
Peter Mitsisd7a37502016-10-13 11:37:40 -04001539/**
1540 * @brief Initialize a message queue.
1541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001542 * This routine initializes a message queue object, prior to its first use.
1543 *
1544 * @param q Address of the message queue.
1545 * @param buffer Pointer to ring buffer that holds queued messages.
1546 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001547 * @param max_msgs Maximum number of messages that can be queued.
1548 *
1549 * @return N/A
1550 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001551extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001552 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001553
1554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * @param q Address of the message queue.
1560 * @param data Pointer to the message.
1561 * @param timeout Waiting period to add the message (in milliseconds),
1562 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001564 * @retval 0 if successful.
1565 * @retval -ENOMSG if returned without waiting or after a queue purge.
1566 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001567 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001569
1570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001571 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * This routine receives a message from message queue @a q in a "first in,
1574 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001576 * @param q Address of the message queue.
1577 * @param data Address of area to hold the received message.
1578 * @param timeout Waiting period to receive the message (in milliseconds),
1579 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001581 * @retval 0 if successful.
1582 * @retval -ENOMSG if returned without waiting.
1583 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001586
1587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * This routine discards all unreceived messages in a message queue's ring
1591 * buffer. Any threads that are blocked waiting to send a message to the
1592 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001595 *
1596 * @return N/A
1597 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001598extern void k_msgq_purge(struct k_msgq *q);
1599
Peter Mitsis67be2492016-10-07 11:44:34 -04001600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * This routine returns the number of unused entries in a message queue's
1604 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * @param q Address of the message queue.
1607 *
1608 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001609 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001610static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001611{
1612 return q->max_msgs - q->used_msgs;
1613}
1614
Peter Mitsisd7a37502016-10-13 11:37:40 -04001615/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001618 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @param q Address of the message queue.
1621 *
1622 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001623 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001624static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625{
1626 return q->used_msgs;
1627}
1628
1629struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001630 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631 void *addr_in_pool;
1632 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001633 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634};
1635
1636/* mailboxes */
1637
1638struct k_mbox_msg {
1639 /** internal use only - needed for legacy API support */
1640 uint32_t _mailbox;
1641 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001642 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643 /** application-defined information value */
1644 uint32_t info;
1645 /** sender's message data buffer */
1646 void *tx_data;
1647 /** internal use only - needed for legacy API support */
1648 void *_rx_data;
1649 /** message data block descriptor */
1650 struct k_mem_block tx_block;
1651 /** source thread id */
1652 k_tid_t rx_source_thread;
1653 /** target thread id */
1654 k_tid_t tx_target_thread;
1655 /** internal use only - thread waiting on send (may be a dummy) */
1656 k_tid_t _syncing_thread;
1657#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1658 /** internal use only - semaphore used during asynchronous send */
1659 struct k_sem *_async_sem;
1660#endif
1661};
1662
1663struct k_mbox {
1664 _wait_q_t tx_msg_queue;
1665 _wait_q_t rx_msg_queue;
1666
1667 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1668};
1669
1670#define K_MBOX_INITIALIZER(obj) \
1671 { \
1672 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1673 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1674 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1675 }
1676
Peter Mitsis12092702016-10-14 12:57:23 -04001677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001680 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001681 *
1682 * extern struct k_mbox @a name;
1683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001685 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001686#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001687 struct k_mbox name \
1688 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001689 K_MBOX_INITIALIZER(name) \
1690
Peter Mitsis12092702016-10-14 12:57:23 -04001691/**
1692 * @brief Initialize a mailbox.
1693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * This routine initializes a mailbox object, prior to its first use.
1695 *
1696 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001697 *
1698 * @return N/A
1699 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700extern void k_mbox_init(struct k_mbox *mbox);
1701
Peter Mitsis12092702016-10-14 12:57:23 -04001702/**
1703 * @brief Send a mailbox message in a synchronous manner.
1704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * This routine sends a message to @a mbox and waits for a receiver to both
1706 * receive and process it. The message data may be in a buffer, in a memory
1707 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * @param mbox Address of the mailbox.
1710 * @param tx_msg Address of the transmit message descriptor.
1711 * @param timeout Waiting period for the message to be received (in
1712 * milliseconds), or one of the special values K_NO_WAIT
1713 * and K_FOREVER. Once the message has been received,
1714 * this routine waits as long as necessary for the message
1715 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04001716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * @retval 0 if successful.
1718 * @retval -ENOMSG if returned without waiting.
1719 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001720 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001721extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001722 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001723
1724#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1725/**
1726 * @brief Send a mailbox message in an asynchronous manner.
1727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * This routine sends a message to @a mbox without waiting for a receiver
1729 * to process it. The message data may be in a buffer, in a memory pool block,
1730 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
1731 * will be given when the message has been both received and completely
1732 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @param mbox Address of the mailbox.
1735 * @param tx_msg Address of the transmit message descriptor.
1736 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04001737 *
1738 * @return N/A
1739 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001740extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001742#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743
Peter Mitsis12092702016-10-14 12:57:23 -04001744/**
1745 * @brief Receive a mailbox message.
1746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * This routine receives a message from @a mbox, then optionally retrieves
1748 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @param mbox Address of the mailbox.
1751 * @param rx_msg Address of the receive message descriptor.
1752 * @param buffer Address of the buffer to receive data, or NULL to defer data
1753 * retrieval and message disposal until later.
1754 * @param timeout Waiting period for a message to be received (in
1755 * milliseconds), or one of the special values K_NO_WAIT
1756 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @retval 0 if successful.
1759 * @retval -ENOMSG if returned without waiting.
1760 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001761 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001762extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001763 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001764
1765/**
1766 * @brief Retrieve mailbox message data into a buffer.
1767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * This routine completes the processing of a received message by retrieving
1769 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001770 *
1771 * Alternatively, this routine can be used to dispose of a received message
1772 * without retrieving its data.
1773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * @param rx_msg Address of the receive message descriptor.
1775 * @param buffer Address of the buffer to receive data, or NULL to discard
1776 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04001777 *
1778 * @return N/A
1779 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001780extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001781
1782/**
1783 * @brief Retrieve mailbox message data into a memory pool block.
1784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * This routine completes the processing of a received message by retrieving
1786 * its data into a memory pool block, then disposing of the message.
1787 * The memory pool block that results from successful retrieval must be
1788 * returned to the pool once the data has been processed, even in cases
1789 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04001790 *
1791 * Alternatively, this routine can be used to dispose of a received message
1792 * without retrieving its data. In this case there is no need to return a
1793 * memory pool block to the pool.
1794 *
1795 * This routine allocates a new memory pool block for the data only if the
1796 * data is not already in one. If a new block cannot be allocated, the routine
1797 * returns a failure code and the received message is left unchanged. This
1798 * permits the caller to reattempt data retrieval at a later time or to dispose
1799 * of the received message without retrieving its data.
1800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * @param rx_msg Address of a receive message descriptor.
1802 * @param pool Address of memory pool, or NULL to discard data.
1803 * @param block Address of the area to hold memory pool block info.
1804 * @param timeout Waiting period to wait for a memory pool block (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 -ENOMEM 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_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001813 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814 struct k_mem_block *block, int32_t timeout);
1815
1816/* pipes */
1817
1818struct k_pipe {
1819 unsigned char *buffer; /* Pipe buffer: may be NULL */
1820 size_t size; /* Buffer size */
1821 size_t bytes_used; /* # bytes used in buffer */
1822 size_t read_index; /* Where in buffer to read from */
1823 size_t write_index; /* Where in buffer to write */
1824
1825 struct {
1826 _wait_q_t readers; /* Reader wait queue */
1827 _wait_q_t writers; /* Writer wait queue */
1828 } wait_q;
1829
1830 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1831};
1832
Peter Mitsise5d9c582016-10-14 14:44:57 -04001833#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001834 { \
1835 .buffer = pipe_buffer, \
1836 .size = pipe_buffer_size, \
1837 .bytes_used = 0, \
1838 .read_index = 0, \
1839 .write_index = 0, \
1840 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1841 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1842 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1843 }
1844
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001846 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849 *
1850 * extern struct k_pipe @a name;
1851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @param name Name of the pipe.
1853 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
1854 * or zero if no ring buffer is used.
1855 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001857#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1858 static unsigned char __noinit __aligned(pipe_align) \
1859 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001860 struct k_pipe name \
1861 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001862 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001863
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * This routine initializes a pipe object, prior to its first use.
1868 *
1869 * @param pipe Address of the pipe.
1870 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
1871 * is used.
1872 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
1873 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001874 *
1875 * @return N/A
1876 */
1877extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1878 size_t size);
1879
1880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001883 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001885 * @param pipe Address of the pipe.
1886 * @param data Address of data to write.
1887 * @param bytes_to_write Size of data (in bytes).
1888 * @param bytes_written Address of area to hold the number of bytes written.
1889 * @param min_xfer Minimum number of bytes to write.
1890 * @param timeout Waiting period to wait for the data to be written (in
1891 * milliseconds), or one of the special values K_NO_WAIT
1892 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * @retval 0 if at least @a min_xfer data bytes were written.
1895 * @retval -EIO if returned without waiting; zero data bytes were written.
1896 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1897 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001898 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001899extern int k_pipe_put(struct k_pipe *pipe, void *data,
1900 size_t bytes_to_write, size_t *bytes_written,
1901 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
1903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001904 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001906 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001908 * @param pipe Address of the pipe.
1909 * @param data Address to place the data read from pipe.
1910 * @param bytes_to_read Maximum number of data bytes to read.
1911 * @param bytes_read Address of area to hold the number of bytes read.
1912 * @param min_xfer Minimum number of data bytes to read.
1913 * @param timeout Waiting period to wait for the data to be read (in
1914 * milliseconds), or one of the special values K_NO_WAIT
1915 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * @retval 0 if at least @a min_xfer data bytes were read.
1918 * @retval -EIO if returned without waiting; zero data bytes were read.
1919 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1920 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001922extern int k_pipe_get(struct k_pipe *pipe, void *data,
1923 size_t bytes_to_read, size_t *bytes_read,
1924 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001925
Peter Mitsis2fef0232016-10-14 14:53:44 -04001926#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001928 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * This routine writes the data contained in a memory block to @a pipe.
1931 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935 * @param block Memory block containing data to send
1936 * @param size Number of data bytes in memory block to send
1937 * @param sem Semaphore to signal upon completion (else NULL)
1938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940 */
1941extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1942 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001943#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944
1945/**
1946 * memory management
1947 */
1948
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001949/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001951struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001953 uint32_t num_blocks;
1954 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955 char *buffer;
1956 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001957 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001958
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001959 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960};
1961
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001962#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
1963 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964 { \
1965 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001966 .num_blocks = slab_num_blocks, \
1967 .block_size = slab_block_size, \
1968 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969 .free_list = NULL, \
1970 .num_used = 0, \
1971 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1972 }
1973
Peter Mitsis578f9112016-10-07 13:50:31 -04001974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * @brief Statically define and initialize a memory slab allocator.
Peter Mitsis578f9112016-10-07 13:50:31 -04001976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * The slab allocator's buffer contains @a slab_num_blocks memory blocks
1978 * that are @a slab_block_size bytes long. The buffer is aligned to a
1979 * @a slab_align -byte boundary. To ensure that each memory block is aligned
1980 * to a @a slab_align -byte boundary, @a slab_block_size must be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001981 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04001982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * The slab allocator can be accessed outside the module where it is defined
1984 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
1986 * extern struct k_mem_slab @a name;
1987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988 * @param name Name of the memory slab.
1989 * @param slab_block_size Size of each memory block (in bytes).
1990 * @param slab_num_blocks Number memory blocks.
1991 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04001992 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001993#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
1994 char __noinit __aligned(slab_align) \
1995 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
1996 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001997 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001998 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
1999 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002000
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002001/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002002 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * @param slab Address of the memory slab.
2007 * @param buffer Pointer to buffer used for the memory blocks.
2008 * @param block_size Size of each memory block (in bytes).
2009 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002010 *
2011 * @return N/A
2012 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002013extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002014 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002015
2016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @param slab Address of the memory slab.
2022 * @param mem Pointer to block address area.
2023 * @param timeout Maximum time to wait for operation to complete
2024 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2025 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027 * @retval 0 if successful. The block address area pointed at by @a mem
2028 * is set to the starting address of the memory block.
2029 * @retval -ENOMEM if failed immediately.
2030 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002031 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002032extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2033 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002034
2035/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * This routine releases a previously allocated memory block back to its
2039 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @param slab Address of the memory slab.
2042 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002043 *
2044 * @return N/A
2045 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002046extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002049 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002051 * This routine gets the number of memory blocks that are currently
2052 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002057 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002058static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002060 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061}
2062
Peter Mitsisc001aa82016-10-13 13:53:37 -04002063/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002064 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066 * This routine gets the number of memory blocks that are currently
2067 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002072 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002073static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002074{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002075 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002076}
2077
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078/* memory pools */
2079
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002080/*
2081 * Memory pool requires a buffer and two arrays of structures for the
2082 * memory block accounting:
2083 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2084 * status of four blocks of memory.
2085 */
2086struct k_mem_pool_quad_block {
2087 char *mem_blocks; /* pointer to the first of four memory blocks */
2088 uint32_t mem_status; /* four bits. If bit is set, memory block is
2089 allocated */
2090};
2091/*
2092 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2093 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2094 * block size is 4 times less than the previous one and thus requires 4 times
2095 * bigger array of k_mem_pool_quad_block structures to keep track of the
2096 * memory blocks.
2097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002099/*
2100 * The array of k_mem_pool_block_set keeps the information of each array of
2101 * k_mem_pool_quad_block structures
2102 */
2103struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002104 size_t block_size; /* memory block size */
2105 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002106 struct k_mem_pool_quad_block *quad_block;
2107 int count;
2108};
2109
2110/* Memory pool descriptor */
2111struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002112 size_t max_block_size;
2113 size_t min_block_size;
2114 uint32_t nr_of_maxblocks;
2115 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002116 struct k_mem_pool_block_set *block_set;
2117 char *bufblock;
2118 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2120};
2121
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002122#ifdef CONFIG_ARM
2123#define _SECTION_TYPE_SIGN "%"
2124#else
2125#define _SECTION_TYPE_SIGN "@"
2126#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002128/*
2129 * Static memory pool initialization
2130 */
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002131/**
2132 * @cond internal
2133 * Make Doxygen skip assembler macros
2134 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002135/*
2136 * Use .altmacro to be able to recalculate values and pass them as string
2137 * arguments when calling assembler macros resursively
2138 */
2139__asm__(".altmacro\n\t");
2140
2141/*
2142 * Recursively calls a macro
2143 * The followig global symbols need to be initialized:
2144 * __memory_pool_max_block_size - maximal size of the memory block
2145 * __memory_pool_min_block_size - minimal size of the memory block
2146 * Notes:
2147 * Global symbols are used due the fact that assembler macro allows only
2148 * one argument be passed with the % conversion
2149 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2150 * is used instead of / 4.
2151 * n_max argument needs to go first in the invoked macro, as some
2152 * assemblers concatenate \name and %(\n_max * 4) arguments
2153 * if \name goes first
2154 */
2155__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2156 ".ifge __memory_pool_max_block_size >> 2 -"
2157 " __memory_pool_min_block_size\n\t\t"
2158 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2159 "\\macro_name %(\\n_max * 4) \\name\n\t"
2160 ".endif\n\t"
2161 ".endm\n");
2162
2163/*
2164 * Build quad blocks
2165 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2166 * structures and recursively calls itself for the next array, 4 times
2167 * larger.
2168 * The followig global symbols need to be initialized:
2169 * __memory_pool_max_block_size - maximal size of the memory block
2170 * __memory_pool_min_block_size - minimal size of the memory block
2171 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2172 */
2173__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002174 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002175 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2176 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2177 ".if \\n_max % 4\n\t\t"
2178 ".skip __memory_pool_quad_block_size\n\t"
2179 ".endif\n\t"
2180 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2181 ".endm\n");
2182
2183/*
2184 * Build block sets and initialize them
2185 * Macro initializes the k_mem_pool_block_set structure and
2186 * recursively calls itself for the next one.
2187 * The followig global symbols need to be initialized:
2188 * __memory_pool_max_block_size - maximal size of the memory block
2189 * __memory_pool_min_block_size - minimal size of the memory block
2190 * __memory_pool_block_set_count, the number of the elements in the
2191 * block set array must be set to 0. Macro calculates it's real
2192 * value.
2193 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2194 * structures, _build_quad_blocks must be called prior it.
2195 */
2196__asm__(".macro _build_block_set n_max, name\n\t"
2197 ".int __memory_pool_max_block_size\n\t" /* block_size */
2198 ".if \\n_max % 4\n\t\t"
2199 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2200 ".else\n\t\t"
2201 ".int \\n_max >> 2\n\t"
2202 ".endif\n\t"
2203 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2204 ".int 0\n\t" /* count */
2205 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2206 "__do_recurse _build_block_set \\name \\n_max\n\t"
2207 ".endm\n");
2208
2209/*
2210 * Build a memory pool structure and initialize it
2211 * Macro uses __memory_pool_block_set_count global symbol,
2212 * block set addresses and buffer address, it may be called only after
2213 * _build_block_set
2214 */
2215__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002216 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002217 _SECTION_TYPE_SIGN "progbits\n\t"
2218 ".globl \\name\n\t"
2219 "\\name:\n\t"
2220 ".int \\max_size\n\t" /* max_block_size */
2221 ".int \\min_size\n\t" /* min_block_size */
2222 ".int \\n_max\n\t" /* nr_of_maxblocks */
2223 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2224 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2225 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2226 ".int 0\n\t" /* wait_q->head */
2227 ".int 0\n\t" /* wait_q->next */
2228 ".popsection\n\t"
2229 ".endm\n");
2230
2231#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2232 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2233 _SECTION_TYPE_SIGN "progbits\n\t"); \
2234 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2235 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2236 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2237 STRINGIFY(name) "\n\t"); \
2238 __asm__(".popsection\n\t")
2239
2240#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2241 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2242 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2243 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2244 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002245 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002246 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2247 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2248 STRINGIFY(name) "\n\t"); \
2249 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2250 __asm__(".int __memory_pool_block_set_count\n\t"); \
2251 __asm__(".popsection\n\t"); \
2252 extern uint32_t _mem_pool_block_set_count_##name; \
2253 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2254
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002255#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2256 char __noinit __aligned(align) \
2257 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002258
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002259/*
2260 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2261 * to __memory_pool_quad_block_size absolute symbol.
2262 * This function does not get called, but compiler calculates the value and
2263 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2264 */
2265static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2266{
2267 __asm__(".globl __memory_pool_quad_block_size\n\t"
2268#ifdef CONFIG_NIOS2
2269 "__memory_pool_quad_block_size = %0\n\t"
2270#else
2271 "__memory_pool_quad_block_size = %c0\n\t"
2272#endif
2273 :
2274 : "n"(sizeof(struct k_mem_pool_quad_block)));
2275}
2276
2277/**
2278 * @endcond
2279 * End of assembler macros that Doxygen has to skip
2280 */
2281
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002282/**
2283 * @brief Define a memory pool
2284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002285 * This defines and initializes a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2288 * long. The memory pool allows blocks to be repeatedly partitioned into
2289 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2290 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
2291 * aligned to @a align -byte boundary, @a min_size must be a multiple of
2292 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002293 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 * If the pool is to be accessed outside the module where it is defined, it
2295 * can be declared via
2296 *
2297 * extern struct k_mem_pool @a name;
2298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002299 * @param name Name of the memory pool.
2300 * @param min_size Size of the smallest blocks in the pool (in bytes).
2301 * @param max_size Size of the largest blocks in the pool (in bytes).
2302 * @param n_max Number of maximum sized blocks in the pool.
2303 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002304 */
2305#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002306 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2307 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002308 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002309 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2310 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2311 extern struct k_mem_pool name
2312
Peter Mitsis937042c2016-10-13 13:18:26 -04002313/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * @param pool Address of the memory pool.
2319 * @param block Pointer to block descriptor for the allocated memory.
2320 * @param size Amount of memory to allocate (in bytes).
2321 * @param timeout Maximum time to wait for operation to complete
2322 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2323 * or K_FOREVER to wait as long as necessary.
2324 *
2325 * @retval 0 if successful. The @a data field of the block descriptor
2326 * is set to the starting address of the memory block.
2327 * @retval -ENOMEM if unable to allocate a memory block.
2328 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002329 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002330extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002331 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002332
2333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * This routine releases a previously allocated memory block back to its
2337 * memory pool.
2338 *
2339 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002340 *
2341 * @return N/A
2342 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002344
2345/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * This routine instructs a memory pool to concatenate unused memory blocks
2349 * into larger blocks wherever possible. Manually defragmenting the memory
2350 * pool may speed up future allocations of memory blocks by eliminating the
2351 * need for the memory pool to perform an automatic partial defragmentation.
2352 *
2353 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002354 *
2355 * @return N/A
2356 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002357extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002358
2359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002363 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002368 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002369extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002370
2371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002373 *
2374 * This routine provides traditional free() semantics. The memory being
2375 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002378 *
2379 * @return N/A
2380 */
2381extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382
2383/*
2384 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2385 * hook into the device subsystem, which itself uses nanokernel semaphores,
2386 * and thus currently requires the definition of nano_sem.
2387 */
2388#include <legacy.h>
2389#include <arch/cpu.h>
2390
2391/*
2392 * private APIs that are utilized by one or more public APIs
2393 */
2394
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002397extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398
2399#ifdef __cplusplus
2400}
2401#endif
2402
Andrew Boiee004dec2016-11-07 09:01:19 -08002403#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2404/*
2405 * Define new and delete operators.
2406 * At this moment, the operators do nothing since objects are supposed
2407 * to be statically allocated.
2408 */
2409inline void operator delete(void *ptr)
2410{
2411 (void)ptr;
2412}
2413
2414inline void operator delete[](void *ptr)
2415{
2416 (void)ptr;
2417}
2418
2419inline void *operator new(size_t size)
2420{
2421 (void)size;
2422 return NULL;
2423}
2424
2425inline void *operator new[](size_t size)
2426{
2427 (void)size;
2428 return NULL;
2429}
2430
2431/* Placement versions of operator new and delete */
2432inline void operator delete(void *ptr1, void *ptr2)
2433{
2434 (void)ptr1;
2435 (void)ptr2;
2436}
2437
2438inline void operator delete[](void *ptr1, void *ptr2)
2439{
2440 (void)ptr1;
2441 (void)ptr2;
2442}
2443
2444inline void *operator new(size_t size, void *ptr)
2445{
2446 (void)size;
2447 return ptr;
2448}
2449
2450inline void *operator new[](size_t size, void *ptr)
2451{
2452 (void)size;
2453 return ptr;
2454}
2455
2456#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2457
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458#endif /* _kernel__h_ */