blob: 30c1c8fcfb9e78f485dbedef404e0c2e4f143f31 [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>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
21#include <sections.h>
22#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040030
31#ifdef __cplusplus
32extern "C" {
33#endif
34
Anas Nashifbbb157d2017-01-15 08:46:31 -050035/**
36 * @brief Kernel APIs
37 * @defgroup kernel_apis Kernel APIs
38 * @{
39 * @}
40 */
41
Anas Nashif61f4b242016-11-18 10:53:59 -050042#ifdef CONFIG_KERNEL_DEBUG
43#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040044#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
45#else
46#define K_DEBUG(fmt, ...)
47#endif
48
Benjamin Walsh2f280412017-01-14 19:23:46 -050049#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
50#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
51#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
52#elif defined(CONFIG_COOP_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
54#define _NUM_PREEMPT_PRIO (0)
55#elif defined(CONFIG_PREEMPT_ENABLED)
56#define _NUM_COOP_PRIO (0)
57#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
58#else
59#error "invalid configuration"
60#endif
61
62#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_PRIO_PREEMPT(x) (x)
64
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_ANY NULL
66#define K_END NULL
67
Benjamin Walshedb35702017-01-14 18:47:22 -050068#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050070#elif defined(CONFIG_COOP_ENABLED)
71#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
72#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050074#else
75#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#endif
77
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050078#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
80#else
81#define K_LOWEST_THREAD_PRIO -1
82#endif
83
Benjamin Walshfab8d922016-11-08 15:36:36 -050084#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
85
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
87#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
88
89typedef sys_dlist_t _wait_q_t;
90
Anas Nashif2f203c22016-12-18 06:57:45 -050091#ifdef CONFIG_OBJECT_TRACING
92#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
93#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094#else
Anas Nashif2f203c22016-12-18 06:57:45 -050095#define _OBJECT_TRACING_INIT
96#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#endif
98
Benjamin Walshacc68c12017-01-29 18:57:45 -050099#ifdef CONFIG_POLL
100#define _POLL_EVENT_OBJ_INIT \
101 .poll_event = NULL,
102#define _POLL_EVENT struct k_poll_event *poll_event
103#else
104#define _POLL_EVENT_OBJ_INIT
105#define _POLL_EVENT
106#endif
107
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500108#define tcs k_thread
109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400126typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128enum execution_context_types {
129 K_ISR = 0,
130 K_COOP_THREAD,
131 K_PREEMPT_THREAD,
132};
133
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100135 * @defgroup profiling_apis Profiling APIs
136 * @ingroup kernel_apis
137 * @{
138 */
139
140/**
141 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
142 *
143 * This routine calls @ref stack_analyze on the 4 call stacks declared and
144 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
145 *
146 * CONFIG_MAIN_STACK_SIZE
147 * CONFIG_IDLE_STACK_SIZE
148 * CONFIG_ISR_STACK_SIZE
149 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
150 *
151 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
152 * produce output.
153 *
154 * @return N/A
155 */
156extern void k_call_stacks_analyze(void);
157
158/**
159 * @} end defgroup profiling_apis
160 */
161
162/**
Allan Stephensc98da842016-11-11 15:45:03 -0500163 * @defgroup thread_apis Thread APIs
164 * @ingroup kernel_apis
165 * @{
166 */
167
168/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500169 * @typedef k_thread_entry_t
170 * @brief Thread entry point function type.
171 *
172 * A thread's entry point function is invoked when the thread starts executing.
173 * Up to 3 argument values can be passed to the function.
174 *
175 * The thread terminates execution permanently if the entry point function
176 * returns. The thread is responsible for releasing any shared resources
177 * it may own (such as mutexes and dynamically allocated memory), prior to
178 * returning.
179 *
180 * @param p1 First argument.
181 * @param p2 Second argument.
182 * @param p3 Third argument.
183 *
184 * @return N/A
185 */
186typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
187
Benjamin Walshed240f22017-01-22 13:05:08 -0500188#endif /* !_ASMLANGUAGE */
189
190
191/*
192 * Thread user options. May be needed by assembly code. Common part uses low
193 * bits, arch-specific use high bits.
194 */
195
196/* system thread that must not abort */
197#define K_ESSENTIAL (1 << 0)
198
199#if defined(CONFIG_FP_SHARING)
200/* thread uses floating point registers */
201#define K_FP_REGS (1 << 1)
202#endif
203
204#ifdef CONFIG_X86
205/* x86 Bitmask definitions for threads user options */
206
207#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
208/* thread uses SSEx (and also FP) registers */
209#define K_SSE_REGS (1 << 7)
210#endif
211#endif
212
213/* end - thread options */
214
215#if !defined(_ASMLANGUAGE)
216
Allan Stephens5eceb852016-11-16 10:16:30 -0500217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500218 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500222 * The new thread may be scheduled for immediate execution or a delayed start.
223 * If the newly spawned thread does not have a delayed start the kernel
224 * scheduler may preempt the current thread to allow the new thread to
225 * execute.
226 *
227 * Thread options are architecture-specific, and can include K_ESSENTIAL,
228 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
229 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 *
231 * @param stack Pointer to the stack space.
232 * @param stack_size Stack size in bytes.
233 * @param entry Thread entry function.
234 * @param p1 1st entry point parameter.
235 * @param p2 2nd entry point parameter.
236 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @param prio Thread priority.
238 * @param options Thread options.
239 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500241 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400242 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500243extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500244 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500246 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400247
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500249 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400250 *
Allan Stephensc98da842016-11-11 15:45:03 -0500251 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500252 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500254 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400255 *
256 * @return N/A
257 */
Kumar Galacc334c72017-04-21 10:55:34 -0500258extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400259
260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500261 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400262 *
263 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500264 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400266 * @return N/A
267 */
Kumar Galacc334c72017-04-21 10:55:34 -0500268extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400269
270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500271 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500273 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400274 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500275 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400276 *
277 * @return N/A
278 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400279extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400280
281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500282 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500284 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500286 * If @a thread is not currently sleeping, the routine has no effect.
287 *
288 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400289 *
290 * @return N/A
291 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400292extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400293
294/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500295 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500297 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400298 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400299extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400300
301/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500302 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500304 * This routine prevents @a thread from executing if it has not yet started
305 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500307 * @param thread ID of thread to cancel.
308 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700309 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500310 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400311 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400312extern int k_thread_cancel(k_tid_t thread);
313
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400314/**
Allan Stephensc98da842016-11-11 15:45:03 -0500315 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500317 * This routine permanently stops execution of @a thread. The thread is taken
318 * off all kernel queues it is part of (i.e. the ready queue, the timeout
319 * queue, or a kernel object wait queue). However, any kernel resources the
320 * thread might currently own (such as mutexes or memory blocks) are not
321 * released. It is the responsibility of the caller of this routine to ensure
322 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500324 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400325 *
326 * @return N/A
327 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400328extern void k_thread_abort(k_tid_t thread);
329
Allan Stephensc98da842016-11-11 15:45:03 -0500330/**
331 * @cond INTERNAL_HIDDEN
332 */
333
Benjamin Walshd211a522016-12-06 11:44:01 -0500334/* timeout has timed out and is not on _timeout_q anymore */
335#define _EXPIRED (-2)
336
337/* timeout is not in use */
338#define _INACTIVE (-1)
339
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400340struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400341 union {
342 char *init_stack;
343 struct k_thread *thread;
344 };
345 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500346 void (*init_entry)(void *, void *, void *);
347 void *init_p1;
348 void *init_p2;
349 void *init_p3;
350 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500351 u32_t init_options;
352 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500353 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500354 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400355};
356
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400357#define _THREAD_INITIALIZER(stack, stack_size, \
358 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500359 prio, options, delay, abort, groups) \
360 { \
Mazen NEIFER967cb2e2017-02-02 10:42:18 +0100361 {.init_stack = (stack)}, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500362 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400363 .init_entry = (void (*)(void *, void *, void *))entry, \
364 .init_p1 = (void *)p1, \
365 .init_p2 = (void *)p2, \
366 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500367 .init_prio = (prio), \
368 .init_options = (options), \
369 .init_delay = (delay), \
370 .init_abort = (abort), \
371 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400372 }
373
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400374/**
Allan Stephensc98da842016-11-11 15:45:03 -0500375 * INTERNAL_HIDDEN @endcond
376 */
377
378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500379 * @brief Statically define and initialize a thread.
380 *
381 * The thread may be scheduled for immediate execution or a delayed start.
382 *
383 * Thread options are architecture-specific, and can include K_ESSENTIAL,
384 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
385 * them using "|" (the logical OR operator).
386 *
387 * The ID of the thread can be accessed using:
388 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500389 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500390 *
391 * @param name Name of the thread.
392 * @param stack_size Stack size in bytes.
393 * @param entry Thread entry function.
394 * @param p1 1st entry point parameter.
395 * @param p2 2nd entry point parameter.
396 * @param p3 3rd entry point parameter.
397 * @param prio Thread priority.
398 * @param options Thread options.
399 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400400 *
401 * @internal It has been observed that the x86 compiler by default aligns
402 * these _static_thread_data structures to 32-byte boundaries, thereby
403 * wasting space. To work around this, force a 4-byte alignment.
404 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500405#define K_THREAD_DEFINE(name, stack_size, \
406 entry, p1, p2, p3, \
407 prio, options, delay) \
408 char __noinit __stack _k_thread_obj_##name[stack_size]; \
409 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500410 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500411 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
412 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500413 NULL, 0); \
414 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400415
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500417 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500419 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500421 * @param thread ID of thread whose priority is needed.
422 *
423 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400424 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500425extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400426
427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500428 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500430 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431 *
432 * Rescheduling can occur immediately depending on the priority @a thread is
433 * set to:
434 *
435 * - If its priority is raised above the priority of the caller of this
436 * function, and the caller is preemptible, @a thread will be scheduled in.
437 *
438 * - If the caller operates on itself, it lowers its priority below that of
439 * other threads in the system, and the caller is preemptible, the thread of
440 * highest priority will be scheduled in.
441 *
442 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
443 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
444 * highest priority.
445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 * @param prio New priority.
448 *
449 * @warning Changing the priority of a thread currently involved in mutex
450 * priority inheritance may result in undefined behavior.
451 *
452 * @return N/A
453 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400454extern void k_thread_priority_set(k_tid_t thread, int prio);
455
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * This routine prevents the kernel scheduler from making @a thread the
460 * current thread. All other internal operations on @a thread are still
461 * performed; for example, any timeout it is waiting on keeps ticking,
462 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * If @a thread is already suspended, the routine has no effect.
465 *
466 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 *
468 * @return N/A
469 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400470extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471
472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500473 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500475 * This routine allows the kernel scheduler to make @a thread the current
476 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500478 * If @a thread is not currently suspended, the routine has no effect.
479 *
480 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400481 *
482 * @return N/A
483 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400484extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400485
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500487 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * This routine specifies how the scheduler will perform time slicing of
490 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500492 * To enable time slicing, @a slice must be non-zero. The scheduler
493 * ensures that no thread runs for more than the specified time limit
494 * before other threads of that priority are given a chance to execute.
495 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700496 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500498 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400499 * execute. Once the scheduler selects a thread for execution, there is no
500 * minimum guaranteed time the thread will execute before threads of greater or
501 * equal priority are scheduled.
502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500503 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400504 * for execution, this routine has no effect; the thread is immediately
505 * rescheduled after the slice period expires.
506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * To disable timeslicing, set both @a slice and @a prio to zero.
508 *
509 * @param slice Maximum time slice length (in milliseconds).
510 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400511 *
512 * @return N/A
513 */
Kumar Galacc334c72017-04-21 10:55:34 -0500514extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400516/**
Allan Stephensc98da842016-11-11 15:45:03 -0500517 * @} end defgroup thread_apis
518 */
519
520/**
521 * @addtogroup isr_apis
522 * @{
523 */
524
525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500526 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400527 *
Allan Stephensc98da842016-11-11 15:45:03 -0500528 * This routine allows the caller to customize its actions, depending on
529 * whether it is a thread or an ISR.
530 *
531 * @note Can be called by ISRs.
532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500533 * @return 0 if invoked by a thread.
534 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400535 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500536extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400537
Benjamin Walsh445830d2016-11-10 15:54:27 -0500538/**
539 * @brief Determine if code is running in a preemptible thread.
540 *
Allan Stephensc98da842016-11-11 15:45:03 -0500541 * This routine allows the caller to customize its actions, depending on
542 * whether it can be preempted by another thread. The routine returns a 'true'
543 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500544 *
Allan Stephensc98da842016-11-11 15:45:03 -0500545 * - The code is running in a thread, not at ISR.
546 * - The thread's priority is in the preemptible range.
547 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500548 *
Allan Stephensc98da842016-11-11 15:45:03 -0500549 * @note Can be called by ISRs.
550 *
551 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500552 * @return Non-zero if invoked by a preemptible thread.
553 */
554extern int k_is_preempt_thread(void);
555
Allan Stephensc98da842016-11-11 15:45:03 -0500556/**
557 * @} end addtogroup isr_apis
558 */
559
560/**
561 * @addtogroup thread_apis
562 * @{
563 */
564
565/**
566 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500567 *
Allan Stephensc98da842016-11-11 15:45:03 -0500568 * This routine prevents the current thread from being preempted by another
569 * thread by instructing the scheduler to treat it as a cooperative thread.
570 * If the thread subsequently performs an operation that makes it unready,
571 * it will be context switched out in the normal manner. When the thread
572 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500573 *
Allan Stephensc98da842016-11-11 15:45:03 -0500574 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500575 *
Allan Stephensc98da842016-11-11 15:45:03 -0500576 * @note k_sched_lock() and k_sched_unlock() should normally be used
577 * when the operation being performed can be safely interrupted by ISRs.
578 * However, if the amount of processing involved is very small, better
579 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500580 *
581 * @return N/A
582 */
583extern void k_sched_lock(void);
584
Allan Stephensc98da842016-11-11 15:45:03 -0500585/**
586 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500587 *
Allan Stephensc98da842016-11-11 15:45:03 -0500588 * This routine reverses the effect of a previous call to k_sched_lock().
589 * A thread must call the routine once for each time it called k_sched_lock()
590 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500591 *
592 * @return N/A
593 */
594extern void k_sched_unlock(void);
595
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500599 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500601 * Custom data is not used by the kernel itself, and is freely available
602 * for a thread to use as it sees fit. It can be used as a framework
603 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
607 * @return N/A
608 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400609extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610
611/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500612 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500614 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500616 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400617 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400618extern void *k_thread_custom_data_get(void);
619
620/**
Allan Stephensc98da842016-11-11 15:45:03 -0500621 * @} end addtogroup thread_apis
622 */
623
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400624#include <sys_clock.h>
625
Allan Stephensc2f15a42016-11-17 12:24:22 -0500626/**
627 * @addtogroup clock_apis
628 * @{
629 */
630
631/**
632 * @brief Generate null timeout delay.
633 *
634 * This macro generates a timeout delay that that instructs a kernel API
635 * not to wait if the requested operation cannot be performed immediately.
636 *
637 * @return Timeout delay value.
638 */
639#define K_NO_WAIT 0
640
641/**
642 * @brief Generate timeout delay from milliseconds.
643 *
644 * This macro generates a timeout delay that that instructs a kernel API
645 * to wait up to @a ms milliseconds to perform the requested operation.
646 *
647 * @param ms Duration in milliseconds.
648 *
649 * @return Timeout delay value.
650 */
Johan Hedberg14471692016-11-13 10:52:15 +0200651#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500652
653/**
654 * @brief Generate timeout delay from seconds.
655 *
656 * This macro generates a timeout delay that that instructs a kernel API
657 * to wait up to @a s seconds to perform the requested operation.
658 *
659 * @param s Duration in seconds.
660 *
661 * @return Timeout delay value.
662 */
Johan Hedberg14471692016-11-13 10:52:15 +0200663#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500664
665/**
666 * @brief Generate timeout delay from minutes.
667 *
668 * This macro generates a timeout delay that that instructs a kernel API
669 * to wait up to @a m minutes to perform the requested operation.
670 *
671 * @param m Duration in minutes.
672 *
673 * @return Timeout delay value.
674 */
Johan Hedberg14471692016-11-13 10:52:15 +0200675#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500676
677/**
678 * @brief Generate timeout delay from hours.
679 *
680 * This macro generates a timeout delay that that instructs a kernel API
681 * to wait up to @a h hours to perform the requested operation.
682 *
683 * @param h Duration in hours.
684 *
685 * @return Timeout delay value.
686 */
Johan Hedberg14471692016-11-13 10:52:15 +0200687#define K_HOURS(h) K_MINUTES((h) * 60)
688
Allan Stephensc98da842016-11-11 15:45:03 -0500689/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500690 * @brief Generate infinite timeout delay.
691 *
692 * This macro generates a timeout delay that that instructs a kernel API
693 * to wait as long as necessary to perform the requested operation.
694 *
695 * @return Timeout delay value.
696 */
697#define K_FOREVER (-1)
698
699/**
700 * @} end addtogroup clock_apis
701 */
702
703/**
Allan Stephensc98da842016-11-11 15:45:03 -0500704 * @cond INTERNAL_HIDDEN
705 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400706
Benjamin Walsh62092182016-12-20 14:39:08 -0500707/* kernel clocks */
708
709#if (sys_clock_ticks_per_sec == 1000) || \
710 (sys_clock_ticks_per_sec == 500) || \
711 (sys_clock_ticks_per_sec == 250) || \
712 (sys_clock_ticks_per_sec == 125) || \
713 (sys_clock_ticks_per_sec == 100) || \
714 (sys_clock_ticks_per_sec == 50) || \
715 (sys_clock_ticks_per_sec == 25) || \
716 (sys_clock_ticks_per_sec == 20) || \
717 (sys_clock_ticks_per_sec == 10) || \
718 (sys_clock_ticks_per_sec == 1)
719
720 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
721#else
722 /* yields horrible 64-bit math on many architectures: try to avoid */
723 #define _NON_OPTIMIZED_TICKS_PER_SEC
724#endif
725
726#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500727extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500728#else
Kumar Galacc334c72017-04-21 10:55:34 -0500729static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500730{
Kumar Galacc334c72017-04-21 10:55:34 -0500731 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500732}
733#endif
734
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500735/* added tick needed to account for tick in progress */
736#define _TICK_ALIGN 1
737
Kumar Galacc334c72017-04-21 10:55:34 -0500738static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400739{
Benjamin Walsh62092182016-12-20 14:39:08 -0500740#ifdef CONFIG_SYS_CLOCK_EXISTS
741
742#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500743 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400744#else
Kumar Galacc334c72017-04-21 10:55:34 -0500745 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500746#endif
747
748#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400749 __ASSERT(ticks == 0, "");
750 return 0;
751#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400752}
753
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400754/* timeouts */
755
756struct _timeout;
757typedef void (*_timeout_func_t)(struct _timeout *t);
758
759struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500760 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400761 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400762 sys_dlist_t *wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -0500763 s32_t delta_ticks_from_prev;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764 _timeout_func_t func;
765};
766
Kumar Galacc334c72017-04-21 10:55:34 -0500767extern s32_t _timeout_remaining_get(struct _timeout *timeout);
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200768
Allan Stephensc98da842016-11-11 15:45:03 -0500769/**
770 * INTERNAL_HIDDEN @endcond
771 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500772
Allan Stephensc98da842016-11-11 15:45:03 -0500773/**
774 * @cond INTERNAL_HIDDEN
775 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400776
777struct k_timer {
778 /*
779 * _timeout structure must be first here if we want to use
780 * dynamic timer allocation. timeout.node is used in the double-linked
781 * list of free timers
782 */
783 struct _timeout timeout;
784
Allan Stephens45bfa372016-10-12 12:39:42 -0500785 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400786 _wait_q_t wait_q;
787
788 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500789 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400790
791 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500792 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400793
794 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500795 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400796
Allan Stephens45bfa372016-10-12 12:39:42 -0500797 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500798 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400799
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500800 /* user-specific data, also used to support legacy features */
801 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400802
Anas Nashif2f203c22016-12-18 06:57:45 -0500803 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400804};
805
Allan Stephens1342adb2016-11-03 13:54:53 -0500806#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400807 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500808 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500809 .timeout.wait_q = NULL, \
810 .timeout.thread = NULL, \
811 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400812 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500813 .expiry_fn = expiry, \
814 .stop_fn = stop, \
815 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500816 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500817 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400818 }
819
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820/**
Allan Stephensc98da842016-11-11 15:45:03 -0500821 * INTERNAL_HIDDEN @endcond
822 */
823
824/**
825 * @defgroup timer_apis Timer APIs
826 * @ingroup kernel_apis
827 * @{
828 */
829
830/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500831 * @typedef k_timer_expiry_t
832 * @brief Timer expiry function type.
833 *
834 * A timer's expiry function is executed by the system clock interrupt handler
835 * each time the timer expires. The expiry function is optional, and is only
836 * invoked if the timer has been initialized with one.
837 *
838 * @param timer Address of timer.
839 *
840 * @return N/A
841 */
842typedef void (*k_timer_expiry_t)(struct k_timer *timer);
843
844/**
845 * @typedef k_timer_stop_t
846 * @brief Timer stop function type.
847 *
848 * A timer's stop function is executed if the timer is stopped prematurely.
849 * The function runs in the context of the thread that stops the timer.
850 * The stop function is optional, and is only invoked if the timer has been
851 * initialized with one.
852 *
853 * @param timer Address of timer.
854 *
855 * @return N/A
856 */
857typedef void (*k_timer_stop_t)(struct k_timer *timer);
858
859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500864 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
866 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @param expiry_fn Function to invoke each time the timer expires.
868 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500870#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500871 struct k_timer name \
872 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500873 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874
Allan Stephens45bfa372016-10-12 12:39:42 -0500875/**
876 * @brief Initialize a timer.
877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500879 *
880 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @param expiry_fn Function to invoke each time the timer expires.
882 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500883 *
884 * @return N/A
885 */
886extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500887 k_timer_expiry_t expiry_fn,
888 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700889
Allan Stephens45bfa372016-10-12 12:39:42 -0500890/**
891 * @brief Start a timer.
892 *
893 * This routine starts a timer, and resets its status to zero. The timer
894 * begins counting down using the specified duration and period values.
895 *
896 * Attempting to start a timer that is already running is permitted.
897 * The timer's status is reset to zero and the timer begins counting down
898 * using the new duration and period values.
899 *
900 * @param timer Address of timer.
901 * @param duration Initial timer duration (in milliseconds).
902 * @param period Timer period (in milliseconds).
903 *
904 * @return N/A
905 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400906extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -0500907 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -0500908
909/**
910 * @brief Stop a timer.
911 *
912 * This routine stops a running timer prematurely. The timer's stop function,
913 * if one exists, is invoked by the caller.
914 *
915 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500917 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -0500918 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
919 * if @a k_timer_stop is to be called from ISRs.
920 *
Allan Stephens45bfa372016-10-12 12:39:42 -0500921 * @param timer Address of timer.
922 *
923 * @return N/A
924 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500926
927/**
928 * @brief Read timer status.
929 *
930 * This routine reads the timer's status, which indicates the number of times
931 * it has expired since its status was last read.
932 *
933 * Calling this routine resets the timer's status to zero.
934 *
935 * @param timer Address of timer.
936 *
937 * @return Timer status.
938 */
Kumar Galacc334c72017-04-21 10:55:34 -0500939extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500940
941/**
942 * @brief Synchronize thread to timer expiration.
943 *
944 * This routine blocks the calling thread until the timer's status is non-zero
945 * (indicating that it has expired at least once since it was last examined)
946 * or the timer is stopped. If the timer status is already non-zero,
947 * or the timer is already stopped, the caller continues without waiting.
948 *
949 * Calling this routine resets the timer's status to zero.
950 *
951 * This routine must not be used by interrupt handlers, since they are not
952 * allowed to block.
953 *
954 * @param timer Address of timer.
955 *
956 * @return Timer status.
957 */
Kumar Galacc334c72017-04-21 10:55:34 -0500958extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500959
960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500962 *
963 * This routine computes the (approximate) time remaining before a running
964 * timer next expires. If the timer is not running, it returns zero.
965 *
966 * @param timer Address of timer.
967 *
968 * @return Remaining time (in milliseconds).
969 */
Kumar Galacc334c72017-04-21 10:55:34 -0500970static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200971{
972 return _timeout_remaining_get(&timer->timeout);
973}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400974
Allan Stephensc98da842016-11-11 15:45:03 -0500975/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500976 * @brief Associate user-specific data with a timer.
977 *
978 * This routine records the @a user_data with the @a timer, to be retrieved
979 * later.
980 *
981 * It can be used e.g. in a timer handler shared across multiple subsystems to
982 * retrieve data specific to the subsystem this timer is associated with.
983 *
984 * @param timer Address of timer.
985 * @param user_data User data to associate with the timer.
986 *
987 * @return N/A
988 */
989static inline void k_timer_user_data_set(struct k_timer *timer,
990 void *user_data)
991{
992 timer->user_data = user_data;
993}
994
995/**
996 * @brief Retrieve the user-specific data from a timer.
997 *
998 * @param timer Address of timer.
999 *
1000 * @return The user data.
1001 */
1002static inline void *k_timer_user_data_get(struct k_timer *timer)
1003{
1004 return timer->user_data;
1005}
1006
1007/**
Allan Stephensc98da842016-11-11 15:45:03 -05001008 * @} end defgroup timer_apis
1009 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001010
Allan Stephensc98da842016-11-11 15:45:03 -05001011/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001012 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001013 * @{
1014 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001015
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * This routine returns the elapsed time since the system booted,
1020 * in milliseconds.
1021 *
1022 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001023 */
Kumar Galacc334c72017-04-21 10:55:34 -05001024extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001025
1026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001027 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * This routine returns the lower 32-bits of the elapsed time since the system
1030 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001032 * This routine can be more efficient than k_uptime_get(), as it reduces the
1033 * need for interrupt locking and 64-bit math. However, the 32-bit result
1034 * cannot hold a system uptime time larger than approximately 50 days, so the
1035 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001038 */
Kumar Galacc334c72017-04-21 10:55:34 -05001039extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001040
1041/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * This routine computes the elapsed time between the current system uptime
1045 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * @param reftime Pointer to a reference time, which is updated to the current
1048 * uptime upon return.
1049 *
1050 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001051 */
Kumar Galacc334c72017-04-21 10:55:34 -05001052extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001053
1054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001057 * This routine computes the elapsed time between the current system uptime
1058 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1061 * need for interrupt locking and 64-bit math. However, the 32-bit result
1062 * cannot hold an elapsed time larger than approximately 50 days, so the
1063 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * @param reftime Pointer to a reference time, which is updated to the current
1066 * uptime upon return.
1067 *
1068 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001069 */
Kumar Galacc334c72017-04-21 10:55:34 -05001070extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001071
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * This routine returns the current time, as measured by the system's hardware
1076 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001079 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001080#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001081
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001083 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001084 */
1085
Allan Stephensc98da842016-11-11 15:45:03 -05001086/**
1087 * @cond INTERNAL_HIDDEN
1088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001089
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001090struct k_queue {
1091 _wait_q_t wait_q;
1092 sys_slist_t data_q;
1093 _POLL_EVENT;
1094
1095 _OBJECT_TRACING_NEXT_PTR(k_queue);
1096};
1097
1098#define K_QUEUE_INITIALIZER(obj) \
1099 { \
1100 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1101 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1102 _POLL_EVENT_OBJ_INIT \
1103 _OBJECT_TRACING_INIT \
1104 }
1105
1106/**
1107 * INTERNAL_HIDDEN @endcond
1108 */
1109
1110/**
1111 * @defgroup queue_apis Queue APIs
1112 * @ingroup kernel_apis
1113 * @{
1114 */
1115
1116/**
1117 * @brief Initialize a queue.
1118 *
1119 * This routine initializes a queue object, prior to its first use.
1120 *
1121 * @param queue Address of the queue.
1122 *
1123 * @return N/A
1124 */
1125extern void k_queue_init(struct k_queue *queue);
1126
1127/**
1128 * @brief Append an element to the end of a queue.
1129 *
1130 * This routine appends a data item to @a queue. A queue data item must be
1131 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1132 * reserved for the kernel's use.
1133 *
1134 * @note Can be called by ISRs.
1135 *
1136 * @param queue Address of the queue.
1137 * @param data Address of the data item.
1138 *
1139 * @return N/A
1140 */
1141extern void k_queue_append(struct k_queue *queue, void *data);
1142
1143/**
1144 * @brief Prepend an element to a queue.
1145 *
1146 * This routine prepends a data item to @a queue. A queue data item must be
1147 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1148 * reserved for the kernel's use.
1149 *
1150 * @note Can be called by ISRs.
1151 *
1152 * @param queue Address of the queue.
1153 * @param data Address of the data item.
1154 *
1155 * @return N/A
1156 */
1157extern void k_queue_prepend(struct k_queue *queue, void *data);
1158
1159/**
1160 * @brief Inserts an element to a queue.
1161 *
1162 * This routine inserts a data item to @a queue after previous item. A queue
1163 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1164 * item are reserved for the kernel's use.
1165 *
1166 * @note Can be called by ISRs.
1167 *
1168 * @param queue Address of the queue.
1169 * @param prev Address of the previous data item.
1170 * @param data Address of the data item.
1171 *
1172 * @return N/A
1173 */
1174extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1175
1176/**
1177 * @brief Atomically append a list of elements to a queue.
1178 *
1179 * This routine adds a list of data items to @a queue in one operation.
1180 * The data items must be in a singly-linked list, with the first 32 bits
1181 * in each data item pointing to the next data item; the list must be
1182 * NULL-terminated.
1183 *
1184 * @note Can be called by ISRs.
1185 *
1186 * @param queue Address of the queue.
1187 * @param head Pointer to first node in singly-linked list.
1188 * @param tail Pointer to last node in singly-linked list.
1189 *
1190 * @return N/A
1191 */
1192extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1193
1194/**
1195 * @brief Atomically add a list of elements to a queue.
1196 *
1197 * This routine adds a list of data items to @a queue in one operation.
1198 * The data items must be in a singly-linked list implemented using a
1199 * sys_slist_t object. Upon completion, the original list is empty.
1200 *
1201 * @note Can be called by ISRs.
1202 *
1203 * @param queue Address of the queue.
1204 * @param list Pointer to sys_slist_t object.
1205 *
1206 * @return N/A
1207 */
1208extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1209
1210/**
1211 * @brief Get an element from a queue.
1212 *
1213 * This routine removes first data item from @a queue. The first 32 bits of the
1214 * data item are reserved for the kernel's use.
1215 *
1216 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1217 *
1218 * @param queue Address of the queue.
1219 * @param timeout Waiting period to obtain a data item (in milliseconds),
1220 * or one of the special values K_NO_WAIT and K_FOREVER.
1221 *
1222 * @return Address of the data item if successful; NULL if returned
1223 * without waiting, or waiting period timed out.
1224 */
Kumar Galacc334c72017-04-21 10:55:34 -05001225extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001226
1227/**
1228 * @brief Query a queue to see if it has data available.
1229 *
1230 * Note that the data might be already gone by the time this function returns
1231 * if other threads are also trying to read from the queue.
1232 *
1233 * @note Can be called by ISRs.
1234 *
1235 * @param queue Address of the queue.
1236 *
1237 * @return Non-zero if the queue is empty.
1238 * @return 0 if data is available.
1239 */
1240static inline int k_queue_is_empty(struct k_queue *queue)
1241{
1242 return (int)sys_slist_is_empty(&queue->data_q);
1243}
1244
1245/**
1246 * @brief Statically define and initialize a queue.
1247 *
1248 * The queue can be accessed outside the module where it is defined using:
1249 *
1250 * @code extern struct k_queue <name>; @endcode
1251 *
1252 * @param name Name of the queue.
1253 */
1254#define K_QUEUE_DEFINE(name) \
1255 struct k_queue name \
1256 __in_section(_k_queue, static, name) = \
1257 K_QUEUE_INITIALIZER(name)
1258
1259/**
1260 * @} end defgroup queue_apis
1261 */
1262
1263/**
1264 * @cond INTERNAL_HIDDEN
1265 */
1266
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001267struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001268 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001269};
1270
Allan Stephensc98da842016-11-11 15:45:03 -05001271#define K_FIFO_INITIALIZER(obj) \
1272 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001273 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001274 }
1275
1276/**
1277 * INTERNAL_HIDDEN @endcond
1278 */
1279
1280/**
1281 * @defgroup fifo_apis Fifo APIs
1282 * @ingroup kernel_apis
1283 * @{
1284 */
1285
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001287 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001289 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001291 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292 *
1293 * @return N/A
1294 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001295#define k_fifo_init(fifo) \
1296 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297
1298/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001299 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301 * This routine adds a data item to @a fifo. A fifo data item must be
1302 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1303 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305 * @note Can be called by ISRs.
1306 *
1307 * @param fifo Address of the fifo.
1308 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001309 *
1310 * @return N/A
1311 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001312#define k_fifo_put(fifo, data) \
1313 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001314
1315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001318 * This routine adds a list of data items to @a fifo in one operation.
1319 * The data items must be in a singly-linked list, with the first 32 bits
1320 * each data item pointing to the next data item; the list must be
1321 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001325 * @param fifo Address of the fifo.
1326 * @param head Pointer to first node in singly-linked list.
1327 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001328 *
1329 * @return N/A
1330 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001331#define k_fifo_put_list(fifo, head, tail) \
1332 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001333
1334/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001335 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001337 * This routine adds a list of data items to @a fifo in one operation.
1338 * The data items must be in a singly-linked list implemented using a
1339 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001340 * and must be re-initialized via sys_slist_init().
1341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001342 * @note Can be called by ISRs.
1343 *
1344 * @param fifo Address of the fifo.
1345 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346 *
1347 * @return N/A
1348 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001349#define k_fifo_put_slist(fifo, list) \
1350 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001351
1352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001353 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001355 * This routine removes a data item from @a fifo in a "first in, first out"
1356 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1359 *
1360 * @param fifo Address of the fifo.
1361 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001362 * or one of the special values K_NO_WAIT and K_FOREVER.
1363 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001364 * @return Address of the data item if successful; NULL if returned
1365 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001367#define k_fifo_get(fifo, timeout) \
1368 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001369
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001370/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001371 * @brief Query a fifo to see if it has data available.
1372 *
1373 * Note that the data might be already gone by the time this function returns
1374 * if other threads is also trying to read from the fifo.
1375 *
1376 * @note Can be called by ISRs.
1377 *
1378 * @param fifo Address of the fifo.
1379 *
1380 * @return Non-zero if the fifo is empty.
1381 * @return 0 if data is available.
1382 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001383#define k_fifo_is_empty(fifo) \
1384 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001385
1386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001387 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001391 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001394 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001396 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001397 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001398 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399
Allan Stephensc98da842016-11-11 15:45:03 -05001400/**
1401 * @} end defgroup fifo_apis
1402 */
1403
1404/**
1405 * @cond INTERNAL_HIDDEN
1406 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407
1408struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001409 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410};
1411
Allan Stephensc98da842016-11-11 15:45:03 -05001412#define K_LIFO_INITIALIZER(obj) \
1413 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001414 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001415 }
1416
1417/**
1418 * INTERNAL_HIDDEN @endcond
1419 */
1420
1421/**
1422 * @defgroup lifo_apis Lifo APIs
1423 * @ingroup kernel_apis
1424 * @{
1425 */
1426
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001428 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001430 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001432 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001433 *
1434 * @return N/A
1435 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001436#define k_lifo_init(lifo) \
1437 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001438
1439/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001440 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * This routine adds a data item to @a lifo. A lifo data item must be
1443 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1444 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * @note Can be called by ISRs.
1447 *
1448 * @param lifo Address of the lifo.
1449 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450 *
1451 * @return N/A
1452 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001453#define k_lifo_put(lifo, data) \
1454 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001455
1456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * This routine removes a data item from @a lifo in a "last in, first out"
1460 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1463 *
1464 * @param lifo Address of the lifo.
1465 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001466 * or one of the special values K_NO_WAIT and K_FOREVER.
1467 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001468 * @return Address of the data item if successful; NULL if returned
1469 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001470 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001471#define k_lifo_get(lifo, timeout) \
1472 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001473
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001479 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001483#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001484 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001485 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001486 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001487
Allan Stephensc98da842016-11-11 15:45:03 -05001488/**
1489 * @} end defgroup lifo_apis
1490 */
1491
1492/**
1493 * @cond INTERNAL_HIDDEN
1494 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001495
1496struct k_stack {
1497 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001498 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499
Anas Nashif2f203c22016-12-18 06:57:45 -05001500 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501};
1502
Allan Stephensc98da842016-11-11 15:45:03 -05001503#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1504 { \
1505 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1506 .base = stack_buffer, \
1507 .next = stack_buffer, \
1508 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001509 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001510 }
1511
1512/**
1513 * INTERNAL_HIDDEN @endcond
1514 */
1515
1516/**
1517 * @defgroup stack_apis Stack APIs
1518 * @ingroup kernel_apis
1519 * @{
1520 */
1521
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001522/**
1523 * @brief Initialize a stack.
1524 *
1525 * This routine initializes a stack object, prior to its first use.
1526 *
1527 * @param stack Address of the stack.
1528 * @param buffer Address of array used to hold stacked values.
1529 * @param num_entries Maximum number of values that can be stacked.
1530 *
1531 * @return N/A
1532 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001533extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001534 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535
1536/**
1537 * @brief Push an element onto a stack.
1538 *
1539 * This routine adds a 32-bit value @a data to @a stack.
1540 *
1541 * @note Can be called by ISRs.
1542 *
1543 * @param stack Address of the stack.
1544 * @param data Value to push onto the stack.
1545 *
1546 * @return N/A
1547 */
Kumar Galacc334c72017-04-21 10:55:34 -05001548extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549
1550/**
1551 * @brief Pop an element from a stack.
1552 *
1553 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1554 * manner and stores the value in @a data.
1555 *
1556 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1557 *
1558 * @param stack Address of the stack.
1559 * @param data Address of area to hold the value popped from the stack.
1560 * @param timeout Waiting period to obtain a value (in milliseconds),
1561 * or one of the special values K_NO_WAIT and K_FOREVER.
1562 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001563 * @retval 0 Element popped from stack.
1564 * @retval -EBUSY Returned without waiting.
1565 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001566 */
Kumar Galacc334c72017-04-21 10:55:34 -05001567extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001570 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001572 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001573 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001574 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001576 * @param name Name of the stack.
1577 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001578 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001579#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001580 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001581 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001582 struct k_stack name \
1583 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001584 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1585 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001586
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587/**
Allan Stephensc98da842016-11-11 15:45:03 -05001588 * @} end defgroup stack_apis
1589 */
1590
Allan Stephens6bba9b02016-11-16 14:56:54 -05001591struct k_work;
1592
Allan Stephensc98da842016-11-11 15:45:03 -05001593/**
1594 * @defgroup workqueue_apis Workqueue Thread APIs
1595 * @ingroup kernel_apis
1596 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001597 */
1598
Allan Stephens6bba9b02016-11-16 14:56:54 -05001599/**
1600 * @typedef k_work_handler_t
1601 * @brief Work item handler function type.
1602 *
1603 * A work item's handler function is executed by a workqueue's thread
1604 * when the work item is processed by the workqueue.
1605 *
1606 * @param work Address of the work item.
1607 *
1608 * @return N/A
1609 */
1610typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001611
1612/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001613 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001614 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001615
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001616struct k_work_q {
1617 struct k_fifo fifo;
1618};
1619
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001620enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001621 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001622};
1623
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001624struct k_work {
1625 void *_reserved; /* Used by k_fifo implementation. */
1626 k_work_handler_t handler;
1627 atomic_t flags[1];
1628};
1629
Allan Stephens6bba9b02016-11-16 14:56:54 -05001630struct k_delayed_work {
1631 struct k_work work;
1632 struct _timeout timeout;
1633 struct k_work_q *work_q;
1634};
1635
1636extern struct k_work_q k_sys_work_q;
1637
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001638/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001639 * INTERNAL_HIDDEN @endcond
1640 */
1641
1642/**
1643 * @brief Initialize a statically-defined work item.
1644 *
1645 * This macro can be used to initialize a statically-defined workqueue work
1646 * item, prior to its first use. For example,
1647 *
1648 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1649 *
1650 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651 */
1652#define K_WORK_INITIALIZER(work_handler) \
1653 { \
1654 ._reserved = NULL, \
1655 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001656 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657 }
1658
1659/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001660 * @brief Initialize a work item.
1661 *
1662 * This routine initializes a workqueue work item, prior to its first use.
1663 *
1664 * @param work Address of work item.
1665 * @param handler Function to invoke each time work item is processed.
1666 *
1667 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001668 */
1669static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1670{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001671 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001672 work->handler = handler;
1673}
1674
1675/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001676 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001677 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001678 * This routine submits work item @a work to be processed by workqueue
1679 * @a work_q. If the work item is already pending in the workqueue's queue
1680 * as a result of an earlier submission, this routine has no effect on the
1681 * work item. If the work item has already been processed, or is currently
1682 * being processed, its work is considered complete and the work item can be
1683 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001684 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001685 * @warning
1686 * A submitted work item must not be modified until it has been processed
1687 * by the workqueue.
1688 *
1689 * @note Can be called by ISRs.
1690 *
1691 * @param work_q Address of workqueue.
1692 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001693 *
1694 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001695 */
1696static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1697 struct k_work *work)
1698{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001699 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700 k_fifo_put(&work_q->fifo, work);
1701 }
1702}
1703
1704/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001705 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001706 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001707 * This routine indicates if work item @a work is pending in a workqueue's
1708 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001710 * @note Can be called by ISRs.
1711 *
1712 * @param work Address of work item.
1713 *
1714 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001715 */
1716static inline int k_work_pending(struct k_work *work)
1717{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001718 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001719}
1720
1721/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001722 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001724 * This routine starts workqueue @a work_q. The workqueue spawns its work
1725 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001727 * @param work_q Address of workqueue.
1728 * @param stack Pointer to work queue thread's stack space.
1729 * @param stack_size Size of the work queue thread's stack (in bytes).
1730 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
1732 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001733 */
Allan Stephens904cf972016-10-07 13:59:23 -05001734extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001735 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001736
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001738 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001740 * This routine initializes a workqueue delayed work item, prior to
1741 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001743 * @param work Address of delayed work item.
1744 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745 *
1746 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001748extern void k_delayed_work_init(struct k_delayed_work *work,
1749 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750
1751/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001752 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001753 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001754 * This routine schedules work item @a work to be processed by workqueue
1755 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001756 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001757 * Only when the countdown completes is the work item actually submitted to
1758 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001760 * Submitting a previously submitted delayed work item that is still
1761 * counting down cancels the existing submission and restarts the countdown
1762 * using the new delay. If the work item is currently pending on the
1763 * workqueue's queue because the countdown has completed it is too late to
1764 * resubmit the item, and resubmission fails without impacting the work item.
1765 * If the work item has already been processed, or is currently being processed,
1766 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001767 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001768 * @warning
1769 * A delayed work item must not be modified until it has been processed
1770 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001771 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001772 * @note Can be called by ISRs.
1773 *
1774 * @param work_q Address of workqueue.
1775 * @param work Address of delayed work item.
1776 * @param delay Delay before submitting the work item (in milliseconds).
1777 *
1778 * @retval 0 Work item countdown started.
1779 * @retval -EINPROGRESS Work item is already pending.
1780 * @retval -EINVAL Work item is being processed or has completed its work.
1781 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001782 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001783extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1784 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05001785 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001786
1787/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001788 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001789 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001790 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07001791 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05001792 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001793 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001794 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001796 * @param work Address of delayed work item.
1797 *
David B. Kinder8b986d72017-04-18 15:56:26 -07001798 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001799 * @retval -EINPROGRESS Work item is already pending.
1800 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001801 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001802extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805 * @brief Submit a work item to the system workqueue.
1806 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001807 * This routine submits work item @a work to be processed by the system
1808 * workqueue. If the work item is already pending in the workqueue's queue
1809 * as a result of an earlier submission, this routine has no effect on the
1810 * work item. If the work item has already been processed, or is currently
1811 * being processed, its work is considered complete and the work item can be
1812 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001814 * @warning
1815 * Work items submitted to the system workqueue should avoid using handlers
1816 * that block or yield since this may prevent the system workqueue from
1817 * processing other work items in a timely manner.
1818 *
1819 * @note Can be called by ISRs.
1820 *
1821 * @param work Address of work item.
1822 *
1823 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824 */
1825static inline void k_work_submit(struct k_work *work)
1826{
1827 k_work_submit_to_queue(&k_sys_work_q, work);
1828}
1829
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001830/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831 * @brief Submit a delayed work item to the system workqueue.
1832 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001833 * This routine schedules work item @a work to be processed by the system
1834 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001835 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001836 * Only when the countdown completes is the work item actually submitted to
1837 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001839 * Submitting a previously submitted delayed work item that is still
1840 * counting down cancels the existing submission and restarts the countdown
1841 * using the new delay. If the work item is currently pending on the
1842 * workqueue's queue because the countdown has completed it is too late to
1843 * resubmit the item, and resubmission fails without impacting the work item.
1844 * If the work item has already been processed, or is currently being processed,
1845 * its work is considered complete and the work item can be resubmitted.
1846 *
1847 * @warning
1848 * Work items submitted to the system workqueue should avoid using handlers
1849 * that block or yield since this may prevent the system workqueue from
1850 * processing other work items in a timely manner.
1851 *
1852 * @note Can be called by ISRs.
1853 *
1854 * @param work Address of delayed work item.
1855 * @param delay Delay before submitting the work item (in milliseconds).
1856 *
1857 * @retval 0 Work item countdown started.
1858 * @retval -EINPROGRESS Work item is already pending.
1859 * @retval -EINVAL Work item is being processed or has completed its work.
1860 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001861 */
1862static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05001863 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001865 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866}
1867
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001868/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001869 * @brief Get time remaining before a delayed work gets scheduled.
1870 *
1871 * This routine computes the (approximate) time remaining before a
1872 * delayed work gets executed. If the delayed work is not waiting to be
1873 * schedules, it returns zero.
1874 *
1875 * @param work Delayed work item.
1876 *
1877 * @return Remaining time (in milliseconds).
1878 */
Kumar Galacc334c72017-04-21 10:55:34 -05001879static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02001880{
1881 return _timeout_remaining_get(&work->timeout);
1882}
1883
1884/**
Allan Stephensc98da842016-11-11 15:45:03 -05001885 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001886 */
1887
Allan Stephensc98da842016-11-11 15:45:03 -05001888/**
1889 * @cond INTERNAL_HIDDEN
1890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001891
1892struct k_mutex {
1893 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001894 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05001895 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897
Anas Nashif2f203c22016-12-18 06:57:45 -05001898 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001899};
1900
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901#define K_MUTEX_INITIALIZER(obj) \
1902 { \
1903 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1904 .owner = NULL, \
1905 .lock_count = 0, \
1906 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001907 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001908 }
1909
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001910/**
Allan Stephensc98da842016-11-11 15:45:03 -05001911 * INTERNAL_HIDDEN @endcond
1912 */
1913
1914/**
1915 * @defgroup mutex_apis Mutex APIs
1916 * @ingroup kernel_apis
1917 * @{
1918 */
1919
1920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001925 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001927 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001930 struct k_mutex name \
1931 __in_section(_k_mutex, static, name) = \
1932 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001937 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939 * Upon completion, the mutex is available and does not have an owner.
1940 *
1941 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001942 *
1943 * @return N/A
1944 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001946
1947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001950 * This routine locks @a mutex. If the mutex is locked by another thread,
1951 * the calling thread waits until the mutex becomes available or until
1952 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * A thread is permitted to lock a mutex it has already locked. The operation
1955 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001957 * @param mutex Address of the mutex.
1958 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959 * or one of the special values K_NO_WAIT and K_FOREVER.
1960 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001961 * @retval 0 Mutex locked.
1962 * @retval -EBUSY Returned without waiting.
1963 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 */
Kumar Galacc334c72017-04-21 10:55:34 -05001965extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966
1967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001968 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001970 * This routine unlocks @a mutex. The mutex must already be locked by the
1971 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
1973 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 * thread.
1976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
1979 * @return N/A
1980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981extern void k_mutex_unlock(struct k_mutex *mutex);
1982
Allan Stephensc98da842016-11-11 15:45:03 -05001983/**
1984 * @} end defgroup mutex_apis
1985 */
1986
1987/**
1988 * @cond INTERNAL_HIDDEN
1989 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990
1991struct k_sem {
1992 _wait_q_t wait_q;
1993 unsigned int count;
1994 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05001995 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996
Anas Nashif2f203c22016-12-18 06:57:45 -05001997 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998};
1999
Allan Stephensc98da842016-11-11 15:45:03 -05002000#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2001 { \
2002 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2003 .count = initial_count, \
2004 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002005 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002006 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002007 }
2008
2009/**
2010 * INTERNAL_HIDDEN @endcond
2011 */
2012
2013/**
2014 * @defgroup semaphore_apis Semaphore APIs
2015 * @ingroup kernel_apis
2016 * @{
2017 */
2018
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002024 * @param sem Address of the semaphore.
2025 * @param initial_count Initial semaphore count.
2026 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002027 *
2028 * @return N/A
2029 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2031 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002032
2033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2039 *
2040 * @param sem Address of the semaphore.
2041 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002042 * or one of the special values K_NO_WAIT and K_FOREVER.
2043 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002044 * @note When porting code from the nanokernel legacy API to the new API, be
2045 * careful with the return value of this function. The return value is the
2046 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2047 * non-zero means failure, while the nano_sem_take family returns 1 for success
2048 * and 0 for failure.
2049 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002050 * @retval 0 Semaphore taken.
2051 * @retval -EBUSY Returned without waiting.
2052 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002053 */
Kumar Galacc334c72017-04-21 10:55:34 -05002054extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002055
2056/**
2057 * @brief Give a semaphore.
2058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002059 * This routine gives @a sem, unless the semaphore is already at its maximum
2060 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002062 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002064 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002065 *
2066 * @return N/A
2067 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068extern void k_sem_give(struct k_sem *sem);
2069
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002070/**
2071 * @brief Reset a semaphore's count to zero.
2072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002076 *
2077 * @return N/A
2078 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002079static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002080{
2081 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002082}
2083
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002084/**
2085 * @brief Get a semaphore's count.
2086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002087 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002092 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002093static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094{
2095 return sem->count;
2096}
2097
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002102 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002103 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * @param name Name of the semaphore.
2106 * @param initial_count Initial semaphore count.
2107 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002108 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002110 struct k_sem name \
2111 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112 K_SEM_INITIALIZER(name, initial_count, count_limit)
2113
Allan Stephensc98da842016-11-11 15:45:03 -05002114/**
2115 * @} end defgroup semaphore_apis
2116 */
2117
2118/**
2119 * @defgroup alert_apis Alert APIs
2120 * @ingroup kernel_apis
2121 * @{
2122 */
2123
Allan Stephens5eceb852016-11-16 10:16:30 -05002124/**
2125 * @typedef k_alert_handler_t
2126 * @brief Alert handler function type.
2127 *
2128 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002129 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002130 * and is only invoked if the alert has been initialized with one.
2131 *
2132 * @param alert Address of the alert.
2133 *
2134 * @return 0 if alert has been consumed; non-zero if alert should pend.
2135 */
2136typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002137
2138/**
2139 * @} end defgroup alert_apis
2140 */
2141
2142/**
2143 * @cond INTERNAL_HIDDEN
2144 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002145
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002146#define K_ALERT_DEFAULT NULL
2147#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002149struct k_alert {
2150 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151 atomic_t send_count;
2152 struct k_work work_item;
2153 struct k_sem sem;
2154
Anas Nashif2f203c22016-12-18 06:57:45 -05002155 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156};
2157
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002158extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002160#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002162 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002164 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002165 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002166 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167 }
2168
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169/**
Allan Stephensc98da842016-11-11 15:45:03 -05002170 * INTERNAL_HIDDEN @endcond
2171 */
2172
2173/**
2174 * @addtogroup alert_apis
2175 * @{
2176 */
2177
2178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002180 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002181 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002183 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @param name Name of the alert.
2186 * @param alert_handler Action to take when alert is sent. Specify either
2187 * the address of a function to be invoked by the system workqueue
2188 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2189 * K_ALERT_DEFAULT (which causes the alert to pend).
2190 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002192#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002193 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002194 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002195 K_ALERT_INITIALIZER(name, alert_handler, \
2196 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002201 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002203 * @param alert Address of the alert.
2204 * @param handler Action to take when alert is sent. Specify either the address
2205 * of a function to be invoked by the system workqueue thread,
2206 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2207 * K_ALERT_DEFAULT (which causes the alert to pend).
2208 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002209 *
2210 * @return N/A
2211 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002212extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2213 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214
2215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002216 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002220 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2221 *
2222 * @param alert Address of the alert.
2223 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002224 * or one of the special values K_NO_WAIT and K_FOREVER.
2225 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002226 * @retval 0 Alert received.
2227 * @retval -EBUSY Returned without waiting.
2228 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 */
Kumar Galacc334c72017-04-21 10:55:34 -05002230extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002231
2232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * This routine signals @a alert. The action specified for @a alert will
2236 * be taken, which may trigger the execution of an alert handler function
2237 * and/or cause the alert to pend (assuming the alert has not reached its
2238 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002240 * @note Can be called by ISRs.
2241 *
2242 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243 *
2244 * @return N/A
2245 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002246extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247
2248/**
Allan Stephensc98da842016-11-11 15:45:03 -05002249 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 */
2251
Allan Stephensc98da842016-11-11 15:45:03 -05002252/**
2253 * @cond INTERNAL_HIDDEN
2254 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255
2256struct k_msgq {
2257 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002258 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002259 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260 char *buffer_start;
2261 char *buffer_end;
2262 char *read_ptr;
2263 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002264 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002265
Anas Nashif2f203c22016-12-18 06:57:45 -05002266 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267};
2268
Peter Mitsis1da807e2016-10-06 11:36:59 -04002269#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 { \
2271 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002272 .max_msgs = q_max_msgs, \
2273 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002275 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276 .read_ptr = q_buffer, \
2277 .write_ptr = q_buffer, \
2278 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002279 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280 }
2281
Peter Mitsis1da807e2016-10-06 11:36:59 -04002282/**
Allan Stephensc98da842016-11-11 15:45:03 -05002283 * INTERNAL_HIDDEN @endcond
2284 */
2285
2286/**
2287 * @defgroup msgq_apis Message Queue APIs
2288 * @ingroup kernel_apis
2289 * @{
2290 */
2291
2292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002295 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2296 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002297 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2298 * message is similarly aligned to this boundary, @a q_msg_size must also be
2299 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * The message queue can be accessed outside the module where it is defined
2302 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002304 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002306 * @param q_name Name of the message queue.
2307 * @param q_msg_size Message size (in bytes).
2308 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002309 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002310 */
2311#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2312 static char __noinit __aligned(q_align) \
2313 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002314 struct k_msgq q_name \
2315 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002316 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2317 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318
Peter Mitsisd7a37502016-10-13 11:37:40 -04002319/**
2320 * @brief Initialize a message queue.
2321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * This routine initializes a message queue object, prior to its first use.
2323 *
Allan Stephensda827222016-11-09 14:23:58 -06002324 * The message queue's ring buffer must contain space for @a max_msgs messages,
2325 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2326 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2327 * that each message is similarly aligned to this boundary, @a q_msg_size
2328 * must also be a multiple of N.
2329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @param q Address of the message queue.
2331 * @param buffer Pointer to ring buffer that holds queued messages.
2332 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002333 * @param max_msgs Maximum number of messages that can be queued.
2334 *
2335 * @return N/A
2336 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002337extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002338 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002339
2340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002344 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002345 * @note Can be called by ISRs.
2346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @param q Address of the message queue.
2348 * @param data Pointer to the message.
2349 * @param timeout Waiting period to add the message (in milliseconds),
2350 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002351 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002352 * @retval 0 Message sent.
2353 * @retval -ENOMSG Returned without waiting or queue purged.
2354 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002355 */
Kumar Galacc334c72017-04-21 10:55:34 -05002356extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002357
2358/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * This routine receives a message from message queue @a q in a "first in,
2362 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002363 *
Allan Stephensc98da842016-11-11 15:45:03 -05002364 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * @param q Address of the message queue.
2367 * @param data Address of area to hold the received message.
2368 * @param timeout Waiting period to receive the message (in milliseconds),
2369 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002370 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002371 * @retval 0 Message received.
2372 * @retval -ENOMSG Returned without waiting.
2373 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002374 */
Kumar Galacc334c72017-04-21 10:55:34 -05002375extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002376
2377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * This routine discards all unreceived messages in a message queue's ring
2381 * buffer. Any threads that are blocked waiting to send a message to the
2382 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002385 *
2386 * @return N/A
2387 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388extern void k_msgq_purge(struct k_msgq *q);
2389
Peter Mitsis67be2492016-10-07 11:44:34 -04002390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * This routine returns the number of unused entries in a message queue's
2394 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @param q Address of the message queue.
2397 *
2398 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002399 */
Kumar Galacc334c72017-04-21 10:55:34 -05002400static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002401{
2402 return q->max_msgs - q->used_msgs;
2403}
2404
Peter Mitsisd7a37502016-10-13 11:37:40 -04002405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002406 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @param q Address of the message queue.
2411 *
2412 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002413 */
Kumar Galacc334c72017-04-21 10:55:34 -05002414static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415{
2416 return q->used_msgs;
2417}
2418
Allan Stephensc98da842016-11-11 15:45:03 -05002419/**
2420 * @} end defgroup msgq_apis
2421 */
2422
2423/**
2424 * @defgroup mem_pool_apis Memory Pool APIs
2425 * @ingroup kernel_apis
2426 * @{
2427 */
2428
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002430 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431 void *addr_in_pool;
2432 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002433 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434};
2435
Allan Stephensc98da842016-11-11 15:45:03 -05002436/**
2437 * @} end defgroup mem_pool_apis
2438 */
2439
2440/**
2441 * @defgroup mailbox_apis Mailbox APIs
2442 * @ingroup kernel_apis
2443 * @{
2444 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
2446struct k_mbox_msg {
2447 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002448 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002450 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002452 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453 /** sender's message data buffer */
2454 void *tx_data;
2455 /** internal use only - needed for legacy API support */
2456 void *_rx_data;
2457 /** message data block descriptor */
2458 struct k_mem_block tx_block;
2459 /** source thread id */
2460 k_tid_t rx_source_thread;
2461 /** target thread id */
2462 k_tid_t tx_target_thread;
2463 /** internal use only - thread waiting on send (may be a dummy) */
2464 k_tid_t _syncing_thread;
2465#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2466 /** internal use only - semaphore used during asynchronous send */
2467 struct k_sem *_async_sem;
2468#endif
2469};
2470
Allan Stephensc98da842016-11-11 15:45:03 -05002471/**
2472 * @cond INTERNAL_HIDDEN
2473 */
2474
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475struct k_mbox {
2476 _wait_q_t tx_msg_queue;
2477 _wait_q_t rx_msg_queue;
2478
Anas Nashif2f203c22016-12-18 06:57:45 -05002479 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480};
2481
2482#define K_MBOX_INITIALIZER(obj) \
2483 { \
2484 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2485 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002486 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487 }
2488
Peter Mitsis12092702016-10-14 12:57:23 -04002489/**
Allan Stephensc98da842016-11-11 15:45:03 -05002490 * INTERNAL_HIDDEN @endcond
2491 */
2492
2493/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002496 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002498 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002503 struct k_mbox name \
2504 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 K_MBOX_INITIALIZER(name) \
2506
Peter Mitsis12092702016-10-14 12:57:23 -04002507/**
2508 * @brief Initialize a mailbox.
2509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * This routine initializes a mailbox object, prior to its first use.
2511 *
2512 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002513 *
2514 * @return N/A
2515 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516extern void k_mbox_init(struct k_mbox *mbox);
2517
Peter Mitsis12092702016-10-14 12:57:23 -04002518/**
2519 * @brief Send a mailbox message in a synchronous manner.
2520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * This routine sends a message to @a mbox and waits for a receiver to both
2522 * receive and process it. The message data may be in a buffer, in a memory
2523 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * @param mbox Address of the mailbox.
2526 * @param tx_msg Address of the transmit message descriptor.
2527 * @param timeout Waiting period for the message to be received (in
2528 * milliseconds), or one of the special values K_NO_WAIT
2529 * and K_FOREVER. Once the message has been received,
2530 * this routine waits as long as necessary for the message
2531 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002532 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002533 * @retval 0 Message sent.
2534 * @retval -ENOMSG Returned without waiting.
2535 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002536 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002537extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002538 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002539
Peter Mitsis12092702016-10-14 12:57:23 -04002540/**
2541 * @brief Send a mailbox message in an asynchronous manner.
2542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * This routine sends a message to @a mbox without waiting for a receiver
2544 * to process it. The message data may be in a buffer, in a memory pool block,
2545 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2546 * will be given when the message has been both received and completely
2547 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @param mbox Address of the mailbox.
2550 * @param tx_msg Address of the transmit message descriptor.
2551 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002552 *
2553 * @return N/A
2554 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002555extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 struct k_sem *sem);
2557
Peter Mitsis12092702016-10-14 12:57:23 -04002558/**
2559 * @brief Receive a mailbox message.
2560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * This routine receives a message from @a mbox, then optionally retrieves
2562 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 * @param mbox Address of the mailbox.
2565 * @param rx_msg Address of the receive message descriptor.
2566 * @param buffer Address of the buffer to receive data, or NULL to defer data
2567 * retrieval and message disposal until later.
2568 * @param timeout Waiting period for a message to be received (in
2569 * milliseconds), or one of the special values K_NO_WAIT
2570 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002571 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002572 * @retval 0 Message received.
2573 * @retval -ENOMSG Returned without waiting.
2574 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002575 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002576extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002577 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002578
2579/**
2580 * @brief Retrieve mailbox message data into a buffer.
2581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * This routine completes the processing of a received message by retrieving
2583 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002584 *
2585 * Alternatively, this routine can be used to dispose of a received message
2586 * without retrieving its data.
2587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * @param rx_msg Address of the receive message descriptor.
2589 * @param buffer Address of the buffer to receive data, or NULL to discard
2590 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002591 *
2592 * @return N/A
2593 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002594extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002595
2596/**
2597 * @brief Retrieve mailbox message data into a memory pool block.
2598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * This routine completes the processing of a received message by retrieving
2600 * its data into a memory pool block, then disposing of the message.
2601 * The memory pool block that results from successful retrieval must be
2602 * returned to the pool once the data has been processed, even in cases
2603 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002604 *
2605 * Alternatively, this routine can be used to dispose of a received message
2606 * without retrieving its data. In this case there is no need to return a
2607 * memory pool block to the pool.
2608 *
2609 * This routine allocates a new memory pool block for the data only if the
2610 * data is not already in one. If a new block cannot be allocated, the routine
2611 * returns a failure code and the received message is left unchanged. This
2612 * permits the caller to reattempt data retrieval at a later time or to dispose
2613 * of the received message without retrieving its data.
2614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @param rx_msg Address of a receive message descriptor.
2616 * @param pool Address of memory pool, or NULL to discard data.
2617 * @param block Address of the area to hold memory pool block info.
2618 * @param timeout Waiting period to wait for a memory pool block (in
2619 * milliseconds), or one of the special values K_NO_WAIT
2620 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002621 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002622 * @retval 0 Data retrieved.
2623 * @retval -ENOMEM Returned without waiting.
2624 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002625 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002626extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002627 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002628 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629
Allan Stephensc98da842016-11-11 15:45:03 -05002630/**
2631 * @} end defgroup mailbox_apis
2632 */
2633
2634/**
2635 * @cond INTERNAL_HIDDEN
2636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637
2638struct k_pipe {
2639 unsigned char *buffer; /* Pipe buffer: may be NULL */
2640 size_t size; /* Buffer size */
2641 size_t bytes_used; /* # bytes used in buffer */
2642 size_t read_index; /* Where in buffer to read from */
2643 size_t write_index; /* Where in buffer to write */
2644
2645 struct {
2646 _wait_q_t readers; /* Reader wait queue */
2647 _wait_q_t writers; /* Writer wait queue */
2648 } wait_q;
2649
Anas Nashif2f203c22016-12-18 06:57:45 -05002650 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651};
2652
Peter Mitsise5d9c582016-10-14 14:44:57 -04002653#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654 { \
2655 .buffer = pipe_buffer, \
2656 .size = pipe_buffer_size, \
2657 .bytes_used = 0, \
2658 .read_index = 0, \
2659 .write_index = 0, \
2660 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2661 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002662 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663 }
2664
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665/**
Allan Stephensc98da842016-11-11 15:45:03 -05002666 * INTERNAL_HIDDEN @endcond
2667 */
2668
2669/**
2670 * @defgroup pipe_apis Pipe APIs
2671 * @ingroup kernel_apis
2672 * @{
2673 */
2674
2675/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002679 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002680 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * @param name Name of the pipe.
2683 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2684 * or zero if no ring buffer is used.
2685 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002687#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2688 static unsigned char __noinit __aligned(pipe_align) \
2689 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002690 struct k_pipe name \
2691 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002692 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * This routine initializes a pipe object, prior to its first use.
2698 *
2699 * @param pipe Address of the pipe.
2700 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2701 * is used.
2702 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2703 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704 *
2705 * @return N/A
2706 */
2707extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2708 size_t size);
2709
2710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002713 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002715 * @param pipe Address of the pipe.
2716 * @param data Address of data to write.
2717 * @param bytes_to_write Size of data (in bytes).
2718 * @param bytes_written Address of area to hold the number of bytes written.
2719 * @param min_xfer Minimum number of bytes to write.
2720 * @param timeout Waiting period to wait for the data to be written (in
2721 * milliseconds), or one of the special values K_NO_WAIT
2722 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002724 * @retval 0 At least @a min_xfer bytes of data were written.
2725 * @retval -EIO Returned without waiting; zero data bytes were written.
2726 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002729extern int k_pipe_put(struct k_pipe *pipe, void *data,
2730 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002731 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732
2733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * @param pipe Address of the pipe.
2739 * @param data Address to place the data read from pipe.
2740 * @param bytes_to_read Maximum number of data bytes to read.
2741 * @param bytes_read Address of area to hold the number of bytes read.
2742 * @param min_xfer Minimum number of data bytes to read.
2743 * @param timeout Waiting period to wait for the data to be read (in
2744 * milliseconds), or one of the special values K_NO_WAIT
2745 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002747 * @retval 0 At least @a min_xfer bytes of data were read.
2748 * @retval -EIO Returned without waiting; zero data bytes were read.
2749 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002752extern int k_pipe_get(struct k_pipe *pipe, void *data,
2753 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002754 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
2756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * This routine writes the data contained in a memory block to @a pipe.
2760 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764 * @param block Memory block containing data to send
2765 * @param size Number of data bytes in memory block to send
2766 * @param sem Semaphore to signal upon completion (else NULL)
2767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 */
2770extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2771 size_t size, struct k_sem *sem);
2772
2773/**
Allan Stephensc98da842016-11-11 15:45:03 -05002774 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775 */
2776
Allan Stephensc98da842016-11-11 15:45:03 -05002777/**
2778 * @cond INTERNAL_HIDDEN
2779 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002781struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002783 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002784 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785 char *buffer;
2786 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05002787 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788
Anas Nashif2f203c22016-12-18 06:57:45 -05002789 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790};
2791
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002792#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2793 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 { \
2795 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002796 .num_blocks = slab_num_blocks, \
2797 .block_size = slab_block_size, \
2798 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799 .free_list = NULL, \
2800 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002801 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802 }
2803
Peter Mitsis578f9112016-10-07 13:50:31 -04002804/**
Allan Stephensc98da842016-11-11 15:45:03 -05002805 * INTERNAL_HIDDEN @endcond
2806 */
2807
2808/**
2809 * @defgroup mem_slab_apis Memory Slab APIs
2810 * @ingroup kernel_apis
2811 * @{
2812 */
2813
2814/**
Allan Stephensda827222016-11-09 14:23:58 -06002815 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002816 *
Allan Stephensda827222016-11-09 14:23:58 -06002817 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002819 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2820 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002821 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002822 *
Allan Stephensda827222016-11-09 14:23:58 -06002823 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002825 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002826 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @param name Name of the memory slab.
2829 * @param slab_block_size Size of each memory block (in bytes).
2830 * @param slab_num_blocks Number memory blocks.
2831 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002832 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002833#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2834 char __noinit __aligned(slab_align) \
2835 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2836 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002837 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002838 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2839 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002841/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002842 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002845 *
Allan Stephensda827222016-11-09 14:23:58 -06002846 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2847 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2848 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2849 * To ensure that each memory block is similarly aligned to this boundary,
2850 * @a slab_block_size must also be a multiple of N.
2851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @param slab Address of the memory slab.
2853 * @param buffer Pointer to buffer used for the memory blocks.
2854 * @param block_size Size of each memory block (in bytes).
2855 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002856 *
2857 * @return N/A
2858 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002859extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002860 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002861
2862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @param slab Address of the memory slab.
2868 * @param mem Pointer to block address area.
2869 * @param timeout Maximum time to wait for operation to complete
2870 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2871 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002872 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002873 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002875 * @retval -ENOMEM Returned without waiting.
2876 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002877 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002878extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05002879 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002880
2881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * This routine releases a previously allocated memory block back to its
2885 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * @param slab Address of the memory slab.
2888 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002889 *
2890 * @return N/A
2891 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002892extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002893
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * This routine gets the number of memory blocks that are currently
2898 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002903 */
Kumar Galacc334c72017-04-21 10:55:34 -05002904static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002905{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002906 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002907}
2908
Peter Mitsisc001aa82016-10-13 13:53:37 -04002909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * This routine gets the number of memory blocks that are currently
2913 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002918 */
Kumar Galacc334c72017-04-21 10:55:34 -05002919static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002920{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002921 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002922}
2923
Allan Stephensc98da842016-11-11 15:45:03 -05002924/**
2925 * @} end defgroup mem_slab_apis
2926 */
2927
2928/**
2929 * @cond INTERNAL_HIDDEN
2930 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002932/*
2933 * Memory pool requires a buffer and two arrays of structures for the
2934 * memory block accounting:
2935 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2936 * status of four blocks of memory.
2937 */
2938struct k_mem_pool_quad_block {
2939 char *mem_blocks; /* pointer to the first of four memory blocks */
Kumar Galacc334c72017-04-21 10:55:34 -05002940 u32_t mem_status; /* four bits. If bit is set, memory block is
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002941 allocated */
2942};
2943/*
2944 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2945 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2946 * block size is 4 times less than the previous one and thus requires 4 times
2947 * bigger array of k_mem_pool_quad_block structures to keep track of the
2948 * memory blocks.
2949 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002951/*
2952 * The array of k_mem_pool_block_set keeps the information of each array of
2953 * k_mem_pool_quad_block structures
2954 */
2955struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002956 size_t block_size; /* memory block size */
Kumar Galacc334c72017-04-21 10:55:34 -05002957 u32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002958 struct k_mem_pool_quad_block *quad_block;
2959 int count;
2960};
2961
2962/* Memory pool descriptor */
2963struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002964 size_t max_block_size;
2965 size_t min_block_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002966 u32_t nr_of_maxblocks;
2967 u32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002968 struct k_mem_pool_block_set *block_set;
2969 char *bufblock;
2970 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002971 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972};
2973
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002974#ifdef CONFIG_ARM
2975#define _SECTION_TYPE_SIGN "%"
2976#else
2977#define _SECTION_TYPE_SIGN "@"
2978#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002980/*
2981 * Static memory pool initialization
2982 */
Allan Stephensc98da842016-11-11 15:45:03 -05002983
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002984/*
2985 * Use .altmacro to be able to recalculate values and pass them as string
David B. Kinder8b986d72017-04-18 15:56:26 -07002986 * arguments when calling assembler macros recursively
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002987 */
2988__asm__(".altmacro\n\t");
2989
2990/*
2991 * Recursively calls a macro
David B. Kinder8b986d72017-04-18 15:56:26 -07002992 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002993 * __memory_pool_max_block_size - maximal size of the memory block
2994 * __memory_pool_min_block_size - minimal size of the memory block
2995 * Notes:
2996 * Global symbols are used due the fact that assembler macro allows only
2997 * one argument be passed with the % conversion
2998 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2999 * is used instead of / 4.
3000 * n_max argument needs to go first in the invoked macro, as some
3001 * assemblers concatenate \name and %(\n_max * 4) arguments
3002 * if \name goes first
3003 */
3004__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3005 ".ifge __memory_pool_max_block_size >> 2 -"
3006 " __memory_pool_min_block_size\n\t\t"
3007 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3008 "\\macro_name %(\\n_max * 4) \\name\n\t"
3009 ".endif\n\t"
3010 ".endm\n");
3011
3012/*
3013 * Build quad blocks
3014 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3015 * structures and recursively calls itself for the next array, 4 times
3016 * larger.
David B. Kinder8b986d72017-04-18 15:56:26 -07003017 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003018 * __memory_pool_max_block_size - maximal size of the memory block
3019 * __memory_pool_min_block_size - minimal size of the memory block
3020 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3021 */
3022__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003023 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003024 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3025 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3026 ".if \\n_max % 4\n\t\t"
3027 ".skip __memory_pool_quad_block_size\n\t"
3028 ".endif\n\t"
3029 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3030 ".endm\n");
3031
3032/*
3033 * Build block sets and initialize them
3034 * Macro initializes the k_mem_pool_block_set structure and
3035 * recursively calls itself for the next one.
David B. Kinder8b986d72017-04-18 15:56:26 -07003036 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003037 * __memory_pool_max_block_size - maximal size of the memory block
3038 * __memory_pool_min_block_size - minimal size of the memory block
3039 * __memory_pool_block_set_count, the number of the elements in the
3040 * block set array must be set to 0. Macro calculates it's real
3041 * value.
3042 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3043 * structures, _build_quad_blocks must be called prior it.
3044 */
3045__asm__(".macro _build_block_set n_max, name\n\t"
3046 ".int __memory_pool_max_block_size\n\t" /* block_size */
3047 ".if \\n_max % 4\n\t\t"
3048 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3049 ".else\n\t\t"
3050 ".int \\n_max >> 2\n\t"
3051 ".endif\n\t"
3052 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3053 ".int 0\n\t" /* count */
3054 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3055 "__do_recurse _build_block_set \\name \\n_max\n\t"
3056 ".endm\n");
3057
3058/*
3059 * Build a memory pool structure and initialize it
3060 * Macro uses __memory_pool_block_set_count global symbol,
3061 * block set addresses and buffer address, it may be called only after
3062 * _build_block_set
3063 */
3064__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003065 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003066 _SECTION_TYPE_SIGN "progbits\n\t"
3067 ".globl \\name\n\t"
3068 "\\name:\n\t"
3069 ".int \\max_size\n\t" /* max_block_size */
3070 ".int \\min_size\n\t" /* min_block_size */
3071 ".int \\n_max\n\t" /* nr_of_maxblocks */
3072 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3073 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3074 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3075 ".int 0\n\t" /* wait_q->head */
3076 ".int 0\n\t" /* wait_q->next */
3077 ".popsection\n\t"
3078 ".endm\n");
3079
3080#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3081 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3082 _SECTION_TYPE_SIGN "progbits\n\t"); \
3083 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3084 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3085 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3086 STRINGIFY(name) "\n\t"); \
3087 __asm__(".popsection\n\t")
3088
3089#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3090 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3091 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3092 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3093 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003094 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003095 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3096 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3097 STRINGIFY(name) "\n\t"); \
3098 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3099 __asm__(".int __memory_pool_block_set_count\n\t"); \
3100 __asm__(".popsection\n\t"); \
Kumar Galacc334c72017-04-21 10:55:34 -05003101 extern u32_t _mem_pool_block_set_count_##name; \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003102 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3103
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003104#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3105 char __noinit __aligned(align) \
3106 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003107
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003108/*
3109 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3110 * to __memory_pool_quad_block_size absolute symbol.
3111 * This function does not get called, but compiler calculates the value and
3112 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3113 */
3114static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3115{
3116 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003117#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003118 "__memory_pool_quad_block_size = %0\n\t"
3119#else
3120 "__memory_pool_quad_block_size = %c0\n\t"
3121#endif
3122 :
3123 : "n"(sizeof(struct k_mem_pool_quad_block)));
3124}
3125
3126/**
Allan Stephensc98da842016-11-11 15:45:03 -05003127 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003128 */
3129
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003130/**
Allan Stephensc98da842016-11-11 15:45:03 -05003131 * @addtogroup mem_pool_apis
3132 * @{
3133 */
3134
3135/**
3136 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3139 * long. The memory pool allows blocks to be repeatedly partitioned into
3140 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3141 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003142 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003144 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003145 * If the pool is to be accessed outside the module where it is defined, it
3146 * can be declared via
3147 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003148 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @param name Name of the memory pool.
3151 * @param min_size Size of the smallest blocks in the pool (in bytes).
3152 * @param max_size Size of the largest blocks in the pool (in bytes).
3153 * @param n_max Number of maximum sized blocks in the pool.
3154 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003155 */
3156#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003157 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3158 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003159 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003160 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3161 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3162 extern struct k_mem_pool name
3163
Peter Mitsis937042c2016-10-13 13:18:26 -04003164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003165 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003169 * @param pool Address of the memory pool.
3170 * @param block Pointer to block descriptor for the allocated memory.
3171 * @param size Amount of memory to allocate (in bytes).
3172 * @param timeout Maximum time to wait for operation to complete
3173 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3174 * or K_FOREVER to wait as long as necessary.
3175 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003176 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003178 * @retval -ENOMEM Returned without waiting.
3179 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003180 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003181extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003182 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003183
3184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003187 * This routine releases a previously allocated memory block back to its
3188 * memory pool.
3189 *
3190 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003191 *
3192 * @return N/A
3193 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003195
3196/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * This routine instructs a memory pool to concatenate unused memory blocks
3200 * into larger blocks wherever possible. Manually defragmenting the memory
3201 * pool may speed up future allocations of memory blocks by eliminating the
3202 * need for the memory pool to perform an automatic partial defragmentation.
3203 *
3204 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003205 *
3206 * @return N/A
3207 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003208extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003209
3210/**
Allan Stephensc98da842016-11-11 15:45:03 -05003211 * @} end addtogroup mem_pool_apis
3212 */
3213
3214/**
3215 * @defgroup heap_apis Heap Memory Pool APIs
3216 * @ingroup kernel_apis
3217 * @{
3218 */
3219
3220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003224 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003226 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003228 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003229 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003230extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003231
3232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003234 *
3235 * This routine provides traditional free() semantics. The memory being
3236 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003237 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003238 * If @a ptr is NULL, no operation is performed.
3239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003240 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003241 *
3242 * @return N/A
3243 */
3244extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245
Allan Stephensc98da842016-11-11 15:45:03 -05003246/**
3247 * @} end defgroup heap_apis
3248 */
3249
Benjamin Walshacc68c12017-01-29 18:57:45 -05003250/* polling API - PRIVATE */
3251
Benjamin Walshb0179862017-02-02 16:39:57 -05003252#ifdef CONFIG_POLL
3253#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3254#else
3255#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3256#endif
3257
Benjamin Walshacc68c12017-01-29 18:57:45 -05003258/* private - implementation data created as needed, per-type */
3259struct _poller {
3260 struct k_thread *thread;
3261};
3262
3263/* private - types bit positions */
3264enum _poll_types_bits {
3265 /* can be used to ignore an event */
3266 _POLL_TYPE_IGNORE,
3267
3268 /* to be signaled by k_poll_signal() */
3269 _POLL_TYPE_SIGNAL,
3270
3271 /* semaphore availability */
3272 _POLL_TYPE_SEM_AVAILABLE,
3273
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003274 /* queue/fifo/lifo data availability */
3275 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003276
3277 _POLL_NUM_TYPES
3278};
3279
3280#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3281
3282/* private - states bit positions */
3283enum _poll_states_bits {
3284 /* default state when creating event */
3285 _POLL_STATE_NOT_READY,
3286
3287 /* there was another poller already on the object */
3288 _POLL_STATE_EADDRINUSE,
3289
3290 /* signaled by k_poll_signal() */
3291 _POLL_STATE_SIGNALED,
3292
3293 /* semaphore is available */
3294 _POLL_STATE_SEM_AVAILABLE,
3295
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003296 /* data is available to read on queue/fifo/lifo */
3297 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003298
3299 _POLL_NUM_STATES
3300};
3301
3302#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3303
3304#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003305 (32 - (0 \
3306 + 8 /* tag */ \
3307 + _POLL_NUM_TYPES \
3308 + _POLL_NUM_STATES \
3309 + 1 /* modes */ \
3310 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003311
3312#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3313#error overflow of 32-bit word in struct k_poll_event
3314#endif
3315
3316/* end of polling API - PRIVATE */
3317
3318
3319/**
3320 * @defgroup poll_apis Async polling APIs
3321 * @ingroup kernel_apis
3322 * @{
3323 */
3324
3325/* Public polling API */
3326
3327/* public - values for k_poll_event.type bitfield */
3328#define K_POLL_TYPE_IGNORE 0
3329#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3330#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003331#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3332#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003333
3334/* public - polling modes */
3335enum k_poll_modes {
3336 /* polling thread does not take ownership of objects when available */
3337 K_POLL_MODE_NOTIFY_ONLY = 0,
3338
3339 K_POLL_NUM_MODES
3340};
3341
3342/* public - values for k_poll_event.state bitfield */
3343#define K_POLL_STATE_NOT_READY 0
3344#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3345#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3346#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003347#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3348#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003349
3350/* public - poll signal object */
3351struct k_poll_signal {
3352 /* PRIVATE - DO NOT TOUCH */
3353 struct k_poll_event *poll_event;
3354
3355 /*
3356 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3357 * user resets it to 0.
3358 */
3359 unsigned int signaled;
3360
3361 /* custom result value passed to k_poll_signal() if needed */
3362 int result;
3363};
3364
3365#define K_POLL_SIGNAL_INITIALIZER() \
3366 { \
3367 .poll_event = NULL, \
3368 .signaled = 0, \
3369 .result = 0, \
3370 }
3371
3372struct k_poll_event {
3373 /* PRIVATE - DO NOT TOUCH */
3374 struct _poller *poller;
3375
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003376 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003377 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003378
Benjamin Walshacc68c12017-01-29 18:57:45 -05003379 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003380 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003381
3382 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003383 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003384
3385 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003386 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003387
3388 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003389 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003390
3391 /* per-type data */
3392 union {
3393 void *obj;
3394 struct k_poll_signal *signal;
3395 struct k_sem *sem;
3396 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003397 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003398 };
3399};
3400
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003401#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003402 { \
3403 .poller = NULL, \
3404 .type = event_type, \
3405 .state = K_POLL_STATE_NOT_READY, \
3406 .mode = event_mode, \
3407 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003408 { .obj = event_obj }, \
3409 }
3410
3411#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3412 event_tag) \
3413 { \
3414 .type = event_type, \
3415 .tag = event_tag, \
3416 .state = K_POLL_STATE_NOT_READY, \
3417 .mode = event_mode, \
3418 .unused = 0, \
3419 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003420 }
3421
3422/**
3423 * @brief Initialize one struct k_poll_event instance
3424 *
3425 * After this routine is called on a poll event, the event it ready to be
3426 * placed in an event array to be passed to k_poll().
3427 *
3428 * @param event The event to initialize.
3429 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3430 * values. Only values that apply to the same object being polled
3431 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3432 * event.
3433 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3434 * @param obj Kernel object or poll signal.
3435 *
3436 * @return N/A
3437 */
3438
Kumar Galacc334c72017-04-21 10:55:34 -05003439extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003440 int mode, void *obj);
3441
3442/**
3443 * @brief Wait for one or many of multiple poll events to occur
3444 *
3445 * This routine allows a thread to wait concurrently for one or many of
3446 * multiple poll events to have occurred. Such events can be a kernel object
3447 * being available, like a semaphore, or a poll signal event.
3448 *
3449 * When an event notifies that a kernel object is available, the kernel object
3450 * is not "given" to the thread calling k_poll(): it merely signals the fact
3451 * that the object was available when the k_poll() call was in effect. Also,
3452 * all threads trying to acquire an object the regular way, i.e. by pending on
3453 * the object, have precedence over the thread polling on the object. This
3454 * means that the polling thread will never get the poll event on an object
3455 * until the object becomes available and its pend queue is empty. For this
3456 * reason, the k_poll() call is more effective when the objects being polled
3457 * only have one thread, the polling thread, trying to acquire them.
3458 *
3459 * Only one thread can be polling for a particular object at a given time. If
3460 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3461 * and returns as soon as it has finished handling the other events. This means
3462 * that k_poll() can return -EADDRINUSE and have the state value of some events
3463 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3464 * parameter is ignored.
3465 *
3466 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3467 * events that were passed to k_poll() and check the state field for the values
3468 * that were expected and take the associated actions.
3469 *
3470 * Before being reused for another call to k_poll(), the user has to reset the
3471 * state field to K_POLL_STATE_NOT_READY.
3472 *
3473 * @param events An array of pointers to events to be polled for.
3474 * @param num_events The number of events in the array.
3475 * @param timeout Waiting period for an event to be ready (in milliseconds),
3476 * or one of the special values K_NO_WAIT and K_FOREVER.
3477 *
3478 * @retval 0 One or more events are ready.
3479 * @retval -EADDRINUSE One or more objects already had a poller.
3480 * @retval -EAGAIN Waiting period timed out.
3481 */
3482
3483extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003484 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003485
3486/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003487 * @brief Initialize a poll signal object.
3488 *
3489 * Ready a poll signal object to be signaled via k_poll_signal().
3490 *
3491 * @param signal A poll signal.
3492 *
3493 * @return N/A
3494 */
3495
3496extern void k_poll_signal_init(struct k_poll_signal *signal);
3497
3498/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003499 * @brief Signal a poll signal object.
3500 *
3501 * This routine makes ready a poll signal, which is basically a poll event of
3502 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3503 * made ready to run. A @a result value can be specified.
3504 *
3505 * The poll signal contains a 'signaled' field that, when set by
3506 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3507 * be reset by the user before being passed again to k_poll() or k_poll() will
3508 * consider it being signaled, and will return immediately.
3509 *
3510 * @param signal A poll signal.
3511 * @param result The value to store in the result field of the signal.
3512 *
3513 * @retval 0 The signal was delivered successfully.
3514 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3515 */
3516
3517extern int k_poll_signal(struct k_poll_signal *signal, int result);
3518
3519/* private internal function */
3520extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003521 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003522
3523/**
3524 * @} end defgroup poll_apis
3525 */
3526
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003527/**
3528 * @brief Make the CPU idle.
3529 *
3530 * This function makes the CPU idle until an event wakes it up.
3531 *
3532 * In a regular system, the idle thread should be the only thread responsible
3533 * for making the CPU idle and triggering any type of power management.
3534 * However, in some more constrained systems, such as a single-threaded system,
3535 * the only thread would be responsible for this if needed.
3536 *
3537 * @return N/A
3538 */
3539extern void k_cpu_idle(void);
3540
3541/**
3542 * @brief Make the CPU idle in an atomic fashion.
3543 *
3544 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3545 * must be done atomically before making the CPU idle.
3546 *
3547 * @param key Interrupt locking key obtained from irq_lock().
3548 *
3549 * @return N/A
3550 */
3551extern void k_cpu_atomic_idle(unsigned int key);
3552
Kumar Galacc334c72017-04-21 10:55:34 -05003553extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003554
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555#include <arch/cpu.h>
3556
Andrew Boiecdb94d62017-04-18 15:22:05 -07003557#ifdef _ARCH_EXCEPT
3558/* This archtecture has direct support for triggering a CPU exception */
3559#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3560#else
3561
3562#include <misc/printk.h>
3563
3564/* NOTE: This is the implementation for arches that do not implement
3565 * _ARCH_EXCEPT() to generate a real CPU exception.
3566 *
3567 * We won't have a real exception frame to determine the PC value when
3568 * the oops occurred, so print file and line number before we jump into
3569 * the fatal error handler.
3570 */
3571#define _k_except_reason(reason) do { \
3572 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3573 _NanoFatalErrorHandler(reason, &_default_esf); \
3574 CODE_UNREACHABLE; \
3575 } while (0)
3576
3577#endif /* _ARCH__EXCEPT */
3578
3579/**
3580 * @brief Fatally terminate a thread
3581 *
3582 * This should be called when a thread has encountered an unrecoverable
3583 * runtime condition and needs to terminate. What this ultimately
3584 * means is determined by the _fatal_error_handler() implementation, which
3585 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3586 *
3587 * If this is called from ISR context, the default system fatal error handler
3588 * will treat it as an unrecoverable system error, just like k_panic().
3589 */
3590#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3591
3592/**
3593 * @brief Fatally terminate the system
3594 *
3595 * This should be called when the Zephyr kernel has encountered an
3596 * unrecoverable runtime condition and needs to terminate. What this ultimately
3597 * means is determined by the _fatal_error_handler() implementation, which
3598 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3599 */
3600#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3601
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602/*
3603 * private APIs that are utilized by one or more public APIs
3604 */
3605
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003606#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003607extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003608#else
3609#define _init_static_threads() do { } while ((0))
3610#endif
3611
3612extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003613extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003614
3615#ifdef __cplusplus
3616}
3617#endif
3618
Andrew Boiee004dec2016-11-07 09:01:19 -08003619#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3620/*
3621 * Define new and delete operators.
3622 * At this moment, the operators do nothing since objects are supposed
3623 * to be statically allocated.
3624 */
3625inline void operator delete(void *ptr)
3626{
3627 (void)ptr;
3628}
3629
3630inline void operator delete[](void *ptr)
3631{
3632 (void)ptr;
3633}
3634
3635inline void *operator new(size_t size)
3636{
3637 (void)size;
3638 return NULL;
3639}
3640
3641inline void *operator new[](size_t size)
3642{
3643 (void)size;
3644 return NULL;
3645}
3646
3647/* Placement versions of operator new and delete */
3648inline void operator delete(void *ptr1, void *ptr2)
3649{
3650 (void)ptr1;
3651 (void)ptr2;
3652}
3653
3654inline void operator delete[](void *ptr1, void *ptr2)
3655{
3656 (void)ptr1;
3657 (void)ptr2;
3658}
3659
3660inline void *operator new(size_t size, void *ptr)
3661{
3662 (void)size;
3663 return ptr;
3664}
3665
3666inline void *operator new[](size_t size, void *ptr)
3667{
3668 (void)size;
3669 return ptr;
3670}
3671
3672#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3673
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003674#endif /* !_ASMLANGUAGE */
3675
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003676#endif /* _kernel__h_ */