blob: dd5fa1e4c896774d1160e052c621241586a9c5ed [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>
Benjamin Walsh62092182016-12-20 14:39:08 -050035#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050036#include <kernel_version.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Anas Nashif61f4b242016-11-18 10:53:59 -050042#ifdef CONFIG_KERNEL_DEBUG
43#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040044#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
45#else
46#define K_DEBUG(fmt, ...)
47#endif
48
49#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
50#define K_PRIO_PREEMPT(x) (x)
51
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
Benjamin Walshfab8d922016-11-08 15:36:36 -050067#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
Anas Nashif2f203c22016-12-18 06:57:45 -050074#ifdef CONFIG_OBJECT_TRACING
75#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
76#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#else
Anas Nashif2f203c22016-12-18 06:57:45 -050078#define _OBJECT_TRACING_INIT
79#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050082#define tcs k_thread
83struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400105/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100106 * @defgroup profiling_apis Profiling APIs
107 * @ingroup kernel_apis
108 * @{
109 */
110
111/**
112 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
113 *
114 * This routine calls @ref stack_analyze on the 4 call stacks declared and
115 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
116 *
117 * CONFIG_MAIN_STACK_SIZE
118 * CONFIG_IDLE_STACK_SIZE
119 * CONFIG_ISR_STACK_SIZE
120 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
121 *
122 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
123 * produce output.
124 *
125 * @return N/A
126 */
127extern void k_call_stacks_analyze(void);
128
129/**
130 * @} end defgroup profiling_apis
131 */
132
133/**
Allan Stephensc98da842016-11-11 15:45:03 -0500134 * @defgroup thread_apis Thread APIs
135 * @ingroup kernel_apis
136 * @{
137 */
138
139/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500140 * @typedef k_thread_entry_t
141 * @brief Thread entry point function type.
142 *
143 * A thread's entry point function is invoked when the thread starts executing.
144 * Up to 3 argument values can be passed to the function.
145 *
146 * The thread terminates execution permanently if the entry point function
147 * returns. The thread is responsible for releasing any shared resources
148 * it may own (such as mutexes and dynamically allocated memory), prior to
149 * returning.
150 *
151 * @param p1 First argument.
152 * @param p2 Second argument.
153 * @param p3 Third argument.
154 *
155 * @return N/A
156 */
157typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
158
159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500160 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500162 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500164 * The new thread may be scheduled for immediate execution or a delayed start.
165 * If the newly spawned thread does not have a delayed start the kernel
166 * scheduler may preempt the current thread to allow the new thread to
167 * execute.
168 *
169 * Thread options are architecture-specific, and can include K_ESSENTIAL,
170 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
171 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400172 *
173 * @param stack Pointer to the stack space.
174 * @param stack_size Stack size in bytes.
175 * @param entry Thread entry function.
176 * @param p1 1st entry point parameter.
177 * @param p2 2nd entry point parameter.
178 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500179 * @param prio Thread priority.
180 * @param options Thread options.
181 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500183 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400184 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500185extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500186 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400187 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500188 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400189
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500191 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400192 *
Allan Stephensc98da842016-11-11 15:45:03 -0500193 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500194 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
198 * @return N/A
199 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400200extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201
202/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500203 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400204 *
205 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500206 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400208 * @return N/A
209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400210extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211
212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500213 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500215 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400216 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500217 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
219 * @return N/A
220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400221extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400222
223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500224 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500226 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500228 * If @a thread is not currently sleeping, the routine has no effect.
229 *
230 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400231 *
232 * @return N/A
233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400234extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400235
236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500239 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400240 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400241extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242
243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500244 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500246 * This routine prevents @a thread from executing if it has not yet started
247 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @param thread ID of thread to cancel.
250 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500251 * @retval 0 Thread spawning cancelled.
252 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400254extern int k_thread_cancel(k_tid_t thread);
255
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400256/**
Allan Stephensc98da842016-11-11 15:45:03 -0500257 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500259 * This routine permanently stops execution of @a thread. The thread is taken
260 * off all kernel queues it is part of (i.e. the ready queue, the timeout
261 * queue, or a kernel object wait queue). However, any kernel resources the
262 * thread might currently own (such as mutexes or memory blocks) are not
263 * released. It is the responsibility of the caller of this routine to ensure
264 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500266 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267 *
268 * @return N/A
269 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400270extern void k_thread_abort(k_tid_t thread);
271
Allan Stephensc98da842016-11-11 15:45:03 -0500272/**
273 * @cond INTERNAL_HIDDEN
274 */
275
Benjamin Walshd211a522016-12-06 11:44:01 -0500276/* timeout has timed out and is not on _timeout_q anymore */
277#define _EXPIRED (-2)
278
279/* timeout is not in use */
280#define _INACTIVE (-1)
281
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400282#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400283#define _THREAD_TIMEOUT_INIT(obj) \
284 (obj).nano_timeout = { \
285 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400286 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500288 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400289 },
290#else
291#define _THREAD_TIMEOUT_INIT(obj)
292#endif
293
294#ifdef CONFIG_ERRNO
295#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
296#else
297#define _THREAD_ERRNO_INIT(obj)
298#endif
299
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400300struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400301 union {
302 char *init_stack;
303 struct k_thread *thread;
304 };
305 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500306 void (*init_entry)(void *, void *, void *);
307 void *init_p1;
308 void *init_p2;
309 void *init_p3;
310 int init_prio;
311 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400312 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500313 void (*init_abort)(void);
314 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400315};
316
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400317#define _THREAD_INITIALIZER(stack, stack_size, \
318 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500319 prio, options, delay, abort, groups) \
320 { \
321 .init_stack = (stack), \
322 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400323 .init_entry = (void (*)(void *, void *, void *))entry, \
324 .init_p1 = (void *)p1, \
325 .init_p2 = (void *)p2, \
326 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500327 .init_prio = (prio), \
328 .init_options = (options), \
329 .init_delay = (delay), \
330 .init_abort = (abort), \
331 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400332 }
333
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400334/**
Allan Stephensc98da842016-11-11 15:45:03 -0500335 * INTERNAL_HIDDEN @endcond
336 */
337
338/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500339 * @brief Statically define and initialize a thread.
340 *
341 * The thread may be scheduled for immediate execution or a delayed start.
342 *
343 * Thread options are architecture-specific, and can include K_ESSENTIAL,
344 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
345 * them using "|" (the logical OR operator).
346 *
347 * The ID of the thread can be accessed using:
348 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500349 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500350 *
351 * @param name Name of the thread.
352 * @param stack_size Stack size in bytes.
353 * @param entry Thread entry function.
354 * @param p1 1st entry point parameter.
355 * @param p2 2nd entry point parameter.
356 * @param p3 3rd entry point parameter.
357 * @param prio Thread priority.
358 * @param options Thread options.
359 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400360 *
361 * @internal It has been observed that the x86 compiler by default aligns
362 * these _static_thread_data structures to 32-byte boundaries, thereby
363 * wasting space. To work around this, force a 4-byte alignment.
364 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500365#define K_THREAD_DEFINE(name, stack_size, \
366 entry, p1, p2, p3, \
367 prio, options, delay) \
368 char __noinit __stack _k_thread_obj_##name[stack_size]; \
369 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500370 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500371 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
372 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500373 NULL, 0); \
374 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400375
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500377 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500379 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @param thread ID of thread whose priority is needed.
382 *
383 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500385extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386
387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500390 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400391 *
392 * Rescheduling can occur immediately depending on the priority @a thread is
393 * set to:
394 *
395 * - If its priority is raised above the priority of the caller of this
396 * function, and the caller is preemptible, @a thread will be scheduled in.
397 *
398 * - If the caller operates on itself, it lowers its priority below that of
399 * other threads in the system, and the caller is preemptible, the thread of
400 * highest priority will be scheduled in.
401 *
402 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
403 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
404 * highest priority.
405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 * @param prio New priority.
408 *
409 * @warning Changing the priority of a thread currently involved in mutex
410 * priority inheritance may result in undefined behavior.
411 *
412 * @return N/A
413 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400414extern void k_thread_priority_set(k_tid_t thread, int prio);
415
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500417 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500419 * This routine prevents the kernel scheduler from making @a thread the
420 * current thread. All other internal operations on @a thread are still
421 * performed; for example, any timeout it is waiting on keeps ticking,
422 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * If @a thread is already suspended, the routine has no effect.
425 *
426 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 *
428 * @return N/A
429 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400430extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431
432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500433 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * This routine allows the kernel scheduler to make @a thread the current
436 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500438 * If @a thread is not currently suspended, the routine has no effect.
439 *
440 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441 *
442 * @return N/A
443 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400444extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500447 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500449 * This routine specifies how the scheduler will perform time slicing of
450 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * To enable time slicing, @a slice must be non-zero. The scheduler
453 * ensures that no thread runs for more than the specified time limit
454 * before other threads of that priority are given a chance to execute.
455 * Any thread whose priority is higher than @a prio is exempted, and may
456 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 * execute. Once the scheduler selects a thread for execution, there is no
460 * minimum guaranteed time the thread will execute before threads of greater or
461 * equal priority are scheduled.
462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 * for execution, this routine has no effect; the thread is immediately
465 * rescheduled after the slice period expires.
466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500467 * To disable timeslicing, set both @a slice and @a prio to zero.
468 *
469 * @param slice Maximum time slice length (in milliseconds).
470 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
472 * @return N/A
473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400475
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400476/**
Allan Stephensc98da842016-11-11 15:45:03 -0500477 * @} end defgroup thread_apis
478 */
479
480/**
481 * @addtogroup isr_apis
482 * @{
483 */
484
485/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500486 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487 *
Allan Stephensc98da842016-11-11 15:45:03 -0500488 * This routine allows the caller to customize its actions, depending on
489 * whether it is a thread or an ISR.
490 *
491 * @note Can be called by ISRs.
492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * @return 0 if invoked by a thread.
494 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400495 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500496extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400497
Benjamin Walsh445830d2016-11-10 15:54:27 -0500498/**
499 * @brief Determine if code is running in a preemptible thread.
500 *
Allan Stephensc98da842016-11-11 15:45:03 -0500501 * This routine allows the caller to customize its actions, depending on
502 * whether it can be preempted by another thread. The routine returns a 'true'
503 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500504 *
Allan Stephensc98da842016-11-11 15:45:03 -0500505 * - The code is running in a thread, not at ISR.
506 * - The thread's priority is in the preemptible range.
507 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500508 *
Allan Stephensc98da842016-11-11 15:45:03 -0500509 * @note Can be called by ISRs.
510 *
511 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500512 * @return Non-zero if invoked by a preemptible thread.
513 */
514extern int k_is_preempt_thread(void);
515
Allan Stephensc98da842016-11-11 15:45:03 -0500516/**
517 * @} end addtogroup isr_apis
518 */
519
520/**
521 * @addtogroup thread_apis
522 * @{
523 */
524
525/**
526 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500527 *
Allan Stephensc98da842016-11-11 15:45:03 -0500528 * This routine prevents the current thread from being preempted by another
529 * thread by instructing the scheduler to treat it as a cooperative thread.
530 * If the thread subsequently performs an operation that makes it unready,
531 * it will be context switched out in the normal manner. When the thread
532 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500533 *
Allan Stephensc98da842016-11-11 15:45:03 -0500534 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500535 *
Allan Stephensc98da842016-11-11 15:45:03 -0500536 * @note k_sched_lock() and k_sched_unlock() should normally be used
537 * when the operation being performed can be safely interrupted by ISRs.
538 * However, if the amount of processing involved is very small, better
539 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500540 *
541 * @return N/A
542 */
543extern void k_sched_lock(void);
544
Allan Stephensc98da842016-11-11 15:45:03 -0500545/**
546 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500547 *
Allan Stephensc98da842016-11-11 15:45:03 -0500548 * This routine reverses the effect of a previous call to k_sched_lock().
549 * A thread must call the routine once for each time it called k_sched_lock()
550 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500551 *
552 * @return N/A
553 */
554extern void k_sched_unlock(void);
555
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500557 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500559 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * Custom data is not used by the kernel itself, and is freely available
562 * for a thread to use as it sees fit. It can be used as a framework
563 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500565 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400566 *
567 * @return N/A
568 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400569extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400570
571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500574 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500576 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578extern void *k_thread_custom_data_get(void);
579
580/**
Allan Stephensc98da842016-11-11 15:45:03 -0500581 * @} end addtogroup thread_apis
582 */
583
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400584#include <sys_clock.h>
585
Allan Stephensc2f15a42016-11-17 12:24:22 -0500586/**
587 * @addtogroup clock_apis
588 * @{
589 */
590
591/**
592 * @brief Generate null timeout delay.
593 *
594 * This macro generates a timeout delay that that instructs a kernel API
595 * not to wait if the requested operation cannot be performed immediately.
596 *
597 * @return Timeout delay value.
598 */
599#define K_NO_WAIT 0
600
601/**
602 * @brief Generate timeout delay from milliseconds.
603 *
604 * This macro generates a timeout delay that that instructs a kernel API
605 * to wait up to @a ms milliseconds to perform the requested operation.
606 *
607 * @param ms Duration in milliseconds.
608 *
609 * @return Timeout delay value.
610 */
Johan Hedberg14471692016-11-13 10:52:15 +0200611#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500612
613/**
614 * @brief Generate timeout delay from seconds.
615 *
616 * This macro generates a timeout delay that that instructs a kernel API
617 * to wait up to @a s seconds to perform the requested operation.
618 *
619 * @param s Duration in seconds.
620 *
621 * @return Timeout delay value.
622 */
Johan Hedberg14471692016-11-13 10:52:15 +0200623#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500624
625/**
626 * @brief Generate timeout delay from minutes.
627 *
628 * This macro generates a timeout delay that that instructs a kernel API
629 * to wait up to @a m minutes to perform the requested operation.
630 *
631 * @param m Duration in minutes.
632 *
633 * @return Timeout delay value.
634 */
Johan Hedberg14471692016-11-13 10:52:15 +0200635#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500636
637/**
638 * @brief Generate timeout delay from hours.
639 *
640 * This macro generates a timeout delay that that instructs a kernel API
641 * to wait up to @a h hours to perform the requested operation.
642 *
643 * @param h Duration in hours.
644 *
645 * @return Timeout delay value.
646 */
Johan Hedberg14471692016-11-13 10:52:15 +0200647#define K_HOURS(h) K_MINUTES((h) * 60)
648
Allan Stephensc98da842016-11-11 15:45:03 -0500649/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500650 * @brief Generate infinite timeout delay.
651 *
652 * This macro generates a timeout delay that that instructs a kernel API
653 * to wait as long as necessary to perform the requested operation.
654 *
655 * @return Timeout delay value.
656 */
657#define K_FOREVER (-1)
658
659/**
660 * @} end addtogroup clock_apis
661 */
662
663/**
Allan Stephensc98da842016-11-11 15:45:03 -0500664 * @cond INTERNAL_HIDDEN
665 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400666
Benjamin Walsh62092182016-12-20 14:39:08 -0500667/* kernel clocks */
668
669#if (sys_clock_ticks_per_sec == 1000) || \
670 (sys_clock_ticks_per_sec == 500) || \
671 (sys_clock_ticks_per_sec == 250) || \
672 (sys_clock_ticks_per_sec == 125) || \
673 (sys_clock_ticks_per_sec == 100) || \
674 (sys_clock_ticks_per_sec == 50) || \
675 (sys_clock_ticks_per_sec == 25) || \
676 (sys_clock_ticks_per_sec == 20) || \
677 (sys_clock_ticks_per_sec == 10) || \
678 (sys_clock_ticks_per_sec == 1)
679
680 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
681#else
682 /* yields horrible 64-bit math on many architectures: try to avoid */
683 #define _NON_OPTIMIZED_TICKS_PER_SEC
684#endif
685
686#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
687extern int32_t _ms_to_ticks(int32_t ms);
688#else
689static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
690{
691 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
692}
693#endif
694
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500695/* added tick needed to account for tick in progress */
696#define _TICK_ALIGN 1
697
Benjamin Walsh62092182016-12-20 14:39:08 -0500698static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400699{
Benjamin Walsh62092182016-12-20 14:39:08 -0500700#ifdef CONFIG_SYS_CLOCK_EXISTS
701
702#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400703 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400704#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500705 return (uint64_t)ticks * _ms_per_tick;
706#endif
707
708#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400709 __ASSERT(ticks == 0, "");
710 return 0;
711#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400712}
713
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400714/* timeouts */
715
716struct _timeout;
717typedef void (*_timeout_func_t)(struct _timeout *t);
718
719struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500720 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400721 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722 sys_dlist_t *wait_q;
723 int32_t delta_ticks_from_prev;
724 _timeout_func_t func;
725};
726
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200727extern int32_t _timeout_remaining_get(struct _timeout *timeout);
728
Allan Stephensc98da842016-11-11 15:45:03 -0500729/**
730 * INTERNAL_HIDDEN @endcond
731 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500732
Allan Stephensc98da842016-11-11 15:45:03 -0500733/**
734 * @cond INTERNAL_HIDDEN
735 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400736
737struct k_timer {
738 /*
739 * _timeout structure must be first here if we want to use
740 * dynamic timer allocation. timeout.node is used in the double-linked
741 * list of free timers
742 */
743 struct _timeout timeout;
744
Allan Stephens45bfa372016-10-12 12:39:42 -0500745 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400746 _wait_q_t wait_q;
747
748 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500749 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400750
751 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500752 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753
754 /* timer period */
755 int32_t period;
756
Allan Stephens45bfa372016-10-12 12:39:42 -0500757 /* timer status */
758 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759
Allan Stephens45bfa372016-10-12 12:39:42 -0500760 /* used to support legacy timer APIs */
761 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400762
Anas Nashif2f203c22016-12-18 06:57:45 -0500763 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764};
765
Allan Stephens1342adb2016-11-03 13:54:53 -0500766#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400767 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500768 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500769 .timeout.wait_q = NULL, \
770 .timeout.thread = NULL, \
771 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400772 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500773 .expiry_fn = expiry, \
774 .stop_fn = stop, \
775 .status = 0, \
776 ._legacy_data = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500777 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778 }
779
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780/**
Allan Stephensc98da842016-11-11 15:45:03 -0500781 * INTERNAL_HIDDEN @endcond
782 */
783
784/**
785 * @defgroup timer_apis Timer APIs
786 * @ingroup kernel_apis
787 * @{
788 */
789
790/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500791 * @typedef k_timer_expiry_t
792 * @brief Timer expiry function type.
793 *
794 * A timer's expiry function is executed by the system clock interrupt handler
795 * each time the timer expires. The expiry function is optional, and is only
796 * invoked if the timer has been initialized with one.
797 *
798 * @param timer Address of timer.
799 *
800 * @return N/A
801 */
802typedef void (*k_timer_expiry_t)(struct k_timer *timer);
803
804/**
805 * @typedef k_timer_stop_t
806 * @brief Timer stop function type.
807 *
808 * A timer's stop function is executed if the timer is stopped prematurely.
809 * The function runs in the context of the thread that stops the timer.
810 * The stop function is optional, and is only invoked if the timer has been
811 * initialized with one.
812 *
813 * @param timer Address of timer.
814 *
815 * @return N/A
816 */
817typedef void (*k_timer_stop_t)(struct k_timer *timer);
818
819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500824 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @param expiry_fn Function to invoke each time the timer expires.
828 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500830#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500831 struct k_timer name \
832 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500833 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400834
Allan Stephens45bfa372016-10-12 12:39:42 -0500835/**
836 * @brief Initialize a timer.
837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500839 *
840 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @param expiry_fn Function to invoke each time the timer expires.
842 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500843 *
844 * @return N/A
845 */
846extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500847 k_timer_expiry_t expiry_fn,
848 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700849
Allan Stephens45bfa372016-10-12 12:39:42 -0500850/**
851 * @brief Start a timer.
852 *
853 * This routine starts a timer, and resets its status to zero. The timer
854 * begins counting down using the specified duration and period values.
855 *
856 * Attempting to start a timer that is already running is permitted.
857 * The timer's status is reset to zero and the timer begins counting down
858 * using the new duration and period values.
859 *
860 * @param timer Address of timer.
861 * @param duration Initial timer duration (in milliseconds).
862 * @param period Timer period (in milliseconds).
863 *
864 * @return N/A
865 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500867 int32_t duration, int32_t period);
868
869/**
870 * @brief Stop a timer.
871 *
872 * This routine stops a running timer prematurely. The timer's stop function,
873 * if one exists, is invoked by the caller.
874 *
875 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500877 *
878 * @param timer Address of timer.
879 *
880 * @return N/A
881 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500883
884/**
885 * @brief Read timer status.
886 *
887 * This routine reads the timer's status, which indicates the number of times
888 * it has expired since its status was last read.
889 *
890 * Calling this routine resets the timer's status to zero.
891 *
892 * @param timer Address of timer.
893 *
894 * @return Timer status.
895 */
896extern uint32_t k_timer_status_get(struct k_timer *timer);
897
898/**
899 * @brief Synchronize thread to timer expiration.
900 *
901 * This routine blocks the calling thread until the timer's status is non-zero
902 * (indicating that it has expired at least once since it was last examined)
903 * or the timer is stopped. If the timer status is already non-zero,
904 * or the timer is already stopped, the caller continues without waiting.
905 *
906 * Calling this routine resets the timer's status to zero.
907 *
908 * This routine must not be used by interrupt handlers, since they are not
909 * allowed to block.
910 *
911 * @param timer Address of timer.
912 *
913 * @return Timer status.
914 */
915extern uint32_t k_timer_status_sync(struct k_timer *timer);
916
917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500919 *
920 * This routine computes the (approximate) time remaining before a running
921 * timer next expires. If the timer is not running, it returns zero.
922 *
923 * @param timer Address of timer.
924 *
925 * @return Remaining time (in milliseconds).
926 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200927static inline int32_t k_timer_remaining_get(struct k_timer *timer)
928{
929 return _timeout_remaining_get(&timer->timeout);
930}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400931
Allan Stephensc98da842016-11-11 15:45:03 -0500932/**
933 * @} end defgroup timer_apis
934 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400935
Allan Stephensc98da842016-11-11 15:45:03 -0500936/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500937 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500938 * @{
939 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500940
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500942 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * This routine returns the elapsed time since the system booted,
945 * in milliseconds.
946 *
947 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400948 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400950
951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * This routine returns the lower 32-bits of the elapsed time since the system
955 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500957 * This routine can be more efficient than k_uptime_get(), as it reduces the
958 * need for interrupt locking and 64-bit math. However, the 32-bit result
959 * cannot hold a system uptime time larger than approximately 50 days, so the
960 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400963 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400964extern uint32_t k_uptime_get_32(void);
965
966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * This routine computes the elapsed time between the current system uptime
970 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @param reftime Pointer to a reference time, which is updated to the current
973 * uptime upon return.
974 *
975 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400978
979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500980 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * This routine computes the elapsed time between the current system uptime
983 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * This routine can be more efficient than k_uptime_delta(), as it reduces the
986 * need for interrupt locking and 64-bit math. However, the 32-bit result
987 * cannot hold an elapsed time larger than approximately 50 days, so the
988 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @param reftime Pointer to a reference time, which is updated to the current
991 * uptime upon return.
992 *
993 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400994 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400995extern uint32_t k_uptime_delta_32(int64_t *reftime);
996
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * This routine returns the current time, as measured by the system's hardware
1001 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001005extern uint32_t k_cycle_get_32(void);
1006
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001007/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001008 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001009 */
1010
Allan Stephensc98da842016-11-11 15:45:03 -05001011/**
1012 * @cond INTERNAL_HIDDEN
1013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001014
1015struct k_fifo {
1016 _wait_q_t wait_q;
1017 sys_slist_t data_q;
1018
Anas Nashif2f203c22016-12-18 06:57:45 -05001019 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020};
1021
Allan Stephensc98da842016-11-11 15:45:03 -05001022#define K_FIFO_INITIALIZER(obj) \
1023 { \
1024 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1025 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001026 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001027 }
1028
1029/**
1030 * INTERNAL_HIDDEN @endcond
1031 */
1032
1033/**
1034 * @defgroup fifo_apis Fifo APIs
1035 * @ingroup kernel_apis
1036 * @{
1037 */
1038
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
1046 * @return N/A
1047 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049
1050/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001051 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001053 * This routine adds a data item to @a fifo. A fifo data item must be
1054 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1055 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001057 * @note Can be called by ISRs.
1058 *
1059 * @param fifo Address of the fifo.
1060 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 *
1062 * @return N/A
1063 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001064extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001065
1066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * This routine adds a list of data items to @a fifo in one operation.
1070 * The data items must be in a singly-linked list, with the first 32 bits
1071 * each data item pointing to the next data item; the list must be
1072 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @param fifo Address of the fifo.
1077 * @param head Pointer to first node in singly-linked list.
1078 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001079 *
1080 * @return N/A
1081 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083
1084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001085 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * This routine adds a list of data items to @a fifo in one operation.
1088 * The data items must be in a singly-linked list implemented using a
1089 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090 * and must be re-initialized via sys_slist_init().
1091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * @note Can be called by ISRs.
1093 *
1094 * @param fifo Address of the fifo.
1095 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 *
1097 * @return N/A
1098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100
1101/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001104 * This routine removes a data item from @a fifo in a "first in, first out"
1105 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1108 *
1109 * @param fifo Address of the fifo.
1110 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 * or one of the special values K_NO_WAIT and K_FOREVER.
1112 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001113 * @return Address of the data item if successful; NULL if returned
1114 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001116extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1117
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001123 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001128 struct k_fifo name \
1129 __in_section(_k_fifo, static, name) = \
1130 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131
Allan Stephensc98da842016-11-11 15:45:03 -05001132/**
1133 * @} end defgroup fifo_apis
1134 */
1135
1136/**
1137 * @cond INTERNAL_HIDDEN
1138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001139
1140struct k_lifo {
1141 _wait_q_t wait_q;
1142 void *list;
1143
Anas Nashif2f203c22016-12-18 06:57:45 -05001144 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001145};
1146
Allan Stephensc98da842016-11-11 15:45:03 -05001147#define K_LIFO_INITIALIZER(obj) \
1148 { \
1149 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1150 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001151 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001152 }
1153
1154/**
1155 * INTERNAL_HIDDEN @endcond
1156 */
1157
1158/**
1159 * @defgroup lifo_apis Lifo APIs
1160 * @ingroup kernel_apis
1161 * @{
1162 */
1163
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
1171 * @return N/A
1172 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174
1175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * This routine adds a data item to @a lifo. A lifo data item must be
1179 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1180 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * @note Can be called by ISRs.
1183 *
1184 * @param lifo Address of the lifo.
1185 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 *
1187 * @return N/A
1188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001190
1191/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001194 * This routine removes a data item from @a lifo in a "last in, first out"
1195 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1198 *
1199 * @param lifo Address of the lifo.
1200 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001201 * or one of the special values K_NO_WAIT and K_FOREVER.
1202 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001203 * @return Address of the data item if successful; NULL if returned
1204 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1207
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001209 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001211 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001212 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001213 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001218 struct k_lifo name \
1219 __in_section(_k_lifo, static, name) = \
1220 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001221
Allan Stephensc98da842016-11-11 15:45:03 -05001222/**
1223 * @} end defgroup lifo_apis
1224 */
1225
1226/**
1227 * @cond INTERNAL_HIDDEN
1228 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
1230struct k_stack {
1231 _wait_q_t wait_q;
1232 uint32_t *base, *next, *top;
1233
Anas Nashif2f203c22016-12-18 06:57:45 -05001234 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001235};
1236
Allan Stephensc98da842016-11-11 15:45:03 -05001237#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1238 { \
1239 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1240 .base = stack_buffer, \
1241 .next = stack_buffer, \
1242 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001243 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001244 }
1245
1246/**
1247 * INTERNAL_HIDDEN @endcond
1248 */
1249
1250/**
1251 * @defgroup stack_apis Stack APIs
1252 * @ingroup kernel_apis
1253 * @{
1254 */
1255
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001256/**
1257 * @brief Initialize a stack.
1258 *
1259 * This routine initializes a stack object, prior to its first use.
1260 *
1261 * @param stack Address of the stack.
1262 * @param buffer Address of array used to hold stacked values.
1263 * @param num_entries Maximum number of values that can be stacked.
1264 *
1265 * @return N/A
1266 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001267extern void k_stack_init(struct k_stack *stack,
1268 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001269
1270/**
1271 * @brief Push an element onto a stack.
1272 *
1273 * This routine adds a 32-bit value @a data to @a stack.
1274 *
1275 * @note Can be called by ISRs.
1276 *
1277 * @param stack Address of the stack.
1278 * @param data Value to push onto the stack.
1279 *
1280 * @return N/A
1281 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001282extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001283
1284/**
1285 * @brief Pop an element from a stack.
1286 *
1287 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1288 * manner and stores the value in @a data.
1289 *
1290 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1291 *
1292 * @param stack Address of the stack.
1293 * @param data Address of area to hold the value popped from the stack.
1294 * @param timeout Waiting period to obtain a value (in milliseconds),
1295 * or one of the special values K_NO_WAIT and K_FOREVER.
1296 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001297 * @retval 0 Element popped from stack.
1298 * @retval -EBUSY Returned without waiting.
1299 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1302
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001303/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001304 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001306 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001307 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001308 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001310 * @param name Name of the stack.
1311 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001313#define K_STACK_DEFINE(name, stack_num_entries) \
1314 uint32_t __noinit \
1315 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001316 struct k_stack name \
1317 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001318 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1319 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001320
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321/**
Allan Stephensc98da842016-11-11 15:45:03 -05001322 * @} end defgroup stack_apis
1323 */
1324
Allan Stephens6bba9b02016-11-16 14:56:54 -05001325struct k_work;
1326
Allan Stephensc98da842016-11-11 15:45:03 -05001327/**
1328 * @defgroup workqueue_apis Workqueue Thread APIs
1329 * @ingroup kernel_apis
1330 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331 */
1332
Allan Stephens6bba9b02016-11-16 14:56:54 -05001333/**
1334 * @typedef k_work_handler_t
1335 * @brief Work item handler function type.
1336 *
1337 * A work item's handler function is executed by a workqueue's thread
1338 * when the work item is processed by the workqueue.
1339 *
1340 * @param work Address of the work item.
1341 *
1342 * @return N/A
1343 */
1344typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001345
1346/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001347 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001349
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001350struct k_work_q {
1351 struct k_fifo fifo;
1352};
1353
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001355 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001356};
1357
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001358struct k_work {
1359 void *_reserved; /* Used by k_fifo implementation. */
1360 k_work_handler_t handler;
1361 atomic_t flags[1];
1362};
1363
Allan Stephens6bba9b02016-11-16 14:56:54 -05001364struct k_delayed_work {
1365 struct k_work work;
1366 struct _timeout timeout;
1367 struct k_work_q *work_q;
1368};
1369
1370extern struct k_work_q k_sys_work_q;
1371
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001372/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001373 * INTERNAL_HIDDEN @endcond
1374 */
1375
1376/**
1377 * @brief Initialize a statically-defined work item.
1378 *
1379 * This macro can be used to initialize a statically-defined workqueue work
1380 * item, prior to its first use. For example,
1381 *
1382 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1383 *
1384 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001385 */
1386#define K_WORK_INITIALIZER(work_handler) \
1387 { \
1388 ._reserved = NULL, \
1389 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001390 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 }
1392
1393/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001394 * @brief Initialize a work item.
1395 *
1396 * This routine initializes a workqueue work item, prior to its first use.
1397 *
1398 * @param work Address of work item.
1399 * @param handler Function to invoke each time work item is processed.
1400 *
1401 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402 */
1403static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1404{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001405 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406 work->handler = handler;
1407}
1408
1409/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001410 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001411 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001412 * This routine submits work item @a work to be processed by workqueue
1413 * @a work_q. If the work item is already pending in the workqueue's queue
1414 * as a result of an earlier submission, this routine has no effect on the
1415 * work item. If the work item has already been processed, or is currently
1416 * being processed, its work is considered complete and the work item can be
1417 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001418 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001419 * @warning
1420 * A submitted work item must not be modified until it has been processed
1421 * by the workqueue.
1422 *
1423 * @note Can be called by ISRs.
1424 *
1425 * @param work_q Address of workqueue.
1426 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001427 *
1428 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429 */
1430static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1431 struct k_work *work)
1432{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001433 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 k_fifo_put(&work_q->fifo, work);
1435 }
1436}
1437
1438/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001439 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001440 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001441 * This routine indicates if work item @a work is pending in a workqueue's
1442 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001443 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001444 * @note Can be called by ISRs.
1445 *
1446 * @param work Address of work item.
1447 *
1448 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001449 */
1450static inline int k_work_pending(struct k_work *work)
1451{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001452 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001453}
1454
1455/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001456 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 * This routine starts workqueue @a work_q. The workqueue spawns its work
1459 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001460 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001461 * @param work_q Address of workqueue.
1462 * @param stack Pointer to work queue thread's stack space.
1463 * @param stack_size Size of the work queue thread's stack (in bytes).
1464 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465 *
1466 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467 */
Allan Stephens904cf972016-10-07 13:59:23 -05001468extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001469 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001470
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001471/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001472 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001473 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001474 * This routine initializes a workqueue delayed work item, prior to
1475 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001477 * @param work Address of delayed work item.
1478 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
1480 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001481 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001482extern void k_delayed_work_init(struct k_delayed_work *work,
1483 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001484
1485/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001486 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001487 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001488 * This routine schedules work item @a work to be processed by workqueue
1489 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1490 * an asychronous countdown for the work item and then returns to the caller.
1491 * Only when the countdown completes is the work item actually submitted to
1492 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001493 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001494 * Submitting a previously submitted delayed work item that is still
1495 * counting down cancels the existing submission and restarts the countdown
1496 * using the new delay. If the work item is currently pending on the
1497 * workqueue's queue because the countdown has completed it is too late to
1498 * resubmit the item, and resubmission fails without impacting the work item.
1499 * If the work item has already been processed, or is currently being processed,
1500 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001502 * @warning
1503 * A delayed work item must not be modified until it has been processed
1504 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001506 * @note Can be called by ISRs.
1507 *
1508 * @param work_q Address of workqueue.
1509 * @param work Address of delayed work item.
1510 * @param delay Delay before submitting the work item (in milliseconds).
1511 *
1512 * @retval 0 Work item countdown started.
1513 * @retval -EINPROGRESS Work item is already pending.
1514 * @retval -EINVAL Work item is being processed or has completed its work.
1515 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001516 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001517extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1518 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001519 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001520
1521/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001522 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001523 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001524 * This routine cancels the submission of delayed work item @a work.
1525 * A delayed work item can only be cancelled while its countdown is still
1526 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001527 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001528 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001529 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001530 * @param work Address of delayed work item.
1531 *
1532 * @retval 0 Work item countdown cancelled.
1533 * @retval -EINPROGRESS Work item is already pending.
1534 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001535 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001536extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001538/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001539 * @brief Submit a work item to the system workqueue.
1540 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001541 * This routine submits work item @a work to be processed by the system
1542 * workqueue. If the work item is already pending in the workqueue's queue
1543 * as a result of an earlier submission, this routine has no effect on the
1544 * work item. If the work item has already been processed, or is currently
1545 * being processed, its work is considered complete and the work item can be
1546 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001547 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001548 * @warning
1549 * Work items submitted to the system workqueue should avoid using handlers
1550 * that block or yield since this may prevent the system workqueue from
1551 * processing other work items in a timely manner.
1552 *
1553 * @note Can be called by ISRs.
1554 *
1555 * @param work Address of work item.
1556 *
1557 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001558 */
1559static inline void k_work_submit(struct k_work *work)
1560{
1561 k_work_submit_to_queue(&k_sys_work_q, work);
1562}
1563
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001564/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001565 * @brief Submit a delayed work item to the system workqueue.
1566 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001567 * This routine schedules work item @a work to be processed by the system
1568 * workqueue after a delay of @a delay milliseconds. The routine initiates
1569 * an asychronous countdown for the work item and then returns to the caller.
1570 * Only when the countdown completes is the work item actually submitted to
1571 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001573 * Submitting a previously submitted delayed work item that is still
1574 * counting down cancels the existing submission and restarts the countdown
1575 * using the new delay. If the work item is currently pending on the
1576 * workqueue's queue because the countdown has completed it is too late to
1577 * resubmit the item, and resubmission fails without impacting the work item.
1578 * If the work item has already been processed, or is currently being processed,
1579 * its work is considered complete and the work item can be resubmitted.
1580 *
1581 * @warning
1582 * Work items submitted to the system workqueue should avoid using handlers
1583 * that block or yield since this may prevent the system workqueue from
1584 * processing other work items in a timely manner.
1585 *
1586 * @note Can be called by ISRs.
1587 *
1588 * @param work Address of delayed work item.
1589 * @param delay Delay before submitting the work item (in milliseconds).
1590 *
1591 * @retval 0 Work item countdown started.
1592 * @retval -EINPROGRESS Work item is already pending.
1593 * @retval -EINVAL Work item is being processed or has completed its work.
1594 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001595 */
1596static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001597 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001598{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001599 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001600}
1601
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001602/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001603 * @brief Get time remaining before a delayed work gets scheduled.
1604 *
1605 * This routine computes the (approximate) time remaining before a
1606 * delayed work gets executed. If the delayed work is not waiting to be
1607 * schedules, it returns zero.
1608 *
1609 * @param work Delayed work item.
1610 *
1611 * @return Remaining time (in milliseconds).
1612 */
1613static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1614{
1615 return _timeout_remaining_get(&work->timeout);
1616}
1617
1618/**
Allan Stephensc98da842016-11-11 15:45:03 -05001619 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001620 */
1621
Allan Stephensc98da842016-11-11 15:45:03 -05001622/**
1623 * @cond INTERNAL_HIDDEN
1624 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625
1626struct k_mutex {
1627 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001628 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629 uint32_t lock_count;
1630 int owner_orig_prio;
1631#ifdef CONFIG_OBJECT_MONITOR
1632 int num_lock_state_changes;
1633 int num_conflicts;
1634#endif
1635
Anas Nashif2f203c22016-12-18 06:57:45 -05001636 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001637};
1638
1639#ifdef CONFIG_OBJECT_MONITOR
1640#define _MUTEX_INIT_OBJECT_MONITOR \
1641 .num_lock_state_changes = 0, .num_conflicts = 0,
1642#else
1643#define _MUTEX_INIT_OBJECT_MONITOR
1644#endif
1645
1646#define K_MUTEX_INITIALIZER(obj) \
1647 { \
1648 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1649 .owner = NULL, \
1650 .lock_count = 0, \
1651 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1652 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001653 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001654 }
1655
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001656/**
Allan Stephensc98da842016-11-11 15:45:03 -05001657 * INTERNAL_HIDDEN @endcond
1658 */
1659
1660/**
1661 * @defgroup mutex_apis Mutex APIs
1662 * @ingroup kernel_apis
1663 * @{
1664 */
1665
1666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001669 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001670 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001671 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001674 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001676 struct k_mutex name \
1677 __in_section(_k_mutex, static, name) = \
1678 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001679
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001680/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001681 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * Upon completion, the mutex is available and does not have an owner.
1686 *
1687 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001688 *
1689 * @return N/A
1690 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001691extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001692
1693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * This routine locks @a mutex. If the mutex is locked by another thread,
1697 * the calling thread waits until the mutex becomes available or until
1698 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * A thread is permitted to lock a mutex it has already locked. The operation
1701 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001703 * @param mutex Address of the mutex.
1704 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 * or one of the special values K_NO_WAIT and K_FOREVER.
1706 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001707 * @retval 0 Mutex locked.
1708 * @retval -EBUSY Returned without waiting.
1709 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001710 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001711extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001712
1713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine unlocks @a mutex. The mutex must already be locked by the
1717 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001718 *
1719 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 * thread.
1722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 *
1725 * @return N/A
1726 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727extern void k_mutex_unlock(struct k_mutex *mutex);
1728
Allan Stephensc98da842016-11-11 15:45:03 -05001729/**
1730 * @} end defgroup mutex_apis
1731 */
1732
1733/**
1734 * @cond INTERNAL_HIDDEN
1735 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001736
1737struct k_sem {
1738 _wait_q_t wait_q;
1739 unsigned int count;
1740 unsigned int limit;
1741
Anas Nashif2f203c22016-12-18 06:57:45 -05001742 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743};
1744
Allan Stephensc98da842016-11-11 15:45:03 -05001745#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1746 { \
1747 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1748 .count = initial_count, \
1749 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001750 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001751 }
1752
1753/**
1754 * INTERNAL_HIDDEN @endcond
1755 */
1756
1757/**
1758 * @defgroup semaphore_apis Semaphore APIs
1759 * @ingroup kernel_apis
1760 * @{
1761 */
1762
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001763/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * @param sem Address of the semaphore.
1769 * @param initial_count Initial semaphore count.
1770 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001771 *
1772 * @return N/A
1773 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1775 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001776
1777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001782 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1783 *
1784 * @param sem Address of the semaphore.
1785 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001786 * or one of the special values K_NO_WAIT and K_FOREVER.
1787 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001788 * @note When porting code from the nanokernel legacy API to the new API, be
1789 * careful with the return value of this function. The return value is the
1790 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1791 * non-zero means failure, while the nano_sem_take family returns 1 for success
1792 * and 0 for failure.
1793 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001794 * @retval 0 Semaphore taken.
1795 * @retval -EBUSY Returned without waiting.
1796 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001799
1800/**
1801 * @brief Give a semaphore.
1802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * This routine gives @a sem, unless the semaphore is already at its maximum
1804 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001808 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001809 *
1810 * @return N/A
1811 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812extern void k_sem_give(struct k_sem *sem);
1813
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001814/**
1815 * @brief Reset a semaphore's count to zero.
1816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001819 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001820 *
1821 * @return N/A
1822 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001823static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824{
1825 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826}
1827
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001828/**
1829 * @brief Get a semaphore's count.
1830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001833 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001836 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001837static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838{
1839 return sem->count;
1840}
1841
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001846 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001847 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * @param name Name of the semaphore.
1850 * @param initial_count Initial semaphore count.
1851 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001852 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001853#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001854 struct k_sem name \
1855 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001856 K_SEM_INITIALIZER(name, initial_count, count_limit)
1857
Allan Stephensc98da842016-11-11 15:45:03 -05001858/**
1859 * @} end defgroup semaphore_apis
1860 */
1861
1862/**
1863 * @defgroup alert_apis Alert APIs
1864 * @ingroup kernel_apis
1865 * @{
1866 */
1867
Allan Stephens5eceb852016-11-16 10:16:30 -05001868/**
1869 * @typedef k_alert_handler_t
1870 * @brief Alert handler function type.
1871 *
1872 * An alert's alert handler function is invoked by the system workqueue
1873 * when the alert is signalled. The alert handler function is optional,
1874 * and is only invoked if the alert has been initialized with one.
1875 *
1876 * @param alert Address of the alert.
1877 *
1878 * @return 0 if alert has been consumed; non-zero if alert should pend.
1879 */
1880typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001881
1882/**
1883 * @} end defgroup alert_apis
1884 */
1885
1886/**
1887 * @cond INTERNAL_HIDDEN
1888 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001889
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001890#define K_ALERT_DEFAULT NULL
1891#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001893struct k_alert {
1894 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895 atomic_t send_count;
1896 struct k_work work_item;
1897 struct k_sem sem;
1898
Anas Nashif2f203c22016-12-18 06:57:45 -05001899 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900};
1901
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001902extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001904#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001906 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001907 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001908 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001909 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001910 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911 }
1912
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913/**
Allan Stephensc98da842016-11-11 15:45:03 -05001914 * INTERNAL_HIDDEN @endcond
1915 */
1916
1917/**
1918 * @addtogroup alert_apis
1919 * @{
1920 */
1921
1922/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001925 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001927 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001929 * @param name Name of the alert.
1930 * @param alert_handler Action to take when alert is sent. Specify either
1931 * the address of a function to be invoked by the system workqueue
1932 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1933 * K_ALERT_DEFAULT (which causes the alert to pend).
1934 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001936#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001937 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001938 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001939 K_ALERT_INITIALIZER(name, alert_handler, \
1940 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001941
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001943 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001945 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001947 * @param alert Address of the alert.
1948 * @param handler Action to take when alert is sent. Specify either the address
1949 * of a function to be invoked by the system workqueue thread,
1950 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1951 * K_ALERT_DEFAULT (which causes the alert to pend).
1952 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
1954 * @return N/A
1955 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001956extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1957 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958
1959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001960 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001962 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001964 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1965 *
1966 * @param alert Address of the alert.
1967 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 * or one of the special values K_NO_WAIT and K_FOREVER.
1969 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001970 * @retval 0 Alert received.
1971 * @retval -EBUSY Returned without waiting.
1972 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001974extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975
1976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * This routine signals @a alert. The action specified for @a alert will
1980 * be taken, which may trigger the execution of an alert handler function
1981 * and/or cause the alert to pend (assuming the alert has not reached its
1982 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * @note Can be called by ISRs.
1985 *
1986 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987 *
1988 * @return N/A
1989 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001990extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991
1992/**
Allan Stephensc98da842016-11-11 15:45:03 -05001993 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001994 */
1995
Allan Stephensc98da842016-11-11 15:45:03 -05001996/**
1997 * @cond INTERNAL_HIDDEN
1998 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
2000struct k_msgq {
2001 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002002 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003 uint32_t max_msgs;
2004 char *buffer_start;
2005 char *buffer_end;
2006 char *read_ptr;
2007 char *write_ptr;
2008 uint32_t used_msgs;
2009
Anas Nashif2f203c22016-12-18 06:57:45 -05002010 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002011};
2012
Peter Mitsis1da807e2016-10-06 11:36:59 -04002013#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014 { \
2015 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002016 .max_msgs = q_max_msgs, \
2017 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002019 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002020 .read_ptr = q_buffer, \
2021 .write_ptr = q_buffer, \
2022 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002023 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002024 }
2025
Peter Mitsis1da807e2016-10-06 11:36:59 -04002026/**
Allan Stephensc98da842016-11-11 15:45:03 -05002027 * INTERNAL_HIDDEN @endcond
2028 */
2029
2030/**
2031 * @defgroup msgq_apis Message Queue APIs
2032 * @ingroup kernel_apis
2033 * @{
2034 */
2035
2036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2040 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002041 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2042 * message is similarly aligned to this boundary, @a q_msg_size must also be
2043 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * The message queue can be accessed outside the module where it is defined
2046 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002047 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002048 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002050 * @param q_name Name of the message queue.
2051 * @param q_msg_size Message size (in bytes).
2052 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002053 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002054 */
2055#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2056 static char __noinit __aligned(q_align) \
2057 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002058 struct k_msgq q_name \
2059 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002060 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2061 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062
Peter Mitsisd7a37502016-10-13 11:37:40 -04002063/**
2064 * @brief Initialize a message queue.
2065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066 * This routine initializes a message queue object, prior to its first use.
2067 *
Allan Stephensda827222016-11-09 14:23:58 -06002068 * The message queue's ring buffer must contain space for @a max_msgs messages,
2069 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2070 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2071 * that each message is similarly aligned to this boundary, @a q_msg_size
2072 * must also be a multiple of N.
2073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @param q Address of the message queue.
2075 * @param buffer Pointer to ring buffer that holds queued messages.
2076 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002077 * @param max_msgs Maximum number of messages that can be queued.
2078 *
2079 * @return N/A
2080 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002081extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002082 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002083
2084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002087 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002088 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002089 * @note Can be called by ISRs.
2090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * @param q Address of the message queue.
2092 * @param data Pointer to the message.
2093 * @param timeout Waiting period to add the message (in milliseconds),
2094 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002095 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002096 * @retval 0 Message sent.
2097 * @retval -ENOMSG Returned without waiting or queue purged.
2098 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002099 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002101
2102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * This routine receives a message from message queue @a q in a "first in,
2106 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002107 *
Allan Stephensc98da842016-11-11 15:45:03 -05002108 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @param q Address of the message queue.
2111 * @param data Address of area to hold the received message.
2112 * @param timeout Waiting period to receive the message (in milliseconds),
2113 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002114 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002115 * @retval 0 Message received.
2116 * @retval -ENOMSG Returned without waiting.
2117 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002118 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002120
2121/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002122 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * This routine discards all unreceived messages in a message queue's ring
2125 * buffer. Any threads that are blocked waiting to send a message to the
2126 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002129 *
2130 * @return N/A
2131 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132extern void k_msgq_purge(struct k_msgq *q);
2133
Peter Mitsis67be2492016-10-07 11:44:34 -04002134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * This routine returns the number of unused entries in a message queue's
2138 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * @param q Address of the message queue.
2141 *
2142 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002143 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002144static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002145{
2146 return q->max_msgs - q->used_msgs;
2147}
2148
Peter Mitsisd7a37502016-10-13 11:37:40 -04002149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002152 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002154 * @param q Address of the message queue.
2155 *
2156 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002157 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002158static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159{
2160 return q->used_msgs;
2161}
2162
Allan Stephensc98da842016-11-11 15:45:03 -05002163/**
2164 * @} end defgroup msgq_apis
2165 */
2166
2167/**
2168 * @defgroup mem_pool_apis Memory Pool APIs
2169 * @ingroup kernel_apis
2170 * @{
2171 */
2172
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002174 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175 void *addr_in_pool;
2176 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002177 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178};
2179
Allan Stephensc98da842016-11-11 15:45:03 -05002180/**
2181 * @} end defgroup mem_pool_apis
2182 */
2183
2184/**
2185 * @defgroup mailbox_apis Mailbox APIs
2186 * @ingroup kernel_apis
2187 * @{
2188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189
2190struct k_mbox_msg {
2191 /** internal use only - needed for legacy API support */
2192 uint32_t _mailbox;
2193 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002194 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195 /** application-defined information value */
2196 uint32_t info;
2197 /** sender's message data buffer */
2198 void *tx_data;
2199 /** internal use only - needed for legacy API support */
2200 void *_rx_data;
2201 /** message data block descriptor */
2202 struct k_mem_block tx_block;
2203 /** source thread id */
2204 k_tid_t rx_source_thread;
2205 /** target thread id */
2206 k_tid_t tx_target_thread;
2207 /** internal use only - thread waiting on send (may be a dummy) */
2208 k_tid_t _syncing_thread;
2209#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2210 /** internal use only - semaphore used during asynchronous send */
2211 struct k_sem *_async_sem;
2212#endif
2213};
2214
Allan Stephensc98da842016-11-11 15:45:03 -05002215/**
2216 * @cond INTERNAL_HIDDEN
2217 */
2218
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219struct k_mbox {
2220 _wait_q_t tx_msg_queue;
2221 _wait_q_t rx_msg_queue;
2222
Anas Nashif2f203c22016-12-18 06:57:45 -05002223 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224};
2225
2226#define K_MBOX_INITIALIZER(obj) \
2227 { \
2228 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2229 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002230 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231 }
2232
Peter Mitsis12092702016-10-14 12:57:23 -04002233/**
Allan Stephensc98da842016-11-11 15:45:03 -05002234 * INTERNAL_HIDDEN @endcond
2235 */
2236
2237/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002238 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002240 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002242 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002244 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002247 struct k_mbox name \
2248 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249 K_MBOX_INITIALIZER(name) \
2250
Peter Mitsis12092702016-10-14 12:57:23 -04002251/**
2252 * @brief Initialize a mailbox.
2253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002254 * This routine initializes a mailbox object, prior to its first use.
2255 *
2256 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002257 *
2258 * @return N/A
2259 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260extern void k_mbox_init(struct k_mbox *mbox);
2261
Peter Mitsis12092702016-10-14 12:57:23 -04002262/**
2263 * @brief Send a mailbox message in a synchronous manner.
2264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * This routine sends a message to @a mbox and waits for a receiver to both
2266 * receive and process it. The message data may be in a buffer, in a memory
2267 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002269 * @param mbox Address of the mailbox.
2270 * @param tx_msg Address of the transmit message descriptor.
2271 * @param timeout Waiting period for the message to be received (in
2272 * milliseconds), or one of the special values K_NO_WAIT
2273 * and K_FOREVER. Once the message has been received,
2274 * this routine waits as long as necessary for the message
2275 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002276 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002277 * @retval 0 Message sent.
2278 * @retval -ENOMSG Returned without waiting.
2279 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002280 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002281extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002283
Peter Mitsis12092702016-10-14 12:57:23 -04002284/**
2285 * @brief Send a mailbox message in an asynchronous manner.
2286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * This routine sends a message to @a mbox without waiting for a receiver
2288 * to process it. The message data may be in a buffer, in a memory pool block,
2289 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2290 * will be given when the message has been both received and completely
2291 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @param mbox Address of the mailbox.
2294 * @param tx_msg Address of the transmit message descriptor.
2295 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002296 *
2297 * @return N/A
2298 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002299extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 struct k_sem *sem);
2301
Peter Mitsis12092702016-10-14 12:57:23 -04002302/**
2303 * @brief Receive a mailbox message.
2304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002305 * This routine receives a message from @a mbox, then optionally retrieves
2306 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002308 * @param mbox Address of the mailbox.
2309 * @param rx_msg Address of the receive message descriptor.
2310 * @param buffer Address of the buffer to receive data, or NULL to defer data
2311 * retrieval and message disposal until later.
2312 * @param timeout Waiting period for a message to be received (in
2313 * milliseconds), or one of the special values K_NO_WAIT
2314 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002315 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002316 * @retval 0 Message received.
2317 * @retval -ENOMSG Returned without waiting.
2318 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002319 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002320extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002322
2323/**
2324 * @brief Retrieve mailbox message data into a buffer.
2325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * This routine completes the processing of a received message by retrieving
2327 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002328 *
2329 * Alternatively, this routine can be used to dispose of a received message
2330 * without retrieving its data.
2331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @param rx_msg Address of the receive message descriptor.
2333 * @param buffer Address of the buffer to receive data, or NULL to discard
2334 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002335 *
2336 * @return N/A
2337 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002338extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002339
2340/**
2341 * @brief Retrieve mailbox message data into a memory pool block.
2342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * This routine completes the processing of a received message by retrieving
2344 * its data into a memory pool block, then disposing of the message.
2345 * The memory pool block that results from successful retrieval must be
2346 * returned to the pool once the data has been processed, even in cases
2347 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002348 *
2349 * Alternatively, this routine can be used to dispose of a received message
2350 * without retrieving its data. In this case there is no need to return a
2351 * memory pool block to the pool.
2352 *
2353 * This routine allocates a new memory pool block for the data only if the
2354 * data is not already in one. If a new block cannot be allocated, the routine
2355 * returns a failure code and the received message is left unchanged. This
2356 * permits the caller to reattempt data retrieval at a later time or to dispose
2357 * of the received message without retrieving its data.
2358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * @param rx_msg Address of a receive message descriptor.
2360 * @param pool Address of memory pool, or NULL to discard data.
2361 * @param block Address of the area to hold memory pool block info.
2362 * @param timeout Waiting period to wait for a memory pool block (in
2363 * milliseconds), or one of the special values K_NO_WAIT
2364 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002365 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002366 * @retval 0 Data retrieved.
2367 * @retval -ENOMEM Returned without waiting.
2368 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002369 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002370extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002371 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372 struct k_mem_block *block, int32_t timeout);
2373
Allan Stephensc98da842016-11-11 15:45:03 -05002374/**
2375 * @} end defgroup mailbox_apis
2376 */
2377
2378/**
2379 * @cond INTERNAL_HIDDEN
2380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
2382struct k_pipe {
2383 unsigned char *buffer; /* Pipe buffer: may be NULL */
2384 size_t size; /* Buffer size */
2385 size_t bytes_used; /* # bytes used in buffer */
2386 size_t read_index; /* Where in buffer to read from */
2387 size_t write_index; /* Where in buffer to write */
2388
2389 struct {
2390 _wait_q_t readers; /* Reader wait queue */
2391 _wait_q_t writers; /* Writer wait queue */
2392 } wait_q;
2393
Anas Nashif2f203c22016-12-18 06:57:45 -05002394 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395};
2396
Peter Mitsise5d9c582016-10-14 14:44:57 -04002397#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398 { \
2399 .buffer = pipe_buffer, \
2400 .size = pipe_buffer_size, \
2401 .bytes_used = 0, \
2402 .read_index = 0, \
2403 .write_index = 0, \
2404 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2405 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002406 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407 }
2408
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002409/**
Allan Stephensc98da842016-11-11 15:45:03 -05002410 * INTERNAL_HIDDEN @endcond
2411 */
2412
2413/**
2414 * @defgroup pipe_apis Pipe APIs
2415 * @ingroup kernel_apis
2416 * @{
2417 */
2418
2419/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002424 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002426 * @param name Name of the pipe.
2427 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2428 * or zero if no ring buffer is used.
2429 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002431#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2432 static unsigned char __noinit __aligned(pipe_align) \
2433 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002434 struct k_pipe name \
2435 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002436 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002441 * This routine initializes a pipe object, prior to its first use.
2442 *
2443 * @param pipe Address of the pipe.
2444 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2445 * is used.
2446 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2447 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002448 *
2449 * @return N/A
2450 */
2451extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2452 size_t size);
2453
2454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002455 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002457 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @param pipe Address of the pipe.
2460 * @param data Address of data to write.
2461 * @param bytes_to_write Size of data (in bytes).
2462 * @param bytes_written Address of area to hold the number of bytes written.
2463 * @param min_xfer Minimum number of bytes to write.
2464 * @param timeout Waiting period to wait for the data to be written (in
2465 * milliseconds), or one of the special values K_NO_WAIT
2466 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002468 * @retval 0 At least @a min_xfer bytes of data were written.
2469 * @retval -EIO Returned without waiting; zero data bytes were written.
2470 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002473extern int k_pipe_put(struct k_pipe *pipe, void *data,
2474 size_t bytes_to_write, size_t *bytes_written,
2475 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476
2477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @param pipe Address of the pipe.
2483 * @param data Address to place the data read from pipe.
2484 * @param bytes_to_read Maximum number of data bytes to read.
2485 * @param bytes_read Address of area to hold the number of bytes read.
2486 * @param min_xfer Minimum number of data bytes to read.
2487 * @param timeout Waiting period to wait for the data to be read (in
2488 * milliseconds), or one of the special values K_NO_WAIT
2489 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002490 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002491 * @retval 0 At least @a min_xfer bytes of data were read.
2492 * @retval -EIO Returned without waiting; zero data bytes were read.
2493 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002496extern int k_pipe_get(struct k_pipe *pipe, void *data,
2497 size_t bytes_to_read, size_t *bytes_read,
2498 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499
2500/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * This routine writes the data contained in a memory block to @a pipe.
2504 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508 * @param block Memory block containing data to send
2509 * @param size Number of data bytes in memory block to send
2510 * @param sem Semaphore to signal upon completion (else NULL)
2511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 */
2514extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2515 size_t size, struct k_sem *sem);
2516
2517/**
Allan Stephensc98da842016-11-11 15:45:03 -05002518 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519 */
2520
Allan Stephensc98da842016-11-11 15:45:03 -05002521/**
2522 * @cond INTERNAL_HIDDEN
2523 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002525struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002527 uint32_t num_blocks;
2528 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529 char *buffer;
2530 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002531 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532
Anas Nashif2f203c22016-12-18 06:57:45 -05002533 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534};
2535
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002536#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2537 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 { \
2539 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002540 .num_blocks = slab_num_blocks, \
2541 .block_size = slab_block_size, \
2542 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543 .free_list = NULL, \
2544 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002545 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546 }
2547
Peter Mitsis578f9112016-10-07 13:50:31 -04002548/**
Allan Stephensc98da842016-11-11 15:45:03 -05002549 * INTERNAL_HIDDEN @endcond
2550 */
2551
2552/**
2553 * @defgroup mem_slab_apis Memory Slab APIs
2554 * @ingroup kernel_apis
2555 * @{
2556 */
2557
2558/**
Allan Stephensda827222016-11-09 14:23:58 -06002559 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002560 *
Allan Stephensda827222016-11-09 14:23:58 -06002561 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002563 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2564 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002565 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002566 *
Allan Stephensda827222016-11-09 14:23:58 -06002567 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002569 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002570 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002572 * @param name Name of the memory slab.
2573 * @param slab_block_size Size of each memory block (in bytes).
2574 * @param slab_num_blocks Number memory blocks.
2575 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002576 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002577#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2578 char __noinit __aligned(slab_align) \
2579 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2580 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002581 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002582 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2583 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002585/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002586 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002589 *
Allan Stephensda827222016-11-09 14:23:58 -06002590 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2591 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2592 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2593 * To ensure that each memory block is similarly aligned to this boundary,
2594 * @a slab_block_size must also be a multiple of N.
2595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @param slab Address of the memory slab.
2597 * @param buffer Pointer to buffer used for the memory blocks.
2598 * @param block_size Size of each memory block (in bytes).
2599 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002600 *
2601 * @return N/A
2602 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002603extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002604 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002605
2606/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002607 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002609 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * @param slab Address of the memory slab.
2612 * @param mem Pointer to block address area.
2613 * @param timeout Maximum time to wait for operation to complete
2614 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2615 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002616 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002617 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002619 * @retval -ENOMEM Returned without waiting.
2620 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002621 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002622extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2623 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002624
2625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * This routine releases a previously allocated memory block back to its
2629 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * @param slab Address of the memory slab.
2632 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002633 *
2634 * @return N/A
2635 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002636extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * This routine gets the number of memory blocks that are currently
2642 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002647 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002648static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002650 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651}
2652
Peter Mitsisc001aa82016-10-13 13:53:37 -04002653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * This routine gets the number of memory blocks that are currently
2657 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002659 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002662 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002663static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002664{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002665 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002666}
2667
Allan Stephensc98da842016-11-11 15:45:03 -05002668/**
2669 * @} end defgroup mem_slab_apis
2670 */
2671
2672/**
2673 * @cond INTERNAL_HIDDEN
2674 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002676/*
2677 * Memory pool requires a buffer and two arrays of structures for the
2678 * memory block accounting:
2679 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2680 * status of four blocks of memory.
2681 */
2682struct k_mem_pool_quad_block {
2683 char *mem_blocks; /* pointer to the first of four memory blocks */
2684 uint32_t mem_status; /* four bits. If bit is set, memory block is
2685 allocated */
2686};
2687/*
2688 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2689 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2690 * block size is 4 times less than the previous one and thus requires 4 times
2691 * bigger array of k_mem_pool_quad_block structures to keep track of the
2692 * memory blocks.
2693 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002695/*
2696 * The array of k_mem_pool_block_set keeps the information of each array of
2697 * k_mem_pool_quad_block structures
2698 */
2699struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002700 size_t block_size; /* memory block size */
2701 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002702 struct k_mem_pool_quad_block *quad_block;
2703 int count;
2704};
2705
2706/* Memory pool descriptor */
2707struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002708 size_t max_block_size;
2709 size_t min_block_size;
2710 uint32_t nr_of_maxblocks;
2711 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002712 struct k_mem_pool_block_set *block_set;
2713 char *bufblock;
2714 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002715 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716};
2717
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002718#ifdef CONFIG_ARM
2719#define _SECTION_TYPE_SIGN "%"
2720#else
2721#define _SECTION_TYPE_SIGN "@"
2722#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002724/*
2725 * Static memory pool initialization
2726 */
Allan Stephensc98da842016-11-11 15:45:03 -05002727
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002728/*
2729 * Use .altmacro to be able to recalculate values and pass them as string
2730 * arguments when calling assembler macros resursively
2731 */
2732__asm__(".altmacro\n\t");
2733
2734/*
2735 * Recursively calls a macro
2736 * The followig global symbols need to be initialized:
2737 * __memory_pool_max_block_size - maximal size of the memory block
2738 * __memory_pool_min_block_size - minimal size of the memory block
2739 * Notes:
2740 * Global symbols are used due the fact that assembler macro allows only
2741 * one argument be passed with the % conversion
2742 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2743 * is used instead of / 4.
2744 * n_max argument needs to go first in the invoked macro, as some
2745 * assemblers concatenate \name and %(\n_max * 4) arguments
2746 * if \name goes first
2747 */
2748__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2749 ".ifge __memory_pool_max_block_size >> 2 -"
2750 " __memory_pool_min_block_size\n\t\t"
2751 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2752 "\\macro_name %(\\n_max * 4) \\name\n\t"
2753 ".endif\n\t"
2754 ".endm\n");
2755
2756/*
2757 * Build quad blocks
2758 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2759 * structures and recursively calls itself for the next array, 4 times
2760 * larger.
2761 * The followig global symbols need to be initialized:
2762 * __memory_pool_max_block_size - maximal size of the memory block
2763 * __memory_pool_min_block_size - minimal size of the memory block
2764 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2765 */
2766__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002767 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002768 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2769 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2770 ".if \\n_max % 4\n\t\t"
2771 ".skip __memory_pool_quad_block_size\n\t"
2772 ".endif\n\t"
2773 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2774 ".endm\n");
2775
2776/*
2777 * Build block sets and initialize them
2778 * Macro initializes the k_mem_pool_block_set structure and
2779 * recursively calls itself for the next one.
2780 * The followig global symbols need to be initialized:
2781 * __memory_pool_max_block_size - maximal size of the memory block
2782 * __memory_pool_min_block_size - minimal size of the memory block
2783 * __memory_pool_block_set_count, the number of the elements in the
2784 * block set array must be set to 0. Macro calculates it's real
2785 * value.
2786 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2787 * structures, _build_quad_blocks must be called prior it.
2788 */
2789__asm__(".macro _build_block_set n_max, name\n\t"
2790 ".int __memory_pool_max_block_size\n\t" /* block_size */
2791 ".if \\n_max % 4\n\t\t"
2792 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2793 ".else\n\t\t"
2794 ".int \\n_max >> 2\n\t"
2795 ".endif\n\t"
2796 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2797 ".int 0\n\t" /* count */
2798 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2799 "__do_recurse _build_block_set \\name \\n_max\n\t"
2800 ".endm\n");
2801
2802/*
2803 * Build a memory pool structure and initialize it
2804 * Macro uses __memory_pool_block_set_count global symbol,
2805 * block set addresses and buffer address, it may be called only after
2806 * _build_block_set
2807 */
2808__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002809 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002810 _SECTION_TYPE_SIGN "progbits\n\t"
2811 ".globl \\name\n\t"
2812 "\\name:\n\t"
2813 ".int \\max_size\n\t" /* max_block_size */
2814 ".int \\min_size\n\t" /* min_block_size */
2815 ".int \\n_max\n\t" /* nr_of_maxblocks */
2816 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2817 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2818 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2819 ".int 0\n\t" /* wait_q->head */
2820 ".int 0\n\t" /* wait_q->next */
2821 ".popsection\n\t"
2822 ".endm\n");
2823
2824#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2825 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2826 _SECTION_TYPE_SIGN "progbits\n\t"); \
2827 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2828 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2829 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2830 STRINGIFY(name) "\n\t"); \
2831 __asm__(".popsection\n\t")
2832
2833#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2834 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2835 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2836 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2837 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002838 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002839 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2840 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2841 STRINGIFY(name) "\n\t"); \
2842 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2843 __asm__(".int __memory_pool_block_set_count\n\t"); \
2844 __asm__(".popsection\n\t"); \
2845 extern uint32_t _mem_pool_block_set_count_##name; \
2846 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2847
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002848#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2849 char __noinit __aligned(align) \
2850 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002851
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002852/*
2853 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2854 * to __memory_pool_quad_block_size absolute symbol.
2855 * This function does not get called, but compiler calculates the value and
2856 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2857 */
2858static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2859{
2860 __asm__(".globl __memory_pool_quad_block_size\n\t"
2861#ifdef CONFIG_NIOS2
2862 "__memory_pool_quad_block_size = %0\n\t"
2863#else
2864 "__memory_pool_quad_block_size = %c0\n\t"
2865#endif
2866 :
2867 : "n"(sizeof(struct k_mem_pool_quad_block)));
2868}
2869
2870/**
Allan Stephensc98da842016-11-11 15:45:03 -05002871 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002872 */
2873
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002874/**
Allan Stephensc98da842016-11-11 15:45:03 -05002875 * @addtogroup mem_pool_apis
2876 * @{
2877 */
2878
2879/**
2880 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2883 * long. The memory pool allows blocks to be repeatedly partitioned into
2884 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2885 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002886 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002888 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889 * If the pool is to be accessed outside the module where it is defined, it
2890 * can be declared via
2891 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002892 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * @param name Name of the memory pool.
2895 * @param min_size Size of the smallest blocks in the pool (in bytes).
2896 * @param max_size Size of the largest blocks in the pool (in bytes).
2897 * @param n_max Number of maximum sized blocks in the pool.
2898 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002899 */
2900#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002901 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2902 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002903 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002904 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2905 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2906 extern struct k_mem_pool name
2907
Peter Mitsis937042c2016-10-13 13:18:26 -04002908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @param pool Address of the memory pool.
2914 * @param block Pointer to block descriptor for the allocated memory.
2915 * @param size Amount of memory to allocate (in bytes).
2916 * @param timeout Maximum time to wait for operation to complete
2917 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2918 * or K_FOREVER to wait as long as necessary.
2919 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002920 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002922 * @retval -ENOMEM Returned without waiting.
2923 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002924 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002925extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002926 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002927
2928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * This routine releases a previously allocated memory block back to its
2932 * memory pool.
2933 *
2934 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002935 *
2936 * @return N/A
2937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002939
2940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * This routine instructs a memory pool to concatenate unused memory blocks
2944 * into larger blocks wherever possible. Manually defragmenting the memory
2945 * pool may speed up future allocations of memory blocks by eliminating the
2946 * need for the memory pool to perform an automatic partial defragmentation.
2947 *
2948 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002949 *
2950 * @return N/A
2951 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002952extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002953
2954/**
Allan Stephensc98da842016-11-11 15:45:03 -05002955 * @} end addtogroup mem_pool_apis
2956 */
2957
2958/**
2959 * @defgroup heap_apis Heap Memory Pool APIs
2960 * @ingroup kernel_apis
2961 * @{
2962 */
2963
2964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002968 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002973 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002974extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002975
2976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002978 *
2979 * This routine provides traditional free() semantics. The memory being
2980 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002981 *
Anas Nashif345fdd52016-12-20 08:36:04 -05002982 * If @a ptr is NULL, no operation is performed.
2983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002985 *
2986 * @return N/A
2987 */
2988extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989
Allan Stephensc98da842016-11-11 15:45:03 -05002990/**
2991 * @} end defgroup heap_apis
2992 */
2993
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05002994/**
2995 * @brief Make the CPU idle.
2996 *
2997 * This function makes the CPU idle until an event wakes it up.
2998 *
2999 * In a regular system, the idle thread should be the only thread responsible
3000 * for making the CPU idle and triggering any type of power management.
3001 * However, in some more constrained systems, such as a single-threaded system,
3002 * the only thread would be responsible for this if needed.
3003 *
3004 * @return N/A
3005 */
3006extern void k_cpu_idle(void);
3007
3008/**
3009 * @brief Make the CPU idle in an atomic fashion.
3010 *
3011 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3012 * must be done atomically before making the CPU idle.
3013 *
3014 * @param key Interrupt locking key obtained from irq_lock().
3015 *
3016 * @return N/A
3017 */
3018extern void k_cpu_atomic_idle(unsigned int key);
3019
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020/*
3021 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
3022 * hook into the device subsystem, which itself uses nanokernel semaphores,
3023 * and thus currently requires the definition of nano_sem.
3024 */
3025#include <legacy.h>
3026#include <arch/cpu.h>
3027
3028/*
3029 * private APIs that are utilized by one or more public APIs
3030 */
3031
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003032#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003034#else
3035#define _init_static_threads() do { } while ((0))
3036#endif
3037
3038extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003039extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040
3041#ifdef __cplusplus
3042}
3043#endif
3044
Andrew Boiee004dec2016-11-07 09:01:19 -08003045#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3046/*
3047 * Define new and delete operators.
3048 * At this moment, the operators do nothing since objects are supposed
3049 * to be statically allocated.
3050 */
3051inline void operator delete(void *ptr)
3052{
3053 (void)ptr;
3054}
3055
3056inline void operator delete[](void *ptr)
3057{
3058 (void)ptr;
3059}
3060
3061inline void *operator new(size_t size)
3062{
3063 (void)size;
3064 return NULL;
3065}
3066
3067inline void *operator new[](size_t size)
3068{
3069 (void)size;
3070 return NULL;
3071}
3072
3073/* Placement versions of operator new and delete */
3074inline void operator delete(void *ptr1, void *ptr2)
3075{
3076 (void)ptr1;
3077 (void)ptr2;
3078}
3079
3080inline void operator delete[](void *ptr1, void *ptr2)
3081{
3082 (void)ptr1;
3083 (void)ptr2;
3084}
3085
3086inline void *operator new(size_t size, void *ptr)
3087{
3088 (void)size;
3089 return ptr;
3090}
3091
3092inline void *operator new[](size_t size, void *ptr)
3093{
3094 (void)size;
3095 return ptr;
3096}
3097
3098#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3099
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100#endif /* _kernel__h_ */