blob: dd42117e123cf45a41697ec7a40ff683fafda8f9 [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 { \
378 .init_stack = (stack), \
379 .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 *
935 * @param timer Address of timer.
936 *
937 * @return N/A
938 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400939extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500940
941/**
942 * @brief Read timer status.
943 *
944 * This routine reads the timer's status, which indicates the number of times
945 * it has expired since its status was last read.
946 *
947 * Calling this routine resets the timer's status to zero.
948 *
949 * @param timer Address of timer.
950 *
951 * @return Timer status.
952 */
953extern uint32_t k_timer_status_get(struct k_timer *timer);
954
955/**
956 * @brief Synchronize thread to timer expiration.
957 *
958 * This routine blocks the calling thread until the timer's status is non-zero
959 * (indicating that it has expired at least once since it was last examined)
960 * or the timer is stopped. If the timer status is already non-zero,
961 * or the timer is already stopped, the caller continues without waiting.
962 *
963 * Calling this routine resets the timer's status to zero.
964 *
965 * This routine must not be used by interrupt handlers, since they are not
966 * allowed to block.
967 *
968 * @param timer Address of timer.
969 *
970 * @return Timer status.
971 */
972extern uint32_t k_timer_status_sync(struct k_timer *timer);
973
974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500976 *
977 * This routine computes the (approximate) time remaining before a running
978 * timer next expires. If the timer is not running, it returns zero.
979 *
980 * @param timer Address of timer.
981 *
982 * @return Remaining time (in milliseconds).
983 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200984static inline int32_t k_timer_remaining_get(struct k_timer *timer)
985{
986 return _timeout_remaining_get(&timer->timeout);
987}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400988
Allan Stephensc98da842016-11-11 15:45:03 -0500989/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500990 * @brief Associate user-specific data with a timer.
991 *
992 * This routine records the @a user_data with the @a timer, to be retrieved
993 * later.
994 *
995 * It can be used e.g. in a timer handler shared across multiple subsystems to
996 * retrieve data specific to the subsystem this timer is associated with.
997 *
998 * @param timer Address of timer.
999 * @param user_data User data to associate with the timer.
1000 *
1001 * @return N/A
1002 */
1003static inline void k_timer_user_data_set(struct k_timer *timer,
1004 void *user_data)
1005{
1006 timer->user_data = user_data;
1007}
1008
1009/**
1010 * @brief Retrieve the user-specific data from a timer.
1011 *
1012 * @param timer Address of timer.
1013 *
1014 * @return The user data.
1015 */
1016static inline void *k_timer_user_data_get(struct k_timer *timer)
1017{
1018 return timer->user_data;
1019}
1020
1021/**
Allan Stephensc98da842016-11-11 15:45:03 -05001022 * @} end defgroup timer_apis
1023 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001024
Allan Stephensc98da842016-11-11 15:45:03 -05001025/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001026 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001027 * @{
1028 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001029
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001030/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001031 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * This routine returns the elapsed time since the system booted,
1034 * in milliseconds.
1035 *
1036 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001037 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001038extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001039
1040/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * This routine returns the lower 32-bits of the elapsed time since the system
1044 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * This routine can be more efficient than k_uptime_get(), as it reduces the
1047 * need for interrupt locking and 64-bit math. However, the 32-bit result
1048 * cannot hold a system uptime time larger than approximately 50 days, so the
1049 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001051 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001052 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001053extern uint32_t k_uptime_get_32(void);
1054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine computes the elapsed time between the current system uptime
1059 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * @param reftime Pointer to a reference time, which is updated to the current
1062 * uptime upon return.
1063 *
1064 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001065 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001067
1068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * This routine computes the elapsed time between the current system uptime
1072 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1075 * need for interrupt locking and 64-bit math. However, the 32-bit result
1076 * cannot hold an elapsed time larger than approximately 50 days, so the
1077 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * @param reftime Pointer to a reference time, which is updated to the current
1080 * uptime upon return.
1081 *
1082 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001083 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001084extern uint32_t k_uptime_delta_32(int64_t *reftime);
1085
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * This routine returns the current time, as measured by the system's hardware
1090 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001094extern uint32_t k_cycle_get_32(void);
1095
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001097 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098 */
1099
Allan Stephensc98da842016-11-11 15:45:03 -05001100/**
1101 * @cond INTERNAL_HIDDEN
1102 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001103
1104struct k_fifo {
1105 _wait_q_t wait_q;
1106 sys_slist_t data_q;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001107 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001108
Anas Nashif2f203c22016-12-18 06:57:45 -05001109 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001110};
1111
Allan Stephensc98da842016-11-11 15:45:03 -05001112#define K_FIFO_INITIALIZER(obj) \
1113 { \
1114 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1115 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05001116 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05001117 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001118 }
1119
1120/**
1121 * INTERNAL_HIDDEN @endcond
1122 */
1123
1124/**
1125 * @defgroup fifo_apis Fifo APIs
1126 * @ingroup kernel_apis
1127 * @{
1128 */
1129
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
1137 * @return N/A
1138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001139extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140
1141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001142 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * This routine adds a data item to @a fifo. A fifo data item must be
1145 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1146 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001148 * @note Can be called by ISRs.
1149 *
1150 * @param fifo Address of the fifo.
1151 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
1153 * @return N/A
1154 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001156
1157/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001158 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001160 * This routine adds a list of data items to @a fifo in one operation.
1161 * The data items must be in a singly-linked list, with the first 32 bits
1162 * each data item pointing to the next data item; the list must be
1163 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * @param fifo Address of the fifo.
1168 * @param head Pointer to first node in singly-linked list.
1169 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
1171 * @return N/A
1172 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174
1175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * This routine adds a list of data items to @a fifo in one operation.
1179 * The data items must be in a singly-linked list implemented using a
1180 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 * and must be re-initialized via sys_slist_init().
1182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * @note Can be called by ISRs.
1184 *
1185 * @param fifo Address of the fifo.
1186 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 *
1188 * @return N/A
1189 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001190extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191
1192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * This routine removes a data item from @a fifo in a "first in, first out"
1196 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001198 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1199 *
1200 * @param fifo Address of the fifo.
1201 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 * or one of the special values K_NO_WAIT and K_FOREVER.
1203 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001204 * @return Address of the data item if successful; NULL if returned
1205 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001207extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1208
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001210 * @brief Query a fifo to see if it has data available.
1211 *
1212 * Note that the data might be already gone by the time this function returns
1213 * if other threads is also trying to read from the fifo.
1214 *
1215 * @note Can be called by ISRs.
1216 *
1217 * @param fifo Address of the fifo.
1218 *
1219 * @return Non-zero if the fifo is empty.
1220 * @return 0 if data is available.
1221 */
1222static inline int k_fifo_is_empty(struct k_fifo *fifo)
1223{
1224 return (int)sys_slist_is_empty(&fifo->data_q);
1225}
1226
1227/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001230 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001232 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001236#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001237 struct k_fifo name \
1238 __in_section(_k_fifo, static, name) = \
1239 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001240
Allan Stephensc98da842016-11-11 15:45:03 -05001241/**
1242 * @} end defgroup fifo_apis
1243 */
1244
1245/**
1246 * @cond INTERNAL_HIDDEN
1247 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001248
1249struct k_lifo {
1250 _wait_q_t wait_q;
1251 void *list;
1252
Anas Nashif2f203c22016-12-18 06:57:45 -05001253 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001254};
1255
Allan Stephensc98da842016-11-11 15:45:03 -05001256#define K_LIFO_INITIALIZER(obj) \
1257 { \
1258 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1259 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001260 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001261 }
1262
1263/**
1264 * INTERNAL_HIDDEN @endcond
1265 */
1266
1267/**
1268 * @defgroup lifo_apis Lifo APIs
1269 * @ingroup kernel_apis
1270 * @{
1271 */
1272
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001274 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001278 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 *
1280 * @return N/A
1281 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001282extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283
1284/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001285 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001287 * This routine adds a data item to @a lifo. A lifo data item must be
1288 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1289 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001291 * @note Can be called by ISRs.
1292 *
1293 * @param lifo Address of the lifo.
1294 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
1296 * @return N/A
1297 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001298extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299
1300/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * This routine removes a data item from @a lifo in a "last in, first out"
1304 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001306 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1307 *
1308 * @param lifo Address of the lifo.
1309 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 * or one of the special values K_NO_WAIT and K_FOREVER.
1311 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001312 * @return Address of the data item if successful; NULL if returned
1313 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001314 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1316
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001317/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001318 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001320 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001321 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001322 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001324 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001325 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001326#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001327 struct k_lifo name \
1328 __in_section(_k_lifo, static, name) = \
1329 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330
Allan Stephensc98da842016-11-11 15:45:03 -05001331/**
1332 * @} end defgroup lifo_apis
1333 */
1334
1335/**
1336 * @cond INTERNAL_HIDDEN
1337 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001338
1339struct k_stack {
1340 _wait_q_t wait_q;
1341 uint32_t *base, *next, *top;
1342
Anas Nashif2f203c22016-12-18 06:57:45 -05001343 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344};
1345
Allan Stephensc98da842016-11-11 15:45:03 -05001346#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1347 { \
1348 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1349 .base = stack_buffer, \
1350 .next = stack_buffer, \
1351 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001352 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001353 }
1354
1355/**
1356 * INTERNAL_HIDDEN @endcond
1357 */
1358
1359/**
1360 * @defgroup stack_apis Stack APIs
1361 * @ingroup kernel_apis
1362 * @{
1363 */
1364
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365/**
1366 * @brief Initialize a stack.
1367 *
1368 * This routine initializes a stack object, prior to its first use.
1369 *
1370 * @param stack Address of the stack.
1371 * @param buffer Address of array used to hold stacked values.
1372 * @param num_entries Maximum number of values that can be stacked.
1373 *
1374 * @return N/A
1375 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001376extern void k_stack_init(struct k_stack *stack,
1377 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378
1379/**
1380 * @brief Push an element onto a stack.
1381 *
1382 * This routine adds a 32-bit value @a data to @a stack.
1383 *
1384 * @note Can be called by ISRs.
1385 *
1386 * @param stack Address of the stack.
1387 * @param data Value to push onto the stack.
1388 *
1389 * @return N/A
1390 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001392
1393/**
1394 * @brief Pop an element from a stack.
1395 *
1396 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1397 * manner and stores the value in @a data.
1398 *
1399 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1400 *
1401 * @param stack Address of the stack.
1402 * @param data Address of area to hold the value popped from the stack.
1403 * @param timeout Waiting period to obtain a value (in milliseconds),
1404 * or one of the special values K_NO_WAIT and K_FOREVER.
1405 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001406 * @retval 0 Element popped from stack.
1407 * @retval -EBUSY Returned without waiting.
1408 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1411
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001413 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001415 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001416 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001417 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001419 * @param name Name of the stack.
1420 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001421 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001422#define K_STACK_DEFINE(name, stack_num_entries) \
1423 uint32_t __noinit \
1424 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001425 struct k_stack name \
1426 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001427 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1428 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430/**
Allan Stephensc98da842016-11-11 15:45:03 -05001431 * @} end defgroup stack_apis
1432 */
1433
Allan Stephens6bba9b02016-11-16 14:56:54 -05001434struct k_work;
1435
Allan Stephensc98da842016-11-11 15:45:03 -05001436/**
1437 * @defgroup workqueue_apis Workqueue Thread APIs
1438 * @ingroup kernel_apis
1439 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440 */
1441
Allan Stephens6bba9b02016-11-16 14:56:54 -05001442/**
1443 * @typedef k_work_handler_t
1444 * @brief Work item handler function type.
1445 *
1446 * A work item's handler function is executed by a workqueue's thread
1447 * when the work item is processed by the workqueue.
1448 *
1449 * @param work Address of the work item.
1450 *
1451 * @return N/A
1452 */
1453typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454
1455/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001456 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001457 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459struct k_work_q {
1460 struct k_fifo fifo;
1461};
1462
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001464 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465};
1466
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467struct k_work {
1468 void *_reserved; /* Used by k_fifo implementation. */
1469 k_work_handler_t handler;
1470 atomic_t flags[1];
1471};
1472
Allan Stephens6bba9b02016-11-16 14:56:54 -05001473struct k_delayed_work {
1474 struct k_work work;
1475 struct _timeout timeout;
1476 struct k_work_q *work_q;
1477};
1478
1479extern struct k_work_q k_sys_work_q;
1480
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001481/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001482 * INTERNAL_HIDDEN @endcond
1483 */
1484
1485/**
1486 * @brief Initialize a statically-defined work item.
1487 *
1488 * This macro can be used to initialize a statically-defined workqueue work
1489 * item, prior to its first use. For example,
1490 *
1491 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1492 *
1493 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001494 */
1495#define K_WORK_INITIALIZER(work_handler) \
1496 { \
1497 ._reserved = NULL, \
1498 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001499 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001500 }
1501
1502/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001503 * @brief Initialize a work item.
1504 *
1505 * This routine initializes a workqueue work item, prior to its first use.
1506 *
1507 * @param work Address of work item.
1508 * @param handler Function to invoke each time work item is processed.
1509 *
1510 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001511 */
1512static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1513{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001514 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515 work->handler = handler;
1516}
1517
1518/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001519 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001520 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001521 * This routine submits work item @a work to be processed by workqueue
1522 * @a work_q. If the work item is already pending in the workqueue's queue
1523 * as a result of an earlier submission, this routine has no effect on the
1524 * work item. If the work item has already been processed, or is currently
1525 * being processed, its work is considered complete and the work item can be
1526 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001527 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001528 * @warning
1529 * A submitted work item must not be modified until it has been processed
1530 * by the workqueue.
1531 *
1532 * @note Can be called by ISRs.
1533 *
1534 * @param work_q Address of workqueue.
1535 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001536 *
1537 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538 */
1539static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1540 struct k_work *work)
1541{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001542 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001543 k_fifo_put(&work_q->fifo, work);
1544 }
1545}
1546
1547/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001548 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001549 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001550 * This routine indicates if work item @a work is pending in a workqueue's
1551 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001552 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001553 * @note Can be called by ISRs.
1554 *
1555 * @param work Address of work item.
1556 *
1557 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001558 */
1559static inline int k_work_pending(struct k_work *work)
1560{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001561 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001562}
1563
1564/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001565 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001566 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001567 * This routine starts workqueue @a work_q. The workqueue spawns its work
1568 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001570 * @param work_q Address of workqueue.
1571 * @param stack Pointer to work queue thread's stack space.
1572 * @param stack_size Size of the work queue thread's stack (in bytes).
1573 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001574 *
1575 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001576 */
Allan Stephens904cf972016-10-07 13:59:23 -05001577extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001578 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001580/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001581 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001582 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001583 * This routine initializes a workqueue delayed work item, prior to
1584 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001585 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001586 * @param work Address of delayed work item.
1587 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588 *
1589 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001590 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001591extern void k_delayed_work_init(struct k_delayed_work *work,
1592 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001593
1594/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001595 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001596 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001597 * This routine schedules work item @a work to be processed by workqueue
1598 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1599 * an asychronous countdown for the work item and then returns to the caller.
1600 * Only when the countdown completes is the work item actually submitted to
1601 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001602 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001603 * Submitting a previously submitted delayed work item that is still
1604 * counting down cancels the existing submission and restarts the countdown
1605 * using the new delay. If the work item is currently pending on the
1606 * workqueue's queue because the countdown has completed it is too late to
1607 * resubmit the item, and resubmission fails without impacting the work item.
1608 * If the work item has already been processed, or is currently being processed,
1609 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001610 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001611 * @warning
1612 * A delayed work item must not be modified until it has been processed
1613 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001614 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001615 * @note Can be called by ISRs.
1616 *
1617 * @param work_q Address of workqueue.
1618 * @param work Address of delayed work item.
1619 * @param delay Delay before submitting the work item (in milliseconds).
1620 *
1621 * @retval 0 Work item countdown started.
1622 * @retval -EINPROGRESS Work item is already pending.
1623 * @retval -EINVAL Work item is being processed or has completed its work.
1624 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001626extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1627 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001628 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629
1630/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001631 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001633 * This routine cancels the submission of delayed work item @a work.
1634 * A delayed work item can only be cancelled while its countdown is still
1635 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001636 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001637 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001638 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001639 * @param work Address of delayed work item.
1640 *
1641 * @retval 0 Work item countdown cancelled.
1642 * @retval -EINPROGRESS Work item is already pending.
1643 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001644 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001645extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001646
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001647/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648 * @brief Submit a work item to the system workqueue.
1649 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001650 * This routine submits work item @a work to be processed by the system
1651 * workqueue. If the work item is already pending in the workqueue's queue
1652 * as a result of an earlier submission, this routine has no effect on the
1653 * work item. If the work item has already been processed, or is currently
1654 * being processed, its work is considered complete and the work item can be
1655 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001657 * @warning
1658 * Work items submitted to the system workqueue should avoid using handlers
1659 * that block or yield since this may prevent the system workqueue from
1660 * processing other work items in a timely manner.
1661 *
1662 * @note Can be called by ISRs.
1663 *
1664 * @param work Address of work item.
1665 *
1666 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001667 */
1668static inline void k_work_submit(struct k_work *work)
1669{
1670 k_work_submit_to_queue(&k_sys_work_q, work);
1671}
1672
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001673/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001674 * @brief Submit a delayed work item to the system workqueue.
1675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001676 * This routine schedules work item @a work to be processed by the system
1677 * workqueue after a delay of @a delay milliseconds. The routine initiates
1678 * an asychronous countdown for the work item and then returns to the caller.
1679 * Only when the countdown completes is the work item actually submitted to
1680 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001681 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001682 * Submitting a previously submitted delayed work item that is still
1683 * counting down cancels the existing submission and restarts the countdown
1684 * using the new delay. If the work item is currently pending on the
1685 * workqueue's queue because the countdown has completed it is too late to
1686 * resubmit the item, and resubmission fails without impacting the work item.
1687 * If the work item has already been processed, or is currently being processed,
1688 * its work is considered complete and the work item can be resubmitted.
1689 *
1690 * @warning
1691 * Work items submitted to the system workqueue should avoid using handlers
1692 * that block or yield since this may prevent the system workqueue from
1693 * processing other work items in a timely manner.
1694 *
1695 * @note Can be called by ISRs.
1696 *
1697 * @param work Address of delayed work item.
1698 * @param delay Delay before submitting the work item (in milliseconds).
1699 *
1700 * @retval 0 Work item countdown started.
1701 * @retval -EINPROGRESS Work item is already pending.
1702 * @retval -EINVAL Work item is being processed or has completed its work.
1703 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001704 */
1705static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001706 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001708 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001709}
1710
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001711/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001712 * @brief Get time remaining before a delayed work gets scheduled.
1713 *
1714 * This routine computes the (approximate) time remaining before a
1715 * delayed work gets executed. If the delayed work is not waiting to be
1716 * schedules, it returns zero.
1717 *
1718 * @param work Delayed work item.
1719 *
1720 * @return Remaining time (in milliseconds).
1721 */
1722static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1723{
1724 return _timeout_remaining_get(&work->timeout);
1725}
1726
1727/**
Allan Stephensc98da842016-11-11 15:45:03 -05001728 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001729 */
1730
Allan Stephensc98da842016-11-11 15:45:03 -05001731/**
1732 * @cond INTERNAL_HIDDEN
1733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734
1735struct k_mutex {
1736 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001737 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001738 uint32_t lock_count;
1739 int owner_orig_prio;
1740#ifdef CONFIG_OBJECT_MONITOR
1741 int num_lock_state_changes;
1742 int num_conflicts;
1743#endif
1744
Anas Nashif2f203c22016-12-18 06:57:45 -05001745 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746};
1747
1748#ifdef CONFIG_OBJECT_MONITOR
1749#define _MUTEX_INIT_OBJECT_MONITOR \
1750 .num_lock_state_changes = 0, .num_conflicts = 0,
1751#else
1752#define _MUTEX_INIT_OBJECT_MONITOR
1753#endif
1754
1755#define K_MUTEX_INITIALIZER(obj) \
1756 { \
1757 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1758 .owner = NULL, \
1759 .lock_count = 0, \
1760 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1761 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001762 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001763 }
1764
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765/**
Allan Stephensc98da842016-11-11 15:45:03 -05001766 * INTERNAL_HIDDEN @endcond
1767 */
1768
1769/**
1770 * @defgroup mutex_apis Mutex APIs
1771 * @ingroup kernel_apis
1772 * @{
1773 */
1774
1775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001780 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001782 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001784#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001785 struct k_mutex name \
1786 __in_section(_k_mutex, static, name) = \
1787 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001788
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001789/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001792 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * Upon completion, the mutex is available and does not have an owner.
1795 *
1796 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797 *
1798 * @return N/A
1799 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001800extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801
1802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001805 * This routine locks @a mutex. If the mutex is locked by another thread,
1806 * the calling thread waits until the mutex becomes available or until
1807 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001809 * A thread is permitted to lock a mutex it has already locked. The operation
1810 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * @param mutex Address of the mutex.
1813 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 * or one of the special values K_NO_WAIT and K_FOREVER.
1815 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001816 * @retval 0 Mutex locked.
1817 * @retval -EBUSY Returned without waiting.
1818 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001820extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821
1822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * This routine unlocks @a mutex. The mutex must already be locked by the
1826 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
1828 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001829 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001830 * thread.
1831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833 *
1834 * @return N/A
1835 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001836extern void k_mutex_unlock(struct k_mutex *mutex);
1837
Allan Stephensc98da842016-11-11 15:45:03 -05001838/**
1839 * @} end defgroup mutex_apis
1840 */
1841
1842/**
1843 * @cond INTERNAL_HIDDEN
1844 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001845
1846struct k_sem {
1847 _wait_q_t wait_q;
1848 unsigned int count;
1849 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001850 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001851
Anas Nashif2f203c22016-12-18 06:57:45 -05001852 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001853};
1854
Allan Stephensc98da842016-11-11 15:45:03 -05001855#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1856 { \
1857 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1858 .count = initial_count, \
1859 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05001860 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05001861 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001862 }
1863
1864/**
1865 * INTERNAL_HIDDEN @endcond
1866 */
1867
1868/**
1869 * @defgroup semaphore_apis Semaphore APIs
1870 * @ingroup kernel_apis
1871 * @{
1872 */
1873
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001879 * @param sem Address of the semaphore.
1880 * @param initial_count Initial semaphore count.
1881 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001882 *
1883 * @return N/A
1884 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001885extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1886 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001887
1888/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1894 *
1895 * @param sem Address of the semaphore.
1896 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001897 * or one of the special values K_NO_WAIT and K_FOREVER.
1898 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001899 * @note When porting code from the nanokernel legacy API to the new API, be
1900 * careful with the return value of this function. The return value is the
1901 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1902 * non-zero means failure, while the nano_sem_take family returns 1 for success
1903 * and 0 for failure.
1904 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001905 * @retval 0 Semaphore taken.
1906 * @retval -EBUSY Returned without waiting.
1907 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001908 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001910
1911/**
1912 * @brief Give a semaphore.
1913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001914 * This routine gives @a sem, unless the semaphore is already at its maximum
1915 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001920 *
1921 * @return N/A
1922 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923extern void k_sem_give(struct k_sem *sem);
1924
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001925/**
1926 * @brief Reset a semaphore's count to zero.
1927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001928 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001931 *
1932 * @return N/A
1933 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001934static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935{
1936 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937}
1938
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001939/**
1940 * @brief Get a semaphore's count.
1941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001942 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001944 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001947 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001948static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949{
1950 return sem->count;
1951}
1952
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001957 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001958 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001960 * @param name Name of the semaphore.
1961 * @param initial_count Initial semaphore count.
1962 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001963 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001965 struct k_sem name \
1966 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967 K_SEM_INITIALIZER(name, initial_count, count_limit)
1968
Allan Stephensc98da842016-11-11 15:45:03 -05001969/**
1970 * @} end defgroup semaphore_apis
1971 */
1972
1973/**
1974 * @defgroup alert_apis Alert APIs
1975 * @ingroup kernel_apis
1976 * @{
1977 */
1978
Allan Stephens5eceb852016-11-16 10:16:30 -05001979/**
1980 * @typedef k_alert_handler_t
1981 * @brief Alert handler function type.
1982 *
1983 * An alert's alert handler function is invoked by the system workqueue
1984 * when the alert is signalled. The alert handler function is optional,
1985 * and is only invoked if the alert has been initialized with one.
1986 *
1987 * @param alert Address of the alert.
1988 *
1989 * @return 0 if alert has been consumed; non-zero if alert should pend.
1990 */
1991typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001992
1993/**
1994 * @} end defgroup alert_apis
1995 */
1996
1997/**
1998 * @cond INTERNAL_HIDDEN
1999 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002000
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002001#define K_ALERT_DEFAULT NULL
2002#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002004struct k_alert {
2005 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002006 atomic_t send_count;
2007 struct k_work work_item;
2008 struct k_sem sem;
2009
Anas Nashif2f203c22016-12-18 06:57:45 -05002010 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002011};
2012
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002013extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002015#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002016 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002017 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002019 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002020 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002021 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002022 }
2023
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024/**
Allan Stephensc98da842016-11-11 15:45:03 -05002025 * INTERNAL_HIDDEN @endcond
2026 */
2027
2028/**
2029 * @addtogroup alert_apis
2030 * @{
2031 */
2032
2033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002036 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002038 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002040 * @param name Name of the alert.
2041 * @param alert_handler Action to take when alert is sent. Specify either
2042 * the address of a function to be invoked by the system workqueue
2043 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2044 * K_ALERT_DEFAULT (which causes the alert to pend).
2045 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002047#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002048 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002049 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002050 K_ALERT_INITIALIZER(name, alert_handler, \
2051 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002052
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @param alert Address of the alert.
2059 * @param handler Action to take when alert is sent. Specify either the address
2060 * of a function to be invoked by the system workqueue thread,
2061 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2062 * K_ALERT_DEFAULT (which causes the alert to pend).
2063 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
2065 * @return N/A
2066 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002067extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2068 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069
2070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2076 *
2077 * @param alert Address of the alert.
2078 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 * or one of the special values K_NO_WAIT and K_FOREVER.
2080 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002081 * @retval 0 Alert received.
2082 * @retval -EBUSY Returned without waiting.
2083 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002085extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086
2087/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * This routine signals @a alert. The action specified for @a alert will
2091 * be taken, which may trigger the execution of an alert handler function
2092 * and/or cause the alert to pend (assuming the alert has not reached its
2093 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * @note Can be called by ISRs.
2096 *
2097 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
2099 * @return N/A
2100 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002101extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102
2103/**
Allan Stephensc98da842016-11-11 15:45:03 -05002104 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002105 */
2106
Allan Stephensc98da842016-11-11 15:45:03 -05002107/**
2108 * @cond INTERNAL_HIDDEN
2109 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110
2111struct k_msgq {
2112 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002113 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114 uint32_t max_msgs;
2115 char *buffer_start;
2116 char *buffer_end;
2117 char *read_ptr;
2118 char *write_ptr;
2119 uint32_t used_msgs;
2120
Anas Nashif2f203c22016-12-18 06:57:45 -05002121 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122};
2123
Peter Mitsis1da807e2016-10-06 11:36:59 -04002124#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125 { \
2126 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002127 .max_msgs = q_max_msgs, \
2128 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002130 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131 .read_ptr = q_buffer, \
2132 .write_ptr = q_buffer, \
2133 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002134 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135 }
2136
Peter Mitsis1da807e2016-10-06 11:36:59 -04002137/**
Allan Stephensc98da842016-11-11 15:45:03 -05002138 * INTERNAL_HIDDEN @endcond
2139 */
2140
2141/**
2142 * @defgroup msgq_apis Message Queue APIs
2143 * @ingroup kernel_apis
2144 * @{
2145 */
2146
2147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2151 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002152 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2153 * message is similarly aligned to this boundary, @a q_msg_size must also be
2154 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002156 * The message queue can be accessed outside the module where it is defined
2157 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002159 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 * @param q_name Name of the message queue.
2162 * @param q_msg_size Message size (in bytes).
2163 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002164 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002165 */
2166#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2167 static char __noinit __aligned(q_align) \
2168 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002169 struct k_msgq q_name \
2170 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002171 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2172 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173
Peter Mitsisd7a37502016-10-13 11:37:40 -04002174/**
2175 * @brief Initialize a message queue.
2176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002177 * This routine initializes a message queue object, prior to its first use.
2178 *
Allan Stephensda827222016-11-09 14:23:58 -06002179 * The message queue's ring buffer must contain space for @a max_msgs messages,
2180 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2181 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2182 * that each message is similarly aligned to this boundary, @a q_msg_size
2183 * must also be a multiple of N.
2184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @param q Address of the message queue.
2186 * @param buffer Pointer to ring buffer that holds queued messages.
2187 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002188 * @param max_msgs Maximum number of messages that can be queued.
2189 *
2190 * @return N/A
2191 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002192extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002193 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002194
2195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002198 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002199 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002200 * @note Can be called by ISRs.
2201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * @param q Address of the message queue.
2203 * @param data Pointer to the message.
2204 * @param timeout Waiting period to add the message (in milliseconds),
2205 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002206 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002207 * @retval 0 Message sent.
2208 * @retval -ENOMSG Returned without waiting or queue purged.
2209 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002212
2213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002214 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002216 * This routine receives a message from message queue @a q in a "first in,
2217 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002218 *
Allan Stephensc98da842016-11-11 15:45:03 -05002219 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002221 * @param q Address of the message queue.
2222 * @param data Address of area to hold the received message.
2223 * @param timeout Waiting period to receive the message (in milliseconds),
2224 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002225 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002226 * @retval 0 Message received.
2227 * @retval -ENOMSG Returned without waiting.
2228 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002229 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002231
2232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * This routine discards all unreceived messages in a message queue's ring
2236 * buffer. Any threads that are blocked waiting to send a message to the
2237 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002239 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002240 *
2241 * @return N/A
2242 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243extern void k_msgq_purge(struct k_msgq *q);
2244
Peter Mitsis67be2492016-10-07 11:44:34 -04002245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002246 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * This routine returns the number of unused entries in a message queue's
2249 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002251 * @param q Address of the message queue.
2252 *
2253 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002254 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002255static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002256{
2257 return q->max_msgs - q->used_msgs;
2258}
2259
Peter Mitsisd7a37502016-10-13 11:37:40 -04002260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002263 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * @param q Address of the message queue.
2266 *
2267 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002268 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002269static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270{
2271 return q->used_msgs;
2272}
2273
Allan Stephensc98da842016-11-11 15:45:03 -05002274/**
2275 * @} end defgroup msgq_apis
2276 */
2277
2278/**
2279 * @defgroup mem_pool_apis Memory Pool APIs
2280 * @ingroup kernel_apis
2281 * @{
2282 */
2283
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002285 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002286 void *addr_in_pool;
2287 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002288 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289};
2290
Allan Stephensc98da842016-11-11 15:45:03 -05002291/**
2292 * @} end defgroup mem_pool_apis
2293 */
2294
2295/**
2296 * @defgroup mailbox_apis Mailbox APIs
2297 * @ingroup kernel_apis
2298 * @{
2299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300
2301struct k_mbox_msg {
2302 /** internal use only - needed for legacy API support */
2303 uint32_t _mailbox;
2304 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002305 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 /** application-defined information value */
2307 uint32_t info;
2308 /** sender's message data buffer */
2309 void *tx_data;
2310 /** internal use only - needed for legacy API support */
2311 void *_rx_data;
2312 /** message data block descriptor */
2313 struct k_mem_block tx_block;
2314 /** source thread id */
2315 k_tid_t rx_source_thread;
2316 /** target thread id */
2317 k_tid_t tx_target_thread;
2318 /** internal use only - thread waiting on send (may be a dummy) */
2319 k_tid_t _syncing_thread;
2320#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2321 /** internal use only - semaphore used during asynchronous send */
2322 struct k_sem *_async_sem;
2323#endif
2324};
2325
Allan Stephensc98da842016-11-11 15:45:03 -05002326/**
2327 * @cond INTERNAL_HIDDEN
2328 */
2329
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330struct k_mbox {
2331 _wait_q_t tx_msg_queue;
2332 _wait_q_t rx_msg_queue;
2333
Anas Nashif2f203c22016-12-18 06:57:45 -05002334 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335};
2336
2337#define K_MBOX_INITIALIZER(obj) \
2338 { \
2339 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2340 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002341 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 }
2343
Peter Mitsis12092702016-10-14 12:57:23 -04002344/**
Allan Stephensc98da842016-11-11 15:45:03 -05002345 * INTERNAL_HIDDEN @endcond
2346 */
2347
2348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002353 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002358 struct k_mbox name \
2359 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 K_MBOX_INITIALIZER(name) \
2361
Peter Mitsis12092702016-10-14 12:57:23 -04002362/**
2363 * @brief Initialize a mailbox.
2364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * This routine initializes a mailbox object, prior to its first use.
2366 *
2367 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002368 *
2369 * @return N/A
2370 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371extern void k_mbox_init(struct k_mbox *mbox);
2372
Peter Mitsis12092702016-10-14 12:57:23 -04002373/**
2374 * @brief Send a mailbox message in a synchronous manner.
2375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 * This routine sends a message to @a mbox and waits for a receiver to both
2377 * receive and process it. The message data may be in a buffer, in a memory
2378 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @param mbox Address of the mailbox.
2381 * @param tx_msg Address of the transmit message descriptor.
2382 * @param timeout Waiting period for the message to be received (in
2383 * milliseconds), or one of the special values K_NO_WAIT
2384 * and K_FOREVER. Once the message has been received,
2385 * this routine waits as long as necessary for the message
2386 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002387 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002388 * @retval 0 Message sent.
2389 * @retval -ENOMSG Returned without waiting.
2390 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002391 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002392extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002394
Peter Mitsis12092702016-10-14 12:57:23 -04002395/**
2396 * @brief Send a mailbox message in an asynchronous manner.
2397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * This routine sends a message to @a mbox without waiting for a receiver
2399 * to process it. The message data may be in a buffer, in a memory pool block,
2400 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2401 * will be given when the message has been both received and completely
2402 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @param mbox Address of the mailbox.
2405 * @param tx_msg Address of the transmit message descriptor.
2406 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002407 *
2408 * @return N/A
2409 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002410extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411 struct k_sem *sem);
2412
Peter Mitsis12092702016-10-14 12:57:23 -04002413/**
2414 * @brief Receive a mailbox message.
2415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * This routine receives a message from @a mbox, then optionally retrieves
2417 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * @param mbox Address of the mailbox.
2420 * @param rx_msg Address of the receive message descriptor.
2421 * @param buffer Address of the buffer to receive data, or NULL to defer data
2422 * retrieval and message disposal until later.
2423 * @param timeout Waiting period for a message to be received (in
2424 * milliseconds), or one of the special values K_NO_WAIT
2425 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002426 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002427 * @retval 0 Message received.
2428 * @retval -ENOMSG Returned without waiting.
2429 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002430 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002431extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002433
2434/**
2435 * @brief Retrieve mailbox message data into a buffer.
2436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * This routine completes the processing of a received message by retrieving
2438 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002439 *
2440 * Alternatively, this routine can be used to dispose of a received message
2441 * without retrieving its data.
2442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * @param rx_msg Address of the receive message descriptor.
2444 * @param buffer Address of the buffer to receive data, or NULL to discard
2445 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002446 *
2447 * @return N/A
2448 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002449extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002450
2451/**
2452 * @brief Retrieve mailbox message data into a memory pool block.
2453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454 * This routine completes the processing of a received message by retrieving
2455 * its data into a memory pool block, then disposing of the message.
2456 * The memory pool block that results from successful retrieval must be
2457 * returned to the pool once the data has been processed, even in cases
2458 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002459 *
2460 * Alternatively, this routine can be used to dispose of a received message
2461 * without retrieving its data. In this case there is no need to return a
2462 * memory pool block to the pool.
2463 *
2464 * This routine allocates a new memory pool block for the data only if the
2465 * data is not already in one. If a new block cannot be allocated, the routine
2466 * returns a failure code and the received message is left unchanged. This
2467 * permits the caller to reattempt data retrieval at a later time or to dispose
2468 * of the received message without retrieving its data.
2469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @param rx_msg Address of a receive message descriptor.
2471 * @param pool Address of memory pool, or NULL to discard data.
2472 * @param block Address of the area to hold memory pool block info.
2473 * @param timeout Waiting period to wait for a memory pool block (in
2474 * milliseconds), or one of the special values K_NO_WAIT
2475 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002476 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002477 * @retval 0 Data retrieved.
2478 * @retval -ENOMEM Returned without waiting.
2479 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002480 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002481extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002482 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483 struct k_mem_block *block, int32_t timeout);
2484
Allan Stephensc98da842016-11-11 15:45:03 -05002485/**
2486 * @} end defgroup mailbox_apis
2487 */
2488
2489/**
2490 * @cond INTERNAL_HIDDEN
2491 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002492
2493struct k_pipe {
2494 unsigned char *buffer; /* Pipe buffer: may be NULL */
2495 size_t size; /* Buffer size */
2496 size_t bytes_used; /* # bytes used in buffer */
2497 size_t read_index; /* Where in buffer to read from */
2498 size_t write_index; /* Where in buffer to write */
2499
2500 struct {
2501 _wait_q_t readers; /* Reader wait queue */
2502 _wait_q_t writers; /* Writer wait queue */
2503 } wait_q;
2504
Anas Nashif2f203c22016-12-18 06:57:45 -05002505 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506};
2507
Peter Mitsise5d9c582016-10-14 14:44:57 -04002508#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509 { \
2510 .buffer = pipe_buffer, \
2511 .size = pipe_buffer_size, \
2512 .bytes_used = 0, \
2513 .read_index = 0, \
2514 .write_index = 0, \
2515 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2516 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002517 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002518 }
2519
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520/**
Allan Stephensc98da842016-11-11 15:45:03 -05002521 * INTERNAL_HIDDEN @endcond
2522 */
2523
2524/**
2525 * @defgroup pipe_apis Pipe APIs
2526 * @ingroup kernel_apis
2527 * @{
2528 */
2529
2530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002534 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002535 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @param name Name of the pipe.
2538 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2539 * or zero if no ring buffer is used.
2540 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002542#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2543 static unsigned char __noinit __aligned(pipe_align) \
2544 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002545 struct k_pipe name \
2546 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002547 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002552 * This routine initializes a pipe object, prior to its first use.
2553 *
2554 * @param pipe Address of the pipe.
2555 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2556 * is used.
2557 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2558 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 *
2560 * @return N/A
2561 */
2562extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2563 size_t size);
2564
2565/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002566 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * @param pipe Address of the pipe.
2571 * @param data Address of data to write.
2572 * @param bytes_to_write Size of data (in bytes).
2573 * @param bytes_written Address of area to hold the number of bytes written.
2574 * @param min_xfer Minimum number of bytes to write.
2575 * @param timeout Waiting period to wait for the data to be written (in
2576 * milliseconds), or one of the special values K_NO_WAIT
2577 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002579 * @retval 0 At least @a min_xfer bytes of data were written.
2580 * @retval -EIO Returned without waiting; zero data bytes were written.
2581 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002584extern int k_pipe_put(struct k_pipe *pipe, void *data,
2585 size_t bytes_to_write, size_t *bytes_written,
2586 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587
2588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * @param pipe Address of the pipe.
2594 * @param data Address to place the data read from pipe.
2595 * @param bytes_to_read Maximum number of data bytes to read.
2596 * @param bytes_read Address of area to hold the number of bytes read.
2597 * @param min_xfer Minimum number of data bytes to read.
2598 * @param timeout Waiting period to wait for the data to be read (in
2599 * milliseconds), or one of the special values K_NO_WAIT
2600 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002602 * @retval 0 At least @a min_xfer bytes of data were read.
2603 * @retval -EIO Returned without waiting; zero data bytes were read.
2604 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002606 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002607extern int k_pipe_get(struct k_pipe *pipe, void *data,
2608 size_t bytes_to_read, size_t *bytes_read,
2609 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610
2611/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002614 * This routine writes the data contained in a memory block to @a pipe.
2615 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002619 * @param block Memory block containing data to send
2620 * @param size Number of data bytes in memory block to send
2621 * @param sem Semaphore to signal upon completion (else NULL)
2622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002623 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624 */
2625extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2626 size_t size, struct k_sem *sem);
2627
2628/**
Allan Stephensc98da842016-11-11 15:45:03 -05002629 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630 */
2631
Allan Stephensc98da842016-11-11 15:45:03 -05002632/**
2633 * @cond INTERNAL_HIDDEN
2634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002636struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002638 uint32_t num_blocks;
2639 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640 char *buffer;
2641 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002642 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643
Anas Nashif2f203c22016-12-18 06:57:45 -05002644 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645};
2646
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002647#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2648 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649 { \
2650 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002651 .num_blocks = slab_num_blocks, \
2652 .block_size = slab_block_size, \
2653 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654 .free_list = NULL, \
2655 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002656 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657 }
2658
Peter Mitsis578f9112016-10-07 13:50:31 -04002659/**
Allan Stephensc98da842016-11-11 15:45:03 -05002660 * INTERNAL_HIDDEN @endcond
2661 */
2662
2663/**
2664 * @defgroup mem_slab_apis Memory Slab APIs
2665 * @ingroup kernel_apis
2666 * @{
2667 */
2668
2669/**
Allan Stephensda827222016-11-09 14:23:58 -06002670 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002671 *
Allan Stephensda827222016-11-09 14:23:58 -06002672 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002674 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2675 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002676 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002677 *
Allan Stephensda827222016-11-09 14:23:58 -06002678 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002680 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002681 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @param name Name of the memory slab.
2684 * @param slab_block_size Size of each memory block (in bytes).
2685 * @param slab_num_blocks Number memory blocks.
2686 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002687 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002688#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2689 char __noinit __aligned(slab_align) \
2690 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2691 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002692 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002693 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2694 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002696/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002697 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002700 *
Allan Stephensda827222016-11-09 14:23:58 -06002701 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2702 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2703 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2704 * To ensure that each memory block is similarly aligned to this boundary,
2705 * @a slab_block_size must also be a multiple of N.
2706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @param slab Address of the memory slab.
2708 * @param buffer Pointer to buffer used for the memory blocks.
2709 * @param block_size Size of each memory block (in bytes).
2710 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002711 *
2712 * @return N/A
2713 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002714extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002715 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002716
2717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @param slab Address of the memory slab.
2723 * @param mem Pointer to block address area.
2724 * @param timeout Maximum time to wait for operation to complete
2725 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2726 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002727 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002728 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002730 * @retval -ENOMEM Returned without waiting.
2731 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002732 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002733extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2734 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002735
2736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * This routine releases a previously allocated memory block back to its
2740 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * @param slab Address of the memory slab.
2743 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002744 *
2745 * @return N/A
2746 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002747extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * This routine gets the number of memory blocks that are currently
2753 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002758 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002759static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002761 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762}
2763
Peter Mitsisc001aa82016-10-13 13:53:37 -04002764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * This routine gets the number of memory blocks that are currently
2768 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002772 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002773 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002774static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002775{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002776 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002777}
2778
Allan Stephensc98da842016-11-11 15:45:03 -05002779/**
2780 * @} end defgroup mem_slab_apis
2781 */
2782
2783/**
2784 * @cond INTERNAL_HIDDEN
2785 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002787/*
2788 * Memory pool requires a buffer and two arrays of structures for the
2789 * memory block accounting:
2790 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2791 * status of four blocks of memory.
2792 */
2793struct k_mem_pool_quad_block {
2794 char *mem_blocks; /* pointer to the first of four memory blocks */
2795 uint32_t mem_status; /* four bits. If bit is set, memory block is
2796 allocated */
2797};
2798/*
2799 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2800 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2801 * block size is 4 times less than the previous one and thus requires 4 times
2802 * bigger array of k_mem_pool_quad_block structures to keep track of the
2803 * memory blocks.
2804 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002806/*
2807 * The array of k_mem_pool_block_set keeps the information of each array of
2808 * k_mem_pool_quad_block structures
2809 */
2810struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002811 size_t block_size; /* memory block size */
2812 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002813 struct k_mem_pool_quad_block *quad_block;
2814 int count;
2815};
2816
2817/* Memory pool descriptor */
2818struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002819 size_t max_block_size;
2820 size_t min_block_size;
2821 uint32_t nr_of_maxblocks;
2822 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002823 struct k_mem_pool_block_set *block_set;
2824 char *bufblock;
2825 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002826 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827};
2828
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002829#ifdef CONFIG_ARM
2830#define _SECTION_TYPE_SIGN "%"
2831#else
2832#define _SECTION_TYPE_SIGN "@"
2833#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002835/*
2836 * Static memory pool initialization
2837 */
Allan Stephensc98da842016-11-11 15:45:03 -05002838
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002839/*
2840 * Use .altmacro to be able to recalculate values and pass them as string
2841 * arguments when calling assembler macros resursively
2842 */
2843__asm__(".altmacro\n\t");
2844
2845/*
2846 * Recursively calls a macro
2847 * The followig global symbols need to be initialized:
2848 * __memory_pool_max_block_size - maximal size of the memory block
2849 * __memory_pool_min_block_size - minimal size of the memory block
2850 * Notes:
2851 * Global symbols are used due the fact that assembler macro allows only
2852 * one argument be passed with the % conversion
2853 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2854 * is used instead of / 4.
2855 * n_max argument needs to go first in the invoked macro, as some
2856 * assemblers concatenate \name and %(\n_max * 4) arguments
2857 * if \name goes first
2858 */
2859__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2860 ".ifge __memory_pool_max_block_size >> 2 -"
2861 " __memory_pool_min_block_size\n\t\t"
2862 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2863 "\\macro_name %(\\n_max * 4) \\name\n\t"
2864 ".endif\n\t"
2865 ".endm\n");
2866
2867/*
2868 * Build quad blocks
2869 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2870 * structures and recursively calls itself for the next array, 4 times
2871 * larger.
2872 * The followig global symbols need to be initialized:
2873 * __memory_pool_max_block_size - maximal size of the memory block
2874 * __memory_pool_min_block_size - minimal size of the memory block
2875 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2876 */
2877__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002878 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002879 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2880 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2881 ".if \\n_max % 4\n\t\t"
2882 ".skip __memory_pool_quad_block_size\n\t"
2883 ".endif\n\t"
2884 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2885 ".endm\n");
2886
2887/*
2888 * Build block sets and initialize them
2889 * Macro initializes the k_mem_pool_block_set structure and
2890 * recursively calls itself for the next one.
2891 * The followig global symbols need to be initialized:
2892 * __memory_pool_max_block_size - maximal size of the memory block
2893 * __memory_pool_min_block_size - minimal size of the memory block
2894 * __memory_pool_block_set_count, the number of the elements in the
2895 * block set array must be set to 0. Macro calculates it's real
2896 * value.
2897 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2898 * structures, _build_quad_blocks must be called prior it.
2899 */
2900__asm__(".macro _build_block_set n_max, name\n\t"
2901 ".int __memory_pool_max_block_size\n\t" /* block_size */
2902 ".if \\n_max % 4\n\t\t"
2903 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2904 ".else\n\t\t"
2905 ".int \\n_max >> 2\n\t"
2906 ".endif\n\t"
2907 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2908 ".int 0\n\t" /* count */
2909 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2910 "__do_recurse _build_block_set \\name \\n_max\n\t"
2911 ".endm\n");
2912
2913/*
2914 * Build a memory pool structure and initialize it
2915 * Macro uses __memory_pool_block_set_count global symbol,
2916 * block set addresses and buffer address, it may be called only after
2917 * _build_block_set
2918 */
2919__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002920 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002921 _SECTION_TYPE_SIGN "progbits\n\t"
2922 ".globl \\name\n\t"
2923 "\\name:\n\t"
2924 ".int \\max_size\n\t" /* max_block_size */
2925 ".int \\min_size\n\t" /* min_block_size */
2926 ".int \\n_max\n\t" /* nr_of_maxblocks */
2927 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2928 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2929 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2930 ".int 0\n\t" /* wait_q->head */
2931 ".int 0\n\t" /* wait_q->next */
2932 ".popsection\n\t"
2933 ".endm\n");
2934
2935#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2936 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2937 _SECTION_TYPE_SIGN "progbits\n\t"); \
2938 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2939 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2940 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2941 STRINGIFY(name) "\n\t"); \
2942 __asm__(".popsection\n\t")
2943
2944#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2945 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2946 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2947 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2948 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002949 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002950 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2951 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2952 STRINGIFY(name) "\n\t"); \
2953 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2954 __asm__(".int __memory_pool_block_set_count\n\t"); \
2955 __asm__(".popsection\n\t"); \
2956 extern uint32_t _mem_pool_block_set_count_##name; \
2957 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2958
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002959#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2960 char __noinit __aligned(align) \
2961 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002962
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002963/*
2964 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2965 * to __memory_pool_quad_block_size absolute symbol.
2966 * This function does not get called, but compiler calculates the value and
2967 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2968 */
2969static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2970{
2971 __asm__(".globl __memory_pool_quad_block_size\n\t"
2972#ifdef CONFIG_NIOS2
2973 "__memory_pool_quad_block_size = %0\n\t"
2974#else
2975 "__memory_pool_quad_block_size = %c0\n\t"
2976#endif
2977 :
2978 : "n"(sizeof(struct k_mem_pool_quad_block)));
2979}
2980
2981/**
Allan Stephensc98da842016-11-11 15:45:03 -05002982 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002983 */
2984
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002985/**
Allan Stephensc98da842016-11-11 15:45:03 -05002986 * @addtogroup mem_pool_apis
2987 * @{
2988 */
2989
2990/**
2991 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2994 * long. The memory pool allows blocks to be repeatedly partitioned into
2995 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2996 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002997 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002999 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003000 * If the pool is to be accessed outside the module where it is defined, it
3001 * can be declared via
3002 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003003 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * @param name Name of the memory pool.
3006 * @param min_size Size of the smallest blocks in the pool (in bytes).
3007 * @param max_size Size of the largest blocks in the pool (in bytes).
3008 * @param n_max Number of maximum sized blocks in the pool.
3009 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003010 */
3011#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003012 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3013 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003014 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003015 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3016 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3017 extern struct k_mem_pool name
3018
Peter Mitsis937042c2016-10-13 13:18:26 -04003019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * @param pool Address of the memory pool.
3025 * @param block Pointer to block descriptor for the allocated memory.
3026 * @param size Amount of memory to allocate (in bytes).
3027 * @param timeout Maximum time to wait for operation to complete
3028 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3029 * or K_FOREVER to wait as long as necessary.
3030 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003031 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003033 * @retval -ENOMEM Returned without waiting.
3034 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003035 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003036extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003037 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003038
3039/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003040 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003042 * This routine releases a previously allocated memory block back to its
3043 * memory pool.
3044 *
3045 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003046 *
3047 * @return N/A
3048 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003050
3051/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * This routine instructs a memory pool to concatenate unused memory blocks
3055 * into larger blocks wherever possible. Manually defragmenting the memory
3056 * pool may speed up future allocations of memory blocks by eliminating the
3057 * need for the memory pool to perform an automatic partial defragmentation.
3058 *
3059 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003060 *
3061 * @return N/A
3062 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003063extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003064
3065/**
Allan Stephensc98da842016-11-11 15:45:03 -05003066 * @} end addtogroup mem_pool_apis
3067 */
3068
3069/**
3070 * @defgroup heap_apis Heap Memory Pool APIs
3071 * @ingroup kernel_apis
3072 * @{
3073 */
3074
3075/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003078 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003079 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003084 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003085extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003086
3087/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003088 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003089 *
3090 * This routine provides traditional free() semantics. The memory being
3091 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003092 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003093 * If @a ptr is NULL, no operation is performed.
3094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003096 *
3097 * @return N/A
3098 */
3099extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100
Allan Stephensc98da842016-11-11 15:45:03 -05003101/**
3102 * @} end defgroup heap_apis
3103 */
3104
Benjamin Walshacc68c12017-01-29 18:57:45 -05003105/* polling API - PRIVATE */
3106
3107/* private - implementation data created as needed, per-type */
3108struct _poller {
3109 struct k_thread *thread;
3110};
3111
3112/* private - types bit positions */
3113enum _poll_types_bits {
3114 /* can be used to ignore an event */
3115 _POLL_TYPE_IGNORE,
3116
3117 /* to be signaled by k_poll_signal() */
3118 _POLL_TYPE_SIGNAL,
3119
3120 /* semaphore availability */
3121 _POLL_TYPE_SEM_AVAILABLE,
3122
3123 /* fifo data availability */
3124 _POLL_TYPE_FIFO_DATA_AVAILABLE,
3125
3126 _POLL_NUM_TYPES
3127};
3128
3129#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3130
3131/* private - states bit positions */
3132enum _poll_states_bits {
3133 /* default state when creating event */
3134 _POLL_STATE_NOT_READY,
3135
3136 /* there was another poller already on the object */
3137 _POLL_STATE_EADDRINUSE,
3138
3139 /* signaled by k_poll_signal() */
3140 _POLL_STATE_SIGNALED,
3141
3142 /* semaphore is available */
3143 _POLL_STATE_SEM_AVAILABLE,
3144
3145 /* data is available to read on fifo */
3146 _POLL_STATE_FIFO_DATA_AVAILABLE,
3147
3148 _POLL_NUM_STATES
3149};
3150
3151#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3152
3153#define _POLL_EVENT_NUM_UNUSED_BITS \
3154 (32 - (_POLL_NUM_TYPES + _POLL_NUM_STATES + 1 /* modes */))
3155
3156#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3157#error overflow of 32-bit word in struct k_poll_event
3158#endif
3159
3160/* end of polling API - PRIVATE */
3161
3162
3163/**
3164 * @defgroup poll_apis Async polling APIs
3165 * @ingroup kernel_apis
3166 * @{
3167 */
3168
3169/* Public polling API */
3170
3171/* public - values for k_poll_event.type bitfield */
3172#define K_POLL_TYPE_IGNORE 0
3173#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3174#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
3175#define K_POLL_TYPE_FIFO_DATA_AVAILABLE \
3176 _POLL_TYPE_BIT(_POLL_TYPE_FIFO_DATA_AVAILABLE)
3177
3178/* public - polling modes */
3179enum k_poll_modes {
3180 /* polling thread does not take ownership of objects when available */
3181 K_POLL_MODE_NOTIFY_ONLY = 0,
3182
3183 K_POLL_NUM_MODES
3184};
3185
3186/* public - values for k_poll_event.state bitfield */
3187#define K_POLL_STATE_NOT_READY 0
3188#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3189#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3190#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
3191#define K_POLL_STATE_FIFO_DATA_AVAILABLE \
3192 _POLL_STATE_BIT(_POLL_STATE_FIFO_DATA_AVAILABLE)
3193
3194/* public - poll signal object */
3195struct k_poll_signal {
3196 /* PRIVATE - DO NOT TOUCH */
3197 struct k_poll_event *poll_event;
3198
3199 /*
3200 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3201 * user resets it to 0.
3202 */
3203 unsigned int signaled;
3204
3205 /* custom result value passed to k_poll_signal() if needed */
3206 int result;
3207};
3208
3209#define K_POLL_SIGNAL_INITIALIZER() \
3210 { \
3211 .poll_event = NULL, \
3212 .signaled = 0, \
3213 .result = 0, \
3214 }
3215
3216struct k_poll_event {
3217 /* PRIVATE - DO NOT TOUCH */
3218 struct _poller *poller;
3219
3220 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
3221 uint32_t type:_POLL_NUM_TYPES;
3222
3223 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
3224 uint32_t state:_POLL_NUM_STATES;
3225
3226 /* mode of operation, from enum k_poll_modes */
3227 uint32_t mode:1;
3228
3229 /* unused bits in 32-bit word */
3230 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
3231
3232 /* per-type data */
3233 union {
3234 void *obj;
3235 struct k_poll_signal *signal;
3236 struct k_sem *sem;
3237 struct k_fifo *fifo;
3238 };
3239};
3240
3241#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_data) \
3242 { \
3243 .poller = NULL, \
3244 .type = event_type, \
3245 .state = K_POLL_STATE_NOT_READY, \
3246 .mode = event_mode, \
3247 .unused = 0, \
3248 { .obj = event_data }, \
3249 }
3250
3251/**
3252 * @brief Initialize one struct k_poll_event instance
3253 *
3254 * After this routine is called on a poll event, the event it ready to be
3255 * placed in an event array to be passed to k_poll().
3256 *
3257 * @param event The event to initialize.
3258 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3259 * values. Only values that apply to the same object being polled
3260 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3261 * event.
3262 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3263 * @param obj Kernel object or poll signal.
3264 *
3265 * @return N/A
3266 */
3267
3268extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
3269 int mode, void *obj);
3270
3271/**
3272 * @brief Wait for one or many of multiple poll events to occur
3273 *
3274 * This routine allows a thread to wait concurrently for one or many of
3275 * multiple poll events to have occurred. Such events can be a kernel object
3276 * being available, like a semaphore, or a poll signal event.
3277 *
3278 * When an event notifies that a kernel object is available, the kernel object
3279 * is not "given" to the thread calling k_poll(): it merely signals the fact
3280 * that the object was available when the k_poll() call was in effect. Also,
3281 * all threads trying to acquire an object the regular way, i.e. by pending on
3282 * the object, have precedence over the thread polling on the object. This
3283 * means that the polling thread will never get the poll event on an object
3284 * until the object becomes available and its pend queue is empty. For this
3285 * reason, the k_poll() call is more effective when the objects being polled
3286 * only have one thread, the polling thread, trying to acquire them.
3287 *
3288 * Only one thread can be polling for a particular object at a given time. If
3289 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3290 * and returns as soon as it has finished handling the other events. This means
3291 * that k_poll() can return -EADDRINUSE and have the state value of some events
3292 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3293 * parameter is ignored.
3294 *
3295 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3296 * events that were passed to k_poll() and check the state field for the values
3297 * that were expected and take the associated actions.
3298 *
3299 * Before being reused for another call to k_poll(), the user has to reset the
3300 * state field to K_POLL_STATE_NOT_READY.
3301 *
3302 * @param events An array of pointers to events to be polled for.
3303 * @param num_events The number of events in the array.
3304 * @param timeout Waiting period for an event to be ready (in milliseconds),
3305 * or one of the special values K_NO_WAIT and K_FOREVER.
3306 *
3307 * @retval 0 One or more events are ready.
3308 * @retval -EADDRINUSE One or more objects already had a poller.
3309 * @retval -EAGAIN Waiting period timed out.
3310 */
3311
3312extern int k_poll(struct k_poll_event *events, int num_events,
3313 int32_t timeout);
3314
3315/**
3316 * @brief Signal a poll signal object.
3317 *
3318 * This routine makes ready a poll signal, which is basically a poll event of
3319 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3320 * made ready to run. A @a result value can be specified.
3321 *
3322 * The poll signal contains a 'signaled' field that, when set by
3323 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3324 * be reset by the user before being passed again to k_poll() or k_poll() will
3325 * consider it being signaled, and will return immediately.
3326 *
3327 * @param signal A poll signal.
3328 * @param result The value to store in the result field of the signal.
3329 *
3330 * @retval 0 The signal was delivered successfully.
3331 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3332 */
3333
3334extern int k_poll_signal(struct k_poll_signal *signal, int result);
3335
3336/* private internal function */
3337extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
3338 uint32_t state);
3339
3340/**
3341 * @} end defgroup poll_apis
3342 */
3343
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003344/**
3345 * @brief Make the CPU idle.
3346 *
3347 * This function makes the CPU idle until an event wakes it up.
3348 *
3349 * In a regular system, the idle thread should be the only thread responsible
3350 * for making the CPU idle and triggering any type of power management.
3351 * However, in some more constrained systems, such as a single-threaded system,
3352 * the only thread would be responsible for this if needed.
3353 *
3354 * @return N/A
3355 */
3356extern void k_cpu_idle(void);
3357
3358/**
3359 * @brief Make the CPU idle in an atomic fashion.
3360 *
3361 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3362 * must be done atomically before making the CPU idle.
3363 *
3364 * @param key Interrupt locking key obtained from irq_lock().
3365 *
3366 * @return N/A
3367 */
3368extern void k_cpu_atomic_idle(unsigned int key);
3369
Andrew Boie350f88d2017-01-18 13:13:45 -08003370extern void _sys_power_save_idle_exit(int32_t ticks);
3371
Anas Nashifa6149502017-01-17 07:47:31 -05003372/* Include legacy APIs */
3373#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003375#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376#include <arch/cpu.h>
3377
3378/*
3379 * private APIs that are utilized by one or more public APIs
3380 */
3381
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003382#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003383extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003384#else
3385#define _init_static_threads() do { } while ((0))
3386#endif
3387
3388extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003389extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003390
3391#ifdef __cplusplus
3392}
3393#endif
3394
Andrew Boiee004dec2016-11-07 09:01:19 -08003395#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3396/*
3397 * Define new and delete operators.
3398 * At this moment, the operators do nothing since objects are supposed
3399 * to be statically allocated.
3400 */
3401inline void operator delete(void *ptr)
3402{
3403 (void)ptr;
3404}
3405
3406inline void operator delete[](void *ptr)
3407{
3408 (void)ptr;
3409}
3410
3411inline void *operator new(size_t size)
3412{
3413 (void)size;
3414 return NULL;
3415}
3416
3417inline void *operator new[](size_t size)
3418{
3419 (void)size;
3420 return NULL;
3421}
3422
3423/* Placement versions of operator new and delete */
3424inline void operator delete(void *ptr1, void *ptr2)
3425{
3426 (void)ptr1;
3427 (void)ptr2;
3428}
3429
3430inline void operator delete[](void *ptr1, void *ptr2)
3431{
3432 (void)ptr1;
3433 (void)ptr2;
3434}
3435
3436inline void *operator new(size_t size, void *ptr)
3437{
3438 (void)size;
3439 return ptr;
3440}
3441
3442inline void *operator new[](size_t size, void *ptr)
3443{
3444 (void)size;
3445 return ptr;
3446}
3447
3448#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3449
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003450#endif /* !_ASMLANGUAGE */
3451
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003452#endif /* _kernel__h_ */