blob: 2e7801a92e2dadff6e6db14f71674c5f59714e2c [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
Anas Nashif61f4b242016-11-18 10:53:59 -050040#ifdef CONFIG_KERNEL_DEBUG
41#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040042#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
43#else
44#define K_DEBUG(fmt, ...)
45#endif
46
47#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
48#define K_PRIO_PREEMPT(x) (x)
49
Benjamin Walsh456c6da2016-09-02 18:55:39 -040050#define K_ANY NULL
51#define K_END NULL
52
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#if CONFIG_NUM_COOP_PRIORITIES > 0
54#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
55#else
56#define K_HIGHEST_THREAD_PRIO 0
57#endif
58
59#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
60#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
61#else
62#define K_LOWEST_THREAD_PRIO -1
63#endif
64
Benjamin Walshfab8d922016-11-08 15:36:36 -050065#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
68#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
69
70typedef sys_dlist_t _wait_q_t;
71
72#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
73#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
74#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
75#else
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
77#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
78#endif
79
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050080#define tcs k_thread
81struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082struct k_mutex;
83struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040084struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040091struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040092struct k_mem_pool;
93struct k_timer;
94
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040095typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097enum execution_context_types {
98 K_ISR = 0,
99 K_COOP_THREAD,
100 K_PREEMPT_THREAD,
101};
102
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400103/**
Allan Stephensc98da842016-11-11 15:45:03 -0500104 * @defgroup thread_apis Thread APIs
105 * @ingroup kernel_apis
106 * @{
107 */
108
109/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500110 * @typedef k_thread_entry_t
111 * @brief Thread entry point function type.
112 *
113 * A thread's entry point function is invoked when the thread starts executing.
114 * Up to 3 argument values can be passed to the function.
115 *
116 * The thread terminates execution permanently if the entry point function
117 * returns. The thread is responsible for releasing any shared resources
118 * it may own (such as mutexes and dynamically allocated memory), prior to
119 * returning.
120 *
121 * @param p1 First argument.
122 * @param p2 Second argument.
123 * @param p3 Third argument.
124 *
125 * @return N/A
126 */
127typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
128
129/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500130 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500132 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500134 * The new thread may be scheduled for immediate execution or a delayed start.
135 * If the newly spawned thread does not have a delayed start the kernel
136 * scheduler may preempt the current thread to allow the new thread to
137 * execute.
138 *
139 * Thread options are architecture-specific, and can include K_ESSENTIAL,
140 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
141 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400142 *
143 * @param stack Pointer to the stack space.
144 * @param stack_size Stack size in bytes.
145 * @param entry Thread entry function.
146 * @param p1 1st entry point parameter.
147 * @param p2 2nd entry point parameter.
148 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500149 * @param prio Thread priority.
150 * @param options Thread options.
151 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500153 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500155extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500156 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500158 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500161 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400162 *
Allan Stephensc98da842016-11-11 15:45:03 -0500163 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500164 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
168 * @return N/A
169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400170extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400171
172/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500173 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400174 *
175 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500176 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400177 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 * @return N/A
179 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400180extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400181
182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500183 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500185 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400186 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500187 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 *
189 * @return N/A
190 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400191extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400192
193/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500194 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * If @a thread is not currently sleeping, the routine has no effect.
199 *
200 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 *
202 * @return N/A
203 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400205
206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500207 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500209 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400211extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212
213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500214 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500216 * This routine prevents @a thread from executing if it has not yet started
217 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @param thread ID of thread to cancel.
220 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500221 * @retval 0 Thread spawning cancelled.
222 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400224extern int k_thread_cancel(k_tid_t thread);
225
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400226/**
Allan Stephensc98da842016-11-11 15:45:03 -0500227 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * This routine permanently stops execution of @a thread. The thread is taken
230 * off all kernel queues it is part of (i.e. the ready queue, the timeout
231 * queue, or a kernel object wait queue). However, any kernel resources the
232 * thread might currently own (such as mutexes or memory blocks) are not
233 * released. It is the responsibility of the caller of this routine to ensure
234 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500236 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400237 *
238 * @return N/A
239 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400240extern void k_thread_abort(k_tid_t thread);
241
Allan Stephensc98da842016-11-11 15:45:03 -0500242/**
243 * @cond INTERNAL_HIDDEN
244 */
245
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400246#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400247#define _THREAD_TIMEOUT_INIT(obj) \
248 (obj).nano_timeout = { \
249 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400250 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400251 .wait_q = NULL, \
252 .delta_ticks_from_prev = -1, \
253 },
254#else
255#define _THREAD_TIMEOUT_INIT(obj)
256#endif
257
258#ifdef CONFIG_ERRNO
259#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
260#else
261#define _THREAD_ERRNO_INIT(obj)
262#endif
263
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400264struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400265 union {
266 char *init_stack;
267 struct k_thread *thread;
268 };
269 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500270 void (*init_entry)(void *, void *, void *);
271 void *init_p1;
272 void *init_p2;
273 void *init_p3;
274 int init_prio;
275 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400276 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500277 void (*init_abort)(void);
278 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400279};
280
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400281#define _THREAD_INITIALIZER(stack, stack_size, \
282 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500283 prio, options, delay, abort, groups) \
284 { \
285 .init_stack = (stack), \
286 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400287 .init_entry = (void (*)(void *, void *, void *))entry, \
288 .init_p1 = (void *)p1, \
289 .init_p2 = (void *)p2, \
290 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500291 .init_prio = (prio), \
292 .init_options = (options), \
293 .init_delay = (delay), \
294 .init_abort = (abort), \
295 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400296 }
297
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400298/**
Allan Stephensc98da842016-11-11 15:45:03 -0500299 * INTERNAL_HIDDEN @endcond
300 */
301
302/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500303 * @brief Statically define and initialize a thread.
304 *
305 * The thread may be scheduled for immediate execution or a delayed start.
306 *
307 * Thread options are architecture-specific, and can include K_ESSENTIAL,
308 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
309 * them using "|" (the logical OR operator).
310 *
311 * The ID of the thread can be accessed using:
312 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500313 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500314 *
315 * @param name Name of the thread.
316 * @param stack_size Stack size in bytes.
317 * @param entry Thread entry function.
318 * @param p1 1st entry point parameter.
319 * @param p2 2nd entry point parameter.
320 * @param p3 3rd entry point parameter.
321 * @param prio Thread priority.
322 * @param options Thread options.
323 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400324 *
325 * @internal It has been observed that the x86 compiler by default aligns
326 * these _static_thread_data structures to 32-byte boundaries, thereby
327 * wasting space. To work around this, force a 4-byte alignment.
328 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500329#define K_THREAD_DEFINE(name, stack_size, \
330 entry, p1, p2, p3, \
331 prio, options, delay) \
332 char __noinit __stack _k_thread_obj_##name[stack_size]; \
333 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500334 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500335 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
336 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500337 NULL, 0); \
338 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400339
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500341 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500343 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500345 * @param thread ID of thread whose priority is needed.
346 *
347 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400348 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500349extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400350
351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500352 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500354 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355 *
356 * Rescheduling can occur immediately depending on the priority @a thread is
357 * set to:
358 *
359 * - If its priority is raised above the priority of the caller of this
360 * function, and the caller is preemptible, @a thread will be scheduled in.
361 *
362 * - If the caller operates on itself, it lowers its priority below that of
363 * other threads in the system, and the caller is preemptible, the thread of
364 * highest priority will be scheduled in.
365 *
366 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
367 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
368 * highest priority.
369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500370 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400371 * @param prio New priority.
372 *
373 * @warning Changing the priority of a thread currently involved in mutex
374 * priority inheritance may result in undefined behavior.
375 *
376 * @return N/A
377 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378extern void k_thread_priority_set(k_tid_t thread, int prio);
379
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500383 * This routine prevents the kernel scheduler from making @a thread the
384 * current thread. All other internal operations on @a thread are still
385 * performed; for example, any timeout it is waiting on keeps ticking,
386 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * If @a thread is already suspended, the routine has no effect.
389 *
390 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400391 *
392 * @return N/A
393 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400394extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400395
396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500399 * This routine allows the kernel scheduler to make @a thread the current
400 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * If @a thread is not currently suspended, the routine has no effect.
403 *
404 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 *
406 * @return N/A
407 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400408extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400409
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500411 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500413 * This routine specifies how the scheduler will perform time slicing of
414 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500416 * To enable time slicing, @a slice must be non-zero. The scheduler
417 * ensures that no thread runs for more than the specified time limit
418 * before other threads of that priority are given a chance to execute.
419 * Any thread whose priority is higher than @a prio is exempted, and may
420 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500422 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 * execute. Once the scheduler selects a thread for execution, there is no
424 * minimum guaranteed time the thread will execute before threads of greater or
425 * equal priority are scheduled.
426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500427 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400428 * for execution, this routine has no effect; the thread is immediately
429 * rescheduled after the slice period expires.
430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * To disable timeslicing, set both @a slice and @a prio to zero.
432 *
433 * @param slice Maximum time slice length (in milliseconds).
434 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
436 * @return N/A
437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400438extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400439
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400440/**
Allan Stephensc98da842016-11-11 15:45:03 -0500441 * @} end defgroup thread_apis
442 */
443
444/**
445 * @addtogroup isr_apis
446 * @{
447 */
448
449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500450 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
Allan Stephensc98da842016-11-11 15:45:03 -0500452 * This routine allows the caller to customize its actions, depending on
453 * whether it is a thread or an ISR.
454 *
455 * @note Can be called by ISRs.
456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @return 0 if invoked by a thread.
458 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500460extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461
Benjamin Walsh445830d2016-11-10 15:54:27 -0500462/**
463 * @brief Determine if code is running in a preemptible thread.
464 *
Allan Stephensc98da842016-11-11 15:45:03 -0500465 * This routine allows the caller to customize its actions, depending on
466 * whether it can be preempted by another thread. The routine returns a 'true'
467 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500468 *
Allan Stephensc98da842016-11-11 15:45:03 -0500469 * - The code is running in a thread, not at ISR.
470 * - The thread's priority is in the preemptible range.
471 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500472 *
Allan Stephensc98da842016-11-11 15:45:03 -0500473 * @note Can be called by ISRs.
474 *
475 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500476 * @return Non-zero if invoked by a preemptible thread.
477 */
478extern int k_is_preempt_thread(void);
479
Allan Stephensc98da842016-11-11 15:45:03 -0500480/**
481 * @} end addtogroup isr_apis
482 */
483
484/**
485 * @addtogroup thread_apis
486 * @{
487 */
488
489/**
490 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500491 *
Allan Stephensc98da842016-11-11 15:45:03 -0500492 * This routine prevents the current thread from being preempted by another
493 * thread by instructing the scheduler to treat it as a cooperative thread.
494 * If the thread subsequently performs an operation that makes it unready,
495 * it will be context switched out in the normal manner. When the thread
496 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500497 *
Allan Stephensc98da842016-11-11 15:45:03 -0500498 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500499 *
Allan Stephensc98da842016-11-11 15:45:03 -0500500 * @note k_sched_lock() and k_sched_unlock() should normally be used
501 * when the operation being performed can be safely interrupted by ISRs.
502 * However, if the amount of processing involved is very small, better
503 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500504 *
505 * @return N/A
506 */
507extern void k_sched_lock(void);
508
Allan Stephensc98da842016-11-11 15:45:03 -0500509/**
510 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500511 *
Allan Stephensc98da842016-11-11 15:45:03 -0500512 * This routine reverses the effect of a previous call to k_sched_lock().
513 * A thread must call the routine once for each time it called k_sched_lock()
514 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500515 *
516 * @return N/A
517 */
518extern void k_sched_unlock(void);
519
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500521 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500523 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500525 * Custom data is not used by the kernel itself, and is freely available
526 * for a thread to use as it sees fit. It can be used as a framework
527 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
531 * @return N/A
532 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534
535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500536 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500538 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500540 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400542extern void *k_thread_custom_data_get(void);
543
544/**
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * @} end addtogroup thread_apis
546 */
547
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400548#include <sys_clock.h>
549
Allan Stephensc2f15a42016-11-17 12:24:22 -0500550/**
551 * @addtogroup clock_apis
552 * @{
553 */
554
555/**
556 * @brief Generate null timeout delay.
557 *
558 * This macro generates a timeout delay that that instructs a kernel API
559 * not to wait if the requested operation cannot be performed immediately.
560 *
561 * @return Timeout delay value.
562 */
563#define K_NO_WAIT 0
564
565/**
566 * @brief Generate timeout delay from milliseconds.
567 *
568 * This macro generates a timeout delay that that instructs a kernel API
569 * to wait up to @a ms milliseconds to perform the requested operation.
570 *
571 * @param ms Duration in milliseconds.
572 *
573 * @return Timeout delay value.
574 */
Johan Hedberg14471692016-11-13 10:52:15 +0200575#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500576
577/**
578 * @brief Generate timeout delay from seconds.
579 *
580 * This macro generates a timeout delay that that instructs a kernel API
581 * to wait up to @a s seconds to perform the requested operation.
582 *
583 * @param s Duration in seconds.
584 *
585 * @return Timeout delay value.
586 */
Johan Hedberg14471692016-11-13 10:52:15 +0200587#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500588
589/**
590 * @brief Generate timeout delay from minutes.
591 *
592 * This macro generates a timeout delay that that instructs a kernel API
593 * to wait up to @a m minutes to perform the requested operation.
594 *
595 * @param m Duration in minutes.
596 *
597 * @return Timeout delay value.
598 */
Johan Hedberg14471692016-11-13 10:52:15 +0200599#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500600
601/**
602 * @brief Generate timeout delay from hours.
603 *
604 * This macro generates a timeout delay that that instructs a kernel API
605 * to wait up to @a h hours to perform the requested operation.
606 *
607 * @param h Duration in hours.
608 *
609 * @return Timeout delay value.
610 */
Johan Hedberg14471692016-11-13 10:52:15 +0200611#define K_HOURS(h) K_MINUTES((h) * 60)
612
Allan Stephensc98da842016-11-11 15:45:03 -0500613/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500614 * @brief Generate infinite timeout delay.
615 *
616 * This macro generates a timeout delay that that instructs a kernel API
617 * to wait as long as necessary to perform the requested operation.
618 *
619 * @return Timeout delay value.
620 */
621#define K_FOREVER (-1)
622
623/**
624 * @} end addtogroup clock_apis
625 */
626
627/**
Allan Stephensc98da842016-11-11 15:45:03 -0500628 * @cond INTERNAL_HIDDEN
629 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400630
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500631/* added tick needed to account for tick in progress */
632#define _TICK_ALIGN 1
633
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400634static int64_t __ticks_to_ms(int64_t ticks)
635{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400636#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400637 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400638#else
639 __ASSERT(ticks == 0, "");
640 return 0;
641#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400642}
643
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400644/* timeouts */
645
646struct _timeout;
647typedef void (*_timeout_func_t)(struct _timeout *t);
648
649struct _timeout {
650 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400651 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400652 sys_dlist_t *wait_q;
653 int32_t delta_ticks_from_prev;
654 _timeout_func_t func;
655};
656
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200657extern int32_t _timeout_remaining_get(struct _timeout *timeout);
658
Allan Stephensc98da842016-11-11 15:45:03 -0500659/**
660 * INTERNAL_HIDDEN @endcond
661 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500662
Allan Stephensc98da842016-11-11 15:45:03 -0500663/**
664 * @cond INTERNAL_HIDDEN
665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400666
667struct k_timer {
668 /*
669 * _timeout structure must be first here if we want to use
670 * dynamic timer allocation. timeout.node is used in the double-linked
671 * list of free timers
672 */
673 struct _timeout timeout;
674
Allan Stephens45bfa372016-10-12 12:39:42 -0500675 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400676 _wait_q_t wait_q;
677
678 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500679 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400680
681 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500682 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683
684 /* timer period */
685 int32_t period;
686
Allan Stephens45bfa372016-10-12 12:39:42 -0500687 /* timer status */
688 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400689
Allan Stephens45bfa372016-10-12 12:39:42 -0500690 /* used to support legacy timer APIs */
691 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400692
693 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
694};
695
Allan Stephens1342adb2016-11-03 13:54:53 -0500696#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400697 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500698 .timeout.delta_ticks_from_prev = -1, \
699 .timeout.wait_q = NULL, \
700 .timeout.thread = NULL, \
701 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400702 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500703 .expiry_fn = expiry, \
704 .stop_fn = stop, \
705 .status = 0, \
706 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400707 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
708 }
709
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710/**
Allan Stephensc98da842016-11-11 15:45:03 -0500711 * INTERNAL_HIDDEN @endcond
712 */
713
714/**
715 * @defgroup timer_apis Timer APIs
716 * @ingroup kernel_apis
717 * @{
718 */
719
720/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500721 * @typedef k_timer_expiry_t
722 * @brief Timer expiry function type.
723 *
724 * A timer's expiry function is executed by the system clock interrupt handler
725 * each time the timer expires. The expiry function is optional, and is only
726 * invoked if the timer has been initialized with one.
727 *
728 * @param timer Address of timer.
729 *
730 * @return N/A
731 */
732typedef void (*k_timer_expiry_t)(struct k_timer *timer);
733
734/**
735 * @typedef k_timer_stop_t
736 * @brief Timer stop function type.
737 *
738 * A timer's stop function is executed if the timer is stopped prematurely.
739 * The function runs in the context of the thread that stops the timer.
740 * The stop function is optional, and is only invoked if the timer has been
741 * initialized with one.
742 *
743 * @param timer Address of timer.
744 *
745 * @return N/A
746 */
747typedef void (*k_timer_stop_t)(struct k_timer *timer);
748
749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400753 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500754 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 *
756 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * @param expiry_fn Function to invoke each time the timer expires.
758 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500760#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500761 struct k_timer name \
762 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500763 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764
Allan Stephens45bfa372016-10-12 12:39:42 -0500765/**
766 * @brief Initialize a timer.
767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500769 *
770 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * @param expiry_fn Function to invoke each time the timer expires.
772 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500773 *
774 * @return N/A
775 */
776extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500777 k_timer_expiry_t expiry_fn,
778 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700779
Allan Stephens45bfa372016-10-12 12:39:42 -0500780/**
781 * @brief Start a timer.
782 *
783 * This routine starts a timer, and resets its status to zero. The timer
784 * begins counting down using the specified duration and period values.
785 *
786 * Attempting to start a timer that is already running is permitted.
787 * The timer's status is reset to zero and the timer begins counting down
788 * using the new duration and period values.
789 *
790 * @param timer Address of timer.
791 * @param duration Initial timer duration (in milliseconds).
792 * @param period Timer period (in milliseconds).
793 *
794 * @return N/A
795 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400796extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500797 int32_t duration, int32_t period);
798
799/**
800 * @brief Stop a timer.
801 *
802 * This routine stops a running timer prematurely. The timer's stop function,
803 * if one exists, is invoked by the caller.
804 *
805 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500807 *
808 * @param timer Address of timer.
809 *
810 * @return N/A
811 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400812extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500813
814/**
815 * @brief Read timer status.
816 *
817 * This routine reads the timer's status, which indicates the number of times
818 * it has expired since its status was last read.
819 *
820 * Calling this routine resets the timer's status to zero.
821 *
822 * @param timer Address of timer.
823 *
824 * @return Timer status.
825 */
826extern uint32_t k_timer_status_get(struct k_timer *timer);
827
828/**
829 * @brief Synchronize thread to timer expiration.
830 *
831 * This routine blocks the calling thread until the timer's status is non-zero
832 * (indicating that it has expired at least once since it was last examined)
833 * or the timer is stopped. If the timer status is already non-zero,
834 * or the timer is already stopped, the caller continues without waiting.
835 *
836 * Calling this routine resets the timer's status to zero.
837 *
838 * This routine must not be used by interrupt handlers, since they are not
839 * allowed to block.
840 *
841 * @param timer Address of timer.
842 *
843 * @return Timer status.
844 */
845extern uint32_t k_timer_status_sync(struct k_timer *timer);
846
847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500849 *
850 * This routine computes the (approximate) time remaining before a running
851 * timer next expires. If the timer is not running, it returns zero.
852 *
853 * @param timer Address of timer.
854 *
855 * @return Remaining time (in milliseconds).
856 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200857static inline int32_t k_timer_remaining_get(struct k_timer *timer)
858{
859 return _timeout_remaining_get(&timer->timeout);
860}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400861
Allan Stephensc98da842016-11-11 15:45:03 -0500862/**
863 * @} end defgroup timer_apis
864 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400865
Allan Stephensc98da842016-11-11 15:45:03 -0500866/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500867 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500868 * @{
869 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500870
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400871/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * This routine returns the elapsed time since the system booted,
875 * in milliseconds.
876 *
877 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400878 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400880
881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * This routine returns the lower 32-bits of the elapsed time since the system
885 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * This routine can be more efficient than k_uptime_get(), as it reduces the
888 * need for interrupt locking and 64-bit math. However, the 32-bit result
889 * cannot hold a system uptime time larger than approximately 50 days, so the
890 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400893 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400894extern uint32_t k_uptime_get_32(void);
895
896/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * This routine computes the elapsed time between the current system uptime
900 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500902 * @param reftime Pointer to a reference time, which is updated to the current
903 * uptime upon return.
904 *
905 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400906 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400908
909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500910 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500912 * This routine computes the elapsed time between the current system uptime
913 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * This routine can be more efficient than k_uptime_delta(), as it reduces the
916 * need for interrupt locking and 64-bit math. However, the 32-bit result
917 * cannot hold an elapsed time larger than approximately 50 days, so the
918 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * @param reftime Pointer to a reference time, which is updated to the current
921 * uptime upon return.
922 *
923 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400924 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400925extern uint32_t k_uptime_delta_32(int64_t *reftime);
926
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * This routine returns the current time, as measured by the system's hardware
931 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400935extern uint32_t k_cycle_get_32(void);
936
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400937/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500938 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400939 */
940
Allan Stephensc98da842016-11-11 15:45:03 -0500941/**
942 * @cond INTERNAL_HIDDEN
943 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400944
945struct k_fifo {
946 _wait_q_t wait_q;
947 sys_slist_t data_q;
948
949 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
950};
951
Allan Stephensc98da842016-11-11 15:45:03 -0500952#define K_FIFO_INITIALIZER(obj) \
953 { \
954 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
955 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
956 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
957 }
958
959/**
960 * INTERNAL_HIDDEN @endcond
961 */
962
963/**
964 * @defgroup fifo_apis Fifo APIs
965 * @ingroup kernel_apis
966 * @{
967 */
968
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500970 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 *
976 * @return N/A
977 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400978extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979
980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * This routine adds a data item to @a fifo. A fifo data item must be
984 * aligned on a 4-byte boundary, and the first 32 bits of the item are
985 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * @note Can be called by ISRs.
988 *
989 * @param fifo Address of the fifo.
990 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 *
992 * @return N/A
993 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400994extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995
996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * This routine adds a list of data items to @a fifo in one operation.
1000 * The data items must be in a singly-linked list, with the first 32 bits
1001 * each data item pointing to the next data item; the list must be
1002 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @param fifo Address of the fifo.
1007 * @param head Pointer to first node in singly-linked list.
1008 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
1010 * @return N/A
1011 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001012extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013
1014/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * This routine adds a list of data items to @a fifo in one operation.
1018 * The data items must be in a singly-linked list implemented using a
1019 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 * and must be re-initialized via sys_slist_init().
1021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * @note Can be called by ISRs.
1023 *
1024 * @param fifo Address of the fifo.
1025 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 *
1027 * @return N/A
1028 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030
1031/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * This routine removes a data item from @a fifo in a "first in, first out"
1035 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1038 *
1039 * @param fifo Address of the fifo.
1040 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001041 * or one of the special values K_NO_WAIT and K_FOREVER.
1042 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001043 * @return Address of the data item if successful; NULL if returned
1044 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001046extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1047
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001051 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001053 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001056 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001057#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001058 struct k_fifo name \
1059 __in_section(_k_fifo, static, name) = \
1060 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001061
Allan Stephensc98da842016-11-11 15:45:03 -05001062/**
1063 * @} end defgroup fifo_apis
1064 */
1065
1066/**
1067 * @cond INTERNAL_HIDDEN
1068 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069
1070struct k_lifo {
1071 _wait_q_t wait_q;
1072 void *list;
1073
1074 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
1075};
1076
Allan Stephensc98da842016-11-11 15:45:03 -05001077#define K_LIFO_INITIALIZER(obj) \
1078 { \
1079 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1080 .list = NULL, \
1081 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1082 }
1083
1084/**
1085 * INTERNAL_HIDDEN @endcond
1086 */
1087
1088/**
1089 * @defgroup lifo_apis Lifo APIs
1090 * @ingroup kernel_apis
1091 * @{
1092 */
1093
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001095 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
1101 * @return N/A
1102 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001103extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104
1105/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * This routine adds a data item to @a lifo. A lifo data item must be
1109 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1110 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * @note Can be called by ISRs.
1113 *
1114 * @param lifo Address of the lifo.
1115 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
1117 * @return N/A
1118 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120
1121/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * This routine removes a data item from @a lifo in a "last in, first out"
1125 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1128 *
1129 * @param lifo Address of the lifo.
1130 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 * or one of the special values K_NO_WAIT and K_FOREVER.
1132 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001133 * @return Address of the data item if successful; NULL if returned
1134 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001136extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1137
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001141 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001143 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001148 struct k_lifo name \
1149 __in_section(_k_lifo, static, name) = \
1150 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001151
Allan Stephensc98da842016-11-11 15:45:03 -05001152/**
1153 * @} end defgroup lifo_apis
1154 */
1155
1156/**
1157 * @cond INTERNAL_HIDDEN
1158 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159
1160struct k_stack {
1161 _wait_q_t wait_q;
1162 uint32_t *base, *next, *top;
1163
1164 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
1165};
1166
Allan Stephensc98da842016-11-11 15:45:03 -05001167#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1168 { \
1169 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1170 .base = stack_buffer, \
1171 .next = stack_buffer, \
1172 .top = stack_buffer + stack_num_entries, \
1173 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1174 }
1175
1176/**
1177 * INTERNAL_HIDDEN @endcond
1178 */
1179
1180/**
1181 * @defgroup stack_apis Stack APIs
1182 * @ingroup kernel_apis
1183 * @{
1184 */
1185
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186/**
1187 * @brief Initialize a stack.
1188 *
1189 * This routine initializes a stack object, prior to its first use.
1190 *
1191 * @param stack Address of the stack.
1192 * @param buffer Address of array used to hold stacked values.
1193 * @param num_entries Maximum number of values that can be stacked.
1194 *
1195 * @return N/A
1196 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001197extern void k_stack_init(struct k_stack *stack,
1198 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001199
1200/**
1201 * @brief Push an element onto a stack.
1202 *
1203 * This routine adds a 32-bit value @a data to @a stack.
1204 *
1205 * @note Can be called by ISRs.
1206 *
1207 * @param stack Address of the stack.
1208 * @param data Value to push onto the stack.
1209 *
1210 * @return N/A
1211 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001212extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001213
1214/**
1215 * @brief Pop an element from a stack.
1216 *
1217 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1218 * manner and stores the value in @a data.
1219 *
1220 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1221 *
1222 * @param stack Address of the stack.
1223 * @param data Address of area to hold the value popped from the stack.
1224 * @param timeout Waiting period to obtain a value (in milliseconds),
1225 * or one of the special values K_NO_WAIT and K_FOREVER.
1226 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001227 * @retval 0 Element popped from stack.
1228 * @retval -EBUSY Returned without waiting.
1229 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001230 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001231extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1232
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001238 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * @param name Name of the stack.
1241 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001243#define K_STACK_DEFINE(name, stack_num_entries) \
1244 uint32_t __noinit \
1245 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001246 struct k_stack name \
1247 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001248 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1249 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001250
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001251/**
Allan Stephensc98da842016-11-11 15:45:03 -05001252 * @} end defgroup stack_apis
1253 */
1254
Allan Stephens6bba9b02016-11-16 14:56:54 -05001255struct k_work;
1256
Allan Stephensc98da842016-11-11 15:45:03 -05001257/**
1258 * @defgroup workqueue_apis Workqueue Thread APIs
1259 * @ingroup kernel_apis
1260 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001261 */
1262
Allan Stephens6bba9b02016-11-16 14:56:54 -05001263/**
1264 * @typedef k_work_handler_t
1265 * @brief Work item handler function type.
1266 *
1267 * A work item's handler function is executed by a workqueue's thread
1268 * when the work item is processed by the workqueue.
1269 *
1270 * @param work Address of the work item.
1271 *
1272 * @return N/A
1273 */
1274typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275
1276/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001277 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001279
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001280struct k_work_q {
1281 struct k_fifo fifo;
1282};
1283
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001285 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286};
1287
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001288struct k_work {
1289 void *_reserved; /* Used by k_fifo implementation. */
1290 k_work_handler_t handler;
1291 atomic_t flags[1];
1292};
1293
Allan Stephens6bba9b02016-11-16 14:56:54 -05001294struct k_delayed_work {
1295 struct k_work work;
1296 struct _timeout timeout;
1297 struct k_work_q *work_q;
1298};
1299
1300extern struct k_work_q k_sys_work_q;
1301
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001303 * INTERNAL_HIDDEN @endcond
1304 */
1305
1306/**
1307 * @brief Initialize a statically-defined work item.
1308 *
1309 * This macro can be used to initialize a statically-defined workqueue work
1310 * item, prior to its first use. For example,
1311 *
1312 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1313 *
1314 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315 */
1316#define K_WORK_INITIALIZER(work_handler) \
1317 { \
1318 ._reserved = NULL, \
1319 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001320 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321 }
1322
1323/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001324 * @brief Initialize a work item.
1325 *
1326 * This routine initializes a workqueue work item, prior to its first use.
1327 *
1328 * @param work Address of work item.
1329 * @param handler Function to invoke each time work item is processed.
1330 *
1331 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001332 */
1333static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1334{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001335 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001336 work->handler = handler;
1337}
1338
1339/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001340 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001341 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001342 * This routine submits work item @a work to be processed by workqueue
1343 * @a work_q. If the work item is already pending in the workqueue's queue
1344 * as a result of an earlier submission, this routine has no effect on the
1345 * work item. If the work item has already been processed, or is currently
1346 * being processed, its work is considered complete and the work item can be
1347 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001348 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001349 * @warning
1350 * A submitted work item must not be modified until it has been processed
1351 * by the workqueue.
1352 *
1353 * @note Can be called by ISRs.
1354 *
1355 * @param work_q Address of workqueue.
1356 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001357 *
1358 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001359 */
1360static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1361 struct k_work *work)
1362{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001363 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364 k_fifo_put(&work_q->fifo, work);
1365 }
1366}
1367
1368/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001369 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001370 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001371 * This routine indicates if work item @a work is pending in a workqueue's
1372 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001373 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001374 * @note Can be called by ISRs.
1375 *
1376 * @param work Address of work item.
1377 *
1378 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001379 */
1380static inline int k_work_pending(struct k_work *work)
1381{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001382 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001383}
1384
1385/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001386 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001387 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001388 * This routine starts workqueue @a work_q. The workqueue spawns its work
1389 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001391 * @param work_q Address of workqueue.
1392 * @param stack Pointer to work queue thread's stack space.
1393 * @param stack_size Size of the work queue thread's stack (in bytes).
1394 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 *
1396 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397 */
Allan Stephens904cf972016-10-07 13:59:23 -05001398extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001399 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001401/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001402 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001403 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001404 * This routine initializes a workqueue delayed work item, prior to
1405 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001406 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001407 * @param work Address of delayed work item.
1408 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001409 *
1410 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001412extern void k_delayed_work_init(struct k_delayed_work *work,
1413 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414
1415/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001416 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001418 * This routine schedules work item @a work to be processed by workqueue
1419 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1420 * an asychronous countdown for the work item and then returns to the caller.
1421 * Only when the countdown completes is the work item actually submitted to
1422 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001424 * Submitting a previously submitted delayed work item that is still
1425 * counting down cancels the existing submission and restarts the countdown
1426 * using the new delay. If the work item is currently pending on the
1427 * workqueue's queue because the countdown has completed it is too late to
1428 * resubmit the item, and resubmission fails without impacting the work item.
1429 * If the work item has already been processed, or is currently being processed,
1430 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001432 * @warning
1433 * A delayed work item must not be modified until it has been processed
1434 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001436 * @note Can be called by ISRs.
1437 *
1438 * @param work_q Address of workqueue.
1439 * @param work Address of delayed work item.
1440 * @param delay Delay before submitting the work item (in milliseconds).
1441 *
1442 * @retval 0 Work item countdown started.
1443 * @retval -EINPROGRESS Work item is already pending.
1444 * @retval -EINVAL Work item is being processed or has completed its work.
1445 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001447extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1448 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001449 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450
1451/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001452 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001454 * This routine cancels the submission of delayed work item @a work.
1455 * A delayed work item can only be cancelled while its countdown is still
1456 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001457 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001460 * @param work Address of delayed work item.
1461 *
1462 * @retval 0 Work item countdown cancelled.
1463 * @retval -EINPROGRESS Work item is already pending.
1464 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001466extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001468/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001469 * @brief Submit a work item to the system workqueue.
1470 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001471 * This routine submits work item @a work to be processed by the system
1472 * workqueue. If the work item is already pending in the workqueue's queue
1473 * as a result of an earlier submission, this routine has no effect on the
1474 * work item. If the work item has already been processed, or is currently
1475 * being processed, its work is considered complete and the work item can be
1476 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001477 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001478 * @warning
1479 * Work items submitted to the system workqueue should avoid using handlers
1480 * that block or yield since this may prevent the system workqueue from
1481 * processing other work items in a timely manner.
1482 *
1483 * @note Can be called by ISRs.
1484 *
1485 * @param work Address of work item.
1486 *
1487 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001488 */
1489static inline void k_work_submit(struct k_work *work)
1490{
1491 k_work_submit_to_queue(&k_sys_work_q, work);
1492}
1493
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001495 * @brief Submit a delayed work item to the system workqueue.
1496 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001497 * This routine schedules work item @a work to be processed by the system
1498 * workqueue after a delay of @a delay milliseconds. The routine initiates
1499 * an asychronous countdown for the work item and then returns to the caller.
1500 * Only when the countdown completes is the work item actually submitted to
1501 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001503 * Submitting a previously submitted delayed work item that is still
1504 * counting down cancels the existing submission and restarts the countdown
1505 * using the new delay. If the work item is currently pending on the
1506 * workqueue's queue because the countdown has completed it is too late to
1507 * resubmit the item, and resubmission fails without impacting the work item.
1508 * If the work item has already been processed, or is currently being processed,
1509 * its work is considered complete and the work item can be resubmitted.
1510 *
1511 * @warning
1512 * Work items submitted to the system workqueue should avoid using handlers
1513 * that block or yield since this may prevent the system workqueue from
1514 * processing other work items in a timely manner.
1515 *
1516 * @note Can be called by ISRs.
1517 *
1518 * @param work Address of delayed work item.
1519 * @param delay Delay before submitting the work item (in milliseconds).
1520 *
1521 * @retval 0 Work item countdown started.
1522 * @retval -EINPROGRESS Work item is already pending.
1523 * @retval -EINVAL Work item is being processed or has completed its work.
1524 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525 */
1526static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001527 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001528{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001529 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530}
1531
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001532/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001533 * @brief Get time remaining before a delayed work gets scheduled.
1534 *
1535 * This routine computes the (approximate) time remaining before a
1536 * delayed work gets executed. If the delayed work is not waiting to be
1537 * schedules, it returns zero.
1538 *
1539 * @param work Delayed work item.
1540 *
1541 * @return Remaining time (in milliseconds).
1542 */
1543static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1544{
1545 return _timeout_remaining_get(&work->timeout);
1546}
1547
1548/**
Allan Stephensc98da842016-11-11 15:45:03 -05001549 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001550 */
1551
Allan Stephensc98da842016-11-11 15:45:03 -05001552/**
1553 * @cond INTERNAL_HIDDEN
1554 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001555
1556struct k_mutex {
1557 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001558 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001559 uint32_t lock_count;
1560 int owner_orig_prio;
1561#ifdef CONFIG_OBJECT_MONITOR
1562 int num_lock_state_changes;
1563 int num_conflicts;
1564#endif
1565
1566 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1567};
1568
1569#ifdef CONFIG_OBJECT_MONITOR
1570#define _MUTEX_INIT_OBJECT_MONITOR \
1571 .num_lock_state_changes = 0, .num_conflicts = 0,
1572#else
1573#define _MUTEX_INIT_OBJECT_MONITOR
1574#endif
1575
1576#define K_MUTEX_INITIALIZER(obj) \
1577 { \
1578 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1579 .owner = NULL, \
1580 .lock_count = 0, \
1581 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1582 _MUTEX_INIT_OBJECT_MONITOR \
1583 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1584 }
1585
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001586/**
Allan Stephensc98da842016-11-11 15:45:03 -05001587 * INTERNAL_HIDDEN @endcond
1588 */
1589
1590/**
1591 * @defgroup mutex_apis Mutex APIs
1592 * @ingroup kernel_apis
1593 * @{
1594 */
1595
1596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001597 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001599 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001600 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001601 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001604 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001605#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001606 struct k_mutex name \
1607 __in_section(_k_mutex, static, name) = \
1608 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001609
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001613 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001615 * Upon completion, the mutex is available and does not have an owner.
1616 *
1617 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618 *
1619 * @return N/A
1620 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001621extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001622
1623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * This routine locks @a mutex. If the mutex is locked by another thread,
1627 * the calling thread waits until the mutex becomes available or until
1628 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001630 * A thread is permitted to lock a mutex it has already locked. The operation
1631 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @param mutex Address of the mutex.
1634 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001635 * or one of the special values K_NO_WAIT and K_FOREVER.
1636 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001637 * @retval 0 Mutex locked.
1638 * @retval -EBUSY Returned without waiting.
1639 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001640 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001641extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642
1643/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001646 * This routine unlocks @a mutex. The mutex must already be locked by the
1647 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648 *
1649 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001651 * thread.
1652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 *
1655 * @return N/A
1656 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657extern void k_mutex_unlock(struct k_mutex *mutex);
1658
Allan Stephensc98da842016-11-11 15:45:03 -05001659/**
1660 * @} end defgroup mutex_apis
1661 */
1662
1663/**
1664 * @cond INTERNAL_HIDDEN
1665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001666
1667struct k_sem {
1668 _wait_q_t wait_q;
1669 unsigned int count;
1670 unsigned int limit;
1671
1672 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1673};
1674
Allan Stephensc98da842016-11-11 15:45:03 -05001675#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1676 { \
1677 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1678 .count = initial_count, \
1679 .limit = count_limit, \
1680 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1681 }
1682
1683/**
1684 * INTERNAL_HIDDEN @endcond
1685 */
1686
1687/**
1688 * @defgroup semaphore_apis Semaphore APIs
1689 * @ingroup kernel_apis
1690 * @{
1691 */
1692
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001698 * @param sem Address of the semaphore.
1699 * @param initial_count Initial semaphore count.
1700 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001701 *
1702 * @return N/A
1703 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001704extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1705 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001706
1707/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1713 *
1714 * @param sem Address of the semaphore.
1715 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001716 * or one of the special values K_NO_WAIT and K_FOREVER.
1717 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001718 * @note When porting code from the nanokernel legacy API to the new API, be
1719 * careful with the return value of this function. The return value is the
1720 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1721 * non-zero means failure, while the nano_sem_take family returns 1 for success
1722 * and 0 for failure.
1723 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001724 * @retval 0 Semaphore taken.
1725 * @retval -EBUSY Returned without waiting.
1726 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001727 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001729
1730/**
1731 * @brief Give a semaphore.
1732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * This routine gives @a sem, unless the semaphore is already at its maximum
1734 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001739 *
1740 * @return N/A
1741 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001742extern void k_sem_give(struct k_sem *sem);
1743
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001744/**
1745 * @brief Reset a semaphore's count to zero.
1746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001750 *
1751 * @return N/A
1752 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001753static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754{
1755 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001756}
1757
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001758/**
1759 * @brief Get a semaphore's count.
1760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001765 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001766 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001767static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768{
1769 return sem->count;
1770}
1771
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001776 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001777 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @param name Name of the semaphore.
1780 * @param initial_count Initial semaphore count.
1781 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001782 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001783#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001784 struct k_sem name \
1785 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001786 K_SEM_INITIALIZER(name, initial_count, count_limit)
1787
Allan Stephensc98da842016-11-11 15:45:03 -05001788/**
1789 * @} end defgroup semaphore_apis
1790 */
1791
1792/**
1793 * @defgroup alert_apis Alert APIs
1794 * @ingroup kernel_apis
1795 * @{
1796 */
1797
Allan Stephens5eceb852016-11-16 10:16:30 -05001798/**
1799 * @typedef k_alert_handler_t
1800 * @brief Alert handler function type.
1801 *
1802 * An alert's alert handler function is invoked by the system workqueue
1803 * when the alert is signalled. The alert handler function is optional,
1804 * and is only invoked if the alert has been initialized with one.
1805 *
1806 * @param alert Address of the alert.
1807 *
1808 * @return 0 if alert has been consumed; non-zero if alert should pend.
1809 */
1810typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001811
1812/**
1813 * @} end defgroup alert_apis
1814 */
1815
1816/**
1817 * @cond INTERNAL_HIDDEN
1818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001820#define K_ALERT_DEFAULT NULL
1821#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001822
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001823struct k_alert {
1824 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825 atomic_t send_count;
1826 struct k_work work_item;
1827 struct k_sem sem;
1828
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001829 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830};
1831
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001832extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001833
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001834#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001835 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001836 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001838 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001839 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001840 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1841 }
1842
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001843/**
Allan Stephensc98da842016-11-11 15:45:03 -05001844 * INTERNAL_HIDDEN @endcond
1845 */
1846
1847/**
1848 * @addtogroup alert_apis
1849 * @{
1850 */
1851
1852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001853 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001854 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001855 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001857 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001859 * @param name Name of the alert.
1860 * @param alert_handler Action to take when alert is sent. Specify either
1861 * the address of a function to be invoked by the system workqueue
1862 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1863 * K_ALERT_DEFAULT (which causes the alert to pend).
1864 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001866#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001867 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001868 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001869 K_ALERT_INITIALIZER(name, alert_handler, \
1870 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001871
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001872/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * @param alert Address of the alert.
1878 * @param handler Action to take when alert is sent. Specify either the address
1879 * of a function to be invoked by the system workqueue thread,
1880 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1881 * K_ALERT_DEFAULT (which causes the alert to pend).
1882 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001883 *
1884 * @return N/A
1885 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001886extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1887 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888
1889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001890 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1895 *
1896 * @param alert Address of the alert.
1897 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 * or one of the special values K_NO_WAIT and K_FOREVER.
1899 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001900 * @retval 0 Alert received.
1901 * @retval -EBUSY Returned without waiting.
1902 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001904extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001905
1906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 * This routine signals @a alert. The action specified for @a alert will
1910 * be taken, which may trigger the execution of an alert handler function
1911 * and/or cause the alert to pend (assuming the alert has not reached its
1912 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001914 * @note Can be called by ISRs.
1915 *
1916 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917 *
1918 * @return N/A
1919 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001920extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921
1922/**
Allan Stephensc98da842016-11-11 15:45:03 -05001923 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924 */
1925
Allan Stephensc98da842016-11-11 15:45:03 -05001926/**
1927 * @cond INTERNAL_HIDDEN
1928 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929
1930struct k_msgq {
1931 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001932 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933 uint32_t max_msgs;
1934 char *buffer_start;
1935 char *buffer_end;
1936 char *read_ptr;
1937 char *write_ptr;
1938 uint32_t used_msgs;
1939
1940 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1941};
1942
Peter Mitsis1da807e2016-10-06 11:36:59 -04001943#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944 { \
1945 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001946 .max_msgs = q_max_msgs, \
1947 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001949 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950 .read_ptr = q_buffer, \
1951 .write_ptr = q_buffer, \
1952 .used_msgs = 0, \
1953 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1954 }
1955
Peter Mitsis1da807e2016-10-06 11:36:59 -04001956/**
Allan Stephensc98da842016-11-11 15:45:03 -05001957 * INTERNAL_HIDDEN @endcond
1958 */
1959
1960/**
1961 * @defgroup msgq_apis Message Queue APIs
1962 * @ingroup kernel_apis
1963 * @{
1964 */
1965
1966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1970 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001971 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1972 * message is similarly aligned to this boundary, @a q_msg_size must also be
1973 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * The message queue can be accessed outside the module where it is defined
1976 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001978 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * @param q_name Name of the message queue.
1981 * @param q_msg_size Message size (in bytes).
1982 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001983 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001984 */
1985#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1986 static char __noinit __aligned(q_align) \
1987 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001988 struct k_msgq q_name \
1989 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001990 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1991 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992
Peter Mitsisd7a37502016-10-13 11:37:40 -04001993/**
1994 * @brief Initialize a message queue.
1995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001996 * This routine initializes a message queue object, prior to its first use.
1997 *
Allan Stephensda827222016-11-09 14:23:58 -06001998 * The message queue's ring buffer must contain space for @a max_msgs messages,
1999 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2000 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2001 * that each message is similarly aligned to this boundary, @a q_msg_size
2002 * must also be a multiple of N.
2003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * @param q Address of the message queue.
2005 * @param buffer Pointer to ring buffer that holds queued messages.
2006 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002007 * @param max_msgs Maximum number of messages that can be queued.
2008 *
2009 * @return N/A
2010 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002011extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002012 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002013
2014/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002015 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002018 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002019 * @note Can be called by ISRs.
2020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @param q Address of the message queue.
2022 * @param data Pointer to the message.
2023 * @param timeout Waiting period to add the message (in milliseconds),
2024 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002025 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002026 * @retval 0 Message sent.
2027 * @retval -ENOMSG Returned without waiting or queue purged.
2028 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002029 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002031
2032/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * This routine receives a message from message queue @a q in a "first in,
2036 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002037 *
Allan Stephensc98da842016-11-11 15:45:03 -05002038 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002040 * @param q Address of the message queue.
2041 * @param data Address of area to hold the received message.
2042 * @param timeout Waiting period to receive the message (in milliseconds),
2043 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002044 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002045 * @retval 0 Message received.
2046 * @retval -ENOMSG Returned without waiting.
2047 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002048 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002050
2051/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002052 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * This routine discards all unreceived messages in a message queue's ring
2055 * buffer. Any threads that are blocked waiting to send a message to the
2056 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002059 *
2060 * @return N/A
2061 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062extern void k_msgq_purge(struct k_msgq *q);
2063
Peter Mitsis67be2492016-10-07 11:44:34 -04002064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002067 * This routine returns the number of unused entries in a message queue's
2068 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070 * @param q Address of the message queue.
2071 *
2072 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002073 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002074static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002075{
2076 return q->max_msgs - q->used_msgs;
2077}
2078
Peter Mitsisd7a37502016-10-13 11:37:40 -04002079/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002084 * @param q Address of the message queue.
2085 *
2086 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002087 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002088static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089{
2090 return q->used_msgs;
2091}
2092
Allan Stephensc98da842016-11-11 15:45:03 -05002093/**
2094 * @} end defgroup msgq_apis
2095 */
2096
2097/**
2098 * @defgroup mem_pool_apis Memory Pool APIs
2099 * @ingroup kernel_apis
2100 * @{
2101 */
2102
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002103struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002104 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002105 void *addr_in_pool;
2106 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002107 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108};
2109
Allan Stephensc98da842016-11-11 15:45:03 -05002110/**
2111 * @} end defgroup mem_pool_apis
2112 */
2113
2114/**
2115 * @defgroup mailbox_apis Mailbox APIs
2116 * @ingroup kernel_apis
2117 * @{
2118 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119
2120struct k_mbox_msg {
2121 /** internal use only - needed for legacy API support */
2122 uint32_t _mailbox;
2123 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002124 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125 /** application-defined information value */
2126 uint32_t info;
2127 /** sender's message data buffer */
2128 void *tx_data;
2129 /** internal use only - needed for legacy API support */
2130 void *_rx_data;
2131 /** message data block descriptor */
2132 struct k_mem_block tx_block;
2133 /** source thread id */
2134 k_tid_t rx_source_thread;
2135 /** target thread id */
2136 k_tid_t tx_target_thread;
2137 /** internal use only - thread waiting on send (may be a dummy) */
2138 k_tid_t _syncing_thread;
2139#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2140 /** internal use only - semaphore used during asynchronous send */
2141 struct k_sem *_async_sem;
2142#endif
2143};
2144
Allan Stephensc98da842016-11-11 15:45:03 -05002145/**
2146 * @cond INTERNAL_HIDDEN
2147 */
2148
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149struct k_mbox {
2150 _wait_q_t tx_msg_queue;
2151 _wait_q_t rx_msg_queue;
2152
2153 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
2154};
2155
2156#define K_MBOX_INITIALIZER(obj) \
2157 { \
2158 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2159 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
2160 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2161 }
2162
Peter Mitsis12092702016-10-14 12:57:23 -04002163/**
Allan Stephensc98da842016-11-11 15:45:03 -05002164 * INTERNAL_HIDDEN @endcond
2165 */
2166
2167/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002172 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002177 struct k_mbox name \
2178 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179 K_MBOX_INITIALIZER(name) \
2180
Peter Mitsis12092702016-10-14 12:57:23 -04002181/**
2182 * @brief Initialize a mailbox.
2183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * This routine initializes a mailbox object, prior to its first use.
2185 *
2186 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002187 *
2188 * @return N/A
2189 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190extern void k_mbox_init(struct k_mbox *mbox);
2191
Peter Mitsis12092702016-10-14 12:57:23 -04002192/**
2193 * @brief Send a mailbox message in a synchronous manner.
2194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * This routine sends a message to @a mbox and waits for a receiver to both
2196 * receive and process it. The message data may be in a buffer, in a memory
2197 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @param mbox Address of the mailbox.
2200 * @param tx_msg Address of the transmit message descriptor.
2201 * @param timeout Waiting period for the message to be received (in
2202 * milliseconds), or one of the special values K_NO_WAIT
2203 * and K_FOREVER. Once the message has been received,
2204 * this routine waits as long as necessary for the message
2205 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002206 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002207 * @retval 0 Message sent.
2208 * @retval -ENOMSG Returned without waiting.
2209 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002210 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002211extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002213
Peter Mitsis12092702016-10-14 12:57:23 -04002214/**
2215 * @brief Send a mailbox message in an asynchronous manner.
2216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * This routine sends a message to @a mbox without waiting for a receiver
2218 * to process it. The message data may be in a buffer, in a memory pool block,
2219 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2220 * will be given when the message has been both received and completely
2221 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002223 * @param mbox Address of the mailbox.
2224 * @param tx_msg Address of the transmit message descriptor.
2225 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002226 *
2227 * @return N/A
2228 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002229extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230 struct k_sem *sem);
2231
Peter Mitsis12092702016-10-14 12:57:23 -04002232/**
2233 * @brief Receive a mailbox message.
2234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * This routine receives a message from @a mbox, then optionally retrieves
2236 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002238 * @param mbox Address of the mailbox.
2239 * @param rx_msg Address of the receive message descriptor.
2240 * @param buffer Address of the buffer to receive data, or NULL to defer data
2241 * retrieval and message disposal until later.
2242 * @param timeout Waiting period for a message to be received (in
2243 * milliseconds), or one of the special values K_NO_WAIT
2244 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002245 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002246 * @retval 0 Message received.
2247 * @retval -ENOMSG Returned without waiting.
2248 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002249 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002250extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002252
2253/**
2254 * @brief Retrieve mailbox message data into a buffer.
2255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002256 * This routine completes the processing of a received message by retrieving
2257 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002258 *
2259 * Alternatively, this routine can be used to dispose of a received message
2260 * without retrieving its data.
2261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * @param rx_msg Address of the receive message descriptor.
2263 * @param buffer Address of the buffer to receive data, or NULL to discard
2264 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002265 *
2266 * @return N/A
2267 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002268extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002269
2270/**
2271 * @brief Retrieve mailbox message data into a memory pool block.
2272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002273 * This routine completes the processing of a received message by retrieving
2274 * its data into a memory pool block, then disposing of the message.
2275 * The memory pool block that results from successful retrieval must be
2276 * returned to the pool once the data has been processed, even in cases
2277 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002278 *
2279 * Alternatively, this routine can be used to dispose of a received message
2280 * without retrieving its data. In this case there is no need to return a
2281 * memory pool block to the pool.
2282 *
2283 * This routine allocates a new memory pool block for the data only if the
2284 * data is not already in one. If a new block cannot be allocated, the routine
2285 * returns a failure code and the received message is left unchanged. This
2286 * permits the caller to reattempt data retrieval at a later time or to dispose
2287 * of the received message without retrieving its data.
2288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002289 * @param rx_msg Address of a receive message descriptor.
2290 * @param pool Address of memory pool, or NULL to discard data.
2291 * @param block Address of the area to hold memory pool block info.
2292 * @param timeout Waiting period to wait for a memory pool block (in
2293 * milliseconds), or one of the special values K_NO_WAIT
2294 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002295 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002296 * @retval 0 Data retrieved.
2297 * @retval -ENOMEM Returned without waiting.
2298 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002299 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002300extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002301 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302 struct k_mem_block *block, int32_t timeout);
2303
Allan Stephensc98da842016-11-11 15:45:03 -05002304/**
2305 * @} end defgroup mailbox_apis
2306 */
2307
2308/**
2309 * @cond INTERNAL_HIDDEN
2310 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311
2312struct k_pipe {
2313 unsigned char *buffer; /* Pipe buffer: may be NULL */
2314 size_t size; /* Buffer size */
2315 size_t bytes_used; /* # bytes used in buffer */
2316 size_t read_index; /* Where in buffer to read from */
2317 size_t write_index; /* Where in buffer to write */
2318
2319 struct {
2320 _wait_q_t readers; /* Reader wait queue */
2321 _wait_q_t writers; /* Writer wait queue */
2322 } wait_q;
2323
2324 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2325};
2326
Peter Mitsise5d9c582016-10-14 14:44:57 -04002327#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328 { \
2329 .buffer = pipe_buffer, \
2330 .size = pipe_buffer_size, \
2331 .bytes_used = 0, \
2332 .read_index = 0, \
2333 .write_index = 0, \
2334 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2335 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2336 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2337 }
2338
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339/**
Allan Stephensc98da842016-11-11 15:45:03 -05002340 * INTERNAL_HIDDEN @endcond
2341 */
2342
2343/**
2344 * @defgroup pipe_apis Pipe APIs
2345 * @ingroup kernel_apis
2346 * @{
2347 */
2348
2349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002354 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @param name Name of the pipe.
2357 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2358 * or zero if no ring buffer is used.
2359 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002361#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2362 static unsigned char __noinit __aligned(pipe_align) \
2363 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002364 struct k_pipe name \
2365 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002366 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * This routine initializes a pipe object, prior to its first use.
2372 *
2373 * @param pipe Address of the pipe.
2374 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2375 * is used.
2376 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2377 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 *
2379 * @return N/A
2380 */
2381extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2382 size_t size);
2383
2384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * @param pipe Address of the pipe.
2390 * @param data Address of data to write.
2391 * @param bytes_to_write Size of data (in bytes).
2392 * @param bytes_written Address of area to hold the number of bytes written.
2393 * @param min_xfer Minimum number of bytes to write.
2394 * @param timeout Waiting period to wait for the data to be written (in
2395 * milliseconds), or one of the special values K_NO_WAIT
2396 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002398 * @retval 0 At least @a min_xfer bytes of data were written.
2399 * @retval -EIO Returned without waiting; zero data bytes were written.
2400 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002403extern int k_pipe_put(struct k_pipe *pipe, void *data,
2404 size_t bytes_to_write, size_t *bytes_written,
2405 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406
2407/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * @param pipe Address of the pipe.
2413 * @param data Address to place the data read from pipe.
2414 * @param bytes_to_read Maximum number of data bytes to read.
2415 * @param bytes_read Address of area to hold the number of bytes read.
2416 * @param min_xfer Minimum number of data bytes to read.
2417 * @param timeout Waiting period to wait for the data to be read (in
2418 * milliseconds), or one of the special values K_NO_WAIT
2419 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002421 * @retval 0 At least @a min_xfer bytes of data were read.
2422 * @retval -EIO Returned without waiting; zero data bytes were read.
2423 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002425 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002426extern int k_pipe_get(struct k_pipe *pipe, void *data,
2427 size_t bytes_to_read, size_t *bytes_read,
2428 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429
2430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * This routine writes the data contained in a memory block to @a pipe.
2434 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 * @param block Memory block containing data to send
2439 * @param size Number of data bytes in memory block to send
2440 * @param sem Semaphore to signal upon completion (else NULL)
2441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443 */
2444extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2445 size_t size, struct k_sem *sem);
2446
2447/**
Allan Stephensc98da842016-11-11 15:45:03 -05002448 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 */
2450
Allan Stephensc98da842016-11-11 15:45:03 -05002451/**
2452 * @cond INTERNAL_HIDDEN
2453 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002455struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002457 uint32_t num_blocks;
2458 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459 char *buffer;
2460 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002461 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002463 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464};
2465
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002466#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2467 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468 { \
2469 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002470 .num_blocks = slab_num_blocks, \
2471 .block_size = slab_block_size, \
2472 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473 .free_list = NULL, \
2474 .num_used = 0, \
2475 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2476 }
2477
Peter Mitsis578f9112016-10-07 13:50:31 -04002478/**
Allan Stephensc98da842016-11-11 15:45:03 -05002479 * INTERNAL_HIDDEN @endcond
2480 */
2481
2482/**
2483 * @defgroup mem_slab_apis Memory Slab APIs
2484 * @ingroup kernel_apis
2485 * @{
2486 */
2487
2488/**
Allan Stephensda827222016-11-09 14:23:58 -06002489 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002490 *
Allan Stephensda827222016-11-09 14:23:58 -06002491 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002493 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2494 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002495 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002496 *
Allan Stephensda827222016-11-09 14:23:58 -06002497 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002500 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002502 * @param name Name of the memory slab.
2503 * @param slab_block_size Size of each memory block (in bytes).
2504 * @param slab_num_blocks Number memory blocks.
2505 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002506 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002507#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2508 char __noinit __aligned(slab_align) \
2509 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2510 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002511 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002512 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2513 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002515/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002516 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002519 *
Allan Stephensda827222016-11-09 14:23:58 -06002520 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2521 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2522 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2523 * To ensure that each memory block is similarly aligned to this boundary,
2524 * @a slab_block_size must also be a multiple of N.
2525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @param slab Address of the memory slab.
2527 * @param buffer Pointer to buffer used for the memory blocks.
2528 * @param block_size Size of each memory block (in bytes).
2529 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002530 *
2531 * @return N/A
2532 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002533extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002534 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002535
2536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @param slab Address of the memory slab.
2542 * @param mem Pointer to block address area.
2543 * @param timeout Maximum time to wait for operation to complete
2544 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2545 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002546 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002547 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002549 * @retval -ENOMEM Returned without waiting.
2550 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002551 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002552extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2553 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002554
2555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine releases a previously allocated memory block back to its
2559 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * @param slab Address of the memory slab.
2562 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002563 *
2564 * @return N/A
2565 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002566extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002568/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * This routine gets the number of memory blocks that are currently
2572 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002577 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002578static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002580 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581}
2582
Peter Mitsisc001aa82016-10-13 13:53:37 -04002583/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * This routine gets the number of memory blocks that are currently
2587 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002592 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002593static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002594{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002595 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002596}
2597
Allan Stephensc98da842016-11-11 15:45:03 -05002598/**
2599 * @} end defgroup mem_slab_apis
2600 */
2601
2602/**
2603 * @cond INTERNAL_HIDDEN
2604 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002606/*
2607 * Memory pool requires a buffer and two arrays of structures for the
2608 * memory block accounting:
2609 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2610 * status of four blocks of memory.
2611 */
2612struct k_mem_pool_quad_block {
2613 char *mem_blocks; /* pointer to the first of four memory blocks */
2614 uint32_t mem_status; /* four bits. If bit is set, memory block is
2615 allocated */
2616};
2617/*
2618 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2619 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2620 * block size is 4 times less than the previous one and thus requires 4 times
2621 * bigger array of k_mem_pool_quad_block structures to keep track of the
2622 * memory blocks.
2623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002625/*
2626 * The array of k_mem_pool_block_set keeps the information of each array of
2627 * k_mem_pool_quad_block structures
2628 */
2629struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002630 size_t block_size; /* memory block size */
2631 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002632 struct k_mem_pool_quad_block *quad_block;
2633 int count;
2634};
2635
2636/* Memory pool descriptor */
2637struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002638 size_t max_block_size;
2639 size_t min_block_size;
2640 uint32_t nr_of_maxblocks;
2641 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002642 struct k_mem_pool_block_set *block_set;
2643 char *bufblock;
2644 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2646};
2647
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002648#ifdef CONFIG_ARM
2649#define _SECTION_TYPE_SIGN "%"
2650#else
2651#define _SECTION_TYPE_SIGN "@"
2652#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002653
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002654/*
2655 * Static memory pool initialization
2656 */
Allan Stephensc98da842016-11-11 15:45:03 -05002657
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002658/*
2659 * Use .altmacro to be able to recalculate values and pass them as string
2660 * arguments when calling assembler macros resursively
2661 */
2662__asm__(".altmacro\n\t");
2663
2664/*
2665 * Recursively calls a macro
2666 * The followig global symbols need to be initialized:
2667 * __memory_pool_max_block_size - maximal size of the memory block
2668 * __memory_pool_min_block_size - minimal size of the memory block
2669 * Notes:
2670 * Global symbols are used due the fact that assembler macro allows only
2671 * one argument be passed with the % conversion
2672 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2673 * is used instead of / 4.
2674 * n_max argument needs to go first in the invoked macro, as some
2675 * assemblers concatenate \name and %(\n_max * 4) arguments
2676 * if \name goes first
2677 */
2678__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2679 ".ifge __memory_pool_max_block_size >> 2 -"
2680 " __memory_pool_min_block_size\n\t\t"
2681 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2682 "\\macro_name %(\\n_max * 4) \\name\n\t"
2683 ".endif\n\t"
2684 ".endm\n");
2685
2686/*
2687 * Build quad blocks
2688 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2689 * structures and recursively calls itself for the next array, 4 times
2690 * larger.
2691 * The followig global symbols need to be initialized:
2692 * __memory_pool_max_block_size - maximal size of the memory block
2693 * __memory_pool_min_block_size - minimal size of the memory block
2694 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2695 */
2696__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002697 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002698 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2699 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2700 ".if \\n_max % 4\n\t\t"
2701 ".skip __memory_pool_quad_block_size\n\t"
2702 ".endif\n\t"
2703 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2704 ".endm\n");
2705
2706/*
2707 * Build block sets and initialize them
2708 * Macro initializes the k_mem_pool_block_set structure and
2709 * recursively calls itself for the next one.
2710 * The followig global symbols need to be initialized:
2711 * __memory_pool_max_block_size - maximal size of the memory block
2712 * __memory_pool_min_block_size - minimal size of the memory block
2713 * __memory_pool_block_set_count, the number of the elements in the
2714 * block set array must be set to 0. Macro calculates it's real
2715 * value.
2716 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2717 * structures, _build_quad_blocks must be called prior it.
2718 */
2719__asm__(".macro _build_block_set n_max, name\n\t"
2720 ".int __memory_pool_max_block_size\n\t" /* block_size */
2721 ".if \\n_max % 4\n\t\t"
2722 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2723 ".else\n\t\t"
2724 ".int \\n_max >> 2\n\t"
2725 ".endif\n\t"
2726 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2727 ".int 0\n\t" /* count */
2728 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2729 "__do_recurse _build_block_set \\name \\n_max\n\t"
2730 ".endm\n");
2731
2732/*
2733 * Build a memory pool structure and initialize it
2734 * Macro uses __memory_pool_block_set_count global symbol,
2735 * block set addresses and buffer address, it may be called only after
2736 * _build_block_set
2737 */
2738__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002739 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002740 _SECTION_TYPE_SIGN "progbits\n\t"
2741 ".globl \\name\n\t"
2742 "\\name:\n\t"
2743 ".int \\max_size\n\t" /* max_block_size */
2744 ".int \\min_size\n\t" /* min_block_size */
2745 ".int \\n_max\n\t" /* nr_of_maxblocks */
2746 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2747 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2748 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2749 ".int 0\n\t" /* wait_q->head */
2750 ".int 0\n\t" /* wait_q->next */
2751 ".popsection\n\t"
2752 ".endm\n");
2753
2754#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2755 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2756 _SECTION_TYPE_SIGN "progbits\n\t"); \
2757 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2758 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2759 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2760 STRINGIFY(name) "\n\t"); \
2761 __asm__(".popsection\n\t")
2762
2763#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2764 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2765 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2766 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2767 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002768 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002769 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2770 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2771 STRINGIFY(name) "\n\t"); \
2772 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2773 __asm__(".int __memory_pool_block_set_count\n\t"); \
2774 __asm__(".popsection\n\t"); \
2775 extern uint32_t _mem_pool_block_set_count_##name; \
2776 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2777
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002778#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2779 char __noinit __aligned(align) \
2780 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002781
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002782/*
2783 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2784 * to __memory_pool_quad_block_size absolute symbol.
2785 * This function does not get called, but compiler calculates the value and
2786 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2787 */
2788static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2789{
2790 __asm__(".globl __memory_pool_quad_block_size\n\t"
2791#ifdef CONFIG_NIOS2
2792 "__memory_pool_quad_block_size = %0\n\t"
2793#else
2794 "__memory_pool_quad_block_size = %c0\n\t"
2795#endif
2796 :
2797 : "n"(sizeof(struct k_mem_pool_quad_block)));
2798}
2799
2800/**
Allan Stephensc98da842016-11-11 15:45:03 -05002801 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002802 */
2803
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002804/**
Allan Stephensc98da842016-11-11 15:45:03 -05002805 * @addtogroup mem_pool_apis
2806 * @{
2807 */
2808
2809/**
2810 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2813 * long. The memory pool allows blocks to be repeatedly partitioned into
2814 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2815 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002816 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002818 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819 * If the pool is to be accessed outside the module where it is defined, it
2820 * can be declared via
2821 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002822 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * @param name Name of the memory pool.
2825 * @param min_size Size of the smallest blocks in the pool (in bytes).
2826 * @param max_size Size of the largest blocks in the pool (in bytes).
2827 * @param n_max Number of maximum sized blocks in the pool.
2828 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002829 */
2830#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002831 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2832 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002833 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002834 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2835 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2836 extern struct k_mem_pool name
2837
Peter Mitsis937042c2016-10-13 13:18:26 -04002838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002843 * @param pool Address of the memory pool.
2844 * @param block Pointer to block descriptor for the allocated memory.
2845 * @param size Amount of memory to allocate (in bytes).
2846 * @param timeout Maximum time to wait for operation to complete
2847 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2848 * or K_FOREVER to wait as long as necessary.
2849 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002850 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002851 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002852 * @retval -ENOMEM Returned without waiting.
2853 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002854 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002855extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002856 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002857
2858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * This routine releases a previously allocated memory block back to its
2862 * memory pool.
2863 *
2864 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002865 *
2866 * @return N/A
2867 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002869
2870/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * This routine instructs a memory pool to concatenate unused memory blocks
2874 * into larger blocks wherever possible. Manually defragmenting the memory
2875 * pool may speed up future allocations of memory blocks by eliminating the
2876 * need for the memory pool to perform an automatic partial defragmentation.
2877 *
2878 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002879 *
2880 * @return N/A
2881 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002882extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002883
2884/**
Allan Stephensc98da842016-11-11 15:45:03 -05002885 * @} end addtogroup mem_pool_apis
2886 */
2887
2888/**
2889 * @defgroup heap_apis Heap Memory Pool APIs
2890 * @ingroup kernel_apis
2891 * @{
2892 */
2893
2894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002898 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002903 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002904extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002905
2906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002908 *
2909 * This routine provides traditional free() semantics. The memory being
2910 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002913 *
2914 * @return N/A
2915 */
2916extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917
Allan Stephensc98da842016-11-11 15:45:03 -05002918/**
2919 * @} end defgroup heap_apis
2920 */
2921
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002922/*
2923 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2924 * hook into the device subsystem, which itself uses nanokernel semaphores,
2925 * and thus currently requires the definition of nano_sem.
2926 */
2927#include <legacy.h>
2928#include <arch/cpu.h>
2929
2930/*
2931 * private APIs that are utilized by one or more public APIs
2932 */
2933
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002936extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937
2938#ifdef __cplusplus
2939}
2940#endif
2941
Andrew Boiee004dec2016-11-07 09:01:19 -08002942#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2943/*
2944 * Define new and delete operators.
2945 * At this moment, the operators do nothing since objects are supposed
2946 * to be statically allocated.
2947 */
2948inline void operator delete(void *ptr)
2949{
2950 (void)ptr;
2951}
2952
2953inline void operator delete[](void *ptr)
2954{
2955 (void)ptr;
2956}
2957
2958inline void *operator new(size_t size)
2959{
2960 (void)size;
2961 return NULL;
2962}
2963
2964inline void *operator new[](size_t size)
2965{
2966 (void)size;
2967 return NULL;
2968}
2969
2970/* Placement versions of operator new and delete */
2971inline void operator delete(void *ptr1, void *ptr2)
2972{
2973 (void)ptr1;
2974 (void)ptr2;
2975}
2976
2977inline void operator delete[](void *ptr1, void *ptr2)
2978{
2979 (void)ptr1;
2980 (void)ptr2;
2981}
2982
2983inline void *operator new(size_t size, void *ptr)
2984{
2985 (void)size;
2986 return ptr;
2987}
2988
2989inline void *operator new[](size_t size, void *ptr)
2990{
2991 (void)size;
2992 return ptr;
2993}
2994
2995#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2996
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997#endif /* _kernel__h_ */