blob: d49f8789d38e1376e0adde7a8b568183615bb984 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
Anas Nashif61f4b242016-11-18 10:53:59 -050040#ifdef CONFIG_KERNEL_DEBUG
41#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040042#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
43#else
44#define K_DEBUG(fmt, ...)
45#endif
46
47#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
48#define K_PRIO_PREEMPT(x) (x)
49
Benjamin Walsh456c6da2016-09-02 18:55:39 -040050#define K_ANY NULL
51#define K_END NULL
52
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#if CONFIG_NUM_COOP_PRIORITIES > 0
54#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
55#else
56#define K_HIGHEST_THREAD_PRIO 0
57#endif
58
59#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
60#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
61#else
62#define K_LOWEST_THREAD_PRIO -1
63#endif
64
Benjamin Walshfab8d922016-11-08 15:36:36 -050065#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
68#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
69
70typedef sys_dlist_t _wait_q_t;
71
72#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
73#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
74#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
75#else
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
77#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
78#endif
79
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050080#define tcs k_thread
81struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082struct k_mutex;
83struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040084struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040091struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040092struct k_mem_pool;
93struct k_timer;
94
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040095typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097enum execution_context_types {
98 K_ISR = 0,
99 K_COOP_THREAD,
100 K_PREEMPT_THREAD,
101};
102
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400103/**
Allan Stephensc98da842016-11-11 15:45:03 -0500104 * @defgroup thread_apis Thread APIs
105 * @ingroup kernel_apis
106 * @{
107 */
108
109/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500110 * @typedef k_thread_entry_t
111 * @brief Thread entry point function type.
112 *
113 * A thread's entry point function is invoked when the thread starts executing.
114 * Up to 3 argument values can be passed to the function.
115 *
116 * The thread terminates execution permanently if the entry point function
117 * returns. The thread is responsible for releasing any shared resources
118 * it may own (such as mutexes and dynamically allocated memory), prior to
119 * returning.
120 *
121 * @param p1 First argument.
122 * @param p2 Second argument.
123 * @param p3 Third argument.
124 *
125 * @return N/A
126 */
127typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
128
129/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500130 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500132 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500134 * The new thread may be scheduled for immediate execution or a delayed start.
135 * If the newly spawned thread does not have a delayed start the kernel
136 * scheduler may preempt the current thread to allow the new thread to
137 * execute.
138 *
139 * Thread options are architecture-specific, and can include K_ESSENTIAL,
140 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
141 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400142 *
143 * @param stack Pointer to the stack space.
144 * @param stack_size Stack size in bytes.
145 * @param entry Thread entry function.
146 * @param p1 1st entry point parameter.
147 * @param p2 2nd entry point parameter.
148 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500149 * @param prio Thread priority.
150 * @param options Thread options.
151 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500153 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500155extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500156 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500158 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500161 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400162 *
Allan Stephensc98da842016-11-11 15:45:03 -0500163 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500164 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
168 * @return N/A
169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400170extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400171
172/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500173 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400174 *
175 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500176 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400177 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 * @return N/A
179 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400180extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400181
182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500183 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500185 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400186 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500187 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 *
189 * @return N/A
190 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400191extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400192
193/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500194 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * If @a thread is not currently sleeping, the routine has no effect.
199 *
200 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 *
202 * @return N/A
203 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400205
206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500207 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500209 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400211extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212
213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500214 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500216 * This routine prevents @a thread from executing if it has not yet started
217 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @param thread ID of thread to cancel.
220 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500221 * @retval 0 Thread spawning cancelled.
222 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400224extern int k_thread_cancel(k_tid_t thread);
225
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400226/**
Allan Stephensc98da842016-11-11 15:45:03 -0500227 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * This routine permanently stops execution of @a thread. The thread is taken
230 * off all kernel queues it is part of (i.e. the ready queue, the timeout
231 * queue, or a kernel object wait queue). However, any kernel resources the
232 * thread might currently own (such as mutexes or memory blocks) are not
233 * released. It is the responsibility of the caller of this routine to ensure
234 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500236 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400237 *
238 * @return N/A
239 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400240extern void k_thread_abort(k_tid_t thread);
241
Allan Stephensc98da842016-11-11 15:45:03 -0500242/**
243 * @cond INTERNAL_HIDDEN
244 */
245
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400246#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400247#define _THREAD_TIMEOUT_INIT(obj) \
248 (obj).nano_timeout = { \
249 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400250 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400251 .wait_q = NULL, \
252 .delta_ticks_from_prev = -1, \
253 },
254#else
255#define _THREAD_TIMEOUT_INIT(obj)
256#endif
257
258#ifdef CONFIG_ERRNO
259#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
260#else
261#define _THREAD_ERRNO_INIT(obj)
262#endif
263
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400264struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400265 union {
266 char *init_stack;
267 struct k_thread *thread;
268 };
269 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500270 void (*init_entry)(void *, void *, void *);
271 void *init_p1;
272 void *init_p2;
273 void *init_p3;
274 int init_prio;
275 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400276 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500277 void (*init_abort)(void);
278 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400279};
280
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400281#define _THREAD_INITIALIZER(stack, stack_size, \
282 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500283 prio, options, delay, abort, groups) \
284 { \
285 .init_stack = (stack), \
286 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400287 .init_entry = (void (*)(void *, void *, void *))entry, \
288 .init_p1 = (void *)p1, \
289 .init_p2 = (void *)p2, \
290 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500291 .init_prio = (prio), \
292 .init_options = (options), \
293 .init_delay = (delay), \
294 .init_abort = (abort), \
295 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400296 }
297
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400298/**
Allan Stephensc98da842016-11-11 15:45:03 -0500299 * INTERNAL_HIDDEN @endcond
300 */
301
302/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500303 * @brief Statically define and initialize a thread.
304 *
305 * The thread may be scheduled for immediate execution or a delayed start.
306 *
307 * Thread options are architecture-specific, and can include K_ESSENTIAL,
308 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
309 * them using "|" (the logical OR operator).
310 *
311 * The ID of the thread can be accessed using:
312 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500313 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500314 *
315 * @param name Name of the thread.
316 * @param stack_size Stack size in bytes.
317 * @param entry Thread entry function.
318 * @param p1 1st entry point parameter.
319 * @param p2 2nd entry point parameter.
320 * @param p3 3rd entry point parameter.
321 * @param prio Thread priority.
322 * @param options Thread options.
323 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400324 *
325 * @internal It has been observed that the x86 compiler by default aligns
326 * these _static_thread_data structures to 32-byte boundaries, thereby
327 * wasting space. To work around this, force a 4-byte alignment.
328 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500329#define K_THREAD_DEFINE(name, stack_size, \
330 entry, p1, p2, p3, \
331 prio, options, delay) \
332 char __noinit __stack _k_thread_obj_##name[stack_size]; \
333 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500334 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500335 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
336 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500337 NULL, 0); \
338 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400339
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500341 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500343 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500345 * @param thread ID of thread whose priority is needed.
346 *
347 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400348 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500349extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400350
351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500352 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500354 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355 *
356 * Rescheduling can occur immediately depending on the priority @a thread is
357 * set to:
358 *
359 * - If its priority is raised above the priority of the caller of this
360 * function, and the caller is preemptible, @a thread will be scheduled in.
361 *
362 * - If the caller operates on itself, it lowers its priority below that of
363 * other threads in the system, and the caller is preemptible, the thread of
364 * highest priority will be scheduled in.
365 *
366 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
367 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
368 * highest priority.
369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500370 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400371 * @param prio New priority.
372 *
373 * @warning Changing the priority of a thread currently involved in mutex
374 * priority inheritance may result in undefined behavior.
375 *
376 * @return N/A
377 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378extern void k_thread_priority_set(k_tid_t thread, int prio);
379
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500383 * This routine prevents the kernel scheduler from making @a thread the
384 * current thread. All other internal operations on @a thread are still
385 * performed; for example, any timeout it is waiting on keeps ticking,
386 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * If @a thread is already suspended, the routine has no effect.
389 *
390 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400391 *
392 * @return N/A
393 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400394extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400395
396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500399 * This routine allows the kernel scheduler to make @a thread the current
400 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * If @a thread is not currently suspended, the routine has no effect.
403 *
404 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 *
406 * @return N/A
407 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400408extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400409
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500411 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500413 * This routine specifies how the scheduler will perform time slicing of
414 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500416 * To enable time slicing, @a slice must be non-zero. The scheduler
417 * ensures that no thread runs for more than the specified time limit
418 * before other threads of that priority are given a chance to execute.
419 * Any thread whose priority is higher than @a prio is exempted, and may
420 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500422 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 * execute. Once the scheduler selects a thread for execution, there is no
424 * minimum guaranteed time the thread will execute before threads of greater or
425 * equal priority are scheduled.
426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500427 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400428 * for execution, this routine has no effect; the thread is immediately
429 * rescheduled after the slice period expires.
430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * To disable timeslicing, set both @a slice and @a prio to zero.
432 *
433 * @param slice Maximum time slice length (in milliseconds).
434 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
436 * @return N/A
437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400438extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400439
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400440/**
Allan Stephensc98da842016-11-11 15:45:03 -0500441 * @} end defgroup thread_apis
442 */
443
444/**
445 * @addtogroup isr_apis
446 * @{
447 */
448
449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500450 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
Allan Stephensc98da842016-11-11 15:45:03 -0500452 * This routine allows the caller to customize its actions, depending on
453 * whether it is a thread or an ISR.
454 *
455 * @note Can be called by ISRs.
456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @return 0 if invoked by a thread.
458 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500460extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461
Benjamin Walsh445830d2016-11-10 15:54:27 -0500462/**
463 * @brief Determine if code is running in a preemptible thread.
464 *
Allan Stephensc98da842016-11-11 15:45:03 -0500465 * This routine allows the caller to customize its actions, depending on
466 * whether it can be preempted by another thread. The routine returns a 'true'
467 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500468 *
Allan Stephensc98da842016-11-11 15:45:03 -0500469 * - The code is running in a thread, not at ISR.
470 * - The thread's priority is in the preemptible range.
471 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500472 *
Allan Stephensc98da842016-11-11 15:45:03 -0500473 * @note Can be called by ISRs.
474 *
475 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500476 * @return Non-zero if invoked by a preemptible thread.
477 */
478extern int k_is_preempt_thread(void);
479
Allan Stephensc98da842016-11-11 15:45:03 -0500480/**
481 * @} end addtogroup isr_apis
482 */
483
484/**
485 * @addtogroup thread_apis
486 * @{
487 */
488
489/**
490 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500491 *
Allan Stephensc98da842016-11-11 15:45:03 -0500492 * This routine prevents the current thread from being preempted by another
493 * thread by instructing the scheduler to treat it as a cooperative thread.
494 * If the thread subsequently performs an operation that makes it unready,
495 * it will be context switched out in the normal manner. When the thread
496 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500497 *
Allan Stephensc98da842016-11-11 15:45:03 -0500498 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500499 *
Allan Stephensc98da842016-11-11 15:45:03 -0500500 * @note k_sched_lock() and k_sched_unlock() should normally be used
501 * when the operation being performed can be safely interrupted by ISRs.
502 * However, if the amount of processing involved is very small, better
503 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500504 *
505 * @return N/A
506 */
507extern void k_sched_lock(void);
508
Allan Stephensc98da842016-11-11 15:45:03 -0500509/**
510 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500511 *
Allan Stephensc98da842016-11-11 15:45:03 -0500512 * This routine reverses the effect of a previous call to k_sched_lock().
513 * A thread must call the routine once for each time it called k_sched_lock()
514 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500515 *
516 * @return N/A
517 */
518extern void k_sched_unlock(void);
519
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500521 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500523 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500525 * Custom data is not used by the kernel itself, and is freely available
526 * for a thread to use as it sees fit. It can be used as a framework
527 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
531 * @return N/A
532 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534
535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500536 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500538 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500540 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400542extern void *k_thread_custom_data_get(void);
543
544/**
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * @} end addtogroup thread_apis
546 */
547
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400548#include <sys_clock.h>
549
Allan Stephensc2f15a42016-11-17 12:24:22 -0500550/**
551 * @addtogroup clock_apis
552 * @{
553 */
554
555/**
556 * @brief Generate null timeout delay.
557 *
558 * This macro generates a timeout delay that that instructs a kernel API
559 * not to wait if the requested operation cannot be performed immediately.
560 *
561 * @return Timeout delay value.
562 */
563#define K_NO_WAIT 0
564
565/**
566 * @brief Generate timeout delay from milliseconds.
567 *
568 * This macro generates a timeout delay that that instructs a kernel API
569 * to wait up to @a ms milliseconds to perform the requested operation.
570 *
571 * @param ms Duration in milliseconds.
572 *
573 * @return Timeout delay value.
574 */
Johan Hedberg14471692016-11-13 10:52:15 +0200575#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500576
577/**
578 * @brief Generate timeout delay from seconds.
579 *
580 * This macro generates a timeout delay that that instructs a kernel API
581 * to wait up to @a s seconds to perform the requested operation.
582 *
583 * @param s Duration in seconds.
584 *
585 * @return Timeout delay value.
586 */
Johan Hedberg14471692016-11-13 10:52:15 +0200587#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500588
589/**
590 * @brief Generate timeout delay from minutes.
591 *
592 * This macro generates a timeout delay that that instructs a kernel API
593 * to wait up to @a m minutes to perform the requested operation.
594 *
595 * @param m Duration in minutes.
596 *
597 * @return Timeout delay value.
598 */
Johan Hedberg14471692016-11-13 10:52:15 +0200599#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500600
601/**
602 * @brief Generate timeout delay from hours.
603 *
604 * This macro generates a timeout delay that that instructs a kernel API
605 * to wait up to @a h hours to perform the requested operation.
606 *
607 * @param h Duration in hours.
608 *
609 * @return Timeout delay value.
610 */
Johan Hedberg14471692016-11-13 10:52:15 +0200611#define K_HOURS(h) K_MINUTES((h) * 60)
612
Allan Stephensc98da842016-11-11 15:45:03 -0500613/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500614 * @brief Generate infinite timeout delay.
615 *
616 * This macro generates a timeout delay that that instructs a kernel API
617 * to wait as long as necessary to perform the requested operation.
618 *
619 * @return Timeout delay value.
620 */
621#define K_FOREVER (-1)
622
623/**
624 * @} end addtogroup clock_apis
625 */
626
627/**
Allan Stephensc98da842016-11-11 15:45:03 -0500628 * @cond INTERNAL_HIDDEN
629 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400630
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500631/* added tick needed to account for tick in progress */
632#define _TICK_ALIGN 1
633
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400634static int64_t __ticks_to_ms(int64_t ticks)
635{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400636#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400637 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400638#else
639 __ASSERT(ticks == 0, "");
640 return 0;
641#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400642}
643
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400644/* timeouts */
645
646struct _timeout;
647typedef void (*_timeout_func_t)(struct _timeout *t);
648
649struct _timeout {
650 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400651 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400652 sys_dlist_t *wait_q;
653 int32_t delta_ticks_from_prev;
654 _timeout_func_t func;
655};
656
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,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001394 size_t stack_size, int 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 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001697 * @note When porting code from the nanokernel legacy API to the new API, be
1698 * careful with the return value of this function. The return value is the
1699 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1700 * non-zero means failure, while the nano_sem_take family returns 1 for success
1701 * and 0 for failure.
1702 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001703 * @retval 0 Semaphore taken.
1704 * @retval -EBUSY Returned without waiting.
1705 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001706 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001708
1709/**
1710 * @brief Give a semaphore.
1711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * This routine gives @a sem, unless the semaphore is already at its maximum
1713 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001718 *
1719 * @return N/A
1720 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001721extern void k_sem_give(struct k_sem *sem);
1722
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001723/**
1724 * @brief Reset a semaphore's count to zero.
1725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001729 *
1730 * @return N/A
1731 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001732static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001733{
1734 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001735}
1736
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001737/**
1738 * @brief Get a semaphore's count.
1739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001745 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001746static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747{
1748 return sem->count;
1749}
1750
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001755 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001756 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @param name Name of the semaphore.
1759 * @param initial_count Initial semaphore count.
1760 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001761 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001762#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001763 struct k_sem name \
1764 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001765 K_SEM_INITIALIZER(name, initial_count, count_limit)
1766
Allan Stephensc98da842016-11-11 15:45:03 -05001767/**
1768 * @} end defgroup semaphore_apis
1769 */
1770
1771/**
1772 * @defgroup alert_apis Alert APIs
1773 * @ingroup kernel_apis
1774 * @{
1775 */
1776
Allan Stephens5eceb852016-11-16 10:16:30 -05001777/**
1778 * @typedef k_alert_handler_t
1779 * @brief Alert handler function type.
1780 *
1781 * An alert's alert handler function is invoked by the system workqueue
1782 * when the alert is signalled. The alert handler function is optional,
1783 * and is only invoked if the alert has been initialized with one.
1784 *
1785 * @param alert Address of the alert.
1786 *
1787 * @return 0 if alert has been consumed; non-zero if alert should pend.
1788 */
1789typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001790
1791/**
1792 * @} end defgroup alert_apis
1793 */
1794
1795/**
1796 * @cond INTERNAL_HIDDEN
1797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001799#define K_ALERT_DEFAULT NULL
1800#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001801
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001802struct k_alert {
1803 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804 atomic_t send_count;
1805 struct k_work work_item;
1806 struct k_sem sem;
1807
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001808 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001809};
1810
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001811extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001813#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001815 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001816 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001817 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001818 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1820 }
1821
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822/**
Allan Stephensc98da842016-11-11 15:45:03 -05001823 * INTERNAL_HIDDEN @endcond
1824 */
1825
1826/**
1827 * @addtogroup alert_apis
1828 * @{
1829 */
1830
1831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001834 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001835 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001836 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * @param name Name of the alert.
1839 * @param alert_handler Action to take when alert is sent. Specify either
1840 * the address of a function to be invoked by the system workqueue
1841 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1842 * K_ALERT_DEFAULT (which causes the alert to pend).
1843 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001845#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001846 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001847 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001848 K_ALERT_INITIALIZER(name, alert_handler, \
1849 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001850
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * @param alert Address of the alert.
1857 * @param handler Action to take when alert is sent. Specify either the address
1858 * of a function to be invoked by the system workqueue thread,
1859 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1860 * K_ALERT_DEFAULT (which causes the alert to pend).
1861 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 *
1863 * @return N/A
1864 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001865extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1866 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867
1868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001869 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001871 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1874 *
1875 * @param alert Address of the alert.
1876 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 * or one of the special values K_NO_WAIT and K_FOREVER.
1878 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001879 * @retval 0 Alert received.
1880 * @retval -EBUSY Returned without waiting.
1881 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001883extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884
1885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * This routine signals @a alert. The action specified for @a alert will
1889 * be taken, which may trigger the execution of an alert handler function
1890 * and/or cause the alert to pend (assuming the alert has not reached its
1891 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * @note Can be called by ISRs.
1894 *
1895 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
1897 * @return N/A
1898 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001899extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900
1901/**
Allan Stephensc98da842016-11-11 15:45:03 -05001902 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903 */
1904
Allan Stephensc98da842016-11-11 15:45:03 -05001905/**
1906 * @cond INTERNAL_HIDDEN
1907 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001908
1909struct k_msgq {
1910 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001911 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001912 uint32_t max_msgs;
1913 char *buffer_start;
1914 char *buffer_end;
1915 char *read_ptr;
1916 char *write_ptr;
1917 uint32_t used_msgs;
1918
1919 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1920};
1921
Peter Mitsis1da807e2016-10-06 11:36:59 -04001922#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923 { \
1924 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001925 .max_msgs = q_max_msgs, \
1926 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001928 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929 .read_ptr = q_buffer, \
1930 .write_ptr = q_buffer, \
1931 .used_msgs = 0, \
1932 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1933 }
1934
Peter Mitsis1da807e2016-10-06 11:36:59 -04001935/**
Allan Stephensc98da842016-11-11 15:45:03 -05001936 * INTERNAL_HIDDEN @endcond
1937 */
1938
1939/**
1940 * @defgroup msgq_apis Message Queue APIs
1941 * @ingroup kernel_apis
1942 * @{
1943 */
1944
1945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1949 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001950 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1951 * message is similarly aligned to this boundary, @a q_msg_size must also be
1952 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * The message queue can be accessed outside the module where it is defined
1955 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001957 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959 * @param q_name Name of the message queue.
1960 * @param q_msg_size Message size (in bytes).
1961 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001962 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001963 */
1964#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1965 static char __noinit __aligned(q_align) \
1966 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001967 struct k_msgq q_name \
1968 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001969 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1970 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971
Peter Mitsisd7a37502016-10-13 11:37:40 -04001972/**
1973 * @brief Initialize a message queue.
1974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * This routine initializes a message queue object, prior to its first use.
1976 *
Allan Stephensda827222016-11-09 14:23:58 -06001977 * The message queue's ring buffer must contain space for @a max_msgs messages,
1978 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1979 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1980 * that each message is similarly aligned to this boundary, @a q_msg_size
1981 * must also be a multiple of N.
1982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @param q Address of the message queue.
1984 * @param buffer Pointer to ring buffer that holds queued messages.
1985 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001986 * @param max_msgs Maximum number of messages that can be queued.
1987 *
1988 * @return N/A
1989 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001990extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001991 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001992
1993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001994 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001996 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001997 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001998 * @note Can be called by ISRs.
1999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002000 * @param q Address of the message queue.
2001 * @param data Pointer to the message.
2002 * @param timeout Waiting period to add the message (in milliseconds),
2003 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002004 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002005 * @retval 0 Message sent.
2006 * @retval -ENOMSG Returned without waiting or queue purged.
2007 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002008 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002010
2011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002014 * This routine receives a message from message queue @a q in a "first in,
2015 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002016 *
Allan Stephensc98da842016-11-11 15:45:03 -05002017 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * @param q Address of the message queue.
2020 * @param data Address of area to hold the received message.
2021 * @param timeout Waiting period to receive the message (in milliseconds),
2022 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002023 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002024 * @retval 0 Message received.
2025 * @retval -ENOMSG Returned without waiting.
2026 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002027 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002029
2030/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002031 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 * This routine discards all unreceived messages in a message queue's ring
2034 * buffer. Any threads that are blocked waiting to send a message to the
2035 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002038 *
2039 * @return N/A
2040 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041extern void k_msgq_purge(struct k_msgq *q);
2042
Peter Mitsis67be2492016-10-07 11:44:34 -04002043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002044 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002046 * This routine returns the number of unused entries in a message queue's
2047 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002049 * @param q Address of the message queue.
2050 *
2051 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002052 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002053static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002054{
2055 return q->max_msgs - q->used_msgs;
2056}
2057
Peter Mitsisd7a37502016-10-13 11:37:40 -04002058/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002059 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * @param q Address of the message queue.
2064 *
2065 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002066 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002067static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068{
2069 return q->used_msgs;
2070}
2071
Allan Stephensc98da842016-11-11 15:45:03 -05002072/**
2073 * @} end defgroup msgq_apis
2074 */
2075
2076/**
2077 * @defgroup mem_pool_apis Memory Pool APIs
2078 * @ingroup kernel_apis
2079 * @{
2080 */
2081
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002082struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002083 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002084 void *addr_in_pool;
2085 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002086 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087};
2088
Allan Stephensc98da842016-11-11 15:45:03 -05002089/**
2090 * @} end defgroup mem_pool_apis
2091 */
2092
2093/**
2094 * @defgroup mailbox_apis Mailbox APIs
2095 * @ingroup kernel_apis
2096 * @{
2097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098
2099struct k_mbox_msg {
2100 /** internal use only - needed for legacy API support */
2101 uint32_t _mailbox;
2102 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002103 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104 /** application-defined information value */
2105 uint32_t info;
2106 /** sender's message data buffer */
2107 void *tx_data;
2108 /** internal use only - needed for legacy API support */
2109 void *_rx_data;
2110 /** message data block descriptor */
2111 struct k_mem_block tx_block;
2112 /** source thread id */
2113 k_tid_t rx_source_thread;
2114 /** target thread id */
2115 k_tid_t tx_target_thread;
2116 /** internal use only - thread waiting on send (may be a dummy) */
2117 k_tid_t _syncing_thread;
2118#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2119 /** internal use only - semaphore used during asynchronous send */
2120 struct k_sem *_async_sem;
2121#endif
2122};
2123
Allan Stephensc98da842016-11-11 15:45:03 -05002124/**
2125 * @cond INTERNAL_HIDDEN
2126 */
2127
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128struct k_mbox {
2129 _wait_q_t tx_msg_queue;
2130 _wait_q_t rx_msg_queue;
2131
2132 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
2133};
2134
2135#define K_MBOX_INITIALIZER(obj) \
2136 { \
2137 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2138 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
2139 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2140 }
2141
Peter Mitsis12092702016-10-14 12:57:23 -04002142/**
Allan Stephensc98da842016-11-11 15:45:03 -05002143 * INTERNAL_HIDDEN @endcond
2144 */
2145
2146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002151 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002154 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002156 struct k_mbox name \
2157 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 K_MBOX_INITIALIZER(name) \
2159
Peter Mitsis12092702016-10-14 12:57:23 -04002160/**
2161 * @brief Initialize a mailbox.
2162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * This routine initializes a mailbox object, prior to its first use.
2164 *
2165 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002166 *
2167 * @return N/A
2168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169extern void k_mbox_init(struct k_mbox *mbox);
2170
Peter Mitsis12092702016-10-14 12:57:23 -04002171/**
2172 * @brief Send a mailbox message in a synchronous manner.
2173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * This routine sends a message to @a mbox and waits for a receiver to both
2175 * receive and process it. The message data may be in a buffer, in a memory
2176 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * @param mbox Address of the mailbox.
2179 * @param tx_msg Address of the transmit message descriptor.
2180 * @param timeout Waiting period for the message to be received (in
2181 * milliseconds), or one of the special values K_NO_WAIT
2182 * and K_FOREVER. Once the message has been received,
2183 * this routine waits as long as necessary for the message
2184 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002185 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002186 * @retval 0 Message sent.
2187 * @retval -ENOMSG Returned without waiting.
2188 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002189 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002190extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002192
Peter Mitsis12092702016-10-14 12:57:23 -04002193/**
2194 * @brief Send a mailbox message in an asynchronous manner.
2195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * This routine sends a message to @a mbox without waiting for a receiver
2197 * to process it. The message data may be in a buffer, in a memory pool block,
2198 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2199 * will be given when the message has been both received and completely
2200 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * @param mbox Address of the mailbox.
2203 * @param tx_msg Address of the transmit message descriptor.
2204 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002205 *
2206 * @return N/A
2207 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002208extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002209 struct k_sem *sem);
2210
Peter Mitsis12092702016-10-14 12:57:23 -04002211/**
2212 * @brief Receive a mailbox message.
2213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002214 * This routine receives a message from @a mbox, then optionally retrieves
2215 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * @param mbox Address of the mailbox.
2218 * @param rx_msg Address of the receive message descriptor.
2219 * @param buffer Address of the buffer to receive data, or NULL to defer data
2220 * retrieval and message disposal until later.
2221 * @param timeout Waiting period for a message to be received (in
2222 * milliseconds), or one of the special values K_NO_WAIT
2223 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002224 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002225 * @retval 0 Message received.
2226 * @retval -ENOMSG Returned without waiting.
2227 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002228 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002229extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002231
2232/**
2233 * @brief Retrieve mailbox message data into a buffer.
2234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * This routine completes the processing of a received message by retrieving
2236 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002237 *
2238 * Alternatively, this routine can be used to dispose of a received message
2239 * without retrieving its data.
2240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * @param rx_msg Address of the receive message descriptor.
2242 * @param buffer Address of the buffer to receive data, or NULL to discard
2243 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002244 *
2245 * @return N/A
2246 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002247extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002248
2249/**
2250 * @brief Retrieve mailbox message data into a memory pool block.
2251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002252 * This routine completes the processing of a received message by retrieving
2253 * its data into a memory pool block, then disposing of the message.
2254 * The memory pool block that results from successful retrieval must be
2255 * returned to the pool once the data has been processed, even in cases
2256 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002257 *
2258 * Alternatively, this routine can be used to dispose of a received message
2259 * without retrieving its data. In this case there is no need to return a
2260 * memory pool block to the pool.
2261 *
2262 * This routine allocates a new memory pool block for the data only if the
2263 * data is not already in one. If a new block cannot be allocated, the routine
2264 * returns a failure code and the received message is left unchanged. This
2265 * permits the caller to reattempt data retrieval at a later time or to dispose
2266 * of the received message without retrieving its data.
2267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * @param rx_msg Address of a receive message descriptor.
2269 * @param pool Address of memory pool, or NULL to discard data.
2270 * @param block Address of the area to hold memory pool block info.
2271 * @param timeout Waiting period to wait for a memory pool block (in
2272 * milliseconds), or one of the special values K_NO_WAIT
2273 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002274 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002275 * @retval 0 Data retrieved.
2276 * @retval -ENOMEM Returned without waiting.
2277 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002278 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002279extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002280 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 struct k_mem_block *block, int32_t timeout);
2282
Allan Stephensc98da842016-11-11 15:45:03 -05002283/**
2284 * @} end defgroup mailbox_apis
2285 */
2286
2287/**
2288 * @cond INTERNAL_HIDDEN
2289 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290
2291struct k_pipe {
2292 unsigned char *buffer; /* Pipe buffer: may be NULL */
2293 size_t size; /* Buffer size */
2294 size_t bytes_used; /* # bytes used in buffer */
2295 size_t read_index; /* Where in buffer to read from */
2296 size_t write_index; /* Where in buffer to write */
2297
2298 struct {
2299 _wait_q_t readers; /* Reader wait queue */
2300 _wait_q_t writers; /* Writer wait queue */
2301 } wait_q;
2302
2303 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2304};
2305
Peter Mitsise5d9c582016-10-14 14:44:57 -04002306#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 { \
2308 .buffer = pipe_buffer, \
2309 .size = pipe_buffer_size, \
2310 .bytes_used = 0, \
2311 .read_index = 0, \
2312 .write_index = 0, \
2313 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2314 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2315 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2316 }
2317
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318/**
Allan Stephensc98da842016-11-11 15:45:03 -05002319 * INTERNAL_HIDDEN @endcond
2320 */
2321
2322/**
2323 * @defgroup pipe_apis Pipe APIs
2324 * @ingroup kernel_apis
2325 * @{
2326 */
2327
2328/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002333 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * @param name Name of the pipe.
2336 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2337 * or zero if no ring buffer is used.
2338 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002340#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2341 static unsigned char __noinit __aligned(pipe_align) \
2342 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002343 struct k_pipe name \
2344 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002345 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002346
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * This routine initializes a pipe object, prior to its first use.
2351 *
2352 * @param pipe Address of the pipe.
2353 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2354 * is used.
2355 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2356 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357 *
2358 * @return N/A
2359 */
2360extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2361 size_t size);
2362
2363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002364 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * @param pipe Address of the pipe.
2369 * @param data Address of data to write.
2370 * @param bytes_to_write Size of data (in bytes).
2371 * @param bytes_written Address of area to hold the number of bytes written.
2372 * @param min_xfer Minimum number of bytes to write.
2373 * @param timeout Waiting period to wait for the data to be written (in
2374 * milliseconds), or one of the special values K_NO_WAIT
2375 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002377 * @retval 0 At least @a min_xfer bytes of data were written.
2378 * @retval -EIO Returned without waiting; zero data bytes were written.
2379 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002382extern int k_pipe_put(struct k_pipe *pipe, void *data,
2383 size_t bytes_to_write, size_t *bytes_written,
2384 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385
2386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @param pipe Address of the pipe.
2392 * @param data Address to place the data read from pipe.
2393 * @param bytes_to_read Maximum number of data bytes to read.
2394 * @param bytes_read Address of area to hold the number of bytes read.
2395 * @param min_xfer Minimum number of data bytes to read.
2396 * @param timeout Waiting period to wait for the data to be read (in
2397 * milliseconds), or one of the special values K_NO_WAIT
2398 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002400 * @retval 0 At least @a min_xfer bytes of data were read.
2401 * @retval -EIO Returned without waiting; zero data bytes were read.
2402 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002405extern int k_pipe_get(struct k_pipe *pipe, void *data,
2406 size_t bytes_to_read, size_t *bytes_read,
2407 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408
2409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * This routine writes the data contained in a memory block to @a pipe.
2413 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417 * @param block Memory block containing data to send
2418 * @param size Number of data bytes in memory block to send
2419 * @param sem Semaphore to signal upon completion (else NULL)
2420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422 */
2423extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2424 size_t size, struct k_sem *sem);
2425
2426/**
Allan Stephensc98da842016-11-11 15:45:03 -05002427 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 */
2429
Allan Stephensc98da842016-11-11 15:45:03 -05002430/**
2431 * @cond INTERNAL_HIDDEN
2432 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002434struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002436 uint32_t num_blocks;
2437 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 char *buffer;
2439 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002440 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002442 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443};
2444
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002445#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2446 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447 { \
2448 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002449 .num_blocks = slab_num_blocks, \
2450 .block_size = slab_block_size, \
2451 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452 .free_list = NULL, \
2453 .num_used = 0, \
2454 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2455 }
2456
Peter Mitsis578f9112016-10-07 13:50:31 -04002457/**
Allan Stephensc98da842016-11-11 15:45:03 -05002458 * INTERNAL_HIDDEN @endcond
2459 */
2460
2461/**
2462 * @defgroup mem_slab_apis Memory Slab APIs
2463 * @ingroup kernel_apis
2464 * @{
2465 */
2466
2467/**
Allan Stephensda827222016-11-09 14:23:58 -06002468 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002469 *
Allan Stephensda827222016-11-09 14:23:58 -06002470 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002472 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2473 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002474 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002475 *
Allan Stephensda827222016-11-09 14:23:58 -06002476 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002479 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * @param name Name of the memory slab.
2482 * @param slab_block_size Size of each memory block (in bytes).
2483 * @param slab_num_blocks Number memory blocks.
2484 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002485 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002486#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2487 char __noinit __aligned(slab_align) \
2488 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2489 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002490 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002491 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2492 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002494/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002495 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002498 *
Allan Stephensda827222016-11-09 14:23:58 -06002499 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2500 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2501 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2502 * To ensure that each memory block is similarly aligned to this boundary,
2503 * @a slab_block_size must also be a multiple of N.
2504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505 * @param slab Address of the memory slab.
2506 * @param buffer Pointer to buffer used for the memory blocks.
2507 * @param block_size Size of each memory block (in bytes).
2508 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002509 *
2510 * @return N/A
2511 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002512extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002513 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002514
2515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * @param slab Address of the memory slab.
2521 * @param mem Pointer to block address area.
2522 * @param timeout Maximum time to wait for operation to complete
2523 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2524 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002525 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002526 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002528 * @retval -ENOMEM Returned without waiting.
2529 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002530 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002531extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2532 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002533
2534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * This routine releases a previously allocated memory block back to its
2538 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @param slab Address of the memory slab.
2541 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002542 *
2543 * @return N/A
2544 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002545extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * This routine gets the number of memory blocks that are currently
2551 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002556 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002557static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002559 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560}
2561
Peter Mitsisc001aa82016-10-13 13:53:37 -04002562/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002565 * This routine gets the number of memory blocks that are currently
2566 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002571 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002572static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002573{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002574 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002575}
2576
Allan Stephensc98da842016-11-11 15:45:03 -05002577/**
2578 * @} end defgroup mem_slab_apis
2579 */
2580
2581/**
2582 * @cond INTERNAL_HIDDEN
2583 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002585/*
2586 * Memory pool requires a buffer and two arrays of structures for the
2587 * memory block accounting:
2588 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2589 * status of four blocks of memory.
2590 */
2591struct k_mem_pool_quad_block {
2592 char *mem_blocks; /* pointer to the first of four memory blocks */
2593 uint32_t mem_status; /* four bits. If bit is set, memory block is
2594 allocated */
2595};
2596/*
2597 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2598 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2599 * block size is 4 times less than the previous one and thus requires 4 times
2600 * bigger array of k_mem_pool_quad_block structures to keep track of the
2601 * memory blocks.
2602 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002604/*
2605 * The array of k_mem_pool_block_set keeps the information of each array of
2606 * k_mem_pool_quad_block structures
2607 */
2608struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002609 size_t block_size; /* memory block size */
2610 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002611 struct k_mem_pool_quad_block *quad_block;
2612 int count;
2613};
2614
2615/* Memory pool descriptor */
2616struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002617 size_t max_block_size;
2618 size_t min_block_size;
2619 uint32_t nr_of_maxblocks;
2620 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002621 struct k_mem_pool_block_set *block_set;
2622 char *bufblock;
2623 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2625};
2626
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002627#ifdef CONFIG_ARM
2628#define _SECTION_TYPE_SIGN "%"
2629#else
2630#define _SECTION_TYPE_SIGN "@"
2631#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002633/*
2634 * Static memory pool initialization
2635 */
Allan Stephensc98da842016-11-11 15:45:03 -05002636
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002637/*
2638 * Use .altmacro to be able to recalculate values and pass them as string
2639 * arguments when calling assembler macros resursively
2640 */
2641__asm__(".altmacro\n\t");
2642
2643/*
2644 * Recursively calls a macro
2645 * The followig global symbols need to be initialized:
2646 * __memory_pool_max_block_size - maximal size of the memory block
2647 * __memory_pool_min_block_size - minimal size of the memory block
2648 * Notes:
2649 * Global symbols are used due the fact that assembler macro allows only
2650 * one argument be passed with the % conversion
2651 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2652 * is used instead of / 4.
2653 * n_max argument needs to go first in the invoked macro, as some
2654 * assemblers concatenate \name and %(\n_max * 4) arguments
2655 * if \name goes first
2656 */
2657__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2658 ".ifge __memory_pool_max_block_size >> 2 -"
2659 " __memory_pool_min_block_size\n\t\t"
2660 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2661 "\\macro_name %(\\n_max * 4) \\name\n\t"
2662 ".endif\n\t"
2663 ".endm\n");
2664
2665/*
2666 * Build quad blocks
2667 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2668 * structures and recursively calls itself for the next array, 4 times
2669 * larger.
2670 * The followig global symbols need to be initialized:
2671 * __memory_pool_max_block_size - maximal size of the memory block
2672 * __memory_pool_min_block_size - minimal size of the memory block
2673 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2674 */
2675__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002676 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002677 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2678 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2679 ".if \\n_max % 4\n\t\t"
2680 ".skip __memory_pool_quad_block_size\n\t"
2681 ".endif\n\t"
2682 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2683 ".endm\n");
2684
2685/*
2686 * Build block sets and initialize them
2687 * Macro initializes the k_mem_pool_block_set structure and
2688 * recursively calls itself for the next one.
2689 * The followig global symbols need to be initialized:
2690 * __memory_pool_max_block_size - maximal size of the memory block
2691 * __memory_pool_min_block_size - minimal size of the memory block
2692 * __memory_pool_block_set_count, the number of the elements in the
2693 * block set array must be set to 0. Macro calculates it's real
2694 * value.
2695 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2696 * structures, _build_quad_blocks must be called prior it.
2697 */
2698__asm__(".macro _build_block_set n_max, name\n\t"
2699 ".int __memory_pool_max_block_size\n\t" /* block_size */
2700 ".if \\n_max % 4\n\t\t"
2701 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2702 ".else\n\t\t"
2703 ".int \\n_max >> 2\n\t"
2704 ".endif\n\t"
2705 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2706 ".int 0\n\t" /* count */
2707 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2708 "__do_recurse _build_block_set \\name \\n_max\n\t"
2709 ".endm\n");
2710
2711/*
2712 * Build a memory pool structure and initialize it
2713 * Macro uses __memory_pool_block_set_count global symbol,
2714 * block set addresses and buffer address, it may be called only after
2715 * _build_block_set
2716 */
2717__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002718 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002719 _SECTION_TYPE_SIGN "progbits\n\t"
2720 ".globl \\name\n\t"
2721 "\\name:\n\t"
2722 ".int \\max_size\n\t" /* max_block_size */
2723 ".int \\min_size\n\t" /* min_block_size */
2724 ".int \\n_max\n\t" /* nr_of_maxblocks */
2725 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2726 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2727 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2728 ".int 0\n\t" /* wait_q->head */
2729 ".int 0\n\t" /* wait_q->next */
2730 ".popsection\n\t"
2731 ".endm\n");
2732
2733#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2734 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2735 _SECTION_TYPE_SIGN "progbits\n\t"); \
2736 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2737 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2738 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2739 STRINGIFY(name) "\n\t"); \
2740 __asm__(".popsection\n\t")
2741
2742#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2743 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2744 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2745 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2746 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002747 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002748 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2749 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2750 STRINGIFY(name) "\n\t"); \
2751 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2752 __asm__(".int __memory_pool_block_set_count\n\t"); \
2753 __asm__(".popsection\n\t"); \
2754 extern uint32_t _mem_pool_block_set_count_##name; \
2755 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2756
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002757#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2758 char __noinit __aligned(align) \
2759 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002760
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002761/*
2762 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2763 * to __memory_pool_quad_block_size absolute symbol.
2764 * This function does not get called, but compiler calculates the value and
2765 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2766 */
2767static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2768{
2769 __asm__(".globl __memory_pool_quad_block_size\n\t"
2770#ifdef CONFIG_NIOS2
2771 "__memory_pool_quad_block_size = %0\n\t"
2772#else
2773 "__memory_pool_quad_block_size = %c0\n\t"
2774#endif
2775 :
2776 : "n"(sizeof(struct k_mem_pool_quad_block)));
2777}
2778
2779/**
Allan Stephensc98da842016-11-11 15:45:03 -05002780 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002781 */
2782
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002783/**
Allan Stephensc98da842016-11-11 15:45:03 -05002784 * @addtogroup mem_pool_apis
2785 * @{
2786 */
2787
2788/**
2789 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002791 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2792 * long. The memory pool allows blocks to be repeatedly partitioned into
2793 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2794 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002795 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002797 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 * If the pool is to be accessed outside the module where it is defined, it
2799 * can be declared via
2800 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002801 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * @param name Name of the memory pool.
2804 * @param min_size Size of the smallest blocks in the pool (in bytes).
2805 * @param max_size Size of the largest blocks in the pool (in bytes).
2806 * @param n_max Number of maximum sized blocks in the pool.
2807 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002808 */
2809#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002810 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2811 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002812 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002813 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2814 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2815 extern struct k_mem_pool name
2816
Peter Mitsis937042c2016-10-13 13:18:26 -04002817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002822 * @param pool Address of the memory pool.
2823 * @param block Pointer to block descriptor for the allocated memory.
2824 * @param size Amount of memory to allocate (in bytes).
2825 * @param timeout Maximum time to wait for operation to complete
2826 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2827 * or K_FOREVER to wait as long as necessary.
2828 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002829 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002831 * @retval -ENOMEM Returned without waiting.
2832 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002833 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002834extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002835 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002836
2837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * This routine releases a previously allocated memory block back to its
2841 * memory pool.
2842 *
2843 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002844 *
2845 * @return N/A
2846 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002848
2849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * This routine instructs a memory pool to concatenate unused memory blocks
2853 * into larger blocks wherever possible. Manually defragmenting the memory
2854 * pool may speed up future allocations of memory blocks by eliminating the
2855 * need for the memory pool to perform an automatic partial defragmentation.
2856 *
2857 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002858 *
2859 * @return N/A
2860 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002861extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002862
2863/**
Allan Stephensc98da842016-11-11 15:45:03 -05002864 * @} end addtogroup mem_pool_apis
2865 */
2866
2867/**
2868 * @defgroup heap_apis Heap Memory Pool APIs
2869 * @ingroup kernel_apis
2870 * @{
2871 */
2872
2873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002877 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002882 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002883extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002884
2885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002887 *
2888 * This routine provides traditional free() semantics. The memory being
2889 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002892 *
2893 * @return N/A
2894 */
2895extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896
Allan Stephensc98da842016-11-11 15:45:03 -05002897/**
2898 * @} end defgroup heap_apis
2899 */
2900
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901/*
2902 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2903 * hook into the device subsystem, which itself uses nanokernel semaphores,
2904 * and thus currently requires the definition of nano_sem.
2905 */
2906#include <legacy.h>
2907#include <arch/cpu.h>
2908
2909/*
2910 * private APIs that are utilized by one or more public APIs
2911 */
2912
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002915extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916
2917#ifdef __cplusplus
2918}
2919#endif
2920
Andrew Boiee004dec2016-11-07 09:01:19 -08002921#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2922/*
2923 * Define new and delete operators.
2924 * At this moment, the operators do nothing since objects are supposed
2925 * to be statically allocated.
2926 */
2927inline void operator delete(void *ptr)
2928{
2929 (void)ptr;
2930}
2931
2932inline void operator delete[](void *ptr)
2933{
2934 (void)ptr;
2935}
2936
2937inline void *operator new(size_t size)
2938{
2939 (void)size;
2940 return NULL;
2941}
2942
2943inline void *operator new[](size_t size)
2944{
2945 (void)size;
2946 return NULL;
2947}
2948
2949/* Placement versions of operator new and delete */
2950inline void operator delete(void *ptr1, void *ptr2)
2951{
2952 (void)ptr1;
2953 (void)ptr2;
2954}
2955
2956inline void operator delete[](void *ptr1, void *ptr2)
2957{
2958 (void)ptr1;
2959 (void)ptr2;
2960}
2961
2962inline void *operator new(size_t size, void *ptr)
2963{
2964 (void)size;
2965 return ptr;
2966}
2967
2968inline void *operator new[](size_t size, void *ptr)
2969{
2970 (void)size;
2971 return ptr;
2972}
2973
2974#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2975
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976#endif /* _kernel__h_ */