blob: ee04edd73e6127530eb427c8ccfb989059c64ea8 [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};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700212
213typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700214#endif /* CONFIG_THREAD_STACK_INFO */
215
216struct k_thread {
217
218 struct _thread_base base;
219
220 /* defined by the architecture, but all archs need these */
221 struct _caller_saved caller_saved;
222 struct _callee_saved callee_saved;
223
224 /* static thread init data */
225 void *init_data;
226
227 /* abort function */
228 void (*fn_abort)(void);
229
230#if defined(CONFIG_THREAD_MONITOR)
231 /* thread entry and parameters description */
232 struct __thread_entry *entry;
233
234 /* next item in list of all threads */
235 struct k_thread *next_thread;
236#endif
237
238#ifdef CONFIG_THREAD_CUSTOM_DATA
239 /* crude thread-local storage */
240 void *custom_data;
241#endif
242
243#ifdef CONFIG_ERRNO
244 /* per-thread errno variable */
245 int errno_var;
246#endif
247
248#if defined(CONFIG_THREAD_STACK_INFO)
249 /* Stack Info */
250 struct _thread_stack_info stack_info;
251#endif /* CONFIG_THREAD_STACK_INFO */
252
253 /* arch-specifics: must always be at the end */
254 struct _thread_arch arch;
255};
256
257typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400258typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700259#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261enum execution_context_types {
262 K_ISR = 0,
263 K_COOP_THREAD,
264 K_PREEMPT_THREAD,
265};
266
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100268 * @defgroup profiling_apis Profiling APIs
269 * @ingroup kernel_apis
270 * @{
271 */
272
273/**
274 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
275 *
276 * This routine calls @ref stack_analyze on the 4 call stacks declared and
277 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
278 *
279 * CONFIG_MAIN_STACK_SIZE
280 * CONFIG_IDLE_STACK_SIZE
281 * CONFIG_ISR_STACK_SIZE
282 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
283 *
284 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
285 * produce output.
286 *
287 * @return N/A
288 */
289extern void k_call_stacks_analyze(void);
290
291/**
292 * @} end defgroup profiling_apis
293 */
294
295/**
Allan Stephensc98da842016-11-11 15:45:03 -0500296 * @defgroup thread_apis Thread APIs
297 * @ingroup kernel_apis
298 * @{
299 */
300
301/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500302 * @typedef k_thread_entry_t
303 * @brief Thread entry point function type.
304 *
305 * A thread's entry point function is invoked when the thread starts executing.
306 * Up to 3 argument values can be passed to the function.
307 *
308 * The thread terminates execution permanently if the entry point function
309 * returns. The thread is responsible for releasing any shared resources
310 * it may own (such as mutexes and dynamically allocated memory), prior to
311 * returning.
312 *
313 * @param p1 First argument.
314 * @param p2 Second argument.
315 * @param p3 Third argument.
316 *
317 * @return N/A
318 */
319typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
320
Benjamin Walshed240f22017-01-22 13:05:08 -0500321#endif /* !_ASMLANGUAGE */
322
323
324/*
325 * Thread user options. May be needed by assembly code. Common part uses low
326 * bits, arch-specific use high bits.
327 */
328
329/* system thread that must not abort */
330#define K_ESSENTIAL (1 << 0)
331
332#if defined(CONFIG_FP_SHARING)
333/* thread uses floating point registers */
334#define K_FP_REGS (1 << 1)
335#endif
336
337#ifdef CONFIG_X86
338/* x86 Bitmask definitions for threads user options */
339
340#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
341/* thread uses SSEx (and also FP) registers */
342#define K_SSE_REGS (1 << 7)
343#endif
344#endif
345
346/* end - thread options */
347
348#if !defined(_ASMLANGUAGE)
349
Allan Stephens5eceb852016-11-16 10:16:30 -0500350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500355 * The new thread may be scheduled for immediate execution or a delayed start.
356 * If the newly spawned thread does not have a delayed start the kernel
357 * scheduler may preempt the current thread to allow the new thread to
358 * execute.
359 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700360 * Kernel data structures for bookkeeping and context storage for this thread
361 * will be placed at the beginning of the thread's stack memory region and may
362 * become corrupted if too much of the stack is used. This function has been
363 * deprecated in favor of k_thread_create() to give the user more control on
364 * where these data structures reside.
365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500366 * Thread options are architecture-specific, and can include K_ESSENTIAL,
367 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
368 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400369 *
370 * @param stack Pointer to the stack space.
371 * @param stack_size Stack size in bytes.
372 * @param entry Thread entry function.
373 * @param p1 1st entry point parameter.
374 * @param p2 2nd entry point parameter.
375 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500376 * @param prio Thread priority.
377 * @param options Thread options.
378 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500380 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400381 */
Andrew Boied26cf2d2017-03-30 13:07:02 -0700382extern __deprecated k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500383 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500385 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400386
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700388 * @brief Create a thread.
389 *
390 * This routine initializes a thread, then schedules it for execution.
391 *
392 * The new thread may be scheduled for immediate execution or a delayed start.
393 * If the newly spawned thread does not have a delayed start the kernel
394 * scheduler may preempt the current thread to allow the new thread to
395 * execute.
396 *
397 * Thread options are architecture-specific, and can include K_ESSENTIAL,
398 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
399 * them using "|" (the logical OR operator).
400 *
401 * Historically, users often would use the beginning of the stack memory region
402 * to store the struct k_thread data, although corruption will occur if the
403 * stack overflows this region and stack protection features may not detect this
404 * situation.
405 *
406 * @param new_thread Pointer to uninitialized struct k_thread
407 * @param stack Pointer to the stack space.
408 * @param stack_size Stack size in bytes.
409 * @param entry Thread entry function.
410 * @param p1 1st entry point parameter.
411 * @param p2 2nd entry point parameter.
412 * @param p3 3rd entry point parameter.
413 * @param prio Thread priority.
414 * @param options Thread options.
415 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
416 *
417 * @return ID of new thread.
418 */
419extern k_tid_t k_thread_create(struct k_thread *new_thread, char *stack,
420 size_t stack_size,
421 void (*entry)(void *, void *, void*),
422 void *p1, void *p2, void *p3,
423 int prio, u32_t options, s32_t delay);
424
425/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500426 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400427 *
Allan Stephensc98da842016-11-11 15:45:03 -0500428 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500429 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432 *
433 * @return N/A
434 */
Kumar Galacc334c72017-04-21 10:55:34 -0500435extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400436
437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500438 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400439 *
440 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500441 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400443 * @return N/A
444 */
Kumar Galacc334c72017-04-21 10:55:34 -0500445extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400446
447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500448 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500450 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 *
454 * @return N/A
455 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400456extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400457
458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500459 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500461 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500463 * If @a thread is not currently sleeping, the routine has no effect.
464 *
465 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400466 *
467 * @return N/A
468 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400469extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400470
471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500472 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400476extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477
478/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500481 * This routine prevents @a thread from executing if it has not yet started
482 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * @param thread ID of thread to cancel.
485 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700486 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500487 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400489extern int k_thread_cancel(k_tid_t thread);
490
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491/**
Allan Stephensc98da842016-11-11 15:45:03 -0500492 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500494 * This routine permanently stops execution of @a thread. The thread is taken
495 * off all kernel queues it is part of (i.e. the ready queue, the timeout
496 * queue, or a kernel object wait queue). However, any kernel resources the
497 * thread might currently own (such as mutexes or memory blocks) are not
498 * released. It is the responsibility of the caller of this routine to ensure
499 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500501 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400502 *
503 * @return N/A
504 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400505extern void k_thread_abort(k_tid_t thread);
506
Allan Stephensc98da842016-11-11 15:45:03 -0500507/**
508 * @cond INTERNAL_HIDDEN
509 */
510
Benjamin Walshd211a522016-12-06 11:44:01 -0500511/* timeout has timed out and is not on _timeout_q anymore */
512#define _EXPIRED (-2)
513
514/* timeout is not in use */
515#define _INACTIVE (-1)
516
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400517struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700518 struct k_thread *init_thread;
519 char *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400520 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500521 void (*init_entry)(void *, void *, void *);
522 void *init_p1;
523 void *init_p2;
524 void *init_p3;
525 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500526 u32_t init_options;
527 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500528 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500529 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400530};
531
Andrew Boied26cf2d2017-03-30 13:07:02 -0700532#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400533 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500534 prio, options, delay, abort, groups) \
535 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700536 .init_thread = (thread), \
537 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500538 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400539 .init_entry = (void (*)(void *, void *, void *))entry, \
540 .init_p1 = (void *)p1, \
541 .init_p2 = (void *)p2, \
542 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500543 .init_prio = (prio), \
544 .init_options = (options), \
545 .init_delay = (delay), \
546 .init_abort = (abort), \
547 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400548 }
549
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400550/**
Allan Stephensc98da842016-11-11 15:45:03 -0500551 * INTERNAL_HIDDEN @endcond
552 */
553
554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500555 * @brief Statically define and initialize a thread.
556 *
557 * The thread may be scheduled for immediate execution or a delayed start.
558 *
559 * Thread options are architecture-specific, and can include K_ESSENTIAL,
560 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
561 * them using "|" (the logical OR operator).
562 *
563 * The ID of the thread can be accessed using:
564 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500565 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500566 *
567 * @param name Name of the thread.
568 * @param stack_size Stack size in bytes.
569 * @param entry Thread entry function.
570 * @param p1 1st entry point parameter.
571 * @param p2 2nd entry point parameter.
572 * @param p3 3rd entry point parameter.
573 * @param prio Thread priority.
574 * @param options Thread options.
575 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400576 *
577 * @internal It has been observed that the x86 compiler by default aligns
578 * these _static_thread_data structures to 32-byte boundaries, thereby
579 * wasting space. To work around this, force a 4-byte alignment.
580 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500581#define K_THREAD_DEFINE(name, stack_size, \
582 entry, p1, p2, p3, \
583 prio, options, delay) \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700584 char __noinit __stack _k_thread_stack_##name[stack_size]; \
585 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500586 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500587 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700588 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
589 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500590 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700591 NULL, 0); \
592 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500595 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500599 * @param thread ID of thread whose priority is needed.
600 *
601 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500603extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604
605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500606 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500608 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609 *
610 * Rescheduling can occur immediately depending on the priority @a thread is
611 * set to:
612 *
613 * - If its priority is raised above the priority of the caller of this
614 * function, and the caller is preemptible, @a thread will be scheduled in.
615 *
616 * - If the caller operates on itself, it lowers its priority below that of
617 * other threads in the system, and the caller is preemptible, the thread of
618 * highest priority will be scheduled in.
619 *
620 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
621 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
622 * highest priority.
623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500624 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625 * @param prio New priority.
626 *
627 * @warning Changing the priority of a thread currently involved in mutex
628 * priority inheritance may result in undefined behavior.
629 *
630 * @return N/A
631 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400632extern void k_thread_priority_set(k_tid_t thread, int prio);
633
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500635 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500637 * This routine prevents the kernel scheduler from making @a thread the
638 * current thread. All other internal operations on @a thread are still
639 * performed; for example, any timeout it is waiting on keeps ticking,
640 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * If @a thread is already suspended, the routine has no effect.
643 *
644 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400645 *
646 * @return N/A
647 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400648extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400649
650/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500651 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500653 * This routine allows the kernel scheduler to make @a thread the current
654 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * If @a thread is not currently suspended, the routine has no effect.
657 *
658 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400659 *
660 * @return N/A
661 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400662extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400663
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500665 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500667 * This routine specifies how the scheduler will perform time slicing of
668 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * To enable time slicing, @a slice must be non-zero. The scheduler
671 * ensures that no thread runs for more than the specified time limit
672 * before other threads of that priority are given a chance to execute.
673 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700674 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500676 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677 * execute. Once the scheduler selects a thread for execution, there is no
678 * minimum guaranteed time the thread will execute before threads of greater or
679 * equal priority are scheduled.
680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 * for execution, this routine has no effect; the thread is immediately
683 * rescheduled after the slice period expires.
684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * To disable timeslicing, set both @a slice and @a prio to zero.
686 *
687 * @param slice Maximum time slice length (in milliseconds).
688 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689 *
690 * @return N/A
691 */
Kumar Galacc334c72017-04-21 10:55:34 -0500692extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400693
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694/**
Allan Stephensc98da842016-11-11 15:45:03 -0500695 * @} end defgroup thread_apis
696 */
697
698/**
699 * @addtogroup isr_apis
700 * @{
701 */
702
703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500704 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400705 *
Allan Stephensc98da842016-11-11 15:45:03 -0500706 * This routine allows the caller to customize its actions, depending on
707 * whether it is a thread or an ISR.
708 *
709 * @note Can be called by ISRs.
710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500711 * @return 0 if invoked by a thread.
712 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400713 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500714extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400715
Benjamin Walsh445830d2016-11-10 15:54:27 -0500716/**
717 * @brief Determine if code is running in a preemptible thread.
718 *
Allan Stephensc98da842016-11-11 15:45:03 -0500719 * This routine allows the caller to customize its actions, depending on
720 * whether it can be preempted by another thread. The routine returns a 'true'
721 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500722 *
Allan Stephensc98da842016-11-11 15:45:03 -0500723 * - The code is running in a thread, not at ISR.
724 * - The thread's priority is in the preemptible range.
725 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500726 *
Allan Stephensc98da842016-11-11 15:45:03 -0500727 * @note Can be called by ISRs.
728 *
729 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500730 * @return Non-zero if invoked by a preemptible thread.
731 */
732extern int k_is_preempt_thread(void);
733
Allan Stephensc98da842016-11-11 15:45:03 -0500734/**
735 * @} end addtogroup isr_apis
736 */
737
738/**
739 * @addtogroup thread_apis
740 * @{
741 */
742
743/**
744 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500745 *
Allan Stephensc98da842016-11-11 15:45:03 -0500746 * This routine prevents the current thread from being preempted by another
747 * thread by instructing the scheduler to treat it as a cooperative thread.
748 * If the thread subsequently performs an operation that makes it unready,
749 * it will be context switched out in the normal manner. When the thread
750 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500751 *
Allan Stephensc98da842016-11-11 15:45:03 -0500752 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500753 *
Allan Stephensc98da842016-11-11 15:45:03 -0500754 * @note k_sched_lock() and k_sched_unlock() should normally be used
755 * when the operation being performed can be safely interrupted by ISRs.
756 * However, if the amount of processing involved is very small, better
757 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500758 *
759 * @return N/A
760 */
761extern void k_sched_lock(void);
762
Allan Stephensc98da842016-11-11 15:45:03 -0500763/**
764 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500765 *
Allan Stephensc98da842016-11-11 15:45:03 -0500766 * This routine reverses the effect of a previous call to k_sched_lock().
767 * A thread must call the routine once for each time it called k_sched_lock()
768 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500769 *
770 * @return N/A
771 */
772extern void k_sched_unlock(void);
773
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500779 * Custom data is not used by the kernel itself, and is freely available
780 * for a thread to use as it sees fit. It can be used as a framework
781 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
785 * @return N/A
786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400787extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788
789/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400796extern void *k_thread_custom_data_get(void);
797
798/**
Allan Stephensc98da842016-11-11 15:45:03 -0500799 * @} end addtogroup thread_apis
800 */
801
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400802#include <sys_clock.h>
803
Allan Stephensc2f15a42016-11-17 12:24:22 -0500804/**
805 * @addtogroup clock_apis
806 * @{
807 */
808
809/**
810 * @brief Generate null timeout delay.
811 *
812 * This macro generates a timeout delay that that instructs a kernel API
813 * not to wait if the requested operation cannot be performed immediately.
814 *
815 * @return Timeout delay value.
816 */
817#define K_NO_WAIT 0
818
819/**
820 * @brief Generate timeout delay from milliseconds.
821 *
822 * This macro generates a timeout delay that that instructs a kernel API
823 * to wait up to @a ms milliseconds to perform the requested operation.
824 *
825 * @param ms Duration in milliseconds.
826 *
827 * @return Timeout delay value.
828 */
Johan Hedberg14471692016-11-13 10:52:15 +0200829#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500830
831/**
832 * @brief Generate timeout delay from seconds.
833 *
834 * This macro generates a timeout delay that that instructs a kernel API
835 * to wait up to @a s seconds to perform the requested operation.
836 *
837 * @param s Duration in seconds.
838 *
839 * @return Timeout delay value.
840 */
Johan Hedberg14471692016-11-13 10:52:15 +0200841#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500842
843/**
844 * @brief Generate timeout delay from minutes.
845 *
846 * This macro generates a timeout delay that that instructs a kernel API
847 * to wait up to @a m minutes to perform the requested operation.
848 *
849 * @param m Duration in minutes.
850 *
851 * @return Timeout delay value.
852 */
Johan Hedberg14471692016-11-13 10:52:15 +0200853#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500854
855/**
856 * @brief Generate timeout delay from hours.
857 *
858 * This macro generates a timeout delay that that instructs a kernel API
859 * to wait up to @a h hours to perform the requested operation.
860 *
861 * @param h Duration in hours.
862 *
863 * @return Timeout delay value.
864 */
Johan Hedberg14471692016-11-13 10:52:15 +0200865#define K_HOURS(h) K_MINUTES((h) * 60)
866
Allan Stephensc98da842016-11-11 15:45:03 -0500867/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500868 * @brief Generate infinite timeout delay.
869 *
870 * This macro generates a timeout delay that that instructs a kernel API
871 * to wait as long as necessary to perform the requested operation.
872 *
873 * @return Timeout delay value.
874 */
875#define K_FOREVER (-1)
876
877/**
878 * @} end addtogroup clock_apis
879 */
880
881/**
Allan Stephensc98da842016-11-11 15:45:03 -0500882 * @cond INTERNAL_HIDDEN
883 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400884
Benjamin Walsh62092182016-12-20 14:39:08 -0500885/* kernel clocks */
886
887#if (sys_clock_ticks_per_sec == 1000) || \
888 (sys_clock_ticks_per_sec == 500) || \
889 (sys_clock_ticks_per_sec == 250) || \
890 (sys_clock_ticks_per_sec == 125) || \
891 (sys_clock_ticks_per_sec == 100) || \
892 (sys_clock_ticks_per_sec == 50) || \
893 (sys_clock_ticks_per_sec == 25) || \
894 (sys_clock_ticks_per_sec == 20) || \
895 (sys_clock_ticks_per_sec == 10) || \
896 (sys_clock_ticks_per_sec == 1)
897
898 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
899#else
900 /* yields horrible 64-bit math on many architectures: try to avoid */
901 #define _NON_OPTIMIZED_TICKS_PER_SEC
902#endif
903
904#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500905extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500906#else
Kumar Galacc334c72017-04-21 10:55:34 -0500907static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500908{
Kumar Galacc334c72017-04-21 10:55:34 -0500909 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500910}
911#endif
912
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500913/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800914#ifdef CONFIG_TICKLESS_KERNEL
915#define _TICK_ALIGN 0
916#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500917#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800918#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500919
Kumar Galacc334c72017-04-21 10:55:34 -0500920static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400921{
Benjamin Walsh62092182016-12-20 14:39:08 -0500922#ifdef CONFIG_SYS_CLOCK_EXISTS
923
924#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500925 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400926#else
Kumar Galacc334c72017-04-21 10:55:34 -0500927 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500928#endif
929
930#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400931 __ASSERT(ticks == 0, "");
932 return 0;
933#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400934}
935
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400936struct k_timer {
937 /*
938 * _timeout structure must be first here if we want to use
939 * dynamic timer allocation. timeout.node is used in the double-linked
940 * list of free timers
941 */
942 struct _timeout timeout;
943
Allan Stephens45bfa372016-10-12 12:39:42 -0500944 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945 _wait_q_t wait_q;
946
947 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500948 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949
950 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500951 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400952
953 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500954 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400955
Allan Stephens45bfa372016-10-12 12:39:42 -0500956 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500957 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400958
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500959 /* user-specific data, also used to support legacy features */
960 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400961
Anas Nashif2f203c22016-12-18 06:57:45 -0500962 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400963};
964
Allan Stephens1342adb2016-11-03 13:54:53 -0500965#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500967 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500968 .timeout.wait_q = NULL, \
969 .timeout.thread = NULL, \
970 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500972 .expiry_fn = expiry, \
973 .stop_fn = stop, \
974 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500975 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500976 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977 }
978
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979/**
Allan Stephensc98da842016-11-11 15:45:03 -0500980 * INTERNAL_HIDDEN @endcond
981 */
982
983/**
984 * @defgroup timer_apis Timer APIs
985 * @ingroup kernel_apis
986 * @{
987 */
988
989/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500990 * @typedef k_timer_expiry_t
991 * @brief Timer expiry function type.
992 *
993 * A timer's expiry function is executed by the system clock interrupt handler
994 * each time the timer expires. The expiry function is optional, and is only
995 * invoked if the timer has been initialized with one.
996 *
997 * @param timer Address of timer.
998 *
999 * @return N/A
1000 */
1001typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1002
1003/**
1004 * @typedef k_timer_stop_t
1005 * @brief Timer stop function type.
1006 *
1007 * A timer's stop function is executed if the timer is stopped prematurely.
1008 * The function runs in the context of the thread that stops the timer.
1009 * The stop function is optional, and is only invoked if the timer has been
1010 * initialized with one.
1011 *
1012 * @param timer Address of timer.
1013 *
1014 * @return N/A
1015 */
1016typedef void (*k_timer_stop_t)(struct k_timer *timer);
1017
1018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001023 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
1025 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * @param expiry_fn Function to invoke each time the timer expires.
1027 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001029#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001030 struct k_timer name \
1031 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -05001032 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033
Allan Stephens45bfa372016-10-12 12:39:42 -05001034/**
1035 * @brief Initialize a timer.
1036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001038 *
1039 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * @param expiry_fn Function to invoke each time the timer expires.
1041 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001042 *
1043 * @return N/A
1044 */
1045extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001046 k_timer_expiry_t expiry_fn,
1047 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001048
Allan Stephens45bfa372016-10-12 12:39:42 -05001049/**
1050 * @brief Start a timer.
1051 *
1052 * This routine starts a timer, and resets its status to zero. The timer
1053 * begins counting down using the specified duration and period values.
1054 *
1055 * Attempting to start a timer that is already running is permitted.
1056 * The timer's status is reset to zero and the timer begins counting down
1057 * using the new duration and period values.
1058 *
1059 * @param timer Address of timer.
1060 * @param duration Initial timer duration (in milliseconds).
1061 * @param period Timer period (in milliseconds).
1062 *
1063 * @return N/A
1064 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001065extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001066 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001067
1068/**
1069 * @brief Stop a timer.
1070 *
1071 * This routine stops a running timer prematurely. The timer's stop function,
1072 * if one exists, is invoked by the caller.
1073 *
1074 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001076 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001077 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1078 * if @a k_timer_stop is to be called from ISRs.
1079 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001080 * @param timer Address of timer.
1081 *
1082 * @return N/A
1083 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001084extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001085
1086/**
1087 * @brief Read timer status.
1088 *
1089 * This routine reads the timer's status, which indicates the number of times
1090 * it has expired since its status was last read.
1091 *
1092 * Calling this routine resets the timer's status to zero.
1093 *
1094 * @param timer Address of timer.
1095 *
1096 * @return Timer status.
1097 */
Kumar Galacc334c72017-04-21 10:55:34 -05001098extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001099
1100/**
1101 * @brief Synchronize thread to timer expiration.
1102 *
1103 * This routine blocks the calling thread until the timer's status is non-zero
1104 * (indicating that it has expired at least once since it was last examined)
1105 * or the timer is stopped. If the timer status is already non-zero,
1106 * or the timer is already stopped, the caller continues without waiting.
1107 *
1108 * Calling this routine resets the timer's status to zero.
1109 *
1110 * This routine must not be used by interrupt handlers, since they are not
1111 * allowed to block.
1112 *
1113 * @param timer Address of timer.
1114 *
1115 * @return Timer status.
1116 */
Kumar Galacc334c72017-04-21 10:55:34 -05001117extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001118
1119/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001121 *
1122 * This routine computes the (approximate) time remaining before a running
1123 * timer next expires. If the timer is not running, it returns zero.
1124 *
1125 * @param timer Address of timer.
1126 *
1127 * @return Remaining time (in milliseconds).
1128 */
Kumar Galacc334c72017-04-21 10:55:34 -05001129static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001130{
1131 return _timeout_remaining_get(&timer->timeout);
1132}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001133
Allan Stephensc98da842016-11-11 15:45:03 -05001134/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001135 * @brief Associate user-specific data with a timer.
1136 *
1137 * This routine records the @a user_data with the @a timer, to be retrieved
1138 * later.
1139 *
1140 * It can be used e.g. in a timer handler shared across multiple subsystems to
1141 * retrieve data specific to the subsystem this timer is associated with.
1142 *
1143 * @param timer Address of timer.
1144 * @param user_data User data to associate with the timer.
1145 *
1146 * @return N/A
1147 */
1148static inline void k_timer_user_data_set(struct k_timer *timer,
1149 void *user_data)
1150{
1151 timer->user_data = user_data;
1152}
1153
1154/**
1155 * @brief Retrieve the user-specific data from a timer.
1156 *
1157 * @param timer Address of timer.
1158 *
1159 * @return The user data.
1160 */
1161static inline void *k_timer_user_data_get(struct k_timer *timer)
1162{
1163 return timer->user_data;
1164}
1165
1166/**
Allan Stephensc98da842016-11-11 15:45:03 -05001167 * @} end defgroup timer_apis
1168 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001169
Allan Stephensc98da842016-11-11 15:45:03 -05001170/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001171 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001172 * @{
1173 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001174
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * This routine returns the elapsed time since the system booted,
1179 * in milliseconds.
1180 *
1181 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001182 */
Kumar Galacc334c72017-04-21 10:55:34 -05001183extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001184
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001185#ifdef CONFIG_TICKLESS_KERNEL
1186/**
1187 * @brief Enable clock always on in tickless kernel
1188 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001189 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001190 * there are no timer events programmed in tickless kernel
1191 * scheduling. This is necessary if the clock is used to track
1192 * passage of time.
1193 *
1194 * @retval prev_status Previous status of always on flag
1195 */
1196static inline int k_enable_sys_clock_always_on(void)
1197{
1198 int prev_status = _sys_clock_always_on;
1199
1200 _sys_clock_always_on = 1;
1201 _enable_sys_clock();
1202
1203 return prev_status;
1204}
1205
1206/**
1207 * @brief Disable clock always on in tickless kernel
1208 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001209 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001210 * there are no timer events programmed in tickless kernel
1211 * scheduling. To save power, this routine should be called
1212 * immediately when clock is not used to track time.
1213 */
1214static inline void k_disable_sys_clock_always_on(void)
1215{
1216 _sys_clock_always_on = 0;
1217}
1218#else
1219#define k_enable_sys_clock_always_on() do { } while ((0))
1220#define k_disable_sys_clock_always_on() do { } while ((0))
1221#endif
1222
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * This routine returns the lower 32-bits of the elapsed time since the system
1227 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * This routine can be more efficient than k_uptime_get(), as it reduces the
1230 * need for interrupt locking and 64-bit math. However, the 32-bit result
1231 * cannot hold a system uptime time larger than approximately 50 days, so the
1232 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001235 */
Kumar Galacc334c72017-04-21 10:55:34 -05001236extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001237
1238/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * This routine computes the elapsed time between the current system uptime
1242 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * @param reftime Pointer to a reference time, which is updated to the current
1245 * uptime upon return.
1246 *
1247 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001248 */
Kumar Galacc334c72017-04-21 10:55:34 -05001249extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001250
1251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * This routine computes the elapsed time between the current system uptime
1255 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001257 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1258 * need for interrupt locking and 64-bit math. However, the 32-bit result
1259 * cannot hold an elapsed time larger than approximately 50 days, so the
1260 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * @param reftime Pointer to a reference time, which is updated to the current
1263 * uptime upon return.
1264 *
1265 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001266 */
Kumar Galacc334c72017-04-21 10:55:34 -05001267extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001268
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001270 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * This routine returns the current time, as measured by the system's hardware
1273 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001277#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001280 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001281 */
1282
Allan Stephensc98da842016-11-11 15:45:03 -05001283/**
1284 * @cond INTERNAL_HIDDEN
1285 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001287struct k_queue {
1288 _wait_q_t wait_q;
1289 sys_slist_t data_q;
1290 _POLL_EVENT;
1291
1292 _OBJECT_TRACING_NEXT_PTR(k_queue);
1293};
1294
1295#define K_QUEUE_INITIALIZER(obj) \
1296 { \
1297 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1298 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1299 _POLL_EVENT_OBJ_INIT \
1300 _OBJECT_TRACING_INIT \
1301 }
1302
1303/**
1304 * INTERNAL_HIDDEN @endcond
1305 */
1306
1307/**
1308 * @defgroup queue_apis Queue APIs
1309 * @ingroup kernel_apis
1310 * @{
1311 */
1312
1313/**
1314 * @brief Initialize a queue.
1315 *
1316 * This routine initializes a queue object, prior to its first use.
1317 *
1318 * @param queue Address of the queue.
1319 *
1320 * @return N/A
1321 */
1322extern void k_queue_init(struct k_queue *queue);
1323
1324/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001325 * @brief Cancel waiting on a queue.
1326 *
1327 * This routine causes first thread pending on @a queue, if any, to
1328 * return from k_queue_get() call with NULL value (as if timeout expired).
1329 *
1330 * @note Can be called by ISRs.
1331 *
1332 * @param queue Address of the queue.
1333 *
1334 * @return N/A
1335 */
1336extern void k_queue_cancel_wait(struct k_queue *queue);
1337
1338/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001339 * @brief Append an element to the end of a queue.
1340 *
1341 * This routine appends a data item to @a queue. A queue data item must be
1342 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1343 * reserved for the kernel's use.
1344 *
1345 * @note Can be called by ISRs.
1346 *
1347 * @param queue Address of the queue.
1348 * @param data Address of the data item.
1349 *
1350 * @return N/A
1351 */
1352extern void k_queue_append(struct k_queue *queue, void *data);
1353
1354/**
1355 * @brief Prepend an element to a queue.
1356 *
1357 * This routine prepends a data item to @a queue. A queue data item must be
1358 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1359 * reserved for the kernel's use.
1360 *
1361 * @note Can be called by ISRs.
1362 *
1363 * @param queue Address of the queue.
1364 * @param data Address of the data item.
1365 *
1366 * @return N/A
1367 */
1368extern void k_queue_prepend(struct k_queue *queue, void *data);
1369
1370/**
1371 * @brief Inserts an element to a queue.
1372 *
1373 * This routine inserts a data item to @a queue after previous item. A queue
1374 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1375 * item are reserved for the kernel's use.
1376 *
1377 * @note Can be called by ISRs.
1378 *
1379 * @param queue Address of the queue.
1380 * @param prev Address of the previous data item.
1381 * @param data Address of the data item.
1382 *
1383 * @return N/A
1384 */
1385extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1386
1387/**
1388 * @brief Atomically append a list of elements to a queue.
1389 *
1390 * This routine adds a list of data items to @a queue in one operation.
1391 * The data items must be in a singly-linked list, with the first 32 bits
1392 * in each data item pointing to the next data item; the list must be
1393 * NULL-terminated.
1394 *
1395 * @note Can be called by ISRs.
1396 *
1397 * @param queue Address of the queue.
1398 * @param head Pointer to first node in singly-linked list.
1399 * @param tail Pointer to last node in singly-linked list.
1400 *
1401 * @return N/A
1402 */
1403extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1404
1405/**
1406 * @brief Atomically add a list of elements to a queue.
1407 *
1408 * This routine adds a list of data items to @a queue in one operation.
1409 * The data items must be in a singly-linked list implemented using a
1410 * sys_slist_t object. Upon completion, the original list is empty.
1411 *
1412 * @note Can be called by ISRs.
1413 *
1414 * @param queue Address of the queue.
1415 * @param list Pointer to sys_slist_t object.
1416 *
1417 * @return N/A
1418 */
1419extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1420
1421/**
1422 * @brief Get an element from a queue.
1423 *
1424 * This routine removes first data item from @a queue. The first 32 bits of the
1425 * data item are reserved for the kernel's use.
1426 *
1427 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1428 *
1429 * @param queue Address of the queue.
1430 * @param timeout Waiting period to obtain a data item (in milliseconds),
1431 * or one of the special values K_NO_WAIT and K_FOREVER.
1432 *
1433 * @return Address of the data item if successful; NULL if returned
1434 * without waiting, or waiting period timed out.
1435 */
Kumar Galacc334c72017-04-21 10:55:34 -05001436extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001437
1438/**
1439 * @brief Query a queue to see if it has data available.
1440 *
1441 * Note that the data might be already gone by the time this function returns
1442 * if other threads are also trying to read from the queue.
1443 *
1444 * @note Can be called by ISRs.
1445 *
1446 * @param queue Address of the queue.
1447 *
1448 * @return Non-zero if the queue is empty.
1449 * @return 0 if data is available.
1450 */
1451static inline int k_queue_is_empty(struct k_queue *queue)
1452{
1453 return (int)sys_slist_is_empty(&queue->data_q);
1454}
1455
1456/**
1457 * @brief Statically define and initialize a queue.
1458 *
1459 * The queue can be accessed outside the module where it is defined using:
1460 *
1461 * @code extern struct k_queue <name>; @endcode
1462 *
1463 * @param name Name of the queue.
1464 */
1465#define K_QUEUE_DEFINE(name) \
1466 struct k_queue name \
1467 __in_section(_k_queue, static, name) = \
1468 K_QUEUE_INITIALIZER(name)
1469
1470/**
1471 * @} end defgroup queue_apis
1472 */
1473
1474/**
1475 * @cond INTERNAL_HIDDEN
1476 */
1477
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001478struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001479 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480};
1481
Allan Stephensc98da842016-11-11 15:45:03 -05001482#define K_FIFO_INITIALIZER(obj) \
1483 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001484 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001485 }
1486
1487/**
1488 * INTERNAL_HIDDEN @endcond
1489 */
1490
1491/**
1492 * @defgroup fifo_apis Fifo APIs
1493 * @ingroup kernel_apis
1494 * @{
1495 */
1496
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001500 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001502 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001503 *
1504 * @return N/A
1505 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001506#define k_fifo_init(fifo) \
1507 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508
1509/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001510 * @brief Cancel waiting on a fifo.
1511 *
1512 * This routine causes first thread pending on @a fifo, if any, to
1513 * return from k_fifo_get() call with NULL value (as if timeout
1514 * expired).
1515 *
1516 * @note Can be called by ISRs.
1517 *
1518 * @param fifo Address of the fifo.
1519 *
1520 * @return N/A
1521 */
1522#define k_fifo_cancel_wait(fifo) \
1523 k_queue_cancel_wait((struct k_queue *) fifo)
1524
1525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001528 * This routine adds a data item to @a fifo. A fifo data item must be
1529 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1530 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001532 * @note Can be called by ISRs.
1533 *
1534 * @param fifo Address of the fifo.
1535 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536 *
1537 * @return N/A
1538 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001539#define k_fifo_put(fifo, data) \
1540 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001541
1542/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001543 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001545 * This routine adds a list of data items to @a fifo in one operation.
1546 * The data items must be in a singly-linked list, with the first 32 bits
1547 * each data item pointing to the next data item; the list must be
1548 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001550 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @param fifo Address of the fifo.
1553 * @param head Pointer to first node in singly-linked list.
1554 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001555 *
1556 * @return N/A
1557 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001558#define k_fifo_put_list(fifo, head, tail) \
1559 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001560
1561/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001564 * This routine adds a list of data items to @a fifo in one operation.
1565 * The data items must be in a singly-linked list implemented using a
1566 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001567 * and must be re-initialized via sys_slist_init().
1568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001569 * @note Can be called by ISRs.
1570 *
1571 * @param fifo Address of the fifo.
1572 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001573 *
1574 * @return N/A
1575 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001576#define k_fifo_put_slist(fifo, list) \
1577 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001578
1579/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001580 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001582 * This routine removes a data item from @a fifo in a "first in, first out"
1583 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001585 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1586 *
1587 * @param fifo Address of the fifo.
1588 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001589 * or one of the special values K_NO_WAIT and K_FOREVER.
1590 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001591 * @return Address of the data item if successful; NULL if returned
1592 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001594#define k_fifo_get(fifo, timeout) \
1595 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001596
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001597/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001598 * @brief Query a fifo to see if it has data available.
1599 *
1600 * Note that the data might be already gone by the time this function returns
1601 * if other threads is also trying to read from the fifo.
1602 *
1603 * @note Can be called by ISRs.
1604 *
1605 * @param fifo Address of the fifo.
1606 *
1607 * @return Non-zero if the fifo is empty.
1608 * @return 0 if data is available.
1609 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001610#define k_fifo_is_empty(fifo) \
1611 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001612
1613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001617 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001618 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001621 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001622#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001623 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001624 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001625 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001626
Allan Stephensc98da842016-11-11 15:45:03 -05001627/**
1628 * @} end defgroup fifo_apis
1629 */
1630
1631/**
1632 * @cond INTERNAL_HIDDEN
1633 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634
1635struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001636 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001637};
1638
Allan Stephensc98da842016-11-11 15:45:03 -05001639#define K_LIFO_INITIALIZER(obj) \
1640 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001641 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001642 }
1643
1644/**
1645 * INTERNAL_HIDDEN @endcond
1646 */
1647
1648/**
1649 * @defgroup lifo_apis Lifo APIs
1650 * @ingroup kernel_apis
1651 * @{
1652 */
1653
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660 *
1661 * @return N/A
1662 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001663#define k_lifo_init(lifo) \
1664 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001665
1666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001669 * This routine adds a data item to @a lifo. A lifo data item must be
1670 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1671 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @note Can be called by ISRs.
1674 *
1675 * @param lifo Address of the lifo.
1676 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001677 *
1678 * @return N/A
1679 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001680#define k_lifo_put(lifo, data) \
1681 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682
1683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * This routine removes a data item from @a lifo in a "last in, first out"
1687 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1690 *
1691 * @param lifo Address of the lifo.
1692 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693 * or one of the special values K_NO_WAIT and K_FOREVER.
1694 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001695 * @return Address of the data item if successful; NULL if returned
1696 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001697 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001698#define k_lifo_get(lifo, timeout) \
1699 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001701/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001706 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001710#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001711 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001712 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001713 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714
Allan Stephensc98da842016-11-11 15:45:03 -05001715/**
1716 * @} end defgroup lifo_apis
1717 */
1718
1719/**
1720 * @cond INTERNAL_HIDDEN
1721 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001722
1723struct k_stack {
1724 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001725 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726
Anas Nashif2f203c22016-12-18 06:57:45 -05001727 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728};
1729
Allan Stephensc98da842016-11-11 15:45:03 -05001730#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1731 { \
1732 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1733 .base = stack_buffer, \
1734 .next = stack_buffer, \
1735 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001736 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001737 }
1738
1739/**
1740 * INTERNAL_HIDDEN @endcond
1741 */
1742
1743/**
1744 * @defgroup stack_apis Stack APIs
1745 * @ingroup kernel_apis
1746 * @{
1747 */
1748
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749/**
1750 * @brief Initialize a stack.
1751 *
1752 * This routine initializes a stack object, prior to its first use.
1753 *
1754 * @param stack Address of the stack.
1755 * @param buffer Address of array used to hold stacked values.
1756 * @param num_entries Maximum number of values that can be stacked.
1757 *
1758 * @return N/A
1759 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001760extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001761 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762
1763/**
1764 * @brief Push an element onto a stack.
1765 *
1766 * This routine adds a 32-bit value @a data to @a stack.
1767 *
1768 * @note Can be called by ISRs.
1769 *
1770 * @param stack Address of the stack.
1771 * @param data Value to push onto the stack.
1772 *
1773 * @return N/A
1774 */
Kumar Galacc334c72017-04-21 10:55:34 -05001775extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776
1777/**
1778 * @brief Pop an element from a stack.
1779 *
1780 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1781 * manner and stores the value in @a data.
1782 *
1783 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1784 *
1785 * @param stack Address of the stack.
1786 * @param data Address of area to hold the value popped from the stack.
1787 * @param timeout Waiting period to obtain a value (in milliseconds),
1788 * or one of the special values K_NO_WAIT and K_FOREVER.
1789 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001790 * @retval 0 Element popped from stack.
1791 * @retval -EBUSY Returned without waiting.
1792 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 */
Kumar Galacc334c72017-04-21 10:55:34 -05001794extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001801 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * @param name Name of the stack.
1804 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001806#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001807 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001808 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001809 struct k_stack name \
1810 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001811 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1812 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814/**
Allan Stephensc98da842016-11-11 15:45:03 -05001815 * @} end defgroup stack_apis
1816 */
1817
Allan Stephens6bba9b02016-11-16 14:56:54 -05001818struct k_work;
1819
Allan Stephensc98da842016-11-11 15:45:03 -05001820/**
1821 * @defgroup workqueue_apis Workqueue Thread APIs
1822 * @ingroup kernel_apis
1823 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824 */
1825
Allan Stephens6bba9b02016-11-16 14:56:54 -05001826/**
1827 * @typedef k_work_handler_t
1828 * @brief Work item handler function type.
1829 *
1830 * A work item's handler function is executed by a workqueue's thread
1831 * when the work item is processed by the workqueue.
1832 *
1833 * @param work Address of the work item.
1834 *
1835 * @return N/A
1836 */
1837typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838
1839/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001840 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001841 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001842
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001843struct k_work_q {
1844 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001845 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001846};
1847
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001848enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001849 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001850};
1851
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001852struct k_work {
1853 void *_reserved; /* Used by k_fifo implementation. */
1854 k_work_handler_t handler;
1855 atomic_t flags[1];
1856};
1857
Allan Stephens6bba9b02016-11-16 14:56:54 -05001858struct k_delayed_work {
1859 struct k_work work;
1860 struct _timeout timeout;
1861 struct k_work_q *work_q;
1862};
1863
1864extern struct k_work_q k_sys_work_q;
1865
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001867 * INTERNAL_HIDDEN @endcond
1868 */
1869
1870/**
1871 * @brief Initialize a statically-defined work item.
1872 *
1873 * This macro can be used to initialize a statically-defined workqueue work
1874 * item, prior to its first use. For example,
1875 *
1876 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1877 *
1878 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879 */
1880#define K_WORK_INITIALIZER(work_handler) \
1881 { \
1882 ._reserved = NULL, \
1883 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001884 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001885 }
1886
1887/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001888 * @brief Initialize a work item.
1889 *
1890 * This routine initializes a workqueue work item, prior to its first use.
1891 *
1892 * @param work Address of work item.
1893 * @param handler Function to invoke each time work item is processed.
1894 *
1895 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896 */
1897static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1898{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001899 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900 work->handler = handler;
1901}
1902
1903/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001904 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001905 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001906 * This routine submits work item @a work to be processed by workqueue
1907 * @a work_q. If the work item is already pending in the workqueue's queue
1908 * as a result of an earlier submission, this routine has no effect on the
1909 * work item. If the work item has already been processed, or is currently
1910 * being processed, its work is considered complete and the work item can be
1911 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001912 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001913 * @warning
1914 * A submitted work item must not be modified until it has been processed
1915 * by the workqueue.
1916 *
1917 * @note Can be called by ISRs.
1918 *
1919 * @param work_q Address of workqueue.
1920 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001921 *
1922 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923 */
1924static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1925 struct k_work *work)
1926{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001927 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001928 k_fifo_put(&work_q->fifo, work);
1929 }
1930}
1931
1932/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001933 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001935 * This routine indicates if work item @a work is pending in a workqueue's
1936 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001937 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001938 * @note Can be called by ISRs.
1939 *
1940 * @param work Address of work item.
1941 *
1942 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001943 */
1944static inline int k_work_pending(struct k_work *work)
1945{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001946 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001947}
1948
1949/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001950 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001952 * This routine starts workqueue @a work_q. The workqueue spawns its work
1953 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001955 * @param work_q Address of workqueue.
1956 * @param stack Pointer to work queue thread's stack space.
1957 * @param stack_size Size of the work queue thread's stack (in bytes).
1958 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959 *
1960 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961 */
Allan Stephens904cf972016-10-07 13:59:23 -05001962extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001963 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001966 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001968 * This routine initializes a workqueue delayed work item, prior to
1969 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001971 * @param work Address of delayed work item.
1972 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
1974 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001976extern void k_delayed_work_init(struct k_delayed_work *work,
1977 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001978
1979/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001980 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001982 * This routine schedules work item @a work to be processed by workqueue
1983 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001984 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001985 * Only when the countdown completes is the work item actually submitted to
1986 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001987 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001988 * Submitting a previously submitted delayed work item that is still
1989 * counting down cancels the existing submission and restarts the countdown
1990 * using the new delay. If the work item is currently pending on the
1991 * workqueue's queue because the countdown has completed it is too late to
1992 * resubmit the item, and resubmission fails without impacting the work item.
1993 * If the work item has already been processed, or is currently being processed,
1994 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001996 * @warning
1997 * A delayed work item must not be modified until it has been processed
1998 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002000 * @note Can be called by ISRs.
2001 *
2002 * @param work_q Address of workqueue.
2003 * @param work Address of delayed work item.
2004 * @param delay Delay before submitting the work item (in milliseconds).
2005 *
2006 * @retval 0 Work item countdown started.
2007 * @retval -EINPROGRESS Work item is already pending.
2008 * @retval -EINVAL Work item is being processed or has completed its work.
2009 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002010 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002011extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2012 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002013 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014
2015/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002016 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002018 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002019 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002020 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002022 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002023 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002024 * @param work Address of delayed work item.
2025 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002026 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002027 * @retval -EINPROGRESS Work item is already pending.
2028 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002030extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002032/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033 * @brief Submit a work item to the system workqueue.
2034 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002035 * This routine submits work item @a work to be processed by the system
2036 * workqueue. If the work item is already pending in the workqueue's queue
2037 * as a result of an earlier submission, this routine has no effect on the
2038 * work item. If the work item has already been processed, or is currently
2039 * being processed, its work is considered complete and the work item can be
2040 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002042 * @warning
2043 * Work items submitted to the system workqueue should avoid using handlers
2044 * that block or yield since this may prevent the system workqueue from
2045 * processing other work items in a timely manner.
2046 *
2047 * @note Can be called by ISRs.
2048 *
2049 * @param work Address of work item.
2050 *
2051 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002052 */
2053static inline void k_work_submit(struct k_work *work)
2054{
2055 k_work_submit_to_queue(&k_sys_work_q, work);
2056}
2057
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059 * @brief Submit a delayed work item to the system workqueue.
2060 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002061 * This routine schedules work item @a work to be processed by the system
2062 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002063 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002064 * Only when the countdown completes is the work item actually submitted to
2065 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002067 * Submitting a previously submitted delayed work item that is still
2068 * counting down cancels the existing submission and restarts the countdown
2069 * using the new delay. If the work item is currently pending on the
2070 * workqueue's queue because the countdown has completed it is too late to
2071 * resubmit the item, and resubmission fails without impacting the work item.
2072 * If the work item has already been processed, or is currently being processed,
2073 * its work is considered complete and the work item can be resubmitted.
2074 *
2075 * @warning
2076 * Work items submitted to the system workqueue should avoid using handlers
2077 * that block or yield since this may prevent the system workqueue from
2078 * processing other work items in a timely manner.
2079 *
2080 * @note Can be called by ISRs.
2081 *
2082 * @param work Address of delayed work item.
2083 * @param delay Delay before submitting the work item (in milliseconds).
2084 *
2085 * @retval 0 Work item countdown started.
2086 * @retval -EINPROGRESS Work item is already pending.
2087 * @retval -EINVAL Work item is being processed or has completed its work.
2088 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089 */
2090static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002091 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002093 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094}
2095
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002097 * @brief Get time remaining before a delayed work gets scheduled.
2098 *
2099 * This routine computes the (approximate) time remaining before a
2100 * delayed work gets executed. If the delayed work is not waiting to be
2101 * schedules, it returns zero.
2102 *
2103 * @param work Delayed work item.
2104 *
2105 * @return Remaining time (in milliseconds).
2106 */
Kumar Galacc334c72017-04-21 10:55:34 -05002107static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002108{
2109 return _timeout_remaining_get(&work->timeout);
2110}
2111
2112/**
Allan Stephensc98da842016-11-11 15:45:03 -05002113 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114 */
2115
Allan Stephensc98da842016-11-11 15:45:03 -05002116/**
2117 * @cond INTERNAL_HIDDEN
2118 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119
2120struct k_mutex {
2121 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002122 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002123 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125
Anas Nashif2f203c22016-12-18 06:57:45 -05002126 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127};
2128
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129#define K_MUTEX_INITIALIZER(obj) \
2130 { \
2131 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2132 .owner = NULL, \
2133 .lock_count = 0, \
2134 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002135 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002136 }
2137
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138/**
Allan Stephensc98da842016-11-11 15:45:03 -05002139 * INTERNAL_HIDDEN @endcond
2140 */
2141
2142/**
2143 * @defgroup mutex_apis Mutex APIs
2144 * @ingroup kernel_apis
2145 * @{
2146 */
2147
2148/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002151 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002153 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002158 struct k_mutex name \
2159 __in_section(_k_mutex, static, name) = \
2160 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * Upon completion, the mutex is available and does not have an owner.
2168 *
2169 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170 *
2171 * @return N/A
2172 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174
2175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002176 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * This routine locks @a mutex. If the mutex is locked by another thread,
2179 * the calling thread waits until the mutex becomes available or until
2180 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182 * A thread is permitted to lock a mutex it has already locked. The operation
2183 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @param mutex Address of the mutex.
2186 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002187 * or one of the special values K_NO_WAIT and K_FOREVER.
2188 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002189 * @retval 0 Mutex locked.
2190 * @retval -EBUSY Returned without waiting.
2191 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 */
Kumar Galacc334c72017-04-21 10:55:34 -05002193extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002194
2195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002198 * This routine unlocks @a mutex. The mutex must already be locked by the
2199 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002200 *
2201 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203 * thread.
2204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002205 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002206 *
2207 * @return N/A
2208 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002209extern void k_mutex_unlock(struct k_mutex *mutex);
2210
Allan Stephensc98da842016-11-11 15:45:03 -05002211/**
2212 * @} end defgroup mutex_apis
2213 */
2214
2215/**
2216 * @cond INTERNAL_HIDDEN
2217 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218
2219struct k_sem {
2220 _wait_q_t wait_q;
2221 unsigned int count;
2222 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002223 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224
Anas Nashif2f203c22016-12-18 06:57:45 -05002225 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226};
2227
Allan Stephensc98da842016-11-11 15:45:03 -05002228#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2229 { \
2230 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2231 .count = initial_count, \
2232 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002233 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002234 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002235 }
2236
2237/**
2238 * INTERNAL_HIDDEN @endcond
2239 */
2240
2241/**
2242 * @defgroup semaphore_apis Semaphore APIs
2243 * @ingroup kernel_apis
2244 * @{
2245 */
2246
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002250 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002252 * @param sem Address of the semaphore.
2253 * @param initial_count Initial semaphore count.
2254 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002255 *
2256 * @return N/A
2257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2259 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002260
2261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002266 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2267 *
2268 * @param sem Address of the semaphore.
2269 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002270 * or one of the special values K_NO_WAIT and K_FOREVER.
2271 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002272 * @note When porting code from the nanokernel legacy API to the new API, be
2273 * careful with the return value of this function. The return value is the
2274 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2275 * non-zero means failure, while the nano_sem_take family returns 1 for success
2276 * and 0 for failure.
2277 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002278 * @retval 0 Semaphore taken.
2279 * @retval -EBUSY Returned without waiting.
2280 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002281 */
Kumar Galacc334c72017-04-21 10:55:34 -05002282extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002283
2284/**
2285 * @brief Give a semaphore.
2286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * This routine gives @a sem, unless the semaphore is already at its maximum
2288 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002292 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002293 *
2294 * @return N/A
2295 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296extern void k_sem_give(struct k_sem *sem);
2297
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002298/**
2299 * @brief Reset a semaphore's count to zero.
2300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002304 *
2305 * @return N/A
2306 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002307static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308{
2309 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310}
2311
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002312/**
2313 * @brief Get a semaphore's count.
2314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002320 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002321static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322{
2323 return sem->count;
2324}
2325
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002330 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002331 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002333 * @param name Name of the semaphore.
2334 * @param initial_count Initial semaphore count.
2335 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002336 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002338 struct k_sem name \
2339 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340 K_SEM_INITIALIZER(name, initial_count, count_limit)
2341
Allan Stephensc98da842016-11-11 15:45:03 -05002342/**
2343 * @} end defgroup semaphore_apis
2344 */
2345
2346/**
2347 * @defgroup alert_apis Alert APIs
2348 * @ingroup kernel_apis
2349 * @{
2350 */
2351
Allan Stephens5eceb852016-11-16 10:16:30 -05002352/**
2353 * @typedef k_alert_handler_t
2354 * @brief Alert handler function type.
2355 *
2356 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002357 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002358 * and is only invoked if the alert has been initialized with one.
2359 *
2360 * @param alert Address of the alert.
2361 *
2362 * @return 0 if alert has been consumed; non-zero if alert should pend.
2363 */
2364typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002365
2366/**
2367 * @} end defgroup alert_apis
2368 */
2369
2370/**
2371 * @cond INTERNAL_HIDDEN
2372 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002374#define K_ALERT_DEFAULT NULL
2375#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002377struct k_alert {
2378 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 atomic_t send_count;
2380 struct k_work work_item;
2381 struct k_sem sem;
2382
Anas Nashif2f203c22016-12-18 06:57:45 -05002383 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384};
2385
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002386extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002388#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002390 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002392 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002393 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002394 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395 }
2396
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397/**
Allan Stephensc98da842016-11-11 15:45:03 -05002398 * INTERNAL_HIDDEN @endcond
2399 */
2400
2401/**
2402 * @addtogroup alert_apis
2403 * @{
2404 */
2405
2406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002409 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002411 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 * @param name Name of the alert.
2414 * @param alert_handler Action to take when alert is sent. Specify either
2415 * the address of a function to be invoked by the system workqueue
2416 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2417 * K_ALERT_DEFAULT (which causes the alert to pend).
2418 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002420#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002421 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002422 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002423 K_ALERT_INITIALIZER(name, alert_handler, \
2424 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002425
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * @param alert Address of the alert.
2432 * @param handler Action to take when alert is sent. Specify either the address
2433 * of a function to be invoked by the system workqueue thread,
2434 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2435 * K_ALERT_DEFAULT (which causes the alert to pend).
2436 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
2438 * @return N/A
2439 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002440extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2441 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442
2443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2449 *
2450 * @param alert Address of the alert.
2451 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002452 * or one of the special values K_NO_WAIT and K_FOREVER.
2453 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002454 * @retval 0 Alert received.
2455 * @retval -EBUSY Returned without waiting.
2456 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 */
Kumar Galacc334c72017-04-21 10:55:34 -05002458extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459
2460/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * This routine signals @a alert. The action specified for @a alert will
2464 * be taken, which may trigger the execution of an alert handler function
2465 * and/or cause the alert to pend (assuming the alert has not reached its
2466 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002468 * @note Can be called by ISRs.
2469 *
2470 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
2472 * @return N/A
2473 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002474extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475
2476/**
Allan Stephensc98da842016-11-11 15:45:03 -05002477 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478 */
2479
Allan Stephensc98da842016-11-11 15:45:03 -05002480/**
2481 * @cond INTERNAL_HIDDEN
2482 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483
2484struct k_msgq {
2485 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002486 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002487 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488 char *buffer_start;
2489 char *buffer_end;
2490 char *read_ptr;
2491 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002492 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493
Anas Nashif2f203c22016-12-18 06:57:45 -05002494 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495};
2496
Peter Mitsis1da807e2016-10-06 11:36:59 -04002497#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498 { \
2499 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002500 .max_msgs = q_max_msgs, \
2501 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002503 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504 .read_ptr = q_buffer, \
2505 .write_ptr = q_buffer, \
2506 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002507 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508 }
2509
Peter Mitsis1da807e2016-10-06 11:36:59 -04002510/**
Allan Stephensc98da842016-11-11 15:45:03 -05002511 * INTERNAL_HIDDEN @endcond
2512 */
2513
2514/**
2515 * @defgroup msgq_apis Message Queue APIs
2516 * @ingroup kernel_apis
2517 * @{
2518 */
2519
2520/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2524 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002525 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2526 * message is similarly aligned to this boundary, @a q_msg_size must also be
2527 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * The message queue can be accessed outside the module where it is defined
2530 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002532 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @param q_name Name of the message queue.
2535 * @param q_msg_size Message size (in bytes).
2536 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002537 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002538 */
2539#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2540 static char __noinit __aligned(q_align) \
2541 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002542 struct k_msgq q_name \
2543 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002544 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2545 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
Peter Mitsisd7a37502016-10-13 11:37:40 -04002547/**
2548 * @brief Initialize a message queue.
2549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * This routine initializes a message queue object, prior to its first use.
2551 *
Allan Stephensda827222016-11-09 14:23:58 -06002552 * The message queue's ring buffer must contain space for @a max_msgs messages,
2553 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2554 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2555 * that each message is similarly aligned to this boundary, @a q_msg_size
2556 * must also be a multiple of N.
2557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * @param q Address of the message queue.
2559 * @param buffer Pointer to ring buffer that holds queued messages.
2560 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002561 * @param max_msgs Maximum number of messages that can be queued.
2562 *
2563 * @return N/A
2564 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002565extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002566 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002567
2568/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002572 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002573 * @note Can be called by ISRs.
2574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @param q Address of the message queue.
2576 * @param data Pointer to the message.
2577 * @param timeout Waiting period to add the message (in milliseconds),
2578 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002579 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002580 * @retval 0 Message sent.
2581 * @retval -ENOMSG Returned without waiting or queue purged.
2582 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002583 */
Kumar Galacc334c72017-04-21 10:55:34 -05002584extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002585
2586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * This routine receives a message from message queue @a q in a "first in,
2590 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002591 *
Allan Stephensc98da842016-11-11 15:45:03 -05002592 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * @param q Address of the message queue.
2595 * @param data Address of area to hold the received message.
2596 * @param timeout Waiting period to receive the message (in milliseconds),
2597 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002598 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002599 * @retval 0 Message received.
2600 * @retval -ENOMSG Returned without waiting.
2601 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002602 */
Kumar Galacc334c72017-04-21 10:55:34 -05002603extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002604
2605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * This routine discards all unreceived messages in a message queue's ring
2609 * buffer. Any threads that are blocked waiting to send a message to the
2610 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002613 *
2614 * @return N/A
2615 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616extern void k_msgq_purge(struct k_msgq *q);
2617
Peter Mitsis67be2492016-10-07 11:44:34 -04002618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * This routine returns the number of unused entries in a message queue's
2622 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @param q Address of the message queue.
2625 *
2626 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002627 */
Kumar Galacc334c72017-04-21 10:55:34 -05002628static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002629{
2630 return q->max_msgs - q->used_msgs;
2631}
2632
Peter Mitsisd7a37502016-10-13 11:37:40 -04002633/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002634 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * @param q Address of the message queue.
2639 *
2640 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002641 */
Kumar Galacc334c72017-04-21 10:55:34 -05002642static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643{
2644 return q->used_msgs;
2645}
2646
Allan Stephensc98da842016-11-11 15:45:03 -05002647/**
2648 * @} end defgroup msgq_apis
2649 */
2650
2651/**
2652 * @defgroup mem_pool_apis Memory Pool APIs
2653 * @ingroup kernel_apis
2654 * @{
2655 */
2656
Andy Ross73cb9582017-05-09 10:42:39 -07002657/* Note on sizing: the use of a 20 bit field for block means that,
2658 * assuming a reasonable minimum block size of 16 bytes, we're limited
2659 * to 16M of memory managed by a single pool. Long term it would be
2660 * good to move to a variable bit size based on configuration.
2661 */
2662struct k_mem_block_id {
2663 u32_t pool : 8;
2664 u32_t level : 4;
2665 u32_t block : 20;
2666};
2667
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002670 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671};
2672
Allan Stephensc98da842016-11-11 15:45:03 -05002673/**
2674 * @} end defgroup mem_pool_apis
2675 */
2676
2677/**
2678 * @defgroup mailbox_apis Mailbox APIs
2679 * @ingroup kernel_apis
2680 * @{
2681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
2683struct k_mbox_msg {
2684 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002685 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002687 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002689 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 /** sender's message data buffer */
2691 void *tx_data;
2692 /** internal use only - needed for legacy API support */
2693 void *_rx_data;
2694 /** message data block descriptor */
2695 struct k_mem_block tx_block;
2696 /** source thread id */
2697 k_tid_t rx_source_thread;
2698 /** target thread id */
2699 k_tid_t tx_target_thread;
2700 /** internal use only - thread waiting on send (may be a dummy) */
2701 k_tid_t _syncing_thread;
2702#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2703 /** internal use only - semaphore used during asynchronous send */
2704 struct k_sem *_async_sem;
2705#endif
2706};
2707
Allan Stephensc98da842016-11-11 15:45:03 -05002708/**
2709 * @cond INTERNAL_HIDDEN
2710 */
2711
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712struct k_mbox {
2713 _wait_q_t tx_msg_queue;
2714 _wait_q_t rx_msg_queue;
2715
Anas Nashif2f203c22016-12-18 06:57:45 -05002716 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717};
2718
2719#define K_MBOX_INITIALIZER(obj) \
2720 { \
2721 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2722 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002723 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 }
2725
Peter Mitsis12092702016-10-14 12:57:23 -04002726/**
Allan Stephensc98da842016-11-11 15:45:03 -05002727 * INTERNAL_HIDDEN @endcond
2728 */
2729
2730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002735 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002738 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002740 struct k_mbox name \
2741 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 K_MBOX_INITIALIZER(name) \
2743
Peter Mitsis12092702016-10-14 12:57:23 -04002744/**
2745 * @brief Initialize a mailbox.
2746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * This routine initializes a mailbox object, prior to its first use.
2748 *
2749 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002750 *
2751 * @return N/A
2752 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753extern void k_mbox_init(struct k_mbox *mbox);
2754
Peter Mitsis12092702016-10-14 12:57:23 -04002755/**
2756 * @brief Send a mailbox message in a synchronous manner.
2757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * This routine sends a message to @a mbox and waits for a receiver to both
2759 * receive and process it. The message data may be in a buffer, in a memory
2760 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * @param mbox Address of the mailbox.
2763 * @param tx_msg Address of the transmit message descriptor.
2764 * @param timeout Waiting period for the message to be received (in
2765 * milliseconds), or one of the special values K_NO_WAIT
2766 * and K_FOREVER. Once the message has been received,
2767 * this routine waits as long as necessary for the message
2768 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002769 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002770 * @retval 0 Message sent.
2771 * @retval -ENOMSG Returned without waiting.
2772 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002773 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002774extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002775 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002776
Peter Mitsis12092702016-10-14 12:57:23 -04002777/**
2778 * @brief Send a mailbox message in an asynchronous manner.
2779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * This routine sends a message to @a mbox without waiting for a receiver
2781 * to process it. The message data may be in a buffer, in a memory pool block,
2782 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2783 * will be given when the message has been both received and completely
2784 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * @param mbox Address of the mailbox.
2787 * @param tx_msg Address of the transmit message descriptor.
2788 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002789 *
2790 * @return N/A
2791 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002792extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793 struct k_sem *sem);
2794
Peter Mitsis12092702016-10-14 12:57:23 -04002795/**
2796 * @brief Receive a mailbox message.
2797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * This routine receives a message from @a mbox, then optionally retrieves
2799 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @param mbox Address of the mailbox.
2802 * @param rx_msg Address of the receive message descriptor.
2803 * @param buffer Address of the buffer to receive data, or NULL to defer data
2804 * retrieval and message disposal until later.
2805 * @param timeout Waiting period for a message to be received (in
2806 * milliseconds), or one of the special values K_NO_WAIT
2807 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002808 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002809 * @retval 0 Message received.
2810 * @retval -ENOMSG Returned without waiting.
2811 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002812 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002813extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002814 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002815
2816/**
2817 * @brief Retrieve mailbox message data into a buffer.
2818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * This routine completes the processing of a received message by retrieving
2820 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002821 *
2822 * Alternatively, this routine can be used to dispose of a received message
2823 * without retrieving its data.
2824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @param rx_msg Address of the receive message descriptor.
2826 * @param buffer Address of the buffer to receive data, or NULL to discard
2827 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002828 *
2829 * @return N/A
2830 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002831extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002832
2833/**
2834 * @brief Retrieve mailbox message data into a memory pool block.
2835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002836 * This routine completes the processing of a received message by retrieving
2837 * its data into a memory pool block, then disposing of the message.
2838 * The memory pool block that results from successful retrieval must be
2839 * returned to the pool once the data has been processed, even in cases
2840 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002841 *
2842 * Alternatively, this routine can be used to dispose of a received message
2843 * without retrieving its data. In this case there is no need to return a
2844 * memory pool block to the pool.
2845 *
2846 * This routine allocates a new memory pool block for the data only if the
2847 * data is not already in one. If a new block cannot be allocated, the routine
2848 * returns a failure code and the received message is left unchanged. This
2849 * permits the caller to reattempt data retrieval at a later time or to dispose
2850 * of the received message without retrieving its data.
2851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @param rx_msg Address of a receive message descriptor.
2853 * @param pool Address of memory pool, or NULL to discard data.
2854 * @param block Address of the area to hold memory pool block info.
2855 * @param timeout Waiting period to wait for a memory pool block (in
2856 * milliseconds), or one of the special values K_NO_WAIT
2857 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002858 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002859 * @retval 0 Data retrieved.
2860 * @retval -ENOMEM Returned without waiting.
2861 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002862 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002863extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002864 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002865 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866
Allan Stephensc98da842016-11-11 15:45:03 -05002867/**
2868 * @} end defgroup mailbox_apis
2869 */
2870
2871/**
2872 * @cond INTERNAL_HIDDEN
2873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874
2875struct k_pipe {
2876 unsigned char *buffer; /* Pipe buffer: may be NULL */
2877 size_t size; /* Buffer size */
2878 size_t bytes_used; /* # bytes used in buffer */
2879 size_t read_index; /* Where in buffer to read from */
2880 size_t write_index; /* Where in buffer to write */
2881
2882 struct {
2883 _wait_q_t readers; /* Reader wait queue */
2884 _wait_q_t writers; /* Writer wait queue */
2885 } wait_q;
2886
Anas Nashif2f203c22016-12-18 06:57:45 -05002887 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888};
2889
Peter Mitsise5d9c582016-10-14 14:44:57 -04002890#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891 { \
2892 .buffer = pipe_buffer, \
2893 .size = pipe_buffer_size, \
2894 .bytes_used = 0, \
2895 .read_index = 0, \
2896 .write_index = 0, \
2897 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2898 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002899 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900 }
2901
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902/**
Allan Stephensc98da842016-11-11 15:45:03 -05002903 * INTERNAL_HIDDEN @endcond
2904 */
2905
2906/**
2907 * @defgroup pipe_apis Pipe APIs
2908 * @ingroup kernel_apis
2909 * @{
2910 */
2911
2912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002917 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @param name Name of the pipe.
2920 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2921 * or zero if no ring buffer is used.
2922 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002923 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002924#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2925 static unsigned char __noinit __aligned(pipe_align) \
2926 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002927 struct k_pipe name \
2928 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002929 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * This routine initializes a pipe object, prior to its first use.
2935 *
2936 * @param pipe Address of the pipe.
2937 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2938 * is used.
2939 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2940 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 *
2942 * @return N/A
2943 */
2944extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2945 size_t size);
2946
2947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @param pipe Address of the pipe.
2953 * @param data Address of data to write.
2954 * @param bytes_to_write Size of data (in bytes).
2955 * @param bytes_written Address of area to hold the number of bytes written.
2956 * @param min_xfer Minimum number of bytes to write.
2957 * @param timeout Waiting period to wait for the data to be written (in
2958 * milliseconds), or one of the special values K_NO_WAIT
2959 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002961 * @retval 0 At least @a min_xfer bytes of data were written.
2962 * @retval -EIO Returned without waiting; zero data bytes were written.
2963 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002966extern int k_pipe_put(struct k_pipe *pipe, void *data,
2967 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002968 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969
2970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @param pipe Address of the pipe.
2976 * @param data Address to place the data read from pipe.
2977 * @param bytes_to_read Maximum number of data bytes to read.
2978 * @param bytes_read Address of area to hold the number of bytes read.
2979 * @param min_xfer Minimum number of data bytes to read.
2980 * @param timeout Waiting period to wait for the data to be read (in
2981 * milliseconds), or one of the special values K_NO_WAIT
2982 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002984 * @retval 0 At least @a min_xfer bytes of data were read.
2985 * @retval -EIO Returned without waiting; zero data bytes were read.
2986 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002989extern int k_pipe_get(struct k_pipe *pipe, void *data,
2990 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002991 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992
2993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * This routine writes the data contained in a memory block to @a pipe.
2997 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003000 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001 * @param block Memory block containing data to send
3002 * @param size Number of data bytes in memory block to send
3003 * @param sem Semaphore to signal upon completion (else NULL)
3004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006 */
3007extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3008 size_t size, struct k_sem *sem);
3009
3010/**
Allan Stephensc98da842016-11-11 15:45:03 -05003011 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012 */
3013
Allan Stephensc98da842016-11-11 15:45:03 -05003014/**
3015 * @cond INTERNAL_HIDDEN
3016 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003017
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003018struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003020 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003021 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022 char *buffer;
3023 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003024 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025
Anas Nashif2f203c22016-12-18 06:57:45 -05003026 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027};
3028
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003029#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
3030 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003031 { \
3032 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003033 .num_blocks = slab_num_blocks, \
3034 .block_size = slab_block_size, \
3035 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036 .free_list = NULL, \
3037 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003038 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039 }
3040
Peter Mitsis578f9112016-10-07 13:50:31 -04003041/**
Allan Stephensc98da842016-11-11 15:45:03 -05003042 * INTERNAL_HIDDEN @endcond
3043 */
3044
3045/**
3046 * @defgroup mem_slab_apis Memory Slab APIs
3047 * @ingroup kernel_apis
3048 * @{
3049 */
3050
3051/**
Allan Stephensda827222016-11-09 14:23:58 -06003052 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003053 *
Allan Stephensda827222016-11-09 14:23:58 -06003054 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003056 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3057 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003058 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003059 *
Allan Stephensda827222016-11-09 14:23:58 -06003060 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003062 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003063 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * @param name Name of the memory slab.
3066 * @param slab_block_size Size of each memory block (in bytes).
3067 * @param slab_num_blocks Number memory blocks.
3068 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003069 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003070#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3071 char __noinit __aligned(slab_align) \
3072 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3073 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003074 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003075 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
3076 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003078/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003079 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003082 *
Allan Stephensda827222016-11-09 14:23:58 -06003083 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3084 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3085 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3086 * To ensure that each memory block is similarly aligned to this boundary,
3087 * @a slab_block_size must also be a multiple of N.
3088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @param slab Address of the memory slab.
3090 * @param buffer Pointer to buffer used for the memory blocks.
3091 * @param block_size Size of each memory block (in bytes).
3092 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003093 *
3094 * @return N/A
3095 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003096extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003097 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003098
3099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * @param slab Address of the memory slab.
3105 * @param mem Pointer to block address area.
3106 * @param timeout Maximum time to wait for operation to complete
3107 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3108 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003109 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003110 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003112 * @retval -ENOMEM Returned without waiting.
3113 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003114 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003115extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003116 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003117
3118/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * This routine releases a previously allocated memory block back to its
3122 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * @param slab Address of the memory slab.
3125 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003126 *
3127 * @return N/A
3128 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003129extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003132 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * This routine gets the number of memory blocks that are currently
3135 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003140 */
Kumar Galacc334c72017-04-21 10:55:34 -05003141static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003142{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003143 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003144}
3145
Peter Mitsisc001aa82016-10-13 13:53:37 -04003146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * This routine gets the number of memory blocks that are currently
3150 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003155 */
Kumar Galacc334c72017-04-21 10:55:34 -05003156static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003157{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003158 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003159}
3160
Allan Stephensc98da842016-11-11 15:45:03 -05003161/**
3162 * @} end defgroup mem_slab_apis
3163 */
3164
3165/**
3166 * @cond INTERNAL_HIDDEN
3167 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168
Andy Ross73cb9582017-05-09 10:42:39 -07003169struct k_mem_pool_lvl {
3170 union {
3171 u32_t *bits_p;
3172 u32_t bits;
3173 };
3174 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003175};
3176
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003177struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003178 void *buf;
3179 size_t max_sz;
3180 u16_t n_max;
3181 u8_t n_levels;
3182 u8_t max_inline_level;
3183 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003184 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185};
3186
Andy Ross73cb9582017-05-09 10:42:39 -07003187#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188
Andy Ross73cb9582017-05-09 10:42:39 -07003189#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3190
3191#define _MPOOL_LVLS(maxsz, minsz) \
3192 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3193 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3194 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3195 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3196 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3197 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3198 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3199 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3200 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3201 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3202 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3203 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3204 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3205 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3206 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3207 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3208
3209/* Rounds the needed bits up to integer multiples of u32_t */
3210#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3211 ((((n_max) << (2*(l))) + 31) / 32)
3212
3213/* One word gets stored free unioned with the pointer, otherwise the
3214 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003215 */
Andy Ross73cb9582017-05-09 10:42:39 -07003216#define _MPOOL_LBIT_WORDS(n_max, l) \
3217 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3218 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003219
Andy Ross73cb9582017-05-09 10:42:39 -07003220/* How many bytes for the bitfields of a single level? */
3221#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3222 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3223 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003224
Andy Ross73cb9582017-05-09 10:42:39 -07003225/* Size of the bitmap array that follows the buffer in allocated memory */
3226#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3227 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3228 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3229 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3230 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3231 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3232 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3233 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3234 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3235 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3236 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3237 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3238 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3239 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3240 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3241 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3242 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003243
3244/**
Allan Stephensc98da842016-11-11 15:45:03 -05003245 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003246 */
3247
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003248/**
Allan Stephensc98da842016-11-11 15:45:03 -05003249 * @addtogroup mem_pool_apis
3250 * @{
3251 */
3252
3253/**
3254 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3257 * long. The memory pool allows blocks to be repeatedly partitioned into
3258 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003259 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003260 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003261 * If the pool is to be accessed outside the module where it is defined, it
3262 * can be declared via
3263 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003264 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003267 * @param minsz Size of the smallest blocks in the pool (in bytes).
3268 * @param maxsz Size of the largest blocks in the pool (in bytes).
3269 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003271 */
Andy Ross73cb9582017-05-09 10:42:39 -07003272#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3273 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3274 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3275 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3276 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3277 .buf = _mpool_buf_##name, \
3278 .max_sz = maxsz, \
3279 .n_max = nmax, \
3280 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3281 .levels = _mpool_lvls_##name, \
3282 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003283
Peter Mitsis937042c2016-10-13 13:18:26 -04003284/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * @param pool Address of the memory pool.
3290 * @param block Pointer to block descriptor for the allocated memory.
3291 * @param size Amount of memory to allocate (in bytes).
3292 * @param timeout Maximum time to wait for operation to complete
3293 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3294 * or K_FOREVER to wait as long as necessary.
3295 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003296 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003298 * @retval -ENOMEM Returned without waiting.
3299 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003300 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003301extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003302 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003303
3304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * This routine releases a previously allocated memory block back to its
3308 * memory pool.
3309 *
3310 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003311 *
3312 * @return N/A
3313 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003315
3316/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003318 *
Andy Ross73cb9582017-05-09 10:42:39 -07003319 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003320 *
Andy Ross73cb9582017-05-09 10:42:39 -07003321 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003322 *
3323 * @return N/A
3324 */
Andy Ross73cb9582017-05-09 10:42:39 -07003325static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003326
3327/**
Allan Stephensc98da842016-11-11 15:45:03 -05003328 * @} end addtogroup mem_pool_apis
3329 */
3330
3331/**
3332 * @defgroup heap_apis Heap Memory Pool APIs
3333 * @ingroup kernel_apis
3334 * @{
3335 */
3336
3337/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003340 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003341 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003346 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003347extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003348
3349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003351 *
3352 * This routine provides traditional free() semantics. The memory being
3353 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003354 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003355 * If @a ptr is NULL, no operation is performed.
3356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003358 *
3359 * @return N/A
3360 */
3361extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362
Allan Stephensc98da842016-11-11 15:45:03 -05003363/**
3364 * @} end defgroup heap_apis
3365 */
3366
Benjamin Walshacc68c12017-01-29 18:57:45 -05003367/* polling API - PRIVATE */
3368
Benjamin Walshb0179862017-02-02 16:39:57 -05003369#ifdef CONFIG_POLL
3370#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3371#else
3372#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3373#endif
3374
Benjamin Walshacc68c12017-01-29 18:57:45 -05003375/* private - implementation data created as needed, per-type */
3376struct _poller {
3377 struct k_thread *thread;
3378};
3379
3380/* private - types bit positions */
3381enum _poll_types_bits {
3382 /* can be used to ignore an event */
3383 _POLL_TYPE_IGNORE,
3384
3385 /* to be signaled by k_poll_signal() */
3386 _POLL_TYPE_SIGNAL,
3387
3388 /* semaphore availability */
3389 _POLL_TYPE_SEM_AVAILABLE,
3390
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003391 /* queue/fifo/lifo data availability */
3392 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003393
3394 _POLL_NUM_TYPES
3395};
3396
3397#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3398
3399/* private - states bit positions */
3400enum _poll_states_bits {
3401 /* default state when creating event */
3402 _POLL_STATE_NOT_READY,
3403
3404 /* there was another poller already on the object */
3405 _POLL_STATE_EADDRINUSE,
3406
3407 /* signaled by k_poll_signal() */
3408 _POLL_STATE_SIGNALED,
3409
3410 /* semaphore is available */
3411 _POLL_STATE_SEM_AVAILABLE,
3412
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003413 /* data is available to read on queue/fifo/lifo */
3414 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003415
3416 _POLL_NUM_STATES
3417};
3418
3419#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3420
3421#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003422 (32 - (0 \
3423 + 8 /* tag */ \
3424 + _POLL_NUM_TYPES \
3425 + _POLL_NUM_STATES \
3426 + 1 /* modes */ \
3427 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003428
3429#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3430#error overflow of 32-bit word in struct k_poll_event
3431#endif
3432
3433/* end of polling API - PRIVATE */
3434
3435
3436/**
3437 * @defgroup poll_apis Async polling APIs
3438 * @ingroup kernel_apis
3439 * @{
3440 */
3441
3442/* Public polling API */
3443
3444/* public - values for k_poll_event.type bitfield */
3445#define K_POLL_TYPE_IGNORE 0
3446#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3447#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003448#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3449#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003450
3451/* public - polling modes */
3452enum k_poll_modes {
3453 /* polling thread does not take ownership of objects when available */
3454 K_POLL_MODE_NOTIFY_ONLY = 0,
3455
3456 K_POLL_NUM_MODES
3457};
3458
3459/* public - values for k_poll_event.state bitfield */
3460#define K_POLL_STATE_NOT_READY 0
3461#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3462#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3463#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003464#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3465#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003466
3467/* public - poll signal object */
3468struct k_poll_signal {
3469 /* PRIVATE - DO NOT TOUCH */
3470 struct k_poll_event *poll_event;
3471
3472 /*
3473 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3474 * user resets it to 0.
3475 */
3476 unsigned int signaled;
3477
3478 /* custom result value passed to k_poll_signal() if needed */
3479 int result;
3480};
3481
3482#define K_POLL_SIGNAL_INITIALIZER() \
3483 { \
3484 .poll_event = NULL, \
3485 .signaled = 0, \
3486 .result = 0, \
3487 }
3488
3489struct k_poll_event {
3490 /* PRIVATE - DO NOT TOUCH */
3491 struct _poller *poller;
3492
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003493 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003494 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003495
Benjamin Walshacc68c12017-01-29 18:57:45 -05003496 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003497 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003498
3499 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003500 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003501
3502 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003503 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003504
3505 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003506 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003507
3508 /* per-type data */
3509 union {
3510 void *obj;
3511 struct k_poll_signal *signal;
3512 struct k_sem *sem;
3513 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003514 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003515 };
3516};
3517
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003518#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003519 { \
3520 .poller = NULL, \
3521 .type = event_type, \
3522 .state = K_POLL_STATE_NOT_READY, \
3523 .mode = event_mode, \
3524 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003525 { .obj = event_obj }, \
3526 }
3527
3528#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3529 event_tag) \
3530 { \
3531 .type = event_type, \
3532 .tag = event_tag, \
3533 .state = K_POLL_STATE_NOT_READY, \
3534 .mode = event_mode, \
3535 .unused = 0, \
3536 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003537 }
3538
3539/**
3540 * @brief Initialize one struct k_poll_event instance
3541 *
3542 * After this routine is called on a poll event, the event it ready to be
3543 * placed in an event array to be passed to k_poll().
3544 *
3545 * @param event The event to initialize.
3546 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3547 * values. Only values that apply to the same object being polled
3548 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3549 * event.
3550 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3551 * @param obj Kernel object or poll signal.
3552 *
3553 * @return N/A
3554 */
3555
Kumar Galacc334c72017-04-21 10:55:34 -05003556extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003557 int mode, void *obj);
3558
3559/**
3560 * @brief Wait for one or many of multiple poll events to occur
3561 *
3562 * This routine allows a thread to wait concurrently for one or many of
3563 * multiple poll events to have occurred. Such events can be a kernel object
3564 * being available, like a semaphore, or a poll signal event.
3565 *
3566 * When an event notifies that a kernel object is available, the kernel object
3567 * is not "given" to the thread calling k_poll(): it merely signals the fact
3568 * that the object was available when the k_poll() call was in effect. Also,
3569 * all threads trying to acquire an object the regular way, i.e. by pending on
3570 * the object, have precedence over the thread polling on the object. This
3571 * means that the polling thread will never get the poll event on an object
3572 * until the object becomes available and its pend queue is empty. For this
3573 * reason, the k_poll() call is more effective when the objects being polled
3574 * only have one thread, the polling thread, trying to acquire them.
3575 *
3576 * Only one thread can be polling for a particular object at a given time. If
3577 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3578 * and returns as soon as it has finished handling the other events. This means
3579 * that k_poll() can return -EADDRINUSE and have the state value of some events
3580 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3581 * parameter is ignored.
3582 *
3583 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3584 * events that were passed to k_poll() and check the state field for the values
3585 * that were expected and take the associated actions.
3586 *
3587 * Before being reused for another call to k_poll(), the user has to reset the
3588 * state field to K_POLL_STATE_NOT_READY.
3589 *
3590 * @param events An array of pointers to events to be polled for.
3591 * @param num_events The number of events in the array.
3592 * @param timeout Waiting period for an event to be ready (in milliseconds),
3593 * or one of the special values K_NO_WAIT and K_FOREVER.
3594 *
3595 * @retval 0 One or more events are ready.
3596 * @retval -EADDRINUSE One or more objects already had a poller.
3597 * @retval -EAGAIN Waiting period timed out.
3598 */
3599
3600extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003601 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003602
3603/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003604 * @brief Initialize a poll signal object.
3605 *
3606 * Ready a poll signal object to be signaled via k_poll_signal().
3607 *
3608 * @param signal A poll signal.
3609 *
3610 * @return N/A
3611 */
3612
3613extern void k_poll_signal_init(struct k_poll_signal *signal);
3614
3615/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003616 * @brief Signal a poll signal object.
3617 *
3618 * This routine makes ready a poll signal, which is basically a poll event of
3619 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3620 * made ready to run. A @a result value can be specified.
3621 *
3622 * The poll signal contains a 'signaled' field that, when set by
3623 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3624 * be reset by the user before being passed again to k_poll() or k_poll() will
3625 * consider it being signaled, and will return immediately.
3626 *
3627 * @param signal A poll signal.
3628 * @param result The value to store in the result field of the signal.
3629 *
3630 * @retval 0 The signal was delivered successfully.
3631 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3632 */
3633
3634extern int k_poll_signal(struct k_poll_signal *signal, int result);
3635
3636/* private internal function */
3637extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003638 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003639
3640/**
3641 * @} end defgroup poll_apis
3642 */
3643
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003644/**
3645 * @brief Make the CPU idle.
3646 *
3647 * This function makes the CPU idle until an event wakes it up.
3648 *
3649 * In a regular system, the idle thread should be the only thread responsible
3650 * for making the CPU idle and triggering any type of power management.
3651 * However, in some more constrained systems, such as a single-threaded system,
3652 * the only thread would be responsible for this if needed.
3653 *
3654 * @return N/A
3655 */
3656extern void k_cpu_idle(void);
3657
3658/**
3659 * @brief Make the CPU idle in an atomic fashion.
3660 *
3661 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3662 * must be done atomically before making the CPU idle.
3663 *
3664 * @param key Interrupt locking key obtained from irq_lock().
3665 *
3666 * @return N/A
3667 */
3668extern void k_cpu_atomic_idle(unsigned int key);
3669
Kumar Galacc334c72017-04-21 10:55:34 -05003670extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003671
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003672#include <arch/cpu.h>
3673
Andrew Boiecdb94d62017-04-18 15:22:05 -07003674#ifdef _ARCH_EXCEPT
3675/* This archtecture has direct support for triggering a CPU exception */
3676#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3677#else
3678
3679#include <misc/printk.h>
3680
3681/* NOTE: This is the implementation for arches that do not implement
3682 * _ARCH_EXCEPT() to generate a real CPU exception.
3683 *
3684 * We won't have a real exception frame to determine the PC value when
3685 * the oops occurred, so print file and line number before we jump into
3686 * the fatal error handler.
3687 */
3688#define _k_except_reason(reason) do { \
3689 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3690 _NanoFatalErrorHandler(reason, &_default_esf); \
3691 CODE_UNREACHABLE; \
3692 } while (0)
3693
3694#endif /* _ARCH__EXCEPT */
3695
3696/**
3697 * @brief Fatally terminate a thread
3698 *
3699 * This should be called when a thread has encountered an unrecoverable
3700 * runtime condition and needs to terminate. What this ultimately
3701 * means is determined by the _fatal_error_handler() implementation, which
3702 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3703 *
3704 * If this is called from ISR context, the default system fatal error handler
3705 * will treat it as an unrecoverable system error, just like k_panic().
3706 */
3707#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3708
3709/**
3710 * @brief Fatally terminate the system
3711 *
3712 * This should be called when the Zephyr kernel has encountered an
3713 * unrecoverable runtime condition and needs to terminate. What this ultimately
3714 * means is determined by the _fatal_error_handler() implementation, which
3715 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3716 */
3717#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3718
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003719/*
3720 * private APIs that are utilized by one or more public APIs
3721 */
3722
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003723#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003724extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003725#else
3726#define _init_static_threads() do { } while ((0))
3727#endif
3728
3729extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003730extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003731
3732#ifdef __cplusplus
3733}
3734#endif
3735
Andrew Boiee004dec2016-11-07 09:01:19 -08003736#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3737/*
3738 * Define new and delete operators.
3739 * At this moment, the operators do nothing since objects are supposed
3740 * to be statically allocated.
3741 */
3742inline void operator delete(void *ptr)
3743{
3744 (void)ptr;
3745}
3746
3747inline void operator delete[](void *ptr)
3748{
3749 (void)ptr;
3750}
3751
3752inline void *operator new(size_t size)
3753{
3754 (void)size;
3755 return NULL;
3756}
3757
3758inline void *operator new[](size_t size)
3759{
3760 (void)size;
3761 return NULL;
3762}
3763
3764/* Placement versions of operator new and delete */
3765inline void operator delete(void *ptr1, void *ptr2)
3766{
3767 (void)ptr1;
3768 (void)ptr2;
3769}
3770
3771inline void operator delete[](void *ptr1, void *ptr2)
3772{
3773 (void)ptr1;
3774 (void)ptr2;
3775}
3776
3777inline void *operator new(size_t size, void *ptr)
3778{
3779 (void)size;
3780 return ptr;
3781}
3782
3783inline void *operator new[](size_t size, void *ptr)
3784{
3785 (void)size;
3786 return ptr;
3787}
3788
3789#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3790
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003791#endif /* !_ASMLANGUAGE */
3792
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793#endif /* _kernel__h_ */