blob: 1fbe0c06e6dd9e4c74d741e35a9f5ff200a40c46 [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 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700358 * Kernel data structures for bookkeeping and context storage for this thread
359 * will be placed at the beginning of the thread's stack memory region and may
360 * become corrupted if too much of the stack is used. This function has been
361 * deprecated in favor of k_thread_create() to give the user more control on
362 * where these data structures reside.
363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500364 * Thread options are architecture-specific, and can include K_ESSENTIAL,
365 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
366 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400367 *
368 * @param stack Pointer to the stack space.
369 * @param stack_size Stack size in bytes.
370 * @param entry Thread entry function.
371 * @param p1 1st entry point parameter.
372 * @param p2 2nd entry point parameter.
373 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500374 * @param prio Thread priority.
375 * @param options Thread options.
376 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500378 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379 */
Andrew Boied26cf2d2017-03-30 13:07:02 -0700380extern __deprecated k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500381 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400382 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500383 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700386 * @brief Create a thread.
387 *
388 * This routine initializes a thread, then schedules it for execution.
389 *
390 * The new thread may be scheduled for immediate execution or a delayed start.
391 * If the newly spawned thread does not have a delayed start the kernel
392 * scheduler may preempt the current thread to allow the new thread to
393 * execute.
394 *
395 * Thread options are architecture-specific, and can include K_ESSENTIAL,
396 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
397 * them using "|" (the logical OR operator).
398 *
399 * Historically, users often would use the beginning of the stack memory region
400 * to store the struct k_thread data, although corruption will occur if the
401 * stack overflows this region and stack protection features may not detect this
402 * situation.
403 *
404 * @param new_thread Pointer to uninitialized struct k_thread
405 * @param stack Pointer to the stack space.
406 * @param stack_size Stack size in bytes.
407 * @param entry Thread entry function.
408 * @param p1 1st entry point parameter.
409 * @param p2 2nd entry point parameter.
410 * @param p3 3rd entry point parameter.
411 * @param prio Thread priority.
412 * @param options Thread options.
413 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
414 *
415 * @return ID of new thread.
416 */
417extern k_tid_t k_thread_create(struct k_thread *new_thread, char *stack,
418 size_t stack_size,
419 void (*entry)(void *, void *, void*),
420 void *p1, void *p2, void *p3,
421 int prio, u32_t options, s32_t delay);
422
423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400425 *
Allan Stephensc98da842016-11-11 15:45:03 -0500426 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500427 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500429 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 *
431 * @return N/A
432 */
Kumar Galacc334c72017-04-21 10:55:34 -0500433extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400434
435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500439 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400440 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441 * @return N/A
442 */
Kumar Galacc334c72017-04-21 10:55:34 -0500443extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444
445/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500448 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400449 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500450 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 *
452 * @return N/A
453 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400454extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400455
456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500461 * If @a thread is not currently sleeping, the routine has no effect.
462 *
463 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400464 *
465 * @return N/A
466 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400467extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400468
469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500470 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500472 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475
476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500477 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * This routine prevents @a thread from executing if it has not yet started
480 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500482 * @param thread ID of thread to cancel.
483 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700484 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500485 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400486 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400487extern int k_thread_cancel(k_tid_t thread);
488
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400489/**
Allan Stephensc98da842016-11-11 15:45:03 -0500490 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500492 * This routine permanently stops execution of @a thread. The thread is taken
493 * off all kernel queues it is part of (i.e. the ready queue, the timeout
494 * queue, or a kernel object wait queue). However, any kernel resources the
495 * thread might currently own (such as mutexes or memory blocks) are not
496 * released. It is the responsibility of the caller of this routine to ensure
497 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500499 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400500 *
501 * @return N/A
502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503extern void k_thread_abort(k_tid_t thread);
504
Allan Stephensc98da842016-11-11 15:45:03 -0500505/**
506 * @cond INTERNAL_HIDDEN
507 */
508
Benjamin Walshd211a522016-12-06 11:44:01 -0500509/* timeout has timed out and is not on _timeout_q anymore */
510#define _EXPIRED (-2)
511
512/* timeout is not in use */
513#define _INACTIVE (-1)
514
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400515struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700516 struct k_thread *init_thread;
517 char *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400518 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500519 void (*init_entry)(void *, void *, void *);
520 void *init_p1;
521 void *init_p2;
522 void *init_p3;
523 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500524 u32_t init_options;
525 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500526 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500527 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400528};
529
Andrew Boied26cf2d2017-03-30 13:07:02 -0700530#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400531 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500532 prio, options, delay, abort, groups) \
533 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700534 .init_thread = (thread), \
535 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500536 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400537 .init_entry = (void (*)(void *, void *, void *))entry, \
538 .init_p1 = (void *)p1, \
539 .init_p2 = (void *)p2, \
540 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500541 .init_prio = (prio), \
542 .init_options = (options), \
543 .init_delay = (delay), \
544 .init_abort = (abort), \
545 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400546 }
547
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400548/**
Allan Stephensc98da842016-11-11 15:45:03 -0500549 * INTERNAL_HIDDEN @endcond
550 */
551
552/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500553 * @brief Statically define and initialize a thread.
554 *
555 * The thread may be scheduled for immediate execution or a delayed start.
556 *
557 * Thread options are architecture-specific, and can include K_ESSENTIAL,
558 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
559 * them using "|" (the logical OR operator).
560 *
561 * The ID of the thread can be accessed using:
562 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500563 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500564 *
565 * @param name Name of the thread.
566 * @param stack_size Stack size in bytes.
567 * @param entry Thread entry function.
568 * @param p1 1st entry point parameter.
569 * @param p2 2nd entry point parameter.
570 * @param p3 3rd entry point parameter.
571 * @param prio Thread priority.
572 * @param options Thread options.
573 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400574 *
575 * @internal It has been observed that the x86 compiler by default aligns
576 * these _static_thread_data structures to 32-byte boundaries, thereby
577 * wasting space. To work around this, force a 4-byte alignment.
578 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500579#define K_THREAD_DEFINE(name, stack_size, \
580 entry, p1, p2, p3, \
581 prio, options, delay) \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700582 char __noinit __stack _k_thread_stack_##name[stack_size]; \
583 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500584 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500585 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700586 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
587 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500588 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700589 NULL, 0); \
590 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500593 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * @param thread ID of thread whose priority is needed.
598 *
599 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500601extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602
603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 *
608 * Rescheduling can occur immediately depending on the priority @a thread is
609 * set to:
610 *
611 * - If its priority is raised above the priority of the caller of this
612 * function, and the caller is preemptible, @a thread will be scheduled in.
613 *
614 * - If the caller operates on itself, it lowers its priority below that of
615 * other threads in the system, and the caller is preemptible, the thread of
616 * highest priority will be scheduled in.
617 *
618 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
619 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
620 * highest priority.
621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 * @param prio New priority.
624 *
625 * @warning Changing the priority of a thread currently involved in mutex
626 * priority inheritance may result in undefined behavior.
627 *
628 * @return N/A
629 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400630extern void k_thread_priority_set(k_tid_t thread, int prio);
631
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500633 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500635 * This routine prevents the kernel scheduler from making @a thread the
636 * current thread. All other internal operations on @a thread are still
637 * performed; for example, any timeout it is waiting on keeps ticking,
638 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500640 * If @a thread is already suspended, the routine has no effect.
641 *
642 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400643 *
644 * @return N/A
645 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400646extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400647
648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500649 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500651 * This routine allows the kernel scheduler to make @a thread the current
652 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500654 * If @a thread is not currently suspended, the routine has no effect.
655 *
656 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657 *
658 * @return N/A
659 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400660extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400661
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500665 * This routine specifies how the scheduler will perform time slicing of
666 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500668 * To enable time slicing, @a slice must be non-zero. The scheduler
669 * ensures that no thread runs for more than the specified time limit
670 * before other threads of that priority are given a chance to execute.
671 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700672 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500674 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 * execute. Once the scheduler selects a thread for execution, there is no
676 * minimum guaranteed time the thread will execute before threads of greater or
677 * equal priority are scheduled.
678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500679 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 * for execution, this routine has no effect; the thread is immediately
681 * rescheduled after the slice period expires.
682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500683 * To disable timeslicing, set both @a slice and @a prio to zero.
684 *
685 * @param slice Maximum time slice length (in milliseconds).
686 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687 *
688 * @return N/A
689 */
Kumar Galacc334c72017-04-21 10:55:34 -0500690extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400691
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692/**
Allan Stephensc98da842016-11-11 15:45:03 -0500693 * @} end defgroup thread_apis
694 */
695
696/**
697 * @addtogroup isr_apis
698 * @{
699 */
700
701/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500702 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703 *
Allan Stephensc98da842016-11-11 15:45:03 -0500704 * This routine allows the caller to customize its actions, depending on
705 * whether it is a thread or an ISR.
706 *
707 * @note Can be called by ISRs.
708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @return 0 if invoked by a thread.
710 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500712extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400713
Benjamin Walsh445830d2016-11-10 15:54:27 -0500714/**
715 * @brief Determine if code is running in a preemptible thread.
716 *
Allan Stephensc98da842016-11-11 15:45:03 -0500717 * This routine allows the caller to customize its actions, depending on
718 * whether it can be preempted by another thread. The routine returns a 'true'
719 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500720 *
Allan Stephensc98da842016-11-11 15:45:03 -0500721 * - The code is running in a thread, not at ISR.
722 * - The thread's priority is in the preemptible range.
723 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500724 *
Allan Stephensc98da842016-11-11 15:45:03 -0500725 * @note Can be called by ISRs.
726 *
727 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500728 * @return Non-zero if invoked by a preemptible thread.
729 */
730extern int k_is_preempt_thread(void);
731
Allan Stephensc98da842016-11-11 15:45:03 -0500732/**
733 * @} end addtogroup isr_apis
734 */
735
736/**
737 * @addtogroup thread_apis
738 * @{
739 */
740
741/**
742 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500743 *
Allan Stephensc98da842016-11-11 15:45:03 -0500744 * This routine prevents the current thread from being preempted by another
745 * thread by instructing the scheduler to treat it as a cooperative thread.
746 * If the thread subsequently performs an operation that makes it unready,
747 * it will be context switched out in the normal manner. When the thread
748 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500749 *
Allan Stephensc98da842016-11-11 15:45:03 -0500750 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500751 *
Allan Stephensc98da842016-11-11 15:45:03 -0500752 * @note k_sched_lock() and k_sched_unlock() should normally be used
753 * when the operation being performed can be safely interrupted by ISRs.
754 * However, if the amount of processing involved is very small, better
755 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500756 *
757 * @return N/A
758 */
759extern void k_sched_lock(void);
760
Allan Stephensc98da842016-11-11 15:45:03 -0500761/**
762 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500763 *
Allan Stephensc98da842016-11-11 15:45:03 -0500764 * This routine reverses the effect of a previous call to k_sched_lock().
765 * A thread must call the routine once for each time it called k_sched_lock()
766 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500767 *
768 * @return N/A
769 */
770extern void k_sched_unlock(void);
771
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * Custom data is not used by the kernel itself, and is freely available
778 * for a thread to use as it sees fit. It can be used as a framework
779 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
783 * @return N/A
784 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400785extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400786
787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400794extern void *k_thread_custom_data_get(void);
795
796/**
Allan Stephensc98da842016-11-11 15:45:03 -0500797 * @} end addtogroup thread_apis
798 */
799
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400800#include <sys_clock.h>
801
Allan Stephensc2f15a42016-11-17 12:24:22 -0500802/**
803 * @addtogroup clock_apis
804 * @{
805 */
806
807/**
808 * @brief Generate null timeout delay.
809 *
810 * This macro generates a timeout delay that that instructs a kernel API
811 * not to wait if the requested operation cannot be performed immediately.
812 *
813 * @return Timeout delay value.
814 */
815#define K_NO_WAIT 0
816
817/**
818 * @brief Generate timeout delay from milliseconds.
819 *
820 * This macro generates a timeout delay that that instructs a kernel API
821 * to wait up to @a ms milliseconds to perform the requested operation.
822 *
823 * @param ms Duration in milliseconds.
824 *
825 * @return Timeout delay value.
826 */
Johan Hedberg14471692016-11-13 10:52:15 +0200827#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500828
829/**
830 * @brief Generate timeout delay from seconds.
831 *
832 * This macro generates a timeout delay that that instructs a kernel API
833 * to wait up to @a s seconds to perform the requested operation.
834 *
835 * @param s Duration in seconds.
836 *
837 * @return Timeout delay value.
838 */
Johan Hedberg14471692016-11-13 10:52:15 +0200839#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500840
841/**
842 * @brief Generate timeout delay from minutes.
843 *
844 * This macro generates a timeout delay that that instructs a kernel API
845 * to wait up to @a m minutes to perform the requested operation.
846 *
847 * @param m Duration in minutes.
848 *
849 * @return Timeout delay value.
850 */
Johan Hedberg14471692016-11-13 10:52:15 +0200851#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500852
853/**
854 * @brief Generate timeout delay from hours.
855 *
856 * This macro generates a timeout delay that that instructs a kernel API
857 * to wait up to @a h hours to perform the requested operation.
858 *
859 * @param h Duration in hours.
860 *
861 * @return Timeout delay value.
862 */
Johan Hedberg14471692016-11-13 10:52:15 +0200863#define K_HOURS(h) K_MINUTES((h) * 60)
864
Allan Stephensc98da842016-11-11 15:45:03 -0500865/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500866 * @brief Generate infinite timeout delay.
867 *
868 * This macro generates a timeout delay that that instructs a kernel API
869 * to wait as long as necessary to perform the requested operation.
870 *
871 * @return Timeout delay value.
872 */
873#define K_FOREVER (-1)
874
875/**
876 * @} end addtogroup clock_apis
877 */
878
879/**
Allan Stephensc98da842016-11-11 15:45:03 -0500880 * @cond INTERNAL_HIDDEN
881 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400882
Benjamin Walsh62092182016-12-20 14:39:08 -0500883/* kernel clocks */
884
885#if (sys_clock_ticks_per_sec == 1000) || \
886 (sys_clock_ticks_per_sec == 500) || \
887 (sys_clock_ticks_per_sec == 250) || \
888 (sys_clock_ticks_per_sec == 125) || \
889 (sys_clock_ticks_per_sec == 100) || \
890 (sys_clock_ticks_per_sec == 50) || \
891 (sys_clock_ticks_per_sec == 25) || \
892 (sys_clock_ticks_per_sec == 20) || \
893 (sys_clock_ticks_per_sec == 10) || \
894 (sys_clock_ticks_per_sec == 1)
895
896 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
897#else
898 /* yields horrible 64-bit math on many architectures: try to avoid */
899 #define _NON_OPTIMIZED_TICKS_PER_SEC
900#endif
901
902#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500903extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500904#else
Kumar Galacc334c72017-04-21 10:55:34 -0500905static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500906{
Kumar Galacc334c72017-04-21 10:55:34 -0500907 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500908}
909#endif
910
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500911/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800912#ifdef CONFIG_TICKLESS_KERNEL
913#define _TICK_ALIGN 0
914#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500915#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800916#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500917
Kumar Galacc334c72017-04-21 10:55:34 -0500918static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400919{
Benjamin Walsh62092182016-12-20 14:39:08 -0500920#ifdef CONFIG_SYS_CLOCK_EXISTS
921
922#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500923 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400924#else
Kumar Galacc334c72017-04-21 10:55:34 -0500925 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500926#endif
927
928#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400929 __ASSERT(ticks == 0, "");
930 return 0;
931#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400932}
933
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400934struct k_timer {
935 /*
936 * _timeout structure must be first here if we want to use
937 * dynamic timer allocation. timeout.node is used in the double-linked
938 * list of free timers
939 */
940 struct _timeout timeout;
941
Allan Stephens45bfa372016-10-12 12:39:42 -0500942 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943 _wait_q_t wait_q;
944
945 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500946 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400947
948 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500949 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950
951 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500952 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400953
Allan Stephens45bfa372016-10-12 12:39:42 -0500954 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500955 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400956
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500957 /* user-specific data, also used to support legacy features */
958 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400959
Anas Nashif2f203c22016-12-18 06:57:45 -0500960 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400961};
962
Allan Stephens1342adb2016-11-03 13:54:53 -0500963#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400964 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500965 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500966 .timeout.wait_q = NULL, \
967 .timeout.thread = NULL, \
968 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400969 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500970 .expiry_fn = expiry, \
971 .stop_fn = stop, \
972 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500973 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500974 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400975 }
976
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977/**
Allan Stephensc98da842016-11-11 15:45:03 -0500978 * INTERNAL_HIDDEN @endcond
979 */
980
981/**
982 * @defgroup timer_apis Timer APIs
983 * @ingroup kernel_apis
984 * @{
985 */
986
987/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500988 * @typedef k_timer_expiry_t
989 * @brief Timer expiry function type.
990 *
991 * A timer's expiry function is executed by the system clock interrupt handler
992 * each time the timer expires. The expiry function is optional, and is only
993 * invoked if the timer has been initialized with one.
994 *
995 * @param timer Address of timer.
996 *
997 * @return N/A
998 */
999typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1000
1001/**
1002 * @typedef k_timer_stop_t
1003 * @brief Timer stop function type.
1004 *
1005 * A timer's stop function is executed if the timer is stopped prematurely.
1006 * The function runs in the context of the thread that stops the timer.
1007 * The stop function is optional, and is only invoked if the timer has been
1008 * initialized with one.
1009 *
1010 * @param timer Address of timer.
1011 *
1012 * @return N/A
1013 */
1014typedef void (*k_timer_stop_t)(struct k_timer *timer);
1015
1016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001021 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 *
1023 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * @param expiry_fn Function to invoke each time the timer expires.
1025 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001027#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001028 struct k_timer name \
1029 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -05001030 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031
Allan Stephens45bfa372016-10-12 12:39:42 -05001032/**
1033 * @brief Initialize a timer.
1034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001036 *
1037 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * @param expiry_fn Function to invoke each time the timer expires.
1039 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001040 *
1041 * @return N/A
1042 */
1043extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001044 k_timer_expiry_t expiry_fn,
1045 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001046
Allan Stephens45bfa372016-10-12 12:39:42 -05001047/**
1048 * @brief Start a timer.
1049 *
1050 * This routine starts a timer, and resets its status to zero. The timer
1051 * begins counting down using the specified duration and period values.
1052 *
1053 * Attempting to start a timer that is already running is permitted.
1054 * The timer's status is reset to zero and the timer begins counting down
1055 * using the new duration and period values.
1056 *
1057 * @param timer Address of timer.
1058 * @param duration Initial timer duration (in milliseconds).
1059 * @param period Timer period (in milliseconds).
1060 *
1061 * @return N/A
1062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001064 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001065
1066/**
1067 * @brief Stop a timer.
1068 *
1069 * This routine stops a running timer prematurely. The timer's stop function,
1070 * if one exists, is invoked by the caller.
1071 *
1072 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001074 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001075 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1076 * if @a k_timer_stop is to be called from ISRs.
1077 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001078 * @param timer Address of timer.
1079 *
1080 * @return N/A
1081 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001083
1084/**
1085 * @brief Read timer status.
1086 *
1087 * This routine reads the timer's status, which indicates the number of times
1088 * it has expired since its status was last read.
1089 *
1090 * Calling this routine resets the timer's status to zero.
1091 *
1092 * @param timer Address of timer.
1093 *
1094 * @return Timer status.
1095 */
Kumar Galacc334c72017-04-21 10:55:34 -05001096extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001097
1098/**
1099 * @brief Synchronize thread to timer expiration.
1100 *
1101 * This routine blocks the calling thread until the timer's status is non-zero
1102 * (indicating that it has expired at least once since it was last examined)
1103 * or the timer is stopped. If the timer status is already non-zero,
1104 * or the timer is already stopped, the caller continues without waiting.
1105 *
1106 * Calling this routine resets the timer's status to zero.
1107 *
1108 * This routine must not be used by interrupt handlers, since they are not
1109 * allowed to block.
1110 *
1111 * @param timer Address of timer.
1112 *
1113 * @return Timer status.
1114 */
Kumar Galacc334c72017-04-21 10:55:34 -05001115extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001116
1117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001119 *
1120 * This routine computes the (approximate) time remaining before a running
1121 * timer next expires. If the timer is not running, it returns zero.
1122 *
1123 * @param timer Address of timer.
1124 *
1125 * @return Remaining time (in milliseconds).
1126 */
Kumar Galacc334c72017-04-21 10:55:34 -05001127static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001128{
1129 return _timeout_remaining_get(&timer->timeout);
1130}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001131
Allan Stephensc98da842016-11-11 15:45:03 -05001132/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001133 * @brief Associate user-specific data with a timer.
1134 *
1135 * This routine records the @a user_data with the @a timer, to be retrieved
1136 * later.
1137 *
1138 * It can be used e.g. in a timer handler shared across multiple subsystems to
1139 * retrieve data specific to the subsystem this timer is associated with.
1140 *
1141 * @param timer Address of timer.
1142 * @param user_data User data to associate with the timer.
1143 *
1144 * @return N/A
1145 */
1146static inline void k_timer_user_data_set(struct k_timer *timer,
1147 void *user_data)
1148{
1149 timer->user_data = user_data;
1150}
1151
1152/**
1153 * @brief Retrieve the user-specific data from a timer.
1154 *
1155 * @param timer Address of timer.
1156 *
1157 * @return The user data.
1158 */
1159static inline void *k_timer_user_data_get(struct k_timer *timer)
1160{
1161 return timer->user_data;
1162}
1163
1164/**
Allan Stephensc98da842016-11-11 15:45:03 -05001165 * @} end defgroup timer_apis
1166 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001167
Allan Stephensc98da842016-11-11 15:45:03 -05001168/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001169 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001170 * @{
1171 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001172
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001173/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * This routine returns the elapsed time since the system booted,
1177 * in milliseconds.
1178 *
1179 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001180 */
Kumar Galacc334c72017-04-21 10:55:34 -05001181extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001182
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001183#ifdef CONFIG_TICKLESS_KERNEL
1184/**
1185 * @brief Enable clock always on in tickless kernel
1186 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001187 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001188 * there are no timer events programmed in tickless kernel
1189 * scheduling. This is necessary if the clock is used to track
1190 * passage of time.
1191 *
1192 * @retval prev_status Previous status of always on flag
1193 */
1194static inline int k_enable_sys_clock_always_on(void)
1195{
1196 int prev_status = _sys_clock_always_on;
1197
1198 _sys_clock_always_on = 1;
1199 _enable_sys_clock();
1200
1201 return prev_status;
1202}
1203
1204/**
1205 * @brief Disable clock always on in tickless kernel
1206 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001207 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001208 * there are no timer events programmed in tickless kernel
1209 * scheduling. To save power, this routine should be called
1210 * immediately when clock is not used to track time.
1211 */
1212static inline void k_disable_sys_clock_always_on(void)
1213{
1214 _sys_clock_always_on = 0;
1215}
1216#else
1217#define k_enable_sys_clock_always_on() do { } while ((0))
1218#define k_disable_sys_clock_always_on() do { } while ((0))
1219#endif
1220
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001221/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001222 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * This routine returns the lower 32-bits of the elapsed time since the system
1225 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001227 * This routine can be more efficient than k_uptime_get(), as it reduces the
1228 * need for interrupt locking and 64-bit math. However, the 32-bit result
1229 * cannot hold a system uptime time larger than approximately 50 days, so the
1230 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001232 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001233 */
Kumar Galacc334c72017-04-21 10:55:34 -05001234extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001235
1236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * This routine computes the elapsed time between the current system uptime
1240 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * @param reftime Pointer to a reference time, which is updated to the current
1243 * uptime upon return.
1244 *
1245 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001246 */
Kumar Galacc334c72017-04-21 10:55:34 -05001247extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001248
1249/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001250 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * This routine computes the elapsed time between the current system uptime
1253 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001255 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1256 * need for interrupt locking and 64-bit math. However, the 32-bit result
1257 * cannot hold an elapsed time larger than approximately 50 days, so the
1258 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * @param reftime Pointer to a reference time, which is updated to the current
1261 * uptime upon return.
1262 *
1263 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001264 */
Kumar Galacc334c72017-04-21 10:55:34 -05001265extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001266
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001270 * This routine returns the current time, as measured by the system's hardware
1271 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001275#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001276
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001277/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001278 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279 */
1280
Allan Stephensc98da842016-11-11 15:45:03 -05001281/**
1282 * @cond INTERNAL_HIDDEN
1283 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001285struct k_queue {
1286 _wait_q_t wait_q;
1287 sys_slist_t data_q;
1288 _POLL_EVENT;
1289
1290 _OBJECT_TRACING_NEXT_PTR(k_queue);
1291};
1292
1293#define K_QUEUE_INITIALIZER(obj) \
1294 { \
1295 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1296 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1297 _POLL_EVENT_OBJ_INIT \
1298 _OBJECT_TRACING_INIT \
1299 }
1300
1301/**
1302 * INTERNAL_HIDDEN @endcond
1303 */
1304
1305/**
1306 * @defgroup queue_apis Queue APIs
1307 * @ingroup kernel_apis
1308 * @{
1309 */
1310
1311/**
1312 * @brief Initialize a queue.
1313 *
1314 * This routine initializes a queue object, prior to its first use.
1315 *
1316 * @param queue Address of the queue.
1317 *
1318 * @return N/A
1319 */
1320extern void k_queue_init(struct k_queue *queue);
1321
1322/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001323 * @brief Cancel waiting on a queue.
1324 *
1325 * This routine causes first thread pending on @a queue, if any, to
1326 * return from k_queue_get() call with NULL value (as if timeout expired).
1327 *
1328 * @note Can be called by ISRs.
1329 *
1330 * @param queue Address of the queue.
1331 *
1332 * @return N/A
1333 */
1334extern void k_queue_cancel_wait(struct k_queue *queue);
1335
1336/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001337 * @brief Append an element to the end of a queue.
1338 *
1339 * This routine appends a data item to @a queue. A queue data item must be
1340 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1341 * reserved for the kernel's use.
1342 *
1343 * @note Can be called by ISRs.
1344 *
1345 * @param queue Address of the queue.
1346 * @param data Address of the data item.
1347 *
1348 * @return N/A
1349 */
1350extern void k_queue_append(struct k_queue *queue, void *data);
1351
1352/**
1353 * @brief Prepend an element to a queue.
1354 *
1355 * This routine prepends a data item to @a queue. A queue data item must be
1356 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1357 * reserved for the kernel's use.
1358 *
1359 * @note Can be called by ISRs.
1360 *
1361 * @param queue Address of the queue.
1362 * @param data Address of the data item.
1363 *
1364 * @return N/A
1365 */
1366extern void k_queue_prepend(struct k_queue *queue, void *data);
1367
1368/**
1369 * @brief Inserts an element to a queue.
1370 *
1371 * This routine inserts a data item to @a queue after previous item. A queue
1372 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1373 * item are reserved for the kernel's use.
1374 *
1375 * @note Can be called by ISRs.
1376 *
1377 * @param queue Address of the queue.
1378 * @param prev Address of the previous data item.
1379 * @param data Address of the data item.
1380 *
1381 * @return N/A
1382 */
1383extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1384
1385/**
1386 * @brief Atomically append a list of elements to a queue.
1387 *
1388 * This routine adds a list of data items to @a queue in one operation.
1389 * The data items must be in a singly-linked list, with the first 32 bits
1390 * in each data item pointing to the next data item; the list must be
1391 * NULL-terminated.
1392 *
1393 * @note Can be called by ISRs.
1394 *
1395 * @param queue Address of the queue.
1396 * @param head Pointer to first node in singly-linked list.
1397 * @param tail Pointer to last node in singly-linked list.
1398 *
1399 * @return N/A
1400 */
1401extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1402
1403/**
1404 * @brief Atomically add a list of elements to a queue.
1405 *
1406 * This routine adds a list of data items to @a queue in one operation.
1407 * The data items must be in a singly-linked list implemented using a
1408 * sys_slist_t object. Upon completion, the original list is empty.
1409 *
1410 * @note Can be called by ISRs.
1411 *
1412 * @param queue Address of the queue.
1413 * @param list Pointer to sys_slist_t object.
1414 *
1415 * @return N/A
1416 */
1417extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1418
1419/**
1420 * @brief Get an element from a queue.
1421 *
1422 * This routine removes first data item from @a queue. The first 32 bits of the
1423 * data item are reserved for the kernel's use.
1424 *
1425 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1426 *
1427 * @param queue Address of the queue.
1428 * @param timeout Waiting period to obtain a data item (in milliseconds),
1429 * or one of the special values K_NO_WAIT and K_FOREVER.
1430 *
1431 * @return Address of the data item if successful; NULL if returned
1432 * without waiting, or waiting period timed out.
1433 */
Kumar Galacc334c72017-04-21 10:55:34 -05001434extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001435
1436/**
1437 * @brief Query a queue to see if it has data available.
1438 *
1439 * Note that the data might be already gone by the time this function returns
1440 * if other threads are also trying to read from the queue.
1441 *
1442 * @note Can be called by ISRs.
1443 *
1444 * @param queue Address of the queue.
1445 *
1446 * @return Non-zero if the queue is empty.
1447 * @return 0 if data is available.
1448 */
1449static inline int k_queue_is_empty(struct k_queue *queue)
1450{
1451 return (int)sys_slist_is_empty(&queue->data_q);
1452}
1453
1454/**
1455 * @brief Statically define and initialize a queue.
1456 *
1457 * The queue can be accessed outside the module where it is defined using:
1458 *
1459 * @code extern struct k_queue <name>; @endcode
1460 *
1461 * @param name Name of the queue.
1462 */
1463#define K_QUEUE_DEFINE(name) \
1464 struct k_queue name \
1465 __in_section(_k_queue, static, name) = \
1466 K_QUEUE_INITIALIZER(name)
1467
1468/**
1469 * @} end defgroup queue_apis
1470 */
1471
1472/**
1473 * @cond INTERNAL_HIDDEN
1474 */
1475
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001476struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001477 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001478};
1479
Allan Stephensc98da842016-11-11 15:45:03 -05001480#define K_FIFO_INITIALIZER(obj) \
1481 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001482 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001483 }
1484
1485/**
1486 * INTERNAL_HIDDEN @endcond
1487 */
1488
1489/**
1490 * @defgroup fifo_apis Fifo APIs
1491 * @ingroup kernel_apis
1492 * @{
1493 */
1494
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001495/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001500 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001501 *
1502 * @return N/A
1503 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001504#define k_fifo_init(fifo) \
1505 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001506
1507/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001508 * @brief Cancel waiting on a fifo.
1509 *
1510 * This routine causes first thread pending on @a fifo, if any, to
1511 * return from k_fifo_get() call with NULL value (as if timeout
1512 * expired).
1513 *
1514 * @note Can be called by ISRs.
1515 *
1516 * @param fifo Address of the fifo.
1517 *
1518 * @return N/A
1519 */
1520#define k_fifo_cancel_wait(fifo) \
1521 k_queue_cancel_wait((struct k_queue *) fifo)
1522
1523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * This routine adds a data item to @a fifo. A fifo data item must be
1527 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1528 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001530 * @note Can be called by ISRs.
1531 *
1532 * @param fifo Address of the fifo.
1533 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 *
1535 * @return N/A
1536 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001537#define k_fifo_put(fifo, data) \
1538 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001539
1540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001543 * This routine adds a list of data items to @a fifo in one operation.
1544 * The data items must be in a singly-linked list, with the first 32 bits
1545 * each data item pointing to the next data item; the list must be
1546 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001548 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001550 * @param fifo Address of the fifo.
1551 * @param head Pointer to first node in singly-linked list.
1552 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001553 *
1554 * @return N/A
1555 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001556#define k_fifo_put_list(fifo, head, tail) \
1557 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001558
1559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001560 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * This routine adds a list of data items to @a fifo in one operation.
1563 * The data items must be in a singly-linked list implemented using a
1564 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565 * and must be re-initialized via sys_slist_init().
1566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * @note Can be called by ISRs.
1568 *
1569 * @param fifo Address of the fifo.
1570 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001571 *
1572 * @return N/A
1573 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001574#define k_fifo_put_slist(fifo, list) \
1575 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001576
1577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001580 * This routine removes a data item from @a fifo in a "first in, first out"
1581 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001583 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1584 *
1585 * @param fifo Address of the fifo.
1586 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001587 * or one of the special values K_NO_WAIT and K_FOREVER.
1588 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001589 * @return Address of the data item if successful; NULL if returned
1590 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001592#define k_fifo_get(fifo, timeout) \
1593 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001594
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001595/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001596 * @brief Query a fifo to see if it has data available.
1597 *
1598 * Note that the data might be already gone by the time this function returns
1599 * if other threads is also trying to read from the fifo.
1600 *
1601 * @note Can be called by ISRs.
1602 *
1603 * @param fifo Address of the fifo.
1604 *
1605 * @return Non-zero if the fifo is empty.
1606 * @return 0 if data is available.
1607 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001608#define k_fifo_is_empty(fifo) \
1609 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001610
1611/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001615 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001616 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001618 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001619 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001620#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001621 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001622 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001623 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001624
Allan Stephensc98da842016-11-11 15:45:03 -05001625/**
1626 * @} end defgroup fifo_apis
1627 */
1628
1629/**
1630 * @cond INTERNAL_HIDDEN
1631 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632
1633struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001634 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635};
1636
Allan Stephensc98da842016-11-11 15:45:03 -05001637#define K_LIFO_INITIALIZER(obj) \
1638 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001639 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001640 }
1641
1642/**
1643 * INTERNAL_HIDDEN @endcond
1644 */
1645
1646/**
1647 * @defgroup lifo_apis Lifo APIs
1648 * @ingroup kernel_apis
1649 * @{
1650 */
1651
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001658 *
1659 * @return N/A
1660 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001661#define k_lifo_init(lifo) \
1662 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001663
1664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001665 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * This routine adds a data item to @a lifo. A lifo data item must be
1668 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1669 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001671 * @note Can be called by ISRs.
1672 *
1673 * @param lifo Address of the lifo.
1674 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001675 *
1676 * @return N/A
1677 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001678#define k_lifo_put(lifo, data) \
1679 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001680
1681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001682 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * This routine removes a data item from @a lifo in a "last in, first out"
1685 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1688 *
1689 * @param lifo Address of the lifo.
1690 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001691 * or one of the special values K_NO_WAIT and K_FOREVER.
1692 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001693 * @return Address of the data item if successful; NULL if returned
1694 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001696#define k_lifo_get(lifo, timeout) \
1697 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001698
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001703 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001704 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001708#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001709 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001710 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001711 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001712
Allan Stephensc98da842016-11-11 15:45:03 -05001713/**
1714 * @} end defgroup lifo_apis
1715 */
1716
1717/**
1718 * @cond INTERNAL_HIDDEN
1719 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001720
1721struct k_stack {
1722 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001723 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724
Anas Nashif2f203c22016-12-18 06:57:45 -05001725 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726};
1727
Allan Stephensc98da842016-11-11 15:45:03 -05001728#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1729 { \
1730 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1731 .base = stack_buffer, \
1732 .next = stack_buffer, \
1733 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001734 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001735 }
1736
1737/**
1738 * INTERNAL_HIDDEN @endcond
1739 */
1740
1741/**
1742 * @defgroup stack_apis Stack APIs
1743 * @ingroup kernel_apis
1744 * @{
1745 */
1746
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747/**
1748 * @brief Initialize a stack.
1749 *
1750 * This routine initializes a stack object, prior to its first use.
1751 *
1752 * @param stack Address of the stack.
1753 * @param buffer Address of array used to hold stacked values.
1754 * @param num_entries Maximum number of values that can be stacked.
1755 *
1756 * @return N/A
1757 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001758extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001759 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760
1761/**
1762 * @brief Push an element onto a stack.
1763 *
1764 * This routine adds a 32-bit value @a data to @a stack.
1765 *
1766 * @note Can be called by ISRs.
1767 *
1768 * @param stack Address of the stack.
1769 * @param data Value to push onto the stack.
1770 *
1771 * @return N/A
1772 */
Kumar Galacc334c72017-04-21 10:55:34 -05001773extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774
1775/**
1776 * @brief Pop an element from a stack.
1777 *
1778 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1779 * manner and stores the value in @a data.
1780 *
1781 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1782 *
1783 * @param stack Address of the stack.
1784 * @param data Address of area to hold the value popped from the stack.
1785 * @param timeout Waiting period to obtain a value (in milliseconds),
1786 * or one of the special values K_NO_WAIT and K_FOREVER.
1787 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001788 * @retval 0 Element popped from stack.
1789 * @retval -EBUSY Returned without waiting.
1790 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 */
Kumar Galacc334c72017-04-21 10:55:34 -05001792extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001793
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001799 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * @param name Name of the stack.
1802 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001804#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001805 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001806 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001807 struct k_stack name \
1808 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001809 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1810 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001811
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812/**
Allan Stephensc98da842016-11-11 15:45:03 -05001813 * @} end defgroup stack_apis
1814 */
1815
Allan Stephens6bba9b02016-11-16 14:56:54 -05001816struct k_work;
1817
Allan Stephensc98da842016-11-11 15:45:03 -05001818/**
1819 * @defgroup workqueue_apis Workqueue Thread APIs
1820 * @ingroup kernel_apis
1821 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001822 */
1823
Allan Stephens6bba9b02016-11-16 14:56:54 -05001824/**
1825 * @typedef k_work_handler_t
1826 * @brief Work item handler function type.
1827 *
1828 * A work item's handler function is executed by a workqueue's thread
1829 * when the work item is processed by the workqueue.
1830 *
1831 * @param work Address of the work item.
1832 *
1833 * @return N/A
1834 */
1835typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001836
1837/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001838 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001839 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001840
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001841struct k_work_q {
1842 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001843 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001844};
1845
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001846enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001847 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001848};
1849
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001850struct k_work {
1851 void *_reserved; /* Used by k_fifo implementation. */
1852 k_work_handler_t handler;
1853 atomic_t flags[1];
1854};
1855
Allan Stephens6bba9b02016-11-16 14:56:54 -05001856struct k_delayed_work {
1857 struct k_work work;
1858 struct _timeout timeout;
1859 struct k_work_q *work_q;
1860};
1861
1862extern struct k_work_q k_sys_work_q;
1863
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001865 * INTERNAL_HIDDEN @endcond
1866 */
1867
1868/**
1869 * @brief Initialize a statically-defined work item.
1870 *
1871 * This macro can be used to initialize a statically-defined workqueue work
1872 * item, prior to its first use. For example,
1873 *
1874 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1875 *
1876 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877 */
1878#define K_WORK_INITIALIZER(work_handler) \
1879 { \
1880 ._reserved = NULL, \
1881 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001882 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883 }
1884
1885/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001886 * @brief Initialize a work item.
1887 *
1888 * This routine initializes a workqueue work item, prior to its first use.
1889 *
1890 * @param work Address of work item.
1891 * @param handler Function to invoke each time work item is processed.
1892 *
1893 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001894 */
1895static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1896{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001897 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001898 work->handler = handler;
1899}
1900
1901/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001902 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001903 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001904 * This routine submits work item @a work to be processed by workqueue
1905 * @a work_q. If the work item is already pending in the workqueue's queue
1906 * as a result of an earlier submission, this routine has no effect on the
1907 * work item. If the work item has already been processed, or is currently
1908 * being processed, its work is considered complete and the work item can be
1909 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001910 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001911 * @warning
1912 * A submitted work item must not be modified until it has been processed
1913 * by the workqueue.
1914 *
1915 * @note Can be called by ISRs.
1916 *
1917 * @param work_q Address of workqueue.
1918 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001919 *
1920 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921 */
1922static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1923 struct k_work *work)
1924{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001925 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926 k_fifo_put(&work_q->fifo, work);
1927 }
1928}
1929
1930/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001931 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001933 * This routine indicates if work item @a work is pending in a workqueue's
1934 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001936 * @note Can be called by ISRs.
1937 *
1938 * @param work Address of work item.
1939 *
1940 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001941 */
1942static inline int k_work_pending(struct k_work *work)
1943{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001944 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001945}
1946
1947/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001948 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001950 * This routine starts workqueue @a work_q. The workqueue spawns its work
1951 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001953 * @param work_q Address of workqueue.
1954 * @param stack Pointer to work queue thread's stack space.
1955 * @param stack_size Size of the work queue thread's stack (in bytes).
1956 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001957 *
1958 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959 */
Allan Stephens904cf972016-10-07 13:59:23 -05001960extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001961 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001963/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001964 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001966 * This routine initializes a workqueue delayed work item, prior to
1967 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001969 * @param work Address of delayed work item.
1970 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
1972 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001974extern void k_delayed_work_init(struct k_delayed_work *work,
1975 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976
1977/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001978 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001979 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001980 * This routine schedules work item @a work to be processed by workqueue
1981 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001982 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001983 * Only when the countdown completes is the work item actually submitted to
1984 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001986 * Submitting a previously submitted delayed work item that is still
1987 * counting down cancels the existing submission and restarts the countdown
1988 * using the new delay. If the work item is currently pending on the
1989 * workqueue's queue because the countdown has completed it is too late to
1990 * resubmit the item, and resubmission fails without impacting the work item.
1991 * If the work item has already been processed, or is currently being processed,
1992 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001993 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001994 * @warning
1995 * A delayed work item must not be modified until it has been processed
1996 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001998 * @note Can be called by ISRs.
1999 *
2000 * @param work_q Address of workqueue.
2001 * @param work Address of delayed work item.
2002 * @param delay Delay before submitting the work item (in milliseconds).
2003 *
2004 * @retval 0 Work item countdown started.
2005 * @retval -EINPROGRESS Work item is already pending.
2006 * @retval -EINVAL Work item is being processed or has completed its work.
2007 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002008 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002009extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2010 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002011 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002012
2013/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002014 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002015 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002016 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002017 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002018 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002019 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002020 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002022 * @param work Address of delayed work item.
2023 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002024 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002025 * @retval -EINPROGRESS Work item is already pending.
2026 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002028extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031 * @brief Submit a work item to the system workqueue.
2032 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002033 * This routine submits work item @a work to be processed by the system
2034 * workqueue. If the work item is already pending in the workqueue's queue
2035 * as a result of an earlier submission, this routine has no effect on the
2036 * work item. If the work item has already been processed, or is currently
2037 * being processed, its work is considered complete and the work item can be
2038 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002039 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002040 * @warning
2041 * Work items submitted to the system workqueue should avoid using handlers
2042 * that block or yield since this may prevent the system workqueue from
2043 * processing other work items in a timely manner.
2044 *
2045 * @note Can be called by ISRs.
2046 *
2047 * @param work Address of work item.
2048 *
2049 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050 */
2051static inline void k_work_submit(struct k_work *work)
2052{
2053 k_work_submit_to_queue(&k_sys_work_q, work);
2054}
2055
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002056/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057 * @brief Submit a delayed work item to the system workqueue.
2058 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002059 * This routine schedules work item @a work to be processed by the system
2060 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002061 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002062 * Only when the countdown completes is the work item actually submitted to
2063 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002064 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002065 * Submitting a previously submitted delayed work item that is still
2066 * counting down cancels the existing submission and restarts the countdown
2067 * using the new delay. If the work item is currently pending on the
2068 * workqueue's queue because the countdown has completed it is too late to
2069 * resubmit the item, and resubmission fails without impacting the work item.
2070 * If the work item has already been processed, or is currently being processed,
2071 * its work is considered complete and the work item can be resubmitted.
2072 *
2073 * @warning
2074 * Work items submitted to the system workqueue should avoid using handlers
2075 * that block or yield since this may prevent the system workqueue from
2076 * processing other work items in a timely manner.
2077 *
2078 * @note Can be called by ISRs.
2079 *
2080 * @param work Address of delayed work item.
2081 * @param delay Delay before submitting the work item (in milliseconds).
2082 *
2083 * @retval 0 Work item countdown started.
2084 * @retval -EINPROGRESS Work item is already pending.
2085 * @retval -EINVAL Work item is being processed or has completed its work.
2086 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087 */
2088static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002089 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002091 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092}
2093
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002095 * @brief Get time remaining before a delayed work gets scheduled.
2096 *
2097 * This routine computes the (approximate) time remaining before a
2098 * delayed work gets executed. If the delayed work is not waiting to be
2099 * schedules, it returns zero.
2100 *
2101 * @param work Delayed work item.
2102 *
2103 * @return Remaining time (in milliseconds).
2104 */
Kumar Galacc334c72017-04-21 10:55:34 -05002105static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002106{
2107 return _timeout_remaining_get(&work->timeout);
2108}
2109
2110/**
Allan Stephensc98da842016-11-11 15:45:03 -05002111 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112 */
2113
Allan Stephensc98da842016-11-11 15:45:03 -05002114/**
2115 * @cond INTERNAL_HIDDEN
2116 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117
2118struct k_mutex {
2119 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002120 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002121 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123
Anas Nashif2f203c22016-12-18 06:57:45 -05002124 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125};
2126
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127#define K_MUTEX_INITIALIZER(obj) \
2128 { \
2129 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2130 .owner = NULL, \
2131 .lock_count = 0, \
2132 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002133 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002134 }
2135
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136/**
Allan Stephensc98da842016-11-11 15:45:03 -05002137 * INTERNAL_HIDDEN @endcond
2138 */
2139
2140/**
2141 * @defgroup mutex_apis Mutex APIs
2142 * @ingroup kernel_apis
2143 * @{
2144 */
2145
2146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002151 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002156 struct k_mutex name \
2157 __in_section(_k_mutex, static, name) = \
2158 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * Upon completion, the mutex is available and does not have an owner.
2166 *
2167 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
2169 * @return N/A
2170 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172
2173/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002176 * This routine locks @a mutex. If the mutex is locked by another thread,
2177 * the calling thread waits until the mutex becomes available or until
2178 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * A thread is permitted to lock a mutex it has already locked. The operation
2181 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * @param mutex Address of the mutex.
2184 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 * or one of the special values K_NO_WAIT and K_FOREVER.
2186 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002187 * @retval 0 Mutex locked.
2188 * @retval -EBUSY Returned without waiting.
2189 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190 */
Kumar Galacc334c72017-04-21 10:55:34 -05002191extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192
2193/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002194 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * This routine unlocks @a mutex. The mutex must already be locked by the
2197 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002198 *
2199 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002200 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002201 * thread.
2202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002203 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002204 *
2205 * @return N/A
2206 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207extern void k_mutex_unlock(struct k_mutex *mutex);
2208
Allan Stephensc98da842016-11-11 15:45:03 -05002209/**
2210 * @} end defgroup mutex_apis
2211 */
2212
2213/**
2214 * @cond INTERNAL_HIDDEN
2215 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216
2217struct k_sem {
2218 _wait_q_t wait_q;
2219 unsigned int count;
2220 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002221 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222
Anas Nashif2f203c22016-12-18 06:57:45 -05002223 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224};
2225
Allan Stephensc98da842016-11-11 15:45:03 -05002226#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2227 { \
2228 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2229 .count = initial_count, \
2230 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002231 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002232 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002233 }
2234
2235/**
2236 * INTERNAL_HIDDEN @endcond
2237 */
2238
2239/**
2240 * @defgroup semaphore_apis Semaphore APIs
2241 * @ingroup kernel_apis
2242 * @{
2243 */
2244
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002246 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002250 * @param sem Address of the semaphore.
2251 * @param initial_count Initial semaphore count.
2252 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002253 *
2254 * @return N/A
2255 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2257 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002258
2259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002260 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2265 *
2266 * @param sem Address of the semaphore.
2267 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002268 * or one of the special values K_NO_WAIT and K_FOREVER.
2269 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002270 * @note When porting code from the nanokernel legacy API to the new API, be
2271 * careful with the return value of this function. The return value is the
2272 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2273 * non-zero means failure, while the nano_sem_take family returns 1 for success
2274 * and 0 for failure.
2275 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002276 * @retval 0 Semaphore taken.
2277 * @retval -EBUSY Returned without waiting.
2278 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002279 */
Kumar Galacc334c72017-04-21 10:55:34 -05002280extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002281
2282/**
2283 * @brief Give a semaphore.
2284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002285 * This routine gives @a sem, unless the semaphore is already at its maximum
2286 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002288 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002291 *
2292 * @return N/A
2293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294extern void k_sem_give(struct k_sem *sem);
2295
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002296/**
2297 * @brief Reset a semaphore's count to zero.
2298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002299 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002302 *
2303 * @return N/A
2304 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002305static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306{
2307 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308}
2309
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002310/**
2311 * @brief Get a semaphore's count.
2312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002318 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002319static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320{
2321 return sem->count;
2322}
2323
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002328 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002329 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * @param name Name of the semaphore.
2332 * @param initial_count Initial semaphore count.
2333 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002334 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002336 struct k_sem name \
2337 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338 K_SEM_INITIALIZER(name, initial_count, count_limit)
2339
Allan Stephensc98da842016-11-11 15:45:03 -05002340/**
2341 * @} end defgroup semaphore_apis
2342 */
2343
2344/**
2345 * @defgroup alert_apis Alert APIs
2346 * @ingroup kernel_apis
2347 * @{
2348 */
2349
Allan Stephens5eceb852016-11-16 10:16:30 -05002350/**
2351 * @typedef k_alert_handler_t
2352 * @brief Alert handler function type.
2353 *
2354 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002355 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002356 * and is only invoked if the alert has been initialized with one.
2357 *
2358 * @param alert Address of the alert.
2359 *
2360 * @return 0 if alert has been consumed; non-zero if alert should pend.
2361 */
2362typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002363
2364/**
2365 * @} end defgroup alert_apis
2366 */
2367
2368/**
2369 * @cond INTERNAL_HIDDEN
2370 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002372#define K_ALERT_DEFAULT NULL
2373#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002375struct k_alert {
2376 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 atomic_t send_count;
2378 struct k_work work_item;
2379 struct k_sem sem;
2380
Anas Nashif2f203c22016-12-18 06:57:45 -05002381 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382};
2383
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002384extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002386#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002388 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002390 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002391 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002392 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393 }
2394
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395/**
Allan Stephensc98da842016-11-11 15:45:03 -05002396 * INTERNAL_HIDDEN @endcond
2397 */
2398
2399/**
2400 * @addtogroup alert_apis
2401 * @{
2402 */
2403
2404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002407 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002409 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @param name Name of the alert.
2412 * @param alert_handler Action to take when alert is sent. Specify either
2413 * the address of a function to be invoked by the system workqueue
2414 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2415 * K_ALERT_DEFAULT (which causes the alert to pend).
2416 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002418#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002419 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002420 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002421 K_ALERT_INITIALIZER(name, alert_handler, \
2422 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002423
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * @param alert Address of the alert.
2430 * @param handler Action to take when alert is sent. Specify either the address
2431 * of a function to be invoked by the system workqueue thread,
2432 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2433 * K_ALERT_DEFAULT (which causes the alert to pend).
2434 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
2436 * @return N/A
2437 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002438extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2439 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440
2441/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2447 *
2448 * @param alert Address of the alert.
2449 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450 * or one of the special values K_NO_WAIT and K_FOREVER.
2451 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002452 * @retval 0 Alert received.
2453 * @retval -EBUSY Returned without waiting.
2454 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 */
Kumar Galacc334c72017-04-21 10:55:34 -05002456extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457
2458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * This routine signals @a alert. The action specified for @a alert will
2462 * be taken, which may trigger the execution of an alert handler function
2463 * and/or cause the alert to pend (assuming the alert has not reached its
2464 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466 * @note Can be called by ISRs.
2467 *
2468 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
2470 * @return N/A
2471 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002472extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473
2474/**
Allan Stephensc98da842016-11-11 15:45:03 -05002475 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476 */
2477
Allan Stephensc98da842016-11-11 15:45:03 -05002478/**
2479 * @cond INTERNAL_HIDDEN
2480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481
2482struct k_msgq {
2483 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002484 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002485 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486 char *buffer_start;
2487 char *buffer_end;
2488 char *read_ptr;
2489 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002490 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491
Anas Nashif2f203c22016-12-18 06:57:45 -05002492 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493};
2494
Peter Mitsis1da807e2016-10-06 11:36:59 -04002495#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496 { \
2497 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002498 .max_msgs = q_max_msgs, \
2499 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002501 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502 .read_ptr = q_buffer, \
2503 .write_ptr = q_buffer, \
2504 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002505 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 }
2507
Peter Mitsis1da807e2016-10-06 11:36:59 -04002508/**
Allan Stephensc98da842016-11-11 15:45:03 -05002509 * INTERNAL_HIDDEN @endcond
2510 */
2511
2512/**
2513 * @defgroup msgq_apis Message Queue APIs
2514 * @ingroup kernel_apis
2515 * @{
2516 */
2517
2518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2522 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002523 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2524 * message is similarly aligned to this boundary, @a q_msg_size must also be
2525 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * The message queue can be accessed outside the module where it is defined
2528 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002530 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param q_name Name of the message queue.
2533 * @param q_msg_size Message size (in bytes).
2534 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002535 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002536 */
2537#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2538 static char __noinit __aligned(q_align) \
2539 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002540 struct k_msgq q_name \
2541 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002542 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2543 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544
Peter Mitsisd7a37502016-10-13 11:37:40 -04002545/**
2546 * @brief Initialize a message queue.
2547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * This routine initializes a message queue object, prior to its first use.
2549 *
Allan Stephensda827222016-11-09 14:23:58 -06002550 * The message queue's ring buffer must contain space for @a max_msgs messages,
2551 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2552 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2553 * that each message is similarly aligned to this boundary, @a q_msg_size
2554 * must also be a multiple of N.
2555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @param q Address of the message queue.
2557 * @param buffer Pointer to ring buffer that holds queued messages.
2558 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002559 * @param max_msgs Maximum number of messages that can be queued.
2560 *
2561 * @return N/A
2562 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002563extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002564 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002565
2566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002567 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002570 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002571 * @note Can be called by ISRs.
2572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * @param q Address of the message queue.
2574 * @param data Pointer to the message.
2575 * @param timeout Waiting period to add the message (in milliseconds),
2576 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002577 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002578 * @retval 0 Message sent.
2579 * @retval -ENOMSG Returned without waiting or queue purged.
2580 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002581 */
Kumar Galacc334c72017-04-21 10:55:34 -05002582extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002583
2584/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * This routine receives a message from message queue @a q in a "first in,
2588 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002589 *
Allan Stephensc98da842016-11-11 15:45:03 -05002590 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * @param q Address of the message queue.
2593 * @param data Address of area to hold the received message.
2594 * @param timeout Waiting period to receive the message (in milliseconds),
2595 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002596 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002597 * @retval 0 Message received.
2598 * @retval -ENOMSG Returned without waiting.
2599 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002600 */
Kumar Galacc334c72017-04-21 10:55:34 -05002601extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002602
2603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * This routine discards all unreceived messages in a message queue's ring
2607 * buffer. Any threads that are blocked waiting to send a message to the
2608 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002611 *
2612 * @return N/A
2613 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614extern void k_msgq_purge(struct k_msgq *q);
2615
Peter Mitsis67be2492016-10-07 11:44:34 -04002616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * This routine returns the number of unused entries in a message queue's
2620 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * @param q Address of the message queue.
2623 *
2624 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002625 */
Kumar Galacc334c72017-04-21 10:55:34 -05002626static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002627{
2628 return q->max_msgs - q->used_msgs;
2629}
2630
Peter Mitsisd7a37502016-10-13 11:37:40 -04002631/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002634 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @param q Address of the message queue.
2637 *
2638 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002639 */
Kumar Galacc334c72017-04-21 10:55:34 -05002640static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641{
2642 return q->used_msgs;
2643}
2644
Allan Stephensc98da842016-11-11 15:45:03 -05002645/**
2646 * @} end defgroup msgq_apis
2647 */
2648
2649/**
2650 * @defgroup mem_pool_apis Memory Pool APIs
2651 * @ingroup kernel_apis
2652 * @{
2653 */
2654
Andy Ross73cb9582017-05-09 10:42:39 -07002655/* Note on sizing: the use of a 20 bit field for block means that,
2656 * assuming a reasonable minimum block size of 16 bytes, we're limited
2657 * to 16M of memory managed by a single pool. Long term it would be
2658 * good to move to a variable bit size based on configuration.
2659 */
2660struct k_mem_block_id {
2661 u32_t pool : 8;
2662 u32_t level : 4;
2663 u32_t block : 20;
2664};
2665
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002668 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669};
2670
Allan Stephensc98da842016-11-11 15:45:03 -05002671/**
2672 * @} end defgroup mem_pool_apis
2673 */
2674
2675/**
2676 * @defgroup mailbox_apis Mailbox APIs
2677 * @ingroup kernel_apis
2678 * @{
2679 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
2681struct k_mbox_msg {
2682 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002683 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002685 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002687 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 /** sender's message data buffer */
2689 void *tx_data;
2690 /** internal use only - needed for legacy API support */
2691 void *_rx_data;
2692 /** message data block descriptor */
2693 struct k_mem_block tx_block;
2694 /** source thread id */
2695 k_tid_t rx_source_thread;
2696 /** target thread id */
2697 k_tid_t tx_target_thread;
2698 /** internal use only - thread waiting on send (may be a dummy) */
2699 k_tid_t _syncing_thread;
2700#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2701 /** internal use only - semaphore used during asynchronous send */
2702 struct k_sem *_async_sem;
2703#endif
2704};
2705
Allan Stephensc98da842016-11-11 15:45:03 -05002706/**
2707 * @cond INTERNAL_HIDDEN
2708 */
2709
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710struct k_mbox {
2711 _wait_q_t tx_msg_queue;
2712 _wait_q_t rx_msg_queue;
2713
Anas Nashif2f203c22016-12-18 06:57:45 -05002714 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715};
2716
2717#define K_MBOX_INITIALIZER(obj) \
2718 { \
2719 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2720 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002721 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 }
2723
Peter Mitsis12092702016-10-14 12:57:23 -04002724/**
Allan Stephensc98da842016-11-11 15:45:03 -05002725 * INTERNAL_HIDDEN @endcond
2726 */
2727
2728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002733 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002736 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002738 struct k_mbox name \
2739 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740 K_MBOX_INITIALIZER(name) \
2741
Peter Mitsis12092702016-10-14 12:57:23 -04002742/**
2743 * @brief Initialize a mailbox.
2744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * This routine initializes a mailbox object, prior to its first use.
2746 *
2747 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002748 *
2749 * @return N/A
2750 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751extern void k_mbox_init(struct k_mbox *mbox);
2752
Peter Mitsis12092702016-10-14 12:57:23 -04002753/**
2754 * @brief Send a mailbox message in a synchronous manner.
2755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * This routine sends a message to @a mbox and waits for a receiver to both
2757 * receive and process it. The message data may be in a buffer, in a memory
2758 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * @param mbox Address of the mailbox.
2761 * @param tx_msg Address of the transmit message descriptor.
2762 * @param timeout Waiting period for the message to be received (in
2763 * milliseconds), or one of the special values K_NO_WAIT
2764 * and K_FOREVER. Once the message has been received,
2765 * this routine waits as long as necessary for the message
2766 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002767 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002768 * @retval 0 Message sent.
2769 * @retval -ENOMSG Returned without waiting.
2770 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002771 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002772extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002773 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002774
Peter Mitsis12092702016-10-14 12:57:23 -04002775/**
2776 * @brief Send a mailbox message in an asynchronous manner.
2777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002778 * This routine sends a message to @a mbox without waiting for a receiver
2779 * to process it. The message data may be in a buffer, in a memory pool block,
2780 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2781 * will be given when the message has been both received and completely
2782 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @param mbox Address of the mailbox.
2785 * @param tx_msg Address of the transmit message descriptor.
2786 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002787 *
2788 * @return N/A
2789 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002790extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 struct k_sem *sem);
2792
Peter Mitsis12092702016-10-14 12:57:23 -04002793/**
2794 * @brief Receive a mailbox message.
2795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * This routine receives a message from @a mbox, then optionally retrieves
2797 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * @param mbox Address of the mailbox.
2800 * @param rx_msg Address of the receive message descriptor.
2801 * @param buffer Address of the buffer to receive data, or NULL to defer data
2802 * retrieval and message disposal until later.
2803 * @param timeout Waiting period for a message to be received (in
2804 * milliseconds), or one of the special values K_NO_WAIT
2805 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002806 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002807 * @retval 0 Message received.
2808 * @retval -ENOMSG Returned without waiting.
2809 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002810 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002811extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002812 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002813
2814/**
2815 * @brief Retrieve mailbox message data into a buffer.
2816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * This routine completes the processing of a received message by retrieving
2818 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002819 *
2820 * Alternatively, this routine can be used to dispose of a received message
2821 * without retrieving its data.
2822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @param rx_msg Address of the receive message descriptor.
2824 * @param buffer Address of the buffer to receive data, or NULL to discard
2825 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002826 *
2827 * @return N/A
2828 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002829extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002830
2831/**
2832 * @brief Retrieve mailbox message data into a memory pool block.
2833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * This routine completes the processing of a received message by retrieving
2835 * its data into a memory pool block, then disposing of the message.
2836 * The memory pool block that results from successful retrieval must be
2837 * returned to the pool once the data has been processed, even in cases
2838 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002839 *
2840 * Alternatively, this routine can be used to dispose of a received message
2841 * without retrieving its data. In this case there is no need to return a
2842 * memory pool block to the pool.
2843 *
2844 * This routine allocates a new memory pool block for the data only if the
2845 * data is not already in one. If a new block cannot be allocated, the routine
2846 * returns a failure code and the received message is left unchanged. This
2847 * permits the caller to reattempt data retrieval at a later time or to dispose
2848 * of the received message without retrieving its data.
2849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @param rx_msg Address of a receive message descriptor.
2851 * @param pool Address of memory pool, or NULL to discard data.
2852 * @param block Address of the area to hold memory pool block info.
2853 * @param timeout Waiting period to wait for a memory pool block (in
2854 * milliseconds), or one of the special values K_NO_WAIT
2855 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002856 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002857 * @retval 0 Data retrieved.
2858 * @retval -ENOMEM Returned without waiting.
2859 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002860 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002861extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002862 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002863 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864
Allan Stephensc98da842016-11-11 15:45:03 -05002865/**
2866 * @} end defgroup mailbox_apis
2867 */
2868
2869/**
2870 * @cond INTERNAL_HIDDEN
2871 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872
2873struct k_pipe {
2874 unsigned char *buffer; /* Pipe buffer: may be NULL */
2875 size_t size; /* Buffer size */
2876 size_t bytes_used; /* # bytes used in buffer */
2877 size_t read_index; /* Where in buffer to read from */
2878 size_t write_index; /* Where in buffer to write */
2879
2880 struct {
2881 _wait_q_t readers; /* Reader wait queue */
2882 _wait_q_t writers; /* Writer wait queue */
2883 } wait_q;
2884
Anas Nashif2f203c22016-12-18 06:57:45 -05002885 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002886};
2887
Peter Mitsise5d9c582016-10-14 14:44:57 -04002888#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889 { \
2890 .buffer = pipe_buffer, \
2891 .size = pipe_buffer_size, \
2892 .bytes_used = 0, \
2893 .read_index = 0, \
2894 .write_index = 0, \
2895 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2896 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002897 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898 }
2899
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900/**
Allan Stephensc98da842016-11-11 15:45:03 -05002901 * INTERNAL_HIDDEN @endcond
2902 */
2903
2904/**
2905 * @defgroup pipe_apis Pipe APIs
2906 * @ingroup kernel_apis
2907 * @{
2908 */
2909
2910/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002914 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002915 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * @param name Name of the pipe.
2918 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2919 * or zero if no ring buffer is used.
2920 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002922#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2923 static unsigned char __noinit __aligned(pipe_align) \
2924 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002925 struct k_pipe name \
2926 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002927 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * This routine initializes a pipe object, prior to its first use.
2933 *
2934 * @param pipe Address of the pipe.
2935 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2936 * is used.
2937 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2938 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939 *
2940 * @return N/A
2941 */
2942extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2943 size_t size);
2944
2945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @param pipe Address of the pipe.
2951 * @param data Address of data to write.
2952 * @param bytes_to_write Size of data (in bytes).
2953 * @param bytes_written Address of area to hold the number of bytes written.
2954 * @param min_xfer Minimum number of bytes to write.
2955 * @param timeout Waiting period to wait for the data to be written (in
2956 * milliseconds), or one of the special values K_NO_WAIT
2957 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002958 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002959 * @retval 0 At least @a min_xfer bytes of data were written.
2960 * @retval -EIO Returned without waiting; zero data bytes were written.
2961 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002964extern int k_pipe_put(struct k_pipe *pipe, void *data,
2965 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002966 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002967
2968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @param pipe Address of the pipe.
2974 * @param data Address to place the data read from pipe.
2975 * @param bytes_to_read Maximum number of data bytes to read.
2976 * @param bytes_read Address of area to hold the number of bytes read.
2977 * @param min_xfer Minimum number of data bytes to read.
2978 * @param timeout Waiting period to wait for the data to be read (in
2979 * milliseconds), or one of the special values K_NO_WAIT
2980 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002982 * @retval 0 At least @a min_xfer bytes of data were read.
2983 * @retval -EIO Returned without waiting; zero data bytes were read.
2984 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002987extern int k_pipe_get(struct k_pipe *pipe, void *data,
2988 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002989 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990
2991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * This routine writes the data contained in a memory block to @a pipe.
2995 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 * @param block Memory block containing data to send
3000 * @param size Number of data bytes in memory block to send
3001 * @param sem Semaphore to signal upon completion (else NULL)
3002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003003 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004 */
3005extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3006 size_t size, struct k_sem *sem);
3007
3008/**
Allan Stephensc98da842016-11-11 15:45:03 -05003009 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010 */
3011
Allan Stephensc98da842016-11-11 15:45:03 -05003012/**
3013 * @cond INTERNAL_HIDDEN
3014 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003016struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003017 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003018 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003019 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020 char *buffer;
3021 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003022 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023
Anas Nashif2f203c22016-12-18 06:57:45 -05003024 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025};
3026
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003027#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
3028 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029 { \
3030 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003031 .num_blocks = slab_num_blocks, \
3032 .block_size = slab_block_size, \
3033 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003034 .free_list = NULL, \
3035 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003036 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037 }
3038
Peter Mitsis578f9112016-10-07 13:50:31 -04003039/**
Allan Stephensc98da842016-11-11 15:45:03 -05003040 * INTERNAL_HIDDEN @endcond
3041 */
3042
3043/**
3044 * @defgroup mem_slab_apis Memory Slab APIs
3045 * @ingroup kernel_apis
3046 * @{
3047 */
3048
3049/**
Allan Stephensda827222016-11-09 14:23:58 -06003050 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003051 *
Allan Stephensda827222016-11-09 14:23:58 -06003052 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003054 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3055 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003056 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003057 *
Allan Stephensda827222016-11-09 14:23:58 -06003058 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003060 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003061 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @param name Name of the memory slab.
3064 * @param slab_block_size Size of each memory block (in bytes).
3065 * @param slab_num_blocks Number memory blocks.
3066 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003067 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003068#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3069 char __noinit __aligned(slab_align) \
3070 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3071 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003072 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003073 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
3074 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003076/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003077 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003080 *
Allan Stephensda827222016-11-09 14:23:58 -06003081 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3082 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3083 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3084 * To ensure that each memory block is similarly aligned to this boundary,
3085 * @a slab_block_size must also be a multiple of N.
3086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003087 * @param slab Address of the memory slab.
3088 * @param buffer Pointer to buffer used for the memory blocks.
3089 * @param block_size Size of each memory block (in bytes).
3090 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003091 *
3092 * @return N/A
3093 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003094extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003095 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003096
3097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * @param slab Address of the memory slab.
3103 * @param mem Pointer to block address area.
3104 * @param timeout Maximum time to wait for operation to complete
3105 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3106 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003107 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003108 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003110 * @retval -ENOMEM Returned without waiting.
3111 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003112 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003113extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003114 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003115
3116/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * This routine releases a previously allocated memory block back to its
3120 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * @param slab Address of the memory slab.
3123 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003124 *
3125 * @return N/A
3126 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003127extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003128
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003129/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003132 * This routine gets the number of memory blocks that are currently
3133 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003138 */
Kumar Galacc334c72017-04-21 10:55:34 -05003139static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003141 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003142}
3143
Peter Mitsisc001aa82016-10-13 13:53:37 -04003144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003145 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * This routine gets the number of memory blocks that are currently
3148 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003153 */
Kumar Galacc334c72017-04-21 10:55:34 -05003154static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003155{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003156 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003157}
3158
Allan Stephensc98da842016-11-11 15:45:03 -05003159/**
3160 * @} end defgroup mem_slab_apis
3161 */
3162
3163/**
3164 * @cond INTERNAL_HIDDEN
3165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166
Andy Ross73cb9582017-05-09 10:42:39 -07003167struct k_mem_pool_lvl {
3168 union {
3169 u32_t *bits_p;
3170 u32_t bits;
3171 };
3172 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003173};
3174
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003175struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003176 void *buf;
3177 size_t max_sz;
3178 u16_t n_max;
3179 u8_t n_levels;
3180 u8_t max_inline_level;
3181 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003182 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183};
3184
Andy Ross73cb9582017-05-09 10:42:39 -07003185#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186
Andy Ross73cb9582017-05-09 10:42:39 -07003187#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3188
3189#define _MPOOL_LVLS(maxsz, minsz) \
3190 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3191 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3192 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3193 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3194 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3195 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3196 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3197 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3198 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3199 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3200 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3201 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3202 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3203 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3204 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3205 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3206
3207/* Rounds the needed bits up to integer multiples of u32_t */
3208#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3209 ((((n_max) << (2*(l))) + 31) / 32)
3210
3211/* One word gets stored free unioned with the pointer, otherwise the
3212 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003213 */
Andy Ross73cb9582017-05-09 10:42:39 -07003214#define _MPOOL_LBIT_WORDS(n_max, l) \
3215 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3216 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003217
Andy Ross73cb9582017-05-09 10:42:39 -07003218/* How many bytes for the bitfields of a single level? */
3219#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3220 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3221 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003222
Andy Ross73cb9582017-05-09 10:42:39 -07003223/* Size of the bitmap array that follows the buffer in allocated memory */
3224#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3225 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3226 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3227 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3228 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3229 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3230 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3231 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3232 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3233 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3234 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3235 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3236 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3237 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3238 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3239 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3240 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003241
3242/**
Allan Stephensc98da842016-11-11 15:45:03 -05003243 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003244 */
3245
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003246/**
Allan Stephensc98da842016-11-11 15:45:03 -05003247 * @addtogroup mem_pool_apis
3248 * @{
3249 */
3250
3251/**
3252 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3255 * long. The memory pool allows blocks to be repeatedly partitioned into
3256 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003257 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003258 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 * If the pool is to be accessed outside the module where it is defined, it
3260 * can be declared via
3261 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003262 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003265 * @param minsz Size of the smallest blocks in the pool (in bytes).
3266 * @param maxsz Size of the largest blocks in the pool (in bytes).
3267 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003269 */
Andy Ross73cb9582017-05-09 10:42:39 -07003270#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3271 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3272 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3273 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3274 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3275 .buf = _mpool_buf_##name, \
3276 .max_sz = maxsz, \
3277 .n_max = nmax, \
3278 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3279 .levels = _mpool_lvls_##name, \
3280 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003281
Peter Mitsis937042c2016-10-13 13:18:26 -04003282/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003283 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * @param pool Address of the memory pool.
3288 * @param block Pointer to block descriptor for the allocated memory.
3289 * @param size Amount of memory to allocate (in bytes).
3290 * @param timeout Maximum time to wait for operation to complete
3291 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3292 * or K_FOREVER to wait as long as necessary.
3293 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003294 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003296 * @retval -ENOMEM Returned without waiting.
3297 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003298 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003299extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003300 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003301
3302/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * This routine releases a previously allocated memory block back to its
3306 * memory pool.
3307 *
3308 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003309 *
3310 * @return N/A
3311 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003313
3314/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003316 *
Andy Ross73cb9582017-05-09 10:42:39 -07003317 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 *
Andy Ross73cb9582017-05-09 10:42:39 -07003319 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003320 *
3321 * @return N/A
3322 */
Andy Ross73cb9582017-05-09 10:42:39 -07003323static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003324
3325/**
Allan Stephensc98da842016-11-11 15:45:03 -05003326 * @} end addtogroup mem_pool_apis
3327 */
3328
3329/**
3330 * @defgroup heap_apis Heap Memory Pool APIs
3331 * @ingroup kernel_apis
3332 * @{
3333 */
3334
3335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003339 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003344 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003345extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003346
3347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003349 *
3350 * This routine provides traditional free() semantics. The memory being
3351 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003352 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003353 * If @a ptr is NULL, no operation is performed.
3354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003356 *
3357 * @return N/A
3358 */
3359extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360
Allan Stephensc98da842016-11-11 15:45:03 -05003361/**
3362 * @} end defgroup heap_apis
3363 */
3364
Benjamin Walshacc68c12017-01-29 18:57:45 -05003365/* polling API - PRIVATE */
3366
Benjamin Walshb0179862017-02-02 16:39:57 -05003367#ifdef CONFIG_POLL
3368#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3369#else
3370#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3371#endif
3372
Benjamin Walshacc68c12017-01-29 18:57:45 -05003373/* private - implementation data created as needed, per-type */
3374struct _poller {
3375 struct k_thread *thread;
3376};
3377
3378/* private - types bit positions */
3379enum _poll_types_bits {
3380 /* can be used to ignore an event */
3381 _POLL_TYPE_IGNORE,
3382
3383 /* to be signaled by k_poll_signal() */
3384 _POLL_TYPE_SIGNAL,
3385
3386 /* semaphore availability */
3387 _POLL_TYPE_SEM_AVAILABLE,
3388
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003389 /* queue/fifo/lifo data availability */
3390 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003391
3392 _POLL_NUM_TYPES
3393};
3394
3395#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3396
3397/* private - states bit positions */
3398enum _poll_states_bits {
3399 /* default state when creating event */
3400 _POLL_STATE_NOT_READY,
3401
3402 /* there was another poller already on the object */
3403 _POLL_STATE_EADDRINUSE,
3404
3405 /* signaled by k_poll_signal() */
3406 _POLL_STATE_SIGNALED,
3407
3408 /* semaphore is available */
3409 _POLL_STATE_SEM_AVAILABLE,
3410
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003411 /* data is available to read on queue/fifo/lifo */
3412 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003413
3414 _POLL_NUM_STATES
3415};
3416
3417#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3418
3419#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003420 (32 - (0 \
3421 + 8 /* tag */ \
3422 + _POLL_NUM_TYPES \
3423 + _POLL_NUM_STATES \
3424 + 1 /* modes */ \
3425 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003426
3427#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3428#error overflow of 32-bit word in struct k_poll_event
3429#endif
3430
3431/* end of polling API - PRIVATE */
3432
3433
3434/**
3435 * @defgroup poll_apis Async polling APIs
3436 * @ingroup kernel_apis
3437 * @{
3438 */
3439
3440/* Public polling API */
3441
3442/* public - values for k_poll_event.type bitfield */
3443#define K_POLL_TYPE_IGNORE 0
3444#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3445#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003446#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3447#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003448
3449/* public - polling modes */
3450enum k_poll_modes {
3451 /* polling thread does not take ownership of objects when available */
3452 K_POLL_MODE_NOTIFY_ONLY = 0,
3453
3454 K_POLL_NUM_MODES
3455};
3456
3457/* public - values for k_poll_event.state bitfield */
3458#define K_POLL_STATE_NOT_READY 0
3459#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3460#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3461#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003462#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3463#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003464
3465/* public - poll signal object */
3466struct k_poll_signal {
3467 /* PRIVATE - DO NOT TOUCH */
3468 struct k_poll_event *poll_event;
3469
3470 /*
3471 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3472 * user resets it to 0.
3473 */
3474 unsigned int signaled;
3475
3476 /* custom result value passed to k_poll_signal() if needed */
3477 int result;
3478};
3479
3480#define K_POLL_SIGNAL_INITIALIZER() \
3481 { \
3482 .poll_event = NULL, \
3483 .signaled = 0, \
3484 .result = 0, \
3485 }
3486
3487struct k_poll_event {
3488 /* PRIVATE - DO NOT TOUCH */
3489 struct _poller *poller;
3490
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003491 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003492 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003493
Benjamin Walshacc68c12017-01-29 18:57:45 -05003494 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003495 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003496
3497 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003498 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003499
3500 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003501 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003502
3503 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003504 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003505
3506 /* per-type data */
3507 union {
3508 void *obj;
3509 struct k_poll_signal *signal;
3510 struct k_sem *sem;
3511 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003512 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003513 };
3514};
3515
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003516#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003517 { \
3518 .poller = NULL, \
3519 .type = event_type, \
3520 .state = K_POLL_STATE_NOT_READY, \
3521 .mode = event_mode, \
3522 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003523 { .obj = event_obj }, \
3524 }
3525
3526#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3527 event_tag) \
3528 { \
3529 .type = event_type, \
3530 .tag = event_tag, \
3531 .state = K_POLL_STATE_NOT_READY, \
3532 .mode = event_mode, \
3533 .unused = 0, \
3534 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003535 }
3536
3537/**
3538 * @brief Initialize one struct k_poll_event instance
3539 *
3540 * After this routine is called on a poll event, the event it ready to be
3541 * placed in an event array to be passed to k_poll().
3542 *
3543 * @param event The event to initialize.
3544 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3545 * values. Only values that apply to the same object being polled
3546 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3547 * event.
3548 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3549 * @param obj Kernel object or poll signal.
3550 *
3551 * @return N/A
3552 */
3553
Kumar Galacc334c72017-04-21 10:55:34 -05003554extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003555 int mode, void *obj);
3556
3557/**
3558 * @brief Wait for one or many of multiple poll events to occur
3559 *
3560 * This routine allows a thread to wait concurrently for one or many of
3561 * multiple poll events to have occurred. Such events can be a kernel object
3562 * being available, like a semaphore, or a poll signal event.
3563 *
3564 * When an event notifies that a kernel object is available, the kernel object
3565 * is not "given" to the thread calling k_poll(): it merely signals the fact
3566 * that the object was available when the k_poll() call was in effect. Also,
3567 * all threads trying to acquire an object the regular way, i.e. by pending on
3568 * the object, have precedence over the thread polling on the object. This
3569 * means that the polling thread will never get the poll event on an object
3570 * until the object becomes available and its pend queue is empty. For this
3571 * reason, the k_poll() call is more effective when the objects being polled
3572 * only have one thread, the polling thread, trying to acquire them.
3573 *
3574 * Only one thread can be polling for a particular object at a given time. If
3575 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3576 * and returns as soon as it has finished handling the other events. This means
3577 * that k_poll() can return -EADDRINUSE and have the state value of some events
3578 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3579 * parameter is ignored.
3580 *
3581 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3582 * events that were passed to k_poll() and check the state field for the values
3583 * that were expected and take the associated actions.
3584 *
3585 * Before being reused for another call to k_poll(), the user has to reset the
3586 * state field to K_POLL_STATE_NOT_READY.
3587 *
3588 * @param events An array of pointers to events to be polled for.
3589 * @param num_events The number of events in the array.
3590 * @param timeout Waiting period for an event to be ready (in milliseconds),
3591 * or one of the special values K_NO_WAIT and K_FOREVER.
3592 *
3593 * @retval 0 One or more events are ready.
3594 * @retval -EADDRINUSE One or more objects already had a poller.
3595 * @retval -EAGAIN Waiting period timed out.
3596 */
3597
3598extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003599 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003600
3601/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003602 * @brief Initialize a poll signal object.
3603 *
3604 * Ready a poll signal object to be signaled via k_poll_signal().
3605 *
3606 * @param signal A poll signal.
3607 *
3608 * @return N/A
3609 */
3610
3611extern void k_poll_signal_init(struct k_poll_signal *signal);
3612
3613/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003614 * @brief Signal a poll signal object.
3615 *
3616 * This routine makes ready a poll signal, which is basically a poll event of
3617 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3618 * made ready to run. A @a result value can be specified.
3619 *
3620 * The poll signal contains a 'signaled' field that, when set by
3621 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3622 * be reset by the user before being passed again to k_poll() or k_poll() will
3623 * consider it being signaled, and will return immediately.
3624 *
3625 * @param signal A poll signal.
3626 * @param result The value to store in the result field of the signal.
3627 *
3628 * @retval 0 The signal was delivered successfully.
3629 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3630 */
3631
3632extern int k_poll_signal(struct k_poll_signal *signal, int result);
3633
3634/* private internal function */
3635extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003636 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003637
3638/**
3639 * @} end defgroup poll_apis
3640 */
3641
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003642/**
3643 * @brief Make the CPU idle.
3644 *
3645 * This function makes the CPU idle until an event wakes it up.
3646 *
3647 * In a regular system, the idle thread should be the only thread responsible
3648 * for making the CPU idle and triggering any type of power management.
3649 * However, in some more constrained systems, such as a single-threaded system,
3650 * the only thread would be responsible for this if needed.
3651 *
3652 * @return N/A
3653 */
3654extern void k_cpu_idle(void);
3655
3656/**
3657 * @brief Make the CPU idle in an atomic fashion.
3658 *
3659 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3660 * must be done atomically before making the CPU idle.
3661 *
3662 * @param key Interrupt locking key obtained from irq_lock().
3663 *
3664 * @return N/A
3665 */
3666extern void k_cpu_atomic_idle(unsigned int key);
3667
Kumar Galacc334c72017-04-21 10:55:34 -05003668extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003669
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670#include <arch/cpu.h>
3671
Andrew Boiecdb94d62017-04-18 15:22:05 -07003672#ifdef _ARCH_EXCEPT
3673/* This archtecture has direct support for triggering a CPU exception */
3674#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3675#else
3676
3677#include <misc/printk.h>
3678
3679/* NOTE: This is the implementation for arches that do not implement
3680 * _ARCH_EXCEPT() to generate a real CPU exception.
3681 *
3682 * We won't have a real exception frame to determine the PC value when
3683 * the oops occurred, so print file and line number before we jump into
3684 * the fatal error handler.
3685 */
3686#define _k_except_reason(reason) do { \
3687 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3688 _NanoFatalErrorHandler(reason, &_default_esf); \
3689 CODE_UNREACHABLE; \
3690 } while (0)
3691
3692#endif /* _ARCH__EXCEPT */
3693
3694/**
3695 * @brief Fatally terminate a thread
3696 *
3697 * This should be called when a thread has encountered an unrecoverable
3698 * runtime condition and needs to terminate. What this ultimately
3699 * means is determined by the _fatal_error_handler() implementation, which
3700 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3701 *
3702 * If this is called from ISR context, the default system fatal error handler
3703 * will treat it as an unrecoverable system error, just like k_panic().
3704 */
3705#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3706
3707/**
3708 * @brief Fatally terminate the system
3709 *
3710 * This should be called when the Zephyr kernel has encountered an
3711 * unrecoverable runtime condition and needs to terminate. What this ultimately
3712 * means is determined by the _fatal_error_handler() implementation, which
3713 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3714 */
3715#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3716
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003717/*
3718 * private APIs that are utilized by one or more public APIs
3719 */
3720
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003721#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003722extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003723#else
3724#define _init_static_threads() do { } while ((0))
3725#endif
3726
3727extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003728extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729
3730#ifdef __cplusplus
3731}
3732#endif
3733
Andrew Boiee004dec2016-11-07 09:01:19 -08003734#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3735/*
3736 * Define new and delete operators.
3737 * At this moment, the operators do nothing since objects are supposed
3738 * to be statically allocated.
3739 */
3740inline void operator delete(void *ptr)
3741{
3742 (void)ptr;
3743}
3744
3745inline void operator delete[](void *ptr)
3746{
3747 (void)ptr;
3748}
3749
3750inline void *operator new(size_t size)
3751{
3752 (void)size;
3753 return NULL;
3754}
3755
3756inline void *operator new[](size_t size)
3757{
3758 (void)size;
3759 return NULL;
3760}
3761
3762/* Placement versions of operator new and delete */
3763inline void operator delete(void *ptr1, void *ptr2)
3764{
3765 (void)ptr1;
3766 (void)ptr2;
3767}
3768
3769inline void operator delete[](void *ptr1, void *ptr2)
3770{
3771 (void)ptr1;
3772 (void)ptr2;
3773}
3774
3775inline void *operator new(size_t size, void *ptr)
3776{
3777 (void)size;
3778 return ptr;
3779}
3780
3781inline void *operator new[](size_t size, void *ptr)
3782{
3783 (void)size;
3784 return ptr;
3785}
3786
3787#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3788
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003789#endif /* !_ASMLANGUAGE */
3790
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791#endif /* _kernel__h_ */