blob: 611de311840351eb098eceb90a4a8169dcd06b4e [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 {
1286 _wait_q_t wait_q;
1287 sys_slist_t data_q;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001288 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001289
Anas Nashif2f203c22016-12-18 06:57:45 -05001290 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291};
1292
Allan Stephensc98da842016-11-11 15:45:03 -05001293#define K_FIFO_INITIALIZER(obj) \
1294 { \
1295 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1296 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05001297 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05001298 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001299 }
1300
1301/**
1302 * INTERNAL_HIDDEN @endcond
1303 */
1304
1305/**
1306 * @defgroup fifo_apis Fifo APIs
1307 * @ingroup kernel_apis
1308 * @{
1309 */
1310
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001311/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001312 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001314 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001317 *
1318 * @return N/A
1319 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001320extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001321
1322/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001325 * This routine adds a data item to @a fifo. A fifo data item must be
1326 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1327 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001329 * @note Can be called by ISRs.
1330 *
1331 * @param fifo Address of the fifo.
1332 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001333 *
1334 * @return N/A
1335 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001336extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001337
1338/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001339 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341 * This routine adds a list of data items to @a fifo in one operation.
1342 * The data items must be in a singly-linked list, with the first 32 bits
1343 * each data item pointing to the next data item; the list must be
1344 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001346 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001348 * @param fifo Address of the fifo.
1349 * @param head Pointer to first node in singly-linked list.
1350 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001351 *
1352 * @return N/A
1353 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001355
1356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * This routine adds a list of data items to @a fifo in one operation.
1360 * The data items must be in a singly-linked list implemented using a
1361 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001362 * and must be re-initialized via sys_slist_init().
1363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001364 * @note Can be called by ISRs.
1365 *
1366 * @param fifo Address of the fifo.
1367 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 *
1369 * @return N/A
1370 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001371extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372
1373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * This routine removes a data item from @a fifo in a "first in, first out"
1377 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001379 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1380 *
1381 * @param fifo Address of the fifo.
1382 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001383 * or one of the special values K_NO_WAIT and K_FOREVER.
1384 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001385 * @return Address of the data item if successful; NULL if returned
1386 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001387 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1389
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001391 * @brief Query a fifo to see if it has data available.
1392 *
1393 * Note that the data might be already gone by the time this function returns
1394 * if other threads is also trying to read from the fifo.
1395 *
1396 * @note Can be called by ISRs.
1397 *
1398 * @param fifo Address of the fifo.
1399 *
1400 * @return Non-zero if the fifo is empty.
1401 * @return 0 if data is available.
1402 */
1403static inline int k_fifo_is_empty(struct k_fifo *fifo)
1404{
1405 return (int)sys_slist_is_empty(&fifo->data_q);
1406}
1407
1408/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001411 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001413 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001415 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001416 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001418 struct k_fifo name \
1419 __in_section(_k_fifo, static, name) = \
1420 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001421
Allan Stephensc98da842016-11-11 15:45:03 -05001422/**
1423 * @} end defgroup fifo_apis
1424 */
1425
1426/**
1427 * @cond INTERNAL_HIDDEN
1428 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429
1430struct k_lifo {
1431 _wait_q_t wait_q;
1432 void *list;
1433
Anas Nashif2f203c22016-12-18 06:57:45 -05001434 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435};
1436
Allan Stephensc98da842016-11-11 15:45:03 -05001437#define K_LIFO_INITIALIZER(obj) \
1438 { \
1439 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1440 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001441 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001442 }
1443
1444/**
1445 * INTERNAL_HIDDEN @endcond
1446 */
1447
1448/**
1449 * @defgroup lifo_apis Lifo APIs
1450 * @ingroup kernel_apis
1451 * @{
1452 */
1453
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001460 *
1461 * @return N/A
1462 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001464
1465/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * This routine adds a data item to @a lifo. A lifo data item must be
1469 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1470 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001472 * @note Can be called by ISRs.
1473 *
1474 * @param lifo Address of the lifo.
1475 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 *
1477 * @return N/A
1478 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001479extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001480
1481/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001482 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001484 * This routine removes a data item from @a lifo in a "last in, first out"
1485 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001487 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1488 *
1489 * @param lifo Address of the lifo.
1490 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001491 * or one of the special values K_NO_WAIT and K_FOREVER.
1492 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001493 * @return Address of the data item if successful; NULL if returned
1494 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001496extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1497
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001501 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001503 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001506 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001508 struct k_lifo name \
1509 __in_section(_k_lifo, static, name) = \
1510 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001511
Allan Stephensc98da842016-11-11 15:45:03 -05001512/**
1513 * @} end defgroup lifo_apis
1514 */
1515
1516/**
1517 * @cond INTERNAL_HIDDEN
1518 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519
1520struct k_stack {
1521 _wait_q_t wait_q;
1522 uint32_t *base, *next, *top;
1523
Anas Nashif2f203c22016-12-18 06:57:45 -05001524 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525};
1526
Allan Stephensc98da842016-11-11 15:45:03 -05001527#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1528 { \
1529 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1530 .base = stack_buffer, \
1531 .next = stack_buffer, \
1532 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001533 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001534 }
1535
1536/**
1537 * INTERNAL_HIDDEN @endcond
1538 */
1539
1540/**
1541 * @defgroup stack_apis Stack APIs
1542 * @ingroup kernel_apis
1543 * @{
1544 */
1545
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001546/**
1547 * @brief Initialize a stack.
1548 *
1549 * This routine initializes a stack object, prior to its first use.
1550 *
1551 * @param stack Address of the stack.
1552 * @param buffer Address of array used to hold stacked values.
1553 * @param num_entries Maximum number of values that can be stacked.
1554 *
1555 * @return N/A
1556 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001557extern void k_stack_init(struct k_stack *stack,
1558 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559
1560/**
1561 * @brief Push an element onto a stack.
1562 *
1563 * This routine adds a 32-bit value @a data to @a stack.
1564 *
1565 * @note Can be called by ISRs.
1566 *
1567 * @param stack Address of the stack.
1568 * @param data Value to push onto the stack.
1569 *
1570 * @return N/A
1571 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573
1574/**
1575 * @brief Pop an element from a stack.
1576 *
1577 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1578 * manner and stores the value in @a data.
1579 *
1580 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1581 *
1582 * @param stack Address of the stack.
1583 * @param data Address of area to hold the value popped from the stack.
1584 * @param timeout Waiting period to obtain a value (in milliseconds),
1585 * or one of the special values K_NO_WAIT and K_FOREVER.
1586 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001587 * @retval 0 Element popped from stack.
1588 * @retval -EBUSY Returned without waiting.
1589 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1592
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001597 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001598 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001600 * @param name Name of the stack.
1601 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001602 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001603#define K_STACK_DEFINE(name, stack_num_entries) \
1604 uint32_t __noinit \
1605 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001606 struct k_stack name \
1607 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001608 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1609 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001610
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001611/**
Allan Stephensc98da842016-11-11 15:45:03 -05001612 * @} end defgroup stack_apis
1613 */
1614
Allan Stephens6bba9b02016-11-16 14:56:54 -05001615struct k_work;
1616
Allan Stephensc98da842016-11-11 15:45:03 -05001617/**
1618 * @defgroup workqueue_apis Workqueue Thread APIs
1619 * @ingroup kernel_apis
1620 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001621 */
1622
Allan Stephens6bba9b02016-11-16 14:56:54 -05001623/**
1624 * @typedef k_work_handler_t
1625 * @brief Work item handler function type.
1626 *
1627 * A work item's handler function is executed by a workqueue's thread
1628 * when the work item is processed by the workqueue.
1629 *
1630 * @param work Address of the work item.
1631 *
1632 * @return N/A
1633 */
1634typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635
1636/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001637 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001638 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001639
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001640struct k_work_q {
1641 struct k_fifo fifo;
1642};
1643
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001644enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001645 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001646};
1647
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648struct k_work {
1649 void *_reserved; /* Used by k_fifo implementation. */
1650 k_work_handler_t handler;
1651 atomic_t flags[1];
1652};
1653
Allan Stephens6bba9b02016-11-16 14:56:54 -05001654struct k_delayed_work {
1655 struct k_work work;
1656 struct _timeout timeout;
1657 struct k_work_q *work_q;
1658};
1659
1660extern struct k_work_q k_sys_work_q;
1661
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001662/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001663 * INTERNAL_HIDDEN @endcond
1664 */
1665
1666/**
1667 * @brief Initialize a statically-defined work item.
1668 *
1669 * This macro can be used to initialize a statically-defined workqueue work
1670 * item, prior to its first use. For example,
1671 *
1672 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1673 *
1674 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675 */
1676#define K_WORK_INITIALIZER(work_handler) \
1677 { \
1678 ._reserved = NULL, \
1679 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001680 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001681 }
1682
1683/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001684 * @brief Initialize a work item.
1685 *
1686 * This routine initializes a workqueue work item, prior to its first use.
1687 *
1688 * @param work Address of work item.
1689 * @param handler Function to invoke each time work item is processed.
1690 *
1691 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001692 */
1693static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1694{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001695 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001696 work->handler = handler;
1697}
1698
1699/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001700 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001701 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001702 * This routine submits work item @a work to be processed by workqueue
1703 * @a work_q. If the work item is already pending in the workqueue's queue
1704 * as a result of an earlier submission, this routine has no effect on the
1705 * work item. If the work item has already been processed, or is currently
1706 * being processed, its work is considered complete and the work item can be
1707 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001708 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001709 * @warning
1710 * A submitted work item must not be modified until it has been processed
1711 * by the workqueue.
1712 *
1713 * @note Can be called by ISRs.
1714 *
1715 * @param work_q Address of workqueue.
1716 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001717 *
1718 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001719 */
1720static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1721 struct k_work *work)
1722{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001723 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724 k_fifo_put(&work_q->fifo, work);
1725 }
1726}
1727
1728/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001729 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001731 * This routine indicates if work item @a work is pending in a workqueue's
1732 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001734 * @note Can be called by ISRs.
1735 *
1736 * @param work Address of work item.
1737 *
1738 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001739 */
1740static inline int k_work_pending(struct k_work *work)
1741{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001742 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001743}
1744
1745/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001746 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001747 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001748 * This routine starts workqueue @a work_q. The workqueue spawns its work
1749 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001751 * @param work_q Address of workqueue.
1752 * @param stack Pointer to work queue thread's stack space.
1753 * @param stack_size Size of the work queue thread's stack (in bytes).
1754 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755 *
1756 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001757 */
Allan Stephens904cf972016-10-07 13:59:23 -05001758extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001759 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001762 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001764 * This routine initializes a workqueue delayed work item, prior to
1765 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001767 * @param work Address of delayed work item.
1768 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 *
1770 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001771 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001772extern void k_delayed_work_init(struct k_delayed_work *work,
1773 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774
1775/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001776 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001777 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001778 * This routine schedules work item @a work to be processed by workqueue
1779 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1780 * an asychronous countdown for the work item and then returns to the caller.
1781 * Only when the countdown completes is the work item actually submitted to
1782 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001783 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001784 * Submitting a previously submitted delayed work item that is still
1785 * counting down cancels the existing submission and restarts the countdown
1786 * using the new delay. If the work item is currently pending on the
1787 * workqueue's queue because the countdown has completed it is too late to
1788 * resubmit the item, and resubmission fails without impacting the work item.
1789 * If the work item has already been processed, or is currently being processed,
1790 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001792 * @warning
1793 * A delayed work item must not be modified until it has been processed
1794 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001796 * @note Can be called by ISRs.
1797 *
1798 * @param work_q Address of workqueue.
1799 * @param work Address of delayed work item.
1800 * @param delay Delay before submitting the work item (in milliseconds).
1801 *
1802 * @retval 0 Work item countdown started.
1803 * @retval -EINPROGRESS Work item is already pending.
1804 * @retval -EINVAL Work item is being processed or has completed its work.
1805 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001807extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1808 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001809 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001810
1811/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001812 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001814 * This routine cancels the submission of delayed work item @a work.
1815 * A delayed work item can only be cancelled while its countdown is still
1816 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001817 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001818 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001820 * @param work Address of delayed work item.
1821 *
1822 * @retval 0 Work item countdown cancelled.
1823 * @retval -EINPROGRESS Work item is already pending.
1824 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001826extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001827
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001828/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001829 * @brief Submit a work item to the system workqueue.
1830 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001831 * This routine submits work item @a work to be processed by the system
1832 * workqueue. If the work item is already pending in the workqueue's queue
1833 * as a result of an earlier submission, this routine has no effect on the
1834 * work item. If the work item has already been processed, or is currently
1835 * being processed, its work is considered complete and the work item can be
1836 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001838 * @warning
1839 * Work items submitted to the system workqueue should avoid using handlers
1840 * that block or yield since this may prevent the system workqueue from
1841 * processing other work items in a timely manner.
1842 *
1843 * @note Can be called by ISRs.
1844 *
1845 * @param work Address of work item.
1846 *
1847 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001848 */
1849static inline void k_work_submit(struct k_work *work)
1850{
1851 k_work_submit_to_queue(&k_sys_work_q, work);
1852}
1853
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001854/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855 * @brief Submit a delayed work item to the system workqueue.
1856 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001857 * This routine schedules work item @a work to be processed by the system
1858 * workqueue after a delay of @a delay milliseconds. The routine initiates
1859 * an asychronous countdown for the work item and then returns to the caller.
1860 * Only when the countdown completes is the work item actually submitted to
1861 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001862 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001863 * Submitting a previously submitted delayed work item that is still
1864 * counting down cancels the existing submission and restarts the countdown
1865 * using the new delay. If the work item is currently pending on the
1866 * workqueue's queue because the countdown has completed it is too late to
1867 * resubmit the item, and resubmission fails without impacting the work item.
1868 * If the work item has already been processed, or is currently being processed,
1869 * its work is considered complete and the work item can be resubmitted.
1870 *
1871 * @warning
1872 * Work items submitted to the system workqueue should avoid using handlers
1873 * that block or yield since this may prevent the system workqueue from
1874 * processing other work items in a timely manner.
1875 *
1876 * @note Can be called by ISRs.
1877 *
1878 * @param work Address of delayed work item.
1879 * @param delay Delay before submitting the work item (in milliseconds).
1880 *
1881 * @retval 0 Work item countdown started.
1882 * @retval -EINPROGRESS Work item is already pending.
1883 * @retval -EINVAL Work item is being processed or has completed its work.
1884 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001885 */
1886static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001887 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001889 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890}
1891
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001893 * @brief Get time remaining before a delayed work gets scheduled.
1894 *
1895 * This routine computes the (approximate) time remaining before a
1896 * delayed work gets executed. If the delayed work is not waiting to be
1897 * schedules, it returns zero.
1898 *
1899 * @param work Delayed work item.
1900 *
1901 * @return Remaining time (in milliseconds).
1902 */
1903static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1904{
1905 return _timeout_remaining_get(&work->timeout);
1906}
1907
1908/**
Allan Stephensc98da842016-11-11 15:45:03 -05001909 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001910 */
1911
Allan Stephensc98da842016-11-11 15:45:03 -05001912/**
1913 * @cond INTERNAL_HIDDEN
1914 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001915
1916struct k_mutex {
1917 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001918 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001919 uint32_t lock_count;
1920 int owner_orig_prio;
1921#ifdef CONFIG_OBJECT_MONITOR
1922 int num_lock_state_changes;
1923 int num_conflicts;
1924#endif
1925
Anas Nashif2f203c22016-12-18 06:57:45 -05001926 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927};
1928
1929#ifdef CONFIG_OBJECT_MONITOR
1930#define _MUTEX_INIT_OBJECT_MONITOR \
1931 .num_lock_state_changes = 0, .num_conflicts = 0,
1932#else
1933#define _MUTEX_INIT_OBJECT_MONITOR
1934#endif
1935
1936#define K_MUTEX_INITIALIZER(obj) \
1937 { \
1938 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1939 .owner = NULL, \
1940 .lock_count = 0, \
1941 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1942 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001943 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944 }
1945
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001946/**
Allan Stephensc98da842016-11-11 15:45:03 -05001947 * INTERNAL_HIDDEN @endcond
1948 */
1949
1950/**
1951 * @defgroup mutex_apis Mutex APIs
1952 * @ingroup kernel_apis
1953 * @{
1954 */
1955
1956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001961 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001966 struct k_mutex name \
1967 __in_section(_k_mutex, static, name) = \
1968 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * Upon completion, the mutex is available and does not have an owner.
1976 *
1977 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
1979 * @return N/A
1980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982
1983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001984 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * This routine locks @a mutex. If the mutex is locked by another thread,
1987 * the calling thread waits until the mutex becomes available or until
1988 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001990 * A thread is permitted to lock a mutex it has already locked. The operation
1991 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * @param mutex Address of the mutex.
1994 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995 * or one of the special values K_NO_WAIT and K_FOREVER.
1996 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001997 * @retval 0 Mutex locked.
1998 * @retval -EBUSY Returned without waiting.
1999 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002001extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002
2003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * This routine unlocks @a mutex. The mutex must already be locked by the
2007 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 *
2009 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 * thread.
2012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002013 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014 *
2015 * @return N/A
2016 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017extern void k_mutex_unlock(struct k_mutex *mutex);
2018
Allan Stephensc98da842016-11-11 15:45:03 -05002019/**
2020 * @} end defgroup mutex_apis
2021 */
2022
2023/**
2024 * @cond INTERNAL_HIDDEN
2025 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026
2027struct k_sem {
2028 _wait_q_t wait_q;
2029 unsigned int count;
2030 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002031 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032
Anas Nashif2f203c22016-12-18 06:57:45 -05002033 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034};
2035
Allan Stephensc98da842016-11-11 15:45:03 -05002036#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2037 { \
2038 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2039 .count = initial_count, \
2040 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002041 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002042 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002043 }
2044
2045/**
2046 * INTERNAL_HIDDEN @endcond
2047 */
2048
2049/**
2050 * @defgroup semaphore_apis Semaphore APIs
2051 * @ingroup kernel_apis
2052 * @{
2053 */
2054
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002060 * @param sem Address of the semaphore.
2061 * @param initial_count Initial semaphore count.
2062 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002063 *
2064 * @return N/A
2065 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2067 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002068
2069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002072 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2075 *
2076 * @param sem Address of the semaphore.
2077 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002078 * or one of the special values K_NO_WAIT and K_FOREVER.
2079 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002080 * @note When porting code from the nanokernel legacy API to the new API, be
2081 * careful with the return value of this function. The return value is the
2082 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2083 * non-zero means failure, while the nano_sem_take family returns 1 for success
2084 * and 0 for failure.
2085 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002086 * @retval 0 Semaphore taken.
2087 * @retval -EBUSY Returned without waiting.
2088 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002089 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002091
2092/**
2093 * @brief Give a semaphore.
2094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * This routine gives @a sem, unless the semaphore is already at its maximum
2096 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002101 *
2102 * @return N/A
2103 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104extern void k_sem_give(struct k_sem *sem);
2105
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002106/**
2107 * @brief Reset a semaphore's count to zero.
2108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002112 *
2113 * @return N/A
2114 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002115static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116{
2117 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118}
2119
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002120/**
2121 * @brief Get a semaphore's count.
2122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002125 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002128 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002129static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002130{
2131 return sem->count;
2132}
2133
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002138 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002139 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 * @param name Name of the semaphore.
2142 * @param initial_count Initial semaphore count.
2143 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002144 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002145#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002146 struct k_sem name \
2147 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148 K_SEM_INITIALIZER(name, initial_count, count_limit)
2149
Allan Stephensc98da842016-11-11 15:45:03 -05002150/**
2151 * @} end defgroup semaphore_apis
2152 */
2153
2154/**
2155 * @defgroup alert_apis Alert APIs
2156 * @ingroup kernel_apis
2157 * @{
2158 */
2159
Allan Stephens5eceb852016-11-16 10:16:30 -05002160/**
2161 * @typedef k_alert_handler_t
2162 * @brief Alert handler function type.
2163 *
2164 * An alert's alert handler function is invoked by the system workqueue
2165 * when the alert is signalled. The alert handler function is optional,
2166 * and is only invoked if the alert has been initialized with one.
2167 *
2168 * @param alert Address of the alert.
2169 *
2170 * @return 0 if alert has been consumed; non-zero if alert should pend.
2171 */
2172typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002173
2174/**
2175 * @} end defgroup alert_apis
2176 */
2177
2178/**
2179 * @cond INTERNAL_HIDDEN
2180 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002182#define K_ALERT_DEFAULT NULL
2183#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002185struct k_alert {
2186 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002187 atomic_t send_count;
2188 struct k_work work_item;
2189 struct k_sem sem;
2190
Anas Nashif2f203c22016-12-18 06:57:45 -05002191 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192};
2193
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002194extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002196#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002198 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002199 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002200 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002201 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002202 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203 }
2204
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205/**
Allan Stephensc98da842016-11-11 15:45:03 -05002206 * INTERNAL_HIDDEN @endcond
2207 */
2208
2209/**
2210 * @addtogroup alert_apis
2211 * @{
2212 */
2213
2214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002215 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002217 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002219 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002221 * @param name Name of the alert.
2222 * @param alert_handler Action to take when alert is sent. Specify either
2223 * the address of a function to be invoked by the system workqueue
2224 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2225 * K_ALERT_DEFAULT (which causes the alert to pend).
2226 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002228#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002229 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002230 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002231 K_ALERT_INITIALIZER(name, alert_handler, \
2232 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002239 * @param alert Address of the alert.
2240 * @param handler Action to take when alert is sent. Specify either the address
2241 * of a function to be invoked by the system workqueue thread,
2242 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2243 * K_ALERT_DEFAULT (which causes the alert to pend).
2244 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
2246 * @return N/A
2247 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002248extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2249 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250
2251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002252 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002254 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002256 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2257 *
2258 * @param alert Address of the alert.
2259 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 * or one of the special values K_NO_WAIT and K_FOREVER.
2261 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002262 * @retval 0 Alert received.
2263 * @retval -EBUSY Returned without waiting.
2264 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002266extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267
2268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002269 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * This routine signals @a alert. The action specified for @a alert will
2272 * be taken, which may trigger the execution of an alert handler function
2273 * and/or cause the alert to pend (assuming the alert has not reached its
2274 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * @note Can be called by ISRs.
2277 *
2278 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
2280 * @return N/A
2281 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002282extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002283
2284/**
Allan Stephensc98da842016-11-11 15:45:03 -05002285 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002286 */
2287
Allan Stephensc98da842016-11-11 15:45:03 -05002288/**
2289 * @cond INTERNAL_HIDDEN
2290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002291
2292struct k_msgq {
2293 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002294 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 uint32_t max_msgs;
2296 char *buffer_start;
2297 char *buffer_end;
2298 char *read_ptr;
2299 char *write_ptr;
2300 uint32_t used_msgs;
2301
Anas Nashif2f203c22016-12-18 06:57:45 -05002302 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303};
2304
Peter Mitsis1da807e2016-10-06 11:36:59 -04002305#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 { \
2307 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002308 .max_msgs = q_max_msgs, \
2309 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002311 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 .read_ptr = q_buffer, \
2313 .write_ptr = q_buffer, \
2314 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002315 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316 }
2317
Peter Mitsis1da807e2016-10-06 11:36:59 -04002318/**
Allan Stephensc98da842016-11-11 15:45:03 -05002319 * INTERNAL_HIDDEN @endcond
2320 */
2321
2322/**
2323 * @defgroup msgq_apis Message Queue APIs
2324 * @ingroup kernel_apis
2325 * @{
2326 */
2327
2328/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2332 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002333 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2334 * message is similarly aligned to this boundary, @a q_msg_size must also be
2335 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * The message queue can be accessed outside the module where it is defined
2338 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002340 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @param q_name Name of the message queue.
2343 * @param q_msg_size Message size (in bytes).
2344 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002345 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002346 */
2347#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2348 static char __noinit __aligned(q_align) \
2349 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002350 struct k_msgq q_name \
2351 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002352 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2353 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
Peter Mitsisd7a37502016-10-13 11:37:40 -04002355/**
2356 * @brief Initialize a message queue.
2357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * This routine initializes a message queue object, prior to its first use.
2359 *
Allan Stephensda827222016-11-09 14:23:58 -06002360 * The message queue's ring buffer must contain space for @a max_msgs messages,
2361 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2362 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2363 * that each message is similarly aligned to this boundary, @a q_msg_size
2364 * must also be a multiple of N.
2365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * @param q Address of the message queue.
2367 * @param buffer Pointer to ring buffer that holds queued messages.
2368 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002369 * @param max_msgs Maximum number of messages that can be queued.
2370 *
2371 * @return N/A
2372 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002373extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002374 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002375
2376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002380 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002381 * @note Can be called by ISRs.
2382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @param q Address of the message queue.
2384 * @param data Pointer to the message.
2385 * @param timeout Waiting period to add the message (in milliseconds),
2386 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002387 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002388 * @retval 0 Message sent.
2389 * @retval -ENOMSG Returned without waiting or queue purged.
2390 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002391 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002393
2394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * This routine receives a message from message queue @a q in a "first in,
2398 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002399 *
Allan Stephensc98da842016-11-11 15:45:03 -05002400 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * @param q Address of the message queue.
2403 * @param data Address of area to hold the received message.
2404 * @param timeout Waiting period to receive the message (in milliseconds),
2405 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002406 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002407 * @retval 0 Message received.
2408 * @retval -ENOMSG Returned without waiting.
2409 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002410 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002412
2413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * This routine discards all unreceived messages in a message queue's ring
2417 * buffer. Any threads that are blocked waiting to send a message to the
2418 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002421 *
2422 * @return N/A
2423 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424extern void k_msgq_purge(struct k_msgq *q);
2425
Peter Mitsis67be2492016-10-07 11:44:34 -04002426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * This routine returns the number of unused entries in a message queue's
2430 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 * @param q Address of the message queue.
2433 *
2434 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002435 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002436static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002437{
2438 return q->max_msgs - q->used_msgs;
2439}
2440
Peter Mitsisd7a37502016-10-13 11:37:40 -04002441/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * @param q Address of the message queue.
2447 *
2448 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002449 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002450static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451{
2452 return q->used_msgs;
2453}
2454
Allan Stephensc98da842016-11-11 15:45:03 -05002455/**
2456 * @} end defgroup msgq_apis
2457 */
2458
2459/**
2460 * @defgroup mem_pool_apis Memory Pool APIs
2461 * @ingroup kernel_apis
2462 * @{
2463 */
2464
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002466 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467 void *addr_in_pool;
2468 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002469 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470};
2471
Allan Stephensc98da842016-11-11 15:45:03 -05002472/**
2473 * @} end defgroup mem_pool_apis
2474 */
2475
2476/**
2477 * @defgroup mailbox_apis Mailbox APIs
2478 * @ingroup kernel_apis
2479 * @{
2480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481
2482struct k_mbox_msg {
2483 /** internal use only - needed for legacy API support */
2484 uint32_t _mailbox;
2485 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002486 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487 /** application-defined information value */
2488 uint32_t info;
2489 /** sender's message data buffer */
2490 void *tx_data;
2491 /** internal use only - needed for legacy API support */
2492 void *_rx_data;
2493 /** message data block descriptor */
2494 struct k_mem_block tx_block;
2495 /** source thread id */
2496 k_tid_t rx_source_thread;
2497 /** target thread id */
2498 k_tid_t tx_target_thread;
2499 /** internal use only - thread waiting on send (may be a dummy) */
2500 k_tid_t _syncing_thread;
2501#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2502 /** internal use only - semaphore used during asynchronous send */
2503 struct k_sem *_async_sem;
2504#endif
2505};
2506
Allan Stephensc98da842016-11-11 15:45:03 -05002507/**
2508 * @cond INTERNAL_HIDDEN
2509 */
2510
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511struct k_mbox {
2512 _wait_q_t tx_msg_queue;
2513 _wait_q_t rx_msg_queue;
2514
Anas Nashif2f203c22016-12-18 06:57:45 -05002515 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516};
2517
2518#define K_MBOX_INITIALIZER(obj) \
2519 { \
2520 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2521 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002522 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523 }
2524
Peter Mitsis12092702016-10-14 12:57:23 -04002525/**
Allan Stephensc98da842016-11-11 15:45:03 -05002526 * INTERNAL_HIDDEN @endcond
2527 */
2528
2529/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002534 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002537 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002539 struct k_mbox name \
2540 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541 K_MBOX_INITIALIZER(name) \
2542
Peter Mitsis12092702016-10-14 12:57:23 -04002543/**
2544 * @brief Initialize a mailbox.
2545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * This routine initializes a mailbox object, prior to its first use.
2547 *
2548 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002549 *
2550 * @return N/A
2551 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552extern void k_mbox_init(struct k_mbox *mbox);
2553
Peter Mitsis12092702016-10-14 12:57:23 -04002554/**
2555 * @brief Send a mailbox message in a synchronous manner.
2556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * This routine sends a message to @a mbox and waits for a receiver to both
2558 * receive and process it. The message data may be in a buffer, in a memory
2559 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * @param mbox Address of the mailbox.
2562 * @param tx_msg Address of the transmit message descriptor.
2563 * @param timeout Waiting period for the message to be received (in
2564 * milliseconds), or one of the special values K_NO_WAIT
2565 * and K_FOREVER. Once the message has been received,
2566 * this routine waits as long as necessary for the message
2567 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002568 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002569 * @retval 0 Message sent.
2570 * @retval -ENOMSG Returned without waiting.
2571 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002572 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002573extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002574 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002575
Peter Mitsis12092702016-10-14 12:57:23 -04002576/**
2577 * @brief Send a mailbox message in an asynchronous manner.
2578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * This routine sends a message to @a mbox without waiting for a receiver
2580 * to process it. The message data may be in a buffer, in a memory pool block,
2581 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2582 * will be given when the message has been both received and completely
2583 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585 * @param mbox Address of the mailbox.
2586 * @param tx_msg Address of the transmit message descriptor.
2587 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002588 *
2589 * @return N/A
2590 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002591extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 struct k_sem *sem);
2593
Peter Mitsis12092702016-10-14 12:57:23 -04002594/**
2595 * @brief Receive a mailbox message.
2596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * This routine receives a message from @a mbox, then optionally retrieves
2598 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * @param mbox Address of the mailbox.
2601 * @param rx_msg Address of the receive message descriptor.
2602 * @param buffer Address of the buffer to receive data, or NULL to defer data
2603 * retrieval and message disposal until later.
2604 * @param timeout Waiting period for a message to be received (in
2605 * milliseconds), or one of the special values K_NO_WAIT
2606 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002607 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002608 * @retval 0 Message received.
2609 * @retval -ENOMSG Returned without waiting.
2610 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002611 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002612extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002614
2615/**
2616 * @brief Retrieve mailbox message data into a buffer.
2617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * This routine completes the processing of a received message by retrieving
2619 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002620 *
2621 * Alternatively, this routine can be used to dispose of a received message
2622 * without retrieving its data.
2623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @param rx_msg Address of the receive message descriptor.
2625 * @param buffer Address of the buffer to receive data, or NULL to discard
2626 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002627 *
2628 * @return N/A
2629 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002630extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002631
2632/**
2633 * @brief Retrieve mailbox message data into a memory pool block.
2634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * This routine completes the processing of a received message by retrieving
2636 * its data into a memory pool block, then disposing of the message.
2637 * The memory pool block that results from successful retrieval must be
2638 * returned to the pool once the data has been processed, even in cases
2639 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002640 *
2641 * Alternatively, this routine can be used to dispose of a received message
2642 * without retrieving its data. In this case there is no need to return a
2643 * memory pool block to the pool.
2644 *
2645 * This routine allocates a new memory pool block for the data only if the
2646 * data is not already in one. If a new block cannot be allocated, the routine
2647 * returns a failure code and the received message is left unchanged. This
2648 * permits the caller to reattempt data retrieval at a later time or to dispose
2649 * of the received message without retrieving its data.
2650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * @param rx_msg Address of a receive message descriptor.
2652 * @param pool Address of memory pool, or NULL to discard data.
2653 * @param block Address of the area to hold memory pool block info.
2654 * @param timeout Waiting period to wait for a memory pool block (in
2655 * milliseconds), or one of the special values K_NO_WAIT
2656 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002657 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002658 * @retval 0 Data retrieved.
2659 * @retval -ENOMEM Returned without waiting.
2660 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002661 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002662extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002663 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 struct k_mem_block *block, int32_t timeout);
2665
Allan Stephensc98da842016-11-11 15:45:03 -05002666/**
2667 * @} end defgroup mailbox_apis
2668 */
2669
2670/**
2671 * @cond INTERNAL_HIDDEN
2672 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673
2674struct k_pipe {
2675 unsigned char *buffer; /* Pipe buffer: may be NULL */
2676 size_t size; /* Buffer size */
2677 size_t bytes_used; /* # bytes used in buffer */
2678 size_t read_index; /* Where in buffer to read from */
2679 size_t write_index; /* Where in buffer to write */
2680
2681 struct {
2682 _wait_q_t readers; /* Reader wait queue */
2683 _wait_q_t writers; /* Writer wait queue */
2684 } wait_q;
2685
Anas Nashif2f203c22016-12-18 06:57:45 -05002686 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687};
2688
Peter Mitsise5d9c582016-10-14 14:44:57 -04002689#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 { \
2691 .buffer = pipe_buffer, \
2692 .size = pipe_buffer_size, \
2693 .bytes_used = 0, \
2694 .read_index = 0, \
2695 .write_index = 0, \
2696 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2697 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002698 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699 }
2700
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701/**
Allan Stephensc98da842016-11-11 15:45:03 -05002702 * INTERNAL_HIDDEN @endcond
2703 */
2704
2705/**
2706 * @defgroup pipe_apis Pipe APIs
2707 * @ingroup kernel_apis
2708 * @{
2709 */
2710
2711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002712 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002716 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @param name Name of the pipe.
2719 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2720 * or zero if no ring buffer is used.
2721 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002722 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002723#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2724 static unsigned char __noinit __aligned(pipe_align) \
2725 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002726 struct k_pipe name \
2727 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002728 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * This routine initializes a pipe object, prior to its first use.
2734 *
2735 * @param pipe Address of the pipe.
2736 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2737 * is used.
2738 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2739 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740 *
2741 * @return N/A
2742 */
2743extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2744 size_t size);
2745
2746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002749 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002751 * @param pipe Address of the pipe.
2752 * @param data Address of data to write.
2753 * @param bytes_to_write Size of data (in bytes).
2754 * @param bytes_written Address of area to hold the number of bytes written.
2755 * @param min_xfer Minimum number of bytes to write.
2756 * @param timeout Waiting period to wait for the data to be written (in
2757 * milliseconds), or one of the special values K_NO_WAIT
2758 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002760 * @retval 0 At least @a min_xfer bytes of data were written.
2761 * @retval -EIO Returned without waiting; zero data bytes were written.
2762 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002765extern int k_pipe_put(struct k_pipe *pipe, void *data,
2766 size_t bytes_to_write, size_t *bytes_written,
2767 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768
2769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002772 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002774 * @param pipe Address of the pipe.
2775 * @param data Address to place the data read from pipe.
2776 * @param bytes_to_read Maximum number of data bytes to read.
2777 * @param bytes_read Address of area to hold the number of bytes read.
2778 * @param min_xfer Minimum number of data bytes to read.
2779 * @param timeout Waiting period to wait for the data to be read (in
2780 * milliseconds), or one of the special values K_NO_WAIT
2781 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002783 * @retval 0 At least @a min_xfer bytes of data were read.
2784 * @retval -EIO Returned without waiting; zero data bytes were read.
2785 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002788extern int k_pipe_get(struct k_pipe *pipe, void *data,
2789 size_t bytes_to_read, size_t *bytes_read,
2790 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791
2792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * This routine writes the data contained in a memory block to @a pipe.
2796 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 * @param block Memory block containing data to send
2801 * @param size Number of data bytes in memory block to send
2802 * @param sem Semaphore to signal upon completion (else NULL)
2803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805 */
2806extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2807 size_t size, struct k_sem *sem);
2808
2809/**
Allan Stephensc98da842016-11-11 15:45:03 -05002810 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811 */
2812
Allan Stephensc98da842016-11-11 15:45:03 -05002813/**
2814 * @cond INTERNAL_HIDDEN
2815 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002817struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002819 uint32_t num_blocks;
2820 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821 char *buffer;
2822 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002823 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824
Anas Nashif2f203c22016-12-18 06:57:45 -05002825 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826};
2827
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002828#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2829 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 { \
2831 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002832 .num_blocks = slab_num_blocks, \
2833 .block_size = slab_block_size, \
2834 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835 .free_list = NULL, \
2836 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002837 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 }
2839
Peter Mitsis578f9112016-10-07 13:50:31 -04002840/**
Allan Stephensc98da842016-11-11 15:45:03 -05002841 * INTERNAL_HIDDEN @endcond
2842 */
2843
2844/**
2845 * @defgroup mem_slab_apis Memory Slab APIs
2846 * @ingroup kernel_apis
2847 * @{
2848 */
2849
2850/**
Allan Stephensda827222016-11-09 14:23:58 -06002851 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002852 *
Allan Stephensda827222016-11-09 14:23:58 -06002853 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002855 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2856 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002857 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002858 *
Allan Stephensda827222016-11-09 14:23:58 -06002859 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002862 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * @param name Name of the memory slab.
2865 * @param slab_block_size Size of each memory block (in bytes).
2866 * @param slab_num_blocks Number memory blocks.
2867 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002868 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002869#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2870 char __noinit __aligned(slab_align) \
2871 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2872 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002873 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002874 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2875 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002876
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002877/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002878 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002881 *
Allan Stephensda827222016-11-09 14:23:58 -06002882 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2883 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2884 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2885 * To ensure that each memory block is similarly aligned to this boundary,
2886 * @a slab_block_size must also be a multiple of N.
2887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param slab Address of the memory slab.
2889 * @param buffer Pointer to buffer used for the memory blocks.
2890 * @param block_size Size of each memory block (in bytes).
2891 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002892 *
2893 * @return N/A
2894 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002895extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002896 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002897
2898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @param slab Address of the memory slab.
2904 * @param mem Pointer to block address area.
2905 * @param timeout Maximum time to wait for operation to complete
2906 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2907 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002908 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002909 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002911 * @retval -ENOMEM Returned without waiting.
2912 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002913 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002914extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2915 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002916
2917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * This routine releases a previously allocated memory block back to its
2921 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * @param slab Address of the memory slab.
2924 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002925 *
2926 * @return N/A
2927 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002928extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * This routine gets the number of memory blocks that are currently
2934 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002939 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002940static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002942 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943}
2944
Peter Mitsisc001aa82016-10-13 13:53:37 -04002945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * This routine gets the number of memory blocks that are currently
2949 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002951 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002954 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002955static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002956{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002957 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002958}
2959
Allan Stephensc98da842016-11-11 15:45:03 -05002960/**
2961 * @} end defgroup mem_slab_apis
2962 */
2963
2964/**
2965 * @cond INTERNAL_HIDDEN
2966 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002967
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002968/*
2969 * Memory pool requires a buffer and two arrays of structures for the
2970 * memory block accounting:
2971 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2972 * status of four blocks of memory.
2973 */
2974struct k_mem_pool_quad_block {
2975 char *mem_blocks; /* pointer to the first of four memory blocks */
2976 uint32_t mem_status; /* four bits. If bit is set, memory block is
2977 allocated */
2978};
2979/*
2980 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2981 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2982 * block size is 4 times less than the previous one and thus requires 4 times
2983 * bigger array of k_mem_pool_quad_block structures to keep track of the
2984 * memory blocks.
2985 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002987/*
2988 * The array of k_mem_pool_block_set keeps the information of each array of
2989 * k_mem_pool_quad_block structures
2990 */
2991struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002992 size_t block_size; /* memory block size */
2993 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002994 struct k_mem_pool_quad_block *quad_block;
2995 int count;
2996};
2997
2998/* Memory pool descriptor */
2999struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04003000 size_t max_block_size;
3001 size_t min_block_size;
3002 uint32_t nr_of_maxblocks;
3003 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003004 struct k_mem_pool_block_set *block_set;
3005 char *bufblock;
3006 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05003007 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008};
3009
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003010#ifdef CONFIG_ARM
3011#define _SECTION_TYPE_SIGN "%"
3012#else
3013#define _SECTION_TYPE_SIGN "@"
3014#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003016/*
3017 * Static memory pool initialization
3018 */
Allan Stephensc98da842016-11-11 15:45:03 -05003019
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003020/*
3021 * Use .altmacro to be able to recalculate values and pass them as string
3022 * arguments when calling assembler macros resursively
3023 */
3024__asm__(".altmacro\n\t");
3025
3026/*
3027 * Recursively calls a macro
3028 * The followig global symbols need to be initialized:
3029 * __memory_pool_max_block_size - maximal size of the memory block
3030 * __memory_pool_min_block_size - minimal size of the memory block
3031 * Notes:
3032 * Global symbols are used due the fact that assembler macro allows only
3033 * one argument be passed with the % conversion
3034 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3035 * is used instead of / 4.
3036 * n_max argument needs to go first in the invoked macro, as some
3037 * assemblers concatenate \name and %(\n_max * 4) arguments
3038 * if \name goes first
3039 */
3040__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3041 ".ifge __memory_pool_max_block_size >> 2 -"
3042 " __memory_pool_min_block_size\n\t\t"
3043 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3044 "\\macro_name %(\\n_max * 4) \\name\n\t"
3045 ".endif\n\t"
3046 ".endm\n");
3047
3048/*
3049 * Build quad blocks
3050 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3051 * structures and recursively calls itself for the next array, 4 times
3052 * larger.
3053 * The followig global symbols need to be initialized:
3054 * __memory_pool_max_block_size - maximal size of the memory block
3055 * __memory_pool_min_block_size - minimal size of the memory block
3056 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3057 */
3058__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003059 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003060 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3061 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3062 ".if \\n_max % 4\n\t\t"
3063 ".skip __memory_pool_quad_block_size\n\t"
3064 ".endif\n\t"
3065 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3066 ".endm\n");
3067
3068/*
3069 * Build block sets and initialize them
3070 * Macro initializes the k_mem_pool_block_set structure and
3071 * recursively calls itself for the next one.
3072 * The followig global symbols need to be initialized:
3073 * __memory_pool_max_block_size - maximal size of the memory block
3074 * __memory_pool_min_block_size - minimal size of the memory block
3075 * __memory_pool_block_set_count, the number of the elements in the
3076 * block set array must be set to 0. Macro calculates it's real
3077 * value.
3078 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3079 * structures, _build_quad_blocks must be called prior it.
3080 */
3081__asm__(".macro _build_block_set n_max, name\n\t"
3082 ".int __memory_pool_max_block_size\n\t" /* block_size */
3083 ".if \\n_max % 4\n\t\t"
3084 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3085 ".else\n\t\t"
3086 ".int \\n_max >> 2\n\t"
3087 ".endif\n\t"
3088 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3089 ".int 0\n\t" /* count */
3090 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3091 "__do_recurse _build_block_set \\name \\n_max\n\t"
3092 ".endm\n");
3093
3094/*
3095 * Build a memory pool structure and initialize it
3096 * Macro uses __memory_pool_block_set_count global symbol,
3097 * block set addresses and buffer address, it may be called only after
3098 * _build_block_set
3099 */
3100__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003101 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003102 _SECTION_TYPE_SIGN "progbits\n\t"
3103 ".globl \\name\n\t"
3104 "\\name:\n\t"
3105 ".int \\max_size\n\t" /* max_block_size */
3106 ".int \\min_size\n\t" /* min_block_size */
3107 ".int \\n_max\n\t" /* nr_of_maxblocks */
3108 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3109 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3110 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3111 ".int 0\n\t" /* wait_q->head */
3112 ".int 0\n\t" /* wait_q->next */
3113 ".popsection\n\t"
3114 ".endm\n");
3115
3116#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3117 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3118 _SECTION_TYPE_SIGN "progbits\n\t"); \
3119 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3120 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3121 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3122 STRINGIFY(name) "\n\t"); \
3123 __asm__(".popsection\n\t")
3124
3125#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3126 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3127 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3128 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3129 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003130 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003131 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3132 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3133 STRINGIFY(name) "\n\t"); \
3134 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3135 __asm__(".int __memory_pool_block_set_count\n\t"); \
3136 __asm__(".popsection\n\t"); \
3137 extern uint32_t _mem_pool_block_set_count_##name; \
3138 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3139
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003140#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3141 char __noinit __aligned(align) \
3142 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003143
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003144/*
3145 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3146 * to __memory_pool_quad_block_size absolute symbol.
3147 * This function does not get called, but compiler calculates the value and
3148 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3149 */
3150static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3151{
3152 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003153#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003154 "__memory_pool_quad_block_size = %0\n\t"
3155#else
3156 "__memory_pool_quad_block_size = %c0\n\t"
3157#endif
3158 :
3159 : "n"(sizeof(struct k_mem_pool_quad_block)));
3160}
3161
3162/**
Allan Stephensc98da842016-11-11 15:45:03 -05003163 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003164 */
3165
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003166/**
Allan Stephensc98da842016-11-11 15:45:03 -05003167 * @addtogroup mem_pool_apis
3168 * @{
3169 */
3170
3171/**
3172 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3175 * long. The memory pool allows blocks to be repeatedly partitioned into
3176 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3177 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003178 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003180 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 * If the pool is to be accessed outside the module where it is defined, it
3182 * can be declared via
3183 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003184 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @param name Name of the memory pool.
3187 * @param min_size Size of the smallest blocks in the pool (in bytes).
3188 * @param max_size Size of the largest blocks in the pool (in bytes).
3189 * @param n_max Number of maximum sized blocks in the pool.
3190 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003191 */
3192#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003193 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3194 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003195 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003196 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3197 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3198 extern struct k_mem_pool name
3199
Peter Mitsis937042c2016-10-13 13:18:26 -04003200/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @param pool Address of the memory pool.
3206 * @param block Pointer to block descriptor for the allocated memory.
3207 * @param size Amount of memory to allocate (in bytes).
3208 * @param timeout Maximum time to wait for operation to complete
3209 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3210 * or K_FOREVER to wait as long as necessary.
3211 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003212 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003214 * @retval -ENOMEM Returned without waiting.
3215 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003216 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003217extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003218 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003219
3220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * This routine releases a previously allocated memory block back to its
3224 * memory pool.
3225 *
3226 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003227 *
3228 * @return N/A
3229 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003231
3232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003235 * This routine instructs a memory pool to concatenate unused memory blocks
3236 * into larger blocks wherever possible. Manually defragmenting the memory
3237 * pool may speed up future allocations of memory blocks by eliminating the
3238 * need for the memory pool to perform an automatic partial defragmentation.
3239 *
3240 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003241 *
3242 * @return N/A
3243 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003244extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003245
3246/**
Allan Stephensc98da842016-11-11 15:45:03 -05003247 * @} end addtogroup mem_pool_apis
3248 */
3249
3250/**
3251 * @defgroup heap_apis Heap Memory Pool APIs
3252 * @ingroup kernel_apis
3253 * @{
3254 */
3255
3256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003260 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003265 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003266extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003267
3268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003270 *
3271 * This routine provides traditional free() semantics. The memory being
3272 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003273 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003274 * If @a ptr is NULL, no operation is performed.
3275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003277 *
3278 * @return N/A
3279 */
3280extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281
Allan Stephensc98da842016-11-11 15:45:03 -05003282/**
3283 * @} end defgroup heap_apis
3284 */
3285
Benjamin Walshacc68c12017-01-29 18:57:45 -05003286/* polling API - PRIVATE */
3287
Benjamin Walshb0179862017-02-02 16:39:57 -05003288#ifdef CONFIG_POLL
3289#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3290#else
3291#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3292#endif
3293
Benjamin Walshacc68c12017-01-29 18:57:45 -05003294/* private - implementation data created as needed, per-type */
3295struct _poller {
3296 struct k_thread *thread;
3297};
3298
3299/* private - types bit positions */
3300enum _poll_types_bits {
3301 /* can be used to ignore an event */
3302 _POLL_TYPE_IGNORE,
3303
3304 /* to be signaled by k_poll_signal() */
3305 _POLL_TYPE_SIGNAL,
3306
3307 /* semaphore availability */
3308 _POLL_TYPE_SEM_AVAILABLE,
3309
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003310 /* queue/fifo/lifo data availability */
3311 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003312
3313 _POLL_NUM_TYPES
3314};
3315
3316#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3317
3318/* private - states bit positions */
3319enum _poll_states_bits {
3320 /* default state when creating event */
3321 _POLL_STATE_NOT_READY,
3322
3323 /* there was another poller already on the object */
3324 _POLL_STATE_EADDRINUSE,
3325
3326 /* signaled by k_poll_signal() */
3327 _POLL_STATE_SIGNALED,
3328
3329 /* semaphore is available */
3330 _POLL_STATE_SEM_AVAILABLE,
3331
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003332 /* data is available to read on queue/fifo/lifo */
3333 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003334
3335 _POLL_NUM_STATES
3336};
3337
3338#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3339
3340#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003341 (32 - (0 \
3342 + 8 /* tag */ \
3343 + _POLL_NUM_TYPES \
3344 + _POLL_NUM_STATES \
3345 + 1 /* modes */ \
3346 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003347
3348#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3349#error overflow of 32-bit word in struct k_poll_event
3350#endif
3351
3352/* end of polling API - PRIVATE */
3353
3354
3355/**
3356 * @defgroup poll_apis Async polling APIs
3357 * @ingroup kernel_apis
3358 * @{
3359 */
3360
3361/* Public polling API */
3362
3363/* public - values for k_poll_event.type bitfield */
3364#define K_POLL_TYPE_IGNORE 0
3365#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3366#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003367#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3368#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003369
3370/* public - polling modes */
3371enum k_poll_modes {
3372 /* polling thread does not take ownership of objects when available */
3373 K_POLL_MODE_NOTIFY_ONLY = 0,
3374
3375 K_POLL_NUM_MODES
3376};
3377
3378/* public - values for k_poll_event.state bitfield */
3379#define K_POLL_STATE_NOT_READY 0
3380#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3381#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3382#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003383#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3384#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003385
3386/* public - poll signal object */
3387struct k_poll_signal {
3388 /* PRIVATE - DO NOT TOUCH */
3389 struct k_poll_event *poll_event;
3390
3391 /*
3392 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3393 * user resets it to 0.
3394 */
3395 unsigned int signaled;
3396
3397 /* custom result value passed to k_poll_signal() if needed */
3398 int result;
3399};
3400
3401#define K_POLL_SIGNAL_INITIALIZER() \
3402 { \
3403 .poll_event = NULL, \
3404 .signaled = 0, \
3405 .result = 0, \
3406 }
3407
3408struct k_poll_event {
3409 /* PRIVATE - DO NOT TOUCH */
3410 struct _poller *poller;
3411
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003412 /* optional user-specified tag, opaque, untouched by the API */
3413 uint32_t tag:8;
3414
Benjamin Walshacc68c12017-01-29 18:57:45 -05003415 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
3416 uint32_t type:_POLL_NUM_TYPES;
3417
3418 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
3419 uint32_t state:_POLL_NUM_STATES;
3420
3421 /* mode of operation, from enum k_poll_modes */
3422 uint32_t mode:1;
3423
3424 /* unused bits in 32-bit word */
3425 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
3426
3427 /* per-type data */
3428 union {
3429 void *obj;
3430 struct k_poll_signal *signal;
3431 struct k_sem *sem;
3432 struct k_fifo *fifo;
3433 };
3434};
3435
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003436#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003437 { \
3438 .poller = NULL, \
3439 .type = event_type, \
3440 .state = K_POLL_STATE_NOT_READY, \
3441 .mode = event_mode, \
3442 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003443 { .obj = event_obj }, \
3444 }
3445
3446#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3447 event_tag) \
3448 { \
3449 .type = event_type, \
3450 .tag = event_tag, \
3451 .state = K_POLL_STATE_NOT_READY, \
3452 .mode = event_mode, \
3453 .unused = 0, \
3454 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003455 }
3456
3457/**
3458 * @brief Initialize one struct k_poll_event instance
3459 *
3460 * After this routine is called on a poll event, the event it ready to be
3461 * placed in an event array to be passed to k_poll().
3462 *
3463 * @param event The event to initialize.
3464 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3465 * values. Only values that apply to the same object being polled
3466 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3467 * event.
3468 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3469 * @param obj Kernel object or poll signal.
3470 *
3471 * @return N/A
3472 */
3473
3474extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
3475 int mode, void *obj);
3476
3477/**
3478 * @brief Wait for one or many of multiple poll events to occur
3479 *
3480 * This routine allows a thread to wait concurrently for one or many of
3481 * multiple poll events to have occurred. Such events can be a kernel object
3482 * being available, like a semaphore, or a poll signal event.
3483 *
3484 * When an event notifies that a kernel object is available, the kernel object
3485 * is not "given" to the thread calling k_poll(): it merely signals the fact
3486 * that the object was available when the k_poll() call was in effect. Also,
3487 * all threads trying to acquire an object the regular way, i.e. by pending on
3488 * the object, have precedence over the thread polling on the object. This
3489 * means that the polling thread will never get the poll event on an object
3490 * until the object becomes available and its pend queue is empty. For this
3491 * reason, the k_poll() call is more effective when the objects being polled
3492 * only have one thread, the polling thread, trying to acquire them.
3493 *
3494 * Only one thread can be polling for a particular object at a given time. If
3495 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3496 * and returns as soon as it has finished handling the other events. This means
3497 * that k_poll() can return -EADDRINUSE and have the state value of some events
3498 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3499 * parameter is ignored.
3500 *
3501 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3502 * events that were passed to k_poll() and check the state field for the values
3503 * that were expected and take the associated actions.
3504 *
3505 * Before being reused for another call to k_poll(), the user has to reset the
3506 * state field to K_POLL_STATE_NOT_READY.
3507 *
3508 * @param events An array of pointers to events to be polled for.
3509 * @param num_events The number of events in the array.
3510 * @param timeout Waiting period for an event to be ready (in milliseconds),
3511 * or one of the special values K_NO_WAIT and K_FOREVER.
3512 *
3513 * @retval 0 One or more events are ready.
3514 * @retval -EADDRINUSE One or more objects already had a poller.
3515 * @retval -EAGAIN Waiting period timed out.
3516 */
3517
3518extern int k_poll(struct k_poll_event *events, int num_events,
3519 int32_t timeout);
3520
3521/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003522 * @brief Initialize a poll signal object.
3523 *
3524 * Ready a poll signal object to be signaled via k_poll_signal().
3525 *
3526 * @param signal A poll signal.
3527 *
3528 * @return N/A
3529 */
3530
3531extern void k_poll_signal_init(struct k_poll_signal *signal);
3532
3533/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003534 * @brief Signal a poll signal object.
3535 *
3536 * This routine makes ready a poll signal, which is basically a poll event of
3537 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3538 * made ready to run. A @a result value can be specified.
3539 *
3540 * The poll signal contains a 'signaled' field that, when set by
3541 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3542 * be reset by the user before being passed again to k_poll() or k_poll() will
3543 * consider it being signaled, and will return immediately.
3544 *
3545 * @param signal A poll signal.
3546 * @param result The value to store in the result field of the signal.
3547 *
3548 * @retval 0 The signal was delivered successfully.
3549 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3550 */
3551
3552extern int k_poll_signal(struct k_poll_signal *signal, int result);
3553
3554/* private internal function */
3555extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
3556 uint32_t state);
3557
3558/**
3559 * @} end defgroup poll_apis
3560 */
3561
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003562/**
3563 * @brief Make the CPU idle.
3564 *
3565 * This function makes the CPU idle until an event wakes it up.
3566 *
3567 * In a regular system, the idle thread should be the only thread responsible
3568 * for making the CPU idle and triggering any type of power management.
3569 * However, in some more constrained systems, such as a single-threaded system,
3570 * the only thread would be responsible for this if needed.
3571 *
3572 * @return N/A
3573 */
3574extern void k_cpu_idle(void);
3575
3576/**
3577 * @brief Make the CPU idle in an atomic fashion.
3578 *
3579 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3580 * must be done atomically before making the CPU idle.
3581 *
3582 * @param key Interrupt locking key obtained from irq_lock().
3583 *
3584 * @return N/A
3585 */
3586extern void k_cpu_atomic_idle(unsigned int key);
3587
Andrew Boie350f88d2017-01-18 13:13:45 -08003588extern void _sys_power_save_idle_exit(int32_t ticks);
3589
Anas Nashifa6149502017-01-17 07:47:31 -05003590/* Include legacy APIs */
3591#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003592#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003593#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003594#include <arch/cpu.h>
3595
3596/*
3597 * private APIs that are utilized by one or more public APIs
3598 */
3599
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003600#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003601extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003602#else
3603#define _init_static_threads() do { } while ((0))
3604#endif
3605
3606extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003607extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003608
3609#ifdef __cplusplus
3610}
3611#endif
3612
Andrew Boiee004dec2016-11-07 09:01:19 -08003613#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3614/*
3615 * Define new and delete operators.
3616 * At this moment, the operators do nothing since objects are supposed
3617 * to be statically allocated.
3618 */
3619inline void operator delete(void *ptr)
3620{
3621 (void)ptr;
3622}
3623
3624inline void operator delete[](void *ptr)
3625{
3626 (void)ptr;
3627}
3628
3629inline void *operator new(size_t size)
3630{
3631 (void)size;
3632 return NULL;
3633}
3634
3635inline void *operator new[](size_t size)
3636{
3637 (void)size;
3638 return NULL;
3639}
3640
3641/* Placement versions of operator new and delete */
3642inline void operator delete(void *ptr1, void *ptr2)
3643{
3644 (void)ptr1;
3645 (void)ptr2;
3646}
3647
3648inline void operator delete[](void *ptr1, void *ptr2)
3649{
3650 (void)ptr1;
3651 (void)ptr2;
3652}
3653
3654inline void *operator new(size_t size, void *ptr)
3655{
3656 (void)size;
3657 return ptr;
3658}
3659
3660inline void *operator new[](size_t size, void *ptr)
3661{
3662 (void)size;
3663 return ptr;
3664}
3665
3666#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3667
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003668#endif /* !_ASMLANGUAGE */
3669
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670#endif /* _kernel__h_ */