blob: 7f324423b780aaacd0ef312f12a62ede2ca81a5f [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
Benjamin Walsh456c6da2016-09-02 18:55:39 -040049#define K_ANY NULL
50#define K_END NULL
51
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#if CONFIG_NUM_COOP_PRIORITIES > 0
53#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
54#else
55#define K_HIGHEST_THREAD_PRIO 0
56#endif
57
58#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
59#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
60#else
61#define K_LOWEST_THREAD_PRIO -1
62#endif
63
Benjamin Walshfab8d922016-11-08 15:36:36 -050064#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
67#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
68
69typedef sys_dlist_t _wait_q_t;
70
71#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
72#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
73#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
74#else
75#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
76#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
77#endif
78
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050079#define tcs k_thread
80struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081struct k_mutex;
82struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040083struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084struct k_msgq;
85struct k_mbox;
86struct k_pipe;
87struct k_fifo;
88struct k_lifo;
89struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040090struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040091struct k_mem_pool;
92struct k_timer;
93
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040094typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096enum execution_context_types {
97 K_ISR = 0,
98 K_COOP_THREAD,
99 K_PREEMPT_THREAD,
100};
101
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400102/**
Allan Stephensc98da842016-11-11 15:45:03 -0500103 * @defgroup thread_apis Thread APIs
104 * @ingroup kernel_apis
105 * @{
106 */
107
108/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500109 * @typedef k_thread_entry_t
110 * @brief Thread entry point function type.
111 *
112 * A thread's entry point function is invoked when the thread starts executing.
113 * Up to 3 argument values can be passed to the function.
114 *
115 * The thread terminates execution permanently if the entry point function
116 * returns. The thread is responsible for releasing any shared resources
117 * it may own (such as mutexes and dynamically allocated memory), prior to
118 * returning.
119 *
120 * @param p1 First argument.
121 * @param p2 Second argument.
122 * @param p3 Third argument.
123 *
124 * @return N/A
125 */
126typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
127
128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500129 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500131 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500133 * The new thread may be scheduled for immediate execution or a delayed start.
134 * If the newly spawned thread does not have a delayed start the kernel
135 * scheduler may preempt the current thread to allow the new thread to
136 * execute.
137 *
138 * Thread options are architecture-specific, and can include K_ESSENTIAL,
139 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
140 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400141 *
142 * @param stack Pointer to the stack space.
143 * @param stack_size Stack size in bytes.
144 * @param entry Thread entry function.
145 * @param p1 1st entry point parameter.
146 * @param p2 2nd entry point parameter.
147 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500148 * @param prio Thread priority.
149 * @param options Thread options.
150 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500152 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400153 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400154extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500155 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400156 void *p1, void *p2, void *p3,
157 int32_t prio, uint32_t options, int32_t delay);
158
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500160 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400161 *
Allan Stephensc98da842016-11-11 15:45:03 -0500162 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500163 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500165 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400166 *
167 * @return N/A
168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400169extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400170
171/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500172 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400173 *
174 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500175 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400176 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400177 * @return N/A
178 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400179extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180
181/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500182 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500184 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400185 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500186 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400187 *
188 * @return N/A
189 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400190extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400191
192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500193 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500195 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500197 * If @a thread is not currently sleeping, the routine has no effect.
198 *
199 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400200 *
201 * @return N/A
202 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400203extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400204
205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500206 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500208 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400210extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211
212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500213 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500215 * This routine prevents @a thread from executing if it has not yet started
216 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500218 * @param thread ID of thread to cancel.
219 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500220 * @retval 0 Thread spawning cancelled.
221 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400223extern int k_thread_cancel(k_tid_t thread);
224
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225/**
Allan Stephensc98da842016-11-11 15:45:03 -0500226 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500228 * This routine permanently stops execution of @a thread. The thread is taken
229 * off all kernel queues it is part of (i.e. the ready queue, the timeout
230 * queue, or a kernel object wait queue). However, any kernel resources the
231 * thread might currently own (such as mutexes or memory blocks) are not
232 * released. It is the responsibility of the caller of this routine to ensure
233 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500235 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400236 *
237 * @return N/A
238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400239extern void k_thread_abort(k_tid_t thread);
240
Allan Stephensc98da842016-11-11 15:45:03 -0500241/**
242 * @cond INTERNAL_HIDDEN
243 */
244
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400245#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246#define _THREAD_TIMEOUT_INIT(obj) \
247 (obj).nano_timeout = { \
248 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400249 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400250 .wait_q = NULL, \
251 .delta_ticks_from_prev = -1, \
252 },
253#else
254#define _THREAD_TIMEOUT_INIT(obj)
255#endif
256
257#ifdef CONFIG_ERRNO
258#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
259#else
260#define _THREAD_ERRNO_INIT(obj)
261#endif
262
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400263struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400264 union {
265 char *init_stack;
266 struct k_thread *thread;
267 };
268 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500269 void (*init_entry)(void *, void *, void *);
270 void *init_p1;
271 void *init_p2;
272 void *init_p3;
273 int init_prio;
274 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400275 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500276 void (*init_abort)(void);
277 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400278};
279
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400280#define _THREAD_INITIALIZER(stack, stack_size, \
281 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500282 prio, options, delay, abort, groups) \
283 { \
284 .init_stack = (stack), \
285 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400286 .init_entry = (void (*)(void *, void *, void *))entry, \
287 .init_p1 = (void *)p1, \
288 .init_p2 = (void *)p2, \
289 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500290 .init_prio = (prio), \
291 .init_options = (options), \
292 .init_delay = (delay), \
293 .init_abort = (abort), \
294 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400295 }
296
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400297/**
Allan Stephensc98da842016-11-11 15:45:03 -0500298 * INTERNAL_HIDDEN @endcond
299 */
300
301/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500302 * @brief Statically define and initialize a thread.
303 *
304 * The thread may be scheduled for immediate execution or a delayed start.
305 *
306 * Thread options are architecture-specific, and can include K_ESSENTIAL,
307 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
308 * them using "|" (the logical OR operator).
309 *
310 * The ID of the thread can be accessed using:
311 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500312 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500313 *
314 * @param name Name of the thread.
315 * @param stack_size Stack size in bytes.
316 * @param entry Thread entry function.
317 * @param p1 1st entry point parameter.
318 * @param p2 2nd entry point parameter.
319 * @param p3 3rd entry point parameter.
320 * @param prio Thread priority.
321 * @param options Thread options.
322 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400323 *
324 * @internal It has been observed that the x86 compiler by default aligns
325 * these _static_thread_data structures to 32-byte boundaries, thereby
326 * wasting space. To work around this, force a 4-byte alignment.
327 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500328#define K_THREAD_DEFINE(name, stack_size, \
329 entry, p1, p2, p3, \
330 prio, options, delay) \
331 char __noinit __stack _k_thread_obj_##name[stack_size]; \
332 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500333 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500334 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
335 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500336 NULL, 0); \
337 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400338
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400339/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500340 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500342 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500344 * @param thread ID of thread whose priority is needed.
345 *
346 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400347 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500348extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400349
350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400354 *
355 * Rescheduling can occur immediately depending on the priority @a thread is
356 * set to:
357 *
358 * - If its priority is raised above the priority of the caller of this
359 * function, and the caller is preemptible, @a thread will be scheduled in.
360 *
361 * - If the caller operates on itself, it lowers its priority below that of
362 * other threads in the system, and the caller is preemptible, the thread of
363 * highest priority will be scheduled in.
364 *
365 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
366 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
367 * highest priority.
368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500369 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400370 * @param prio New priority.
371 *
372 * @warning Changing the priority of a thread currently involved in mutex
373 * priority inheritance may result in undefined behavior.
374 *
375 * @return N/A
376 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400377extern void k_thread_priority_set(k_tid_t thread, int prio);
378
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500380 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500382 * This routine prevents the kernel scheduler from making @a thread the
383 * current thread. All other internal operations on @a thread are still
384 * performed; for example, any timeout it is waiting on keeps ticking,
385 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500387 * If @a thread is already suspended, the routine has no effect.
388 *
389 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400390 *
391 * @return N/A
392 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400393extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400394
395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500396 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500398 * This routine allows the kernel scheduler to make @a thread the current
399 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500401 * If @a thread is not currently suspended, the routine has no effect.
402 *
403 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400404 *
405 * @return N/A
406 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400407extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400408
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500410 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500412 * This routine specifies how the scheduler will perform time slicing of
413 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500415 * To enable time slicing, @a slice must be non-zero. The scheduler
416 * ensures that no thread runs for more than the specified time limit
417 * before other threads of that priority are given a chance to execute.
418 * Any thread whose priority is higher than @a prio is exempted, and may
419 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500421 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400422 * execute. Once the scheduler selects a thread for execution, there is no
423 * minimum guaranteed time the thread will execute before threads of greater or
424 * equal priority are scheduled.
425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500426 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 * for execution, this routine has no effect; the thread is immediately
428 * rescheduled after the slice period expires.
429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500430 * To disable timeslicing, set both @a slice and @a prio to zero.
431 *
432 * @param slice Maximum time slice length (in milliseconds).
433 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
435 * @return N/A
436 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400437extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400438
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400439/**
Allan Stephensc98da842016-11-11 15:45:03 -0500440 * @} end defgroup thread_apis
441 */
442
443/**
444 * @addtogroup isr_apis
445 * @{
446 */
447
448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500449 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400450 *
Allan Stephensc98da842016-11-11 15:45:03 -0500451 * This routine allows the caller to customize its actions, depending on
452 * whether it is a thread or an ISR.
453 *
454 * @note Can be called by ISRs.
455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500456 * @return 0 if invoked by a thread.
457 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500459extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400460
Benjamin Walsh445830d2016-11-10 15:54:27 -0500461/**
462 * @brief Determine if code is running in a preemptible thread.
463 *
Allan Stephensc98da842016-11-11 15:45:03 -0500464 * This routine allows the caller to customize its actions, depending on
465 * whether it can be preempted by another thread. The routine returns a 'true'
466 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500467 *
Allan Stephensc98da842016-11-11 15:45:03 -0500468 * - The code is running in a thread, not at ISR.
469 * - The thread's priority is in the preemptible range.
470 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500471 *
Allan Stephensc98da842016-11-11 15:45:03 -0500472 * @note Can be called by ISRs.
473 *
474 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500475 * @return Non-zero if invoked by a preemptible thread.
476 */
477extern int k_is_preempt_thread(void);
478
Allan Stephensc98da842016-11-11 15:45:03 -0500479/**
480 * @} end addtogroup isr_apis
481 */
482
483/**
484 * @addtogroup thread_apis
485 * @{
486 */
487
488/**
489 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500490 *
Allan Stephensc98da842016-11-11 15:45:03 -0500491 * This routine prevents the current thread from being preempted by another
492 * thread by instructing the scheduler to treat it as a cooperative thread.
493 * If the thread subsequently performs an operation that makes it unready,
494 * it will be context switched out in the normal manner. When the thread
495 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500496 *
Allan Stephensc98da842016-11-11 15:45:03 -0500497 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500498 *
Allan Stephensc98da842016-11-11 15:45:03 -0500499 * @note k_sched_lock() and k_sched_unlock() should normally be used
500 * when the operation being performed can be safely interrupted by ISRs.
501 * However, if the amount of processing involved is very small, better
502 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500503 *
504 * @return N/A
505 */
506extern void k_sched_lock(void);
507
Allan Stephensc98da842016-11-11 15:45:03 -0500508/**
509 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500510 *
Allan Stephensc98da842016-11-11 15:45:03 -0500511 * This routine reverses the effect of a previous call to k_sched_lock().
512 * A thread must call the routine once for each time it called k_sched_lock()
513 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500514 *
515 * @return N/A
516 */
517extern void k_sched_unlock(void);
518
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500520 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500522 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500524 * Custom data is not used by the kernel itself, and is freely available
525 * for a thread to use as it sees fit. It can be used as a framework
526 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500528 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400529 *
530 * @return N/A
531 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400532extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400533
534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500535 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500537 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500539 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400540 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400541extern void *k_thread_custom_data_get(void);
542
543/**
Allan Stephensc98da842016-11-11 15:45:03 -0500544 * @} end addtogroup thread_apis
545 */
546
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400547#include <sys_clock.h>
548
Allan Stephensc2f15a42016-11-17 12:24:22 -0500549/**
550 * @addtogroup clock_apis
551 * @{
552 */
553
554/**
555 * @brief Generate null timeout delay.
556 *
557 * This macro generates a timeout delay that that instructs a kernel API
558 * not to wait if the requested operation cannot be performed immediately.
559 *
560 * @return Timeout delay value.
561 */
562#define K_NO_WAIT 0
563
564/**
565 * @brief Generate timeout delay from milliseconds.
566 *
567 * This macro generates a timeout delay that that instructs a kernel API
568 * to wait up to @a ms milliseconds to perform the requested operation.
569 *
570 * @param ms Duration in milliseconds.
571 *
572 * @return Timeout delay value.
573 */
Johan Hedberg14471692016-11-13 10:52:15 +0200574#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500575
576/**
577 * @brief Generate timeout delay from seconds.
578 *
579 * This macro generates a timeout delay that that instructs a kernel API
580 * to wait up to @a s seconds to perform the requested operation.
581 *
582 * @param s Duration in seconds.
583 *
584 * @return Timeout delay value.
585 */
Johan Hedberg14471692016-11-13 10:52:15 +0200586#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500587
588/**
589 * @brief Generate timeout delay from minutes.
590 *
591 * This macro generates a timeout delay that that instructs a kernel API
592 * to wait up to @a m minutes to perform the requested operation.
593 *
594 * @param m Duration in minutes.
595 *
596 * @return Timeout delay value.
597 */
Johan Hedberg14471692016-11-13 10:52:15 +0200598#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500599
600/**
601 * @brief Generate timeout delay from hours.
602 *
603 * This macro generates a timeout delay that that instructs a kernel API
604 * to wait up to @a h hours to perform the requested operation.
605 *
606 * @param h Duration in hours.
607 *
608 * @return Timeout delay value.
609 */
Johan Hedberg14471692016-11-13 10:52:15 +0200610#define K_HOURS(h) K_MINUTES((h) * 60)
611
Allan Stephensc98da842016-11-11 15:45:03 -0500612/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500613 * @brief Generate infinite timeout delay.
614 *
615 * This macro generates a timeout delay that that instructs a kernel API
616 * to wait as long as necessary to perform the requested operation.
617 *
618 * @return Timeout delay value.
619 */
620#define K_FOREVER (-1)
621
622/**
623 * @} end addtogroup clock_apis
624 */
625
626/**
Allan Stephensc98da842016-11-11 15:45:03 -0500627 * @cond INTERNAL_HIDDEN
628 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400629
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500630/* added tick needed to account for tick in progress */
631#define _TICK_ALIGN 1
632
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400633static int64_t __ticks_to_ms(int64_t ticks)
634{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400635#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400636 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400637#else
638 __ASSERT(ticks == 0, "");
639 return 0;
640#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400641}
642
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400643/* timeouts */
644
645struct _timeout;
646typedef void (*_timeout_func_t)(struct _timeout *t);
647
648struct _timeout {
649 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400650 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400651 sys_dlist_t *wait_q;
652 int32_t delta_ticks_from_prev;
653 _timeout_func_t func;
654};
655
Allan Stephensc98da842016-11-11 15:45:03 -0500656/**
657 * INTERNAL_HIDDEN @endcond
658 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500659
Allan Stephensc98da842016-11-11 15:45:03 -0500660/**
661 * @cond INTERNAL_HIDDEN
662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400663
664struct k_timer {
665 /*
666 * _timeout structure must be first here if we want to use
667 * dynamic timer allocation. timeout.node is used in the double-linked
668 * list of free timers
669 */
670 struct _timeout timeout;
671
Allan Stephens45bfa372016-10-12 12:39:42 -0500672 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400673 _wait_q_t wait_q;
674
675 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500676 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400677
678 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500679 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400680
681 /* timer period */
682 int32_t period;
683
Allan Stephens45bfa372016-10-12 12:39:42 -0500684 /* timer status */
685 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400686
Allan Stephens45bfa372016-10-12 12:39:42 -0500687 /* used to support legacy timer APIs */
688 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400689
690 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
691};
692
Allan Stephens1342adb2016-11-03 13:54:53 -0500693#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500695 .timeout.delta_ticks_from_prev = -1, \
696 .timeout.wait_q = NULL, \
697 .timeout.thread = NULL, \
698 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500700 .expiry_fn = expiry, \
701 .stop_fn = stop, \
702 .status = 0, \
703 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400704 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
705 }
706
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707/**
Allan Stephensc98da842016-11-11 15:45:03 -0500708 * INTERNAL_HIDDEN @endcond
709 */
710
711/**
712 * @defgroup timer_apis Timer APIs
713 * @ingroup kernel_apis
714 * @{
715 */
716
717/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500718 * @typedef k_timer_expiry_t
719 * @brief Timer expiry function type.
720 *
721 * A timer's expiry function is executed by the system clock interrupt handler
722 * each time the timer expires. The expiry function is optional, and is only
723 * invoked if the timer has been initialized with one.
724 *
725 * @param timer Address of timer.
726 *
727 * @return N/A
728 */
729typedef void (*k_timer_expiry_t)(struct k_timer *timer);
730
731/**
732 * @typedef k_timer_stop_t
733 * @brief Timer stop function type.
734 *
735 * A timer's stop function is executed if the timer is stopped prematurely.
736 * The function runs in the context of the thread that stops the timer.
737 * The stop function is optional, and is only invoked if the timer has been
738 * initialized with one.
739 *
740 * @param timer Address of timer.
741 *
742 * @return N/A
743 */
744typedef void (*k_timer_stop_t)(struct k_timer *timer);
745
746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500747 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500749 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500751 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752 *
753 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500754 * @param expiry_fn Function to invoke each time the timer expires.
755 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500757#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500758 struct k_timer name \
759 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500760 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400761
Allan Stephens45bfa372016-10-12 12:39:42 -0500762/**
763 * @brief Initialize a timer.
764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500766 *
767 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * @param expiry_fn Function to invoke each time the timer expires.
769 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500770 *
771 * @return N/A
772 */
773extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500774 k_timer_expiry_t expiry_fn,
775 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700776
Allan Stephens45bfa372016-10-12 12:39:42 -0500777/**
778 * @brief Start a timer.
779 *
780 * This routine starts a timer, and resets its status to zero. The timer
781 * begins counting down using the specified duration and period values.
782 *
783 * Attempting to start a timer that is already running is permitted.
784 * The timer's status is reset to zero and the timer begins counting down
785 * using the new duration and period values.
786 *
787 * @param timer Address of timer.
788 * @param duration Initial timer duration (in milliseconds).
789 * @param period Timer period (in milliseconds).
790 *
791 * @return N/A
792 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400793extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500794 int32_t duration, int32_t period);
795
796/**
797 * @brief Stop a timer.
798 *
799 * This routine stops a running timer prematurely. The timer's stop function,
800 * if one exists, is invoked by the caller.
801 *
802 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500804 *
805 * @param timer Address of timer.
806 *
807 * @return N/A
808 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400809extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500810
811/**
812 * @brief Read timer status.
813 *
814 * This routine reads the timer's status, which indicates the number of times
815 * it has expired since its status was last read.
816 *
817 * Calling this routine resets the timer's status to zero.
818 *
819 * @param timer Address of timer.
820 *
821 * @return Timer status.
822 */
823extern uint32_t k_timer_status_get(struct k_timer *timer);
824
825/**
826 * @brief Synchronize thread to timer expiration.
827 *
828 * This routine blocks the calling thread until the timer's status is non-zero
829 * (indicating that it has expired at least once since it was last examined)
830 * or the timer is stopped. If the timer status is already non-zero,
831 * or the timer is already stopped, the caller continues without waiting.
832 *
833 * Calling this routine resets the timer's status to zero.
834 *
835 * This routine must not be used by interrupt handlers, since they are not
836 * allowed to block.
837 *
838 * @param timer Address of timer.
839 *
840 * @return Timer status.
841 */
842extern uint32_t k_timer_status_sync(struct k_timer *timer);
843
844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500846 *
847 * This routine computes the (approximate) time remaining before a running
848 * timer next expires. If the timer is not running, it returns zero.
849 *
850 * @param timer Address of timer.
851 *
852 * @return Remaining time (in milliseconds).
853 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400854extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400855
Allan Stephensc98da842016-11-11 15:45:03 -0500856/**
857 * @} end defgroup timer_apis
858 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400859
Allan Stephensc98da842016-11-11 15:45:03 -0500860/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500861 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500862 * @{
863 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500864
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * This routine returns the elapsed time since the system booted,
869 * in milliseconds.
870 *
871 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400872 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400873extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400874
875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * This routine returns the lower 32-bits of the elapsed time since the system
879 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * This routine can be more efficient than k_uptime_get(), as it reduces the
882 * need for interrupt locking and 64-bit math. However, the 32-bit result
883 * cannot hold a system uptime time larger than approximately 50 days, so the
884 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400887 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400888extern uint32_t k_uptime_get_32(void);
889
890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * This routine computes the elapsed time between the current system uptime
894 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500896 * @param reftime Pointer to a reference time, which is updated to the current
897 * uptime upon return.
898 *
899 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400900 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400901extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400902
903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500904 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 * This routine computes the elapsed time between the current system uptime
907 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * This routine can be more efficient than k_uptime_delta(), as it reduces the
910 * need for interrupt locking and 64-bit math. However, the 32-bit result
911 * cannot hold an elapsed time larger than approximately 50 days, so the
912 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @param reftime Pointer to a reference time, which is updated to the current
915 * uptime upon return.
916 *
917 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400918 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400919extern uint32_t k_uptime_delta_32(int64_t *reftime);
920
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500924 * This routine returns the current time, as measured by the system's hardware
925 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500927 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400929extern uint32_t k_cycle_get_32(void);
930
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400931/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500932 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400933 */
934
Allan Stephensc98da842016-11-11 15:45:03 -0500935/**
936 * @cond INTERNAL_HIDDEN
937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400938
939struct k_fifo {
940 _wait_q_t wait_q;
941 sys_slist_t data_q;
942
943 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
944};
945
Allan Stephensc98da842016-11-11 15:45:03 -0500946#define K_FIFO_INITIALIZER(obj) \
947 { \
948 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
949 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
950 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
951 }
952
953/**
954 * INTERNAL_HIDDEN @endcond
955 */
956
957/**
958 * @defgroup fifo_apis Fifo APIs
959 * @ingroup kernel_apis
960 * @{
961 */
962
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500966 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 *
970 * @return N/A
971 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400972extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973
974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500977 * This routine adds a data item to @a fifo. A fifo data item must be
978 * aligned on a 4-byte boundary, and the first 32 bits of the item are
979 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @note Can be called by ISRs.
982 *
983 * @param fifo Address of the fifo.
984 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985 *
986 * @return N/A
987 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400988extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989
990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine adds a list of data items to @a fifo in one operation.
994 * The data items must be in a singly-linked list, with the first 32 bits
995 * each data item pointing to the next data item; the list must be
996 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * @param fifo Address of the fifo.
1001 * @param head Pointer to first node in singly-linked list.
1002 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
1004 * @return N/A
1005 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001006extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007
1008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * This routine adds a list of data items to @a fifo in one operation.
1012 * The data items must be in a singly-linked list implemented using a
1013 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 * and must be re-initialized via sys_slist_init().
1015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * @note Can be called by ISRs.
1017 *
1018 * @param fifo Address of the fifo.
1019 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 *
1021 * @return N/A
1022 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001023extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024
1025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * This routine removes a data item from @a fifo in a "first in, first out"
1029 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001031 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1032 *
1033 * @param fifo Address of the fifo.
1034 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 * or one of the special values K_NO_WAIT and K_FOREVER.
1036 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001037 * @return Address of the data item if successful; NULL if returned
1038 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001040extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1041
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001047 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001051#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001052 struct k_fifo name \
1053 __in_section(_k_fifo, static, name) = \
1054 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055
Allan Stephensc98da842016-11-11 15:45:03 -05001056/**
1057 * @} end defgroup fifo_apis
1058 */
1059
1060/**
1061 * @cond INTERNAL_HIDDEN
1062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063
1064struct k_lifo {
1065 _wait_q_t wait_q;
1066 void *list;
1067
1068 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
1069};
1070
Allan Stephensc98da842016-11-11 15:45:03 -05001071#define K_LIFO_INITIALIZER(obj) \
1072 { \
1073 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1074 .list = NULL, \
1075 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1076 }
1077
1078/**
1079 * INTERNAL_HIDDEN @endcond
1080 */
1081
1082/**
1083 * @defgroup lifo_apis Lifo APIs
1084 * @ingroup kernel_apis
1085 * @{
1086 */
1087
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001091 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
1095 * @return N/A
1096 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098
1099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * This routine adds a data item to @a lifo. A lifo data item must be
1103 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1104 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * @note Can be called by ISRs.
1107 *
1108 * @param lifo Address of the lifo.
1109 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001110 *
1111 * @return N/A
1112 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001113extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114
1115/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * This routine removes a data item from @a lifo in a "last in, first out"
1119 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1122 *
1123 * @param lifo Address of the lifo.
1124 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 * or one of the special values K_NO_WAIT and K_FOREVER.
1126 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001127 * @return Address of the data item if successful; NULL if returned
1128 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001130extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1131
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001137 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001142 struct k_lifo name \
1143 __in_section(_k_lifo, static, name) = \
1144 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001145
Allan Stephensc98da842016-11-11 15:45:03 -05001146/**
1147 * @} end defgroup lifo_apis
1148 */
1149
1150/**
1151 * @cond INTERNAL_HIDDEN
1152 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001153
1154struct k_stack {
1155 _wait_q_t wait_q;
1156 uint32_t *base, *next, *top;
1157
1158 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
1159};
1160
Allan Stephensc98da842016-11-11 15:45:03 -05001161#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1162 { \
1163 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1164 .base = stack_buffer, \
1165 .next = stack_buffer, \
1166 .top = stack_buffer + stack_num_entries, \
1167 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1168 }
1169
1170/**
1171 * INTERNAL_HIDDEN @endcond
1172 */
1173
1174/**
1175 * @defgroup stack_apis Stack APIs
1176 * @ingroup kernel_apis
1177 * @{
1178 */
1179
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180/**
1181 * @brief Initialize a stack.
1182 *
1183 * This routine initializes a stack object, prior to its first use.
1184 *
1185 * @param stack Address of the stack.
1186 * @param buffer Address of array used to hold stacked values.
1187 * @param num_entries Maximum number of values that can be stacked.
1188 *
1189 * @return N/A
1190 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001191extern void k_stack_init(struct k_stack *stack,
1192 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193
1194/**
1195 * @brief Push an element onto a stack.
1196 *
1197 * This routine adds a 32-bit value @a data to @a stack.
1198 *
1199 * @note Can be called by ISRs.
1200 *
1201 * @param stack Address of the stack.
1202 * @param data Value to push onto the stack.
1203 *
1204 * @return N/A
1205 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001207
1208/**
1209 * @brief Pop an element from a stack.
1210 *
1211 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1212 * manner and stores the value in @a data.
1213 *
1214 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1215 *
1216 * @param stack Address of the stack.
1217 * @param data Address of area to hold the value popped from the stack.
1218 * @param timeout Waiting period to obtain a value (in milliseconds),
1219 * or one of the special values K_NO_WAIT and K_FOREVER.
1220 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001221 * @retval 0 Element popped from stack.
1222 * @retval -EBUSY Returned without waiting.
1223 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001225extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1226
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001230 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001232 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * @param name Name of the stack.
1235 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001237#define K_STACK_DEFINE(name, stack_num_entries) \
1238 uint32_t __noinit \
1239 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001240 struct k_stack name \
1241 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001242 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1243 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001244
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245/**
Allan Stephensc98da842016-11-11 15:45:03 -05001246 * @} end defgroup stack_apis
1247 */
1248
Allan Stephens6bba9b02016-11-16 14:56:54 -05001249struct k_work;
1250
Allan Stephensc98da842016-11-11 15:45:03 -05001251/**
1252 * @defgroup workqueue_apis Workqueue Thread APIs
1253 * @ingroup kernel_apis
1254 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001255 */
1256
Allan Stephens6bba9b02016-11-16 14:56:54 -05001257/**
1258 * @typedef k_work_handler_t
1259 * @brief Work item handler function type.
1260 *
1261 * A work item's handler function is executed by a workqueue's thread
1262 * when the work item is processed by the workqueue.
1263 *
1264 * @param work Address of the work item.
1265 *
1266 * @return N/A
1267 */
1268typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269
1270/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001271 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001272 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001273
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001274struct k_work_q {
1275 struct k_fifo fifo;
1276};
1277
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001279 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001280};
1281
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001282struct k_work {
1283 void *_reserved; /* Used by k_fifo implementation. */
1284 k_work_handler_t handler;
1285 atomic_t flags[1];
1286};
1287
Allan Stephens6bba9b02016-11-16 14:56:54 -05001288struct k_delayed_work {
1289 struct k_work work;
1290 struct _timeout timeout;
1291 struct k_work_q *work_q;
1292};
1293
1294extern struct k_work_q k_sys_work_q;
1295
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001297 * INTERNAL_HIDDEN @endcond
1298 */
1299
1300/**
1301 * @brief Initialize a statically-defined work item.
1302 *
1303 * This macro can be used to initialize a statically-defined workqueue work
1304 * item, prior to its first use. For example,
1305 *
1306 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1307 *
1308 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001309 */
1310#define K_WORK_INITIALIZER(work_handler) \
1311 { \
1312 ._reserved = NULL, \
1313 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001314 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315 }
1316
1317/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001318 * @brief Initialize a work item.
1319 *
1320 * This routine initializes a workqueue work item, prior to its first use.
1321 *
1322 * @param work Address of work item.
1323 * @param handler Function to invoke each time work item is processed.
1324 *
1325 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001326 */
1327static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1328{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001329 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330 work->handler = handler;
1331}
1332
1333/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001334 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001335 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001336 * This routine submits work item @a work to be processed by workqueue
1337 * @a work_q. If the work item is already pending in the workqueue's queue
1338 * as a result of an earlier submission, this routine has no effect on the
1339 * work item. If the work item has already been processed, or is currently
1340 * being processed, its work is considered complete and the work item can be
1341 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001342 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001343 * @warning
1344 * A submitted work item must not be modified until it has been processed
1345 * by the workqueue.
1346 *
1347 * @note Can be called by ISRs.
1348 *
1349 * @param work_q Address of workqueue.
1350 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001351 *
1352 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001353 */
1354static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1355 struct k_work *work)
1356{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001357 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001358 k_fifo_put(&work_q->fifo, work);
1359 }
1360}
1361
1362/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001363 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001365 * This routine indicates if work item @a work is pending in a workqueue's
1366 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001367 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001368 * @note Can be called by ISRs.
1369 *
1370 * @param work Address of work item.
1371 *
1372 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001373 */
1374static inline int k_work_pending(struct k_work *work)
1375{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001376 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001377}
1378
1379/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001380 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001381 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001382 * This routine starts workqueue @a work_q. The workqueue spawns its work
1383 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001384 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001385 * @param work_q Address of workqueue.
1386 * @param stack Pointer to work queue thread's stack space.
1387 * @param stack_size Size of the work queue thread's stack (in bytes).
1388 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001389 *
1390 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 */
Allan Stephens904cf972016-10-07 13:59:23 -05001392extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1393 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001396 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001397 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001398 * This routine initializes a workqueue delayed work item, prior to
1399 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001400 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001401 * @param work Address of delayed work item.
1402 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001403 *
1404 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001406extern void k_delayed_work_init(struct k_delayed_work *work,
1407 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408
1409/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001410 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001412 * This routine schedules work item @a work to be processed by workqueue
1413 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1414 * an asychronous countdown for the work item and then returns to the caller.
1415 * Only when the countdown completes is the work item actually submitted to
1416 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001418 * Submitting a previously submitted delayed work item that is still
1419 * counting down cancels the existing submission and restarts the countdown
1420 * using the new delay. If the work item is currently pending on the
1421 * workqueue's queue because the countdown has completed it is too late to
1422 * resubmit the item, and resubmission fails without impacting the work item.
1423 * If the work item has already been processed, or is currently being processed,
1424 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001426 * @warning
1427 * A delayed work item must not be modified until it has been processed
1428 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001430 * @note Can be called by ISRs.
1431 *
1432 * @param work_q Address of workqueue.
1433 * @param work Address of delayed work item.
1434 * @param delay Delay before submitting the work item (in milliseconds).
1435 *
1436 * @retval 0 Work item countdown started.
1437 * @retval -EINPROGRESS Work item is already pending.
1438 * @retval -EINVAL Work item is being processed or has completed its work.
1439 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001441extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1442 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001443 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444
1445/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001446 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001447 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001448 * This routine cancels the submission of delayed work item @a work.
1449 * A delayed work item can only be cancelled while its countdown is still
1450 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001451 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001452 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001454 * @param work Address of delayed work item.
1455 *
1456 * @retval 0 Work item countdown cancelled.
1457 * @retval -EINPROGRESS Work item is already pending.
1458 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001460extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001461
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001462/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463 * @brief Submit a work item to the system workqueue.
1464 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001465 * This routine submits work item @a work to be processed by the system
1466 * workqueue. If the work item is already pending in the workqueue's queue
1467 * as a result of an earlier submission, this routine has no effect on the
1468 * work item. If the work item has already been processed, or is currently
1469 * being processed, its work is considered complete and the work item can be
1470 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001471 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001472 * @warning
1473 * Work items submitted to the system workqueue should avoid using handlers
1474 * that block or yield since this may prevent the system workqueue from
1475 * processing other work items in a timely manner.
1476 *
1477 * @note Can be called by ISRs.
1478 *
1479 * @param work Address of work item.
1480 *
1481 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001482 */
1483static inline void k_work_submit(struct k_work *work)
1484{
1485 k_work_submit_to_queue(&k_sys_work_q, work);
1486}
1487
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001488/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001489 * @brief Submit a delayed work item to the system workqueue.
1490 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001491 * This routine schedules work item @a work to be processed by the system
1492 * workqueue after a delay of @a delay milliseconds. The routine initiates
1493 * an asychronous countdown for the work item and then returns to the caller.
1494 * Only when the countdown completes is the work item actually submitted to
1495 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001496 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001497 * Submitting a previously submitted delayed work item that is still
1498 * counting down cancels the existing submission and restarts the countdown
1499 * using the new delay. If the work item is currently pending on the
1500 * workqueue's queue because the countdown has completed it is too late to
1501 * resubmit the item, and resubmission fails without impacting the work item.
1502 * If the work item has already been processed, or is currently being processed,
1503 * its work is considered complete and the work item can be resubmitted.
1504 *
1505 * @warning
1506 * Work items submitted to the system workqueue should avoid using handlers
1507 * that block or yield since this may prevent the system workqueue from
1508 * processing other work items in a timely manner.
1509 *
1510 * @note Can be called by ISRs.
1511 *
1512 * @param work Address of delayed work item.
1513 * @param delay Delay before submitting the work item (in milliseconds).
1514 *
1515 * @retval 0 Work item countdown started.
1516 * @retval -EINPROGRESS Work item is already pending.
1517 * @retval -EINVAL Work item is being processed or has completed its work.
1518 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519 */
1520static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001521 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001522{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001523 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001524}
1525
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001526/**
Allan Stephensc98da842016-11-11 15:45:03 -05001527 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001528 */
1529
Allan Stephensc98da842016-11-11 15:45:03 -05001530/**
1531 * @cond INTERNAL_HIDDEN
1532 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533
1534struct k_mutex {
1535 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001536 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537 uint32_t lock_count;
1538 int owner_orig_prio;
1539#ifdef CONFIG_OBJECT_MONITOR
1540 int num_lock_state_changes;
1541 int num_conflicts;
1542#endif
1543
1544 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1545};
1546
1547#ifdef CONFIG_OBJECT_MONITOR
1548#define _MUTEX_INIT_OBJECT_MONITOR \
1549 .num_lock_state_changes = 0, .num_conflicts = 0,
1550#else
1551#define _MUTEX_INIT_OBJECT_MONITOR
1552#endif
1553
1554#define K_MUTEX_INITIALIZER(obj) \
1555 { \
1556 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1557 .owner = NULL, \
1558 .lock_count = 0, \
1559 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1560 _MUTEX_INIT_OBJECT_MONITOR \
1561 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1562 }
1563
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001564/**
Allan Stephensc98da842016-11-11 15:45:03 -05001565 * INTERNAL_HIDDEN @endcond
1566 */
1567
1568/**
1569 * @defgroup mutex_apis Mutex APIs
1570 * @ingroup kernel_apis
1571 * @{
1572 */
1573
1574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001575 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001577 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001578 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001579 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001581 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001582 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001583#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001584 struct k_mutex name \
1585 __in_section(_k_mutex, static, name) = \
1586 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001593 * Upon completion, the mutex is available and does not have an owner.
1594 *
1595 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596 *
1597 * @return N/A
1598 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001599extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001600
1601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001602 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001604 * This routine locks @a mutex. If the mutex is locked by another thread,
1605 * the calling thread waits until the mutex becomes available or until
1606 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * A thread is permitted to lock a mutex it has already locked. The operation
1609 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * @param mutex Address of the mutex.
1612 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001613 * or one of the special values K_NO_WAIT and K_FOREVER.
1614 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001615 * @retval 0 Mutex locked.
1616 * @retval -EBUSY Returned without waiting.
1617 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001619extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620
1621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * This routine unlocks @a mutex. The mutex must already be locked by the
1625 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001626 *
1627 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001628 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001629 * thread.
1630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001632 *
1633 * @return N/A
1634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635extern void k_mutex_unlock(struct k_mutex *mutex);
1636
Allan Stephensc98da842016-11-11 15:45:03 -05001637/**
1638 * @} end defgroup mutex_apis
1639 */
1640
1641/**
1642 * @cond INTERNAL_HIDDEN
1643 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001644
1645struct k_sem {
1646 _wait_q_t wait_q;
1647 unsigned int count;
1648 unsigned int limit;
1649
1650 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1651};
1652
Allan Stephensc98da842016-11-11 15:45:03 -05001653#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1654 { \
1655 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1656 .count = initial_count, \
1657 .limit = count_limit, \
1658 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1659 }
1660
1661/**
1662 * INTERNAL_HIDDEN @endcond
1663 */
1664
1665/**
1666 * @defgroup semaphore_apis Semaphore APIs
1667 * @ingroup kernel_apis
1668 * @{
1669 */
1670
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001676 * @param sem Address of the semaphore.
1677 * @param initial_count Initial semaphore count.
1678 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001679 *
1680 * @return N/A
1681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001682extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1683 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001684
1685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1691 *
1692 * @param sem Address of the semaphore.
1693 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001694 * or one of the special values K_NO_WAIT and K_FOREVER.
1695 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001696 * @retval 0 Semaphore taken.
1697 * @retval -EBUSY Returned without waiting.
1698 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001699 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001701
1702/**
1703 * @brief Give a semaphore.
1704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * This routine gives @a sem, unless the semaphore is already at its maximum
1706 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001711 *
1712 * @return N/A
1713 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714extern void k_sem_give(struct k_sem *sem);
1715
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001716/**
1717 * @brief Reset a semaphore's count to zero.
1718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001722 *
1723 * @return N/A
1724 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001725static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726{
1727 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728}
1729
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001730/**
1731 * @brief Get a semaphore's count.
1732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001738 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001739static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740{
1741 return sem->count;
1742}
1743
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001744/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001745 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001748 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001749 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @param name Name of the semaphore.
1752 * @param initial_count Initial semaphore count.
1753 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001754 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001755#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001756 struct k_sem name \
1757 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001758 K_SEM_INITIALIZER(name, initial_count, count_limit)
1759
Allan Stephensc98da842016-11-11 15:45:03 -05001760/**
1761 * @} end defgroup semaphore_apis
1762 */
1763
1764/**
1765 * @defgroup alert_apis Alert APIs
1766 * @ingroup kernel_apis
1767 * @{
1768 */
1769
Allan Stephens5eceb852016-11-16 10:16:30 -05001770/**
1771 * @typedef k_alert_handler_t
1772 * @brief Alert handler function type.
1773 *
1774 * An alert's alert handler function is invoked by the system workqueue
1775 * when the alert is signalled. The alert handler function is optional,
1776 * and is only invoked if the alert has been initialized with one.
1777 *
1778 * @param alert Address of the alert.
1779 *
1780 * @return 0 if alert has been consumed; non-zero if alert should pend.
1781 */
1782typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001783
1784/**
1785 * @} end defgroup alert_apis
1786 */
1787
1788/**
1789 * @cond INTERNAL_HIDDEN
1790 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001792#define K_ALERT_DEFAULT NULL
1793#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001794
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001795struct k_alert {
1796 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001797 atomic_t send_count;
1798 struct k_work work_item;
1799 struct k_sem sem;
1800
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001801 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802};
1803
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001804extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001806#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001807 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001808 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001809 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001810 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001811 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1813 }
1814
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815/**
Allan Stephensc98da842016-11-11 15:45:03 -05001816 * INTERNAL_HIDDEN @endcond
1817 */
1818
1819/**
1820 * @addtogroup alert_apis
1821 * @{
1822 */
1823
1824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001826 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001827 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001828 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001829 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * @param name Name of the alert.
1832 * @param alert_handler Action to take when alert is sent. Specify either
1833 * the address of a function to be invoked by the system workqueue
1834 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1835 * K_ALERT_DEFAULT (which causes the alert to pend).
1836 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001838#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001839 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001840 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001841 K_ALERT_INITIALIZER(name, alert_handler, \
1842 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001843
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * @param alert Address of the alert.
1850 * @param handler Action to take when alert is sent. Specify either the address
1851 * of a function to be invoked by the system workqueue thread,
1852 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1853 * K_ALERT_DEFAULT (which causes the alert to pend).
1854 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
1856 * @return N/A
1857 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001858extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1859 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860
1861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001866 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1867 *
1868 * @param alert Address of the alert.
1869 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 * or one of the special values K_NO_WAIT and K_FOREVER.
1871 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001872 * @retval 0 Alert received.
1873 * @retval -EBUSY Returned without waiting.
1874 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001876extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877
1878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * This routine signals @a alert. The action specified for @a alert will
1882 * be taken, which may trigger the execution of an alert handler function
1883 * and/or cause the alert to pend (assuming the alert has not reached its
1884 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @note Can be called by ISRs.
1887 *
1888 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 *
1890 * @return N/A
1891 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001892extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001893
1894/**
Allan Stephensc98da842016-11-11 15:45:03 -05001895 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896 */
1897
Allan Stephensc98da842016-11-11 15:45:03 -05001898/**
1899 * @cond INTERNAL_HIDDEN
1900 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901
1902struct k_msgq {
1903 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001904 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 uint32_t max_msgs;
1906 char *buffer_start;
1907 char *buffer_end;
1908 char *read_ptr;
1909 char *write_ptr;
1910 uint32_t used_msgs;
1911
1912 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1913};
1914
Peter Mitsis1da807e2016-10-06 11:36:59 -04001915#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916 { \
1917 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001918 .max_msgs = q_max_msgs, \
1919 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001920 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001921 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001922 .read_ptr = q_buffer, \
1923 .write_ptr = q_buffer, \
1924 .used_msgs = 0, \
1925 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1926 }
1927
Peter Mitsis1da807e2016-10-06 11:36:59 -04001928/**
Allan Stephensc98da842016-11-11 15:45:03 -05001929 * INTERNAL_HIDDEN @endcond
1930 */
1931
1932/**
1933 * @defgroup msgq_apis Message Queue APIs
1934 * @ingroup kernel_apis
1935 * @{
1936 */
1937
1938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001941 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1942 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001943 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1944 * message is similarly aligned to this boundary, @a q_msg_size must also be
1945 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001947 * The message queue can be accessed outside the module where it is defined
1948 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001950 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952 * @param q_name Name of the message queue.
1953 * @param q_msg_size Message size (in bytes).
1954 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001955 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001956 */
1957#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1958 static char __noinit __aligned(q_align) \
1959 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001960 struct k_msgq q_name \
1961 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001962 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1963 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964
Peter Mitsisd7a37502016-10-13 11:37:40 -04001965/**
1966 * @brief Initialize a message queue.
1967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001968 * This routine initializes a message queue object, prior to its first use.
1969 *
Allan Stephensda827222016-11-09 14:23:58 -06001970 * The message queue's ring buffer must contain space for @a max_msgs messages,
1971 * each of which is @a msg_size bytes long. The buffer must be aligned to an
1972 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
1973 * that each message is similarly aligned to this boundary, @a q_msg_size
1974 * must also be a multiple of N.
1975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001976 * @param q Address of the message queue.
1977 * @param buffer Pointer to ring buffer that holds queued messages.
1978 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04001979 * @param max_msgs Maximum number of messages that can be queued.
1980 *
1981 * @return N/A
1982 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001983extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001984 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001985
1986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001990 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05001991 * @note Can be called by ISRs.
1992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * @param q Address of the message queue.
1994 * @param data Pointer to the message.
1995 * @param timeout Waiting period to add the message (in milliseconds),
1996 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04001997 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001998 * @retval 0 Message sent.
1999 * @retval -ENOMSG Returned without waiting or queue purged.
2000 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002001 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002002extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002003
2004/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002005 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * This routine receives a message from message queue @a q in a "first in,
2008 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002009 *
Allan Stephensc98da842016-11-11 15:45:03 -05002010 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @param q Address of the message queue.
2013 * @param data Address of area to hold the received message.
2014 * @param timeout Waiting period to receive the message (in milliseconds),
2015 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002016 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002017 * @retval 0 Message received.
2018 * @retval -ENOMSG Returned without waiting.
2019 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002020 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002022
2023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002024 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026 * This routine discards all unreceived messages in a message queue's ring
2027 * buffer. Any threads that are blocked waiting to send a message to the
2028 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002030 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002031 *
2032 * @return N/A
2033 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034extern void k_msgq_purge(struct k_msgq *q);
2035
Peter Mitsis67be2492016-10-07 11:44:34 -04002036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * This routine returns the number of unused entries in a message queue's
2040 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002042 * @param q Address of the message queue.
2043 *
2044 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002045 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002046static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002047{
2048 return q->max_msgs - q->used_msgs;
2049}
2050
Peter Mitsisd7a37502016-10-13 11:37:40 -04002051/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002052 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @param q Address of the message queue.
2057 *
2058 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002059 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002060static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061{
2062 return q->used_msgs;
2063}
2064
Allan Stephensc98da842016-11-11 15:45:03 -05002065/**
2066 * @} end defgroup msgq_apis
2067 */
2068
2069/**
2070 * @defgroup mem_pool_apis Memory Pool APIs
2071 * @ingroup kernel_apis
2072 * @{
2073 */
2074
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002075struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002076 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002077 void *addr_in_pool;
2078 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002079 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002080};
2081
Allan Stephensc98da842016-11-11 15:45:03 -05002082/**
2083 * @} end defgroup mem_pool_apis
2084 */
2085
2086/**
2087 * @defgroup mailbox_apis Mailbox APIs
2088 * @ingroup kernel_apis
2089 * @{
2090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091
2092struct k_mbox_msg {
2093 /** internal use only - needed for legacy API support */
2094 uint32_t _mailbox;
2095 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002096 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002097 /** application-defined information value */
2098 uint32_t info;
2099 /** sender's message data buffer */
2100 void *tx_data;
2101 /** internal use only - needed for legacy API support */
2102 void *_rx_data;
2103 /** message data block descriptor */
2104 struct k_mem_block tx_block;
2105 /** source thread id */
2106 k_tid_t rx_source_thread;
2107 /** target thread id */
2108 k_tid_t tx_target_thread;
2109 /** internal use only - thread waiting on send (may be a dummy) */
2110 k_tid_t _syncing_thread;
2111#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2112 /** internal use only - semaphore used during asynchronous send */
2113 struct k_sem *_async_sem;
2114#endif
2115};
2116
Allan Stephensc98da842016-11-11 15:45:03 -05002117/**
2118 * @cond INTERNAL_HIDDEN
2119 */
2120
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002121struct k_mbox {
2122 _wait_q_t tx_msg_queue;
2123 _wait_q_t rx_msg_queue;
2124
2125 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
2126};
2127
2128#define K_MBOX_INITIALIZER(obj) \
2129 { \
2130 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2131 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
2132 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2133 }
2134
Peter Mitsis12092702016-10-14 12:57:23 -04002135/**
Allan Stephensc98da842016-11-11 15:45:03 -05002136 * INTERNAL_HIDDEN @endcond
2137 */
2138
2139/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002142 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002144 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002149 struct k_mbox name \
2150 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151 K_MBOX_INITIALIZER(name) \
2152
Peter Mitsis12092702016-10-14 12:57:23 -04002153/**
2154 * @brief Initialize a mailbox.
2155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002156 * This routine initializes a mailbox object, prior to its first use.
2157 *
2158 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002159 *
2160 * @return N/A
2161 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162extern void k_mbox_init(struct k_mbox *mbox);
2163
Peter Mitsis12092702016-10-14 12:57:23 -04002164/**
2165 * @brief Send a mailbox message in a synchronous manner.
2166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * This routine sends a message to @a mbox and waits for a receiver to both
2168 * receive and process it. The message data may be in a buffer, in a memory
2169 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002171 * @param mbox Address of the mailbox.
2172 * @param tx_msg Address of the transmit message descriptor.
2173 * @param timeout Waiting period for the message to be received (in
2174 * milliseconds), or one of the special values K_NO_WAIT
2175 * and K_FOREVER. Once the message has been received,
2176 * this routine waits as long as necessary for the message
2177 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002178 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002179 * @retval 0 Message sent.
2180 * @retval -ENOMSG Returned without waiting.
2181 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002182 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002183extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002185
Peter Mitsis12092702016-10-14 12:57:23 -04002186/**
2187 * @brief Send a mailbox message in an asynchronous manner.
2188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002189 * This routine sends a message to @a mbox without waiting for a receiver
2190 * to process it. The message data may be in a buffer, in a memory pool block,
2191 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2192 * will be given when the message has been both received and completely
2193 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * @param mbox Address of the mailbox.
2196 * @param tx_msg Address of the transmit message descriptor.
2197 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002198 *
2199 * @return N/A
2200 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002201extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202 struct k_sem *sem);
2203
Peter Mitsis12092702016-10-14 12:57:23 -04002204/**
2205 * @brief Receive a mailbox message.
2206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002207 * This routine receives a message from @a mbox, then optionally retrieves
2208 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002210 * @param mbox Address of the mailbox.
2211 * @param rx_msg Address of the receive message descriptor.
2212 * @param buffer Address of the buffer to receive data, or NULL to defer data
2213 * retrieval and message disposal until later.
2214 * @param timeout Waiting period for a message to be received (in
2215 * milliseconds), or one of the special values K_NO_WAIT
2216 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002217 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002218 * @retval 0 Message received.
2219 * @retval -ENOMSG Returned without waiting.
2220 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002221 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002222extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002224
2225/**
2226 * @brief Retrieve mailbox message data into a buffer.
2227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002228 * This routine completes the processing of a received message by retrieving
2229 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002230 *
2231 * Alternatively, this routine can be used to dispose of a received message
2232 * without retrieving its data.
2233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002234 * @param rx_msg Address of the receive message descriptor.
2235 * @param buffer Address of the buffer to receive data, or NULL to discard
2236 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002237 *
2238 * @return N/A
2239 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002240extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002241
2242/**
2243 * @brief Retrieve mailbox message data into a memory pool block.
2244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002245 * This routine completes the processing of a received message by retrieving
2246 * its data into a memory pool block, then disposing of the message.
2247 * The memory pool block that results from successful retrieval must be
2248 * returned to the pool once the data has been processed, even in cases
2249 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002250 *
2251 * Alternatively, this routine can be used to dispose of a received message
2252 * without retrieving its data. In this case there is no need to return a
2253 * memory pool block to the pool.
2254 *
2255 * This routine allocates a new memory pool block for the data only if the
2256 * data is not already in one. If a new block cannot be allocated, the routine
2257 * returns a failure code and the received message is left unchanged. This
2258 * permits the caller to reattempt data retrieval at a later time or to dispose
2259 * of the received message without retrieving its data.
2260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261 * @param rx_msg Address of a receive message descriptor.
2262 * @param pool Address of memory pool, or NULL to discard data.
2263 * @param block Address of the area to hold memory pool block info.
2264 * @param timeout Waiting period to wait for a memory pool block (in
2265 * milliseconds), or one of the special values K_NO_WAIT
2266 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002267 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002268 * @retval 0 Data retrieved.
2269 * @retval -ENOMEM Returned without waiting.
2270 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002271 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002272extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002273 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274 struct k_mem_block *block, int32_t timeout);
2275
Allan Stephensc98da842016-11-11 15:45:03 -05002276/**
2277 * @} end defgroup mailbox_apis
2278 */
2279
2280/**
2281 * @cond INTERNAL_HIDDEN
2282 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002283
2284struct k_pipe {
2285 unsigned char *buffer; /* Pipe buffer: may be NULL */
2286 size_t size; /* Buffer size */
2287 size_t bytes_used; /* # bytes used in buffer */
2288 size_t read_index; /* Where in buffer to read from */
2289 size_t write_index; /* Where in buffer to write */
2290
2291 struct {
2292 _wait_q_t readers; /* Reader wait queue */
2293 _wait_q_t writers; /* Writer wait queue */
2294 } wait_q;
2295
2296 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
2297};
2298
Peter Mitsise5d9c582016-10-14 14:44:57 -04002299#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 { \
2301 .buffer = pipe_buffer, \
2302 .size = pipe_buffer_size, \
2303 .bytes_used = 0, \
2304 .read_index = 0, \
2305 .write_index = 0, \
2306 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2307 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
2308 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2309 }
2310
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311/**
Allan Stephensc98da842016-11-11 15:45:03 -05002312 * INTERNAL_HIDDEN @endcond
2313 */
2314
2315/**
2316 * @defgroup pipe_apis Pipe APIs
2317 * @ingroup kernel_apis
2318 * @{
2319 */
2320
2321/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002324 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002326 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002328 * @param name Name of the pipe.
2329 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2330 * or zero if no ring buffer is used.
2331 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002333#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2334 static unsigned char __noinit __aligned(pipe_align) \
2335 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002336 struct k_pipe name \
2337 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002338 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * This routine initializes a pipe object, prior to its first use.
2344 *
2345 * @param pipe Address of the pipe.
2346 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2347 * is used.
2348 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2349 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350 *
2351 * @return N/A
2352 */
2353extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2354 size_t size);
2355
2356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002357 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @param pipe Address of the pipe.
2362 * @param data Address of data to write.
2363 * @param bytes_to_write Size of data (in bytes).
2364 * @param bytes_written Address of area to hold the number of bytes written.
2365 * @param min_xfer Minimum number of bytes to write.
2366 * @param timeout Waiting period to wait for the data to be written (in
2367 * milliseconds), or one of the special values K_NO_WAIT
2368 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002370 * @retval 0 At least @a min_xfer bytes of data were written.
2371 * @retval -EIO Returned without waiting; zero data bytes were written.
2372 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002373 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002375extern int k_pipe_put(struct k_pipe *pipe, void *data,
2376 size_t bytes_to_write, size_t *bytes_written,
2377 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378
2379/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * @param pipe Address of the pipe.
2385 * @param data Address to place the data read from pipe.
2386 * @param bytes_to_read Maximum number of data bytes to read.
2387 * @param bytes_read Address of area to hold the number of bytes read.
2388 * @param min_xfer Minimum number of data bytes to read.
2389 * @param timeout Waiting period to wait for the data to be read (in
2390 * milliseconds), or one of the special values K_NO_WAIT
2391 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002393 * @retval 0 At least @a min_xfer bytes of data were read.
2394 * @retval -EIO Returned without waiting; zero data bytes were read.
2395 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002398extern int k_pipe_get(struct k_pipe *pipe, void *data,
2399 size_t bytes_to_read, size_t *bytes_read,
2400 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002401
2402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * This routine writes the data contained in a memory block to @a pipe.
2406 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 * @param block Memory block containing data to send
2411 * @param size Number of data bytes in memory block to send
2412 * @param sem Semaphore to signal upon completion (else NULL)
2413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 */
2416extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2417 size_t size, struct k_sem *sem);
2418
2419/**
Allan Stephensc98da842016-11-11 15:45:03 -05002420 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421 */
2422
Allan Stephensc98da842016-11-11 15:45:03 -05002423/**
2424 * @cond INTERNAL_HIDDEN
2425 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002427struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002429 uint32_t num_blocks;
2430 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431 char *buffer;
2432 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002433 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002435 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436};
2437
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002438#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2439 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440 { \
2441 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002442 .num_blocks = slab_num_blocks, \
2443 .block_size = slab_block_size, \
2444 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 .free_list = NULL, \
2446 .num_used = 0, \
2447 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
2448 }
2449
Peter Mitsis578f9112016-10-07 13:50:31 -04002450/**
Allan Stephensc98da842016-11-11 15:45:03 -05002451 * INTERNAL_HIDDEN @endcond
2452 */
2453
2454/**
2455 * @defgroup mem_slab_apis Memory Slab APIs
2456 * @ingroup kernel_apis
2457 * @{
2458 */
2459
2460/**
Allan Stephensda827222016-11-09 14:23:58 -06002461 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002462 *
Allan Stephensda827222016-11-09 14:23:58 -06002463 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002464 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002465 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2466 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002467 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002468 *
Allan Stephensda827222016-11-09 14:23:58 -06002469 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002472 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * @param name Name of the memory slab.
2475 * @param slab_block_size Size of each memory block (in bytes).
2476 * @param slab_num_blocks Number memory blocks.
2477 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002478 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002479#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2480 char __noinit __aligned(slab_align) \
2481 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2482 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002483 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002484 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2485 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002487/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002488 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002491 *
Allan Stephensda827222016-11-09 14:23:58 -06002492 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2493 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2494 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2495 * To ensure that each memory block is similarly aligned to this boundary,
2496 * @a slab_block_size must also be a multiple of N.
2497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @param slab Address of the memory slab.
2499 * @param buffer Pointer to buffer used for the memory blocks.
2500 * @param block_size Size of each memory block (in bytes).
2501 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002502 *
2503 * @return N/A
2504 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002505extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002506 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002507
2508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @param slab Address of the memory slab.
2514 * @param mem Pointer to block address area.
2515 * @param timeout Maximum time to wait for operation to complete
2516 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2517 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002518 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002519 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002521 * @retval -ENOMEM Returned without waiting.
2522 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002523 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002524extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2525 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002526
2527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * This routine releases a previously allocated memory block back to its
2531 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @param slab Address of the memory slab.
2534 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002535 *
2536 * @return N/A
2537 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002538extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * This routine gets the number of memory blocks that are currently
2544 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002549 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002550static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002552 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553}
2554
Peter Mitsisc001aa82016-10-13 13:53:37 -04002555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine gets the number of memory blocks that are currently
2559 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002564 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002565static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002566{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002567 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002568}
2569
Allan Stephensc98da842016-11-11 15:45:03 -05002570/**
2571 * @} end defgroup mem_slab_apis
2572 */
2573
2574/**
2575 * @cond INTERNAL_HIDDEN
2576 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002578/*
2579 * Memory pool requires a buffer and two arrays of structures for the
2580 * memory block accounting:
2581 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2582 * status of four blocks of memory.
2583 */
2584struct k_mem_pool_quad_block {
2585 char *mem_blocks; /* pointer to the first of four memory blocks */
2586 uint32_t mem_status; /* four bits. If bit is set, memory block is
2587 allocated */
2588};
2589/*
2590 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2591 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2592 * block size is 4 times less than the previous one and thus requires 4 times
2593 * bigger array of k_mem_pool_quad_block structures to keep track of the
2594 * memory blocks.
2595 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002597/*
2598 * The array of k_mem_pool_block_set keeps the information of each array of
2599 * k_mem_pool_quad_block structures
2600 */
2601struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002602 size_t block_size; /* memory block size */
2603 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002604 struct k_mem_pool_quad_block *quad_block;
2605 int count;
2606};
2607
2608/* Memory pool descriptor */
2609struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002610 size_t max_block_size;
2611 size_t min_block_size;
2612 uint32_t nr_of_maxblocks;
2613 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002614 struct k_mem_pool_block_set *block_set;
2615 char *bufblock;
2616 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2618};
2619
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002620#ifdef CONFIG_ARM
2621#define _SECTION_TYPE_SIGN "%"
2622#else
2623#define _SECTION_TYPE_SIGN "@"
2624#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002626/*
2627 * Static memory pool initialization
2628 */
Allan Stephensc98da842016-11-11 15:45:03 -05002629
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002630/*
2631 * Use .altmacro to be able to recalculate values and pass them as string
2632 * arguments when calling assembler macros resursively
2633 */
2634__asm__(".altmacro\n\t");
2635
2636/*
2637 * Recursively calls a macro
2638 * The followig global symbols need to be initialized:
2639 * __memory_pool_max_block_size - maximal size of the memory block
2640 * __memory_pool_min_block_size - minimal size of the memory block
2641 * Notes:
2642 * Global symbols are used due the fact that assembler macro allows only
2643 * one argument be passed with the % conversion
2644 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2645 * is used instead of / 4.
2646 * n_max argument needs to go first in the invoked macro, as some
2647 * assemblers concatenate \name and %(\n_max * 4) arguments
2648 * if \name goes first
2649 */
2650__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2651 ".ifge __memory_pool_max_block_size >> 2 -"
2652 " __memory_pool_min_block_size\n\t\t"
2653 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2654 "\\macro_name %(\\n_max * 4) \\name\n\t"
2655 ".endif\n\t"
2656 ".endm\n");
2657
2658/*
2659 * Build quad blocks
2660 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2661 * structures and recursively calls itself for the next array, 4 times
2662 * larger.
2663 * The followig global symbols need to be initialized:
2664 * __memory_pool_max_block_size - maximal size of the memory block
2665 * __memory_pool_min_block_size - minimal size of the memory block
2666 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2667 */
2668__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002669 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002670 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2671 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2672 ".if \\n_max % 4\n\t\t"
2673 ".skip __memory_pool_quad_block_size\n\t"
2674 ".endif\n\t"
2675 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2676 ".endm\n");
2677
2678/*
2679 * Build block sets and initialize them
2680 * Macro initializes the k_mem_pool_block_set structure and
2681 * recursively calls itself for the next one.
2682 * The followig global symbols need to be initialized:
2683 * __memory_pool_max_block_size - maximal size of the memory block
2684 * __memory_pool_min_block_size - minimal size of the memory block
2685 * __memory_pool_block_set_count, the number of the elements in the
2686 * block set array must be set to 0. Macro calculates it's real
2687 * value.
2688 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2689 * structures, _build_quad_blocks must be called prior it.
2690 */
2691__asm__(".macro _build_block_set n_max, name\n\t"
2692 ".int __memory_pool_max_block_size\n\t" /* block_size */
2693 ".if \\n_max % 4\n\t\t"
2694 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2695 ".else\n\t\t"
2696 ".int \\n_max >> 2\n\t"
2697 ".endif\n\t"
2698 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2699 ".int 0\n\t" /* count */
2700 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2701 "__do_recurse _build_block_set \\name \\n_max\n\t"
2702 ".endm\n");
2703
2704/*
2705 * Build a memory pool structure and initialize it
2706 * Macro uses __memory_pool_block_set_count global symbol,
2707 * block set addresses and buffer address, it may be called only after
2708 * _build_block_set
2709 */
2710__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002711 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002712 _SECTION_TYPE_SIGN "progbits\n\t"
2713 ".globl \\name\n\t"
2714 "\\name:\n\t"
2715 ".int \\max_size\n\t" /* max_block_size */
2716 ".int \\min_size\n\t" /* min_block_size */
2717 ".int \\n_max\n\t" /* nr_of_maxblocks */
2718 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2719 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2720 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2721 ".int 0\n\t" /* wait_q->head */
2722 ".int 0\n\t" /* wait_q->next */
2723 ".popsection\n\t"
2724 ".endm\n");
2725
2726#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2727 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2728 _SECTION_TYPE_SIGN "progbits\n\t"); \
2729 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2730 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2731 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2732 STRINGIFY(name) "\n\t"); \
2733 __asm__(".popsection\n\t")
2734
2735#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2736 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2737 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2738 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2739 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002740 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002741 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2742 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2743 STRINGIFY(name) "\n\t"); \
2744 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2745 __asm__(".int __memory_pool_block_set_count\n\t"); \
2746 __asm__(".popsection\n\t"); \
2747 extern uint32_t _mem_pool_block_set_count_##name; \
2748 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2749
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002750#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2751 char __noinit __aligned(align) \
2752 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002753
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002754/*
2755 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2756 * to __memory_pool_quad_block_size absolute symbol.
2757 * This function does not get called, but compiler calculates the value and
2758 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2759 */
2760static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2761{
2762 __asm__(".globl __memory_pool_quad_block_size\n\t"
2763#ifdef CONFIG_NIOS2
2764 "__memory_pool_quad_block_size = %0\n\t"
2765#else
2766 "__memory_pool_quad_block_size = %c0\n\t"
2767#endif
2768 :
2769 : "n"(sizeof(struct k_mem_pool_quad_block)));
2770}
2771
2772/**
Allan Stephensc98da842016-11-11 15:45:03 -05002773 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002774 */
2775
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002776/**
Allan Stephensc98da842016-11-11 15:45:03 -05002777 * @addtogroup mem_pool_apis
2778 * @{
2779 */
2780
2781/**
2782 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2785 * long. The memory pool allows blocks to be repeatedly partitioned into
2786 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2787 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002788 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002789 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002790 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791 * If the pool is to be accessed outside the module where it is defined, it
2792 * can be declared via
2793 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002794 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @param name Name of the memory pool.
2797 * @param min_size Size of the smallest blocks in the pool (in bytes).
2798 * @param max_size Size of the largest blocks in the pool (in bytes).
2799 * @param n_max Number of maximum sized blocks in the pool.
2800 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002801 */
2802#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002803 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2804 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002805 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002806 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2807 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2808 extern struct k_mem_pool name
2809
Peter Mitsis937042c2016-10-13 13:18:26 -04002810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002811 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * @param pool Address of the memory pool.
2816 * @param block Pointer to block descriptor for the allocated memory.
2817 * @param size Amount of memory to allocate (in bytes).
2818 * @param timeout Maximum time to wait for operation to complete
2819 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2820 * or K_FOREVER to wait as long as necessary.
2821 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002822 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002824 * @retval -ENOMEM Returned without waiting.
2825 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002826 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002827extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002828 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002829
2830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * This routine releases a previously allocated memory block back to its
2834 * memory pool.
2835 *
2836 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002837 *
2838 * @return N/A
2839 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002841
2842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002843 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * This routine instructs a memory pool to concatenate unused memory blocks
2846 * into larger blocks wherever possible. Manually defragmenting the memory
2847 * pool may speed up future allocations of memory blocks by eliminating the
2848 * need for the memory pool to perform an automatic partial defragmentation.
2849 *
2850 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002851 *
2852 * @return N/A
2853 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002854extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002855
2856/**
Allan Stephensc98da842016-11-11 15:45:03 -05002857 * @} end addtogroup mem_pool_apis
2858 */
2859
2860/**
2861 * @defgroup heap_apis Heap Memory Pool APIs
2862 * @ingroup kernel_apis
2863 * @{
2864 */
2865
2866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002870 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002875 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002876extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002877
2878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002880 *
2881 * This routine provides traditional free() semantics. The memory being
2882 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002885 *
2886 * @return N/A
2887 */
2888extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889
Allan Stephensc98da842016-11-11 15:45:03 -05002890/**
2891 * @} end defgroup heap_apis
2892 */
2893
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894/*
2895 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2896 * hook into the device subsystem, which itself uses nanokernel semaphores,
2897 * and thus currently requires the definition of nano_sem.
2898 */
2899#include <legacy.h>
2900#include <arch/cpu.h>
2901
2902/*
2903 * private APIs that are utilized by one or more public APIs
2904 */
2905
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002907extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002908extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002909
2910#ifdef __cplusplus
2911}
2912#endif
2913
Andrew Boiee004dec2016-11-07 09:01:19 -08002914#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2915/*
2916 * Define new and delete operators.
2917 * At this moment, the operators do nothing since objects are supposed
2918 * to be statically allocated.
2919 */
2920inline void operator delete(void *ptr)
2921{
2922 (void)ptr;
2923}
2924
2925inline void operator delete[](void *ptr)
2926{
2927 (void)ptr;
2928}
2929
2930inline void *operator new(size_t size)
2931{
2932 (void)size;
2933 return NULL;
2934}
2935
2936inline void *operator new[](size_t size)
2937{
2938 (void)size;
2939 return NULL;
2940}
2941
2942/* Placement versions of operator new and delete */
2943inline void operator delete(void *ptr1, void *ptr2)
2944{
2945 (void)ptr1;
2946 (void)ptr2;
2947}
2948
2949inline void operator delete[](void *ptr1, void *ptr2)
2950{
2951 (void)ptr1;
2952 (void)ptr2;
2953}
2954
2955inline void *operator new(size_t size, void *ptr)
2956{
2957 (void)size;
2958 return ptr;
2959}
2960
2961inline void *operator new[](size_t size, void *ptr)
2962{
2963 (void)size;
2964 return ptr;
2965}
2966
2967#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2968
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969#endif /* _kernel__h_ */