blob: 826052ea23b751b8888b717da43b3c2612ae963b [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 */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800867#ifdef CONFIG_TICKLESS_KERNEL
868#define _TICK_ALIGN 0
869#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500870#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800871#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500872
Kumar Galacc334c72017-04-21 10:55:34 -0500873static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400874{
Benjamin Walsh62092182016-12-20 14:39:08 -0500875#ifdef CONFIG_SYS_CLOCK_EXISTS
876
877#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500878 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400879#else
Kumar Galacc334c72017-04-21 10:55:34 -0500880 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500881#endif
882
883#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400884 __ASSERT(ticks == 0, "");
885 return 0;
886#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400887}
888
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889struct k_timer {
890 /*
891 * _timeout structure must be first here if we want to use
892 * dynamic timer allocation. timeout.node is used in the double-linked
893 * list of free timers
894 */
895 struct _timeout timeout;
896
Allan Stephens45bfa372016-10-12 12:39:42 -0500897 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400898 _wait_q_t wait_q;
899
900 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500901 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400902
903 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500904 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400905
906 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500907 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908
Allan Stephens45bfa372016-10-12 12:39:42 -0500909 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500910 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400911
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500912 /* user-specific data, also used to support legacy features */
913 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400914
Anas Nashif2f203c22016-12-18 06:57:45 -0500915 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400916};
917
Allan Stephens1342adb2016-11-03 13:54:53 -0500918#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400919 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500920 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500921 .timeout.wait_q = NULL, \
922 .timeout.thread = NULL, \
923 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400924 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500925 .expiry_fn = expiry, \
926 .stop_fn = stop, \
927 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500928 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500929 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930 }
931
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932/**
Allan Stephensc98da842016-11-11 15:45:03 -0500933 * INTERNAL_HIDDEN @endcond
934 */
935
936/**
937 * @defgroup timer_apis Timer APIs
938 * @ingroup kernel_apis
939 * @{
940 */
941
942/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500943 * @typedef k_timer_expiry_t
944 * @brief Timer expiry function type.
945 *
946 * A timer's expiry function is executed by the system clock interrupt handler
947 * each time the timer expires. The expiry function is optional, and is only
948 * invoked if the timer has been initialized with one.
949 *
950 * @param timer Address of timer.
951 *
952 * @return N/A
953 */
954typedef void (*k_timer_expiry_t)(struct k_timer *timer);
955
956/**
957 * @typedef k_timer_stop_t
958 * @brief Timer stop function type.
959 *
960 * A timer's stop function is executed if the timer is stopped prematurely.
961 * The function runs in the context of the thread that stops the timer.
962 * The stop function is optional, and is only invoked if the timer has been
963 * initialized with one.
964 *
965 * @param timer Address of timer.
966 *
967 * @return N/A
968 */
969typedef void (*k_timer_stop_t)(struct k_timer *timer);
970
971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500976 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977 *
978 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @param expiry_fn Function to invoke each time the timer expires.
980 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500982#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500983 struct k_timer name \
984 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500985 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400986
Allan Stephens45bfa372016-10-12 12:39:42 -0500987/**
988 * @brief Initialize a timer.
989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500991 *
992 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @param expiry_fn Function to invoke each time the timer expires.
994 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500995 *
996 * @return N/A
997 */
998extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500999 k_timer_expiry_t expiry_fn,
1000 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001001
Allan Stephens45bfa372016-10-12 12:39:42 -05001002/**
1003 * @brief Start a timer.
1004 *
1005 * This routine starts a timer, and resets its status to zero. The timer
1006 * begins counting down using the specified duration and period values.
1007 *
1008 * Attempting to start a timer that is already running is permitted.
1009 * The timer's status is reset to zero and the timer begins counting down
1010 * using the new duration and period values.
1011 *
1012 * @param timer Address of timer.
1013 * @param duration Initial timer duration (in milliseconds).
1014 * @param period Timer period (in milliseconds).
1015 *
1016 * @return N/A
1017 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001018extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001019 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001020
1021/**
1022 * @brief Stop a timer.
1023 *
1024 * This routine stops a running timer prematurely. The timer's stop function,
1025 * if one exists, is invoked by the caller.
1026 *
1027 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001029 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001030 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1031 * if @a k_timer_stop is to be called from ISRs.
1032 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001033 * @param timer Address of timer.
1034 *
1035 * @return N/A
1036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001038
1039/**
1040 * @brief Read timer status.
1041 *
1042 * This routine reads the timer's status, which indicates the number of times
1043 * it has expired since its status was last read.
1044 *
1045 * Calling this routine resets the timer's status to zero.
1046 *
1047 * @param timer Address of timer.
1048 *
1049 * @return Timer status.
1050 */
Kumar Galacc334c72017-04-21 10:55:34 -05001051extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001052
1053/**
1054 * @brief Synchronize thread to timer expiration.
1055 *
1056 * This routine blocks the calling thread until the timer's status is non-zero
1057 * (indicating that it has expired at least once since it was last examined)
1058 * or the timer is stopped. If the timer status is already non-zero,
1059 * or the timer is already stopped, the caller continues without waiting.
1060 *
1061 * Calling this routine resets the timer's status to zero.
1062 *
1063 * This routine must not be used by interrupt handlers, since they are not
1064 * allowed to block.
1065 *
1066 * @param timer Address of timer.
1067 *
1068 * @return Timer status.
1069 */
Kumar Galacc334c72017-04-21 10:55:34 -05001070extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001071
1072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001074 *
1075 * This routine computes the (approximate) time remaining before a running
1076 * timer next expires. If the timer is not running, it returns zero.
1077 *
1078 * @param timer Address of timer.
1079 *
1080 * @return Remaining time (in milliseconds).
1081 */
Kumar Galacc334c72017-04-21 10:55:34 -05001082static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001083{
1084 return _timeout_remaining_get(&timer->timeout);
1085}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001086
Allan Stephensc98da842016-11-11 15:45:03 -05001087/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001088 * @brief Associate user-specific data with a timer.
1089 *
1090 * This routine records the @a user_data with the @a timer, to be retrieved
1091 * later.
1092 *
1093 * It can be used e.g. in a timer handler shared across multiple subsystems to
1094 * retrieve data specific to the subsystem this timer is associated with.
1095 *
1096 * @param timer Address of timer.
1097 * @param user_data User data to associate with the timer.
1098 *
1099 * @return N/A
1100 */
1101static inline void k_timer_user_data_set(struct k_timer *timer,
1102 void *user_data)
1103{
1104 timer->user_data = user_data;
1105}
1106
1107/**
1108 * @brief Retrieve the user-specific data from a timer.
1109 *
1110 * @param timer Address of timer.
1111 *
1112 * @return The user data.
1113 */
1114static inline void *k_timer_user_data_get(struct k_timer *timer)
1115{
1116 return timer->user_data;
1117}
1118
1119/**
Allan Stephensc98da842016-11-11 15:45:03 -05001120 * @} end defgroup timer_apis
1121 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001122
Allan Stephensc98da842016-11-11 15:45:03 -05001123/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001124 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001125 * @{
1126 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001127
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * This routine returns the elapsed time since the system booted,
1132 * in milliseconds.
1133 *
1134 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001135 */
Kumar Galacc334c72017-04-21 10:55:34 -05001136extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001137
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001138#ifdef CONFIG_TICKLESS_KERNEL
1139/**
1140 * @brief Enable clock always on in tickless kernel
1141 *
1142 * This routine enables keepng the clock running when
1143 * there are no timer events programmed in tickless kernel
1144 * scheduling. This is necessary if the clock is used to track
1145 * passage of time.
1146 *
1147 * @retval prev_status Previous status of always on flag
1148 */
1149static inline int k_enable_sys_clock_always_on(void)
1150{
1151 int prev_status = _sys_clock_always_on;
1152
1153 _sys_clock_always_on = 1;
1154 _enable_sys_clock();
1155
1156 return prev_status;
1157}
1158
1159/**
1160 * @brief Disable clock always on in tickless kernel
1161 *
1162 * This routine disables keepng the clock running when
1163 * there are no timer events programmed in tickless kernel
1164 * scheduling. To save power, this routine should be called
1165 * immediately when clock is not used to track time.
1166 */
1167static inline void k_disable_sys_clock_always_on(void)
1168{
1169 _sys_clock_always_on = 0;
1170}
1171#else
1172#define k_enable_sys_clock_always_on() do { } while ((0))
1173#define k_disable_sys_clock_always_on() do { } while ((0))
1174#endif
1175
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * This routine returns the lower 32-bits of the elapsed time since the system
1180 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * This routine can be more efficient than k_uptime_get(), as it reduces the
1183 * need for interrupt locking and 64-bit math. However, the 32-bit result
1184 * cannot hold a system uptime time larger than approximately 50 days, so the
1185 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001187 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001188 */
Kumar Galacc334c72017-04-21 10:55:34 -05001189extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001190
1191/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001194 * This routine computes the elapsed time between the current system uptime
1195 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @param reftime Pointer to a reference time, which is updated to the current
1198 * uptime upon return.
1199 *
1200 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001201 */
Kumar Galacc334c72017-04-21 10:55:34 -05001202extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001203
1204/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001205 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001207 * This routine computes the elapsed time between the current system uptime
1208 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001210 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1211 * need for interrupt locking and 64-bit math. However, the 32-bit result
1212 * cannot hold an elapsed time larger than approximately 50 days, so the
1213 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @param reftime Pointer to a reference time, which is updated to the current
1216 * uptime upon return.
1217 *
1218 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001219 */
Kumar Galacc334c72017-04-21 10:55:34 -05001220extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001221
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * This routine returns the current time, as measured by the system's hardware
1226 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001230#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001231
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001233 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001234 */
1235
Allan Stephensc98da842016-11-11 15:45:03 -05001236/**
1237 * @cond INTERNAL_HIDDEN
1238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001239
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001240struct k_queue {
1241 _wait_q_t wait_q;
1242 sys_slist_t data_q;
1243 _POLL_EVENT;
1244
1245 _OBJECT_TRACING_NEXT_PTR(k_queue);
1246};
1247
1248#define K_QUEUE_INITIALIZER(obj) \
1249 { \
1250 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1251 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1252 _POLL_EVENT_OBJ_INIT \
1253 _OBJECT_TRACING_INIT \
1254 }
1255
1256/**
1257 * INTERNAL_HIDDEN @endcond
1258 */
1259
1260/**
1261 * @defgroup queue_apis Queue APIs
1262 * @ingroup kernel_apis
1263 * @{
1264 */
1265
1266/**
1267 * @brief Initialize a queue.
1268 *
1269 * This routine initializes a queue object, prior to its first use.
1270 *
1271 * @param queue Address of the queue.
1272 *
1273 * @return N/A
1274 */
1275extern void k_queue_init(struct k_queue *queue);
1276
1277/**
1278 * @brief Append an element to the end of a queue.
1279 *
1280 * This routine appends a data item to @a queue. A queue data item must be
1281 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1282 * reserved for the kernel's use.
1283 *
1284 * @note Can be called by ISRs.
1285 *
1286 * @param queue Address of the queue.
1287 * @param data Address of the data item.
1288 *
1289 * @return N/A
1290 */
1291extern void k_queue_append(struct k_queue *queue, void *data);
1292
1293/**
1294 * @brief Prepend an element to a queue.
1295 *
1296 * This routine prepends a data item to @a queue. A queue data item must be
1297 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1298 * reserved for the kernel's use.
1299 *
1300 * @note Can be called by ISRs.
1301 *
1302 * @param queue Address of the queue.
1303 * @param data Address of the data item.
1304 *
1305 * @return N/A
1306 */
1307extern void k_queue_prepend(struct k_queue *queue, void *data);
1308
1309/**
1310 * @brief Inserts an element to a queue.
1311 *
1312 * This routine inserts a data item to @a queue after previous item. A queue
1313 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1314 * item are reserved for the kernel's use.
1315 *
1316 * @note Can be called by ISRs.
1317 *
1318 * @param queue Address of the queue.
1319 * @param prev Address of the previous data item.
1320 * @param data Address of the data item.
1321 *
1322 * @return N/A
1323 */
1324extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1325
1326/**
1327 * @brief Atomically append a list of elements to a queue.
1328 *
1329 * This routine adds a list of data items to @a queue in one operation.
1330 * The data items must be in a singly-linked list, with the first 32 bits
1331 * in each data item pointing to the next data item; the list must be
1332 * NULL-terminated.
1333 *
1334 * @note Can be called by ISRs.
1335 *
1336 * @param queue Address of the queue.
1337 * @param head Pointer to first node in singly-linked list.
1338 * @param tail Pointer to last node in singly-linked list.
1339 *
1340 * @return N/A
1341 */
1342extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1343
1344/**
1345 * @brief Atomically add a list of elements to a queue.
1346 *
1347 * This routine adds a list of data items to @a queue in one operation.
1348 * The data items must be in a singly-linked list implemented using a
1349 * sys_slist_t object. Upon completion, the original list is empty.
1350 *
1351 * @note Can be called by ISRs.
1352 *
1353 * @param queue Address of the queue.
1354 * @param list Pointer to sys_slist_t object.
1355 *
1356 * @return N/A
1357 */
1358extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1359
1360/**
1361 * @brief Get an element from a queue.
1362 *
1363 * This routine removes first data item from @a queue. The first 32 bits of the
1364 * data item are reserved for the kernel's use.
1365 *
1366 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1367 *
1368 * @param queue Address of the queue.
1369 * @param timeout Waiting period to obtain a data item (in milliseconds),
1370 * or one of the special values K_NO_WAIT and K_FOREVER.
1371 *
1372 * @return Address of the data item if successful; NULL if returned
1373 * without waiting, or waiting period timed out.
1374 */
Kumar Galacc334c72017-04-21 10:55:34 -05001375extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001376
1377/**
1378 * @brief Query a queue to see if it has data available.
1379 *
1380 * Note that the data might be already gone by the time this function returns
1381 * if other threads are also trying to read from the queue.
1382 *
1383 * @note Can be called by ISRs.
1384 *
1385 * @param queue Address of the queue.
1386 *
1387 * @return Non-zero if the queue is empty.
1388 * @return 0 if data is available.
1389 */
1390static inline int k_queue_is_empty(struct k_queue *queue)
1391{
1392 return (int)sys_slist_is_empty(&queue->data_q);
1393}
1394
1395/**
1396 * @brief Statically define and initialize a queue.
1397 *
1398 * The queue can be accessed outside the module where it is defined using:
1399 *
1400 * @code extern struct k_queue <name>; @endcode
1401 *
1402 * @param name Name of the queue.
1403 */
1404#define K_QUEUE_DEFINE(name) \
1405 struct k_queue name \
1406 __in_section(_k_queue, static, name) = \
1407 K_QUEUE_INITIALIZER(name)
1408
1409/**
1410 * @} end defgroup queue_apis
1411 */
1412
1413/**
1414 * @cond INTERNAL_HIDDEN
1415 */
1416
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001418 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001419};
1420
Allan Stephensc98da842016-11-11 15:45:03 -05001421#define K_FIFO_INITIALIZER(obj) \
1422 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001423 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001424 }
1425
1426/**
1427 * INTERNAL_HIDDEN @endcond
1428 */
1429
1430/**
1431 * @defgroup fifo_apis Fifo APIs
1432 * @ingroup kernel_apis
1433 * @{
1434 */
1435
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001437 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001439 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001441 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442 *
1443 * @return N/A
1444 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001445#define k_fifo_init(fifo) \
1446 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001447
1448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001449 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * This routine adds a data item to @a fifo. A fifo data item must be
1452 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1453 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * @note Can be called by ISRs.
1456 *
1457 * @param fifo Address of the fifo.
1458 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
1460 * @return N/A
1461 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001462#define k_fifo_put(fifo, data) \
1463 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001464
1465/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001468 * This routine adds a list of data items to @a fifo in one operation.
1469 * The data items must be in a singly-linked list, with the first 32 bits
1470 * each data item pointing to the next data item; the list must be
1471 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001473 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * @param fifo Address of the fifo.
1476 * @param head Pointer to first node in singly-linked list.
1477 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478 *
1479 * @return N/A
1480 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001481#define k_fifo_put_list(fifo, head, tail) \
1482 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483
1484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001485 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001487 * This routine adds a list of data items to @a fifo in one operation.
1488 * The data items must be in a singly-linked list implemented using a
1489 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001490 * and must be re-initialized via sys_slist_init().
1491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * @note Can be called by ISRs.
1493 *
1494 * @param fifo Address of the fifo.
1495 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
1497 * @return N/A
1498 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001499#define k_fifo_put_slist(fifo, list) \
1500 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001501
1502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001503 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * This routine removes a data item from @a fifo in a "first in, first out"
1506 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001508 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1509 *
1510 * @param fifo Address of the fifo.
1511 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001512 * or one of the special values K_NO_WAIT and K_FOREVER.
1513 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001514 * @return Address of the data item if successful; NULL if returned
1515 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001516 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001517#define k_fifo_get(fifo, timeout) \
1518 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001520/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001521 * @brief Query a fifo to see if it has data available.
1522 *
1523 * Note that the data might be already gone by the time this function returns
1524 * if other threads is also trying to read from the fifo.
1525 *
1526 * @note Can be called by ISRs.
1527 *
1528 * @param fifo Address of the fifo.
1529 *
1530 * @return Non-zero if the fifo is empty.
1531 * @return 0 if data is available.
1532 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001533#define k_fifo_is_empty(fifo) \
1534 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001535
1536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001540 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001541 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001543 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001544 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001546 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001547 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001548 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001549
Allan Stephensc98da842016-11-11 15:45:03 -05001550/**
1551 * @} end defgroup fifo_apis
1552 */
1553
1554/**
1555 * @cond INTERNAL_HIDDEN
1556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001557
1558struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001559 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001560};
1561
Allan Stephensc98da842016-11-11 15:45:03 -05001562#define K_LIFO_INITIALIZER(obj) \
1563 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001564 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001565 }
1566
1567/**
1568 * INTERNAL_HIDDEN @endcond
1569 */
1570
1571/**
1572 * @defgroup lifo_apis Lifo APIs
1573 * @ingroup kernel_apis
1574 * @{
1575 */
1576
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001580 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001582 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001583 *
1584 * @return N/A
1585 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001586#define k_lifo_init(lifo) \
1587 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588
1589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * This routine adds a data item to @a lifo. A lifo data item must be
1593 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1594 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * @note Can be called by ISRs.
1597 *
1598 * @param lifo Address of the lifo.
1599 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001600 *
1601 * @return N/A
1602 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001603#define k_lifo_put(lifo, data) \
1604 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001605
1606/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * This routine removes a data item from @a lifo in a "last in, first out"
1610 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1613 *
1614 * @param lifo Address of the lifo.
1615 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001616 * or one of the special values K_NO_WAIT and K_FOREVER.
1617 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001618 * @return Address of the data item if successful; NULL if returned
1619 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001621#define k_lifo_get(lifo, timeout) \
1622 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001623
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001627 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001628 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001629 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001632 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001633#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001634 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001635 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001636 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001637
Allan Stephensc98da842016-11-11 15:45:03 -05001638/**
1639 * @} end defgroup lifo_apis
1640 */
1641
1642/**
1643 * @cond INTERNAL_HIDDEN
1644 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001645
1646struct k_stack {
1647 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001648 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001649
Anas Nashif2f203c22016-12-18 06:57:45 -05001650 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651};
1652
Allan Stephensc98da842016-11-11 15:45:03 -05001653#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1654 { \
1655 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1656 .base = stack_buffer, \
1657 .next = stack_buffer, \
1658 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001659 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001660 }
1661
1662/**
1663 * INTERNAL_HIDDEN @endcond
1664 */
1665
1666/**
1667 * @defgroup stack_apis Stack APIs
1668 * @ingroup kernel_apis
1669 * @{
1670 */
1671
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672/**
1673 * @brief Initialize a stack.
1674 *
1675 * This routine initializes a stack object, prior to its first use.
1676 *
1677 * @param stack Address of the stack.
1678 * @param buffer Address of array used to hold stacked values.
1679 * @param num_entries Maximum number of values that can be stacked.
1680 *
1681 * @return N/A
1682 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001683extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001684 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685
1686/**
1687 * @brief Push an element onto a stack.
1688 *
1689 * This routine adds a 32-bit value @a data to @a stack.
1690 *
1691 * @note Can be called by ISRs.
1692 *
1693 * @param stack Address of the stack.
1694 * @param data Value to push onto the stack.
1695 *
1696 * @return N/A
1697 */
Kumar Galacc334c72017-04-21 10:55:34 -05001698extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699
1700/**
1701 * @brief Pop an element from a stack.
1702 *
1703 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1704 * manner and stores the value in @a data.
1705 *
1706 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1707 *
1708 * @param stack Address of the stack.
1709 * @param data Address of area to hold the value popped from the stack.
1710 * @param timeout Waiting period to obtain a value (in milliseconds),
1711 * or one of the special values K_NO_WAIT and K_FOREVER.
1712 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001713 * @retval 0 Element popped from stack.
1714 * @retval -EBUSY Returned without waiting.
1715 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 */
Kumar Galacc334c72017-04-21 10:55:34 -05001717extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001724 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @param name Name of the stack.
1727 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001729#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001730 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001731 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001732 struct k_stack name \
1733 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001734 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1735 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001736
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737/**
Allan Stephensc98da842016-11-11 15:45:03 -05001738 * @} end defgroup stack_apis
1739 */
1740
Allan Stephens6bba9b02016-11-16 14:56:54 -05001741struct k_work;
1742
Allan Stephensc98da842016-11-11 15:45:03 -05001743/**
1744 * @defgroup workqueue_apis Workqueue Thread APIs
1745 * @ingroup kernel_apis
1746 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747 */
1748
Allan Stephens6bba9b02016-11-16 14:56:54 -05001749/**
1750 * @typedef k_work_handler_t
1751 * @brief Work item handler function type.
1752 *
1753 * A work item's handler function is executed by a workqueue's thread
1754 * when the work item is processed by the workqueue.
1755 *
1756 * @param work Address of the work item.
1757 *
1758 * @return N/A
1759 */
1760typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001761
1762/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001763 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001764 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001765
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766struct k_work_q {
1767 struct k_fifo fifo;
1768};
1769
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001770enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001771 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001772};
1773
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774struct k_work {
1775 void *_reserved; /* Used by k_fifo implementation. */
1776 k_work_handler_t handler;
1777 atomic_t flags[1];
1778};
1779
Allan Stephens6bba9b02016-11-16 14:56:54 -05001780struct k_delayed_work {
1781 struct k_work work;
1782 struct _timeout timeout;
1783 struct k_work_q *work_q;
1784};
1785
1786extern struct k_work_q k_sys_work_q;
1787
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001788/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001789 * INTERNAL_HIDDEN @endcond
1790 */
1791
1792/**
1793 * @brief Initialize a statically-defined work item.
1794 *
1795 * This macro can be used to initialize a statically-defined workqueue work
1796 * item, prior to its first use. For example,
1797 *
1798 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1799 *
1800 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001801 */
1802#define K_WORK_INITIALIZER(work_handler) \
1803 { \
1804 ._reserved = NULL, \
1805 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001806 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001807 }
1808
1809/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001810 * @brief Initialize a work item.
1811 *
1812 * This routine initializes a workqueue work item, prior to its first use.
1813 *
1814 * @param work Address of work item.
1815 * @param handler Function to invoke each time work item is processed.
1816 *
1817 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818 */
1819static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1820{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001821 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001822 work->handler = handler;
1823}
1824
1825/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001826 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001827 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001828 * This routine submits work item @a work to be processed by workqueue
1829 * @a work_q. If the work item is already pending in the workqueue's queue
1830 * as a result of an earlier submission, this routine has no effect on the
1831 * work item. If the work item has already been processed, or is currently
1832 * being processed, its work is considered complete and the work item can be
1833 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001834 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001835 * @warning
1836 * A submitted work item must not be modified until it has been processed
1837 * by the workqueue.
1838 *
1839 * @note Can be called by ISRs.
1840 *
1841 * @param work_q Address of workqueue.
1842 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001843 *
1844 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001845 */
1846static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1847 struct k_work *work)
1848{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001849 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001850 k_fifo_put(&work_q->fifo, work);
1851 }
1852}
1853
1854/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001855 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001857 * This routine indicates if work item @a work is pending in a workqueue's
1858 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001860 * @note Can be called by ISRs.
1861 *
1862 * @param work Address of work item.
1863 *
1864 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001865 */
1866static inline int k_work_pending(struct k_work *work)
1867{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001868 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001869}
1870
1871/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001872 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001873 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001874 * This routine starts workqueue @a work_q. The workqueue spawns its work
1875 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001877 * @param work_q Address of workqueue.
1878 * @param stack Pointer to work queue thread's stack space.
1879 * @param stack_size Size of the work queue thread's stack (in bytes).
1880 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 *
1882 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883 */
Allan Stephens904cf972016-10-07 13:59:23 -05001884extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001885 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001886
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001888 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001890 * This routine initializes a workqueue delayed work item, prior to
1891 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001893 * @param work Address of delayed work item.
1894 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
1896 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001898extern void k_delayed_work_init(struct k_delayed_work *work,
1899 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900
1901/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001902 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001904 * This routine schedules work item @a work to be processed by workqueue
1905 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001906 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001907 * Only when the countdown completes is the work item actually submitted to
1908 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001910 * Submitting a previously submitted delayed work item that is still
1911 * counting down cancels the existing submission and restarts the countdown
1912 * using the new delay. If the work item is currently pending on the
1913 * workqueue's queue because the countdown has completed it is too late to
1914 * resubmit the item, and resubmission fails without impacting the work item.
1915 * If the work item has already been processed, or is currently being processed,
1916 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001917 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001918 * @warning
1919 * A delayed work item must not be modified until it has been processed
1920 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001922 * @note Can be called by ISRs.
1923 *
1924 * @param work_q Address of workqueue.
1925 * @param work Address of delayed work item.
1926 * @param delay Delay before submitting the work item (in milliseconds).
1927 *
1928 * @retval 0 Work item countdown started.
1929 * @retval -EINPROGRESS Work item is already pending.
1930 * @retval -EINVAL Work item is being processed or has completed its work.
1931 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001933extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1934 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05001935 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001936
1937/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001938 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001940 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07001941 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05001942 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001943 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001944 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001946 * @param work Address of delayed work item.
1947 *
David B. Kinder8b986d72017-04-18 15:56:26 -07001948 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001949 * @retval -EINPROGRESS Work item is already pending.
1950 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001952extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001953
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955 * @brief Submit a work item to the system workqueue.
1956 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001957 * This routine submits work item @a work to be processed by the system
1958 * workqueue. If the work item is already pending in the workqueue's queue
1959 * as a result of an earlier submission, this routine has no effect on the
1960 * work item. If the work item has already been processed, or is currently
1961 * being processed, its work is considered complete and the work item can be
1962 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001964 * @warning
1965 * Work items submitted to the system workqueue should avoid using handlers
1966 * that block or yield since this may prevent the system workqueue from
1967 * processing other work items in a timely manner.
1968 *
1969 * @note Can be called by ISRs.
1970 *
1971 * @param work Address of work item.
1972 *
1973 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974 */
1975static inline void k_work_submit(struct k_work *work)
1976{
1977 k_work_submit_to_queue(&k_sys_work_q, work);
1978}
1979
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981 * @brief Submit a delayed work item to the system workqueue.
1982 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001983 * This routine schedules work item @a work to be processed by the system
1984 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001985 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001986 * Only when the countdown completes is the work item actually submitted to
1987 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001988 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001989 * Submitting a previously submitted delayed work item that is still
1990 * counting down cancels the existing submission and restarts the countdown
1991 * using the new delay. If the work item is currently pending on the
1992 * workqueue's queue because the countdown has completed it is too late to
1993 * resubmit the item, and resubmission fails without impacting the work item.
1994 * If the work item has already been processed, or is currently being processed,
1995 * its work is considered complete and the work item can be resubmitted.
1996 *
1997 * @warning
1998 * Work items submitted to the system workqueue should avoid using handlers
1999 * that block or yield since this may prevent the system workqueue from
2000 * processing other work items in a timely manner.
2001 *
2002 * @note Can be called by ISRs.
2003 *
2004 * @param work Address of delayed work item.
2005 * @param delay Delay before submitting the work item (in milliseconds).
2006 *
2007 * @retval 0 Work item countdown started.
2008 * @retval -EINPROGRESS Work item is already pending.
2009 * @retval -EINVAL Work item is being processed or has completed its work.
2010 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002011 */
2012static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002013 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002015 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002016}
2017
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002019 * @brief Get time remaining before a delayed work gets scheduled.
2020 *
2021 * This routine computes the (approximate) time remaining before a
2022 * delayed work gets executed. If the delayed work is not waiting to be
2023 * schedules, it returns zero.
2024 *
2025 * @param work Delayed work item.
2026 *
2027 * @return Remaining time (in milliseconds).
2028 */
Kumar Galacc334c72017-04-21 10:55:34 -05002029static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002030{
2031 return _timeout_remaining_get(&work->timeout);
2032}
2033
2034/**
Allan Stephensc98da842016-11-11 15:45:03 -05002035 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036 */
2037
Allan Stephensc98da842016-11-11 15:45:03 -05002038/**
2039 * @cond INTERNAL_HIDDEN
2040 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041
2042struct k_mutex {
2043 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002044 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002045 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047
Anas Nashif2f203c22016-12-18 06:57:45 -05002048 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049};
2050
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051#define K_MUTEX_INITIALIZER(obj) \
2052 { \
2053 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2054 .owner = NULL, \
2055 .lock_count = 0, \
2056 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002057 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058 }
2059
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002060/**
Allan Stephensc98da842016-11-11 15:45:03 -05002061 * INTERNAL_HIDDEN @endcond
2062 */
2063
2064/**
2065 * @defgroup mutex_apis Mutex APIs
2066 * @ingroup kernel_apis
2067 * @{
2068 */
2069
2070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002075 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002080 struct k_mutex name \
2081 __in_section(_k_mutex, static, name) = \
2082 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002087 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * Upon completion, the mutex is available and does not have an owner.
2090 *
2091 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 *
2093 * @return N/A
2094 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002096
2097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 * This routine locks @a mutex. If the mutex is locked by another thread,
2101 * the calling thread waits until the mutex becomes available or until
2102 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * A thread is permitted to lock a mutex it has already locked. The operation
2105 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @param mutex Address of the mutex.
2108 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 * or one of the special values K_NO_WAIT and K_FOREVER.
2110 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002111 * @retval 0 Mutex locked.
2112 * @retval -EBUSY Returned without waiting.
2113 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 */
Kumar Galacc334c72017-04-21 10:55:34 -05002115extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116
2117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002118 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * This routine unlocks @a mutex. The mutex must already be locked by the
2121 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 *
2123 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002125 * thread.
2126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002128 *
2129 * @return N/A
2130 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131extern void k_mutex_unlock(struct k_mutex *mutex);
2132
Allan Stephensc98da842016-11-11 15:45:03 -05002133/**
2134 * @} end defgroup mutex_apis
2135 */
2136
2137/**
2138 * @cond INTERNAL_HIDDEN
2139 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002140
2141struct k_sem {
2142 _wait_q_t wait_q;
2143 unsigned int count;
2144 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002145 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146
Anas Nashif2f203c22016-12-18 06:57:45 -05002147 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148};
2149
Allan Stephensc98da842016-11-11 15:45:03 -05002150#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2151 { \
2152 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2153 .count = initial_count, \
2154 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002155 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002156 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002157 }
2158
2159/**
2160 * INTERNAL_HIDDEN @endcond
2161 */
2162
2163/**
2164 * @defgroup semaphore_apis Semaphore APIs
2165 * @ingroup kernel_apis
2166 * @{
2167 */
2168
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002169/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @param sem Address of the semaphore.
2175 * @param initial_count Initial semaphore count.
2176 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002177 *
2178 * @return N/A
2179 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2181 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002182
2183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002188 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2189 *
2190 * @param sem Address of the semaphore.
2191 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002192 * or one of the special values K_NO_WAIT and K_FOREVER.
2193 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002194 * @note When porting code from the nanokernel legacy API to the new API, be
2195 * careful with the return value of this function. The return value is the
2196 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2197 * non-zero means failure, while the nano_sem_take family returns 1 for success
2198 * and 0 for failure.
2199 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002200 * @retval 0 Semaphore taken.
2201 * @retval -EBUSY Returned without waiting.
2202 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002203 */
Kumar Galacc334c72017-04-21 10:55:34 -05002204extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002205
2206/**
2207 * @brief Give a semaphore.
2208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002209 * This routine gives @a sem, unless the semaphore is already at its maximum
2210 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002212 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002214 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002215 *
2216 * @return N/A
2217 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218extern void k_sem_give(struct k_sem *sem);
2219
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002220/**
2221 * @brief Reset a semaphore's count to zero.
2222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002223 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002225 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002226 *
2227 * @return N/A
2228 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002229static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230{
2231 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232}
2233
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002234/**
2235 * @brief Get a semaphore's count.
2236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002239 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002242 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002243static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244{
2245 return sem->count;
2246}
2247
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002249 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002251 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002252 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002253 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002255 * @param name Name of the semaphore.
2256 * @param initial_count Initial semaphore count.
2257 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002258 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002260 struct k_sem name \
2261 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262 K_SEM_INITIALIZER(name, initial_count, count_limit)
2263
Allan Stephensc98da842016-11-11 15:45:03 -05002264/**
2265 * @} end defgroup semaphore_apis
2266 */
2267
2268/**
2269 * @defgroup alert_apis Alert APIs
2270 * @ingroup kernel_apis
2271 * @{
2272 */
2273
Allan Stephens5eceb852016-11-16 10:16:30 -05002274/**
2275 * @typedef k_alert_handler_t
2276 * @brief Alert handler function type.
2277 *
2278 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002279 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002280 * and is only invoked if the alert has been initialized with one.
2281 *
2282 * @param alert Address of the alert.
2283 *
2284 * @return 0 if alert has been consumed; non-zero if alert should pend.
2285 */
2286typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002287
2288/**
2289 * @} end defgroup alert_apis
2290 */
2291
2292/**
2293 * @cond INTERNAL_HIDDEN
2294 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002296#define K_ALERT_DEFAULT NULL
2297#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002299struct k_alert {
2300 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 atomic_t send_count;
2302 struct k_work work_item;
2303 struct k_sem sem;
2304
Anas Nashif2f203c22016-12-18 06:57:45 -05002305 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306};
2307
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002308extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002310#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002312 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002314 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002315 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002316 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317 }
2318
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319/**
Allan Stephensc98da842016-11-11 15:45:03 -05002320 * INTERNAL_HIDDEN @endcond
2321 */
2322
2323/**
2324 * @addtogroup alert_apis
2325 * @{
2326 */
2327
2328/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002331 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002333 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * @param name Name of the alert.
2336 * @param alert_handler Action to take when alert is sent. Specify either
2337 * the address of a function to be invoked by the system workqueue
2338 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2339 * K_ALERT_DEFAULT (which causes the alert to pend).
2340 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002342#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002343 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002344 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002345 K_ALERT_INITIALIZER(name, alert_handler, \
2346 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * @param alert Address of the alert.
2354 * @param handler Action to take when alert is sent. Specify either the address
2355 * of a function to be invoked by the system workqueue thread,
2356 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2357 * K_ALERT_DEFAULT (which causes the alert to pend).
2358 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
2360 * @return N/A
2361 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002362extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2363 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364
2365/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002370 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2371 *
2372 * @param alert Address of the alert.
2373 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374 * or one of the special values K_NO_WAIT and K_FOREVER.
2375 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002376 * @retval 0 Alert received.
2377 * @retval -EBUSY Returned without waiting.
2378 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379 */
Kumar Galacc334c72017-04-21 10:55:34 -05002380extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381
2382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * This routine signals @a alert. The action specified for @a alert will
2386 * be taken, which may trigger the execution of an alert handler function
2387 * and/or cause the alert to pend (assuming the alert has not reached its
2388 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002390 * @note Can be called by ISRs.
2391 *
2392 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393 *
2394 * @return N/A
2395 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002396extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397
2398/**
Allan Stephensc98da842016-11-11 15:45:03 -05002399 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002400 */
2401
Allan Stephensc98da842016-11-11 15:45:03 -05002402/**
2403 * @cond INTERNAL_HIDDEN
2404 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405
2406struct k_msgq {
2407 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002408 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002409 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 char *buffer_start;
2411 char *buffer_end;
2412 char *read_ptr;
2413 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002414 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415
Anas Nashif2f203c22016-12-18 06:57:45 -05002416 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417};
2418
Peter Mitsis1da807e2016-10-06 11:36:59 -04002419#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420 { \
2421 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002422 .max_msgs = q_max_msgs, \
2423 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002425 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426 .read_ptr = q_buffer, \
2427 .write_ptr = q_buffer, \
2428 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002429 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430 }
2431
Peter Mitsis1da807e2016-10-06 11:36:59 -04002432/**
Allan Stephensc98da842016-11-11 15:45:03 -05002433 * INTERNAL_HIDDEN @endcond
2434 */
2435
2436/**
2437 * @defgroup msgq_apis Message Queue APIs
2438 * @ingroup kernel_apis
2439 * @{
2440 */
2441
2442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2446 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002447 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2448 * message is similarly aligned to this boundary, @a q_msg_size must also be
2449 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * The message queue can be accessed outside the module where it is defined
2452 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002453 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002454 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @param q_name Name of the message queue.
2457 * @param q_msg_size Message size (in bytes).
2458 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002459 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002460 */
2461#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2462 static char __noinit __aligned(q_align) \
2463 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002464 struct k_msgq q_name \
2465 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002466 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2467 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468
Peter Mitsisd7a37502016-10-13 11:37:40 -04002469/**
2470 * @brief Initialize a message queue.
2471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 * This routine initializes a message queue object, prior to its first use.
2473 *
Allan Stephensda827222016-11-09 14:23:58 -06002474 * The message queue's ring buffer must contain space for @a max_msgs messages,
2475 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2476 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2477 * that each message is similarly aligned to this boundary, @a q_msg_size
2478 * must also be a multiple of N.
2479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * @param q Address of the message queue.
2481 * @param buffer Pointer to ring buffer that holds queued messages.
2482 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002483 * @param max_msgs Maximum number of messages that can be queued.
2484 *
2485 * @return N/A
2486 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002487extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002488 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002489
2490/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002494 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002495 * @note Can be called by ISRs.
2496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @param q Address of the message queue.
2498 * @param data Pointer to the message.
2499 * @param timeout Waiting period to add the message (in milliseconds),
2500 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002501 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002502 * @retval 0 Message sent.
2503 * @retval -ENOMSG Returned without waiting or queue purged.
2504 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002505 */
Kumar Galacc334c72017-04-21 10:55:34 -05002506extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002507
2508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * This routine receives a message from message queue @a q in a "first in,
2512 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002513 *
Allan Stephensc98da842016-11-11 15:45:03 -05002514 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @param q Address of the message queue.
2517 * @param data Address of area to hold the received message.
2518 * @param timeout Waiting period to receive the message (in milliseconds),
2519 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002520 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002521 * @retval 0 Message received.
2522 * @retval -ENOMSG Returned without waiting.
2523 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002524 */
Kumar Galacc334c72017-04-21 10:55:34 -05002525extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002526
2527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002530 * This routine discards all unreceived messages in a message queue's ring
2531 * buffer. Any threads that are blocked waiting to send a message to the
2532 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002535 *
2536 * @return N/A
2537 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538extern void k_msgq_purge(struct k_msgq *q);
2539
Peter Mitsis67be2492016-10-07 11:44:34 -04002540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * This routine returns the number of unused entries in a message queue's
2544 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * @param q Address of the message queue.
2547 *
2548 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002549 */
Kumar Galacc334c72017-04-21 10:55:34 -05002550static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002551{
2552 return q->max_msgs - q->used_msgs;
2553}
2554
Peter Mitsisd7a37502016-10-13 11:37:40 -04002555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @param q Address of the message queue.
2561 *
2562 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002563 */
Kumar Galacc334c72017-04-21 10:55:34 -05002564static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565{
2566 return q->used_msgs;
2567}
2568
Allan Stephensc98da842016-11-11 15:45:03 -05002569/**
2570 * @} end defgroup msgq_apis
2571 */
2572
2573/**
2574 * @defgroup mem_pool_apis Memory Pool APIs
2575 * @ingroup kernel_apis
2576 * @{
2577 */
2578
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002580 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581 void *addr_in_pool;
2582 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002583 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584};
2585
Allan Stephensc98da842016-11-11 15:45:03 -05002586/**
2587 * @} end defgroup mem_pool_apis
2588 */
2589
2590/**
2591 * @defgroup mailbox_apis Mailbox APIs
2592 * @ingroup kernel_apis
2593 * @{
2594 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595
2596struct k_mbox_msg {
2597 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002598 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002600 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002602 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603 /** sender's message data buffer */
2604 void *tx_data;
2605 /** internal use only - needed for legacy API support */
2606 void *_rx_data;
2607 /** message data block descriptor */
2608 struct k_mem_block tx_block;
2609 /** source thread id */
2610 k_tid_t rx_source_thread;
2611 /** target thread id */
2612 k_tid_t tx_target_thread;
2613 /** internal use only - thread waiting on send (may be a dummy) */
2614 k_tid_t _syncing_thread;
2615#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2616 /** internal use only - semaphore used during asynchronous send */
2617 struct k_sem *_async_sem;
2618#endif
2619};
2620
Allan Stephensc98da842016-11-11 15:45:03 -05002621/**
2622 * @cond INTERNAL_HIDDEN
2623 */
2624
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625struct k_mbox {
2626 _wait_q_t tx_msg_queue;
2627 _wait_q_t rx_msg_queue;
2628
Anas Nashif2f203c22016-12-18 06:57:45 -05002629 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630};
2631
2632#define K_MBOX_INITIALIZER(obj) \
2633 { \
2634 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2635 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002636 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637 }
2638
Peter Mitsis12092702016-10-14 12:57:23 -04002639/**
Allan Stephensc98da842016-11-11 15:45:03 -05002640 * INTERNAL_HIDDEN @endcond
2641 */
2642
2643/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002648 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002651 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002653 struct k_mbox name \
2654 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655 K_MBOX_INITIALIZER(name) \
2656
Peter Mitsis12092702016-10-14 12:57:23 -04002657/**
2658 * @brief Initialize a mailbox.
2659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * This routine initializes a mailbox object, prior to its first use.
2661 *
2662 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002663 *
2664 * @return N/A
2665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666extern void k_mbox_init(struct k_mbox *mbox);
2667
Peter Mitsis12092702016-10-14 12:57:23 -04002668/**
2669 * @brief Send a mailbox message in a synchronous manner.
2670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * This routine sends a message to @a mbox and waits for a receiver to both
2672 * receive and process it. The message data may be in a buffer, in a memory
2673 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @param mbox Address of the mailbox.
2676 * @param tx_msg Address of the transmit message descriptor.
2677 * @param timeout Waiting period for the message to be received (in
2678 * milliseconds), or one of the special values K_NO_WAIT
2679 * and K_FOREVER. Once the message has been received,
2680 * this routine waits as long as necessary for the message
2681 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002682 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002683 * @retval 0 Message sent.
2684 * @retval -ENOMSG Returned without waiting.
2685 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002686 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002687extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002688 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002689
Peter Mitsis12092702016-10-14 12:57:23 -04002690/**
2691 * @brief Send a mailbox message in an asynchronous manner.
2692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002693 * This routine sends a message to @a mbox without waiting for a receiver
2694 * to process it. The message data may be in a buffer, in a memory pool block,
2695 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2696 * will be given when the message has been both received and completely
2697 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002699 * @param mbox Address of the mailbox.
2700 * @param tx_msg Address of the transmit message descriptor.
2701 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002702 *
2703 * @return N/A
2704 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002705extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 struct k_sem *sem);
2707
Peter Mitsis12092702016-10-14 12:57:23 -04002708/**
2709 * @brief Receive a mailbox message.
2710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * This routine receives a message from @a mbox, then optionally retrieves
2712 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * @param mbox Address of the mailbox.
2715 * @param rx_msg Address of the receive message descriptor.
2716 * @param buffer Address of the buffer to receive data, or NULL to defer data
2717 * retrieval and message disposal until later.
2718 * @param timeout Waiting period for a message to be received (in
2719 * milliseconds), or one of the special values K_NO_WAIT
2720 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002721 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002722 * @retval 0 Message received.
2723 * @retval -ENOMSG Returned without waiting.
2724 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002725 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002726extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002727 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002728
2729/**
2730 * @brief Retrieve mailbox message data into a buffer.
2731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * This routine completes the processing of a received message by retrieving
2733 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002734 *
2735 * Alternatively, this routine can be used to dispose of a received message
2736 * without retrieving its data.
2737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * @param rx_msg Address of the receive message descriptor.
2739 * @param buffer Address of the buffer to receive data, or NULL to discard
2740 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002741 *
2742 * @return N/A
2743 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002744extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002745
2746/**
2747 * @brief Retrieve mailbox message data into a memory pool block.
2748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002749 * This routine completes the processing of a received message by retrieving
2750 * its data into a memory pool block, then disposing of the message.
2751 * The memory pool block that results from successful retrieval must be
2752 * returned to the pool once the data has been processed, even in cases
2753 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002754 *
2755 * Alternatively, this routine can be used to dispose of a received message
2756 * without retrieving its data. In this case there is no need to return a
2757 * memory pool block to the pool.
2758 *
2759 * This routine allocates a new memory pool block for the data only if the
2760 * data is not already in one. If a new block cannot be allocated, the routine
2761 * returns a failure code and the received message is left unchanged. This
2762 * permits the caller to reattempt data retrieval at a later time or to dispose
2763 * of the received message without retrieving its data.
2764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @param rx_msg Address of a receive message descriptor.
2766 * @param pool Address of memory pool, or NULL to discard data.
2767 * @param block Address of the area to hold memory pool block info.
2768 * @param timeout Waiting period to wait for a memory pool block (in
2769 * milliseconds), or one of the special values K_NO_WAIT
2770 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002771 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002772 * @retval 0 Data retrieved.
2773 * @retval -ENOMEM Returned without waiting.
2774 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002775 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002776extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002777 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002778 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779
Allan Stephensc98da842016-11-11 15:45:03 -05002780/**
2781 * @} end defgroup mailbox_apis
2782 */
2783
2784/**
2785 * @cond INTERNAL_HIDDEN
2786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787
2788struct k_pipe {
2789 unsigned char *buffer; /* Pipe buffer: may be NULL */
2790 size_t size; /* Buffer size */
2791 size_t bytes_used; /* # bytes used in buffer */
2792 size_t read_index; /* Where in buffer to read from */
2793 size_t write_index; /* Where in buffer to write */
2794
2795 struct {
2796 _wait_q_t readers; /* Reader wait queue */
2797 _wait_q_t writers; /* Writer wait queue */
2798 } wait_q;
2799
Anas Nashif2f203c22016-12-18 06:57:45 -05002800 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801};
2802
Peter Mitsise5d9c582016-10-14 14:44:57 -04002803#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804 { \
2805 .buffer = pipe_buffer, \
2806 .size = pipe_buffer_size, \
2807 .bytes_used = 0, \
2808 .read_index = 0, \
2809 .write_index = 0, \
2810 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2811 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002812 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813 }
2814
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815/**
Allan Stephensc98da842016-11-11 15:45:03 -05002816 * INTERNAL_HIDDEN @endcond
2817 */
2818
2819/**
2820 * @defgroup pipe_apis Pipe APIs
2821 * @ingroup kernel_apis
2822 * @{
2823 */
2824
2825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002826 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002830 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002832 * @param name Name of the pipe.
2833 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2834 * or zero if no ring buffer is used.
2835 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002836 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002837#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2838 static unsigned char __noinit __aligned(pipe_align) \
2839 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002840 struct k_pipe name \
2841 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002842 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * This routine initializes a pipe object, prior to its first use.
2848 *
2849 * @param pipe Address of the pipe.
2850 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2851 * is used.
2852 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2853 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854 *
2855 * @return N/A
2856 */
2857extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2858 size_t size);
2859
2860/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @param pipe Address of the pipe.
2866 * @param data Address of data to write.
2867 * @param bytes_to_write Size of data (in bytes).
2868 * @param bytes_written Address of area to hold the number of bytes written.
2869 * @param min_xfer Minimum number of bytes to write.
2870 * @param timeout Waiting period to wait for the data to be written (in
2871 * milliseconds), or one of the special values K_NO_WAIT
2872 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002873 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002874 * @retval 0 At least @a min_xfer bytes of data were written.
2875 * @retval -EIO Returned without waiting; zero data bytes were written.
2876 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002879extern int k_pipe_put(struct k_pipe *pipe, void *data,
2880 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002881 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882
2883/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param pipe Address of the pipe.
2889 * @param data Address to place the data read from pipe.
2890 * @param bytes_to_read Maximum number of data bytes to read.
2891 * @param bytes_read Address of area to hold the number of bytes read.
2892 * @param min_xfer Minimum number of data bytes to read.
2893 * @param timeout Waiting period to wait for the data to be read (in
2894 * milliseconds), or one of the special values K_NO_WAIT
2895 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002897 * @retval 0 At least @a min_xfer bytes of data were read.
2898 * @retval -EIO Returned without waiting; zero data bytes were read.
2899 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002902extern int k_pipe_get(struct k_pipe *pipe, void *data,
2903 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002904 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002905
2906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * This routine writes the data contained in a memory block to @a pipe.
2910 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914 * @param block Memory block containing data to send
2915 * @param size Number of data bytes in memory block to send
2916 * @param sem Semaphore to signal upon completion (else NULL)
2917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919 */
2920extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2921 size_t size, struct k_sem *sem);
2922
2923/**
Allan Stephensc98da842016-11-11 15:45:03 -05002924 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002925 */
2926
Allan Stephensc98da842016-11-11 15:45:03 -05002927/**
2928 * @cond INTERNAL_HIDDEN
2929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002931struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002933 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002934 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935 char *buffer;
2936 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05002937 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938
Anas Nashif2f203c22016-12-18 06:57:45 -05002939 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940};
2941
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002942#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2943 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 { \
2945 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002946 .num_blocks = slab_num_blocks, \
2947 .block_size = slab_block_size, \
2948 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 .free_list = NULL, \
2950 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002951 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002952 }
2953
Peter Mitsis578f9112016-10-07 13:50:31 -04002954/**
Allan Stephensc98da842016-11-11 15:45:03 -05002955 * INTERNAL_HIDDEN @endcond
2956 */
2957
2958/**
2959 * @defgroup mem_slab_apis Memory Slab APIs
2960 * @ingroup kernel_apis
2961 * @{
2962 */
2963
2964/**
Allan Stephensda827222016-11-09 14:23:58 -06002965 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002966 *
Allan Stephensda827222016-11-09 14:23:58 -06002967 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002969 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2970 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002971 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002972 *
Allan Stephensda827222016-11-09 14:23:58 -06002973 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002975 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002976 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @param name Name of the memory slab.
2979 * @param slab_block_size Size of each memory block (in bytes).
2980 * @param slab_num_blocks Number memory blocks.
2981 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002982 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002983#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2984 char __noinit __aligned(slab_align) \
2985 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2986 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002987 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002988 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2989 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002991/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002992 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002995 *
Allan Stephensda827222016-11-09 14:23:58 -06002996 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2997 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2998 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2999 * To ensure that each memory block is similarly aligned to this boundary,
3000 * @a slab_block_size must also be a multiple of N.
3001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @param slab Address of the memory slab.
3003 * @param buffer Pointer to buffer used for the memory blocks.
3004 * @param block_size Size of each memory block (in bytes).
3005 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003006 *
3007 * @return N/A
3008 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003009extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003010 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003011
3012/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * @param slab Address of the memory slab.
3018 * @param mem Pointer to block address area.
3019 * @param timeout Maximum time to wait for operation to complete
3020 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3021 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003022 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003023 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003025 * @retval -ENOMEM Returned without waiting.
3026 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003027 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003028extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003029 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003030
3031/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * This routine releases a previously allocated memory block back to its
3035 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @param slab Address of the memory slab.
3038 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003039 *
3040 * @return N/A
3041 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003042extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * This routine gets the number of memory blocks that are currently
3048 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003053 */
Kumar Galacc334c72017-04-21 10:55:34 -05003054static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003055{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003056 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003057}
3058
Peter Mitsisc001aa82016-10-13 13:53:37 -04003059/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * This routine gets the number of memory blocks that are currently
3063 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003067 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003068 */
Kumar Galacc334c72017-04-21 10:55:34 -05003069static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003070{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003071 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003072}
3073
Allan Stephensc98da842016-11-11 15:45:03 -05003074/**
3075 * @} end defgroup mem_slab_apis
3076 */
3077
3078/**
3079 * @cond INTERNAL_HIDDEN
3080 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003082/*
3083 * Memory pool requires a buffer and two arrays of structures for the
3084 * memory block accounting:
3085 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
3086 * status of four blocks of memory.
3087 */
3088struct k_mem_pool_quad_block {
3089 char *mem_blocks; /* pointer to the first of four memory blocks */
Kumar Galacc334c72017-04-21 10:55:34 -05003090 u32_t mem_status; /* four bits. If bit is set, memory block is
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003091 allocated */
3092};
3093/*
3094 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
3095 * blocks of one size. Block sizes go from maximal to minimal. Next memory
3096 * block size is 4 times less than the previous one and thus requires 4 times
3097 * bigger array of k_mem_pool_quad_block structures to keep track of the
3098 * memory blocks.
3099 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003101/*
3102 * The array of k_mem_pool_block_set keeps the information of each array of
3103 * k_mem_pool_quad_block structures
3104 */
3105struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04003106 size_t block_size; /* memory block size */
Kumar Galacc334c72017-04-21 10:55:34 -05003107 u32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003108 struct k_mem_pool_quad_block *quad_block;
3109 int count;
3110};
3111
3112/* Memory pool descriptor */
3113struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04003114 size_t max_block_size;
3115 size_t min_block_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003116 u32_t nr_of_maxblocks;
3117 u32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003118 struct k_mem_pool_block_set *block_set;
3119 char *bufblock;
3120 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05003121 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122};
3123
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003124#ifdef CONFIG_ARM
3125#define _SECTION_TYPE_SIGN "%"
3126#else
3127#define _SECTION_TYPE_SIGN "@"
3128#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003130/*
3131 * Static memory pool initialization
3132 */
Allan Stephensc98da842016-11-11 15:45:03 -05003133
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003134/*
3135 * Use .altmacro to be able to recalculate values and pass them as string
David B. Kinder8b986d72017-04-18 15:56:26 -07003136 * arguments when calling assembler macros recursively
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003137 */
3138__asm__(".altmacro\n\t");
3139
3140/*
3141 * Recursively calls a macro
David B. Kinder8b986d72017-04-18 15:56:26 -07003142 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003143 * __memory_pool_max_block_size - maximal size of the memory block
3144 * __memory_pool_min_block_size - minimal size of the memory block
3145 * Notes:
3146 * Global symbols are used due the fact that assembler macro allows only
3147 * one argument be passed with the % conversion
3148 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3149 * is used instead of / 4.
3150 * n_max argument needs to go first in the invoked macro, as some
3151 * assemblers concatenate \name and %(\n_max * 4) arguments
3152 * if \name goes first
3153 */
3154__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3155 ".ifge __memory_pool_max_block_size >> 2 -"
3156 " __memory_pool_min_block_size\n\t\t"
3157 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3158 "\\macro_name %(\\n_max * 4) \\name\n\t"
3159 ".endif\n\t"
3160 ".endm\n");
3161
3162/*
3163 * Build quad blocks
3164 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3165 * structures and recursively calls itself for the next array, 4 times
3166 * larger.
David B. Kinder8b986d72017-04-18 15:56:26 -07003167 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003168 * __memory_pool_max_block_size - maximal size of the memory block
3169 * __memory_pool_min_block_size - minimal size of the memory block
3170 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3171 */
3172__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003173 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003174 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3175 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3176 ".if \\n_max % 4\n\t\t"
3177 ".skip __memory_pool_quad_block_size\n\t"
3178 ".endif\n\t"
3179 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3180 ".endm\n");
3181
3182/*
3183 * Build block sets and initialize them
3184 * Macro initializes the k_mem_pool_block_set structure and
3185 * recursively calls itself for the next one.
David B. Kinder8b986d72017-04-18 15:56:26 -07003186 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003187 * __memory_pool_max_block_size - maximal size of the memory block
3188 * __memory_pool_min_block_size - minimal size of the memory block
3189 * __memory_pool_block_set_count, the number of the elements in the
3190 * block set array must be set to 0. Macro calculates it's real
3191 * value.
3192 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3193 * structures, _build_quad_blocks must be called prior it.
3194 */
3195__asm__(".macro _build_block_set n_max, name\n\t"
3196 ".int __memory_pool_max_block_size\n\t" /* block_size */
3197 ".if \\n_max % 4\n\t\t"
3198 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3199 ".else\n\t\t"
3200 ".int \\n_max >> 2\n\t"
3201 ".endif\n\t"
3202 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3203 ".int 0\n\t" /* count */
3204 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3205 "__do_recurse _build_block_set \\name \\n_max\n\t"
3206 ".endm\n");
3207
3208/*
3209 * Build a memory pool structure and initialize it
3210 * Macro uses __memory_pool_block_set_count global symbol,
3211 * block set addresses and buffer address, it may be called only after
3212 * _build_block_set
3213 */
3214__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003215 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003216 _SECTION_TYPE_SIGN "progbits\n\t"
3217 ".globl \\name\n\t"
3218 "\\name:\n\t"
3219 ".int \\max_size\n\t" /* max_block_size */
3220 ".int \\min_size\n\t" /* min_block_size */
3221 ".int \\n_max\n\t" /* nr_of_maxblocks */
3222 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3223 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3224 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3225 ".int 0\n\t" /* wait_q->head */
3226 ".int 0\n\t" /* wait_q->next */
3227 ".popsection\n\t"
3228 ".endm\n");
3229
3230#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3231 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3232 _SECTION_TYPE_SIGN "progbits\n\t"); \
3233 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3234 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3235 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3236 STRINGIFY(name) "\n\t"); \
3237 __asm__(".popsection\n\t")
3238
3239#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3240 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3241 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3242 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3243 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003244 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003245 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3246 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3247 STRINGIFY(name) "\n\t"); \
3248 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3249 __asm__(".int __memory_pool_block_set_count\n\t"); \
3250 __asm__(".popsection\n\t"); \
Kumar Galacc334c72017-04-21 10:55:34 -05003251 extern u32_t _mem_pool_block_set_count_##name; \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003252 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3253
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003254#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3255 char __noinit __aligned(align) \
3256 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003257
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003258/*
3259 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3260 * to __memory_pool_quad_block_size absolute symbol.
3261 * This function does not get called, but compiler calculates the value and
3262 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3263 */
3264static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3265{
3266 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003267#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003268 "__memory_pool_quad_block_size = %0\n\t"
3269#else
3270 "__memory_pool_quad_block_size = %c0\n\t"
3271#endif
3272 :
3273 : "n"(sizeof(struct k_mem_pool_quad_block)));
3274}
3275
3276/**
Allan Stephensc98da842016-11-11 15:45:03 -05003277 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003278 */
3279
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003280/**
Allan Stephensc98da842016-11-11 15:45:03 -05003281 * @addtogroup mem_pool_apis
3282 * @{
3283 */
3284
3285/**
3286 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3289 * long. The memory pool allows blocks to be repeatedly partitioned into
3290 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3291 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003292 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003294 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 * If the pool is to be accessed outside the module where it is defined, it
3296 * can be declared via
3297 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003298 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003300 * @param name Name of the memory pool.
3301 * @param min_size Size of the smallest blocks in the pool (in bytes).
3302 * @param max_size Size of the largest blocks in the pool (in bytes).
3303 * @param n_max Number of maximum sized blocks in the pool.
3304 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003305 */
3306#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003307 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3308 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003309 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003310 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3311 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3312 extern struct k_mem_pool name
3313
Peter Mitsis937042c2016-10-13 13:18:26 -04003314/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003319 * @param pool Address of the memory pool.
3320 * @param block Pointer to block descriptor for the allocated memory.
3321 * @param size Amount of memory to allocate (in bytes).
3322 * @param timeout Maximum time to wait for operation to complete
3323 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3324 * or K_FOREVER to wait as long as necessary.
3325 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003326 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003328 * @retval -ENOMEM Returned without waiting.
3329 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003330 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003331extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003332 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003333
3334/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * This routine releases a previously allocated memory block back to its
3338 * memory pool.
3339 *
3340 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003341 *
3342 * @return N/A
3343 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003345
3346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * This routine instructs a memory pool to concatenate unused memory blocks
3350 * into larger blocks wherever possible. Manually defragmenting the memory
3351 * pool may speed up future allocations of memory blocks by eliminating the
3352 * need for the memory pool to perform an automatic partial defragmentation.
3353 *
3354 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003355 *
3356 * @return N/A
3357 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003358extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003359
3360/**
Allan Stephensc98da842016-11-11 15:45:03 -05003361 * @} end addtogroup mem_pool_apis
3362 */
3363
3364/**
3365 * @defgroup heap_apis Heap Memory Pool APIs
3366 * @ingroup kernel_apis
3367 * @{
3368 */
3369
3370/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003373 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003374 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003379 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003380extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003381
3382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003384 *
3385 * This routine provides traditional free() semantics. The memory being
3386 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003387 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003388 * If @a ptr is NULL, no operation is performed.
3389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003391 *
3392 * @return N/A
3393 */
3394extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395
Allan Stephensc98da842016-11-11 15:45:03 -05003396/**
3397 * @} end defgroup heap_apis
3398 */
3399
Benjamin Walshacc68c12017-01-29 18:57:45 -05003400/* polling API - PRIVATE */
3401
Benjamin Walshb0179862017-02-02 16:39:57 -05003402#ifdef CONFIG_POLL
3403#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3404#else
3405#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3406#endif
3407
Benjamin Walshacc68c12017-01-29 18:57:45 -05003408/* private - implementation data created as needed, per-type */
3409struct _poller {
3410 struct k_thread *thread;
3411};
3412
3413/* private - types bit positions */
3414enum _poll_types_bits {
3415 /* can be used to ignore an event */
3416 _POLL_TYPE_IGNORE,
3417
3418 /* to be signaled by k_poll_signal() */
3419 _POLL_TYPE_SIGNAL,
3420
3421 /* semaphore availability */
3422 _POLL_TYPE_SEM_AVAILABLE,
3423
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003424 /* queue/fifo/lifo data availability */
3425 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003426
3427 _POLL_NUM_TYPES
3428};
3429
3430#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3431
3432/* private - states bit positions */
3433enum _poll_states_bits {
3434 /* default state when creating event */
3435 _POLL_STATE_NOT_READY,
3436
3437 /* there was another poller already on the object */
3438 _POLL_STATE_EADDRINUSE,
3439
3440 /* signaled by k_poll_signal() */
3441 _POLL_STATE_SIGNALED,
3442
3443 /* semaphore is available */
3444 _POLL_STATE_SEM_AVAILABLE,
3445
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003446 /* data is available to read on queue/fifo/lifo */
3447 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003448
3449 _POLL_NUM_STATES
3450};
3451
3452#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3453
3454#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003455 (32 - (0 \
3456 + 8 /* tag */ \
3457 + _POLL_NUM_TYPES \
3458 + _POLL_NUM_STATES \
3459 + 1 /* modes */ \
3460 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003461
3462#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3463#error overflow of 32-bit word in struct k_poll_event
3464#endif
3465
3466/* end of polling API - PRIVATE */
3467
3468
3469/**
3470 * @defgroup poll_apis Async polling APIs
3471 * @ingroup kernel_apis
3472 * @{
3473 */
3474
3475/* Public polling API */
3476
3477/* public - values for k_poll_event.type bitfield */
3478#define K_POLL_TYPE_IGNORE 0
3479#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3480#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003481#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3482#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003483
3484/* public - polling modes */
3485enum k_poll_modes {
3486 /* polling thread does not take ownership of objects when available */
3487 K_POLL_MODE_NOTIFY_ONLY = 0,
3488
3489 K_POLL_NUM_MODES
3490};
3491
3492/* public - values for k_poll_event.state bitfield */
3493#define K_POLL_STATE_NOT_READY 0
3494#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3495#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3496#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003497#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3498#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003499
3500/* public - poll signal object */
3501struct k_poll_signal {
3502 /* PRIVATE - DO NOT TOUCH */
3503 struct k_poll_event *poll_event;
3504
3505 /*
3506 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3507 * user resets it to 0.
3508 */
3509 unsigned int signaled;
3510
3511 /* custom result value passed to k_poll_signal() if needed */
3512 int result;
3513};
3514
3515#define K_POLL_SIGNAL_INITIALIZER() \
3516 { \
3517 .poll_event = NULL, \
3518 .signaled = 0, \
3519 .result = 0, \
3520 }
3521
3522struct k_poll_event {
3523 /* PRIVATE - DO NOT TOUCH */
3524 struct _poller *poller;
3525
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003526 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003527 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003528
Benjamin Walshacc68c12017-01-29 18:57:45 -05003529 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003530 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003531
3532 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003533 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003534
3535 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003536 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003537
3538 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003539 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003540
3541 /* per-type data */
3542 union {
3543 void *obj;
3544 struct k_poll_signal *signal;
3545 struct k_sem *sem;
3546 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003547 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003548 };
3549};
3550
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003551#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003552 { \
3553 .poller = NULL, \
3554 .type = event_type, \
3555 .state = K_POLL_STATE_NOT_READY, \
3556 .mode = event_mode, \
3557 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003558 { .obj = event_obj }, \
3559 }
3560
3561#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3562 event_tag) \
3563 { \
3564 .type = event_type, \
3565 .tag = event_tag, \
3566 .state = K_POLL_STATE_NOT_READY, \
3567 .mode = event_mode, \
3568 .unused = 0, \
3569 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003570 }
3571
3572/**
3573 * @brief Initialize one struct k_poll_event instance
3574 *
3575 * After this routine is called on a poll event, the event it ready to be
3576 * placed in an event array to be passed to k_poll().
3577 *
3578 * @param event The event to initialize.
3579 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3580 * values. Only values that apply to the same object being polled
3581 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3582 * event.
3583 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3584 * @param obj Kernel object or poll signal.
3585 *
3586 * @return N/A
3587 */
3588
Kumar Galacc334c72017-04-21 10:55:34 -05003589extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003590 int mode, void *obj);
3591
3592/**
3593 * @brief Wait for one or many of multiple poll events to occur
3594 *
3595 * This routine allows a thread to wait concurrently for one or many of
3596 * multiple poll events to have occurred. Such events can be a kernel object
3597 * being available, like a semaphore, or a poll signal event.
3598 *
3599 * When an event notifies that a kernel object is available, the kernel object
3600 * is not "given" to the thread calling k_poll(): it merely signals the fact
3601 * that the object was available when the k_poll() call was in effect. Also,
3602 * all threads trying to acquire an object the regular way, i.e. by pending on
3603 * the object, have precedence over the thread polling on the object. This
3604 * means that the polling thread will never get the poll event on an object
3605 * until the object becomes available and its pend queue is empty. For this
3606 * reason, the k_poll() call is more effective when the objects being polled
3607 * only have one thread, the polling thread, trying to acquire them.
3608 *
3609 * Only one thread can be polling for a particular object at a given time. If
3610 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3611 * and returns as soon as it has finished handling the other events. This means
3612 * that k_poll() can return -EADDRINUSE and have the state value of some events
3613 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3614 * parameter is ignored.
3615 *
3616 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3617 * events that were passed to k_poll() and check the state field for the values
3618 * that were expected and take the associated actions.
3619 *
3620 * Before being reused for another call to k_poll(), the user has to reset the
3621 * state field to K_POLL_STATE_NOT_READY.
3622 *
3623 * @param events An array of pointers to events to be polled for.
3624 * @param num_events The number of events in the array.
3625 * @param timeout Waiting period for an event to be ready (in milliseconds),
3626 * or one of the special values K_NO_WAIT and K_FOREVER.
3627 *
3628 * @retval 0 One or more events are ready.
3629 * @retval -EADDRINUSE One or more objects already had a poller.
3630 * @retval -EAGAIN Waiting period timed out.
3631 */
3632
3633extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003634 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003635
3636/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003637 * @brief Initialize a poll signal object.
3638 *
3639 * Ready a poll signal object to be signaled via k_poll_signal().
3640 *
3641 * @param signal A poll signal.
3642 *
3643 * @return N/A
3644 */
3645
3646extern void k_poll_signal_init(struct k_poll_signal *signal);
3647
3648/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003649 * @brief Signal a poll signal object.
3650 *
3651 * This routine makes ready a poll signal, which is basically a poll event of
3652 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3653 * made ready to run. A @a result value can be specified.
3654 *
3655 * The poll signal contains a 'signaled' field that, when set by
3656 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3657 * be reset by the user before being passed again to k_poll() or k_poll() will
3658 * consider it being signaled, and will return immediately.
3659 *
3660 * @param signal A poll signal.
3661 * @param result The value to store in the result field of the signal.
3662 *
3663 * @retval 0 The signal was delivered successfully.
3664 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3665 */
3666
3667extern int k_poll_signal(struct k_poll_signal *signal, int result);
3668
3669/* private internal function */
3670extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003671 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003672
3673/**
3674 * @} end defgroup poll_apis
3675 */
3676
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003677/**
3678 * @brief Make the CPU idle.
3679 *
3680 * This function makes the CPU idle until an event wakes it up.
3681 *
3682 * In a regular system, the idle thread should be the only thread responsible
3683 * for making the CPU idle and triggering any type of power management.
3684 * However, in some more constrained systems, such as a single-threaded system,
3685 * the only thread would be responsible for this if needed.
3686 *
3687 * @return N/A
3688 */
3689extern void k_cpu_idle(void);
3690
3691/**
3692 * @brief Make the CPU idle in an atomic fashion.
3693 *
3694 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3695 * must be done atomically before making the CPU idle.
3696 *
3697 * @param key Interrupt locking key obtained from irq_lock().
3698 *
3699 * @return N/A
3700 */
3701extern void k_cpu_atomic_idle(unsigned int key);
3702
Kumar Galacc334c72017-04-21 10:55:34 -05003703extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003704
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003705#include <arch/cpu.h>
3706
Andrew Boiecdb94d62017-04-18 15:22:05 -07003707#ifdef _ARCH_EXCEPT
3708/* This archtecture has direct support for triggering a CPU exception */
3709#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3710#else
3711
3712#include <misc/printk.h>
3713
3714/* NOTE: This is the implementation for arches that do not implement
3715 * _ARCH_EXCEPT() to generate a real CPU exception.
3716 *
3717 * We won't have a real exception frame to determine the PC value when
3718 * the oops occurred, so print file and line number before we jump into
3719 * the fatal error handler.
3720 */
3721#define _k_except_reason(reason) do { \
3722 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3723 _NanoFatalErrorHandler(reason, &_default_esf); \
3724 CODE_UNREACHABLE; \
3725 } while (0)
3726
3727#endif /* _ARCH__EXCEPT */
3728
3729/**
3730 * @brief Fatally terminate a thread
3731 *
3732 * This should be called when a thread has encountered an unrecoverable
3733 * runtime condition and needs to terminate. What this ultimately
3734 * means is determined by the _fatal_error_handler() implementation, which
3735 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3736 *
3737 * If this is called from ISR context, the default system fatal error handler
3738 * will treat it as an unrecoverable system error, just like k_panic().
3739 */
3740#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3741
3742/**
3743 * @brief Fatally terminate the system
3744 *
3745 * This should be called when the Zephyr kernel has encountered an
3746 * unrecoverable runtime condition and needs to terminate. What this ultimately
3747 * means is determined by the _fatal_error_handler() implementation, which
3748 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3749 */
3750#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3751
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752/*
3753 * private APIs that are utilized by one or more public APIs
3754 */
3755
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003756#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003757extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003758#else
3759#define _init_static_threads() do { } while ((0))
3760#endif
3761
3762extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003763extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764
3765#ifdef __cplusplus
3766}
3767#endif
3768
Andrew Boiee004dec2016-11-07 09:01:19 -08003769#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3770/*
3771 * Define new and delete operators.
3772 * At this moment, the operators do nothing since objects are supposed
3773 * to be statically allocated.
3774 */
3775inline void operator delete(void *ptr)
3776{
3777 (void)ptr;
3778}
3779
3780inline void operator delete[](void *ptr)
3781{
3782 (void)ptr;
3783}
3784
3785inline void *operator new(size_t size)
3786{
3787 (void)size;
3788 return NULL;
3789}
3790
3791inline void *operator new[](size_t size)
3792{
3793 (void)size;
3794 return NULL;
3795}
3796
3797/* Placement versions of operator new and delete */
3798inline void operator delete(void *ptr1, void *ptr2)
3799{
3800 (void)ptr1;
3801 (void)ptr2;
3802}
3803
3804inline void operator delete[](void *ptr1, void *ptr2)
3805{
3806 (void)ptr1;
3807 (void)ptr2;
3808}
3809
3810inline void *operator new(size_t size, void *ptr)
3811{
3812 (void)size;
3813 return ptr;
3814}
3815
3816inline void *operator new[](size_t size, void *ptr)
3817{
3818 (void)size;
3819 return ptr;
3820}
3821
3822#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3823
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003824#endif /* !_ASMLANGUAGE */
3825
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003826#endif /* _kernel__h_ */