blob: 71461d0ded49c5d28ada86c3cd2f4145c18ee980 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
49#define K_FOREVER (-1)
50#define K_NO_WAIT 0
51
52#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
Benjamin Walshfab8d922016-11-08 15:36:36 -050067#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
74#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
75#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
77#else
78#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
79#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
80#endif
81
82#define k_thread tcs
83struct tcs;
84struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
99/* threads/scheduler/execution contexts */
100
101enum execution_context_types {
102 K_ISR = 0,
103 K_COOP_THREAD,
104 K_PREEMPT_THREAD,
105};
106
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400108
109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500110 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500112 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500114 * The new thread may be scheduled for immediate execution or a delayed start.
115 * If the newly spawned thread does not have a delayed start the kernel
116 * scheduler may preempt the current thread to allow the new thread to
117 * execute.
118 *
119 * Thread options are architecture-specific, and can include K_ESSENTIAL,
120 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
121 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400122 *
123 * @param stack Pointer to the stack space.
124 * @param stack_size Stack size in bytes.
125 * @param entry Thread entry function.
126 * @param p1 1st entry point parameter.
127 * @param p2 2nd entry point parameter.
128 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500129 * @param prio Thread priority.
130 * @param options Thread options.
131 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500133 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400135extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
136 void (*entry)(void *, void *, void*),
137 void *p1, void *p2, void *p3,
138 int32_t prio, uint32_t options, int32_t delay);
139
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500141 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500143 * This routine puts the currently thread to sleep for @a duration
144 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500146 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400147 *
148 * @return N/A
149 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400150extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400151
152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500153 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 *
155 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500156 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400157 *
158 * @warning This routine utilizes the system clock, so it must not be invoked
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500159 * until the system clock is operational or while interrupts are locked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400160 *
161 * @return N/A
162 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400163extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400164
165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500168 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400169 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500170 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400171 *
172 * @return N/A
173 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400174extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400175
176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500177 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500179 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500181 * If @a thread is not currently sleeping, the routine has no effect.
182 *
183 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 *
185 * @return N/A
186 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400187extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188
189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500190 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500192 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400193 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400194extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195
196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500197 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500199 * This routine prevents @a thread from executing if it has not yet started
200 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500202 * @param thread ID of thread to cancel.
203 *
204 * @retval 0 if successful.
205 * @retval -EINVAL if the thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400207extern int k_thread_cancel(k_tid_t thread);
208
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * @brief Abort thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500212 * This routine permanently stops execution of @a thread. The thread is taken
213 * off all kernel queues it is part of (i.e. the ready queue, the timeout
214 * queue, or a kernel object wait queue). However, any kernel resources the
215 * thread might currently own (such as mutexes or memory blocks) are not
216 * released. It is the responsibility of the caller of this routine to ensure
217 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
221 * @return N/A
222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400223extern void k_thread_abort(k_tid_t thread);
224
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400225#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226#define _THREAD_TIMEOUT_INIT(obj) \
227 (obj).nano_timeout = { \
228 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400229 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400230 .wait_q = NULL, \
231 .delta_ticks_from_prev = -1, \
232 },
233#else
234#define _THREAD_TIMEOUT_INIT(obj)
235#endif
236
237#ifdef CONFIG_ERRNO
238#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
239#else
240#define _THREAD_ERRNO_INIT(obj)
241#endif
242
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400243struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400244 union {
245 char *init_stack;
246 struct k_thread *thread;
247 };
248 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500249 void (*init_entry)(void *, void *, void *);
250 void *init_p1;
251 void *init_p2;
252 void *init_p3;
253 int init_prio;
254 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400255 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500256 void (*init_abort)(void);
257 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258};
259
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400260#define _THREAD_INITIALIZER(stack, stack_size, \
261 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500262 prio, options, delay, abort, groups) \
263 { \
264 .init_stack = (stack), \
265 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400266 .init_entry = (void (*)(void *, void *, void *))entry, \
267 .init_p1 = (void *)p1, \
268 .init_p2 = (void *)p2, \
269 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500270 .init_prio = (prio), \
271 .init_options = (options), \
272 .init_delay = (delay), \
273 .init_abort = (abort), \
274 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400275 }
276
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400277/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500278 * @brief Statically define and initialize a thread.
279 *
280 * The thread may be scheduled for immediate execution or a delayed start.
281 *
282 * Thread options are architecture-specific, and can include K_ESSENTIAL,
283 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
284 * them using "|" (the logical OR operator).
285 *
286 * The ID of the thread can be accessed using:
287 *
288 * extern const k_tid_t @a name;
289 *
290 * @param name Name of the thread.
291 * @param stack_size Stack size in bytes.
292 * @param entry Thread entry function.
293 * @param p1 1st entry point parameter.
294 * @param p2 2nd entry point parameter.
295 * @param p3 3rd entry point parameter.
296 * @param prio Thread priority.
297 * @param options Thread options.
298 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400299 *
300 * @internal It has been observed that the x86 compiler by default aligns
301 * these _static_thread_data structures to 32-byte boundaries, thereby
302 * wasting space. To work around this, force a 4-byte alignment.
303 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500304#define K_THREAD_DEFINE(name, stack_size, \
305 entry, p1, p2, p3, \
306 prio, options, delay) \
307 char __noinit __stack _k_thread_obj_##name[stack_size]; \
308 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500309 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500310 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
311 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500312 NULL, 0); \
313 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400314
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500316 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500318 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500320 * @param thread ID of thread whose priority is needed.
321 *
322 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400323 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500324extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400325
326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500327 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500329 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400330 *
331 * Rescheduling can occur immediately depending on the priority @a thread is
332 * set to:
333 *
334 * - If its priority is raised above the priority of the caller of this
335 * function, and the caller is preemptible, @a thread will be scheduled in.
336 *
337 * - If the caller operates on itself, it lowers its priority below that of
338 * other threads in the system, and the caller is preemptible, the thread of
339 * highest priority will be scheduled in.
340 *
341 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
342 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
343 * highest priority.
344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500345 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400346 * @param prio New priority.
347 *
348 * @warning Changing the priority of a thread currently involved in mutex
349 * priority inheritance may result in undefined behavior.
350 *
351 * @return N/A
352 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400353extern void k_thread_priority_set(k_tid_t thread, int prio);
354
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500356 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500358 * This routine prevents the kernel scheduler from making @a thread the
359 * current thread. All other internal operations on @a thread are still
360 * performed; for example, any timeout it is waiting on keeps ticking,
361 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500363 * If @a thread is already suspended, the routine has no effect.
364 *
365 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400366 *
367 * @return N/A
368 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400369extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400370
371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500372 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500374 * This routine allows the kernel scheduler to make @a thread the current
375 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500377 * If @a thread is not currently suspended, the routine has no effect.
378 *
379 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380 *
381 * @return N/A
382 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400383extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500386 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * This routine specifies how the scheduler will perform time slicing of
389 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500391 * To enable time slicing, @a slice must be non-zero. The scheduler
392 * ensures that no thread runs for more than the specified time limit
393 * before other threads of that priority are given a chance to execute.
394 * Any thread whose priority is higher than @a prio is exempted, and may
395 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 * execute. Once the scheduler selects a thread for execution, there is no
399 * minimum guaranteed time the thread will execute before threads of greater or
400 * equal priority are scheduled.
401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403 * for execution, this routine has no effect; the thread is immediately
404 * rescheduled after the slice period expires.
405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * To disable timeslicing, set both @a slice and @a prio to zero.
407 *
408 * @param slice Maximum time slice length (in milliseconds).
409 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410 *
411 * @return N/A
412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400413extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400414
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500416 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500418 * @return 0 if invoked by a thread.
419 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500421extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400422
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500423/*
424 * @brief Lock the scheduler
425 *
426 * Prevent another thread from preempting the current thread.
427 *
428 * @note If the thread does an operation that causes it to pend, it will still
429 * be context switched out.
430 *
431 * @note Similar to irq_lock, the scheduler lock state is tracked per-thread.
432 *
433 * This should be chosen over irq_lock when possible, basically when the data
434 * protected by it is not accessible from ISRs. However, the associated
435 * k_sched_unlock() is heavier to use than irq_unlock, so if the amount of
436 * processing is really small, irq_lock might be a better choice.
437 *
438 * Can be called recursively.
439 *
440 * @return N/A
441 */
442extern void k_sched_lock(void);
443
444/*
445 * @brief Unlock the scheduler
446 *
447 * Re-enable scheduling previously disabled by k_sched_lock(). Must be called
448 * an equal amount of times k_sched_lock() was called. Threads are rescheduled
449 * upon exit.
450 *
451 * @return N/A
452 */
453extern void k_sched_unlock(void);
454
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500456 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500460 * Custom data is not used by the kernel itself, and is freely available
461 * for a thread to use as it sees fit. It can be used as a framework
462 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
466 * @return N/A
467 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400468extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469
470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500471 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500473 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500475 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400476 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400477extern void *k_thread_custom_data_get(void);
478
479/**
480 * kernel timing
481 */
482
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400483#include <sys_clock.h>
484
485/* private internal time manipulation (users should never play with ticks) */
486
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500487/* added tick needed to account for tick in progress */
488#define _TICK_ALIGN 1
489
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400490static int64_t __ticks_to_ms(int64_t ticks)
491{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400492#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400493 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400494#else
495 __ASSERT(ticks == 0, "");
496 return 0;
497#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400498}
499
500
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400501/* timeouts */
502
503struct _timeout;
504typedef void (*_timeout_func_t)(struct _timeout *t);
505
506struct _timeout {
507 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400508 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400509 sys_dlist_t *wait_q;
510 int32_t delta_ticks_from_prev;
511 _timeout_func_t func;
512};
513
Allan Stephens45bfa372016-10-12 12:39:42 -0500514
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515/* timers */
516
517struct k_timer {
518 /*
519 * _timeout structure must be first here if we want to use
520 * dynamic timer allocation. timeout.node is used in the double-linked
521 * list of free timers
522 */
523 struct _timeout timeout;
524
Allan Stephens45bfa372016-10-12 12:39:42 -0500525 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400526 _wait_q_t wait_q;
527
528 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500529 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400530
531 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500532 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533
534 /* timer period */
535 int32_t period;
536
Allan Stephens45bfa372016-10-12 12:39:42 -0500537 /* timer status */
538 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400539
Allan Stephens45bfa372016-10-12 12:39:42 -0500540 /* used to support legacy timer APIs */
541 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400542
543 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
544};
545
Allan Stephens1342adb2016-11-03 13:54:53 -0500546#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400547 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500548 .timeout.delta_ticks_from_prev = -1, \
549 .timeout.wait_q = NULL, \
550 .timeout.thread = NULL, \
551 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400552 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500553 .expiry_fn = expiry, \
554 .stop_fn = stop, \
555 .status = 0, \
556 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400557 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
558 }
559
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500563 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
565 * extern struct k_timer @a name;
566 *
567 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500568 * @param expiry_fn Function to invoke each time the timer expires.
569 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400570 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500571#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500572 struct k_timer name \
573 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500574 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400575
Allan Stephens45bfa372016-10-12 12:39:42 -0500576/**
577 * @brief Initialize a timer.
578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500579 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500580 *
581 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @param expiry_fn Function to invoke each time the timer expires.
583 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500584 *
585 * @return N/A
586 */
587extern void k_timer_init(struct k_timer *timer,
588 void (*expiry_fn)(struct k_timer *),
589 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700590
Allan Stephens45bfa372016-10-12 12:39:42 -0500591/**
592 * @brief Start a timer.
593 *
594 * This routine starts a timer, and resets its status to zero. The timer
595 * begins counting down using the specified duration and period values.
596 *
597 * Attempting to start a timer that is already running is permitted.
598 * The timer's status is reset to zero and the timer begins counting down
599 * using the new duration and period values.
600 *
601 * @param timer Address of timer.
602 * @param duration Initial timer duration (in milliseconds).
603 * @param period Timer period (in milliseconds).
604 *
605 * @return N/A
606 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400607extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500608 int32_t duration, int32_t period);
609
610/**
611 * @brief Stop a timer.
612 *
613 * This routine stops a running timer prematurely. The timer's stop function,
614 * if one exists, is invoked by the caller.
615 *
616 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500617 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500618 *
619 * @param timer Address of timer.
620 *
621 * @return N/A
622 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400623extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500624
625/**
626 * @brief Read timer status.
627 *
628 * This routine reads the timer's status, which indicates the number of times
629 * it has expired since its status was last read.
630 *
631 * Calling this routine resets the timer's status to zero.
632 *
633 * @param timer Address of timer.
634 *
635 * @return Timer status.
636 */
637extern uint32_t k_timer_status_get(struct k_timer *timer);
638
639/**
640 * @brief Synchronize thread to timer expiration.
641 *
642 * This routine blocks the calling thread until the timer's status is non-zero
643 * (indicating that it has expired at least once since it was last examined)
644 * or the timer is stopped. If the timer status is already non-zero,
645 * or the timer is already stopped, the caller continues without waiting.
646 *
647 * Calling this routine resets the timer's status to zero.
648 *
649 * This routine must not be used by interrupt handlers, since they are not
650 * allowed to block.
651 *
652 * @param timer Address of timer.
653 *
654 * @return Timer status.
655 */
656extern uint32_t k_timer_status_sync(struct k_timer *timer);
657
658/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500659 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500660 *
661 * This routine computes the (approximate) time remaining before a running
662 * timer next expires. If the timer is not running, it returns zero.
663 *
664 * @param timer Address of timer.
665 *
666 * @return Remaining time (in milliseconds).
667 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400669
670
Allan Stephens45bfa372016-10-12 12:39:42 -0500671/* kernel clocks */
672
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400673/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500674 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500676 * This routine returns the elapsed time since the system booted,
677 * in milliseconds.
678 *
679 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400680 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400681extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400682
683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500684 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500686 * This routine returns the lower 32-bits of the elapsed time since the system
687 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500689 * This routine can be more efficient than k_uptime_get(), as it reduces the
690 * need for interrupt locking and 64-bit math. However, the 32-bit result
691 * cannot hold a system uptime time larger than approximately 50 days, so the
692 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400695 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400696extern uint32_t k_uptime_get_32(void);
697
698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500699 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * This routine computes the elapsed time between the current system uptime
702 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * @param reftime Pointer to a reference time, which is updated to the current
705 * uptime upon return.
706 *
707 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400708 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400709extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400710
711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500712 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500714 * This routine computes the elapsed time between the current system uptime
715 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500717 * This routine can be more efficient than k_uptime_delta(), as it reduces the
718 * need for interrupt locking and 64-bit math. However, the 32-bit result
719 * cannot hold an elapsed time larger than approximately 50 days, so the
720 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500722 * @param reftime Pointer to a reference time, which is updated to the current
723 * uptime upon return.
724 *
725 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400726 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400727extern uint32_t k_uptime_delta_32(int64_t *reftime);
728
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * This routine returns the current time, as measured by the system's hardware
733 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500735 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400736 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400737extern uint32_t k_cycle_get_32(void);
738
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400739/**
740 * data transfers (basic)
741 */
742
743/* fifos */
744
745struct k_fifo {
746 _wait_q_t wait_q;
747 sys_slist_t data_q;
748
749 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
750};
751
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500753 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400758 *
759 * @return N/A
760 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400761extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400762
763/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500764 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * This routine adds a data item to @a fifo. A fifo data item must be
767 * aligned on a 4-byte boundary, and the first 32 bits of the item are
768 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * @note Can be called by ISRs.
771 *
772 * @param fifo Address of the fifo.
773 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
775 * @return N/A
776 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400777extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778
779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine adds a list of data items to @a fifo in one operation.
783 * The data items must be in a singly-linked list, with the first 32 bits
784 * each data item pointing to the next data item; the list must be
785 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500789 * @param fifo Address of the fifo.
790 * @param head Pointer to first node in singly-linked list.
791 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 *
793 * @return N/A
794 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400795extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796
797/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * This routine adds a list of data items to @a fifo in one operation.
801 * The data items must be in a singly-linked list implemented using a
802 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 * and must be re-initialized via sys_slist_init().
804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @note Can be called by ISRs.
806 *
807 * @param fifo Address of the fifo.
808 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 *
810 * @return N/A
811 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400812extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813
814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * This routine removes a data item from @a fifo in a "first in, first out"
818 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
821 *
822 * @param fifo Address of the fifo.
823 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 * or one of the special values K_NO_WAIT and K_FOREVER.
825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @return Address of the data item if successful.
827 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400829extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
830
831#define K_FIFO_INITIALIZER(obj) \
832 { \
833 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400834 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400835 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
836 }
837
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
843 * extern struct k_fifo @a name;
844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400847#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500848 struct k_fifo name \
849 __in_section(_k_fifo, static, name) = \
850 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400851
852/* lifos */
853
854struct k_lifo {
855 _wait_q_t wait_q;
856 void *list;
857
858 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
859};
860
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
868 * @return N/A
869 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400870extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871
872/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * This routine adds a data item to @a lifo. A lifo data item must be
876 * aligned on a 4-byte boundary, and the first 32 bits of the item are
877 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * @note Can be called by ISRs.
880 *
881 * @param lifo Address of the lifo.
882 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
884 * @return N/A
885 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400886extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887
888/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * This routine removes a data item from @a lifo in a "last in, first out"
892 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500894 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
895 *
896 * @param lifo Address of the lifo.
897 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 * or one of the special values K_NO_WAIT and K_FOREVER.
899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500900 * @return Address of the data item if successful.
901 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400902 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400903extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
904
905#define K_LIFO_INITIALIZER(obj) \
906 { \
907 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
908 .list = NULL, \
909 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
910 }
911
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500913 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916 *
917 * extern struct k_lifo @a name;
918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500919 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400921#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500922 struct k_lifo name \
923 __in_section(_k_lifo, static, name) = \
924 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925
926/* stacks */
927
928struct k_stack {
929 _wait_q_t wait_q;
930 uint32_t *base, *next, *top;
931
932 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
933};
934
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935/**
936 * @brief Initialize a stack.
937 *
938 * This routine initializes a stack object, prior to its first use.
939 *
940 * @param stack Address of the stack.
941 * @param buffer Address of array used to hold stacked values.
942 * @param num_entries Maximum number of values that can be stacked.
943 *
944 * @return N/A
945 */
Allan Stephens018cd9a2016-10-07 15:13:24 -0500946extern void k_stack_init(struct k_stack *stack,
947 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948
949/**
950 * @brief Push an element onto a stack.
951 *
952 * This routine adds a 32-bit value @a data to @a stack.
953 *
954 * @note Can be called by ISRs.
955 *
956 * @param stack Address of the stack.
957 * @param data Value to push onto the stack.
958 *
959 * @return N/A
960 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400961extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962
963/**
964 * @brief Pop an element from a stack.
965 *
966 * This routine removes a 32-bit value from @a stack in a "last in, first out"
967 * manner and stores the value in @a data.
968 *
969 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
970 *
971 * @param stack Address of the stack.
972 * @param data Address of area to hold the value popped from the stack.
973 * @param timeout Waiting period to obtain a value (in milliseconds),
974 * or one of the special values K_NO_WAIT and K_FOREVER.
975 *
976 * @retval 0 if successful.
977 * @retval -EBUSY if returned without waiting.
978 * @retval -EAGAIN if waiting period timed out.
979 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400980extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
981
Peter Mitsis602e6a82016-10-17 11:48:43 -0400982#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983 { \
984 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
985 .base = stack_buffer, \
986 .next = stack_buffer, \
987 .top = stack_buffer + stack_num_entries, \
988 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
989 }
990
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
996 * extern struct k_stack @a name;
997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * @param name Name of the stack.
999 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001001#define K_STACK_DEFINE(name, stack_num_entries) \
1002 uint32_t __noinit \
1003 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001004 struct k_stack name \
1005 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001006 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1007 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001008
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001009/**
1010 * workqueues
1011 */
1012
1013struct k_work;
1014
1015typedef void (*k_work_handler_t)(struct k_work *);
1016
1017/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001019 * queued to it. This is useful for drivers which need to schedule
1020 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001022 * space.
1023 */
1024struct k_work_q {
1025 struct k_fifo fifo;
1026};
1027
1028/**
1029 * @brief Work flags.
1030 */
1031enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001032 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033};
1034
1035/**
1036 * @brief An item which can be scheduled on a @ref k_work_q.
1037 */
1038struct k_work {
1039 void *_reserved; /* Used by k_fifo implementation. */
1040 k_work_handler_t handler;
1041 atomic_t flags[1];
1042};
1043
1044/**
1045 * @brief Statically initialize work item
1046 */
1047#define K_WORK_INITIALIZER(work_handler) \
1048 { \
1049 ._reserved = NULL, \
1050 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001051 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052 }
1053
1054/**
1055 * @brief Dynamically initialize work item
1056 */
1057static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1058{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001059 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001060 work->handler = handler;
1061}
1062
1063/**
1064 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001065 *
1066 * This procedure schedules a work item to be processed.
1067 * In the case where the work item has already been submitted and is pending
1068 * execution, calling this function will result in a no-op. In this case, the
1069 * work item must not be modified externally (e.g. by the caller of this
1070 * function), since that could cause the work item to be processed in a
1071 * corrupted state.
1072 *
1073 * @param work_q to schedule the work item
1074 * @param work work item
1075 *
1076 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077 */
1078static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1079 struct k_work *work)
1080{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001081 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082 k_fifo_put(&work_q->fifo, work);
1083 }
1084}
1085
1086/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001087 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
1089 * @param work Work item to query
1090 *
1091 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001092 */
1093static inline int k_work_pending(struct k_work *work)
1094{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001095 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001096}
1097
1098/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099 * @brief Start a new workqueue.
1100 *
1101 * This routine must not be called from an ISR.
1102 *
1103 * @param work_q Pointer to Work queue
1104 * @param stack Pointer to work queue thread's stack
1105 * @param stack_size Size of the work queue thread's stack
1106 * @param prio Priority of the work queue's thread
1107 *
1108 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109 */
Allan Stephens904cf972016-10-07 13:59:23 -05001110extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1111 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001113#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001114
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115/**
1116 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117 */
1118struct k_delayed_work {
1119 struct k_work work;
1120 struct _timeout timeout;
1121 struct k_work_q *work_q;
1122};
1123
1124/**
1125 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
1127 * Initialize a delayed work item.
1128 *
1129 * @param work Delayed work item
1130 * @param handler Routine invoked when processing delayed work item
1131 *
1132 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001133 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001134extern void k_delayed_work_init(struct k_delayed_work *work,
1135 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001136
1137/**
1138 * @brief Submit a delayed work item to a workqueue.
1139 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141 * Once the delay has passed, the work item is submitted to the work queue:
1142 * at this point, it is no longer possible to cancel it. Once the work item's
1143 * handler is about to be executed, the work is considered complete and can be
1144 * resubmitted.
1145 *
1146 * Care must be taken if the handler blocks or yield as there is no implicit
1147 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1148 * it should be explicitly done between the submitter and the handler.
1149 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001150 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001151 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001152 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001156extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1157 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001158 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159
1160/**
1161 * @brief Cancel a delayed work item
1162 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001164 * or is idle, this will do nothing. The only case where this can fail is when
1165 * the work has been submitted to the work queue, but the handler has not run
1166 * yet.
1167 *
1168 * @param work Delayed work item to be canceled
1169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001172extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001174#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001175
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176extern struct k_work_q k_sys_work_q;
1177
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179 * @brief Submit a work item to the system workqueue.
1180 *
1181 * @ref k_work_submit_to_queue
1182 *
1183 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001185 * unexpected behavior.
1186 */
1187static inline void k_work_submit(struct k_work *work)
1188{
1189 k_work_submit_to_queue(&k_sys_work_q, work);
1190}
1191
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001192#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001194 * @brief Submit a delayed work item to the system workqueue.
1195 *
1196 * @ref k_delayed_work_submit_to_queue
1197 *
1198 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001199 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200 * unexpected behavior.
1201 */
1202static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001203 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001204{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001205 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206}
1207
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001208#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
1210/**
1211 * synchronization
1212 */
1213
1214/* mutexes */
1215
1216struct k_mutex {
1217 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001218 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001219 uint32_t lock_count;
1220 int owner_orig_prio;
1221#ifdef CONFIG_OBJECT_MONITOR
1222 int num_lock_state_changes;
1223 int num_conflicts;
1224#endif
1225
1226 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1227};
1228
1229#ifdef CONFIG_OBJECT_MONITOR
1230#define _MUTEX_INIT_OBJECT_MONITOR \
1231 .num_lock_state_changes = 0, .num_conflicts = 0,
1232#else
1233#define _MUTEX_INIT_OBJECT_MONITOR
1234#endif
1235
1236#define K_MUTEX_INITIALIZER(obj) \
1237 { \
1238 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1239 .owner = NULL, \
1240 .lock_count = 0, \
1241 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1242 _MUTEX_INIT_OBJECT_MONITOR \
1243 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1244 }
1245
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 *
1251 * extern struct k_mutex @a name;
1252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001253 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001254 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001255#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001256 struct k_mutex name \
1257 __in_section(_k_mutex, static, name) = \
1258 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001259
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001263 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001265 * Upon completion, the mutex is available and does not have an owner.
1266 *
1267 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 *
1269 * @return N/A
1270 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001271extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272
1273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001274 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * This routine locks @a mutex. If the mutex is locked by another thread,
1277 * the calling thread waits until the mutex becomes available or until
1278 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * A thread is permitted to lock a mutex it has already locked. The operation
1281 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001283 * @param mutex Address of the mutex.
1284 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001285 * or one of the special values K_NO_WAIT and K_FOREVER.
1286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001287 * @retval 0 if successful.
1288 * @retval -EBUSY if returned without waiting.
1289 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292
1293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * This routine unlocks @a mutex. The mutex must already be locked by the
1297 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001298 *
1299 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 * thread.
1302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304 *
1305 * @return N/A
1306 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307extern void k_mutex_unlock(struct k_mutex *mutex);
1308
1309/* semaphores */
1310
1311struct k_sem {
1312 _wait_q_t wait_q;
1313 unsigned int count;
1314 unsigned int limit;
1315
1316 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1317};
1318
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001319/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001320 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001322 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001324 * @param sem Address of the semaphore.
1325 * @param initial_count Initial semaphore count.
1326 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001327 *
1328 * @return N/A
1329 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1331 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001332
1333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001334 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1339 *
1340 * @param sem Address of the semaphore.
1341 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001342 * or one of the special values K_NO_WAIT and K_FOREVER.
1343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001344 * @retval 0 if successful.
1345 * @retval -EBUSY if returned without waiting.
1346 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001347 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001349
1350/**
1351 * @brief Give a semaphore.
1352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * This routine gives @a sem, unless the semaphore is already at its maximum
1354 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001359 *
1360 * @return N/A
1361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001362extern void k_sem_give(struct k_sem *sem);
1363
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001364/**
1365 * @brief Reset a semaphore's count to zero.
1366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001367 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001369 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001370 *
1371 * @return N/A
1372 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001373static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374{
1375 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001376}
1377
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001378/**
1379 * @brief Get a semaphore's count.
1380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001383 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001385 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001386 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001387static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388{
1389 return sem->count;
1390}
1391
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1393 { \
1394 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1395 .count = initial_count, \
1396 .limit = count_limit, \
1397 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1398 }
1399
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001403 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001404 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001405 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * @param name Name of the semaphore.
1408 * @param initial_count Initial semaphore count.
1409 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001410 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001412 struct k_sem name \
1413 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414 K_SEM_INITIALIZER(name, initial_count, count_limit)
1415
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001416/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001418#define K_ALERT_DEFAULT NULL
1419#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001421typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001422
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001423struct k_alert {
1424 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425 atomic_t send_count;
1426 struct k_work work_item;
1427 struct k_sem sem;
1428
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001429 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430};
1431
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001432extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001434#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001436 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001437 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001438 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001439 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1441 }
1442
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001447 *
1448 * extern struct k_alert @a name;
1449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * @param name Name of the alert.
1451 * @param alert_handler Action to take when alert is sent. Specify either
1452 * the address of a function to be invoked by the system workqueue
1453 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1454 * K_ALERT_DEFAULT (which causes the alert to pend).
1455 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001457#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001458 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001459 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001460 K_ALERT_INITIALIZER(name, alert_handler, \
1461 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * @param alert Address of the alert.
1469 * @param handler Action to take when alert is sent. Specify either the address
1470 * of a function to be invoked by the system workqueue thread,
1471 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1472 * K_ALERT_DEFAULT (which causes the alert to pend).
1473 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474 *
1475 * @return N/A
1476 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001477extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1478 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479
1480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001485 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1486 *
1487 * @param alert Address of the alert.
1488 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 * or one of the special values K_NO_WAIT and K_FOREVER.
1490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * @retval 0 if successful.
1492 * @retval -EBUSY if returned without waiting.
1493 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001495extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496
1497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001500 * This routine signals @a alert. The action specified for @a alert will
1501 * be taken, which may trigger the execution of an alert handler function
1502 * and/or cause the alert to pend (assuming the alert has not reached its
1503 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @note Can be called by ISRs.
1506 *
1507 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 *
1509 * @return N/A
1510 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001511extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001512
1513/**
1514 * data transfers (complex)
1515 */
1516
1517/* message queues */
1518
1519struct k_msgq {
1520 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001521 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001522 uint32_t max_msgs;
1523 char *buffer_start;
1524 char *buffer_end;
1525 char *read_ptr;
1526 char *write_ptr;
1527 uint32_t used_msgs;
1528
1529 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1530};
1531
Peter Mitsis1da807e2016-10-06 11:36:59 -04001532#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533 { \
1534 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001535 .max_msgs = q_max_msgs, \
1536 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001538 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001539 .read_ptr = q_buffer, \
1540 .write_ptr = q_buffer, \
1541 .used_msgs = 0, \
1542 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1543 }
1544
Peter Mitsis1da807e2016-10-06 11:36:59 -04001545/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001546 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001548 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1549 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
1550 * @a q_align -byte boundary. To ensure that each message is aligned to a
1551 * @a q_align -byte boundary, @a q_msg_size must be a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001553 * The message queue can be accessed outside the module where it is defined
1554 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001555 *
1556 * extern struct k_msgq @a name;
1557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001558 * @param q_name Name of the message queue.
1559 * @param q_msg_size Message size (in bytes).
1560 * @param q_max_msgs Maximum number of messages that can be queued.
1561 * @param q_align Alignment of the message queue's ring buffer (power of 2).
Peter Mitsis1da807e2016-10-06 11:36:59 -04001562 */
1563#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1564 static char __noinit __aligned(q_align) \
1565 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001566 struct k_msgq q_name \
1567 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001568 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1569 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570
Peter Mitsisd7a37502016-10-13 11:37:40 -04001571/**
1572 * @brief Initialize a message queue.
1573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * This routine initializes a message queue object, prior to its first use.
1575 *
1576 * @param q Address of the message queue.
1577 * @param buffer Pointer to ring buffer that holds queued messages.
1578 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001579 * @param max_msgs Maximum number of messages that can be queued.
1580 *
1581 * @return N/A
1582 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001583extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001584 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001585
1586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001587 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * @param q Address of the message queue.
1592 * @param data Pointer to the message.
1593 * @param timeout Waiting period to add the message (in milliseconds),
1594 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * @retval 0 if successful.
1597 * @retval -ENOMSG if returned without waiting or after a queue purge.
1598 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001600extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001601
1602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * This routine receives a message from message queue @a q in a "first in,
1606 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * @param q Address of the message queue.
1609 * @param data Address of area to hold the received message.
1610 * @param timeout Waiting period to receive the message (in milliseconds),
1611 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001613 * @retval 0 if successful.
1614 * @retval -ENOMSG if returned without waiting.
1615 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001616 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001618
1619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * This routine discards all unreceived messages in a message queue's ring
1623 * buffer. Any threads that are blocked waiting to send a message to the
1624 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001627 *
1628 * @return N/A
1629 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001630extern void k_msgq_purge(struct k_msgq *q);
1631
Peter Mitsis67be2492016-10-07 11:44:34 -04001632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001635 * This routine returns the number of unused entries in a message queue's
1636 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001638 * @param q Address of the message queue.
1639 *
1640 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001641 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001642static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001643{
1644 return q->max_msgs - q->used_msgs;
1645}
1646
Peter Mitsisd7a37502016-10-13 11:37:40 -04001647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001648 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * @param q Address of the message queue.
1653 *
1654 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001655 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001656static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657{
1658 return q->used_msgs;
1659}
1660
1661struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001662 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663 void *addr_in_pool;
1664 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001665 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001666};
1667
1668/* mailboxes */
1669
1670struct k_mbox_msg {
1671 /** internal use only - needed for legacy API support */
1672 uint32_t _mailbox;
1673 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001674 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675 /** application-defined information value */
1676 uint32_t info;
1677 /** sender's message data buffer */
1678 void *tx_data;
1679 /** internal use only - needed for legacy API support */
1680 void *_rx_data;
1681 /** message data block descriptor */
1682 struct k_mem_block tx_block;
1683 /** source thread id */
1684 k_tid_t rx_source_thread;
1685 /** target thread id */
1686 k_tid_t tx_target_thread;
1687 /** internal use only - thread waiting on send (may be a dummy) */
1688 k_tid_t _syncing_thread;
1689#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1690 /** internal use only - semaphore used during asynchronous send */
1691 struct k_sem *_async_sem;
1692#endif
1693};
1694
1695struct k_mbox {
1696 _wait_q_t tx_msg_queue;
1697 _wait_q_t rx_msg_queue;
1698
1699 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1700};
1701
1702#define K_MBOX_INITIALIZER(obj) \
1703 { \
1704 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1705 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1706 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1707 }
1708
Peter Mitsis12092702016-10-14 12:57:23 -04001709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713 *
1714 * extern struct k_mbox @a name;
1715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001717 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001719 struct k_mbox name \
1720 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001721 K_MBOX_INITIALIZER(name) \
1722
Peter Mitsis12092702016-10-14 12:57:23 -04001723/**
1724 * @brief Initialize a mailbox.
1725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * This routine initializes a mailbox object, prior to its first use.
1727 *
1728 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001729 *
1730 * @return N/A
1731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732extern void k_mbox_init(struct k_mbox *mbox);
1733
Peter Mitsis12092702016-10-14 12:57:23 -04001734/**
1735 * @brief Send a mailbox message in a synchronous manner.
1736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * This routine sends a message to @a mbox and waits for a receiver to both
1738 * receive and process it. The message data may be in a buffer, in a memory
1739 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @param mbox Address of the mailbox.
1742 * @param tx_msg Address of the transmit message descriptor.
1743 * @param timeout Waiting period for the message to be received (in
1744 * milliseconds), or one of the special values K_NO_WAIT
1745 * and K_FOREVER. Once the message has been received,
1746 * this routine waits as long as necessary for the message
1747 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @retval 0 if successful.
1750 * @retval -ENOMSG if returned without waiting.
1751 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001752 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001753extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001755
1756#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1757/**
1758 * @brief Send a mailbox message in an asynchronous manner.
1759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * This routine sends a message to @a mbox without waiting for a receiver
1761 * to process it. The message data may be in a buffer, in a memory pool block,
1762 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
1763 * will be given when the message has been both received and completely
1764 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04001765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @param mbox Address of the mailbox.
1767 * @param tx_msg Address of the transmit message descriptor.
1768 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04001769 *
1770 * @return N/A
1771 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001772extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001773 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001774#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001775
Peter Mitsis12092702016-10-14 12:57:23 -04001776/**
1777 * @brief Receive a mailbox message.
1778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * This routine receives a message from @a mbox, then optionally retrieves
1780 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001782 * @param mbox Address of the mailbox.
1783 * @param rx_msg Address of the receive message descriptor.
1784 * @param buffer Address of the buffer to receive data, or NULL to defer data
1785 * retrieval and message disposal until later.
1786 * @param timeout Waiting period for a message to be received (in
1787 * milliseconds), or one of the special values K_NO_WAIT
1788 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @retval 0 if successful.
1791 * @retval -ENOMSG if returned without waiting.
1792 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001793 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001794extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001796
1797/**
1798 * @brief Retrieve mailbox message data into a buffer.
1799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * This routine completes the processing of a received message by retrieving
1801 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04001802 *
1803 * Alternatively, this routine can be used to dispose of a received message
1804 * without retrieving its data.
1805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * @param rx_msg Address of the receive message descriptor.
1807 * @param buffer Address of the buffer to receive data, or NULL to discard
1808 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04001809 *
1810 * @return N/A
1811 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001812extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001813
1814/**
1815 * @brief Retrieve mailbox message data into a memory pool block.
1816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * This routine completes the processing of a received message by retrieving
1818 * its data into a memory pool block, then disposing of the message.
1819 * The memory pool block that results from successful retrieval must be
1820 * returned to the pool once the data has been processed, even in cases
1821 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04001822 *
1823 * Alternatively, this routine can be used to dispose of a received message
1824 * without retrieving its data. In this case there is no need to return a
1825 * memory pool block to the pool.
1826 *
1827 * This routine allocates a new memory pool block for the data only if the
1828 * data is not already in one. If a new block cannot be allocated, the routine
1829 * returns a failure code and the received message is left unchanged. This
1830 * permits the caller to reattempt data retrieval at a later time or to dispose
1831 * of the received message without retrieving its data.
1832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001833 * @param rx_msg Address of a receive message descriptor.
1834 * @param pool Address of memory pool, or NULL to discard data.
1835 * @param block Address of the area to hold memory pool block info.
1836 * @param timeout Waiting period to wait for a memory pool block (in
1837 * milliseconds), or one of the special values K_NO_WAIT
1838 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04001839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * @retval 0 if successful.
1841 * @retval -ENOMEM if returned without waiting.
1842 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001843 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001844extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001845 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001846 struct k_mem_block *block, int32_t timeout);
1847
1848/* pipes */
1849
1850struct k_pipe {
1851 unsigned char *buffer; /* Pipe buffer: may be NULL */
1852 size_t size; /* Buffer size */
1853 size_t bytes_used; /* # bytes used in buffer */
1854 size_t read_index; /* Where in buffer to read from */
1855 size_t write_index; /* Where in buffer to write */
1856
1857 struct {
1858 _wait_q_t readers; /* Reader wait queue */
1859 _wait_q_t writers; /* Writer wait queue */
1860 } wait_q;
1861
1862 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1863};
1864
Peter Mitsise5d9c582016-10-14 14:44:57 -04001865#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866 { \
1867 .buffer = pipe_buffer, \
1868 .size = pipe_buffer_size, \
1869 .bytes_used = 0, \
1870 .read_index = 0, \
1871 .write_index = 0, \
1872 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1873 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1874 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1875 }
1876
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
1882 * extern struct k_pipe @a name;
1883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * @param name Name of the pipe.
1885 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
1886 * or zero if no ring buffer is used.
1887 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001889#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1890 static unsigned char __noinit __aligned(pipe_align) \
1891 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001892 struct k_pipe name \
1893 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001894 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001899 * This routine initializes a pipe object, prior to its first use.
1900 *
1901 * @param pipe Address of the pipe.
1902 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
1903 * is used.
1904 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
1905 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001906 *
1907 * @return N/A
1908 */
1909extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1910 size_t size);
1911
1912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * @param pipe Address of the pipe.
1918 * @param data Address of data to write.
1919 * @param bytes_to_write Size of data (in bytes).
1920 * @param bytes_written Address of area to hold the number of bytes written.
1921 * @param min_xfer Minimum number of bytes to write.
1922 * @param timeout Waiting period to wait for the data to be written (in
1923 * milliseconds), or one of the special values K_NO_WAIT
1924 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001926 * @retval 0 if at least @a min_xfer data bytes were written.
1927 * @retval -EIO if returned without waiting; zero data bytes were written.
1928 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1929 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001931extern int k_pipe_put(struct k_pipe *pipe, void *data,
1932 size_t bytes_to_write, size_t *bytes_written,
1933 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934
1935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001936 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001938 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * @param pipe Address of the pipe.
1941 * @param data Address to place the data read from pipe.
1942 * @param bytes_to_read Maximum number of data bytes to read.
1943 * @param bytes_read Address of area to hold the number of bytes read.
1944 * @param min_xfer Minimum number of data bytes to read.
1945 * @param timeout Waiting period to wait for the data to be read (in
1946 * milliseconds), or one of the special values K_NO_WAIT
1947 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * @retval 0 if at least @a min_xfer data bytes were read.
1950 * @retval -EIO if returned without waiting; zero data bytes were read.
1951 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
1952 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001953 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001954extern int k_pipe_get(struct k_pipe *pipe, void *data,
1955 size_t bytes_to_read, size_t *bytes_read,
1956 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957
Peter Mitsis2fef0232016-10-14 14:53:44 -04001958#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001960 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001962 * This routine writes the data contained in a memory block to @a pipe.
1963 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001966 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967 * @param block Memory block containing data to send
1968 * @param size Number of data bytes in memory block to send
1969 * @param sem Semaphore to signal upon completion (else NULL)
1970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001972 */
1973extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1974 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001975#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976
1977/**
1978 * memory management
1979 */
1980
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001981/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001983struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001985 uint32_t num_blocks;
1986 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001987 char *buffer;
1988 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001989 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001991 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992};
1993
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001994#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
1995 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996 { \
1997 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001998 .num_blocks = slab_num_blocks, \
1999 .block_size = slab_block_size, \
2000 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002001 .free_list = NULL, \
2002 .num_used = 0, \
2003 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2004 }
2005
Peter Mitsis578f9112016-10-07 13:50:31 -04002006/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @brief Statically define and initialize a memory slab allocator.
Peter Mitsis578f9112016-10-07 13:50:31 -04002008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * The slab allocator's buffer contains @a slab_num_blocks memory blocks
2010 * that are @a slab_block_size bytes long. The buffer is aligned to a
2011 * @a slab_align -byte boundary. To ensure that each memory block is aligned
2012 * to a @a slab_align -byte boundary, @a slab_block_size must be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002013 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002015 * The slab allocator can be accessed outside the module where it is defined
2016 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
2018 * extern struct k_mem_slab @a name;
2019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * @param name Name of the memory slab.
2021 * @param slab_block_size Size of each memory block (in bytes).
2022 * @param slab_num_blocks Number memory blocks.
2023 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002024 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002025#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2026 char __noinit __aligned(slab_align) \
2027 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2028 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002029 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002030 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2031 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002033/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002034 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @param slab Address of the memory slab.
2039 * @param buffer Pointer to buffer used for the memory blocks.
2040 * @param block_size Size of each memory block (in bytes).
2041 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002042 *
2043 * @return N/A
2044 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002045extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002046 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002047
2048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002049 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002051 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002053 * @param slab Address of the memory slab.
2054 * @param mem Pointer to block address area.
2055 * @param timeout Maximum time to wait for operation to complete
2056 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2057 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002059 * @retval 0 if successful. The block address area pointed at by @a mem
2060 * is set to the starting address of the memory block.
2061 * @retval -ENOMEM if failed immediately.
2062 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002063 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002064extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2065 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002066
2067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070 * This routine releases a previously allocated memory block back to its
2071 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @param slab Address of the memory slab.
2074 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002075 *
2076 * @return N/A
2077 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002078extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002080/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002081 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002083 * This routine gets the number of memory blocks that are currently
2084 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002086 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002089 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002090static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002092 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093}
2094
Peter Mitsisc001aa82016-10-13 13:53:37 -04002095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * This routine gets the number of memory blocks that are currently
2099 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002104 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002105static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002106{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002107 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002108}
2109
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110/* memory pools */
2111
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002112/*
2113 * Memory pool requires a buffer and two arrays of structures for the
2114 * memory block accounting:
2115 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2116 * status of four blocks of memory.
2117 */
2118struct k_mem_pool_quad_block {
2119 char *mem_blocks; /* pointer to the first of four memory blocks */
2120 uint32_t mem_status; /* four bits. If bit is set, memory block is
2121 allocated */
2122};
2123/*
2124 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2125 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2126 * block size is 4 times less than the previous one and thus requires 4 times
2127 * bigger array of k_mem_pool_quad_block structures to keep track of the
2128 * memory blocks.
2129 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002130
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002131/*
2132 * The array of k_mem_pool_block_set keeps the information of each array of
2133 * k_mem_pool_quad_block structures
2134 */
2135struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002136 size_t block_size; /* memory block size */
2137 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002138 struct k_mem_pool_quad_block *quad_block;
2139 int count;
2140};
2141
2142/* Memory pool descriptor */
2143struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002144 size_t max_block_size;
2145 size_t min_block_size;
2146 uint32_t nr_of_maxblocks;
2147 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002148 struct k_mem_pool_block_set *block_set;
2149 char *bufblock;
2150 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2152};
2153
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002154#ifdef CONFIG_ARM
2155#define _SECTION_TYPE_SIGN "%"
2156#else
2157#define _SECTION_TYPE_SIGN "@"
2158#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002160/*
2161 * Static memory pool initialization
2162 */
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002163/**
2164 * @cond internal
2165 * Make Doxygen skip assembler macros
2166 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002167/*
2168 * Use .altmacro to be able to recalculate values and pass them as string
2169 * arguments when calling assembler macros resursively
2170 */
2171__asm__(".altmacro\n\t");
2172
2173/*
2174 * Recursively calls a macro
2175 * The followig global symbols need to be initialized:
2176 * __memory_pool_max_block_size - maximal size of the memory block
2177 * __memory_pool_min_block_size - minimal size of the memory block
2178 * Notes:
2179 * Global symbols are used due the fact that assembler macro allows only
2180 * one argument be passed with the % conversion
2181 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2182 * is used instead of / 4.
2183 * n_max argument needs to go first in the invoked macro, as some
2184 * assemblers concatenate \name and %(\n_max * 4) arguments
2185 * if \name goes first
2186 */
2187__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2188 ".ifge __memory_pool_max_block_size >> 2 -"
2189 " __memory_pool_min_block_size\n\t\t"
2190 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2191 "\\macro_name %(\\n_max * 4) \\name\n\t"
2192 ".endif\n\t"
2193 ".endm\n");
2194
2195/*
2196 * Build quad blocks
2197 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2198 * structures and recursively calls itself for the next array, 4 times
2199 * larger.
2200 * The followig global symbols need to be initialized:
2201 * __memory_pool_max_block_size - maximal size of the memory block
2202 * __memory_pool_min_block_size - minimal size of the memory block
2203 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2204 */
2205__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002206 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002207 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2208 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2209 ".if \\n_max % 4\n\t\t"
2210 ".skip __memory_pool_quad_block_size\n\t"
2211 ".endif\n\t"
2212 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2213 ".endm\n");
2214
2215/*
2216 * Build block sets and initialize them
2217 * Macro initializes the k_mem_pool_block_set structure and
2218 * recursively calls itself for the next one.
2219 * The followig global symbols need to be initialized:
2220 * __memory_pool_max_block_size - maximal size of the memory block
2221 * __memory_pool_min_block_size - minimal size of the memory block
2222 * __memory_pool_block_set_count, the number of the elements in the
2223 * block set array must be set to 0. Macro calculates it's real
2224 * value.
2225 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2226 * structures, _build_quad_blocks must be called prior it.
2227 */
2228__asm__(".macro _build_block_set n_max, name\n\t"
2229 ".int __memory_pool_max_block_size\n\t" /* block_size */
2230 ".if \\n_max % 4\n\t\t"
2231 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2232 ".else\n\t\t"
2233 ".int \\n_max >> 2\n\t"
2234 ".endif\n\t"
2235 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2236 ".int 0\n\t" /* count */
2237 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2238 "__do_recurse _build_block_set \\name \\n_max\n\t"
2239 ".endm\n");
2240
2241/*
2242 * Build a memory pool structure and initialize it
2243 * Macro uses __memory_pool_block_set_count global symbol,
2244 * block set addresses and buffer address, it may be called only after
2245 * _build_block_set
2246 */
2247__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002248 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002249 _SECTION_TYPE_SIGN "progbits\n\t"
2250 ".globl \\name\n\t"
2251 "\\name:\n\t"
2252 ".int \\max_size\n\t" /* max_block_size */
2253 ".int \\min_size\n\t" /* min_block_size */
2254 ".int \\n_max\n\t" /* nr_of_maxblocks */
2255 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2256 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2257 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2258 ".int 0\n\t" /* wait_q->head */
2259 ".int 0\n\t" /* wait_q->next */
2260 ".popsection\n\t"
2261 ".endm\n");
2262
2263#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2264 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2265 _SECTION_TYPE_SIGN "progbits\n\t"); \
2266 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2267 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2268 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2269 STRINGIFY(name) "\n\t"); \
2270 __asm__(".popsection\n\t")
2271
2272#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2273 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2274 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2275 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2276 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002277 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002278 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2279 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2280 STRINGIFY(name) "\n\t"); \
2281 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2282 __asm__(".int __memory_pool_block_set_count\n\t"); \
2283 __asm__(".popsection\n\t"); \
2284 extern uint32_t _mem_pool_block_set_count_##name; \
2285 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2286
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002287#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2288 char __noinit __aligned(align) \
2289 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002290
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002291/*
2292 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2293 * to __memory_pool_quad_block_size absolute symbol.
2294 * This function does not get called, but compiler calculates the value and
2295 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2296 */
2297static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2298{
2299 __asm__(".globl __memory_pool_quad_block_size\n\t"
2300#ifdef CONFIG_NIOS2
2301 "__memory_pool_quad_block_size = %0\n\t"
2302#else
2303 "__memory_pool_quad_block_size = %c0\n\t"
2304#endif
2305 :
2306 : "n"(sizeof(struct k_mem_pool_quad_block)));
2307}
2308
2309/**
2310 * @endcond
2311 * End of assembler macros that Doxygen has to skip
2312 */
2313
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002314/**
2315 * @brief Define a memory pool
2316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * This defines and initializes a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2320 * long. The memory pool allows blocks to be repeatedly partitioned into
2321 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2322 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
2323 * aligned to @a align -byte boundary, @a min_size must be a multiple of
2324 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002325 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 * If the pool is to be accessed outside the module where it is defined, it
2327 * can be declared via
2328 *
2329 * extern struct k_mem_pool @a name;
2330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * @param name Name of the memory pool.
2332 * @param min_size Size of the smallest blocks in the pool (in bytes).
2333 * @param max_size Size of the largest blocks in the pool (in bytes).
2334 * @param n_max Number of maximum sized blocks in the pool.
2335 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002336 */
2337#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002338 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2339 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002340 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002341 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2342 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2343 extern struct k_mem_pool name
2344
Peter Mitsis937042c2016-10-13 13:18:26 -04002345/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @param pool Address of the memory pool.
2351 * @param block Pointer to block descriptor for the allocated memory.
2352 * @param size Amount of memory to allocate (in bytes).
2353 * @param timeout Maximum time to wait for operation to complete
2354 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2355 * or K_FOREVER to wait as long as necessary.
2356 *
2357 * @retval 0 if successful. The @a data field of the block descriptor
2358 * is set to the starting address of the memory block.
2359 * @retval -ENOMEM if unable to allocate a memory block.
2360 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002361 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002362extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002363 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002364
2365/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * This routine releases a previously allocated memory block back to its
2369 * memory pool.
2370 *
2371 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002372 *
2373 * @return N/A
2374 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002376
2377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * This routine instructs a memory pool to concatenate unused memory blocks
2381 * into larger blocks wherever possible. Manually defragmenting the memory
2382 * pool may speed up future allocations of memory blocks by eliminating the
2383 * need for the memory pool to perform an automatic partial defragmentation.
2384 *
2385 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002386 *
2387 * @return N/A
2388 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002389extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002390
2391/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002395 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002400 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002401extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002402
2403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002405 *
2406 * This routine provides traditional free() semantics. The memory being
2407 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002410 *
2411 * @return N/A
2412 */
2413extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414
2415/*
2416 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2417 * hook into the device subsystem, which itself uses nanokernel semaphores,
2418 * and thus currently requires the definition of nano_sem.
2419 */
2420#include <legacy.h>
2421#include <arch/cpu.h>
2422
2423/*
2424 * private APIs that are utilized by one or more public APIs
2425 */
2426
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002429extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430
2431#ifdef __cplusplus
2432}
2433#endif
2434
Andrew Boiee004dec2016-11-07 09:01:19 -08002435#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2436/*
2437 * Define new and delete operators.
2438 * At this moment, the operators do nothing since objects are supposed
2439 * to be statically allocated.
2440 */
2441inline void operator delete(void *ptr)
2442{
2443 (void)ptr;
2444}
2445
2446inline void operator delete[](void *ptr)
2447{
2448 (void)ptr;
2449}
2450
2451inline void *operator new(size_t size)
2452{
2453 (void)size;
2454 return NULL;
2455}
2456
2457inline void *operator new[](size_t size)
2458{
2459 (void)size;
2460 return NULL;
2461}
2462
2463/* Placement versions of operator new and delete */
2464inline void operator delete(void *ptr1, void *ptr2)
2465{
2466 (void)ptr1;
2467 (void)ptr2;
2468}
2469
2470inline void operator delete[](void *ptr1, void *ptr2)
2471{
2472 (void)ptr1;
2473 (void)ptr2;
2474}
2475
2476inline void *operator new(size_t size, void *ptr)
2477{
2478 (void)size;
2479 return ptr;
2480}
2481
2482inline void *operator new[](size_t size, void *ptr)
2483{
2484 (void)size;
2485 return ptr;
2486}
2487
2488#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2489
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002490#endif /* _kernel__h_ */