blob: 715f6acc803d1aa7c56f00c79092a8ed1f6f4ad8 [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 Walsh456c6da2016-09-02 18:55:39 -0400155extern k_tid_t k_thread_spawn(char *stack, unsigned 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,
158 int32_t prio, uint32_t options, int32_t delay);
159
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
Allan Stephensc98da842016-11-11 15:45:03 -0500657/**
658 * INTERNAL_HIDDEN @endcond
659 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500660
Allan Stephensc98da842016-11-11 15:45:03 -0500661/**
662 * @cond INTERNAL_HIDDEN
663 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400664
665struct k_timer {
666 /*
667 * _timeout structure must be first here if we want to use
668 * dynamic timer allocation. timeout.node is used in the double-linked
669 * list of free timers
670 */
671 struct _timeout timeout;
672
Allan Stephens45bfa372016-10-12 12:39:42 -0500673 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400674 _wait_q_t wait_q;
675
676 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500677 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400678
679 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500680 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400681
682 /* timer period */
683 int32_t period;
684
Allan Stephens45bfa372016-10-12 12:39:42 -0500685 /* timer status */
686 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400687
Allan Stephens45bfa372016-10-12 12:39:42 -0500688 /* used to support legacy timer APIs */
689 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400690
691 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
692};
693
Allan Stephens1342adb2016-11-03 13:54:53 -0500694#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400695 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500696 .timeout.delta_ticks_from_prev = -1, \
697 .timeout.wait_q = NULL, \
698 .timeout.thread = NULL, \
699 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400700 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500701 .expiry_fn = expiry, \
702 .stop_fn = stop, \
703 .status = 0, \
704 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400705 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
706 }
707
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708/**
Allan Stephensc98da842016-11-11 15:45:03 -0500709 * INTERNAL_HIDDEN @endcond
710 */
711
712/**
713 * @defgroup timer_apis Timer APIs
714 * @ingroup kernel_apis
715 * @{
716 */
717
718/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500719 * @typedef k_timer_expiry_t
720 * @brief Timer expiry function type.
721 *
722 * A timer's expiry function is executed by the system clock interrupt handler
723 * each time the timer expires. The expiry function is optional, and is only
724 * invoked if the timer has been initialized with one.
725 *
726 * @param timer Address of timer.
727 *
728 * @return N/A
729 */
730typedef void (*k_timer_expiry_t)(struct k_timer *timer);
731
732/**
733 * @typedef k_timer_stop_t
734 * @brief Timer stop function type.
735 *
736 * A timer's stop function is executed if the timer is stopped prematurely.
737 * The function runs in the context of the thread that stops the timer.
738 * The stop function is optional, and is only invoked if the timer has been
739 * initialized with one.
740 *
741 * @param timer Address of timer.
742 *
743 * @return N/A
744 */
745typedef void (*k_timer_stop_t)(struct k_timer *timer);
746
747/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500748 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500752 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400753 *
754 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * @param expiry_fn Function to invoke each time the timer expires.
756 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500758#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500759 struct k_timer name \
760 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500761 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400762
Allan Stephens45bfa372016-10-12 12:39:42 -0500763/**
764 * @brief Initialize a timer.
765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500767 *
768 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500769 * @param expiry_fn Function to invoke each time the timer expires.
770 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500771 *
772 * @return N/A
773 */
774extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500775 k_timer_expiry_t expiry_fn,
776 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700777
Allan Stephens45bfa372016-10-12 12:39:42 -0500778/**
779 * @brief Start a timer.
780 *
781 * This routine starts a timer, and resets its status to zero. The timer
782 * begins counting down using the specified duration and period values.
783 *
784 * Attempting to start a timer that is already running is permitted.
785 * The timer's status is reset to zero and the timer begins counting down
786 * using the new duration and period values.
787 *
788 * @param timer Address of timer.
789 * @param duration Initial timer duration (in milliseconds).
790 * @param period Timer period (in milliseconds).
791 *
792 * @return N/A
793 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400794extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500795 int32_t duration, int32_t period);
796
797/**
798 * @brief Stop a timer.
799 *
800 * This routine stops a running timer prematurely. The timer's stop function,
801 * if one exists, is invoked by the caller.
802 *
803 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500805 *
806 * @param timer Address of timer.
807 *
808 * @return N/A
809 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500811
812/**
813 * @brief Read timer status.
814 *
815 * This routine reads the timer's status, which indicates the number of times
816 * it has expired since its status was last read.
817 *
818 * Calling this routine resets the timer's status to zero.
819 *
820 * @param timer Address of timer.
821 *
822 * @return Timer status.
823 */
824extern uint32_t k_timer_status_get(struct k_timer *timer);
825
826/**
827 * @brief Synchronize thread to timer expiration.
828 *
829 * This routine blocks the calling thread until the timer's status is non-zero
830 * (indicating that it has expired at least once since it was last examined)
831 * or the timer is stopped. If the timer status is already non-zero,
832 * or the timer is already stopped, the caller continues without waiting.
833 *
834 * Calling this routine resets the timer's status to zero.
835 *
836 * This routine must not be used by interrupt handlers, since they are not
837 * allowed to block.
838 *
839 * @param timer Address of timer.
840 *
841 * @return Timer status.
842 */
843extern uint32_t k_timer_status_sync(struct k_timer *timer);
844
845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500847 *
848 * This routine computes the (approximate) time remaining before a running
849 * timer next expires. If the timer is not running, it returns zero.
850 *
851 * @param timer Address of timer.
852 *
853 * @return Remaining time (in milliseconds).
854 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400855extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400856
Allan Stephensc98da842016-11-11 15:45:03 -0500857/**
858 * @} end defgroup timer_apis
859 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400860
Allan Stephensc98da842016-11-11 15:45:03 -0500861/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500862 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500863 * @{
864 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500865
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * This routine returns the elapsed time since the system booted,
870 * in milliseconds.
871 *
872 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400875
876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * This routine returns the lower 32-bits of the elapsed time since the system
880 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * This routine can be more efficient than k_uptime_get(), as it reduces the
883 * need for interrupt locking and 64-bit math. However, the 32-bit result
884 * cannot hold a system uptime time larger than approximately 50 days, so the
885 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400888 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400889extern uint32_t k_uptime_get_32(void);
890
891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500894 * This routine computes the elapsed time between the current system uptime
895 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * @param reftime Pointer to a reference time, which is updated to the current
898 * uptime upon return.
899 *
900 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400901 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400902extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400903
904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * This routine computes the elapsed time between the current system uptime
908 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500910 * This routine can be more efficient than k_uptime_delta(), as it reduces the
911 * need for interrupt locking and 64-bit math. However, the 32-bit result
912 * cannot hold an elapsed time larger than approximately 50 days, so the
913 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * @param reftime Pointer to a reference time, which is updated to the current
916 * uptime upon return.
917 *
918 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400919 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400920extern uint32_t k_uptime_delta_32(int64_t *reftime);
921
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * This routine returns the current time, as measured by the system's hardware
926 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930extern uint32_t k_cycle_get_32(void);
931
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400932/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500933 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400934 */
935
Allan Stephensc98da842016-11-11 15:45:03 -0500936/**
937 * @cond INTERNAL_HIDDEN
938 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400939
940struct k_fifo {
941 _wait_q_t wait_q;
942 sys_slist_t data_q;
943
944 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
945};
946
Allan Stephensc98da842016-11-11 15:45:03 -0500947#define K_FIFO_INITIALIZER(obj) \
948 { \
949 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
950 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
951 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
952 }
953
954/**
955 * INTERNAL_HIDDEN @endcond
956 */
957
958/**
959 * @defgroup fifo_apis Fifo APIs
960 * @ingroup kernel_apis
961 * @{
962 */
963
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970 *
971 * @return N/A
972 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400973extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400974
975/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500976 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500978 * This routine adds a data item to @a fifo. A fifo data item must be
979 * aligned on a 4-byte boundary, and the first 32 bits of the item are
980 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * @note Can be called by ISRs.
983 *
984 * @param fifo Address of the fifo.
985 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
987 * @return N/A
988 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400989extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990
991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * This routine adds a list of data items to @a fifo in one operation.
995 * The data items must be in a singly-linked list, with the first 32 bits
996 * each data item pointing to the next data item; the list must be
997 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * @param fifo Address of the fifo.
1002 * @param head Pointer to first node in singly-linked list.
1003 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
1005 * @return N/A
1006 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001007extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008
1009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * This routine adds a list of data items to @a fifo in one operation.
1013 * The data items must be in a singly-linked list implemented using a
1014 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 * and must be re-initialized via sys_slist_init().
1016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @note Can be called by ISRs.
1018 *
1019 * @param fifo Address of the fifo.
1020 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 *
1022 * @return N/A
1023 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001024extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025
1026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001027 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * This routine removes a data item from @a fifo in a "first in, first out"
1030 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1033 *
1034 * @param fifo Address of the fifo.
1035 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 * or one of the special values K_NO_WAIT and K_FOREVER.
1037 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001038 * @return Address of the data item if successful; NULL if returned
1039 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001040 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001041extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1042
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001048 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001053 struct k_fifo name \
1054 __in_section(_k_fifo, static, name) = \
1055 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001056
Allan Stephensc98da842016-11-11 15:45:03 -05001057/**
1058 * @} end defgroup fifo_apis
1059 */
1060
1061/**
1062 * @cond INTERNAL_HIDDEN
1063 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001064
1065struct k_lifo {
1066 _wait_q_t wait_q;
1067 void *list;
1068
1069 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
1070};
1071
Allan Stephensc98da842016-11-11 15:45:03 -05001072#define K_LIFO_INITIALIZER(obj) \
1073 { \
1074 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1075 .list = NULL, \
1076 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1077 }
1078
1079/**
1080 * INTERNAL_HIDDEN @endcond
1081 */
1082
1083/**
1084 * @defgroup lifo_apis Lifo APIs
1085 * @ingroup kernel_apis
1086 * @{
1087 */
1088
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001094 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 *
1096 * @return N/A
1097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099
1100/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * This routine adds a data item to @a lifo. A lifo data item must be
1104 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1105 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @note Can be called by ISRs.
1108 *
1109 * @param lifo Address of the lifo.
1110 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
1112 * @return N/A
1113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001114extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115
1116/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * This routine removes a data item from @a lifo in a "last in, first out"
1120 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1123 *
1124 * @param lifo Address of the lifo.
1125 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 * or one of the special values K_NO_WAIT and K_FOREVER.
1127 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001128 * @return Address of the data item if successful; NULL if returned
1129 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1132
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001138 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001140 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001143 struct k_lifo name \
1144 __in_section(_k_lifo, static, name) = \
1145 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001146
Allan Stephensc98da842016-11-11 15:45:03 -05001147/**
1148 * @} end defgroup lifo_apis
1149 */
1150
1151/**
1152 * @cond INTERNAL_HIDDEN
1153 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001154
1155struct k_stack {
1156 _wait_q_t wait_q;
1157 uint32_t *base, *next, *top;
1158
1159 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
1160};
1161
Allan Stephensc98da842016-11-11 15:45:03 -05001162#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1163 { \
1164 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1165 .base = stack_buffer, \
1166 .next = stack_buffer, \
1167 .top = stack_buffer + stack_num_entries, \
1168 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1169 }
1170
1171/**
1172 * INTERNAL_HIDDEN @endcond
1173 */
1174
1175/**
1176 * @defgroup stack_apis Stack APIs
1177 * @ingroup kernel_apis
1178 * @{
1179 */
1180
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181/**
1182 * @brief Initialize a stack.
1183 *
1184 * This routine initializes a stack object, prior to its first use.
1185 *
1186 * @param stack Address of the stack.
1187 * @param buffer Address of array used to hold stacked values.
1188 * @param num_entries Maximum number of values that can be stacked.
1189 *
1190 * @return N/A
1191 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001192extern void k_stack_init(struct k_stack *stack,
1193 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001194
1195/**
1196 * @brief Push an element onto a stack.
1197 *
1198 * This routine adds a 32-bit value @a data to @a stack.
1199 *
1200 * @note Can be called by ISRs.
1201 *
1202 * @param stack Address of the stack.
1203 * @param data Value to push onto the stack.
1204 *
1205 * @return N/A
1206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001207extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001208
1209/**
1210 * @brief Pop an element from a stack.
1211 *
1212 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1213 * manner and stores the value in @a data.
1214 *
1215 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1216 *
1217 * @param stack Address of the stack.
1218 * @param data Address of area to hold the value popped from the stack.
1219 * @param timeout Waiting period to obtain a value (in milliseconds),
1220 * or one of the special values K_NO_WAIT and K_FOREVER.
1221 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001222 * @retval 0 Element popped from stack.
1223 * @retval -EBUSY Returned without waiting.
1224 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001226extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001233 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * @param name Name of the stack.
1236 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001238#define K_STACK_DEFINE(name, stack_num_entries) \
1239 uint32_t __noinit \
1240 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001241 struct k_stack name \
1242 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001243 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1244 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246/**
Allan Stephensc98da842016-11-11 15:45:03 -05001247 * @} end defgroup stack_apis
1248 */
1249
Allan Stephens6bba9b02016-11-16 14:56:54 -05001250struct k_work;
1251
Allan Stephensc98da842016-11-11 15:45:03 -05001252/**
1253 * @defgroup workqueue_apis Workqueue Thread APIs
1254 * @ingroup kernel_apis
1255 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001256 */
1257
Allan Stephens6bba9b02016-11-16 14:56:54 -05001258/**
1259 * @typedef k_work_handler_t
1260 * @brief Work item handler function type.
1261 *
1262 * A work item's handler function is executed by a workqueue's thread
1263 * when the work item is processed by the workqueue.
1264 *
1265 * @param work Address of the work item.
1266 *
1267 * @return N/A
1268 */
1269typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001270
1271/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001272 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001274
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275struct k_work_q {
1276 struct k_fifo fifo;
1277};
1278
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001280 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001281};
1282
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001283struct k_work {
1284 void *_reserved; /* Used by k_fifo implementation. */
1285 k_work_handler_t handler;
1286 atomic_t flags[1];
1287};
1288
Allan Stephens6bba9b02016-11-16 14:56:54 -05001289struct k_delayed_work {
1290 struct k_work work;
1291 struct _timeout timeout;
1292 struct k_work_q *work_q;
1293};
1294
1295extern struct k_work_q k_sys_work_q;
1296
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001297/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001298 * INTERNAL_HIDDEN @endcond
1299 */
1300
1301/**
1302 * @brief Initialize a statically-defined work item.
1303 *
1304 * This macro can be used to initialize a statically-defined workqueue work
1305 * item, prior to its first use. For example,
1306 *
1307 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1308 *
1309 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001310 */
1311#define K_WORK_INITIALIZER(work_handler) \
1312 { \
1313 ._reserved = NULL, \
1314 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001315 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316 }
1317
1318/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001319 * @brief Initialize a work item.
1320 *
1321 * This routine initializes a workqueue work item, prior to its first use.
1322 *
1323 * @param work Address of work item.
1324 * @param handler Function to invoke each time work item is processed.
1325 *
1326 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001327 */
1328static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1329{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001330 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331 work->handler = handler;
1332}
1333
1334/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001335 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001336 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001337 * This routine submits work item @a work to be processed by workqueue
1338 * @a work_q. If the work item is already pending in the workqueue's queue
1339 * as a result of an earlier submission, this routine has no effect on the
1340 * work item. If the work item has already been processed, or is currently
1341 * being processed, its work is considered complete and the work item can be
1342 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001344 * @warning
1345 * A submitted work item must not be modified until it has been processed
1346 * by the workqueue.
1347 *
1348 * @note Can be called by ISRs.
1349 *
1350 * @param work_q Address of workqueue.
1351 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001352 *
1353 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354 */
1355static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1356 struct k_work *work)
1357{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001358 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001359 k_fifo_put(&work_q->fifo, work);
1360 }
1361}
1362
1363/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001364 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001365 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001366 * This routine indicates if work item @a work is pending in a workqueue's
1367 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001369 * @note Can be called by ISRs.
1370 *
1371 * @param work Address of work item.
1372 *
1373 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001374 */
1375static inline int k_work_pending(struct k_work *work)
1376{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001377 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001378}
1379
1380/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001381 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001382 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001383 * This routine starts workqueue @a work_q. The workqueue spawns its work
1384 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001385 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001386 * @param work_q Address of workqueue.
1387 * @param stack Pointer to work queue thread's stack space.
1388 * @param stack_size Size of the work queue thread's stack (in bytes).
1389 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 *
1391 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392 */
Allan Stephens904cf972016-10-07 13:59:23 -05001393extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1394 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001396/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001397 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001399 * This routine initializes a workqueue delayed work item, prior to
1400 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001401 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001402 * @param work Address of delayed work item.
1403 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001404 *
1405 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001407extern void k_delayed_work_init(struct k_delayed_work *work,
1408 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001409
1410/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001411 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001413 * This routine schedules work item @a work to be processed by workqueue
1414 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1415 * an asychronous countdown for the work item and then returns to the caller.
1416 * Only when the countdown completes is the work item actually submitted to
1417 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001419 * Submitting a previously submitted delayed work item that is still
1420 * counting down cancels the existing submission and restarts the countdown
1421 * using the new delay. If the work item is currently pending on the
1422 * workqueue's queue because the countdown has completed it is too late to
1423 * resubmit the item, and resubmission fails without impacting the work item.
1424 * If the work item has already been processed, or is currently being processed,
1425 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001427 * @warning
1428 * A delayed work item must not be modified until it has been processed
1429 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001431 * @note Can be called by ISRs.
1432 *
1433 * @param work_q Address of workqueue.
1434 * @param work Address of delayed work item.
1435 * @param delay Delay before submitting the work item (in milliseconds).
1436 *
1437 * @retval 0 Work item countdown started.
1438 * @retval -EINPROGRESS Work item is already pending.
1439 * @retval -EINVAL Work item is being processed or has completed its work.
1440 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001442extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1443 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001444 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445
1446/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001447 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001448 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001449 * This routine cancels the submission of delayed work item @a work.
1450 * A delayed work item can only be cancelled while its countdown is still
1451 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001452 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001453 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001455 * @param work Address of delayed work item.
1456 *
1457 * @retval 0 Work item countdown cancelled.
1458 * @retval -EINPROGRESS Work item is already pending.
1459 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001461extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464 * @brief Submit a work item to the system workqueue.
1465 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001466 * This routine submits work item @a work to be processed by the system
1467 * workqueue. If the work item is already pending in the workqueue's queue
1468 * as a result of an earlier submission, this routine has no effect on the
1469 * work item. If the work item has already been processed, or is currently
1470 * being processed, its work is considered complete and the work item can be
1471 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001472 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001473 * @warning
1474 * Work items submitted to the system workqueue should avoid using handlers
1475 * that block or yield since this may prevent the system workqueue from
1476 * processing other work items in a timely manner.
1477 *
1478 * @note Can be called by ISRs.
1479 *
1480 * @param work Address of work item.
1481 *
1482 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001483 */
1484static inline void k_work_submit(struct k_work *work)
1485{
1486 k_work_submit_to_queue(&k_sys_work_q, work);
1487}
1488
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001490 * @brief Submit a delayed work item to the system workqueue.
1491 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001492 * This routine schedules work item @a work to be processed by the system
1493 * workqueue after a delay of @a delay milliseconds. The routine initiates
1494 * an asychronous countdown for the work item and then returns to the caller.
1495 * Only when the countdown completes is the work item actually submitted to
1496 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001497 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001498 * Submitting a previously submitted delayed work item that is still
1499 * counting down cancels the existing submission and restarts the countdown
1500 * using the new delay. If the work item is currently pending on the
1501 * workqueue's queue because the countdown has completed it is too late to
1502 * resubmit the item, and resubmission fails without impacting the work item.
1503 * If the work item has already been processed, or is currently being processed,
1504 * its work is considered complete and the work item can be resubmitted.
1505 *
1506 * @warning
1507 * Work items submitted to the system workqueue should avoid using handlers
1508 * that block or yield since this may prevent the system workqueue from
1509 * processing other work items in a timely manner.
1510 *
1511 * @note Can be called by ISRs.
1512 *
1513 * @param work Address of delayed work item.
1514 * @param delay Delay before submitting the work item (in milliseconds).
1515 *
1516 * @retval 0 Work item countdown started.
1517 * @retval -EINPROGRESS Work item is already pending.
1518 * @retval -EINVAL Work item is being processed or has completed its work.
1519 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001520 */
1521static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001522 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001523{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001524 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525}
1526
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001527/**
Allan Stephensc98da842016-11-11 15:45:03 -05001528 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001529 */
1530
Allan Stephensc98da842016-11-11 15:45:03 -05001531/**
1532 * @cond INTERNAL_HIDDEN
1533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001534
1535struct k_mutex {
1536 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001537 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538 uint32_t lock_count;
1539 int owner_orig_prio;
1540#ifdef CONFIG_OBJECT_MONITOR
1541 int num_lock_state_changes;
1542 int num_conflicts;
1543#endif
1544
1545 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1546};
1547
1548#ifdef CONFIG_OBJECT_MONITOR
1549#define _MUTEX_INIT_OBJECT_MONITOR \
1550 .num_lock_state_changes = 0, .num_conflicts = 0,
1551#else
1552#define _MUTEX_INIT_OBJECT_MONITOR
1553#endif
1554
1555#define K_MUTEX_INITIALIZER(obj) \
1556 { \
1557 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1558 .owner = NULL, \
1559 .lock_count = 0, \
1560 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1561 _MUTEX_INIT_OBJECT_MONITOR \
1562 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1563 }
1564
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565/**
Allan Stephensc98da842016-11-11 15:45:03 -05001566 * INTERNAL_HIDDEN @endcond
1567 */
1568
1569/**
1570 * @defgroup mutex_apis Mutex APIs
1571 * @ingroup kernel_apis
1572 * @{
1573 */
1574
1575/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001576 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001579 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001580 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001582 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001583 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001584#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001585 struct k_mutex name \
1586 __in_section(_k_mutex, static, name) = \
1587 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001588
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * Upon completion, the mutex is available and does not have an owner.
1595 *
1596 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001597 *
1598 * @return N/A
1599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001600extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001601
1602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * This routine locks @a mutex. If the mutex is locked by another thread,
1606 * the calling thread waits until the mutex becomes available or until
1607 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * A thread is permitted to lock a mutex it has already locked. The operation
1610 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param mutex Address of the mutex.
1613 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001614 * or one of the special values K_NO_WAIT and K_FOREVER.
1615 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001616 * @retval 0 Mutex locked.
1617 * @retval -EBUSY Returned without waiting.
1618 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001619 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001620extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001621
1622/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001623 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * This routine unlocks @a mutex. The mutex must already be locked by the
1626 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001627 *
1628 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630 * thread.
1631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001633 *
1634 * @return N/A
1635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001636extern void k_mutex_unlock(struct k_mutex *mutex);
1637
Allan Stephensc98da842016-11-11 15:45:03 -05001638/**
1639 * @} end defgroup mutex_apis
1640 */
1641
1642/**
1643 * @cond INTERNAL_HIDDEN
1644 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001645
1646struct k_sem {
1647 _wait_q_t wait_q;
1648 unsigned int count;
1649 unsigned int limit;
1650
1651 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1652};
1653
Allan Stephensc98da842016-11-11 15:45:03 -05001654#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1655 { \
1656 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1657 .count = initial_count, \
1658 .limit = count_limit, \
1659 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1660 }
1661
1662/**
1663 * INTERNAL_HIDDEN @endcond
1664 */
1665
1666/**
1667 * @defgroup semaphore_apis Semaphore APIs
1668 * @ingroup kernel_apis
1669 * @{
1670 */
1671
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001675 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * @param sem Address of the semaphore.
1678 * @param initial_count Initial semaphore count.
1679 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001680 *
1681 * @return N/A
1682 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001683extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1684 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001685
1686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1692 *
1693 * @param sem Address of the semaphore.
1694 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001695 * or one of the special values K_NO_WAIT and K_FOREVER.
1696 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001697 * @retval 0 Semaphore taken.
1698 * @retval -EBUSY Returned without waiting.
1699 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001700 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001701extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001702
1703/**
1704 * @brief Give a semaphore.
1705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * This routine gives @a sem, unless the semaphore is already at its maximum
1707 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001712 *
1713 * @return N/A
1714 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001715extern void k_sem_give(struct k_sem *sem);
1716
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001717/**
1718 * @brief Reset a semaphore's count to zero.
1719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001723 *
1724 * @return N/A
1725 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001726static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727{
1728 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001729}
1730
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001731/**
1732 * @brief Get a semaphore's count.
1733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001739 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001740static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741{
1742 return sem->count;
1743}
1744
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001748 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001749 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001750 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @param name Name of the semaphore.
1753 * @param initial_count Initial semaphore count.
1754 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001755 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001756#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001757 struct k_sem name \
1758 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759 K_SEM_INITIALIZER(name, initial_count, count_limit)
1760
Allan Stephensc98da842016-11-11 15:45:03 -05001761/**
1762 * @} end defgroup semaphore_apis
1763 */
1764
1765/**
1766 * @defgroup alert_apis Alert APIs
1767 * @ingroup kernel_apis
1768 * @{
1769 */
1770
Allan Stephens5eceb852016-11-16 10:16:30 -05001771/**
1772 * @typedef k_alert_handler_t
1773 * @brief Alert handler function type.
1774 *
1775 * An alert's alert handler function is invoked by the system workqueue
1776 * when the alert is signalled. The alert handler function is optional,
1777 * and is only invoked if the alert has been initialized with one.
1778 *
1779 * @param alert Address of the alert.
1780 *
1781 * @return 0 if alert has been consumed; non-zero if alert should pend.
1782 */
1783typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001784
1785/**
1786 * @} end defgroup alert_apis
1787 */
1788
1789/**
1790 * @cond INTERNAL_HIDDEN
1791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001792
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001793#define K_ALERT_DEFAULT NULL
1794#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001796struct k_alert {
1797 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798 atomic_t send_count;
1799 struct k_work work_item;
1800 struct k_sem sem;
1801
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001802 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803};
1804
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001805extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001807#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001808 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001809 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001810 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001811 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001812 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1814 }
1815
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816/**
Allan Stephensc98da842016-11-11 15:45:03 -05001817 * INTERNAL_HIDDEN @endcond
1818 */
1819
1820/**
1821 * @addtogroup alert_apis
1822 * @{
1823 */
1824
1825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001826 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001828 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001830 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * @param name Name of the alert.
1833 * @param alert_handler Action to take when alert is sent. Specify either
1834 * the address of a function to be invoked by the system workqueue
1835 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1836 * K_ALERT_DEFAULT (which causes the alert to pend).
1837 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001838 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001839#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001840 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001841 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001842 K_ALERT_INITIALIZER(name, alert_handler, \
1843 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001844
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001846 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @param alert Address of the alert.
1851 * @param handler Action to take when alert is sent. Specify either the address
1852 * of a function to be invoked by the system workqueue thread,
1853 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1854 * K_ALERT_DEFAULT (which causes the alert to pend).
1855 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 *
1857 * @return N/A
1858 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001859extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1860 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861
1862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001863 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1868 *
1869 * @param alert Address of the alert.
1870 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 * or one of the special values K_NO_WAIT and K_FOREVER.
1872 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001873 * @retval 0 Alert received.
1874 * @retval -EBUSY Returned without waiting.
1875 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001877extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878
1879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * This routine signals @a alert. The action specified for @a alert will
1883 * be taken, which may trigger the execution of an alert handler function
1884 * and/or cause the alert to pend (assuming the alert has not reached its
1885 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * @note Can be called by ISRs.
1888 *
1889 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
1891 * @return N/A
1892 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001893extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001894
1895/**
Allan Stephensc98da842016-11-11 15:45:03 -05001896 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897 */
1898
Allan Stephensc98da842016-11-11 15:45:03 -05001899/**
1900 * @cond INTERNAL_HIDDEN
1901 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
1903struct k_msgq {
1904 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001905 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001906 uint32_t max_msgs;
1907 char *buffer_start;
1908 char *buffer_end;
1909 char *read_ptr;
1910 char *write_ptr;
1911 uint32_t used_msgs;
1912
1913 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1914};
1915
Peter Mitsis1da807e2016-10-06 11:36:59 -04001916#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001917 { \
1918 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001919 .max_msgs = q_max_msgs, \
1920 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001922 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923 .read_ptr = q_buffer, \
1924 .write_ptr = q_buffer, \
1925 .used_msgs = 0, \
1926 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1927 }
1928
Peter Mitsis1da807e2016-10-06 11:36:59 -04001929/**
Allan Stephensc98da842016-11-11 15:45:03 -05001930 * INTERNAL_HIDDEN @endcond
1931 */
1932
1933/**
1934 * @defgroup msgq_apis Message Queue APIs
1935 * @ingroup kernel_apis
1936 * @{
1937 */
1938
1939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1943 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001944 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1945 * message is similarly aligned to this boundary, @a q_msg_size must also be
1946 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * The message queue can be accessed outside the module where it is defined
1949 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001951 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001953 * @param q_name Name of the message queue.
1954 * @param q_msg_size Message size (in bytes).
1955 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001956 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001957 */
1958#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1959 static char __noinit __aligned(q_align) \
1960 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001961 struct k_msgq q_name \
1962 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001963 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1964 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965
Peter Mitsisd7a37502016-10-13 11:37:40 -04001966/**
1967 * @brief Initialize a message queue.
1968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * This routine initializes a message queue object, prior to its first use.
1970 *
Allan Stephensda827222016-11-09 14:23:58 -06001971 * The message queue's ring buffer must contain space for @a max_msgs messages,
1972 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1973 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1974 * that each message is similarly aligned to this boundary, @a q_msg_size
1975 * must also be a multiple of N.
1976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @param q Address of the message queue.
1978 * @param buffer Pointer to ring buffer that holds queued messages.
1979 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001980 * @param max_msgs Maximum number of messages that can be queued.
1981 *
1982 * @return N/A
1983 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001984extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001985 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001986
1987/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001990 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001991 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001992 * @note Can be called by ISRs.
1993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001994 * @param q Address of the message queue.
1995 * @param data Pointer to the message.
1996 * @param timeout Waiting period to add the message (in milliseconds),
1997 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001998 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001999 * @retval 0 Message sent.
2000 * @retval -ENOMSG Returned without waiting or queue purged.
2001 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002002 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002004
2005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * This routine receives a message from message queue @a q in a "first in,
2009 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002010 *
Allan Stephensc98da842016-11-11 15:45:03 -05002011 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002013 * @param q Address of the message queue.
2014 * @param data Address of area to hold the received message.
2015 * @param timeout Waiting period to receive the message (in milliseconds),
2016 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002017 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002018 * @retval 0 Message received.
2019 * @retval -ENOMSG Returned without waiting.
2020 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002021 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002022extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002023
2024/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027 * This routine discards all unreceived messages in a message queue's ring
2028 * buffer. Any threads that are blocked waiting to send a message to the
2029 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002031 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002032 *
2033 * @return N/A
2034 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002035extern void k_msgq_purge(struct k_msgq *q);
2036
Peter Mitsis67be2492016-10-07 11:44:34 -04002037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002040 * This routine returns the number of unused entries in a message queue's
2041 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002043 * @param q Address of the message queue.
2044 *
2045 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002046 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002047static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002048{
2049 return q->max_msgs - q->used_msgs;
2050}
2051
Peter Mitsisd7a37502016-10-13 11:37:40 -04002052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002053 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002055 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057 * @param q Address of the message queue.
2058 *
2059 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002060 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002061static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062{
2063 return q->used_msgs;
2064}
2065
Allan Stephensc98da842016-11-11 15:45:03 -05002066/**
2067 * @} end defgroup msgq_apis
2068 */
2069
2070/**
2071 * @defgroup mem_pool_apis Memory Pool APIs
2072 * @ingroup kernel_apis
2073 * @{
2074 */
2075
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002077 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002078 void *addr_in_pool;
2079 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002080 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081};
2082
Allan Stephensc98da842016-11-11 15:45:03 -05002083/**
2084 * @} end defgroup mem_pool_apis
2085 */
2086
2087/**
2088 * @defgroup mailbox_apis Mailbox APIs
2089 * @ingroup kernel_apis
2090 * @{
2091 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092
2093struct k_mbox_msg {
2094 /** internal use only - needed for legacy API support */
2095 uint32_t _mailbox;
2096 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002097 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098 /** application-defined information value */
2099 uint32_t info;
2100 /** sender's message data buffer */
2101 void *tx_data;
2102 /** internal use only - needed for legacy API support */
2103 void *_rx_data;
2104 /** message data block descriptor */
2105 struct k_mem_block tx_block;
2106 /** source thread id */
2107 k_tid_t rx_source_thread;
2108 /** target thread id */
2109 k_tid_t tx_target_thread;
2110 /** internal use only - thread waiting on send (may be a dummy) */
2111 k_tid_t _syncing_thread;
2112#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2113 /** internal use only - semaphore used during asynchronous send */
2114 struct k_sem *_async_sem;
2115#endif
2116};
2117
Allan Stephensc98da842016-11-11 15:45:03 -05002118/**
2119 * @cond INTERNAL_HIDDEN
2120 */
2121
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122struct k_mbox {
2123 _wait_q_t tx_msg_queue;
2124 _wait_q_t rx_msg_queue;
2125
2126 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
2127};
2128
2129#define K_MBOX_INITIALIZER(obj) \
2130 { \
2131 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2132 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
2133 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2134 }
2135
Peter Mitsis12092702016-10-14 12:57:23 -04002136/**
Allan Stephensc98da842016-11-11 15:45:03 -05002137 * INTERNAL_HIDDEN @endcond
2138 */
2139
2140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002143 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002145 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002148 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002150 struct k_mbox name \
2151 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002152 K_MBOX_INITIALIZER(name) \
2153
Peter Mitsis12092702016-10-14 12:57:23 -04002154/**
2155 * @brief Initialize a mailbox.
2156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002157 * This routine initializes a mailbox object, prior to its first use.
2158 *
2159 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002160 *
2161 * @return N/A
2162 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163extern void k_mbox_init(struct k_mbox *mbox);
2164
Peter Mitsis12092702016-10-14 12:57:23 -04002165/**
2166 * @brief Send a mailbox message in a synchronous manner.
2167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * This routine sends a message to @a mbox and waits for a receiver to both
2169 * receive and process it. The message data may be in a buffer, in a memory
2170 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * @param mbox Address of the mailbox.
2173 * @param tx_msg Address of the transmit message descriptor.
2174 * @param timeout Waiting period for the message to be received (in
2175 * milliseconds), or one of the special values K_NO_WAIT
2176 * and K_FOREVER. Once the message has been received,
2177 * this routine waits as long as necessary for the message
2178 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002179 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002180 * @retval 0 Message sent.
2181 * @retval -ENOMSG Returned without waiting.
2182 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002183 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002184extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002186
Peter Mitsis12092702016-10-14 12:57:23 -04002187/**
2188 * @brief Send a mailbox message in an asynchronous manner.
2189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002190 * This routine sends a message to @a mbox without waiting for a receiver
2191 * to process it. The message data may be in a buffer, in a memory pool block,
2192 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2193 * will be given when the message has been both received and completely
2194 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * @param mbox Address of the mailbox.
2197 * @param tx_msg Address of the transmit message descriptor.
2198 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002199 *
2200 * @return N/A
2201 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002202extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203 struct k_sem *sem);
2204
Peter Mitsis12092702016-10-14 12:57:23 -04002205/**
2206 * @brief Receive a mailbox message.
2207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002208 * This routine receives a message from @a mbox, then optionally retrieves
2209 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002211 * @param mbox Address of the mailbox.
2212 * @param rx_msg Address of the receive message descriptor.
2213 * @param buffer Address of the buffer to receive data, or NULL to defer data
2214 * retrieval and message disposal until later.
2215 * @param timeout Waiting period for a message to be received (in
2216 * milliseconds), or one of the special values K_NO_WAIT
2217 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002218 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002219 * @retval 0 Message received.
2220 * @retval -ENOMSG Returned without waiting.
2221 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002222 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002223extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002225
2226/**
2227 * @brief Retrieve mailbox message data into a buffer.
2228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002229 * This routine completes the processing of a received message by retrieving
2230 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002231 *
2232 * Alternatively, this routine can be used to dispose of a received message
2233 * without retrieving its data.
2234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * @param rx_msg Address of the receive message descriptor.
2236 * @param buffer Address of the buffer to receive data, or NULL to discard
2237 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002238 *
2239 * @return N/A
2240 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002241extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002242
2243/**
2244 * @brief Retrieve mailbox message data into a memory pool block.
2245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002246 * This routine completes the processing of a received message by retrieving
2247 * its data into a memory pool block, then disposing of the message.
2248 * The memory pool block that results from successful retrieval must be
2249 * returned to the pool once the data has been processed, even in cases
2250 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002251 *
2252 * Alternatively, this routine can be used to dispose of a received message
2253 * without retrieving its data. In this case there is no need to return a
2254 * memory pool block to the pool.
2255 *
2256 * This routine allocates a new memory pool block for the data only if the
2257 * data is not already in one. If a new block cannot be allocated, the routine
2258 * returns a failure code and the received message is left unchanged. This
2259 * permits the caller to reattempt data retrieval at a later time or to dispose
2260 * of the received message without retrieving its data.
2261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * @param rx_msg Address of a receive message descriptor.
2263 * @param pool Address of memory pool, or NULL to discard data.
2264 * @param block Address of the area to hold memory pool block info.
2265 * @param timeout Waiting period to wait for a memory pool block (in
2266 * milliseconds), or one of the special values K_NO_WAIT
2267 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002268 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002269 * @retval 0 Data retrieved.
2270 * @retval -ENOMEM Returned without waiting.
2271 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002272 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002273extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002274 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275 struct k_mem_block *block, int32_t timeout);
2276
Allan Stephensc98da842016-11-11 15:45:03 -05002277/**
2278 * @} end defgroup mailbox_apis
2279 */
2280
2281/**
2282 * @cond INTERNAL_HIDDEN
2283 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284
2285struct k_pipe {
2286 unsigned char *buffer; /* Pipe buffer: may be NULL */
2287 size_t size; /* Buffer size */
2288 size_t bytes_used; /* # bytes used in buffer */
2289 size_t read_index; /* Where in buffer to read from */
2290 size_t write_index; /* Where in buffer to write */
2291
2292 struct {
2293 _wait_q_t readers; /* Reader wait queue */
2294 _wait_q_t writers; /* Writer wait queue */
2295 } wait_q;
2296
2297 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2298};
2299
Peter Mitsise5d9c582016-10-14 14:44:57 -04002300#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 { \
2302 .buffer = pipe_buffer, \
2303 .size = pipe_buffer_size, \
2304 .bytes_used = 0, \
2305 .read_index = 0, \
2306 .write_index = 0, \
2307 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2308 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2309 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2310 }
2311
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002312/**
Allan Stephensc98da842016-11-11 15:45:03 -05002313 * INTERNAL_HIDDEN @endcond
2314 */
2315
2316/**
2317 * @defgroup pipe_apis Pipe APIs
2318 * @ingroup kernel_apis
2319 * @{
2320 */
2321
2322/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002327 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * @param name Name of the pipe.
2330 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2331 * or zero if no ring buffer is used.
2332 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002334#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2335 static unsigned char __noinit __aligned(pipe_align) \
2336 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002337 struct k_pipe name \
2338 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002339 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * This routine initializes a pipe object, prior to its first use.
2345 *
2346 * @param pipe Address of the pipe.
2347 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2348 * is used.
2349 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2350 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351 *
2352 * @return N/A
2353 */
2354extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2355 size_t size);
2356
2357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param pipe Address of the pipe.
2363 * @param data Address of data to write.
2364 * @param bytes_to_write Size of data (in bytes).
2365 * @param bytes_written Address of area to hold the number of bytes written.
2366 * @param min_xfer Minimum number of bytes to write.
2367 * @param timeout Waiting period to wait for the data to be written (in
2368 * milliseconds), or one of the special values K_NO_WAIT
2369 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002371 * @retval 0 At least @a min_xfer bytes of data were written.
2372 * @retval -EIO Returned without waiting; zero data bytes were written.
2373 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002376extern int k_pipe_put(struct k_pipe *pipe, void *data,
2377 size_t bytes_to_write, size_t *bytes_written,
2378 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379
2380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * @param pipe Address of the pipe.
2386 * @param data Address to place the data read from pipe.
2387 * @param bytes_to_read Maximum number of data bytes to read.
2388 * @param bytes_read Address of area to hold the number of bytes read.
2389 * @param min_xfer Minimum number of data bytes to read.
2390 * @param timeout Waiting period to wait for the data to be read (in
2391 * milliseconds), or one of the special values K_NO_WAIT
2392 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002394 * @retval 0 At least @a min_xfer bytes of data were read.
2395 * @retval -EIO Returned without waiting; zero data bytes were read.
2396 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002399extern int k_pipe_get(struct k_pipe *pipe, void *data,
2400 size_t bytes_to_read, size_t *bytes_read,
2401 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402
2403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002406 * This routine writes the data contained in a memory block to @a pipe.
2407 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411 * @param block Memory block containing data to send
2412 * @param size Number of data bytes in memory block to send
2413 * @param sem Semaphore to signal upon completion (else NULL)
2414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416 */
2417extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2418 size_t size, struct k_sem *sem);
2419
2420/**
Allan Stephensc98da842016-11-11 15:45:03 -05002421 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422 */
2423
Allan Stephensc98da842016-11-11 15:45:03 -05002424/**
2425 * @cond INTERNAL_HIDDEN
2426 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002428struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002430 uint32_t num_blocks;
2431 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432 char *buffer;
2433 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002434 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002436 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437};
2438
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002439#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2440 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441 { \
2442 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002443 .num_blocks = slab_num_blocks, \
2444 .block_size = slab_block_size, \
2445 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446 .free_list = NULL, \
2447 .num_used = 0, \
2448 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2449 }
2450
Peter Mitsis578f9112016-10-07 13:50:31 -04002451/**
Allan Stephensc98da842016-11-11 15:45:03 -05002452 * INTERNAL_HIDDEN @endcond
2453 */
2454
2455/**
2456 * @defgroup mem_slab_apis Memory Slab APIs
2457 * @ingroup kernel_apis
2458 * @{
2459 */
2460
2461/**
Allan Stephensda827222016-11-09 14:23:58 -06002462 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002463 *
Allan Stephensda827222016-11-09 14:23:58 -06002464 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002466 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2467 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002468 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002469 *
Allan Stephensda827222016-11-09 14:23:58 -06002470 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002473 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * @param name Name of the memory slab.
2476 * @param slab_block_size Size of each memory block (in bytes).
2477 * @param slab_num_blocks Number memory blocks.
2478 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002479 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002480#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2481 char __noinit __aligned(slab_align) \
2482 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2483 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002484 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002485 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2486 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002488/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002489 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002492 *
Allan Stephensda827222016-11-09 14:23:58 -06002493 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2494 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2495 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2496 * To ensure that each memory block is similarly aligned to this boundary,
2497 * @a slab_block_size must also be a multiple of N.
2498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @param slab Address of the memory slab.
2500 * @param buffer Pointer to buffer used for the memory blocks.
2501 * @param block_size Size of each memory block (in bytes).
2502 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002503 *
2504 * @return N/A
2505 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002506extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002507 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002508
2509/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * @param slab Address of the memory slab.
2515 * @param mem Pointer to block address area.
2516 * @param timeout Maximum time to wait for operation to complete
2517 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2518 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002519 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002520 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002522 * @retval -ENOMEM Returned without waiting.
2523 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002524 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002525extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2526 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002527
2528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * This routine releases a previously allocated memory block back to its
2532 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @param slab Address of the memory slab.
2535 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002536 *
2537 * @return N/A
2538 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002539extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002541/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * This routine gets the number of memory blocks that are currently
2545 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002550 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002551static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002553 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554}
2555
Peter Mitsisc001aa82016-10-13 13:53:37 -04002556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * This routine gets the number of memory blocks that are currently
2560 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002565 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002566static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002567{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002568 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002569}
2570
Allan Stephensc98da842016-11-11 15:45:03 -05002571/**
2572 * @} end defgroup mem_slab_apis
2573 */
2574
2575/**
2576 * @cond INTERNAL_HIDDEN
2577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002579/*
2580 * Memory pool requires a buffer and two arrays of structures for the
2581 * memory block accounting:
2582 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2583 * status of four blocks of memory.
2584 */
2585struct k_mem_pool_quad_block {
2586 char *mem_blocks; /* pointer to the first of four memory blocks */
2587 uint32_t mem_status; /* four bits. If bit is set, memory block is
2588 allocated */
2589};
2590/*
2591 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2592 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2593 * block size is 4 times less than the previous one and thus requires 4 times
2594 * bigger array of k_mem_pool_quad_block structures to keep track of the
2595 * memory blocks.
2596 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002598/*
2599 * The array of k_mem_pool_block_set keeps the information of each array of
2600 * k_mem_pool_quad_block structures
2601 */
2602struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002603 size_t block_size; /* memory block size */
2604 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002605 struct k_mem_pool_quad_block *quad_block;
2606 int count;
2607};
2608
2609/* Memory pool descriptor */
2610struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002611 size_t max_block_size;
2612 size_t min_block_size;
2613 uint32_t nr_of_maxblocks;
2614 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002615 struct k_mem_pool_block_set *block_set;
2616 char *bufblock;
2617 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2619};
2620
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002621#ifdef CONFIG_ARM
2622#define _SECTION_TYPE_SIGN "%"
2623#else
2624#define _SECTION_TYPE_SIGN "@"
2625#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002626
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002627/*
2628 * Static memory pool initialization
2629 */
Allan Stephensc98da842016-11-11 15:45:03 -05002630
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002631/*
2632 * Use .altmacro to be able to recalculate values and pass them as string
2633 * arguments when calling assembler macros resursively
2634 */
2635__asm__(".altmacro\n\t");
2636
2637/*
2638 * Recursively calls a macro
2639 * The followig global symbols need to be initialized:
2640 * __memory_pool_max_block_size - maximal size of the memory block
2641 * __memory_pool_min_block_size - minimal size of the memory block
2642 * Notes:
2643 * Global symbols are used due the fact that assembler macro allows only
2644 * one argument be passed with the % conversion
2645 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2646 * is used instead of / 4.
2647 * n_max argument needs to go first in the invoked macro, as some
2648 * assemblers concatenate \name and %(\n_max * 4) arguments
2649 * if \name goes first
2650 */
2651__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2652 ".ifge __memory_pool_max_block_size >> 2 -"
2653 " __memory_pool_min_block_size\n\t\t"
2654 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2655 "\\macro_name %(\\n_max * 4) \\name\n\t"
2656 ".endif\n\t"
2657 ".endm\n");
2658
2659/*
2660 * Build quad blocks
2661 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2662 * structures and recursively calls itself for the next array, 4 times
2663 * larger.
2664 * The followig global symbols need to be initialized:
2665 * __memory_pool_max_block_size - maximal size of the memory block
2666 * __memory_pool_min_block_size - minimal size of the memory block
2667 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2668 */
2669__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002670 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002671 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2672 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2673 ".if \\n_max % 4\n\t\t"
2674 ".skip __memory_pool_quad_block_size\n\t"
2675 ".endif\n\t"
2676 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2677 ".endm\n");
2678
2679/*
2680 * Build block sets and initialize them
2681 * Macro initializes the k_mem_pool_block_set structure and
2682 * recursively calls itself for the next one.
2683 * The followig global symbols need to be initialized:
2684 * __memory_pool_max_block_size - maximal size of the memory block
2685 * __memory_pool_min_block_size - minimal size of the memory block
2686 * __memory_pool_block_set_count, the number of the elements in the
2687 * block set array must be set to 0. Macro calculates it's real
2688 * value.
2689 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2690 * structures, _build_quad_blocks must be called prior it.
2691 */
2692__asm__(".macro _build_block_set n_max, name\n\t"
2693 ".int __memory_pool_max_block_size\n\t" /* block_size */
2694 ".if \\n_max % 4\n\t\t"
2695 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2696 ".else\n\t\t"
2697 ".int \\n_max >> 2\n\t"
2698 ".endif\n\t"
2699 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2700 ".int 0\n\t" /* count */
2701 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2702 "__do_recurse _build_block_set \\name \\n_max\n\t"
2703 ".endm\n");
2704
2705/*
2706 * Build a memory pool structure and initialize it
2707 * Macro uses __memory_pool_block_set_count global symbol,
2708 * block set addresses and buffer address, it may be called only after
2709 * _build_block_set
2710 */
2711__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002712 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002713 _SECTION_TYPE_SIGN "progbits\n\t"
2714 ".globl \\name\n\t"
2715 "\\name:\n\t"
2716 ".int \\max_size\n\t" /* max_block_size */
2717 ".int \\min_size\n\t" /* min_block_size */
2718 ".int \\n_max\n\t" /* nr_of_maxblocks */
2719 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2720 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2721 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2722 ".int 0\n\t" /* wait_q->head */
2723 ".int 0\n\t" /* wait_q->next */
2724 ".popsection\n\t"
2725 ".endm\n");
2726
2727#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2728 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2729 _SECTION_TYPE_SIGN "progbits\n\t"); \
2730 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2731 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2732 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2733 STRINGIFY(name) "\n\t"); \
2734 __asm__(".popsection\n\t")
2735
2736#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2737 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2738 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2739 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2740 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002741 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002742 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2743 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2744 STRINGIFY(name) "\n\t"); \
2745 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2746 __asm__(".int __memory_pool_block_set_count\n\t"); \
2747 __asm__(".popsection\n\t"); \
2748 extern uint32_t _mem_pool_block_set_count_##name; \
2749 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2750
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002751#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2752 char __noinit __aligned(align) \
2753 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002754
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002755/*
2756 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2757 * to __memory_pool_quad_block_size absolute symbol.
2758 * This function does not get called, but compiler calculates the value and
2759 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2760 */
2761static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2762{
2763 __asm__(".globl __memory_pool_quad_block_size\n\t"
2764#ifdef CONFIG_NIOS2
2765 "__memory_pool_quad_block_size = %0\n\t"
2766#else
2767 "__memory_pool_quad_block_size = %c0\n\t"
2768#endif
2769 :
2770 : "n"(sizeof(struct k_mem_pool_quad_block)));
2771}
2772
2773/**
Allan Stephensc98da842016-11-11 15:45:03 -05002774 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002775 */
2776
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002777/**
Allan Stephensc98da842016-11-11 15:45:03 -05002778 * @addtogroup mem_pool_apis
2779 * @{
2780 */
2781
2782/**
2783 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002785 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2786 * long. The memory pool allows blocks to be repeatedly partitioned into
2787 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2788 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002789 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002791 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 * If the pool is to be accessed outside the module where it is defined, it
2793 * can be declared via
2794 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002795 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002797 * @param name Name of the memory pool.
2798 * @param min_size Size of the smallest blocks in the pool (in bytes).
2799 * @param max_size Size of the largest blocks in the pool (in bytes).
2800 * @param n_max Number of maximum sized blocks in the pool.
2801 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002802 */
2803#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002804 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2805 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002806 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002807 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2808 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2809 extern struct k_mem_pool name
2810
Peter Mitsis937042c2016-10-13 13:18:26 -04002811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @param pool Address of the memory pool.
2817 * @param block Pointer to block descriptor for the allocated memory.
2818 * @param size Amount of memory to allocate (in bytes).
2819 * @param timeout Maximum time to wait for operation to complete
2820 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2821 * or K_FOREVER to wait as long as necessary.
2822 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002823 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002825 * @retval -ENOMEM Returned without waiting.
2826 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002827 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002828extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002829 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002830
2831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002832 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * This routine releases a previously allocated memory block back to its
2835 * memory pool.
2836 *
2837 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002838 *
2839 * @return N/A
2840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002842
2843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * This routine instructs a memory pool to concatenate unused memory blocks
2847 * into larger blocks wherever possible. Manually defragmenting the memory
2848 * pool may speed up future allocations of memory blocks by eliminating the
2849 * need for the memory pool to perform an automatic partial defragmentation.
2850 *
2851 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002852 *
2853 * @return N/A
2854 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002855extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002856
2857/**
Allan Stephensc98da842016-11-11 15:45:03 -05002858 * @} end addtogroup mem_pool_apis
2859 */
2860
2861/**
2862 * @defgroup heap_apis Heap Memory Pool APIs
2863 * @ingroup kernel_apis
2864 * @{
2865 */
2866
2867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002871 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002876 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002877extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002878
2879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002881 *
2882 * This routine provides traditional free() semantics. The memory being
2883 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002886 *
2887 * @return N/A
2888 */
2889extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002890
Allan Stephensc98da842016-11-11 15:45:03 -05002891/**
2892 * @} end defgroup heap_apis
2893 */
2894
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002895/*
2896 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2897 * hook into the device subsystem, which itself uses nanokernel semaphores,
2898 * and thus currently requires the definition of nano_sem.
2899 */
2900#include <legacy.h>
2901#include <arch/cpu.h>
2902
2903/*
2904 * private APIs that are utilized by one or more public APIs
2905 */
2906
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002907extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002909extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910
2911#ifdef __cplusplus
2912}
2913#endif
2914
Andrew Boiee004dec2016-11-07 09:01:19 -08002915#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2916/*
2917 * Define new and delete operators.
2918 * At this moment, the operators do nothing since objects are supposed
2919 * to be statically allocated.
2920 */
2921inline void operator delete(void *ptr)
2922{
2923 (void)ptr;
2924}
2925
2926inline void operator delete[](void *ptr)
2927{
2928 (void)ptr;
2929}
2930
2931inline void *operator new(size_t size)
2932{
2933 (void)size;
2934 return NULL;
2935}
2936
2937inline void *operator new[](size_t size)
2938{
2939 (void)size;
2940 return NULL;
2941}
2942
2943/* Placement versions of operator new and delete */
2944inline void operator delete(void *ptr1, void *ptr2)
2945{
2946 (void)ptr1;
2947 (void)ptr2;
2948}
2949
2950inline void operator delete[](void *ptr1, void *ptr2)
2951{
2952 (void)ptr1;
2953 (void)ptr2;
2954}
2955
2956inline void *operator new(size_t size, void *ptr)
2957{
2958 (void)size;
2959 return ptr;
2960}
2961
2962inline void *operator new[](size_t size, void *ptr)
2963{
2964 (void)size;
2965 return ptr;
2966}
2967
2968#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2969
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970#endif /* _kernel__h_ */