blob: 51e604443519c39aa1a3b8db2528c5caee194049 [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 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001142 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001143 * 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 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001162 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001163 * 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/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001278 * @brief Cancel waiting on a queue.
1279 *
1280 * This routine causes first thread pending on @a queue, if any, to
1281 * return from k_queue_get() call with NULL value (as if timeout expired).
1282 *
1283 * @note Can be called by ISRs.
1284 *
1285 * @param queue Address of the queue.
1286 *
1287 * @return N/A
1288 */
1289extern void k_queue_cancel_wait(struct k_queue *queue);
1290
1291/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001292 * @brief Append an element to the end of a queue.
1293 *
1294 * This routine appends a data item to @a queue. A queue data item must be
1295 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1296 * reserved for the kernel's use.
1297 *
1298 * @note Can be called by ISRs.
1299 *
1300 * @param queue Address of the queue.
1301 * @param data Address of the data item.
1302 *
1303 * @return N/A
1304 */
1305extern void k_queue_append(struct k_queue *queue, void *data);
1306
1307/**
1308 * @brief Prepend an element to a queue.
1309 *
1310 * This routine prepends a data item to @a queue. A queue data item must be
1311 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1312 * reserved for the kernel's use.
1313 *
1314 * @note Can be called by ISRs.
1315 *
1316 * @param queue Address of the queue.
1317 * @param data Address of the data item.
1318 *
1319 * @return N/A
1320 */
1321extern void k_queue_prepend(struct k_queue *queue, void *data);
1322
1323/**
1324 * @brief Inserts an element to a queue.
1325 *
1326 * This routine inserts a data item to @a queue after previous item. A queue
1327 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1328 * item are reserved for the kernel's use.
1329 *
1330 * @note Can be called by ISRs.
1331 *
1332 * @param queue Address of the queue.
1333 * @param prev Address of the previous data item.
1334 * @param data Address of the data item.
1335 *
1336 * @return N/A
1337 */
1338extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1339
1340/**
1341 * @brief Atomically append a list of elements to a queue.
1342 *
1343 * This routine adds a list of data items to @a queue in one operation.
1344 * The data items must be in a singly-linked list, with the first 32 bits
1345 * in each data item pointing to the next data item; the list must be
1346 * NULL-terminated.
1347 *
1348 * @note Can be called by ISRs.
1349 *
1350 * @param queue Address of the queue.
1351 * @param head Pointer to first node in singly-linked list.
1352 * @param tail Pointer to last node in singly-linked list.
1353 *
1354 * @return N/A
1355 */
1356extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1357
1358/**
1359 * @brief Atomically add a list of elements to a queue.
1360 *
1361 * This routine adds a list of data items to @a queue in one operation.
1362 * The data items must be in a singly-linked list implemented using a
1363 * sys_slist_t object. Upon completion, the original list is empty.
1364 *
1365 * @note Can be called by ISRs.
1366 *
1367 * @param queue Address of the queue.
1368 * @param list Pointer to sys_slist_t object.
1369 *
1370 * @return N/A
1371 */
1372extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1373
1374/**
1375 * @brief Get an element from a queue.
1376 *
1377 * This routine removes first data item from @a queue. The first 32 bits of the
1378 * data item are reserved for the kernel's use.
1379 *
1380 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1381 *
1382 * @param queue Address of the queue.
1383 * @param timeout Waiting period to obtain a data item (in milliseconds),
1384 * or one of the special values K_NO_WAIT and K_FOREVER.
1385 *
1386 * @return Address of the data item if successful; NULL if returned
1387 * without waiting, or waiting period timed out.
1388 */
Kumar Galacc334c72017-04-21 10:55:34 -05001389extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001390
1391/**
1392 * @brief Query a queue to see if it has data available.
1393 *
1394 * Note that the data might be already gone by the time this function returns
1395 * if other threads are also trying to read from the queue.
1396 *
1397 * @note Can be called by ISRs.
1398 *
1399 * @param queue Address of the queue.
1400 *
1401 * @return Non-zero if the queue is empty.
1402 * @return 0 if data is available.
1403 */
1404static inline int k_queue_is_empty(struct k_queue *queue)
1405{
1406 return (int)sys_slist_is_empty(&queue->data_q);
1407}
1408
1409/**
1410 * @brief Statically define and initialize a queue.
1411 *
1412 * The queue can be accessed outside the module where it is defined using:
1413 *
1414 * @code extern struct k_queue <name>; @endcode
1415 *
1416 * @param name Name of the queue.
1417 */
1418#define K_QUEUE_DEFINE(name) \
1419 struct k_queue name \
1420 __in_section(_k_queue, static, name) = \
1421 K_QUEUE_INITIALIZER(name)
1422
1423/**
1424 * @} end defgroup queue_apis
1425 */
1426
1427/**
1428 * @cond INTERNAL_HIDDEN
1429 */
1430
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001432 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433};
1434
Allan Stephensc98da842016-11-11 15:45:03 -05001435#define K_FIFO_INITIALIZER(obj) \
1436 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001437 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001438 }
1439
1440/**
1441 * INTERNAL_HIDDEN @endcond
1442 */
1443
1444/**
1445 * @defgroup fifo_apis Fifo APIs
1446 * @ingroup kernel_apis
1447 * @{
1448 */
1449
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456 *
1457 * @return N/A
1458 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001459#define k_fifo_init(fifo) \
1460 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001461
1462/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001463 * @brief Cancel waiting on a fifo.
1464 *
1465 * This routine causes first thread pending on @a fifo, if any, to
1466 * return from k_fifo_get() call with NULL value (as if timeout
1467 * expired).
1468 *
1469 * @note Can be called by ISRs.
1470 *
1471 * @param fifo Address of the fifo.
1472 *
1473 * @return N/A
1474 */
1475#define k_fifo_cancel_wait(fifo) \
1476 k_queue_cancel_wait((struct k_queue *) fifo)
1477
1478/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001479 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * This routine adds a data item to @a fifo. A fifo data item must be
1482 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1483 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001485 * @note Can be called by ISRs.
1486 *
1487 * @param fifo Address of the fifo.
1488 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
1490 * @return N/A
1491 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001492#define k_fifo_put(fifo, data) \
1493 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494
1495/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * This routine adds a list of data items to @a fifo in one operation.
1499 * The data items must be in a singly-linked list, with the first 32 bits
1500 * each data item pointing to the next data item; the list must be
1501 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001503 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @param fifo Address of the fifo.
1506 * @param head Pointer to first node in singly-linked list.
1507 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 *
1509 * @return N/A
1510 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001511#define k_fifo_put_list(fifo, head, tail) \
1512 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513
1514/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001515 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001517 * This routine adds a list of data items to @a fifo in one operation.
1518 * The data items must be in a singly-linked list implemented using a
1519 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001520 * and must be re-initialized via sys_slist_init().
1521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001522 * @note Can be called by ISRs.
1523 *
1524 * @param fifo Address of the fifo.
1525 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001526 *
1527 * @return N/A
1528 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001529#define k_fifo_put_slist(fifo, list) \
1530 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001531
1532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001533 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535 * This routine removes a data item from @a fifo in a "first in, first out"
1536 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001538 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1539 *
1540 * @param fifo Address of the fifo.
1541 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001542 * or one of the special values K_NO_WAIT and K_FOREVER.
1543 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001544 * @return Address of the data item if successful; NULL if returned
1545 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001546 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001547#define k_fifo_get(fifo, timeout) \
1548 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001549
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001550/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001551 * @brief Query a fifo to see if it has data available.
1552 *
1553 * Note that the data might be already gone by the time this function returns
1554 * if other threads is also trying to read from the fifo.
1555 *
1556 * @note Can be called by ISRs.
1557 *
1558 * @param fifo Address of the fifo.
1559 *
1560 * @return Non-zero if the fifo is empty.
1561 * @return 0 if data is available.
1562 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001563#define k_fifo_is_empty(fifo) \
1564 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001565
1566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001569 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001570 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001571 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001574 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001575#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001576 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001577 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001578 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001579
Allan Stephensc98da842016-11-11 15:45:03 -05001580/**
1581 * @} end defgroup fifo_apis
1582 */
1583
1584/**
1585 * @cond INTERNAL_HIDDEN
1586 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001587
1588struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001589 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001590};
1591
Allan Stephensc98da842016-11-11 15:45:03 -05001592#define K_LIFO_INITIALIZER(obj) \
1593 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001594 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001595 }
1596
1597/**
1598 * INTERNAL_HIDDEN @endcond
1599 */
1600
1601/**
1602 * @defgroup lifo_apis Lifo APIs
1603 * @ingroup kernel_apis
1604 * @{
1605 */
1606
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001610 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001613 *
1614 * @return N/A
1615 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001616#define k_lifo_init(lifo) \
1617 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618
1619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * This routine adds a data item to @a lifo. A lifo data item must be
1623 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1624 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @note Can be called by ISRs.
1627 *
1628 * @param lifo Address of the lifo.
1629 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630 *
1631 * @return N/A
1632 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001633#define k_lifo_put(lifo, data) \
1634 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001635
1636/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001637 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * This routine removes a data item from @a lifo in a "last in, first out"
1640 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001642 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1643 *
1644 * @param lifo Address of the lifo.
1645 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646 * or one of the special values K_NO_WAIT and K_FOREVER.
1647 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001648 * @return Address of the data item if successful; NULL if returned
1649 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001651#define k_lifo_get(lifo, timeout) \
1652 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001653
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001658 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001659 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001661 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001664 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001665 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001666 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001667
Allan Stephensc98da842016-11-11 15:45:03 -05001668/**
1669 * @} end defgroup lifo_apis
1670 */
1671
1672/**
1673 * @cond INTERNAL_HIDDEN
1674 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675
1676struct k_stack {
1677 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001678 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001679
Anas Nashif2f203c22016-12-18 06:57:45 -05001680 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001681};
1682
Allan Stephensc98da842016-11-11 15:45:03 -05001683#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1684 { \
1685 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1686 .base = stack_buffer, \
1687 .next = stack_buffer, \
1688 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001689 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001690 }
1691
1692/**
1693 * INTERNAL_HIDDEN @endcond
1694 */
1695
1696/**
1697 * @defgroup stack_apis Stack APIs
1698 * @ingroup kernel_apis
1699 * @{
1700 */
1701
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702/**
1703 * @brief Initialize a stack.
1704 *
1705 * This routine initializes a stack object, prior to its first use.
1706 *
1707 * @param stack Address of the stack.
1708 * @param buffer Address of array used to hold stacked values.
1709 * @param num_entries Maximum number of values that can be stacked.
1710 *
1711 * @return N/A
1712 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001713extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001714 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715
1716/**
1717 * @brief Push an element onto a stack.
1718 *
1719 * This routine adds a 32-bit value @a data to @a stack.
1720 *
1721 * @note Can be called by ISRs.
1722 *
1723 * @param stack Address of the stack.
1724 * @param data Value to push onto the stack.
1725 *
1726 * @return N/A
1727 */
Kumar Galacc334c72017-04-21 10:55:34 -05001728extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729
1730/**
1731 * @brief Pop an element from a stack.
1732 *
1733 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1734 * manner and stores the value in @a data.
1735 *
1736 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1737 *
1738 * @param stack Address of the stack.
1739 * @param data Address of area to hold the value popped from the stack.
1740 * @param timeout Waiting period to obtain a value (in milliseconds),
1741 * or one of the special values K_NO_WAIT and K_FOREVER.
1742 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001743 * @retval 0 Element popped from stack.
1744 * @retval -EBUSY Returned without waiting.
1745 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 */
Kumar Galacc334c72017-04-21 10:55:34 -05001747extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001754 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001756 * @param name Name of the stack.
1757 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001759#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001760 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001761 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001762 struct k_stack name \
1763 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001764 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1765 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001767/**
Allan Stephensc98da842016-11-11 15:45:03 -05001768 * @} end defgroup stack_apis
1769 */
1770
Allan Stephens6bba9b02016-11-16 14:56:54 -05001771struct k_work;
1772
Allan Stephensc98da842016-11-11 15:45:03 -05001773/**
1774 * @defgroup workqueue_apis Workqueue Thread APIs
1775 * @ingroup kernel_apis
1776 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001777 */
1778
Allan Stephens6bba9b02016-11-16 14:56:54 -05001779/**
1780 * @typedef k_work_handler_t
1781 * @brief Work item handler function type.
1782 *
1783 * A work item's handler function is executed by a workqueue's thread
1784 * when the work item is processed by the workqueue.
1785 *
1786 * @param work Address of the work item.
1787 *
1788 * @return N/A
1789 */
1790typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791
1792/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001793 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001794 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001795
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001796struct k_work_q {
1797 struct k_fifo fifo;
1798};
1799
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001800enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001801 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802};
1803
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804struct k_work {
1805 void *_reserved; /* Used by k_fifo implementation. */
1806 k_work_handler_t handler;
1807 atomic_t flags[1];
1808};
1809
Allan Stephens6bba9b02016-11-16 14:56:54 -05001810struct k_delayed_work {
1811 struct k_work work;
1812 struct _timeout timeout;
1813 struct k_work_q *work_q;
1814};
1815
1816extern struct k_work_q k_sys_work_q;
1817
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001819 * INTERNAL_HIDDEN @endcond
1820 */
1821
1822/**
1823 * @brief Initialize a statically-defined work item.
1824 *
1825 * This macro can be used to initialize a statically-defined workqueue work
1826 * item, prior to its first use. For example,
1827 *
1828 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1829 *
1830 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831 */
1832#define K_WORK_INITIALIZER(work_handler) \
1833 { \
1834 ._reserved = NULL, \
1835 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001836 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837 }
1838
1839/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001840 * @brief Initialize a work item.
1841 *
1842 * This routine initializes a workqueue work item, prior to its first use.
1843 *
1844 * @param work Address of work item.
1845 * @param handler Function to invoke each time work item is processed.
1846 *
1847 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001848 */
1849static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1850{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001851 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001852 work->handler = handler;
1853}
1854
1855/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001856 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001857 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001858 * This routine submits work item @a work to be processed by workqueue
1859 * @a work_q. If the work item is already pending in the workqueue's queue
1860 * as a result of an earlier submission, this routine has no effect on the
1861 * work item. If the work item has already been processed, or is currently
1862 * being processed, its work is considered complete and the work item can be
1863 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001864 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001865 * @warning
1866 * A submitted work item must not be modified until it has been processed
1867 * by the workqueue.
1868 *
1869 * @note Can be called by ISRs.
1870 *
1871 * @param work_q Address of workqueue.
1872 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001873 *
1874 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001875 */
1876static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1877 struct k_work *work)
1878{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001879 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001880 k_fifo_put(&work_q->fifo, work);
1881 }
1882}
1883
1884/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001885 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001887 * This routine indicates if work item @a work is pending in a workqueue's
1888 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001890 * @note Can be called by ISRs.
1891 *
1892 * @param work Address of work item.
1893 *
1894 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001895 */
1896static inline int k_work_pending(struct k_work *work)
1897{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001898 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001899}
1900
1901/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001902 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001904 * This routine starts workqueue @a work_q. The workqueue spawns its work
1905 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001907 * @param work_q Address of workqueue.
1908 * @param stack Pointer to work queue thread's stack space.
1909 * @param stack_size Size of the work queue thread's stack (in bytes).
1910 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911 *
1912 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913 */
Allan Stephens904cf972016-10-07 13:59:23 -05001914extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001915 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001918 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001920 * This routine initializes a workqueue delayed work item, prior to
1921 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001923 * @param work Address of delayed work item.
1924 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
1926 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001928extern void k_delayed_work_init(struct k_delayed_work *work,
1929 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930
1931/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001932 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001934 * This routine schedules work item @a work to be processed by workqueue
1935 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001936 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001937 * Only when the countdown completes is the work item actually submitted to
1938 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001940 * Submitting a previously submitted delayed work item that is still
1941 * counting down cancels the existing submission and restarts the countdown
1942 * using the new delay. If the work item is currently pending on the
1943 * workqueue's queue because the countdown has completed it is too late to
1944 * resubmit the item, and resubmission fails without impacting the work item.
1945 * If the work item has already been processed, or is currently being processed,
1946 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001947 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001948 * @warning
1949 * A delayed work item must not be modified until it has been processed
1950 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001952 * @note Can be called by ISRs.
1953 *
1954 * @param work_q Address of workqueue.
1955 * @param work Address of delayed work item.
1956 * @param delay Delay before submitting the work item (in milliseconds).
1957 *
1958 * @retval 0 Work item countdown started.
1959 * @retval -EINPROGRESS Work item is already pending.
1960 * @retval -EINVAL Work item is being processed or has completed its work.
1961 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001963extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1964 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05001965 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966
1967/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001968 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001970 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07001971 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05001972 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001974 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001976 * @param work Address of delayed work item.
1977 *
David B. Kinder8b986d72017-04-18 15:56:26 -07001978 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001979 * @retval -EINPROGRESS Work item is already pending.
1980 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001982extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985 * @brief Submit a work item to the system workqueue.
1986 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001987 * This routine submits work item @a work to be processed by the system
1988 * workqueue. If the work item is already pending in the workqueue's queue
1989 * as a result of an earlier submission, this routine has no effect on the
1990 * work item. If the work item has already been processed, or is currently
1991 * being processed, its work is considered complete and the work item can be
1992 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001993 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001994 * @warning
1995 * Work items submitted to the system workqueue should avoid using handlers
1996 * that block or yield since this may prevent the system workqueue from
1997 * processing other work items in a timely manner.
1998 *
1999 * @note Can be called by ISRs.
2000 *
2001 * @param work Address of work item.
2002 *
2003 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004 */
2005static inline void k_work_submit(struct k_work *work)
2006{
2007 k_work_submit_to_queue(&k_sys_work_q, work);
2008}
2009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002011 * @brief Submit a delayed work item to the system workqueue.
2012 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002013 * This routine schedules work item @a work to be processed by the system
2014 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002015 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002016 * Only when the countdown completes is the work item actually submitted to
2017 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002019 * Submitting a previously submitted delayed work item that is still
2020 * counting down cancels the existing submission and restarts the countdown
2021 * using the new delay. If the work item is currently pending on the
2022 * workqueue's queue because the countdown has completed it is too late to
2023 * resubmit the item, and resubmission fails without impacting the work item.
2024 * If the work item has already been processed, or is currently being processed,
2025 * its work is considered complete and the work item can be resubmitted.
2026 *
2027 * @warning
2028 * Work items submitted to the system workqueue should avoid using handlers
2029 * that block or yield since this may prevent the system workqueue from
2030 * processing other work items in a timely manner.
2031 *
2032 * @note Can be called by ISRs.
2033 *
2034 * @param work Address of delayed work item.
2035 * @param delay Delay before submitting the work item (in milliseconds).
2036 *
2037 * @retval 0 Work item countdown started.
2038 * @retval -EINPROGRESS Work item is already pending.
2039 * @retval -EINVAL Work item is being processed or has completed its work.
2040 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041 */
2042static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002043 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002045 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046}
2047
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002049 * @brief Get time remaining before a delayed work gets scheduled.
2050 *
2051 * This routine computes the (approximate) time remaining before a
2052 * delayed work gets executed. If the delayed work is not waiting to be
2053 * schedules, it returns zero.
2054 *
2055 * @param work Delayed work item.
2056 *
2057 * @return Remaining time (in milliseconds).
2058 */
Kumar Galacc334c72017-04-21 10:55:34 -05002059static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002060{
2061 return _timeout_remaining_get(&work->timeout);
2062}
2063
2064/**
Allan Stephensc98da842016-11-11 15:45:03 -05002065 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 */
2067
Allan Stephensc98da842016-11-11 15:45:03 -05002068/**
2069 * @cond INTERNAL_HIDDEN
2070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071
2072struct k_mutex {
2073 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002074 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002075 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002077
Anas Nashif2f203c22016-12-18 06:57:45 -05002078 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079};
2080
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081#define K_MUTEX_INITIALIZER(obj) \
2082 { \
2083 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2084 .owner = NULL, \
2085 .lock_count = 0, \
2086 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002087 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088 }
2089
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090/**
Allan Stephensc98da842016-11-11 15:45:03 -05002091 * INTERNAL_HIDDEN @endcond
2092 */
2093
2094/**
2095 * @defgroup mutex_apis Mutex APIs
2096 * @ingroup kernel_apis
2097 * @{
2098 */
2099
2100/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002105 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002110 struct k_mutex name \
2111 __in_section(_k_mutex, static, name) = \
2112 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * Upon completion, the mutex is available and does not have an owner.
2120 *
2121 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 *
2123 * @return N/A
2124 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126
2127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * This routine locks @a mutex. If the mutex is locked by another thread,
2131 * the calling thread waits until the mutex becomes available or until
2132 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002134 * A thread is permitted to lock a mutex it has already locked. The operation
2135 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002137 * @param mutex Address of the mutex.
2138 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002139 * or one of the special values K_NO_WAIT and K_FOREVER.
2140 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002141 * @retval 0 Mutex locked.
2142 * @retval -EBUSY Returned without waiting.
2143 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 */
Kumar Galacc334c72017-04-21 10:55:34 -05002145extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146
2147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * This routine unlocks @a mutex. The mutex must already be locked by the
2151 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 *
2153 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002154 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002155 * thread.
2156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002157 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
2159 * @return N/A
2160 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161extern void k_mutex_unlock(struct k_mutex *mutex);
2162
Allan Stephensc98da842016-11-11 15:45:03 -05002163/**
2164 * @} end defgroup mutex_apis
2165 */
2166
2167/**
2168 * @cond INTERNAL_HIDDEN
2169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002170
2171struct k_sem {
2172 _wait_q_t wait_q;
2173 unsigned int count;
2174 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002175 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176
Anas Nashif2f203c22016-12-18 06:57:45 -05002177 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178};
2179
Allan Stephensc98da842016-11-11 15:45:03 -05002180#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2181 { \
2182 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2183 .count = initial_count, \
2184 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002185 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002186 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002187 }
2188
2189/**
2190 * INTERNAL_HIDDEN @endcond
2191 */
2192
2193/**
2194 * @defgroup semaphore_apis Semaphore APIs
2195 * @ingroup kernel_apis
2196 * @{
2197 */
2198
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002200 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002204 * @param sem Address of the semaphore.
2205 * @param initial_count Initial semaphore count.
2206 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002207 *
2208 * @return N/A
2209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2211 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002212
2213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002214 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002216 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2219 *
2220 * @param sem Address of the semaphore.
2221 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002222 * or one of the special values K_NO_WAIT and K_FOREVER.
2223 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002224 * @note When porting code from the nanokernel legacy API to the new API, be
2225 * careful with the return value of this function. The return value is the
2226 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2227 * non-zero means failure, while the nano_sem_take family returns 1 for success
2228 * and 0 for failure.
2229 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002230 * @retval 0 Semaphore taken.
2231 * @retval -EBUSY Returned without waiting.
2232 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002233 */
Kumar Galacc334c72017-04-21 10:55:34 -05002234extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002235
2236/**
2237 * @brief Give a semaphore.
2238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002239 * This routine gives @a sem, unless the semaphore is already at its maximum
2240 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002242 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002244 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002245 *
2246 * @return N/A
2247 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248extern void k_sem_give(struct k_sem *sem);
2249
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002250/**
2251 * @brief Reset a semaphore's count to zero.
2252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002253 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002255 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002256 *
2257 * @return N/A
2258 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002259static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260{
2261 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262}
2263
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002264/**
2265 * @brief Get a semaphore's count.
2266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002267 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002269 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002272 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002273static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274{
2275 return sem->count;
2276}
2277
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002278/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002279 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002281 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002282 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002283 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002285 * @param name Name of the semaphore.
2286 * @param initial_count Initial semaphore count.
2287 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002288 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002290 struct k_sem name \
2291 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292 K_SEM_INITIALIZER(name, initial_count, count_limit)
2293
Allan Stephensc98da842016-11-11 15:45:03 -05002294/**
2295 * @} end defgroup semaphore_apis
2296 */
2297
2298/**
2299 * @defgroup alert_apis Alert APIs
2300 * @ingroup kernel_apis
2301 * @{
2302 */
2303
Allan Stephens5eceb852016-11-16 10:16:30 -05002304/**
2305 * @typedef k_alert_handler_t
2306 * @brief Alert handler function type.
2307 *
2308 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002309 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002310 * and is only invoked if the alert has been initialized with one.
2311 *
2312 * @param alert Address of the alert.
2313 *
2314 * @return 0 if alert has been consumed; non-zero if alert should pend.
2315 */
2316typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002317
2318/**
2319 * @} end defgroup alert_apis
2320 */
2321
2322/**
2323 * @cond INTERNAL_HIDDEN
2324 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002325
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002326#define K_ALERT_DEFAULT NULL
2327#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002329struct k_alert {
2330 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 atomic_t send_count;
2332 struct k_work work_item;
2333 struct k_sem sem;
2334
Anas Nashif2f203c22016-12-18 06:57:45 -05002335 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336};
2337
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002338extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002340#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002342 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002344 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002345 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002346 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347 }
2348
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349/**
Allan Stephensc98da842016-11-11 15:45:03 -05002350 * INTERNAL_HIDDEN @endcond
2351 */
2352
2353/**
2354 * @addtogroup alert_apis
2355 * @{
2356 */
2357
2358/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002361 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002363 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * @param name Name of the alert.
2366 * @param alert_handler Action to take when alert is sent. Specify either
2367 * the address of a function to be invoked by the system workqueue
2368 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2369 * K_ALERT_DEFAULT (which causes the alert to pend).
2370 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002372#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002373 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002374 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002375 K_ALERT_INITIALIZER(name, alert_handler, \
2376 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @param alert Address of the alert.
2384 * @param handler Action to take when alert is sent. Specify either the address
2385 * of a function to be invoked by the system workqueue thread,
2386 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2387 * K_ALERT_DEFAULT (which causes the alert to pend).
2388 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389 *
2390 * @return N/A
2391 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002392extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2393 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394
2395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2401 *
2402 * @param alert Address of the alert.
2403 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 * or one of the special values K_NO_WAIT and K_FOREVER.
2405 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002406 * @retval 0 Alert received.
2407 * @retval -EBUSY Returned without waiting.
2408 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002409 */
Kumar Galacc334c72017-04-21 10:55:34 -05002410extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411
2412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * This routine signals @a alert. The action specified for @a alert will
2416 * be taken, which may trigger the execution of an alert handler function
2417 * and/or cause the alert to pend (assuming the alert has not reached its
2418 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * @note Can be called by ISRs.
2421 *
2422 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 *
2424 * @return N/A
2425 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002426extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427
2428/**
Allan Stephensc98da842016-11-11 15:45:03 -05002429 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430 */
2431
Allan Stephensc98da842016-11-11 15:45:03 -05002432/**
2433 * @cond INTERNAL_HIDDEN
2434 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435
2436struct k_msgq {
2437 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002438 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002439 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440 char *buffer_start;
2441 char *buffer_end;
2442 char *read_ptr;
2443 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002444 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
Anas Nashif2f203c22016-12-18 06:57:45 -05002446 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447};
2448
Peter Mitsis1da807e2016-10-06 11:36:59 -04002449#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 { \
2451 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002452 .max_msgs = q_max_msgs, \
2453 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002455 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 .read_ptr = q_buffer, \
2457 .write_ptr = q_buffer, \
2458 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002459 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460 }
2461
Peter Mitsis1da807e2016-10-06 11:36:59 -04002462/**
Allan Stephensc98da842016-11-11 15:45:03 -05002463 * INTERNAL_HIDDEN @endcond
2464 */
2465
2466/**
2467 * @defgroup msgq_apis Message Queue APIs
2468 * @ingroup kernel_apis
2469 * @{
2470 */
2471
2472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2476 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002477 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2478 * message is similarly aligned to this boundary, @a q_msg_size must also be
2479 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * The message queue can be accessed outside the module where it is defined
2482 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002484 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * @param q_name Name of the message queue.
2487 * @param q_msg_size Message size (in bytes).
2488 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002489 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002490 */
2491#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2492 static char __noinit __aligned(q_align) \
2493 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002494 struct k_msgq q_name \
2495 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002496 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2497 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498
Peter Mitsisd7a37502016-10-13 11:37:40 -04002499/**
2500 * @brief Initialize a message queue.
2501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002502 * This routine initializes a message queue object, prior to its first use.
2503 *
Allan Stephensda827222016-11-09 14:23:58 -06002504 * The message queue's ring buffer must contain space for @a max_msgs messages,
2505 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2506 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2507 * that each message is similarly aligned to this boundary, @a q_msg_size
2508 * must also be a multiple of N.
2509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @param q Address of the message queue.
2511 * @param buffer Pointer to ring buffer that holds queued messages.
2512 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002513 * @param max_msgs Maximum number of messages that can be queued.
2514 *
2515 * @return N/A
2516 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002517extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002518 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002519
2520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002524 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002525 * @note Can be called by ISRs.
2526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @param q Address of the message queue.
2528 * @param data Pointer to the message.
2529 * @param timeout Waiting period to add the message (in milliseconds),
2530 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002531 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002532 * @retval 0 Message sent.
2533 * @retval -ENOMSG Returned without waiting or queue purged.
2534 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002535 */
Kumar Galacc334c72017-04-21 10:55:34 -05002536extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002537
2538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * This routine receives a message from message queue @a q in a "first in,
2542 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002543 *
Allan Stephensc98da842016-11-11 15:45:03 -05002544 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002546 * @param q Address of the message queue.
2547 * @param data Address of area to hold the received message.
2548 * @param timeout Waiting period to receive the message (in milliseconds),
2549 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002550 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002551 * @retval 0 Message received.
2552 * @retval -ENOMSG Returned without waiting.
2553 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002554 */
Kumar Galacc334c72017-04-21 10:55:34 -05002555extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002556
2557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * This routine discards all unreceived messages in a message queue's ring
2561 * buffer. Any threads that are blocked waiting to send a message to the
2562 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002565 *
2566 * @return N/A
2567 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568extern void k_msgq_purge(struct k_msgq *q);
2569
Peter Mitsis67be2492016-10-07 11:44:34 -04002570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * This routine returns the number of unused entries in a message queue's
2574 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * @param q Address of the message queue.
2577 *
2578 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002579 */
Kumar Galacc334c72017-04-21 10:55:34 -05002580static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002581{
2582 return q->max_msgs - q->used_msgs;
2583}
2584
Peter Mitsisd7a37502016-10-13 11:37:40 -04002585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * @param q Address of the message queue.
2591 *
2592 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002593 */
Kumar Galacc334c72017-04-21 10:55:34 -05002594static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595{
2596 return q->used_msgs;
2597}
2598
Allan Stephensc98da842016-11-11 15:45:03 -05002599/**
2600 * @} end defgroup msgq_apis
2601 */
2602
2603/**
2604 * @defgroup mem_pool_apis Memory Pool APIs
2605 * @ingroup kernel_apis
2606 * @{
2607 */
2608
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002610 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002611 void *addr_in_pool;
2612 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002613 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614};
2615
Allan Stephensc98da842016-11-11 15:45:03 -05002616/**
2617 * @} end defgroup mem_pool_apis
2618 */
2619
2620/**
2621 * @defgroup mailbox_apis Mailbox APIs
2622 * @ingroup kernel_apis
2623 * @{
2624 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625
2626struct k_mbox_msg {
2627 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002628 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002630 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002632 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 /** sender's message data buffer */
2634 void *tx_data;
2635 /** internal use only - needed for legacy API support */
2636 void *_rx_data;
2637 /** message data block descriptor */
2638 struct k_mem_block tx_block;
2639 /** source thread id */
2640 k_tid_t rx_source_thread;
2641 /** target thread id */
2642 k_tid_t tx_target_thread;
2643 /** internal use only - thread waiting on send (may be a dummy) */
2644 k_tid_t _syncing_thread;
2645#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2646 /** internal use only - semaphore used during asynchronous send */
2647 struct k_sem *_async_sem;
2648#endif
2649};
2650
Allan Stephensc98da842016-11-11 15:45:03 -05002651/**
2652 * @cond INTERNAL_HIDDEN
2653 */
2654
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655struct k_mbox {
2656 _wait_q_t tx_msg_queue;
2657 _wait_q_t rx_msg_queue;
2658
Anas Nashif2f203c22016-12-18 06:57:45 -05002659 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660};
2661
2662#define K_MBOX_INITIALIZER(obj) \
2663 { \
2664 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2665 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002666 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667 }
2668
Peter Mitsis12092702016-10-14 12:57:23 -04002669/**
Allan Stephensc98da842016-11-11 15:45:03 -05002670 * INTERNAL_HIDDEN @endcond
2671 */
2672
2673/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002677 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002678 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002683 struct k_mbox name \
2684 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 K_MBOX_INITIALIZER(name) \
2686
Peter Mitsis12092702016-10-14 12:57:23 -04002687/**
2688 * @brief Initialize a mailbox.
2689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * This routine initializes a mailbox object, prior to its first use.
2691 *
2692 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002693 *
2694 * @return N/A
2695 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696extern void k_mbox_init(struct k_mbox *mbox);
2697
Peter Mitsis12092702016-10-14 12:57:23 -04002698/**
2699 * @brief Send a mailbox message in a synchronous manner.
2700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * This routine sends a message to @a mbox and waits for a receiver to both
2702 * receive and process it. The message data may be in a buffer, in a memory
2703 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @param mbox Address of the mailbox.
2706 * @param tx_msg Address of the transmit message descriptor.
2707 * @param timeout Waiting period for the message to be received (in
2708 * milliseconds), or one of the special values K_NO_WAIT
2709 * and K_FOREVER. Once the message has been received,
2710 * this routine waits as long as necessary for the message
2711 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002712 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002713 * @retval 0 Message sent.
2714 * @retval -ENOMSG Returned without waiting.
2715 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002716 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002717extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002718 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002719
Peter Mitsis12092702016-10-14 12:57:23 -04002720/**
2721 * @brief Send a mailbox message in an asynchronous manner.
2722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002723 * This routine sends a message to @a mbox without waiting for a receiver
2724 * to process it. The message data may be in a buffer, in a memory pool block,
2725 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2726 * will be given when the message has been both received and completely
2727 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * @param mbox Address of the mailbox.
2730 * @param tx_msg Address of the transmit message descriptor.
2731 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002732 *
2733 * @return N/A
2734 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002735extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 struct k_sem *sem);
2737
Peter Mitsis12092702016-10-14 12:57:23 -04002738/**
2739 * @brief Receive a mailbox message.
2740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * This routine receives a message from @a mbox, then optionally retrieves
2742 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002744 * @param mbox Address of the mailbox.
2745 * @param rx_msg Address of the receive message descriptor.
2746 * @param buffer Address of the buffer to receive data, or NULL to defer data
2747 * retrieval and message disposal until later.
2748 * @param timeout Waiting period for a message to be received (in
2749 * milliseconds), or one of the special values K_NO_WAIT
2750 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002751 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002752 * @retval 0 Message received.
2753 * @retval -ENOMSG Returned without waiting.
2754 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002755 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002756extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002757 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002758
2759/**
2760 * @brief Retrieve mailbox message data into a buffer.
2761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * This routine completes the processing of a received message by retrieving
2763 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002764 *
2765 * Alternatively, this routine can be used to dispose of a received message
2766 * without retrieving its data.
2767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @param rx_msg Address of the receive message descriptor.
2769 * @param buffer Address of the buffer to receive data, or NULL to discard
2770 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002771 *
2772 * @return N/A
2773 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002774extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002775
2776/**
2777 * @brief Retrieve mailbox message data into a memory pool block.
2778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * This routine completes the processing of a received message by retrieving
2780 * its data into a memory pool block, then disposing of the message.
2781 * The memory pool block that results from successful retrieval must be
2782 * returned to the pool once the data has been processed, even in cases
2783 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002784 *
2785 * Alternatively, this routine can be used to dispose of a received message
2786 * without retrieving its data. In this case there is no need to return a
2787 * memory pool block to the pool.
2788 *
2789 * This routine allocates a new memory pool block for the data only if the
2790 * data is not already in one. If a new block cannot be allocated, the routine
2791 * returns a failure code and the received message is left unchanged. This
2792 * permits the caller to reattempt data retrieval at a later time or to dispose
2793 * of the received message without retrieving its data.
2794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * @param rx_msg Address of a receive message descriptor.
2796 * @param pool Address of memory pool, or NULL to discard data.
2797 * @param block Address of the area to hold memory pool block info.
2798 * @param timeout Waiting period to wait for a memory pool block (in
2799 * milliseconds), or one of the special values K_NO_WAIT
2800 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002801 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002802 * @retval 0 Data retrieved.
2803 * @retval -ENOMEM Returned without waiting.
2804 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002805 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002806extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002807 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002808 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809
Allan Stephensc98da842016-11-11 15:45:03 -05002810/**
2811 * @} end defgroup mailbox_apis
2812 */
2813
2814/**
2815 * @cond INTERNAL_HIDDEN
2816 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817
2818struct k_pipe {
2819 unsigned char *buffer; /* Pipe buffer: may be NULL */
2820 size_t size; /* Buffer size */
2821 size_t bytes_used; /* # bytes used in buffer */
2822 size_t read_index; /* Where in buffer to read from */
2823 size_t write_index; /* Where in buffer to write */
2824
2825 struct {
2826 _wait_q_t readers; /* Reader wait queue */
2827 _wait_q_t writers; /* Writer wait queue */
2828 } wait_q;
2829
Anas Nashif2f203c22016-12-18 06:57:45 -05002830 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831};
2832
Peter Mitsise5d9c582016-10-14 14:44:57 -04002833#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 { \
2835 .buffer = pipe_buffer, \
2836 .size = pipe_buffer_size, \
2837 .bytes_used = 0, \
2838 .read_index = 0, \
2839 .write_index = 0, \
2840 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2841 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002842 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843 }
2844
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002845/**
Allan Stephensc98da842016-11-11 15:45:03 -05002846 * INTERNAL_HIDDEN @endcond
2847 */
2848
2849/**
2850 * @defgroup pipe_apis Pipe APIs
2851 * @ingroup kernel_apis
2852 * @{
2853 */
2854
2855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002860 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @param name Name of the pipe.
2863 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2864 * or zero if no ring buffer is used.
2865 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002867#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2868 static unsigned char __noinit __aligned(pipe_align) \
2869 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002870 struct k_pipe name \
2871 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002872 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002873
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * This routine initializes a pipe object, prior to its first use.
2878 *
2879 * @param pipe Address of the pipe.
2880 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2881 * is used.
2882 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2883 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002884 *
2885 * @return N/A
2886 */
2887extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2888 size_t size);
2889
2890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @param pipe Address of the pipe.
2896 * @param data Address of data to write.
2897 * @param bytes_to_write Size of data (in bytes).
2898 * @param bytes_written Address of area to hold the number of bytes written.
2899 * @param min_xfer Minimum number of bytes to write.
2900 * @param timeout Waiting period to wait for the data to be written (in
2901 * milliseconds), or one of the special values K_NO_WAIT
2902 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002903 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002904 * @retval 0 At least @a min_xfer bytes of data were written.
2905 * @retval -EIO Returned without waiting; zero data bytes were written.
2906 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002909extern int k_pipe_put(struct k_pipe *pipe, void *data,
2910 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002911 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912
2913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @param pipe Address of the pipe.
2919 * @param data Address to place the data read from pipe.
2920 * @param bytes_to_read Maximum number of data bytes to read.
2921 * @param bytes_read Address of area to hold the number of bytes read.
2922 * @param min_xfer Minimum number of data bytes to read.
2923 * @param timeout Waiting period to wait for the data to be read (in
2924 * milliseconds), or one of the special values K_NO_WAIT
2925 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002927 * @retval 0 At least @a min_xfer bytes of data were read.
2928 * @retval -EIO Returned without waiting; zero data bytes were read.
2929 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002932extern int k_pipe_get(struct k_pipe *pipe, void *data,
2933 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002934 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935
2936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * This routine writes the data contained in a memory block to @a pipe.
2940 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 * @param block Memory block containing data to send
2945 * @param size Number of data bytes in memory block to send
2946 * @param sem Semaphore to signal upon completion (else NULL)
2947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 */
2950extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2951 size_t size, struct k_sem *sem);
2952
2953/**
Allan Stephensc98da842016-11-11 15:45:03 -05002954 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955 */
2956
Allan Stephensc98da842016-11-11 15:45:03 -05002957/**
2958 * @cond INTERNAL_HIDDEN
2959 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002961struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002963 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002964 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965 char *buffer;
2966 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05002967 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002968
Anas Nashif2f203c22016-12-18 06:57:45 -05002969 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970};
2971
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002972#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2973 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002974 { \
2975 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002976 .num_blocks = slab_num_blocks, \
2977 .block_size = slab_block_size, \
2978 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979 .free_list = NULL, \
2980 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002981 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 }
2983
Peter Mitsis578f9112016-10-07 13:50:31 -04002984/**
Allan Stephensc98da842016-11-11 15:45:03 -05002985 * INTERNAL_HIDDEN @endcond
2986 */
2987
2988/**
2989 * @defgroup mem_slab_apis Memory Slab APIs
2990 * @ingroup kernel_apis
2991 * @{
2992 */
2993
2994/**
Allan Stephensda827222016-11-09 14:23:58 -06002995 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002996 *
Allan Stephensda827222016-11-09 14:23:58 -06002997 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002999 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3000 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003001 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003002 *
Allan Stephensda827222016-11-09 14:23:58 -06003003 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003005 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003006 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param name Name of the memory slab.
3009 * @param slab_block_size Size of each memory block (in bytes).
3010 * @param slab_num_blocks Number memory blocks.
3011 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003012 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003013#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3014 char __noinit __aligned(slab_align) \
3015 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3016 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003017 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003018 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
3019 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003021/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003022 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003025 *
Allan Stephensda827222016-11-09 14:23:58 -06003026 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3027 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3028 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3029 * To ensure that each memory block is similarly aligned to this boundary,
3030 * @a slab_block_size must also be a multiple of N.
3031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @param slab Address of the memory slab.
3033 * @param buffer Pointer to buffer used for the memory blocks.
3034 * @param block_size Size of each memory block (in bytes).
3035 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003036 *
3037 * @return N/A
3038 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003039extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003040 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003041
3042/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003043 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * @param slab Address of the memory slab.
3048 * @param mem Pointer to block address area.
3049 * @param timeout Maximum time to wait for operation to complete
3050 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3051 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003052 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003053 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003055 * @retval -ENOMEM Returned without waiting.
3056 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003057 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003058extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003059 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003060
3061/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003064 * This routine releases a previously allocated memory block back to its
3065 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003067 * @param slab Address of the memory slab.
3068 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003069 *
3070 * @return N/A
3071 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003072extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003074/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003077 * This routine gets the number of memory blocks that are currently
3078 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003080 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003082 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003083 */
Kumar Galacc334c72017-04-21 10:55:34 -05003084static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003086 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087}
3088
Peter Mitsisc001aa82016-10-13 13:53:37 -04003089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * This routine gets the number of memory blocks that are currently
3093 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003098 */
Kumar Galacc334c72017-04-21 10:55:34 -05003099static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003100{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003101 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003102}
3103
Allan Stephensc98da842016-11-11 15:45:03 -05003104/**
3105 * @} end defgroup mem_slab_apis
3106 */
3107
3108/**
3109 * @cond INTERNAL_HIDDEN
3110 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003112/*
3113 * Memory pool requires a buffer and two arrays of structures for the
3114 * memory block accounting:
3115 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
3116 * status of four blocks of memory.
3117 */
3118struct k_mem_pool_quad_block {
3119 char *mem_blocks; /* pointer to the first of four memory blocks */
Kumar Galacc334c72017-04-21 10:55:34 -05003120 u32_t mem_status; /* four bits. If bit is set, memory block is
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003121 allocated */
3122};
3123/*
3124 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
3125 * blocks of one size. Block sizes go from maximal to minimal. Next memory
3126 * block size is 4 times less than the previous one and thus requires 4 times
3127 * bigger array of k_mem_pool_quad_block structures to keep track of the
3128 * memory blocks.
3129 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003131/*
3132 * The array of k_mem_pool_block_set keeps the information of each array of
3133 * k_mem_pool_quad_block structures
3134 */
3135struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04003136 size_t block_size; /* memory block size */
Kumar Galacc334c72017-04-21 10:55:34 -05003137 u32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003138 struct k_mem_pool_quad_block *quad_block;
3139 int count;
3140};
3141
3142/* Memory pool descriptor */
3143struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04003144 size_t max_block_size;
3145 size_t min_block_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003146 u32_t nr_of_maxblocks;
3147 u32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003148 struct k_mem_pool_block_set *block_set;
3149 char *bufblock;
3150 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05003151 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152};
3153
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003154#ifdef CONFIG_ARM
3155#define _SECTION_TYPE_SIGN "%"
3156#else
3157#define _SECTION_TYPE_SIGN "@"
3158#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003160/*
3161 * Static memory pool initialization
3162 */
Allan Stephensc98da842016-11-11 15:45:03 -05003163
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003164/*
3165 * Use .altmacro to be able to recalculate values and pass them as string
David B. Kinder8b986d72017-04-18 15:56:26 -07003166 * arguments when calling assembler macros recursively
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003167 */
3168__asm__(".altmacro\n\t");
3169
3170/*
3171 * Recursively calls a macro
David B. Kinder8b986d72017-04-18 15:56:26 -07003172 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003173 * __memory_pool_max_block_size - maximal size of the memory block
3174 * __memory_pool_min_block_size - minimal size of the memory block
3175 * Notes:
3176 * Global symbols are used due the fact that assembler macro allows only
3177 * one argument be passed with the % conversion
3178 * Some assemblers do not get division operation ("/"). To avoid it >> 2
3179 * is used instead of / 4.
3180 * n_max argument needs to go first in the invoked macro, as some
3181 * assemblers concatenate \name and %(\n_max * 4) arguments
3182 * if \name goes first
3183 */
3184__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
3185 ".ifge __memory_pool_max_block_size >> 2 -"
3186 " __memory_pool_min_block_size\n\t\t"
3187 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
3188 "\\macro_name %(\\n_max * 4) \\name\n\t"
3189 ".endif\n\t"
3190 ".endm\n");
3191
3192/*
3193 * Build quad blocks
3194 * Macro allocates space in memory for the array of k_mem_pool_quad_block
3195 * structures and recursively calls itself for the next array, 4 times
3196 * larger.
David B. Kinder8b986d72017-04-18 15:56:26 -07003197 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003198 * __memory_pool_max_block_size - maximal size of the memory block
3199 * __memory_pool_min_block_size - minimal size of the memory block
3200 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
3201 */
3202__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003203 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003204 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
3205 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
3206 ".if \\n_max % 4\n\t\t"
3207 ".skip __memory_pool_quad_block_size\n\t"
3208 ".endif\n\t"
3209 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
3210 ".endm\n");
3211
3212/*
3213 * Build block sets and initialize them
3214 * Macro initializes the k_mem_pool_block_set structure and
3215 * recursively calls itself for the next one.
David B. Kinder8b986d72017-04-18 15:56:26 -07003216 * The following global symbols need to be initialized:
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003217 * __memory_pool_max_block_size - maximal size of the memory block
3218 * __memory_pool_min_block_size - minimal size of the memory block
3219 * __memory_pool_block_set_count, the number of the elements in the
3220 * block set array must be set to 0. Macro calculates it's real
3221 * value.
3222 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
3223 * structures, _build_quad_blocks must be called prior it.
3224 */
3225__asm__(".macro _build_block_set n_max, name\n\t"
3226 ".int __memory_pool_max_block_size\n\t" /* block_size */
3227 ".if \\n_max % 4\n\t\t"
3228 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
3229 ".else\n\t\t"
3230 ".int \\n_max >> 2\n\t"
3231 ".endif\n\t"
3232 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
3233 ".int 0\n\t" /* count */
3234 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
3235 "__do_recurse _build_block_set \\name \\n_max\n\t"
3236 ".endm\n");
3237
3238/*
3239 * Build a memory pool structure and initialize it
3240 * Macro uses __memory_pool_block_set_count global symbol,
3241 * block set addresses and buffer address, it may be called only after
3242 * _build_block_set
3243 */
3244__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05003245 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003246 _SECTION_TYPE_SIGN "progbits\n\t"
3247 ".globl \\name\n\t"
3248 "\\name:\n\t"
3249 ".int \\max_size\n\t" /* max_block_size */
3250 ".int \\min_size\n\t" /* min_block_size */
3251 ".int \\n_max\n\t" /* nr_of_maxblocks */
3252 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
3253 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
3254 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
3255 ".int 0\n\t" /* wait_q->head */
3256 ".int 0\n\t" /* wait_q->next */
3257 ".popsection\n\t"
3258 ".endm\n");
3259
3260#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
3261 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3262 _SECTION_TYPE_SIGN "progbits\n\t"); \
3263 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
3264 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3265 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
3266 STRINGIFY(name) "\n\t"); \
3267 __asm__(".popsection\n\t")
3268
3269#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
3270 __asm__("__memory_pool_block_set_count = 0\n\t"); \
3271 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
3272 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
3273 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04003274 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003275 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
3276 __asm__("_build_block_set " STRINGIFY(n_max) " " \
3277 STRINGIFY(name) "\n\t"); \
3278 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
3279 __asm__(".int __memory_pool_block_set_count\n\t"); \
3280 __asm__(".popsection\n\t"); \
Kumar Galacc334c72017-04-21 10:55:34 -05003281 extern u32_t _mem_pool_block_set_count_##name; \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003282 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
3283
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003284#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
3285 char __noinit __aligned(align) \
3286 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003287
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003288/*
3289 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
3290 * to __memory_pool_quad_block_size absolute symbol.
3291 * This function does not get called, but compiler calculates the value and
3292 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
3293 */
3294static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
3295{
3296 __asm__(".globl __memory_pool_quad_block_size\n\t"
Mazen NEIFERdc391f52017-01-22 17:20:22 +01003297#if defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003298 "__memory_pool_quad_block_size = %0\n\t"
3299#else
3300 "__memory_pool_quad_block_size = %c0\n\t"
3301#endif
3302 :
3303 : "n"(sizeof(struct k_mem_pool_quad_block)));
3304}
3305
3306/**
Allan Stephensc98da842016-11-11 15:45:03 -05003307 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003308 */
3309
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003310/**
Allan Stephensc98da842016-11-11 15:45:03 -05003311 * @addtogroup mem_pool_apis
3312 * @{
3313 */
3314
3315/**
3316 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3319 * long. The memory pool allows blocks to be repeatedly partitioned into
3320 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
3321 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06003322 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003323 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003324 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003325 * If the pool is to be accessed outside the module where it is defined, it
3326 * can be declared via
3327 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003328 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @param name Name of the memory pool.
3331 * @param min_size Size of the smallest blocks in the pool (in bytes).
3332 * @param max_size Size of the largest blocks in the pool (in bytes).
3333 * @param n_max Number of maximum sized blocks in the pool.
3334 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003335 */
3336#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003337 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
3338 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003339 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003340 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
3341 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
3342 extern struct k_mem_pool name
3343
Peter Mitsis937042c2016-10-13 13:18:26 -04003344/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @param pool Address of the memory pool.
3350 * @param block Pointer to block descriptor for the allocated memory.
3351 * @param size Amount of memory to allocate (in bytes).
3352 * @param timeout Maximum time to wait for operation to complete
3353 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3354 * or K_FOREVER to wait as long as necessary.
3355 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003356 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003358 * @retval -ENOMEM Returned without waiting.
3359 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003360 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003361extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003362 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003363
3364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * This routine releases a previously allocated memory block back to its
3368 * memory pool.
3369 *
3370 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003371 *
3372 * @return N/A
3373 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003375
3376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * This routine instructs a memory pool to concatenate unused memory blocks
3380 * into larger blocks wherever possible. Manually defragmenting the memory
3381 * pool may speed up future allocations of memory blocks by eliminating the
3382 * need for the memory pool to perform an automatic partial defragmentation.
3383 *
3384 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003385 *
3386 * @return N/A
3387 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003388extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04003389
3390/**
Allan Stephensc98da842016-11-11 15:45:03 -05003391 * @} end addtogroup mem_pool_apis
3392 */
3393
3394/**
3395 * @defgroup heap_apis Heap Memory Pool APIs
3396 * @ingroup kernel_apis
3397 * @{
3398 */
3399
3400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003404 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003409 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003410extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003411
3412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003414 *
3415 * This routine provides traditional free() semantics. The memory being
3416 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003417 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003418 * If @a ptr is NULL, no operation is performed.
3419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003421 *
3422 * @return N/A
3423 */
3424extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425
Allan Stephensc98da842016-11-11 15:45:03 -05003426/**
3427 * @} end defgroup heap_apis
3428 */
3429
Benjamin Walshacc68c12017-01-29 18:57:45 -05003430/* polling API - PRIVATE */
3431
Benjamin Walshb0179862017-02-02 16:39:57 -05003432#ifdef CONFIG_POLL
3433#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3434#else
3435#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3436#endif
3437
Benjamin Walshacc68c12017-01-29 18:57:45 -05003438/* private - implementation data created as needed, per-type */
3439struct _poller {
3440 struct k_thread *thread;
3441};
3442
3443/* private - types bit positions */
3444enum _poll_types_bits {
3445 /* can be used to ignore an event */
3446 _POLL_TYPE_IGNORE,
3447
3448 /* to be signaled by k_poll_signal() */
3449 _POLL_TYPE_SIGNAL,
3450
3451 /* semaphore availability */
3452 _POLL_TYPE_SEM_AVAILABLE,
3453
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003454 /* queue/fifo/lifo data availability */
3455 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003456
3457 _POLL_NUM_TYPES
3458};
3459
3460#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3461
3462/* private - states bit positions */
3463enum _poll_states_bits {
3464 /* default state when creating event */
3465 _POLL_STATE_NOT_READY,
3466
3467 /* there was another poller already on the object */
3468 _POLL_STATE_EADDRINUSE,
3469
3470 /* signaled by k_poll_signal() */
3471 _POLL_STATE_SIGNALED,
3472
3473 /* semaphore is available */
3474 _POLL_STATE_SEM_AVAILABLE,
3475
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003476 /* data is available to read on queue/fifo/lifo */
3477 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003478
3479 _POLL_NUM_STATES
3480};
3481
3482#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3483
3484#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003485 (32 - (0 \
3486 + 8 /* tag */ \
3487 + _POLL_NUM_TYPES \
3488 + _POLL_NUM_STATES \
3489 + 1 /* modes */ \
3490 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003491
3492#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3493#error overflow of 32-bit word in struct k_poll_event
3494#endif
3495
3496/* end of polling API - PRIVATE */
3497
3498
3499/**
3500 * @defgroup poll_apis Async polling APIs
3501 * @ingroup kernel_apis
3502 * @{
3503 */
3504
3505/* Public polling API */
3506
3507/* public - values for k_poll_event.type bitfield */
3508#define K_POLL_TYPE_IGNORE 0
3509#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3510#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003511#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3512#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003513
3514/* public - polling modes */
3515enum k_poll_modes {
3516 /* polling thread does not take ownership of objects when available */
3517 K_POLL_MODE_NOTIFY_ONLY = 0,
3518
3519 K_POLL_NUM_MODES
3520};
3521
3522/* public - values for k_poll_event.state bitfield */
3523#define K_POLL_STATE_NOT_READY 0
3524#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3525#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3526#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003527#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3528#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003529
3530/* public - poll signal object */
3531struct k_poll_signal {
3532 /* PRIVATE - DO NOT TOUCH */
3533 struct k_poll_event *poll_event;
3534
3535 /*
3536 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3537 * user resets it to 0.
3538 */
3539 unsigned int signaled;
3540
3541 /* custom result value passed to k_poll_signal() if needed */
3542 int result;
3543};
3544
3545#define K_POLL_SIGNAL_INITIALIZER() \
3546 { \
3547 .poll_event = NULL, \
3548 .signaled = 0, \
3549 .result = 0, \
3550 }
3551
3552struct k_poll_event {
3553 /* PRIVATE - DO NOT TOUCH */
3554 struct _poller *poller;
3555
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003556 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003557 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003558
Benjamin Walshacc68c12017-01-29 18:57:45 -05003559 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003560 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003561
3562 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003563 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003564
3565 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003566 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003567
3568 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003569 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003570
3571 /* per-type data */
3572 union {
3573 void *obj;
3574 struct k_poll_signal *signal;
3575 struct k_sem *sem;
3576 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003577 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003578 };
3579};
3580
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003581#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003582 { \
3583 .poller = NULL, \
3584 .type = event_type, \
3585 .state = K_POLL_STATE_NOT_READY, \
3586 .mode = event_mode, \
3587 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003588 { .obj = event_obj }, \
3589 }
3590
3591#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3592 event_tag) \
3593 { \
3594 .type = event_type, \
3595 .tag = event_tag, \
3596 .state = K_POLL_STATE_NOT_READY, \
3597 .mode = event_mode, \
3598 .unused = 0, \
3599 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003600 }
3601
3602/**
3603 * @brief Initialize one struct k_poll_event instance
3604 *
3605 * After this routine is called on a poll event, the event it ready to be
3606 * placed in an event array to be passed to k_poll().
3607 *
3608 * @param event The event to initialize.
3609 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3610 * values. Only values that apply to the same object being polled
3611 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3612 * event.
3613 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3614 * @param obj Kernel object or poll signal.
3615 *
3616 * @return N/A
3617 */
3618
Kumar Galacc334c72017-04-21 10:55:34 -05003619extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003620 int mode, void *obj);
3621
3622/**
3623 * @brief Wait for one or many of multiple poll events to occur
3624 *
3625 * This routine allows a thread to wait concurrently for one or many of
3626 * multiple poll events to have occurred. Such events can be a kernel object
3627 * being available, like a semaphore, or a poll signal event.
3628 *
3629 * When an event notifies that a kernel object is available, the kernel object
3630 * is not "given" to the thread calling k_poll(): it merely signals the fact
3631 * that the object was available when the k_poll() call was in effect. Also,
3632 * all threads trying to acquire an object the regular way, i.e. by pending on
3633 * the object, have precedence over the thread polling on the object. This
3634 * means that the polling thread will never get the poll event on an object
3635 * until the object becomes available and its pend queue is empty. For this
3636 * reason, the k_poll() call is more effective when the objects being polled
3637 * only have one thread, the polling thread, trying to acquire them.
3638 *
3639 * Only one thread can be polling for a particular object at a given time. If
3640 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3641 * and returns as soon as it has finished handling the other events. This means
3642 * that k_poll() can return -EADDRINUSE and have the state value of some events
3643 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3644 * parameter is ignored.
3645 *
3646 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3647 * events that were passed to k_poll() and check the state field for the values
3648 * that were expected and take the associated actions.
3649 *
3650 * Before being reused for another call to k_poll(), the user has to reset the
3651 * state field to K_POLL_STATE_NOT_READY.
3652 *
3653 * @param events An array of pointers to events to be polled for.
3654 * @param num_events The number of events in the array.
3655 * @param timeout Waiting period for an event to be ready (in milliseconds),
3656 * or one of the special values K_NO_WAIT and K_FOREVER.
3657 *
3658 * @retval 0 One or more events are ready.
3659 * @retval -EADDRINUSE One or more objects already had a poller.
3660 * @retval -EAGAIN Waiting period timed out.
3661 */
3662
3663extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003664 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003665
3666/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003667 * @brief Initialize a poll signal object.
3668 *
3669 * Ready a poll signal object to be signaled via k_poll_signal().
3670 *
3671 * @param signal A poll signal.
3672 *
3673 * @return N/A
3674 */
3675
3676extern void k_poll_signal_init(struct k_poll_signal *signal);
3677
3678/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003679 * @brief Signal a poll signal object.
3680 *
3681 * This routine makes ready a poll signal, which is basically a poll event of
3682 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3683 * made ready to run. A @a result value can be specified.
3684 *
3685 * The poll signal contains a 'signaled' field that, when set by
3686 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3687 * be reset by the user before being passed again to k_poll() or k_poll() will
3688 * consider it being signaled, and will return immediately.
3689 *
3690 * @param signal A poll signal.
3691 * @param result The value to store in the result field of the signal.
3692 *
3693 * @retval 0 The signal was delivered successfully.
3694 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3695 */
3696
3697extern int k_poll_signal(struct k_poll_signal *signal, int result);
3698
3699/* private internal function */
3700extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003701 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003702
3703/**
3704 * @} end defgroup poll_apis
3705 */
3706
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003707/**
3708 * @brief Make the CPU idle.
3709 *
3710 * This function makes the CPU idle until an event wakes it up.
3711 *
3712 * In a regular system, the idle thread should be the only thread responsible
3713 * for making the CPU idle and triggering any type of power management.
3714 * However, in some more constrained systems, such as a single-threaded system,
3715 * the only thread would be responsible for this if needed.
3716 *
3717 * @return N/A
3718 */
3719extern void k_cpu_idle(void);
3720
3721/**
3722 * @brief Make the CPU idle in an atomic fashion.
3723 *
3724 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3725 * must be done atomically before making the CPU idle.
3726 *
3727 * @param key Interrupt locking key obtained from irq_lock().
3728 *
3729 * @return N/A
3730 */
3731extern void k_cpu_atomic_idle(unsigned int key);
3732
Kumar Galacc334c72017-04-21 10:55:34 -05003733extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003734
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735#include <arch/cpu.h>
3736
Andrew Boiecdb94d62017-04-18 15:22:05 -07003737#ifdef _ARCH_EXCEPT
3738/* This archtecture has direct support for triggering a CPU exception */
3739#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3740#else
3741
3742#include <misc/printk.h>
3743
3744/* NOTE: This is the implementation for arches that do not implement
3745 * _ARCH_EXCEPT() to generate a real CPU exception.
3746 *
3747 * We won't have a real exception frame to determine the PC value when
3748 * the oops occurred, so print file and line number before we jump into
3749 * the fatal error handler.
3750 */
3751#define _k_except_reason(reason) do { \
3752 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3753 _NanoFatalErrorHandler(reason, &_default_esf); \
3754 CODE_UNREACHABLE; \
3755 } while (0)
3756
3757#endif /* _ARCH__EXCEPT */
3758
3759/**
3760 * @brief Fatally terminate a thread
3761 *
3762 * This should be called when a thread has encountered an unrecoverable
3763 * runtime condition and needs to terminate. What this ultimately
3764 * means is determined by the _fatal_error_handler() implementation, which
3765 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3766 *
3767 * If this is called from ISR context, the default system fatal error handler
3768 * will treat it as an unrecoverable system error, just like k_panic().
3769 */
3770#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3771
3772/**
3773 * @brief Fatally terminate the system
3774 *
3775 * This should be called when the Zephyr kernel has encountered an
3776 * unrecoverable runtime condition and needs to terminate. What this ultimately
3777 * means is determined by the _fatal_error_handler() implementation, which
3778 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3779 */
3780#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3781
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003782/*
3783 * private APIs that are utilized by one or more public APIs
3784 */
3785
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003786#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003788#else
3789#define _init_static_threads() do { } while ((0))
3790#endif
3791
3792extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003793extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794
3795#ifdef __cplusplus
3796}
3797#endif
3798
Andrew Boiee004dec2016-11-07 09:01:19 -08003799#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3800/*
3801 * Define new and delete operators.
3802 * At this moment, the operators do nothing since objects are supposed
3803 * to be statically allocated.
3804 */
3805inline void operator delete(void *ptr)
3806{
3807 (void)ptr;
3808}
3809
3810inline void operator delete[](void *ptr)
3811{
3812 (void)ptr;
3813}
3814
3815inline void *operator new(size_t size)
3816{
3817 (void)size;
3818 return NULL;
3819}
3820
3821inline void *operator new[](size_t size)
3822{
3823 (void)size;
3824 return NULL;
3825}
3826
3827/* Placement versions of operator new and delete */
3828inline void operator delete(void *ptr1, void *ptr2)
3829{
3830 (void)ptr1;
3831 (void)ptr2;
3832}
3833
3834inline void operator delete[](void *ptr1, void *ptr2)
3835{
3836 (void)ptr1;
3837 (void)ptr2;
3838}
3839
3840inline void *operator new(size_t size, void *ptr)
3841{
3842 (void)size;
3843 return ptr;
3844}
3845
3846inline void *operator new[](size_t size, void *ptr)
3847{
3848 (void)size;
3849 return ptr;
3850}
3851
3852#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3853
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003854#endif /* !_ASMLANGUAGE */
3855
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856#endif /* _kernel__h_ */