blob: d1888133698ae222d4fed1cfde7258d449598c36 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
49#define K_FOREVER (-1)
50#define K_NO_WAIT 0
51
52#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
Benjamin Walshfab8d922016-11-08 15:36:36 -050067#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
74#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
75#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
77#else
78#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
79#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
80#endif
81
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050082#define tcs k_thread
83struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400105/**
Allan Stephensc98da842016-11-11 15:45:03 -0500106 * @defgroup thread_apis Thread APIs
107 * @ingroup kernel_apis
108 * @{
109 */
110
111/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500112 * @typedef k_thread_entry_t
113 * @brief Thread entry point function type.
114 *
115 * A thread's entry point function is invoked when the thread starts executing.
116 * Up to 3 argument values can be passed to the function.
117 *
118 * The thread terminates execution permanently if the entry point function
119 * returns. The thread is responsible for releasing any shared resources
120 * it may own (such as mutexes and dynamically allocated memory), prior to
121 * returning.
122 *
123 * @param p1 First argument.
124 * @param p2 Second argument.
125 * @param p3 Third argument.
126 *
127 * @return N/A
128 */
129typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
130
131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500132 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500134 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500136 * The new thread may be scheduled for immediate execution or a delayed start.
137 * If the newly spawned thread does not have a delayed start the kernel
138 * scheduler may preempt the current thread to allow the new thread to
139 * execute.
140 *
141 * Thread options are architecture-specific, and can include K_ESSENTIAL,
142 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
143 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400144 *
145 * @param stack Pointer to the stack space.
146 * @param stack_size Stack size in bytes.
147 * @param entry Thread entry function.
148 * @param p1 1st entry point parameter.
149 * @param p2 2nd entry point parameter.
150 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500151 * @param prio Thread priority.
152 * @param options Thread options.
153 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500155 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500158 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159 void *p1, void *p2, void *p3,
160 int32_t prio, uint32_t options, int32_t delay);
161
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500163 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400164 *
Allan Stephensc98da842016-11-11 15:45:03 -0500165 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500168 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400169 *
170 * @return N/A
171 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400172extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400173
174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500175 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400176 *
177 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500178 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400179 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180 * @return N/A
181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400182extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400183
184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500185 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500187 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500189 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400190 *
191 * @return N/A
192 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400193extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400194
195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500200 * If @a thread is not currently sleeping, the routine has no effect.
201 *
202 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400203 *
204 * @return N/A
205 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207
208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500209 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500211 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400213extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214
215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500216 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500218 * This routine prevents @a thread from executing if it has not yet started
219 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500221 * @param thread ID of thread to cancel.
222 *
223 * @retval 0 if successful.
224 * @retval -EINVAL if the thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226extern int k_thread_cancel(k_tid_t thread);
227
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228/**
Allan Stephensc98da842016-11-11 15:45:03 -0500229 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500231 * This routine permanently stops execution of @a thread. The thread is taken
232 * off all kernel queues it is part of (i.e. the ready queue, the timeout
233 * queue, or a kernel object wait queue). However, any kernel resources the
234 * thread might currently own (such as mutexes or memory blocks) are not
235 * released. It is the responsibility of the caller of this routine to ensure
236 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500238 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400239 *
240 * @return N/A
241 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400242extern void k_thread_abort(k_tid_t thread);
243
Allan Stephensc98da842016-11-11 15:45:03 -0500244/**
245 * @cond INTERNAL_HIDDEN
246 */
247
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400248#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249#define _THREAD_TIMEOUT_INIT(obj) \
250 (obj).nano_timeout = { \
251 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400252 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400253 .wait_q = NULL, \
254 .delta_ticks_from_prev = -1, \
255 },
256#else
257#define _THREAD_TIMEOUT_INIT(obj)
258#endif
259
260#ifdef CONFIG_ERRNO
261#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
262#else
263#define _THREAD_ERRNO_INIT(obj)
264#endif
265
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400266struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400267 union {
268 char *init_stack;
269 struct k_thread *thread;
270 };
271 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500272 void (*init_entry)(void *, void *, void *);
273 void *init_p1;
274 void *init_p2;
275 void *init_p3;
276 int init_prio;
277 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400278 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500279 void (*init_abort)(void);
280 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400281};
282
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400283#define _THREAD_INITIALIZER(stack, stack_size, \
284 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500285 prio, options, delay, abort, groups) \
286 { \
287 .init_stack = (stack), \
288 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400289 .init_entry = (void (*)(void *, void *, void *))entry, \
290 .init_p1 = (void *)p1, \
291 .init_p2 = (void *)p2, \
292 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500293 .init_prio = (prio), \
294 .init_options = (options), \
295 .init_delay = (delay), \
296 .init_abort = (abort), \
297 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400298 }
299
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400300/**
Allan Stephensc98da842016-11-11 15:45:03 -0500301 * INTERNAL_HIDDEN @endcond
302 */
303
304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500305 * @brief Statically define and initialize a thread.
306 *
307 * The thread may be scheduled for immediate execution or a delayed start.
308 *
309 * Thread options are architecture-specific, and can include K_ESSENTIAL,
310 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
311 * them using "|" (the logical OR operator).
312 *
313 * The ID of the thread can be accessed using:
314 *
315 * extern const k_tid_t @a name;
316 *
317 * @param name Name of the thread.
318 * @param stack_size Stack size in bytes.
319 * @param entry Thread entry function.
320 * @param p1 1st entry point parameter.
321 * @param p2 2nd entry point parameter.
322 * @param p3 3rd entry point parameter.
323 * @param prio Thread priority.
324 * @param options Thread options.
325 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400326 *
327 * @internal It has been observed that the x86 compiler by default aligns
328 * these _static_thread_data structures to 32-byte boundaries, thereby
329 * wasting space. To work around this, force a 4-byte alignment.
330 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500331#define K_THREAD_DEFINE(name, stack_size, \
332 entry, p1, p2, p3, \
333 prio, options, delay) \
334 char __noinit __stack _k_thread_obj_##name[stack_size]; \
335 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500336 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500337 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
338 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500339 NULL, 0); \
340 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400341
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500343 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500345 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500347 * @param thread ID of thread whose priority is needed.
348 *
349 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400350 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500351extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352
353/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500354 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500356 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400357 *
358 * Rescheduling can occur immediately depending on the priority @a thread is
359 * set to:
360 *
361 * - If its priority is raised above the priority of the caller of this
362 * function, and the caller is preemptible, @a thread will be scheduled in.
363 *
364 * - If the caller operates on itself, it lowers its priority below that of
365 * other threads in the system, and the caller is preemptible, the thread of
366 * highest priority will be scheduled in.
367 *
368 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
369 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
370 * highest priority.
371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500372 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400373 * @param prio New priority.
374 *
375 * @warning Changing the priority of a thread currently involved in mutex
376 * priority inheritance may result in undefined behavior.
377 *
378 * @return N/A
379 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400380extern void k_thread_priority_set(k_tid_t thread, int prio);
381
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500383 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * This routine prevents the kernel scheduler from making @a thread the
386 * current thread. All other internal operations on @a thread are still
387 * performed; for example, any timeout it is waiting on keeps ticking,
388 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500390 * If @a thread is already suspended, the routine has no effect.
391 *
392 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400393 *
394 * @return N/A
395 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400396extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400397
398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500399 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500401 * This routine allows the kernel scheduler to make @a thread the current
402 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500404 * If @a thread is not currently suspended, the routine has no effect.
405 *
406 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 *
408 * @return N/A
409 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400410extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400411
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500413 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500415 * This routine specifies how the scheduler will perform time slicing of
416 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500418 * To enable time slicing, @a slice must be non-zero. The scheduler
419 * ensures that no thread runs for more than the specified time limit
420 * before other threads of that priority are given a chance to execute.
421 * Any thread whose priority is higher than @a prio is exempted, and may
422 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400425 * execute. Once the scheduler selects a thread for execution, there is no
426 * minimum guaranteed time the thread will execute before threads of greater or
427 * equal priority are scheduled.
428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500429 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 * for execution, this routine has no effect; the thread is immediately
431 * rescheduled after the slice period expires.
432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500433 * To disable timeslicing, set both @a slice and @a prio to zero.
434 *
435 * @param slice Maximum time slice length (in milliseconds).
436 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * @return N/A
439 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400440extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400441
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442/**
Allan Stephensc98da842016-11-11 15:45:03 -0500443 * @} end defgroup thread_apis
444 */
445
446/**
447 * @addtogroup isr_apis
448 * @{
449 */
450
451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 *
Allan Stephensc98da842016-11-11 15:45:03 -0500454 * This routine allows the caller to customize its actions, depending on
455 * whether it is a thread or an ISR.
456 *
457 * @note Can be called by ISRs.
458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * @return 0 if invoked by a thread.
460 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400461 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500462extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400463
Benjamin Walsh445830d2016-11-10 15:54:27 -0500464/**
465 * @brief Determine if code is running in a preemptible thread.
466 *
Allan Stephensc98da842016-11-11 15:45:03 -0500467 * This routine allows the caller to customize its actions, depending on
468 * whether it can be preempted by another thread. The routine returns a 'true'
469 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500470 *
Allan Stephensc98da842016-11-11 15:45:03 -0500471 * - The code is running in a thread, not at ISR.
472 * - The thread's priority is in the preemptible range.
473 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500474 *
Allan Stephensc98da842016-11-11 15:45:03 -0500475 * @note Can be called by ISRs.
476 *
477 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500478 * @return Non-zero if invoked by a preemptible thread.
479 */
480extern int k_is_preempt_thread(void);
481
Allan Stephensc98da842016-11-11 15:45:03 -0500482/**
483 * @} end addtogroup isr_apis
484 */
485
486/**
487 * @addtogroup thread_apis
488 * @{
489 */
490
491/**
492 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500493 *
Allan Stephensc98da842016-11-11 15:45:03 -0500494 * This routine prevents the current thread from being preempted by another
495 * thread by instructing the scheduler to treat it as a cooperative thread.
496 * If the thread subsequently performs an operation that makes it unready,
497 * it will be context switched out in the normal manner. When the thread
498 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500499 *
Allan Stephensc98da842016-11-11 15:45:03 -0500500 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500501 *
Allan Stephensc98da842016-11-11 15:45:03 -0500502 * @note k_sched_lock() and k_sched_unlock() should normally be used
503 * when the operation being performed can be safely interrupted by ISRs.
504 * However, if the amount of processing involved is very small, better
505 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500506 *
507 * @return N/A
508 */
509extern void k_sched_lock(void);
510
Allan Stephensc98da842016-11-11 15:45:03 -0500511/**
512 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500513 *
Allan Stephensc98da842016-11-11 15:45:03 -0500514 * This routine reverses the effect of a previous call to k_sched_lock().
515 * A thread must call the routine once for each time it called k_sched_lock()
516 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500517 *
518 * @return N/A
519 */
520extern void k_sched_unlock(void);
521
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500523 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500525 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500527 * Custom data is not used by the kernel itself, and is freely available
528 * for a thread to use as it sees fit. It can be used as a framework
529 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532 *
533 * @return N/A
534 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536
537/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500538 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500540 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500542 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400544extern void *k_thread_custom_data_get(void);
545
546/**
Allan Stephensc98da842016-11-11 15:45:03 -0500547 * @} end addtogroup thread_apis
548 */
549
550/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400551 * kernel timing
552 */
553
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400554#include <sys_clock.h>
555
Johan Hedberg14471692016-11-13 10:52:15 +0200556/* Convenience helpers to convert durations into milliseconds */
557#define K_MSEC(ms) (ms)
558#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
559#define K_MINUTES(m) K_SECONDS((m) * 60)
560#define K_HOURS(h) K_MINUTES((h) * 60)
561
Allan Stephensc98da842016-11-11 15:45:03 -0500562/**
563 * @cond INTERNAL_HIDDEN
564 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400565
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500566/* added tick needed to account for tick in progress */
567#define _TICK_ALIGN 1
568
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400569static int64_t __ticks_to_ms(int64_t ticks)
570{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400571#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400572 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400573#else
574 __ASSERT(ticks == 0, "");
575 return 0;
576#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400577}
578
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579/* timeouts */
580
581struct _timeout;
582typedef void (*_timeout_func_t)(struct _timeout *t);
583
584struct _timeout {
585 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400586 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400587 sys_dlist_t *wait_q;
588 int32_t delta_ticks_from_prev;
589 _timeout_func_t func;
590};
591
Allan Stephensc98da842016-11-11 15:45:03 -0500592/**
593 * INTERNAL_HIDDEN @endcond
594 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500595
Allan Stephensc98da842016-11-11 15:45:03 -0500596/**
597 * @cond INTERNAL_HIDDEN
598 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400599
600struct k_timer {
601 /*
602 * _timeout structure must be first here if we want to use
603 * dynamic timer allocation. timeout.node is used in the double-linked
604 * list of free timers
605 */
606 struct _timeout timeout;
607
Allan Stephens45bfa372016-10-12 12:39:42 -0500608 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400609 _wait_q_t wait_q;
610
611 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500612 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400613
614 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500615 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400616
617 /* timer period */
618 int32_t period;
619
Allan Stephens45bfa372016-10-12 12:39:42 -0500620 /* timer status */
621 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622
Allan Stephens45bfa372016-10-12 12:39:42 -0500623 /* used to support legacy timer APIs */
624 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400625
626 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
627};
628
Allan Stephens1342adb2016-11-03 13:54:53 -0500629#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400630 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500631 .timeout.delta_ticks_from_prev = -1, \
632 .timeout.wait_q = NULL, \
633 .timeout.thread = NULL, \
634 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400635 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500636 .expiry_fn = expiry, \
637 .stop_fn = stop, \
638 .status = 0, \
639 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400640 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
641 }
642
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400643/**
Allan Stephensc98da842016-11-11 15:45:03 -0500644 * INTERNAL_HIDDEN @endcond
645 */
646
647/**
648 * @defgroup timer_apis Timer APIs
649 * @ingroup kernel_apis
650 * @{
651 */
652
653/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500654 * @typedef k_timer_expiry_t
655 * @brief Timer expiry function type.
656 *
657 * A timer's expiry function is executed by the system clock interrupt handler
658 * each time the timer expires. The expiry function is optional, and is only
659 * invoked if the timer has been initialized with one.
660 *
661 * @param timer Address of timer.
662 *
663 * @return N/A
664 */
665typedef void (*k_timer_expiry_t)(struct k_timer *timer);
666
667/**
668 * @typedef k_timer_stop_t
669 * @brief Timer stop function type.
670 *
671 * A timer's stop function is executed if the timer is stopped prematurely.
672 * The function runs in the context of the thread that stops the timer.
673 * The stop function is optional, and is only invoked if the timer has been
674 * initialized with one.
675 *
676 * @param timer Address of timer.
677 *
678 * @return N/A
679 */
680typedef void (*k_timer_stop_t)(struct k_timer *timer);
681
682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686 *
687 * extern struct k_timer @a name;
688 *
689 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * @param expiry_fn Function to invoke each time the timer expires.
691 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500693#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500694 struct k_timer name \
695 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500696 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400697
Allan Stephens45bfa372016-10-12 12:39:42 -0500698/**
699 * @brief Initialize a timer.
700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500702 *
703 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * @param expiry_fn Function to invoke each time the timer expires.
705 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500706 *
707 * @return N/A
708 */
709extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500710 k_timer_expiry_t expiry_fn,
711 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700712
Allan Stephens45bfa372016-10-12 12:39:42 -0500713/**
714 * @brief Start a timer.
715 *
716 * This routine starts a timer, and resets its status to zero. The timer
717 * begins counting down using the specified duration and period values.
718 *
719 * Attempting to start a timer that is already running is permitted.
720 * The timer's status is reset to zero and the timer begins counting down
721 * using the new duration and period values.
722 *
723 * @param timer Address of timer.
724 * @param duration Initial timer duration (in milliseconds).
725 * @param period Timer period (in milliseconds).
726 *
727 * @return N/A
728 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400729extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500730 int32_t duration, int32_t period);
731
732/**
733 * @brief Stop a timer.
734 *
735 * This routine stops a running timer prematurely. The timer's stop function,
736 * if one exists, is invoked by the caller.
737 *
738 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500739 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500740 *
741 * @param timer Address of timer.
742 *
743 * @return N/A
744 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400745extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500746
747/**
748 * @brief Read timer status.
749 *
750 * This routine reads the timer's status, which indicates the number of times
751 * it has expired since its status was last read.
752 *
753 * Calling this routine resets the timer's status to zero.
754 *
755 * @param timer Address of timer.
756 *
757 * @return Timer status.
758 */
759extern uint32_t k_timer_status_get(struct k_timer *timer);
760
761/**
762 * @brief Synchronize thread to timer expiration.
763 *
764 * This routine blocks the calling thread until the timer's status is non-zero
765 * (indicating that it has expired at least once since it was last examined)
766 * or the timer is stopped. If the timer status is already non-zero,
767 * or the timer is already stopped, the caller continues without waiting.
768 *
769 * Calling this routine resets the timer's status to zero.
770 *
771 * This routine must not be used by interrupt handlers, since they are not
772 * allowed to block.
773 *
774 * @param timer Address of timer.
775 *
776 * @return Timer status.
777 */
778extern uint32_t k_timer_status_sync(struct k_timer *timer);
779
780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500782 *
783 * This routine computes the (approximate) time remaining before a running
784 * timer next expires. If the timer is not running, it returns zero.
785 *
786 * @param timer Address of timer.
787 *
788 * @return Remaining time (in milliseconds).
789 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400790extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400791
Allan Stephensc98da842016-11-11 15:45:03 -0500792/**
793 * @} end defgroup timer_apis
794 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400795
Allan Stephensc98da842016-11-11 15:45:03 -0500796/**
797 * @defgroup clock_apis Kernel Clock APIs
798 * @ingroup kernel_apis
799 * @{
800 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500801
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * This routine returns the elapsed time since the system booted,
806 * in milliseconds.
807 *
808 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400809 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400811
812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * This routine returns the lower 32-bits of the elapsed time since the system
816 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * This routine can be more efficient than k_uptime_get(), as it reduces the
819 * need for interrupt locking and 64-bit math. However, the 32-bit result
820 * cannot hold a system uptime time larger than approximately 50 days, so the
821 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400824 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400825extern uint32_t k_uptime_get_32(void);
826
827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * This routine computes the elapsed time between the current system uptime
831 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @param reftime Pointer to a reference time, which is updated to the current
834 * uptime upon return.
835 *
836 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400837 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400838extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400839
840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * This routine computes the elapsed time between the current system uptime
844 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * This routine can be more efficient than k_uptime_delta(), as it reduces the
847 * need for interrupt locking and 64-bit math. However, the 32-bit result
848 * cannot hold an elapsed time larger than approximately 50 days, so the
849 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @param reftime Pointer to a reference time, which is updated to the current
852 * uptime upon return.
853 *
854 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400855 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400856extern uint32_t k_uptime_delta_32(int64_t *reftime);
857
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * This routine returns the current time, as measured by the system's hardware
862 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866extern uint32_t k_cycle_get_32(void);
867
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400868/**
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * @} end defgroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400870 */
871
Allan Stephensc98da842016-11-11 15:45:03 -0500872/**
873 * @cond INTERNAL_HIDDEN
874 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400875
876struct k_fifo {
877 _wait_q_t wait_q;
878 sys_slist_t data_q;
879
880 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
881};
882
Allan Stephensc98da842016-11-11 15:45:03 -0500883#define K_FIFO_INITIALIZER(obj) \
884 { \
885 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
886 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
887 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
888 }
889
890/**
891 * INTERNAL_HIDDEN @endcond
892 */
893
894/**
895 * @defgroup fifo_apis Fifo APIs
896 * @ingroup kernel_apis
897 * @{
898 */
899
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500903 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 *
907 * @return N/A
908 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400909extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910
911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * This routine adds a data item to @a fifo. A fifo data item must be
915 * aligned on a 4-byte boundary, and the first 32 bits of the item are
916 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @note Can be called by ISRs.
919 *
920 * @param fifo Address of the fifo.
921 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
923 * @return N/A
924 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926
927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * This routine adds a list of data items to @a fifo in one operation.
931 * The data items must be in a singly-linked list, with the first 32 bits
932 * each data item pointing to the next data item; the list must be
933 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * @param fifo Address of the fifo.
938 * @param head Pointer to first node in singly-linked list.
939 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 *
941 * @return N/A
942 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944
945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500946 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * This routine adds a list of data items to @a fifo in one operation.
949 * The data items must be in a singly-linked list implemented using a
950 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951 * and must be re-initialized via sys_slist_init().
952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * @note Can be called by ISRs.
954 *
955 * @param fifo Address of the fifo.
956 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400957 *
958 * @return N/A
959 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400960extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961
962/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500963 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * This routine removes a data item from @a fifo in a "first in, first out"
966 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
969 *
970 * @param fifo Address of the fifo.
971 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400972 * or one of the special values K_NO_WAIT and K_FOREVER.
973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * @return Address of the data item if successful.
975 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
978
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500980 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400983 *
984 * extern struct k_fifo @a name;
985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400987 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400988#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500989 struct k_fifo name \
990 __in_section(_k_fifo, static, name) = \
991 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992
Allan Stephensc98da842016-11-11 15:45:03 -0500993/**
994 * @} end defgroup fifo_apis
995 */
996
997/**
998 * @cond INTERNAL_HIDDEN
999 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001000
1001struct k_lifo {
1002 _wait_q_t wait_q;
1003 void *list;
1004
1005 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
1006};
1007
Allan Stephensc98da842016-11-11 15:45:03 -05001008#define K_LIFO_INITIALIZER(obj) \
1009 { \
1010 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1011 .list = NULL, \
1012 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1013 }
1014
1015/**
1016 * INTERNAL_HIDDEN @endcond
1017 */
1018
1019/**
1020 * @defgroup lifo_apis Lifo APIs
1021 * @ingroup kernel_apis
1022 * @{
1023 */
1024
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001031 *
1032 * @return N/A
1033 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001034extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035
1036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * This routine adds a data item to @a lifo. A lifo data item must be
1040 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1041 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * @note Can be called by ISRs.
1044 *
1045 * @param lifo Address of the lifo.
1046 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
1048 * @return N/A
1049 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001050extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051
1052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001053 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * This routine removes a data item from @a lifo in a "last in, first out"
1056 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1059 *
1060 * @param lifo Address of the lifo.
1061 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062 * or one of the special values K_NO_WAIT and K_FOREVER.
1063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001064 * @return Address of the data item if successful.
1065 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1068
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001072 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
1074 * extern struct k_lifo @a name;
1075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001078#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001079 struct k_lifo name \
1080 __in_section(_k_lifo, static, name) = \
1081 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082
Allan Stephensc98da842016-11-11 15:45:03 -05001083/**
1084 * @} end defgroup lifo_apis
1085 */
1086
1087/**
1088 * @cond INTERNAL_HIDDEN
1089 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001090
1091struct k_stack {
1092 _wait_q_t wait_q;
1093 uint32_t *base, *next, *top;
1094
1095 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
1096};
1097
Allan Stephensc98da842016-11-11 15:45:03 -05001098#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1099 { \
1100 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1101 .base = stack_buffer, \
1102 .next = stack_buffer, \
1103 .top = stack_buffer + stack_num_entries, \
1104 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1105 }
1106
1107/**
1108 * INTERNAL_HIDDEN @endcond
1109 */
1110
1111/**
1112 * @defgroup stack_apis Stack APIs
1113 * @ingroup kernel_apis
1114 * @{
1115 */
1116
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117/**
1118 * @brief Initialize a stack.
1119 *
1120 * This routine initializes a stack object, prior to its first use.
1121 *
1122 * @param stack Address of the stack.
1123 * @param buffer Address of array used to hold stacked values.
1124 * @param num_entries Maximum number of values that can be stacked.
1125 *
1126 * @return N/A
1127 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001128extern void k_stack_init(struct k_stack *stack,
1129 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001130
1131/**
1132 * @brief Push an element onto a stack.
1133 *
1134 * This routine adds a 32-bit value @a data to @a stack.
1135 *
1136 * @note Can be called by ISRs.
1137 *
1138 * @param stack Address of the stack.
1139 * @param data Value to push onto the stack.
1140 *
1141 * @return N/A
1142 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001143extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144
1145/**
1146 * @brief Pop an element from a stack.
1147 *
1148 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1149 * manner and stores the value in @a data.
1150 *
1151 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1152 *
1153 * @param stack Address of the stack.
1154 * @param data Address of area to hold the value popped from the stack.
1155 * @param timeout Waiting period to obtain a value (in milliseconds),
1156 * or one of the special values K_NO_WAIT and K_FOREVER.
1157 *
1158 * @retval 0 if successful.
1159 * @retval -EBUSY if returned without waiting.
1160 * @retval -EAGAIN if waiting period timed out.
1161 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001162extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1163
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
1169 * extern struct k_stack @a name;
1170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001171 * @param name Name of the stack.
1172 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001174#define K_STACK_DEFINE(name, stack_num_entries) \
1175 uint32_t __noinit \
1176 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001177 struct k_stack name \
1178 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001179 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1180 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001181
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182/**
Allan Stephensc98da842016-11-11 15:45:03 -05001183 * @} end defgroup stack_apis
1184 */
1185
Allan Stephens6bba9b02016-11-16 14:56:54 -05001186struct k_work;
1187
Allan Stephensc98da842016-11-11 15:45:03 -05001188/**
1189 * @defgroup workqueue_apis Workqueue Thread APIs
1190 * @ingroup kernel_apis
1191 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192 */
1193
Allan Stephens6bba9b02016-11-16 14:56:54 -05001194/**
1195 * @typedef k_work_handler_t
1196 * @brief Work item handler function type.
1197 *
1198 * A work item's handler function is executed by a workqueue's thread
1199 * when the work item is processed by the workqueue.
1200 *
1201 * @param work Address of the work item.
1202 *
1203 * @return N/A
1204 */
1205typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206
1207/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001208 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001210
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211struct k_work_q {
1212 struct k_fifo fifo;
1213};
1214
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001215enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001216 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217};
1218
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001219struct k_work {
1220 void *_reserved; /* Used by k_fifo implementation. */
1221 k_work_handler_t handler;
1222 atomic_t flags[1];
1223};
1224
Allan Stephens6bba9b02016-11-16 14:56:54 -05001225struct k_delayed_work {
1226 struct k_work work;
1227 struct _timeout timeout;
1228 struct k_work_q *work_q;
1229};
1230
1231extern struct k_work_q k_sys_work_q;
1232
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001233/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001234 * INTERNAL_HIDDEN @endcond
1235 */
1236
1237/**
1238 * @brief Initialize a statically-defined work item.
1239 *
1240 * This macro can be used to initialize a statically-defined workqueue work
1241 * item, prior to its first use. For example,
1242 *
1243 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1244 *
1245 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246 */
1247#define K_WORK_INITIALIZER(work_handler) \
1248 { \
1249 ._reserved = NULL, \
1250 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001251 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001252 }
1253
1254/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001255 * @brief Initialize a work item.
1256 *
1257 * This routine initializes a workqueue work item, prior to its first use.
1258 *
1259 * @param work Address of work item.
1260 * @param handler Function to invoke each time work item is processed.
1261 *
1262 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001263 */
1264static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1265{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001266 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001267 work->handler = handler;
1268}
1269
1270/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001271 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001272 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001273 * This routine submits work item @a work to be processed by workqueue
1274 * @a work_q. If the work item is already pending in the workqueue's queue
1275 * as a result of an earlier submission, this routine has no effect on the
1276 * work item. If the work item has already been processed, or is currently
1277 * being processed, its work is considered complete and the work item can be
1278 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001279 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001280 * @warning
1281 * A submitted work item must not be modified until it has been processed
1282 * by the workqueue.
1283 *
1284 * @note Can be called by ISRs.
1285 *
1286 * @param work_q Address of workqueue.
1287 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001288 *
1289 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290 */
1291static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1292 struct k_work *work)
1293{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001294 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001295 k_fifo_put(&work_q->fifo, work);
1296 }
1297}
1298
1299/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001300 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001302 * This routine indicates if work item @a work is pending in a workqueue's
1303 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001305 * @note Can be called by ISRs.
1306 *
1307 * @param work Address of work item.
1308 *
1309 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001310 */
1311static inline int k_work_pending(struct k_work *work)
1312{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001313 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001314}
1315
1316/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001317 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001319 * This routine starts workqueue @a work_q. The workqueue spawns its work
1320 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001321 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001322 * @param work_q Address of workqueue.
1323 * @param stack Pointer to work queue thread's stack space.
1324 * @param stack_size Size of the work queue thread's stack (in bytes).
1325 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001326 *
1327 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328 */
Allan Stephens904cf972016-10-07 13:59:23 -05001329extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1330 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001332/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001333 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001334 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001335 * This routine initializes a workqueue delayed work item, prior to
1336 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001337 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001338 * @param work Address of delayed work item.
1339 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001340 *
1341 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001342 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001343extern void k_delayed_work_init(struct k_delayed_work *work,
1344 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001345
1346/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001347 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001349 * This routine schedules work item @a work to be processed by workqueue
1350 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1351 * an asychronous countdown for the work item and then returns to the caller.
1352 * Only when the countdown completes is the work item actually submitted to
1353 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001355 * Submitting a previously submitted delayed work item that is still
1356 * counting down cancels the existing submission and restarts the countdown
1357 * using the new delay. If the work item is currently pending on the
1358 * workqueue's queue because the countdown has completed it is too late to
1359 * resubmit the item, and resubmission fails without impacting the work item.
1360 * If the work item has already been processed, or is currently being processed,
1361 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001362 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001363 * @warning
1364 * A delayed work item must not be modified until it has been processed
1365 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001366 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001367 * @note Can be called by ISRs.
1368 *
1369 * @param work_q Address of workqueue.
1370 * @param work Address of delayed work item.
1371 * @param delay Delay before submitting the work item (in milliseconds).
1372 *
1373 * @retval 0 Work item countdown started.
1374 * @retval -EINPROGRESS Work item is already pending.
1375 * @retval -EINVAL Work item is being processed or has completed its work.
1376 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001378extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1379 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001380 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001381
1382/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001383 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001385 * This routine cancels the submission of delayed work item @a work.
1386 * A delayed work item can only be cancelled while its countdown is still
1387 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001389 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001391 * @param work Address of delayed work item.
1392 *
1393 * @retval 0 Work item countdown cancelled.
1394 * @retval -EINPROGRESS Work item is already pending.
1395 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001397extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001399/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400 * @brief Submit a work item to the system workqueue.
1401 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001402 * This routine submits work item @a work to be processed by the system
1403 * workqueue. If the work item is already pending in the workqueue's queue
1404 * as a result of an earlier submission, this routine has no effect on the
1405 * work item. If the work item has already been processed, or is currently
1406 * being processed, its work is considered complete and the work item can be
1407 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001409 * @warning
1410 * Work items submitted to the system workqueue should avoid using handlers
1411 * that block or yield since this may prevent the system workqueue from
1412 * processing other work items in a timely manner.
1413 *
1414 * @note Can be called by ISRs.
1415 *
1416 * @param work Address of work item.
1417 *
1418 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001419 */
1420static inline void k_work_submit(struct k_work *work)
1421{
1422 k_work_submit_to_queue(&k_sys_work_q, work);
1423}
1424
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426 * @brief Submit a delayed work item to the system workqueue.
1427 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001428 * This routine schedules work item @a work to be processed by the system
1429 * workqueue after a delay of @a delay milliseconds. The routine initiates
1430 * an asychronous countdown for the work item and then returns to the caller.
1431 * Only when the countdown completes is the work item actually submitted to
1432 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001434 * Submitting a previously submitted delayed work item that is still
1435 * counting down cancels the existing submission and restarts the countdown
1436 * using the new delay. If the work item is currently pending on the
1437 * workqueue's queue because the countdown has completed it is too late to
1438 * resubmit the item, and resubmission fails without impacting the work item.
1439 * If the work item has already been processed, or is currently being processed,
1440 * its work is considered complete and the work item can be resubmitted.
1441 *
1442 * @warning
1443 * Work items submitted to the system workqueue should avoid using handlers
1444 * that block or yield since this may prevent the system workqueue from
1445 * processing other work items in a timely manner.
1446 *
1447 * @note Can be called by ISRs.
1448 *
1449 * @param work Address of delayed work item.
1450 * @param delay Delay before submitting the work item (in milliseconds).
1451 *
1452 * @retval 0 Work item countdown started.
1453 * @retval -EINPROGRESS Work item is already pending.
1454 * @retval -EINVAL Work item is being processed or has completed its work.
1455 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456 */
1457static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001460 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001461}
1462
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463/**
Allan Stephensc98da842016-11-11 15:45:03 -05001464 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465 */
1466
Allan Stephensc98da842016-11-11 15:45:03 -05001467/**
1468 * @cond INTERNAL_HIDDEN
1469 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001470
1471struct k_mutex {
1472 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001473 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001474 uint32_t lock_count;
1475 int owner_orig_prio;
1476#ifdef CONFIG_OBJECT_MONITOR
1477 int num_lock_state_changes;
1478 int num_conflicts;
1479#endif
1480
1481 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1482};
1483
1484#ifdef CONFIG_OBJECT_MONITOR
1485#define _MUTEX_INIT_OBJECT_MONITOR \
1486 .num_lock_state_changes = 0, .num_conflicts = 0,
1487#else
1488#define _MUTEX_INIT_OBJECT_MONITOR
1489#endif
1490
1491#define K_MUTEX_INITIALIZER(obj) \
1492 { \
1493 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1494 .owner = NULL, \
1495 .lock_count = 0, \
1496 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1497 _MUTEX_INIT_OBJECT_MONITOR \
1498 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1499 }
1500
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001501/**
Allan Stephensc98da842016-11-11 15:45:03 -05001502 * INTERNAL_HIDDEN @endcond
1503 */
1504
1505/**
1506 * @defgroup mutex_apis Mutex APIs
1507 * @ingroup kernel_apis
1508 * @{
1509 */
1510
1511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001515 *
1516 * extern struct k_mutex @a name;
1517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001518 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001519 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001520#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001521 struct k_mutex name \
1522 __in_section(_k_mutex, static, name) = \
1523 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001524
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001528 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001530 * Upon completion, the mutex is available and does not have an owner.
1531 *
1532 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001533 *
1534 * @return N/A
1535 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537
1538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * This routine locks @a mutex. If the mutex is locked by another thread,
1542 * the calling thread waits until the mutex becomes available or until
1543 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001545 * A thread is permitted to lock a mutex it has already locked. The operation
1546 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001548 * @param mutex Address of the mutex.
1549 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001550 * or one of the special values K_NO_WAIT and K_FOREVER.
1551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @retval 0 if successful.
1553 * @retval -EBUSY if returned without waiting.
1554 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001555 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001556extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001557
1558/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001561 * This routine unlocks @a mutex. The mutex must already be locked by the
1562 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001563 *
1564 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001565 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001566 * thread.
1567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001568 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
1570 * @return N/A
1571 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572extern void k_mutex_unlock(struct k_mutex *mutex);
1573
Allan Stephensc98da842016-11-11 15:45:03 -05001574/**
1575 * @} end defgroup mutex_apis
1576 */
1577
1578/**
1579 * @cond INTERNAL_HIDDEN
1580 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001581
1582struct k_sem {
1583 _wait_q_t wait_q;
1584 unsigned int count;
1585 unsigned int limit;
1586
1587 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1588};
1589
Allan Stephensc98da842016-11-11 15:45:03 -05001590#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1591 { \
1592 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1593 .count = initial_count, \
1594 .limit = count_limit, \
1595 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1596 }
1597
1598/**
1599 * INTERNAL_HIDDEN @endcond
1600 */
1601
1602/**
1603 * @defgroup semaphore_apis Semaphore APIs
1604 * @ingroup kernel_apis
1605 * @{
1606 */
1607
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001608/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001613 * @param sem Address of the semaphore.
1614 * @param initial_count Initial semaphore count.
1615 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001616 *
1617 * @return N/A
1618 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001619extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1620 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001621
1622/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001623 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001627 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1628 *
1629 * @param sem Address of the semaphore.
1630 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001631 * or one of the special values K_NO_WAIT and K_FOREVER.
1632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @retval 0 if successful.
1634 * @retval -EBUSY if returned without waiting.
1635 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001637extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001638
1639/**
1640 * @brief Give a semaphore.
1641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001642 * This routine gives @a sem, unless the semaphore is already at its maximum
1643 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001645 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001647 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001648 *
1649 * @return N/A
1650 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651extern void k_sem_give(struct k_sem *sem);
1652
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001653/**
1654 * @brief Reset a semaphore's count to zero.
1655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001658 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001659 *
1660 * @return N/A
1661 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001662static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663{
1664 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001665}
1666
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001667/**
1668 * @brief Get a semaphore's count.
1669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001670 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001675 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001676static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001677{
1678 return sem->count;
1679}
1680
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001682 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001685 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * @param name Name of the semaphore.
1689 * @param initial_count Initial semaphore count.
1690 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001691 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001692#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001693 struct k_sem name \
1694 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001695 K_SEM_INITIALIZER(name, initial_count, count_limit)
1696
Allan Stephensc98da842016-11-11 15:45:03 -05001697/**
1698 * @} end defgroup semaphore_apis
1699 */
1700
1701/**
1702 * @defgroup alert_apis Alert APIs
1703 * @ingroup kernel_apis
1704 * @{
1705 */
1706
Allan Stephens5eceb852016-11-16 10:16:30 -05001707/**
1708 * @typedef k_alert_handler_t
1709 * @brief Alert handler function type.
1710 *
1711 * An alert's alert handler function is invoked by the system workqueue
1712 * when the alert is signalled. The alert handler function is optional,
1713 * and is only invoked if the alert has been initialized with one.
1714 *
1715 * @param alert Address of the alert.
1716 *
1717 * @return 0 if alert has been consumed; non-zero if alert should pend.
1718 */
1719typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001720
1721/**
1722 * @} end defgroup alert_apis
1723 */
1724
1725/**
1726 * @cond INTERNAL_HIDDEN
1727 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001729#define K_ALERT_DEFAULT NULL
1730#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001731
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001732struct k_alert {
1733 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734 atomic_t send_count;
1735 struct k_work work_item;
1736 struct k_sem sem;
1737
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001738 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001739};
1740
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001741extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001742
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001743#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001744 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001745 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001747 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001748 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1750 }
1751
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752/**
Allan Stephensc98da842016-11-11 15:45:03 -05001753 * INTERNAL_HIDDEN @endcond
1754 */
1755
1756/**
1757 * @addtogroup alert_apis
1758 * @{
1759 */
1760
1761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765 *
1766 * extern struct k_alert @a name;
1767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * @param name Name of the alert.
1769 * @param alert_handler Action to take when alert is sent. Specify either
1770 * the address of a function to be invoked by the system workqueue
1771 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1772 * K_ALERT_DEFAULT (which causes the alert to pend).
1773 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001775#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001776 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001777 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001778 K_ALERT_INITIALIZER(name, alert_handler, \
1779 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001780
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001782 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001784 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001786 * @param alert Address of the alert.
1787 * @param handler Action to take when alert is sent. Specify either the address
1788 * of a function to be invoked by the system workqueue thread,
1789 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1790 * K_ALERT_DEFAULT (which causes the alert to pend).
1791 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001792 *
1793 * @return N/A
1794 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001795extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1796 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797
1798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1804 *
1805 * @param alert Address of the alert.
1806 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 * or one of the special values K_NO_WAIT and K_FOREVER.
1808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001809 * @retval 0 if successful.
1810 * @retval -EBUSY if returned without waiting.
1811 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001812 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001813extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814
1815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * This routine signals @a alert. The action specified for @a alert will
1819 * be taken, which may trigger the execution of an alert handler function
1820 * and/or cause the alert to pend (assuming the alert has not reached its
1821 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * @note Can be called by ISRs.
1824 *
1825 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001826 *
1827 * @return N/A
1828 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001829extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830
1831/**
Allan Stephensc98da842016-11-11 15:45:03 -05001832 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001833 */
1834
Allan Stephensc98da842016-11-11 15:45:03 -05001835/**
1836 * @cond INTERNAL_HIDDEN
1837 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838
1839struct k_msgq {
1840 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001841 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001842 uint32_t max_msgs;
1843 char *buffer_start;
1844 char *buffer_end;
1845 char *read_ptr;
1846 char *write_ptr;
1847 uint32_t used_msgs;
1848
1849 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1850};
1851
Peter Mitsis1da807e2016-10-06 11:36:59 -04001852#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001853 { \
1854 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001855 .max_msgs = q_max_msgs, \
1856 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001857 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001858 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001859 .read_ptr = q_buffer, \
1860 .write_ptr = q_buffer, \
1861 .used_msgs = 0, \
1862 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1863 }
1864
Peter Mitsis1da807e2016-10-06 11:36:59 -04001865/**
Allan Stephensc98da842016-11-11 15:45:03 -05001866 * INTERNAL_HIDDEN @endcond
1867 */
1868
1869/**
1870 * @defgroup msgq_apis Message Queue APIs
1871 * @ingroup kernel_apis
1872 * @{
1873 */
1874
1875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1879 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001880 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1881 * message is similarly aligned to this boundary, @a q_msg_size must also be
1882 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001884 * The message queue can be accessed outside the module where it is defined
1885 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 *
1887 * extern struct k_msgq @a name;
1888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * @param q_name Name of the message queue.
1890 * @param q_msg_size Message size (in bytes).
1891 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001892 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001893 */
1894#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1895 static char __noinit __aligned(q_align) \
1896 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001897 struct k_msgq q_name \
1898 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001899 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1900 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901
Peter Mitsisd7a37502016-10-13 11:37:40 -04001902/**
1903 * @brief Initialize a message queue.
1904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * This routine initializes a message queue object, prior to its first use.
1906 *
Allan Stephensda827222016-11-09 14:23:58 -06001907 * The message queue's ring buffer must contain space for @a max_msgs messages,
1908 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1909 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1910 * that each message is similarly aligned to this boundary, @a q_msg_size
1911 * must also be a multiple of N.
1912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @param q Address of the message queue.
1914 * @param buffer Pointer to ring buffer that holds queued messages.
1915 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001916 * @param max_msgs Maximum number of messages that can be queued.
1917 *
1918 * @return N/A
1919 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001920extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001921 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001922
1923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001926 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001927 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001928 * @note Can be called by ISRs.
1929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * @param q Address of the message queue.
1931 * @param data Pointer to the message.
1932 * @param timeout Waiting period to add the message (in milliseconds),
1933 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * @retval 0 if successful.
1936 * @retval -ENOMSG if returned without waiting or after a queue purge.
1937 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001938 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001940
1941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * This routine receives a message from message queue @a q in a "first in,
1945 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001946 *
Allan Stephensc98da842016-11-11 15:45:03 -05001947 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * @param q Address of the message queue.
1950 * @param data Address of area to hold the received message.
1951 * @param timeout Waiting period to receive the message (in milliseconds),
1952 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @retval 0 if successful.
1955 * @retval -ENOMSG if returned without waiting.
1956 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001957 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001958extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001959
1960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * This routine discards all unreceived messages in a message queue's ring
1964 * buffer. Any threads that are blocked waiting to send a message to the
1965 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001968 *
1969 * @return N/A
1970 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971extern void k_msgq_purge(struct k_msgq *q);
1972
Peter Mitsis67be2492016-10-07 11:44:34 -04001973/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001976 * This routine returns the number of unused entries in a message queue's
1977 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * @param q Address of the message queue.
1980 *
1981 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001982 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001983static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001984{
1985 return q->max_msgs - q->used_msgs;
1986}
1987
Peter Mitsisd7a37502016-10-13 11:37:40 -04001988/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * @param q Address of the message queue.
1994 *
1995 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001996 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001997static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998{
1999 return q->used_msgs;
2000}
2001
Allan Stephensc98da842016-11-11 15:45:03 -05002002/**
2003 * @} end defgroup msgq_apis
2004 */
2005
2006/**
2007 * @defgroup mem_pool_apis Memory Pool APIs
2008 * @ingroup kernel_apis
2009 * @{
2010 */
2011
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002012struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002013 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014 void *addr_in_pool;
2015 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002016 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017};
2018
Allan Stephensc98da842016-11-11 15:45:03 -05002019/**
2020 * @} end defgroup mem_pool_apis
2021 */
2022
2023/**
2024 * @defgroup mailbox_apis Mailbox APIs
2025 * @ingroup kernel_apis
2026 * @{
2027 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028
2029struct k_mbox_msg {
2030 /** internal use only - needed for legacy API support */
2031 uint32_t _mailbox;
2032 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002033 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034 /** application-defined information value */
2035 uint32_t info;
2036 /** sender's message data buffer */
2037 void *tx_data;
2038 /** internal use only - needed for legacy API support */
2039 void *_rx_data;
2040 /** message data block descriptor */
2041 struct k_mem_block tx_block;
2042 /** source thread id */
2043 k_tid_t rx_source_thread;
2044 /** target thread id */
2045 k_tid_t tx_target_thread;
2046 /** internal use only - thread waiting on send (may be a dummy) */
2047 k_tid_t _syncing_thread;
2048#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2049 /** internal use only - semaphore used during asynchronous send */
2050 struct k_sem *_async_sem;
2051#endif
2052};
2053
Allan Stephensc98da842016-11-11 15:45:03 -05002054/**
2055 * @cond INTERNAL_HIDDEN
2056 */
2057
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058struct k_mbox {
2059 _wait_q_t tx_msg_queue;
2060 _wait_q_t rx_msg_queue;
2061
2062 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
2063};
2064
2065#define K_MBOX_INITIALIZER(obj) \
2066 { \
2067 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2068 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
2069 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2070 }
2071
Peter Mitsis12092702016-10-14 12:57:23 -04002072/**
Allan Stephensc98da842016-11-11 15:45:03 -05002073 * INTERNAL_HIDDEN @endcond
2074 */
2075
2076/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002079 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002080 *
2081 * extern struct k_mbox @a name;
2082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002083 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002084 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002086 struct k_mbox name \
2087 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088 K_MBOX_INITIALIZER(name) \
2089
Peter Mitsis12092702016-10-14 12:57:23 -04002090/**
2091 * @brief Initialize a mailbox.
2092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * This routine initializes a mailbox object, prior to its first use.
2094 *
2095 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002096 *
2097 * @return N/A
2098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002099extern void k_mbox_init(struct k_mbox *mbox);
2100
Peter Mitsis12092702016-10-14 12:57:23 -04002101/**
2102 * @brief Send a mailbox message in a synchronous manner.
2103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * This routine sends a message to @a mbox and waits for a receiver to both
2105 * receive and process it. The message data may be in a buffer, in a memory
2106 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * @param mbox Address of the mailbox.
2109 * @param tx_msg Address of the transmit message descriptor.
2110 * @param timeout Waiting period for the message to be received (in
2111 * milliseconds), or one of the special values K_NO_WAIT
2112 * and K_FOREVER. Once the message has been received,
2113 * this routine waits as long as necessary for the message
2114 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * @retval 0 if successful.
2117 * @retval -ENOMSG if returned without waiting.
2118 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002119 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002120extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002121 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002122
Peter Mitsis12092702016-10-14 12:57:23 -04002123/**
2124 * @brief Send a mailbox message in an asynchronous manner.
2125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * This routine sends a message to @a mbox without waiting for a receiver
2127 * to process it. The message data may be in a buffer, in a memory pool block,
2128 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2129 * will be given when the message has been both received and completely
2130 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @param mbox Address of the mailbox.
2133 * @param tx_msg Address of the transmit message descriptor.
2134 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002135 *
2136 * @return N/A
2137 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002138extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139 struct k_sem *sem);
2140
Peter Mitsis12092702016-10-14 12:57:23 -04002141/**
2142 * @brief Receive a mailbox message.
2143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * This routine receives a message from @a mbox, then optionally retrieves
2145 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * @param mbox Address of the mailbox.
2148 * @param rx_msg Address of the receive message descriptor.
2149 * @param buffer Address of the buffer to receive data, or NULL to defer data
2150 * retrieval and message disposal until later.
2151 * @param timeout Waiting period for a message to be received (in
2152 * milliseconds), or one of the special values K_NO_WAIT
2153 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * @retval 0 if successful.
2156 * @retval -ENOMSG if returned without waiting.
2157 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002158 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002159extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002161
2162/**
2163 * @brief Retrieve mailbox message data into a buffer.
2164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * This routine completes the processing of a received message by retrieving
2166 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002167 *
2168 * Alternatively, this routine can be used to dispose of a received message
2169 * without retrieving its data.
2170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002171 * @param rx_msg Address of the receive message descriptor.
2172 * @param buffer Address of the buffer to receive data, or NULL to discard
2173 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002174 *
2175 * @return N/A
2176 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002177extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002178
2179/**
2180 * @brief Retrieve mailbox message data into a memory pool block.
2181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182 * This routine completes the processing of a received message by retrieving
2183 * its data into a memory pool block, then disposing of the message.
2184 * The memory pool block that results from successful retrieval must be
2185 * returned to the pool once the data has been processed, even in cases
2186 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002187 *
2188 * Alternatively, this routine can be used to dispose of a received message
2189 * without retrieving its data. In this case there is no need to return a
2190 * memory pool block to the pool.
2191 *
2192 * This routine allocates a new memory pool block for the data only if the
2193 * data is not already in one. If a new block cannot be allocated, the routine
2194 * returns a failure code and the received message is left unchanged. This
2195 * permits the caller to reattempt data retrieval at a later time or to dispose
2196 * of the received message without retrieving its data.
2197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002198 * @param rx_msg Address of a receive message descriptor.
2199 * @param pool Address of memory pool, or NULL to discard data.
2200 * @param block Address of the area to hold memory pool block info.
2201 * @param timeout Waiting period to wait for a memory pool block (in
2202 * milliseconds), or one of the special values K_NO_WAIT
2203 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002205 * @retval 0 if successful.
2206 * @retval -ENOMEM if returned without waiting.
2207 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002208 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002209extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002210 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211 struct k_mem_block *block, int32_t timeout);
2212
Allan Stephensc98da842016-11-11 15:45:03 -05002213/**
2214 * @} end defgroup mailbox_apis
2215 */
2216
2217/**
2218 * @cond INTERNAL_HIDDEN
2219 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220
2221struct k_pipe {
2222 unsigned char *buffer; /* Pipe buffer: may be NULL */
2223 size_t size; /* Buffer size */
2224 size_t bytes_used; /* # bytes used in buffer */
2225 size_t read_index; /* Where in buffer to read from */
2226 size_t write_index; /* Where in buffer to write */
2227
2228 struct {
2229 _wait_q_t readers; /* Reader wait queue */
2230 _wait_q_t writers; /* Writer wait queue */
2231 } wait_q;
2232
2233 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2234};
2235
Peter Mitsise5d9c582016-10-14 14:44:57 -04002236#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237 { \
2238 .buffer = pipe_buffer, \
2239 .size = pipe_buffer_size, \
2240 .bytes_used = 0, \
2241 .read_index = 0, \
2242 .write_index = 0, \
2243 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2244 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2245 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2246 }
2247
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248/**
Allan Stephensc98da842016-11-11 15:45:03 -05002249 * INTERNAL_HIDDEN @endcond
2250 */
2251
2252/**
2253 * @defgroup pipe_apis Pipe APIs
2254 * @ingroup kernel_apis
2255 * @{
2256 */
2257
2258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002259 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
2263 * extern struct k_pipe @a name;
2264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * @param name Name of the pipe.
2266 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2267 * or zero if no ring buffer is used.
2268 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002270#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2271 static unsigned char __noinit __aligned(pipe_align) \
2272 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002273 struct k_pipe name \
2274 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002275 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002278 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002280 * This routine initializes a pipe object, prior to its first use.
2281 *
2282 * @param pipe Address of the pipe.
2283 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2284 * is used.
2285 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2286 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287 *
2288 * @return N/A
2289 */
2290extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2291 size_t size);
2292
2293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002296 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002298 * @param pipe Address of the pipe.
2299 * @param data Address of data to write.
2300 * @param bytes_to_write Size of data (in bytes).
2301 * @param bytes_written Address of area to hold the number of bytes written.
2302 * @param min_xfer Minimum number of bytes to write.
2303 * @param timeout Waiting period to wait for the data to be written (in
2304 * milliseconds), or one of the special values K_NO_WAIT
2305 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002307 * @retval 0 if at least @a min_xfer data bytes were written.
2308 * @retval -EIO if returned without waiting; zero data bytes were written.
2309 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
2310 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002312extern int k_pipe_put(struct k_pipe *pipe, void *data,
2313 size_t bytes_to_write, size_t *bytes_written,
2314 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315
2316/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321 * @param pipe Address of the pipe.
2322 * @param data Address to place the data read from pipe.
2323 * @param bytes_to_read Maximum number of data bytes to read.
2324 * @param bytes_read Address of area to hold the number of bytes read.
2325 * @param min_xfer Minimum number of data bytes to read.
2326 * @param timeout Waiting period to wait for the data to be read (in
2327 * milliseconds), or one of the special values K_NO_WAIT
2328 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @retval 0 if at least @a min_xfer data bytes were read.
2331 * @retval -EIO if returned without waiting; zero data bytes were read.
2332 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
2333 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002335extern int k_pipe_get(struct k_pipe *pipe, void *data,
2336 size_t bytes_to_read, size_t *bytes_read,
2337 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338
2339/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * This routine writes the data contained in a memory block to @a pipe.
2343 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347 * @param block Memory block containing data to send
2348 * @param size Number of data bytes in memory block to send
2349 * @param sem Semaphore to signal upon completion (else NULL)
2350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352 */
2353extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2354 size_t size, struct k_sem *sem);
2355
2356/**
Allan Stephensc98da842016-11-11 15:45:03 -05002357 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358 */
2359
Allan Stephensc98da842016-11-11 15:45:03 -05002360/**
2361 * @cond INTERNAL_HIDDEN
2362 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002364struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002366 uint32_t num_blocks;
2367 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 char *buffer;
2369 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002370 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002372 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373};
2374
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002375#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2376 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 { \
2378 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002379 .num_blocks = slab_num_blocks, \
2380 .block_size = slab_block_size, \
2381 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382 .free_list = NULL, \
2383 .num_used = 0, \
2384 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2385 }
2386
Peter Mitsis578f9112016-10-07 13:50:31 -04002387/**
Allan Stephensc98da842016-11-11 15:45:03 -05002388 * INTERNAL_HIDDEN @endcond
2389 */
2390
2391/**
2392 * @defgroup mem_slab_apis Memory Slab APIs
2393 * @ingroup kernel_apis
2394 * @{
2395 */
2396
2397/**
Allan Stephensda827222016-11-09 14:23:58 -06002398 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002399 *
Allan Stephensda827222016-11-09 14:23:58 -06002400 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002402 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2403 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002404 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002405 *
Allan Stephensda827222016-11-09 14:23:58 -06002406 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 *
2409 * extern struct k_mem_slab @a name;
2410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @param name Name of the memory slab.
2412 * @param slab_block_size Size of each memory block (in bytes).
2413 * @param slab_num_blocks Number memory blocks.
2414 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002415 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002416#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2417 char __noinit __aligned(slab_align) \
2418 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2419 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002420 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002421 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2422 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002423
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002424/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002425 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002428 *
Allan Stephensda827222016-11-09 14:23:58 -06002429 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2430 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2431 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2432 * To ensure that each memory block is similarly aligned to this boundary,
2433 * @a slab_block_size must also be a multiple of N.
2434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * @param slab Address of the memory slab.
2436 * @param buffer Pointer to buffer used for the memory blocks.
2437 * @param block_size Size of each memory block (in bytes).
2438 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002439 *
2440 * @return N/A
2441 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002442extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002443 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002444
2445/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * @param slab Address of the memory slab.
2451 * @param mem Pointer to block address area.
2452 * @param timeout Maximum time to wait for operation to complete
2453 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2454 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @retval 0 if successful. The block address area pointed at by @a mem
2457 * is set to the starting address of the memory block.
2458 * @retval -ENOMEM if failed immediately.
2459 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002460 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002461extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2462 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002463
2464/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467 * This routine releases a previously allocated memory block back to its
2468 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @param slab Address of the memory slab.
2471 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002472 *
2473 * @return N/A
2474 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002475extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * This routine gets the number of memory blocks that are currently
2481 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002486 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002487static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002489 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002490}
2491
Peter Mitsisc001aa82016-10-13 13:53:37 -04002492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * This routine gets the number of memory blocks that are currently
2496 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002501 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002502static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002503{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002504 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002505}
2506
Allan Stephensc98da842016-11-11 15:45:03 -05002507/**
2508 * @} end defgroup mem_slab_apis
2509 */
2510
2511/**
2512 * @cond INTERNAL_HIDDEN
2513 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002515/*
2516 * Memory pool requires a buffer and two arrays of structures for the
2517 * memory block accounting:
2518 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2519 * status of four blocks of memory.
2520 */
2521struct k_mem_pool_quad_block {
2522 char *mem_blocks; /* pointer to the first of four memory blocks */
2523 uint32_t mem_status; /* four bits. If bit is set, memory block is
2524 allocated */
2525};
2526/*
2527 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2528 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2529 * block size is 4 times less than the previous one and thus requires 4 times
2530 * bigger array of k_mem_pool_quad_block structures to keep track of the
2531 * memory blocks.
2532 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002534/*
2535 * The array of k_mem_pool_block_set keeps the information of each array of
2536 * k_mem_pool_quad_block structures
2537 */
2538struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002539 size_t block_size; /* memory block size */
2540 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002541 struct k_mem_pool_quad_block *quad_block;
2542 int count;
2543};
2544
2545/* Memory pool descriptor */
2546struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002547 size_t max_block_size;
2548 size_t min_block_size;
2549 uint32_t nr_of_maxblocks;
2550 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002551 struct k_mem_pool_block_set *block_set;
2552 char *bufblock;
2553 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2555};
2556
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002557#ifdef CONFIG_ARM
2558#define _SECTION_TYPE_SIGN "%"
2559#else
2560#define _SECTION_TYPE_SIGN "@"
2561#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002563/*
2564 * Static memory pool initialization
2565 */
Allan Stephensc98da842016-11-11 15:45:03 -05002566
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002567/*
2568 * Use .altmacro to be able to recalculate values and pass them as string
2569 * arguments when calling assembler macros resursively
2570 */
2571__asm__(".altmacro\n\t");
2572
2573/*
2574 * Recursively calls a macro
2575 * The followig global symbols need to be initialized:
2576 * __memory_pool_max_block_size - maximal size of the memory block
2577 * __memory_pool_min_block_size - minimal size of the memory block
2578 * Notes:
2579 * Global symbols are used due the fact that assembler macro allows only
2580 * one argument be passed with the % conversion
2581 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2582 * is used instead of / 4.
2583 * n_max argument needs to go first in the invoked macro, as some
2584 * assemblers concatenate \name and %(\n_max * 4) arguments
2585 * if \name goes first
2586 */
2587__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2588 ".ifge __memory_pool_max_block_size >> 2 -"
2589 " __memory_pool_min_block_size\n\t\t"
2590 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2591 "\\macro_name %(\\n_max * 4) \\name\n\t"
2592 ".endif\n\t"
2593 ".endm\n");
2594
2595/*
2596 * Build quad blocks
2597 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2598 * structures and recursively calls itself for the next array, 4 times
2599 * larger.
2600 * The followig global symbols need to be initialized:
2601 * __memory_pool_max_block_size - maximal size of the memory block
2602 * __memory_pool_min_block_size - minimal size of the memory block
2603 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2604 */
2605__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002606 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002607 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2608 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2609 ".if \\n_max % 4\n\t\t"
2610 ".skip __memory_pool_quad_block_size\n\t"
2611 ".endif\n\t"
2612 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2613 ".endm\n");
2614
2615/*
2616 * Build block sets and initialize them
2617 * Macro initializes the k_mem_pool_block_set structure and
2618 * recursively calls itself for the next one.
2619 * The followig global symbols need to be initialized:
2620 * __memory_pool_max_block_size - maximal size of the memory block
2621 * __memory_pool_min_block_size - minimal size of the memory block
2622 * __memory_pool_block_set_count, the number of the elements in the
2623 * block set array must be set to 0. Macro calculates it's real
2624 * value.
2625 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2626 * structures, _build_quad_blocks must be called prior it.
2627 */
2628__asm__(".macro _build_block_set n_max, name\n\t"
2629 ".int __memory_pool_max_block_size\n\t" /* block_size */
2630 ".if \\n_max % 4\n\t\t"
2631 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2632 ".else\n\t\t"
2633 ".int \\n_max >> 2\n\t"
2634 ".endif\n\t"
2635 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2636 ".int 0\n\t" /* count */
2637 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2638 "__do_recurse _build_block_set \\name \\n_max\n\t"
2639 ".endm\n");
2640
2641/*
2642 * Build a memory pool structure and initialize it
2643 * Macro uses __memory_pool_block_set_count global symbol,
2644 * block set addresses and buffer address, it may be called only after
2645 * _build_block_set
2646 */
2647__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002648 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002649 _SECTION_TYPE_SIGN "progbits\n\t"
2650 ".globl \\name\n\t"
2651 "\\name:\n\t"
2652 ".int \\max_size\n\t" /* max_block_size */
2653 ".int \\min_size\n\t" /* min_block_size */
2654 ".int \\n_max\n\t" /* nr_of_maxblocks */
2655 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2656 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2657 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2658 ".int 0\n\t" /* wait_q->head */
2659 ".int 0\n\t" /* wait_q->next */
2660 ".popsection\n\t"
2661 ".endm\n");
2662
2663#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2664 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2665 _SECTION_TYPE_SIGN "progbits\n\t"); \
2666 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2667 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2668 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2669 STRINGIFY(name) "\n\t"); \
2670 __asm__(".popsection\n\t")
2671
2672#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2673 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2674 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2675 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2676 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002677 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002678 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2679 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2680 STRINGIFY(name) "\n\t"); \
2681 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2682 __asm__(".int __memory_pool_block_set_count\n\t"); \
2683 __asm__(".popsection\n\t"); \
2684 extern uint32_t _mem_pool_block_set_count_##name; \
2685 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2686
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002687#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2688 char __noinit __aligned(align) \
2689 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002690
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002691/*
2692 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2693 * to __memory_pool_quad_block_size absolute symbol.
2694 * This function does not get called, but compiler calculates the value and
2695 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2696 */
2697static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2698{
2699 __asm__(".globl __memory_pool_quad_block_size\n\t"
2700#ifdef CONFIG_NIOS2
2701 "__memory_pool_quad_block_size = %0\n\t"
2702#else
2703 "__memory_pool_quad_block_size = %c0\n\t"
2704#endif
2705 :
2706 : "n"(sizeof(struct k_mem_pool_quad_block)));
2707}
2708
2709/**
Allan Stephensc98da842016-11-11 15:45:03 -05002710 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002711 */
2712
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002713/**
Allan Stephensc98da842016-11-11 15:45:03 -05002714 * @addtogroup mem_pool_apis
2715 * @{
2716 */
2717
2718/**
2719 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002721 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2722 * long. The memory pool allows blocks to be repeatedly partitioned into
2723 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2724 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002725 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002727 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728 * If the pool is to be accessed outside the module where it is defined, it
2729 * can be declared via
2730 *
2731 * extern struct k_mem_pool @a name;
2732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * @param name Name of the memory pool.
2734 * @param min_size Size of the smallest blocks in the pool (in bytes).
2735 * @param max_size Size of the largest blocks in the pool (in bytes).
2736 * @param n_max Number of maximum sized blocks in the pool.
2737 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002738 */
2739#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002740 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2741 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002742 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002743 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2744 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2745 extern struct k_mem_pool name
2746
Peter Mitsis937042c2016-10-13 13:18:26 -04002747/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002748 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * @param pool Address of the memory pool.
2753 * @param block Pointer to block descriptor for the allocated memory.
2754 * @param size Amount of memory to allocate (in bytes).
2755 * @param timeout Maximum time to wait for operation to complete
2756 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2757 * or K_FOREVER to wait as long as necessary.
2758 *
2759 * @retval 0 if successful. The @a data field of the block descriptor
2760 * is set to the starting address of the memory block.
2761 * @retval -ENOMEM if unable to allocate a memory block.
2762 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002763 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002764extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002765 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002766
2767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * This routine releases a previously allocated memory block back to its
2771 * memory pool.
2772 *
2773 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002774 *
2775 * @return N/A
2776 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002778
2779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 * This routine instructs a memory pool to concatenate unused memory blocks
2783 * into larger blocks wherever possible. Manually defragmenting the memory
2784 * pool may speed up future allocations of memory blocks by eliminating the
2785 * need for the memory pool to perform an automatic partial defragmentation.
2786 *
2787 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002788 *
2789 * @return N/A
2790 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002791extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002792
2793/**
Allan Stephensc98da842016-11-11 15:45:03 -05002794 * @} end addtogroup mem_pool_apis
2795 */
2796
2797/**
2798 * @defgroup heap_apis Heap Memory Pool APIs
2799 * @ingroup kernel_apis
2800 * @{
2801 */
2802
2803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002806 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002807 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002811 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002812 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002813extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002814
2815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002817 *
2818 * This routine provides traditional free() semantics. The memory being
2819 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002822 *
2823 * @return N/A
2824 */
2825extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826
Allan Stephensc98da842016-11-11 15:45:03 -05002827/**
2828 * @} end defgroup heap_apis
2829 */
2830
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831/*
2832 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2833 * hook into the device subsystem, which itself uses nanokernel semaphores,
2834 * and thus currently requires the definition of nano_sem.
2835 */
2836#include <legacy.h>
2837#include <arch/cpu.h>
2838
2839/*
2840 * private APIs that are utilized by one or more public APIs
2841 */
2842
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002845extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846
2847#ifdef __cplusplus
2848}
2849#endif
2850
Andrew Boiee004dec2016-11-07 09:01:19 -08002851#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2852/*
2853 * Define new and delete operators.
2854 * At this moment, the operators do nothing since objects are supposed
2855 * to be statically allocated.
2856 */
2857inline void operator delete(void *ptr)
2858{
2859 (void)ptr;
2860}
2861
2862inline void operator delete[](void *ptr)
2863{
2864 (void)ptr;
2865}
2866
2867inline void *operator new(size_t size)
2868{
2869 (void)size;
2870 return NULL;
2871}
2872
2873inline void *operator new[](size_t size)
2874{
2875 (void)size;
2876 return NULL;
2877}
2878
2879/* Placement versions of operator new and delete */
2880inline void operator delete(void *ptr1, void *ptr2)
2881{
2882 (void)ptr1;
2883 (void)ptr2;
2884}
2885
2886inline void operator delete[](void *ptr1, void *ptr2)
2887{
2888 (void)ptr1;
2889 (void)ptr2;
2890}
2891
2892inline void *operator new(size_t size, void *ptr)
2893{
2894 (void)size;
2895 return ptr;
2896}
2897
2898inline void *operator new[](size_t size, void *ptr)
2899{
2900 (void)size;
2901 return ptr;
2902}
2903
2904#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2905
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906#endif /* _kernel__h_ */