blob: 50972ad57d16aa1768d7698a66b2ad4e0b73c1b2 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
18#include <stdint.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
21#include <sections.h>
22#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Anas Nashifbbb157d2017-01-15 08:46:31 -050035/**
36 * @brief Kernel APIs
37 * @defgroup kernel_apis Kernel APIs
38 * @{
39 * @}
40 */
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
Benjamin Walsh2f280412017-01-14 19:23:46 -050049#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
50#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
51#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
52#elif defined(CONFIG_COOP_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
54#define _NUM_PREEMPT_PRIO (0)
55#elif defined(CONFIG_PREEMPT_ENABLED)
56#define _NUM_COOP_PRIO (0)
57#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
58#else
59#error "invalid configuration"
60#endif
61
62#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_PRIO_PREEMPT(x) (x)
64
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_ANY NULL
66#define K_END NULL
67
Benjamin Walshedb35702017-01-14 18:47:22 -050068#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050070#elif defined(CONFIG_COOP_ENABLED)
71#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
72#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050074#else
75#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#endif
77
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050078#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
80#else
81#define K_LOWEST_THREAD_PRIO -1
82#endif
83
Benjamin Walshfab8d922016-11-08 15:36:36 -050084#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
85
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
87#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
88
89typedef sys_dlist_t _wait_q_t;
90
Anas Nashif2f203c22016-12-18 06:57:45 -050091#ifdef CONFIG_OBJECT_TRACING
92#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
93#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094#else
Anas Nashif2f203c22016-12-18 06:57:45 -050095#define _OBJECT_TRACING_INIT
96#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#endif
98
Benjamin Walshacc68c12017-01-29 18:57:45 -050099#ifdef CONFIG_POLL
100#define _POLL_EVENT_OBJ_INIT \
101 .poll_event = NULL,
102#define _POLL_EVENT struct k_poll_event *poll_event
103#else
104#define _POLL_EVENT_OBJ_INIT
105#define _POLL_EVENT
106#endif
107
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500108#define tcs k_thread
109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
116struct k_fifo;
117struct k_lifo;
118struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400119struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400120struct k_mem_pool;
121struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500122struct k_poll_event;
123struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400125typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127enum execution_context_types {
128 K_ISR = 0,
129 K_COOP_THREAD,
130 K_PREEMPT_THREAD,
131};
132
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400133/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100134 * @defgroup profiling_apis Profiling APIs
135 * @ingroup kernel_apis
136 * @{
137 */
138
139/**
140 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
141 *
142 * This routine calls @ref stack_analyze on the 4 call stacks declared and
143 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
144 *
145 * CONFIG_MAIN_STACK_SIZE
146 * CONFIG_IDLE_STACK_SIZE
147 * CONFIG_ISR_STACK_SIZE
148 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
149 *
150 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
151 * produce output.
152 *
153 * @return N/A
154 */
155extern void k_call_stacks_analyze(void);
156
157/**
158 * @} end defgroup profiling_apis
159 */
160
161/**
Allan Stephensc98da842016-11-11 15:45:03 -0500162 * @defgroup thread_apis Thread APIs
163 * @ingroup kernel_apis
164 * @{
165 */
166
167/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500168 * @typedef k_thread_entry_t
169 * @brief Thread entry point function type.
170 *
171 * A thread's entry point function is invoked when the thread starts executing.
172 * Up to 3 argument values can be passed to the function.
173 *
174 * The thread terminates execution permanently if the entry point function
175 * returns. The thread is responsible for releasing any shared resources
176 * it may own (such as mutexes and dynamically allocated memory), prior to
177 * returning.
178 *
179 * @param p1 First argument.
180 * @param p2 Second argument.
181 * @param p3 Third argument.
182 *
183 * @return N/A
184 */
185typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
186
Benjamin Walshed240f22017-01-22 13:05:08 -0500187#endif /* !_ASMLANGUAGE */
188
189
190/*
191 * Thread user options. May be needed by assembly code. Common part uses low
192 * bits, arch-specific use high bits.
193 */
194
195/* system thread that must not abort */
196#define K_ESSENTIAL (1 << 0)
197
198#if defined(CONFIG_FP_SHARING)
199/* thread uses floating point registers */
200#define K_FP_REGS (1 << 1)
201#endif
202
203#ifdef CONFIG_X86
204/* x86 Bitmask definitions for threads user options */
205
206#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
207/* thread uses SSEx (and also FP) registers */
208#define K_SSE_REGS (1 << 7)
209#endif
210#endif
211
212/* end - thread options */
213
214#if !defined(_ASMLANGUAGE)
215
Allan Stephens5eceb852016-11-16 10:16:30 -0500216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500217 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500221 * The new thread may be scheduled for immediate execution or a delayed start.
222 * If the newly spawned thread does not have a delayed start the kernel
223 * scheduler may preempt the current thread to allow the new thread to
224 * execute.
225 *
226 * Thread options are architecture-specific, and can include K_ESSENTIAL,
227 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
228 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400229 *
230 * @param stack Pointer to the stack space.
231 * @param stack_size Stack size in bytes.
232 * @param entry Thread entry function.
233 * @param p1 1st entry point parameter.
234 * @param p2 2nd entry point parameter.
235 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500236 * @param prio Thread priority.
237 * @param options Thread options.
238 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500240 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400241 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500242extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500243 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400244 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500245 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500248 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400249 *
Allan Stephensc98da842016-11-11 15:45:03 -0500250 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500251 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500253 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400254 *
255 * @return N/A
256 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400257extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400258
259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500260 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400261 *
262 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500263 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400264 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 * @return N/A
266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400267extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400268
269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500270 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500272 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400273 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500274 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400275 *
276 * @return N/A
277 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400278extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400279
280/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500281 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500283 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500285 * If @a thread is not currently sleeping, the routine has no effect.
286 *
287 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400288 *
289 * @return N/A
290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400291extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400292
293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500294 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500296 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400297 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400298extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400299
300/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500301 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500303 * This routine prevents @a thread from executing if it has not yet started
304 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500306 * @param thread ID of thread to cancel.
307 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500308 * @retval 0 Thread spawning cancelled.
309 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400310 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400311extern int k_thread_cancel(k_tid_t thread);
312
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400313/**
Allan Stephensc98da842016-11-11 15:45:03 -0500314 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500316 * This routine permanently stops execution of @a thread. The thread is taken
317 * off all kernel queues it is part of (i.e. the ready queue, the timeout
318 * queue, or a kernel object wait queue). However, any kernel resources the
319 * thread might currently own (such as mutexes or memory blocks) are not
320 * released. It is the responsibility of the caller of this routine to ensure
321 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500323 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400324 *
325 * @return N/A
326 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400327extern void k_thread_abort(k_tid_t thread);
328
Allan Stephensc98da842016-11-11 15:45:03 -0500329/**
330 * @cond INTERNAL_HIDDEN
331 */
332
Benjamin Walshd211a522016-12-06 11:44:01 -0500333/* timeout has timed out and is not on _timeout_q anymore */
334#define _EXPIRED (-2)
335
336/* timeout is not in use */
337#define _INACTIVE (-1)
338
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400339#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400340#define _THREAD_TIMEOUT_INIT(obj) \
341 (obj).nano_timeout = { \
342 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400343 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400344 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500345 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400346 },
347#else
348#define _THREAD_TIMEOUT_INIT(obj)
349#endif
350
351#ifdef CONFIG_ERRNO
352#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
353#else
354#define _THREAD_ERRNO_INIT(obj)
355#endif
356
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400357struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400358 union {
359 char *init_stack;
360 struct k_thread *thread;
361 };
362 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500363 void (*init_entry)(void *, void *, void *);
364 void *init_p1;
365 void *init_p2;
366 void *init_p3;
367 int init_prio;
368 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400369 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500370 void (*init_abort)(void);
371 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400372};
373
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400374#define _THREAD_INITIALIZER(stack, stack_size, \
375 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500376 prio, options, delay, abort, groups) \
377 { \
Mazen NEIFER967cb2e2017-02-02 10:42:18 +0100378 {.init_stack = (stack)}, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500379 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400380 .init_entry = (void (*)(void *, void *, void *))entry, \
381 .init_p1 = (void *)p1, \
382 .init_p2 = (void *)p2, \
383 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500384 .init_prio = (prio), \
385 .init_options = (options), \
386 .init_delay = (delay), \
387 .init_abort = (abort), \
388 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400389 }
390
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400391/**
Allan Stephensc98da842016-11-11 15:45:03 -0500392 * INTERNAL_HIDDEN @endcond
393 */
394
395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500396 * @brief Statically define and initialize a thread.
397 *
398 * The thread may be scheduled for immediate execution or a delayed start.
399 *
400 * Thread options are architecture-specific, and can include K_ESSENTIAL,
401 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
402 * them using "|" (the logical OR operator).
403 *
404 * The ID of the thread can be accessed using:
405 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500406 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500407 *
408 * @param name Name of the thread.
409 * @param stack_size Stack size in bytes.
410 * @param entry Thread entry function.
411 * @param p1 1st entry point parameter.
412 * @param p2 2nd entry point parameter.
413 * @param p3 3rd entry point parameter.
414 * @param prio Thread priority.
415 * @param options Thread options.
416 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400417 *
418 * @internal It has been observed that the x86 compiler by default aligns
419 * these _static_thread_data structures to 32-byte boundaries, thereby
420 * wasting space. To work around this, force a 4-byte alignment.
421 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500422#define K_THREAD_DEFINE(name, stack_size, \
423 entry, p1, p2, p3, \
424 prio, options, delay) \
425 char __noinit __stack _k_thread_obj_##name[stack_size]; \
426 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500427 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500428 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
429 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500430 NULL, 0); \
431 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400432
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500438 * @param thread ID of thread whose priority is needed.
439 *
440 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500442extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400443
444/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500445 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500447 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 *
449 * Rescheduling can occur immediately depending on the priority @a thread is
450 * set to:
451 *
452 * - If its priority is raised above the priority of the caller of this
453 * function, and the caller is preemptible, @a thread will be scheduled in.
454 *
455 * - If the caller operates on itself, it lowers its priority below that of
456 * other threads in the system, and the caller is preemptible, the thread of
457 * highest priority will be scheduled in.
458 *
459 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
460 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
461 * highest priority.
462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 * @param prio New priority.
465 *
466 * @warning Changing the priority of a thread currently involved in mutex
467 * priority inheritance may result in undefined behavior.
468 *
469 * @return N/A
470 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400471extern void k_thread_priority_set(k_tid_t thread, int prio);
472
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500476 * This routine prevents the kernel scheduler from making @a thread the
477 * current thread. All other internal operations on @a thread are still
478 * performed; for example, any timeout it is waiting on keeps ticking,
479 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500481 * If @a thread is already suspended, the routine has no effect.
482 *
483 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400484 *
485 * @return N/A
486 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400487extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488
489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500490 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500492 * This routine allows the kernel scheduler to make @a thread the current
493 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500495 * If @a thread is not currently suspended, the routine has no effect.
496 *
497 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
499 * @return N/A
500 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400501extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400502
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500504 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500506 * This routine specifies how the scheduler will perform time slicing of
507 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500509 * To enable time slicing, @a slice must be non-zero. The scheduler
510 * ensures that no thread runs for more than the specified time limit
511 * before other threads of that priority are given a chance to execute.
512 * Any thread whose priority is higher than @a prio is exempted, and may
513 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500515 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400516 * execute. Once the scheduler selects a thread for execution, there is no
517 * minimum guaranteed time the thread will execute before threads of greater or
518 * equal priority are scheduled.
519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500520 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400521 * for execution, this routine has no effect; the thread is immediately
522 * rescheduled after the slice period expires.
523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500524 * To disable timeslicing, set both @a slice and @a prio to zero.
525 *
526 * @param slice Maximum time slice length (in milliseconds).
527 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
529 * @return N/A
530 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400531extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400532
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400533/**
Allan Stephensc98da842016-11-11 15:45:03 -0500534 * @} end defgroup thread_apis
535 */
536
537/**
538 * @addtogroup isr_apis
539 * @{
540 */
541
542/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500543 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400544 *
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * This routine allows the caller to customize its actions, depending on
546 * whether it is a thread or an ISR.
547 *
548 * @note Can be called by ISRs.
549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500550 * @return 0 if invoked by a thread.
551 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400552 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500553extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400554
Benjamin Walsh445830d2016-11-10 15:54:27 -0500555/**
556 * @brief Determine if code is running in a preemptible thread.
557 *
Allan Stephensc98da842016-11-11 15:45:03 -0500558 * This routine allows the caller to customize its actions, depending on
559 * whether it can be preempted by another thread. The routine returns a 'true'
560 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500561 *
Allan Stephensc98da842016-11-11 15:45:03 -0500562 * - The code is running in a thread, not at ISR.
563 * - The thread's priority is in the preemptible range.
564 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500565 *
Allan Stephensc98da842016-11-11 15:45:03 -0500566 * @note Can be called by ISRs.
567 *
568 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500569 * @return Non-zero if invoked by a preemptible thread.
570 */
571extern int k_is_preempt_thread(void);
572
Allan Stephensc98da842016-11-11 15:45:03 -0500573/**
574 * @} end addtogroup isr_apis
575 */
576
577/**
578 * @addtogroup thread_apis
579 * @{
580 */
581
582/**
583 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500584 *
Allan Stephensc98da842016-11-11 15:45:03 -0500585 * This routine prevents the current thread from being preempted by another
586 * thread by instructing the scheduler to treat it as a cooperative thread.
587 * If the thread subsequently performs an operation that makes it unready,
588 * it will be context switched out in the normal manner. When the thread
589 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500590 *
Allan Stephensc98da842016-11-11 15:45:03 -0500591 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500592 *
Allan Stephensc98da842016-11-11 15:45:03 -0500593 * @note k_sched_lock() and k_sched_unlock() should normally be used
594 * when the operation being performed can be safely interrupted by ISRs.
595 * However, if the amount of processing involved is very small, better
596 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500597 *
598 * @return N/A
599 */
600extern void k_sched_lock(void);
601
Allan Stephensc98da842016-11-11 15:45:03 -0500602/**
603 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500604 *
Allan Stephensc98da842016-11-11 15:45:03 -0500605 * This routine reverses the effect of a previous call to k_sched_lock().
606 * A thread must call the routine once for each time it called k_sched_lock()
607 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500608 *
609 * @return N/A
610 */
611extern void k_sched_unlock(void);
612
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500614 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500616 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500618 * Custom data is not used by the kernel itself, and is freely available
619 * for a thread to use as it sees fit. It can be used as a framework
620 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 *
624 * @return N/A
625 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400626extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400627
628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500631 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500633 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400635extern void *k_thread_custom_data_get(void);
636
637/**
Allan Stephensc98da842016-11-11 15:45:03 -0500638 * @} end addtogroup thread_apis
639 */
640
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400641#include <sys_clock.h>
642
Allan Stephensc2f15a42016-11-17 12:24:22 -0500643/**
644 * @addtogroup clock_apis
645 * @{
646 */
647
648/**
649 * @brief Generate null timeout delay.
650 *
651 * This macro generates a timeout delay that that instructs a kernel API
652 * not to wait if the requested operation cannot be performed immediately.
653 *
654 * @return Timeout delay value.
655 */
656#define K_NO_WAIT 0
657
658/**
659 * @brief Generate timeout delay from milliseconds.
660 *
661 * This macro generates a timeout delay that that instructs a kernel API
662 * to wait up to @a ms milliseconds to perform the requested operation.
663 *
664 * @param ms Duration in milliseconds.
665 *
666 * @return Timeout delay value.
667 */
Johan Hedberg14471692016-11-13 10:52:15 +0200668#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500669
670/**
671 * @brief Generate timeout delay from seconds.
672 *
673 * This macro generates a timeout delay that that instructs a kernel API
674 * to wait up to @a s seconds to perform the requested operation.
675 *
676 * @param s Duration in seconds.
677 *
678 * @return Timeout delay value.
679 */
Johan Hedberg14471692016-11-13 10:52:15 +0200680#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500681
682/**
683 * @brief Generate timeout delay from minutes.
684 *
685 * This macro generates a timeout delay that that instructs a kernel API
686 * to wait up to @a m minutes to perform the requested operation.
687 *
688 * @param m Duration in minutes.
689 *
690 * @return Timeout delay value.
691 */
Johan Hedberg14471692016-11-13 10:52:15 +0200692#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500693
694/**
695 * @brief Generate timeout delay from hours.
696 *
697 * This macro generates a timeout delay that that instructs a kernel API
698 * to wait up to @a h hours to perform the requested operation.
699 *
700 * @param h Duration in hours.
701 *
702 * @return Timeout delay value.
703 */
Johan Hedberg14471692016-11-13 10:52:15 +0200704#define K_HOURS(h) K_MINUTES((h) * 60)
705
Allan Stephensc98da842016-11-11 15:45:03 -0500706/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500707 * @brief Generate infinite timeout delay.
708 *
709 * This macro generates a timeout delay that that instructs a kernel API
710 * to wait as long as necessary to perform the requested operation.
711 *
712 * @return Timeout delay value.
713 */
714#define K_FOREVER (-1)
715
716/**
717 * @} end addtogroup clock_apis
718 */
719
720/**
Allan Stephensc98da842016-11-11 15:45:03 -0500721 * @cond INTERNAL_HIDDEN
722 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400723
Benjamin Walsh62092182016-12-20 14:39:08 -0500724/* kernel clocks */
725
726#if (sys_clock_ticks_per_sec == 1000) || \
727 (sys_clock_ticks_per_sec == 500) || \
728 (sys_clock_ticks_per_sec == 250) || \
729 (sys_clock_ticks_per_sec == 125) || \
730 (sys_clock_ticks_per_sec == 100) || \
731 (sys_clock_ticks_per_sec == 50) || \
732 (sys_clock_ticks_per_sec == 25) || \
733 (sys_clock_ticks_per_sec == 20) || \
734 (sys_clock_ticks_per_sec == 10) || \
735 (sys_clock_ticks_per_sec == 1)
736
737 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
738#else
739 /* yields horrible 64-bit math on many architectures: try to avoid */
740 #define _NON_OPTIMIZED_TICKS_PER_SEC
741#endif
742
743#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
744extern int32_t _ms_to_ticks(int32_t ms);
745#else
746static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
747{
748 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
749}
750#endif
751
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500752/* added tick needed to account for tick in progress */
753#define _TICK_ALIGN 1
754
Benjamin Walsh62092182016-12-20 14:39:08 -0500755static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400756{
Benjamin Walsh62092182016-12-20 14:39:08 -0500757#ifdef CONFIG_SYS_CLOCK_EXISTS
758
759#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400760 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400761#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500762 return (uint64_t)ticks * _ms_per_tick;
763#endif
764
765#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400766 __ASSERT(ticks == 0, "");
767 return 0;
768#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400769}
770
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400771/* timeouts */
772
773struct _timeout;
774typedef void (*_timeout_func_t)(struct _timeout *t);
775
776struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500777 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400778 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400779 sys_dlist_t *wait_q;
780 int32_t delta_ticks_from_prev;
781 _timeout_func_t func;
782};
783
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200784extern int32_t _timeout_remaining_get(struct _timeout *timeout);
785
Allan Stephensc98da842016-11-11 15:45:03 -0500786/**
787 * INTERNAL_HIDDEN @endcond
788 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500789
Allan Stephensc98da842016-11-11 15:45:03 -0500790/**
791 * @cond INTERNAL_HIDDEN
792 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400793
794struct k_timer {
795 /*
796 * _timeout structure must be first here if we want to use
797 * dynamic timer allocation. timeout.node is used in the double-linked
798 * list of free timers
799 */
800 struct _timeout timeout;
801
Allan Stephens45bfa372016-10-12 12:39:42 -0500802 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400803 _wait_q_t wait_q;
804
805 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500806 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400807
808 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500809 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400810
811 /* timer period */
812 int32_t period;
813
Allan Stephens45bfa372016-10-12 12:39:42 -0500814 /* timer status */
815 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400816
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500817 /* user-specific data, also used to support legacy features */
818 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400819
Anas Nashif2f203c22016-12-18 06:57:45 -0500820 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400821};
822
Allan Stephens1342adb2016-11-03 13:54:53 -0500823#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400824 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500825 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500826 .timeout.wait_q = NULL, \
827 .timeout.thread = NULL, \
828 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400829 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500830 .expiry_fn = expiry, \
831 .stop_fn = stop, \
832 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500833 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500834 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400835 }
836
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837/**
Allan Stephensc98da842016-11-11 15:45:03 -0500838 * INTERNAL_HIDDEN @endcond
839 */
840
841/**
842 * @defgroup timer_apis Timer APIs
843 * @ingroup kernel_apis
844 * @{
845 */
846
847/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500848 * @typedef k_timer_expiry_t
849 * @brief Timer expiry function type.
850 *
851 * A timer's expiry function is executed by the system clock interrupt handler
852 * each time the timer expires. The expiry function is optional, and is only
853 * invoked if the timer has been initialized with one.
854 *
855 * @param timer Address of timer.
856 *
857 * @return N/A
858 */
859typedef void (*k_timer_expiry_t)(struct k_timer *timer);
860
861/**
862 * @typedef k_timer_stop_t
863 * @brief Timer stop function type.
864 *
865 * A timer's stop function is executed if the timer is stopped prematurely.
866 * The function runs in the context of the thread that stops the timer.
867 * The stop function is optional, and is only invoked if the timer has been
868 * initialized with one.
869 *
870 * @param timer Address of timer.
871 *
872 * @return N/A
873 */
874typedef void (*k_timer_stop_t)(struct k_timer *timer);
875
876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500881 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
883 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * @param expiry_fn Function to invoke each time the timer expires.
885 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500887#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500888 struct k_timer name \
889 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500890 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891
Allan Stephens45bfa372016-10-12 12:39:42 -0500892/**
893 * @brief Initialize a timer.
894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500896 *
897 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500898 * @param expiry_fn Function to invoke each time the timer expires.
899 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500900 *
901 * @return N/A
902 */
903extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500904 k_timer_expiry_t expiry_fn,
905 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700906
Allan Stephens45bfa372016-10-12 12:39:42 -0500907/**
908 * @brief Start a timer.
909 *
910 * This routine starts a timer, and resets its status to zero. The timer
911 * begins counting down using the specified duration and period values.
912 *
913 * Attempting to start a timer that is already running is permitted.
914 * The timer's status is reset to zero and the timer begins counting down
915 * using the new duration and period values.
916 *
917 * @param timer Address of timer.
918 * @param duration Initial timer duration (in milliseconds).
919 * @param period Timer period (in milliseconds).
920 *
921 * @return N/A
922 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500924 int32_t duration, int32_t period);
925
926/**
927 * @brief Stop a timer.
928 *
929 * This routine stops a running timer prematurely. The timer's stop function,
930 * if one exists, is invoked by the caller.
931 *
932 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500934 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -0500935 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
936 * if @a k_timer_stop is to be called from ISRs.
937 *
Allan Stephens45bfa372016-10-12 12:39:42 -0500938 * @param timer Address of timer.
939 *
940 * @return N/A
941 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400942extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500943
944/**
945 * @brief Read timer status.
946 *
947 * This routine reads the timer's status, which indicates the number of times
948 * it has expired since its status was last read.
949 *
950 * Calling this routine resets the timer's status to zero.
951 *
952 * @param timer Address of timer.
953 *
954 * @return Timer status.
955 */
956extern uint32_t k_timer_status_get(struct k_timer *timer);
957
958/**
959 * @brief Synchronize thread to timer expiration.
960 *
961 * This routine blocks the calling thread until the timer's status is non-zero
962 * (indicating that it has expired at least once since it was last examined)
963 * or the timer is stopped. If the timer status is already non-zero,
964 * or the timer is already stopped, the caller continues without waiting.
965 *
966 * Calling this routine resets the timer's status to zero.
967 *
968 * This routine must not be used by interrupt handlers, since they are not
969 * allowed to block.
970 *
971 * @param timer Address of timer.
972 *
973 * @return Timer status.
974 */
975extern uint32_t k_timer_status_sync(struct k_timer *timer);
976
977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500978 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500979 *
980 * This routine computes the (approximate) time remaining before a running
981 * timer next expires. If the timer is not running, it returns zero.
982 *
983 * @param timer Address of timer.
984 *
985 * @return Remaining time (in milliseconds).
986 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200987static inline int32_t k_timer_remaining_get(struct k_timer *timer)
988{
989 return _timeout_remaining_get(&timer->timeout);
990}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400991
Allan Stephensc98da842016-11-11 15:45:03 -0500992/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500993 * @brief Associate user-specific data with a timer.
994 *
995 * This routine records the @a user_data with the @a timer, to be retrieved
996 * later.
997 *
998 * It can be used e.g. in a timer handler shared across multiple subsystems to
999 * retrieve data specific to the subsystem this timer is associated with.
1000 *
1001 * @param timer Address of timer.
1002 * @param user_data User data to associate with the timer.
1003 *
1004 * @return N/A
1005 */
1006static inline void k_timer_user_data_set(struct k_timer *timer,
1007 void *user_data)
1008{
1009 timer->user_data = user_data;
1010}
1011
1012/**
1013 * @brief Retrieve the user-specific data from a timer.
1014 *
1015 * @param timer Address of timer.
1016 *
1017 * @return The user data.
1018 */
1019static inline void *k_timer_user_data_get(struct k_timer *timer)
1020{
1021 return timer->user_data;
1022}
1023
1024/**
Allan Stephensc98da842016-11-11 15:45:03 -05001025 * @} end defgroup timer_apis
1026 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001027
Allan Stephensc98da842016-11-11 15:45:03 -05001028/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001029 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001030 * @{
1031 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001032
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001036 * This routine returns the elapsed time since the system booted,
1037 * in milliseconds.
1038 *
1039 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001040 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001041extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001042
1043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * This routine returns the lower 32-bits of the elapsed time since the system
1047 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * This routine can be more efficient than k_uptime_get(), as it reduces the
1050 * need for interrupt locking and 64-bit math. However, the 32-bit result
1051 * cannot hold a system uptime time larger than approximately 50 days, so the
1052 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001055 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001056extern uint32_t k_uptime_get_32(void);
1057
1058/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001059 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * This routine computes the elapsed time between the current system uptime
1062 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001064 * @param reftime Pointer to a reference time, which is updated to the current
1065 * uptime upon return.
1066 *
1067 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001068 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001070
1071/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001072 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * This routine computes the elapsed time between the current system uptime
1075 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001077 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1078 * need for interrupt locking and 64-bit math. However, the 32-bit result
1079 * cannot hold an elapsed time larger than approximately 50 days, so the
1080 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * @param reftime Pointer to a reference time, which is updated to the current
1083 * uptime upon return.
1084 *
1085 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001086 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001087extern uint32_t k_uptime_delta_32(int64_t *reftime);
1088
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * This routine returns the current time, as measured by the system's hardware
1093 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001095 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097extern uint32_t k_cycle_get_32(void);
1098
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001100 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001101 */
1102
Allan Stephensc98da842016-11-11 15:45:03 -05001103/**
1104 * @cond INTERNAL_HIDDEN
1105 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106
1107struct k_fifo {
1108 _wait_q_t wait_q;
1109 sys_slist_t data_q;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001110 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001111
Anas Nashif2f203c22016-12-18 06:57:45 -05001112 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001113};
1114
Allan Stephensc98da842016-11-11 15:45:03 -05001115#define K_FIFO_INITIALIZER(obj) \
1116 { \
1117 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1118 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05001119 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05001120 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001121 }
1122
1123/**
1124 * INTERNAL_HIDDEN @endcond
1125 */
1126
1127/**
1128 * @defgroup fifo_apis Fifo APIs
1129 * @ingroup kernel_apis
1130 * @{
1131 */
1132
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
1140 * @return N/A
1141 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001142extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143
1144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001147 * This routine adds a data item to @a fifo. A fifo data item must be
1148 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1149 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * @note Can be called by ISRs.
1152 *
1153 * @param fifo Address of the fifo.
1154 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 *
1156 * @return N/A
1157 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001158extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159
1160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001161 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * This routine adds a list of data items to @a fifo in one operation.
1164 * The data items must be in a singly-linked list, with the first 32 bits
1165 * each data item pointing to the next data item; the list must be
1166 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * @param fifo Address of the fifo.
1171 * @param head Pointer to first node in singly-linked list.
1172 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
1174 * @return N/A
1175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177
1178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * This routine adds a list of data items to @a fifo in one operation.
1182 * The data items must be in a singly-linked list implemented using a
1183 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 * and must be re-initialized via sys_slist_init().
1185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @note Can be called by ISRs.
1187 *
1188 * @param fifo Address of the fifo.
1189 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001190 *
1191 * @return N/A
1192 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194
1195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001198 * This routine removes a data item from @a fifo in a "first in, first out"
1199 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001201 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1202 *
1203 * @param fifo Address of the fifo.
1204 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 * or one of the special values K_NO_WAIT and K_FOREVER.
1206 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001207 * @return Address of the data item if successful; NULL if returned
1208 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001210extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1211
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001212/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001213 * @brief Query a fifo to see if it has data available.
1214 *
1215 * Note that the data might be already gone by the time this function returns
1216 * if other threads is also trying to read from the fifo.
1217 *
1218 * @note Can be called by ISRs.
1219 *
1220 * @param fifo Address of the fifo.
1221 *
1222 * @return Non-zero if the fifo is empty.
1223 * @return 0 if data is available.
1224 */
1225static inline int k_fifo_is_empty(struct k_fifo *fifo)
1226{
1227 return (int)sys_slist_is_empty(&fifo->data_q);
1228}
1229
1230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001235 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001239#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001240 struct k_fifo name \
1241 __in_section(_k_fifo, static, name) = \
1242 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001243
Allan Stephensc98da842016-11-11 15:45:03 -05001244/**
1245 * @} end defgroup fifo_apis
1246 */
1247
1248/**
1249 * @cond INTERNAL_HIDDEN
1250 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001251
1252struct k_lifo {
1253 _wait_q_t wait_q;
1254 void *list;
1255
Anas Nashif2f203c22016-12-18 06:57:45 -05001256 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001257};
1258
Allan Stephensc98da842016-11-11 15:45:03 -05001259#define K_LIFO_INITIALIZER(obj) \
1260 { \
1261 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1262 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001263 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001264 }
1265
1266/**
1267 * INTERNAL_HIDDEN @endcond
1268 */
1269
1270/**
1271 * @defgroup lifo_apis Lifo APIs
1272 * @ingroup kernel_apis
1273 * @{
1274 */
1275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001282 *
1283 * @return N/A
1284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286
1287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine adds a data item to @a lifo. A lifo data item must be
1291 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1292 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * @note Can be called by ISRs.
1295 *
1296 * @param lifo Address of the lifo.
1297 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001298 *
1299 * @return N/A
1300 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302
1303/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001304 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001306 * This routine removes a data item from @a lifo in a "last in, first out"
1307 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001309 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1310 *
1311 * @param lifo Address of the lifo.
1312 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001313 * or one of the special values K_NO_WAIT and K_FOREVER.
1314 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001315 * @return Address of the data item if successful; NULL if returned
1316 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001317 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001318extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1319
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001320/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001324 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001325 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001327 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001328 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001329#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001330 struct k_lifo name \
1331 __in_section(_k_lifo, static, name) = \
1332 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333
Allan Stephensc98da842016-11-11 15:45:03 -05001334/**
1335 * @} end defgroup lifo_apis
1336 */
1337
1338/**
1339 * @cond INTERNAL_HIDDEN
1340 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001341
1342struct k_stack {
1343 _wait_q_t wait_q;
1344 uint32_t *base, *next, *top;
1345
Anas Nashif2f203c22016-12-18 06:57:45 -05001346 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001347};
1348
Allan Stephensc98da842016-11-11 15:45:03 -05001349#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1350 { \
1351 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1352 .base = stack_buffer, \
1353 .next = stack_buffer, \
1354 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001355 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001356 }
1357
1358/**
1359 * INTERNAL_HIDDEN @endcond
1360 */
1361
1362/**
1363 * @defgroup stack_apis Stack APIs
1364 * @ingroup kernel_apis
1365 * @{
1366 */
1367
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001368/**
1369 * @brief Initialize a stack.
1370 *
1371 * This routine initializes a stack object, prior to its first use.
1372 *
1373 * @param stack Address of the stack.
1374 * @param buffer Address of array used to hold stacked values.
1375 * @param num_entries Maximum number of values that can be stacked.
1376 *
1377 * @return N/A
1378 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001379extern void k_stack_init(struct k_stack *stack,
1380 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381
1382/**
1383 * @brief Push an element onto a stack.
1384 *
1385 * This routine adds a 32-bit value @a data to @a stack.
1386 *
1387 * @note Can be called by ISRs.
1388 *
1389 * @param stack Address of the stack.
1390 * @param data Value to push onto the stack.
1391 *
1392 * @return N/A
1393 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001395
1396/**
1397 * @brief Pop an element from a stack.
1398 *
1399 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1400 * manner and stores the value in @a data.
1401 *
1402 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1403 *
1404 * @param stack Address of the stack.
1405 * @param data Address of area to hold the value popped from the stack.
1406 * @param timeout Waiting period to obtain a value (in milliseconds),
1407 * or one of the special values K_NO_WAIT and K_FOREVER.
1408 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001409 * @retval 0 Element popped from stack.
1410 * @retval -EBUSY Returned without waiting.
1411 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1414
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001416 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001418 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001419 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001420 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001422 * @param name Name of the stack.
1423 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001424 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001425#define K_STACK_DEFINE(name, stack_num_entries) \
1426 uint32_t __noinit \
1427 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001428 struct k_stack name \
1429 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001430 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1431 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001432
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433/**
Allan Stephensc98da842016-11-11 15:45:03 -05001434 * @} end defgroup stack_apis
1435 */
1436
Allan Stephens6bba9b02016-11-16 14:56:54 -05001437struct k_work;
1438
Allan Stephensc98da842016-11-11 15:45:03 -05001439/**
1440 * @defgroup workqueue_apis Workqueue Thread APIs
1441 * @ingroup kernel_apis
1442 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001443 */
1444
Allan Stephens6bba9b02016-11-16 14:56:54 -05001445/**
1446 * @typedef k_work_handler_t
1447 * @brief Work item handler function type.
1448 *
1449 * A work item's handler function is executed by a workqueue's thread
1450 * when the work item is processed by the workqueue.
1451 *
1452 * @param work Address of the work item.
1453 *
1454 * @return N/A
1455 */
1456typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001457
1458/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001459 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001461
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462struct k_work_q {
1463 struct k_fifo fifo;
1464};
1465
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001466enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001467 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001468};
1469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001470struct k_work {
1471 void *_reserved; /* Used by k_fifo implementation. */
1472 k_work_handler_t handler;
1473 atomic_t flags[1];
1474};
1475
Allan Stephens6bba9b02016-11-16 14:56:54 -05001476struct k_delayed_work {
1477 struct k_work work;
1478 struct _timeout timeout;
1479 struct k_work_q *work_q;
1480};
1481
1482extern struct k_work_q k_sys_work_q;
1483
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001484/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001485 * INTERNAL_HIDDEN @endcond
1486 */
1487
1488/**
1489 * @brief Initialize a statically-defined work item.
1490 *
1491 * This macro can be used to initialize a statically-defined workqueue work
1492 * item, prior to its first use. For example,
1493 *
1494 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1495 *
1496 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001497 */
1498#define K_WORK_INITIALIZER(work_handler) \
1499 { \
1500 ._reserved = NULL, \
1501 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001502 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503 }
1504
1505/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001506 * @brief Initialize a work item.
1507 *
1508 * This routine initializes a workqueue work item, prior to its first use.
1509 *
1510 * @param work Address of work item.
1511 * @param handler Function to invoke each time work item is processed.
1512 *
1513 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001514 */
1515static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1516{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001517 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001518 work->handler = handler;
1519}
1520
1521/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001522 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001523 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001524 * This routine submits work item @a work to be processed by workqueue
1525 * @a work_q. If the work item is already pending in the workqueue's queue
1526 * as a result of an earlier submission, this routine has no effect on the
1527 * work item. If the work item has already been processed, or is currently
1528 * being processed, its work is considered complete and the work item can be
1529 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001530 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001531 * @warning
1532 * A submitted work item must not be modified until it has been processed
1533 * by the workqueue.
1534 *
1535 * @note Can be called by ISRs.
1536 *
1537 * @param work_q Address of workqueue.
1538 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001539 *
1540 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001541 */
1542static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1543 struct k_work *work)
1544{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001545 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001546 k_fifo_put(&work_q->fifo, work);
1547 }
1548}
1549
1550/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001551 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001552 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001553 * This routine indicates if work item @a work is pending in a workqueue's
1554 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001555 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001556 * @note Can be called by ISRs.
1557 *
1558 * @param work Address of work item.
1559 *
1560 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001561 */
1562static inline int k_work_pending(struct k_work *work)
1563{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001564 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001565}
1566
1567/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001568 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001570 * This routine starts workqueue @a work_q. The workqueue spawns its work
1571 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001573 * @param work_q Address of workqueue.
1574 * @param stack Pointer to work queue thread's stack space.
1575 * @param stack_size Size of the work queue thread's stack (in bytes).
1576 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001577 *
1578 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579 */
Allan Stephens904cf972016-10-07 13:59:23 -05001580extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001581 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001582
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001583/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001584 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001585 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001586 * This routine initializes a workqueue delayed work item, prior to
1587 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001589 * @param work Address of delayed work item.
1590 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 *
1592 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001593 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001594extern void k_delayed_work_init(struct k_delayed_work *work,
1595 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001596
1597/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001598 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001599 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001600 * This routine schedules work item @a work to be processed by workqueue
1601 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1602 * an asychronous countdown for the work item and then returns to the caller.
1603 * Only when the countdown completes is the work item actually submitted to
1604 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001605 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001606 * Submitting a previously submitted delayed work item that is still
1607 * counting down cancels the existing submission and restarts the countdown
1608 * using the new delay. If the work item is currently pending on the
1609 * workqueue's queue because the countdown has completed it is too late to
1610 * resubmit the item, and resubmission fails without impacting the work item.
1611 * If the work item has already been processed, or is currently being processed,
1612 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001613 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001614 * @warning
1615 * A delayed work item must not be modified until it has been processed
1616 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001618 * @note Can be called by ISRs.
1619 *
1620 * @param work_q Address of workqueue.
1621 * @param work Address of delayed work item.
1622 * @param delay Delay before submitting the work item (in milliseconds).
1623 *
1624 * @retval 0 Work item countdown started.
1625 * @retval -EINPROGRESS Work item is already pending.
1626 * @retval -EINVAL Work item is being processed or has completed its work.
1627 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001628 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001629extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1630 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001631 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632
1633/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001634 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001636 * This routine cancels the submission of delayed work item @a work.
1637 * A delayed work item can only be cancelled while its countdown is still
1638 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001640 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001641 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001642 * @param work Address of delayed work item.
1643 *
1644 * @retval 0 Work item countdown cancelled.
1645 * @retval -EINPROGRESS Work item is already pending.
1646 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001648extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001649
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651 * @brief Submit a work item to the system workqueue.
1652 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001653 * This routine submits work item @a work to be processed by the system
1654 * workqueue. If the work item is already pending in the workqueue's queue
1655 * as a result of an earlier submission, this routine has no effect on the
1656 * work item. If the work item has already been processed, or is currently
1657 * being processed, its work is considered complete and the work item can be
1658 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001659 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001660 * @warning
1661 * Work items submitted to the system workqueue should avoid using handlers
1662 * that block or yield since this may prevent the system workqueue from
1663 * processing other work items in a timely manner.
1664 *
1665 * @note Can be called by ISRs.
1666 *
1667 * @param work Address of work item.
1668 *
1669 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001670 */
1671static inline void k_work_submit(struct k_work *work)
1672{
1673 k_work_submit_to_queue(&k_sys_work_q, work);
1674}
1675
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001676/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001677 * @brief Submit a delayed work item to the system workqueue.
1678 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001679 * This routine schedules work item @a work to be processed by the system
1680 * workqueue after a delay of @a delay milliseconds. The routine initiates
1681 * an asychronous countdown for the work item and then returns to the caller.
1682 * Only when the countdown completes is the work item actually submitted to
1683 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001684 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001685 * Submitting a previously submitted delayed work item that is still
1686 * counting down cancels the existing submission and restarts the countdown
1687 * using the new delay. If the work item is currently pending on the
1688 * workqueue's queue because the countdown has completed it is too late to
1689 * resubmit the item, and resubmission fails without impacting the work item.
1690 * If the work item has already been processed, or is currently being processed,
1691 * its work is considered complete and the work item can be resubmitted.
1692 *
1693 * @warning
1694 * Work items submitted to the system workqueue should avoid using handlers
1695 * that block or yield since this may prevent the system workqueue from
1696 * processing other work items in a timely manner.
1697 *
1698 * @note Can be called by ISRs.
1699 *
1700 * @param work Address of delayed work item.
1701 * @param delay Delay before submitting the work item (in milliseconds).
1702 *
1703 * @retval 0 Work item countdown started.
1704 * @retval -EINPROGRESS Work item is already pending.
1705 * @retval -EINVAL Work item is being processed or has completed its work.
1706 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707 */
1708static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001709 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001710{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001711 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001712}
1713
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001715 * @brief Get time remaining before a delayed work gets scheduled.
1716 *
1717 * This routine computes the (approximate) time remaining before a
1718 * delayed work gets executed. If the delayed work is not waiting to be
1719 * schedules, it returns zero.
1720 *
1721 * @param work Delayed work item.
1722 *
1723 * @return Remaining time (in milliseconds).
1724 */
1725static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1726{
1727 return _timeout_remaining_get(&work->timeout);
1728}
1729
1730/**
Allan Stephensc98da842016-11-11 15:45:03 -05001731 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732 */
1733
Allan Stephensc98da842016-11-11 15:45:03 -05001734/**
1735 * @cond INTERNAL_HIDDEN
1736 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737
1738struct k_mutex {
1739 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001740 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741 uint32_t lock_count;
1742 int owner_orig_prio;
1743#ifdef CONFIG_OBJECT_MONITOR
1744 int num_lock_state_changes;
1745 int num_conflicts;
1746#endif
1747
Anas Nashif2f203c22016-12-18 06:57:45 -05001748 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749};
1750
1751#ifdef CONFIG_OBJECT_MONITOR
1752#define _MUTEX_INIT_OBJECT_MONITOR \
1753 .num_lock_state_changes = 0, .num_conflicts = 0,
1754#else
1755#define _MUTEX_INIT_OBJECT_MONITOR
1756#endif
1757
1758#define K_MUTEX_INITIALIZER(obj) \
1759 { \
1760 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1761 .owner = NULL, \
1762 .lock_count = 0, \
1763 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1764 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001765 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766 }
1767
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768/**
Allan Stephensc98da842016-11-11 15:45:03 -05001769 * INTERNAL_HIDDEN @endcond
1770 */
1771
1772/**
1773 * @defgroup mutex_apis Mutex APIs
1774 * @ingroup kernel_apis
1775 * @{
1776 */
1777
1778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001783 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001787#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001788 struct k_mutex name \
1789 __in_section(_k_mutex, static, name) = \
1790 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * Upon completion, the mutex is available and does not have an owner.
1798 *
1799 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
1801 * @return N/A
1802 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804
1805/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001808 * This routine locks @a mutex. If the mutex is locked by another thread,
1809 * the calling thread waits until the mutex becomes available or until
1810 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * A thread is permitted to lock a mutex it has already locked. The operation
1813 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @param mutex Address of the mutex.
1816 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 * or one of the special values K_NO_WAIT and K_FOREVER.
1818 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001819 * @retval 0 Mutex locked.
1820 * @retval -EBUSY Returned without waiting.
1821 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824
1825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001826 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * This routine unlocks @a mutex. The mutex must already be locked by the
1829 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001830 *
1831 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833 * thread.
1834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836 *
1837 * @return N/A
1838 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001839extern void k_mutex_unlock(struct k_mutex *mutex);
1840
Allan Stephensc98da842016-11-11 15:45:03 -05001841/**
1842 * @} end defgroup mutex_apis
1843 */
1844
1845/**
1846 * @cond INTERNAL_HIDDEN
1847 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001848
1849struct k_sem {
1850 _wait_q_t wait_q;
1851 unsigned int count;
1852 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001853 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001854
Anas Nashif2f203c22016-12-18 06:57:45 -05001855 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001856};
1857
Allan Stephensc98da842016-11-11 15:45:03 -05001858#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1859 { \
1860 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1861 .count = initial_count, \
1862 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05001863 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05001864 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001865 }
1866
1867/**
1868 * INTERNAL_HIDDEN @endcond
1869 */
1870
1871/**
1872 * @defgroup semaphore_apis Semaphore APIs
1873 * @ingroup kernel_apis
1874 * @{
1875 */
1876
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001882 * @param sem Address of the semaphore.
1883 * @param initial_count Initial semaphore count.
1884 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001885 *
1886 * @return N/A
1887 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1889 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001890
1891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1897 *
1898 * @param sem Address of the semaphore.
1899 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001900 * or one of the special values K_NO_WAIT and K_FOREVER.
1901 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001902 * @note When porting code from the nanokernel legacy API to the new API, be
1903 * careful with the return value of this function. The return value is the
1904 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1905 * non-zero means failure, while the nano_sem_take family returns 1 for success
1906 * and 0 for failure.
1907 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001908 * @retval 0 Semaphore taken.
1909 * @retval -EBUSY Returned without waiting.
1910 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001911 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001912extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001913
1914/**
1915 * @brief Give a semaphore.
1916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine gives @a sem, unless the semaphore is already at its maximum
1918 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001923 *
1924 * @return N/A
1925 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926extern void k_sem_give(struct k_sem *sem);
1927
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001928/**
1929 * @brief Reset a semaphore's count to zero.
1930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001934 *
1935 * @return N/A
1936 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001937static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938{
1939 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940}
1941
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001942/**
1943 * @brief Get a semaphore's count.
1944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001945 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001947 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001950 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001951static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952{
1953 return sem->count;
1954}
1955
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001960 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001961 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * @param name Name of the semaphore.
1964 * @param initial_count Initial semaphore count.
1965 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001966 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001968 struct k_sem name \
1969 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970 K_SEM_INITIALIZER(name, initial_count, count_limit)
1971
Allan Stephensc98da842016-11-11 15:45:03 -05001972/**
1973 * @} end defgroup semaphore_apis
1974 */
1975
1976/**
1977 * @defgroup alert_apis Alert APIs
1978 * @ingroup kernel_apis
1979 * @{
1980 */
1981
Allan Stephens5eceb852016-11-16 10:16:30 -05001982/**
1983 * @typedef k_alert_handler_t
1984 * @brief Alert handler function type.
1985 *
1986 * An alert's alert handler function is invoked by the system workqueue
1987 * when the alert is signalled. The alert handler function is optional,
1988 * and is only invoked if the alert has been initialized with one.
1989 *
1990 * @param alert Address of the alert.
1991 *
1992 * @return 0 if alert has been consumed; non-zero if alert should pend.
1993 */
1994typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001995
1996/**
1997 * @} end defgroup alert_apis
1998 */
1999
2000/**
2001 * @cond INTERNAL_HIDDEN
2002 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002004#define K_ALERT_DEFAULT NULL
2005#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002006
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002007struct k_alert {
2008 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009 atomic_t send_count;
2010 struct k_work work_item;
2011 struct k_sem sem;
2012
Anas Nashif2f203c22016-12-18 06:57:45 -05002013 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014};
2015
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002016extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002018#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002019 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002020 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002022 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002023 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002024 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025 }
2026
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027/**
Allan Stephensc98da842016-11-11 15:45:03 -05002028 * INTERNAL_HIDDEN @endcond
2029 */
2030
2031/**
2032 * @addtogroup alert_apis
2033 * @{
2034 */
2035
2036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002039 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002041 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002043 * @param name Name of the alert.
2044 * @param alert_handler Action to take when alert is sent. Specify either
2045 * the address of a function to be invoked by the system workqueue
2046 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2047 * K_ALERT_DEFAULT (which causes the alert to pend).
2048 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002050#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002051 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002052 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002053 K_ALERT_INITIALIZER(name, alert_handler, \
2054 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002056/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002059 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061 * @param alert Address of the alert.
2062 * @param handler Action to take when alert is sent. Specify either the address
2063 * of a function to be invoked by the system workqueue thread,
2064 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2065 * K_ALERT_DEFAULT (which causes the alert to pend).
2066 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 *
2068 * @return N/A
2069 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002070extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2071 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072
2073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002078 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2079 *
2080 * @param alert Address of the alert.
2081 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 * or one of the special values K_NO_WAIT and K_FOREVER.
2083 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002084 * @retval 0 Alert received.
2085 * @retval -EBUSY Returned without waiting.
2086 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002088extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089
2090/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * This routine signals @a alert. The action specified for @a alert will
2094 * be taken, which may trigger the execution of an alert handler function
2095 * and/or cause the alert to pend (assuming the alert has not reached its
2096 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @note Can be called by ISRs.
2099 *
2100 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 *
2102 * @return N/A
2103 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002104extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002105
2106/**
Allan Stephensc98da842016-11-11 15:45:03 -05002107 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108 */
2109
Allan Stephensc98da842016-11-11 15:45:03 -05002110/**
2111 * @cond INTERNAL_HIDDEN
2112 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113
2114struct k_msgq {
2115 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002116 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117 uint32_t max_msgs;
2118 char *buffer_start;
2119 char *buffer_end;
2120 char *read_ptr;
2121 char *write_ptr;
2122 uint32_t used_msgs;
2123
Anas Nashif2f203c22016-12-18 06:57:45 -05002124 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125};
2126
Peter Mitsis1da807e2016-10-06 11:36:59 -04002127#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128 { \
2129 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002130 .max_msgs = q_max_msgs, \
2131 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002133 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002134 .read_ptr = q_buffer, \
2135 .write_ptr = q_buffer, \
2136 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002137 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002138 }
2139
Peter Mitsis1da807e2016-10-06 11:36:59 -04002140/**
Allan Stephensc98da842016-11-11 15:45:03 -05002141 * INTERNAL_HIDDEN @endcond
2142 */
2143
2144/**
2145 * @defgroup msgq_apis Message Queue APIs
2146 * @ingroup kernel_apis
2147 * @{
2148 */
2149
2150/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002151 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2154 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002155 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2156 * message is similarly aligned to this boundary, @a q_msg_size must also be
2157 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * The message queue can be accessed outside the module where it is defined
2160 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002162 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002164 * @param q_name Name of the message queue.
2165 * @param q_msg_size Message size (in bytes).
2166 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002167 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002168 */
2169#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2170 static char __noinit __aligned(q_align) \
2171 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002172 struct k_msgq q_name \
2173 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002174 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2175 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176
Peter Mitsisd7a37502016-10-13 11:37:40 -04002177/**
2178 * @brief Initialize a message queue.
2179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * This routine initializes a message queue object, prior to its first use.
2181 *
Allan Stephensda827222016-11-09 14:23:58 -06002182 * The message queue's ring buffer must contain space for @a max_msgs messages,
2183 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2184 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2185 * that each message is similarly aligned to this boundary, @a q_msg_size
2186 * must also be a multiple of N.
2187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002188 * @param q Address of the message queue.
2189 * @param buffer Pointer to ring buffer that holds queued messages.
2190 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002191 * @param max_msgs Maximum number of messages that can be queued.
2192 *
2193 * @return N/A
2194 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002195extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002196 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002197
2198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002201 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002202 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002203 * @note Can be called by ISRs.
2204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002205 * @param q Address of the message queue.
2206 * @param data Pointer to the message.
2207 * @param timeout Waiting period to add the message (in milliseconds),
2208 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002209 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002210 * @retval 0 Message sent.
2211 * @retval -ENOMSG Returned without waiting or queue purged.
2212 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002213 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002215
2216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002219 * This routine receives a message from message queue @a q in a "first in,
2220 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002221 *
Allan Stephensc98da842016-11-11 15:45:03 -05002222 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002224 * @param q Address of the message queue.
2225 * @param data Address of area to hold the received message.
2226 * @param timeout Waiting period to receive the message (in milliseconds),
2227 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002228 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002229 * @retval 0 Message received.
2230 * @retval -ENOMSG Returned without waiting.
2231 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002232 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002234
2235/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002236 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002238 * This routine discards all unreceived messages in a message queue's ring
2239 * buffer. Any threads that are blocked waiting to send a message to the
2240 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002242 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002243 *
2244 * @return N/A
2245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246extern void k_msgq_purge(struct k_msgq *q);
2247
Peter Mitsis67be2492016-10-07 11:44:34 -04002248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002249 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002251 * This routine returns the number of unused entries in a message queue's
2252 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002254 * @param q Address of the message queue.
2255 *
2256 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002257 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002258static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002259{
2260 return q->max_msgs - q->used_msgs;
2261}
2262
Peter Mitsisd7a37502016-10-13 11:37:40 -04002263/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002266 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * @param q Address of the message queue.
2269 *
2270 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002271 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002272static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273{
2274 return q->used_msgs;
2275}
2276
Allan Stephensc98da842016-11-11 15:45:03 -05002277/**
2278 * @} end defgroup msgq_apis
2279 */
2280
2281/**
2282 * @defgroup mem_pool_apis Memory Pool APIs
2283 * @ingroup kernel_apis
2284 * @{
2285 */
2286
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002288 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289 void *addr_in_pool;
2290 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002291 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292};
2293
Allan Stephensc98da842016-11-11 15:45:03 -05002294/**
2295 * @} end defgroup mem_pool_apis
2296 */
2297
2298/**
2299 * @defgroup mailbox_apis Mailbox APIs
2300 * @ingroup kernel_apis
2301 * @{
2302 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303
2304struct k_mbox_msg {
2305 /** internal use only - needed for legacy API support */
2306 uint32_t _mailbox;
2307 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002308 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309 /** application-defined information value */
2310 uint32_t info;
2311 /** sender's message data buffer */
2312 void *tx_data;
2313 /** internal use only - needed for legacy API support */
2314 void *_rx_data;
2315 /** message data block descriptor */
2316 struct k_mem_block tx_block;
2317 /** source thread id */
2318 k_tid_t rx_source_thread;
2319 /** target thread id */
2320 k_tid_t tx_target_thread;
2321 /** internal use only - thread waiting on send (may be a dummy) */
2322 k_tid_t _syncing_thread;
2323#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2324 /** internal use only - semaphore used during asynchronous send */
2325 struct k_sem *_async_sem;
2326#endif
2327};
2328
Allan Stephensc98da842016-11-11 15:45:03 -05002329/**
2330 * @cond INTERNAL_HIDDEN
2331 */
2332
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002333struct k_mbox {
2334 _wait_q_t tx_msg_queue;
2335 _wait_q_t rx_msg_queue;
2336
Anas Nashif2f203c22016-12-18 06:57:45 -05002337 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338};
2339
2340#define K_MBOX_INITIALIZER(obj) \
2341 { \
2342 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2343 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002344 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345 }
2346
Peter Mitsis12092702016-10-14 12:57:23 -04002347/**
Allan Stephensc98da842016-11-11 15:45:03 -05002348 * INTERNAL_HIDDEN @endcond
2349 */
2350
2351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002356 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002359 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002361 struct k_mbox name \
2362 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 K_MBOX_INITIALIZER(name) \
2364
Peter Mitsis12092702016-10-14 12:57:23 -04002365/**
2366 * @brief Initialize a mailbox.
2367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * This routine initializes a mailbox object, prior to its first use.
2369 *
2370 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002371 *
2372 * @return N/A
2373 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374extern void k_mbox_init(struct k_mbox *mbox);
2375
Peter Mitsis12092702016-10-14 12:57:23 -04002376/**
2377 * @brief Send a mailbox message in a synchronous manner.
2378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * This routine sends a message to @a mbox and waits for a receiver to both
2380 * receive and process it. The message data may be in a buffer, in a memory
2381 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @param mbox Address of the mailbox.
2384 * @param tx_msg Address of the transmit message descriptor.
2385 * @param timeout Waiting period for the message to be received (in
2386 * milliseconds), or one of the special values K_NO_WAIT
2387 * and K_FOREVER. Once the message has been received,
2388 * this routine waits as long as necessary for the message
2389 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002390 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002391 * @retval 0 Message sent.
2392 * @retval -ENOMSG Returned without waiting.
2393 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002394 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002395extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002397
Peter Mitsis12092702016-10-14 12:57:23 -04002398/**
2399 * @brief Send a mailbox message in an asynchronous manner.
2400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * This routine sends a message to @a mbox without waiting for a receiver
2402 * to process it. The message data may be in a buffer, in a memory pool block,
2403 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2404 * will be given when the message has been both received and completely
2405 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @param mbox Address of the mailbox.
2408 * @param tx_msg Address of the transmit message descriptor.
2409 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002410 *
2411 * @return N/A
2412 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002413extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414 struct k_sem *sem);
2415
Peter Mitsis12092702016-10-14 12:57:23 -04002416/**
2417 * @brief Receive a mailbox message.
2418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * This routine receives a message from @a mbox, then optionally retrieves
2420 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * @param mbox Address of the mailbox.
2423 * @param rx_msg Address of the receive message descriptor.
2424 * @param buffer Address of the buffer to receive data, or NULL to defer data
2425 * retrieval and message disposal until later.
2426 * @param timeout Waiting period for a message to be received (in
2427 * milliseconds), or one of the special values K_NO_WAIT
2428 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002429 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002430 * @retval 0 Message received.
2431 * @retval -ENOMSG Returned without waiting.
2432 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002433 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002434extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002436
2437/**
2438 * @brief Retrieve mailbox message data into a buffer.
2439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002440 * This routine completes the processing of a received message by retrieving
2441 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002442 *
2443 * Alternatively, this routine can be used to dispose of a received message
2444 * without retrieving its data.
2445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * @param rx_msg Address of the receive message descriptor.
2447 * @param buffer Address of the buffer to receive data, or NULL to discard
2448 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002449 *
2450 * @return N/A
2451 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002452extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002453
2454/**
2455 * @brief Retrieve mailbox message data into a memory pool block.
2456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002457 * This routine completes the processing of a received message by retrieving
2458 * its data into a memory pool block, then disposing of the message.
2459 * The memory pool block that results from successful retrieval must be
2460 * returned to the pool once the data has been processed, even in cases
2461 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002462 *
2463 * Alternatively, this routine can be used to dispose of a received message
2464 * without retrieving its data. In this case there is no need to return a
2465 * memory pool block to the pool.
2466 *
2467 * This routine allocates a new memory pool block for the data only if the
2468 * data is not already in one. If a new block cannot be allocated, the routine
2469 * returns a failure code and the received message is left unchanged. This
2470 * permits the caller to reattempt data retrieval at a later time or to dispose
2471 * of the received message without retrieving its data.
2472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @param rx_msg Address of a receive message descriptor.
2474 * @param pool Address of memory pool, or NULL to discard data.
2475 * @param block Address of the area to hold memory pool block info.
2476 * @param timeout Waiting period to wait for a memory pool block (in
2477 * milliseconds), or one of the special values K_NO_WAIT
2478 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002479 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002480 * @retval 0 Data retrieved.
2481 * @retval -ENOMEM Returned without waiting.
2482 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002483 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002484extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002485 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486 struct k_mem_block *block, int32_t timeout);
2487
Allan Stephensc98da842016-11-11 15:45:03 -05002488/**
2489 * @} end defgroup mailbox_apis
2490 */
2491
2492/**
2493 * @cond INTERNAL_HIDDEN
2494 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495
2496struct k_pipe {
2497 unsigned char *buffer; /* Pipe buffer: may be NULL */
2498 size_t size; /* Buffer size */
2499 size_t bytes_used; /* # bytes used in buffer */
2500 size_t read_index; /* Where in buffer to read from */
2501 size_t write_index; /* Where in buffer to write */
2502
2503 struct {
2504 _wait_q_t readers; /* Reader wait queue */
2505 _wait_q_t writers; /* Writer wait queue */
2506 } wait_q;
2507
Anas Nashif2f203c22016-12-18 06:57:45 -05002508 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509};
2510
Peter Mitsise5d9c582016-10-14 14:44:57 -04002511#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 { \
2513 .buffer = pipe_buffer, \
2514 .size = pipe_buffer_size, \
2515 .bytes_used = 0, \
2516 .read_index = 0, \
2517 .write_index = 0, \
2518 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2519 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002520 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521 }
2522
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002523/**
Allan Stephensc98da842016-11-11 15:45:03 -05002524 * INTERNAL_HIDDEN @endcond
2525 */
2526
2527/**
2528 * @defgroup pipe_apis Pipe APIs
2529 * @ingroup kernel_apis
2530 * @{
2531 */
2532
2533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002538 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @param name Name of the pipe.
2541 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2542 * or zero if no ring buffer is used.
2543 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002545#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2546 static unsigned char __noinit __aligned(pipe_align) \
2547 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002548 struct k_pipe name \
2549 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002550 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * This routine initializes a pipe object, prior to its first use.
2556 *
2557 * @param pipe Address of the pipe.
2558 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2559 * is used.
2560 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2561 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 *
2563 * @return N/A
2564 */
2565extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2566 size_t size);
2567
2568/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * @param pipe Address of the pipe.
2574 * @param data Address of data to write.
2575 * @param bytes_to_write Size of data (in bytes).
2576 * @param bytes_written Address of area to hold the number of bytes written.
2577 * @param min_xfer Minimum number of bytes to write.
2578 * @param timeout Waiting period to wait for the data to be written (in
2579 * milliseconds), or one of the special values K_NO_WAIT
2580 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002582 * @retval 0 At least @a min_xfer bytes of data were written.
2583 * @retval -EIO Returned without waiting; zero data bytes were written.
2584 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002587extern int k_pipe_put(struct k_pipe *pipe, void *data,
2588 size_t bytes_to_write, size_t *bytes_written,
2589 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590
2591/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @param pipe Address of the pipe.
2597 * @param data Address to place the data read from pipe.
2598 * @param bytes_to_read Maximum number of data bytes to read.
2599 * @param bytes_read Address of area to hold the number of bytes read.
2600 * @param min_xfer Minimum number of data bytes to read.
2601 * @param timeout Waiting period to wait for the data to be read (in
2602 * milliseconds), or one of the special values K_NO_WAIT
2603 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002605 * @retval 0 At least @a min_xfer bytes of data were read.
2606 * @retval -EIO Returned without waiting; zero data bytes were read.
2607 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002610extern int k_pipe_get(struct k_pipe *pipe, void *data,
2611 size_t bytes_to_read, size_t *bytes_read,
2612 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613
2614/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * This routine writes the data contained in a memory block to @a pipe.
2618 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002619 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002622 * @param block Memory block containing data to send
2623 * @param size Number of data bytes in memory block to send
2624 * @param sem Semaphore to signal upon completion (else NULL)
2625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627 */
2628extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2629 size_t size, struct k_sem *sem);
2630
2631/**
Allan Stephensc98da842016-11-11 15:45:03 -05002632 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 */
2634
Allan Stephensc98da842016-11-11 15:45:03 -05002635/**
2636 * @cond INTERNAL_HIDDEN
2637 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002639struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002641 uint32_t num_blocks;
2642 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643 char *buffer;
2644 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002645 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646
Anas Nashif2f203c22016-12-18 06:57:45 -05002647 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002648};
2649
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002650#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2651 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652 { \
2653 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002654 .num_blocks = slab_num_blocks, \
2655 .block_size = slab_block_size, \
2656 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657 .free_list = NULL, \
2658 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002659 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660 }
2661
Peter Mitsis578f9112016-10-07 13:50:31 -04002662/**
Allan Stephensc98da842016-11-11 15:45:03 -05002663 * INTERNAL_HIDDEN @endcond
2664 */
2665
2666/**
2667 * @defgroup mem_slab_apis Memory Slab APIs
2668 * @ingroup kernel_apis
2669 * @{
2670 */
2671
2672/**
Allan Stephensda827222016-11-09 14:23:58 -06002673 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002674 *
Allan Stephensda827222016-11-09 14:23:58 -06002675 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002677 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2678 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002679 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002680 *
Allan Stephensda827222016-11-09 14:23:58 -06002681 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002684 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @param name Name of the memory slab.
2687 * @param slab_block_size Size of each memory block (in bytes).
2688 * @param slab_num_blocks Number memory blocks.
2689 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002690 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002691#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2692 char __noinit __aligned(slab_align) \
2693 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2694 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002695 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002696 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2697 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002699/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002700 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002703 *
Allan Stephensda827222016-11-09 14:23:58 -06002704 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2705 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2706 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2707 * To ensure that each memory block is similarly aligned to this boundary,
2708 * @a slab_block_size must also be a multiple of N.
2709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002710 * @param slab Address of the memory slab.
2711 * @param buffer Pointer to buffer used for the memory blocks.
2712 * @param block_size Size of each memory block (in bytes).
2713 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002714 *
2715 * @return N/A
2716 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002717extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002718 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002719
2720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002721 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002723 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002725 * @param slab Address of the memory slab.
2726 * @param mem Pointer to block address area.
2727 * @param timeout Maximum time to wait for operation to complete
2728 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2729 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002730 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002731 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002733 * @retval -ENOMEM Returned without waiting.
2734 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002735 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002736extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2737 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002738
2739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * This routine releases a previously allocated memory block back to its
2743 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * @param slab Address of the memory slab.
2746 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002747 *
2748 * @return N/A
2749 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002750extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * This routine gets the number of memory blocks that are currently
2756 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002761 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002762static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002764 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765}
2766
Peter Mitsisc001aa82016-10-13 13:53:37 -04002767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * This routine gets the number of memory blocks that are currently
2771 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002773 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002776 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002777static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002778{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002779 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002780}
2781
Allan Stephensc98da842016-11-11 15:45:03 -05002782/**
2783 * @} end defgroup mem_slab_apis
2784 */
2785
2786/**
2787 * @cond INTERNAL_HIDDEN
2788 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002790/*
2791 * Memory pool requires a buffer and two arrays of structures for the
2792 * memory block accounting:
2793 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2794 * status of four blocks of memory.
2795 */
2796struct k_mem_pool_quad_block {
2797 char *mem_blocks; /* pointer to the first of four memory blocks */
2798 uint32_t mem_status; /* four bits. If bit is set, memory block is
2799 allocated */
2800};
2801/*
2802 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2803 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2804 * block size is 4 times less than the previous one and thus requires 4 times
2805 * bigger array of k_mem_pool_quad_block structures to keep track of the
2806 * memory blocks.
2807 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002808
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002809/*
2810 * The array of k_mem_pool_block_set keeps the information of each array of
2811 * k_mem_pool_quad_block structures
2812 */
2813struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002814 size_t block_size; /* memory block size */
2815 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002816 struct k_mem_pool_quad_block *quad_block;
2817 int count;
2818};
2819
2820/* Memory pool descriptor */
2821struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002822 size_t max_block_size;
2823 size_t min_block_size;
2824 uint32_t nr_of_maxblocks;
2825 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002826 struct k_mem_pool_block_set *block_set;
2827 char *bufblock;
2828 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002829 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830};
2831
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002832#ifdef CONFIG_ARM
2833#define _SECTION_TYPE_SIGN "%"
2834#else
2835#define _SECTION_TYPE_SIGN "@"
2836#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002838/*
2839 * Static memory pool initialization
2840 */
Allan Stephensc98da842016-11-11 15:45:03 -05002841
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002842/*
2843 * Use .altmacro to be able to recalculate values and pass them as string
2844 * arguments when calling assembler macros resursively
2845 */
2846__asm__(".altmacro\n\t");
2847
2848/*
2849 * Recursively calls a macro
2850 * The followig global symbols need to be initialized:
2851 * __memory_pool_max_block_size - maximal size of the memory block
2852 * __memory_pool_min_block_size - minimal size of the memory block
2853 * Notes:
2854 * Global symbols are used due the fact that assembler macro allows only
2855 * one argument be passed with the % conversion
2856 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2857 * is used instead of / 4.
2858 * n_max argument needs to go first in the invoked macro, as some
2859 * assemblers concatenate \name and %(\n_max * 4) arguments
2860 * if \name goes first
2861 */
2862__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2863 ".ifge __memory_pool_max_block_size >> 2 -"
2864 " __memory_pool_min_block_size\n\t\t"
2865 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2866 "\\macro_name %(\\n_max * 4) \\name\n\t"
2867 ".endif\n\t"
2868 ".endm\n");
2869
2870/*
2871 * Build quad blocks
2872 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2873 * structures and recursively calls itself for the next array, 4 times
2874 * larger.
2875 * The followig global symbols need to be initialized:
2876 * __memory_pool_max_block_size - maximal size of the memory block
2877 * __memory_pool_min_block_size - minimal size of the memory block
2878 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2879 */
2880__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002881 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002882 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2883 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2884 ".if \\n_max % 4\n\t\t"
2885 ".skip __memory_pool_quad_block_size\n\t"
2886 ".endif\n\t"
2887 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2888 ".endm\n");
2889
2890/*
2891 * Build block sets and initialize them
2892 * Macro initializes the k_mem_pool_block_set structure and
2893 * recursively calls itself for the next one.
2894 * The followig global symbols need to be initialized:
2895 * __memory_pool_max_block_size - maximal size of the memory block
2896 * __memory_pool_min_block_size - minimal size of the memory block
2897 * __memory_pool_block_set_count, the number of the elements in the
2898 * block set array must be set to 0. Macro calculates it's real
2899 * value.
2900 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2901 * structures, _build_quad_blocks must be called prior it.
2902 */
2903__asm__(".macro _build_block_set n_max, name\n\t"
2904 ".int __memory_pool_max_block_size\n\t" /* block_size */
2905 ".if \\n_max % 4\n\t\t"
2906 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2907 ".else\n\t\t"
2908 ".int \\n_max >> 2\n\t"
2909 ".endif\n\t"
2910 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2911 ".int 0\n\t" /* count */
2912 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2913 "__do_recurse _build_block_set \\name \\n_max\n\t"
2914 ".endm\n");
2915
2916/*
2917 * Build a memory pool structure and initialize it
2918 * Macro uses __memory_pool_block_set_count global symbol,
2919 * block set addresses and buffer address, it may be called only after
2920 * _build_block_set
2921 */
2922__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002923 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002924 _SECTION_TYPE_SIGN "progbits\n\t"
2925 ".globl \\name\n\t"
2926 "\\name:\n\t"
2927 ".int \\max_size\n\t" /* max_block_size */
2928 ".int \\min_size\n\t" /* min_block_size */
2929 ".int \\n_max\n\t" /* nr_of_maxblocks */
2930 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2931 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2932 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2933 ".int 0\n\t" /* wait_q->head */
2934 ".int 0\n\t" /* wait_q->next */
2935 ".popsection\n\t"
2936 ".endm\n");
2937
2938#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2939 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2940 _SECTION_TYPE_SIGN "progbits\n\t"); \
2941 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2942 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2943 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2944 STRINGIFY(name) "\n\t"); \
2945 __asm__(".popsection\n\t")
2946
2947#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2948 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2949 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2950 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2951 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002952 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002953 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2954 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2955 STRINGIFY(name) "\n\t"); \
2956 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2957 __asm__(".int __memory_pool_block_set_count\n\t"); \
2958 __asm__(".popsection\n\t"); \
2959 extern uint32_t _mem_pool_block_set_count_##name; \
2960 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2961
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002962#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2963 char __noinit __aligned(align) \
2964 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002965
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002966/*
2967 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2968 * to __memory_pool_quad_block_size absolute symbol.
2969 * This function does not get called, but compiler calculates the value and
2970 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2971 */
2972static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2973{
2974 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01002975#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002976 "__memory_pool_quad_block_size = %0\n\t"
2977#else
2978 "__memory_pool_quad_block_size = %c0\n\t"
2979#endif
2980 :
2981 : "n"(sizeof(struct k_mem_pool_quad_block)));
2982}
2983
2984/**
Allan Stephensc98da842016-11-11 15:45:03 -05002985 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002986 */
2987
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002988/**
Allan Stephensc98da842016-11-11 15:45:03 -05002989 * @addtogroup mem_pool_apis
2990 * @{
2991 */
2992
2993/**
2994 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2997 * long. The memory pool allows blocks to be repeatedly partitioned into
2998 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2999 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003000 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003002 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003003 * If the pool is to be accessed outside the module where it is defined, it
3004 * can be declared via
3005 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003006 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param name Name of the memory pool.
3009 * @param min_size Size of the smallest blocks in the pool (in bytes).
3010 * @param max_size Size of the largest blocks in the pool (in bytes).
3011 * @param n_max Number of maximum sized blocks in the pool.
3012 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003013 */
3014#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003015 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3016 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003017 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003018 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3019 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3020 extern struct k_mem_pool name
3021
Peter Mitsis937042c2016-10-13 13:18:26 -04003022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @param pool Address of the memory pool.
3028 * @param block Pointer to block descriptor for the allocated memory.
3029 * @param size Amount of memory to allocate (in bytes).
3030 * @param timeout Maximum time to wait for operation to complete
3031 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3032 * or K_FOREVER to wait as long as necessary.
3033 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003034 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003036 * @retval -ENOMEM Returned without waiting.
3037 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003038 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003039extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003040 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003041
3042/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003043 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * This routine releases a previously allocated memory block back to its
3046 * memory pool.
3047 *
3048 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003049 *
3050 * @return N/A
3051 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003052extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003053
3054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * This routine instructs a memory pool to concatenate unused memory blocks
3058 * into larger blocks wherever possible. Manually defragmenting the memory
3059 * pool may speed up future allocations of memory blocks by eliminating the
3060 * need for the memory pool to perform an automatic partial defragmentation.
3061 *
3062 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003063 *
3064 * @return N/A
3065 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003066extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003067
3068/**
Allan Stephensc98da842016-11-11 15:45:03 -05003069 * @} end addtogroup mem_pool_apis
3070 */
3071
3072/**
3073 * @defgroup heap_apis Heap Memory Pool APIs
3074 * @ingroup kernel_apis
3075 * @{
3076 */
3077
3078/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003082 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003084 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003087 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003088extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003089
3090/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003091 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003092 *
3093 * This routine provides traditional free() semantics. The memory being
3094 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003095 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003096 * If @a ptr is NULL, no operation is performed.
3097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003099 *
3100 * @return N/A
3101 */
3102extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103
Allan Stephensc98da842016-11-11 15:45:03 -05003104/**
3105 * @} end defgroup heap_apis
3106 */
3107
Benjamin Walshacc68c12017-01-29 18:57:45 -05003108/* polling API - PRIVATE */
3109
Benjamin Walshb0179862017-02-02 16:39:57 -05003110#ifdef CONFIG_POLL
3111#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3112#else
3113#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3114#endif
3115
Benjamin Walshacc68c12017-01-29 18:57:45 -05003116/* private - implementation data created as needed, per-type */
3117struct _poller {
3118 struct k_thread *thread;
3119};
3120
3121/* private - types bit positions */
3122enum _poll_types_bits {
3123 /* can be used to ignore an event */
3124 _POLL_TYPE_IGNORE,
3125
3126 /* to be signaled by k_poll_signal() */
3127 _POLL_TYPE_SIGNAL,
3128
3129 /* semaphore availability */
3130 _POLL_TYPE_SEM_AVAILABLE,
3131
3132 /* fifo data availability */
3133 _POLL_TYPE_FIFO_DATA_AVAILABLE,
3134
3135 _POLL_NUM_TYPES
3136};
3137
3138#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3139
3140/* private - states bit positions */
3141enum _poll_states_bits {
3142 /* default state when creating event */
3143 _POLL_STATE_NOT_READY,
3144
3145 /* there was another poller already on the object */
3146 _POLL_STATE_EADDRINUSE,
3147
3148 /* signaled by k_poll_signal() */
3149 _POLL_STATE_SIGNALED,
3150
3151 /* semaphore is available */
3152 _POLL_STATE_SEM_AVAILABLE,
3153
3154 /* data is available to read on fifo */
3155 _POLL_STATE_FIFO_DATA_AVAILABLE,
3156
3157 _POLL_NUM_STATES
3158};
3159
3160#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3161
3162#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003163 (32 - (0 \
3164 + 8 /* tag */ \
3165 + _POLL_NUM_TYPES \
3166 + _POLL_NUM_STATES \
3167 + 1 /* modes */ \
3168 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003169
3170#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3171#error overflow of 32-bit word in struct k_poll_event
3172#endif
3173
3174/* end of polling API - PRIVATE */
3175
3176
3177/**
3178 * @defgroup poll_apis Async polling APIs
3179 * @ingroup kernel_apis
3180 * @{
3181 */
3182
3183/* Public polling API */
3184
3185/* public - values for k_poll_event.type bitfield */
3186#define K_POLL_TYPE_IGNORE 0
3187#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3188#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
3189#define K_POLL_TYPE_FIFO_DATA_AVAILABLE \
3190 _POLL_TYPE_BIT(_POLL_TYPE_FIFO_DATA_AVAILABLE)
3191
3192/* public - polling modes */
3193enum k_poll_modes {
3194 /* polling thread does not take ownership of objects when available */
3195 K_POLL_MODE_NOTIFY_ONLY = 0,
3196
3197 K_POLL_NUM_MODES
3198};
3199
3200/* public - values for k_poll_event.state bitfield */
3201#define K_POLL_STATE_NOT_READY 0
3202#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3203#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3204#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
3205#define K_POLL_STATE_FIFO_DATA_AVAILABLE \
3206 _POLL_STATE_BIT(_POLL_STATE_FIFO_DATA_AVAILABLE)
3207
3208/* public - poll signal object */
3209struct k_poll_signal {
3210 /* PRIVATE - DO NOT TOUCH */
3211 struct k_poll_event *poll_event;
3212
3213 /*
3214 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3215 * user resets it to 0.
3216 */
3217 unsigned int signaled;
3218
3219 /* custom result value passed to k_poll_signal() if needed */
3220 int result;
3221};
3222
3223#define K_POLL_SIGNAL_INITIALIZER() \
3224 { \
3225 .poll_event = NULL, \
3226 .signaled = 0, \
3227 .result = 0, \
3228 }
3229
3230struct k_poll_event {
3231 /* PRIVATE - DO NOT TOUCH */
3232 struct _poller *poller;
3233
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003234 /* optional user-specified tag, opaque, untouched by the API */
3235 uint32_t tag:8;
3236
Benjamin Walshacc68c12017-01-29 18:57:45 -05003237 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
3238 uint32_t type:_POLL_NUM_TYPES;
3239
3240 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
3241 uint32_t state:_POLL_NUM_STATES;
3242
3243 /* mode of operation, from enum k_poll_modes */
3244 uint32_t mode:1;
3245
3246 /* unused bits in 32-bit word */
3247 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
3248
3249 /* per-type data */
3250 union {
3251 void *obj;
3252 struct k_poll_signal *signal;
3253 struct k_sem *sem;
3254 struct k_fifo *fifo;
3255 };
3256};
3257
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003258#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003259 { \
3260 .poller = NULL, \
3261 .type = event_type, \
3262 .state = K_POLL_STATE_NOT_READY, \
3263 .mode = event_mode, \
3264 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003265 { .obj = event_obj }, \
3266 }
3267
3268#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3269 event_tag) \
3270 { \
3271 .type = event_type, \
3272 .tag = event_tag, \
3273 .state = K_POLL_STATE_NOT_READY, \
3274 .mode = event_mode, \
3275 .unused = 0, \
3276 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003277 }
3278
3279/**
3280 * @brief Initialize one struct k_poll_event instance
3281 *
3282 * After this routine is called on a poll event, the event it ready to be
3283 * placed in an event array to be passed to k_poll().
3284 *
3285 * @param event The event to initialize.
3286 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3287 * values. Only values that apply to the same object being polled
3288 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3289 * event.
3290 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3291 * @param obj Kernel object or poll signal.
3292 *
3293 * @return N/A
3294 */
3295
3296extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
3297 int mode, void *obj);
3298
3299/**
3300 * @brief Wait for one or many of multiple poll events to occur
3301 *
3302 * This routine allows a thread to wait concurrently for one or many of
3303 * multiple poll events to have occurred. Such events can be a kernel object
3304 * being available, like a semaphore, or a poll signal event.
3305 *
3306 * When an event notifies that a kernel object is available, the kernel object
3307 * is not "given" to the thread calling k_poll(): it merely signals the fact
3308 * that the object was available when the k_poll() call was in effect. Also,
3309 * all threads trying to acquire an object the regular way, i.e. by pending on
3310 * the object, have precedence over the thread polling on the object. This
3311 * means that the polling thread will never get the poll event on an object
3312 * until the object becomes available and its pend queue is empty. For this
3313 * reason, the k_poll() call is more effective when the objects being polled
3314 * only have one thread, the polling thread, trying to acquire them.
3315 *
3316 * Only one thread can be polling for a particular object at a given time. If
3317 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3318 * and returns as soon as it has finished handling the other events. This means
3319 * that k_poll() can return -EADDRINUSE and have the state value of some events
3320 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3321 * parameter is ignored.
3322 *
3323 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3324 * events that were passed to k_poll() and check the state field for the values
3325 * that were expected and take the associated actions.
3326 *
3327 * Before being reused for another call to k_poll(), the user has to reset the
3328 * state field to K_POLL_STATE_NOT_READY.
3329 *
3330 * @param events An array of pointers to events to be polled for.
3331 * @param num_events The number of events in the array.
3332 * @param timeout Waiting period for an event to be ready (in milliseconds),
3333 * or one of the special values K_NO_WAIT and K_FOREVER.
3334 *
3335 * @retval 0 One or more events are ready.
3336 * @retval -EADDRINUSE One or more objects already had a poller.
3337 * @retval -EAGAIN Waiting period timed out.
3338 */
3339
3340extern int k_poll(struct k_poll_event *events, int num_events,
3341 int32_t timeout);
3342
3343/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003344 * @brief Initialize a poll signal object.
3345 *
3346 * Ready a poll signal object to be signaled via k_poll_signal().
3347 *
3348 * @param signal A poll signal.
3349 *
3350 * @return N/A
3351 */
3352
3353extern void k_poll_signal_init(struct k_poll_signal *signal);
3354
3355/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003356 * @brief Signal a poll signal object.
3357 *
3358 * This routine makes ready a poll signal, which is basically a poll event of
3359 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3360 * made ready to run. A @a result value can be specified.
3361 *
3362 * The poll signal contains a 'signaled' field that, when set by
3363 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3364 * be reset by the user before being passed again to k_poll() or k_poll() will
3365 * consider it being signaled, and will return immediately.
3366 *
3367 * @param signal A poll signal.
3368 * @param result The value to store in the result field of the signal.
3369 *
3370 * @retval 0 The signal was delivered successfully.
3371 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3372 */
3373
3374extern int k_poll_signal(struct k_poll_signal *signal, int result);
3375
3376/* private internal function */
3377extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
3378 uint32_t state);
3379
3380/**
3381 * @} end defgroup poll_apis
3382 */
3383
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003384/**
3385 * @brief Make the CPU idle.
3386 *
3387 * This function makes the CPU idle until an event wakes it up.
3388 *
3389 * In a regular system, the idle thread should be the only thread responsible
3390 * for making the CPU idle and triggering any type of power management.
3391 * However, in some more constrained systems, such as a single-threaded system,
3392 * the only thread would be responsible for this if needed.
3393 *
3394 * @return N/A
3395 */
3396extern void k_cpu_idle(void);
3397
3398/**
3399 * @brief Make the CPU idle in an atomic fashion.
3400 *
3401 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3402 * must be done atomically before making the CPU idle.
3403 *
3404 * @param key Interrupt locking key obtained from irq_lock().
3405 *
3406 * @return N/A
3407 */
3408extern void k_cpu_atomic_idle(unsigned int key);
3409
Andrew Boie350f88d2017-01-18 13:13:45 -08003410extern void _sys_power_save_idle_exit(int32_t ticks);
3411
Anas Nashifa6149502017-01-17 07:47:31 -05003412/* Include legacy APIs */
3413#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003415#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416#include <arch/cpu.h>
3417
3418/*
3419 * private APIs that are utilized by one or more public APIs
3420 */
3421
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003422#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003423extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003424#else
3425#define _init_static_threads() do { } while ((0))
3426#endif
3427
3428extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003429extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430
3431#ifdef __cplusplus
3432}
3433#endif
3434
Andrew Boiee004dec2016-11-07 09:01:19 -08003435#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3436/*
3437 * Define new and delete operators.
3438 * At this moment, the operators do nothing since objects are supposed
3439 * to be statically allocated.
3440 */
3441inline void operator delete(void *ptr)
3442{
3443 (void)ptr;
3444}
3445
3446inline void operator delete[](void *ptr)
3447{
3448 (void)ptr;
3449}
3450
3451inline void *operator new(size_t size)
3452{
3453 (void)size;
3454 return NULL;
3455}
3456
3457inline void *operator new[](size_t size)
3458{
3459 (void)size;
3460 return NULL;
3461}
3462
3463/* Placement versions of operator new and delete */
3464inline void operator delete(void *ptr1, void *ptr2)
3465{
3466 (void)ptr1;
3467 (void)ptr2;
3468}
3469
3470inline void operator delete[](void *ptr1, void *ptr2)
3471{
3472 (void)ptr1;
3473 (void)ptr2;
3474}
3475
3476inline void *operator new(size_t size, void *ptr)
3477{
3478 (void)size;
3479 return ptr;
3480}
3481
3482inline void *operator new[](size_t size, void *ptr)
3483{
3484 (void)size;
3485 return ptr;
3486}
3487
3488#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3489
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003490#endif /* !_ASMLANGUAGE */
3491
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003492#endif /* _kernel__h_ */