blob: 9f678349ecd3d49febcb1bb61716b566aa1c4761 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
Anas Nashif61f4b242016-11-18 10:53:59 -050040#ifdef CONFIG_KERNEL_DEBUG
41#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040042#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
43#else
44#define K_DEBUG(fmt, ...)
45#endif
46
47#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
48#define K_PRIO_PREEMPT(x) (x)
49
Benjamin Walsh456c6da2016-09-02 18:55:39 -040050#define K_ANY NULL
51#define K_END NULL
52
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#if CONFIG_NUM_COOP_PRIORITIES > 0
54#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
55#else
56#define K_HIGHEST_THREAD_PRIO 0
57#endif
58
59#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
60#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
61#else
62#define K_LOWEST_THREAD_PRIO -1
63#endif
64
Benjamin Walshfab8d922016-11-08 15:36:36 -050065#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
68#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
69
70typedef sys_dlist_t _wait_q_t;
71
Anas Nashif2f203c22016-12-18 06:57:45 -050072#ifdef CONFIG_OBJECT_TRACING
73#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
74#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#else
Anas Nashif2f203c22016-12-18 06:57:45 -050076#define _OBJECT_TRACING_INIT
77#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050080#define tcs k_thread
81struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082struct k_mutex;
83struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040084struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040091struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040092struct k_mem_pool;
93struct k_timer;
94
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040095typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097enum execution_context_types {
98 K_ISR = 0,
99 K_COOP_THREAD,
100 K_PREEMPT_THREAD,
101};
102
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400103/**
Allan Stephensc98da842016-11-11 15:45:03 -0500104 * @defgroup thread_apis Thread APIs
105 * @ingroup kernel_apis
106 * @{
107 */
108
109/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500110 * @typedef k_thread_entry_t
111 * @brief Thread entry point function type.
112 *
113 * A thread's entry point function is invoked when the thread starts executing.
114 * Up to 3 argument values can be passed to the function.
115 *
116 * The thread terminates execution permanently if the entry point function
117 * returns. The thread is responsible for releasing any shared resources
118 * it may own (such as mutexes and dynamically allocated memory), prior to
119 * returning.
120 *
121 * @param p1 First argument.
122 * @param p2 Second argument.
123 * @param p3 Third argument.
124 *
125 * @return N/A
126 */
127typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
128
129/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500130 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500132 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500134 * The new thread may be scheduled for immediate execution or a delayed start.
135 * If the newly spawned thread does not have a delayed start the kernel
136 * scheduler may preempt the current thread to allow the new thread to
137 * execute.
138 *
139 * Thread options are architecture-specific, and can include K_ESSENTIAL,
140 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
141 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400142 *
143 * @param stack Pointer to the stack space.
144 * @param stack_size Stack size in bytes.
145 * @param entry Thread entry function.
146 * @param p1 1st entry point parameter.
147 * @param p2 2nd entry point parameter.
148 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500149 * @param prio Thread priority.
150 * @param options Thread options.
151 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500153 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500155extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500156 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500158 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500161 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400162 *
Allan Stephensc98da842016-11-11 15:45:03 -0500163 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500164 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
168 * @return N/A
169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400170extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400171
172/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500173 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400174 *
175 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500176 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400177 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 * @return N/A
179 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400180extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400181
182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500183 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500185 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400186 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500187 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 *
189 * @return N/A
190 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400191extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400192
193/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500194 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * If @a thread is not currently sleeping, the routine has no effect.
199 *
200 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201 *
202 * @return N/A
203 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400205
206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500207 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500209 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400211extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212
213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500214 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500216 * This routine prevents @a thread from executing if it has not yet started
217 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @param thread ID of thread to cancel.
220 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500221 * @retval 0 Thread spawning cancelled.
222 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400224extern int k_thread_cancel(k_tid_t thread);
225
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400226/**
Allan Stephensc98da842016-11-11 15:45:03 -0500227 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500229 * This routine permanently stops execution of @a thread. The thread is taken
230 * off all kernel queues it is part of (i.e. the ready queue, the timeout
231 * queue, or a kernel object wait queue). However, any kernel resources the
232 * thread might currently own (such as mutexes or memory blocks) are not
233 * released. It is the responsibility of the caller of this routine to ensure
234 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500236 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400237 *
238 * @return N/A
239 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400240extern void k_thread_abort(k_tid_t thread);
241
Allan Stephensc98da842016-11-11 15:45:03 -0500242/**
243 * @cond INTERNAL_HIDDEN
244 */
245
Benjamin Walshd211a522016-12-06 11:44:01 -0500246/* timeout has timed out and is not on _timeout_q anymore */
247#define _EXPIRED (-2)
248
249/* timeout is not in use */
250#define _INACTIVE (-1)
251
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400252#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400253#define _THREAD_TIMEOUT_INIT(obj) \
254 (obj).nano_timeout = { \
255 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400256 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400257 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500258 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259 },
260#else
261#define _THREAD_TIMEOUT_INIT(obj)
262#endif
263
264#ifdef CONFIG_ERRNO
265#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
266#else
267#define _THREAD_ERRNO_INIT(obj)
268#endif
269
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400270struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400271 union {
272 char *init_stack;
273 struct k_thread *thread;
274 };
275 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500276 void (*init_entry)(void *, void *, void *);
277 void *init_p1;
278 void *init_p2;
279 void *init_p3;
280 int init_prio;
281 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400282 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500283 void (*init_abort)(void);
284 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400285};
286
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400287#define _THREAD_INITIALIZER(stack, stack_size, \
288 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500289 prio, options, delay, abort, groups) \
290 { \
291 .init_stack = (stack), \
292 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400293 .init_entry = (void (*)(void *, void *, void *))entry, \
294 .init_p1 = (void *)p1, \
295 .init_p2 = (void *)p2, \
296 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500297 .init_prio = (prio), \
298 .init_options = (options), \
299 .init_delay = (delay), \
300 .init_abort = (abort), \
301 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400302 }
303
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400304/**
Allan Stephensc98da842016-11-11 15:45:03 -0500305 * INTERNAL_HIDDEN @endcond
306 */
307
308/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500309 * @brief Statically define and initialize a thread.
310 *
311 * The thread may be scheduled for immediate execution or a delayed start.
312 *
313 * Thread options are architecture-specific, and can include K_ESSENTIAL,
314 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
315 * them using "|" (the logical OR operator).
316 *
317 * The ID of the thread can be accessed using:
318 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500319 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500320 *
321 * @param name Name of the thread.
322 * @param stack_size Stack size in bytes.
323 * @param entry Thread entry function.
324 * @param p1 1st entry point parameter.
325 * @param p2 2nd entry point parameter.
326 * @param p3 3rd entry point parameter.
327 * @param prio Thread priority.
328 * @param options Thread options.
329 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400330 *
331 * @internal It has been observed that the x86 compiler by default aligns
332 * these _static_thread_data structures to 32-byte boundaries, thereby
333 * wasting space. To work around this, force a 4-byte alignment.
334 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500335#define K_THREAD_DEFINE(name, stack_size, \
336 entry, p1, p2, p3, \
337 prio, options, delay) \
338 char __noinit __stack _k_thread_obj_##name[stack_size]; \
339 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500340 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500341 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
342 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500343 NULL, 0); \
344 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400345
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500347 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500349 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * @param thread ID of thread whose priority is needed.
352 *
353 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400354 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500355extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400356
357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500358 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500360 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400361 *
362 * Rescheduling can occur immediately depending on the priority @a thread is
363 * set to:
364 *
365 * - If its priority is raised above the priority of the caller of this
366 * function, and the caller is preemptible, @a thread will be scheduled in.
367 *
368 * - If the caller operates on itself, it lowers its priority below that of
369 * other threads in the system, and the caller is preemptible, the thread of
370 * highest priority will be scheduled in.
371 *
372 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
373 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
374 * highest priority.
375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500376 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400377 * @param prio New priority.
378 *
379 * @warning Changing the priority of a thread currently involved in mutex
380 * priority inheritance may result in undefined behavior.
381 *
382 * @return N/A
383 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384extern void k_thread_priority_set(k_tid_t thread, int prio);
385
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500387 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500389 * This routine prevents the kernel scheduler from making @a thread the
390 * current thread. All other internal operations on @a thread are still
391 * performed; for example, any timeout it is waiting on keeps ticking,
392 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500394 * If @a thread is already suspended, the routine has no effect.
395 *
396 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400397 *
398 * @return N/A
399 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400400extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400401
402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500403 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500405 * This routine allows the kernel scheduler to make @a thread the current
406 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500408 * If @a thread is not currently suspended, the routine has no effect.
409 *
410 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400411 *
412 * @return N/A
413 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400414extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400415
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500417 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500419 * This routine specifies how the scheduler will perform time slicing of
420 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500422 * To enable time slicing, @a slice must be non-zero. The scheduler
423 * ensures that no thread runs for more than the specified time limit
424 * before other threads of that priority are given a chance to execute.
425 * Any thread whose priority is higher than @a prio is exempted, and may
426 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500428 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400429 * execute. Once the scheduler selects a thread for execution, there is no
430 * minimum guaranteed time the thread will execute before threads of greater or
431 * equal priority are scheduled.
432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500433 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 * for execution, this routine has no effect; the thread is immediately
435 * rescheduled after the slice period expires.
436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500437 * To disable timeslicing, set both @a slice and @a prio to zero.
438 *
439 * @param slice Maximum time slice length (in milliseconds).
440 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441 *
442 * @return N/A
443 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400444extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400446/**
Allan Stephensc98da842016-11-11 15:45:03 -0500447 * @} end defgroup thread_apis
448 */
449
450/**
451 * @addtogroup isr_apis
452 * @{
453 */
454
455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500456 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457 *
Allan Stephensc98da842016-11-11 15:45:03 -0500458 * This routine allows the caller to customize its actions, depending on
459 * whether it is a thread or an ISR.
460 *
461 * @note Can be called by ISRs.
462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * @return 0 if invoked by a thread.
464 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500466extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400467
Benjamin Walsh445830d2016-11-10 15:54:27 -0500468/**
469 * @brief Determine if code is running in a preemptible thread.
470 *
Allan Stephensc98da842016-11-11 15:45:03 -0500471 * This routine allows the caller to customize its actions, depending on
472 * whether it can be preempted by another thread. The routine returns a 'true'
473 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500474 *
Allan Stephensc98da842016-11-11 15:45:03 -0500475 * - The code is running in a thread, not at ISR.
476 * - The thread's priority is in the preemptible range.
477 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500478 *
Allan Stephensc98da842016-11-11 15:45:03 -0500479 * @note Can be called by ISRs.
480 *
481 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500482 * @return Non-zero if invoked by a preemptible thread.
483 */
484extern int k_is_preempt_thread(void);
485
Allan Stephensc98da842016-11-11 15:45:03 -0500486/**
487 * @} end addtogroup isr_apis
488 */
489
490/**
491 * @addtogroup thread_apis
492 * @{
493 */
494
495/**
496 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500497 *
Allan Stephensc98da842016-11-11 15:45:03 -0500498 * This routine prevents the current thread from being preempted by another
499 * thread by instructing the scheduler to treat it as a cooperative thread.
500 * If the thread subsequently performs an operation that makes it unready,
501 * it will be context switched out in the normal manner. When the thread
502 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500503 *
Allan Stephensc98da842016-11-11 15:45:03 -0500504 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500505 *
Allan Stephensc98da842016-11-11 15:45:03 -0500506 * @note k_sched_lock() and k_sched_unlock() should normally be used
507 * when the operation being performed can be safely interrupted by ISRs.
508 * However, if the amount of processing involved is very small, better
509 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500510 *
511 * @return N/A
512 */
513extern void k_sched_lock(void);
514
Allan Stephensc98da842016-11-11 15:45:03 -0500515/**
516 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500517 *
Allan Stephensc98da842016-11-11 15:45:03 -0500518 * This routine reverses the effect of a previous call to k_sched_lock().
519 * A thread must call the routine once for each time it called k_sched_lock()
520 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500521 *
522 * @return N/A
523 */
524extern void k_sched_unlock(void);
525
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500527 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * Custom data is not used by the kernel itself, and is freely available
532 * for a thread to use as it sees fit. It can be used as a framework
533 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500535 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536 *
537 * @return N/A
538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400539extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400540
541/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500542 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500544 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500546 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400547 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400548extern void *k_thread_custom_data_get(void);
549
550/**
Allan Stephensc98da842016-11-11 15:45:03 -0500551 * @} end addtogroup thread_apis
552 */
553
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400554#include <sys_clock.h>
555
Allan Stephensc2f15a42016-11-17 12:24:22 -0500556/**
557 * @addtogroup clock_apis
558 * @{
559 */
560
561/**
562 * @brief Generate null timeout delay.
563 *
564 * This macro generates a timeout delay that that instructs a kernel API
565 * not to wait if the requested operation cannot be performed immediately.
566 *
567 * @return Timeout delay value.
568 */
569#define K_NO_WAIT 0
570
571/**
572 * @brief Generate timeout delay from milliseconds.
573 *
574 * This macro generates a timeout delay that that instructs a kernel API
575 * to wait up to @a ms milliseconds to perform the requested operation.
576 *
577 * @param ms Duration in milliseconds.
578 *
579 * @return Timeout delay value.
580 */
Johan Hedberg14471692016-11-13 10:52:15 +0200581#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500582
583/**
584 * @brief Generate timeout delay from seconds.
585 *
586 * This macro generates a timeout delay that that instructs a kernel API
587 * to wait up to @a s seconds to perform the requested operation.
588 *
589 * @param s Duration in seconds.
590 *
591 * @return Timeout delay value.
592 */
Johan Hedberg14471692016-11-13 10:52:15 +0200593#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500594
595/**
596 * @brief Generate timeout delay from minutes.
597 *
598 * This macro generates a timeout delay that that instructs a kernel API
599 * to wait up to @a m minutes to perform the requested operation.
600 *
601 * @param m Duration in minutes.
602 *
603 * @return Timeout delay value.
604 */
Johan Hedberg14471692016-11-13 10:52:15 +0200605#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500606
607/**
608 * @brief Generate timeout delay from hours.
609 *
610 * This macro generates a timeout delay that that instructs a kernel API
611 * to wait up to @a h hours to perform the requested operation.
612 *
613 * @param h Duration in hours.
614 *
615 * @return Timeout delay value.
616 */
Johan Hedberg14471692016-11-13 10:52:15 +0200617#define K_HOURS(h) K_MINUTES((h) * 60)
618
Allan Stephensc98da842016-11-11 15:45:03 -0500619/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500620 * @brief Generate infinite timeout delay.
621 *
622 * This macro generates a timeout delay that that instructs a kernel API
623 * to wait as long as necessary to perform the requested operation.
624 *
625 * @return Timeout delay value.
626 */
627#define K_FOREVER (-1)
628
629/**
630 * @} end addtogroup clock_apis
631 */
632
633/**
Allan Stephensc98da842016-11-11 15:45:03 -0500634 * @cond INTERNAL_HIDDEN
635 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400636
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500637/* added tick needed to account for tick in progress */
638#define _TICK_ALIGN 1
639
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400640static int64_t __ticks_to_ms(int64_t ticks)
641{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400642#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400643 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400644#else
645 __ASSERT(ticks == 0, "");
646 return 0;
647#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400648}
649
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400650/* timeouts */
651
652struct _timeout;
653typedef void (*_timeout_func_t)(struct _timeout *t);
654
655struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500656 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400657 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400658 sys_dlist_t *wait_q;
659 int32_t delta_ticks_from_prev;
660 _timeout_func_t func;
661};
662
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200663extern int32_t _timeout_remaining_get(struct _timeout *timeout);
664
Allan Stephensc98da842016-11-11 15:45:03 -0500665/**
666 * INTERNAL_HIDDEN @endcond
667 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500668
Allan Stephensc98da842016-11-11 15:45:03 -0500669/**
670 * @cond INTERNAL_HIDDEN
671 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400672
673struct k_timer {
674 /*
675 * _timeout structure must be first here if we want to use
676 * dynamic timer allocation. timeout.node is used in the double-linked
677 * list of free timers
678 */
679 struct _timeout timeout;
680
Allan Stephens45bfa372016-10-12 12:39:42 -0500681 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400682 _wait_q_t wait_q;
683
684 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500685 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400686
687 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500688 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400689
690 /* timer period */
691 int32_t period;
692
Allan Stephens45bfa372016-10-12 12:39:42 -0500693 /* timer status */
694 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400695
Allan Stephens45bfa372016-10-12 12:39:42 -0500696 /* used to support legacy timer APIs */
697 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400698
Anas Nashif2f203c22016-12-18 06:57:45 -0500699 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400700};
701
Allan Stephens1342adb2016-11-03 13:54:53 -0500702#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400703 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500704 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500705 .timeout.wait_q = NULL, \
706 .timeout.thread = NULL, \
707 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400708 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500709 .expiry_fn = expiry, \
710 .stop_fn = stop, \
711 .status = 0, \
712 ._legacy_data = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500713 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400714 }
715
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400716/**
Allan Stephensc98da842016-11-11 15:45:03 -0500717 * INTERNAL_HIDDEN @endcond
718 */
719
720/**
721 * @defgroup timer_apis Timer APIs
722 * @ingroup kernel_apis
723 * @{
724 */
725
726/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500727 * @typedef k_timer_expiry_t
728 * @brief Timer expiry function type.
729 *
730 * A timer's expiry function is executed by the system clock interrupt handler
731 * each time the timer expires. The expiry function is optional, and is only
732 * invoked if the timer has been initialized with one.
733 *
734 * @param timer Address of timer.
735 *
736 * @return N/A
737 */
738typedef void (*k_timer_expiry_t)(struct k_timer *timer);
739
740/**
741 * @typedef k_timer_stop_t
742 * @brief Timer stop function type.
743 *
744 * A timer's stop function is executed if the timer is stopped prematurely.
745 * The function runs in the context of the thread that stops the timer.
746 * The stop function is optional, and is only invoked if the timer has been
747 * initialized with one.
748 *
749 * @param timer Address of timer.
750 *
751 * @return N/A
752 */
753typedef void (*k_timer_stop_t)(struct k_timer *timer);
754
755/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500756 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500758 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500760 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400761 *
762 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500763 * @param expiry_fn Function to invoke each time the timer expires.
764 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400765 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500766#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500767 struct k_timer name \
768 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500769 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400770
Allan Stephens45bfa372016-10-12 12:39:42 -0500771/**
772 * @brief Initialize a timer.
773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500774 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500775 *
776 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * @param expiry_fn Function to invoke each time the timer expires.
778 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500779 *
780 * @return N/A
781 */
782extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500783 k_timer_expiry_t expiry_fn,
784 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700785
Allan Stephens45bfa372016-10-12 12:39:42 -0500786/**
787 * @brief Start a timer.
788 *
789 * This routine starts a timer, and resets its status to zero. The timer
790 * begins counting down using the specified duration and period values.
791 *
792 * Attempting to start a timer that is already running is permitted.
793 * The timer's status is reset to zero and the timer begins counting down
794 * using the new duration and period values.
795 *
796 * @param timer Address of timer.
797 * @param duration Initial timer duration (in milliseconds).
798 * @param period Timer period (in milliseconds).
799 *
800 * @return N/A
801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400802extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500803 int32_t duration, int32_t period);
804
805/**
806 * @brief Stop a timer.
807 *
808 * This routine stops a running timer prematurely. The timer's stop function,
809 * if one exists, is invoked by the caller.
810 *
811 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500813 *
814 * @param timer Address of timer.
815 *
816 * @return N/A
817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400818extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500819
820/**
821 * @brief Read timer status.
822 *
823 * This routine reads the timer's status, which indicates the number of times
824 * it has expired since its status was last read.
825 *
826 * Calling this routine resets the timer's status to zero.
827 *
828 * @param timer Address of timer.
829 *
830 * @return Timer status.
831 */
832extern uint32_t k_timer_status_get(struct k_timer *timer);
833
834/**
835 * @brief Synchronize thread to timer expiration.
836 *
837 * This routine blocks the calling thread until the timer's status is non-zero
838 * (indicating that it has expired at least once since it was last examined)
839 * or the timer is stopped. If the timer status is already non-zero,
840 * or the timer is already stopped, the caller continues without waiting.
841 *
842 * Calling this routine resets the timer's status to zero.
843 *
844 * This routine must not be used by interrupt handlers, since they are not
845 * allowed to block.
846 *
847 * @param timer Address of timer.
848 *
849 * @return Timer status.
850 */
851extern uint32_t k_timer_status_sync(struct k_timer *timer);
852
853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500855 *
856 * This routine computes the (approximate) time remaining before a running
857 * timer next expires. If the timer is not running, it returns zero.
858 *
859 * @param timer Address of timer.
860 *
861 * @return Remaining time (in milliseconds).
862 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200863static inline int32_t k_timer_remaining_get(struct k_timer *timer)
864{
865 return _timeout_remaining_get(&timer->timeout);
866}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400867
Allan Stephensc98da842016-11-11 15:45:03 -0500868/**
869 * @} end defgroup timer_apis
870 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400871
Allan Stephensc98da842016-11-11 15:45:03 -0500872/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500873 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500874 * @{
875 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500876
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * This routine returns the elapsed time since the system booted,
881 * in milliseconds.
882 *
883 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400884 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400885extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400886
887/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * This routine returns the lower 32-bits of the elapsed time since the system
891 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * This routine can be more efficient than k_uptime_get(), as it reduces the
894 * need for interrupt locking and 64-bit math. However, the 32-bit result
895 * cannot hold a system uptime time larger than approximately 50 days, so the
896 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500898 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400899 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400900extern uint32_t k_uptime_get_32(void);
901
902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500903 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * This routine computes the elapsed time between the current system uptime
906 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * @param reftime Pointer to a reference time, which is updated to the current
909 * uptime upon return.
910 *
911 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400914
915/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * This routine computes the elapsed time between the current system uptime
919 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * This routine can be more efficient than k_uptime_delta(), as it reduces the
922 * need for interrupt locking and 64-bit math. However, the 32-bit result
923 * cannot hold an elapsed time larger than approximately 50 days, so the
924 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * @param reftime Pointer to a reference time, which is updated to the current
927 * uptime upon return.
928 *
929 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400930 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400931extern uint32_t k_uptime_delta_32(int64_t *reftime);
932
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500936 * This routine returns the current time, as measured by the system's hardware
937 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941extern uint32_t k_cycle_get_32(void);
942
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500944 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945 */
946
Allan Stephensc98da842016-11-11 15:45:03 -0500947/**
948 * @cond INTERNAL_HIDDEN
949 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950
951struct k_fifo {
952 _wait_q_t wait_q;
953 sys_slist_t data_q;
954
Anas Nashif2f203c22016-12-18 06:57:45 -0500955 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400956};
957
Allan Stephensc98da842016-11-11 15:45:03 -0500958#define K_FIFO_INITIALIZER(obj) \
959 { \
960 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
961 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -0500962 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -0500963 }
964
965/**
966 * INTERNAL_HIDDEN @endcond
967 */
968
969/**
970 * @defgroup fifo_apis Fifo APIs
971 * @ingroup kernel_apis
972 * @{
973 */
974
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500976 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500978 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500980 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
982 * @return N/A
983 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400984extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985
986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * This routine adds a data item to @a fifo. A fifo data item must be
990 * aligned on a 4-byte boundary, and the first 32 bits of the item are
991 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @note Can be called by ISRs.
994 *
995 * @param fifo Address of the fifo.
996 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
998 * @return N/A
999 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001000extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001
1002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * This routine adds a list of data items to @a fifo in one operation.
1006 * The data items must be in a singly-linked list, with the first 32 bits
1007 * each data item pointing to the next data item; the list must be
1008 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * @param fifo Address of the fifo.
1013 * @param head Pointer to first node in singly-linked list.
1014 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
1016 * @return N/A
1017 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001018extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019
1020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * This routine adds a list of data items to @a fifo in one operation.
1024 * The data items must be in a singly-linked list implemented using a
1025 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 * and must be re-initialized via sys_slist_init().
1027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @note Can be called by ISRs.
1029 *
1030 * @param fifo Address of the fifo.
1031 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 *
1033 * @return N/A
1034 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001035extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036
1037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * This routine removes a data item from @a fifo in a "first in, first out"
1041 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1044 *
1045 * @param fifo Address of the fifo.
1046 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 * or one of the special values K_NO_WAIT and K_FOREVER.
1048 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001049 * @return Address of the data item if successful; NULL if returned
1050 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1053
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001057 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001058 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001059 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001064 struct k_fifo name \
1065 __in_section(_k_fifo, static, name) = \
1066 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067
Allan Stephensc98da842016-11-11 15:45:03 -05001068/**
1069 * @} end defgroup fifo_apis
1070 */
1071
1072/**
1073 * @cond INTERNAL_HIDDEN
1074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001075
1076struct k_lifo {
1077 _wait_q_t wait_q;
1078 void *list;
1079
Anas Nashif2f203c22016-12-18 06:57:45 -05001080 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001081};
1082
Allan Stephensc98da842016-11-11 15:45:03 -05001083#define K_LIFO_INITIALIZER(obj) \
1084 { \
1085 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1086 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001087 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001088 }
1089
1090/**
1091 * INTERNAL_HIDDEN @endcond
1092 */
1093
1094/**
1095 * @defgroup lifo_apis Lifo APIs
1096 * @ingroup kernel_apis
1097 * @{
1098 */
1099
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001105 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
1107 * @return N/A
1108 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001110
1111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * This routine adds a data item to @a lifo. A lifo data item must be
1115 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1116 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @note Can be called by ISRs.
1119 *
1120 * @param lifo Address of the lifo.
1121 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
1123 * @return N/A
1124 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001125extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126
1127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001130 * This routine removes a data item from @a lifo in a "last in, first out"
1131 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1134 *
1135 * @param lifo Address of the lifo.
1136 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 * or one of the special values K_NO_WAIT and K_FOREVER.
1138 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001139 * @return Address of the data item if successful; NULL if returned
1140 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1143
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001147 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001149 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001153#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001154 struct k_lifo name \
1155 __in_section(_k_lifo, static, name) = \
1156 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157
Allan Stephensc98da842016-11-11 15:45:03 -05001158/**
1159 * @} end defgroup lifo_apis
1160 */
1161
1162/**
1163 * @cond INTERNAL_HIDDEN
1164 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165
1166struct k_stack {
1167 _wait_q_t wait_q;
1168 uint32_t *base, *next, *top;
1169
Anas Nashif2f203c22016-12-18 06:57:45 -05001170 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171};
1172
Allan Stephensc98da842016-11-11 15:45:03 -05001173#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1174 { \
1175 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1176 .base = stack_buffer, \
1177 .next = stack_buffer, \
1178 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001179 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001180 }
1181
1182/**
1183 * INTERNAL_HIDDEN @endcond
1184 */
1185
1186/**
1187 * @defgroup stack_apis Stack APIs
1188 * @ingroup kernel_apis
1189 * @{
1190 */
1191
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192/**
1193 * @brief Initialize a stack.
1194 *
1195 * This routine initializes a stack object, prior to its first use.
1196 *
1197 * @param stack Address of the stack.
1198 * @param buffer Address of array used to hold stacked values.
1199 * @param num_entries Maximum number of values that can be stacked.
1200 *
1201 * @return N/A
1202 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001203extern void k_stack_init(struct k_stack *stack,
1204 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001205
1206/**
1207 * @brief Push an element onto a stack.
1208 *
1209 * This routine adds a 32-bit value @a data to @a stack.
1210 *
1211 * @note Can be called by ISRs.
1212 *
1213 * @param stack Address of the stack.
1214 * @param data Value to push onto the stack.
1215 *
1216 * @return N/A
1217 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001218extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001219
1220/**
1221 * @brief Pop an element from a stack.
1222 *
1223 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1224 * manner and stores the value in @a data.
1225 *
1226 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1227 *
1228 * @param stack Address of the stack.
1229 * @param data Address of area to hold the value popped from the stack.
1230 * @param timeout Waiting period to obtain a value (in milliseconds),
1231 * or one of the special values K_NO_WAIT and K_FOREVER.
1232 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001233 * @retval 0 Element popped from stack.
1234 * @retval -EBUSY Returned without waiting.
1235 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001237extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1238
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001244 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * @param name Name of the stack.
1247 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001249#define K_STACK_DEFINE(name, stack_num_entries) \
1250 uint32_t __noinit \
1251 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001252 struct k_stack name \
1253 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001254 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1255 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001256
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001257/**
Allan Stephensc98da842016-11-11 15:45:03 -05001258 * @} end defgroup stack_apis
1259 */
1260
Allan Stephens6bba9b02016-11-16 14:56:54 -05001261struct k_work;
1262
Allan Stephensc98da842016-11-11 15:45:03 -05001263/**
1264 * @defgroup workqueue_apis Workqueue Thread APIs
1265 * @ingroup kernel_apis
1266 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001267 */
1268
Allan Stephens6bba9b02016-11-16 14:56:54 -05001269/**
1270 * @typedef k_work_handler_t
1271 * @brief Work item handler function type.
1272 *
1273 * A work item's handler function is executed by a workqueue's thread
1274 * when the work item is processed by the workqueue.
1275 *
1276 * @param work Address of the work item.
1277 *
1278 * @return N/A
1279 */
1280typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001281
1282/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001283 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001285
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286struct k_work_q {
1287 struct k_fifo fifo;
1288};
1289
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001291 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001292};
1293
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001294struct k_work {
1295 void *_reserved; /* Used by k_fifo implementation. */
1296 k_work_handler_t handler;
1297 atomic_t flags[1];
1298};
1299
Allan Stephens6bba9b02016-11-16 14:56:54 -05001300struct k_delayed_work {
1301 struct k_work work;
1302 struct _timeout timeout;
1303 struct k_work_q *work_q;
1304};
1305
1306extern struct k_work_q k_sys_work_q;
1307
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001308/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001309 * INTERNAL_HIDDEN @endcond
1310 */
1311
1312/**
1313 * @brief Initialize a statically-defined work item.
1314 *
1315 * This macro can be used to initialize a statically-defined workqueue work
1316 * item, prior to its first use. For example,
1317 *
1318 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1319 *
1320 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321 */
1322#define K_WORK_INITIALIZER(work_handler) \
1323 { \
1324 ._reserved = NULL, \
1325 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001326 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001327 }
1328
1329/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001330 * @brief Initialize a work item.
1331 *
1332 * This routine initializes a workqueue work item, prior to its first use.
1333 *
1334 * @param work Address of work item.
1335 * @param handler Function to invoke each time work item is processed.
1336 *
1337 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001338 */
1339static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1340{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001341 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001342 work->handler = handler;
1343}
1344
1345/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001346 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001347 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001348 * This routine submits work item @a work to be processed by workqueue
1349 * @a work_q. If the work item is already pending in the workqueue's queue
1350 * as a result of an earlier submission, this routine has no effect on the
1351 * work item. If the work item has already been processed, or is currently
1352 * being processed, its work is considered complete and the work item can be
1353 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001354 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001355 * @warning
1356 * A submitted work item must not be modified until it has been processed
1357 * by the workqueue.
1358 *
1359 * @note Can be called by ISRs.
1360 *
1361 * @param work_q Address of workqueue.
1362 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001363 *
1364 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001365 */
1366static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1367 struct k_work *work)
1368{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001369 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370 k_fifo_put(&work_q->fifo, work);
1371 }
1372}
1373
1374/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001375 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001376 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001377 * This routine indicates if work item @a work is pending in a workqueue's
1378 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001379 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001380 * @note Can be called by ISRs.
1381 *
1382 * @param work Address of work item.
1383 *
1384 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001385 */
1386static inline int k_work_pending(struct k_work *work)
1387{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001388 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001389}
1390
1391/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001392 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001394 * This routine starts workqueue @a work_q. The workqueue spawns its work
1395 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001396 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001397 * @param work_q Address of workqueue.
1398 * @param stack Pointer to work queue thread's stack space.
1399 * @param stack_size Size of the work queue thread's stack (in bytes).
1400 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001401 *
1402 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403 */
Allan Stephens904cf972016-10-07 13:59:23 -05001404extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001405 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001407/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001408 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001409 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001410 * This routine initializes a workqueue delayed work item, prior to
1411 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001413 * @param work Address of delayed work item.
1414 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001415 *
1416 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001418extern void k_delayed_work_init(struct k_delayed_work *work,
1419 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420
1421/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001422 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001424 * This routine schedules work item @a work to be processed by workqueue
1425 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1426 * an asychronous countdown for the work item and then returns to the caller.
1427 * Only when the countdown completes is the work item actually submitted to
1428 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001430 * Submitting a previously submitted delayed work item that is still
1431 * counting down cancels the existing submission and restarts the countdown
1432 * using the new delay. If the work item is currently pending on the
1433 * workqueue's queue because the countdown has completed it is too late to
1434 * resubmit the item, and resubmission fails without impacting the work item.
1435 * If the work item has already been processed, or is currently being processed,
1436 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001437 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001438 * @warning
1439 * A delayed work item must not be modified until it has been processed
1440 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001442 * @note Can be called by ISRs.
1443 *
1444 * @param work_q Address of workqueue.
1445 * @param work Address of delayed work item.
1446 * @param delay Delay before submitting the work item (in milliseconds).
1447 *
1448 * @retval 0 Work item countdown started.
1449 * @retval -EINPROGRESS Work item is already pending.
1450 * @retval -EINVAL Work item is being processed or has completed its work.
1451 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001452 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001453extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1454 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001455 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456
1457/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001460 * This routine cancels the submission of delayed work item @a work.
1461 * A delayed work item can only be cancelled while its countdown is still
1462 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001464 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001466 * @param work Address of delayed work item.
1467 *
1468 * @retval 0 Work item countdown cancelled.
1469 * @retval -EINPROGRESS Work item is already pending.
1470 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001471 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001472extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001473
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001475 * @brief Submit a work item to the system workqueue.
1476 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001477 * This routine submits work item @a work to be processed by the system
1478 * workqueue. If the work item is already pending in the workqueue's queue
1479 * as a result of an earlier submission, this routine has no effect on the
1480 * work item. If the work item has already been processed, or is currently
1481 * being processed, its work is considered complete and the work item can be
1482 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001483 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001484 * @warning
1485 * Work items submitted to the system workqueue should avoid using handlers
1486 * that block or yield since this may prevent the system workqueue from
1487 * processing other work items in a timely manner.
1488 *
1489 * @note Can be called by ISRs.
1490 *
1491 * @param work Address of work item.
1492 *
1493 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001494 */
1495static inline void k_work_submit(struct k_work *work)
1496{
1497 k_work_submit_to_queue(&k_sys_work_q, work);
1498}
1499
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501 * @brief Submit a delayed work item to the system workqueue.
1502 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001503 * This routine schedules work item @a work to be processed by the system
1504 * workqueue after a delay of @a delay milliseconds. The routine initiates
1505 * an asychronous countdown for the work item and then returns to the caller.
1506 * Only when the countdown completes is the work item actually submitted to
1507 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001508 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001509 * Submitting a previously submitted delayed work item that is still
1510 * counting down cancels the existing submission and restarts the countdown
1511 * using the new delay. If the work item is currently pending on the
1512 * workqueue's queue because the countdown has completed it is too late to
1513 * resubmit the item, and resubmission fails without impacting the work item.
1514 * If the work item has already been processed, or is currently being processed,
1515 * its work is considered complete and the work item can be resubmitted.
1516 *
1517 * @warning
1518 * Work items submitted to the system workqueue should avoid using handlers
1519 * that block or yield since this may prevent the system workqueue from
1520 * processing other work items in a timely manner.
1521 *
1522 * @note Can be called by ISRs.
1523 *
1524 * @param work Address of delayed work item.
1525 * @param delay Delay before submitting the work item (in milliseconds).
1526 *
1527 * @retval 0 Work item countdown started.
1528 * @retval -EINPROGRESS Work item is already pending.
1529 * @retval -EINVAL Work item is being processed or has completed its work.
1530 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001531 */
1532static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001533 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001534{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001535 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536}
1537
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001539 * @brief Get time remaining before a delayed work gets scheduled.
1540 *
1541 * This routine computes the (approximate) time remaining before a
1542 * delayed work gets executed. If the delayed work is not waiting to be
1543 * schedules, it returns zero.
1544 *
1545 * @param work Delayed work item.
1546 *
1547 * @return Remaining time (in milliseconds).
1548 */
1549static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1550{
1551 return _timeout_remaining_get(&work->timeout);
1552}
1553
1554/**
Allan Stephensc98da842016-11-11 15:45:03 -05001555 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001556 */
1557
Allan Stephensc98da842016-11-11 15:45:03 -05001558/**
1559 * @cond INTERNAL_HIDDEN
1560 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561
1562struct k_mutex {
1563 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001564 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001565 uint32_t lock_count;
1566 int owner_orig_prio;
1567#ifdef CONFIG_OBJECT_MONITOR
1568 int num_lock_state_changes;
1569 int num_conflicts;
1570#endif
1571
Anas Nashif2f203c22016-12-18 06:57:45 -05001572 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573};
1574
1575#ifdef CONFIG_OBJECT_MONITOR
1576#define _MUTEX_INIT_OBJECT_MONITOR \
1577 .num_lock_state_changes = 0, .num_conflicts = 0,
1578#else
1579#define _MUTEX_INIT_OBJECT_MONITOR
1580#endif
1581
1582#define K_MUTEX_INITIALIZER(obj) \
1583 { \
1584 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1585 .owner = NULL, \
1586 .lock_count = 0, \
1587 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1588 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001589 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001590 }
1591
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001592/**
Allan Stephensc98da842016-11-11 15:45:03 -05001593 * INTERNAL_HIDDEN @endcond
1594 */
1595
1596/**
1597 * @defgroup mutex_apis Mutex APIs
1598 * @ingroup kernel_apis
1599 * @{
1600 */
1601
1602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001606 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001607 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001611#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001612 struct k_mutex name \
1613 __in_section(_k_mutex, static, name) = \
1614 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001617 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001621 * Upon completion, the mutex is available and does not have an owner.
1622 *
1623 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001624 *
1625 * @return N/A
1626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001627extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001628
1629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001630 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * This routine locks @a mutex. If the mutex is locked by another thread,
1633 * the calling thread waits until the mutex becomes available or until
1634 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001636 * A thread is permitted to lock a mutex it has already locked. The operation
1637 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @param mutex Address of the mutex.
1640 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001641 * or one of the special values K_NO_WAIT and K_FOREVER.
1642 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001643 * @retval 0 Mutex locked.
1644 * @retval -EBUSY Returned without waiting.
1645 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648
1649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * This routine unlocks @a mutex. The mutex must already be locked by the
1653 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 *
1655 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001657 * thread.
1658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660 *
1661 * @return N/A
1662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663extern void k_mutex_unlock(struct k_mutex *mutex);
1664
Allan Stephensc98da842016-11-11 15:45:03 -05001665/**
1666 * @} end defgroup mutex_apis
1667 */
1668
1669/**
1670 * @cond INTERNAL_HIDDEN
1671 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001672
1673struct k_sem {
1674 _wait_q_t wait_q;
1675 unsigned int count;
1676 unsigned int limit;
1677
Anas Nashif2f203c22016-12-18 06:57:45 -05001678 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001679};
1680
Allan Stephensc98da842016-11-11 15:45:03 -05001681#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1682 { \
1683 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1684 .count = initial_count, \
1685 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001686 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001687 }
1688
1689/**
1690 * INTERNAL_HIDDEN @endcond
1691 */
1692
1693/**
1694 * @defgroup semaphore_apis Semaphore APIs
1695 * @ingroup kernel_apis
1696 * @{
1697 */
1698
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * @param sem Address of the semaphore.
1705 * @param initial_count Initial semaphore count.
1706 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001707 *
1708 * @return N/A
1709 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001710extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1711 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001712
1713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1719 *
1720 * @param sem Address of the semaphore.
1721 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001722 * or one of the special values K_NO_WAIT and K_FOREVER.
1723 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001724 * @note When porting code from the nanokernel legacy API to the new API, be
1725 * careful with the return value of this function. The return value is the
1726 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1727 * non-zero means failure, while the nano_sem_take family returns 1 for success
1728 * and 0 for failure.
1729 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001730 * @retval 0 Semaphore taken.
1731 * @retval -EBUSY Returned without waiting.
1732 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001735
1736/**
1737 * @brief Give a semaphore.
1738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * This routine gives @a sem, unless the semaphore is already at its maximum
1740 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001745 *
1746 * @return N/A
1747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748extern void k_sem_give(struct k_sem *sem);
1749
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001750/**
1751 * @brief Reset a semaphore's count to zero.
1752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001756 *
1757 * @return N/A
1758 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001759static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760{
1761 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001762}
1763
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001764/**
1765 * @brief Get a semaphore's count.
1766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001772 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001773static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774{
1775 return sem->count;
1776}
1777
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001782 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001783 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * @param name Name of the semaphore.
1786 * @param initial_count Initial semaphore count.
1787 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001788 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001789#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001790 struct k_sem name \
1791 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001792 K_SEM_INITIALIZER(name, initial_count, count_limit)
1793
Allan Stephensc98da842016-11-11 15:45:03 -05001794/**
1795 * @} end defgroup semaphore_apis
1796 */
1797
1798/**
1799 * @defgroup alert_apis Alert APIs
1800 * @ingroup kernel_apis
1801 * @{
1802 */
1803
Allan Stephens5eceb852016-11-16 10:16:30 -05001804/**
1805 * @typedef k_alert_handler_t
1806 * @brief Alert handler function type.
1807 *
1808 * An alert's alert handler function is invoked by the system workqueue
1809 * when the alert is signalled. The alert handler function is optional,
1810 * and is only invoked if the alert has been initialized with one.
1811 *
1812 * @param alert Address of the alert.
1813 *
1814 * @return 0 if alert has been consumed; non-zero if alert should pend.
1815 */
1816typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001817
1818/**
1819 * @} end defgroup alert_apis
1820 */
1821
1822/**
1823 * @cond INTERNAL_HIDDEN
1824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001826#define K_ALERT_DEFAULT NULL
1827#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001828
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001829struct k_alert {
1830 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831 atomic_t send_count;
1832 struct k_work work_item;
1833 struct k_sem sem;
1834
Anas Nashif2f203c22016-12-18 06:57:45 -05001835 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001836};
1837
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001838extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001839
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001840#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001841 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001842 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001843 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001844 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001845 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001846 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001847 }
1848
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849/**
Allan Stephensc98da842016-11-11 15:45:03 -05001850 * INTERNAL_HIDDEN @endcond
1851 */
1852
1853/**
1854 * @addtogroup alert_apis
1855 * @{
1856 */
1857
1858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001859 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001861 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001863 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @param name Name of the alert.
1866 * @param alert_handler Action to take when alert is sent. Specify either
1867 * the address of a function to be invoked by the system workqueue
1868 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1869 * K_ALERT_DEFAULT (which causes the alert to pend).
1870 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001872#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001873 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001874 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001875 K_ALERT_INITIALIZER(name, alert_handler, \
1876 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001883 * @param alert Address of the alert.
1884 * @param handler Action to take when alert is sent. Specify either the address
1885 * of a function to be invoked by the system workqueue thread,
1886 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1887 * K_ALERT_DEFAULT (which causes the alert to pend).
1888 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 *
1890 * @return N/A
1891 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001892extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1893 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001894
1895/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001898 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1901 *
1902 * @param alert Address of the alert.
1903 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 * or one of the special values K_NO_WAIT and K_FOREVER.
1905 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001906 * @retval 0 Alert received.
1907 * @retval -EBUSY Returned without waiting.
1908 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001909 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001910extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911
1912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * This routine signals @a alert. The action specified for @a alert will
1916 * be taken, which may trigger the execution of an alert handler function
1917 * and/or cause the alert to pend (assuming the alert has not reached its
1918 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * @note Can be called by ISRs.
1921 *
1922 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
1924 * @return N/A
1925 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001926extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927
1928/**
Allan Stephensc98da842016-11-11 15:45:03 -05001929 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930 */
1931
Allan Stephensc98da842016-11-11 15:45:03 -05001932/**
1933 * @cond INTERNAL_HIDDEN
1934 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935
1936struct k_msgq {
1937 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001938 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 uint32_t max_msgs;
1940 char *buffer_start;
1941 char *buffer_end;
1942 char *read_ptr;
1943 char *write_ptr;
1944 uint32_t used_msgs;
1945
Anas Nashif2f203c22016-12-18 06:57:45 -05001946 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001947};
1948
Peter Mitsis1da807e2016-10-06 11:36:59 -04001949#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001950 { \
1951 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001952 .max_msgs = q_max_msgs, \
1953 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001955 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956 .read_ptr = q_buffer, \
1957 .write_ptr = q_buffer, \
1958 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001959 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960 }
1961
Peter Mitsis1da807e2016-10-06 11:36:59 -04001962/**
Allan Stephensc98da842016-11-11 15:45:03 -05001963 * INTERNAL_HIDDEN @endcond
1964 */
1965
1966/**
1967 * @defgroup msgq_apis Message Queue APIs
1968 * @ingroup kernel_apis
1969 * @{
1970 */
1971
1972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * The message queue's ring buffer contains space for @a q_max_msgs messages,
1976 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06001977 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
1978 * message is similarly aligned to this boundary, @a q_msg_size must also be
1979 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001981 * The message queue can be accessed outside the module where it is defined
1982 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001984 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * @param q_name Name of the message queue.
1987 * @param q_msg_size Message size (in bytes).
1988 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06001989 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04001990 */
1991#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1992 static char __noinit __aligned(q_align) \
1993 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001994 struct k_msgq q_name \
1995 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001996 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1997 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998
Peter Mitsisd7a37502016-10-13 11:37:40 -04001999/**
2000 * @brief Initialize a message queue.
2001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * This routine initializes a message queue object, prior to its first use.
2003 *
Allan Stephensda827222016-11-09 14:23:58 -06002004 * The message queue's ring buffer must contain space for @a max_msgs messages,
2005 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2006 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2007 * that each message is similarly aligned to this boundary, @a q_msg_size
2008 * must also be a multiple of N.
2009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010 * @param q Address of the message queue.
2011 * @param buffer Pointer to ring buffer that holds queued messages.
2012 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002013 * @param max_msgs Maximum number of messages that can be queued.
2014 *
2015 * @return N/A
2016 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002017extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002018 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002019
2020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002024 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002025 * @note Can be called by ISRs.
2026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002027 * @param q Address of the message queue.
2028 * @param data Pointer to the message.
2029 * @param timeout Waiting period to add the message (in milliseconds),
2030 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002031 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002032 * @retval 0 Message sent.
2033 * @retval -ENOMSG Returned without waiting or queue purged.
2034 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002035 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002037
2038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * This routine receives a message from message queue @a q in a "first in,
2042 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002043 *
Allan Stephensc98da842016-11-11 15:45:03 -05002044 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002046 * @param q Address of the message queue.
2047 * @param data Address of area to hold the received message.
2048 * @param timeout Waiting period to receive the message (in milliseconds),
2049 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002050 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002051 * @retval 0 Message received.
2052 * @retval -ENOMSG Returned without waiting.
2053 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002056
2057/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002060 * This routine discards all unreceived messages in a message queue's ring
2061 * buffer. Any threads that are blocked waiting to send a message to the
2062 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002064 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002065 *
2066 * @return N/A
2067 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068extern void k_msgq_purge(struct k_msgq *q);
2069
Peter Mitsis67be2492016-10-07 11:44:34 -04002070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * This routine returns the number of unused entries in a message queue's
2074 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @param q Address of the message queue.
2077 *
2078 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002079 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002080static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002081{
2082 return q->max_msgs - q->used_msgs;
2083}
2084
Peter Mitsisd7a37502016-10-13 11:37:40 -04002085/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002086 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @param q Address of the message queue.
2091 *
2092 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002093 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002094static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095{
2096 return q->used_msgs;
2097}
2098
Allan Stephensc98da842016-11-11 15:45:03 -05002099/**
2100 * @} end defgroup msgq_apis
2101 */
2102
2103/**
2104 * @defgroup mem_pool_apis Memory Pool APIs
2105 * @ingroup kernel_apis
2106 * @{
2107 */
2108
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002110 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002111 void *addr_in_pool;
2112 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002113 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114};
2115
Allan Stephensc98da842016-11-11 15:45:03 -05002116/**
2117 * @} end defgroup mem_pool_apis
2118 */
2119
2120/**
2121 * @defgroup mailbox_apis Mailbox APIs
2122 * @ingroup kernel_apis
2123 * @{
2124 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125
2126struct k_mbox_msg {
2127 /** internal use only - needed for legacy API support */
2128 uint32_t _mailbox;
2129 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002130 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131 /** application-defined information value */
2132 uint32_t info;
2133 /** sender's message data buffer */
2134 void *tx_data;
2135 /** internal use only - needed for legacy API support */
2136 void *_rx_data;
2137 /** message data block descriptor */
2138 struct k_mem_block tx_block;
2139 /** source thread id */
2140 k_tid_t rx_source_thread;
2141 /** target thread id */
2142 k_tid_t tx_target_thread;
2143 /** internal use only - thread waiting on send (may be a dummy) */
2144 k_tid_t _syncing_thread;
2145#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2146 /** internal use only - semaphore used during asynchronous send */
2147 struct k_sem *_async_sem;
2148#endif
2149};
2150
Allan Stephensc98da842016-11-11 15:45:03 -05002151/**
2152 * @cond INTERNAL_HIDDEN
2153 */
2154
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155struct k_mbox {
2156 _wait_q_t tx_msg_queue;
2157 _wait_q_t rx_msg_queue;
2158
Anas Nashif2f203c22016-12-18 06:57:45 -05002159 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160};
2161
2162#define K_MBOX_INITIALIZER(obj) \
2163 { \
2164 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2165 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002166 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167 }
2168
Peter Mitsis12092702016-10-14 12:57:23 -04002169/**
Allan Stephensc98da842016-11-11 15:45:03 -05002170 * INTERNAL_HIDDEN @endcond
2171 */
2172
2173/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002176 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002178 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002183 struct k_mbox name \
2184 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185 K_MBOX_INITIALIZER(name) \
2186
Peter Mitsis12092702016-10-14 12:57:23 -04002187/**
2188 * @brief Initialize a mailbox.
2189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002190 * This routine initializes a mailbox object, prior to its first use.
2191 *
2192 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002193 *
2194 * @return N/A
2195 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196extern void k_mbox_init(struct k_mbox *mbox);
2197
Peter Mitsis12092702016-10-14 12:57:23 -04002198/**
2199 * @brief Send a mailbox message in a synchronous manner.
2200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002201 * This routine sends a message to @a mbox and waits for a receiver to both
2202 * receive and process it. The message data may be in a buffer, in a memory
2203 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002205 * @param mbox Address of the mailbox.
2206 * @param tx_msg Address of the transmit message descriptor.
2207 * @param timeout Waiting period for the message to be received (in
2208 * milliseconds), or one of the special values K_NO_WAIT
2209 * and K_FOREVER. Once the message has been received,
2210 * this routine waits as long as necessary for the message
2211 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002212 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002213 * @retval 0 Message sent.
2214 * @retval -ENOMSG Returned without waiting.
2215 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002216 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002217extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002219
Peter Mitsis12092702016-10-14 12:57:23 -04002220/**
2221 * @brief Send a mailbox message in an asynchronous manner.
2222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002223 * This routine sends a message to @a mbox without waiting for a receiver
2224 * to process it. The message data may be in a buffer, in a memory pool block,
2225 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2226 * will be given when the message has been both received and completely
2227 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002229 * @param mbox Address of the mailbox.
2230 * @param tx_msg Address of the transmit message descriptor.
2231 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002232 *
2233 * @return N/A
2234 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002235extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236 struct k_sem *sem);
2237
Peter Mitsis12092702016-10-14 12:57:23 -04002238/**
2239 * @brief Receive a mailbox message.
2240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * This routine receives a message from @a mbox, then optionally retrieves
2242 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002244 * @param mbox Address of the mailbox.
2245 * @param rx_msg Address of the receive message descriptor.
2246 * @param buffer Address of the buffer to receive data, or NULL to defer data
2247 * retrieval and message disposal until later.
2248 * @param timeout Waiting period for a message to be received (in
2249 * milliseconds), or one of the special values K_NO_WAIT
2250 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002251 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002252 * @retval 0 Message received.
2253 * @retval -ENOMSG Returned without waiting.
2254 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002255 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002256extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002258
2259/**
2260 * @brief Retrieve mailbox message data into a buffer.
2261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * This routine completes the processing of a received message by retrieving
2263 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002264 *
2265 * Alternatively, this routine can be used to dispose of a received message
2266 * without retrieving its data.
2267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * @param rx_msg Address of the receive message descriptor.
2269 * @param buffer Address of the buffer to receive data, or NULL to discard
2270 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002271 *
2272 * @return N/A
2273 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002274extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002275
2276/**
2277 * @brief Retrieve mailbox message data into a memory pool block.
2278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002279 * This routine completes the processing of a received message by retrieving
2280 * its data into a memory pool block, then disposing of the message.
2281 * The memory pool block that results from successful retrieval must be
2282 * returned to the pool once the data has been processed, even in cases
2283 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002284 *
2285 * Alternatively, this routine can be used to dispose of a received message
2286 * without retrieving its data. In this case there is no need to return a
2287 * memory pool block to the pool.
2288 *
2289 * This routine allocates a new memory pool block for the data only if the
2290 * data is not already in one. If a new block cannot be allocated, the routine
2291 * returns a failure code and the received message is left unchanged. This
2292 * permits the caller to reattempt data retrieval at a later time or to dispose
2293 * of the received message without retrieving its data.
2294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002295 * @param rx_msg Address of a receive message descriptor.
2296 * @param pool Address of memory pool, or NULL to discard data.
2297 * @param block Address of the area to hold memory pool block info.
2298 * @param timeout Waiting period to wait for a memory pool block (in
2299 * milliseconds), or one of the special values K_NO_WAIT
2300 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002301 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002302 * @retval 0 Data retrieved.
2303 * @retval -ENOMEM Returned without waiting.
2304 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002305 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002306extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002307 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308 struct k_mem_block *block, int32_t timeout);
2309
Allan Stephensc98da842016-11-11 15:45:03 -05002310/**
2311 * @} end defgroup mailbox_apis
2312 */
2313
2314/**
2315 * @cond INTERNAL_HIDDEN
2316 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317
2318struct k_pipe {
2319 unsigned char *buffer; /* Pipe buffer: may be NULL */
2320 size_t size; /* Buffer size */
2321 size_t bytes_used; /* # bytes used in buffer */
2322 size_t read_index; /* Where in buffer to read from */
2323 size_t write_index; /* Where in buffer to write */
2324
2325 struct {
2326 _wait_q_t readers; /* Reader wait queue */
2327 _wait_q_t writers; /* Writer wait queue */
2328 } wait_q;
2329
Anas Nashif2f203c22016-12-18 06:57:45 -05002330 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331};
2332
Peter Mitsise5d9c582016-10-14 14:44:57 -04002333#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334 { \
2335 .buffer = pipe_buffer, \
2336 .size = pipe_buffer_size, \
2337 .bytes_used = 0, \
2338 .read_index = 0, \
2339 .write_index = 0, \
2340 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2341 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002342 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 }
2344
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345/**
Allan Stephensc98da842016-11-11 15:45:03 -05002346 * INTERNAL_HIDDEN @endcond
2347 */
2348
2349/**
2350 * @defgroup pipe_apis Pipe APIs
2351 * @ingroup kernel_apis
2352 * @{
2353 */
2354
2355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002360 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param name Name of the pipe.
2363 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2364 * or zero if no ring buffer is used.
2365 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002367#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2368 static unsigned char __noinit __aligned(pipe_align) \
2369 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002370 struct k_pipe name \
2371 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002372 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * This routine initializes a pipe object, prior to its first use.
2378 *
2379 * @param pipe Address of the pipe.
2380 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2381 * is used.
2382 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2383 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 *
2385 * @return N/A
2386 */
2387extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2388 size_t size);
2389
2390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 * @param pipe Address of the pipe.
2396 * @param data Address of data to write.
2397 * @param bytes_to_write Size of data (in bytes).
2398 * @param bytes_written Address of area to hold the number of bytes written.
2399 * @param min_xfer Minimum number of bytes to write.
2400 * @param timeout Waiting period to wait for the data to be written (in
2401 * milliseconds), or one of the special values K_NO_WAIT
2402 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002404 * @retval 0 At least @a min_xfer bytes of data were written.
2405 * @retval -EIO Returned without waiting; zero data bytes were written.
2406 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002409extern int k_pipe_put(struct k_pipe *pipe, void *data,
2410 size_t bytes_to_write, size_t *bytes_written,
2411 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412
2413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * @param pipe Address of the pipe.
2419 * @param data Address to place the data read from pipe.
2420 * @param bytes_to_read Maximum number of data bytes to read.
2421 * @param bytes_read Address of area to hold the number of bytes read.
2422 * @param min_xfer Minimum number of data bytes to read.
2423 * @param timeout Waiting period to wait for the data to be read (in
2424 * milliseconds), or one of the special values K_NO_WAIT
2425 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002427 * @retval 0 At least @a min_xfer bytes of data were read.
2428 * @retval -EIO Returned without waiting; zero data bytes were read.
2429 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002432extern int k_pipe_get(struct k_pipe *pipe, void *data,
2433 size_t bytes_to_read, size_t *bytes_read,
2434 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435
2436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * This routine writes the data contained in a memory block to @a pipe.
2440 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 * @param block Memory block containing data to send
2445 * @param size Number of data bytes in memory block to send
2446 * @param sem Semaphore to signal upon completion (else NULL)
2447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 */
2450extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2451 size_t size, struct k_sem *sem);
2452
2453/**
Allan Stephensc98da842016-11-11 15:45:03 -05002454 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455 */
2456
Allan Stephensc98da842016-11-11 15:45:03 -05002457/**
2458 * @cond INTERNAL_HIDDEN
2459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002461struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002463 uint32_t num_blocks;
2464 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465 char *buffer;
2466 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002467 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468
Anas Nashif2f203c22016-12-18 06:57:45 -05002469 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470};
2471
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002472#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2473 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474 { \
2475 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002476 .num_blocks = slab_num_blocks, \
2477 .block_size = slab_block_size, \
2478 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479 .free_list = NULL, \
2480 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002481 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002482 }
2483
Peter Mitsis578f9112016-10-07 13:50:31 -04002484/**
Allan Stephensc98da842016-11-11 15:45:03 -05002485 * INTERNAL_HIDDEN @endcond
2486 */
2487
2488/**
2489 * @defgroup mem_slab_apis Memory Slab APIs
2490 * @ingroup kernel_apis
2491 * @{
2492 */
2493
2494/**
Allan Stephensda827222016-11-09 14:23:58 -06002495 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002496 *
Allan Stephensda827222016-11-09 14:23:58 -06002497 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002499 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2500 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002501 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002502 *
Allan Stephensda827222016-11-09 14:23:58 -06002503 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002506 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * @param name Name of the memory slab.
2509 * @param slab_block_size Size of each memory block (in bytes).
2510 * @param slab_num_blocks Number memory blocks.
2511 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002512 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002513#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2514 char __noinit __aligned(slab_align) \
2515 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2516 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002517 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002518 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2519 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002521/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002522 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002525 *
Allan Stephensda827222016-11-09 14:23:58 -06002526 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2527 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2528 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2529 * To ensure that each memory block is similarly aligned to this boundary,
2530 * @a slab_block_size must also be a multiple of N.
2531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param slab Address of the memory slab.
2533 * @param buffer Pointer to buffer used for the memory blocks.
2534 * @param block_size Size of each memory block (in bytes).
2535 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002536 *
2537 * @return N/A
2538 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002539extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002540 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002541
2542/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002545 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * @param slab Address of the memory slab.
2548 * @param mem Pointer to block address area.
2549 * @param timeout Maximum time to wait for operation to complete
2550 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2551 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002552 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002553 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002555 * @retval -ENOMEM Returned without waiting.
2556 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002557 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002558extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2559 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002560
2561/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 * This routine releases a previously allocated memory block back to its
2565 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002567 * @param slab Address of the memory slab.
2568 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002569 *
2570 * @return N/A
2571 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002572extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * This routine gets the number of memory blocks that are currently
2578 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002583 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002584static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002586 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587}
2588
Peter Mitsisc001aa82016-10-13 13:53:37 -04002589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * This routine gets the number of memory blocks that are currently
2593 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002598 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002599static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002600{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002601 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002602}
2603
Allan Stephensc98da842016-11-11 15:45:03 -05002604/**
2605 * @} end defgroup mem_slab_apis
2606 */
2607
2608/**
2609 * @cond INTERNAL_HIDDEN
2610 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002611
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002612/*
2613 * Memory pool requires a buffer and two arrays of structures for the
2614 * memory block accounting:
2615 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2616 * status of four blocks of memory.
2617 */
2618struct k_mem_pool_quad_block {
2619 char *mem_blocks; /* pointer to the first of four memory blocks */
2620 uint32_t mem_status; /* four bits. If bit is set, memory block is
2621 allocated */
2622};
2623/*
2624 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2625 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2626 * block size is 4 times less than the previous one and thus requires 4 times
2627 * bigger array of k_mem_pool_quad_block structures to keep track of the
2628 * memory blocks.
2629 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002631/*
2632 * The array of k_mem_pool_block_set keeps the information of each array of
2633 * k_mem_pool_quad_block structures
2634 */
2635struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002636 size_t block_size; /* memory block size */
2637 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002638 struct k_mem_pool_quad_block *quad_block;
2639 int count;
2640};
2641
2642/* Memory pool descriptor */
2643struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002644 size_t max_block_size;
2645 size_t min_block_size;
2646 uint32_t nr_of_maxblocks;
2647 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002648 struct k_mem_pool_block_set *block_set;
2649 char *bufblock;
2650 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002651 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652};
2653
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002654#ifdef CONFIG_ARM
2655#define _SECTION_TYPE_SIGN "%"
2656#else
2657#define _SECTION_TYPE_SIGN "@"
2658#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002660/*
2661 * Static memory pool initialization
2662 */
Allan Stephensc98da842016-11-11 15:45:03 -05002663
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002664/*
2665 * Use .altmacro to be able to recalculate values and pass them as string
2666 * arguments when calling assembler macros resursively
2667 */
2668__asm__(".altmacro\n\t");
2669
2670/*
2671 * Recursively calls a macro
2672 * The followig global symbols need to be initialized:
2673 * __memory_pool_max_block_size - maximal size of the memory block
2674 * __memory_pool_min_block_size - minimal size of the memory block
2675 * Notes:
2676 * Global symbols are used due the fact that assembler macro allows only
2677 * one argument be passed with the % conversion
2678 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2679 * is used instead of / 4.
2680 * n_max argument needs to go first in the invoked macro, as some
2681 * assemblers concatenate \name and %(\n_max * 4) arguments
2682 * if \name goes first
2683 */
2684__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2685 ".ifge __memory_pool_max_block_size >> 2 -"
2686 " __memory_pool_min_block_size\n\t\t"
2687 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2688 "\\macro_name %(\\n_max * 4) \\name\n\t"
2689 ".endif\n\t"
2690 ".endm\n");
2691
2692/*
2693 * Build quad blocks
2694 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2695 * structures and recursively calls itself for the next array, 4 times
2696 * larger.
2697 * The followig global symbols need to be initialized:
2698 * __memory_pool_max_block_size - maximal size of the memory block
2699 * __memory_pool_min_block_size - minimal size of the memory block
2700 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2701 */
2702__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002703 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002704 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2705 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2706 ".if \\n_max % 4\n\t\t"
2707 ".skip __memory_pool_quad_block_size\n\t"
2708 ".endif\n\t"
2709 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2710 ".endm\n");
2711
2712/*
2713 * Build block sets and initialize them
2714 * Macro initializes the k_mem_pool_block_set structure and
2715 * recursively calls itself for the next one.
2716 * The followig global symbols need to be initialized:
2717 * __memory_pool_max_block_size - maximal size of the memory block
2718 * __memory_pool_min_block_size - minimal size of the memory block
2719 * __memory_pool_block_set_count, the number of the elements in the
2720 * block set array must be set to 0. Macro calculates it's real
2721 * value.
2722 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2723 * structures, _build_quad_blocks must be called prior it.
2724 */
2725__asm__(".macro _build_block_set n_max, name\n\t"
2726 ".int __memory_pool_max_block_size\n\t" /* block_size */
2727 ".if \\n_max % 4\n\t\t"
2728 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2729 ".else\n\t\t"
2730 ".int \\n_max >> 2\n\t"
2731 ".endif\n\t"
2732 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2733 ".int 0\n\t" /* count */
2734 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2735 "__do_recurse _build_block_set \\name \\n_max\n\t"
2736 ".endm\n");
2737
2738/*
2739 * Build a memory pool structure and initialize it
2740 * Macro uses __memory_pool_block_set_count global symbol,
2741 * block set addresses and buffer address, it may be called only after
2742 * _build_block_set
2743 */
2744__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002745 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002746 _SECTION_TYPE_SIGN "progbits\n\t"
2747 ".globl \\name\n\t"
2748 "\\name:\n\t"
2749 ".int \\max_size\n\t" /* max_block_size */
2750 ".int \\min_size\n\t" /* min_block_size */
2751 ".int \\n_max\n\t" /* nr_of_maxblocks */
2752 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2753 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2754 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2755 ".int 0\n\t" /* wait_q->head */
2756 ".int 0\n\t" /* wait_q->next */
2757 ".popsection\n\t"
2758 ".endm\n");
2759
2760#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2761 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2762 _SECTION_TYPE_SIGN "progbits\n\t"); \
2763 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2764 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2765 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2766 STRINGIFY(name) "\n\t"); \
2767 __asm__(".popsection\n\t")
2768
2769#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2770 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2771 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2772 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2773 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002774 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002775 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2776 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2777 STRINGIFY(name) "\n\t"); \
2778 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2779 __asm__(".int __memory_pool_block_set_count\n\t"); \
2780 __asm__(".popsection\n\t"); \
2781 extern uint32_t _mem_pool_block_set_count_##name; \
2782 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2783
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002784#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2785 char __noinit __aligned(align) \
2786 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002787
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002788/*
2789 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2790 * to __memory_pool_quad_block_size absolute symbol.
2791 * This function does not get called, but compiler calculates the value and
2792 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2793 */
2794static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2795{
2796 __asm__(".globl __memory_pool_quad_block_size\n\t"
2797#ifdef CONFIG_NIOS2
2798 "__memory_pool_quad_block_size = %0\n\t"
2799#else
2800 "__memory_pool_quad_block_size = %c0\n\t"
2801#endif
2802 :
2803 : "n"(sizeof(struct k_mem_pool_quad_block)));
2804}
2805
2806/**
Allan Stephensc98da842016-11-11 15:45:03 -05002807 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002808 */
2809
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002810/**
Allan Stephensc98da842016-11-11 15:45:03 -05002811 * @addtogroup mem_pool_apis
2812 * @{
2813 */
2814
2815/**
2816 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2819 * long. The memory pool allows blocks to be repeatedly partitioned into
2820 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2821 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002822 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002824 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002825 * If the pool is to be accessed outside the module where it is defined, it
2826 * can be declared via
2827 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002828 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * @param name Name of the memory pool.
2831 * @param min_size Size of the smallest blocks in the pool (in bytes).
2832 * @param max_size Size of the largest blocks in the pool (in bytes).
2833 * @param n_max Number of maximum sized blocks in the pool.
2834 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002835 */
2836#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002837 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2838 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002839 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002840 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2841 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2842 extern struct k_mem_pool name
2843
Peter Mitsis937042c2016-10-13 13:18:26 -04002844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * @param pool Address of the memory pool.
2850 * @param block Pointer to block descriptor for the allocated memory.
2851 * @param size Amount of memory to allocate (in bytes).
2852 * @param timeout Maximum time to wait for operation to complete
2853 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2854 * or K_FOREVER to wait as long as necessary.
2855 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002856 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002858 * @retval -ENOMEM Returned without waiting.
2859 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002860 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002861extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002862 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002863
2864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine releases a previously allocated memory block back to its
2868 * memory pool.
2869 *
2870 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002871 *
2872 * @return N/A
2873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002875
2876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * This routine instructs a memory pool to concatenate unused memory blocks
2880 * into larger blocks wherever possible. Manually defragmenting the memory
2881 * pool may speed up future allocations of memory blocks by eliminating the
2882 * need for the memory pool to perform an automatic partial defragmentation.
2883 *
2884 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002885 *
2886 * @return N/A
2887 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002888extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002889
2890/**
Allan Stephensc98da842016-11-11 15:45:03 -05002891 * @} end addtogroup mem_pool_apis
2892 */
2893
2894/**
2895 * @defgroup heap_apis Heap Memory Pool APIs
2896 * @ingroup kernel_apis
2897 * @{
2898 */
2899
2900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002904 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002909 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002910extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002911
2912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002914 *
2915 * This routine provides traditional free() semantics. The memory being
2916 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002917 *
Anas Nashif345fdd52016-12-20 08:36:04 -05002918 * If @a ptr is NULL, no operation is performed.
2919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002921 *
2922 * @return N/A
2923 */
2924extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002925
Allan Stephensc98da842016-11-11 15:45:03 -05002926/**
2927 * @} end defgroup heap_apis
2928 */
2929
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05002930/**
2931 * @brief Make the CPU idle.
2932 *
2933 * This function makes the CPU idle until an event wakes it up.
2934 *
2935 * In a regular system, the idle thread should be the only thread responsible
2936 * for making the CPU idle and triggering any type of power management.
2937 * However, in some more constrained systems, such as a single-threaded system,
2938 * the only thread would be responsible for this if needed.
2939 *
2940 * @return N/A
2941 */
2942extern void k_cpu_idle(void);
2943
2944/**
2945 * @brief Make the CPU idle in an atomic fashion.
2946 *
2947 * Similar to k_cpu_idle(), but called with interrupts locked if operations
2948 * must be done atomically before making the CPU idle.
2949 *
2950 * @param key Interrupt locking key obtained from irq_lock().
2951 *
2952 * @return N/A
2953 */
2954extern void k_cpu_atomic_idle(unsigned int key);
2955
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956/*
2957 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2958 * hook into the device subsystem, which itself uses nanokernel semaphores,
2959 * and thus currently requires the definition of nano_sem.
2960 */
2961#include <legacy.h>
2962#include <arch/cpu.h>
2963
2964/*
2965 * private APIs that are utilized by one or more public APIs
2966 */
2967
Benjamin Walshb12a8e02016-12-14 15:24:12 -05002968#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05002970#else
2971#define _init_static_threads() do { } while ((0))
2972#endif
2973
2974extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002975extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976
2977#ifdef __cplusplus
2978}
2979#endif
2980
Andrew Boiee004dec2016-11-07 09:01:19 -08002981#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2982/*
2983 * Define new and delete operators.
2984 * At this moment, the operators do nothing since objects are supposed
2985 * to be statically allocated.
2986 */
2987inline void operator delete(void *ptr)
2988{
2989 (void)ptr;
2990}
2991
2992inline void operator delete[](void *ptr)
2993{
2994 (void)ptr;
2995}
2996
2997inline void *operator new(size_t size)
2998{
2999 (void)size;
3000 return NULL;
3001}
3002
3003inline void *operator new[](size_t size)
3004{
3005 (void)size;
3006 return NULL;
3007}
3008
3009/* Placement versions of operator new and delete */
3010inline void operator delete(void *ptr1, void *ptr2)
3011{
3012 (void)ptr1;
3013 (void)ptr2;
3014}
3015
3016inline void operator delete[](void *ptr1, void *ptr2)
3017{
3018 (void)ptr1;
3019 (void)ptr2;
3020}
3021
3022inline void *operator new(size_t size, void *ptr)
3023{
3024 (void)size;
3025 return ptr;
3026}
3027
3028inline void *operator new[](size_t size, void *ptr)
3029{
3030 (void)size;
3031 return ptr;
3032}
3033
3034#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3035
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036#endif /* _kernel__h_ */