blob: 2fdf7faf5d29e0ef038220a30a4fc17e9081edc1 [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
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050082#define tcs k_thread
83struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
99/* threads/scheduler/execution contexts */
100
101enum execution_context_types {
102 K_ISR = 0,
103 K_COOP_THREAD,
104 K_PREEMPT_THREAD,
105};
106
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400108
109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500110 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500112 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500114 * The new thread may be scheduled for immediate execution or a delayed start.
115 * If the newly spawned thread does not have a delayed start the kernel
116 * scheduler may preempt the current thread to allow the new thread to
117 * execute.
118 *
119 * Thread options are architecture-specific, and can include K_ESSENTIAL,
120 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
121 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400122 *
123 * @param stack Pointer to the stack space.
124 * @param stack_size Stack size in bytes.
125 * @param entry Thread entry function.
126 * @param p1 1st entry point parameter.
127 * @param p2 2nd entry point parameter.
128 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500129 * @param prio Thread priority.
130 * @param options Thread options.
131 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500133 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400135extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
136 void (*entry)(void *, void *, void*),
137 void *p1, void *p2, void *p3,
138 int32_t prio, uint32_t options, int32_t delay);
139
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500141 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500143 * This routine puts the currently thread to sleep for @a duration
144 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500146 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400147 *
148 * @return N/A
149 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400150extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400151
152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500153 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 *
155 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500156 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400157 *
158 * @warning This routine utilizes the system clock, so it must not be invoked
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500159 * until the system clock is operational or while interrupts are locked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400160 *
161 * @return N/A
162 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400163extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400164
165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500168 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400169 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500170 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400171 *
172 * @return N/A
173 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400174extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400175
176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500177 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500179 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500181 * If @a thread is not currently sleeping, the routine has no effect.
182 *
183 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 *
185 * @return N/A
186 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400187extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188
189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500190 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500192 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400193 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400194extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195
196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500197 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500199 * This routine prevents @a thread from executing if it has not yet started
200 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500202 * @param thread ID of thread to cancel.
203 *
204 * @retval 0 if successful.
205 * @retval -EINVAL if the thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400207extern int k_thread_cancel(k_tid_t thread);
208
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * @brief Abort thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500212 * This routine permanently stops execution of @a thread. The thread is taken
213 * off all kernel queues it is part of (i.e. the ready queue, the timeout
214 * queue, or a kernel object wait queue). However, any kernel resources the
215 * thread might currently own (such as mutexes or memory blocks) are not
216 * released. It is the responsibility of the caller of this routine to ensure
217 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
221 * @return N/A
222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400223extern void k_thread_abort(k_tid_t thread);
224
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400225#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226#define _THREAD_TIMEOUT_INIT(obj) \
227 (obj).nano_timeout = { \
228 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400229 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400230 .wait_q = NULL, \
231 .delta_ticks_from_prev = -1, \
232 },
233#else
234#define _THREAD_TIMEOUT_INIT(obj)
235#endif
236
237#ifdef CONFIG_ERRNO
238#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
239#else
240#define _THREAD_ERRNO_INIT(obj)
241#endif
242
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400243struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400244 union {
245 char *init_stack;
246 struct k_thread *thread;
247 };
248 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500249 void (*init_entry)(void *, void *, void *);
250 void *init_p1;
251 void *init_p2;
252 void *init_p3;
253 int init_prio;
254 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400255 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500256 void (*init_abort)(void);
257 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258};
259
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400260#define _THREAD_INITIALIZER(stack, stack_size, \
261 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500262 prio, options, delay, abort, groups) \
263 { \
264 .init_stack = (stack), \
265 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400266 .init_entry = (void (*)(void *, void *, void *))entry, \
267 .init_p1 = (void *)p1, \
268 .init_p2 = (void *)p2, \
269 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500270 .init_prio = (prio), \
271 .init_options = (options), \
272 .init_delay = (delay), \
273 .init_abort = (abort), \
274 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400275 }
276
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400277/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500278 * @brief Statically define and initialize a thread.
279 *
280 * The thread may be scheduled for immediate execution or a delayed start.
281 *
282 * Thread options are architecture-specific, and can include K_ESSENTIAL,
283 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
284 * them using "|" (the logical OR operator).
285 *
286 * The ID of the thread can be accessed using:
287 *
288 * extern const k_tid_t @a name;
289 *
290 * @param name Name of the thread.
291 * @param stack_size Stack size in bytes.
292 * @param entry Thread entry function.
293 * @param p1 1st entry point parameter.
294 * @param p2 2nd entry point parameter.
295 * @param p3 3rd entry point parameter.
296 * @param prio Thread priority.
297 * @param options Thread options.
298 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400299 *
300 * @internal It has been observed that the x86 compiler by default aligns
301 * these _static_thread_data structures to 32-byte boundaries, thereby
302 * wasting space. To work around this, force a 4-byte alignment.
303 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500304#define K_THREAD_DEFINE(name, stack_size, \
305 entry, p1, p2, p3, \
306 prio, options, delay) \
307 char __noinit __stack _k_thread_obj_##name[stack_size]; \
308 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500309 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500310 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
311 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500312 NULL, 0); \
313 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400314
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500316 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500318 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500320 * @param thread ID of thread whose priority is needed.
321 *
322 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400323 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500324extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400325
326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500327 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500329 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400330 *
331 * Rescheduling can occur immediately depending on the priority @a thread is
332 * set to:
333 *
334 * - If its priority is raised above the priority of the caller of this
335 * function, and the caller is preemptible, @a thread will be scheduled in.
336 *
337 * - If the caller operates on itself, it lowers its priority below that of
338 * other threads in the system, and the caller is preemptible, the thread of
339 * highest priority will be scheduled in.
340 *
341 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
342 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
343 * highest priority.
344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500345 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400346 * @param prio New priority.
347 *
348 * @warning Changing the priority of a thread currently involved in mutex
349 * priority inheritance may result in undefined behavior.
350 *
351 * @return N/A
352 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400353extern void k_thread_priority_set(k_tid_t thread, int prio);
354
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500356 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500358 * This routine prevents the kernel scheduler from making @a thread the
359 * current thread. All other internal operations on @a thread are still
360 * performed; for example, any timeout it is waiting on keeps ticking,
361 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500363 * If @a thread is already suspended, the routine has no effect.
364 *
365 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400366 *
367 * @return N/A
368 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400369extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400370
371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500372 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500374 * This routine allows the kernel scheduler to make @a thread the current
375 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500377 * If @a thread is not currently suspended, the routine has no effect.
378 *
379 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380 *
381 * @return N/A
382 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400383extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500386 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * This routine specifies how the scheduler will perform time slicing of
389 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500391 * To enable time slicing, @a slice must be non-zero. The scheduler
392 * ensures that no thread runs for more than the specified time limit
393 * before other threads of that priority are given a chance to execute.
394 * Any thread whose priority is higher than @a prio is exempted, and may
395 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 * execute. Once the scheduler selects a thread for execution, there is no
399 * minimum guaranteed time the thread will execute before threads of greater or
400 * equal priority are scheduled.
401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403 * for execution, this routine has no effect; the thread is immediately
404 * rescheduled after the slice period expires.
405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * To disable timeslicing, set both @a slice and @a prio to zero.
407 *
408 * @param slice Maximum time slice length (in milliseconds).
409 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410 *
411 * @return N/A
412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400413extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400414
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500416 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500418 * @return 0 if invoked by a thread.
419 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500421extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400422
Benjamin Walsh445830d2016-11-10 15:54:27 -0500423/**
424 * @brief Determine if code is running in a preemptible thread.
425 *
426 * Returns a 'true' value if these conditions are all met:
427 *
428 * - the code is not running in an ISR
429 * - the thread's priority is in the preemptible range
430 * - the thread has not locked the scheduler
431 *
432 * @return 0 if invoked by either an ISR or a cooperative thread.
433 * @return Non-zero if invoked by a preemptible thread.
434 */
435extern int k_is_preempt_thread(void);
436
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500437/*
438 * @brief Lock the scheduler
439 *
440 * Prevent another thread from preempting the current thread.
441 *
442 * @note If the thread does an operation that causes it to pend, it will still
443 * be context switched out.
444 *
445 * @note Similar to irq_lock, the scheduler lock state is tracked per-thread.
446 *
447 * This should be chosen over irq_lock when possible, basically when the data
448 * protected by it is not accessible from ISRs. However, the associated
449 * k_sched_unlock() is heavier to use than irq_unlock, so if the amount of
450 * processing is really small, irq_lock might be a better choice.
451 *
452 * Can be called recursively.
453 *
454 * @return N/A
455 */
456extern void k_sched_lock(void);
457
458/*
459 * @brief Unlock the scheduler
460 *
461 * Re-enable scheduling previously disabled by k_sched_lock(). Must be called
462 * an equal amount of times k_sched_lock() was called. Threads are rescheduled
463 * upon exit.
464 *
465 * @return N/A
466 */
467extern void k_sched_unlock(void);
468
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500470 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500472 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * Custom data is not used by the kernel itself, and is freely available
475 * for a thread to use as it sees fit. It can be used as a framework
476 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500478 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400479 *
480 * @return N/A
481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400482extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483
484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500485 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500487 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400490 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491extern void *k_thread_custom_data_get(void);
492
493/**
494 * kernel timing
495 */
496
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400497#include <sys_clock.h>
498
Johan Hedberg14471692016-11-13 10:52:15 +0200499/* Convenience helpers to convert durations into milliseconds */
500#define K_MSEC(ms) (ms)
501#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
502#define K_MINUTES(m) K_SECONDS((m) * 60)
503#define K_HOURS(h) K_MINUTES((h) * 60)
504
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400505/* private internal time manipulation (users should never play with ticks) */
506
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500507/* added tick needed to account for tick in progress */
508#define _TICK_ALIGN 1
509
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400510static int64_t __ticks_to_ms(int64_t ticks)
511{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400512#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400513 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400514#else
515 __ASSERT(ticks == 0, "");
516 return 0;
517#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400518}
519
520
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521/* timeouts */
522
523struct _timeout;
524typedef void (*_timeout_func_t)(struct _timeout *t);
525
526struct _timeout {
527 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400528 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400529 sys_dlist_t *wait_q;
530 int32_t delta_ticks_from_prev;
531 _timeout_func_t func;
532};
533
Allan Stephens45bfa372016-10-12 12:39:42 -0500534
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535/* timers */
536
537struct k_timer {
538 /*
539 * _timeout structure must be first here if we want to use
540 * dynamic timer allocation. timeout.node is used in the double-linked
541 * list of free timers
542 */
543 struct _timeout timeout;
544
Allan Stephens45bfa372016-10-12 12:39:42 -0500545 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400546 _wait_q_t wait_q;
547
548 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500549 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400550
551 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500552 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400553
554 /* timer period */
555 int32_t period;
556
Allan Stephens45bfa372016-10-12 12:39:42 -0500557 /* timer status */
558 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400559
Allan Stephens45bfa372016-10-12 12:39:42 -0500560 /* used to support legacy timer APIs */
561 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400562
563 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
564};
565
Allan Stephens1342adb2016-11-03 13:54:53 -0500566#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400567 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500568 .timeout.delta_ticks_from_prev = -1, \
569 .timeout.wait_q = NULL, \
570 .timeout.thread = NULL, \
571 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400572 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500573 .expiry_fn = expiry, \
574 .stop_fn = stop, \
575 .status = 0, \
576 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400577 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
578 }
579
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 *
585 * extern struct k_timer @a name;
586 *
587 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500588 * @param expiry_fn Function to invoke each time the timer expires.
589 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500591#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500592 struct k_timer name \
593 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500594 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595
Allan Stephens45bfa372016-10-12 12:39:42 -0500596/**
597 * @brief Initialize a timer.
598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500599 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500600 *
601 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * @param expiry_fn Function to invoke each time the timer expires.
603 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500604 *
605 * @return N/A
606 */
607extern void k_timer_init(struct k_timer *timer,
608 void (*expiry_fn)(struct k_timer *),
609 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700610
Allan Stephens45bfa372016-10-12 12:39:42 -0500611/**
612 * @brief Start a timer.
613 *
614 * This routine starts a timer, and resets its status to zero. The timer
615 * begins counting down using the specified duration and period values.
616 *
617 * Attempting to start a timer that is already running is permitted.
618 * The timer's status is reset to zero and the timer begins counting down
619 * using the new duration and period values.
620 *
621 * @param timer Address of timer.
622 * @param duration Initial timer duration (in milliseconds).
623 * @param period Timer period (in milliseconds).
624 *
625 * @return N/A
626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400627extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500628 int32_t duration, int32_t period);
629
630/**
631 * @brief Stop a timer.
632 *
633 * This routine stops a running timer prematurely. The timer's stop function,
634 * if one exists, is invoked by the caller.
635 *
636 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500637 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500638 *
639 * @param timer Address of timer.
640 *
641 * @return N/A
642 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400643extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500644
645/**
646 * @brief Read timer status.
647 *
648 * This routine reads the timer's status, which indicates the number of times
649 * it has expired since its status was last read.
650 *
651 * Calling this routine resets the timer's status to zero.
652 *
653 * @param timer Address of timer.
654 *
655 * @return Timer status.
656 */
657extern uint32_t k_timer_status_get(struct k_timer *timer);
658
659/**
660 * @brief Synchronize thread to timer expiration.
661 *
662 * This routine blocks the calling thread until the timer's status is non-zero
663 * (indicating that it has expired at least once since it was last examined)
664 * or the timer is stopped. If the timer status is already non-zero,
665 * or the timer is already stopped, the caller continues without waiting.
666 *
667 * Calling this routine resets the timer's status to zero.
668 *
669 * This routine must not be used by interrupt handlers, since they are not
670 * allowed to block.
671 *
672 * @param timer Address of timer.
673 *
674 * @return Timer status.
675 */
676extern uint32_t k_timer_status_sync(struct k_timer *timer);
677
678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500679 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500680 *
681 * This routine computes the (approximate) time remaining before a running
682 * timer next expires. If the timer is not running, it returns zero.
683 *
684 * @param timer Address of timer.
685 *
686 * @return Remaining time (in milliseconds).
687 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400688extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400689
690
Allan Stephens45bfa372016-10-12 12:39:42 -0500691/* kernel clocks */
692
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * This routine returns the elapsed time since the system booted,
697 * in milliseconds.
698 *
699 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400700 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400701extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400702
703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500706 * This routine returns the lower 32-bits of the elapsed time since the system
707 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * This routine can be more efficient than k_uptime_get(), as it reduces the
710 * need for interrupt locking and 64-bit math. However, the 32-bit result
711 * cannot hold a system uptime time larger than approximately 50 days, so the
712 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500714 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400715 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400716extern uint32_t k_uptime_get_32(void);
717
718/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500719 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500721 * This routine computes the elapsed time between the current system uptime
722 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500724 * @param reftime Pointer to a reference time, which is updated to the current
725 * uptime upon return.
726 *
727 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400728 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400729extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400730
731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500734 * This routine computes the elapsed time between the current system uptime
735 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * This routine can be more efficient than k_uptime_delta(), as it reduces the
738 * need for interrupt locking and 64-bit math. However, the 32-bit result
739 * cannot hold an elapsed time larger than approximately 50 days, so the
740 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500742 * @param reftime Pointer to a reference time, which is updated to the current
743 * uptime upon return.
744 *
745 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400746 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400747extern uint32_t k_uptime_delta_32(int64_t *reftime);
748
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * This routine returns the current time, as measured by the system's hardware
753 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400757extern uint32_t k_cycle_get_32(void);
758
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759/**
760 * data transfers (basic)
761 */
762
763/* fifos */
764
765struct k_fifo {
766 _wait_q_t wait_q;
767 sys_slist_t data_q;
768
769 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
770};
771
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778 *
779 * @return N/A
780 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400781extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782
783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * This routine adds a data item to @a fifo. A fifo data item must be
787 * aligned on a 4-byte boundary, and the first 32 bits of the item are
788 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * @note Can be called by ISRs.
791 *
792 * @param fifo Address of the fifo.
793 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
795 * @return N/A
796 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400797extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798
799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * This routine adds a list of data items to @a fifo in one operation.
803 * The data items must be in a singly-linked list, with the first 32 bits
804 * each data item pointing to the next data item; the list must be
805 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @param fifo Address of the fifo.
810 * @param head Pointer to first node in singly-linked list.
811 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
813 * @return N/A
814 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400815extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816
817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * This routine adds a list of data items to @a fifo in one operation.
821 * The data items must be in a singly-linked list implemented using a
822 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 * and must be re-initialized via sys_slist_init().
824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @note Can be called by ISRs.
826 *
827 * @param fifo Address of the fifo.
828 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
830 * @return N/A
831 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400832extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833
834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * This routine removes a data item from @a fifo in a "first in, first out"
838 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
841 *
842 * @param fifo Address of the fifo.
843 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 * or one of the special values K_NO_WAIT and K_FOREVER.
845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @return Address of the data item if successful.
847 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400849extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
850
851#define K_FIFO_INITIALIZER(obj) \
852 { \
853 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400854 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400855 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
856 }
857
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
863 * extern struct k_fifo @a name;
864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400867#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500868 struct k_fifo name \
869 __in_section(_k_fifo, static, name) = \
870 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400871
872/* lifos */
873
874struct k_lifo {
875 _wait_q_t wait_q;
876 void *list;
877
878 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
879};
880
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
888 * @return N/A
889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400890extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891
892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * This routine adds a data item to @a lifo. A lifo data item must be
896 * aligned on a 4-byte boundary, and the first 32 bits of the item are
897 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * @note Can be called by ISRs.
900 *
901 * @param lifo Address of the lifo.
902 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903 *
904 * @return N/A
905 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400906extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400907
908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500911 * This routine removes a data item from @a lifo in a "last in, first out"
912 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
915 *
916 * @param lifo Address of the lifo.
917 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400918 * or one of the special values K_NO_WAIT and K_FOREVER.
919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * @return Address of the data item if successful.
921 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
924
925#define K_LIFO_INITIALIZER(obj) \
926 { \
927 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
928 .list = NULL, \
929 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
930 }
931
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
937 * extern struct k_lifo @a name;
938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500942 struct k_lifo name \
943 __in_section(_k_lifo, static, name) = \
944 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945
946/* stacks */
947
948struct k_stack {
949 _wait_q_t wait_q;
950 uint32_t *base, *next, *top;
951
952 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
953};
954
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500955/**
956 * @brief Initialize a stack.
957 *
958 * This routine initializes a stack object, prior to its first use.
959 *
960 * @param stack Address of the stack.
961 * @param buffer Address of array used to hold stacked values.
962 * @param num_entries Maximum number of values that can be stacked.
963 *
964 * @return N/A
965 */
Allan Stephens018cd9a2016-10-07 15:13:24 -0500966extern void k_stack_init(struct k_stack *stack,
967 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968
969/**
970 * @brief Push an element onto a stack.
971 *
972 * This routine adds a 32-bit value @a data to @a stack.
973 *
974 * @note Can be called by ISRs.
975 *
976 * @param stack Address of the stack.
977 * @param data Value to push onto the stack.
978 *
979 * @return N/A
980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400981extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982
983/**
984 * @brief Pop an element from a stack.
985 *
986 * This routine removes a 32-bit value from @a stack in a "last in, first out"
987 * manner and stores the value in @a data.
988 *
989 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
990 *
991 * @param stack Address of the stack.
992 * @param data Address of area to hold the value popped from the stack.
993 * @param timeout Waiting period to obtain a value (in milliseconds),
994 * or one of the special values K_NO_WAIT and K_FOREVER.
995 *
996 * @retval 0 if successful.
997 * @retval -EBUSY if returned without waiting.
998 * @retval -EAGAIN if waiting period timed out.
999 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001000extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1001
Peter Mitsis602e6a82016-10-17 11:48:43 -04001002#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003 { \
1004 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1005 .base = stack_buffer, \
1006 .next = stack_buffer, \
1007 .top = stack_buffer + stack_num_entries, \
1008 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1009 }
1010
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
1016 * extern struct k_stack @a name;
1017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * @param name Name of the stack.
1019 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001021#define K_STACK_DEFINE(name, stack_num_entries) \
1022 uint32_t __noinit \
1023 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001024 struct k_stack name \
1025 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001026 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1027 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001028
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029/**
1030 * workqueues
1031 */
1032
1033struct k_work;
1034
1035typedef void (*k_work_handler_t)(struct k_work *);
1036
1037/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039 * queued to it. This is useful for drivers which need to schedule
1040 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001041 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042 * space.
1043 */
1044struct k_work_q {
1045 struct k_fifo fifo;
1046};
1047
1048/**
1049 * @brief Work flags.
1050 */
1051enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001052 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053};
1054
1055/**
1056 * @brief An item which can be scheduled on a @ref k_work_q.
1057 */
1058struct k_work {
1059 void *_reserved; /* Used by k_fifo implementation. */
1060 k_work_handler_t handler;
1061 atomic_t flags[1];
1062};
1063
1064/**
1065 * @brief Statically initialize work item
1066 */
1067#define K_WORK_INITIALIZER(work_handler) \
1068 { \
1069 ._reserved = NULL, \
1070 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001071 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072 }
1073
1074/**
1075 * @brief Dynamically initialize work item
1076 */
1077static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1078{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001079 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080 work->handler = handler;
1081}
1082
1083/**
1084 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001085 *
1086 * This procedure schedules a work item to be processed.
1087 * In the case where the work item has already been submitted and is pending
1088 * execution, calling this function will result in a no-op. In this case, the
1089 * work item must not be modified externally (e.g. by the caller of this
1090 * function), since that could cause the work item to be processed in a
1091 * corrupted state.
1092 *
1093 * @param work_q to schedule the work item
1094 * @param work work item
1095 *
1096 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097 */
1098static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1099 struct k_work *work)
1100{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001101 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102 k_fifo_put(&work_q->fifo, work);
1103 }
1104}
1105
1106/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001107 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
1109 * @param work Work item to query
1110 *
1111 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001112 */
1113static inline int k_work_pending(struct k_work *work)
1114{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001115 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001116}
1117
1118/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 * @brief Start a new workqueue.
1120 *
1121 * This routine must not be called from an ISR.
1122 *
1123 * @param work_q Pointer to Work queue
1124 * @param stack Pointer to work queue thread's stack
1125 * @param stack_size Size of the work queue thread's stack
1126 * @param prio Priority of the work queue's thread
1127 *
1128 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001129 */
Allan Stephens904cf972016-10-07 13:59:23 -05001130extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1131 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001132
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001133#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001134
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135/**
1136 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001137 */
1138struct k_delayed_work {
1139 struct k_work work;
1140 struct _timeout timeout;
1141 struct k_work_q *work_q;
1142};
1143
1144/**
1145 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
1147 * Initialize a delayed work item.
1148 *
1149 * @param work Delayed work item
1150 * @param handler Routine invoked when processing delayed work item
1151 *
1152 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001153 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001154extern void k_delayed_work_init(struct k_delayed_work *work,
1155 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001156
1157/**
1158 * @brief Submit a delayed work item to a workqueue.
1159 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161 * Once the delay has passed, the work item is submitted to the work queue:
1162 * at this point, it is no longer possible to cancel it. Once the work item's
1163 * handler is about to be executed, the work is considered complete and can be
1164 * resubmitted.
1165 *
1166 * Care must be taken if the handler blocks or yield as there is no implicit
1167 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1168 * it should be explicitly done between the submitter and the handler.
1169 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001170 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001172 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001175 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001176extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1177 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001178 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179
1180/**
1181 * @brief Cancel a delayed work item
1182 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001183 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001184 * or is idle, this will do nothing. The only case where this can fail is when
1185 * the work has been submitted to the work queue, but the handler has not run
1186 * yet.
1187 *
1188 * @param work Delayed work item to be canceled
1189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001190 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001192extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001194#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001195
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001196extern struct k_work_q k_sys_work_q;
1197
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001199 * @brief Submit a work item to the system workqueue.
1200 *
1201 * @ref k_work_submit_to_queue
1202 *
1203 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001204 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205 * unexpected behavior.
1206 */
1207static inline void k_work_submit(struct k_work *work)
1208{
1209 k_work_submit_to_queue(&k_sys_work_q, work);
1210}
1211
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001212#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001214 * @brief Submit a delayed work item to the system workqueue.
1215 *
1216 * @ref k_delayed_work_submit_to_queue
1217 *
1218 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001219 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001220 * unexpected behavior.
1221 */
1222static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001223 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001224{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001225 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001226}
1227
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001228#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
1230/**
1231 * synchronization
1232 */
1233
1234/* mutexes */
1235
1236struct k_mutex {
1237 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001238 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001239 uint32_t lock_count;
1240 int owner_orig_prio;
1241#ifdef CONFIG_OBJECT_MONITOR
1242 int num_lock_state_changes;
1243 int num_conflicts;
1244#endif
1245
1246 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1247};
1248
1249#ifdef CONFIG_OBJECT_MONITOR
1250#define _MUTEX_INIT_OBJECT_MONITOR \
1251 .num_lock_state_changes = 0, .num_conflicts = 0,
1252#else
1253#define _MUTEX_INIT_OBJECT_MONITOR
1254#endif
1255
1256#define K_MUTEX_INITIALIZER(obj) \
1257 { \
1258 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1259 .owner = NULL, \
1260 .lock_count = 0, \
1261 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1262 _MUTEX_INIT_OBJECT_MONITOR \
1263 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1264 }
1265
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001269 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001270 *
1271 * extern struct k_mutex @a name;
1272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001276 struct k_mutex name \
1277 __in_section(_k_mutex, static, name) = \
1278 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001283 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001285 * Upon completion, the mutex is available and does not have an owner.
1286 *
1287 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001288 *
1289 * @return N/A
1290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292
1293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * This routine locks @a mutex. If the mutex is locked by another thread,
1297 * the calling thread waits until the mutex becomes available or until
1298 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 * A thread is permitted to lock a mutex it has already locked. The operation
1301 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * @param mutex Address of the mutex.
1304 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 * or one of the special values K_NO_WAIT and K_FOREVER.
1306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001307 * @retval 0 if successful.
1308 * @retval -EBUSY if returned without waiting.
1309 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001311extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312
1313/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001314 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * This routine unlocks @a mutex. The mutex must already be locked by the
1317 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 *
1319 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001320 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001321 * thread.
1322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001324 *
1325 * @return N/A
1326 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001327extern void k_mutex_unlock(struct k_mutex *mutex);
1328
1329/* semaphores */
1330
1331struct k_sem {
1332 _wait_q_t wait_q;
1333 unsigned int count;
1334 unsigned int limit;
1335
1336 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1337};
1338
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001339/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001340 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001342 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001344 * @param sem Address of the semaphore.
1345 * @param initial_count Initial semaphore count.
1346 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001347 *
1348 * @return N/A
1349 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001350extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1351 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001352
1353/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1359 *
1360 * @param sem Address of the semaphore.
1361 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001362 * or one of the special values K_NO_WAIT and K_FOREVER.
1363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001364 * @retval 0 if successful.
1365 * @retval -EBUSY if returned without waiting.
1366 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001367 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001368extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001369
1370/**
1371 * @brief Give a semaphore.
1372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001373 * This routine gives @a sem, unless the semaphore is already at its maximum
1374 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001379 *
1380 * @return N/A
1381 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382extern void k_sem_give(struct k_sem *sem);
1383
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001384/**
1385 * @brief Reset a semaphore's count to zero.
1386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001387 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001390 *
1391 * @return N/A
1392 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001393static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394{
1395 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396}
1397
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001398/**
1399 * @brief Get a semaphore's count.
1400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001403 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001405 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001406 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001407static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408{
1409 return sem->count;
1410}
1411
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1413 { \
1414 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1415 .count = initial_count, \
1416 .limit = count_limit, \
1417 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1418 }
1419
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001421 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001423 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001424 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001427 * @param name Name of the semaphore.
1428 * @param initial_count Initial semaphore count.
1429 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001430 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001432 struct k_sem name \
1433 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 K_SEM_INITIALIZER(name, initial_count, count_limit)
1435
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001436/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001437
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001438#define K_ALERT_DEFAULT NULL
1439#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001441typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001442
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001443struct k_alert {
1444 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445 atomic_t send_count;
1446 struct k_work work_item;
1447 struct k_sem sem;
1448
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001449 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450};
1451
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001452extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001454#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001456 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001457 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001458 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001459 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1461 }
1462
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 *
1468 * extern struct k_alert @a name;
1469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001470 * @param name Name of the alert.
1471 * @param alert_handler Action to take when alert is sent. Specify either
1472 * the address of a function to be invoked by the system workqueue
1473 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1474 * K_ALERT_DEFAULT (which causes the alert to pend).
1475 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001477#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001478 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001479 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001480 K_ALERT_INITIALIZER(name, alert_handler, \
1481 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001482
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001484 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001486 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @param alert Address of the alert.
1489 * @param handler Action to take when alert is sent. Specify either the address
1490 * of a function to be invoked by the system workqueue thread,
1491 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1492 * K_ALERT_DEFAULT (which causes the alert to pend).
1493 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 *
1495 * @return N/A
1496 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001497extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1498 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499
1500/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001501 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001503 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1506 *
1507 * @param alert Address of the alert.
1508 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001509 * or one of the special values K_NO_WAIT and K_FOREVER.
1510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001511 * @retval 0 if successful.
1512 * @retval -EBUSY if returned without waiting.
1513 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001514 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001515extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001516
1517/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001518 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001520 * This routine signals @a alert. The action specified for @a alert will
1521 * be taken, which may trigger the execution of an alert handler function
1522 * and/or cause the alert to pend (assuming the alert has not reached its
1523 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * @note Can be called by ISRs.
1526 *
1527 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001528 *
1529 * @return N/A
1530 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001531extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001532
1533/**
1534 * data transfers (complex)
1535 */
1536
1537/* message queues */
1538
1539struct k_msgq {
1540 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001541 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542 uint32_t max_msgs;
1543 char *buffer_start;
1544 char *buffer_end;
1545 char *read_ptr;
1546 char *write_ptr;
1547 uint32_t used_msgs;
1548
1549 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1550};
1551
Peter Mitsis1da807e2016-10-06 11:36:59 -04001552#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001553 { \
1554 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001555 .max_msgs = q_max_msgs, \
1556 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001557 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001558 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559 .read_ptr = q_buffer, \
1560 .write_ptr = q_buffer, \
1561 .used_msgs = 0, \
1562 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1563 }
1564
Peter Mitsis1da807e2016-10-06 11:36:59 -04001565/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001566 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001568 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1569 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001570 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1571 * message is similarly aligned to this boundary, @a q_msg_size must also be
1572 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * The message queue can be accessed outside the module where it is defined
1575 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001576 *
1577 * extern struct k_msgq @a name;
1578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001579 * @param q_name Name of the message queue.
1580 * @param q_msg_size Message size (in bytes).
1581 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001582 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001583 */
1584#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1585 static char __noinit __aligned(q_align) \
1586 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001587 struct k_msgq q_name \
1588 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001589 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1590 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591
Peter Mitsisd7a37502016-10-13 11:37:40 -04001592/**
1593 * @brief Initialize a message queue.
1594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001595 * This routine initializes a message queue object, prior to its first use.
1596 *
Allan Stephensda827222016-11-09 14:23:58 -06001597 * The message queue's ring buffer must contain space for @a max_msgs messages,
1598 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1599 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1600 * that each message is similarly aligned to this boundary, @a q_msg_size
1601 * must also be a multiple of N.
1602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @param q Address of the message queue.
1604 * @param buffer Pointer to ring buffer that holds queued messages.
1605 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001606 * @param max_msgs Maximum number of messages that can be queued.
1607 *
1608 * @return N/A
1609 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001610extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001611 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001612
1613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001617 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001618 * @note Can be called by ISRs.
1619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @param q Address of the message queue.
1621 * @param data Pointer to the message.
1622 * @param timeout Waiting period to add the message (in milliseconds),
1623 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @retval 0 if successful.
1626 * @retval -ENOMSG if returned without waiting or after a queue purge.
1627 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001628 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001630
1631/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001634 * This routine receives a message from message queue @a q in a "first in,
1635 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001636 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001637 * @note Can be called by ISRs.
1638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @param q Address of the message queue.
1640 * @param data Address of area to hold the received message.
1641 * @param timeout Waiting period to receive the message (in milliseconds),
1642 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @retval 0 if successful.
1645 * @retval -ENOMSG if returned without waiting.
1646 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001647 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001649
1650/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * This routine discards all unreceived messages in a message queue's ring
1654 * buffer. Any threads that are blocked waiting to send a message to the
1655 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001658 *
1659 * @return N/A
1660 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001661extern void k_msgq_purge(struct k_msgq *q);
1662
Peter Mitsis67be2492016-10-07 11:44:34 -04001663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001664 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001666 * This routine returns the number of unused entries in a message queue's
1667 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001669 * @param q Address of the message queue.
1670 *
1671 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001672 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001673static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001674{
1675 return q->max_msgs - q->used_msgs;
1676}
1677
Peter Mitsisd7a37502016-10-13 11:37:40 -04001678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001679 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001681 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @param q Address of the message queue.
1684 *
1685 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001686 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001687static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001688{
1689 return q->used_msgs;
1690}
1691
1692struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001693 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694 void *addr_in_pool;
1695 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001696 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001697};
1698
1699/* mailboxes */
1700
1701struct k_mbox_msg {
1702 /** internal use only - needed for legacy API support */
1703 uint32_t _mailbox;
1704 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001705 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001706 /** application-defined information value */
1707 uint32_t info;
1708 /** sender's message data buffer */
1709 void *tx_data;
1710 /** internal use only - needed for legacy API support */
1711 void *_rx_data;
1712 /** message data block descriptor */
1713 struct k_mem_block tx_block;
1714 /** source thread id */
1715 k_tid_t rx_source_thread;
1716 /** target thread id */
1717 k_tid_t tx_target_thread;
1718 /** internal use only - thread waiting on send (may be a dummy) */
1719 k_tid_t _syncing_thread;
1720#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1721 /** internal use only - semaphore used during asynchronous send */
1722 struct k_sem *_async_sem;
1723#endif
1724};
1725
1726struct k_mbox {
1727 _wait_q_t tx_msg_queue;
1728 _wait_q_t rx_msg_queue;
1729
1730 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1731};
1732
1733#define K_MBOX_INITIALIZER(obj) \
1734 { \
1735 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1736 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1737 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1738 }
1739
Peter Mitsis12092702016-10-14 12:57:23 -04001740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 *
1745 * extern struct k_mbox @a name;
1746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001748 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001750 struct k_mbox name \
1751 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001752 K_MBOX_INITIALIZER(name) \
1753
Peter Mitsis12092702016-10-14 12:57:23 -04001754/**
1755 * @brief Initialize a mailbox.
1756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * This routine initializes a mailbox object, prior to its first use.
1758 *
1759 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001760 *
1761 * @return N/A
1762 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001763extern void k_mbox_init(struct k_mbox *mbox);
1764
Peter Mitsis12092702016-10-14 12:57:23 -04001765/**
1766 * @brief Send a mailbox message in a synchronous manner.
1767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * This routine sends a message to @a mbox and waits for a receiver to both
1769 * receive and process it. The message data may be in a buffer, in a memory
1770 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @param mbox Address of the mailbox.
1773 * @param tx_msg Address of the transmit message descriptor.
1774 * @param timeout Waiting period for the message to be received (in
1775 * milliseconds), or one of the special values K_NO_WAIT
1776 * and K_FOREVER. Once the message has been received,
1777 * this routine waits as long as necessary for the message
1778 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @retval 0 if successful.
1781 * @retval -ENOMSG if returned without waiting.
1782 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001783 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001784extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001786
1787#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1788/**
1789 * @brief Send a mailbox message in an asynchronous manner.
1790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * This routine sends a message to @a mbox without waiting for a receiver
1792 * to process it. The message data may be in a buffer, in a memory pool block,
1793 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
1794 * will be given when the message has been both received and completely
1795 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04001796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * @param mbox Address of the mailbox.
1798 * @param tx_msg Address of the transmit message descriptor.
1799 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04001800 *
1801 * @return N/A
1802 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001803extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001805#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806
Peter Mitsis12092702016-10-14 12:57:23 -04001807/**
1808 * @brief Receive a mailbox message.
1809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * This routine receives a message from @a mbox, then optionally retrieves
1811 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001813 * @param mbox Address of the mailbox.
1814 * @param rx_msg Address of the receive message descriptor.
1815 * @param buffer Address of the buffer to receive data, or NULL to defer data
1816 * retrieval and message disposal until later.
1817 * @param timeout Waiting period for a message to be received (in
1818 * milliseconds), or one of the special values K_NO_WAIT
1819 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @retval 0 if successful.
1822 * @retval -ENOMSG if returned without waiting.
1823 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001824 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001825extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001827
1828/**
1829 * @brief Retrieve mailbox message data into a buffer.
1830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * This routine completes the processing of a received message by retrieving
1832 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001833 *
1834 * Alternatively, this routine can be used to dispose of a received message
1835 * without retrieving its data.
1836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001837 * @param rx_msg Address of the receive message descriptor.
1838 * @param buffer Address of the buffer to receive data, or NULL to discard
1839 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04001840 *
1841 * @return N/A
1842 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001843extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001844
1845/**
1846 * @brief Retrieve mailbox message data into a memory pool block.
1847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * This routine completes the processing of a received message by retrieving
1849 * its data into a memory pool block, then disposing of the message.
1850 * The memory pool block that results from successful retrieval must be
1851 * returned to the pool once the data has been processed, even in cases
1852 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04001853 *
1854 * Alternatively, this routine can be used to dispose of a received message
1855 * without retrieving its data. In this case there is no need to return a
1856 * memory pool block to the pool.
1857 *
1858 * This routine allocates a new memory pool block for the data only if the
1859 * data is not already in one. If a new block cannot be allocated, the routine
1860 * returns a failure code and the received message is left unchanged. This
1861 * permits the caller to reattempt data retrieval at a later time or to dispose
1862 * of the received message without retrieving its data.
1863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * @param rx_msg Address of a receive message descriptor.
1865 * @param pool Address of memory pool, or NULL to discard data.
1866 * @param block Address of the area to hold memory pool block info.
1867 * @param timeout Waiting period to wait for a memory pool block (in
1868 * milliseconds), or one of the special values K_NO_WAIT
1869 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001871 * @retval 0 if successful.
1872 * @retval -ENOMEM if returned without waiting.
1873 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001874 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001875extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001876 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877 struct k_mem_block *block, int32_t timeout);
1878
1879/* pipes */
1880
1881struct k_pipe {
1882 unsigned char *buffer; /* Pipe buffer: may be NULL */
1883 size_t size; /* Buffer size */
1884 size_t bytes_used; /* # bytes used in buffer */
1885 size_t read_index; /* Where in buffer to read from */
1886 size_t write_index; /* Where in buffer to write */
1887
1888 struct {
1889 _wait_q_t readers; /* Reader wait queue */
1890 _wait_q_t writers; /* Writer wait queue */
1891 } wait_q;
1892
1893 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1894};
1895
Peter Mitsise5d9c582016-10-14 14:44:57 -04001896#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897 { \
1898 .buffer = pipe_buffer, \
1899 .size = pipe_buffer_size, \
1900 .bytes_used = 0, \
1901 .read_index = 0, \
1902 .write_index = 0, \
1903 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1904 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1905 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1906 }
1907
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001911 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912 *
1913 * extern struct k_pipe @a name;
1914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @param name Name of the pipe.
1916 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
1917 * or zero if no ring buffer is used.
1918 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001920#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1921 static unsigned char __noinit __aligned(pipe_align) \
1922 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001923 struct k_pipe name \
1924 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001925 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001928 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * This routine initializes a pipe object, prior to its first use.
1931 *
1932 * @param pipe Address of the pipe.
1933 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
1934 * is used.
1935 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
1936 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937 *
1938 * @return N/A
1939 */
1940extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1941 size_t size);
1942
1943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * @param pipe Address of the pipe.
1949 * @param data Address of data to write.
1950 * @param bytes_to_write Size of data (in bytes).
1951 * @param bytes_written Address of area to hold the number of bytes written.
1952 * @param min_xfer Minimum number of bytes to write.
1953 * @param timeout Waiting period to wait for the data to be written (in
1954 * milliseconds), or one of the special values K_NO_WAIT
1955 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @retval 0 if at least @a min_xfer data bytes were written.
1958 * @retval -EIO if returned without waiting; zero data bytes were written.
1959 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1960 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001962extern int k_pipe_put(struct k_pipe *pipe, void *data,
1963 size_t bytes_to_write, size_t *bytes_written,
1964 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965
1966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @param pipe Address of the pipe.
1972 * @param data Address to place the data read from pipe.
1973 * @param bytes_to_read Maximum number of data bytes to read.
1974 * @param bytes_read Address of area to hold the number of bytes read.
1975 * @param min_xfer Minimum number of data bytes to read.
1976 * @param timeout Waiting period to wait for the data to be read (in
1977 * milliseconds), or one of the special values K_NO_WAIT
1978 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * @retval 0 if at least @a min_xfer data bytes were read.
1981 * @retval -EIO if returned without waiting; zero data bytes were read.
1982 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1983 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001985extern int k_pipe_get(struct k_pipe *pipe, void *data,
1986 size_t bytes_to_read, size_t *bytes_read,
1987 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001988
Peter Mitsis2fef0232016-10-14 14:53:44 -04001989#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * This routine writes the data contained in a memory block to @a pipe.
1994 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001997 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998 * @param block Memory block containing data to send
1999 * @param size Number of data bytes in memory block to send
2000 * @param sem Semaphore to signal upon completion (else NULL)
2001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003 */
2004extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2005 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04002006#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007
2008/**
2009 * memory management
2010 */
2011
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002012/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002013
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002014struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002015 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002016 uint32_t num_blocks;
2017 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018 char *buffer;
2019 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002020 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002022 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002023};
2024
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002025#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2026 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027 { \
2028 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002029 .num_blocks = slab_num_blocks, \
2030 .block_size = slab_block_size, \
2031 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032 .free_list = NULL, \
2033 .num_used = 0, \
2034 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2035 }
2036
Peter Mitsis578f9112016-10-07 13:50:31 -04002037/**
Allan Stephensda827222016-11-09 14:23:58 -06002038 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002039 *
Allan Stephensda827222016-11-09 14:23:58 -06002040 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002042 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2043 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002044 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002045 *
Allan Stephensda827222016-11-09 14:23:58 -06002046 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002047 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
2049 * extern struct k_mem_slab @a name;
2050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002051 * @param name Name of the memory slab.
2052 * @param slab_block_size Size of each memory block (in bytes).
2053 * @param slab_num_blocks Number memory blocks.
2054 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002055 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002056#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2057 char __noinit __aligned(slab_align) \
2058 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2059 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002060 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002061 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2062 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002064/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002065 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002067 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002068 *
Allan Stephensda827222016-11-09 14:23:58 -06002069 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2070 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2071 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2072 * To ensure that each memory block is similarly aligned to this boundary,
2073 * @a slab_block_size must also be a multiple of N.
2074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * @param slab Address of the memory slab.
2076 * @param buffer Pointer to buffer used for the memory blocks.
2077 * @param block_size Size of each memory block (in bytes).
2078 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002079 *
2080 * @return N/A
2081 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002082extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002083 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002084
2085/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002086 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @param slab Address of the memory slab.
2091 * @param mem Pointer to block address area.
2092 * @param timeout Maximum time to wait for operation to complete
2093 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2094 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @retval 0 if successful. The block address area pointed at by @a mem
2097 * is set to the starting address of the memory block.
2098 * @retval -ENOMEM if failed immediately.
2099 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002100 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002101extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2102 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002103
2104/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * This routine releases a previously allocated memory block back to its
2108 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @param slab Address of the memory slab.
2111 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002112 *
2113 * @return N/A
2114 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002115extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002118 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * This routine gets the number of memory blocks that are currently
2121 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002125 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002126 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002127static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002129 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002130}
2131
Peter Mitsisc001aa82016-10-13 13:53:37 -04002132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * This routine gets the number of memory blocks that are currently
2136 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002138 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002141 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002142static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002143{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002144 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002145}
2146
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147/* memory pools */
2148
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002149/*
2150 * Memory pool requires a buffer and two arrays of structures for the
2151 * memory block accounting:
2152 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2153 * status of four blocks of memory.
2154 */
2155struct k_mem_pool_quad_block {
2156 char *mem_blocks; /* pointer to the first of four memory blocks */
2157 uint32_t mem_status; /* four bits. If bit is set, memory block is
2158 allocated */
2159};
2160/*
2161 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2162 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2163 * block size is 4 times less than the previous one and thus requires 4 times
2164 * bigger array of k_mem_pool_quad_block structures to keep track of the
2165 * memory blocks.
2166 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002168/*
2169 * The array of k_mem_pool_block_set keeps the information of each array of
2170 * k_mem_pool_quad_block structures
2171 */
2172struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002173 size_t block_size; /* memory block size */
2174 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002175 struct k_mem_pool_quad_block *quad_block;
2176 int count;
2177};
2178
2179/* Memory pool descriptor */
2180struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002181 size_t max_block_size;
2182 size_t min_block_size;
2183 uint32_t nr_of_maxblocks;
2184 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002185 struct k_mem_pool_block_set *block_set;
2186 char *bufblock;
2187 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2189};
2190
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002191#ifdef CONFIG_ARM
2192#define _SECTION_TYPE_SIGN "%"
2193#else
2194#define _SECTION_TYPE_SIGN "@"
2195#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002197/*
2198 * Static memory pool initialization
2199 */
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002200/**
2201 * @cond internal
2202 * Make Doxygen skip assembler macros
2203 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002204/*
2205 * Use .altmacro to be able to recalculate values and pass them as string
2206 * arguments when calling assembler macros resursively
2207 */
2208__asm__(".altmacro\n\t");
2209
2210/*
2211 * Recursively calls a macro
2212 * The followig global symbols need to be initialized:
2213 * __memory_pool_max_block_size - maximal size of the memory block
2214 * __memory_pool_min_block_size - minimal size of the memory block
2215 * Notes:
2216 * Global symbols are used due the fact that assembler macro allows only
2217 * one argument be passed with the % conversion
2218 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2219 * is used instead of / 4.
2220 * n_max argument needs to go first in the invoked macro, as some
2221 * assemblers concatenate \name and %(\n_max * 4) arguments
2222 * if \name goes first
2223 */
2224__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2225 ".ifge __memory_pool_max_block_size >> 2 -"
2226 " __memory_pool_min_block_size\n\t\t"
2227 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2228 "\\macro_name %(\\n_max * 4) \\name\n\t"
2229 ".endif\n\t"
2230 ".endm\n");
2231
2232/*
2233 * Build quad blocks
2234 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2235 * structures and recursively calls itself for the next array, 4 times
2236 * larger.
2237 * The followig global symbols need to be initialized:
2238 * __memory_pool_max_block_size - maximal size of the memory block
2239 * __memory_pool_min_block_size - minimal size of the memory block
2240 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2241 */
2242__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002243 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002244 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2245 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2246 ".if \\n_max % 4\n\t\t"
2247 ".skip __memory_pool_quad_block_size\n\t"
2248 ".endif\n\t"
2249 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2250 ".endm\n");
2251
2252/*
2253 * Build block sets and initialize them
2254 * Macro initializes the k_mem_pool_block_set structure and
2255 * recursively calls itself for the next one.
2256 * The followig global symbols need to be initialized:
2257 * __memory_pool_max_block_size - maximal size of the memory block
2258 * __memory_pool_min_block_size - minimal size of the memory block
2259 * __memory_pool_block_set_count, the number of the elements in the
2260 * block set array must be set to 0. Macro calculates it's real
2261 * value.
2262 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2263 * structures, _build_quad_blocks must be called prior it.
2264 */
2265__asm__(".macro _build_block_set n_max, name\n\t"
2266 ".int __memory_pool_max_block_size\n\t" /* block_size */
2267 ".if \\n_max % 4\n\t\t"
2268 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2269 ".else\n\t\t"
2270 ".int \\n_max >> 2\n\t"
2271 ".endif\n\t"
2272 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2273 ".int 0\n\t" /* count */
2274 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2275 "__do_recurse _build_block_set \\name \\n_max\n\t"
2276 ".endm\n");
2277
2278/*
2279 * Build a memory pool structure and initialize it
2280 * Macro uses __memory_pool_block_set_count global symbol,
2281 * block set addresses and buffer address, it may be called only after
2282 * _build_block_set
2283 */
2284__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002285 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002286 _SECTION_TYPE_SIGN "progbits\n\t"
2287 ".globl \\name\n\t"
2288 "\\name:\n\t"
2289 ".int \\max_size\n\t" /* max_block_size */
2290 ".int \\min_size\n\t" /* min_block_size */
2291 ".int \\n_max\n\t" /* nr_of_maxblocks */
2292 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2293 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2294 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2295 ".int 0\n\t" /* wait_q->head */
2296 ".int 0\n\t" /* wait_q->next */
2297 ".popsection\n\t"
2298 ".endm\n");
2299
2300#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2301 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2302 _SECTION_TYPE_SIGN "progbits\n\t"); \
2303 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2304 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2305 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2306 STRINGIFY(name) "\n\t"); \
2307 __asm__(".popsection\n\t")
2308
2309#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2310 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2311 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2312 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2313 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002314 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002315 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2316 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2317 STRINGIFY(name) "\n\t"); \
2318 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2319 __asm__(".int __memory_pool_block_set_count\n\t"); \
2320 __asm__(".popsection\n\t"); \
2321 extern uint32_t _mem_pool_block_set_count_##name; \
2322 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2323
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002324#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2325 char __noinit __aligned(align) \
2326 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002327
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002328/*
2329 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2330 * to __memory_pool_quad_block_size absolute symbol.
2331 * This function does not get called, but compiler calculates the value and
2332 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2333 */
2334static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2335{
2336 __asm__(".globl __memory_pool_quad_block_size\n\t"
2337#ifdef CONFIG_NIOS2
2338 "__memory_pool_quad_block_size = %0\n\t"
2339#else
2340 "__memory_pool_quad_block_size = %c0\n\t"
2341#endif
2342 :
2343 : "n"(sizeof(struct k_mem_pool_quad_block)));
2344}
2345
2346/**
2347 * @endcond
2348 * End of assembler macros that Doxygen has to skip
2349 */
2350
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002351/**
2352 * @brief Define a memory pool
2353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * This defines and initializes a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2357 * long. The memory pool allows blocks to be repeatedly partitioned into
2358 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2359 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002360 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002362 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002363 * If the pool is to be accessed outside the module where it is defined, it
2364 * can be declared via
2365 *
2366 * extern struct k_mem_pool @a name;
2367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * @param name Name of the memory pool.
2369 * @param min_size Size of the smallest blocks in the pool (in bytes).
2370 * @param max_size Size of the largest blocks in the pool (in bytes).
2371 * @param n_max Number of maximum sized blocks in the pool.
2372 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002373 */
2374#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002375 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2376 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002377 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002378 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2379 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2380 extern struct k_mem_pool name
2381
Peter Mitsis937042c2016-10-13 13:18:26 -04002382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387 * @param pool Address of the memory pool.
2388 * @param block Pointer to block descriptor for the allocated memory.
2389 * @param size Amount of memory to allocate (in bytes).
2390 * @param timeout Maximum time to wait for operation to complete
2391 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2392 * or K_FOREVER to wait as long as necessary.
2393 *
2394 * @retval 0 if successful. The @a data field of the block descriptor
2395 * is set to the starting address of the memory block.
2396 * @retval -ENOMEM if unable to allocate a memory block.
2397 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002398 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002399extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002400 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002401
2402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * This routine releases a previously allocated memory block back to its
2406 * memory pool.
2407 *
2408 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002409 *
2410 * @return N/A
2411 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002413
2414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 * This routine instructs a memory pool to concatenate unused memory blocks
2418 * into larger blocks wherever possible. Manually defragmenting the memory
2419 * pool may speed up future allocations of memory blocks by eliminating the
2420 * need for the memory pool to perform an automatic partial defragmentation.
2421 *
2422 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002423 *
2424 * @return N/A
2425 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002426extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002427
2428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002432 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002434 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002437 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002438extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002439
2440/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002441 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002442 *
2443 * This routine provides traditional free() semantics. The memory being
2444 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002447 *
2448 * @return N/A
2449 */
2450extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451
2452/*
2453 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2454 * hook into the device subsystem, which itself uses nanokernel semaphores,
2455 * and thus currently requires the definition of nano_sem.
2456 */
2457#include <legacy.h>
2458#include <arch/cpu.h>
2459
2460/*
2461 * private APIs that are utilized by one or more public APIs
2462 */
2463
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002466extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467
2468#ifdef __cplusplus
2469}
2470#endif
2471
Andrew Boiee004dec2016-11-07 09:01:19 -08002472#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2473/*
2474 * Define new and delete operators.
2475 * At this moment, the operators do nothing since objects are supposed
2476 * to be statically allocated.
2477 */
2478inline void operator delete(void *ptr)
2479{
2480 (void)ptr;
2481}
2482
2483inline void operator delete[](void *ptr)
2484{
2485 (void)ptr;
2486}
2487
2488inline void *operator new(size_t size)
2489{
2490 (void)size;
2491 return NULL;
2492}
2493
2494inline void *operator new[](size_t size)
2495{
2496 (void)size;
2497 return NULL;
2498}
2499
2500/* Placement versions of operator new and delete */
2501inline void operator delete(void *ptr1, void *ptr2)
2502{
2503 (void)ptr1;
2504 (void)ptr2;
2505}
2506
2507inline void operator delete[](void *ptr1, void *ptr2)
2508{
2509 (void)ptr1;
2510 (void)ptr2;
2511}
2512
2513inline void *operator new(size_t size, void *ptr)
2514{
2515 (void)size;
2516 return ptr;
2517}
2518
2519inline void *operator new[](size_t size, void *ptr)
2520{
2521 (void)size;
2522 return ptr;
2523}
2524
2525#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2526
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527#endif /* _kernel__h_ */