blob: 54bce4f47524ab50c6987fb876b7b090a610942b [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>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
101#define _POLL_EVENT_OBJ_INIT \
102 .poll_event = NULL,
103#define _POLL_EVENT struct k_poll_event *poll_event
104#else
105#define _POLL_EVENT_OBJ_INIT
106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct 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
Andrew Boie73abd322017-04-04 13:19:13 -0700126/* timeouts */
127
128struct _timeout;
129typedef void (*_timeout_func_t)(struct _timeout *t);
130
131struct _timeout {
132 sys_dnode_t node;
133 struct k_thread *thread;
134 sys_dlist_t *wait_q;
135 s32_t delta_ticks_from_prev;
136 _timeout_func_t func;
137};
138
139extern s32_t _timeout_remaining_get(struct _timeout *timeout);
140
141/* Threads */
142typedef void (*_thread_entry_t)(void *, void *, void *);
143
144#ifdef CONFIG_THREAD_MONITOR
145struct __thread_entry {
146 _thread_entry_t pEntry;
147 void *parameter1;
148 void *parameter2;
149 void *parameter3;
150};
151#endif
152
153/* can be used for creating 'dummy' threads, e.g. for pending on objects */
154struct _thread_base {
155
156 /* this thread's entry in a ready/wait queue */
157 sys_dnode_t k_q_node;
158
159 /* user facing 'thread options'; values defined in include/kernel.h */
160 u8_t user_options;
161
162 /* thread state */
163 u8_t thread_state;
164
165 /*
166 * scheduler lock count and thread priority
167 *
168 * These two fields control the preemptibility of a thread.
169 *
170 * When the scheduler is locked, sched_locked is decremented, which
171 * means that the scheduler is locked for values from 0xff to 0x01. A
172 * thread is coop if its prio is negative, thus 0x80 to 0xff when
173 * looked at the value as unsigned.
174 *
175 * By putting them end-to-end, this means that a thread is
176 * non-preemptible if the bundled value is greater than or equal to
177 * 0x0080.
178 */
179 union {
180 struct {
181#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
182 u8_t sched_locked;
183 s8_t prio;
184#else /* LITTLE and PDP */
185 s8_t prio;
186 u8_t sched_locked;
187#endif
188 };
189 u16_t preempt;
190 };
191
192 /* data returned by APIs */
193 void *swap_data;
194
195#ifdef CONFIG_SYS_CLOCK_EXISTS
196 /* this thread's entry in a timeout queue */
197 struct _timeout timeout;
198#endif
199
200};
201
202typedef struct _thread_base _thread_base_t;
203
204#if defined(CONFIG_THREAD_STACK_INFO)
205/* Contains the stack information of a thread */
206struct _thread_stack_info {
207 /* Stack Start */
208 u32_t start;
209 /* Stack Size */
210 u32_t size;
211};
212#endif /* CONFIG_THREAD_STACK_INFO */
213
214struct k_thread {
215
216 struct _thread_base base;
217
218 /* defined by the architecture, but all archs need these */
219 struct _caller_saved caller_saved;
220 struct _callee_saved callee_saved;
221
222 /* static thread init data */
223 void *init_data;
224
225 /* abort function */
226 void (*fn_abort)(void);
227
228#if defined(CONFIG_THREAD_MONITOR)
229 /* thread entry and parameters description */
230 struct __thread_entry *entry;
231
232 /* next item in list of all threads */
233 struct k_thread *next_thread;
234#endif
235
236#ifdef CONFIG_THREAD_CUSTOM_DATA
237 /* crude thread-local storage */
238 void *custom_data;
239#endif
240
241#ifdef CONFIG_ERRNO
242 /* per-thread errno variable */
243 int errno_var;
244#endif
245
246#if defined(CONFIG_THREAD_STACK_INFO)
247 /* Stack Info */
248 struct _thread_stack_info stack_info;
249#endif /* CONFIG_THREAD_STACK_INFO */
250
251 /* arch-specifics: must always be at the end */
252 struct _thread_arch arch;
253};
254
255typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400256typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700257#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259enum execution_context_types {
260 K_ISR = 0,
261 K_COOP_THREAD,
262 K_PREEMPT_THREAD,
263};
264
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400265/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100266 * @defgroup profiling_apis Profiling APIs
267 * @ingroup kernel_apis
268 * @{
269 */
270
271/**
272 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
273 *
274 * This routine calls @ref stack_analyze on the 4 call stacks declared and
275 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
276 *
277 * CONFIG_MAIN_STACK_SIZE
278 * CONFIG_IDLE_STACK_SIZE
279 * CONFIG_ISR_STACK_SIZE
280 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
281 *
282 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
283 * produce output.
284 *
285 * @return N/A
286 */
287extern void k_call_stacks_analyze(void);
288
289/**
290 * @} end defgroup profiling_apis
291 */
292
293/**
Allan Stephensc98da842016-11-11 15:45:03 -0500294 * @defgroup thread_apis Thread APIs
295 * @ingroup kernel_apis
296 * @{
297 */
298
299/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500300 * @typedef k_thread_entry_t
301 * @brief Thread entry point function type.
302 *
303 * A thread's entry point function is invoked when the thread starts executing.
304 * Up to 3 argument values can be passed to the function.
305 *
306 * The thread terminates execution permanently if the entry point function
307 * returns. The thread is responsible for releasing any shared resources
308 * it may own (such as mutexes and dynamically allocated memory), prior to
309 * returning.
310 *
311 * @param p1 First argument.
312 * @param p2 Second argument.
313 * @param p3 Third argument.
314 *
315 * @return N/A
316 */
317typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
318
Benjamin Walshed240f22017-01-22 13:05:08 -0500319#endif /* !_ASMLANGUAGE */
320
321
322/*
323 * Thread user options. May be needed by assembly code. Common part uses low
324 * bits, arch-specific use high bits.
325 */
326
327/* system thread that must not abort */
328#define K_ESSENTIAL (1 << 0)
329
330#if defined(CONFIG_FP_SHARING)
331/* thread uses floating point registers */
332#define K_FP_REGS (1 << 1)
333#endif
334
335#ifdef CONFIG_X86
336/* x86 Bitmask definitions for threads user options */
337
338#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
339/* thread uses SSEx (and also FP) registers */
340#define K_SSE_REGS (1 << 7)
341#endif
342#endif
343
344/* end - thread options */
345
346#if !defined(_ASMLANGUAGE)
347
Allan Stephens5eceb852016-11-16 10:16:30 -0500348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500349 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * The new thread may be scheduled for immediate execution or a delayed start.
354 * If the newly spawned thread does not have a delayed start the kernel
355 * scheduler may preempt the current thread to allow the new thread to
356 * execute.
357 *
358 * Thread options are architecture-specific, and can include K_ESSENTIAL,
359 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
360 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400361 *
362 * @param stack Pointer to the stack space.
363 * @param stack_size Stack size in bytes.
364 * @param entry Thread entry function.
365 * @param p1 1st entry point parameter.
366 * @param p2 2nd entry point parameter.
367 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500368 * @param prio Thread priority.
369 * @param options Thread options.
370 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500372 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400373 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500374extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500375 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400376 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500377 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500380 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400381 *
Allan Stephensc98da842016-11-11 15:45:03 -0500382 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500383 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386 *
387 * @return N/A
388 */
Kumar Galacc334c72017-04-21 10:55:34 -0500389extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400390
391/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500392 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400393 *
394 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500395 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400397 * @return N/A
398 */
Kumar Galacc334c72017-04-21 10:55:34 -0500399extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400
401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500402 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500404 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400407 *
408 * @return N/A
409 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400410extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400411
412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500413 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500415 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500417 * If @a thread is not currently sleeping, the routine has no effect.
418 *
419 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 *
421 * @return N/A
422 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400423extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400424
425/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500426 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500428 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400429 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400430extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431
432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500433 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * This routine prevents @a thread from executing if it has not yet started
436 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500438 * @param thread ID of thread to cancel.
439 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700440 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500441 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400443extern int k_thread_cancel(k_tid_t thread);
444
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400445/**
Allan Stephensc98da842016-11-11 15:45:03 -0500446 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500448 * This routine permanently stops execution of @a thread. The thread is taken
449 * off all kernel queues it is part of (i.e. the ready queue, the timeout
450 * queue, or a kernel object wait queue). However, any kernel resources the
451 * thread might currently own (such as mutexes or memory blocks) are not
452 * released. It is the responsibility of the caller of this routine to ensure
453 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456 *
457 * @return N/A
458 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400459extern void k_thread_abort(k_tid_t thread);
460
Allan Stephensc98da842016-11-11 15:45:03 -0500461/**
462 * @cond INTERNAL_HIDDEN
463 */
464
Benjamin Walshd211a522016-12-06 11:44:01 -0500465/* timeout has timed out and is not on _timeout_q anymore */
466#define _EXPIRED (-2)
467
468/* timeout is not in use */
469#define _INACTIVE (-1)
470
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400471struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400472 union {
473 char *init_stack;
474 struct k_thread *thread;
475 };
476 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500477 void (*init_entry)(void *, void *, void *);
478 void *init_p1;
479 void *init_p2;
480 void *init_p3;
481 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500482 u32_t init_options;
483 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500484 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500485 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400486};
487
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400488#define _THREAD_INITIALIZER(stack, stack_size, \
489 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500490 prio, options, delay, abort, groups) \
491 { \
Mazen NEIFER967cb2e2017-02-02 10:42:18 +0100492 {.init_stack = (stack)}, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500493 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400494 .init_entry = (void (*)(void *, void *, void *))entry, \
495 .init_p1 = (void *)p1, \
496 .init_p2 = (void *)p2, \
497 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500498 .init_prio = (prio), \
499 .init_options = (options), \
500 .init_delay = (delay), \
501 .init_abort = (abort), \
502 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503 }
504
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400505/**
Allan Stephensc98da842016-11-11 15:45:03 -0500506 * INTERNAL_HIDDEN @endcond
507 */
508
509/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500510 * @brief Statically define and initialize a thread.
511 *
512 * The thread may be scheduled for immediate execution or a delayed start.
513 *
514 * Thread options are architecture-specific, and can include K_ESSENTIAL,
515 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
516 * them using "|" (the logical OR operator).
517 *
518 * The ID of the thread can be accessed using:
519 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500520 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500521 *
522 * @param name Name of the thread.
523 * @param stack_size Stack size in bytes.
524 * @param entry Thread entry function.
525 * @param p1 1st entry point parameter.
526 * @param p2 2nd entry point parameter.
527 * @param p3 3rd entry point parameter.
528 * @param prio Thread priority.
529 * @param options Thread options.
530 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400531 *
532 * @internal It has been observed that the x86 compiler by default aligns
533 * these _static_thread_data structures to 32-byte boundaries, thereby
534 * wasting space. To work around this, force a 4-byte alignment.
535 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500536#define K_THREAD_DEFINE(name, stack_size, \
537 entry, p1, p2, p3, \
538 prio, options, delay) \
539 char __noinit __stack _k_thread_obj_##name[stack_size]; \
540 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500541 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500542 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
543 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500544 NULL, 0); \
545 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400546
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500548 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500550 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500552 * @param thread ID of thread whose priority is needed.
553 *
554 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400555 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500556extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400557
558/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500559 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400562 *
563 * Rescheduling can occur immediately depending on the priority @a thread is
564 * set to:
565 *
566 * - If its priority is raised above the priority of the caller of this
567 * function, and the caller is preemptible, @a thread will be scheduled in.
568 *
569 * - If the caller operates on itself, it lowers its priority below that of
570 * other threads in the system, and the caller is preemptible, the thread of
571 * highest priority will be scheduled in.
572 *
573 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
574 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
575 * highest priority.
576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578 * @param prio New priority.
579 *
580 * @warning Changing the priority of a thread currently involved in mutex
581 * priority inheritance may result in undefined behavior.
582 *
583 * @return N/A
584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400585extern void k_thread_priority_set(k_tid_t thread, int prio);
586
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500588 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500590 * This routine prevents the kernel scheduler from making @a thread the
591 * current thread. All other internal operations on @a thread are still
592 * performed; for example, any timeout it is waiting on keeps ticking,
593 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * If @a thread is already suspended, the routine has no effect.
596 *
597 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598 *
599 * @return N/A
600 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400601extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602
603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * This routine allows the kernel scheduler to make @a thread the current
607 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500609 * If @a thread is not currently suspended, the routine has no effect.
610 *
611 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
613 * @return N/A
614 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400615extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400616
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500618 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500620 * This routine specifies how the scheduler will perform time slicing of
621 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500623 * To enable time slicing, @a slice must be non-zero. The scheduler
624 * ensures that no thread runs for more than the specified time limit
625 * before other threads of that priority are given a chance to execute.
626 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700627 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 * execute. Once the scheduler selects a thread for execution, there is no
631 * minimum guaranteed time the thread will execute before threads of greater or
632 * equal priority are scheduled.
633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500634 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400635 * for execution, this routine has no effect; the thread is immediately
636 * rescheduled after the slice period expires.
637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500638 * To disable timeslicing, set both @a slice and @a prio to zero.
639 *
640 * @param slice Maximum time slice length (in milliseconds).
641 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400642 *
643 * @return N/A
644 */
Kumar Galacc334c72017-04-21 10:55:34 -0500645extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400646
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400647/**
Allan Stephensc98da842016-11-11 15:45:03 -0500648 * @} end defgroup thread_apis
649 */
650
651/**
652 * @addtogroup isr_apis
653 * @{
654 */
655
656/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500657 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658 *
Allan Stephensc98da842016-11-11 15:45:03 -0500659 * This routine allows the caller to customize its actions, depending on
660 * whether it is a thread or an ISR.
661 *
662 * @note Can be called by ISRs.
663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500664 * @return 0 if invoked by a thread.
665 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500667extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668
Benjamin Walsh445830d2016-11-10 15:54:27 -0500669/**
670 * @brief Determine if code is running in a preemptible thread.
671 *
Allan Stephensc98da842016-11-11 15:45:03 -0500672 * This routine allows the caller to customize its actions, depending on
673 * whether it can be preempted by another thread. The routine returns a 'true'
674 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500675 *
Allan Stephensc98da842016-11-11 15:45:03 -0500676 * - The code is running in a thread, not at ISR.
677 * - The thread's priority is in the preemptible range.
678 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500679 *
Allan Stephensc98da842016-11-11 15:45:03 -0500680 * @note Can be called by ISRs.
681 *
682 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500683 * @return Non-zero if invoked by a preemptible thread.
684 */
685extern int k_is_preempt_thread(void);
686
Allan Stephensc98da842016-11-11 15:45:03 -0500687/**
688 * @} end addtogroup isr_apis
689 */
690
691/**
692 * @addtogroup thread_apis
693 * @{
694 */
695
696/**
697 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500698 *
Allan Stephensc98da842016-11-11 15:45:03 -0500699 * This routine prevents the current thread from being preempted by another
700 * thread by instructing the scheduler to treat it as a cooperative thread.
701 * If the thread subsequently performs an operation that makes it unready,
702 * it will be context switched out in the normal manner. When the thread
703 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500704 *
Allan Stephensc98da842016-11-11 15:45:03 -0500705 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500706 *
Allan Stephensc98da842016-11-11 15:45:03 -0500707 * @note k_sched_lock() and k_sched_unlock() should normally be used
708 * when the operation being performed can be safely interrupted by ISRs.
709 * However, if the amount of processing involved is very small, better
710 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500711 *
712 * @return N/A
713 */
714extern void k_sched_lock(void);
715
Allan Stephensc98da842016-11-11 15:45:03 -0500716/**
717 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500718 *
Allan Stephensc98da842016-11-11 15:45:03 -0500719 * This routine reverses the effect of a previous call to k_sched_lock().
720 * A thread must call the routine once for each time it called k_sched_lock()
721 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500722 *
723 * @return N/A
724 */
725extern void k_sched_unlock(void);
726
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500732 * Custom data is not used by the kernel itself, and is freely available
733 * for a thread to use as it sees fit. It can be used as a framework
734 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500736 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400737 *
738 * @return N/A
739 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400740extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400741
742/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500743 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500745 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500747 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400749extern void *k_thread_custom_data_get(void);
750
751/**
Allan Stephensc98da842016-11-11 15:45:03 -0500752 * @} end addtogroup thread_apis
753 */
754
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400755#include <sys_clock.h>
756
Allan Stephensc2f15a42016-11-17 12:24:22 -0500757/**
758 * @addtogroup clock_apis
759 * @{
760 */
761
762/**
763 * @brief Generate null timeout delay.
764 *
765 * This macro generates a timeout delay that that instructs a kernel API
766 * not to wait if the requested operation cannot be performed immediately.
767 *
768 * @return Timeout delay value.
769 */
770#define K_NO_WAIT 0
771
772/**
773 * @brief Generate timeout delay from milliseconds.
774 *
775 * This macro generates a timeout delay that that instructs a kernel API
776 * to wait up to @a ms milliseconds to perform the requested operation.
777 *
778 * @param ms Duration in milliseconds.
779 *
780 * @return Timeout delay value.
781 */
Johan Hedberg14471692016-11-13 10:52:15 +0200782#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500783
784/**
785 * @brief Generate timeout delay from seconds.
786 *
787 * This macro generates a timeout delay that that instructs a kernel API
788 * to wait up to @a s seconds to perform the requested operation.
789 *
790 * @param s Duration in seconds.
791 *
792 * @return Timeout delay value.
793 */
Johan Hedberg14471692016-11-13 10:52:15 +0200794#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500795
796/**
797 * @brief Generate timeout delay from minutes.
798 *
799 * This macro generates a timeout delay that that instructs a kernel API
800 * to wait up to @a m minutes to perform the requested operation.
801 *
802 * @param m Duration in minutes.
803 *
804 * @return Timeout delay value.
805 */
Johan Hedberg14471692016-11-13 10:52:15 +0200806#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500807
808/**
809 * @brief Generate timeout delay from hours.
810 *
811 * This macro generates a timeout delay that that instructs a kernel API
812 * to wait up to @a h hours to perform the requested operation.
813 *
814 * @param h Duration in hours.
815 *
816 * @return Timeout delay value.
817 */
Johan Hedberg14471692016-11-13 10:52:15 +0200818#define K_HOURS(h) K_MINUTES((h) * 60)
819
Allan Stephensc98da842016-11-11 15:45:03 -0500820/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500821 * @brief Generate infinite timeout delay.
822 *
823 * This macro generates a timeout delay that that instructs a kernel API
824 * to wait as long as necessary to perform the requested operation.
825 *
826 * @return Timeout delay value.
827 */
828#define K_FOREVER (-1)
829
830/**
831 * @} end addtogroup clock_apis
832 */
833
834/**
Allan Stephensc98da842016-11-11 15:45:03 -0500835 * @cond INTERNAL_HIDDEN
836 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400837
Benjamin Walsh62092182016-12-20 14:39:08 -0500838/* kernel clocks */
839
840#if (sys_clock_ticks_per_sec == 1000) || \
841 (sys_clock_ticks_per_sec == 500) || \
842 (sys_clock_ticks_per_sec == 250) || \
843 (sys_clock_ticks_per_sec == 125) || \
844 (sys_clock_ticks_per_sec == 100) || \
845 (sys_clock_ticks_per_sec == 50) || \
846 (sys_clock_ticks_per_sec == 25) || \
847 (sys_clock_ticks_per_sec == 20) || \
848 (sys_clock_ticks_per_sec == 10) || \
849 (sys_clock_ticks_per_sec == 1)
850
851 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
852#else
853 /* yields horrible 64-bit math on many architectures: try to avoid */
854 #define _NON_OPTIMIZED_TICKS_PER_SEC
855#endif
856
857#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500858extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500859#else
Kumar Galacc334c72017-04-21 10:55:34 -0500860static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500861{
Kumar Galacc334c72017-04-21 10:55:34 -0500862 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500863}
864#endif
865
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500866/* added tick needed to account for tick in progress */
867#define _TICK_ALIGN 1
868
Kumar Galacc334c72017-04-21 10:55:34 -0500869static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400870{
Benjamin Walsh62092182016-12-20 14:39:08 -0500871#ifdef CONFIG_SYS_CLOCK_EXISTS
872
873#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500874 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400875#else
Kumar Galacc334c72017-04-21 10:55:34 -0500876 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500877#endif
878
879#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400880 __ASSERT(ticks == 0, "");
881 return 0;
882#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400883}
884
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400885struct k_timer {
886 /*
887 * _timeout structure must be first here if we want to use
888 * dynamic timer allocation. timeout.node is used in the double-linked
889 * list of free timers
890 */
891 struct _timeout timeout;
892
Allan Stephens45bfa372016-10-12 12:39:42 -0500893 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400894 _wait_q_t wait_q;
895
896 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500897 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400898
899 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500900 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400901
902 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500903 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904
Allan Stephens45bfa372016-10-12 12:39:42 -0500905 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500906 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500908 /* user-specific data, also used to support legacy features */
909 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400910
Anas Nashif2f203c22016-12-18 06:57:45 -0500911 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912};
913
Allan Stephens1342adb2016-11-03 13:54:53 -0500914#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400915 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500916 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500917 .timeout.wait_q = NULL, \
918 .timeout.thread = NULL, \
919 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400920 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500921 .expiry_fn = expiry, \
922 .stop_fn = stop, \
923 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500924 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500925 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400926 }
927
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928/**
Allan Stephensc98da842016-11-11 15:45:03 -0500929 * INTERNAL_HIDDEN @endcond
930 */
931
932/**
933 * @defgroup timer_apis Timer APIs
934 * @ingroup kernel_apis
935 * @{
936 */
937
938/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500939 * @typedef k_timer_expiry_t
940 * @brief Timer expiry function type.
941 *
942 * A timer's expiry function is executed by the system clock interrupt handler
943 * each time the timer expires. The expiry function is optional, and is only
944 * invoked if the timer has been initialized with one.
945 *
946 * @param timer Address of timer.
947 *
948 * @return N/A
949 */
950typedef void (*k_timer_expiry_t)(struct k_timer *timer);
951
952/**
953 * @typedef k_timer_stop_t
954 * @brief Timer stop function type.
955 *
956 * A timer's stop function is executed if the timer is stopped prematurely.
957 * The function runs in the context of the thread that stops the timer.
958 * The stop function is optional, and is only invoked if the timer has been
959 * initialized with one.
960 *
961 * @param timer Address of timer.
962 *
963 * @return N/A
964 */
965typedef void (*k_timer_stop_t)(struct k_timer *timer);
966
967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500970 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500972 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
974 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @param expiry_fn Function to invoke each time the timer expires.
976 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500978#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500979 struct k_timer name \
980 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500981 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400982
Allan Stephens45bfa372016-10-12 12:39:42 -0500983/**
984 * @brief Initialize a timer.
985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500987 *
988 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * @param expiry_fn Function to invoke each time the timer expires.
990 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500991 *
992 * @return N/A
993 */
994extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500995 k_timer_expiry_t expiry_fn,
996 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700997
Allan Stephens45bfa372016-10-12 12:39:42 -0500998/**
999 * @brief Start a timer.
1000 *
1001 * This routine starts a timer, and resets its status to zero. The timer
1002 * begins counting down using the specified duration and period values.
1003 *
1004 * Attempting to start a timer that is already running is permitted.
1005 * The timer's status is reset to zero and the timer begins counting down
1006 * using the new duration and period values.
1007 *
1008 * @param timer Address of timer.
1009 * @param duration Initial timer duration (in milliseconds).
1010 * @param period Timer period (in milliseconds).
1011 *
1012 * @return N/A
1013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001014extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001015 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001016
1017/**
1018 * @brief Stop a timer.
1019 *
1020 * This routine stops a running timer prematurely. The timer's stop function,
1021 * if one exists, is invoked by the caller.
1022 *
1023 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001025 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001026 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1027 * if @a k_timer_stop is to be called from ISRs.
1028 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001029 * @param timer Address of timer.
1030 *
1031 * @return N/A
1032 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001034
1035/**
1036 * @brief Read timer status.
1037 *
1038 * This routine reads the timer's status, which indicates the number of times
1039 * it has expired since its status was last read.
1040 *
1041 * Calling this routine resets the timer's status to zero.
1042 *
1043 * @param timer Address of timer.
1044 *
1045 * @return Timer status.
1046 */
Kumar Galacc334c72017-04-21 10:55:34 -05001047extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001048
1049/**
1050 * @brief Synchronize thread to timer expiration.
1051 *
1052 * This routine blocks the calling thread until the timer's status is non-zero
1053 * (indicating that it has expired at least once since it was last examined)
1054 * or the timer is stopped. If the timer status is already non-zero,
1055 * or the timer is already stopped, the caller continues without waiting.
1056 *
1057 * Calling this routine resets the timer's status to zero.
1058 *
1059 * This routine must not be used by interrupt handlers, since they are not
1060 * allowed to block.
1061 *
1062 * @param timer Address of timer.
1063 *
1064 * @return Timer status.
1065 */
Kumar Galacc334c72017-04-21 10:55:34 -05001066extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001067
1068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001070 *
1071 * This routine computes the (approximate) time remaining before a running
1072 * timer next expires. If the timer is not running, it returns zero.
1073 *
1074 * @param timer Address of timer.
1075 *
1076 * @return Remaining time (in milliseconds).
1077 */
Kumar Galacc334c72017-04-21 10:55:34 -05001078static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001079{
1080 return _timeout_remaining_get(&timer->timeout);
1081}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001082
Allan Stephensc98da842016-11-11 15:45:03 -05001083/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001084 * @brief Associate user-specific data with a timer.
1085 *
1086 * This routine records the @a user_data with the @a timer, to be retrieved
1087 * later.
1088 *
1089 * It can be used e.g. in a timer handler shared across multiple subsystems to
1090 * retrieve data specific to the subsystem this timer is associated with.
1091 *
1092 * @param timer Address of timer.
1093 * @param user_data User data to associate with the timer.
1094 *
1095 * @return N/A
1096 */
1097static inline void k_timer_user_data_set(struct k_timer *timer,
1098 void *user_data)
1099{
1100 timer->user_data = user_data;
1101}
1102
1103/**
1104 * @brief Retrieve the user-specific data from a timer.
1105 *
1106 * @param timer Address of timer.
1107 *
1108 * @return The user data.
1109 */
1110static inline void *k_timer_user_data_get(struct k_timer *timer)
1111{
1112 return timer->user_data;
1113}
1114
1115/**
Allan Stephensc98da842016-11-11 15:45:03 -05001116 * @} end defgroup timer_apis
1117 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001118
Allan Stephensc98da842016-11-11 15:45:03 -05001119/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001120 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001121 * @{
1122 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001123
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001124/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * This routine returns the elapsed time since the system booted,
1128 * in milliseconds.
1129 *
1130 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001131 */
Kumar Galacc334c72017-04-21 10:55:34 -05001132extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001133
1134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * This routine returns the lower 32-bits of the elapsed time since the system
1138 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001140 * This routine can be more efficient than k_uptime_get(), as it reduces the
1141 * need for interrupt locking and 64-bit math. However, the 32-bit result
1142 * cannot hold a system uptime time larger than approximately 50 days, so the
1143 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001146 */
Kumar Galacc334c72017-04-21 10:55:34 -05001147extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001148
1149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * This routine computes the elapsed time between the current system uptime
1153 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001155 * @param reftime Pointer to a reference time, which is updated to the current
1156 * uptime upon return.
1157 *
1158 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001159 */
Kumar Galacc334c72017-04-21 10:55:34 -05001160extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001161
1162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * This routine computes the elapsed time between the current system uptime
1166 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1169 * need for interrupt locking and 64-bit math. However, the 32-bit result
1170 * cannot hold an elapsed time larger than approximately 50 days, so the
1171 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001173 * @param reftime Pointer to a reference time, which is updated to the current
1174 * uptime upon return.
1175 *
1176 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001177 */
Kumar Galacc334c72017-04-21 10:55:34 -05001178extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001179
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * This routine returns the current time, as measured by the system's hardware
1184 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001188#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001190/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001191 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192 */
1193
Allan Stephensc98da842016-11-11 15:45:03 -05001194/**
1195 * @cond INTERNAL_HIDDEN
1196 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001198struct k_queue {
1199 _wait_q_t wait_q;
1200 sys_slist_t data_q;
1201 _POLL_EVENT;
1202
1203 _OBJECT_TRACING_NEXT_PTR(k_queue);
1204};
1205
1206#define K_QUEUE_INITIALIZER(obj) \
1207 { \
1208 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1209 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1210 _POLL_EVENT_OBJ_INIT \
1211 _OBJECT_TRACING_INIT \
1212 }
1213
1214/**
1215 * INTERNAL_HIDDEN @endcond
1216 */
1217
1218/**
1219 * @defgroup queue_apis Queue APIs
1220 * @ingroup kernel_apis
1221 * @{
1222 */
1223
1224/**
1225 * @brief Initialize a queue.
1226 *
1227 * This routine initializes a queue object, prior to its first use.
1228 *
1229 * @param queue Address of the queue.
1230 *
1231 * @return N/A
1232 */
1233extern void k_queue_init(struct k_queue *queue);
1234
1235/**
1236 * @brief Append an element to the end of a queue.
1237 *
1238 * This routine appends a data item to @a queue. A queue data item must be
1239 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1240 * reserved for the kernel's use.
1241 *
1242 * @note Can be called by ISRs.
1243 *
1244 * @param queue Address of the queue.
1245 * @param data Address of the data item.
1246 *
1247 * @return N/A
1248 */
1249extern void k_queue_append(struct k_queue *queue, void *data);
1250
1251/**
1252 * @brief Prepend an element to a queue.
1253 *
1254 * This routine prepends a data item to @a queue. A queue data item must be
1255 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1256 * reserved for the kernel's use.
1257 *
1258 * @note Can be called by ISRs.
1259 *
1260 * @param queue Address of the queue.
1261 * @param data Address of the data item.
1262 *
1263 * @return N/A
1264 */
1265extern void k_queue_prepend(struct k_queue *queue, void *data);
1266
1267/**
1268 * @brief Inserts an element to a queue.
1269 *
1270 * This routine inserts a data item to @a queue after previous item. A queue
1271 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1272 * item are reserved for the kernel's use.
1273 *
1274 * @note Can be called by ISRs.
1275 *
1276 * @param queue Address of the queue.
1277 * @param prev Address of the previous data item.
1278 * @param data Address of the data item.
1279 *
1280 * @return N/A
1281 */
1282extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1283
1284/**
1285 * @brief Atomically append a list of elements to a queue.
1286 *
1287 * This routine adds a list of data items to @a queue in one operation.
1288 * The data items must be in a singly-linked list, with the first 32 bits
1289 * in each data item pointing to the next data item; the list must be
1290 * NULL-terminated.
1291 *
1292 * @note Can be called by ISRs.
1293 *
1294 * @param queue Address of the queue.
1295 * @param head Pointer to first node in singly-linked list.
1296 * @param tail Pointer to last node in singly-linked list.
1297 *
1298 * @return N/A
1299 */
1300extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1301
1302/**
1303 * @brief Atomically add a list of elements to a queue.
1304 *
1305 * This routine adds a list of data items to @a queue in one operation.
1306 * The data items must be in a singly-linked list implemented using a
1307 * sys_slist_t object. Upon completion, the original list is empty.
1308 *
1309 * @note Can be called by ISRs.
1310 *
1311 * @param queue Address of the queue.
1312 * @param list Pointer to sys_slist_t object.
1313 *
1314 * @return N/A
1315 */
1316extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1317
1318/**
1319 * @brief Get an element from a queue.
1320 *
1321 * This routine removes first data item from @a queue. The first 32 bits of the
1322 * data item are reserved for the kernel's use.
1323 *
1324 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1325 *
1326 * @param queue Address of the queue.
1327 * @param timeout Waiting period to obtain a data item (in milliseconds),
1328 * or one of the special values K_NO_WAIT and K_FOREVER.
1329 *
1330 * @return Address of the data item if successful; NULL if returned
1331 * without waiting, or waiting period timed out.
1332 */
Kumar Galacc334c72017-04-21 10:55:34 -05001333extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001334
1335/**
1336 * @brief Query a queue to see if it has data available.
1337 *
1338 * Note that the data might be already gone by the time this function returns
1339 * if other threads are also trying to read from the queue.
1340 *
1341 * @note Can be called by ISRs.
1342 *
1343 * @param queue Address of the queue.
1344 *
1345 * @return Non-zero if the queue is empty.
1346 * @return 0 if data is available.
1347 */
1348static inline int k_queue_is_empty(struct k_queue *queue)
1349{
1350 return (int)sys_slist_is_empty(&queue->data_q);
1351}
1352
1353/**
1354 * @brief Statically define and initialize a queue.
1355 *
1356 * The queue can be accessed outside the module where it is defined using:
1357 *
1358 * @code extern struct k_queue <name>; @endcode
1359 *
1360 * @param name Name of the queue.
1361 */
1362#define K_QUEUE_DEFINE(name) \
1363 struct k_queue name \
1364 __in_section(_k_queue, static, name) = \
1365 K_QUEUE_INITIALIZER(name)
1366
1367/**
1368 * @} end defgroup queue_apis
1369 */
1370
1371/**
1372 * @cond INTERNAL_HIDDEN
1373 */
1374
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001375struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001376 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377};
1378
Allan Stephensc98da842016-11-11 15:45:03 -05001379#define K_FIFO_INITIALIZER(obj) \
1380 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001381 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001382 }
1383
1384/**
1385 * INTERNAL_HIDDEN @endcond
1386 */
1387
1388/**
1389 * @defgroup fifo_apis Fifo APIs
1390 * @ingroup kernel_apis
1391 * @{
1392 */
1393
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001395 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001400 *
1401 * @return N/A
1402 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001403#define k_fifo_init(fifo) \
1404 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001405
1406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 * This routine adds a data item to @a fifo. A fifo data item must be
1410 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1411 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001413 * @note Can be called by ISRs.
1414 *
1415 * @param fifo Address of the fifo.
1416 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001417 *
1418 * @return N/A
1419 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001420#define k_fifo_put(fifo, data) \
1421 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001422
1423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001426 * This routine adds a list of data items to @a fifo in one operation.
1427 * The data items must be in a singly-linked list, with the first 32 bits
1428 * each data item pointing to the next data item; the list must be
1429 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001433 * @param fifo Address of the fifo.
1434 * @param head Pointer to first node in singly-linked list.
1435 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436 *
1437 * @return N/A
1438 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001439#define k_fifo_put_list(fifo, head, tail) \
1440 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001441
1442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001443 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001445 * This routine adds a list of data items to @a fifo in one operation.
1446 * The data items must be in a singly-linked list implemented using a
1447 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448 * and must be re-initialized via sys_slist_init().
1449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * @note Can be called by ISRs.
1451 *
1452 * @param fifo Address of the fifo.
1453 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001454 *
1455 * @return N/A
1456 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001457#define k_fifo_put_slist(fifo, list) \
1458 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459
1460/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001461 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001463 * This routine removes a data item from @a fifo in a "first in, first out"
1464 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1467 *
1468 * @param fifo Address of the fifo.
1469 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001470 * or one of the special values K_NO_WAIT and K_FOREVER.
1471 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001472 * @return Address of the data item if successful; NULL if returned
1473 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001475#define k_fifo_get(fifo, timeout) \
1476 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001477
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001479 * @brief Query a fifo to see if it has data available.
1480 *
1481 * Note that the data might be already gone by the time this function returns
1482 * if other threads is also trying to read from the fifo.
1483 *
1484 * @note Can be called by ISRs.
1485 *
1486 * @param fifo Address of the fifo.
1487 *
1488 * @return Non-zero if the fifo is empty.
1489 * @return 0 if data is available.
1490 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001491#define k_fifo_is_empty(fifo) \
1492 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001493
1494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001497 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001499 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001501 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001504 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001505 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001506 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507
Allan Stephensc98da842016-11-11 15:45:03 -05001508/**
1509 * @} end defgroup fifo_apis
1510 */
1511
1512/**
1513 * @cond INTERNAL_HIDDEN
1514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515
1516struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001517 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001518};
1519
Allan Stephensc98da842016-11-11 15:45:03 -05001520#define K_LIFO_INITIALIZER(obj) \
1521 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001522 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001523 }
1524
1525/**
1526 * INTERNAL_HIDDEN @endcond
1527 */
1528
1529/**
1530 * @defgroup lifo_apis Lifo APIs
1531 * @ingroup kernel_apis
1532 * @{
1533 */
1534
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001536 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001538 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001540 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001541 *
1542 * @return N/A
1543 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001544#define k_lifo_init(lifo) \
1545 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001546
1547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001548 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001550 * This routine adds a data item to @a lifo. A lifo data item must be
1551 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1552 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * @note Can be called by ISRs.
1555 *
1556 * @param lifo Address of the lifo.
1557 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001558 *
1559 * @return N/A
1560 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001561#define k_lifo_put(lifo, data) \
1562 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001563
1564/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001565 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * This routine removes a data item from @a lifo in a "last in, first out"
1568 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001570 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1571 *
1572 * @param lifo Address of the lifo.
1573 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001574 * or one of the special values K_NO_WAIT and K_FOREVER.
1575 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001576 * @return Address of the data item if successful; NULL if returned
1577 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001578 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001579#define k_lifo_get(lifo, timeout) \
1580 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001581
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001583 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001585 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001586 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001587 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001592 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001593 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001594 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001595
Allan Stephensc98da842016-11-11 15:45:03 -05001596/**
1597 * @} end defgroup lifo_apis
1598 */
1599
1600/**
1601 * @cond INTERNAL_HIDDEN
1602 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001603
1604struct k_stack {
1605 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001606 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001607
Anas Nashif2f203c22016-12-18 06:57:45 -05001608 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001609};
1610
Allan Stephensc98da842016-11-11 15:45:03 -05001611#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1612 { \
1613 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1614 .base = stack_buffer, \
1615 .next = stack_buffer, \
1616 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001617 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001618 }
1619
1620/**
1621 * INTERNAL_HIDDEN @endcond
1622 */
1623
1624/**
1625 * @defgroup stack_apis Stack APIs
1626 * @ingroup kernel_apis
1627 * @{
1628 */
1629
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001630/**
1631 * @brief Initialize a stack.
1632 *
1633 * This routine initializes a stack object, prior to its first use.
1634 *
1635 * @param stack Address of the stack.
1636 * @param buffer Address of array used to hold stacked values.
1637 * @param num_entries Maximum number of values that can be stacked.
1638 *
1639 * @return N/A
1640 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001641extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001642 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001643
1644/**
1645 * @brief Push an element onto a stack.
1646 *
1647 * This routine adds a 32-bit value @a data to @a stack.
1648 *
1649 * @note Can be called by ISRs.
1650 *
1651 * @param stack Address of the stack.
1652 * @param data Value to push onto the stack.
1653 *
1654 * @return N/A
1655 */
Kumar Galacc334c72017-04-21 10:55:34 -05001656extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657
1658/**
1659 * @brief Pop an element from a stack.
1660 *
1661 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1662 * manner and stores the value in @a data.
1663 *
1664 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1665 *
1666 * @param stack Address of the stack.
1667 * @param data Address of area to hold the value popped from the stack.
1668 * @param timeout Waiting period to obtain a value (in milliseconds),
1669 * or one of the special values K_NO_WAIT and K_FOREVER.
1670 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001671 * @retval 0 Element popped from stack.
1672 * @retval -EBUSY Returned without waiting.
1673 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 */
Kumar Galacc334c72017-04-21 10:55:34 -05001675extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001676
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001680 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001681 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001682 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * @param name Name of the stack.
1685 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001687#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001688 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001689 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001690 struct k_stack name \
1691 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001692 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1693 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001695/**
Allan Stephensc98da842016-11-11 15:45:03 -05001696 * @} end defgroup stack_apis
1697 */
1698
Allan Stephens6bba9b02016-11-16 14:56:54 -05001699struct k_work;
1700
Allan Stephensc98da842016-11-11 15:45:03 -05001701/**
1702 * @defgroup workqueue_apis Workqueue Thread APIs
1703 * @ingroup kernel_apis
1704 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001705 */
1706
Allan Stephens6bba9b02016-11-16 14:56:54 -05001707/**
1708 * @typedef k_work_handler_t
1709 * @brief Work item handler function type.
1710 *
1711 * A work item's handler function is executed by a workqueue's thread
1712 * when the work item is processed by the workqueue.
1713 *
1714 * @param work Address of the work item.
1715 *
1716 * @return N/A
1717 */
1718typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001719
1720/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001721 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001722 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001723
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724struct k_work_q {
1725 struct k_fifo fifo;
1726};
1727
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001729 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001730};
1731
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732struct k_work {
1733 void *_reserved; /* Used by k_fifo implementation. */
1734 k_work_handler_t handler;
1735 atomic_t flags[1];
1736};
1737
Allan Stephens6bba9b02016-11-16 14:56:54 -05001738struct k_delayed_work {
1739 struct k_work work;
1740 struct _timeout timeout;
1741 struct k_work_q *work_q;
1742};
1743
1744extern struct k_work_q k_sys_work_q;
1745
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001747 * INTERNAL_HIDDEN @endcond
1748 */
1749
1750/**
1751 * @brief Initialize a statically-defined work item.
1752 *
1753 * This macro can be used to initialize a statically-defined workqueue work
1754 * item, prior to its first use. For example,
1755 *
1756 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1757 *
1758 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759 */
1760#define K_WORK_INITIALIZER(work_handler) \
1761 { \
1762 ._reserved = NULL, \
1763 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001764 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001765 }
1766
1767/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001768 * @brief Initialize a work item.
1769 *
1770 * This routine initializes a workqueue work item, prior to its first use.
1771 *
1772 * @param work Address of work item.
1773 * @param handler Function to invoke each time work item is processed.
1774 *
1775 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001776 */
1777static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1778{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001779 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001780 work->handler = handler;
1781}
1782
1783/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001784 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001785 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001786 * This routine submits work item @a work to be processed by workqueue
1787 * @a work_q. If the work item is already pending in the workqueue's queue
1788 * as a result of an earlier submission, this routine has no effect on the
1789 * work item. If the work item has already been processed, or is currently
1790 * being processed, its work is considered complete and the work item can be
1791 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001792 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001793 * @warning
1794 * A submitted work item must not be modified until it has been processed
1795 * by the workqueue.
1796 *
1797 * @note Can be called by ISRs.
1798 *
1799 * @param work_q Address of workqueue.
1800 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001801 *
1802 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803 */
1804static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1805 struct k_work *work)
1806{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001807 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001808 k_fifo_put(&work_q->fifo, work);
1809 }
1810}
1811
1812/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001813 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001815 * This routine indicates if work item @a work is pending in a workqueue's
1816 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001818 * @note Can be called by ISRs.
1819 *
1820 * @param work Address of work item.
1821 *
1822 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001823 */
1824static inline int k_work_pending(struct k_work *work)
1825{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001826 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001827}
1828
1829/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001830 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001831 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001832 * This routine starts workqueue @a work_q. The workqueue spawns its work
1833 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001835 * @param work_q Address of workqueue.
1836 * @param stack Pointer to work queue thread's stack space.
1837 * @param stack_size Size of the work queue thread's stack (in bytes).
1838 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839 *
1840 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001841 */
Allan Stephens904cf972016-10-07 13:59:23 -05001842extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001843 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001844
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001846 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001848 * This routine initializes a workqueue delayed work item, prior to
1849 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001851 * @param work Address of delayed work item.
1852 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
1854 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001856extern void k_delayed_work_init(struct k_delayed_work *work,
1857 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858
1859/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001860 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001861 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001862 * This routine schedules work item @a work to be processed by workqueue
1863 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001864 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001865 * Only when the countdown completes is the work item actually submitted to
1866 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001867 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001868 * Submitting a previously submitted delayed work item that is still
1869 * counting down cancels the existing submission and restarts the countdown
1870 * using the new delay. If the work item is currently pending on the
1871 * workqueue's queue because the countdown has completed it is too late to
1872 * resubmit the item, and resubmission fails without impacting the work item.
1873 * If the work item has already been processed, or is currently being processed,
1874 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001875 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001876 * @warning
1877 * A delayed work item must not be modified until it has been processed
1878 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001880 * @note Can be called by ISRs.
1881 *
1882 * @param work_q Address of workqueue.
1883 * @param work Address of delayed work item.
1884 * @param delay Delay before submitting the work item (in milliseconds).
1885 *
1886 * @retval 0 Work item countdown started.
1887 * @retval -EINPROGRESS Work item is already pending.
1888 * @retval -EINVAL Work item is being processed or has completed its work.
1889 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001891extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1892 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05001893 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001894
1895/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001896 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001898 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07001899 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05001900 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001902 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001904 * @param work Address of delayed work item.
1905 *
David B. Kinder8b986d72017-04-18 15:56:26 -07001906 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001907 * @retval -EINPROGRESS Work item is already pending.
1908 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001910extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913 * @brief Submit a work item to the system workqueue.
1914 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001915 * This routine submits work item @a work to be processed by the system
1916 * workqueue. If the work item is already pending in the workqueue's queue
1917 * as a result of an earlier submission, this routine has no effect on the
1918 * work item. If the work item has already been processed, or is currently
1919 * being processed, its work is considered complete and the work item can be
1920 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001922 * @warning
1923 * Work items submitted to the system workqueue should avoid using handlers
1924 * that block or yield since this may prevent the system workqueue from
1925 * processing other work items in a timely manner.
1926 *
1927 * @note Can be called by ISRs.
1928 *
1929 * @param work Address of work item.
1930 *
1931 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932 */
1933static inline void k_work_submit(struct k_work *work)
1934{
1935 k_work_submit_to_queue(&k_sys_work_q, work);
1936}
1937
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001938/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 * @brief Submit a delayed work item to the system workqueue.
1940 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001941 * This routine schedules work item @a work to be processed by the system
1942 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001943 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001944 * Only when the countdown completes is the work item actually submitted to
1945 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001947 * Submitting a previously submitted delayed work item that is still
1948 * counting down cancels the existing submission and restarts the countdown
1949 * using the new delay. If the work item is currently pending on the
1950 * workqueue's queue because the countdown has completed it is too late to
1951 * resubmit the item, and resubmission fails without impacting the work item.
1952 * If the work item has already been processed, or is currently being processed,
1953 * its work is considered complete and the work item can be resubmitted.
1954 *
1955 * @warning
1956 * Work items submitted to the system workqueue should avoid using handlers
1957 * that block or yield since this may prevent the system workqueue from
1958 * processing other work items in a timely manner.
1959 *
1960 * @note Can be called by ISRs.
1961 *
1962 * @param work Address of delayed work item.
1963 * @param delay Delay before submitting the work item (in milliseconds).
1964 *
1965 * @retval 0 Work item countdown started.
1966 * @retval -EINPROGRESS Work item is already pending.
1967 * @retval -EINVAL Work item is being processed or has completed its work.
1968 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969 */
1970static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05001971 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001972{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001973 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974}
1975
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001977 * @brief Get time remaining before a delayed work gets scheduled.
1978 *
1979 * This routine computes the (approximate) time remaining before a
1980 * delayed work gets executed. If the delayed work is not waiting to be
1981 * schedules, it returns zero.
1982 *
1983 * @param work Delayed work item.
1984 *
1985 * @return Remaining time (in milliseconds).
1986 */
Kumar Galacc334c72017-04-21 10:55:34 -05001987static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02001988{
1989 return _timeout_remaining_get(&work->timeout);
1990}
1991
1992/**
Allan Stephensc98da842016-11-11 15:45:03 -05001993 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001994 */
1995
Allan Stephensc98da842016-11-11 15:45:03 -05001996/**
1997 * @cond INTERNAL_HIDDEN
1998 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
2000struct k_mutex {
2001 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002002 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002003 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002005
Anas Nashif2f203c22016-12-18 06:57:45 -05002006 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007};
2008
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009#define K_MUTEX_INITIALIZER(obj) \
2010 { \
2011 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2012 .owner = NULL, \
2013 .lock_count = 0, \
2014 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002015 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002016 }
2017
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018/**
Allan Stephensc98da842016-11-11 15:45:03 -05002019 * INTERNAL_HIDDEN @endcond
2020 */
2021
2022/**
2023 * @defgroup mutex_apis Mutex APIs
2024 * @ingroup kernel_apis
2025 * @{
2026 */
2027
2028/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002029 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002031 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002032 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002033 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002038 struct k_mutex name \
2039 __in_section(_k_mutex, static, name) = \
2040 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002043 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002047 * Upon completion, the mutex is available and does not have an owner.
2048 *
2049 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
2051 * @return N/A
2052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054
2055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * This routine locks @a mutex. If the mutex is locked by another thread,
2059 * the calling thread waits until the mutex becomes available or until
2060 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002062 * A thread is permitted to lock a mutex it has already locked. The operation
2063 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 * @param mutex Address of the mutex.
2066 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 * or one of the special values K_NO_WAIT and K_FOREVER.
2068 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002069 * @retval 0 Mutex locked.
2070 * @retval -EBUSY Returned without waiting.
2071 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 */
Kumar Galacc334c72017-04-21 10:55:34 -05002073extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074
2075/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002078 * This routine unlocks @a mutex. The mutex must already be locked by the
2079 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002080 *
2081 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083 * thread.
2084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
2087 * @return N/A
2088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089extern void k_mutex_unlock(struct k_mutex *mutex);
2090
Allan Stephensc98da842016-11-11 15:45:03 -05002091/**
2092 * @} end defgroup mutex_apis
2093 */
2094
2095/**
2096 * @cond INTERNAL_HIDDEN
2097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098
2099struct k_sem {
2100 _wait_q_t wait_q;
2101 unsigned int count;
2102 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002103 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104
Anas Nashif2f203c22016-12-18 06:57:45 -05002105 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106};
2107
Allan Stephensc98da842016-11-11 15:45:03 -05002108#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2109 { \
2110 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2111 .count = initial_count, \
2112 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002113 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002114 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002115 }
2116
2117/**
2118 * INTERNAL_HIDDEN @endcond
2119 */
2120
2121/**
2122 * @defgroup semaphore_apis Semaphore APIs
2123 * @ingroup kernel_apis
2124 * @{
2125 */
2126
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @param sem Address of the semaphore.
2133 * @param initial_count Initial semaphore count.
2134 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002135 *
2136 * @return N/A
2137 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002138extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2139 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002140
2141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002142 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2147 *
2148 * @param sem Address of the semaphore.
2149 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002150 * or one of the special values K_NO_WAIT and K_FOREVER.
2151 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002152 * @note When porting code from the nanokernel legacy API to the new API, be
2153 * careful with the return value of this function. The return value is the
2154 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2155 * non-zero means failure, while the nano_sem_take family returns 1 for success
2156 * and 0 for failure.
2157 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002158 * @retval 0 Semaphore taken.
2159 * @retval -EBUSY Returned without waiting.
2160 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002161 */
Kumar Galacc334c72017-04-21 10:55:34 -05002162extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002163
2164/**
2165 * @brief Give a semaphore.
2166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * This routine gives @a sem, unless the semaphore is already at its maximum
2168 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002173 *
2174 * @return N/A
2175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176extern void k_sem_give(struct k_sem *sem);
2177
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002178/**
2179 * @brief Reset a semaphore's count to zero.
2180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002184 *
2185 * @return N/A
2186 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002187static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188{
2189 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190}
2191
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002192/**
2193 * @brief Get a semaphore's count.
2194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002197 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002200 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002201static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202{
2203 return sem->count;
2204}
2205
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002207 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002209 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002210 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002211 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002213 * @param name Name of the semaphore.
2214 * @param initial_count Initial semaphore count.
2215 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002218 struct k_sem name \
2219 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 K_SEM_INITIALIZER(name, initial_count, count_limit)
2221
Allan Stephensc98da842016-11-11 15:45:03 -05002222/**
2223 * @} end defgroup semaphore_apis
2224 */
2225
2226/**
2227 * @defgroup alert_apis Alert APIs
2228 * @ingroup kernel_apis
2229 * @{
2230 */
2231
Allan Stephens5eceb852016-11-16 10:16:30 -05002232/**
2233 * @typedef k_alert_handler_t
2234 * @brief Alert handler function type.
2235 *
2236 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002237 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002238 * and is only invoked if the alert has been initialized with one.
2239 *
2240 * @param alert Address of the alert.
2241 *
2242 * @return 0 if alert has been consumed; non-zero if alert should pend.
2243 */
2244typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002245
2246/**
2247 * @} end defgroup alert_apis
2248 */
2249
2250/**
2251 * @cond INTERNAL_HIDDEN
2252 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002254#define K_ALERT_DEFAULT NULL
2255#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002257struct k_alert {
2258 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259 atomic_t send_count;
2260 struct k_work work_item;
2261 struct k_sem sem;
2262
Anas Nashif2f203c22016-12-18 06:57:45 -05002263 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264};
2265
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002266extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002268#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002270 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002272 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002273 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002274 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275 }
2276
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277/**
Allan Stephensc98da842016-11-11 15:45:03 -05002278 * INTERNAL_HIDDEN @endcond
2279 */
2280
2281/**
2282 * @addtogroup alert_apis
2283 * @{
2284 */
2285
2286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002289 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002291 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @param name Name of the alert.
2294 * @param alert_handler Action to take when alert is sent. Specify either
2295 * the address of a function to be invoked by the system workqueue
2296 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2297 * K_ALERT_DEFAULT (which causes the alert to pend).
2298 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002300#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002301 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002302 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002303 K_ALERT_INITIALIZER(name, alert_handler, \
2304 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002307 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002309 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002311 * @param alert Address of the alert.
2312 * @param handler Action to take when alert is sent. Specify either the address
2313 * of a function to be invoked by the system workqueue thread,
2314 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2315 * K_ALERT_DEFAULT (which causes the alert to pend).
2316 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
2318 * @return N/A
2319 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002320extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2321 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322
2323/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002324 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002328 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2329 *
2330 * @param alert Address of the alert.
2331 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 * or one of the special values K_NO_WAIT and K_FOREVER.
2333 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002334 * @retval 0 Alert received.
2335 * @retval -EBUSY Returned without waiting.
2336 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 */
Kumar Galacc334c72017-04-21 10:55:34 -05002338extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339
2340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * This routine signals @a alert. The action specified for @a alert will
2344 * be taken, which may trigger the execution of an alert handler function
2345 * and/or cause the alert to pend (assuming the alert has not reached its
2346 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * @note Can be called by ISRs.
2349 *
2350 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
2352 * @return N/A
2353 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002354extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355
2356/**
Allan Stephensc98da842016-11-11 15:45:03 -05002357 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358 */
2359
Allan Stephensc98da842016-11-11 15:45:03 -05002360/**
2361 * @cond INTERNAL_HIDDEN
2362 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363
2364struct k_msgq {
2365 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002366 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002367 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 char *buffer_start;
2369 char *buffer_end;
2370 char *read_ptr;
2371 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002372 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373
Anas Nashif2f203c22016-12-18 06:57:45 -05002374 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375};
2376
Peter Mitsis1da807e2016-10-06 11:36:59 -04002377#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 { \
2379 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002380 .max_msgs = q_max_msgs, \
2381 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002383 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 .read_ptr = q_buffer, \
2385 .write_ptr = q_buffer, \
2386 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002387 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388 }
2389
Peter Mitsis1da807e2016-10-06 11:36:59 -04002390/**
Allan Stephensc98da842016-11-11 15:45:03 -05002391 * INTERNAL_HIDDEN @endcond
2392 */
2393
2394/**
2395 * @defgroup msgq_apis Message Queue APIs
2396 * @ingroup kernel_apis
2397 * @{
2398 */
2399
2400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2404 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002405 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2406 * message is similarly aligned to this boundary, @a q_msg_size must also be
2407 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * The message queue can be accessed outside the module where it is defined
2410 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002412 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * @param q_name Name of the message queue.
2415 * @param q_msg_size Message size (in bytes).
2416 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002417 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002418 */
2419#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2420 static char __noinit __aligned(q_align) \
2421 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002422 struct k_msgq q_name \
2423 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002424 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2425 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426
Peter Mitsisd7a37502016-10-13 11:37:40 -04002427/**
2428 * @brief Initialize a message queue.
2429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * This routine initializes a message queue object, prior to its first use.
2431 *
Allan Stephensda827222016-11-09 14:23:58 -06002432 * The message queue's ring buffer must contain space for @a max_msgs messages,
2433 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2434 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2435 * that each message is similarly aligned to this boundary, @a q_msg_size
2436 * must also be a multiple of N.
2437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438 * @param q Address of the message queue.
2439 * @param buffer Pointer to ring buffer that holds queued messages.
2440 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002441 * @param max_msgs Maximum number of messages that can be queued.
2442 *
2443 * @return N/A
2444 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002445extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002446 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002447
2448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002452 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002453 * @note Can be called by ISRs.
2454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002455 * @param q Address of the message queue.
2456 * @param data Pointer to the message.
2457 * @param timeout Waiting period to add the message (in milliseconds),
2458 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002459 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002460 * @retval 0 Message sent.
2461 * @retval -ENOMSG Returned without waiting or queue purged.
2462 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002463 */
Kumar Galacc334c72017-04-21 10:55:34 -05002464extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002465
2466/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * This routine receives a message from message queue @a q in a "first in,
2470 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002471 *
Allan Stephensc98da842016-11-11 15:45:03 -05002472 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * @param q Address of the message queue.
2475 * @param data Address of area to hold the received message.
2476 * @param timeout Waiting period to receive the message (in milliseconds),
2477 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002478 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002479 * @retval 0 Message received.
2480 * @retval -ENOMSG Returned without waiting.
2481 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002482 */
Kumar Galacc334c72017-04-21 10:55:34 -05002483extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002484
2485/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002488 * This routine discards all unreceived messages in a message queue's ring
2489 * buffer. Any threads that are blocked waiting to send a message to the
2490 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002493 *
2494 * @return N/A
2495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496extern void k_msgq_purge(struct k_msgq *q);
2497
Peter Mitsis67be2492016-10-07 11:44:34 -04002498/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * This routine returns the number of unused entries in a message queue's
2502 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @param q Address of the message queue.
2505 *
2506 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002507 */
Kumar Galacc334c72017-04-21 10:55:34 -05002508static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002509{
2510 return q->max_msgs - q->used_msgs;
2511}
2512
Peter Mitsisd7a37502016-10-13 11:37:40 -04002513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * @param q Address of the message queue.
2519 *
2520 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002521 */
Kumar Galacc334c72017-04-21 10:55:34 -05002522static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523{
2524 return q->used_msgs;
2525}
2526
Allan Stephensc98da842016-11-11 15:45:03 -05002527/**
2528 * @} end defgroup msgq_apis
2529 */
2530
2531/**
2532 * @defgroup mem_pool_apis Memory Pool APIs
2533 * @ingroup kernel_apis
2534 * @{
2535 */
2536
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002538 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539 void *addr_in_pool;
2540 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002541 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542};
2543
Allan Stephensc98da842016-11-11 15:45:03 -05002544/**
2545 * @} end defgroup mem_pool_apis
2546 */
2547
2548/**
2549 * @defgroup mailbox_apis Mailbox APIs
2550 * @ingroup kernel_apis
2551 * @{
2552 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553
2554struct k_mbox_msg {
2555 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002556 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002558 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002560 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 /** sender's message data buffer */
2562 void *tx_data;
2563 /** internal use only - needed for legacy API support */
2564 void *_rx_data;
2565 /** message data block descriptor */
2566 struct k_mem_block tx_block;
2567 /** source thread id */
2568 k_tid_t rx_source_thread;
2569 /** target thread id */
2570 k_tid_t tx_target_thread;
2571 /** internal use only - thread waiting on send (may be a dummy) */
2572 k_tid_t _syncing_thread;
2573#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2574 /** internal use only - semaphore used during asynchronous send */
2575 struct k_sem *_async_sem;
2576#endif
2577};
2578
Allan Stephensc98da842016-11-11 15:45:03 -05002579/**
2580 * @cond INTERNAL_HIDDEN
2581 */
2582
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583struct k_mbox {
2584 _wait_q_t tx_msg_queue;
2585 _wait_q_t rx_msg_queue;
2586
Anas Nashif2f203c22016-12-18 06:57:45 -05002587 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002588};
2589
2590#define K_MBOX_INITIALIZER(obj) \
2591 { \
2592 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2593 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002594 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595 }
2596
Peter Mitsis12092702016-10-14 12:57:23 -04002597/**
Allan Stephensc98da842016-11-11 15:45:03 -05002598 * INTERNAL_HIDDEN @endcond
2599 */
2600
2601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002605 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002606 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002609 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002611 struct k_mbox name \
2612 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 K_MBOX_INITIALIZER(name) \
2614
Peter Mitsis12092702016-10-14 12:57:23 -04002615/**
2616 * @brief Initialize a mailbox.
2617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * This routine initializes a mailbox object, prior to its first use.
2619 *
2620 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002621 *
2622 * @return N/A
2623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624extern void k_mbox_init(struct k_mbox *mbox);
2625
Peter Mitsis12092702016-10-14 12:57:23 -04002626/**
2627 * @brief Send a mailbox message in a synchronous manner.
2628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * This routine sends a message to @a mbox and waits for a receiver to both
2630 * receive and process it. The message data may be in a buffer, in a memory
2631 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @param mbox Address of the mailbox.
2634 * @param tx_msg Address of the transmit message descriptor.
2635 * @param timeout Waiting period for the message to be received (in
2636 * milliseconds), or one of the special values K_NO_WAIT
2637 * and K_FOREVER. Once the message has been received,
2638 * this routine waits as long as necessary for the message
2639 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002640 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002641 * @retval 0 Message sent.
2642 * @retval -ENOMSG Returned without waiting.
2643 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002644 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002645extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002646 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002647
Peter Mitsis12092702016-10-14 12:57:23 -04002648/**
2649 * @brief Send a mailbox message in an asynchronous manner.
2650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * This routine sends a message to @a mbox without waiting for a receiver
2652 * to process it. The message data may be in a buffer, in a memory pool block,
2653 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2654 * will be given when the message has been both received and completely
2655 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * @param mbox Address of the mailbox.
2658 * @param tx_msg Address of the transmit message descriptor.
2659 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002660 *
2661 * @return N/A
2662 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002663extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 struct k_sem *sem);
2665
Peter Mitsis12092702016-10-14 12:57:23 -04002666/**
2667 * @brief Receive a mailbox message.
2668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * This routine receives a message from @a mbox, then optionally retrieves
2670 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * @param mbox Address of the mailbox.
2673 * @param rx_msg Address of the receive message descriptor.
2674 * @param buffer Address of the buffer to receive data, or NULL to defer data
2675 * retrieval and message disposal until later.
2676 * @param timeout Waiting period for a message to be received (in
2677 * milliseconds), or one of the special values K_NO_WAIT
2678 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002679 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002680 * @retval 0 Message received.
2681 * @retval -ENOMSG Returned without waiting.
2682 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002683 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002684extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002685 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002686
2687/**
2688 * @brief Retrieve mailbox message data into a buffer.
2689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * This routine completes the processing of a received message by retrieving
2691 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002692 *
2693 * Alternatively, this routine can be used to dispose of a received message
2694 * without retrieving its data.
2695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696 * @param rx_msg Address of the receive message descriptor.
2697 * @param buffer Address of the buffer to receive data, or NULL to discard
2698 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002699 *
2700 * @return N/A
2701 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002702extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002703
2704/**
2705 * @brief Retrieve mailbox message data into a memory pool block.
2706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * This routine completes the processing of a received message by retrieving
2708 * its data into a memory pool block, then disposing of the message.
2709 * The memory pool block that results from successful retrieval must be
2710 * returned to the pool once the data has been processed, even in cases
2711 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002712 *
2713 * Alternatively, this routine can be used to dispose of a received message
2714 * without retrieving its data. In this case there is no need to return a
2715 * memory pool block to the pool.
2716 *
2717 * This routine allocates a new memory pool block for the data only if the
2718 * data is not already in one. If a new block cannot be allocated, the routine
2719 * returns a failure code and the received message is left unchanged. This
2720 * permits the caller to reattempt data retrieval at a later time or to dispose
2721 * of the received message without retrieving its data.
2722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002723 * @param rx_msg Address of a receive message descriptor.
2724 * @param pool Address of memory pool, or NULL to discard data.
2725 * @param block Address of the area to hold memory pool block info.
2726 * @param timeout Waiting period to wait for a memory pool block (in
2727 * milliseconds), or one of the special values K_NO_WAIT
2728 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002729 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002730 * @retval 0 Data retrieved.
2731 * @retval -ENOMEM Returned without waiting.
2732 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002733 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002734extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002735 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002736 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737
Allan Stephensc98da842016-11-11 15:45:03 -05002738/**
2739 * @} end defgroup mailbox_apis
2740 */
2741
2742/**
2743 * @cond INTERNAL_HIDDEN
2744 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745
2746struct k_pipe {
2747 unsigned char *buffer; /* Pipe buffer: may be NULL */
2748 size_t size; /* Buffer size */
2749 size_t bytes_used; /* # bytes used in buffer */
2750 size_t read_index; /* Where in buffer to read from */
2751 size_t write_index; /* Where in buffer to write */
2752
2753 struct {
2754 _wait_q_t readers; /* Reader wait queue */
2755 _wait_q_t writers; /* Writer wait queue */
2756 } wait_q;
2757
Anas Nashif2f203c22016-12-18 06:57:45 -05002758 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759};
2760
Peter Mitsise5d9c582016-10-14 14:44:57 -04002761#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 { \
2763 .buffer = pipe_buffer, \
2764 .size = pipe_buffer_size, \
2765 .bytes_used = 0, \
2766 .read_index = 0, \
2767 .write_index = 0, \
2768 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2769 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002770 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 }
2772
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002773/**
Allan Stephensc98da842016-11-11 15:45:03 -05002774 * INTERNAL_HIDDEN @endcond
2775 */
2776
2777/**
2778 * @defgroup pipe_apis Pipe APIs
2779 * @ingroup kernel_apis
2780 * @{
2781 */
2782
2783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002788 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * @param name Name of the pipe.
2791 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2792 * or zero if no ring buffer is used.
2793 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002795#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2796 static unsigned char __noinit __aligned(pipe_align) \
2797 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002798 struct k_pipe name \
2799 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002800 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002805 * This routine initializes a pipe object, prior to its first use.
2806 *
2807 * @param pipe Address of the pipe.
2808 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2809 * is used.
2810 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2811 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812 *
2813 * @return N/A
2814 */
2815extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2816 size_t size);
2817
2818/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @param pipe Address of the pipe.
2824 * @param data Address of data to write.
2825 * @param bytes_to_write Size of data (in bytes).
2826 * @param bytes_written Address of area to hold the number of bytes written.
2827 * @param min_xfer Minimum number of bytes to write.
2828 * @param timeout Waiting period to wait for the data to be written (in
2829 * milliseconds), or one of the special values K_NO_WAIT
2830 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002832 * @retval 0 At least @a min_xfer bytes of data were written.
2833 * @retval -EIO Returned without waiting; zero data bytes were written.
2834 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002835 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002837extern int k_pipe_put(struct k_pipe *pipe, void *data,
2838 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002839 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840
2841/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * @param pipe Address of the pipe.
2847 * @param data Address to place the data read from pipe.
2848 * @param bytes_to_read Maximum number of data bytes to read.
2849 * @param bytes_read Address of area to hold the number of bytes read.
2850 * @param min_xfer Minimum number of data bytes to read.
2851 * @param timeout Waiting period to wait for the data to be read (in
2852 * milliseconds), or one of the special values K_NO_WAIT
2853 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002855 * @retval 0 At least @a min_xfer bytes of data were read.
2856 * @retval -EIO Returned without waiting; zero data bytes were read.
2857 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002860extern int k_pipe_get(struct k_pipe *pipe, void *data,
2861 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002862 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863
2864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine writes the data contained in a memory block to @a pipe.
2868 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872 * @param block Memory block containing data to send
2873 * @param size Number of data bytes in memory block to send
2874 * @param sem Semaphore to signal upon completion (else NULL)
2875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877 */
2878extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2879 size_t size, struct k_sem *sem);
2880
2881/**
Allan Stephensc98da842016-11-11 15:45:03 -05002882 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883 */
2884
Allan Stephensc98da842016-11-11 15:45:03 -05002885/**
2886 * @cond INTERNAL_HIDDEN
2887 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002889struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002890 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002891 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002892 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002893 char *buffer;
2894 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05002895 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896
Anas Nashif2f203c22016-12-18 06:57:45 -05002897 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898};
2899
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002900#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2901 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902 { \
2903 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002904 .num_blocks = slab_num_blocks, \
2905 .block_size = slab_block_size, \
2906 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002907 .free_list = NULL, \
2908 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002909 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910 }
2911
Peter Mitsis578f9112016-10-07 13:50:31 -04002912/**
Allan Stephensc98da842016-11-11 15:45:03 -05002913 * INTERNAL_HIDDEN @endcond
2914 */
2915
2916/**
2917 * @defgroup mem_slab_apis Memory Slab APIs
2918 * @ingroup kernel_apis
2919 * @{
2920 */
2921
2922/**
Allan Stephensda827222016-11-09 14:23:58 -06002923 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002924 *
Allan Stephensda827222016-11-09 14:23:58 -06002925 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002927 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2928 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002929 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002930 *
Allan Stephensda827222016-11-09 14:23:58 -06002931 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002933 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002934 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @param name Name of the memory slab.
2937 * @param slab_block_size Size of each memory block (in bytes).
2938 * @param slab_num_blocks Number memory blocks.
2939 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002940 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002941#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2942 char __noinit __aligned(slab_align) \
2943 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2944 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002945 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002946 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2947 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002948
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002949/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002950 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002953 *
Allan Stephensda827222016-11-09 14:23:58 -06002954 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2955 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2956 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2957 * To ensure that each memory block is similarly aligned to this boundary,
2958 * @a slab_block_size must also be a multiple of N.
2959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @param slab Address of the memory slab.
2961 * @param buffer Pointer to buffer used for the memory blocks.
2962 * @param block_size Size of each memory block (in bytes).
2963 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002964 *
2965 * @return N/A
2966 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002967extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002968 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002969
2970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @param slab Address of the memory slab.
2976 * @param mem Pointer to block address area.
2977 * @param timeout Maximum time to wait for operation to complete
2978 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2979 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002980 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002981 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002982 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002983 * @retval -ENOMEM Returned without waiting.
2984 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002985 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002986extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05002987 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002988
2989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * This routine releases a previously allocated memory block back to its
2993 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * @param slab Address of the memory slab.
2996 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002997 *
2998 * @return N/A
2999 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003000extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003003 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * This routine gets the number of memory blocks that are currently
3006 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003011 */
Kumar Galacc334c72017-04-21 10:55:34 -05003012static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003014 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015}
3016
Peter Mitsisc001aa82016-10-13 13:53:37 -04003017/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003020 * This routine gets the number of memory blocks that are currently
3021 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003026 */
Kumar Galacc334c72017-04-21 10:55:34 -05003027static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003028{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003029 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003030}
3031
Allan Stephensc98da842016-11-11 15:45:03 -05003032/**
3033 * @} end defgroup mem_slab_apis
3034 */
3035
3036/**
3037 * @cond INTERNAL_HIDDEN
3038 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003040/*
3041 * Memory pool requires a buffer and two arrays of structures for the
3042 * memory block accounting:
3043 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
3044 * status of four blocks of memory.
3045 */
3046struct k_mem_pool_quad_block {
3047 char *mem_blocks; /* pointer to the first of four memory blocks */
Kumar Galacc334c72017-04-21 10:55:34 -05003048 u32_t mem_status; /* four bits. If bit is set, memory block is
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003049 allocated */
3050};
3051/*
3052 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
3053 * blocks of one size. Block sizes go from maximal to minimal. Next memory
3054 * block size is 4 times less than the previous one and thus requires 4 times
3055 * bigger array of k_mem_pool_quad_block structures to keep track of the
3056 * memory blocks.
3057 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003059/*
3060 * The array of k_mem_pool_block_set keeps the information of each array of
3061 * k_mem_pool_quad_block structures
3062 */
3063struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04003064 size_t block_size; /* memory block size */
Kumar Galacc334c72017-04-21 10:55:34 -05003065 u32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003066 struct k_mem_pool_quad_block *quad_block;
3067 int count;
3068};
3069
3070/* Memory pool descriptor */
3071struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04003072 size_t max_block_size;
3073 size_t min_block_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003074 u32_t nr_of_maxblocks;
3075 u32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003076 struct k_mem_pool_block_set *block_set;
3077 char *bufblock;
3078 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05003079 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080};
3081
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003082#ifdef CONFIG_ARM
3083#define _SECTION_TYPE_SIGN "%"
3084#else
3085#define _SECTION_TYPE_SIGN "@"
3086#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003088/*
3089 * Static memory pool initialization
3090 */
Allan Stephensc98da842016-11-11 15:45:03 -05003091
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003092/*
3093 * Use .altmacro to be able to recalculate values and pass them as string
David B. Kinder8b986d72017-04-18 15:56:26 -07003094 * arguments when calling assembler macros recursively
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003095 */
3096__asm__(".altmacro\n\t");
3097
3098/*
3099 * Recursively calls a macro
David B. Kinder8b986d72017-04-18 15:56:26 -07003100 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003101 * __memory_pool_max_block_size - maximal size of the memory block
3102 * __memory_pool_min_block_size - minimal size of the memory block
3103 * Notes:
3104 * Global symbols are used due the fact that assembler macro allows only
3105 * one argument be passed with the % conversion
3106 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3107 * is used instead of / 4.
3108 * n_max argument needs to go first in the invoked macro, as some
3109 * assemblers concatenate \name and %(\n_max * 4) arguments
3110 * if \name goes first
3111 */
3112__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3113 ".ifge __memory_pool_max_block_size >> 2 -"
3114 " __memory_pool_min_block_size\n\t\t"
3115 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3116 "\\macro_name %(\\n_max * 4) \\name\n\t"
3117 ".endif\n\t"
3118 ".endm\n");
3119
3120/*
3121 * Build quad blocks
3122 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3123 * structures and recursively calls itself for the next array, 4 times
3124 * larger.
David B. Kinder8b986d72017-04-18 15:56:26 -07003125 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003126 * __memory_pool_max_block_size - maximal size of the memory block
3127 * __memory_pool_min_block_size - minimal size of the memory block
3128 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3129 */
3130__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003131 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003132 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3133 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3134 ".if \\n_max % 4\n\t\t"
3135 ".skip __memory_pool_quad_block_size\n\t"
3136 ".endif\n\t"
3137 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3138 ".endm\n");
3139
3140/*
3141 * Build block sets and initialize them
3142 * Macro initializes the k_mem_pool_block_set structure and
3143 * recursively calls itself for the next one.
David B. Kinder8b986d72017-04-18 15:56:26 -07003144 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003145 * __memory_pool_max_block_size - maximal size of the memory block
3146 * __memory_pool_min_block_size - minimal size of the memory block
3147 * __memory_pool_block_set_count, the number of the elements in the
3148 * block set array must be set to 0. Macro calculates it's real
3149 * value.
3150 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3151 * structures, _build_quad_blocks must be called prior it.
3152 */
3153__asm__(".macro _build_block_set n_max, name\n\t"
3154 ".int __memory_pool_max_block_size\n\t" /* block_size */
3155 ".if \\n_max % 4\n\t\t"
3156 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3157 ".else\n\t\t"
3158 ".int \\n_max >> 2\n\t"
3159 ".endif\n\t"
3160 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3161 ".int 0\n\t" /* count */
3162 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3163 "__do_recurse _build_block_set \\name \\n_max\n\t"
3164 ".endm\n");
3165
3166/*
3167 * Build a memory pool structure and initialize it
3168 * Macro uses __memory_pool_block_set_count global symbol,
3169 * block set addresses and buffer address, it may be called only after
3170 * _build_block_set
3171 */
3172__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003173 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003174 _SECTION_TYPE_SIGN "progbits\n\t"
3175 ".globl \\name\n\t"
3176 "\\name:\n\t"
3177 ".int \\max_size\n\t" /* max_block_size */
3178 ".int \\min_size\n\t" /* min_block_size */
3179 ".int \\n_max\n\t" /* nr_of_maxblocks */
3180 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3181 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3182 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3183 ".int 0\n\t" /* wait_q->head */
3184 ".int 0\n\t" /* wait_q->next */
3185 ".popsection\n\t"
3186 ".endm\n");
3187
3188#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3189 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3190 _SECTION_TYPE_SIGN "progbits\n\t"); \
3191 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3192 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3193 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3194 STRINGIFY(name) "\n\t"); \
3195 __asm__(".popsection\n\t")
3196
3197#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3198 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3199 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3200 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3201 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003202 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003203 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3204 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3205 STRINGIFY(name) "\n\t"); \
3206 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3207 __asm__(".int __memory_pool_block_set_count\n\t"); \
3208 __asm__(".popsection\n\t"); \
Kumar Galacc334c72017-04-21 10:55:34 -05003209 extern u32_t _mem_pool_block_set_count_##name; \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003210 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3211
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003212#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3213 char __noinit __aligned(align) \
3214 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003215
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003216/*
3217 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3218 * to __memory_pool_quad_block_size absolute symbol.
3219 * This function does not get called, but compiler calculates the value and
3220 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3221 */
3222static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3223{
3224 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003225#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003226 "__memory_pool_quad_block_size = %0\n\t"
3227#else
3228 "__memory_pool_quad_block_size = %c0\n\t"
3229#endif
3230 :
3231 : "n"(sizeof(struct k_mem_pool_quad_block)));
3232}
3233
3234/**
Allan Stephensc98da842016-11-11 15:45:03 -05003235 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003236 */
3237
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003238/**
Allan Stephensc98da842016-11-11 15:45:03 -05003239 * @addtogroup mem_pool_apis
3240 * @{
3241 */
3242
3243/**
3244 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3247 * long. The memory pool allows blocks to be repeatedly partitioned into
3248 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3249 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003250 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003252 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003253 * If the pool is to be accessed outside the module where it is defined, it
3254 * can be declared via
3255 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003256 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @param name Name of the memory pool.
3259 * @param min_size Size of the smallest blocks in the pool (in bytes).
3260 * @param max_size Size of the largest blocks in the pool (in bytes).
3261 * @param n_max Number of maximum sized blocks in the pool.
3262 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003263 */
3264#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003265 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3266 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003267 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003268 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3269 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3270 extern struct k_mem_pool name
3271
Peter Mitsis937042c2016-10-13 13:18:26 -04003272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * @param pool Address of the memory pool.
3278 * @param block Pointer to block descriptor for the allocated memory.
3279 * @param size Amount of memory to allocate (in bytes).
3280 * @param timeout Maximum time to wait for operation to complete
3281 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3282 * or K_FOREVER to wait as long as necessary.
3283 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003284 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003286 * @retval -ENOMEM Returned without waiting.
3287 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003288 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003289extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003290 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003291
3292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * This routine releases a previously allocated memory block back to its
3296 * memory pool.
3297 *
3298 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003299 *
3300 * @return N/A
3301 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003303
3304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * This routine instructs a memory pool to concatenate unused memory blocks
3308 * into larger blocks wherever possible. Manually defragmenting the memory
3309 * pool may speed up future allocations of memory blocks by eliminating the
3310 * need for the memory pool to perform an automatic partial defragmentation.
3311 *
3312 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003313 *
3314 * @return N/A
3315 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003316extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003317
3318/**
Allan Stephensc98da842016-11-11 15:45:03 -05003319 * @} end addtogroup mem_pool_apis
3320 */
3321
3322/**
3323 * @defgroup heap_apis Heap Memory Pool APIs
3324 * @ingroup kernel_apis
3325 * @{
3326 */
3327
3328/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003332 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003337 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003338extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003339
3340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003342 *
3343 * This routine provides traditional free() semantics. The memory being
3344 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003345 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003346 * If @a ptr is NULL, no operation is performed.
3347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003349 *
3350 * @return N/A
3351 */
3352extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353
Allan Stephensc98da842016-11-11 15:45:03 -05003354/**
3355 * @} end defgroup heap_apis
3356 */
3357
Benjamin Walshacc68c12017-01-29 18:57:45 -05003358/* polling API - PRIVATE */
3359
Benjamin Walshb0179862017-02-02 16:39:57 -05003360#ifdef CONFIG_POLL
3361#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3362#else
3363#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3364#endif
3365
Benjamin Walshacc68c12017-01-29 18:57:45 -05003366/* private - implementation data created as needed, per-type */
3367struct _poller {
3368 struct k_thread *thread;
3369};
3370
3371/* private - types bit positions */
3372enum _poll_types_bits {
3373 /* can be used to ignore an event */
3374 _POLL_TYPE_IGNORE,
3375
3376 /* to be signaled by k_poll_signal() */
3377 _POLL_TYPE_SIGNAL,
3378
3379 /* semaphore availability */
3380 _POLL_TYPE_SEM_AVAILABLE,
3381
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003382 /* queue/fifo/lifo data availability */
3383 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003384
3385 _POLL_NUM_TYPES
3386};
3387
3388#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3389
3390/* private - states bit positions */
3391enum _poll_states_bits {
3392 /* default state when creating event */
3393 _POLL_STATE_NOT_READY,
3394
3395 /* there was another poller already on the object */
3396 _POLL_STATE_EADDRINUSE,
3397
3398 /* signaled by k_poll_signal() */
3399 _POLL_STATE_SIGNALED,
3400
3401 /* semaphore is available */
3402 _POLL_STATE_SEM_AVAILABLE,
3403
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003404 /* data is available to read on queue/fifo/lifo */
3405 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003406
3407 _POLL_NUM_STATES
3408};
3409
3410#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3411
3412#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003413 (32 - (0 \
3414 + 8 /* tag */ \
3415 + _POLL_NUM_TYPES \
3416 + _POLL_NUM_STATES \
3417 + 1 /* modes */ \
3418 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003419
3420#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3421#error overflow of 32-bit word in struct k_poll_event
3422#endif
3423
3424/* end of polling API - PRIVATE */
3425
3426
3427/**
3428 * @defgroup poll_apis Async polling APIs
3429 * @ingroup kernel_apis
3430 * @{
3431 */
3432
3433/* Public polling API */
3434
3435/* public - values for k_poll_event.type bitfield */
3436#define K_POLL_TYPE_IGNORE 0
3437#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3438#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003439#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3440#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003441
3442/* public - polling modes */
3443enum k_poll_modes {
3444 /* polling thread does not take ownership of objects when available */
3445 K_POLL_MODE_NOTIFY_ONLY = 0,
3446
3447 K_POLL_NUM_MODES
3448};
3449
3450/* public - values for k_poll_event.state bitfield */
3451#define K_POLL_STATE_NOT_READY 0
3452#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3453#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3454#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003455#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3456#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003457
3458/* public - poll signal object */
3459struct k_poll_signal {
3460 /* PRIVATE - DO NOT TOUCH */
3461 struct k_poll_event *poll_event;
3462
3463 /*
3464 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3465 * user resets it to 0.
3466 */
3467 unsigned int signaled;
3468
3469 /* custom result value passed to k_poll_signal() if needed */
3470 int result;
3471};
3472
3473#define K_POLL_SIGNAL_INITIALIZER() \
3474 { \
3475 .poll_event = NULL, \
3476 .signaled = 0, \
3477 .result = 0, \
3478 }
3479
3480struct k_poll_event {
3481 /* PRIVATE - DO NOT TOUCH */
3482 struct _poller *poller;
3483
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003484 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003485 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003486
Benjamin Walshacc68c12017-01-29 18:57:45 -05003487 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003488 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003489
3490 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003491 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003492
3493 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003494 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003495
3496 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003497 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003498
3499 /* per-type data */
3500 union {
3501 void *obj;
3502 struct k_poll_signal *signal;
3503 struct k_sem *sem;
3504 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003505 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003506 };
3507};
3508
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003509#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003510 { \
3511 .poller = NULL, \
3512 .type = event_type, \
3513 .state = K_POLL_STATE_NOT_READY, \
3514 .mode = event_mode, \
3515 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003516 { .obj = event_obj }, \
3517 }
3518
3519#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3520 event_tag) \
3521 { \
3522 .type = event_type, \
3523 .tag = event_tag, \
3524 .state = K_POLL_STATE_NOT_READY, \
3525 .mode = event_mode, \
3526 .unused = 0, \
3527 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003528 }
3529
3530/**
3531 * @brief Initialize one struct k_poll_event instance
3532 *
3533 * After this routine is called on a poll event, the event it ready to be
3534 * placed in an event array to be passed to k_poll().
3535 *
3536 * @param event The event to initialize.
3537 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3538 * values. Only values that apply to the same object being polled
3539 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3540 * event.
3541 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3542 * @param obj Kernel object or poll signal.
3543 *
3544 * @return N/A
3545 */
3546
Kumar Galacc334c72017-04-21 10:55:34 -05003547extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003548 int mode, void *obj);
3549
3550/**
3551 * @brief Wait for one or many of multiple poll events to occur
3552 *
3553 * This routine allows a thread to wait concurrently for one or many of
3554 * multiple poll events to have occurred. Such events can be a kernel object
3555 * being available, like a semaphore, or a poll signal event.
3556 *
3557 * When an event notifies that a kernel object is available, the kernel object
3558 * is not "given" to the thread calling k_poll(): it merely signals the fact
3559 * that the object was available when the k_poll() call was in effect. Also,
3560 * all threads trying to acquire an object the regular way, i.e. by pending on
3561 * the object, have precedence over the thread polling on the object. This
3562 * means that the polling thread will never get the poll event on an object
3563 * until the object becomes available and its pend queue is empty. For this
3564 * reason, the k_poll() call is more effective when the objects being polled
3565 * only have one thread, the polling thread, trying to acquire them.
3566 *
3567 * Only one thread can be polling for a particular object at a given time. If
3568 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3569 * and returns as soon as it has finished handling the other events. This means
3570 * that k_poll() can return -EADDRINUSE and have the state value of some events
3571 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3572 * parameter is ignored.
3573 *
3574 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3575 * events that were passed to k_poll() and check the state field for the values
3576 * that were expected and take the associated actions.
3577 *
3578 * Before being reused for another call to k_poll(), the user has to reset the
3579 * state field to K_POLL_STATE_NOT_READY.
3580 *
3581 * @param events An array of pointers to events to be polled for.
3582 * @param num_events The number of events in the array.
3583 * @param timeout Waiting period for an event to be ready (in milliseconds),
3584 * or one of the special values K_NO_WAIT and K_FOREVER.
3585 *
3586 * @retval 0 One or more events are ready.
3587 * @retval -EADDRINUSE One or more objects already had a poller.
3588 * @retval -EAGAIN Waiting period timed out.
3589 */
3590
3591extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003592 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003593
3594/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003595 * @brief Initialize a poll signal object.
3596 *
3597 * Ready a poll signal object to be signaled via k_poll_signal().
3598 *
3599 * @param signal A poll signal.
3600 *
3601 * @return N/A
3602 */
3603
3604extern void k_poll_signal_init(struct k_poll_signal *signal);
3605
3606/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003607 * @brief Signal a poll signal object.
3608 *
3609 * This routine makes ready a poll signal, which is basically a poll event of
3610 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3611 * made ready to run. A @a result value can be specified.
3612 *
3613 * The poll signal contains a 'signaled' field that, when set by
3614 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3615 * be reset by the user before being passed again to k_poll() or k_poll() will
3616 * consider it being signaled, and will return immediately.
3617 *
3618 * @param signal A poll signal.
3619 * @param result The value to store in the result field of the signal.
3620 *
3621 * @retval 0 The signal was delivered successfully.
3622 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3623 */
3624
3625extern int k_poll_signal(struct k_poll_signal *signal, int result);
3626
3627/* private internal function */
3628extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003629 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003630
3631/**
3632 * @} end defgroup poll_apis
3633 */
3634
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003635/**
3636 * @brief Make the CPU idle.
3637 *
3638 * This function makes the CPU idle until an event wakes it up.
3639 *
3640 * In a regular system, the idle thread should be the only thread responsible
3641 * for making the CPU idle and triggering any type of power management.
3642 * However, in some more constrained systems, such as a single-threaded system,
3643 * the only thread would be responsible for this if needed.
3644 *
3645 * @return N/A
3646 */
3647extern void k_cpu_idle(void);
3648
3649/**
3650 * @brief Make the CPU idle in an atomic fashion.
3651 *
3652 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3653 * must be done atomically before making the CPU idle.
3654 *
3655 * @param key Interrupt locking key obtained from irq_lock().
3656 *
3657 * @return N/A
3658 */
3659extern void k_cpu_atomic_idle(unsigned int key);
3660
Kumar Galacc334c72017-04-21 10:55:34 -05003661extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003662
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003663#include <arch/cpu.h>
3664
Andrew Boiecdb94d62017-04-18 15:22:05 -07003665#ifdef _ARCH_EXCEPT
3666/* This archtecture has direct support for triggering a CPU exception */
3667#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3668#else
3669
3670#include <misc/printk.h>
3671
3672/* NOTE: This is the implementation for arches that do not implement
3673 * _ARCH_EXCEPT() to generate a real CPU exception.
3674 *
3675 * We won't have a real exception frame to determine the PC value when
3676 * the oops occurred, so print file and line number before we jump into
3677 * the fatal error handler.
3678 */
3679#define _k_except_reason(reason) do { \
3680 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3681 _NanoFatalErrorHandler(reason, &_default_esf); \
3682 CODE_UNREACHABLE; \
3683 } while (0)
3684
3685#endif /* _ARCH__EXCEPT */
3686
3687/**
3688 * @brief Fatally terminate a thread
3689 *
3690 * This should be called when a thread has encountered an unrecoverable
3691 * runtime condition and needs to terminate. What this ultimately
3692 * means is determined by the _fatal_error_handler() implementation, which
3693 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3694 *
3695 * If this is called from ISR context, the default system fatal error handler
3696 * will treat it as an unrecoverable system error, just like k_panic().
3697 */
3698#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3699
3700/**
3701 * @brief Fatally terminate the system
3702 *
3703 * This should be called when the Zephyr kernel has encountered an
3704 * unrecoverable runtime condition and needs to terminate. What this ultimately
3705 * means is determined by the _fatal_error_handler() implementation, which
3706 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3707 */
3708#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3709
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710/*
3711 * private APIs that are utilized by one or more public APIs
3712 */
3713
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003714#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003716#else
3717#define _init_static_threads() do { } while ((0))
3718#endif
3719
3720extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003721extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003722
3723#ifdef __cplusplus
3724}
3725#endif
3726
Andrew Boiee004dec2016-11-07 09:01:19 -08003727#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3728/*
3729 * Define new and delete operators.
3730 * At this moment, the operators do nothing since objects are supposed
3731 * to be statically allocated.
3732 */
3733inline void operator delete(void *ptr)
3734{
3735 (void)ptr;
3736}
3737
3738inline void operator delete[](void *ptr)
3739{
3740 (void)ptr;
3741}
3742
3743inline void *operator new(size_t size)
3744{
3745 (void)size;
3746 return NULL;
3747}
3748
3749inline void *operator new[](size_t size)
3750{
3751 (void)size;
3752 return NULL;
3753}
3754
3755/* Placement versions of operator new and delete */
3756inline void operator delete(void *ptr1, void *ptr2)
3757{
3758 (void)ptr1;
3759 (void)ptr2;
3760}
3761
3762inline void operator delete[](void *ptr1, void *ptr2)
3763{
3764 (void)ptr1;
3765 (void)ptr2;
3766}
3767
3768inline void *operator new(size_t size, void *ptr)
3769{
3770 (void)size;
3771 return ptr;
3772}
3773
3774inline void *operator new[](size_t size, void *ptr)
3775{
3776 (void)size;
3777 return ptr;
3778}
3779
3780#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3781
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003782#endif /* !_ASMLANGUAGE */
3783
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003784#endif /* _kernel__h_ */