blob: 4ae060fe8e1b27a19a92db69d76822d7dce021fe [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
1186/**
1187 * @defgroup workqueue_apis Workqueue Thread APIs
1188 * @ingroup kernel_apis
1189 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001190 */
1191
1192struct k_work;
1193
1194typedef void (*k_work_handler_t)(struct k_work *);
1195
1196/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001198 * queued to it. This is useful for drivers which need to schedule
1199 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201 * space.
1202 */
1203struct k_work_q {
1204 struct k_fifo fifo;
1205};
1206
1207/**
1208 * @brief Work flags.
1209 */
1210enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001211 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001212};
1213
1214/**
1215 * @brief An item which can be scheduled on a @ref k_work_q.
1216 */
1217struct k_work {
1218 void *_reserved; /* Used by k_fifo implementation. */
1219 k_work_handler_t handler;
1220 atomic_t flags[1];
1221};
1222
1223/**
1224 * @brief Statically initialize work item
1225 */
1226#define K_WORK_INITIALIZER(work_handler) \
1227 { \
1228 ._reserved = NULL, \
1229 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001230 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001231 }
1232
1233/**
1234 * @brief Dynamically initialize work item
1235 */
1236static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1237{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001238 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001239 work->handler = handler;
1240}
1241
1242/**
1243 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001244 *
1245 * This procedure schedules a work item to be processed.
1246 * In the case where the work item has already been submitted and is pending
1247 * execution, calling this function will result in a no-op. In this case, the
1248 * work item must not be modified externally (e.g. by the caller of this
1249 * function), since that could cause the work item to be processed in a
1250 * corrupted state.
1251 *
1252 * @param work_q to schedule the work item
1253 * @param work work item
1254 *
1255 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001256 */
1257static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1258 struct k_work *work)
1259{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001260 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001261 k_fifo_put(&work_q->fifo, work);
1262 }
1263}
1264
1265/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001266 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001267 *
1268 * @param work Work item to query
1269 *
1270 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001271 */
1272static inline int k_work_pending(struct k_work *work)
1273{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001274 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001275}
1276
1277/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 * @brief Start a new workqueue.
1279 *
1280 * This routine must not be called from an ISR.
1281 *
1282 * @param work_q Pointer to Work queue
1283 * @param stack Pointer to work queue thread's stack
1284 * @param stack_size Size of the work queue thread's stack
1285 * @param prio Priority of the work queue's thread
1286 *
1287 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001288 */
Allan Stephens904cf972016-10-07 13:59:23 -05001289extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1290 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001292#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001293
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001294/**
1295 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296 */
1297struct k_delayed_work {
1298 struct k_work work;
1299 struct _timeout timeout;
1300 struct k_work_q *work_q;
1301};
1302
1303/**
1304 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 *
1306 * Initialize a delayed work item.
1307 *
1308 * @param work Delayed work item
1309 * @param handler Routine invoked when processing delayed work item
1310 *
1311 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001313extern void k_delayed_work_init(struct k_delayed_work *work,
1314 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315
1316/**
1317 * @brief Submit a delayed work item to a workqueue.
1318 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001319 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001320 * Once the delay has passed, the work item is submitted to the work queue:
1321 * at this point, it is no longer possible to cancel it. Once the work item's
1322 * handler is about to be executed, the work is considered complete and can be
1323 * resubmitted.
1324 *
1325 * Care must be taken if the handler blocks or yield as there is no implicit
1326 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1327 * it should be explicitly done between the submitter and the handler.
1328 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001329 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001331 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001333 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001334 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001335extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1336 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001337 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001338
1339/**
1340 * @brief Cancel a delayed work item
1341 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001342 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001343 * or is idle, this will do nothing. The only case where this can fail is when
1344 * the work has been submitted to the work queue, but the handler has not run
1345 * yet.
1346 *
1347 * @param work Delayed work item to be canceled
1348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001350 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001351extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001352
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001353#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001355extern struct k_work_q k_sys_work_q;
1356
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001357/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001358 * @brief Submit a work item to the system workqueue.
1359 *
1360 * @ref k_work_submit_to_queue
1361 *
1362 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001363 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364 * unexpected behavior.
1365 */
1366static inline void k_work_submit(struct k_work *work)
1367{
1368 k_work_submit_to_queue(&k_sys_work_q, work);
1369}
1370
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001371#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373 * @brief Submit a delayed work item to the system workqueue.
1374 *
1375 * @ref k_delayed_work_submit_to_queue
1376 *
1377 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001378 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379 * unexpected behavior.
1380 */
1381static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001382 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001383{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001384 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001385}
1386
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001387#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388
1389/**
Allan Stephensc98da842016-11-11 15:45:03 -05001390 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 */
1392
Allan Stephensc98da842016-11-11 15:45:03 -05001393/**
1394 * @cond INTERNAL_HIDDEN
1395 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396
1397struct k_mutex {
1398 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001399 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400 uint32_t lock_count;
1401 int owner_orig_prio;
1402#ifdef CONFIG_OBJECT_MONITOR
1403 int num_lock_state_changes;
1404 int num_conflicts;
1405#endif
1406
1407 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1408};
1409
1410#ifdef CONFIG_OBJECT_MONITOR
1411#define _MUTEX_INIT_OBJECT_MONITOR \
1412 .num_lock_state_changes = 0, .num_conflicts = 0,
1413#else
1414#define _MUTEX_INIT_OBJECT_MONITOR
1415#endif
1416
1417#define K_MUTEX_INITIALIZER(obj) \
1418 { \
1419 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1420 .owner = NULL, \
1421 .lock_count = 0, \
1422 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1423 _MUTEX_INIT_OBJECT_MONITOR \
1424 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1425 }
1426
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001427/**
Allan Stephensc98da842016-11-11 15:45:03 -05001428 * INTERNAL_HIDDEN @endcond
1429 */
1430
1431/**
1432 * @defgroup mutex_apis Mutex APIs
1433 * @ingroup kernel_apis
1434 * @{
1435 */
1436
1437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001438 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001440 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001441 *
1442 * extern struct k_mutex @a name;
1443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001447 struct k_mutex name \
1448 __in_section(_k_mutex, static, name) = \
1449 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001452 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001454 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001456 * Upon completion, the mutex is available and does not have an owner.
1457 *
1458 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
1460 * @return N/A
1461 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463
1464/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001465 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001467 * This routine locks @a mutex. If the mutex is locked by another thread,
1468 * the calling thread waits until the mutex becomes available or until
1469 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001471 * A thread is permitted to lock a mutex it has already locked. The operation
1472 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001474 * @param mutex Address of the mutex.
1475 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 * or one of the special values K_NO_WAIT and K_FOREVER.
1477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * @retval 0 if successful.
1479 * @retval -EBUSY if returned without waiting.
1480 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001482extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483
1484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001485 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001487 * This routine unlocks @a mutex. The mutex must already be locked by the
1488 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
1490 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492 * thread.
1493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001494 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001495 *
1496 * @return N/A
1497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001498extern void k_mutex_unlock(struct k_mutex *mutex);
1499
Allan Stephensc98da842016-11-11 15:45:03 -05001500/**
1501 * @} end defgroup mutex_apis
1502 */
1503
1504/**
1505 * @cond INTERNAL_HIDDEN
1506 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507
1508struct k_sem {
1509 _wait_q_t wait_q;
1510 unsigned int count;
1511 unsigned int limit;
1512
1513 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1514};
1515
Allan Stephensc98da842016-11-11 15:45:03 -05001516#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1517 { \
1518 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1519 .count = initial_count, \
1520 .limit = count_limit, \
1521 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1522 }
1523
1524/**
1525 * INTERNAL_HIDDEN @endcond
1526 */
1527
1528/**
1529 * @defgroup semaphore_apis Semaphore APIs
1530 * @ingroup kernel_apis
1531 * @{
1532 */
1533
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * @param sem Address of the semaphore.
1540 * @param initial_count Initial semaphore count.
1541 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001542 *
1543 * @return N/A
1544 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1546 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001547
1548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001551 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001553 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1554 *
1555 * @param sem Address of the semaphore.
1556 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001557 * or one of the special values K_NO_WAIT and K_FOREVER.
1558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * @retval 0 if successful.
1560 * @retval -EBUSY if returned without waiting.
1561 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001562 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001563extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001564
1565/**
1566 * @brief Give a semaphore.
1567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001568 * This routine gives @a sem, unless the semaphore is already at its maximum
1569 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001571 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001574 *
1575 * @return N/A
1576 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001577extern void k_sem_give(struct k_sem *sem);
1578
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001579/**
1580 * @brief Reset a semaphore's count to zero.
1581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001582 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001584 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001585 *
1586 * @return N/A
1587 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001588static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001589{
1590 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591}
1592
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001593/**
1594 * @brief Get a semaphore's count.
1595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001598 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001600 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001601 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001602static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001603{
1604 return sem->count;
1605}
1606
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001610 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001611 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001612 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @param name Name of the semaphore.
1615 * @param initial_count Initial semaphore count.
1616 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001617 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001618#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001619 struct k_sem name \
1620 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001621 K_SEM_INITIALIZER(name, initial_count, count_limit)
1622
Allan Stephensc98da842016-11-11 15:45:03 -05001623/**
1624 * @} end defgroup semaphore_apis
1625 */
1626
1627/**
1628 * @defgroup alert_apis Alert APIs
1629 * @ingroup kernel_apis
1630 * @{
1631 */
1632
Allan Stephens5eceb852016-11-16 10:16:30 -05001633/**
1634 * @typedef k_alert_handler_t
1635 * @brief Alert handler function type.
1636 *
1637 * An alert's alert handler function is invoked by the system workqueue
1638 * when the alert is signalled. The alert handler function is optional,
1639 * and is only invoked if the alert has been initialized with one.
1640 *
1641 * @param alert Address of the alert.
1642 *
1643 * @return 0 if alert has been consumed; non-zero if alert should pend.
1644 */
1645typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001646
1647/**
1648 * @} end defgroup alert_apis
1649 */
1650
1651/**
1652 * @cond INTERNAL_HIDDEN
1653 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001654
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001655#define K_ALERT_DEFAULT NULL
1656#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001658struct k_alert {
1659 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001660 atomic_t send_count;
1661 struct k_work work_item;
1662 struct k_sem sem;
1663
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001664 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001665};
1666
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001667extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001668
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001669#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001670 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001671 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001672 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001673 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001674 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1676 }
1677
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001678/**
Allan Stephensc98da842016-11-11 15:45:03 -05001679 * INTERNAL_HIDDEN @endcond
1680 */
1681
1682/**
1683 * @addtogroup alert_apis
1684 * @{
1685 */
1686
1687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001691 *
1692 * extern struct k_alert @a name;
1693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @param name Name of the alert.
1695 * @param alert_handler Action to take when alert is sent. Specify either
1696 * the address of a function to be invoked by the system workqueue
1697 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1698 * K_ALERT_DEFAULT (which causes the alert to pend).
1699 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001701#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001702 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001703 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001704 K_ALERT_INITIALIZER(name, alert_handler, \
1705 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001706
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @param alert Address of the alert.
1713 * @param handler Action to take when alert is sent. Specify either the address
1714 * of a function to be invoked by the system workqueue thread,
1715 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1716 * K_ALERT_DEFAULT (which causes the alert to pend).
1717 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001718 *
1719 * @return N/A
1720 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001721extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1722 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723
1724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001727 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1730 *
1731 * @param alert Address of the alert.
1732 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 * or one of the special values K_NO_WAIT and K_FOREVER.
1734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @retval 0 if successful.
1736 * @retval -EBUSY if returned without waiting.
1737 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001739extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740
1741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * This routine signals @a alert. The action specified for @a alert will
1745 * be taken, which may trigger the execution of an alert handler function
1746 * and/or cause the alert to pend (assuming the alert has not reached its
1747 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @note Can be called by ISRs.
1750 *
1751 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 *
1753 * @return N/A
1754 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001755extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001756
1757/**
Allan Stephensc98da842016-11-11 15:45:03 -05001758 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759 */
1760
Allan Stephensc98da842016-11-11 15:45:03 -05001761/**
1762 * @cond INTERNAL_HIDDEN
1763 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001764
1765struct k_msgq {
1766 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001767 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768 uint32_t max_msgs;
1769 char *buffer_start;
1770 char *buffer_end;
1771 char *read_ptr;
1772 char *write_ptr;
1773 uint32_t used_msgs;
1774
1775 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1776};
1777
Peter Mitsis1da807e2016-10-06 11:36:59 -04001778#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001779 { \
1780 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001781 .max_msgs = q_max_msgs, \
1782 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001783 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001784 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785 .read_ptr = q_buffer, \
1786 .write_ptr = q_buffer, \
1787 .used_msgs = 0, \
1788 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1789 }
1790
Peter Mitsis1da807e2016-10-06 11:36:59 -04001791/**
Allan Stephensc98da842016-11-11 15:45:03 -05001792 * INTERNAL_HIDDEN @endcond
1793 */
1794
1795/**
1796 * @defgroup msgq_apis Message Queue APIs
1797 * @ingroup kernel_apis
1798 * @{
1799 */
1800
1801/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1805 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001806 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1807 * message is similarly aligned to this boundary, @a q_msg_size must also be
1808 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * The message queue can be accessed outside the module where it is defined
1811 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001812 *
1813 * extern struct k_msgq @a name;
1814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @param q_name Name of the message queue.
1816 * @param q_msg_size Message size (in bytes).
1817 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001818 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001819 */
1820#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1821 static char __noinit __aligned(q_align) \
1822 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001823 struct k_msgq q_name \
1824 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001825 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1826 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001827
Peter Mitsisd7a37502016-10-13 11:37:40 -04001828/**
1829 * @brief Initialize a message queue.
1830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * This routine initializes a message queue object, prior to its first use.
1832 *
Allan Stephensda827222016-11-09 14:23:58 -06001833 * The message queue's ring buffer must contain space for @a max_msgs messages,
1834 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1835 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1836 * that each message is similarly aligned to this boundary, @a q_msg_size
1837 * must also be a multiple of N.
1838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001839 * @param q Address of the message queue.
1840 * @param buffer Pointer to ring buffer that holds queued messages.
1841 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001842 * @param max_msgs Maximum number of messages that can be queued.
1843 *
1844 * @return N/A
1845 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001846extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001847 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001848
1849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001853 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001854 * @note Can be called by ISRs.
1855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @param q Address of the message queue.
1857 * @param data Pointer to the message.
1858 * @param timeout Waiting period to add the message (in milliseconds),
1859 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001861 * @retval 0 if successful.
1862 * @retval -ENOMSG if returned without waiting or after a queue purge.
1863 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001864 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001865extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001866
1867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001868 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * This routine receives a message from message queue @a q in a "first in,
1871 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001872 *
Allan Stephensc98da842016-11-11 15:45:03 -05001873 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @param q Address of the message queue.
1876 * @param data Address of area to hold the received message.
1877 * @param timeout Waiting period to receive the message (in milliseconds),
1878 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * @retval 0 if successful.
1881 * @retval -ENOMSG if returned without waiting.
1882 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001883 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001885
1886/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * This routine discards all unreceived messages in a message queue's ring
1890 * buffer. Any threads that are blocked waiting to send a message to the
1891 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001894 *
1895 * @return N/A
1896 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897extern void k_msgq_purge(struct k_msgq *q);
1898
Peter Mitsis67be2492016-10-07 11:44:34 -04001899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001902 * This routine returns the number of unused entries in a message queue's
1903 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @param q Address of the message queue.
1906 *
1907 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001908 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001909static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001910{
1911 return q->max_msgs - q->used_msgs;
1912}
1913
Peter Mitsisd7a37502016-10-13 11:37:40 -04001914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @param q Address of the message queue.
1920 *
1921 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001922 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001923static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924{
1925 return q->used_msgs;
1926}
1927
Allan Stephensc98da842016-11-11 15:45:03 -05001928/**
1929 * @} end defgroup msgq_apis
1930 */
1931
1932/**
1933 * @defgroup mem_pool_apis Memory Pool APIs
1934 * @ingroup kernel_apis
1935 * @{
1936 */
1937
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001939 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940 void *addr_in_pool;
1941 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001942 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001943};
1944
Allan Stephensc98da842016-11-11 15:45:03 -05001945/**
1946 * @} end defgroup mem_pool_apis
1947 */
1948
1949/**
1950 * @defgroup mailbox_apis Mailbox APIs
1951 * @ingroup kernel_apis
1952 * @{
1953 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954
1955struct k_mbox_msg {
1956 /** internal use only - needed for legacy API support */
1957 uint32_t _mailbox;
1958 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001959 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960 /** application-defined information value */
1961 uint32_t info;
1962 /** sender's message data buffer */
1963 void *tx_data;
1964 /** internal use only - needed for legacy API support */
1965 void *_rx_data;
1966 /** message data block descriptor */
1967 struct k_mem_block tx_block;
1968 /** source thread id */
1969 k_tid_t rx_source_thread;
1970 /** target thread id */
1971 k_tid_t tx_target_thread;
1972 /** internal use only - thread waiting on send (may be a dummy) */
1973 k_tid_t _syncing_thread;
1974#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1975 /** internal use only - semaphore used during asynchronous send */
1976 struct k_sem *_async_sem;
1977#endif
1978};
1979
Allan Stephensc98da842016-11-11 15:45:03 -05001980/**
1981 * @cond INTERNAL_HIDDEN
1982 */
1983
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001984struct k_mbox {
1985 _wait_q_t tx_msg_queue;
1986 _wait_q_t rx_msg_queue;
1987
1988 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1989};
1990
1991#define K_MBOX_INITIALIZER(obj) \
1992 { \
1993 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1994 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1995 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1996 }
1997
Peter Mitsis12092702016-10-14 12:57:23 -04001998/**
Allan Stephensc98da842016-11-11 15:45:03 -05001999 * INTERNAL_HIDDEN @endcond
2000 */
2001
2002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002005 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
2007 * extern struct k_mbox @a name;
2008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002010 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002011#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002012 struct k_mbox name \
2013 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014 K_MBOX_INITIALIZER(name) \
2015
Peter Mitsis12092702016-10-14 12:57:23 -04002016/**
2017 * @brief Initialize a mailbox.
2018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * This routine initializes a mailbox object, prior to its first use.
2020 *
2021 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002022 *
2023 * @return N/A
2024 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025extern void k_mbox_init(struct k_mbox *mbox);
2026
Peter Mitsis12092702016-10-14 12:57:23 -04002027/**
2028 * @brief Send a mailbox message in a synchronous manner.
2029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002030 * This routine sends a message to @a mbox and waits for a receiver to both
2031 * receive and process it. The message data may be in a buffer, in a memory
2032 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * @param mbox Address of the mailbox.
2035 * @param tx_msg Address of the transmit message descriptor.
2036 * @param timeout Waiting period for the message to be received (in
2037 * milliseconds), or one of the special values K_NO_WAIT
2038 * and K_FOREVER. Once the message has been received,
2039 * this routine waits as long as necessary for the message
2040 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @retval 0 if successful.
2043 * @retval -ENOMSG if returned without waiting.
2044 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002045 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002046extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002048
Peter Mitsis12092702016-10-14 12:57:23 -04002049/**
2050 * @brief Send a mailbox message in an asynchronous manner.
2051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002052 * This routine sends a message to @a mbox without waiting for a receiver
2053 * to process it. The message data may be in a buffer, in a memory pool block,
2054 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2055 * will be given when the message has been both received and completely
2056 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @param mbox Address of the mailbox.
2059 * @param tx_msg Address of the transmit message descriptor.
2060 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002061 *
2062 * @return N/A
2063 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002064extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002065 struct k_sem *sem);
2066
Peter Mitsis12092702016-10-14 12:57:23 -04002067/**
2068 * @brief Receive a mailbox message.
2069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070 * This routine receives a message from @a mbox, then optionally retrieves
2071 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @param mbox Address of the mailbox.
2074 * @param rx_msg Address of the receive message descriptor.
2075 * @param buffer Address of the buffer to receive data, or NULL to defer data
2076 * retrieval and message disposal until later.
2077 * @param timeout Waiting period for a message to be received (in
2078 * milliseconds), or one of the special values K_NO_WAIT
2079 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002081 * @retval 0 if successful.
2082 * @retval -ENOMSG if returned without waiting.
2083 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002084 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002085extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002086 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002087
2088/**
2089 * @brief Retrieve mailbox message data into a buffer.
2090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * This routine completes the processing of a received message by retrieving
2092 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002093 *
2094 * Alternatively, this routine can be used to dispose of a received message
2095 * without retrieving its data.
2096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002097 * @param rx_msg Address of the receive message descriptor.
2098 * @param buffer Address of the buffer to receive data, or NULL to discard
2099 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002100 *
2101 * @return N/A
2102 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002103extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002104
2105/**
2106 * @brief Retrieve mailbox message data into a memory pool block.
2107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * This routine completes the processing of a received message by retrieving
2109 * its data into a memory pool block, then disposing of the message.
2110 * The memory pool block that results from successful retrieval must be
2111 * returned to the pool once the data has been processed, even in cases
2112 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002113 *
2114 * Alternatively, this routine can be used to dispose of a received message
2115 * without retrieving its data. In this case there is no need to return a
2116 * memory pool block to the pool.
2117 *
2118 * This routine allocates a new memory pool block for the data only if the
2119 * data is not already in one. If a new block cannot be allocated, the routine
2120 * returns a failure code and the received message is left unchanged. This
2121 * permits the caller to reattempt data retrieval at a later time or to dispose
2122 * of the received message without retrieving its data.
2123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * @param rx_msg Address of a receive message descriptor.
2125 * @param pool Address of memory pool, or NULL to discard data.
2126 * @param block Address of the area to hold memory pool block info.
2127 * @param timeout Waiting period to wait for a memory pool block (in
2128 * milliseconds), or one of the special values K_NO_WAIT
2129 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131 * @retval 0 if successful.
2132 * @retval -ENOMEM if returned without waiting.
2133 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002134 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002135extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002136 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002137 struct k_mem_block *block, int32_t timeout);
2138
Allan Stephensc98da842016-11-11 15:45:03 -05002139/**
2140 * @} end defgroup mailbox_apis
2141 */
2142
2143/**
2144 * @cond INTERNAL_HIDDEN
2145 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146
2147struct k_pipe {
2148 unsigned char *buffer; /* Pipe buffer: may be NULL */
2149 size_t size; /* Buffer size */
2150 size_t bytes_used; /* # bytes used in buffer */
2151 size_t read_index; /* Where in buffer to read from */
2152 size_t write_index; /* Where in buffer to write */
2153
2154 struct {
2155 _wait_q_t readers; /* Reader wait queue */
2156 _wait_q_t writers; /* Writer wait queue */
2157 } wait_q;
2158
2159 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2160};
2161
Peter Mitsise5d9c582016-10-14 14:44:57 -04002162#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163 { \
2164 .buffer = pipe_buffer, \
2165 .size = pipe_buffer_size, \
2166 .bytes_used = 0, \
2167 .read_index = 0, \
2168 .write_index = 0, \
2169 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2170 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2171 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2172 }
2173
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174/**
Allan Stephensc98da842016-11-11 15:45:03 -05002175 * INTERNAL_HIDDEN @endcond
2176 */
2177
2178/**
2179 * @defgroup pipe_apis Pipe APIs
2180 * @ingroup kernel_apis
2181 * @{
2182 */
2183
2184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002187 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 *
2189 * extern struct k_pipe @a name;
2190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002191 * @param name Name of the pipe.
2192 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2193 * or zero if no ring buffer is used.
2194 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002196#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2197 static unsigned char __noinit __aligned(pipe_align) \
2198 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002199 struct k_pipe name \
2200 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002201 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002204 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * This routine initializes a pipe object, prior to its first use.
2207 *
2208 * @param pipe Address of the pipe.
2209 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2210 * is used.
2211 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2212 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213 *
2214 * @return N/A
2215 */
2216extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2217 size_t size);
2218
2219/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002220 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002222 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002224 * @param pipe Address of the pipe.
2225 * @param data Address of data to write.
2226 * @param bytes_to_write Size of data (in bytes).
2227 * @param bytes_written Address of area to hold the number of bytes written.
2228 * @param min_xfer Minimum number of bytes to write.
2229 * @param timeout Waiting period to wait for the data to be written (in
2230 * milliseconds), or one of the special values K_NO_WAIT
2231 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * @retval 0 if at least @a min_xfer data bytes were written.
2234 * @retval -EIO if returned without waiting; zero data bytes were written.
2235 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
2236 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002238extern int k_pipe_put(struct k_pipe *pipe, void *data,
2239 size_t bytes_to_write, size_t *bytes_written,
2240 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241
2242/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002243 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002245 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002247 * @param pipe Address of the pipe.
2248 * @param data Address to place the data read from pipe.
2249 * @param bytes_to_read Maximum number of data bytes to read.
2250 * @param bytes_read Address of area to hold the number of bytes read.
2251 * @param min_xfer Minimum number of data bytes to read.
2252 * @param timeout Waiting period to wait for the data to be read (in
2253 * milliseconds), or one of the special values K_NO_WAIT
2254 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002256 * @retval 0 if at least @a min_xfer data bytes were read.
2257 * @retval -EIO if returned without waiting; zero data bytes were read.
2258 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
2259 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002261extern int k_pipe_get(struct k_pipe *pipe, void *data,
2262 size_t bytes_to_read, size_t *bytes_read,
2263 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264
2265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002266 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * This routine writes the data contained in a memory block to @a pipe.
2269 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002272 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273 * @param block Memory block containing data to send
2274 * @param size Number of data bytes in memory block to send
2275 * @param sem Semaphore to signal upon completion (else NULL)
2276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002277 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278 */
2279extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2280 size_t size, struct k_sem *sem);
2281
2282/**
Allan Stephensc98da842016-11-11 15:45:03 -05002283 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 */
2285
Allan Stephensc98da842016-11-11 15:45:03 -05002286/**
2287 * @cond INTERNAL_HIDDEN
2288 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002290struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002291 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002292 uint32_t num_blocks;
2293 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294 char *buffer;
2295 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002296 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002298 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299};
2300
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002301#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2302 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303 { \
2304 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002305 .num_blocks = slab_num_blocks, \
2306 .block_size = slab_block_size, \
2307 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308 .free_list = NULL, \
2309 .num_used = 0, \
2310 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2311 }
2312
Peter Mitsis578f9112016-10-07 13:50:31 -04002313/**
Allan Stephensc98da842016-11-11 15:45:03 -05002314 * INTERNAL_HIDDEN @endcond
2315 */
2316
2317/**
2318 * @defgroup mem_slab_apis Memory Slab APIs
2319 * @ingroup kernel_apis
2320 * @{
2321 */
2322
2323/**
Allan Stephensda827222016-11-09 14:23:58 -06002324 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002325 *
Allan Stephensda827222016-11-09 14:23:58 -06002326 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002328 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2329 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002330 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002331 *
Allan Stephensda827222016-11-09 14:23:58 -06002332 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002333 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 *
2335 * extern struct k_mem_slab @a name;
2336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * @param name Name of the memory slab.
2338 * @param slab_block_size Size of each memory block (in bytes).
2339 * @param slab_num_blocks Number memory blocks.
2340 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002341 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002342#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2343 char __noinit __aligned(slab_align) \
2344 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2345 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002346 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002347 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2348 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002350/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002351 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002354 *
Allan Stephensda827222016-11-09 14:23:58 -06002355 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2356 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2357 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2358 * To ensure that each memory block is similarly aligned to this boundary,
2359 * @a slab_block_size must also be a multiple of N.
2360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @param slab Address of the memory slab.
2362 * @param buffer Pointer to buffer used for the memory blocks.
2363 * @param block_size Size of each memory block (in bytes).
2364 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002365 *
2366 * @return N/A
2367 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002368extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002369 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002370
2371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 * @param slab Address of the memory slab.
2377 * @param mem Pointer to block address area.
2378 * @param timeout Maximum time to wait for operation to complete
2379 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2380 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * @retval 0 if successful. The block address area pointed at by @a mem
2383 * is set to the starting address of the memory block.
2384 * @retval -ENOMEM if failed immediately.
2385 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002386 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002387extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2388 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002389
2390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * This routine releases a previously allocated memory block back to its
2394 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @param slab Address of the memory slab.
2397 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002398 *
2399 * @return N/A
2400 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002401extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002406 * This routine gets the number of memory blocks that are currently
2407 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002412 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002413static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002415 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416}
2417
Peter Mitsisc001aa82016-10-13 13:53:37 -04002418/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * This routine gets the number of memory blocks that are currently
2422 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002426 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002427 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002428static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002429{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002430 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002431}
2432
Allan Stephensc98da842016-11-11 15:45:03 -05002433/**
2434 * @} end defgroup mem_slab_apis
2435 */
2436
2437/**
2438 * @cond INTERNAL_HIDDEN
2439 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002441/*
2442 * Memory pool requires a buffer and two arrays of structures for the
2443 * memory block accounting:
2444 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2445 * status of four blocks of memory.
2446 */
2447struct k_mem_pool_quad_block {
2448 char *mem_blocks; /* pointer to the first of four memory blocks */
2449 uint32_t mem_status; /* four bits. If bit is set, memory block is
2450 allocated */
2451};
2452/*
2453 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2454 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2455 * block size is 4 times less than the previous one and thus requires 4 times
2456 * bigger array of k_mem_pool_quad_block structures to keep track of the
2457 * memory blocks.
2458 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002460/*
2461 * The array of k_mem_pool_block_set keeps the information of each array of
2462 * k_mem_pool_quad_block structures
2463 */
2464struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002465 size_t block_size; /* memory block size */
2466 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002467 struct k_mem_pool_quad_block *quad_block;
2468 int count;
2469};
2470
2471/* Memory pool descriptor */
2472struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002473 size_t max_block_size;
2474 size_t min_block_size;
2475 uint32_t nr_of_maxblocks;
2476 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002477 struct k_mem_pool_block_set *block_set;
2478 char *bufblock;
2479 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2481};
2482
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002483#ifdef CONFIG_ARM
2484#define _SECTION_TYPE_SIGN "%"
2485#else
2486#define _SECTION_TYPE_SIGN "@"
2487#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002489/*
2490 * Static memory pool initialization
2491 */
Allan Stephensc98da842016-11-11 15:45:03 -05002492
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002493/*
2494 * Use .altmacro to be able to recalculate values and pass them as string
2495 * arguments when calling assembler macros resursively
2496 */
2497__asm__(".altmacro\n\t");
2498
2499/*
2500 * Recursively calls a macro
2501 * The followig global symbols need to be initialized:
2502 * __memory_pool_max_block_size - maximal size of the memory block
2503 * __memory_pool_min_block_size - minimal size of the memory block
2504 * Notes:
2505 * Global symbols are used due the fact that assembler macro allows only
2506 * one argument be passed with the % conversion
2507 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2508 * is used instead of / 4.
2509 * n_max argument needs to go first in the invoked macro, as some
2510 * assemblers concatenate \name and %(\n_max * 4) arguments
2511 * if \name goes first
2512 */
2513__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2514 ".ifge __memory_pool_max_block_size >> 2 -"
2515 " __memory_pool_min_block_size\n\t\t"
2516 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2517 "\\macro_name %(\\n_max * 4) \\name\n\t"
2518 ".endif\n\t"
2519 ".endm\n");
2520
2521/*
2522 * Build quad blocks
2523 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2524 * structures and recursively calls itself for the next array, 4 times
2525 * larger.
2526 * The followig global symbols need to be initialized:
2527 * __memory_pool_max_block_size - maximal size of the memory block
2528 * __memory_pool_min_block_size - minimal size of the memory block
2529 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2530 */
2531__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002532 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002533 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2534 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2535 ".if \\n_max % 4\n\t\t"
2536 ".skip __memory_pool_quad_block_size\n\t"
2537 ".endif\n\t"
2538 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2539 ".endm\n");
2540
2541/*
2542 * Build block sets and initialize them
2543 * Macro initializes the k_mem_pool_block_set structure and
2544 * recursively calls itself for the next one.
2545 * The followig global symbols need to be initialized:
2546 * __memory_pool_max_block_size - maximal size of the memory block
2547 * __memory_pool_min_block_size - minimal size of the memory block
2548 * __memory_pool_block_set_count, the number of the elements in the
2549 * block set array must be set to 0. Macro calculates it's real
2550 * value.
2551 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2552 * structures, _build_quad_blocks must be called prior it.
2553 */
2554__asm__(".macro _build_block_set n_max, name\n\t"
2555 ".int __memory_pool_max_block_size\n\t" /* block_size */
2556 ".if \\n_max % 4\n\t\t"
2557 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2558 ".else\n\t\t"
2559 ".int \\n_max >> 2\n\t"
2560 ".endif\n\t"
2561 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2562 ".int 0\n\t" /* count */
2563 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2564 "__do_recurse _build_block_set \\name \\n_max\n\t"
2565 ".endm\n");
2566
2567/*
2568 * Build a memory pool structure and initialize it
2569 * Macro uses __memory_pool_block_set_count global symbol,
2570 * block set addresses and buffer address, it may be called only after
2571 * _build_block_set
2572 */
2573__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002574 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002575 _SECTION_TYPE_SIGN "progbits\n\t"
2576 ".globl \\name\n\t"
2577 "\\name:\n\t"
2578 ".int \\max_size\n\t" /* max_block_size */
2579 ".int \\min_size\n\t" /* min_block_size */
2580 ".int \\n_max\n\t" /* nr_of_maxblocks */
2581 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2582 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2583 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2584 ".int 0\n\t" /* wait_q->head */
2585 ".int 0\n\t" /* wait_q->next */
2586 ".popsection\n\t"
2587 ".endm\n");
2588
2589#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2590 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2591 _SECTION_TYPE_SIGN "progbits\n\t"); \
2592 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2593 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2594 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2595 STRINGIFY(name) "\n\t"); \
2596 __asm__(".popsection\n\t")
2597
2598#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2599 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2600 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2601 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2602 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002603 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002604 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2605 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2606 STRINGIFY(name) "\n\t"); \
2607 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2608 __asm__(".int __memory_pool_block_set_count\n\t"); \
2609 __asm__(".popsection\n\t"); \
2610 extern uint32_t _mem_pool_block_set_count_##name; \
2611 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2612
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002613#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2614 char __noinit __aligned(align) \
2615 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002616
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002617/*
2618 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2619 * to __memory_pool_quad_block_size absolute symbol.
2620 * This function does not get called, but compiler calculates the value and
2621 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2622 */
2623static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2624{
2625 __asm__(".globl __memory_pool_quad_block_size\n\t"
2626#ifdef CONFIG_NIOS2
2627 "__memory_pool_quad_block_size = %0\n\t"
2628#else
2629 "__memory_pool_quad_block_size = %c0\n\t"
2630#endif
2631 :
2632 : "n"(sizeof(struct k_mem_pool_quad_block)));
2633}
2634
2635/**
Allan Stephensc98da842016-11-11 15:45:03 -05002636 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002637 */
2638
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002639/**
Allan Stephensc98da842016-11-11 15:45:03 -05002640 * @addtogroup mem_pool_apis
2641 * @{
2642 */
2643
2644/**
2645 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2648 * long. The memory pool allows blocks to be repeatedly partitioned into
2649 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2650 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002651 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002653 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 * If the pool is to be accessed outside the module where it is defined, it
2655 * can be declared via
2656 *
2657 * extern struct k_mem_pool @a name;
2658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002659 * @param name Name of the memory pool.
2660 * @param min_size Size of the smallest blocks in the pool (in bytes).
2661 * @param max_size Size of the largest blocks in the pool (in bytes).
2662 * @param n_max Number of maximum sized blocks in the pool.
2663 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002664 */
2665#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002666 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2667 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002668 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002669 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2670 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2671 extern struct k_mem_pool name
2672
Peter Mitsis937042c2016-10-13 13:18:26 -04002673/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @param pool Address of the memory pool.
2679 * @param block Pointer to block descriptor for the allocated memory.
2680 * @param size Amount of memory to allocate (in bytes).
2681 * @param timeout Maximum time to wait for operation to complete
2682 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2683 * or K_FOREVER to wait as long as necessary.
2684 *
2685 * @retval 0 if successful. The @a data field of the block descriptor
2686 * is set to the starting address of the memory block.
2687 * @retval -ENOMEM if unable to allocate a memory block.
2688 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002689 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002690extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002691 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002692
2693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * This routine releases a previously allocated memory block back to its
2697 * memory pool.
2698 *
2699 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002700 *
2701 * @return N/A
2702 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002704
2705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002706 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * This routine instructs a memory pool to concatenate unused memory blocks
2709 * into larger blocks wherever possible. Manually defragmenting the memory
2710 * pool may speed up future allocations of memory blocks by eliminating the
2711 * need for the memory pool to perform an automatic partial defragmentation.
2712 *
2713 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002714 *
2715 * @return N/A
2716 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002717extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002718
2719/**
Allan Stephensc98da842016-11-11 15:45:03 -05002720 * @} end addtogroup mem_pool_apis
2721 */
2722
2723/**
2724 * @defgroup heap_apis Heap Memory Pool APIs
2725 * @ingroup kernel_apis
2726 * @{
2727 */
2728
2729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002730 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002733 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002738 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002739extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002740
2741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002743 *
2744 * This routine provides traditional free() semantics. The memory being
2745 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002748 *
2749 * @return N/A
2750 */
2751extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752
Allan Stephensc98da842016-11-11 15:45:03 -05002753/**
2754 * @} end defgroup heap_apis
2755 */
2756
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757/*
2758 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2759 * hook into the device subsystem, which itself uses nanokernel semaphores,
2760 * and thus currently requires the definition of nano_sem.
2761 */
2762#include <legacy.h>
2763#include <arch/cpu.h>
2764
2765/*
2766 * private APIs that are utilized by one or more public APIs
2767 */
2768
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002771extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772
2773#ifdef __cplusplus
2774}
2775#endif
2776
Andrew Boiee004dec2016-11-07 09:01:19 -08002777#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2778/*
2779 * Define new and delete operators.
2780 * At this moment, the operators do nothing since objects are supposed
2781 * to be statically allocated.
2782 */
2783inline void operator delete(void *ptr)
2784{
2785 (void)ptr;
2786}
2787
2788inline void operator delete[](void *ptr)
2789{
2790 (void)ptr;
2791}
2792
2793inline void *operator new(size_t size)
2794{
2795 (void)size;
2796 return NULL;
2797}
2798
2799inline void *operator new[](size_t size)
2800{
2801 (void)size;
2802 return NULL;
2803}
2804
2805/* Placement versions of operator new and delete */
2806inline void operator delete(void *ptr1, void *ptr2)
2807{
2808 (void)ptr1;
2809 (void)ptr2;
2810}
2811
2812inline void operator delete[](void *ptr1, void *ptr2)
2813{
2814 (void)ptr1;
2815 (void)ptr2;
2816}
2817
2818inline void *operator new(size_t size, void *ptr)
2819{
2820 (void)size;
2821 return ptr;
2822}
2823
2824inline void *operator new[](size_t size, void *ptr)
2825{
2826 (void)size;
2827 return ptr;
2828}
2829
2830#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2831
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832#endif /* _kernel__h_ */