blob: 784a89ab70284184881eb050d1f7707bb31caf5d [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
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400106
107/**
Allan Stephensc98da842016-11-11 15:45:03 -0500108 * @defgroup thread_apis Thread APIs
109 * @ingroup kernel_apis
110 * @{
111 */
112
113/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500114 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500116 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500118 * The new thread may be scheduled for immediate execution or a delayed start.
119 * If the newly spawned thread does not have a delayed start the kernel
120 * scheduler may preempt the current thread to allow the new thread to
121 * execute.
122 *
123 * Thread options are architecture-specific, and can include K_ESSENTIAL,
124 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
125 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400126 *
127 * @param stack Pointer to the stack space.
128 * @param stack_size Stack size in bytes.
129 * @param entry Thread entry function.
130 * @param p1 1st entry point parameter.
131 * @param p2 2nd entry point parameter.
132 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500133 * @param prio Thread priority.
134 * @param options Thread options.
135 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500137 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400139extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
Allan Stephensc98da842016-11-11 15:45:03 -0500140 void (*entry)(void *, void *, void *),
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400141 void *p1, void *p2, void *p3,
142 int32_t prio, uint32_t options, int32_t delay);
143
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500145 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400146 *
Allan Stephensc98da842016-11-11 15:45:03 -0500147 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500148 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500150 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400151 *
152 * @return N/A
153 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400154extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400155
156/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500157 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400158 *
159 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500160 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400161 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400162 * @return N/A
163 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400164extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400165
166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500167 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500169 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400170 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500171 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400172 *
173 * @return N/A
174 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400175extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400176
177/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500178 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500180 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500182 * If @a thread is not currently sleeping, the routine has no effect.
183 *
184 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400185 *
186 * @return N/A
187 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400188extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400189
190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500191 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500193 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400194 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400195extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400196
197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500200 * This routine prevents @a thread from executing if it has not yet started
201 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500203 * @param thread ID of thread to cancel.
204 *
205 * @retval 0 if successful.
206 * @retval -EINVAL if the thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400208extern int k_thread_cancel(k_tid_t thread);
209
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210/**
Allan Stephensc98da842016-11-11 15:45:03 -0500211 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500213 * This routine permanently stops execution of @a thread. The thread is taken
214 * off all kernel queues it is part of (i.e. the ready queue, the timeout
215 * queue, or a kernel object wait queue). However, any kernel resources the
216 * thread might currently own (such as mutexes or memory blocks) are not
217 * released. It is the responsibility of the caller of this routine to ensure
218 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
222 * @return N/A
223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400224extern void k_thread_abort(k_tid_t thread);
225
Allan Stephensc98da842016-11-11 15:45:03 -0500226/**
227 * @cond INTERNAL_HIDDEN
228 */
229
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400230#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400231#define _THREAD_TIMEOUT_INIT(obj) \
232 (obj).nano_timeout = { \
233 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400234 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400235 .wait_q = NULL, \
236 .delta_ticks_from_prev = -1, \
237 },
238#else
239#define _THREAD_TIMEOUT_INIT(obj)
240#endif
241
242#ifdef CONFIG_ERRNO
243#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
244#else
245#define _THREAD_ERRNO_INIT(obj)
246#endif
247
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400248struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249 union {
250 char *init_stack;
251 struct k_thread *thread;
252 };
253 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500254 void (*init_entry)(void *, void *, void *);
255 void *init_p1;
256 void *init_p2;
257 void *init_p3;
258 int init_prio;
259 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400260 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500261 void (*init_abort)(void);
262 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400263};
264
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400265#define _THREAD_INITIALIZER(stack, stack_size, \
266 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500267 prio, options, delay, abort, groups) \
268 { \
269 .init_stack = (stack), \
270 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400271 .init_entry = (void (*)(void *, void *, void *))entry, \
272 .init_p1 = (void *)p1, \
273 .init_p2 = (void *)p2, \
274 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500275 .init_prio = (prio), \
276 .init_options = (options), \
277 .init_delay = (delay), \
278 .init_abort = (abort), \
279 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400280 }
281
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400282/**
Allan Stephensc98da842016-11-11 15:45:03 -0500283 * INTERNAL_HIDDEN @endcond
284 */
285
286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500287 * @brief Statically define and initialize a thread.
288 *
289 * The thread may be scheduled for immediate execution or a delayed start.
290 *
291 * Thread options are architecture-specific, and can include K_ESSENTIAL,
292 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
293 * them using "|" (the logical OR operator).
294 *
295 * The ID of the thread can be accessed using:
296 *
297 * extern const k_tid_t @a name;
298 *
299 * @param name Name of the thread.
300 * @param stack_size Stack size in bytes.
301 * @param entry Thread entry function.
302 * @param p1 1st entry point parameter.
303 * @param p2 2nd entry point parameter.
304 * @param p3 3rd entry point parameter.
305 * @param prio Thread priority.
306 * @param options Thread options.
307 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400308 *
309 * @internal It has been observed that the x86 compiler by default aligns
310 * these _static_thread_data structures to 32-byte boundaries, thereby
311 * wasting space. To work around this, force a 4-byte alignment.
312 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500313#define K_THREAD_DEFINE(name, stack_size, \
314 entry, p1, p2, p3, \
315 prio, options, delay) \
316 char __noinit __stack _k_thread_obj_##name[stack_size]; \
317 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500318 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500319 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
320 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500321 NULL, 0); \
322 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400323
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500325 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500327 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500329 * @param thread ID of thread whose priority is needed.
330 *
331 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400332 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500333extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400334
335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500336 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500338 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400339 *
340 * Rescheduling can occur immediately depending on the priority @a thread is
341 * set to:
342 *
343 * - If its priority is raised above the priority of the caller of this
344 * function, and the caller is preemptible, @a thread will be scheduled in.
345 *
346 * - If the caller operates on itself, it lowers its priority below that of
347 * other threads in the system, and the caller is preemptible, the thread of
348 * highest priority will be scheduled in.
349 *
350 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
351 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
352 * highest priority.
353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500354 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355 * @param prio New priority.
356 *
357 * @warning Changing the priority of a thread currently involved in mutex
358 * priority inheritance may result in undefined behavior.
359 *
360 * @return N/A
361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400362extern void k_thread_priority_set(k_tid_t thread, int prio);
363
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500365 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500367 * This routine prevents the kernel scheduler from making @a thread the
368 * current thread. All other internal operations on @a thread are still
369 * performed; for example, any timeout it is waiting on keeps ticking,
370 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500372 * If @a thread is already suspended, the routine has no effect.
373 *
374 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400375 *
376 * @return N/A
377 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400378extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379
380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500383 * This routine allows the kernel scheduler to make @a thread the current
384 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500386 * If @a thread is not currently suspended, the routine has no effect.
387 *
388 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
390 * @return N/A
391 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400392extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400393
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500395 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * This routine specifies how the scheduler will perform time slicing of
398 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500400 * To enable time slicing, @a slice must be non-zero. The scheduler
401 * ensures that no thread runs for more than the specified time limit
402 * before other threads of that priority are given a chance to execute.
403 * Any thread whose priority is higher than @a prio is exempted, and may
404 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 * execute. Once the scheduler selects a thread for execution, there is no
408 * minimum guaranteed time the thread will execute before threads of greater or
409 * equal priority are scheduled.
410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500411 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412 * for execution, this routine has no effect; the thread is immediately
413 * rescheduled after the slice period expires.
414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500415 * To disable timeslicing, set both @a slice and @a prio to zero.
416 *
417 * @param slice Maximum time slice length (in milliseconds).
418 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400419 *
420 * @return N/A
421 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400422extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400423
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400424/**
Allan Stephensc98da842016-11-11 15:45:03 -0500425 * @} end defgroup thread_apis
426 */
427
428/**
429 * @addtogroup isr_apis
430 * @{
431 */
432
433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephensc98da842016-11-11 15:45:03 -0500436 * This routine allows the caller to customize its actions, depending on
437 * whether it is a thread or an ISR.
438 *
439 * @note Can be called by ISRs.
440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500441 * @return 0 if invoked by a thread.
442 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400443 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500444extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445
Benjamin Walsh445830d2016-11-10 15:54:27 -0500446/**
447 * @brief Determine if code is running in a preemptible thread.
448 *
Allan Stephensc98da842016-11-11 15:45:03 -0500449 * This routine allows the caller to customize its actions, depending on
450 * whether it can be preempted by another thread. The routine returns a 'true'
451 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500452 *
Allan Stephensc98da842016-11-11 15:45:03 -0500453 * - The code is running in a thread, not at ISR.
454 * - The thread's priority is in the preemptible range.
455 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500456 *
Allan Stephensc98da842016-11-11 15:45:03 -0500457 * @note Can be called by ISRs.
458 *
459 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500460 * @return Non-zero if invoked by a preemptible thread.
461 */
462extern int k_is_preempt_thread(void);
463
Allan Stephensc98da842016-11-11 15:45:03 -0500464/**
465 * @} end addtogroup isr_apis
466 */
467
468/**
469 * @addtogroup thread_apis
470 * @{
471 */
472
473/**
474 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500475 *
Allan Stephensc98da842016-11-11 15:45:03 -0500476 * This routine prevents the current thread from being preempted by another
477 * thread by instructing the scheduler to treat it as a cooperative thread.
478 * If the thread subsequently performs an operation that makes it unready,
479 * it will be context switched out in the normal manner. When the thread
480 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500481 *
Allan Stephensc98da842016-11-11 15:45:03 -0500482 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500483 *
Allan Stephensc98da842016-11-11 15:45:03 -0500484 * @note k_sched_lock() and k_sched_unlock() should normally be used
485 * when the operation being performed can be safely interrupted by ISRs.
486 * However, if the amount of processing involved is very small, better
487 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500488 *
489 * @return N/A
490 */
491extern void k_sched_lock(void);
492
Allan Stephensc98da842016-11-11 15:45:03 -0500493/**
494 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500495 *
Allan Stephensc98da842016-11-11 15:45:03 -0500496 * This routine reverses the effect of a previous call to k_sched_lock().
497 * A thread must call the routine once for each time it called k_sched_lock()
498 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500499 *
500 * @return N/A
501 */
502extern void k_sched_unlock(void);
503
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400504/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500505 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * Custom data is not used by the kernel itself, and is freely available
510 * for a thread to use as it sees fit. It can be used as a framework
511 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500513 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400514 *
515 * @return N/A
516 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400517extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400518
519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500520 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500522 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500524 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400525 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400526extern void *k_thread_custom_data_get(void);
527
528/**
Allan Stephensc98da842016-11-11 15:45:03 -0500529 * @} end addtogroup thread_apis
530 */
531
532/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533 * kernel timing
534 */
535
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400536#include <sys_clock.h>
537
Johan Hedberg14471692016-11-13 10:52:15 +0200538/* Convenience helpers to convert durations into milliseconds */
539#define K_MSEC(ms) (ms)
540#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
541#define K_MINUTES(m) K_SECONDS((m) * 60)
542#define K_HOURS(h) K_MINUTES((h) * 60)
543
Allan Stephensc98da842016-11-11 15:45:03 -0500544/**
545 * @cond INTERNAL_HIDDEN
546 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400547
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500548/* added tick needed to account for tick in progress */
549#define _TICK_ALIGN 1
550
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400551static int64_t __ticks_to_ms(int64_t ticks)
552{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400553#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400554 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400555#else
556 __ASSERT(ticks == 0, "");
557 return 0;
558#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400559}
560
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400561/* timeouts */
562
563struct _timeout;
564typedef void (*_timeout_func_t)(struct _timeout *t);
565
566struct _timeout {
567 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400568 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400569 sys_dlist_t *wait_q;
570 int32_t delta_ticks_from_prev;
571 _timeout_func_t func;
572};
573
Allan Stephensc98da842016-11-11 15:45:03 -0500574/**
575 * INTERNAL_HIDDEN @endcond
576 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500577
Allan Stephensc98da842016-11-11 15:45:03 -0500578/**
579 * @cond INTERNAL_HIDDEN
580 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400581
582struct k_timer {
583 /*
584 * _timeout structure must be first here if we want to use
585 * dynamic timer allocation. timeout.node is used in the double-linked
586 * list of free timers
587 */
588 struct _timeout timeout;
589
Allan Stephens45bfa372016-10-12 12:39:42 -0500590 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591 _wait_q_t wait_q;
592
593 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500594 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595
596 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500597 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598
599 /* timer period */
600 int32_t period;
601
Allan Stephens45bfa372016-10-12 12:39:42 -0500602 /* timer status */
603 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400604
Allan Stephens45bfa372016-10-12 12:39:42 -0500605 /* used to support legacy timer APIs */
606 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400607
608 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
609};
610
Allan Stephens1342adb2016-11-03 13:54:53 -0500611#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400612 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500613 .timeout.delta_ticks_from_prev = -1, \
614 .timeout.wait_q = NULL, \
615 .timeout.thread = NULL, \
616 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400617 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500618 .expiry_fn = expiry, \
619 .stop_fn = stop, \
620 .status = 0, \
621 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
623 }
624
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625/**
Allan Stephensc98da842016-11-11 15:45:03 -0500626 * INTERNAL_HIDDEN @endcond
627 */
628
629/**
630 * @defgroup timer_apis Timer APIs
631 * @ingroup kernel_apis
632 * @{
633 */
634
635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500636 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500638 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639 *
640 * extern struct k_timer @a name;
641 *
642 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500643 * @param expiry_fn Function to invoke each time the timer expires.
644 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400645 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500646#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500647 struct k_timer name \
648 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500649 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400650
Allan Stephens45bfa372016-10-12 12:39:42 -0500651/**
652 * @brief Initialize a timer.
653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500654 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500655 *
656 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500657 * @param expiry_fn Function to invoke each time the timer expires.
658 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500659 *
660 * @return N/A
661 */
662extern void k_timer_init(struct k_timer *timer,
663 void (*expiry_fn)(struct k_timer *),
664 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700665
Allan Stephens45bfa372016-10-12 12:39:42 -0500666/**
667 * @brief Start a timer.
668 *
669 * This routine starts a timer, and resets its status to zero. The timer
670 * begins counting down using the specified duration and period values.
671 *
672 * Attempting to start a timer that is already running is permitted.
673 * The timer's status is reset to zero and the timer begins counting down
674 * using the new duration and period values.
675 *
676 * @param timer Address of timer.
677 * @param duration Initial timer duration (in milliseconds).
678 * @param period Timer period (in milliseconds).
679 *
680 * @return N/A
681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400682extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500683 int32_t duration, int32_t period);
684
685/**
686 * @brief Stop a timer.
687 *
688 * This routine stops a running timer prematurely. The timer's stop function,
689 * if one exists, is invoked by the caller.
690 *
691 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500692 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500693 *
694 * @param timer Address of timer.
695 *
696 * @return N/A
697 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400698extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500699
700/**
701 * @brief Read timer status.
702 *
703 * This routine reads the timer's status, which indicates the number of times
704 * it has expired since its status was last read.
705 *
706 * Calling this routine resets the timer's status to zero.
707 *
708 * @param timer Address of timer.
709 *
710 * @return Timer status.
711 */
712extern uint32_t k_timer_status_get(struct k_timer *timer);
713
714/**
715 * @brief Synchronize thread to timer expiration.
716 *
717 * This routine blocks the calling thread until the timer's status is non-zero
718 * (indicating that it has expired at least once since it was last examined)
719 * or the timer is stopped. If the timer status is already non-zero,
720 * or the timer is already stopped, the caller continues without waiting.
721 *
722 * Calling this routine resets the timer's status to zero.
723 *
724 * This routine must not be used by interrupt handlers, since they are not
725 * allowed to block.
726 *
727 * @param timer Address of timer.
728 *
729 * @return Timer status.
730 */
731extern uint32_t k_timer_status_sync(struct k_timer *timer);
732
733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500734 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500735 *
736 * This routine computes the (approximate) time remaining before a running
737 * timer next expires. If the timer is not running, it returns zero.
738 *
739 * @param timer Address of timer.
740 *
741 * @return Remaining time (in milliseconds).
742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400743extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400744
Allan Stephensc98da842016-11-11 15:45:03 -0500745/**
746 * @} end defgroup timer_apis
747 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400748
Allan Stephensc98da842016-11-11 15:45:03 -0500749/**
750 * @defgroup clock_apis Kernel Clock APIs
751 * @ingroup kernel_apis
752 * @{
753 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500754
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400755/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500756 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500758 * This routine returns the elapsed time since the system booted,
759 * in milliseconds.
760 *
761 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400762 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400764
765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * This routine returns the lower 32-bits of the elapsed time since the system
769 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * This routine can be more efficient than k_uptime_get(), as it reduces the
772 * need for interrupt locking and 64-bit math. However, the 32-bit result
773 * cannot hold a system uptime time larger than approximately 50 days, so the
774 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400777 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400778extern uint32_t k_uptime_get_32(void);
779
780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * This routine computes the elapsed time between the current system uptime
784 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * @param reftime Pointer to a reference time, which is updated to the current
787 * uptime upon return.
788 *
789 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400790 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400791extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400792
793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * This routine computes the elapsed time between the current system uptime
797 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * This routine can be more efficient than k_uptime_delta(), as it reduces the
800 * need for interrupt locking and 64-bit math. However, the 32-bit result
801 * cannot hold an elapsed time larger than approximately 50 days, so the
802 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @param reftime Pointer to a reference time, which is updated to the current
805 * uptime upon return.
806 *
807 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400808 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400809extern uint32_t k_uptime_delta_32(int64_t *reftime);
810
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * This routine returns the current time, as measured by the system's hardware
815 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400819extern uint32_t k_cycle_get_32(void);
820
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400821/**
Allan Stephensc98da842016-11-11 15:45:03 -0500822 * @} end defgroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400823 */
824
Allan Stephensc98da842016-11-11 15:45:03 -0500825/**
826 * @cond INTERNAL_HIDDEN
827 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400828
829struct k_fifo {
830 _wait_q_t wait_q;
831 sys_slist_t data_q;
832
833 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
834};
835
Allan Stephensc98da842016-11-11 15:45:03 -0500836#define K_FIFO_INITIALIZER(obj) \
837 { \
838 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
839 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
840 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
841 }
842
843/**
844 * INTERNAL_HIDDEN @endcond
845 */
846
847/**
848 * @defgroup fifo_apis Fifo APIs
849 * @ingroup kernel_apis
850 * @{
851 */
852
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
860 * @return N/A
861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400862extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863
864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * This routine adds a data item to @a fifo. A fifo data item must be
868 * aligned on a 4-byte boundary, and the first 32 bits of the item are
869 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @note Can be called by ISRs.
872 *
873 * @param fifo Address of the fifo.
874 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
876 * @return N/A
877 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400878extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879
880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * This routine adds a list of data items to @a fifo in one operation.
884 * The data items must be in a singly-linked list, with the first 32 bits
885 * each data item pointing to the next data item; the list must be
886 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @param fifo Address of the fifo.
891 * @param head Pointer to first node in singly-linked list.
892 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 *
894 * @return N/A
895 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400896extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897
898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * This routine adds a list of data items to @a fifo in one operation.
902 * The data items must be in a singly-linked list implemented using a
903 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400904 * and must be re-initialized via sys_slist_init().
905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 * @note Can be called by ISRs.
907 *
908 * @param fifo Address of the fifo.
909 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 *
911 * @return N/A
912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400914
915/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * This routine removes a data item from @a fifo in a "first in, first out"
919 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
922 *
923 * @param fifo Address of the fifo.
924 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400925 * or one of the special values K_NO_WAIT and K_FOREVER.
926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * @return Address of the data item if successful.
928 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
931
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
937 * extern struct k_fifo @a name;
938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500942 struct k_fifo name \
943 __in_section(_k_fifo, static, name) = \
944 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945
Allan Stephensc98da842016-11-11 15:45:03 -0500946/**
947 * @} end defgroup fifo_apis
948 */
949
950/**
951 * @cond INTERNAL_HIDDEN
952 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400953
954struct k_lifo {
955 _wait_q_t wait_q;
956 void *list;
957
958 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
959};
960
Allan Stephensc98da842016-11-11 15:45:03 -0500961#define K_LIFO_INITIALIZER(obj) \
962 { \
963 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
964 .list = NULL, \
965 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
966 }
967
968/**
969 * INTERNAL_HIDDEN @endcond
970 */
971
972/**
973 * @defgroup lifo_apis Lifo APIs
974 * @ingroup kernel_apis
975 * @{
976 */
977
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
985 * @return N/A
986 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400987extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988
989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * This routine adds a data item to @a lifo. A lifo data item must be
993 * aligned on a 4-byte boundary, and the first 32 bits of the item are
994 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * @note Can be called by ISRs.
997 *
998 * @param lifo Address of the lifo.
999 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
1001 * @return N/A
1002 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004
1005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * This routine removes a data item from @a lifo in a "last in, first out"
1009 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1012 *
1013 * @param lifo Address of the lifo.
1014 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 * or one of the special values K_NO_WAIT and K_FOREVER.
1016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @return Address of the data item if successful.
1018 * @retval NULL if returned without waiting or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1021
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 *
1027 * extern struct k_lifo @a name;
1028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001032 struct k_lifo name \
1033 __in_section(_k_lifo, static, name) = \
1034 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001035
Allan Stephensc98da842016-11-11 15:45:03 -05001036/**
1037 * @} end defgroup lifo_apis
1038 */
1039
1040/**
1041 * @cond INTERNAL_HIDDEN
1042 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001043
1044struct k_stack {
1045 _wait_q_t wait_q;
1046 uint32_t *base, *next, *top;
1047
1048 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
1049};
1050
Allan Stephensc98da842016-11-11 15:45:03 -05001051#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1052 { \
1053 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1054 .base = stack_buffer, \
1055 .next = stack_buffer, \
1056 .top = stack_buffer + stack_num_entries, \
1057 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1058 }
1059
1060/**
1061 * INTERNAL_HIDDEN @endcond
1062 */
1063
1064/**
1065 * @defgroup stack_apis Stack APIs
1066 * @ingroup kernel_apis
1067 * @{
1068 */
1069
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070/**
1071 * @brief Initialize a stack.
1072 *
1073 * This routine initializes a stack object, prior to its first use.
1074 *
1075 * @param stack Address of the stack.
1076 * @param buffer Address of array used to hold stacked values.
1077 * @param num_entries Maximum number of values that can be stacked.
1078 *
1079 * @return N/A
1080 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001081extern void k_stack_init(struct k_stack *stack,
1082 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083
1084/**
1085 * @brief Push an element onto a stack.
1086 *
1087 * This routine adds a 32-bit value @a data to @a stack.
1088 *
1089 * @note Can be called by ISRs.
1090 *
1091 * @param stack Address of the stack.
1092 * @param data Value to push onto the stack.
1093 *
1094 * @return N/A
1095 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097
1098/**
1099 * @brief Pop an element from a stack.
1100 *
1101 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1102 * manner and stores the value in @a data.
1103 *
1104 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1105 *
1106 * @param stack Address of the stack.
1107 * @param data Address of area to hold the value popped from the stack.
1108 * @param timeout Waiting period to obtain a value (in milliseconds),
1109 * or one of the special values K_NO_WAIT and K_FOREVER.
1110 *
1111 * @retval 0 if successful.
1112 * @retval -EBUSY if returned without waiting.
1113 * @retval -EAGAIN if waiting period timed out.
1114 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001115extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1116
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
1122 * extern struct k_stack @a name;
1123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * @param name Name of the stack.
1125 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001127#define K_STACK_DEFINE(name, stack_num_entries) \
1128 uint32_t __noinit \
1129 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001130 struct k_stack name \
1131 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001132 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1133 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001134
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135/**
Allan Stephensc98da842016-11-11 15:45:03 -05001136 * @} end defgroup stack_apis
1137 */
1138
1139/**
1140 * @defgroup workqueue_apis Workqueue Thread APIs
1141 * @ingroup kernel_apis
1142 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001143 */
1144
1145struct k_work;
1146
1147typedef void (*k_work_handler_t)(struct k_work *);
1148
1149/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001151 * queued to it. This is useful for drivers which need to schedule
1152 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001154 * space.
1155 */
1156struct k_work_q {
1157 struct k_fifo fifo;
1158};
1159
1160/**
1161 * @brief Work flags.
1162 */
1163enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001164 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165};
1166
1167/**
1168 * @brief An item which can be scheduled on a @ref k_work_q.
1169 */
1170struct k_work {
1171 void *_reserved; /* Used by k_fifo implementation. */
1172 k_work_handler_t handler;
1173 atomic_t flags[1];
1174};
1175
1176/**
1177 * @brief Statically initialize work item
1178 */
1179#define K_WORK_INITIALIZER(work_handler) \
1180 { \
1181 ._reserved = NULL, \
1182 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001183 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001184 }
1185
1186/**
1187 * @brief Dynamically initialize work item
1188 */
1189static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1190{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001191 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192 work->handler = handler;
1193}
1194
1195/**
1196 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001197 *
1198 * This procedure schedules a work item to be processed.
1199 * In the case where the work item has already been submitted and is pending
1200 * execution, calling this function will result in a no-op. In this case, the
1201 * work item must not be modified externally (e.g. by the caller of this
1202 * function), since that could cause the work item to be processed in a
1203 * corrupted state.
1204 *
1205 * @param work_q to schedule the work item
1206 * @param work work item
1207 *
1208 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209 */
1210static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1211 struct k_work *work)
1212{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001213 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001214 k_fifo_put(&work_q->fifo, work);
1215 }
1216}
1217
1218/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001219 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
1221 * @param work Work item to query
1222 *
1223 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001224 */
1225static inline int k_work_pending(struct k_work *work)
1226{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001227 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001228}
1229
1230/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231 * @brief Start a new workqueue.
1232 *
1233 * This routine must not be called from an ISR.
1234 *
1235 * @param work_q Pointer to Work queue
1236 * @param stack Pointer to work queue thread's stack
1237 * @param stack_size Size of the work queue thread's stack
1238 * @param prio Priority of the work queue's thread
1239 *
1240 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001241 */
Allan Stephens904cf972016-10-07 13:59:23 -05001242extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1243 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001244
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001245#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247/**
1248 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001249 */
1250struct k_delayed_work {
1251 struct k_work work;
1252 struct _timeout timeout;
1253 struct k_work_q *work_q;
1254};
1255
1256/**
1257 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258 *
1259 * Initialize a delayed work item.
1260 *
1261 * @param work Delayed work item
1262 * @param handler Routine invoked when processing delayed work item
1263 *
1264 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001265 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001266extern void k_delayed_work_init(struct k_delayed_work *work,
1267 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001268
1269/**
1270 * @brief Submit a delayed work item to a workqueue.
1271 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273 * Once the delay has passed, the work item is submitted to the work queue:
1274 * at this point, it is no longer possible to cancel it. Once the work item's
1275 * handler is about to be executed, the work is considered complete and can be
1276 * resubmitted.
1277 *
1278 * Care must be taken if the handler blocks or yield as there is no implicit
1279 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1280 * it should be explicitly done between the submitter and the handler.
1281 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001282 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001283 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001284 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001286 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001288extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1289 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001290 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291
1292/**
1293 * @brief Cancel a delayed work item
1294 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296 * or is idle, this will do nothing. The only case where this can fail is when
1297 * the work has been submitted to the work queue, but the handler has not run
1298 * yet.
1299 *
1300 * @param work Delayed work item to be canceled
1301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001302 * @return 0 in case of success, or negative value in case of error.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001304extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001305
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001306#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001308extern struct k_work_q k_sys_work_q;
1309
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001311 * @brief Submit a work item to the system workqueue.
1312 *
1313 * @ref k_work_submit_to_queue
1314 *
1315 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001316 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001317 * unexpected behavior.
1318 */
1319static inline void k_work_submit(struct k_work *work)
1320{
1321 k_work_submit_to_queue(&k_sys_work_q, work);
1322}
1323
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001324#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001325/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001326 * @brief Submit a delayed work item to the system workqueue.
1327 *
1328 * @ref k_delayed_work_submit_to_queue
1329 *
1330 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001331 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001332 * unexpected behavior.
1333 */
1334static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001335 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001336{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001337 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001338}
1339
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001340#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001341
1342/**
Allan Stephensc98da842016-11-11 15:45:03 -05001343 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344 */
1345
Allan Stephensc98da842016-11-11 15:45:03 -05001346/**
1347 * @cond INTERNAL_HIDDEN
1348 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001349
1350struct k_mutex {
1351 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001352 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001353 uint32_t lock_count;
1354 int owner_orig_prio;
1355#ifdef CONFIG_OBJECT_MONITOR
1356 int num_lock_state_changes;
1357 int num_conflicts;
1358#endif
1359
1360 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1361};
1362
1363#ifdef CONFIG_OBJECT_MONITOR
1364#define _MUTEX_INIT_OBJECT_MONITOR \
1365 .num_lock_state_changes = 0, .num_conflicts = 0,
1366#else
1367#define _MUTEX_INIT_OBJECT_MONITOR
1368#endif
1369
1370#define K_MUTEX_INITIALIZER(obj) \
1371 { \
1372 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1373 .owner = NULL, \
1374 .lock_count = 0, \
1375 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1376 _MUTEX_INIT_OBJECT_MONITOR \
1377 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1378 }
1379
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001380/**
Allan Stephensc98da842016-11-11 15:45:03 -05001381 * INTERNAL_HIDDEN @endcond
1382 */
1383
1384/**
1385 * @defgroup mutex_apis Mutex APIs
1386 * @ingroup kernel_apis
1387 * @{
1388 */
1389
1390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001394 *
1395 * extern struct k_mutex @a name;
1396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001400 struct k_mutex name \
1401 __in_section(_k_mutex, static, name) = \
1402 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001405 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 * Upon completion, the mutex is available and does not have an owner.
1410 *
1411 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412 *
1413 * @return N/A
1414 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001416
1417/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001418 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001420 * This routine locks @a mutex. If the mutex is locked by another thread,
1421 * the calling thread waits until the mutex becomes available or until
1422 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * A thread is permitted to lock a mutex it has already locked. The operation
1425 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001427 * @param mutex Address of the mutex.
1428 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429 * or one of the special values K_NO_WAIT and K_FOREVER.
1430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * @retval 0 if successful.
1432 * @retval -EBUSY if returned without waiting.
1433 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001434 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436
1437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001438 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001440 * This routine unlocks @a mutex. The mutex must already be locked by the
1441 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442 *
1443 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445 * thread.
1446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001447 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448 *
1449 * @return N/A
1450 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001451extern void k_mutex_unlock(struct k_mutex *mutex);
1452
Allan Stephensc98da842016-11-11 15:45:03 -05001453/**
1454 * @} end defgroup mutex_apis
1455 */
1456
1457/**
1458 * @cond INTERNAL_HIDDEN
1459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460
1461struct k_sem {
1462 _wait_q_t wait_q;
1463 unsigned int count;
1464 unsigned int limit;
1465
1466 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1467};
1468
Allan Stephensc98da842016-11-11 15:45:03 -05001469#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1470 { \
1471 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1472 .count = initial_count, \
1473 .limit = count_limit, \
1474 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1475 }
1476
1477/**
1478 * INTERNAL_HIDDEN @endcond
1479 */
1480
1481/**
1482 * @defgroup semaphore_apis Semaphore APIs
1483 * @ingroup kernel_apis
1484 * @{
1485 */
1486
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * @param sem Address of the semaphore.
1493 * @param initial_count Initial semaphore count.
1494 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001495 *
1496 * @return N/A
1497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001498extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1499 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001500
1501/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001502 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1507 *
1508 * @param sem Address of the semaphore.
1509 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001510 * or one of the special values K_NO_WAIT and K_FOREVER.
1511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * @retval 0 if successful.
1513 * @retval -EBUSY if returned without waiting.
1514 * @retval -EAGAIN if waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001515 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001516extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001517
1518/**
1519 * @brief Give a semaphore.
1520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001521 * This routine gives @a sem, unless the semaphore is already at its maximum
1522 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001527 *
1528 * @return N/A
1529 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530extern void k_sem_give(struct k_sem *sem);
1531
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001532/**
1533 * @brief Reset a semaphore's count to zero.
1534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001538 *
1539 * @return N/A
1540 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001541static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542{
1543 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544}
1545
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001546/**
1547 * @brief Get a semaphore's count.
1548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001551 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001553 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001554 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001555static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001556{
1557 return sem->count;
1558}
1559
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001561 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001563 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001564 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * @param name Name of the semaphore.
1568 * @param initial_count Initial semaphore count.
1569 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001570 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001571#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001572 struct k_sem name \
1573 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001574 K_SEM_INITIALIZER(name, initial_count, count_limit)
1575
Allan Stephensc98da842016-11-11 15:45:03 -05001576/**
1577 * @} end defgroup semaphore_apis
1578 */
1579
1580/**
1581 * @defgroup alert_apis Alert APIs
1582 * @ingroup kernel_apis
1583 * @{
1584 */
1585
1586typedef int (*k_alert_handler_t)(struct k_alert *);
1587
1588/**
1589 * @} end defgroup alert_apis
1590 */
1591
1592/**
1593 * @cond INTERNAL_HIDDEN
1594 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001595
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001596#define K_ALERT_DEFAULT NULL
1597#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001598
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001599struct k_alert {
1600 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601 atomic_t send_count;
1602 struct k_work work_item;
1603 struct k_sem sem;
1604
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001605 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001606};
1607
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001608extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001609
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001610#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001611 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001612 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001613 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001614 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001615 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001616 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1617 }
1618
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001619/**
Allan Stephensc98da842016-11-11 15:45:03 -05001620 * INTERNAL_HIDDEN @endcond
1621 */
1622
1623/**
1624 * @addtogroup alert_apis
1625 * @{
1626 */
1627
1628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * The alert is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001632 *
1633 * extern struct k_alert @a name;
1634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001635 * @param name Name of the alert.
1636 * @param alert_handler Action to take when alert is sent. Specify either
1637 * the address of a function to be invoked by the system workqueue
1638 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1639 * K_ALERT_DEFAULT (which causes the alert to pend).
1640 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001641 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001642#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001643 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001644 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001645 K_ALERT_INITIALIZER(name, alert_handler, \
1646 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @param alert Address of the alert.
1654 * @param handler Action to take when alert is sent. Specify either the address
1655 * of a function to be invoked by the system workqueue thread,
1656 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1657 * K_ALERT_DEFAULT (which causes the alert to pend).
1658 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001659 *
1660 * @return N/A
1661 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001662extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1663 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001664
1665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001666 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001670 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1671 *
1672 * @param alert Address of the alert.
1673 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001674 * or one of the special values K_NO_WAIT and K_FOREVER.
1675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001676 * @retval 0 if successful.
1677 * @retval -EBUSY if returned without waiting.
1678 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001679 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001680extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001681
1682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * This routine signals @a alert. The action specified for @a alert will
1686 * be taken, which may trigger the execution of an alert handler function
1687 * and/or cause the alert to pend (assuming the alert has not reached its
1688 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @note Can be called by ISRs.
1691 *
1692 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693 *
1694 * @return N/A
1695 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001696extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001697
1698/**
Allan Stephensc98da842016-11-11 15:45:03 -05001699 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700 */
1701
Allan Stephensc98da842016-11-11 15:45:03 -05001702/**
1703 * @cond INTERNAL_HIDDEN
1704 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001705
1706struct k_msgq {
1707 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001708 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001709 uint32_t max_msgs;
1710 char *buffer_start;
1711 char *buffer_end;
1712 char *read_ptr;
1713 char *write_ptr;
1714 uint32_t used_msgs;
1715
1716 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1717};
1718
Peter Mitsis1da807e2016-10-06 11:36:59 -04001719#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001720 { \
1721 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001722 .max_msgs = q_max_msgs, \
1723 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001725 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726 .read_ptr = q_buffer, \
1727 .write_ptr = q_buffer, \
1728 .used_msgs = 0, \
1729 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1730 }
1731
Peter Mitsis1da807e2016-10-06 11:36:59 -04001732/**
Allan Stephensc98da842016-11-11 15:45:03 -05001733 * INTERNAL_HIDDEN @endcond
1734 */
1735
1736/**
1737 * @defgroup msgq_apis Message Queue APIs
1738 * @ingroup kernel_apis
1739 * @{
1740 */
1741
1742/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001745 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1746 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001747 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1748 * message is similarly aligned to this boundary, @a q_msg_size must also be
1749 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * The message queue can be accessed outside the module where it is defined
1752 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 *
1754 * extern struct k_msgq @a name;
1755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001756 * @param q_name Name of the message queue.
1757 * @param q_msg_size Message size (in bytes).
1758 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001759 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001760 */
1761#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1762 static char __noinit __aligned(q_align) \
1763 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001764 struct k_msgq q_name \
1765 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001766 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1767 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768
Peter Mitsisd7a37502016-10-13 11:37:40 -04001769/**
1770 * @brief Initialize a message queue.
1771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * This routine initializes a message queue object, prior to its first use.
1773 *
Allan Stephensda827222016-11-09 14:23:58 -06001774 * The message queue's ring buffer must contain space for @a max_msgs messages,
1775 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1776 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1777 * that each message is similarly aligned to this boundary, @a q_msg_size
1778 * must also be a multiple of N.
1779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @param q Address of the message queue.
1781 * @param buffer Pointer to ring buffer that holds queued messages.
1782 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001783 * @param max_msgs Maximum number of messages that can be queued.
1784 *
1785 * @return N/A
1786 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001787extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001788 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001789
1790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001794 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001795 * @note Can be called by ISRs.
1796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * @param q Address of the message queue.
1798 * @param data Pointer to the message.
1799 * @param timeout Waiting period to add the message (in milliseconds),
1800 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * @retval 0 if successful.
1803 * @retval -ENOMSG if returned without waiting or after a queue purge.
1804 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001805 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001807
1808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001809 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001811 * This routine receives a message from message queue @a q in a "first in,
1812 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001813 *
Allan Stephensc98da842016-11-11 15:45:03 -05001814 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @param q Address of the message queue.
1817 * @param data Address of area to hold the received message.
1818 * @param timeout Waiting period to receive the message (in milliseconds),
1819 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @retval 0 if successful.
1822 * @retval -ENOMSG if returned without waiting.
1823 * @retval -EAGAIN if waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001826
1827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001830 * This routine discards all unreceived messages in a message queue's ring
1831 * buffer. Any threads that are blocked waiting to send a message to the
1832 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001834 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001835 *
1836 * @return N/A
1837 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838extern void k_msgq_purge(struct k_msgq *q);
1839
Peter Mitsis67be2492016-10-07 11:44:34 -04001840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001841 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04001842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843 * This routine returns the number of unused entries in a message queue's
1844 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04001845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001846 * @param q Address of the message queue.
1847 *
1848 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04001849 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001850static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001851{
1852 return q->max_msgs - q->used_msgs;
1853}
1854
Peter Mitsisd7a37502016-10-13 11:37:40 -04001855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * @param q Address of the message queue.
1861 *
1862 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001863 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001864static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001865{
1866 return q->used_msgs;
1867}
1868
Allan Stephensc98da842016-11-11 15:45:03 -05001869/**
1870 * @} end defgroup msgq_apis
1871 */
1872
1873/**
1874 * @defgroup mem_pool_apis Memory Pool APIs
1875 * @ingroup kernel_apis
1876 * @{
1877 */
1878
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001880 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001881 void *addr_in_pool;
1882 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001883 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884};
1885
Allan Stephensc98da842016-11-11 15:45:03 -05001886/**
1887 * @} end defgroup mem_pool_apis
1888 */
1889
1890/**
1891 * @defgroup mailbox_apis Mailbox APIs
1892 * @ingroup kernel_apis
1893 * @{
1894 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895
1896struct k_mbox_msg {
1897 /** internal use only - needed for legacy API support */
1898 uint32_t _mailbox;
1899 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001900 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901 /** application-defined information value */
1902 uint32_t info;
1903 /** sender's message data buffer */
1904 void *tx_data;
1905 /** internal use only - needed for legacy API support */
1906 void *_rx_data;
1907 /** message data block descriptor */
1908 struct k_mem_block tx_block;
1909 /** source thread id */
1910 k_tid_t rx_source_thread;
1911 /** target thread id */
1912 k_tid_t tx_target_thread;
1913 /** internal use only - thread waiting on send (may be a dummy) */
1914 k_tid_t _syncing_thread;
1915#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1916 /** internal use only - semaphore used during asynchronous send */
1917 struct k_sem *_async_sem;
1918#endif
1919};
1920
Allan Stephensc98da842016-11-11 15:45:03 -05001921/**
1922 * @cond INTERNAL_HIDDEN
1923 */
1924
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001925struct k_mbox {
1926 _wait_q_t tx_msg_queue;
1927 _wait_q_t rx_msg_queue;
1928
1929 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1930};
1931
1932#define K_MBOX_INITIALIZER(obj) \
1933 { \
1934 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1935 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1936 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1937 }
1938
Peter Mitsis12092702016-10-14 12:57:23 -04001939/**
Allan Stephensc98da842016-11-11 15:45:03 -05001940 * INTERNAL_HIDDEN @endcond
1941 */
1942
1943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 *
1948 * extern struct k_mbox @a name;
1949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001950 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001951 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001953 struct k_mbox name \
1954 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955 K_MBOX_INITIALIZER(name) \
1956
Peter Mitsis12092702016-10-14 12:57:23 -04001957/**
1958 * @brief Initialize a mailbox.
1959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001960 * This routine initializes a mailbox object, prior to its first use.
1961 *
1962 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04001963 *
1964 * @return N/A
1965 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966extern void k_mbox_init(struct k_mbox *mbox);
1967
Peter Mitsis12092702016-10-14 12:57:23 -04001968/**
1969 * @brief Send a mailbox message in a synchronous manner.
1970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * This routine sends a message to @a mbox and waits for a receiver to both
1972 * receive and process it. The message data may be in a buffer, in a memory
1973 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * @param mbox Address of the mailbox.
1976 * @param tx_msg Address of the transmit message descriptor.
1977 * @param timeout Waiting period for the message to be received (in
1978 * milliseconds), or one of the special values K_NO_WAIT
1979 * and K_FOREVER. Once the message has been received,
1980 * this routine waits as long as necessary for the message
1981 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04001982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @retval 0 if successful.
1984 * @retval -ENOMSG if returned without waiting.
1985 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04001986 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001987extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001988 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001989
Peter Mitsis12092702016-10-14 12:57:23 -04001990/**
1991 * @brief Send a mailbox message in an asynchronous manner.
1992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * This routine sends a message to @a mbox without waiting for a receiver
1994 * to process it. The message data may be in a buffer, in a memory pool block,
1995 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
1996 * will be given when the message has been both received and completely
1997 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04001998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001999 * @param mbox Address of the mailbox.
2000 * @param tx_msg Address of the transmit message descriptor.
2001 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002002 *
2003 * @return N/A
2004 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002005extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002006 struct k_sem *sem);
2007
Peter Mitsis12092702016-10-14 12:57:23 -04002008/**
2009 * @brief Receive a mailbox message.
2010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002011 * This routine receives a message from @a mbox, then optionally retrieves
2012 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002014 * @param mbox Address of the mailbox.
2015 * @param rx_msg Address of the receive message descriptor.
2016 * @param buffer Address of the buffer to receive data, or NULL to defer data
2017 * retrieval and message disposal until later.
2018 * @param timeout Waiting period for a message to be received (in
2019 * milliseconds), or one of the special values K_NO_WAIT
2020 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * @retval 0 if successful.
2023 * @retval -ENOMSG if returned without waiting.
2024 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002025 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002026extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002028
2029/**
2030 * @brief Retrieve mailbox message data into a buffer.
2031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * This routine completes the processing of a received message by retrieving
2033 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002034 *
2035 * Alternatively, this routine can be used to dispose of a received message
2036 * without retrieving its data.
2037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @param rx_msg Address of the receive message descriptor.
2039 * @param buffer Address of the buffer to receive data, or NULL to discard
2040 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002041 *
2042 * @return N/A
2043 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002044extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002045
2046/**
2047 * @brief Retrieve mailbox message data into a memory pool block.
2048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002049 * This routine completes the processing of a received message by retrieving
2050 * its data into a memory pool block, then disposing of the message.
2051 * The memory pool block that results from successful retrieval must be
2052 * returned to the pool once the data has been processed, even in cases
2053 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002054 *
2055 * Alternatively, this routine can be used to dispose of a received message
2056 * without retrieving its data. In this case there is no need to return a
2057 * memory pool block to the pool.
2058 *
2059 * This routine allocates a new memory pool block for the data only if the
2060 * data is not already in one. If a new block cannot be allocated, the routine
2061 * returns a failure code and the received message is left unchanged. This
2062 * permits the caller to reattempt data retrieval at a later time or to dispose
2063 * of the received message without retrieving its data.
2064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 * @param rx_msg Address of a receive message descriptor.
2066 * @param pool Address of memory pool, or NULL to discard data.
2067 * @param block Address of the area to hold memory pool block info.
2068 * @param timeout Waiting period to wait for a memory pool block (in
2069 * milliseconds), or one of the special values K_NO_WAIT
2070 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002072 * @retval 0 if successful.
2073 * @retval -ENOMEM if returned without waiting.
2074 * @retval -EAGAIN if waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002075 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002076extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002077 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078 struct k_mem_block *block, int32_t timeout);
2079
Allan Stephensc98da842016-11-11 15:45:03 -05002080/**
2081 * @} end defgroup mailbox_apis
2082 */
2083
2084/**
2085 * @cond INTERNAL_HIDDEN
2086 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087
2088struct k_pipe {
2089 unsigned char *buffer; /* Pipe buffer: may be NULL */
2090 size_t size; /* Buffer size */
2091 size_t bytes_used; /* # bytes used in buffer */
2092 size_t read_index; /* Where in buffer to read from */
2093 size_t write_index; /* Where in buffer to write */
2094
2095 struct {
2096 _wait_q_t readers; /* Reader wait queue */
2097 _wait_q_t writers; /* Writer wait queue */
2098 } wait_q;
2099
2100 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2101};
2102
Peter Mitsise5d9c582016-10-14 14:44:57 -04002103#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104 { \
2105 .buffer = pipe_buffer, \
2106 .size = pipe_buffer_size, \
2107 .bytes_used = 0, \
2108 .read_index = 0, \
2109 .write_index = 0, \
2110 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2111 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2112 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2113 }
2114
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115/**
Allan Stephensc98da842016-11-11 15:45:03 -05002116 * INTERNAL_HIDDEN @endcond
2117 */
2118
2119/**
2120 * @defgroup pipe_apis Pipe APIs
2121 * @ingroup kernel_apis
2122 * @{
2123 */
2124
2125/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002129 *
2130 * extern struct k_pipe @a name;
2131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @param name Name of the pipe.
2133 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2134 * or zero if no ring buffer is used.
2135 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002137#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2138 static unsigned char __noinit __aligned(pipe_align) \
2139 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002140 struct k_pipe name \
2141 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002142 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * This routine initializes a pipe object, prior to its first use.
2148 *
2149 * @param pipe Address of the pipe.
2150 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2151 * is used.
2152 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2153 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154 *
2155 * @return N/A
2156 */
2157extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2158 size_t size);
2159
2160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * @param pipe Address of the pipe.
2166 * @param data Address of data to write.
2167 * @param bytes_to_write Size of data (in bytes).
2168 * @param bytes_written Address of area to hold the number of bytes written.
2169 * @param min_xfer Minimum number of bytes to write.
2170 * @param timeout Waiting period to wait for the data to be written (in
2171 * milliseconds), or one of the special values K_NO_WAIT
2172 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @retval 0 if at least @a min_xfer data bytes were written.
2175 * @retval -EIO if returned without waiting; zero data bytes were written.
2176 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
2177 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002179extern int k_pipe_put(struct k_pipe *pipe, void *data,
2180 size_t bytes_to_write, size_t *bytes_written,
2181 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182
2183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002188 * @param pipe Address of the pipe.
2189 * @param data Address to place the data read from pipe.
2190 * @param bytes_to_read Maximum number of data bytes to read.
2191 * @param bytes_read Address of area to hold the number of bytes read.
2192 * @param min_xfer Minimum number of data bytes to read.
2193 * @param timeout Waiting period to wait for the data to be read (in
2194 * milliseconds), or one of the special values K_NO_WAIT
2195 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002197 * @retval 0 if at least @a min_xfer data bytes were read.
2198 * @retval -EIO if returned without waiting; zero data bytes were read.
2199 * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer
2200 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002201 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002202extern int k_pipe_get(struct k_pipe *pipe, void *data,
2203 size_t bytes_to_read, size_t *bytes_read,
2204 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205
2206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002207 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002209 * This routine writes the data contained in a memory block to @a pipe.
2210 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002213 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214 * @param block Memory block containing data to send
2215 * @param size Number of data bytes in memory block to send
2216 * @param sem Semaphore to signal upon completion (else NULL)
2217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219 */
2220extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2221 size_t size, struct k_sem *sem);
2222
2223/**
Allan Stephensc98da842016-11-11 15:45:03 -05002224 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225 */
2226
Allan Stephensc98da842016-11-11 15:45:03 -05002227/**
2228 * @cond INTERNAL_HIDDEN
2229 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002231struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002233 uint32_t num_blocks;
2234 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235 char *buffer;
2236 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002237 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002239 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240};
2241
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002242#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2243 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 { \
2245 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002246 .num_blocks = slab_num_blocks, \
2247 .block_size = slab_block_size, \
2248 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249 .free_list = NULL, \
2250 .num_used = 0, \
2251 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2252 }
2253
Peter Mitsis578f9112016-10-07 13:50:31 -04002254/**
Allan Stephensc98da842016-11-11 15:45:03 -05002255 * INTERNAL_HIDDEN @endcond
2256 */
2257
2258/**
2259 * @defgroup mem_slab_apis Memory Slab APIs
2260 * @ingroup kernel_apis
2261 * @{
2262 */
2263
2264/**
Allan Stephensda827222016-11-09 14:23:58 -06002265 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002266 *
Allan Stephensda827222016-11-09 14:23:58 -06002267 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002269 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2270 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002271 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002272 *
Allan Stephensda827222016-11-09 14:23:58 -06002273 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002274 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
2276 * extern struct k_mem_slab @a name;
2277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002278 * @param name Name of the memory slab.
2279 * @param slab_block_size Size of each memory block (in bytes).
2280 * @param slab_num_blocks Number memory blocks.
2281 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002282 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002283#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2284 char __noinit __aligned(slab_align) \
2285 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2286 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002287 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002288 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2289 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002291/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002292 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002295 *
Allan Stephensda827222016-11-09 14:23:58 -06002296 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2297 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2298 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2299 * To ensure that each memory block is similarly aligned to this boundary,
2300 * @a slab_block_size must also be a multiple of N.
2301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002302 * @param slab Address of the memory slab.
2303 * @param buffer Pointer to buffer used for the memory blocks.
2304 * @param block_size Size of each memory block (in bytes).
2305 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002306 *
2307 * @return N/A
2308 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002309extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002310 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002311
2312/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * @param slab Address of the memory slab.
2318 * @param mem Pointer to block address area.
2319 * @param timeout Maximum time to wait for operation to complete
2320 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2321 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @retval 0 if successful. The block address area pointed at by @a mem
2324 * is set to the starting address of the memory block.
2325 * @retval -ENOMEM if failed immediately.
2326 * @retval -EAGAIN if timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002327 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002328extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2329 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002330
2331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * This routine releases a previously allocated memory block back to its
2335 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * @param slab Address of the memory slab.
2338 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002339 *
2340 * @return N/A
2341 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002342extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002344/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * This routine gets the number of memory blocks that are currently
2348 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002353 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002354static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002356 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357}
2358
Peter Mitsisc001aa82016-10-13 13:53:37 -04002359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * This routine gets the number of memory blocks that are currently
2363 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002368 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002369static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002370{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002371 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002372}
2373
Allan Stephensc98da842016-11-11 15:45:03 -05002374/**
2375 * @} end defgroup mem_slab_apis
2376 */
2377
2378/**
2379 * @cond INTERNAL_HIDDEN
2380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002382/*
2383 * Memory pool requires a buffer and two arrays of structures for the
2384 * memory block accounting:
2385 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2386 * status of four blocks of memory.
2387 */
2388struct k_mem_pool_quad_block {
2389 char *mem_blocks; /* pointer to the first of four memory blocks */
2390 uint32_t mem_status; /* four bits. If bit is set, memory block is
2391 allocated */
2392};
2393/*
2394 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2395 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2396 * block size is 4 times less than the previous one and thus requires 4 times
2397 * bigger array of k_mem_pool_quad_block structures to keep track of the
2398 * memory blocks.
2399 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002400
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002401/*
2402 * The array of k_mem_pool_block_set keeps the information of each array of
2403 * k_mem_pool_quad_block structures
2404 */
2405struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002406 size_t block_size; /* memory block size */
2407 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002408 struct k_mem_pool_quad_block *quad_block;
2409 int count;
2410};
2411
2412/* Memory pool descriptor */
2413struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002414 size_t max_block_size;
2415 size_t min_block_size;
2416 uint32_t nr_of_maxblocks;
2417 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002418 struct k_mem_pool_block_set *block_set;
2419 char *bufblock;
2420 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2422};
2423
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002424#ifdef CONFIG_ARM
2425#define _SECTION_TYPE_SIGN "%"
2426#else
2427#define _SECTION_TYPE_SIGN "@"
2428#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002430/*
2431 * Static memory pool initialization
2432 */
Allan Stephensc98da842016-11-11 15:45:03 -05002433
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002434/*
2435 * Use .altmacro to be able to recalculate values and pass them as string
2436 * arguments when calling assembler macros resursively
2437 */
2438__asm__(".altmacro\n\t");
2439
2440/*
2441 * Recursively calls a macro
2442 * The followig global symbols need to be initialized:
2443 * __memory_pool_max_block_size - maximal size of the memory block
2444 * __memory_pool_min_block_size - minimal size of the memory block
2445 * Notes:
2446 * Global symbols are used due the fact that assembler macro allows only
2447 * one argument be passed with the % conversion
2448 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2449 * is used instead of / 4.
2450 * n_max argument needs to go first in the invoked macro, as some
2451 * assemblers concatenate \name and %(\n_max * 4) arguments
2452 * if \name goes first
2453 */
2454__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2455 ".ifge __memory_pool_max_block_size >> 2 -"
2456 " __memory_pool_min_block_size\n\t\t"
2457 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2458 "\\macro_name %(\\n_max * 4) \\name\n\t"
2459 ".endif\n\t"
2460 ".endm\n");
2461
2462/*
2463 * Build quad blocks
2464 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2465 * structures and recursively calls itself for the next array, 4 times
2466 * larger.
2467 * The followig global symbols need to be initialized:
2468 * __memory_pool_max_block_size - maximal size of the memory block
2469 * __memory_pool_min_block_size - minimal size of the memory block
2470 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2471 */
2472__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002473 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002474 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2475 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2476 ".if \\n_max % 4\n\t\t"
2477 ".skip __memory_pool_quad_block_size\n\t"
2478 ".endif\n\t"
2479 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2480 ".endm\n");
2481
2482/*
2483 * Build block sets and initialize them
2484 * Macro initializes the k_mem_pool_block_set structure and
2485 * recursively calls itself for the next one.
2486 * The followig global symbols need to be initialized:
2487 * __memory_pool_max_block_size - maximal size of the memory block
2488 * __memory_pool_min_block_size - minimal size of the memory block
2489 * __memory_pool_block_set_count, the number of the elements in the
2490 * block set array must be set to 0. Macro calculates it's real
2491 * value.
2492 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2493 * structures, _build_quad_blocks must be called prior it.
2494 */
2495__asm__(".macro _build_block_set n_max, name\n\t"
2496 ".int __memory_pool_max_block_size\n\t" /* block_size */
2497 ".if \\n_max % 4\n\t\t"
2498 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2499 ".else\n\t\t"
2500 ".int \\n_max >> 2\n\t"
2501 ".endif\n\t"
2502 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2503 ".int 0\n\t" /* count */
2504 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2505 "__do_recurse _build_block_set \\name \\n_max\n\t"
2506 ".endm\n");
2507
2508/*
2509 * Build a memory pool structure and initialize it
2510 * Macro uses __memory_pool_block_set_count global symbol,
2511 * block set addresses and buffer address, it may be called only after
2512 * _build_block_set
2513 */
2514__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002515 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002516 _SECTION_TYPE_SIGN "progbits\n\t"
2517 ".globl \\name\n\t"
2518 "\\name:\n\t"
2519 ".int \\max_size\n\t" /* max_block_size */
2520 ".int \\min_size\n\t" /* min_block_size */
2521 ".int \\n_max\n\t" /* nr_of_maxblocks */
2522 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2523 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2524 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2525 ".int 0\n\t" /* wait_q->head */
2526 ".int 0\n\t" /* wait_q->next */
2527 ".popsection\n\t"
2528 ".endm\n");
2529
2530#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2531 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2532 _SECTION_TYPE_SIGN "progbits\n\t"); \
2533 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2534 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2535 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2536 STRINGIFY(name) "\n\t"); \
2537 __asm__(".popsection\n\t")
2538
2539#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2540 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2541 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2542 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2543 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002544 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002545 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2546 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2547 STRINGIFY(name) "\n\t"); \
2548 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2549 __asm__(".int __memory_pool_block_set_count\n\t"); \
2550 __asm__(".popsection\n\t"); \
2551 extern uint32_t _mem_pool_block_set_count_##name; \
2552 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2553
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002554#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2555 char __noinit __aligned(align) \
2556 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002557
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002558/*
2559 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2560 * to __memory_pool_quad_block_size absolute symbol.
2561 * This function does not get called, but compiler calculates the value and
2562 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2563 */
2564static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2565{
2566 __asm__(".globl __memory_pool_quad_block_size\n\t"
2567#ifdef CONFIG_NIOS2
2568 "__memory_pool_quad_block_size = %0\n\t"
2569#else
2570 "__memory_pool_quad_block_size = %c0\n\t"
2571#endif
2572 :
2573 : "n"(sizeof(struct k_mem_pool_quad_block)));
2574}
2575
2576/**
Allan Stephensc98da842016-11-11 15:45:03 -05002577 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002578 */
2579
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002580/**
Allan Stephensc98da842016-11-11 15:45:03 -05002581 * @addtogroup mem_pool_apis
2582 * @{
2583 */
2584
2585/**
2586 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2589 * long. The memory pool allows blocks to be repeatedly partitioned into
2590 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2591 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002592 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002594 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002595 * If the pool is to be accessed outside the module where it is defined, it
2596 * can be declared via
2597 *
2598 * extern struct k_mem_pool @a name;
2599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * @param name Name of the memory pool.
2601 * @param min_size Size of the smallest blocks in the pool (in bytes).
2602 * @param max_size Size of the largest blocks in the pool (in bytes).
2603 * @param n_max Number of maximum sized blocks in the pool.
2604 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002605 */
2606#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002607 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2608 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002609 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002610 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2611 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2612 extern struct k_mem_pool name
2613
Peter Mitsis937042c2016-10-13 13:18:26 -04002614/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * @param pool Address of the memory pool.
2620 * @param block Pointer to block descriptor for the allocated memory.
2621 * @param size Amount of memory to allocate (in bytes).
2622 * @param timeout Maximum time to wait for operation to complete
2623 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2624 * or K_FOREVER to wait as long as necessary.
2625 *
2626 * @retval 0 if successful. The @a data field of the block descriptor
2627 * is set to the starting address of the memory block.
2628 * @retval -ENOMEM if unable to allocate a memory block.
2629 * @retval -EAGAIN if timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002630 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002631extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002632 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002633
2634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * This routine releases a previously allocated memory block back to its
2638 * memory pool.
2639 *
2640 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002641 *
2642 * @return N/A
2643 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002644extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002645
2646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * This routine instructs a memory pool to concatenate unused memory blocks
2650 * into larger blocks wherever possible. Manually defragmenting the memory
2651 * pool may speed up future allocations of memory blocks by eliminating the
2652 * need for the memory pool to perform an automatic partial defragmentation.
2653 *
2654 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002655 *
2656 * @return N/A
2657 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002658extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002659
2660/**
Allan Stephensc98da842016-11-11 15:45:03 -05002661 * @} end addtogroup mem_pool_apis
2662 */
2663
2664/**
2665 * @defgroup heap_apis Heap Memory Pool APIs
2666 * @ingroup kernel_apis
2667 * @{
2668 */
2669
2670/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002674 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002679 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002680extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002681
2682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002684 *
2685 * This routine provides traditional free() semantics. The memory being
2686 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002689 *
2690 * @return N/A
2691 */
2692extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693
Allan Stephensc98da842016-11-11 15:45:03 -05002694/**
2695 * @} end defgroup heap_apis
2696 */
2697
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698/*
2699 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2700 * hook into the device subsystem, which itself uses nanokernel semaphores,
2701 * and thus currently requires the definition of nano_sem.
2702 */
2703#include <legacy.h>
2704#include <arch/cpu.h>
2705
2706/*
2707 * private APIs that are utilized by one or more public APIs
2708 */
2709
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002712extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713
2714#ifdef __cplusplus
2715}
2716#endif
2717
Andrew Boiee004dec2016-11-07 09:01:19 -08002718#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2719/*
2720 * Define new and delete operators.
2721 * At this moment, the operators do nothing since objects are supposed
2722 * to be statically allocated.
2723 */
2724inline void operator delete(void *ptr)
2725{
2726 (void)ptr;
2727}
2728
2729inline void operator delete[](void *ptr)
2730{
2731 (void)ptr;
2732}
2733
2734inline void *operator new(size_t size)
2735{
2736 (void)size;
2737 return NULL;
2738}
2739
2740inline void *operator new[](size_t size)
2741{
2742 (void)size;
2743 return NULL;
2744}
2745
2746/* Placement versions of operator new and delete */
2747inline void operator delete(void *ptr1, void *ptr2)
2748{
2749 (void)ptr1;
2750 (void)ptr2;
2751}
2752
2753inline void operator delete[](void *ptr1, void *ptr2)
2754{
2755 (void)ptr1;
2756 (void)ptr2;
2757}
2758
2759inline void *operator new(size_t size, void *ptr)
2760{
2761 (void)size;
2762 return ptr;
2763}
2764
2765inline void *operator new[](size_t size, void *ptr)
2766{
2767 (void)size;
2768 return ptr;
2769}
2770
2771#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2772
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773#endif /* _kernel__h_ */