blob: 7c2795397ef974573c8c138690d6987aa6d043ed [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>
Kumar Galaddece1c2017-04-17 10:18:41 -050019#include <inttypes.h>
Anas Nashif173902f2017-01-17 07:08:56 -050020#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021#include <toolchain.h>
22#include <sections.h>
23#include <atomic.h>
24#include <errno.h>
25#include <misc/__assert.h>
26#include <misc/dlist.h>
27#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050028#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050029#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050030#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
101#define _POLL_EVENT_OBJ_INIT \
102 .poll_event = NULL,
103#define _POLL_EVENT struct k_poll_event *poll_event
104#else
105#define _POLL_EVENT_OBJ_INIT
106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109#define tcs k_thread
110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400127typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400129enum execution_context_types {
130 K_ISR = 0,
131 K_COOP_THREAD,
132 K_PREEMPT_THREAD,
133};
134
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400135/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100136 * @defgroup profiling_apis Profiling APIs
137 * @ingroup kernel_apis
138 * @{
139 */
140
141/**
142 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
143 *
144 * This routine calls @ref stack_analyze on the 4 call stacks declared and
145 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
146 *
147 * CONFIG_MAIN_STACK_SIZE
148 * CONFIG_IDLE_STACK_SIZE
149 * CONFIG_ISR_STACK_SIZE
150 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
151 *
152 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
153 * produce output.
154 *
155 * @return N/A
156 */
157extern void k_call_stacks_analyze(void);
158
159/**
160 * @} end defgroup profiling_apis
161 */
162
163/**
Allan Stephensc98da842016-11-11 15:45:03 -0500164 * @defgroup thread_apis Thread APIs
165 * @ingroup kernel_apis
166 * @{
167 */
168
169/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500170 * @typedef k_thread_entry_t
171 * @brief Thread entry point function type.
172 *
173 * A thread's entry point function is invoked when the thread starts executing.
174 * Up to 3 argument values can be passed to the function.
175 *
176 * The thread terminates execution permanently if the entry point function
177 * returns. The thread is responsible for releasing any shared resources
178 * it may own (such as mutexes and dynamically allocated memory), prior to
179 * returning.
180 *
181 * @param p1 First argument.
182 * @param p2 Second argument.
183 * @param p3 Third argument.
184 *
185 * @return N/A
186 */
187typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
188
Benjamin Walshed240f22017-01-22 13:05:08 -0500189#endif /* !_ASMLANGUAGE */
190
191
192/*
193 * Thread user options. May be needed by assembly code. Common part uses low
194 * bits, arch-specific use high bits.
195 */
196
197/* system thread that must not abort */
198#define K_ESSENTIAL (1 << 0)
199
200#if defined(CONFIG_FP_SHARING)
201/* thread uses floating point registers */
202#define K_FP_REGS (1 << 1)
203#endif
204
205#ifdef CONFIG_X86
206/* x86 Bitmask definitions for threads user options */
207
208#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
209/* thread uses SSEx (and also FP) registers */
210#define K_SSE_REGS (1 << 7)
211#endif
212#endif
213
214/* end - thread options */
215
216#if !defined(_ASMLANGUAGE)
217
Allan Stephens5eceb852016-11-16 10:16:30 -0500218/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500219 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500221 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500223 * The new thread may be scheduled for immediate execution or a delayed start.
224 * If the newly spawned thread does not have a delayed start the kernel
225 * scheduler may preempt the current thread to allow the new thread to
226 * execute.
227 *
228 * Thread options are architecture-specific, and can include K_ESSENTIAL,
229 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
230 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400231 *
232 * @param stack Pointer to the stack space.
233 * @param stack_size Stack size in bytes.
234 * @param entry Thread entry function.
235 * @param p1 1st entry point parameter.
236 * @param p2 2nd entry point parameter.
237 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500238 * @param prio Thread priority.
239 * @param options Thread options.
240 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500242 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400243 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500244extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500245 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500247 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400248
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400249/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500250 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400251 *
Allan Stephensc98da842016-11-11 15:45:03 -0500252 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500253 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500255 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400256 *
257 * @return N/A
258 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400260
261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500262 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400263 *
264 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500265 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400266 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267 * @return N/A
268 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400269extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400270
271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500272 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500274 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400275 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500276 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400277 *
278 * @return N/A
279 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400280extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400281
282/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500283 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500285 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500287 * If @a thread is not currently sleeping, the routine has no effect.
288 *
289 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400290 *
291 * @return N/A
292 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400293extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400294
295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500296 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500298 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400300extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400301
302/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500303 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500305 * This routine prevents @a thread from executing if it has not yet started
306 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500308 * @param thread ID of thread to cancel.
309 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700310 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500311 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400312 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400313extern int k_thread_cancel(k_tid_t thread);
314
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400315/**
Allan Stephensc98da842016-11-11 15:45:03 -0500316 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500318 * This routine permanently stops execution of @a thread. The thread is taken
319 * off all kernel queues it is part of (i.e. the ready queue, the timeout
320 * queue, or a kernel object wait queue). However, any kernel resources the
321 * thread might currently own (such as mutexes or memory blocks) are not
322 * released. It is the responsibility of the caller of this routine to ensure
323 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500325 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400326 *
327 * @return N/A
328 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400329extern void k_thread_abort(k_tid_t thread);
330
Allan Stephensc98da842016-11-11 15:45:03 -0500331/**
332 * @cond INTERNAL_HIDDEN
333 */
334
Benjamin Walshd211a522016-12-06 11:44:01 -0500335/* timeout has timed out and is not on _timeout_q anymore */
336#define _EXPIRED (-2)
337
338/* timeout is not in use */
339#define _INACTIVE (-1)
340
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400341struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400342 union {
343 char *init_stack;
344 struct k_thread *thread;
345 };
346 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500347 void (*init_entry)(void *, void *, void *);
348 void *init_p1;
349 void *init_p2;
350 void *init_p3;
351 int init_prio;
352 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400353 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500354 void (*init_abort)(void);
355 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400356};
357
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400358#define _THREAD_INITIALIZER(stack, stack_size, \
359 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500360 prio, options, delay, abort, groups) \
361 { \
Mazen NEIFER967cb2e2017-02-02 10:42:18 +0100362 {.init_stack = (stack)}, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500363 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400364 .init_entry = (void (*)(void *, void *, void *))entry, \
365 .init_p1 = (void *)p1, \
366 .init_p2 = (void *)p2, \
367 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500368 .init_prio = (prio), \
369 .init_options = (options), \
370 .init_delay = (delay), \
371 .init_abort = (abort), \
372 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400373 }
374
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400375/**
Allan Stephensc98da842016-11-11 15:45:03 -0500376 * INTERNAL_HIDDEN @endcond
377 */
378
379/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500380 * @brief Statically define and initialize a thread.
381 *
382 * The thread may be scheduled for immediate execution or a delayed start.
383 *
384 * Thread options are architecture-specific, and can include K_ESSENTIAL,
385 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
386 * them using "|" (the logical OR operator).
387 *
388 * The ID of the thread can be accessed using:
389 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500390 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500391 *
392 * @param name Name of the thread.
393 * @param stack_size Stack size in bytes.
394 * @param entry Thread entry function.
395 * @param p1 1st entry point parameter.
396 * @param p2 2nd entry point parameter.
397 * @param p3 3rd entry point parameter.
398 * @param prio Thread priority.
399 * @param options Thread options.
400 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400401 *
402 * @internal It has been observed that the x86 compiler by default aligns
403 * these _static_thread_data structures to 32-byte boundaries, thereby
404 * wasting space. To work around this, force a 4-byte alignment.
405 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500406#define K_THREAD_DEFINE(name, stack_size, \
407 entry, p1, p2, p3, \
408 prio, options, delay) \
409 char __noinit __stack _k_thread_obj_##name[stack_size]; \
410 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500411 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500412 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
413 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500414 NULL, 0); \
415 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400416
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500418 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500420 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500422 * @param thread ID of thread whose priority is needed.
423 *
424 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400425 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500426extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427
428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500429 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432 *
433 * Rescheduling can occur immediately depending on the priority @a thread is
434 * set to:
435 *
436 * - If its priority is raised above the priority of the caller of this
437 * function, and the caller is preemptible, @a thread will be scheduled in.
438 *
439 * - If the caller operates on itself, it lowers its priority below that of
440 * other threads in the system, and the caller is preemptible, the thread of
441 * highest priority will be scheduled in.
442 *
443 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
444 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
445 * highest priority.
446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500447 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 * @param prio New priority.
449 *
450 * @warning Changing the priority of a thread currently involved in mutex
451 * priority inheritance may result in undefined behavior.
452 *
453 * @return N/A
454 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400455extern void k_thread_priority_set(k_tid_t thread, int prio);
456
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500460 * This routine prevents the kernel scheduler from making @a thread the
461 * current thread. All other internal operations on @a thread are still
462 * performed; for example, any timeout it is waiting on keeps ticking,
463 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500465 * If @a thread is already suspended, the routine has no effect.
466 *
467 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400468 *
469 * @return N/A
470 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400471extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400472
473/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500476 * This routine allows the kernel scheduler to make @a thread the current
477 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * If @a thread is not currently suspended, the routine has no effect.
480 *
481 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400482 *
483 * @return N/A
484 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400485extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400486
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500488 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500490 * This routine specifies how the scheduler will perform time slicing of
491 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500493 * To enable time slicing, @a slice must be non-zero. The scheduler
494 * ensures that no thread runs for more than the specified time limit
495 * before other threads of that priority are given a chance to execute.
496 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700497 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500499 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400500 * execute. Once the scheduler selects a thread for execution, there is no
501 * minimum guaranteed time the thread will execute before threads of greater or
502 * equal priority are scheduled.
503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500504 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 * for execution, this routine has no effect; the thread is immediately
506 * rescheduled after the slice period expires.
507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500508 * To disable timeslicing, set both @a slice and @a prio to zero.
509 *
510 * @param slice Maximum time slice length (in milliseconds).
511 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400512 *
513 * @return N/A
514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400516
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517/**
Allan Stephensc98da842016-11-11 15:45:03 -0500518 * @} end defgroup thread_apis
519 */
520
521/**
522 * @addtogroup isr_apis
523 * @{
524 */
525
526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500527 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
Allan Stephensc98da842016-11-11 15:45:03 -0500529 * This routine allows the caller to customize its actions, depending on
530 * whether it is a thread or an ISR.
531 *
532 * @note Can be called by ISRs.
533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500534 * @return 0 if invoked by a thread.
535 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500537extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400538
Benjamin Walsh445830d2016-11-10 15:54:27 -0500539/**
540 * @brief Determine if code is running in a preemptible thread.
541 *
Allan Stephensc98da842016-11-11 15:45:03 -0500542 * This routine allows the caller to customize its actions, depending on
543 * whether it can be preempted by another thread. The routine returns a 'true'
544 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500545 *
Allan Stephensc98da842016-11-11 15:45:03 -0500546 * - The code is running in a thread, not at ISR.
547 * - The thread's priority is in the preemptible range.
548 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500549 *
Allan Stephensc98da842016-11-11 15:45:03 -0500550 * @note Can be called by ISRs.
551 *
552 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500553 * @return Non-zero if invoked by a preemptible thread.
554 */
555extern int k_is_preempt_thread(void);
556
Allan Stephensc98da842016-11-11 15:45:03 -0500557/**
558 * @} end addtogroup isr_apis
559 */
560
561/**
562 * @addtogroup thread_apis
563 * @{
564 */
565
566/**
567 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500568 *
Allan Stephensc98da842016-11-11 15:45:03 -0500569 * This routine prevents the current thread from being preempted by another
570 * thread by instructing the scheduler to treat it as a cooperative thread.
571 * If the thread subsequently performs an operation that makes it unready,
572 * it will be context switched out in the normal manner. When the thread
573 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500574 *
Allan Stephensc98da842016-11-11 15:45:03 -0500575 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500576 *
Allan Stephensc98da842016-11-11 15:45:03 -0500577 * @note k_sched_lock() and k_sched_unlock() should normally be used
578 * when the operation being performed can be safely interrupted by ISRs.
579 * However, if the amount of processing involved is very small, better
580 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500581 *
582 * @return N/A
583 */
584extern void k_sched_lock(void);
585
Allan Stephensc98da842016-11-11 15:45:03 -0500586/**
587 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500588 *
Allan Stephensc98da842016-11-11 15:45:03 -0500589 * This routine reverses the effect of a previous call to k_sched_lock().
590 * A thread must call the routine once for each time it called k_sched_lock()
591 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500592 *
593 * @return N/A
594 */
595extern void k_sched_unlock(void);
596
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500598 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * Custom data is not used by the kernel itself, and is freely available
603 * for a thread to use as it sees fit. It can be used as a framework
604 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 *
608 * @return N/A
609 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400610extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400611
612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500615 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500617 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400618 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400619extern void *k_thread_custom_data_get(void);
620
621/**
Allan Stephensc98da842016-11-11 15:45:03 -0500622 * @} end addtogroup thread_apis
623 */
624
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400625#include <sys_clock.h>
626
Allan Stephensc2f15a42016-11-17 12:24:22 -0500627/**
628 * @addtogroup clock_apis
629 * @{
630 */
631
632/**
633 * @brief Generate null timeout delay.
634 *
635 * This macro generates a timeout delay that that instructs a kernel API
636 * not to wait if the requested operation cannot be performed immediately.
637 *
638 * @return Timeout delay value.
639 */
640#define K_NO_WAIT 0
641
642/**
643 * @brief Generate timeout delay from milliseconds.
644 *
645 * This macro generates a timeout delay that that instructs a kernel API
646 * to wait up to @a ms milliseconds to perform the requested operation.
647 *
648 * @param ms Duration in milliseconds.
649 *
650 * @return Timeout delay value.
651 */
Johan Hedberg14471692016-11-13 10:52:15 +0200652#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500653
654/**
655 * @brief Generate timeout delay from seconds.
656 *
657 * This macro generates a timeout delay that that instructs a kernel API
658 * to wait up to @a s seconds to perform the requested operation.
659 *
660 * @param s Duration in seconds.
661 *
662 * @return Timeout delay value.
663 */
Johan Hedberg14471692016-11-13 10:52:15 +0200664#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500665
666/**
667 * @brief Generate timeout delay from minutes.
668 *
669 * This macro generates a timeout delay that that instructs a kernel API
670 * to wait up to @a m minutes to perform the requested operation.
671 *
672 * @param m Duration in minutes.
673 *
674 * @return Timeout delay value.
675 */
Johan Hedberg14471692016-11-13 10:52:15 +0200676#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500677
678/**
679 * @brief Generate timeout delay from hours.
680 *
681 * This macro generates a timeout delay that that instructs a kernel API
682 * to wait up to @a h hours to perform the requested operation.
683 *
684 * @param h Duration in hours.
685 *
686 * @return Timeout delay value.
687 */
Johan Hedberg14471692016-11-13 10:52:15 +0200688#define K_HOURS(h) K_MINUTES((h) * 60)
689
Allan Stephensc98da842016-11-11 15:45:03 -0500690/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500691 * @brief Generate infinite timeout delay.
692 *
693 * This macro generates a timeout delay that that instructs a kernel API
694 * to wait as long as necessary to perform the requested operation.
695 *
696 * @return Timeout delay value.
697 */
698#define K_FOREVER (-1)
699
700/**
701 * @} end addtogroup clock_apis
702 */
703
704/**
Allan Stephensc98da842016-11-11 15:45:03 -0500705 * @cond INTERNAL_HIDDEN
706 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400707
Benjamin Walsh62092182016-12-20 14:39:08 -0500708/* kernel clocks */
709
710#if (sys_clock_ticks_per_sec == 1000) || \
711 (sys_clock_ticks_per_sec == 500) || \
712 (sys_clock_ticks_per_sec == 250) || \
713 (sys_clock_ticks_per_sec == 125) || \
714 (sys_clock_ticks_per_sec == 100) || \
715 (sys_clock_ticks_per_sec == 50) || \
716 (sys_clock_ticks_per_sec == 25) || \
717 (sys_clock_ticks_per_sec == 20) || \
718 (sys_clock_ticks_per_sec == 10) || \
719 (sys_clock_ticks_per_sec == 1)
720
721 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
722#else
723 /* yields horrible 64-bit math on many architectures: try to avoid */
724 #define _NON_OPTIMIZED_TICKS_PER_SEC
725#endif
726
727#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
728extern int32_t _ms_to_ticks(int32_t ms);
729#else
730static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
731{
732 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
733}
734#endif
735
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500736/* added tick needed to account for tick in progress */
737#define _TICK_ALIGN 1
738
Benjamin Walsh62092182016-12-20 14:39:08 -0500739static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400740{
Benjamin Walsh62092182016-12-20 14:39:08 -0500741#ifdef CONFIG_SYS_CLOCK_EXISTS
742
743#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400744 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400745#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500746 return (uint64_t)ticks * _ms_per_tick;
747#endif
748
749#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400750 __ASSERT(ticks == 0, "");
751 return 0;
752#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400753}
754
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400755/* timeouts */
756
757struct _timeout;
758typedef void (*_timeout_func_t)(struct _timeout *t);
759
760struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500761 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400762 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763 sys_dlist_t *wait_q;
764 int32_t delta_ticks_from_prev;
765 _timeout_func_t func;
766};
767
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200768extern int32_t _timeout_remaining_get(struct _timeout *timeout);
769
Allan Stephensc98da842016-11-11 15:45:03 -0500770/**
771 * INTERNAL_HIDDEN @endcond
772 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500773
Allan Stephensc98da842016-11-11 15:45:03 -0500774/**
775 * @cond INTERNAL_HIDDEN
776 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400777
778struct k_timer {
779 /*
780 * _timeout structure must be first here if we want to use
781 * dynamic timer allocation. timeout.node is used in the double-linked
782 * list of free timers
783 */
784 struct _timeout timeout;
785
Allan Stephens45bfa372016-10-12 12:39:42 -0500786 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400787 _wait_q_t wait_q;
788
789 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500790 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400791
792 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500793 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400794
795 /* timer period */
796 int32_t period;
797
Allan Stephens45bfa372016-10-12 12:39:42 -0500798 /* timer status */
799 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400800
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500801 /* user-specific data, also used to support legacy features */
802 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400803
Anas Nashif2f203c22016-12-18 06:57:45 -0500804 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400805};
806
Allan Stephens1342adb2016-11-03 13:54:53 -0500807#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500809 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500810 .timeout.wait_q = NULL, \
811 .timeout.thread = NULL, \
812 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400813 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500814 .expiry_fn = expiry, \
815 .stop_fn = stop, \
816 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500817 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500818 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400819 }
820
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821/**
Allan Stephensc98da842016-11-11 15:45:03 -0500822 * INTERNAL_HIDDEN @endcond
823 */
824
825/**
826 * @defgroup timer_apis Timer APIs
827 * @ingroup kernel_apis
828 * @{
829 */
830
831/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500832 * @typedef k_timer_expiry_t
833 * @brief Timer expiry function type.
834 *
835 * A timer's expiry function is executed by the system clock interrupt handler
836 * each time the timer expires. The expiry function is optional, and is only
837 * invoked if the timer has been initialized with one.
838 *
839 * @param timer Address of timer.
840 *
841 * @return N/A
842 */
843typedef void (*k_timer_expiry_t)(struct k_timer *timer);
844
845/**
846 * @typedef k_timer_stop_t
847 * @brief Timer stop function type.
848 *
849 * A timer's stop function is executed if the timer is stopped prematurely.
850 * The function runs in the context of the thread that stops the timer.
851 * The stop function is optional, and is only invoked if the timer has been
852 * initialized with one.
853 *
854 * @param timer Address of timer.
855 *
856 * @return N/A
857 */
858typedef void (*k_timer_stop_t)(struct k_timer *timer);
859
860/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500865 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 *
867 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * @param expiry_fn Function to invoke each time the timer expires.
869 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500871#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500872 struct k_timer name \
873 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500874 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400875
Allan Stephens45bfa372016-10-12 12:39:42 -0500876/**
877 * @brief Initialize a timer.
878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500880 *
881 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * @param expiry_fn Function to invoke each time the timer expires.
883 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500884 *
885 * @return N/A
886 */
887extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500888 k_timer_expiry_t expiry_fn,
889 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700890
Allan Stephens45bfa372016-10-12 12:39:42 -0500891/**
892 * @brief Start a timer.
893 *
894 * This routine starts a timer, and resets its status to zero. The timer
895 * begins counting down using the specified duration and period values.
896 *
897 * Attempting to start a timer that is already running is permitted.
898 * The timer's status is reset to zero and the timer begins counting down
899 * using the new duration and period values.
900 *
901 * @param timer Address of timer.
902 * @param duration Initial timer duration (in milliseconds).
903 * @param period Timer period (in milliseconds).
904 *
905 * @return N/A
906 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500908 int32_t duration, int32_t period);
909
910/**
911 * @brief Stop a timer.
912 *
913 * This routine stops a running timer prematurely. The timer's stop function,
914 * if one exists, is invoked by the caller.
915 *
916 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500917 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500918 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -0500919 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
920 * if @a k_timer_stop is to be called from ISRs.
921 *
Allan Stephens45bfa372016-10-12 12:39:42 -0500922 * @param timer Address of timer.
923 *
924 * @return N/A
925 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400926extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500927
928/**
929 * @brief Read timer status.
930 *
931 * This routine reads the timer's status, which indicates the number of times
932 * it has expired since its status was last read.
933 *
934 * Calling this routine resets the timer's status to zero.
935 *
936 * @param timer Address of timer.
937 *
938 * @return Timer status.
939 */
940extern uint32_t k_timer_status_get(struct k_timer *timer);
941
942/**
943 * @brief Synchronize thread to timer expiration.
944 *
945 * This routine blocks the calling thread until the timer's status is non-zero
946 * (indicating that it has expired at least once since it was last examined)
947 * or the timer is stopped. If the timer status is already non-zero,
948 * or the timer is already stopped, the caller continues without waiting.
949 *
950 * Calling this routine resets the timer's status to zero.
951 *
952 * This routine must not be used by interrupt handlers, since they are not
953 * allowed to block.
954 *
955 * @param timer Address of timer.
956 *
957 * @return Timer status.
958 */
959extern uint32_t k_timer_status_sync(struct k_timer *timer);
960
961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500963 *
964 * This routine computes the (approximate) time remaining before a running
965 * timer next expires. If the timer is not running, it returns zero.
966 *
967 * @param timer Address of timer.
968 *
969 * @return Remaining time (in milliseconds).
970 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200971static inline int32_t k_timer_remaining_get(struct k_timer *timer)
972{
973 return _timeout_remaining_get(&timer->timeout);
974}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400975
Allan Stephensc98da842016-11-11 15:45:03 -0500976/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500977 * @brief Associate user-specific data with a timer.
978 *
979 * This routine records the @a user_data with the @a timer, to be retrieved
980 * later.
981 *
982 * It can be used e.g. in a timer handler shared across multiple subsystems to
983 * retrieve data specific to the subsystem this timer is associated with.
984 *
985 * @param timer Address of timer.
986 * @param user_data User data to associate with the timer.
987 *
988 * @return N/A
989 */
990static inline void k_timer_user_data_set(struct k_timer *timer,
991 void *user_data)
992{
993 timer->user_data = user_data;
994}
995
996/**
997 * @brief Retrieve the user-specific data from a timer.
998 *
999 * @param timer Address of timer.
1000 *
1001 * @return The user data.
1002 */
1003static inline void *k_timer_user_data_get(struct k_timer *timer)
1004{
1005 return timer->user_data;
1006}
1007
1008/**
Allan Stephensc98da842016-11-11 15:45:03 -05001009 * @} end defgroup timer_apis
1010 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001011
Allan Stephensc98da842016-11-11 15:45:03 -05001012/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001013 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001014 * @{
1015 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001016
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001017/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * This routine returns the elapsed time since the system booted,
1021 * in milliseconds.
1022 *
1023 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001024 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001025extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001026
1027/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * This routine returns the lower 32-bits of the elapsed time since the system
1031 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * This routine can be more efficient than k_uptime_get(), as it reduces the
1034 * need for interrupt locking and 64-bit math. However, the 32-bit result
1035 * cannot hold a system uptime time larger than approximately 50 days, so the
1036 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001039 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001040extern uint32_t k_uptime_get_32(void);
1041
1042/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * This routine computes the elapsed time between the current system uptime
1046 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @param reftime Pointer to a reference time, which is updated to the current
1049 * uptime upon return.
1050 *
1051 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine computes the elapsed time between the current system uptime
1059 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1062 * need for interrupt locking and 64-bit math. However, the 32-bit result
1063 * cannot hold an elapsed time larger than approximately 50 days, so the
1064 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001066 * @param reftime Pointer to a reference time, which is updated to the current
1067 * uptime upon return.
1068 *
1069 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001070 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001071extern uint32_t k_uptime_delta_32(int64_t *reftime);
1072
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * This routine returns the current time, as measured by the system's hardware
1077 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001081#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001083/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001084 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085 */
1086
Allan Stephensc98da842016-11-11 15:45:03 -05001087/**
1088 * @cond INTERNAL_HIDDEN
1089 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001090
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001091struct k_queue {
1092 _wait_q_t wait_q;
1093 sys_slist_t data_q;
1094 _POLL_EVENT;
1095
1096 _OBJECT_TRACING_NEXT_PTR(k_queue);
1097};
1098
1099#define K_QUEUE_INITIALIZER(obj) \
1100 { \
1101 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1102 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1103 _POLL_EVENT_OBJ_INIT \
1104 _OBJECT_TRACING_INIT \
1105 }
1106
1107/**
1108 * INTERNAL_HIDDEN @endcond
1109 */
1110
1111/**
1112 * @defgroup queue_apis Queue APIs
1113 * @ingroup kernel_apis
1114 * @{
1115 */
1116
1117/**
1118 * @brief Initialize a queue.
1119 *
1120 * This routine initializes a queue object, prior to its first use.
1121 *
1122 * @param queue Address of the queue.
1123 *
1124 * @return N/A
1125 */
1126extern void k_queue_init(struct k_queue *queue);
1127
1128/**
1129 * @brief Append an element to the end of a queue.
1130 *
1131 * This routine appends a data item to @a queue. A queue data item must be
1132 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1133 * reserved for the kernel's use.
1134 *
1135 * @note Can be called by ISRs.
1136 *
1137 * @param queue Address of the queue.
1138 * @param data Address of the data item.
1139 *
1140 * @return N/A
1141 */
1142extern void k_queue_append(struct k_queue *queue, void *data);
1143
1144/**
1145 * @brief Prepend an element to a queue.
1146 *
1147 * This routine prepends a data item to @a queue. A queue data item must be
1148 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1149 * reserved for the kernel's use.
1150 *
1151 * @note Can be called by ISRs.
1152 *
1153 * @param queue Address of the queue.
1154 * @param data Address of the data item.
1155 *
1156 * @return N/A
1157 */
1158extern void k_queue_prepend(struct k_queue *queue, void *data);
1159
1160/**
1161 * @brief Inserts an element to a queue.
1162 *
1163 * This routine inserts a data item to @a queue after previous item. A queue
1164 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1165 * item are reserved for the kernel's use.
1166 *
1167 * @note Can be called by ISRs.
1168 *
1169 * @param queue Address of the queue.
1170 * @param prev Address of the previous data item.
1171 * @param data Address of the data item.
1172 *
1173 * @return N/A
1174 */
1175extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1176
1177/**
1178 * @brief Atomically append a list of elements to a queue.
1179 *
1180 * This routine adds a list of data items to @a queue in one operation.
1181 * The data items must be in a singly-linked list, with the first 32 bits
1182 * in each data item pointing to the next data item; the list must be
1183 * NULL-terminated.
1184 *
1185 * @note Can be called by ISRs.
1186 *
1187 * @param queue Address of the queue.
1188 * @param head Pointer to first node in singly-linked list.
1189 * @param tail Pointer to last node in singly-linked list.
1190 *
1191 * @return N/A
1192 */
1193extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1194
1195/**
1196 * @brief Atomically add a list of elements to a queue.
1197 *
1198 * This routine adds a list of data items to @a queue in one operation.
1199 * The data items must be in a singly-linked list implemented using a
1200 * sys_slist_t object. Upon completion, the original list is empty.
1201 *
1202 * @note Can be called by ISRs.
1203 *
1204 * @param queue Address of the queue.
1205 * @param list Pointer to sys_slist_t object.
1206 *
1207 * @return N/A
1208 */
1209extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1210
1211/**
1212 * @brief Get an element from a queue.
1213 *
1214 * This routine removes first data item from @a queue. The first 32 bits of the
1215 * data item are reserved for the kernel's use.
1216 *
1217 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1218 *
1219 * @param queue Address of the queue.
1220 * @param timeout Waiting period to obtain a data item (in milliseconds),
1221 * or one of the special values K_NO_WAIT and K_FOREVER.
1222 *
1223 * @return Address of the data item if successful; NULL if returned
1224 * without waiting, or waiting period timed out.
1225 */
1226extern void *k_queue_get(struct k_queue *queue, int32_t timeout);
1227
1228/**
1229 * @brief Query a queue to see if it has data available.
1230 *
1231 * Note that the data might be already gone by the time this function returns
1232 * if other threads are also trying to read from the queue.
1233 *
1234 * @note Can be called by ISRs.
1235 *
1236 * @param queue Address of the queue.
1237 *
1238 * @return Non-zero if the queue is empty.
1239 * @return 0 if data is available.
1240 */
1241static inline int k_queue_is_empty(struct k_queue *queue)
1242{
1243 return (int)sys_slist_is_empty(&queue->data_q);
1244}
1245
1246/**
1247 * @brief Statically define and initialize a queue.
1248 *
1249 * The queue can be accessed outside the module where it is defined using:
1250 *
1251 * @code extern struct k_queue <name>; @endcode
1252 *
1253 * @param name Name of the queue.
1254 */
1255#define K_QUEUE_DEFINE(name) \
1256 struct k_queue name \
1257 __in_section(_k_queue, static, name) = \
1258 K_QUEUE_INITIALIZER(name)
1259
1260/**
1261 * @} end defgroup queue_apis
1262 */
1263
1264/**
1265 * @cond INTERNAL_HIDDEN
1266 */
1267
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001268struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001269 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001270};
1271
Allan Stephensc98da842016-11-11 15:45:03 -05001272#define K_FIFO_INITIALIZER(obj) \
1273 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001274 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001275 }
1276
1277/**
1278 * INTERNAL_HIDDEN @endcond
1279 */
1280
1281/**
1282 * @defgroup fifo_apis Fifo APIs
1283 * @ingroup kernel_apis
1284 * @{
1285 */
1286
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001293 *
1294 * @return N/A
1295 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001296#define k_fifo_init(fifo) \
1297 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001298
1299/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001302 * This routine adds a data item to @a fifo. A fifo data item must be
1303 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1304 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001306 * @note Can be called by ISRs.
1307 *
1308 * @param fifo Address of the fifo.
1309 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 *
1311 * @return N/A
1312 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001313#define k_fifo_put(fifo, data) \
1314 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001315
1316/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001319 * This routine adds a list of data items to @a fifo in one operation.
1320 * The data items must be in a singly-linked list, with the first 32 bits
1321 * each data item pointing to the next data item; the list must be
1322 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001324 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326 * @param fifo Address of the fifo.
1327 * @param head Pointer to first node in singly-linked list.
1328 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001329 *
1330 * @return N/A
1331 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001332#define k_fifo_put_list(fifo, head, tail) \
1333 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001334
1335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * This routine adds a list of data items to @a fifo in one operation.
1339 * The data items must be in a singly-linked list implemented using a
1340 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001341 * and must be re-initialized via sys_slist_init().
1342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * @note Can be called by ISRs.
1344 *
1345 * @param fifo Address of the fifo.
1346 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001347 *
1348 * @return N/A
1349 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001350#define k_fifo_put_slist(fifo, list) \
1351 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001352
1353/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * This routine removes a data item from @a fifo in a "first in, first out"
1357 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1360 *
1361 * @param fifo Address of the fifo.
1362 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001363 * or one of the special values K_NO_WAIT and K_FOREVER.
1364 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001365 * @return Address of the data item if successful; NULL if returned
1366 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001367 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001368#define k_fifo_get(fifo, timeout) \
1369 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001371/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001372 * @brief Query a fifo to see if it has data available.
1373 *
1374 * Note that the data might be already gone by the time this function returns
1375 * if other threads is also trying to read from the fifo.
1376 *
1377 * @note Can be called by ISRs.
1378 *
1379 * @param fifo Address of the fifo.
1380 *
1381 * @return Non-zero if the fifo is empty.
1382 * @return 0 if data is available.
1383 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001384#define k_fifo_is_empty(fifo) \
1385 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001386
1387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001390 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001392 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001394 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001397 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001398 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001399 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
Allan Stephensc98da842016-11-11 15:45:03 -05001401/**
1402 * @} end defgroup fifo_apis
1403 */
1404
1405/**
1406 * @cond INTERNAL_HIDDEN
1407 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408
1409struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001410 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411};
1412
Allan Stephensc98da842016-11-11 15:45:03 -05001413#define K_LIFO_INITIALIZER(obj) \
1414 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001415 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001416 }
1417
1418/**
1419 * INTERNAL_HIDDEN @endcond
1420 */
1421
1422/**
1423 * @defgroup lifo_apis Lifo APIs
1424 * @ingroup kernel_apis
1425 * @{
1426 */
1427
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001429 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001433 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001434 *
1435 * @return N/A
1436 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001437#define k_lifo_init(lifo) \
1438 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001439
1440/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001441 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001443 * This routine adds a data item to @a lifo. A lifo data item must be
1444 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1445 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001447 * @note Can be called by ISRs.
1448 *
1449 * @param lifo Address of the lifo.
1450 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
1452 * @return N/A
1453 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001454#define k_lifo_put(lifo, data) \
1455 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456
1457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * This routine removes a data item from @a lifo in a "last in, first out"
1461 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001463 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1464 *
1465 * @param lifo Address of the lifo.
1466 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 * or one of the special values K_NO_WAIT and K_FOREVER.
1468 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001469 * @return Address of the data item if successful; NULL if returned
1470 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001471 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001472#define k_lifo_get(lifo, timeout) \
1473 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001474
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001480 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001482 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001484#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001485 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001486 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001487 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001488
Allan Stephensc98da842016-11-11 15:45:03 -05001489/**
1490 * @} end defgroup lifo_apis
1491 */
1492
1493/**
1494 * @cond INTERNAL_HIDDEN
1495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001496
1497struct k_stack {
1498 _wait_q_t wait_q;
1499 uint32_t *base, *next, *top;
1500
Anas Nashif2f203c22016-12-18 06:57:45 -05001501 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502};
1503
Allan Stephensc98da842016-11-11 15:45:03 -05001504#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1505 { \
1506 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1507 .base = stack_buffer, \
1508 .next = stack_buffer, \
1509 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001510 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001511 }
1512
1513/**
1514 * INTERNAL_HIDDEN @endcond
1515 */
1516
1517/**
1518 * @defgroup stack_apis Stack APIs
1519 * @ingroup kernel_apis
1520 * @{
1521 */
1522
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001523/**
1524 * @brief Initialize a stack.
1525 *
1526 * This routine initializes a stack object, prior to its first use.
1527 *
1528 * @param stack Address of the stack.
1529 * @param buffer Address of array used to hold stacked values.
1530 * @param num_entries Maximum number of values that can be stacked.
1531 *
1532 * @return N/A
1533 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001534extern void k_stack_init(struct k_stack *stack,
1535 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001536
1537/**
1538 * @brief Push an element onto a stack.
1539 *
1540 * This routine adds a 32-bit value @a data to @a stack.
1541 *
1542 * @note Can be called by ISRs.
1543 *
1544 * @param stack Address of the stack.
1545 * @param data Value to push onto the stack.
1546 *
1547 * @return N/A
1548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001549extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001550
1551/**
1552 * @brief Pop an element from a stack.
1553 *
1554 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1555 * manner and stores the value in @a data.
1556 *
1557 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1558 *
1559 * @param stack Address of the stack.
1560 * @param data Address of area to hold the value popped from the stack.
1561 * @param timeout Waiting period to obtain a value (in milliseconds),
1562 * or one of the special values K_NO_WAIT and K_FOREVER.
1563 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001564 * @retval 0 Element popped from stack.
1565 * @retval -EBUSY Returned without waiting.
1566 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1569
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001571 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001574 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001575 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001577 * @param name Name of the stack.
1578 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001579 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001580#define K_STACK_DEFINE(name, stack_num_entries) \
1581 uint32_t __noinit \
1582 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001583 struct k_stack name \
1584 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001585 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1586 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001588/**
Allan Stephensc98da842016-11-11 15:45:03 -05001589 * @} end defgroup stack_apis
1590 */
1591
Allan Stephens6bba9b02016-11-16 14:56:54 -05001592struct k_work;
1593
Allan Stephensc98da842016-11-11 15:45:03 -05001594/**
1595 * @defgroup workqueue_apis Workqueue Thread APIs
1596 * @ingroup kernel_apis
1597 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001598 */
1599
Allan Stephens6bba9b02016-11-16 14:56:54 -05001600/**
1601 * @typedef k_work_handler_t
1602 * @brief Work item handler function type.
1603 *
1604 * A work item's handler function is executed by a workqueue's thread
1605 * when the work item is processed by the workqueue.
1606 *
1607 * @param work Address of the work item.
1608 *
1609 * @return N/A
1610 */
1611typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001612
1613/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001614 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001615 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001616
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617struct k_work_q {
1618 struct k_fifo fifo;
1619};
1620
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001621enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001622 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001623};
1624
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625struct k_work {
1626 void *_reserved; /* Used by k_fifo implementation. */
1627 k_work_handler_t handler;
1628 atomic_t flags[1];
1629};
1630
Allan Stephens6bba9b02016-11-16 14:56:54 -05001631struct k_delayed_work {
1632 struct k_work work;
1633 struct _timeout timeout;
1634 struct k_work_q *work_q;
1635};
1636
1637extern struct k_work_q k_sys_work_q;
1638
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001639/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001640 * INTERNAL_HIDDEN @endcond
1641 */
1642
1643/**
1644 * @brief Initialize a statically-defined work item.
1645 *
1646 * This macro can be used to initialize a statically-defined workqueue work
1647 * item, prior to its first use. For example,
1648 *
1649 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1650 *
1651 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001652 */
1653#define K_WORK_INITIALIZER(work_handler) \
1654 { \
1655 ._reserved = NULL, \
1656 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001657 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658 }
1659
1660/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001661 * @brief Initialize a work item.
1662 *
1663 * This routine initializes a workqueue work item, prior to its first use.
1664 *
1665 * @param work Address of work item.
1666 * @param handler Function to invoke each time work item is processed.
1667 *
1668 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001669 */
1670static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1671{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001672 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001673 work->handler = handler;
1674}
1675
1676/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001677 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001678 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001679 * This routine submits work item @a work to be processed by workqueue
1680 * @a work_q. If the work item is already pending in the workqueue's queue
1681 * as a result of an earlier submission, this routine has no effect on the
1682 * work item. If the work item has already been processed, or is currently
1683 * being processed, its work is considered complete and the work item can be
1684 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001685 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001686 * @warning
1687 * A submitted work item must not be modified until it has been processed
1688 * by the workqueue.
1689 *
1690 * @note Can be called by ISRs.
1691 *
1692 * @param work_q Address of workqueue.
1693 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001694 *
1695 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001696 */
1697static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1698 struct k_work *work)
1699{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001700 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001701 k_fifo_put(&work_q->fifo, work);
1702 }
1703}
1704
1705/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001706 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001708 * This routine indicates if work item @a work is pending in a workqueue's
1709 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001710 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001711 * @note Can be called by ISRs.
1712 *
1713 * @param work Address of work item.
1714 *
1715 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001716 */
1717static inline int k_work_pending(struct k_work *work)
1718{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001719 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001720}
1721
1722/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001723 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001725 * This routine starts workqueue @a work_q. The workqueue spawns its work
1726 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001728 * @param work_q Address of workqueue.
1729 * @param stack Pointer to work queue thread's stack space.
1730 * @param stack_size Size of the work queue thread's stack (in bytes).
1731 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 *
1733 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734 */
Allan Stephens904cf972016-10-07 13:59:23 -05001735extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001736 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001739 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001741 * This routine initializes a workqueue delayed work item, prior to
1742 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001744 * @param work Address of delayed work item.
1745 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746 *
1747 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001749extern void k_delayed_work_init(struct k_delayed_work *work,
1750 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001751
1752/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001753 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001755 * This routine schedules work item @a work to be processed by workqueue
1756 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001757 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001758 * Only when the countdown completes is the work item actually submitted to
1759 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001761 * Submitting a previously submitted delayed work item that is still
1762 * counting down cancels the existing submission and restarts the countdown
1763 * using the new delay. If the work item is currently pending on the
1764 * workqueue's queue because the countdown has completed it is too late to
1765 * resubmit the item, and resubmission fails without impacting the work item.
1766 * If the work item has already been processed, or is currently being processed,
1767 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001769 * @warning
1770 * A delayed work item must not be modified until it has been processed
1771 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001772 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001773 * @note Can be called by ISRs.
1774 *
1775 * @param work_q Address of workqueue.
1776 * @param work Address of delayed work item.
1777 * @param delay Delay before submitting the work item (in milliseconds).
1778 *
1779 * @retval 0 Work item countdown started.
1780 * @retval -EINPROGRESS Work item is already pending.
1781 * @retval -EINVAL Work item is being processed or has completed its work.
1782 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001783 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001784extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1785 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001786 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001787
1788/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001789 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001790 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001791 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07001792 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05001793 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001794 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001795 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001796 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001797 * @param work Address of delayed work item.
1798 *
David B. Kinder8b986d72017-04-18 15:56:26 -07001799 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001800 * @retval -EINPROGRESS Work item is already pending.
1801 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001803extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806 * @brief Submit a work item to the system workqueue.
1807 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001808 * This routine submits work item @a work to be processed by the system
1809 * workqueue. If the work item is already pending in the workqueue's queue
1810 * as a result of an earlier submission, this routine has no effect on the
1811 * work item. If the work item has already been processed, or is currently
1812 * being processed, its work is considered complete and the work item can be
1813 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001815 * @warning
1816 * Work items submitted to the system workqueue should avoid using handlers
1817 * that block or yield since this may prevent the system workqueue from
1818 * processing other work items in a timely manner.
1819 *
1820 * @note Can be called by ISRs.
1821 *
1822 * @param work Address of work item.
1823 *
1824 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825 */
1826static inline void k_work_submit(struct k_work *work)
1827{
1828 k_work_submit_to_queue(&k_sys_work_q, work);
1829}
1830
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001831/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001832 * @brief Submit a delayed work item to the system workqueue.
1833 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001834 * This routine schedules work item @a work to be processed by the system
1835 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001836 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001837 * Only when the countdown completes is the work item actually submitted to
1838 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001839 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001840 * Submitting a previously submitted delayed work item that is still
1841 * counting down cancels the existing submission and restarts the countdown
1842 * using the new delay. If the work item is currently pending on the
1843 * workqueue's queue because the countdown has completed it is too late to
1844 * resubmit the item, and resubmission fails without impacting the work item.
1845 * If the work item has already been processed, or is currently being processed,
1846 * its work is considered complete and the work item can be resubmitted.
1847 *
1848 * @warning
1849 * Work items submitted to the system workqueue should avoid using handlers
1850 * that block or yield since this may prevent the system workqueue from
1851 * processing other work items in a timely manner.
1852 *
1853 * @note Can be called by ISRs.
1854 *
1855 * @param work Address of delayed work item.
1856 * @param delay Delay before submitting the work item (in milliseconds).
1857 *
1858 * @retval 0 Work item countdown started.
1859 * @retval -EINPROGRESS Work item is already pending.
1860 * @retval -EINVAL Work item is being processed or has completed its work.
1861 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001862 */
1863static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001864 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001865{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001866 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001867}
1868
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001869/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001870 * @brief Get time remaining before a delayed work gets scheduled.
1871 *
1872 * This routine computes the (approximate) time remaining before a
1873 * delayed work gets executed. If the delayed work is not waiting to be
1874 * schedules, it returns zero.
1875 *
1876 * @param work Delayed work item.
1877 *
1878 * @return Remaining time (in milliseconds).
1879 */
1880static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1881{
1882 return _timeout_remaining_get(&work->timeout);
1883}
1884
1885/**
Allan Stephensc98da842016-11-11 15:45:03 -05001886 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887 */
1888
Allan Stephensc98da842016-11-11 15:45:03 -05001889/**
1890 * @cond INTERNAL_HIDDEN
1891 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892
1893struct k_mutex {
1894 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001895 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896 uint32_t lock_count;
1897 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001898
Anas Nashif2f203c22016-12-18 06:57:45 -05001899 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900};
1901
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902#define K_MUTEX_INITIALIZER(obj) \
1903 { \
1904 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1905 .owner = NULL, \
1906 .lock_count = 0, \
1907 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001908 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909 }
1910
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911/**
Allan Stephensc98da842016-11-11 15:45:03 -05001912 * INTERNAL_HIDDEN @endcond
1913 */
1914
1915/**
1916 * @defgroup mutex_apis Mutex APIs
1917 * @ingroup kernel_apis
1918 * @{
1919 */
1920
1921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001926 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001928 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001931 struct k_mutex name \
1932 __in_section(_k_mutex, static, name) = \
1933 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001936 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001938 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001940 * Upon completion, the mutex is available and does not have an owner.
1941 *
1942 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
1944 * @return N/A
1945 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947
1948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001951 * This routine locks @a mutex. If the mutex is locked by another thread,
1952 * the calling thread waits until the mutex becomes available or until
1953 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001955 * A thread is permitted to lock a mutex it has already locked. The operation
1956 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001958 * @param mutex Address of the mutex.
1959 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 * or one of the special values K_NO_WAIT and K_FOREVER.
1961 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001962 * @retval 0 Mutex locked.
1963 * @retval -EBUSY Returned without waiting.
1964 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967
1968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001969 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * This routine unlocks @a mutex. The mutex must already be locked by the
1972 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
1974 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 * thread.
1977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979 *
1980 * @return N/A
1981 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982extern void k_mutex_unlock(struct k_mutex *mutex);
1983
Allan Stephensc98da842016-11-11 15:45:03 -05001984/**
1985 * @} end defgroup mutex_apis
1986 */
1987
1988/**
1989 * @cond INTERNAL_HIDDEN
1990 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991
1992struct k_sem {
1993 _wait_q_t wait_q;
1994 unsigned int count;
1995 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001996 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997
Anas Nashif2f203c22016-12-18 06:57:45 -05001998 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999};
2000
Allan Stephensc98da842016-11-11 15:45:03 -05002001#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2002 { \
2003 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2004 .count = initial_count, \
2005 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002006 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002007 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002008 }
2009
2010/**
2011 * INTERNAL_HIDDEN @endcond
2012 */
2013
2014/**
2015 * @defgroup semaphore_apis Semaphore APIs
2016 * @ingroup kernel_apis
2017 * @{
2018 */
2019
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002025 * @param sem Address of the semaphore.
2026 * @param initial_count Initial semaphore count.
2027 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002028 *
2029 * @return N/A
2030 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2032 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002033
2034/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2040 *
2041 * @param sem Address of the semaphore.
2042 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002043 * or one of the special values K_NO_WAIT and K_FOREVER.
2044 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002045 * @note When porting code from the nanokernel legacy API to the new API, be
2046 * careful with the return value of this function. The return value is the
2047 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2048 * non-zero means failure, while the nano_sem_take family returns 1 for success
2049 * and 0 for failure.
2050 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002051 * @retval 0 Semaphore taken.
2052 * @retval -EBUSY Returned without waiting.
2053 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002056
2057/**
2058 * @brief Give a semaphore.
2059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002060 * This routine gives @a sem, unless the semaphore is already at its maximum
2061 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002066 *
2067 * @return N/A
2068 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002069extern void k_sem_give(struct k_sem *sem);
2070
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002071/**
2072 * @brief Reset a semaphore's count to zero.
2073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002077 *
2078 * @return N/A
2079 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002080static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081{
2082 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083}
2084
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002085/**
2086 * @brief Get a semaphore's count.
2087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002093 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002094static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095{
2096 return sem->count;
2097}
2098
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002102 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002103 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002104 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * @param name Name of the semaphore.
2107 * @param initial_count Initial semaphore count.
2108 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002109 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002110#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002111 struct k_sem name \
2112 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113 K_SEM_INITIALIZER(name, initial_count, count_limit)
2114
Allan Stephensc98da842016-11-11 15:45:03 -05002115/**
2116 * @} end defgroup semaphore_apis
2117 */
2118
2119/**
2120 * @defgroup alert_apis Alert APIs
2121 * @ingroup kernel_apis
2122 * @{
2123 */
2124
Allan Stephens5eceb852016-11-16 10:16:30 -05002125/**
2126 * @typedef k_alert_handler_t
2127 * @brief Alert handler function type.
2128 *
2129 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002130 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002131 * and is only invoked if the alert has been initialized with one.
2132 *
2133 * @param alert Address of the alert.
2134 *
2135 * @return 0 if alert has been consumed; non-zero if alert should pend.
2136 */
2137typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002138
2139/**
2140 * @} end defgroup alert_apis
2141 */
2142
2143/**
2144 * @cond INTERNAL_HIDDEN
2145 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002147#define K_ALERT_DEFAULT NULL
2148#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002150struct k_alert {
2151 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002152 atomic_t send_count;
2153 struct k_work work_item;
2154 struct k_sem sem;
2155
Anas Nashif2f203c22016-12-18 06:57:45 -05002156 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157};
2158
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002159extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002161#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002163 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002164 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002165 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002166 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002167 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002168 }
2169
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170/**
Allan Stephensc98da842016-11-11 15:45:03 -05002171 * INTERNAL_HIDDEN @endcond
2172 */
2173
2174/**
2175 * @addtogroup alert_apis
2176 * @{
2177 */
2178
2179/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002182 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002184 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * @param name Name of the alert.
2187 * @param alert_handler Action to take when alert is sent. Specify either
2188 * the address of a function to be invoked by the system workqueue
2189 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2190 * K_ALERT_DEFAULT (which causes the alert to pend).
2191 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002193#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002194 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002195 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002196 K_ALERT_INITIALIZER(name, alert_handler, \
2197 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002200 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002204 * @param alert Address of the alert.
2205 * @param handler Action to take when alert is sent. Specify either the address
2206 * of a function to be invoked by the system workqueue thread,
2207 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2208 * K_ALERT_DEFAULT (which causes the alert to pend).
2209 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002210 *
2211 * @return N/A
2212 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002213extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2214 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215
2216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002219 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002221 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2222 *
2223 * @param alert Address of the alert.
2224 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225 * or one of the special values K_NO_WAIT and K_FOREVER.
2226 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002227 * @retval 0 Alert received.
2228 * @retval -EBUSY Returned without waiting.
2229 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002231extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232
2233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002234 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002236 * This routine signals @a alert. The action specified for @a alert will
2237 * be taken, which may trigger the execution of an alert handler function
2238 * and/or cause the alert to pend (assuming the alert has not reached its
2239 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * @note Can be called by ISRs.
2242 *
2243 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244 *
2245 * @return N/A
2246 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002247extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248
2249/**
Allan Stephensc98da842016-11-11 15:45:03 -05002250 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251 */
2252
Allan Stephensc98da842016-11-11 15:45:03 -05002253/**
2254 * @cond INTERNAL_HIDDEN
2255 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256
2257struct k_msgq {
2258 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002259 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260 uint32_t max_msgs;
2261 char *buffer_start;
2262 char *buffer_end;
2263 char *read_ptr;
2264 char *write_ptr;
2265 uint32_t used_msgs;
2266
Anas Nashif2f203c22016-12-18 06:57:45 -05002267 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268};
2269
Peter Mitsis1da807e2016-10-06 11:36:59 -04002270#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 { \
2272 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002273 .max_msgs = q_max_msgs, \
2274 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002276 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277 .read_ptr = q_buffer, \
2278 .write_ptr = q_buffer, \
2279 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002280 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 }
2282
Peter Mitsis1da807e2016-10-06 11:36:59 -04002283/**
Allan Stephensc98da842016-11-11 15:45:03 -05002284 * INTERNAL_HIDDEN @endcond
2285 */
2286
2287/**
2288 * @defgroup msgq_apis Message Queue APIs
2289 * @ingroup kernel_apis
2290 * @{
2291 */
2292
2293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002296 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2297 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002298 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2299 * message is similarly aligned to this boundary, @a q_msg_size must also be
2300 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002302 * The message queue can be accessed outside the module where it is defined
2303 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002305 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002307 * @param q_name Name of the message queue.
2308 * @param q_msg_size Message size (in bytes).
2309 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002310 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002311 */
2312#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2313 static char __noinit __aligned(q_align) \
2314 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002315 struct k_msgq q_name \
2316 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002317 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2318 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319
Peter Mitsisd7a37502016-10-13 11:37:40 -04002320/**
2321 * @brief Initialize a message queue.
2322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * This routine initializes a message queue object, prior to its first use.
2324 *
Allan Stephensda827222016-11-09 14:23:58 -06002325 * The message queue's ring buffer must contain space for @a max_msgs messages,
2326 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2327 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2328 * that each message is similarly aligned to this boundary, @a q_msg_size
2329 * must also be a multiple of N.
2330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * @param q Address of the message queue.
2332 * @param buffer Pointer to ring buffer that holds queued messages.
2333 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002334 * @param max_msgs Maximum number of messages that can be queued.
2335 *
2336 * @return N/A
2337 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002338extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002339 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002340
2341/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002345 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002346 * @note Can be called by ISRs.
2347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * @param q Address of the message queue.
2349 * @param data Pointer to the message.
2350 * @param timeout Waiting period to add the message (in milliseconds),
2351 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002352 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002353 * @retval 0 Message sent.
2354 * @retval -ENOMSG Returned without waiting or queue purged.
2355 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002358
2359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * This routine receives a message from message queue @a q in a "first in,
2363 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002364 *
Allan Stephensc98da842016-11-11 15:45:03 -05002365 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * @param q Address of the message queue.
2368 * @param data Address of area to hold the received message.
2369 * @param timeout Waiting period to receive the message (in milliseconds),
2370 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002371 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002372 * @retval 0 Message received.
2373 * @retval -ENOMSG Returned without waiting.
2374 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002375 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002377
2378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * This routine discards all unreceived messages in a message queue's ring
2382 * buffer. Any threads that are blocked waiting to send a message to the
2383 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002386 *
2387 * @return N/A
2388 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389extern void k_msgq_purge(struct k_msgq *q);
2390
Peter Mitsis67be2492016-10-07 11:44:34 -04002391/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * This routine returns the number of unused entries in a message queue's
2395 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @param q Address of the message queue.
2398 *
2399 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002400 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002401static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002402{
2403 return q->max_msgs - q->used_msgs;
2404}
2405
Peter Mitsisd7a37502016-10-13 11:37:40 -04002406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @param q Address of the message queue.
2412 *
2413 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002414 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002415static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416{
2417 return q->used_msgs;
2418}
2419
Allan Stephensc98da842016-11-11 15:45:03 -05002420/**
2421 * @} end defgroup msgq_apis
2422 */
2423
2424/**
2425 * @defgroup mem_pool_apis Memory Pool APIs
2426 * @ingroup kernel_apis
2427 * @{
2428 */
2429
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002431 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432 void *addr_in_pool;
2433 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002434 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435};
2436
Allan Stephensc98da842016-11-11 15:45:03 -05002437/**
2438 * @} end defgroup mem_pool_apis
2439 */
2440
2441/**
2442 * @defgroup mailbox_apis Mailbox APIs
2443 * @ingroup kernel_apis
2444 * @{
2445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446
2447struct k_mbox_msg {
2448 /** internal use only - needed for legacy API support */
2449 uint32_t _mailbox;
2450 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002451 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452 /** application-defined information value */
2453 uint32_t info;
2454 /** sender's message data buffer */
2455 void *tx_data;
2456 /** internal use only - needed for legacy API support */
2457 void *_rx_data;
2458 /** message data block descriptor */
2459 struct k_mem_block tx_block;
2460 /** source thread id */
2461 k_tid_t rx_source_thread;
2462 /** target thread id */
2463 k_tid_t tx_target_thread;
2464 /** internal use only - thread waiting on send (may be a dummy) */
2465 k_tid_t _syncing_thread;
2466#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2467 /** internal use only - semaphore used during asynchronous send */
2468 struct k_sem *_async_sem;
2469#endif
2470};
2471
Allan Stephensc98da842016-11-11 15:45:03 -05002472/**
2473 * @cond INTERNAL_HIDDEN
2474 */
2475
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476struct k_mbox {
2477 _wait_q_t tx_msg_queue;
2478 _wait_q_t rx_msg_queue;
2479
Anas Nashif2f203c22016-12-18 06:57:45 -05002480 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481};
2482
2483#define K_MBOX_INITIALIZER(obj) \
2484 { \
2485 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2486 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002487 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488 }
2489
Peter Mitsis12092702016-10-14 12:57:23 -04002490/**
Allan Stephensc98da842016-11-11 15:45:03 -05002491 * INTERNAL_HIDDEN @endcond
2492 */
2493
2494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002499 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002504 struct k_mbox name \
2505 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 K_MBOX_INITIALIZER(name) \
2507
Peter Mitsis12092702016-10-14 12:57:23 -04002508/**
2509 * @brief Initialize a mailbox.
2510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * This routine initializes a mailbox object, prior to its first use.
2512 *
2513 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002514 *
2515 * @return N/A
2516 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517extern void k_mbox_init(struct k_mbox *mbox);
2518
Peter Mitsis12092702016-10-14 12:57:23 -04002519/**
2520 * @brief Send a mailbox message in a synchronous manner.
2521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * This routine sends a message to @a mbox and waits for a receiver to both
2523 * receive and process it. The message data may be in a buffer, in a memory
2524 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @param mbox Address of the mailbox.
2527 * @param tx_msg Address of the transmit message descriptor.
2528 * @param timeout Waiting period for the message to be received (in
2529 * milliseconds), or one of the special values K_NO_WAIT
2530 * and K_FOREVER. Once the message has been received,
2531 * this routine waits as long as necessary for the message
2532 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002533 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002534 * @retval 0 Message sent.
2535 * @retval -ENOMSG Returned without waiting.
2536 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002537 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002538extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002540
Peter Mitsis12092702016-10-14 12:57:23 -04002541/**
2542 * @brief Send a mailbox message in an asynchronous manner.
2543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * This routine sends a message to @a mbox without waiting for a receiver
2545 * to process it. The message data may be in a buffer, in a memory pool block,
2546 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2547 * will be given when the message has been both received and completely
2548 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * @param mbox Address of the mailbox.
2551 * @param tx_msg Address of the transmit message descriptor.
2552 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002553 *
2554 * @return N/A
2555 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002556extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557 struct k_sem *sem);
2558
Peter Mitsis12092702016-10-14 12:57:23 -04002559/**
2560 * @brief Receive a mailbox message.
2561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * This routine receives a message from @a mbox, then optionally retrieves
2563 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002565 * @param mbox Address of the mailbox.
2566 * @param rx_msg Address of the receive message descriptor.
2567 * @param buffer Address of the buffer to receive data, or NULL to defer data
2568 * retrieval and message disposal until later.
2569 * @param timeout Waiting period for a message to be received (in
2570 * milliseconds), or one of the special values K_NO_WAIT
2571 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002572 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002573 * @retval 0 Message received.
2574 * @retval -ENOMSG Returned without waiting.
2575 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002576 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002577extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002579
2580/**
2581 * @brief Retrieve mailbox message data into a buffer.
2582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * This routine completes the processing of a received message by retrieving
2584 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002585 *
2586 * Alternatively, this routine can be used to dispose of a received message
2587 * without retrieving its data.
2588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * @param rx_msg Address of the receive message descriptor.
2590 * @param buffer Address of the buffer to receive data, or NULL to discard
2591 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002592 *
2593 * @return N/A
2594 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002595extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002596
2597/**
2598 * @brief Retrieve mailbox message data into a memory pool block.
2599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * This routine completes the processing of a received message by retrieving
2601 * its data into a memory pool block, then disposing of the message.
2602 * The memory pool block that results from successful retrieval must be
2603 * returned to the pool once the data has been processed, even in cases
2604 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002605 *
2606 * Alternatively, this routine can be used to dispose of a received message
2607 * without retrieving its data. In this case there is no need to return a
2608 * memory pool block to the pool.
2609 *
2610 * This routine allocates a new memory pool block for the data only if the
2611 * data is not already in one. If a new block cannot be allocated, the routine
2612 * returns a failure code and the received message is left unchanged. This
2613 * permits the caller to reattempt data retrieval at a later time or to dispose
2614 * of the received message without retrieving its data.
2615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * @param rx_msg Address of a receive message descriptor.
2617 * @param pool Address of memory pool, or NULL to discard data.
2618 * @param block Address of the area to hold memory pool block info.
2619 * @param timeout Waiting period to wait for a memory pool block (in
2620 * milliseconds), or one of the special values K_NO_WAIT
2621 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002622 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002623 * @retval 0 Data retrieved.
2624 * @retval -ENOMEM Returned without waiting.
2625 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002626 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002627extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002628 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629 struct k_mem_block *block, int32_t timeout);
2630
Allan Stephensc98da842016-11-11 15:45:03 -05002631/**
2632 * @} end defgroup mailbox_apis
2633 */
2634
2635/**
2636 * @cond INTERNAL_HIDDEN
2637 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638
2639struct k_pipe {
2640 unsigned char *buffer; /* Pipe buffer: may be NULL */
2641 size_t size; /* Buffer size */
2642 size_t bytes_used; /* # bytes used in buffer */
2643 size_t read_index; /* Where in buffer to read from */
2644 size_t write_index; /* Where in buffer to write */
2645
2646 struct {
2647 _wait_q_t readers; /* Reader wait queue */
2648 _wait_q_t writers; /* Writer wait queue */
2649 } wait_q;
2650
Anas Nashif2f203c22016-12-18 06:57:45 -05002651 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652};
2653
Peter Mitsise5d9c582016-10-14 14:44:57 -04002654#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655 { \
2656 .buffer = pipe_buffer, \
2657 .size = pipe_buffer_size, \
2658 .bytes_used = 0, \
2659 .read_index = 0, \
2660 .write_index = 0, \
2661 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2662 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002663 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 }
2665
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666/**
Allan Stephensc98da842016-11-11 15:45:03 -05002667 * INTERNAL_HIDDEN @endcond
2668 */
2669
2670/**
2671 * @defgroup pipe_apis Pipe APIs
2672 * @ingroup kernel_apis
2673 * @{
2674 */
2675
2676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002680 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002681 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @param name Name of the pipe.
2684 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2685 * or zero if no ring buffer is used.
2686 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002687 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002688#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2689 static unsigned char __noinit __aligned(pipe_align) \
2690 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002691 struct k_pipe name \
2692 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002693 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002698 * This routine initializes a pipe object, prior to its first use.
2699 *
2700 * @param pipe Address of the pipe.
2701 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2702 * is used.
2703 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2704 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705 *
2706 * @return N/A
2707 */
2708extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2709 size_t size);
2710
2711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002712 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * @param pipe Address of the pipe.
2717 * @param data Address of data to write.
2718 * @param bytes_to_write Size of data (in bytes).
2719 * @param bytes_written Address of area to hold the number of bytes written.
2720 * @param min_xfer Minimum number of bytes to write.
2721 * @param timeout Waiting period to wait for the data to be written (in
2722 * milliseconds), or one of the special values K_NO_WAIT
2723 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002725 * @retval 0 At least @a min_xfer bytes of data were written.
2726 * @retval -EIO Returned without waiting; zero data bytes were written.
2727 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002728 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002730extern int k_pipe_put(struct k_pipe *pipe, void *data,
2731 size_t bytes_to_write, size_t *bytes_written,
2732 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
2734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @param pipe Address of the pipe.
2740 * @param data Address to place the data read from pipe.
2741 * @param bytes_to_read Maximum number of data bytes to read.
2742 * @param bytes_read Address of area to hold the number of bytes read.
2743 * @param min_xfer Minimum number of data bytes to read.
2744 * @param timeout Waiting period to wait for the data to be read (in
2745 * milliseconds), or one of the special values K_NO_WAIT
2746 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002748 * @retval 0 At least @a min_xfer bytes of data were read.
2749 * @retval -EIO Returned without waiting; zero data bytes were read.
2750 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002751 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002753extern int k_pipe_get(struct k_pipe *pipe, void *data,
2754 size_t bytes_to_read, size_t *bytes_read,
2755 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756
2757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * This routine writes the data contained in a memory block to @a pipe.
2761 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 * @param block Memory block containing data to send
2766 * @param size Number of data bytes in memory block to send
2767 * @param sem Semaphore to signal upon completion (else NULL)
2768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770 */
2771extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2772 size_t size, struct k_sem *sem);
2773
2774/**
Allan Stephensc98da842016-11-11 15:45:03 -05002775 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776 */
2777
Allan Stephensc98da842016-11-11 15:45:03 -05002778/**
2779 * @cond INTERNAL_HIDDEN
2780 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002782struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002784 uint32_t num_blocks;
2785 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 char *buffer;
2787 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002788 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789
Anas Nashif2f203c22016-12-18 06:57:45 -05002790 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791};
2792
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002793#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2794 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795 { \
2796 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002797 .num_blocks = slab_num_blocks, \
2798 .block_size = slab_block_size, \
2799 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 .free_list = NULL, \
2801 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002802 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803 }
2804
Peter Mitsis578f9112016-10-07 13:50:31 -04002805/**
Allan Stephensc98da842016-11-11 15:45:03 -05002806 * INTERNAL_HIDDEN @endcond
2807 */
2808
2809/**
2810 * @defgroup mem_slab_apis Memory Slab APIs
2811 * @ingroup kernel_apis
2812 * @{
2813 */
2814
2815/**
Allan Stephensda827222016-11-09 14:23:58 -06002816 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002817 *
Allan Stephensda827222016-11-09 14:23:58 -06002818 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002820 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2821 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002822 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002823 *
Allan Stephensda827222016-11-09 14:23:58 -06002824 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002827 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002829 * @param name Name of the memory slab.
2830 * @param slab_block_size Size of each memory block (in bytes).
2831 * @param slab_num_blocks Number memory blocks.
2832 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002833 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002834#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2835 char __noinit __aligned(slab_align) \
2836 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2837 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002838 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002839 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2840 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002842/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002843 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002846 *
Allan Stephensda827222016-11-09 14:23:58 -06002847 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2848 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2849 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2850 * To ensure that each memory block is similarly aligned to this boundary,
2851 * @a slab_block_size must also be a multiple of N.
2852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @param slab Address of the memory slab.
2854 * @param buffer Pointer to buffer used for the memory blocks.
2855 * @param block_size Size of each memory block (in bytes).
2856 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002857 *
2858 * @return N/A
2859 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002860extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002861 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002862
2863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * @param slab Address of the memory slab.
2869 * @param mem Pointer to block address area.
2870 * @param timeout Maximum time to wait for operation to complete
2871 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2872 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002873 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002874 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002876 * @retval -ENOMEM Returned without waiting.
2877 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002878 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002879extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2880 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002881
2882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * This routine releases a previously allocated memory block back to its
2886 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param slab Address of the memory slab.
2889 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002890 *
2891 * @return N/A
2892 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002893extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002895/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * This routine gets the number of memory blocks that are currently
2899 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002904 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002905static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002907 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908}
2909
Peter Mitsisc001aa82016-10-13 13:53:37 -04002910/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * This routine gets the number of memory blocks that are currently
2914 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002919 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002920static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002921{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002922 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002923}
2924
Allan Stephensc98da842016-11-11 15:45:03 -05002925/**
2926 * @} end defgroup mem_slab_apis
2927 */
2928
2929/**
2930 * @cond INTERNAL_HIDDEN
2931 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002933/*
2934 * Memory pool requires a buffer and two arrays of structures for the
2935 * memory block accounting:
2936 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2937 * status of four blocks of memory.
2938 */
2939struct k_mem_pool_quad_block {
2940 char *mem_blocks; /* pointer to the first of four memory blocks */
2941 uint32_t mem_status; /* four bits. If bit is set, memory block is
2942 allocated */
2943};
2944/*
2945 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2946 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2947 * block size is 4 times less than the previous one and thus requires 4 times
2948 * bigger array of k_mem_pool_quad_block structures to keep track of the
2949 * memory blocks.
2950 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002952/*
2953 * The array of k_mem_pool_block_set keeps the information of each array of
2954 * k_mem_pool_quad_block structures
2955 */
2956struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002957 size_t block_size; /* memory block size */
2958 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002959 struct k_mem_pool_quad_block *quad_block;
2960 int count;
2961};
2962
2963/* Memory pool descriptor */
2964struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002965 size_t max_block_size;
2966 size_t min_block_size;
2967 uint32_t nr_of_maxblocks;
2968 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002969 struct k_mem_pool_block_set *block_set;
2970 char *bufblock;
2971 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002972 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973};
2974
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002975#ifdef CONFIG_ARM
2976#define _SECTION_TYPE_SIGN "%"
2977#else
2978#define _SECTION_TYPE_SIGN "@"
2979#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002981/*
2982 * Static memory pool initialization
2983 */
Allan Stephensc98da842016-11-11 15:45:03 -05002984
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002985/*
2986 * Use .altmacro to be able to recalculate values and pass them as string
David B. Kinder8b986d72017-04-18 15:56:26 -07002987 * arguments when calling assembler macros recursively
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002988 */
2989__asm__(".altmacro\n\t");
2990
2991/*
2992 * Recursively calls a macro
David B. Kinder8b986d72017-04-18 15:56:26 -07002993 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002994 * __memory_pool_max_block_size - maximal size of the memory block
2995 * __memory_pool_min_block_size - minimal size of the memory block
2996 * Notes:
2997 * Global symbols are used due the fact that assembler macro allows only
2998 * one argument be passed with the % conversion
2999 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3000 * is used instead of / 4.
3001 * n_max argument needs to go first in the invoked macro, as some
3002 * assemblers concatenate \name and %(\n_max * 4) arguments
3003 * if \name goes first
3004 */
3005__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3006 ".ifge __memory_pool_max_block_size >> 2 -"
3007 " __memory_pool_min_block_size\n\t\t"
3008 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3009 "\\macro_name %(\\n_max * 4) \\name\n\t"
3010 ".endif\n\t"
3011 ".endm\n");
3012
3013/*
3014 * Build quad blocks
3015 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3016 * structures and recursively calls itself for the next array, 4 times
3017 * larger.
David B. Kinder8b986d72017-04-18 15:56:26 -07003018 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003019 * __memory_pool_max_block_size - maximal size of the memory block
3020 * __memory_pool_min_block_size - minimal size of the memory block
3021 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3022 */
3023__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003024 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003025 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3026 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3027 ".if \\n_max % 4\n\t\t"
3028 ".skip __memory_pool_quad_block_size\n\t"
3029 ".endif\n\t"
3030 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3031 ".endm\n");
3032
3033/*
3034 * Build block sets and initialize them
3035 * Macro initializes the k_mem_pool_block_set structure and
3036 * recursively calls itself for the next one.
David B. Kinder8b986d72017-04-18 15:56:26 -07003037 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003038 * __memory_pool_max_block_size - maximal size of the memory block
3039 * __memory_pool_min_block_size - minimal size of the memory block
3040 * __memory_pool_block_set_count, the number of the elements in the
3041 * block set array must be set to 0. Macro calculates it's real
3042 * value.
3043 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3044 * structures, _build_quad_blocks must be called prior it.
3045 */
3046__asm__(".macro _build_block_set n_max, name\n\t"
3047 ".int __memory_pool_max_block_size\n\t" /* block_size */
3048 ".if \\n_max % 4\n\t\t"
3049 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3050 ".else\n\t\t"
3051 ".int \\n_max >> 2\n\t"
3052 ".endif\n\t"
3053 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3054 ".int 0\n\t" /* count */
3055 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3056 "__do_recurse _build_block_set \\name \\n_max\n\t"
3057 ".endm\n");
3058
3059/*
3060 * Build a memory pool structure and initialize it
3061 * Macro uses __memory_pool_block_set_count global symbol,
3062 * block set addresses and buffer address, it may be called only after
3063 * _build_block_set
3064 */
3065__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003066 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003067 _SECTION_TYPE_SIGN "progbits\n\t"
3068 ".globl \\name\n\t"
3069 "\\name:\n\t"
3070 ".int \\max_size\n\t" /* max_block_size */
3071 ".int \\min_size\n\t" /* min_block_size */
3072 ".int \\n_max\n\t" /* nr_of_maxblocks */
3073 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3074 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3075 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3076 ".int 0\n\t" /* wait_q->head */
3077 ".int 0\n\t" /* wait_q->next */
3078 ".popsection\n\t"
3079 ".endm\n");
3080
3081#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3082 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3083 _SECTION_TYPE_SIGN "progbits\n\t"); \
3084 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3085 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3086 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3087 STRINGIFY(name) "\n\t"); \
3088 __asm__(".popsection\n\t")
3089
3090#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3091 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3092 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3093 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3094 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003095 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003096 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3097 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3098 STRINGIFY(name) "\n\t"); \
3099 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3100 __asm__(".int __memory_pool_block_set_count\n\t"); \
3101 __asm__(".popsection\n\t"); \
3102 extern uint32_t _mem_pool_block_set_count_##name; \
3103 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3104
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003105#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3106 char __noinit __aligned(align) \
3107 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003108
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003109/*
3110 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3111 * to __memory_pool_quad_block_size absolute symbol.
3112 * This function does not get called, but compiler calculates the value and
3113 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3114 */
3115static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3116{
3117 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003118#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003119 "__memory_pool_quad_block_size = %0\n\t"
3120#else
3121 "__memory_pool_quad_block_size = %c0\n\t"
3122#endif
3123 :
3124 : "n"(sizeof(struct k_mem_pool_quad_block)));
3125}
3126
3127/**
Allan Stephensc98da842016-11-11 15:45:03 -05003128 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003129 */
3130
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003131/**
Allan Stephensc98da842016-11-11 15:45:03 -05003132 * @addtogroup mem_pool_apis
3133 * @{
3134 */
3135
3136/**
3137 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3140 * long. The memory pool allows blocks to be repeatedly partitioned into
3141 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3142 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003143 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003145 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003146 * If the pool is to be accessed outside the module where it is defined, it
3147 * can be declared via
3148 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003149 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003151 * @param name Name of the memory pool.
3152 * @param min_size Size of the smallest blocks in the pool (in bytes).
3153 * @param max_size Size of the largest blocks in the pool (in bytes).
3154 * @param n_max Number of maximum sized blocks in the pool.
3155 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003156 */
3157#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003158 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3159 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003160 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003161 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3162 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3163 extern struct k_mem_pool name
3164
Peter Mitsis937042c2016-10-13 13:18:26 -04003165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003168 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * @param pool Address of the memory pool.
3171 * @param block Pointer to block descriptor for the allocated memory.
3172 * @param size Amount of memory to allocate (in bytes).
3173 * @param timeout Maximum time to wait for operation to complete
3174 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3175 * or K_FOREVER to wait as long as necessary.
3176 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003177 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003179 * @retval -ENOMEM Returned without waiting.
3180 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003181 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003182extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04003183 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003184
3185/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * This routine releases a previously allocated memory block back to its
3189 * memory pool.
3190 *
3191 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003192 *
3193 * @return N/A
3194 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003195extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003196
3197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * This routine instructs a memory pool to concatenate unused memory blocks
3201 * into larger blocks wherever possible. Manually defragmenting the memory
3202 * pool may speed up future allocations of memory blocks by eliminating the
3203 * need for the memory pool to perform an automatic partial defragmentation.
3204 *
3205 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003206 *
3207 * @return N/A
3208 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003209extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003210
3211/**
Allan Stephensc98da842016-11-11 15:45:03 -05003212 * @} end addtogroup mem_pool_apis
3213 */
3214
3215/**
3216 * @defgroup heap_apis Heap Memory Pool APIs
3217 * @ingroup kernel_apis
3218 * @{
3219 */
3220
3221/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003225 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003230 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003231extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003232
3233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003234 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003235 *
3236 * This routine provides traditional free() semantics. The memory being
3237 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003238 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003239 * If @a ptr is NULL, no operation is performed.
3240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003241 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003242 *
3243 * @return N/A
3244 */
3245extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246
Allan Stephensc98da842016-11-11 15:45:03 -05003247/**
3248 * @} end defgroup heap_apis
3249 */
3250
Benjamin Walshacc68c12017-01-29 18:57:45 -05003251/* polling API - PRIVATE */
3252
Benjamin Walshb0179862017-02-02 16:39:57 -05003253#ifdef CONFIG_POLL
3254#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3255#else
3256#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3257#endif
3258
Benjamin Walshacc68c12017-01-29 18:57:45 -05003259/* private - implementation data created as needed, per-type */
3260struct _poller {
3261 struct k_thread *thread;
3262};
3263
3264/* private - types bit positions */
3265enum _poll_types_bits {
3266 /* can be used to ignore an event */
3267 _POLL_TYPE_IGNORE,
3268
3269 /* to be signaled by k_poll_signal() */
3270 _POLL_TYPE_SIGNAL,
3271
3272 /* semaphore availability */
3273 _POLL_TYPE_SEM_AVAILABLE,
3274
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003275 /* queue/fifo/lifo data availability */
3276 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003277
3278 _POLL_NUM_TYPES
3279};
3280
3281#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3282
3283/* private - states bit positions */
3284enum _poll_states_bits {
3285 /* default state when creating event */
3286 _POLL_STATE_NOT_READY,
3287
3288 /* there was another poller already on the object */
3289 _POLL_STATE_EADDRINUSE,
3290
3291 /* signaled by k_poll_signal() */
3292 _POLL_STATE_SIGNALED,
3293
3294 /* semaphore is available */
3295 _POLL_STATE_SEM_AVAILABLE,
3296
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003297 /* data is available to read on queue/fifo/lifo */
3298 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003299
3300 _POLL_NUM_STATES
3301};
3302
3303#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3304
3305#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003306 (32 - (0 \
3307 + 8 /* tag */ \
3308 + _POLL_NUM_TYPES \
3309 + _POLL_NUM_STATES \
3310 + 1 /* modes */ \
3311 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003312
3313#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3314#error overflow of 32-bit word in struct k_poll_event
3315#endif
3316
3317/* end of polling API - PRIVATE */
3318
3319
3320/**
3321 * @defgroup poll_apis Async polling APIs
3322 * @ingroup kernel_apis
3323 * @{
3324 */
3325
3326/* Public polling API */
3327
3328/* public - values for k_poll_event.type bitfield */
3329#define K_POLL_TYPE_IGNORE 0
3330#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3331#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003332#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3333#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003334
3335/* public - polling modes */
3336enum k_poll_modes {
3337 /* polling thread does not take ownership of objects when available */
3338 K_POLL_MODE_NOTIFY_ONLY = 0,
3339
3340 K_POLL_NUM_MODES
3341};
3342
3343/* public - values for k_poll_event.state bitfield */
3344#define K_POLL_STATE_NOT_READY 0
3345#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3346#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3347#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003348#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3349#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003350
3351/* public - poll signal object */
3352struct k_poll_signal {
3353 /* PRIVATE - DO NOT TOUCH */
3354 struct k_poll_event *poll_event;
3355
3356 /*
3357 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3358 * user resets it to 0.
3359 */
3360 unsigned int signaled;
3361
3362 /* custom result value passed to k_poll_signal() if needed */
3363 int result;
3364};
3365
3366#define K_POLL_SIGNAL_INITIALIZER() \
3367 { \
3368 .poll_event = NULL, \
3369 .signaled = 0, \
3370 .result = 0, \
3371 }
3372
3373struct k_poll_event {
3374 /* PRIVATE - DO NOT TOUCH */
3375 struct _poller *poller;
3376
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003377 /* optional user-specified tag, opaque, untouched by the API */
3378 uint32_t tag:8;
3379
Benjamin Walshacc68c12017-01-29 18:57:45 -05003380 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
3381 uint32_t type:_POLL_NUM_TYPES;
3382
3383 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
3384 uint32_t state:_POLL_NUM_STATES;
3385
3386 /* mode of operation, from enum k_poll_modes */
3387 uint32_t mode:1;
3388
3389 /* unused bits in 32-bit word */
3390 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
3391
3392 /* per-type data */
3393 union {
3394 void *obj;
3395 struct k_poll_signal *signal;
3396 struct k_sem *sem;
3397 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003398 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003399 };
3400};
3401
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003402#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003403 { \
3404 .poller = NULL, \
3405 .type = event_type, \
3406 .state = K_POLL_STATE_NOT_READY, \
3407 .mode = event_mode, \
3408 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003409 { .obj = event_obj }, \
3410 }
3411
3412#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3413 event_tag) \
3414 { \
3415 .type = event_type, \
3416 .tag = event_tag, \
3417 .state = K_POLL_STATE_NOT_READY, \
3418 .mode = event_mode, \
3419 .unused = 0, \
3420 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003421 }
3422
3423/**
3424 * @brief Initialize one struct k_poll_event instance
3425 *
3426 * After this routine is called on a poll event, the event it ready to be
3427 * placed in an event array to be passed to k_poll().
3428 *
3429 * @param event The event to initialize.
3430 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3431 * values. Only values that apply to the same object being polled
3432 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3433 * event.
3434 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3435 * @param obj Kernel object or poll signal.
3436 *
3437 * @return N/A
3438 */
3439
3440extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
3441 int mode, void *obj);
3442
3443/**
3444 * @brief Wait for one or many of multiple poll events to occur
3445 *
3446 * This routine allows a thread to wait concurrently for one or many of
3447 * multiple poll events to have occurred. Such events can be a kernel object
3448 * being available, like a semaphore, or a poll signal event.
3449 *
3450 * When an event notifies that a kernel object is available, the kernel object
3451 * is not "given" to the thread calling k_poll(): it merely signals the fact
3452 * that the object was available when the k_poll() call was in effect. Also,
3453 * all threads trying to acquire an object the regular way, i.e. by pending on
3454 * the object, have precedence over the thread polling on the object. This
3455 * means that the polling thread will never get the poll event on an object
3456 * until the object becomes available and its pend queue is empty. For this
3457 * reason, the k_poll() call is more effective when the objects being polled
3458 * only have one thread, the polling thread, trying to acquire them.
3459 *
3460 * Only one thread can be polling for a particular object at a given time. If
3461 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3462 * and returns as soon as it has finished handling the other events. This means
3463 * that k_poll() can return -EADDRINUSE and have the state value of some events
3464 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3465 * parameter is ignored.
3466 *
3467 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3468 * events that were passed to k_poll() and check the state field for the values
3469 * that were expected and take the associated actions.
3470 *
3471 * Before being reused for another call to k_poll(), the user has to reset the
3472 * state field to K_POLL_STATE_NOT_READY.
3473 *
3474 * @param events An array of pointers to events to be polled for.
3475 * @param num_events The number of events in the array.
3476 * @param timeout Waiting period for an event to be ready (in milliseconds),
3477 * or one of the special values K_NO_WAIT and K_FOREVER.
3478 *
3479 * @retval 0 One or more events are ready.
3480 * @retval -EADDRINUSE One or more objects already had a poller.
3481 * @retval -EAGAIN Waiting period timed out.
3482 */
3483
3484extern int k_poll(struct k_poll_event *events, int num_events,
3485 int32_t timeout);
3486
3487/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003488 * @brief Initialize a poll signal object.
3489 *
3490 * Ready a poll signal object to be signaled via k_poll_signal().
3491 *
3492 * @param signal A poll signal.
3493 *
3494 * @return N/A
3495 */
3496
3497extern void k_poll_signal_init(struct k_poll_signal *signal);
3498
3499/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003500 * @brief Signal a poll signal object.
3501 *
3502 * This routine makes ready a poll signal, which is basically a poll event of
3503 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3504 * made ready to run. A @a result value can be specified.
3505 *
3506 * The poll signal contains a 'signaled' field that, when set by
3507 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3508 * be reset by the user before being passed again to k_poll() or k_poll() will
3509 * consider it being signaled, and will return immediately.
3510 *
3511 * @param signal A poll signal.
3512 * @param result The value to store in the result field of the signal.
3513 *
3514 * @retval 0 The signal was delivered successfully.
3515 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3516 */
3517
3518extern int k_poll_signal(struct k_poll_signal *signal, int result);
3519
3520/* private internal function */
3521extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
3522 uint32_t state);
3523
3524/**
3525 * @} end defgroup poll_apis
3526 */
3527
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003528/**
3529 * @brief Make the CPU idle.
3530 *
3531 * This function makes the CPU idle until an event wakes it up.
3532 *
3533 * In a regular system, the idle thread should be the only thread responsible
3534 * for making the CPU idle and triggering any type of power management.
3535 * However, in some more constrained systems, such as a single-threaded system,
3536 * the only thread would be responsible for this if needed.
3537 *
3538 * @return N/A
3539 */
3540extern void k_cpu_idle(void);
3541
3542/**
3543 * @brief Make the CPU idle in an atomic fashion.
3544 *
3545 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3546 * must be done atomically before making the CPU idle.
3547 *
3548 * @param key Interrupt locking key obtained from irq_lock().
3549 *
3550 * @return N/A
3551 */
3552extern void k_cpu_atomic_idle(unsigned int key);
3553
Andrew Boie350f88d2017-01-18 13:13:45 -08003554extern void _sys_power_save_idle_exit(int32_t ticks);
3555
Anas Nashifa6149502017-01-17 07:47:31 -05003556/* Include legacy APIs */
3557#if defined(CONFIG_LEGACY_KERNEL)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558#include <legacy.h>
Anas Nashifa6149502017-01-17 07:47:31 -05003559#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003560#include <arch/cpu.h>
3561
3562/*
3563 * private APIs that are utilized by one or more public APIs
3564 */
3565
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003566#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003568#else
3569#define _init_static_threads() do { } while ((0))
3570#endif
3571
3572extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003573extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574
3575#ifdef __cplusplus
3576}
3577#endif
3578
Andrew Boiee004dec2016-11-07 09:01:19 -08003579#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3580/*
3581 * Define new and delete operators.
3582 * At this moment, the operators do nothing since objects are supposed
3583 * to be statically allocated.
3584 */
3585inline void operator delete(void *ptr)
3586{
3587 (void)ptr;
3588}
3589
3590inline void operator delete[](void *ptr)
3591{
3592 (void)ptr;
3593}
3594
3595inline void *operator new(size_t size)
3596{
3597 (void)size;
3598 return NULL;
3599}
3600
3601inline void *operator new[](size_t size)
3602{
3603 (void)size;
3604 return NULL;
3605}
3606
3607/* Placement versions of operator new and delete */
3608inline void operator delete(void *ptr1, void *ptr2)
3609{
3610 (void)ptr1;
3611 (void)ptr2;
3612}
3613
3614inline void operator delete[](void *ptr1, void *ptr2)
3615{
3616 (void)ptr1;
3617 (void)ptr2;
3618}
3619
3620inline void *operator new(size_t size, void *ptr)
3621{
3622 (void)size;
3623 return ptr;
3624}
3625
3626inline void *operator new[](size_t size, void *ptr)
3627{
3628 (void)size;
3629 return ptr;
3630}
3631
3632#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3633
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003634#endif /* !_ASMLANGUAGE */
3635
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003636#endif /* _kernel__h_ */