blob: 651d1915d078ce736cfa5903f2daf9c07b4d6520 [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;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400126typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128enum execution_context_types {
129 K_ISR = 0,
130 K_COOP_THREAD,
131 K_PREEMPT_THREAD,
132};
133
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100135 * @defgroup profiling_apis Profiling APIs
136 * @ingroup kernel_apis
137 * @{
138 */
139
140/**
141 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
142 *
143 * This routine calls @ref stack_analyze on the 4 call stacks declared and
144 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
145 *
146 * CONFIG_MAIN_STACK_SIZE
147 * CONFIG_IDLE_STACK_SIZE
148 * CONFIG_ISR_STACK_SIZE
149 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
150 *
151 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
152 * produce output.
153 *
154 * @return N/A
155 */
156extern void k_call_stacks_analyze(void);
157
158/**
159 * @} end defgroup profiling_apis
160 */
161
162/**
Allan Stephensc98da842016-11-11 15:45:03 -0500163 * @defgroup thread_apis Thread APIs
164 * @ingroup kernel_apis
165 * @{
166 */
167
168/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500169 * @typedef k_thread_entry_t
170 * @brief Thread entry point function type.
171 *
172 * A thread's entry point function is invoked when the thread starts executing.
173 * Up to 3 argument values can be passed to the function.
174 *
175 * The thread terminates execution permanently if the entry point function
176 * returns. The thread is responsible for releasing any shared resources
177 * it may own (such as mutexes and dynamically allocated memory), prior to
178 * returning.
179 *
180 * @param p1 First argument.
181 * @param p2 Second argument.
182 * @param p3 Third argument.
183 *
184 * @return N/A
185 */
186typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
187
Benjamin Walshed240f22017-01-22 13:05:08 -0500188#endif /* !_ASMLANGUAGE */
189
190
191/*
192 * Thread user options. May be needed by assembly code. Common part uses low
193 * bits, arch-specific use high bits.
194 */
195
196/* system thread that must not abort */
197#define K_ESSENTIAL (1 << 0)
198
199#if defined(CONFIG_FP_SHARING)
200/* thread uses floating point registers */
201#define K_FP_REGS (1 << 1)
202#endif
203
204#ifdef CONFIG_X86
205/* x86 Bitmask definitions for threads user options */
206
207#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
208/* thread uses SSEx (and also FP) registers */
209#define K_SSE_REGS (1 << 7)
210#endif
211#endif
212
213/* end - thread options */
214
215#if !defined(_ASMLANGUAGE)
216
Allan Stephens5eceb852016-11-16 10:16:30 -0500217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500218 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500222 * The new thread may be scheduled for immediate execution or a delayed start.
223 * If the newly spawned thread does not have a delayed start the kernel
224 * scheduler may preempt the current thread to allow the new thread to
225 * execute.
226 *
227 * Thread options are architecture-specific, and can include K_ESSENTIAL,
228 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
229 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 *
231 * @param stack Pointer to the stack space.
232 * @param stack_size Stack size in bytes.
233 * @param entry Thread entry function.
234 * @param p1 1st entry point parameter.
235 * @param p2 2nd entry point parameter.
236 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @param prio Thread priority.
238 * @param options Thread options.
239 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500241 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500243extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500244 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500246 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400247
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400250 *
Allan Stephensc98da842016-11-11 15:45:03 -0500251 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500252 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500254 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400255 *
256 * @return N/A
257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400259
260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500261 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400262 *
263 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500264 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400266 * @return N/A
267 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400268extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400269
270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500271 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500273 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400274 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500275 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400276 *
277 * @return N/A
278 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400279extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400280
281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500282 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500284 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500286 * If @a thread is not currently sleeping, the routine has no effect.
287 *
288 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400289 *
290 * @return N/A
291 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400292extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400293
294/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500295 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500297 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400298 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400299extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400300
301/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500302 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500304 * This routine prevents @a thread from executing if it has not yet started
305 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500307 * @param thread ID of thread to cancel.
308 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500309 * @retval 0 Thread spawning cancelled.
310 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400311 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400312extern int k_thread_cancel(k_tid_t thread);
313
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400314/**
Allan Stephensc98da842016-11-11 15:45:03 -0500315 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500317 * This routine permanently stops execution of @a thread. The thread is taken
318 * off all kernel queues it is part of (i.e. the ready queue, the timeout
319 * queue, or a kernel object wait queue). However, any kernel resources the
320 * thread might currently own (such as mutexes or memory blocks) are not
321 * released. It is the responsibility of the caller of this routine to ensure
322 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500324 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400325 *
326 * @return N/A
327 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400328extern void k_thread_abort(k_tid_t thread);
329
Allan Stephensc98da842016-11-11 15:45:03 -0500330/**
331 * @cond INTERNAL_HIDDEN
332 */
333
Benjamin Walshd211a522016-12-06 11:44:01 -0500334/* timeout has timed out and is not on _timeout_q anymore */
335#define _EXPIRED (-2)
336
337/* timeout is not in use */
338#define _INACTIVE (-1)
339
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400340#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400341#define _THREAD_TIMEOUT_INIT(obj) \
342 (obj).nano_timeout = { \
343 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400344 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400345 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500346 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400347 },
348#else
349#define _THREAD_TIMEOUT_INIT(obj)
350#endif
351
352#ifdef CONFIG_ERRNO
353#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
354#else
355#define _THREAD_ERRNO_INIT(obj)
356#endif
357
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400358struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400359 union {
360 char *init_stack;
361 struct k_thread *thread;
362 };
363 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500364 void (*init_entry)(void *, void *, void *);
365 void *init_p1;
366 void *init_p2;
367 void *init_p3;
368 int init_prio;
369 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400370 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500371 void (*init_abort)(void);
372 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400373};
374
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400375#define _THREAD_INITIALIZER(stack, stack_size, \
376 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500377 prio, options, delay, abort, groups) \
378 { \
Mazen NEIFER967cb2e2017-02-02 10:42:18 +0100379 {.init_stack = (stack)}, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500380 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400381 .init_entry = (void (*)(void *, void *, void *))entry, \
382 .init_p1 = (void *)p1, \
383 .init_p2 = (void *)p2, \
384 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500385 .init_prio = (prio), \
386 .init_options = (options), \
387 .init_delay = (delay), \
388 .init_abort = (abort), \
389 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400390 }
391
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400392/**
Allan Stephensc98da842016-11-11 15:45:03 -0500393 * INTERNAL_HIDDEN @endcond
394 */
395
396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500397 * @brief Statically define and initialize a thread.
398 *
399 * The thread may be scheduled for immediate execution or a delayed start.
400 *
401 * Thread options are architecture-specific, and can include K_ESSENTIAL,
402 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
403 * them using "|" (the logical OR operator).
404 *
405 * The ID of the thread can be accessed using:
406 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500407 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500408 *
409 * @param name Name of the thread.
410 * @param stack_size Stack size in bytes.
411 * @param entry Thread entry function.
412 * @param p1 1st entry point parameter.
413 * @param p2 2nd entry point parameter.
414 * @param p3 3rd entry point parameter.
415 * @param prio Thread priority.
416 * @param options Thread options.
417 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400418 *
419 * @internal It has been observed that the x86 compiler by default aligns
420 * these _static_thread_data structures to 32-byte boundaries, thereby
421 * wasting space. To work around this, force a 4-byte alignment.
422 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500423#define K_THREAD_DEFINE(name, stack_size, \
424 entry, p1, p2, p3, \
425 prio, options, delay) \
426 char __noinit __stack _k_thread_obj_##name[stack_size]; \
427 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500428 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500429 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
430 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500431 NULL, 0); \
432 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400433
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500437 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500439 * @param thread ID of thread whose priority is needed.
440 *
441 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500443extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444
445/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500448 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400449 *
450 * Rescheduling can occur immediately depending on the priority @a thread is
451 * set to:
452 *
453 * - If its priority is raised above the priority of the caller of this
454 * function, and the caller is preemptible, @a thread will be scheduled in.
455 *
456 * - If the caller operates on itself, it lowers its priority below that of
457 * other threads in the system, and the caller is preemptible, the thread of
458 * highest priority will be scheduled in.
459 *
460 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
461 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
462 * highest priority.
463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 * @param prio New priority.
466 *
467 * @warning Changing the priority of a thread currently involved in mutex
468 * priority inheritance may result in undefined behavior.
469 *
470 * @return N/A
471 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400472extern void k_thread_priority_set(k_tid_t thread, int prio);
473
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500475 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500477 * This routine prevents the kernel scheduler from making @a thread the
478 * current thread. All other internal operations on @a thread are still
479 * performed; for example, any timeout it is waiting on keeps ticking,
480 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500482 * If @a thread is already suspended, the routine has no effect.
483 *
484 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485 *
486 * @return N/A
487 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400488extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400489
490/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500491 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * This routine allows the kernel scheduler to make @a thread the current
494 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500496 * If @a thread is not currently suspended, the routine has no effect.
497 *
498 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400499 *
500 * @return N/A
501 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400502extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400504/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500505 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * This routine specifies how the scheduler will perform time slicing of
508 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500510 * To enable time slicing, @a slice must be non-zero. The scheduler
511 * ensures that no thread runs for more than the specified time limit
512 * before other threads of that priority are given a chance to execute.
513 * Any thread whose priority is higher than @a prio is exempted, and may
514 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500516 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517 * execute. Once the scheduler selects a thread for execution, there is no
518 * minimum guaranteed time the thread will execute before threads of greater or
519 * equal priority are scheduled.
520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500521 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400522 * for execution, this routine has no effect; the thread is immediately
523 * rescheduled after the slice period expires.
524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500525 * To disable timeslicing, set both @a slice and @a prio to zero.
526 *
527 * @param slice Maximum time slice length (in milliseconds).
528 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400529 *
530 * @return N/A
531 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400532extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534/**
Allan Stephensc98da842016-11-11 15:45:03 -0500535 * @} end defgroup thread_apis
536 */
537
538/**
539 * @addtogroup isr_apis
540 * @{
541 */
542
543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500544 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545 *
Allan Stephensc98da842016-11-11 15:45:03 -0500546 * This routine allows the caller to customize its actions, depending on
547 * whether it is a thread or an ISR.
548 *
549 * @note Can be called by ISRs.
550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500551 * @return 0 if invoked by a thread.
552 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400553 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500554extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400555
Benjamin Walsh445830d2016-11-10 15:54:27 -0500556/**
557 * @brief Determine if code is running in a preemptible thread.
558 *
Allan Stephensc98da842016-11-11 15:45:03 -0500559 * This routine allows the caller to customize its actions, depending on
560 * whether it can be preempted by another thread. The routine returns a 'true'
561 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500562 *
Allan Stephensc98da842016-11-11 15:45:03 -0500563 * - The code is running in a thread, not at ISR.
564 * - The thread's priority is in the preemptible range.
565 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500566 *
Allan Stephensc98da842016-11-11 15:45:03 -0500567 * @note Can be called by ISRs.
568 *
569 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500570 * @return Non-zero if invoked by a preemptible thread.
571 */
572extern int k_is_preempt_thread(void);
573
Allan Stephensc98da842016-11-11 15:45:03 -0500574/**
575 * @} end addtogroup isr_apis
576 */
577
578/**
579 * @addtogroup thread_apis
580 * @{
581 */
582
583/**
584 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500585 *
Allan Stephensc98da842016-11-11 15:45:03 -0500586 * This routine prevents the current thread from being preempted by another
587 * thread by instructing the scheduler to treat it as a cooperative thread.
588 * If the thread subsequently performs an operation that makes it unready,
589 * it will be context switched out in the normal manner. When the thread
590 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500591 *
Allan Stephensc98da842016-11-11 15:45:03 -0500592 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500593 *
Allan Stephensc98da842016-11-11 15:45:03 -0500594 * @note k_sched_lock() and k_sched_unlock() should normally be used
595 * when the operation being performed can be safely interrupted by ISRs.
596 * However, if the amount of processing involved is very small, better
597 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500598 *
599 * @return N/A
600 */
601extern void k_sched_lock(void);
602
Allan Stephensc98da842016-11-11 15:45:03 -0500603/**
604 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500605 *
Allan Stephensc98da842016-11-11 15:45:03 -0500606 * This routine reverses the effect of a previous call to k_sched_lock().
607 * A thread must call the routine once for each time it called k_sched_lock()
608 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500609 *
610 * @return N/A
611 */
612extern void k_sched_unlock(void);
613
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500615 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500617 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500619 * Custom data is not used by the kernel itself, and is freely available
620 * for a thread to use as it sees fit. It can be used as a framework
621 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500623 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
625 * @return N/A
626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400627extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628
629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500630 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500632 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500634 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400636extern void *k_thread_custom_data_get(void);
637
638/**
Allan Stephensc98da842016-11-11 15:45:03 -0500639 * @} end addtogroup thread_apis
640 */
641
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400642#include <sys_clock.h>
643
Allan Stephensc2f15a42016-11-17 12:24:22 -0500644/**
645 * @addtogroup clock_apis
646 * @{
647 */
648
649/**
650 * @brief Generate null timeout delay.
651 *
652 * This macro generates a timeout delay that that instructs a kernel API
653 * not to wait if the requested operation cannot be performed immediately.
654 *
655 * @return Timeout delay value.
656 */
657#define K_NO_WAIT 0
658
659/**
660 * @brief Generate timeout delay from milliseconds.
661 *
662 * This macro generates a timeout delay that that instructs a kernel API
663 * to wait up to @a ms milliseconds to perform the requested operation.
664 *
665 * @param ms Duration in milliseconds.
666 *
667 * @return Timeout delay value.
668 */
Johan Hedberg14471692016-11-13 10:52:15 +0200669#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500670
671/**
672 * @brief Generate timeout delay from seconds.
673 *
674 * This macro generates a timeout delay that that instructs a kernel API
675 * to wait up to @a s seconds to perform the requested operation.
676 *
677 * @param s Duration in seconds.
678 *
679 * @return Timeout delay value.
680 */
Johan Hedberg14471692016-11-13 10:52:15 +0200681#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500682
683/**
684 * @brief Generate timeout delay from minutes.
685 *
686 * This macro generates a timeout delay that that instructs a kernel API
687 * to wait up to @a m minutes to perform the requested operation.
688 *
689 * @param m Duration in minutes.
690 *
691 * @return Timeout delay value.
692 */
Johan Hedberg14471692016-11-13 10:52:15 +0200693#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500694
695/**
696 * @brief Generate timeout delay from hours.
697 *
698 * This macro generates a timeout delay that that instructs a kernel API
699 * to wait up to @a h hours to perform the requested operation.
700 *
701 * @param h Duration in hours.
702 *
703 * @return Timeout delay value.
704 */
Johan Hedberg14471692016-11-13 10:52:15 +0200705#define K_HOURS(h) K_MINUTES((h) * 60)
706
Allan Stephensc98da842016-11-11 15:45:03 -0500707/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500708 * @brief Generate infinite timeout delay.
709 *
710 * This macro generates a timeout delay that that instructs a kernel API
711 * to wait as long as necessary to perform the requested operation.
712 *
713 * @return Timeout delay value.
714 */
715#define K_FOREVER (-1)
716
717/**
718 * @} end addtogroup clock_apis
719 */
720
721/**
Allan Stephensc98da842016-11-11 15:45:03 -0500722 * @cond INTERNAL_HIDDEN
723 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400724
Benjamin Walsh62092182016-12-20 14:39:08 -0500725/* kernel clocks */
726
727#if (sys_clock_ticks_per_sec == 1000) || \
728 (sys_clock_ticks_per_sec == 500) || \
729 (sys_clock_ticks_per_sec == 250) || \
730 (sys_clock_ticks_per_sec == 125) || \
731 (sys_clock_ticks_per_sec == 100) || \
732 (sys_clock_ticks_per_sec == 50) || \
733 (sys_clock_ticks_per_sec == 25) || \
734 (sys_clock_ticks_per_sec == 20) || \
735 (sys_clock_ticks_per_sec == 10) || \
736 (sys_clock_ticks_per_sec == 1)
737
738 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
739#else
740 /* yields horrible 64-bit math on many architectures: try to avoid */
741 #define _NON_OPTIMIZED_TICKS_PER_SEC
742#endif
743
744#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
745extern int32_t _ms_to_ticks(int32_t ms);
746#else
747static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
748{
749 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
750}
751#endif
752
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500753/* added tick needed to account for tick in progress */
754#define _TICK_ALIGN 1
755
Benjamin Walsh62092182016-12-20 14:39:08 -0500756static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400757{
Benjamin Walsh62092182016-12-20 14:39:08 -0500758#ifdef CONFIG_SYS_CLOCK_EXISTS
759
760#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400761 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400762#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500763 return (uint64_t)ticks * _ms_per_tick;
764#endif
765
766#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400767 __ASSERT(ticks == 0, "");
768 return 0;
769#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400770}
771
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400772/* timeouts */
773
774struct _timeout;
775typedef void (*_timeout_func_t)(struct _timeout *t);
776
777struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500778 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400779 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400780 sys_dlist_t *wait_q;
781 int32_t delta_ticks_from_prev;
782 _timeout_func_t func;
783};
784
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200785extern int32_t _timeout_remaining_get(struct _timeout *timeout);
786
Allan Stephensc98da842016-11-11 15:45:03 -0500787/**
788 * INTERNAL_HIDDEN @endcond
789 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500790
Allan Stephensc98da842016-11-11 15:45:03 -0500791/**
792 * @cond INTERNAL_HIDDEN
793 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400794
795struct k_timer {
796 /*
797 * _timeout structure must be first here if we want to use
798 * dynamic timer allocation. timeout.node is used in the double-linked
799 * list of free timers
800 */
801 struct _timeout timeout;
802
Allan Stephens45bfa372016-10-12 12:39:42 -0500803 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400804 _wait_q_t wait_q;
805
806 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500807 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808
809 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500810 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400811
812 /* timer period */
813 int32_t period;
814
Allan Stephens45bfa372016-10-12 12:39:42 -0500815 /* timer status */
816 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400817
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500818 /* user-specific data, also used to support legacy features */
819 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400820
Anas Nashif2f203c22016-12-18 06:57:45 -0500821 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400822};
823
Allan Stephens1342adb2016-11-03 13:54:53 -0500824#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400825 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500826 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500827 .timeout.wait_q = NULL, \
828 .timeout.thread = NULL, \
829 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400830 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500831 .expiry_fn = expiry, \
832 .stop_fn = stop, \
833 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500834 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500835 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400836 }
837
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838/**
Allan Stephensc98da842016-11-11 15:45:03 -0500839 * INTERNAL_HIDDEN @endcond
840 */
841
842/**
843 * @defgroup timer_apis Timer APIs
844 * @ingroup kernel_apis
845 * @{
846 */
847
848/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500849 * @typedef k_timer_expiry_t
850 * @brief Timer expiry function type.
851 *
852 * A timer's expiry function is executed by the system clock interrupt handler
853 * each time the timer expires. The expiry function is optional, and is only
854 * invoked if the timer has been initialized with one.
855 *
856 * @param timer Address of timer.
857 *
858 * @return N/A
859 */
860typedef void (*k_timer_expiry_t)(struct k_timer *timer);
861
862/**
863 * @typedef k_timer_stop_t
864 * @brief Timer stop function type.
865 *
866 * A timer's stop function is executed if the timer is stopped prematurely.
867 * The function runs in the context of the thread that stops the timer.
868 * The stop function is optional, and is only invoked if the timer has been
869 * initialized with one.
870 *
871 * @param timer Address of timer.
872 *
873 * @return N/A
874 */
875typedef void (*k_timer_stop_t)(struct k_timer *timer);
876
877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500882 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
884 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * @param expiry_fn Function to invoke each time the timer expires.
886 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500888#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500889 struct k_timer name \
890 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500891 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400892
Allan Stephens45bfa372016-10-12 12:39:42 -0500893/**
894 * @brief Initialize a timer.
895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500896 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500897 *
898 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * @param expiry_fn Function to invoke each time the timer expires.
900 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500901 *
902 * @return N/A
903 */
904extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500905 k_timer_expiry_t expiry_fn,
906 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700907
Allan Stephens45bfa372016-10-12 12:39:42 -0500908/**
909 * @brief Start a timer.
910 *
911 * This routine starts a timer, and resets its status to zero. The timer
912 * begins counting down using the specified duration and period values.
913 *
914 * Attempting to start a timer that is already running is permitted.
915 * The timer's status is reset to zero and the timer begins counting down
916 * using the new duration and period values.
917 *
918 * @param timer Address of timer.
919 * @param duration Initial timer duration (in milliseconds).
920 * @param period Timer period (in milliseconds).
921 *
922 * @return N/A
923 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400924extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500925 int32_t duration, int32_t period);
926
927/**
928 * @brief Stop a timer.
929 *
930 * This routine stops a running timer prematurely. The timer's stop function,
931 * if one exists, is invoked by the caller.
932 *
933 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500935 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -0500936 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
937 * if @a k_timer_stop is to be called from ISRs.
938 *
Allan Stephens45bfa372016-10-12 12:39:42 -0500939 * @param timer Address of timer.
940 *
941 * @return N/A
942 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500944
945/**
946 * @brief Read timer status.
947 *
948 * This routine reads the timer's status, which indicates the number of times
949 * it has expired since its status was last read.
950 *
951 * Calling this routine resets the timer's status to zero.
952 *
953 * @param timer Address of timer.
954 *
955 * @return Timer status.
956 */
957extern uint32_t k_timer_status_get(struct k_timer *timer);
958
959/**
960 * @brief Synchronize thread to timer expiration.
961 *
962 * This routine blocks the calling thread until the timer's status is non-zero
963 * (indicating that it has expired at least once since it was last examined)
964 * or the timer is stopped. If the timer status is already non-zero,
965 * or the timer is already stopped, the caller continues without waiting.
966 *
967 * Calling this routine resets the timer's status to zero.
968 *
969 * This routine must not be used by interrupt handlers, since they are not
970 * allowed to block.
971 *
972 * @param timer Address of timer.
973 *
974 * @return Timer status.
975 */
976extern uint32_t k_timer_status_sync(struct k_timer *timer);
977
978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500980 *
981 * This routine computes the (approximate) time remaining before a running
982 * timer next expires. If the timer is not running, it returns zero.
983 *
984 * @param timer Address of timer.
985 *
986 * @return Remaining time (in milliseconds).
987 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200988static inline int32_t k_timer_remaining_get(struct k_timer *timer)
989{
990 return _timeout_remaining_get(&timer->timeout);
991}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400992
Allan Stephensc98da842016-11-11 15:45:03 -0500993/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500994 * @brief Associate user-specific data with a timer.
995 *
996 * This routine records the @a user_data with the @a timer, to be retrieved
997 * later.
998 *
999 * It can be used e.g. in a timer handler shared across multiple subsystems to
1000 * retrieve data specific to the subsystem this timer is associated with.
1001 *
1002 * @param timer Address of timer.
1003 * @param user_data User data to associate with the timer.
1004 *
1005 * @return N/A
1006 */
1007static inline void k_timer_user_data_set(struct k_timer *timer,
1008 void *user_data)
1009{
1010 timer->user_data = user_data;
1011}
1012
1013/**
1014 * @brief Retrieve the user-specific data from a timer.
1015 *
1016 * @param timer Address of timer.
1017 *
1018 * @return The user data.
1019 */
1020static inline void *k_timer_user_data_get(struct k_timer *timer)
1021{
1022 return timer->user_data;
1023}
1024
1025/**
Allan Stephensc98da842016-11-11 15:45:03 -05001026 * @} end defgroup timer_apis
1027 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001028
Allan Stephensc98da842016-11-11 15:45:03 -05001029/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001030 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001031 * @{
1032 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001033
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001034/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * This routine returns the elapsed time since the system booted,
1038 * in milliseconds.
1039 *
1040 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001043
1044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * This routine returns the lower 32-bits of the elapsed time since the system
1048 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * This routine can be more efficient than k_uptime_get(), as it reduces the
1051 * need for interrupt locking and 64-bit math. However, the 32-bit result
1052 * cannot hold a system uptime time larger than approximately 50 days, so the
1053 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001056 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001057extern uint32_t k_uptime_get_32(void);
1058
1059/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001062 * This routine computes the elapsed time between the current system uptime
1063 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * @param reftime Pointer to a reference time, which is updated to the current
1066 * uptime upon return.
1067 *
1068 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001069 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001071
1072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * This routine computes the elapsed time between the current system uptime
1076 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1079 * need for interrupt locking and 64-bit math. However, the 32-bit result
1080 * cannot hold an elapsed time larger than approximately 50 days, so the
1081 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * @param reftime Pointer to a reference time, which is updated to the current
1084 * uptime upon return.
1085 *
1086 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001087 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001088extern uint32_t k_uptime_delta_32(int64_t *reftime);
1089
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001091 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 * This routine returns the current time, as measured by the system's hardware
1094 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001096 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001098#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001100/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001101 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102 */
1103
Allan Stephensc98da842016-11-11 15:45:03 -05001104/**
1105 * @cond INTERNAL_HIDDEN
1106 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001108struct k_queue {
1109 _wait_q_t wait_q;
1110 sys_slist_t data_q;
1111 _POLL_EVENT;
1112
1113 _OBJECT_TRACING_NEXT_PTR(k_queue);
1114};
1115
1116#define K_QUEUE_INITIALIZER(obj) \
1117 { \
1118 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1119 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1120 _POLL_EVENT_OBJ_INIT \
1121 _OBJECT_TRACING_INIT \
1122 }
1123
1124/**
1125 * INTERNAL_HIDDEN @endcond
1126 */
1127
1128/**
1129 * @defgroup queue_apis Queue APIs
1130 * @ingroup kernel_apis
1131 * @{
1132 */
1133
1134/**
1135 * @brief Initialize a queue.
1136 *
1137 * This routine initializes a queue object, prior to its first use.
1138 *
1139 * @param queue Address of the queue.
1140 *
1141 * @return N/A
1142 */
1143extern void k_queue_init(struct k_queue *queue);
1144
1145/**
1146 * @brief Append an element to the end of a queue.
1147 *
1148 * This routine appends a data item to @a queue. A queue data item must be
1149 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1150 * reserved for the kernel's use.
1151 *
1152 * @note Can be called by ISRs.
1153 *
1154 * @param queue Address of the queue.
1155 * @param data Address of the data item.
1156 *
1157 * @return N/A
1158 */
1159extern void k_queue_append(struct k_queue *queue, void *data);
1160
1161/**
1162 * @brief Prepend an element to a queue.
1163 *
1164 * This routine prepends a data item to @a queue. A queue data item must be
1165 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1166 * reserved for the kernel's use.
1167 *
1168 * @note Can be called by ISRs.
1169 *
1170 * @param queue Address of the queue.
1171 * @param data Address of the data item.
1172 *
1173 * @return N/A
1174 */
1175extern void k_queue_prepend(struct k_queue *queue, void *data);
1176
1177/**
1178 * @brief Inserts an element to a queue.
1179 *
1180 * This routine inserts a data item to @a queue after previous item. A queue
1181 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1182 * item are reserved for the kernel's use.
1183 *
1184 * @note Can be called by ISRs.
1185 *
1186 * @param queue Address of the queue.
1187 * @param prev Address of the previous data item.
1188 * @param data Address of the data item.
1189 *
1190 * @return N/A
1191 */
1192extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1193
1194/**
1195 * @brief Atomically append a list of elements to a queue.
1196 *
1197 * This routine adds a list of data items to @a queue in one operation.
1198 * The data items must be in a singly-linked list, with the first 32 bits
1199 * in each data item pointing to the next data item; the list must be
1200 * NULL-terminated.
1201 *
1202 * @note Can be called by ISRs.
1203 *
1204 * @param queue Address of the queue.
1205 * @param head Pointer to first node in singly-linked list.
1206 * @param tail Pointer to last node in singly-linked list.
1207 *
1208 * @return N/A
1209 */
1210extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1211
1212/**
1213 * @brief Atomically add a list of elements to a queue.
1214 *
1215 * This routine adds a list of data items to @a queue in one operation.
1216 * The data items must be in a singly-linked list implemented using a
1217 * sys_slist_t object. Upon completion, the original list is empty.
1218 *
1219 * @note Can be called by ISRs.
1220 *
1221 * @param queue Address of the queue.
1222 * @param list Pointer to sys_slist_t object.
1223 *
1224 * @return N/A
1225 */
1226extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1227
1228/**
1229 * @brief Get an element from a queue.
1230 *
1231 * This routine removes first data item from @a queue. The first 32 bits of the
1232 * data item are reserved for the kernel's use.
1233 *
1234 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1235 *
1236 * @param queue Address of the queue.
1237 * @param timeout Waiting period to obtain a data item (in milliseconds),
1238 * or one of the special values K_NO_WAIT and K_FOREVER.
1239 *
1240 * @return Address of the data item if successful; NULL if returned
1241 * without waiting, or waiting period timed out.
1242 */
1243extern void *k_queue_get(struct k_queue *queue, int32_t timeout);
1244
1245/**
1246 * @brief Query a queue to see if it has data available.
1247 *
1248 * Note that the data might be already gone by the time this function returns
1249 * if other threads are also trying to read from the queue.
1250 *
1251 * @note Can be called by ISRs.
1252 *
1253 * @param queue Address of the queue.
1254 *
1255 * @return Non-zero if the queue is empty.
1256 * @return 0 if data is available.
1257 */
1258static inline int k_queue_is_empty(struct k_queue *queue)
1259{
1260 return (int)sys_slist_is_empty(&queue->data_q);
1261}
1262
1263/**
1264 * @brief Statically define and initialize a queue.
1265 *
1266 * The queue can be accessed outside the module where it is defined using:
1267 *
1268 * @code extern struct k_queue <name>; @endcode
1269 *
1270 * @param name Name of the queue.
1271 */
1272#define K_QUEUE_DEFINE(name) \
1273 struct k_queue name \
1274 __in_section(_k_queue, static, name) = \
1275 K_QUEUE_INITIALIZER(name)
1276
1277/**
1278 * @} end defgroup queue_apis
1279 */
1280
1281/**
1282 * @cond INTERNAL_HIDDEN
1283 */
1284
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001286 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287};
1288
Allan Stephensc98da842016-11-11 15:45:03 -05001289#define K_FIFO_INITIALIZER(obj) \
1290 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001291 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001292 }
1293
1294/**
1295 * INTERNAL_HIDDEN @endcond
1296 */
1297
1298/**
1299 * @defgroup fifo_apis Fifo APIs
1300 * @ingroup kernel_apis
1301 * @{
1302 */
1303
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001307 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001309 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 *
1311 * @return N/A
1312 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001313#define k_fifo_init(fifo) \
1314 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001315
1316/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001319 * This routine adds a data item to @a fifo. A fifo data item must be
1320 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1321 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * @note Can be called by ISRs.
1324 *
1325 * @param fifo Address of the fifo.
1326 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001327 *
1328 * @return N/A
1329 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001330#define k_fifo_put(fifo, data) \
1331 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001332
1333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001334 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * This routine adds a list of data items to @a fifo in one operation.
1337 * The data items must be in a singly-linked list, with the first 32 bits
1338 * each data item pointing to the next data item; the list must be
1339 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * @param fifo Address of the fifo.
1344 * @param head Pointer to first node in singly-linked list.
1345 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346 *
1347 * @return N/A
1348 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001349#define k_fifo_put_list(fifo, head, tail) \
1350 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001351
1352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001355 * This routine adds a list of data items to @a fifo in one operation.
1356 * The data items must be in a singly-linked list implemented using a
1357 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001358 * and must be re-initialized via sys_slist_init().
1359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * @note Can be called by ISRs.
1361 *
1362 * @param fifo Address of the fifo.
1363 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
1365 * @return N/A
1366 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001367#define k_fifo_put_slist(fifo, list) \
1368 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001369
1370/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001371 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001373 * This routine removes a data item from @a fifo in a "first in, first out"
1374 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1377 *
1378 * @param fifo Address of the fifo.
1379 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001380 * or one of the special values K_NO_WAIT and K_FOREVER.
1381 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001382 * @return Address of the data item if successful; NULL if returned
1383 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001384 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001385#define k_fifo_get(fifo, timeout) \
1386 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001388/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001389 * @brief Query a fifo to see if it has data available.
1390 *
1391 * Note that the data might be already gone by the time this function returns
1392 * if other threads is also trying to read from the fifo.
1393 *
1394 * @note Can be called by ISRs.
1395 *
1396 * @param fifo Address of the fifo.
1397 *
1398 * @return Non-zero if the fifo is empty.
1399 * @return 0 if data is available.
1400 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001401#define k_fifo_is_empty(fifo) \
1402 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001403
1404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001405 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001409 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001411 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001414 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001415 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001416 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417
Allan Stephensc98da842016-11-11 15:45:03 -05001418/**
1419 * @} end defgroup fifo_apis
1420 */
1421
1422/**
1423 * @cond INTERNAL_HIDDEN
1424 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425
1426struct k_lifo {
1427 _wait_q_t wait_q;
1428 void *list;
1429
Anas Nashif2f203c22016-12-18 06:57:45 -05001430 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431};
1432
Allan Stephensc98da842016-11-11 15:45:03 -05001433#define K_LIFO_INITIALIZER(obj) \
1434 { \
1435 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1436 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001437 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001438 }
1439
1440/**
1441 * INTERNAL_HIDDEN @endcond
1442 */
1443
1444/**
1445 * @defgroup lifo_apis Lifo APIs
1446 * @ingroup kernel_apis
1447 * @{
1448 */
1449
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456 *
1457 * @return N/A
1458 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001460
1461/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * This routine adds a data item to @a lifo. A lifo data item must be
1465 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1466 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * @note Can be called by ISRs.
1469 *
1470 * @param lifo Address of the lifo.
1471 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001472 *
1473 * @return N/A
1474 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001475extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476
1477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * This routine removes a data item from @a lifo in a "last in, first out"
1481 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1484 *
1485 * @param lifo Address of the lifo.
1486 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001487 * or one of the special values K_NO_WAIT and K_FOREVER.
1488 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001489 * @return Address of the data item if successful; NULL if returned
1490 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001491 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001492extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1493
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001497 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001499 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001501 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001504 struct k_lifo name \
1505 __in_section(_k_lifo, static, name) = \
1506 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507
Allan Stephensc98da842016-11-11 15:45:03 -05001508/**
1509 * @} end defgroup lifo_apis
1510 */
1511
1512/**
1513 * @cond INTERNAL_HIDDEN
1514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515
1516struct k_stack {
1517 _wait_q_t wait_q;
1518 uint32_t *base, *next, *top;
1519
Anas Nashif2f203c22016-12-18 06:57:45 -05001520 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001521};
1522
Allan Stephensc98da842016-11-11 15:45:03 -05001523#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1524 { \
1525 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1526 .base = stack_buffer, \
1527 .next = stack_buffer, \
1528 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001529 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001530 }
1531
1532/**
1533 * INTERNAL_HIDDEN @endcond
1534 */
1535
1536/**
1537 * @defgroup stack_apis Stack APIs
1538 * @ingroup kernel_apis
1539 * @{
1540 */
1541
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001542/**
1543 * @brief Initialize a stack.
1544 *
1545 * This routine initializes a stack object, prior to its first use.
1546 *
1547 * @param stack Address of the stack.
1548 * @param buffer Address of array used to hold stacked values.
1549 * @param num_entries Maximum number of values that can be stacked.
1550 *
1551 * @return N/A
1552 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001553extern void k_stack_init(struct k_stack *stack,
1554 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555
1556/**
1557 * @brief Push an element onto a stack.
1558 *
1559 * This routine adds a 32-bit value @a data to @a stack.
1560 *
1561 * @note Can be called by ISRs.
1562 *
1563 * @param stack Address of the stack.
1564 * @param data Value to push onto the stack.
1565 *
1566 * @return N/A
1567 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001569
1570/**
1571 * @brief Pop an element from a stack.
1572 *
1573 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1574 * manner and stores the value in @a data.
1575 *
1576 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1577 *
1578 * @param stack Address of the stack.
1579 * @param data Address of area to hold the value popped from the stack.
1580 * @param timeout Waiting period to obtain a value (in milliseconds),
1581 * or one of the special values K_NO_WAIT and K_FOREVER.
1582 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001583 * @retval 0 Element popped from stack.
1584 * @retval -EBUSY Returned without waiting.
1585 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001586 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1588
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001594 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * @param name Name of the stack.
1597 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001598 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001599#define K_STACK_DEFINE(name, stack_num_entries) \
1600 uint32_t __noinit \
1601 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001602 struct k_stack name \
1603 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001604 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1605 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001606
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001607/**
Allan Stephensc98da842016-11-11 15:45:03 -05001608 * @} end defgroup stack_apis
1609 */
1610
Allan Stephens6bba9b02016-11-16 14:56:54 -05001611struct k_work;
1612
Allan Stephensc98da842016-11-11 15:45:03 -05001613/**
1614 * @defgroup workqueue_apis Workqueue Thread APIs
1615 * @ingroup kernel_apis
1616 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617 */
1618
Allan Stephens6bba9b02016-11-16 14:56:54 -05001619/**
1620 * @typedef k_work_handler_t
1621 * @brief Work item handler function type.
1622 *
1623 * A work item's handler function is executed by a workqueue's thread
1624 * when the work item is processed by the workqueue.
1625 *
1626 * @param work Address of the work item.
1627 *
1628 * @return N/A
1629 */
1630typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631
1632/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001633 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001635
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001636struct k_work_q {
1637 struct k_fifo fifo;
1638};
1639
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001640enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001641 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001642};
1643
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001644struct k_work {
1645 void *_reserved; /* Used by k_fifo implementation. */
1646 k_work_handler_t handler;
1647 atomic_t flags[1];
1648};
1649
Allan Stephens6bba9b02016-11-16 14:56:54 -05001650struct k_delayed_work {
1651 struct k_work work;
1652 struct _timeout timeout;
1653 struct k_work_q *work_q;
1654};
1655
1656extern struct k_work_q k_sys_work_q;
1657
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001659 * INTERNAL_HIDDEN @endcond
1660 */
1661
1662/**
1663 * @brief Initialize a statically-defined work item.
1664 *
1665 * This macro can be used to initialize a statically-defined workqueue work
1666 * item, prior to its first use. For example,
1667 *
1668 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1669 *
1670 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001671 */
1672#define K_WORK_INITIALIZER(work_handler) \
1673 { \
1674 ._reserved = NULL, \
1675 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001676 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001677 }
1678
1679/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001680 * @brief Initialize a work item.
1681 *
1682 * This routine initializes a workqueue work item, prior to its first use.
1683 *
1684 * @param work Address of work item.
1685 * @param handler Function to invoke each time work item is processed.
1686 *
1687 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001688 */
1689static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1690{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001691 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001692 work->handler = handler;
1693}
1694
1695/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001696 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001697 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001698 * This routine submits work item @a work to be processed by workqueue
1699 * @a work_q. If the work item is already pending in the workqueue's queue
1700 * as a result of an earlier submission, this routine has no effect on the
1701 * work item. If the work item has already been processed, or is currently
1702 * being processed, its work is considered complete and the work item can be
1703 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001704 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001705 * @warning
1706 * A submitted work item must not be modified until it has been processed
1707 * by the workqueue.
1708 *
1709 * @note Can be called by ISRs.
1710 *
1711 * @param work_q Address of workqueue.
1712 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001713 *
1714 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001715 */
1716static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1717 struct k_work *work)
1718{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001719 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001720 k_fifo_put(&work_q->fifo, work);
1721 }
1722}
1723
1724/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001725 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001727 * This routine indicates if work item @a work is pending in a workqueue's
1728 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001730 * @note Can be called by ISRs.
1731 *
1732 * @param work Address of work item.
1733 *
1734 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001735 */
1736static inline int k_work_pending(struct k_work *work)
1737{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001738 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001739}
1740
1741/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001742 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001744 * This routine starts workqueue @a work_q. The workqueue spawns its work
1745 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001747 * @param work_q Address of workqueue.
1748 * @param stack Pointer to work queue thread's stack space.
1749 * @param stack_size Size of the work queue thread's stack (in bytes).
1750 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
1752 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001753 */
Allan Stephens904cf972016-10-07 13:59:23 -05001754extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001755 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001756
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001758 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001760 * This routine initializes a workqueue delayed work item, prior to
1761 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001763 * @param work Address of delayed work item.
1764 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765 *
1766 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001767 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001768extern void k_delayed_work_init(struct k_delayed_work *work,
1769 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001770
1771/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001772 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001773 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001774 * This routine schedules work item @a work to be processed by workqueue
1775 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1776 * an asychronous countdown for the work item and then returns to the caller.
1777 * Only when the countdown completes is the work item actually submitted to
1778 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001779 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001780 * Submitting a previously submitted delayed work item that is still
1781 * counting down cancels the existing submission and restarts the countdown
1782 * using the new delay. If the work item is currently pending on the
1783 * workqueue's queue because the countdown has completed it is too late to
1784 * resubmit the item, and resubmission fails without impacting the work item.
1785 * If the work item has already been processed, or is currently being processed,
1786 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001787 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001788 * @warning
1789 * A delayed work item must not be modified until it has been processed
1790 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001792 * @note Can be called by ISRs.
1793 *
1794 * @param work_q Address of workqueue.
1795 * @param work Address of delayed work item.
1796 * @param delay Delay before submitting the work item (in milliseconds).
1797 *
1798 * @retval 0 Work item countdown started.
1799 * @retval -EINPROGRESS Work item is already pending.
1800 * @retval -EINVAL Work item is being processed or has completed its work.
1801 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001803extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1804 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001805 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806
1807/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001808 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001809 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001810 * This routine cancels the submission of delayed work item @a work.
1811 * A delayed work item can only be cancelled while its countdown is still
1812 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001814 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001815 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001816 * @param work Address of delayed work item.
1817 *
1818 * @retval 0 Work item countdown cancelled.
1819 * @retval -EINPROGRESS Work item is already pending.
1820 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001822extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825 * @brief Submit a work item to the system workqueue.
1826 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001827 * This routine submits work item @a work to be processed by the system
1828 * workqueue. If the work item is already pending in the workqueue's queue
1829 * as a result of an earlier submission, this routine has no effect on the
1830 * work item. If the work item has already been processed, or is currently
1831 * being processed, its work is considered complete and the work item can be
1832 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001833 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001834 * @warning
1835 * Work items submitted to the system workqueue should avoid using handlers
1836 * that block or yield since this may prevent the system workqueue from
1837 * processing other work items in a timely manner.
1838 *
1839 * @note Can be called by ISRs.
1840 *
1841 * @param work Address of work item.
1842 *
1843 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001844 */
1845static inline void k_work_submit(struct k_work *work)
1846{
1847 k_work_submit_to_queue(&k_sys_work_q, work);
1848}
1849
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001851 * @brief Submit a delayed work item to the system workqueue.
1852 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001853 * This routine schedules work item @a work to be processed by the system
1854 * workqueue after a delay of @a delay milliseconds. The routine initiates
1855 * an asychronous countdown for the work item and then returns to the caller.
1856 * Only when the countdown completes is the work item actually submitted to
1857 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001859 * Submitting a previously submitted delayed work item that is still
1860 * counting down cancels the existing submission and restarts the countdown
1861 * using the new delay. If the work item is currently pending on the
1862 * workqueue's queue because the countdown has completed it is too late to
1863 * resubmit the item, and resubmission fails without impacting the work item.
1864 * If the work item has already been processed, or is currently being processed,
1865 * its work is considered complete and the work item can be resubmitted.
1866 *
1867 * @warning
1868 * Work items submitted to the system workqueue should avoid using handlers
1869 * that block or yield since this may prevent the system workqueue from
1870 * processing other work items in a timely manner.
1871 *
1872 * @note Can be called by ISRs.
1873 *
1874 * @param work Address of delayed work item.
1875 * @param delay Delay before submitting the work item (in milliseconds).
1876 *
1877 * @retval 0 Work item countdown started.
1878 * @retval -EINPROGRESS Work item is already pending.
1879 * @retval -EINVAL Work item is being processed or has completed its work.
1880 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001881 */
1882static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001883 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001885 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001886}
1887
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001889 * @brief Get time remaining before a delayed work gets scheduled.
1890 *
1891 * This routine computes the (approximate) time remaining before a
1892 * delayed work gets executed. If the delayed work is not waiting to be
1893 * schedules, it returns zero.
1894 *
1895 * @param work Delayed work item.
1896 *
1897 * @return Remaining time (in milliseconds).
1898 */
1899static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1900{
1901 return _timeout_remaining_get(&work->timeout);
1902}
1903
1904/**
Allan Stephensc98da842016-11-11 15:45:03 -05001905 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001906 */
1907
Allan Stephensc98da842016-11-11 15:45:03 -05001908/**
1909 * @cond INTERNAL_HIDDEN
1910 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911
1912struct k_mutex {
1913 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001914 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001915 uint32_t lock_count;
1916 int owner_orig_prio;
1917#ifdef CONFIG_OBJECT_MONITOR
1918 int num_lock_state_changes;
1919 int num_conflicts;
1920#endif
1921
Anas Nashif2f203c22016-12-18 06:57:45 -05001922 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923};
1924
1925#ifdef CONFIG_OBJECT_MONITOR
1926#define _MUTEX_INIT_OBJECT_MONITOR \
1927 .num_lock_state_changes = 0, .num_conflicts = 0,
1928#else
1929#define _MUTEX_INIT_OBJECT_MONITOR
1930#endif
1931
1932#define K_MUTEX_INITIALIZER(obj) \
1933 { \
1934 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1935 .owner = NULL, \
1936 .lock_count = 0, \
1937 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1938 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001939 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940 }
1941
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001942/**
Allan Stephensc98da842016-11-11 15:45:03 -05001943 * INTERNAL_HIDDEN @endcond
1944 */
1945
1946/**
1947 * @defgroup mutex_apis Mutex APIs
1948 * @ingroup kernel_apis
1949 * @{
1950 */
1951
1952/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001953 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001955 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001957 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001962 struct k_mutex name \
1963 __in_section(_k_mutex, static, name) = \
1964 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * Upon completion, the mutex is available and does not have an owner.
1972 *
1973 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
1975 * @return N/A
1976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978
1979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001982 * This routine locks @a mutex. If the mutex is locked by another thread,
1983 * the calling thread waits until the mutex becomes available or until
1984 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * A thread is permitted to lock a mutex it has already locked. The operation
1987 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * @param mutex Address of the mutex.
1990 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001991 * or one of the special values K_NO_WAIT and K_FOREVER.
1992 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001993 * @retval 0 Mutex locked.
1994 * @retval -EBUSY Returned without waiting.
1995 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998
1999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002000 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * This routine unlocks @a mutex. The mutex must already be locked by the
2003 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
2005 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 * thread.
2008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
2011 * @return N/A
2012 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002013extern void k_mutex_unlock(struct k_mutex *mutex);
2014
Allan Stephensc98da842016-11-11 15:45:03 -05002015/**
2016 * @} end defgroup mutex_apis
2017 */
2018
2019/**
2020 * @cond INTERNAL_HIDDEN
2021 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002022
2023struct k_sem {
2024 _wait_q_t wait_q;
2025 unsigned int count;
2026 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002027 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028
Anas Nashif2f203c22016-12-18 06:57:45 -05002029 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030};
2031
Allan Stephensc98da842016-11-11 15:45:03 -05002032#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2033 { \
2034 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2035 .count = initial_count, \
2036 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002037 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002038 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002039 }
2040
2041/**
2042 * INTERNAL_HIDDEN @endcond
2043 */
2044
2045/**
2046 * @defgroup semaphore_apis Semaphore APIs
2047 * @ingroup kernel_apis
2048 * @{
2049 */
2050
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002051/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002052 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @param sem Address of the semaphore.
2057 * @param initial_count Initial semaphore count.
2058 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002059 *
2060 * @return N/A
2061 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2063 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002064
2065/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2071 *
2072 * @param sem Address of the semaphore.
2073 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002074 * or one of the special values K_NO_WAIT and K_FOREVER.
2075 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002076 * @note When porting code from the nanokernel legacy API to the new API, be
2077 * careful with the return value of this function. The return value is the
2078 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2079 * non-zero means failure, while the nano_sem_take family returns 1 for success
2080 * and 0 for failure.
2081 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002082 * @retval 0 Semaphore taken.
2083 * @retval -EBUSY Returned without waiting.
2084 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002085 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002086extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002087
2088/**
2089 * @brief Give a semaphore.
2090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * This routine gives @a sem, unless the semaphore is already at its maximum
2092 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002097 *
2098 * @return N/A
2099 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100extern void k_sem_give(struct k_sem *sem);
2101
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002102/**
2103 * @brief Reset a semaphore's count to zero.
2104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002108 *
2109 * @return N/A
2110 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002111static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112{
2113 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114}
2115
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002116/**
2117 * @brief Get a semaphore's count.
2118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002124 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002125static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002126{
2127 return sem->count;
2128}
2129
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002134 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002135 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * @param name Name of the semaphore.
2138 * @param initial_count Initial semaphore count.
2139 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002140 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002141#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002142 struct k_sem name \
2143 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144 K_SEM_INITIALIZER(name, initial_count, count_limit)
2145
Allan Stephensc98da842016-11-11 15:45:03 -05002146/**
2147 * @} end defgroup semaphore_apis
2148 */
2149
2150/**
2151 * @defgroup alert_apis Alert APIs
2152 * @ingroup kernel_apis
2153 * @{
2154 */
2155
Allan Stephens5eceb852016-11-16 10:16:30 -05002156/**
2157 * @typedef k_alert_handler_t
2158 * @brief Alert handler function type.
2159 *
2160 * An alert's alert handler function is invoked by the system workqueue
2161 * when the alert is signalled. The alert handler function is optional,
2162 * and is only invoked if the alert has been initialized with one.
2163 *
2164 * @param alert Address of the alert.
2165 *
2166 * @return 0 if alert has been consumed; non-zero if alert should pend.
2167 */
2168typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002169
2170/**
2171 * @} end defgroup alert_apis
2172 */
2173
2174/**
2175 * @cond INTERNAL_HIDDEN
2176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002178#define K_ALERT_DEFAULT NULL
2179#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002181struct k_alert {
2182 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183 atomic_t send_count;
2184 struct k_work work_item;
2185 struct k_sem sem;
2186
Anas Nashif2f203c22016-12-18 06:57:45 -05002187 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188};
2189
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002190extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002192#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002194 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002196 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002197 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002198 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002199 }
2200
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002201/**
Allan Stephensc98da842016-11-11 15:45:03 -05002202 * INTERNAL_HIDDEN @endcond
2203 */
2204
2205/**
2206 * @addtogroup alert_apis
2207 * @{
2208 */
2209
2210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002211 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002212 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002213 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002215 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * @param name Name of the alert.
2218 * @param alert_handler Action to take when alert is sent. Specify either
2219 * the address of a function to be invoked by the system workqueue
2220 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2221 * K_ALERT_DEFAULT (which causes the alert to pend).
2222 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002224#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002225 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002226 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002227 K_ALERT_INITIALIZER(name, alert_handler, \
2228 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002229
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * @param alert Address of the alert.
2236 * @param handler Action to take when alert is sent. Specify either the address
2237 * of a function to be invoked by the system workqueue thread,
2238 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2239 * K_ALERT_DEFAULT (which causes the alert to pend).
2240 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 *
2242 * @return N/A
2243 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002244extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2245 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246
2247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002250 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002252 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2253 *
2254 * @param alert Address of the alert.
2255 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 * or one of the special values K_NO_WAIT and K_FOREVER.
2257 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002258 * @retval 0 Alert received.
2259 * @retval -EBUSY Returned without waiting.
2260 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002262extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263
2264/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002267 * This routine signals @a alert. The action specified for @a alert will
2268 * be taken, which may trigger the execution of an alert handler function
2269 * and/or cause the alert to pend (assuming the alert has not reached its
2270 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002272 * @note Can be called by ISRs.
2273 *
2274 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
2276 * @return N/A
2277 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002278extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279
2280/**
Allan Stephensc98da842016-11-11 15:45:03 -05002281 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 */
2283
Allan Stephensc98da842016-11-11 15:45:03 -05002284/**
2285 * @cond INTERNAL_HIDDEN
2286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287
2288struct k_msgq {
2289 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002290 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002291 uint32_t max_msgs;
2292 char *buffer_start;
2293 char *buffer_end;
2294 char *read_ptr;
2295 char *write_ptr;
2296 uint32_t used_msgs;
2297
Anas Nashif2f203c22016-12-18 06:57:45 -05002298 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299};
2300
Peter Mitsis1da807e2016-10-06 11:36:59 -04002301#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302 { \
2303 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002304 .max_msgs = q_max_msgs, \
2305 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002307 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308 .read_ptr = q_buffer, \
2309 .write_ptr = q_buffer, \
2310 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002311 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 }
2313
Peter Mitsis1da807e2016-10-06 11:36:59 -04002314/**
Allan Stephensc98da842016-11-11 15:45:03 -05002315 * INTERNAL_HIDDEN @endcond
2316 */
2317
2318/**
2319 * @defgroup msgq_apis Message Queue APIs
2320 * @ingroup kernel_apis
2321 * @{
2322 */
2323
2324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2328 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002329 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2330 * message is similarly aligned to this boundary, @a q_msg_size must also be
2331 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002333 * The message queue can be accessed outside the module where it is defined
2334 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002335 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002336 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 * @param q_name Name of the message queue.
2339 * @param q_msg_size Message size (in bytes).
2340 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002341 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002342 */
2343#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2344 static char __noinit __aligned(q_align) \
2345 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002346 struct k_msgq q_name \
2347 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002348 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2349 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350
Peter Mitsisd7a37502016-10-13 11:37:40 -04002351/**
2352 * @brief Initialize a message queue.
2353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * This routine initializes a message queue object, prior to its first use.
2355 *
Allan Stephensda827222016-11-09 14:23:58 -06002356 * The message queue's ring buffer must contain space for @a max_msgs messages,
2357 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2358 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2359 * that each message is similarly aligned to this boundary, @a q_msg_size
2360 * must also be a multiple of N.
2361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param q Address of the message queue.
2363 * @param buffer Pointer to ring buffer that holds queued messages.
2364 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002365 * @param max_msgs Maximum number of messages that can be queued.
2366 *
2367 * @return N/A
2368 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002369extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002370 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002371
2372/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002373 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002376 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002377 * @note Can be called by ISRs.
2378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * @param q Address of the message queue.
2380 * @param data Pointer to the message.
2381 * @param timeout Waiting period to add the message (in milliseconds),
2382 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002383 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002384 * @retval 0 Message sent.
2385 * @retval -ENOMSG Returned without waiting or queue purged.
2386 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002387 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002389
2390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * This routine receives a message from message queue @a q in a "first in,
2394 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002395 *
Allan Stephensc98da842016-11-11 15:45:03 -05002396 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * @param q Address of the message queue.
2399 * @param data Address of area to hold the received message.
2400 * @param timeout Waiting period to receive the message (in milliseconds),
2401 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002402 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002403 * @retval 0 Message received.
2404 * @retval -ENOMSG Returned without waiting.
2405 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002406 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002408
2409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * This routine discards all unreceived messages in a message queue's ring
2413 * buffer. Any threads that are blocked waiting to send a message to the
2414 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002417 *
2418 * @return N/A
2419 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420extern void k_msgq_purge(struct k_msgq *q);
2421
Peter Mitsis67be2492016-10-07 11:44:34 -04002422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * This routine returns the number of unused entries in a message queue's
2426 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * @param q Address of the message queue.
2429 *
2430 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002431 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002432static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002433{
2434 return q->max_msgs - q->used_msgs;
2435}
2436
Peter Mitsisd7a37502016-10-13 11:37:40 -04002437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002440 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @param q Address of the message queue.
2443 *
2444 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002445 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002446static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447{
2448 return q->used_msgs;
2449}
2450
Allan Stephensc98da842016-11-11 15:45:03 -05002451/**
2452 * @} end defgroup msgq_apis
2453 */
2454
2455/**
2456 * @defgroup mem_pool_apis Memory Pool APIs
2457 * @ingroup kernel_apis
2458 * @{
2459 */
2460
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002462 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002463 void *addr_in_pool;
2464 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002465 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466};
2467
Allan Stephensc98da842016-11-11 15:45:03 -05002468/**
2469 * @} end defgroup mem_pool_apis
2470 */
2471
2472/**
2473 * @defgroup mailbox_apis Mailbox APIs
2474 * @ingroup kernel_apis
2475 * @{
2476 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477
2478struct k_mbox_msg {
2479 /** internal use only - needed for legacy API support */
2480 uint32_t _mailbox;
2481 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002482 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483 /** application-defined information value */
2484 uint32_t info;
2485 /** sender's message data buffer */
2486 void *tx_data;
2487 /** internal use only - needed for legacy API support */
2488 void *_rx_data;
2489 /** message data block descriptor */
2490 struct k_mem_block tx_block;
2491 /** source thread id */
2492 k_tid_t rx_source_thread;
2493 /** target thread id */
2494 k_tid_t tx_target_thread;
2495 /** internal use only - thread waiting on send (may be a dummy) */
2496 k_tid_t _syncing_thread;
2497#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2498 /** internal use only - semaphore used during asynchronous send */
2499 struct k_sem *_async_sem;
2500#endif
2501};
2502
Allan Stephensc98da842016-11-11 15:45:03 -05002503/**
2504 * @cond INTERNAL_HIDDEN
2505 */
2506
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507struct k_mbox {
2508 _wait_q_t tx_msg_queue;
2509 _wait_q_t rx_msg_queue;
2510
Anas Nashif2f203c22016-12-18 06:57:45 -05002511 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512};
2513
2514#define K_MBOX_INITIALIZER(obj) \
2515 { \
2516 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2517 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002518 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519 }
2520
Peter Mitsis12092702016-10-14 12:57:23 -04002521/**
Allan Stephensc98da842016-11-11 15:45:03 -05002522 * INTERNAL_HIDDEN @endcond
2523 */
2524
2525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002530 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002535 struct k_mbox name \
2536 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537 K_MBOX_INITIALIZER(name) \
2538
Peter Mitsis12092702016-10-14 12:57:23 -04002539/**
2540 * @brief Initialize a mailbox.
2541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * This routine initializes a mailbox object, prior to its first use.
2543 *
2544 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002545 *
2546 * @return N/A
2547 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548extern void k_mbox_init(struct k_mbox *mbox);
2549
Peter Mitsis12092702016-10-14 12:57:23 -04002550/**
2551 * @brief Send a mailbox message in a synchronous manner.
2552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * This routine sends a message to @a mbox and waits for a receiver to both
2554 * receive and process it. The message data may be in a buffer, in a memory
2555 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @param mbox Address of the mailbox.
2558 * @param tx_msg Address of the transmit message descriptor.
2559 * @param timeout Waiting period for the message to be received (in
2560 * milliseconds), or one of the special values K_NO_WAIT
2561 * and K_FOREVER. Once the message has been received,
2562 * this routine waits as long as necessary for the message
2563 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002564 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002565 * @retval 0 Message sent.
2566 * @retval -ENOMSG Returned without waiting.
2567 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002568 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002569extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002571
Peter Mitsis12092702016-10-14 12:57:23 -04002572/**
2573 * @brief Send a mailbox message in an asynchronous manner.
2574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * This routine sends a message to @a mbox without waiting for a receiver
2576 * to process it. The message data may be in a buffer, in a memory pool block,
2577 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2578 * will be given when the message has been both received and completely
2579 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * @param mbox Address of the mailbox.
2582 * @param tx_msg Address of the transmit message descriptor.
2583 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002584 *
2585 * @return N/A
2586 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002587extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002588 struct k_sem *sem);
2589
Peter Mitsis12092702016-10-14 12:57:23 -04002590/**
2591 * @brief Receive a mailbox message.
2592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * This routine receives a message from @a mbox, then optionally retrieves
2594 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @param mbox Address of the mailbox.
2597 * @param rx_msg Address of the receive message descriptor.
2598 * @param buffer Address of the buffer to receive data, or NULL to defer data
2599 * retrieval and message disposal until later.
2600 * @param timeout Waiting period for a message to be received (in
2601 * milliseconds), or one of the special values K_NO_WAIT
2602 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002603 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002604 * @retval 0 Message received.
2605 * @retval -ENOMSG Returned without waiting.
2606 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002607 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002608extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002610
2611/**
2612 * @brief Retrieve mailbox message data into a buffer.
2613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002614 * This routine completes the processing of a received message by retrieving
2615 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002616 *
2617 * Alternatively, this routine can be used to dispose of a received message
2618 * without retrieving its data.
2619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * @param rx_msg Address of the receive message descriptor.
2621 * @param buffer Address of the buffer to receive data, or NULL to discard
2622 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002623 *
2624 * @return N/A
2625 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002626extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002627
2628/**
2629 * @brief Retrieve mailbox message data into a memory pool block.
2630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * This routine completes the processing of a received message by retrieving
2632 * its data into a memory pool block, then disposing of the message.
2633 * The memory pool block that results from successful retrieval must be
2634 * returned to the pool once the data has been processed, even in cases
2635 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002636 *
2637 * Alternatively, this routine can be used to dispose of a received message
2638 * without retrieving its data. In this case there is no need to return a
2639 * memory pool block to the pool.
2640 *
2641 * This routine allocates a new memory pool block for the data only if the
2642 * data is not already in one. If a new block cannot be allocated, the routine
2643 * returns a failure code and the received message is left unchanged. This
2644 * permits the caller to reattempt data retrieval at a later time or to dispose
2645 * of the received message without retrieving its data.
2646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * @param rx_msg Address of a receive message descriptor.
2648 * @param pool Address of memory pool, or NULL to discard data.
2649 * @param block Address of the area to hold memory pool block info.
2650 * @param timeout Waiting period to wait for a memory pool block (in
2651 * milliseconds), or one of the special values K_NO_WAIT
2652 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002653 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002654 * @retval 0 Data retrieved.
2655 * @retval -ENOMEM Returned without waiting.
2656 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002657 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002658extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002659 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660 struct k_mem_block *block, int32_t timeout);
2661
Allan Stephensc98da842016-11-11 15:45:03 -05002662/**
2663 * @} end defgroup mailbox_apis
2664 */
2665
2666/**
2667 * @cond INTERNAL_HIDDEN
2668 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
2670struct k_pipe {
2671 unsigned char *buffer; /* Pipe buffer: may be NULL */
2672 size_t size; /* Buffer size */
2673 size_t bytes_used; /* # bytes used in buffer */
2674 size_t read_index; /* Where in buffer to read from */
2675 size_t write_index; /* Where in buffer to write */
2676
2677 struct {
2678 _wait_q_t readers; /* Reader wait queue */
2679 _wait_q_t writers; /* Writer wait queue */
2680 } wait_q;
2681
Anas Nashif2f203c22016-12-18 06:57:45 -05002682 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683};
2684
Peter Mitsise5d9c582016-10-14 14:44:57 -04002685#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686 { \
2687 .buffer = pipe_buffer, \
2688 .size = pipe_buffer_size, \
2689 .bytes_used = 0, \
2690 .read_index = 0, \
2691 .write_index = 0, \
2692 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2693 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002694 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695 }
2696
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002697/**
Allan Stephensc98da842016-11-11 15:45:03 -05002698 * INTERNAL_HIDDEN @endcond
2699 */
2700
2701/**
2702 * @defgroup pipe_apis Pipe APIs
2703 * @ingroup kernel_apis
2704 * @{
2705 */
2706
2707/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002710 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002711 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002712 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * @param name Name of the pipe.
2715 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2716 * or zero if no ring buffer is used.
2717 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002719#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2720 static unsigned char __noinit __aligned(pipe_align) \
2721 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002722 struct k_pipe name \
2723 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002724 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * This routine initializes a pipe object, prior to its first use.
2730 *
2731 * @param pipe Address of the pipe.
2732 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2733 * is used.
2734 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2735 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 *
2737 * @return N/A
2738 */
2739extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2740 size_t size);
2741
2742/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002743 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * @param pipe Address of the pipe.
2748 * @param data Address of data to write.
2749 * @param bytes_to_write Size of data (in bytes).
2750 * @param bytes_written Address of area to hold the number of bytes written.
2751 * @param min_xfer Minimum number of bytes to write.
2752 * @param timeout Waiting period to wait for the data to be written (in
2753 * milliseconds), or one of the special values K_NO_WAIT
2754 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002756 * @retval 0 At least @a min_xfer bytes of data were written.
2757 * @retval -EIO Returned without waiting; zero data bytes were written.
2758 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002761extern int k_pipe_put(struct k_pipe *pipe, void *data,
2762 size_t bytes_to_write, size_t *bytes_written,
2763 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764
2765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002766 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @param pipe Address of the pipe.
2771 * @param data Address to place the data read from pipe.
2772 * @param bytes_to_read Maximum number of data bytes to read.
2773 * @param bytes_read Address of area to hold the number of bytes read.
2774 * @param min_xfer Minimum number of data bytes to read.
2775 * @param timeout Waiting period to wait for the data to be read (in
2776 * milliseconds), or one of the special values K_NO_WAIT
2777 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002779 * @retval 0 At least @a min_xfer bytes of data were read.
2780 * @retval -EIO Returned without waiting; zero data bytes were read.
2781 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002784extern int k_pipe_get(struct k_pipe *pipe, void *data,
2785 size_t bytes_to_read, size_t *bytes_read,
2786 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787
2788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002789 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002791 * This routine writes the data contained in a memory block to @a pipe.
2792 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796 * @param block Memory block containing data to send
2797 * @param size Number of data bytes in memory block to send
2798 * @param sem Semaphore to signal upon completion (else NULL)
2799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 */
2802extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2803 size_t size, struct k_sem *sem);
2804
2805/**
Allan Stephensc98da842016-11-11 15:45:03 -05002806 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807 */
2808
Allan Stephensc98da842016-11-11 15:45:03 -05002809/**
2810 * @cond INTERNAL_HIDDEN
2811 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002813struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002814 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002815 uint32_t num_blocks;
2816 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 char *buffer;
2818 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002819 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820
Anas Nashif2f203c22016-12-18 06:57:45 -05002821 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822};
2823
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002824#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2825 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826 { \
2827 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002828 .num_blocks = slab_num_blocks, \
2829 .block_size = slab_block_size, \
2830 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831 .free_list = NULL, \
2832 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002833 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 }
2835
Peter Mitsis578f9112016-10-07 13:50:31 -04002836/**
Allan Stephensc98da842016-11-11 15:45:03 -05002837 * INTERNAL_HIDDEN @endcond
2838 */
2839
2840/**
2841 * @defgroup mem_slab_apis Memory Slab APIs
2842 * @ingroup kernel_apis
2843 * @{
2844 */
2845
2846/**
Allan Stephensda827222016-11-09 14:23:58 -06002847 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002848 *
Allan Stephensda827222016-11-09 14:23:58 -06002849 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002851 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2852 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002853 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002854 *
Allan Stephensda827222016-11-09 14:23:58 -06002855 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002858 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param name Name of the memory slab.
2861 * @param slab_block_size Size of each memory block (in bytes).
2862 * @param slab_num_blocks Number memory blocks.
2863 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002864 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002865#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2866 char __noinit __aligned(slab_align) \
2867 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2868 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002869 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002870 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2871 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002873/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002874 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002877 *
Allan Stephensda827222016-11-09 14:23:58 -06002878 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2879 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2880 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2881 * To ensure that each memory block is similarly aligned to this boundary,
2882 * @a slab_block_size must also be a multiple of N.
2883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @param slab Address of the memory slab.
2885 * @param buffer Pointer to buffer used for the memory blocks.
2886 * @param block_size Size of each memory block (in bytes).
2887 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002888 *
2889 * @return N/A
2890 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002891extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002892 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002893
2894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * @param slab Address of the memory slab.
2900 * @param mem Pointer to block address area.
2901 * @param timeout Maximum time to wait for operation to complete
2902 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2903 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002904 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002905 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002907 * @retval -ENOMEM Returned without waiting.
2908 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002909 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002910extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2911 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002912
2913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * This routine releases a previously allocated memory block back to its
2917 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @param slab Address of the memory slab.
2920 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002921 *
2922 * @return N/A
2923 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002924extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002925
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002926/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * This routine gets the number of memory blocks that are currently
2930 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002935 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002936static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002938 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939}
2940
Peter Mitsisc001aa82016-10-13 13:53:37 -04002941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * This routine gets the number of memory blocks that are currently
2945 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002947 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002950 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002951static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002952{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002953 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002954}
2955
Allan Stephensc98da842016-11-11 15:45:03 -05002956/**
2957 * @} end defgroup mem_slab_apis
2958 */
2959
2960/**
2961 * @cond INTERNAL_HIDDEN
2962 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002964/*
2965 * Memory pool requires a buffer and two arrays of structures for the
2966 * memory block accounting:
2967 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2968 * status of four blocks of memory.
2969 */
2970struct k_mem_pool_quad_block {
2971 char *mem_blocks; /* pointer to the first of four memory blocks */
2972 uint32_t mem_status; /* four bits. If bit is set, memory block is
2973 allocated */
2974};
2975/*
2976 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2977 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2978 * block size is 4 times less than the previous one and thus requires 4 times
2979 * bigger array of k_mem_pool_quad_block structures to keep track of the
2980 * memory blocks.
2981 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002983/*
2984 * The array of k_mem_pool_block_set keeps the information of each array of
2985 * k_mem_pool_quad_block structures
2986 */
2987struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002988 size_t block_size; /* memory block size */
2989 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002990 struct k_mem_pool_quad_block *quad_block;
2991 int count;
2992};
2993
2994/* Memory pool descriptor */
2995struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002996 size_t max_block_size;
2997 size_t min_block_size;
2998 uint32_t nr_of_maxblocks;
2999 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003000 struct k_mem_pool_block_set *block_set;
3001 char *bufblock;
3002 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05003003 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004};
3005
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003006#ifdef CONFIG_ARM
3007#define _SECTION_TYPE_SIGN "%"
3008#else
3009#define _SECTION_TYPE_SIGN "@"
3010#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003012/*
3013 * Static memory pool initialization
3014 */
Allan Stephensc98da842016-11-11 15:45:03 -05003015
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003016/*
3017 * Use .altmacro to be able to recalculate values and pass them as string
3018 * arguments when calling assembler macros resursively
3019 */
3020__asm__(".altmacro\n\t");
3021
3022/*
3023 * Recursively calls a macro
3024 * The followig global symbols need to be initialized:
3025 * __memory_pool_max_block_size - maximal size of the memory block
3026 * __memory_pool_min_block_size - minimal size of the memory block
3027 * Notes:
3028 * Global symbols are used due the fact that assembler macro allows only
3029 * one argument be passed with the % conversion
3030 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3031 * is used instead of / 4.
3032 * n_max argument needs to go first in the invoked macro, as some
3033 * assemblers concatenate \name and %(\n_max * 4) arguments
3034 * if \name goes first
3035 */
3036__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3037 ".ifge __memory_pool_max_block_size >> 2 -"
3038 " __memory_pool_min_block_size\n\t\t"
3039 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3040 "\\macro_name %(\\n_max * 4) \\name\n\t"
3041 ".endif\n\t"
3042 ".endm\n");
3043
3044/*
3045 * Build quad blocks
3046 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3047 * structures and recursively calls itself for the next array, 4 times
3048 * larger.
3049 * The followig global symbols need to be initialized:
3050 * __memory_pool_max_block_size - maximal size of the memory block
3051 * __memory_pool_min_block_size - minimal size of the memory block
3052 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3053 */
3054__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003055 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003056 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3057 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3058 ".if \\n_max % 4\n\t\t"
3059 ".skip __memory_pool_quad_block_size\n\t"
3060 ".endif\n\t"
3061 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3062 ".endm\n");
3063
3064/*
3065 * Build block sets and initialize them
3066 * Macro initializes the k_mem_pool_block_set structure and
3067 * recursively calls itself for the next one.
3068 * The followig global symbols need to be initialized:
3069 * __memory_pool_max_block_size - maximal size of the memory block
3070 * __memory_pool_min_block_size - minimal size of the memory block
3071 * __memory_pool_block_set_count, the number of the elements in the
3072 * block set array must be set to 0. Macro calculates it's real
3073 * value.
3074 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3075 * structures, _build_quad_blocks must be called prior it.
3076 */
3077__asm__(".macro _build_block_set n_max, name\n\t"
3078 ".int __memory_pool_max_block_size\n\t" /* block_size */
3079 ".if \\n_max % 4\n\t\t"
3080 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3081 ".else\n\t\t"
3082 ".int \\n_max >> 2\n\t"
3083 ".endif\n\t"
3084 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3085 ".int 0\n\t" /* count */
3086 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3087 "__do_recurse _build_block_set \\name \\n_max\n\t"
3088 ".endm\n");
3089
3090/*
3091 * Build a memory pool structure and initialize it
3092 * Macro uses __memory_pool_block_set_count global symbol,
3093 * block set addresses and buffer address, it may be called only after
3094 * _build_block_set
3095 */
3096__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003097 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003098 _SECTION_TYPE_SIGN "progbits\n\t"
3099 ".globl \\name\n\t"
3100 "\\name:\n\t"
3101 ".int \\max_size\n\t" /* max_block_size */
3102 ".int \\min_size\n\t" /* min_block_size */
3103 ".int \\n_max\n\t" /* nr_of_maxblocks */
3104 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3105 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3106 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3107 ".int 0\n\t" /* wait_q->head */
3108 ".int 0\n\t" /* wait_q->next */
3109 ".popsection\n\t"
3110 ".endm\n");
3111
3112#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3113 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3114 _SECTION_TYPE_SIGN "progbits\n\t"); \
3115 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3116 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3117 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3118 STRINGIFY(name) "\n\t"); \
3119 __asm__(".popsection\n\t")
3120
3121#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3122 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3123 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3124 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3125 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003126 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003127 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3128 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3129 STRINGIFY(name) "\n\t"); \
3130 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3131 __asm__(".int __memory_pool_block_set_count\n\t"); \
3132 __asm__(".popsection\n\t"); \
3133 extern uint32_t _mem_pool_block_set_count_##name; \
3134 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3135
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003136#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3137 char __noinit __aligned(align) \
3138 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003139
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003140/*
3141 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3142 * to __memory_pool_quad_block_size absolute symbol.
3143 * This function does not get called, but compiler calculates the value and
3144 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3145 */
3146static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3147{
3148 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003149#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003150 "__memory_pool_quad_block_size = %0\n\t"
3151#else
3152 "__memory_pool_quad_block_size = %c0\n\t"
3153#endif
3154 :
3155 : "n"(sizeof(struct k_mem_pool_quad_block)));
3156}
3157
3158/**
Allan Stephensc98da842016-11-11 15:45:03 -05003159 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003160 */
3161
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003162/**
Allan Stephensc98da842016-11-11 15:45:03 -05003163 * @addtogroup mem_pool_apis
3164 * @{
3165 */
3166
3167/**
3168 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3171 * long. The memory pool allows blocks to be repeatedly partitioned into
3172 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3173 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003174 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003176 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003177 * If the pool is to be accessed outside the module where it is defined, it
3178 * can be declared via
3179 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003180 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @param name Name of the memory pool.
3183 * @param min_size Size of the smallest blocks in the pool (in bytes).
3184 * @param max_size Size of the largest blocks in the pool (in bytes).
3185 * @param n_max Number of maximum sized blocks in the pool.
3186 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003187 */
3188#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003189 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3190 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003191 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003192 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3193 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3194 extern struct k_mem_pool name
3195
Peter Mitsis937042c2016-10-13 13:18:26 -04003196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * @param pool Address of the memory pool.
3202 * @param block Pointer to block descriptor for the allocated memory.
3203 * @param size Amount of memory to allocate (in bytes).
3204 * @param timeout Maximum time to wait for operation to complete
3205 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3206 * or K_FOREVER to wait as long as necessary.
3207 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003208 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003210 * @retval -ENOMEM Returned without waiting.
3211 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003212 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003213extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003214 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003215
3216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003219 * This routine releases a previously allocated memory block back to its
3220 * memory pool.
3221 *
3222 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003223 *
3224 * @return N/A
3225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003227
3228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * This routine instructs a memory pool to concatenate unused memory blocks
3232 * into larger blocks wherever possible. Manually defragmenting the memory
3233 * pool may speed up future allocations of memory blocks by eliminating the
3234 * need for the memory pool to perform an automatic partial defragmentation.
3235 *
3236 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003237 *
3238 * @return N/A
3239 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003240extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003241
3242/**
Allan Stephensc98da842016-11-11 15:45:03 -05003243 * @} end addtogroup mem_pool_apis
3244 */
3245
3246/**
3247 * @defgroup heap_apis Heap Memory Pool APIs
3248 * @ingroup kernel_apis
3249 * @{
3250 */
3251
3252/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003256 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003261 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003262extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003263
3264/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003265 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003266 *
3267 * This routine provides traditional free() semantics. The memory being
3268 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003269 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003270 * If @a ptr is NULL, no operation is performed.
3271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003273 *
3274 * @return N/A
3275 */
3276extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277
Allan Stephensc98da842016-11-11 15:45:03 -05003278/**
3279 * @} end defgroup heap_apis
3280 */
3281
Benjamin Walshacc68c12017-01-29 18:57:45 -05003282/* polling API - PRIVATE */
3283
Benjamin Walshb0179862017-02-02 16:39:57 -05003284#ifdef CONFIG_POLL
3285#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3286#else
3287#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3288#endif
3289
Benjamin Walshacc68c12017-01-29 18:57:45 -05003290/* private - implementation data created as needed, per-type */
3291struct _poller {
3292 struct k_thread *thread;
3293};
3294
3295/* private - types bit positions */
3296enum _poll_types_bits {
3297 /* can be used to ignore an event */
3298 _POLL_TYPE_IGNORE,
3299
3300 /* to be signaled by k_poll_signal() */
3301 _POLL_TYPE_SIGNAL,
3302
3303 /* semaphore availability */
3304 _POLL_TYPE_SEM_AVAILABLE,
3305
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003306 /* queue/fifo/lifo data availability */
3307 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003308
3309 _POLL_NUM_TYPES
3310};
3311
3312#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3313
3314/* private - states bit positions */
3315enum _poll_states_bits {
3316 /* default state when creating event */
3317 _POLL_STATE_NOT_READY,
3318
3319 /* there was another poller already on the object */
3320 _POLL_STATE_EADDRINUSE,
3321
3322 /* signaled by k_poll_signal() */
3323 _POLL_STATE_SIGNALED,
3324
3325 /* semaphore is available */
3326 _POLL_STATE_SEM_AVAILABLE,
3327
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003328 /* data is available to read on queue/fifo/lifo */
3329 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003330
3331 _POLL_NUM_STATES
3332};
3333
3334#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3335
3336#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003337 (32 - (0 \
3338 + 8 /* tag */ \
3339 + _POLL_NUM_TYPES \
3340 + _POLL_NUM_STATES \
3341 + 1 /* modes */ \
3342 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003343
3344#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3345#error overflow of 32-bit word in struct k_poll_event
3346#endif
3347
3348/* end of polling API - PRIVATE */
3349
3350
3351/**
3352 * @defgroup poll_apis Async polling APIs
3353 * @ingroup kernel_apis
3354 * @{
3355 */
3356
3357/* Public polling API */
3358
3359/* public - values for k_poll_event.type bitfield */
3360#define K_POLL_TYPE_IGNORE 0
3361#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3362#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003363#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3364#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003365
3366/* public - polling modes */
3367enum k_poll_modes {
3368 /* polling thread does not take ownership of objects when available */
3369 K_POLL_MODE_NOTIFY_ONLY = 0,
3370
3371 K_POLL_NUM_MODES
3372};
3373
3374/* public - values for k_poll_event.state bitfield */
3375#define K_POLL_STATE_NOT_READY 0
3376#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3377#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3378#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003379#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3380#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003381
3382/* public - poll signal object */
3383struct k_poll_signal {
3384 /* PRIVATE - DO NOT TOUCH */
3385 struct k_poll_event *poll_event;
3386
3387 /*
3388 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3389 * user resets it to 0.
3390 */
3391 unsigned int signaled;
3392
3393 /* custom result value passed to k_poll_signal() if needed */
3394 int result;
3395};
3396
3397#define K_POLL_SIGNAL_INITIALIZER() \
3398 { \
3399 .poll_event = NULL, \
3400 .signaled = 0, \
3401 .result = 0, \
3402 }
3403
3404struct k_poll_event {
3405 /* PRIVATE - DO NOT TOUCH */
3406 struct _poller *poller;
3407
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003408 /* optional user-specified tag, opaque, untouched by the API */
3409 uint32_t tag:8;
3410
Benjamin Walshacc68c12017-01-29 18:57:45 -05003411 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
3412 uint32_t type:_POLL_NUM_TYPES;
3413
3414 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
3415 uint32_t state:_POLL_NUM_STATES;
3416
3417 /* mode of operation, from enum k_poll_modes */
3418 uint32_t mode:1;
3419
3420 /* unused bits in 32-bit word */
3421 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
3422
3423 /* per-type data */
3424 union {
3425 void *obj;
3426 struct k_poll_signal *signal;
3427 struct k_sem *sem;
3428 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003429 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003430 };
3431};
3432
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003433#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003434 { \
3435 .poller = NULL, \
3436 .type = event_type, \
3437 .state = K_POLL_STATE_NOT_READY, \
3438 .mode = event_mode, \
3439 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003440 { .obj = event_obj }, \
3441 }
3442
3443#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3444 event_tag) \
3445 { \
3446 .type = event_type, \
3447 .tag = event_tag, \
3448 .state = K_POLL_STATE_NOT_READY, \
3449 .mode = event_mode, \
3450 .unused = 0, \
3451 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003452 }
3453
3454/**
3455 * @brief Initialize one struct k_poll_event instance
3456 *
3457 * After this routine is called on a poll event, the event it ready to be
3458 * placed in an event array to be passed to k_poll().
3459 *
3460 * @param event The event to initialize.
3461 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3462 * values. Only values that apply to the same object being polled
3463 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3464 * event.
3465 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3466 * @param obj Kernel object or poll signal.
3467 *
3468 * @return N/A
3469 */
3470
3471extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
3472 int mode, void *obj);
3473
3474/**
3475 * @brief Wait for one or many of multiple poll events to occur
3476 *
3477 * This routine allows a thread to wait concurrently for one or many of
3478 * multiple poll events to have occurred. Such events can be a kernel object
3479 * being available, like a semaphore, or a poll signal event.
3480 *
3481 * When an event notifies that a kernel object is available, the kernel object
3482 * is not "given" to the thread calling k_poll(): it merely signals the fact
3483 * that the object was available when the k_poll() call was in effect. Also,
3484 * all threads trying to acquire an object the regular way, i.e. by pending on
3485 * the object, have precedence over the thread polling on the object. This
3486 * means that the polling thread will never get the poll event on an object
3487 * until the object becomes available and its pend queue is empty. For this
3488 * reason, the k_poll() call is more effective when the objects being polled
3489 * only have one thread, the polling thread, trying to acquire them.
3490 *
3491 * Only one thread can be polling for a particular object at a given time. If
3492 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3493 * and returns as soon as it has finished handling the other events. This means
3494 * that k_poll() can return -EADDRINUSE and have the state value of some events
3495 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3496 * parameter is ignored.
3497 *
3498 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3499 * events that were passed to k_poll() and check the state field for the values
3500 * that were expected and take the associated actions.
3501 *
3502 * Before being reused for another call to k_poll(), the user has to reset the
3503 * state field to K_POLL_STATE_NOT_READY.
3504 *
3505 * @param events An array of pointers to events to be polled for.
3506 * @param num_events The number of events in the array.
3507 * @param timeout Waiting period for an event to be ready (in milliseconds),
3508 * or one of the special values K_NO_WAIT and K_FOREVER.
3509 *
3510 * @retval 0 One or more events are ready.
3511 * @retval -EADDRINUSE One or more objects already had a poller.
3512 * @retval -EAGAIN Waiting period timed out.
3513 */
3514
3515extern int k_poll(struct k_poll_event *events, int num_events,
3516 int32_t timeout);
3517
3518/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003519 * @brief Initialize a poll signal object.
3520 *
3521 * Ready a poll signal object to be signaled via k_poll_signal().
3522 *
3523 * @param signal A poll signal.
3524 *
3525 * @return N/A
3526 */
3527
3528extern void k_poll_signal_init(struct k_poll_signal *signal);
3529
3530/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003531 * @brief Signal a poll signal object.
3532 *
3533 * This routine makes ready a poll signal, which is basically a poll event of
3534 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3535 * made ready to run. A @a result value can be specified.
3536 *
3537 * The poll signal contains a 'signaled' field that, when set by
3538 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3539 * be reset by the user before being passed again to k_poll() or k_poll() will
3540 * consider it being signaled, and will return immediately.
3541 *
3542 * @param signal A poll signal.
3543 * @param result The value to store in the result field of the signal.
3544 *
3545 * @retval 0 The signal was delivered successfully.
3546 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3547 */
3548
3549extern int k_poll_signal(struct k_poll_signal *signal, int result);
3550
3551/* private internal function */
3552extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
3553 uint32_t state);
3554
3555/**
3556 * @} end defgroup poll_apis
3557 */
3558
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003559/**
3560 * @brief Make the CPU idle.
3561 *
3562 * This function makes the CPU idle until an event wakes it up.
3563 *
3564 * In a regular system, the idle thread should be the only thread responsible
3565 * for making the CPU idle and triggering any type of power management.
3566 * However, in some more constrained systems, such as a single-threaded system,
3567 * the only thread would be responsible for this if needed.
3568 *
3569 * @return N/A
3570 */
3571extern void k_cpu_idle(void);
3572
3573/**
3574 * @brief Make the CPU idle in an atomic fashion.
3575 *
3576 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3577 * must be done atomically before making the CPU idle.
3578 *
3579 * @param key Interrupt locking key obtained from irq_lock().
3580 *
3581 * @return N/A
3582 */
3583extern void k_cpu_atomic_idle(unsigned int key);
3584
Andrew Boie350f88d2017-01-18 13:13:45 -08003585extern void _sys_power_save_idle_exit(int32_t ticks);
3586
Anas Nashifa6149502017-01-17 07:47:31 -05003587/* Include legacy APIs */
3588#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003589#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003590#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591#include <arch/cpu.h>
3592
3593/*
3594 * private APIs that are utilized by one or more public APIs
3595 */
3596
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003597#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003598extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003599#else
3600#define _init_static_threads() do { } while ((0))
3601#endif
3602
3603extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003604extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003605
3606#ifdef __cplusplus
3607}
3608#endif
3609
Andrew Boiee004dec2016-11-07 09:01:19 -08003610#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3611/*
3612 * Define new and delete operators.
3613 * At this moment, the operators do nothing since objects are supposed
3614 * to be statically allocated.
3615 */
3616inline void operator delete(void *ptr)
3617{
3618 (void)ptr;
3619}
3620
3621inline void operator delete[](void *ptr)
3622{
3623 (void)ptr;
3624}
3625
3626inline void *operator new(size_t size)
3627{
3628 (void)size;
3629 return NULL;
3630}
3631
3632inline void *operator new[](size_t size)
3633{
3634 (void)size;
3635 return NULL;
3636}
3637
3638/* Placement versions of operator new and delete */
3639inline void operator delete(void *ptr1, void *ptr2)
3640{
3641 (void)ptr1;
3642 (void)ptr2;
3643}
3644
3645inline void operator delete[](void *ptr1, void *ptr2)
3646{
3647 (void)ptr1;
3648 (void)ptr2;
3649}
3650
3651inline void *operator new(size_t size, void *ptr)
3652{
3653 (void)size;
3654 return ptr;
3655}
3656
3657inline void *operator new[](size_t size, void *ptr)
3658{
3659 (void)size;
3660 return ptr;
3661}
3662
3663#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3664
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003665#endif /* !_ASMLANGUAGE */
3666
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003667#endif /* _kernel__h_ */