blob: e945d95c60c738a674520bad32595c473877e3e3 [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 {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001427 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428};
1429
Allan Stephensc98da842016-11-11 15:45:03 -05001430#define K_LIFO_INITIALIZER(obj) \
1431 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001432 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001433 }
1434
1435/**
1436 * INTERNAL_HIDDEN @endcond
1437 */
1438
1439/**
1440 * @defgroup lifo_apis Lifo APIs
1441 * @ingroup kernel_apis
1442 * @{
1443 */
1444
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
1452 * @return N/A
1453 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001454#define k_lifo_init(lifo) \
1455 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456
1457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * This routine adds a data item to @a lifo. A lifo data item must be
1461 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1462 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @note Can be called by ISRs.
1465 *
1466 * @param lifo Address of the lifo.
1467 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001468 *
1469 * @return N/A
1470 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001471#define k_lifo_put(lifo, data) \
1472 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001473
1474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * This routine removes a data item from @a lifo in a "last in, first out"
1478 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1481 *
1482 * @param lifo Address of the lifo.
1483 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484 * or one of the special values K_NO_WAIT and K_FOREVER.
1485 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001486 * @return Address of the data item if successful; NULL if returned
1487 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001488 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001489#define k_lifo_get(lifo, timeout) \
1490 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001497 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001502 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001503 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001504 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505
Allan Stephensc98da842016-11-11 15:45:03 -05001506/**
1507 * @} end defgroup lifo_apis
1508 */
1509
1510/**
1511 * @cond INTERNAL_HIDDEN
1512 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001513
1514struct k_stack {
1515 _wait_q_t wait_q;
1516 uint32_t *base, *next, *top;
1517
Anas Nashif2f203c22016-12-18 06:57:45 -05001518 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519};
1520
Allan Stephensc98da842016-11-11 15:45:03 -05001521#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1522 { \
1523 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1524 .base = stack_buffer, \
1525 .next = stack_buffer, \
1526 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001527 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001528 }
1529
1530/**
1531 * INTERNAL_HIDDEN @endcond
1532 */
1533
1534/**
1535 * @defgroup stack_apis Stack APIs
1536 * @ingroup kernel_apis
1537 * @{
1538 */
1539
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001540/**
1541 * @brief Initialize a stack.
1542 *
1543 * This routine initializes a stack object, prior to its first use.
1544 *
1545 * @param stack Address of the stack.
1546 * @param buffer Address of array used to hold stacked values.
1547 * @param num_entries Maximum number of values that can be stacked.
1548 *
1549 * @return N/A
1550 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001551extern void k_stack_init(struct k_stack *stack,
1552 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001553
1554/**
1555 * @brief Push an element onto a stack.
1556 *
1557 * This routine adds a 32-bit value @a data to @a stack.
1558 *
1559 * @note Can be called by ISRs.
1560 *
1561 * @param stack Address of the stack.
1562 * @param data Value to push onto the stack.
1563 *
1564 * @return N/A
1565 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001566extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567
1568/**
1569 * @brief Pop an element from a stack.
1570 *
1571 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1572 * manner and stores the value in @a data.
1573 *
1574 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1575 *
1576 * @param stack Address of the stack.
1577 * @param data Address of area to hold the value popped from the stack.
1578 * @param timeout Waiting period to obtain a value (in milliseconds),
1579 * or one of the special values K_NO_WAIT and K_FOREVER.
1580 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001581 * @retval 0 Element popped from stack.
1582 * @retval -EBUSY Returned without waiting.
1583 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1586
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001592 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * @param name Name of the stack.
1595 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001597#define K_STACK_DEFINE(name, stack_num_entries) \
1598 uint32_t __noinit \
1599 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001600 struct k_stack name \
1601 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001602 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1603 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001604
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001605/**
Allan Stephensc98da842016-11-11 15:45:03 -05001606 * @} end defgroup stack_apis
1607 */
1608
Allan Stephens6bba9b02016-11-16 14:56:54 -05001609struct k_work;
1610
Allan Stephensc98da842016-11-11 15:45:03 -05001611/**
1612 * @defgroup workqueue_apis Workqueue Thread APIs
1613 * @ingroup kernel_apis
1614 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615 */
1616
Allan Stephens6bba9b02016-11-16 14:56:54 -05001617/**
1618 * @typedef k_work_handler_t
1619 * @brief Work item handler function type.
1620 *
1621 * A work item's handler function is executed by a workqueue's thread
1622 * when the work item is processed by the workqueue.
1623 *
1624 * @param work Address of the work item.
1625 *
1626 * @return N/A
1627 */
1628typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629
1630/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001631 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001633
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634struct k_work_q {
1635 struct k_fifo fifo;
1636};
1637
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001638enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001639 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001640};
1641
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001642struct k_work {
1643 void *_reserved; /* Used by k_fifo implementation. */
1644 k_work_handler_t handler;
1645 atomic_t flags[1];
1646};
1647
Allan Stephens6bba9b02016-11-16 14:56:54 -05001648struct k_delayed_work {
1649 struct k_work work;
1650 struct _timeout timeout;
1651 struct k_work_q *work_q;
1652};
1653
1654extern struct k_work_q k_sys_work_q;
1655
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001657 * INTERNAL_HIDDEN @endcond
1658 */
1659
1660/**
1661 * @brief Initialize a statically-defined work item.
1662 *
1663 * This macro can be used to initialize a statically-defined workqueue work
1664 * item, prior to its first use. For example,
1665 *
1666 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1667 *
1668 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001669 */
1670#define K_WORK_INITIALIZER(work_handler) \
1671 { \
1672 ._reserved = NULL, \
1673 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001674 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675 }
1676
1677/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001678 * @brief Initialize a work item.
1679 *
1680 * This routine initializes a workqueue work item, prior to its first use.
1681 *
1682 * @param work Address of work item.
1683 * @param handler Function to invoke each time work item is processed.
1684 *
1685 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001686 */
1687static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1688{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001689 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001690 work->handler = handler;
1691}
1692
1693/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001694 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001695 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001696 * This routine submits work item @a work to be processed by workqueue
1697 * @a work_q. If the work item is already pending in the workqueue's queue
1698 * as a result of an earlier submission, this routine has no effect on the
1699 * work item. If the work item has already been processed, or is currently
1700 * being processed, its work is considered complete and the work item can be
1701 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001702 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001703 * @warning
1704 * A submitted work item must not be modified until it has been processed
1705 * by the workqueue.
1706 *
1707 * @note Can be called by ISRs.
1708 *
1709 * @param work_q Address of workqueue.
1710 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001711 *
1712 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001713 */
1714static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1715 struct k_work *work)
1716{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001717 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718 k_fifo_put(&work_q->fifo, work);
1719 }
1720}
1721
1722/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001723 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001725 * This routine indicates if work item @a work is pending in a workqueue's
1726 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001728 * @note Can be called by ISRs.
1729 *
1730 * @param work Address of work item.
1731 *
1732 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001733 */
1734static inline int k_work_pending(struct k_work *work)
1735{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001736 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001737}
1738
1739/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001740 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001742 * This routine starts workqueue @a work_q. The workqueue spawns its work
1743 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001745 * @param work_q Address of workqueue.
1746 * @param stack Pointer to work queue thread's stack space.
1747 * @param stack_size Size of the work queue thread's stack (in bytes).
1748 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749 *
1750 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001751 */
Allan Stephens904cf972016-10-07 13:59:23 -05001752extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001753 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001756 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001758 * This routine initializes a workqueue delayed work item, prior to
1759 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001761 * @param work Address of delayed work item.
1762 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
1764 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001765 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001766extern void k_delayed_work_init(struct k_delayed_work *work,
1767 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768
1769/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001770 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001771 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001772 * This routine schedules work item @a work to be processed by workqueue
1773 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1774 * an asychronous countdown for the work item and then returns to the caller.
1775 * Only when the countdown completes is the work item actually submitted to
1776 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001777 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001778 * Submitting a previously submitted delayed work item that is still
1779 * counting down cancels the existing submission and restarts the countdown
1780 * using the new delay. If the work item is currently pending on the
1781 * workqueue's queue because the countdown has completed it is too late to
1782 * resubmit the item, and resubmission fails without impacting the work item.
1783 * If the work item has already been processed, or is currently being processed,
1784 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001786 * @warning
1787 * A delayed work item must not be modified until it has been processed
1788 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001789 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001790 * @note Can be called by ISRs.
1791 *
1792 * @param work_q Address of workqueue.
1793 * @param work Address of delayed work item.
1794 * @param delay Delay before submitting the work item (in milliseconds).
1795 *
1796 * @retval 0 Work item countdown started.
1797 * @retval -EINPROGRESS Work item is already pending.
1798 * @retval -EINVAL Work item is being processed or has completed its work.
1799 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001800 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001801extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1802 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001803 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804
1805/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001806 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001807 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001808 * This routine cancels the submission of delayed work item @a work.
1809 * A delayed work item can only be cancelled while its countdown is still
1810 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001811 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001812 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001814 * @param work Address of delayed work item.
1815 *
1816 * @retval 0 Work item countdown cancelled.
1817 * @retval -EINPROGRESS Work item is already pending.
1818 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001820extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823 * @brief Submit a work item to the system workqueue.
1824 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001825 * This routine submits work item @a work to be processed by the system
1826 * workqueue. If the work item is already pending in the workqueue's queue
1827 * as a result of an earlier submission, this routine has no effect on the
1828 * work item. If the work item has already been processed, or is currently
1829 * being processed, its work is considered complete and the work item can be
1830 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001832 * @warning
1833 * Work items submitted to the system workqueue should avoid using handlers
1834 * that block or yield since this may prevent the system workqueue from
1835 * processing other work items in a timely manner.
1836 *
1837 * @note Can be called by ISRs.
1838 *
1839 * @param work Address of work item.
1840 *
1841 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001842 */
1843static inline void k_work_submit(struct k_work *work)
1844{
1845 k_work_submit_to_queue(&k_sys_work_q, work);
1846}
1847
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001849 * @brief Submit a delayed work item to the system workqueue.
1850 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001851 * This routine schedules work item @a work to be processed by the system
1852 * workqueue after a delay of @a delay milliseconds. The routine initiates
1853 * an asychronous countdown for the work item and then returns to the caller.
1854 * Only when the countdown completes is the work item actually submitted to
1855 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001856 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001857 * Submitting a previously submitted delayed work item that is still
1858 * counting down cancels the existing submission and restarts the countdown
1859 * using the new delay. If the work item is currently pending on the
1860 * workqueue's queue because the countdown has completed it is too late to
1861 * resubmit the item, and resubmission fails without impacting the work item.
1862 * If the work item has already been processed, or is currently being processed,
1863 * its work is considered complete and the work item can be resubmitted.
1864 *
1865 * @warning
1866 * Work items submitted to the system workqueue should avoid using handlers
1867 * that block or yield since this may prevent the system workqueue from
1868 * processing other work items in a timely manner.
1869 *
1870 * @note Can be called by ISRs.
1871 *
1872 * @param work Address of delayed work item.
1873 * @param delay Delay before submitting the work item (in milliseconds).
1874 *
1875 * @retval 0 Work item countdown started.
1876 * @retval -EINPROGRESS Work item is already pending.
1877 * @retval -EINVAL Work item is being processed or has completed its work.
1878 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879 */
1880static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001881 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001882{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001883 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884}
1885
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001886/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001887 * @brief Get time remaining before a delayed work gets scheduled.
1888 *
1889 * This routine computes the (approximate) time remaining before a
1890 * delayed work gets executed. If the delayed work is not waiting to be
1891 * schedules, it returns zero.
1892 *
1893 * @param work Delayed work item.
1894 *
1895 * @return Remaining time (in milliseconds).
1896 */
1897static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1898{
1899 return _timeout_remaining_get(&work->timeout);
1900}
1901
1902/**
Allan Stephensc98da842016-11-11 15:45:03 -05001903 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001904 */
1905
Allan Stephensc98da842016-11-11 15:45:03 -05001906/**
1907 * @cond INTERNAL_HIDDEN
1908 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909
1910struct k_mutex {
1911 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001912 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913 uint32_t lock_count;
1914 int owner_orig_prio;
1915#ifdef CONFIG_OBJECT_MONITOR
1916 int num_lock_state_changes;
1917 int num_conflicts;
1918#endif
1919
Anas Nashif2f203c22016-12-18 06:57:45 -05001920 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921};
1922
1923#ifdef CONFIG_OBJECT_MONITOR
1924#define _MUTEX_INIT_OBJECT_MONITOR \
1925 .num_lock_state_changes = 0, .num_conflicts = 0,
1926#else
1927#define _MUTEX_INIT_OBJECT_MONITOR
1928#endif
1929
1930#define K_MUTEX_INITIALIZER(obj) \
1931 { \
1932 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1933 .owner = NULL, \
1934 .lock_count = 0, \
1935 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1936 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001937 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938 }
1939
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001940/**
Allan Stephensc98da842016-11-11 15:45:03 -05001941 * INTERNAL_HIDDEN @endcond
1942 */
1943
1944/**
1945 * @defgroup mutex_apis Mutex APIs
1946 * @ingroup kernel_apis
1947 * @{
1948 */
1949
1950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001951 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001953 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001955 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001960 struct k_mutex name \
1961 __in_section(_k_mutex, static, name) = \
1962 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001965 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * Upon completion, the mutex is available and does not have an owner.
1970 *
1971 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
1973 * @return N/A
1974 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976
1977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * This routine locks @a mutex. If the mutex is locked by another thread,
1981 * the calling thread waits until the mutex becomes available or until
1982 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * A thread is permitted to lock a mutex it has already locked. The operation
1985 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * @param mutex Address of the mutex.
1988 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989 * or one of the special values K_NO_WAIT and K_FOREVER.
1990 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001991 * @retval 0 Mutex locked.
1992 * @retval -EBUSY Returned without waiting.
1993 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001994 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996
1997/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001998 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002000 * This routine unlocks @a mutex. The mutex must already be locked by the
2001 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
2003 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 * thread.
2006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 *
2009 * @return N/A
2010 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002011extern void k_mutex_unlock(struct k_mutex *mutex);
2012
Allan Stephensc98da842016-11-11 15:45:03 -05002013/**
2014 * @} end defgroup mutex_apis
2015 */
2016
2017/**
2018 * @cond INTERNAL_HIDDEN
2019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002020
2021struct k_sem {
2022 _wait_q_t wait_q;
2023 unsigned int count;
2024 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002025 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026
Anas Nashif2f203c22016-12-18 06:57:45 -05002027 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028};
2029
Allan Stephensc98da842016-11-11 15:45:03 -05002030#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2031 { \
2032 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2033 .count = initial_count, \
2034 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002035 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002036 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002037 }
2038
2039/**
2040 * INTERNAL_HIDDEN @endcond
2041 */
2042
2043/**
2044 * @defgroup semaphore_apis Semaphore APIs
2045 * @ingroup kernel_apis
2046 * @{
2047 */
2048
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002050 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002052 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * @param sem Address of the semaphore.
2055 * @param initial_count Initial semaphore count.
2056 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002057 *
2058 * @return N/A
2059 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2061 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002062
2063/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002064 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2069 *
2070 * @param sem Address of the semaphore.
2071 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002072 * or one of the special values K_NO_WAIT and K_FOREVER.
2073 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002074 * @note When porting code from the nanokernel legacy API to the new API, be
2075 * careful with the return value of this function. The return value is the
2076 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2077 * non-zero means failure, while the nano_sem_take family returns 1 for success
2078 * and 0 for failure.
2079 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002080 * @retval 0 Semaphore taken.
2081 * @retval -EBUSY Returned without waiting.
2082 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002083 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002084extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002085
2086/**
2087 * @brief Give a semaphore.
2088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * This routine gives @a sem, unless the semaphore is already at its maximum
2090 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002095 *
2096 * @return N/A
2097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098extern void k_sem_give(struct k_sem *sem);
2099
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002100/**
2101 * @brief Reset a semaphore's count to zero.
2102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002106 *
2107 * @return N/A
2108 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002109static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110{
2111 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112}
2113
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002114/**
2115 * @brief Get a semaphore's count.
2116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002122 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002123static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124{
2125 return sem->count;
2126}
2127
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002132 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002133 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * @param name Name of the semaphore.
2136 * @param initial_count Initial semaphore count.
2137 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002140 struct k_sem name \
2141 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142 K_SEM_INITIALIZER(name, initial_count, count_limit)
2143
Allan Stephensc98da842016-11-11 15:45:03 -05002144/**
2145 * @} end defgroup semaphore_apis
2146 */
2147
2148/**
2149 * @defgroup alert_apis Alert APIs
2150 * @ingroup kernel_apis
2151 * @{
2152 */
2153
Allan Stephens5eceb852016-11-16 10:16:30 -05002154/**
2155 * @typedef k_alert_handler_t
2156 * @brief Alert handler function type.
2157 *
2158 * An alert's alert handler function is invoked by the system workqueue
2159 * when the alert is signalled. The alert handler function is optional,
2160 * and is only invoked if the alert has been initialized with one.
2161 *
2162 * @param alert Address of the alert.
2163 *
2164 * @return 0 if alert has been consumed; non-zero if alert should pend.
2165 */
2166typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002167
2168/**
2169 * @} end defgroup alert_apis
2170 */
2171
2172/**
2173 * @cond INTERNAL_HIDDEN
2174 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002176#define K_ALERT_DEFAULT NULL
2177#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002179struct k_alert {
2180 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181 atomic_t send_count;
2182 struct k_work work_item;
2183 struct k_sem sem;
2184
Anas Nashif2f203c22016-12-18 06:57:45 -05002185 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186};
2187
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002188extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002190#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002192 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002194 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002195 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002196 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197 }
2198
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002199/**
Allan Stephensc98da842016-11-11 15:45:03 -05002200 * INTERNAL_HIDDEN @endcond
2201 */
2202
2203/**
2204 * @addtogroup alert_apis
2205 * @{
2206 */
2207
2208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002209 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002210 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002211 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002212 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002213 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002215 * @param name Name of the alert.
2216 * @param alert_handler Action to take when alert is sent. Specify either
2217 * the address of a function to be invoked by the system workqueue
2218 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2219 * K_ALERT_DEFAULT (which causes the alert to pend).
2220 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002222#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002223 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002224 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002225 K_ALERT_INITIALIZER(name, alert_handler, \
2226 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002229 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * @param alert Address of the alert.
2234 * @param handler Action to take when alert is sent. Specify either the address
2235 * of a function to be invoked by the system workqueue thread,
2236 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2237 * K_ALERT_DEFAULT (which causes the alert to pend).
2238 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002239 *
2240 * @return N/A
2241 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002242extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2243 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244
2245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002246 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002250 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2251 *
2252 * @param alert Address of the alert.
2253 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254 * or one of the special values K_NO_WAIT and K_FOREVER.
2255 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002256 * @retval 0 Alert received.
2257 * @retval -EBUSY Returned without waiting.
2258 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002259 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002260extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261
2262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002263 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * This routine signals @a alert. The action specified for @a alert will
2266 * be taken, which may trigger the execution of an alert handler function
2267 * and/or cause the alert to pend (assuming the alert has not reached its
2268 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * @note Can be called by ISRs.
2271 *
2272 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
2274 * @return N/A
2275 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002276extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277
2278/**
Allan Stephensc98da842016-11-11 15:45:03 -05002279 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280 */
2281
Allan Stephensc98da842016-11-11 15:45:03 -05002282/**
2283 * @cond INTERNAL_HIDDEN
2284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285
2286struct k_msgq {
2287 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002288 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289 uint32_t max_msgs;
2290 char *buffer_start;
2291 char *buffer_end;
2292 char *read_ptr;
2293 char *write_ptr;
2294 uint32_t used_msgs;
2295
Anas Nashif2f203c22016-12-18 06:57:45 -05002296 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297};
2298
Peter Mitsis1da807e2016-10-06 11:36:59 -04002299#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 { \
2301 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002302 .max_msgs = q_max_msgs, \
2303 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002305 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 .read_ptr = q_buffer, \
2307 .write_ptr = q_buffer, \
2308 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002309 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310 }
2311
Peter Mitsis1da807e2016-10-06 11:36:59 -04002312/**
Allan Stephensc98da842016-11-11 15:45:03 -05002313 * INTERNAL_HIDDEN @endcond
2314 */
2315
2316/**
2317 * @defgroup msgq_apis Message Queue APIs
2318 * @ingroup kernel_apis
2319 * @{
2320 */
2321
2322/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2326 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002327 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2328 * message is similarly aligned to this boundary, @a q_msg_size must also be
2329 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * The message queue can be accessed outside the module where it is defined
2332 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002334 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * @param q_name Name of the message queue.
2337 * @param q_msg_size Message size (in bytes).
2338 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002339 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002340 */
2341#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2342 static char __noinit __aligned(q_align) \
2343 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002344 struct k_msgq q_name \
2345 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002346 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2347 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348
Peter Mitsisd7a37502016-10-13 11:37:40 -04002349/**
2350 * @brief Initialize a message queue.
2351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * This routine initializes a message queue object, prior to its first use.
2353 *
Allan Stephensda827222016-11-09 14:23:58 -06002354 * The message queue's ring buffer must contain space for @a max_msgs messages,
2355 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2356 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2357 * that each message is similarly aligned to this boundary, @a q_msg_size
2358 * must also be a multiple of N.
2359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @param q Address of the message queue.
2361 * @param buffer Pointer to ring buffer that holds queued messages.
2362 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002363 * @param max_msgs Maximum number of messages that can be queued.
2364 *
2365 * @return N/A
2366 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002367extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002368 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002369
2370/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002373 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002374 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002375 * @note Can be called by ISRs.
2376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @param q Address of the message queue.
2378 * @param data Pointer to the message.
2379 * @param timeout Waiting period to add the message (in milliseconds),
2380 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002381 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002382 * @retval 0 Message sent.
2383 * @retval -ENOMSG Returned without waiting or queue purged.
2384 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002385 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002387
2388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * This routine receives a message from message queue @a q in a "first in,
2392 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002393 *
Allan Stephensc98da842016-11-11 15:45:03 -05002394 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @param q Address of the message queue.
2397 * @param data Address of area to hold the received message.
2398 * @param timeout Waiting period to receive the message (in milliseconds),
2399 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002400 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002401 * @retval 0 Message received.
2402 * @retval -ENOMSG Returned without waiting.
2403 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002404 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002406
2407/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * This routine discards all unreceived messages in a message queue's ring
2411 * buffer. Any threads that are blocked waiting to send a message to the
2412 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002415 *
2416 * @return N/A
2417 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418extern void k_msgq_purge(struct k_msgq *q);
2419
Peter Mitsis67be2492016-10-07 11:44:34 -04002420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * This routine returns the number of unused entries in a message queue's
2424 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002426 * @param q Address of the message queue.
2427 *
2428 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002429 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002430static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002431{
2432 return q->max_msgs - q->used_msgs;
2433}
2434
Peter Mitsisd7a37502016-10-13 11:37:40 -04002435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002440 * @param q Address of the message queue.
2441 *
2442 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002443 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002444static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445{
2446 return q->used_msgs;
2447}
2448
Allan Stephensc98da842016-11-11 15:45:03 -05002449/**
2450 * @} end defgroup msgq_apis
2451 */
2452
2453/**
2454 * @defgroup mem_pool_apis Memory Pool APIs
2455 * @ingroup kernel_apis
2456 * @{
2457 */
2458
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002460 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461 void *addr_in_pool;
2462 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002463 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464};
2465
Allan Stephensc98da842016-11-11 15:45:03 -05002466/**
2467 * @} end defgroup mem_pool_apis
2468 */
2469
2470/**
2471 * @defgroup mailbox_apis Mailbox APIs
2472 * @ingroup kernel_apis
2473 * @{
2474 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475
2476struct k_mbox_msg {
2477 /** internal use only - needed for legacy API support */
2478 uint32_t _mailbox;
2479 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002480 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481 /** application-defined information value */
2482 uint32_t info;
2483 /** sender's message data buffer */
2484 void *tx_data;
2485 /** internal use only - needed for legacy API support */
2486 void *_rx_data;
2487 /** message data block descriptor */
2488 struct k_mem_block tx_block;
2489 /** source thread id */
2490 k_tid_t rx_source_thread;
2491 /** target thread id */
2492 k_tid_t tx_target_thread;
2493 /** internal use only - thread waiting on send (may be a dummy) */
2494 k_tid_t _syncing_thread;
2495#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2496 /** internal use only - semaphore used during asynchronous send */
2497 struct k_sem *_async_sem;
2498#endif
2499};
2500
Allan Stephensc98da842016-11-11 15:45:03 -05002501/**
2502 * @cond INTERNAL_HIDDEN
2503 */
2504
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505struct k_mbox {
2506 _wait_q_t tx_msg_queue;
2507 _wait_q_t rx_msg_queue;
2508
Anas Nashif2f203c22016-12-18 06:57:45 -05002509 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510};
2511
2512#define K_MBOX_INITIALIZER(obj) \
2513 { \
2514 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2515 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002516 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517 }
2518
Peter Mitsis12092702016-10-14 12:57:23 -04002519/**
Allan Stephensc98da842016-11-11 15:45:03 -05002520 * INTERNAL_HIDDEN @endcond
2521 */
2522
2523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002528 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002531 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002533 struct k_mbox name \
2534 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002535 K_MBOX_INITIALIZER(name) \
2536
Peter Mitsis12092702016-10-14 12:57:23 -04002537/**
2538 * @brief Initialize a mailbox.
2539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * This routine initializes a mailbox object, prior to its first use.
2541 *
2542 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002543 *
2544 * @return N/A
2545 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546extern void k_mbox_init(struct k_mbox *mbox);
2547
Peter Mitsis12092702016-10-14 12:57:23 -04002548/**
2549 * @brief Send a mailbox message in a synchronous manner.
2550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * This routine sends a message to @a mbox and waits for a receiver to both
2552 * receive and process it. The message data may be in a buffer, in a memory
2553 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @param mbox Address of the mailbox.
2556 * @param tx_msg Address of the transmit message descriptor.
2557 * @param timeout Waiting period for the message to be received (in
2558 * milliseconds), or one of the special values K_NO_WAIT
2559 * and K_FOREVER. Once the message has been received,
2560 * this routine waits as long as necessary for the message
2561 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002562 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002563 * @retval 0 Message sent.
2564 * @retval -ENOMSG Returned without waiting.
2565 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002566 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002567extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002569
Peter Mitsis12092702016-10-14 12:57:23 -04002570/**
2571 * @brief Send a mailbox message in an asynchronous manner.
2572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * This routine sends a message to @a mbox without waiting for a receiver
2574 * to process it. The message data may be in a buffer, in a memory pool block,
2575 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2576 * will be given when the message has been both received and completely
2577 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * @param mbox Address of the mailbox.
2580 * @param tx_msg Address of the transmit message descriptor.
2581 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002582 *
2583 * @return N/A
2584 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002585extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 struct k_sem *sem);
2587
Peter Mitsis12092702016-10-14 12:57:23 -04002588/**
2589 * @brief Receive a mailbox message.
2590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * This routine receives a message from @a mbox, then optionally retrieves
2592 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * @param mbox Address of the mailbox.
2595 * @param rx_msg Address of the receive message descriptor.
2596 * @param buffer Address of the buffer to receive data, or NULL to defer data
2597 * retrieval and message disposal until later.
2598 * @param timeout Waiting period for a message to be received (in
2599 * milliseconds), or one of the special values K_NO_WAIT
2600 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002601 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002602 * @retval 0 Message received.
2603 * @retval -ENOMSG Returned without waiting.
2604 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002605 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002606extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002608
2609/**
2610 * @brief Retrieve mailbox message data into a buffer.
2611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * This routine completes the processing of a received message by retrieving
2613 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002614 *
2615 * Alternatively, this routine can be used to dispose of a received message
2616 * without retrieving its data.
2617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * @param rx_msg Address of the receive message descriptor.
2619 * @param buffer Address of the buffer to receive data, or NULL to discard
2620 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002621 *
2622 * @return N/A
2623 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002624extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002625
2626/**
2627 * @brief Retrieve mailbox message data into a memory pool block.
2628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * This routine completes the processing of a received message by retrieving
2630 * its data into a memory pool block, then disposing of the message.
2631 * The memory pool block that results from successful retrieval must be
2632 * returned to the pool once the data has been processed, even in cases
2633 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002634 *
2635 * Alternatively, this routine can be used to dispose of a received message
2636 * without retrieving its data. In this case there is no need to return a
2637 * memory pool block to the pool.
2638 *
2639 * This routine allocates a new memory pool block for the data only if the
2640 * data is not already in one. If a new block cannot be allocated, the routine
2641 * returns a failure code and the received message is left unchanged. This
2642 * permits the caller to reattempt data retrieval at a later time or to dispose
2643 * of the received message without retrieving its data.
2644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * @param rx_msg Address of a receive message descriptor.
2646 * @param pool Address of memory pool, or NULL to discard data.
2647 * @param block Address of the area to hold memory pool block info.
2648 * @param timeout Waiting period to wait for a memory pool block (in
2649 * milliseconds), or one of the special values K_NO_WAIT
2650 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002651 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002652 * @retval 0 Data retrieved.
2653 * @retval -ENOMEM Returned without waiting.
2654 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002655 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002656extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002657 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658 struct k_mem_block *block, int32_t timeout);
2659
Allan Stephensc98da842016-11-11 15:45:03 -05002660/**
2661 * @} end defgroup mailbox_apis
2662 */
2663
2664/**
2665 * @cond INTERNAL_HIDDEN
2666 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667
2668struct k_pipe {
2669 unsigned char *buffer; /* Pipe buffer: may be NULL */
2670 size_t size; /* Buffer size */
2671 size_t bytes_used; /* # bytes used in buffer */
2672 size_t read_index; /* Where in buffer to read from */
2673 size_t write_index; /* Where in buffer to write */
2674
2675 struct {
2676 _wait_q_t readers; /* Reader wait queue */
2677 _wait_q_t writers; /* Writer wait queue */
2678 } wait_q;
2679
Anas Nashif2f203c22016-12-18 06:57:45 -05002680 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681};
2682
Peter Mitsise5d9c582016-10-14 14:44:57 -04002683#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 { \
2685 .buffer = pipe_buffer, \
2686 .size = pipe_buffer_size, \
2687 .bytes_used = 0, \
2688 .read_index = 0, \
2689 .write_index = 0, \
2690 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2691 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002692 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693 }
2694
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002695/**
Allan Stephensc98da842016-11-11 15:45:03 -05002696 * INTERNAL_HIDDEN @endcond
2697 */
2698
2699/**
2700 * @defgroup pipe_apis Pipe APIs
2701 * @ingroup kernel_apis
2702 * @{
2703 */
2704
2705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002706 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002710 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002712 * @param name Name of the pipe.
2713 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2714 * or zero if no ring buffer is used.
2715 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002716 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002717#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2718 static unsigned char __noinit __aligned(pipe_align) \
2719 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002720 struct k_pipe name \
2721 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002722 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002725 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * This routine initializes a pipe object, prior to its first use.
2728 *
2729 * @param pipe Address of the pipe.
2730 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2731 * is used.
2732 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2733 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734 *
2735 * @return N/A
2736 */
2737extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2738 size_t size);
2739
2740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002743 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * @param pipe Address of the pipe.
2746 * @param data Address of data to write.
2747 * @param bytes_to_write Size of data (in bytes).
2748 * @param bytes_written Address of area to hold the number of bytes written.
2749 * @param min_xfer Minimum number of bytes to write.
2750 * @param timeout Waiting period to wait for the data to be written (in
2751 * milliseconds), or one of the special values K_NO_WAIT
2752 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002754 * @retval 0 At least @a min_xfer bytes of data were written.
2755 * @retval -EIO Returned without waiting; zero data bytes were written.
2756 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002759extern int k_pipe_put(struct k_pipe *pipe, void *data,
2760 size_t bytes_to_write, size_t *bytes_written,
2761 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762
2763/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002766 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @param pipe Address of the pipe.
2769 * @param data Address to place the data read from pipe.
2770 * @param bytes_to_read Maximum number of data bytes to read.
2771 * @param bytes_read Address of area to hold the number of bytes read.
2772 * @param min_xfer Minimum number of data bytes to read.
2773 * @param timeout Waiting period to wait for the data to be read (in
2774 * milliseconds), or one of the special values K_NO_WAIT
2775 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002777 * @retval 0 At least @a min_xfer bytes of data were read.
2778 * @retval -EIO Returned without waiting; zero data bytes were read.
2779 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002782extern int k_pipe_get(struct k_pipe *pipe, void *data,
2783 size_t bytes_to_read, size_t *bytes_read,
2784 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785
2786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002787 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002789 * This routine writes the data contained in a memory block to @a pipe.
2790 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 * @param block Memory block containing data to send
2795 * @param size Number of data bytes in memory block to send
2796 * @param sem Semaphore to signal upon completion (else NULL)
2797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799 */
2800extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2801 size_t size, struct k_sem *sem);
2802
2803/**
Allan Stephensc98da842016-11-11 15:45:03 -05002804 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805 */
2806
Allan Stephensc98da842016-11-11 15:45:03 -05002807/**
2808 * @cond INTERNAL_HIDDEN
2809 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002811struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002813 uint32_t num_blocks;
2814 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815 char *buffer;
2816 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002817 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818
Anas Nashif2f203c22016-12-18 06:57:45 -05002819 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820};
2821
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002822#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2823 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824 { \
2825 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002826 .num_blocks = slab_num_blocks, \
2827 .block_size = slab_block_size, \
2828 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829 .free_list = NULL, \
2830 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002831 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 }
2833
Peter Mitsis578f9112016-10-07 13:50:31 -04002834/**
Allan Stephensc98da842016-11-11 15:45:03 -05002835 * INTERNAL_HIDDEN @endcond
2836 */
2837
2838/**
2839 * @defgroup mem_slab_apis Memory Slab APIs
2840 * @ingroup kernel_apis
2841 * @{
2842 */
2843
2844/**
Allan Stephensda827222016-11-09 14:23:58 -06002845 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002846 *
Allan Stephensda827222016-11-09 14:23:58 -06002847 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002849 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2850 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002851 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002852 *
Allan Stephensda827222016-11-09 14:23:58 -06002853 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002856 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * @param name Name of the memory slab.
2859 * @param slab_block_size Size of each memory block (in bytes).
2860 * @param slab_num_blocks Number memory blocks.
2861 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002862 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002863#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2864 char __noinit __aligned(slab_align) \
2865 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2866 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002867 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002868 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2869 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002871/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002872 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002875 *
Allan Stephensda827222016-11-09 14:23:58 -06002876 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2877 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2878 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2879 * To ensure that each memory block is similarly aligned to this boundary,
2880 * @a slab_block_size must also be a multiple of N.
2881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @param slab Address of the memory slab.
2883 * @param buffer Pointer to buffer used for the memory blocks.
2884 * @param block_size Size of each memory block (in bytes).
2885 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002886 *
2887 * @return N/A
2888 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002889extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002890 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002891
2892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * @param slab Address of the memory slab.
2898 * @param mem Pointer to block address area.
2899 * @param timeout Maximum time to wait for operation to complete
2900 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2901 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002902 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002903 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002905 * @retval -ENOMEM Returned without waiting.
2906 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002907 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002908extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2909 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002910
2911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * This routine releases a previously allocated memory block back to its
2915 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * @param slab Address of the memory slab.
2918 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002919 *
2920 * @return N/A
2921 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002922extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * This routine gets the number of memory blocks that are currently
2928 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002933 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002934static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002936 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937}
2938
Peter Mitsisc001aa82016-10-13 13:53:37 -04002939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * This routine gets the number of memory blocks that are currently
2943 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002947 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002948 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002949static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002950{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002951 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002952}
2953
Allan Stephensc98da842016-11-11 15:45:03 -05002954/**
2955 * @} end defgroup mem_slab_apis
2956 */
2957
2958/**
2959 * @cond INTERNAL_HIDDEN
2960 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002962/*
2963 * Memory pool requires a buffer and two arrays of structures for the
2964 * memory block accounting:
2965 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2966 * status of four blocks of memory.
2967 */
2968struct k_mem_pool_quad_block {
2969 char *mem_blocks; /* pointer to the first of four memory blocks */
2970 uint32_t mem_status; /* four bits. If bit is set, memory block is
2971 allocated */
2972};
2973/*
2974 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2975 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2976 * block size is 4 times less than the previous one and thus requires 4 times
2977 * bigger array of k_mem_pool_quad_block structures to keep track of the
2978 * memory blocks.
2979 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002981/*
2982 * The array of k_mem_pool_block_set keeps the information of each array of
2983 * k_mem_pool_quad_block structures
2984 */
2985struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002986 size_t block_size; /* memory block size */
2987 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002988 struct k_mem_pool_quad_block *quad_block;
2989 int count;
2990};
2991
2992/* Memory pool descriptor */
2993struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002994 size_t max_block_size;
2995 size_t min_block_size;
2996 uint32_t nr_of_maxblocks;
2997 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002998 struct k_mem_pool_block_set *block_set;
2999 char *bufblock;
3000 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05003001 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002};
3003
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003004#ifdef CONFIG_ARM
3005#define _SECTION_TYPE_SIGN "%"
3006#else
3007#define _SECTION_TYPE_SIGN "@"
3008#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003010/*
3011 * Static memory pool initialization
3012 */
Allan Stephensc98da842016-11-11 15:45:03 -05003013
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003014/*
3015 * Use .altmacro to be able to recalculate values and pass them as string
3016 * arguments when calling assembler macros resursively
3017 */
3018__asm__(".altmacro\n\t");
3019
3020/*
3021 * Recursively calls a macro
3022 * The followig global symbols need to be initialized:
3023 * __memory_pool_max_block_size - maximal size of the memory block
3024 * __memory_pool_min_block_size - minimal size of the memory block
3025 * Notes:
3026 * Global symbols are used due the fact that assembler macro allows only
3027 * one argument be passed with the % conversion
3028 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3029 * is used instead of / 4.
3030 * n_max argument needs to go first in the invoked macro, as some
3031 * assemblers concatenate \name and %(\n_max * 4) arguments
3032 * if \name goes first
3033 */
3034__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3035 ".ifge __memory_pool_max_block_size >> 2 -"
3036 " __memory_pool_min_block_size\n\t\t"
3037 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3038 "\\macro_name %(\\n_max * 4) \\name\n\t"
3039 ".endif\n\t"
3040 ".endm\n");
3041
3042/*
3043 * Build quad blocks
3044 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3045 * structures and recursively calls itself for the next array, 4 times
3046 * larger.
3047 * The followig global symbols need to be initialized:
3048 * __memory_pool_max_block_size - maximal size of the memory block
3049 * __memory_pool_min_block_size - minimal size of the memory block
3050 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3051 */
3052__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003053 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003054 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3055 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3056 ".if \\n_max % 4\n\t\t"
3057 ".skip __memory_pool_quad_block_size\n\t"
3058 ".endif\n\t"
3059 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3060 ".endm\n");
3061
3062/*
3063 * Build block sets and initialize them
3064 * Macro initializes the k_mem_pool_block_set structure and
3065 * recursively calls itself for the next one.
3066 * The followig global symbols need to be initialized:
3067 * __memory_pool_max_block_size - maximal size of the memory block
3068 * __memory_pool_min_block_size - minimal size of the memory block
3069 * __memory_pool_block_set_count, the number of the elements in the
3070 * block set array must be set to 0. Macro calculates it's real
3071 * value.
3072 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3073 * structures, _build_quad_blocks must be called prior it.
3074 */
3075__asm__(".macro _build_block_set n_max, name\n\t"
3076 ".int __memory_pool_max_block_size\n\t" /* block_size */
3077 ".if \\n_max % 4\n\t\t"
3078 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3079 ".else\n\t\t"
3080 ".int \\n_max >> 2\n\t"
3081 ".endif\n\t"
3082 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3083 ".int 0\n\t" /* count */
3084 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3085 "__do_recurse _build_block_set \\name \\n_max\n\t"
3086 ".endm\n");
3087
3088/*
3089 * Build a memory pool structure and initialize it
3090 * Macro uses __memory_pool_block_set_count global symbol,
3091 * block set addresses and buffer address, it may be called only after
3092 * _build_block_set
3093 */
3094__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003095 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003096 _SECTION_TYPE_SIGN "progbits\n\t"
3097 ".globl \\name\n\t"
3098 "\\name:\n\t"
3099 ".int \\max_size\n\t" /* max_block_size */
3100 ".int \\min_size\n\t" /* min_block_size */
3101 ".int \\n_max\n\t" /* nr_of_maxblocks */
3102 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3103 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3104 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3105 ".int 0\n\t" /* wait_q->head */
3106 ".int 0\n\t" /* wait_q->next */
3107 ".popsection\n\t"
3108 ".endm\n");
3109
3110#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3111 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3112 _SECTION_TYPE_SIGN "progbits\n\t"); \
3113 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3114 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3115 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3116 STRINGIFY(name) "\n\t"); \
3117 __asm__(".popsection\n\t")
3118
3119#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3120 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3121 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3122 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3123 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003124 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003125 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3126 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3127 STRINGIFY(name) "\n\t"); \
3128 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3129 __asm__(".int __memory_pool_block_set_count\n\t"); \
3130 __asm__(".popsection\n\t"); \
3131 extern uint32_t _mem_pool_block_set_count_##name; \
3132 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3133
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003134#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3135 char __noinit __aligned(align) \
3136 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003137
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003138/*
3139 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3140 * to __memory_pool_quad_block_size absolute symbol.
3141 * This function does not get called, but compiler calculates the value and
3142 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3143 */
3144static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3145{
3146 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003147#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003148 "__memory_pool_quad_block_size = %0\n\t"
3149#else
3150 "__memory_pool_quad_block_size = %c0\n\t"
3151#endif
3152 :
3153 : "n"(sizeof(struct k_mem_pool_quad_block)));
3154}
3155
3156/**
Allan Stephensc98da842016-11-11 15:45:03 -05003157 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003158 */
3159
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003160/**
Allan Stephensc98da842016-11-11 15:45:03 -05003161 * @addtogroup mem_pool_apis
3162 * @{
3163 */
3164
3165/**
3166 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003168 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3169 * long. The memory pool allows blocks to be repeatedly partitioned into
3170 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3171 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003172 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003174 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175 * If the pool is to be accessed outside the module where it is defined, it
3176 * can be declared via
3177 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003178 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * @param name Name of the memory pool.
3181 * @param min_size Size of the smallest blocks in the pool (in bytes).
3182 * @param max_size Size of the largest blocks in the pool (in bytes).
3183 * @param n_max Number of maximum sized blocks in the pool.
3184 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003185 */
3186#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003187 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3188 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003189 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003190 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3191 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3192 extern struct k_mem_pool name
3193
Peter Mitsis937042c2016-10-13 13:18:26 -04003194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * @param pool Address of the memory pool.
3200 * @param block Pointer to block descriptor for the allocated memory.
3201 * @param size Amount of memory to allocate (in bytes).
3202 * @param timeout Maximum time to wait for operation to complete
3203 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3204 * or K_FOREVER to wait as long as necessary.
3205 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003206 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003208 * @retval -ENOMEM Returned without waiting.
3209 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003210 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003211extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003212 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003213
3214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * This routine releases a previously allocated memory block back to its
3218 * memory pool.
3219 *
3220 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003221 *
3222 * @return N/A
3223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003225
3226/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * This routine instructs a memory pool to concatenate unused memory blocks
3230 * into larger blocks wherever possible. Manually defragmenting the memory
3231 * pool may speed up future allocations of memory blocks by eliminating the
3232 * need for the memory pool to perform an automatic partial defragmentation.
3233 *
3234 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003235 *
3236 * @return N/A
3237 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003238extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003239
3240/**
Allan Stephensc98da842016-11-11 15:45:03 -05003241 * @} end addtogroup mem_pool_apis
3242 */
3243
3244/**
3245 * @defgroup heap_apis Heap Memory Pool APIs
3246 * @ingroup kernel_apis
3247 * @{
3248 */
3249
3250/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003254 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003259 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003260extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003261
3262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003264 *
3265 * This routine provides traditional free() semantics. The memory being
3266 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003267 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003268 * If @a ptr is NULL, no operation is performed.
3269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003271 *
3272 * @return N/A
3273 */
3274extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275
Allan Stephensc98da842016-11-11 15:45:03 -05003276/**
3277 * @} end defgroup heap_apis
3278 */
3279
Benjamin Walshacc68c12017-01-29 18:57:45 -05003280/* polling API - PRIVATE */
3281
Benjamin Walshb0179862017-02-02 16:39:57 -05003282#ifdef CONFIG_POLL
3283#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3284#else
3285#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3286#endif
3287
Benjamin Walshacc68c12017-01-29 18:57:45 -05003288/* private - implementation data created as needed, per-type */
3289struct _poller {
3290 struct k_thread *thread;
3291};
3292
3293/* private - types bit positions */
3294enum _poll_types_bits {
3295 /* can be used to ignore an event */
3296 _POLL_TYPE_IGNORE,
3297
3298 /* to be signaled by k_poll_signal() */
3299 _POLL_TYPE_SIGNAL,
3300
3301 /* semaphore availability */
3302 _POLL_TYPE_SEM_AVAILABLE,
3303
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003304 /* queue/fifo/lifo data availability */
3305 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003306
3307 _POLL_NUM_TYPES
3308};
3309
3310#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3311
3312/* private - states bit positions */
3313enum _poll_states_bits {
3314 /* default state when creating event */
3315 _POLL_STATE_NOT_READY,
3316
3317 /* there was another poller already on the object */
3318 _POLL_STATE_EADDRINUSE,
3319
3320 /* signaled by k_poll_signal() */
3321 _POLL_STATE_SIGNALED,
3322
3323 /* semaphore is available */
3324 _POLL_STATE_SEM_AVAILABLE,
3325
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003326 /* data is available to read on queue/fifo/lifo */
3327 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003328
3329 _POLL_NUM_STATES
3330};
3331
3332#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3333
3334#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003335 (32 - (0 \
3336 + 8 /* tag */ \
3337 + _POLL_NUM_TYPES \
3338 + _POLL_NUM_STATES \
3339 + 1 /* modes */ \
3340 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003341
3342#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3343#error overflow of 32-bit word in struct k_poll_event
3344#endif
3345
3346/* end of polling API - PRIVATE */
3347
3348
3349/**
3350 * @defgroup poll_apis Async polling APIs
3351 * @ingroup kernel_apis
3352 * @{
3353 */
3354
3355/* Public polling API */
3356
3357/* public - values for k_poll_event.type bitfield */
3358#define K_POLL_TYPE_IGNORE 0
3359#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3360#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003361#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3362#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003363
3364/* public - polling modes */
3365enum k_poll_modes {
3366 /* polling thread does not take ownership of objects when available */
3367 K_POLL_MODE_NOTIFY_ONLY = 0,
3368
3369 K_POLL_NUM_MODES
3370};
3371
3372/* public - values for k_poll_event.state bitfield */
3373#define K_POLL_STATE_NOT_READY 0
3374#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3375#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3376#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003377#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3378#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003379
3380/* public - poll signal object */
3381struct k_poll_signal {
3382 /* PRIVATE - DO NOT TOUCH */
3383 struct k_poll_event *poll_event;
3384
3385 /*
3386 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3387 * user resets it to 0.
3388 */
3389 unsigned int signaled;
3390
3391 /* custom result value passed to k_poll_signal() if needed */
3392 int result;
3393};
3394
3395#define K_POLL_SIGNAL_INITIALIZER() \
3396 { \
3397 .poll_event = NULL, \
3398 .signaled = 0, \
3399 .result = 0, \
3400 }
3401
3402struct k_poll_event {
3403 /* PRIVATE - DO NOT TOUCH */
3404 struct _poller *poller;
3405
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003406 /* optional user-specified tag, opaque, untouched by the API */
3407 uint32_t tag:8;
3408
Benjamin Walshacc68c12017-01-29 18:57:45 -05003409 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
3410 uint32_t type:_POLL_NUM_TYPES;
3411
3412 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
3413 uint32_t state:_POLL_NUM_STATES;
3414
3415 /* mode of operation, from enum k_poll_modes */
3416 uint32_t mode:1;
3417
3418 /* unused bits in 32-bit word */
3419 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
3420
3421 /* per-type data */
3422 union {
3423 void *obj;
3424 struct k_poll_signal *signal;
3425 struct k_sem *sem;
3426 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003427 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003428 };
3429};
3430
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003431#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003432 { \
3433 .poller = NULL, \
3434 .type = event_type, \
3435 .state = K_POLL_STATE_NOT_READY, \
3436 .mode = event_mode, \
3437 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003438 { .obj = event_obj }, \
3439 }
3440
3441#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3442 event_tag) \
3443 { \
3444 .type = event_type, \
3445 .tag = event_tag, \
3446 .state = K_POLL_STATE_NOT_READY, \
3447 .mode = event_mode, \
3448 .unused = 0, \
3449 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003450 }
3451
3452/**
3453 * @brief Initialize one struct k_poll_event instance
3454 *
3455 * After this routine is called on a poll event, the event it ready to be
3456 * placed in an event array to be passed to k_poll().
3457 *
3458 * @param event The event to initialize.
3459 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3460 * values. Only values that apply to the same object being polled
3461 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3462 * event.
3463 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3464 * @param obj Kernel object or poll signal.
3465 *
3466 * @return N/A
3467 */
3468
3469extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
3470 int mode, void *obj);
3471
3472/**
3473 * @brief Wait for one or many of multiple poll events to occur
3474 *
3475 * This routine allows a thread to wait concurrently for one or many of
3476 * multiple poll events to have occurred. Such events can be a kernel object
3477 * being available, like a semaphore, or a poll signal event.
3478 *
3479 * When an event notifies that a kernel object is available, the kernel object
3480 * is not "given" to the thread calling k_poll(): it merely signals the fact
3481 * that the object was available when the k_poll() call was in effect. Also,
3482 * all threads trying to acquire an object the regular way, i.e. by pending on
3483 * the object, have precedence over the thread polling on the object. This
3484 * means that the polling thread will never get the poll event on an object
3485 * until the object becomes available and its pend queue is empty. For this
3486 * reason, the k_poll() call is more effective when the objects being polled
3487 * only have one thread, the polling thread, trying to acquire them.
3488 *
3489 * Only one thread can be polling for a particular object at a given time. If
3490 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3491 * and returns as soon as it has finished handling the other events. This means
3492 * that k_poll() can return -EADDRINUSE and have the state value of some events
3493 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3494 * parameter is ignored.
3495 *
3496 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3497 * events that were passed to k_poll() and check the state field for the values
3498 * that were expected and take the associated actions.
3499 *
3500 * Before being reused for another call to k_poll(), the user has to reset the
3501 * state field to K_POLL_STATE_NOT_READY.
3502 *
3503 * @param events An array of pointers to events to be polled for.
3504 * @param num_events The number of events in the array.
3505 * @param timeout Waiting period for an event to be ready (in milliseconds),
3506 * or one of the special values K_NO_WAIT and K_FOREVER.
3507 *
3508 * @retval 0 One or more events are ready.
3509 * @retval -EADDRINUSE One or more objects already had a poller.
3510 * @retval -EAGAIN Waiting period timed out.
3511 */
3512
3513extern int k_poll(struct k_poll_event *events, int num_events,
3514 int32_t timeout);
3515
3516/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003517 * @brief Initialize a poll signal object.
3518 *
3519 * Ready a poll signal object to be signaled via k_poll_signal().
3520 *
3521 * @param signal A poll signal.
3522 *
3523 * @return N/A
3524 */
3525
3526extern void k_poll_signal_init(struct k_poll_signal *signal);
3527
3528/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003529 * @brief Signal a poll signal object.
3530 *
3531 * This routine makes ready a poll signal, which is basically a poll event of
3532 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3533 * made ready to run. A @a result value can be specified.
3534 *
3535 * The poll signal contains a 'signaled' field that, when set by
3536 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3537 * be reset by the user before being passed again to k_poll() or k_poll() will
3538 * consider it being signaled, and will return immediately.
3539 *
3540 * @param signal A poll signal.
3541 * @param result The value to store in the result field of the signal.
3542 *
3543 * @retval 0 The signal was delivered successfully.
3544 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3545 */
3546
3547extern int k_poll_signal(struct k_poll_signal *signal, int result);
3548
3549/* private internal function */
3550extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
3551 uint32_t state);
3552
3553/**
3554 * @} end defgroup poll_apis
3555 */
3556
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003557/**
3558 * @brief Make the CPU idle.
3559 *
3560 * This function makes the CPU idle until an event wakes it up.
3561 *
3562 * In a regular system, the idle thread should be the only thread responsible
3563 * for making the CPU idle and triggering any type of power management.
3564 * However, in some more constrained systems, such as a single-threaded system,
3565 * the only thread would be responsible for this if needed.
3566 *
3567 * @return N/A
3568 */
3569extern void k_cpu_idle(void);
3570
3571/**
3572 * @brief Make the CPU idle in an atomic fashion.
3573 *
3574 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3575 * must be done atomically before making the CPU idle.
3576 *
3577 * @param key Interrupt locking key obtained from irq_lock().
3578 *
3579 * @return N/A
3580 */
3581extern void k_cpu_atomic_idle(unsigned int key);
3582
Andrew Boie350f88d2017-01-18 13:13:45 -08003583extern void _sys_power_save_idle_exit(int32_t ticks);
3584
Anas Nashifa6149502017-01-17 07:47:31 -05003585/* Include legacy APIs */
3586#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003588#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003589#include <arch/cpu.h>
3590
3591/*
3592 * private APIs that are utilized by one or more public APIs
3593 */
3594
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003595#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003596extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003597#else
3598#define _init_static_threads() do { } while ((0))
3599#endif
3600
3601extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003602extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003603
3604#ifdef __cplusplus
3605}
3606#endif
3607
Andrew Boiee004dec2016-11-07 09:01:19 -08003608#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3609/*
3610 * Define new and delete operators.
3611 * At this moment, the operators do nothing since objects are supposed
3612 * to be statically allocated.
3613 */
3614inline void operator delete(void *ptr)
3615{
3616 (void)ptr;
3617}
3618
3619inline void operator delete[](void *ptr)
3620{
3621 (void)ptr;
3622}
3623
3624inline void *operator new(size_t size)
3625{
3626 (void)size;
3627 return NULL;
3628}
3629
3630inline void *operator new[](size_t size)
3631{
3632 (void)size;
3633 return NULL;
3634}
3635
3636/* Placement versions of operator new and delete */
3637inline void operator delete(void *ptr1, void *ptr2)
3638{
3639 (void)ptr1;
3640 (void)ptr2;
3641}
3642
3643inline void operator delete[](void *ptr1, void *ptr2)
3644{
3645 (void)ptr1;
3646 (void)ptr2;
3647}
3648
3649inline void *operator new(size_t size, void *ptr)
3650{
3651 (void)size;
3652 return ptr;
3653}
3654
3655inline void *operator new[](size_t size, void *ptr)
3656{
3657 (void)size;
3658 return ptr;
3659}
3660
3661#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3662
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003663#endif /* !_ASMLANGUAGE */
3664
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003665#endif /* _kernel__h_ */