blob: 3c172ee95f8131d6a5a4885b9e43cdef5205a623 [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>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#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
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300101#define _POLL_EVENT_OBJ_INIT(obj) \
102 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
103#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500104#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300105#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#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 Boie945af952017-08-22 13:15:23 -0700126enum k_objects {
127 K_OBJ_ALERT,
128 K_OBJ_DELAYED_WORK,
129 K_OBJ_MEM_SLAB,
130 K_OBJ_MSGQ,
131 K_OBJ_MUTEX,
132 K_OBJ_PIPE,
133 K_OBJ_SEM,
134 K_OBJ_STACK,
135 K_OBJ_THREAD,
136 K_OBJ_TIMER,
137 K_OBJ_WORK,
138 K_OBJ_WORK_Q,
139
140 K_OBJ_LAST
141};
142
143#ifdef CONFIG_USERSPACE
144/* Table generated by gperf, these objects are retrieved via
145 * _k_object_find() */
146struct _k_object {
147 char *name;
148 char perms[CONFIG_MAX_THREAD_BYTES];
149 char type;
150 char flags;
151} __packed;
152
153#define K_OBJ_FLAG_INITIALIZED BIT(0)
154/**
155 * Ensure a system object is a valid object of the expected type
156 *
157 * Searches for the object and ensures that it is indeed an object
158 * of the expected type, that the caller has the right permissions on it,
159 * and that the object has been initialized.
160 *
161 * This function is intended to be called on the kernel-side system
162 * call handlers to validate kernel object pointers passed in from
163 * userspace.
164 *
165 * @param obj Address of the kernel object
166 * @param otype Expected type of the kernel object
167 * @param init If true, this is for an init function and we will not error
168 * out if the object is not initialized
169 * @return 0 If the object is valid
170 * -EBADF if not a valid object of the specified type
171 * -EPERM If the caller does not have permissions
172 * -EINVAL Object is not intitialized
173 */
174int _k_object_validate(void *obj, enum k_objects otype, int init);
175
176
177/**
178 * Lookup a kernel object and init its metadata if it exists
179 *
180 * Calling this on an object will make it usable from userspace.
181 * Intended to be called as the last statement in kernel object init
182 * functions.
183 *
184 * @param object Address of the kernel object
185 */
186void _k_object_init(void *obj);
187
188
189/**
190 * grant a thread access to a kernel object
191 *
192 * The thread will be granted access to the object if the caller is from
193 * supervisor mode, or the caller is from user mode AND has permissions
194 * on the object already.
195 *
196 * @param object Address of kernel object
197 * @param thread Thread to grant access to the object
198 */
199void k_object_grant_access(void *object, struct k_thread *thread);
200
201#else
202static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
203{
204 ARG_UNUSED(obj);
205 ARG_UNUSED(otype);
206 ARG_UNUSED(init);
207
208 return 0;
209}
210
211static inline void _k_object_init(void *obj)
212{
213 ARG_UNUSED(obj);
214}
215
216static inline void k_object_grant_access(void *object, struct k_thread *thread)
217{
218 ARG_UNUSED(object);
219 ARG_UNUSED(thread);
220}
221#endif /* CONFIG_USERSPACE */
222
Andrew Boie73abd322017-04-04 13:19:13 -0700223/* timeouts */
224
225struct _timeout;
226typedef void (*_timeout_func_t)(struct _timeout *t);
227
228struct _timeout {
229 sys_dnode_t node;
230 struct k_thread *thread;
231 sys_dlist_t *wait_q;
232 s32_t delta_ticks_from_prev;
233 _timeout_func_t func;
234};
235
236extern s32_t _timeout_remaining_get(struct _timeout *timeout);
237
238/* Threads */
239typedef void (*_thread_entry_t)(void *, void *, void *);
240
241#ifdef CONFIG_THREAD_MONITOR
242struct __thread_entry {
243 _thread_entry_t pEntry;
244 void *parameter1;
245 void *parameter2;
246 void *parameter3;
247};
248#endif
249
250/* can be used for creating 'dummy' threads, e.g. for pending on objects */
251struct _thread_base {
252
253 /* this thread's entry in a ready/wait queue */
254 sys_dnode_t k_q_node;
255
256 /* user facing 'thread options'; values defined in include/kernel.h */
257 u8_t user_options;
258
259 /* thread state */
260 u8_t thread_state;
261
262 /*
263 * scheduler lock count and thread priority
264 *
265 * These two fields control the preemptibility of a thread.
266 *
267 * When the scheduler is locked, sched_locked is decremented, which
268 * means that the scheduler is locked for values from 0xff to 0x01. A
269 * thread is coop if its prio is negative, thus 0x80 to 0xff when
270 * looked at the value as unsigned.
271 *
272 * By putting them end-to-end, this means that a thread is
273 * non-preemptible if the bundled value is greater than or equal to
274 * 0x0080.
275 */
276 union {
277 struct {
278#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
279 u8_t sched_locked;
280 s8_t prio;
281#else /* LITTLE and PDP */
282 s8_t prio;
283 u8_t sched_locked;
284#endif
285 };
286 u16_t preempt;
287 };
288
289 /* data returned by APIs */
290 void *swap_data;
291
292#ifdef CONFIG_SYS_CLOCK_EXISTS
293 /* this thread's entry in a timeout queue */
294 struct _timeout timeout;
295#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700296};
297
298typedef struct _thread_base _thread_base_t;
299
300#if defined(CONFIG_THREAD_STACK_INFO)
301/* Contains the stack information of a thread */
302struct _thread_stack_info {
303 /* Stack Start */
304 u32_t start;
305 /* Stack Size */
306 u32_t size;
307};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700308
309typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700310#endif /* CONFIG_THREAD_STACK_INFO */
311
312struct k_thread {
313
314 struct _thread_base base;
315
316 /* defined by the architecture, but all archs need these */
317 struct _caller_saved caller_saved;
318 struct _callee_saved callee_saved;
319
320 /* static thread init data */
321 void *init_data;
322
323 /* abort function */
324 void (*fn_abort)(void);
325
326#if defined(CONFIG_THREAD_MONITOR)
327 /* thread entry and parameters description */
328 struct __thread_entry *entry;
329
330 /* next item in list of all threads */
331 struct k_thread *next_thread;
332#endif
333
334#ifdef CONFIG_THREAD_CUSTOM_DATA
335 /* crude thread-local storage */
336 void *custom_data;
337#endif
338
339#ifdef CONFIG_ERRNO
340 /* per-thread errno variable */
341 int errno_var;
342#endif
343
344#if defined(CONFIG_THREAD_STACK_INFO)
345 /* Stack Info */
346 struct _thread_stack_info stack_info;
347#endif /* CONFIG_THREAD_STACK_INFO */
348
349 /* arch-specifics: must always be at the end */
350 struct _thread_arch arch;
351};
352
353typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400354typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700355#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400356
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400357enum execution_context_types {
358 K_ISR = 0,
359 K_COOP_THREAD,
360 K_PREEMPT_THREAD,
361};
362
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400363/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100364 * @defgroup profiling_apis Profiling APIs
365 * @ingroup kernel_apis
366 * @{
367 */
368
369/**
370 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
371 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700372 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100373 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
374 *
375 * CONFIG_MAIN_STACK_SIZE
376 * CONFIG_IDLE_STACK_SIZE
377 * CONFIG_ISR_STACK_SIZE
378 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
379 *
380 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
381 * produce output.
382 *
383 * @return N/A
384 */
385extern void k_call_stacks_analyze(void);
386
387/**
388 * @} end defgroup profiling_apis
389 */
390
391/**
Allan Stephensc98da842016-11-11 15:45:03 -0500392 * @defgroup thread_apis Thread APIs
393 * @ingroup kernel_apis
394 * @{
395 */
396
397/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500398 * @typedef k_thread_entry_t
399 * @brief Thread entry point function type.
400 *
401 * A thread's entry point function is invoked when the thread starts executing.
402 * Up to 3 argument values can be passed to the function.
403 *
404 * The thread terminates execution permanently if the entry point function
405 * returns. The thread is responsible for releasing any shared resources
406 * it may own (such as mutexes and dynamically allocated memory), prior to
407 * returning.
408 *
409 * @param p1 First argument.
410 * @param p2 Second argument.
411 * @param p3 Third argument.
412 *
413 * @return N/A
414 */
415typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
416
Benjamin Walshed240f22017-01-22 13:05:08 -0500417#endif /* !_ASMLANGUAGE */
418
419
420/*
421 * Thread user options. May be needed by assembly code. Common part uses low
422 * bits, arch-specific use high bits.
423 */
424
425/* system thread that must not abort */
426#define K_ESSENTIAL (1 << 0)
427
428#if defined(CONFIG_FP_SHARING)
429/* thread uses floating point registers */
430#define K_FP_REGS (1 << 1)
431#endif
432
433#ifdef CONFIG_X86
434/* x86 Bitmask definitions for threads user options */
435
436#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
437/* thread uses SSEx (and also FP) registers */
438#define K_SSE_REGS (1 << 7)
439#endif
440#endif
441
442/* end - thread options */
443
444#if !defined(_ASMLANGUAGE)
445
Andrew Boie507852a2017-07-25 18:47:07 -0700446/* Using typedef deliberately here, this is quite intended to be an opaque
447 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
448 *
449 * The purpose of this data type is to clearly distinguish between the
450 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
451 * buffer which composes the stack data actually used by the underlying
452 * thread; they cannot be used interchangably as some arches precede the
453 * stack buffer region with guard areas that trigger a MPU or MMU fault
454 * if written to.
455 *
456 * APIs that want to work with the buffer inside should continue to use
457 * char *.
458 *
459 * Stacks should always be created with K_THREAD_STACK_DEFINE().
460 */
461struct __packed _k_thread_stack_element {
462 char data;
463};
464typedef struct _k_thread_stack_element *k_thread_stack_t;
465
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400466
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700468 * @brief Create a thread.
469 *
470 * This routine initializes a thread, then schedules it for execution.
471 *
472 * The new thread may be scheduled for immediate execution or a delayed start.
473 * If the newly spawned thread does not have a delayed start the kernel
474 * scheduler may preempt the current thread to allow the new thread to
475 * execute.
476 *
477 * Thread options are architecture-specific, and can include K_ESSENTIAL,
478 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
479 * them using "|" (the logical OR operator).
480 *
481 * Historically, users often would use the beginning of the stack memory region
482 * to store the struct k_thread data, although corruption will occur if the
483 * stack overflows this region and stack protection features may not detect this
484 * situation.
485 *
486 * @param new_thread Pointer to uninitialized struct k_thread
487 * @param stack Pointer to the stack space.
488 * @param stack_size Stack size in bytes.
489 * @param entry Thread entry function.
490 * @param p1 1st entry point parameter.
491 * @param p2 2nd entry point parameter.
492 * @param p3 3rd entry point parameter.
493 * @param prio Thread priority.
494 * @param options Thread options.
495 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
496 *
497 * @return ID of new thread.
498 */
Andrew Boie507852a2017-07-25 18:47:07 -0700499extern k_tid_t k_thread_create(struct k_thread *new_thread,
500 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700501 size_t stack_size,
502 void (*entry)(void *, void *, void*),
503 void *p1, void *p2, void *p3,
504 int prio, u32_t options, s32_t delay);
505
506/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508 *
Allan Stephensc98da842016-11-11 15:45:03 -0500509 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500510 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500512 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400513 *
514 * @return N/A
515 */
Kumar Galacc334c72017-04-21 10:55:34 -0500516extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517
518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500519 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520 *
521 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500522 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400523 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400524 * @return N/A
525 */
Kumar Galacc334c72017-04-21 10:55:34 -0500526extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400527
528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500533 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534 *
535 * @return N/A
536 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400537extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538
539/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500540 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500542 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500544 * If @a thread is not currently sleeping, the routine has no effect.
545 *
546 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400547 *
548 * @return N/A
549 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400550extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400551
552/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500553 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500555 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400557extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558
559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500562 * This routine prevents @a thread from executing if it has not yet started
563 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500565 * @param thread ID of thread to cancel.
566 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700567 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500568 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400569 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400570extern int k_thread_cancel(k_tid_t thread);
571
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400572/**
Allan Stephensc98da842016-11-11 15:45:03 -0500573 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * This routine permanently stops execution of @a thread. The thread is taken
576 * off all kernel queues it is part of (i.e. the ready queue, the timeout
577 * queue, or a kernel object wait queue). However, any kernel resources the
578 * thread might currently own (such as mutexes or memory blocks) are not
579 * released. It is the responsibility of the caller of this routine to ensure
580 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
584 * @return N/A
585 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400586extern void k_thread_abort(k_tid_t thread);
587
Andrew Boie7d627c52017-08-30 11:01:56 -0700588
589/**
590 * @brief Start an inactive thread
591 *
592 * If a thread was created with K_FOREVER in the delay parameter, it will
593 * not be added to the scheduling queue until this function is called
594 * on it.
595 *
596 * @param thread thread to start
597 */
598extern void k_thread_start(k_tid_t thread);
599
Allan Stephensc98da842016-11-11 15:45:03 -0500600/**
601 * @cond INTERNAL_HIDDEN
602 */
603
Benjamin Walshd211a522016-12-06 11:44:01 -0500604/* timeout has timed out and is not on _timeout_q anymore */
605#define _EXPIRED (-2)
606
607/* timeout is not in use */
608#define _INACTIVE (-1)
609
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400610struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700611 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700612 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400613 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500614 void (*init_entry)(void *, void *, void *);
615 void *init_p1;
616 void *init_p2;
617 void *init_p3;
618 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500619 u32_t init_options;
620 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500621 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500622 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400623};
624
Andrew Boied26cf2d2017-03-30 13:07:02 -0700625#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400626 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500627 prio, options, delay, abort, groups) \
628 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700629 .init_thread = (thread), \
630 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500631 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400632 .init_entry = (void (*)(void *, void *, void *))entry, \
633 .init_p1 = (void *)p1, \
634 .init_p2 = (void *)p2, \
635 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500636 .init_prio = (prio), \
637 .init_options = (options), \
638 .init_delay = (delay), \
639 .init_abort = (abort), \
640 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400641 }
642
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400643/**
Allan Stephensc98da842016-11-11 15:45:03 -0500644 * INTERNAL_HIDDEN @endcond
645 */
646
647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500648 * @brief Statically define and initialize a thread.
649 *
650 * The thread may be scheduled for immediate execution or a delayed start.
651 *
652 * Thread options are architecture-specific, and can include K_ESSENTIAL,
653 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
654 * them using "|" (the logical OR operator).
655 *
656 * The ID of the thread can be accessed using:
657 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500658 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500659 *
660 * @param name Name of the thread.
661 * @param stack_size Stack size in bytes.
662 * @param entry Thread entry function.
663 * @param p1 1st entry point parameter.
664 * @param p2 2nd entry point parameter.
665 * @param p3 3rd entry point parameter.
666 * @param prio Thread priority.
667 * @param options Thread options.
668 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400669 *
670 * @internal It has been observed that the x86 compiler by default aligns
671 * these _static_thread_data structures to 32-byte boundaries, thereby
672 * wasting space. To work around this, force a 4-byte alignment.
673 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500674#define K_THREAD_DEFINE(name, stack_size, \
675 entry, p1, p2, p3, \
676 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700677 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700678 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500679 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500680 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700681 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
682 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500683 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700684 NULL, 0); \
685 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400686
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500688 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500692 * @param thread ID of thread whose priority is needed.
693 *
694 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500696extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697
698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500699 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 *
703 * Rescheduling can occur immediately depending on the priority @a thread is
704 * set to:
705 *
706 * - If its priority is raised above the priority of the caller of this
707 * function, and the caller is preemptible, @a thread will be scheduled in.
708 *
709 * - If the caller operates on itself, it lowers its priority below that of
710 * other threads in the system, and the caller is preemptible, the thread of
711 * highest priority will be scheduled in.
712 *
713 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
714 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
715 * highest priority.
716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500717 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 * @param prio New priority.
719 *
720 * @warning Changing the priority of a thread currently involved in mutex
721 * priority inheritance may result in undefined behavior.
722 *
723 * @return N/A
724 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400725extern void k_thread_priority_set(k_tid_t thread, int prio);
726
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400727/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500728 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * This routine prevents the kernel scheduler from making @a thread the
731 * current thread. All other internal operations on @a thread are still
732 * performed; for example, any timeout it is waiting on keeps ticking,
733 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500735 * If @a thread is already suspended, the routine has no effect.
736 *
737 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400738 *
739 * @return N/A
740 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400741extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400742
743/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500744 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500746 * This routine allows the kernel scheduler to make @a thread the current
747 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500749 * If @a thread is not currently suspended, the routine has no effect.
750 *
751 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752 *
753 * @return N/A
754 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400755extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400756
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500758 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500760 * This routine specifies how the scheduler will perform time slicing of
761 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500763 * To enable time slicing, @a slice must be non-zero. The scheduler
764 * ensures that no thread runs for more than the specified time limit
765 * before other threads of that priority are given a chance to execute.
766 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700767 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500769 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 * execute. Once the scheduler selects a thread for execution, there is no
771 * minimum guaranteed time the thread will execute before threads of greater or
772 * equal priority are scheduled.
773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500774 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400775 * for execution, this routine has no effect; the thread is immediately
776 * rescheduled after the slice period expires.
777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * To disable timeslicing, set both @a slice and @a prio to zero.
779 *
780 * @param slice Maximum time slice length (in milliseconds).
781 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
783 * @return N/A
784 */
Kumar Galacc334c72017-04-21 10:55:34 -0500785extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400786
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787/**
Allan Stephensc98da842016-11-11 15:45:03 -0500788 * @} end defgroup thread_apis
789 */
790
791/**
792 * @addtogroup isr_apis
793 * @{
794 */
795
796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephensc98da842016-11-11 15:45:03 -0500799 * This routine allows the caller to customize its actions, depending on
800 * whether it is a thread or an ISR.
801 *
802 * @note Can be called by ISRs.
803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @return 0 if invoked by a thread.
805 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500807extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808
Benjamin Walsh445830d2016-11-10 15:54:27 -0500809/**
810 * @brief Determine if code is running in a preemptible thread.
811 *
Allan Stephensc98da842016-11-11 15:45:03 -0500812 * This routine allows the caller to customize its actions, depending on
813 * whether it can be preempted by another thread. The routine returns a 'true'
814 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500815 *
Allan Stephensc98da842016-11-11 15:45:03 -0500816 * - The code is running in a thread, not at ISR.
817 * - The thread's priority is in the preemptible range.
818 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500819 *
Allan Stephensc98da842016-11-11 15:45:03 -0500820 * @note Can be called by ISRs.
821 *
822 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500823 * @return Non-zero if invoked by a preemptible thread.
824 */
825extern int k_is_preempt_thread(void);
826
Allan Stephensc98da842016-11-11 15:45:03 -0500827/**
828 * @} end addtogroup isr_apis
829 */
830
831/**
832 * @addtogroup thread_apis
833 * @{
834 */
835
836/**
837 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500838 *
Allan Stephensc98da842016-11-11 15:45:03 -0500839 * This routine prevents the current thread from being preempted by another
840 * thread by instructing the scheduler to treat it as a cooperative thread.
841 * If the thread subsequently performs an operation that makes it unready,
842 * it will be context switched out in the normal manner. When the thread
843 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500844 *
Allan Stephensc98da842016-11-11 15:45:03 -0500845 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500846 *
Allan Stephensc98da842016-11-11 15:45:03 -0500847 * @note k_sched_lock() and k_sched_unlock() should normally be used
848 * when the operation being performed can be safely interrupted by ISRs.
849 * However, if the amount of processing involved is very small, better
850 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500851 *
852 * @return N/A
853 */
854extern void k_sched_lock(void);
855
Allan Stephensc98da842016-11-11 15:45:03 -0500856/**
857 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500858 *
Allan Stephensc98da842016-11-11 15:45:03 -0500859 * This routine reverses the effect of a previous call to k_sched_lock().
860 * A thread must call the routine once for each time it called k_sched_lock()
861 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500862 *
863 * @return N/A
864 */
865extern void k_sched_unlock(void);
866
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500870 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * Custom data is not used by the kernel itself, and is freely available
873 * for a thread to use as it sees fit. It can be used as a framework
874 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
878 * @return N/A
879 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400880extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500885 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889extern void *k_thread_custom_data_get(void);
890
891/**
Allan Stephensc98da842016-11-11 15:45:03 -0500892 * @} end addtogroup thread_apis
893 */
894
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400895#include <sys_clock.h>
896
Allan Stephensc2f15a42016-11-17 12:24:22 -0500897/**
898 * @addtogroup clock_apis
899 * @{
900 */
901
902/**
903 * @brief Generate null timeout delay.
904 *
905 * This macro generates a timeout delay that that instructs a kernel API
906 * not to wait if the requested operation cannot be performed immediately.
907 *
908 * @return Timeout delay value.
909 */
910#define K_NO_WAIT 0
911
912/**
913 * @brief Generate timeout delay from milliseconds.
914 *
915 * This macro generates a timeout delay that that instructs a kernel API
916 * to wait up to @a ms milliseconds to perform the requested operation.
917 *
918 * @param ms Duration in milliseconds.
919 *
920 * @return Timeout delay value.
921 */
Johan Hedberg14471692016-11-13 10:52:15 +0200922#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500923
924/**
925 * @brief Generate timeout delay from seconds.
926 *
927 * This macro generates a timeout delay that that instructs a kernel API
928 * to wait up to @a s seconds to perform the requested operation.
929 *
930 * @param s Duration in seconds.
931 *
932 * @return Timeout delay value.
933 */
Johan Hedberg14471692016-11-13 10:52:15 +0200934#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500935
936/**
937 * @brief Generate timeout delay from minutes.
938 *
939 * This macro generates a timeout delay that that instructs a kernel API
940 * to wait up to @a m minutes to perform the requested operation.
941 *
942 * @param m Duration in minutes.
943 *
944 * @return Timeout delay value.
945 */
Johan Hedberg14471692016-11-13 10:52:15 +0200946#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500947
948/**
949 * @brief Generate timeout delay from hours.
950 *
951 * This macro generates a timeout delay that that instructs a kernel API
952 * to wait up to @a h hours to perform the requested operation.
953 *
954 * @param h Duration in hours.
955 *
956 * @return Timeout delay value.
957 */
Johan Hedberg14471692016-11-13 10:52:15 +0200958#define K_HOURS(h) K_MINUTES((h) * 60)
959
Allan Stephensc98da842016-11-11 15:45:03 -0500960/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500961 * @brief Generate infinite timeout delay.
962 *
963 * This macro generates a timeout delay that that instructs a kernel API
964 * to wait as long as necessary to perform the requested operation.
965 *
966 * @return Timeout delay value.
967 */
968#define K_FOREVER (-1)
969
970/**
971 * @} end addtogroup clock_apis
972 */
973
974/**
Allan Stephensc98da842016-11-11 15:45:03 -0500975 * @cond INTERNAL_HIDDEN
976 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400977
Benjamin Walsh62092182016-12-20 14:39:08 -0500978/* kernel clocks */
979
980#if (sys_clock_ticks_per_sec == 1000) || \
981 (sys_clock_ticks_per_sec == 500) || \
982 (sys_clock_ticks_per_sec == 250) || \
983 (sys_clock_ticks_per_sec == 125) || \
984 (sys_clock_ticks_per_sec == 100) || \
985 (sys_clock_ticks_per_sec == 50) || \
986 (sys_clock_ticks_per_sec == 25) || \
987 (sys_clock_ticks_per_sec == 20) || \
988 (sys_clock_ticks_per_sec == 10) || \
989 (sys_clock_ticks_per_sec == 1)
990
991 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
992#else
993 /* yields horrible 64-bit math on many architectures: try to avoid */
994 #define _NON_OPTIMIZED_TICKS_PER_SEC
995#endif
996
997#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500998extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500999#else
Kumar Galacc334c72017-04-21 10:55:34 -05001000static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001001{
Kumar Galacc334c72017-04-21 10:55:34 -05001002 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001003}
1004#endif
1005
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001006/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001007#ifdef CONFIG_TICKLESS_KERNEL
1008#define _TICK_ALIGN 0
1009#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001010#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001011#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001012
Kumar Galacc334c72017-04-21 10:55:34 -05001013static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001014{
Benjamin Walsh62092182016-12-20 14:39:08 -05001015#ifdef CONFIG_SYS_CLOCK_EXISTS
1016
1017#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001018 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001019#else
Kumar Galacc334c72017-04-21 10:55:34 -05001020 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001021#endif
1022
1023#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001024 __ASSERT(ticks == 0, "");
1025 return 0;
1026#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001027}
1028
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029struct k_timer {
1030 /*
1031 * _timeout structure must be first here if we want to use
1032 * dynamic timer allocation. timeout.node is used in the double-linked
1033 * list of free timers
1034 */
1035 struct _timeout timeout;
1036
Allan Stephens45bfa372016-10-12 12:39:42 -05001037 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001038 _wait_q_t wait_q;
1039
1040 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001041 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042
1043 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001044 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001045
1046 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001047 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048
Allan Stephens45bfa372016-10-12 12:39:42 -05001049 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001050 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001051
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001052 /* user-specific data, also used to support legacy features */
1053 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001054
Anas Nashif2f203c22016-12-18 06:57:45 -05001055 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001056};
1057
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001058#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001059 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001060 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001061 .timeout.wait_q = NULL, \
1062 .timeout.thread = NULL, \
1063 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001064 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001065 .expiry_fn = expiry, \
1066 .stop_fn = stop, \
1067 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001068 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001069 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070 }
1071
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001072#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1073
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074/**
Allan Stephensc98da842016-11-11 15:45:03 -05001075 * INTERNAL_HIDDEN @endcond
1076 */
1077
1078/**
1079 * @defgroup timer_apis Timer APIs
1080 * @ingroup kernel_apis
1081 * @{
1082 */
1083
1084/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001085 * @typedef k_timer_expiry_t
1086 * @brief Timer expiry function type.
1087 *
1088 * A timer's expiry function is executed by the system clock interrupt handler
1089 * each time the timer expires. The expiry function is optional, and is only
1090 * invoked if the timer has been initialized with one.
1091 *
1092 * @param timer Address of timer.
1093 *
1094 * @return N/A
1095 */
1096typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1097
1098/**
1099 * @typedef k_timer_stop_t
1100 * @brief Timer stop function type.
1101 *
1102 * A timer's stop function is executed if the timer is stopped prematurely.
1103 * The function runs in the context of the thread that stops the timer.
1104 * The stop function is optional, and is only invoked if the timer has been
1105 * initialized with one.
1106 *
1107 * @param timer Address of timer.
1108 *
1109 * @return N/A
1110 */
1111typedef void (*k_timer_stop_t)(struct k_timer *timer);
1112
1113/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001118 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
1120 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * @param expiry_fn Function to invoke each time the timer expires.
1122 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001124#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001125 struct k_timer name \
1126 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001127 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128
Allan Stephens45bfa372016-10-12 12:39:42 -05001129/**
1130 * @brief Initialize a timer.
1131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001133 *
1134 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @param expiry_fn Function to invoke each time the timer expires.
1136 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001137 *
1138 * @return N/A
1139 */
1140extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001141 k_timer_expiry_t expiry_fn,
1142 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001143
Allan Stephens45bfa372016-10-12 12:39:42 -05001144/**
1145 * @brief Start a timer.
1146 *
1147 * This routine starts a timer, and resets its status to zero. The timer
1148 * begins counting down using the specified duration and period values.
1149 *
1150 * Attempting to start a timer that is already running is permitted.
1151 * The timer's status is reset to zero and the timer begins counting down
1152 * using the new duration and period values.
1153 *
1154 * @param timer Address of timer.
1155 * @param duration Initial timer duration (in milliseconds).
1156 * @param period Timer period (in milliseconds).
1157 *
1158 * @return N/A
1159 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001161 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001162
1163/**
1164 * @brief Stop a timer.
1165 *
1166 * This routine stops a running timer prematurely. The timer's stop function,
1167 * if one exists, is invoked by the caller.
1168 *
1169 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001171 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001172 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1173 * if @a k_timer_stop is to be called from ISRs.
1174 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001175 * @param timer Address of timer.
1176 *
1177 * @return N/A
1178 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001180
1181/**
1182 * @brief Read timer status.
1183 *
1184 * This routine reads the timer's status, which indicates the number of times
1185 * it has expired since its status was last read.
1186 *
1187 * Calling this routine resets the timer's status to zero.
1188 *
1189 * @param timer Address of timer.
1190 *
1191 * @return Timer status.
1192 */
Kumar Galacc334c72017-04-21 10:55:34 -05001193extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001194
1195/**
1196 * @brief Synchronize thread to timer expiration.
1197 *
1198 * This routine blocks the calling thread until the timer's status is non-zero
1199 * (indicating that it has expired at least once since it was last examined)
1200 * or the timer is stopped. If the timer status is already non-zero,
1201 * or the timer is already stopped, the caller continues without waiting.
1202 *
1203 * Calling this routine resets the timer's status to zero.
1204 *
1205 * This routine must not be used by interrupt handlers, since they are not
1206 * allowed to block.
1207 *
1208 * @param timer Address of timer.
1209 *
1210 * @return Timer status.
1211 */
Kumar Galacc334c72017-04-21 10:55:34 -05001212extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001213
1214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001216 *
1217 * This routine computes the (approximate) time remaining before a running
1218 * timer next expires. If the timer is not running, it returns zero.
1219 *
1220 * @param timer Address of timer.
1221 *
1222 * @return Remaining time (in milliseconds).
1223 */
Kumar Galacc334c72017-04-21 10:55:34 -05001224static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001225{
1226 return _timeout_remaining_get(&timer->timeout);
1227}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001228
Allan Stephensc98da842016-11-11 15:45:03 -05001229/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001230 * @brief Associate user-specific data with a timer.
1231 *
1232 * This routine records the @a user_data with the @a timer, to be retrieved
1233 * later.
1234 *
1235 * It can be used e.g. in a timer handler shared across multiple subsystems to
1236 * retrieve data specific to the subsystem this timer is associated with.
1237 *
1238 * @param timer Address of timer.
1239 * @param user_data User data to associate with the timer.
1240 *
1241 * @return N/A
1242 */
1243static inline void k_timer_user_data_set(struct k_timer *timer,
1244 void *user_data)
1245{
1246 timer->user_data = user_data;
1247}
1248
1249/**
1250 * @brief Retrieve the user-specific data from a timer.
1251 *
1252 * @param timer Address of timer.
1253 *
1254 * @return The user data.
1255 */
1256static inline void *k_timer_user_data_get(struct k_timer *timer)
1257{
1258 return timer->user_data;
1259}
1260
1261/**
Allan Stephensc98da842016-11-11 15:45:03 -05001262 * @} end defgroup timer_apis
1263 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001264
Allan Stephensc98da842016-11-11 15:45:03 -05001265/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001266 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001267 * @{
1268 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001269
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001271 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * This routine returns the elapsed time since the system booted,
1274 * in milliseconds.
1275 *
1276 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001277 */
Kumar Galacc334c72017-04-21 10:55:34 -05001278extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001279
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001280#ifdef CONFIG_TICKLESS_KERNEL
1281/**
1282 * @brief Enable clock always on in tickless kernel
1283 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001284 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001285 * there are no timer events programmed in tickless kernel
1286 * scheduling. This is necessary if the clock is used to track
1287 * passage of time.
1288 *
1289 * @retval prev_status Previous status of always on flag
1290 */
1291static inline int k_enable_sys_clock_always_on(void)
1292{
1293 int prev_status = _sys_clock_always_on;
1294
1295 _sys_clock_always_on = 1;
1296 _enable_sys_clock();
1297
1298 return prev_status;
1299}
1300
1301/**
1302 * @brief Disable clock always on in tickless kernel
1303 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001304 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001305 * there are no timer events programmed in tickless kernel
1306 * scheduling. To save power, this routine should be called
1307 * immediately when clock is not used to track time.
1308 */
1309static inline void k_disable_sys_clock_always_on(void)
1310{
1311 _sys_clock_always_on = 0;
1312}
1313#else
1314#define k_enable_sys_clock_always_on() do { } while ((0))
1315#define k_disable_sys_clock_always_on() do { } while ((0))
1316#endif
1317
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001318/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001319 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * This routine returns the lower 32-bits of the elapsed time since the system
1322 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001324 * This routine can be more efficient than k_uptime_get(), as it reduces the
1325 * need for interrupt locking and 64-bit math. However, the 32-bit result
1326 * cannot hold a system uptime time larger than approximately 50 days, so the
1327 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001329 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001330 */
Kumar Galacc334c72017-04-21 10:55:34 -05001331extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001332
1333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001334 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * This routine computes the elapsed time between the current system uptime
1337 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001339 * @param reftime Pointer to a reference time, which is updated to the current
1340 * uptime upon return.
1341 *
1342 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001343 */
Kumar Galacc334c72017-04-21 10:55:34 -05001344extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001345
1346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001347 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001349 * This routine computes the elapsed time between the current system uptime
1350 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001352 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1353 * need for interrupt locking and 64-bit math. However, the 32-bit result
1354 * cannot hold an elapsed time larger than approximately 50 days, so the
1355 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 * @param reftime Pointer to a reference time, which is updated to the current
1358 * uptime upon return.
1359 *
1360 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001361 */
Kumar Galacc334c72017-04-21 10:55:34 -05001362extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001363
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001367 * This routine returns the current time, as measured by the system's hardware
1368 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001370 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001371 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001372#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001375 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001376 */
1377
Allan Stephensc98da842016-11-11 15:45:03 -05001378/**
1379 * @cond INTERNAL_HIDDEN
1380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001381
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001382struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001383 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001384 union {
1385 _wait_q_t wait_q;
1386
1387 _POLL_EVENT;
1388 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001389
1390 _OBJECT_TRACING_NEXT_PTR(k_queue);
1391};
1392
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001393#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001394 { \
1395 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1396 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001397 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001398 _OBJECT_TRACING_INIT \
1399 }
1400
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001401#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1402
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001403/**
1404 * INTERNAL_HIDDEN @endcond
1405 */
1406
1407/**
1408 * @defgroup queue_apis Queue APIs
1409 * @ingroup kernel_apis
1410 * @{
1411 */
1412
1413/**
1414 * @brief Initialize a queue.
1415 *
1416 * This routine initializes a queue object, prior to its first use.
1417 *
1418 * @param queue Address of the queue.
1419 *
1420 * @return N/A
1421 */
1422extern void k_queue_init(struct k_queue *queue);
1423
1424/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001425 * @brief Cancel waiting on a queue.
1426 *
1427 * This routine causes first thread pending on @a queue, if any, to
1428 * return from k_queue_get() call with NULL value (as if timeout expired).
1429 *
1430 * @note Can be called by ISRs.
1431 *
1432 * @param queue Address of the queue.
1433 *
1434 * @return N/A
1435 */
1436extern void k_queue_cancel_wait(struct k_queue *queue);
1437
1438/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001439 * @brief Append an element to the end of a queue.
1440 *
1441 * This routine appends a data item to @a queue. A queue data item must be
1442 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1443 * reserved for the kernel's use.
1444 *
1445 * @note Can be called by ISRs.
1446 *
1447 * @param queue Address of the queue.
1448 * @param data Address of the data item.
1449 *
1450 * @return N/A
1451 */
1452extern void k_queue_append(struct k_queue *queue, void *data);
1453
1454/**
1455 * @brief Prepend an element to a queue.
1456 *
1457 * This routine prepends a data item to @a queue. A queue data item must be
1458 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1459 * reserved for the kernel's use.
1460 *
1461 * @note Can be called by ISRs.
1462 *
1463 * @param queue Address of the queue.
1464 * @param data Address of the data item.
1465 *
1466 * @return N/A
1467 */
1468extern void k_queue_prepend(struct k_queue *queue, void *data);
1469
1470/**
1471 * @brief Inserts an element to a queue.
1472 *
1473 * This routine inserts a data item to @a queue after previous item. A queue
1474 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1475 * item are reserved for the kernel's use.
1476 *
1477 * @note Can be called by ISRs.
1478 *
1479 * @param queue Address of the queue.
1480 * @param prev Address of the previous data item.
1481 * @param data Address of the data item.
1482 *
1483 * @return N/A
1484 */
1485extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1486
1487/**
1488 * @brief Atomically append a list of elements to a queue.
1489 *
1490 * This routine adds a list of data items to @a queue in one operation.
1491 * The data items must be in a singly-linked list, with the first 32 bits
1492 * in each data item pointing to the next data item; the list must be
1493 * NULL-terminated.
1494 *
1495 * @note Can be called by ISRs.
1496 *
1497 * @param queue Address of the queue.
1498 * @param head Pointer to first node in singly-linked list.
1499 * @param tail Pointer to last node in singly-linked list.
1500 *
1501 * @return N/A
1502 */
1503extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1504
1505/**
1506 * @brief Atomically add a list of elements to a queue.
1507 *
1508 * This routine adds a list of data items to @a queue in one operation.
1509 * The data items must be in a singly-linked list implemented using a
1510 * sys_slist_t object. Upon completion, the original list is empty.
1511 *
1512 * @note Can be called by ISRs.
1513 *
1514 * @param queue Address of the queue.
1515 * @param list Pointer to sys_slist_t object.
1516 *
1517 * @return N/A
1518 */
1519extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1520
1521/**
1522 * @brief Get an element from a queue.
1523 *
1524 * This routine removes first data item from @a queue. The first 32 bits of the
1525 * data item are reserved for the kernel's use.
1526 *
1527 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1528 *
1529 * @param queue Address of the queue.
1530 * @param timeout Waiting period to obtain a data item (in milliseconds),
1531 * or one of the special values K_NO_WAIT and K_FOREVER.
1532 *
1533 * @return Address of the data item if successful; NULL if returned
1534 * without waiting, or waiting period timed out.
1535 */
Kumar Galacc334c72017-04-21 10:55:34 -05001536extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001537
1538/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001539 * @brief Remove an element from a queue.
1540 *
1541 * This routine removes data item from @a queue. The first 32 bits of the
1542 * data item are reserved for the kernel's use. Removing elements from k_queue
1543 * rely on sys_slist_find_and_remove which is not a constant time operation.
1544 *
1545 * @note Can be called by ISRs
1546 *
1547 * @param queue Address of the queue.
1548 * @param data Address of the data item.
1549 *
1550 * @return true if data item was removed
1551 */
1552static inline bool k_queue_remove(struct k_queue *queue, void *data)
1553{
1554 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1555}
1556
1557/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001558 * @brief Query a queue to see if it has data available.
1559 *
1560 * Note that the data might be already gone by the time this function returns
1561 * if other threads are also trying to read from the queue.
1562 *
1563 * @note Can be called by ISRs.
1564 *
1565 * @param queue Address of the queue.
1566 *
1567 * @return Non-zero if the queue is empty.
1568 * @return 0 if data is available.
1569 */
1570static inline int k_queue_is_empty(struct k_queue *queue)
1571{
1572 return (int)sys_slist_is_empty(&queue->data_q);
1573}
1574
1575/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001576 * @brief Peek element at the head of queue.
1577 *
1578 * Return element from the head of queue without removing it.
1579 *
1580 * @param queue Address of the queue.
1581 *
1582 * @return Head element, or NULL if queue is empty.
1583 */
1584static inline void *k_queue_peek_head(struct k_queue *queue)
1585{
1586 return sys_slist_peek_head(&queue->data_q);
1587}
1588
1589/**
1590 * @brief Peek element at the tail of queue.
1591 *
1592 * Return element from the tail of queue without removing it.
1593 *
1594 * @param queue Address of the queue.
1595 *
1596 * @return Tail element, or NULL if queue is empty.
1597 */
1598static inline void *k_queue_peek_tail(struct k_queue *queue)
1599{
1600 return sys_slist_peek_tail(&queue->data_q);
1601}
1602
1603/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001604 * @brief Statically define and initialize a queue.
1605 *
1606 * The queue can be accessed outside the module where it is defined using:
1607 *
1608 * @code extern struct k_queue <name>; @endcode
1609 *
1610 * @param name Name of the queue.
1611 */
1612#define K_QUEUE_DEFINE(name) \
1613 struct k_queue name \
1614 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001615 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001616
1617/**
1618 * @} end defgroup queue_apis
1619 */
1620
1621/**
1622 * @cond INTERNAL_HIDDEN
1623 */
1624
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001626 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001627};
1628
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001629#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001630 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001631 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001632 }
1633
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001634#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1635
Allan Stephensc98da842016-11-11 15:45:03 -05001636/**
1637 * INTERNAL_HIDDEN @endcond
1638 */
1639
1640/**
1641 * @defgroup fifo_apis Fifo APIs
1642 * @ingroup kernel_apis
1643 * @{
1644 */
1645
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001647 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652 *
1653 * @return N/A
1654 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001655#define k_fifo_init(fifo) \
1656 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001657
1658/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001659 * @brief Cancel waiting on a fifo.
1660 *
1661 * This routine causes first thread pending on @a fifo, if any, to
1662 * return from k_fifo_get() call with NULL value (as if timeout
1663 * expired).
1664 *
1665 * @note Can be called by ISRs.
1666 *
1667 * @param fifo Address of the fifo.
1668 *
1669 * @return N/A
1670 */
1671#define k_fifo_cancel_wait(fifo) \
1672 k_queue_cancel_wait((struct k_queue *) fifo)
1673
1674/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001675 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * This routine adds a data item to @a fifo. A fifo data item must be
1678 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1679 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001681 * @note Can be called by ISRs.
1682 *
1683 * @param fifo Address of the fifo.
1684 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001685 *
1686 * @return N/A
1687 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001688#define k_fifo_put(fifo, data) \
1689 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001690
1691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * This routine adds a list of data items to @a fifo in one operation.
1695 * The data items must be in a singly-linked list, with the first 32 bits
1696 * each data item pointing to the next data item; the list must be
1697 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * @param fifo Address of the fifo.
1702 * @param head Pointer to first node in singly-linked list.
1703 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001704 *
1705 * @return N/A
1706 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001707#define k_fifo_put_list(fifo, head, tail) \
1708 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709
1710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * This routine adds a list of data items to @a fifo in one operation.
1714 * The data items must be in a singly-linked list implemented using a
1715 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001716 * and must be re-initialized via sys_slist_init().
1717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @note Can be called by ISRs.
1719 *
1720 * @param fifo Address of the fifo.
1721 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 *
1723 * @return N/A
1724 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001725#define k_fifo_put_slist(fifo, list) \
1726 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727
1728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * This routine removes a data item from @a fifo in a "first in, first out"
1732 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1735 *
1736 * @param fifo Address of the fifo.
1737 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 * or one of the special values K_NO_WAIT and K_FOREVER.
1739 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001740 * @return Address of the data item if successful; NULL if returned
1741 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001743#define k_fifo_get(fifo, timeout) \
1744 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001745
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001747 * @brief Query a fifo to see if it has data available.
1748 *
1749 * Note that the data might be already gone by the time this function returns
1750 * if other threads is also trying to read from the fifo.
1751 *
1752 * @note Can be called by ISRs.
1753 *
1754 * @param fifo Address of the fifo.
1755 *
1756 * @return Non-zero if the fifo is empty.
1757 * @return 0 if data is available.
1758 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001759#define k_fifo_is_empty(fifo) \
1760 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001761
1762/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001763 * @brief Peek element at the head of fifo.
1764 *
1765 * Return element from the head of fifo without removing it. A usecase
1766 * for this is if elements of the fifo are themselves containers. Then
1767 * on each iteration of processing, a head container will be peeked,
1768 * and some data processed out of it, and only if the container is empty,
1769 * it will be completely remove from the fifo.
1770 *
1771 * @param fifo Address of the fifo.
1772 *
1773 * @return Head element, or NULL if the fifo is empty.
1774 */
1775#define k_fifo_peek_head(fifo) \
1776 k_queue_peek_head((struct k_queue *) fifo)
1777
1778/**
1779 * @brief Peek element at the tail of fifo.
1780 *
1781 * Return element from the tail of fifo (without removing it). A usecase
1782 * for this is if elements of the fifo are themselves containers. Then
1783 * it may be useful to add more data to the last container in fifo.
1784 *
1785 * @param fifo Address of the fifo.
1786 *
1787 * @return Tail element, or NULL if fifo is empty.
1788 */
1789#define k_fifo_peek_tail(fifo) \
1790 k_queue_peek_tail((struct k_queue *) fifo)
1791
1792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001797 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001801#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001802 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001803 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001804 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805
Allan Stephensc98da842016-11-11 15:45:03 -05001806/**
1807 * @} end defgroup fifo_apis
1808 */
1809
1810/**
1811 * @cond INTERNAL_HIDDEN
1812 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813
1814struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001815 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001816};
1817
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001818#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001819 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001820 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001821 }
1822
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001823#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1824
Allan Stephensc98da842016-11-11 15:45:03 -05001825/**
1826 * INTERNAL_HIDDEN @endcond
1827 */
1828
1829/**
1830 * @defgroup lifo_apis Lifo APIs
1831 * @ingroup kernel_apis
1832 * @{
1833 */
1834
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001840 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001841 *
1842 * @return N/A
1843 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001844#define k_lifo_init(lifo) \
1845 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846
1847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * This routine adds a data item to @a lifo. A lifo data item must be
1851 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1852 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * @note Can be called by ISRs.
1855 *
1856 * @param lifo Address of the lifo.
1857 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001858 *
1859 * @return N/A
1860 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001861#define k_lifo_put(lifo, data) \
1862 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863
1864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001867 * This routine removes a data item from @a lifo in a "last in, first out"
1868 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1871 *
1872 * @param lifo Address of the lifo.
1873 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001874 * or one of the special values K_NO_WAIT and K_FOREVER.
1875 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001876 * @return Address of the data item if successful; NULL if returned
1877 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001879#define k_lifo_get(lifo, timeout) \
1880 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001881
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001883 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001885 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001886 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001887 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001891#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001892 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001893 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001894 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001895
Allan Stephensc98da842016-11-11 15:45:03 -05001896/**
1897 * @} end defgroup lifo_apis
1898 */
1899
1900/**
1901 * @cond INTERNAL_HIDDEN
1902 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903
1904struct k_stack {
1905 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001906 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001907
Anas Nashif2f203c22016-12-18 06:57:45 -05001908 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909};
1910
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001911#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001912 { \
1913 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1914 .base = stack_buffer, \
1915 .next = stack_buffer, \
1916 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001917 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001918 }
1919
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001920#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1921
Allan Stephensc98da842016-11-11 15:45:03 -05001922/**
1923 * INTERNAL_HIDDEN @endcond
1924 */
1925
1926/**
1927 * @defgroup stack_apis Stack APIs
1928 * @ingroup kernel_apis
1929 * @{
1930 */
1931
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001932/**
1933 * @brief Initialize a stack.
1934 *
1935 * This routine initializes a stack object, prior to its first use.
1936 *
1937 * @param stack Address of the stack.
1938 * @param buffer Address of array used to hold stacked values.
1939 * @param num_entries Maximum number of values that can be stacked.
1940 *
1941 * @return N/A
1942 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001943extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001944 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001945
1946/**
1947 * @brief Push an element onto a stack.
1948 *
1949 * This routine adds a 32-bit value @a data to @a stack.
1950 *
1951 * @note Can be called by ISRs.
1952 *
1953 * @param stack Address of the stack.
1954 * @param data Value to push onto the stack.
1955 *
1956 * @return N/A
1957 */
Kumar Galacc334c72017-04-21 10:55:34 -05001958extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959
1960/**
1961 * @brief Pop an element from a stack.
1962 *
1963 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1964 * manner and stores the value in @a data.
1965 *
1966 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1967 *
1968 * @param stack Address of the stack.
1969 * @param data Address of area to hold the value popped from the stack.
1970 * @param timeout Waiting period to obtain a value (in milliseconds),
1971 * or one of the special values K_NO_WAIT and K_FOREVER.
1972 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001973 * @retval 0 Element popped from stack.
1974 * @retval -EBUSY Returned without waiting.
1975 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001976 */
Kumar Galacc334c72017-04-21 10:55:34 -05001977extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001978
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001982 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001984 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * @param name Name of the stack.
1987 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001989#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001990 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001991 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001992 struct k_stack name \
1993 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001994 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001995 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997/**
Allan Stephensc98da842016-11-11 15:45:03 -05001998 * @} end defgroup stack_apis
1999 */
2000
Allan Stephens6bba9b02016-11-16 14:56:54 -05002001struct k_work;
2002
Allan Stephensc98da842016-11-11 15:45:03 -05002003/**
2004 * @defgroup workqueue_apis Workqueue Thread APIs
2005 * @ingroup kernel_apis
2006 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007 */
2008
Allan Stephens6bba9b02016-11-16 14:56:54 -05002009/**
2010 * @typedef k_work_handler_t
2011 * @brief Work item handler function type.
2012 *
2013 * A work item's handler function is executed by a workqueue's thread
2014 * when the work item is processed by the workqueue.
2015 *
2016 * @param work Address of the work item.
2017 *
2018 * @return N/A
2019 */
2020typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021
2022/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002023 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002024 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002025
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002027 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002028 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029};
2030
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002032 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033};
2034
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002035struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002036 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037 k_work_handler_t handler;
2038 atomic_t flags[1];
2039};
2040
Allan Stephens6bba9b02016-11-16 14:56:54 -05002041struct k_delayed_work {
2042 struct k_work work;
2043 struct _timeout timeout;
2044 struct k_work_q *work_q;
2045};
2046
2047extern struct k_work_q k_sys_work_q;
2048
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002050 * INTERNAL_HIDDEN @endcond
2051 */
2052
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002053#define _K_WORK_INITIALIZER(work_handler) \
2054 { \
2055 ._reserved = NULL, \
2056 .handler = work_handler, \
2057 .flags = { 0 } \
2058 }
2059
2060#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2061
Allan Stephens6bba9b02016-11-16 14:56:54 -05002062/**
2063 * @brief Initialize a statically-defined work item.
2064 *
2065 * This macro can be used to initialize a statically-defined workqueue work
2066 * item, prior to its first use. For example,
2067 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002068 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002069 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002070 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002071 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002073#define K_WORK_DEFINE(work, work_handler) \
2074 struct k_work work \
2075 __in_section(_k_work, static, work) = \
2076 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002077
2078/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002079 * @brief Initialize a work item.
2080 *
2081 * This routine initializes a workqueue work item, prior to its first use.
2082 *
2083 * @param work Address of work item.
2084 * @param handler Function to invoke each time work item is processed.
2085 *
2086 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087 */
2088static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2089{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002090 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002092 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093}
2094
2095/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002096 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002097 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002098 * This routine submits work item @a work to be processed by workqueue
2099 * @a work_q. If the work item is already pending in the workqueue's queue
2100 * as a result of an earlier submission, this routine has no effect on the
2101 * work item. If the work item has already been processed, or is currently
2102 * being processed, its work is considered complete and the work item can be
2103 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002104 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002105 * @warning
2106 * A submitted work item must not be modified until it has been processed
2107 * by the workqueue.
2108 *
2109 * @note Can be called by ISRs.
2110 *
2111 * @param work_q Address of workqueue.
2112 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002113 *
2114 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115 */
2116static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2117 struct k_work *work)
2118{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002119 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002120 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002121 }
2122}
2123
2124/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002125 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127 * This routine indicates if work item @a work is pending in a workqueue's
2128 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002129 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002130 * @note Can be called by ISRs.
2131 *
2132 * @param work Address of work item.
2133 *
2134 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002135 */
2136static inline int k_work_pending(struct k_work *work)
2137{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002138 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002139}
2140
2141/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002142 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002144 * This routine starts workqueue @a work_q. The workqueue spawns its work
2145 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002147 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002148 * @param stack Pointer to work queue thread's stack space, as defined by
2149 * K_THREAD_STACK_DEFINE()
2150 * @param stack_size Size of the work queue thread's stack (in bytes), which
2151 * should either be the same constant passed to
2152 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002153 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
2155 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156 */
Andrew Boie507852a2017-07-25 18:47:07 -07002157extern void k_work_q_start(struct k_work_q *work_q,
2158 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002159 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002162 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164 * This routine initializes a workqueue delayed work item, prior to
2165 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002167 * @param work Address of delayed work item.
2168 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
2170 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002172extern void k_delayed_work_init(struct k_delayed_work *work,
2173 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174
2175/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002176 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002178 * This routine schedules work item @a work to be processed by workqueue
2179 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002180 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002181 * Only when the countdown completes is the work item actually submitted to
2182 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * Submitting a previously submitted delayed work item that is still
2185 * counting down cancels the existing submission and restarts the countdown
2186 * using the new delay. If the work item is currently pending on the
2187 * workqueue's queue because the countdown has completed it is too late to
2188 * resubmit the item, and resubmission fails without impacting the work item.
2189 * If the work item has already been processed, or is currently being processed,
2190 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * @warning
2193 * A delayed work item must not be modified until it has been processed
2194 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002196 * @note Can be called by ISRs.
2197 *
2198 * @param work_q Address of workqueue.
2199 * @param work Address of delayed work item.
2200 * @param delay Delay before submitting the work item (in milliseconds).
2201 *
2202 * @retval 0 Work item countdown started.
2203 * @retval -EINPROGRESS Work item is already pending.
2204 * @retval -EINVAL Work item is being processed or has completed its work.
2205 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002206 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002207extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2208 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002209 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210
2211/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002215 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002216 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @param work Address of delayed work item.
2221 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002222 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002223 * @retval -EINPROGRESS Work item is already pending.
2224 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002226extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002229 * @brief Submit a work item to the system workqueue.
2230 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * This routine submits work item @a work to be processed by the system
2232 * workqueue. If the work item is already pending in the workqueue's queue
2233 * as a result of an earlier submission, this routine has no effect on the
2234 * work item. If the work item has already been processed, or is currently
2235 * being processed, its work is considered complete and the work item can be
2236 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238 * @warning
2239 * Work items submitted to the system workqueue should avoid using handlers
2240 * that block or yield since this may prevent the system workqueue from
2241 * processing other work items in a timely manner.
2242 *
2243 * @note Can be called by ISRs.
2244 *
2245 * @param work Address of work item.
2246 *
2247 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248 */
2249static inline void k_work_submit(struct k_work *work)
2250{
2251 k_work_submit_to_queue(&k_sys_work_q, work);
2252}
2253
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255 * @brief Submit a delayed work item to the system workqueue.
2256 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002257 * This routine schedules work item @a work to be processed by the system
2258 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002259 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * Only when the countdown completes is the work item actually submitted to
2261 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002263 * Submitting a previously submitted delayed work item that is still
2264 * counting down cancels the existing submission and restarts the countdown
2265 * using the new delay. If the work item is currently pending on the
2266 * workqueue's queue because the countdown has completed it is too late to
2267 * resubmit the item, and resubmission fails without impacting the work item.
2268 * If the work item has already been processed, or is currently being processed,
2269 * its work is considered complete and the work item can be resubmitted.
2270 *
2271 * @warning
2272 * Work items submitted to the system workqueue should avoid using handlers
2273 * that block or yield since this may prevent the system workqueue from
2274 * processing other work items in a timely manner.
2275 *
2276 * @note Can be called by ISRs.
2277 *
2278 * @param work Address of delayed work item.
2279 * @param delay Delay before submitting the work item (in milliseconds).
2280 *
2281 * @retval 0 Work item countdown started.
2282 * @retval -EINPROGRESS Work item is already pending.
2283 * @retval -EINVAL Work item is being processed or has completed its work.
2284 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285 */
2286static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002287 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002289 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290}
2291
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002293 * @brief Get time remaining before a delayed work gets scheduled.
2294 *
2295 * This routine computes the (approximate) time remaining before a
2296 * delayed work gets executed. If the delayed work is not waiting to be
2297 * schedules, it returns zero.
2298 *
2299 * @param work Delayed work item.
2300 *
2301 * @return Remaining time (in milliseconds).
2302 */
Kumar Galacc334c72017-04-21 10:55:34 -05002303static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002304{
2305 return _timeout_remaining_get(&work->timeout);
2306}
2307
2308/**
Allan Stephensc98da842016-11-11 15:45:03 -05002309 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310 */
2311
Allan Stephensc98da842016-11-11 15:45:03 -05002312/**
2313 * @cond INTERNAL_HIDDEN
2314 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315
2316struct k_mutex {
2317 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002318 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002319 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321
Anas Nashif2f203c22016-12-18 06:57:45 -05002322 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002323};
2324
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002325#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002326 { \
2327 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2328 .owner = NULL, \
2329 .lock_count = 0, \
2330 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002331 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332 }
2333
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002334#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2335
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336/**
Allan Stephensc98da842016-11-11 15:45:03 -05002337 * INTERNAL_HIDDEN @endcond
2338 */
2339
2340/**
2341 * @defgroup mutex_apis Mutex APIs
2342 * @ingroup kernel_apis
2343 * @{
2344 */
2345
2346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002351 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002354 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002356 struct k_mutex name \
2357 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002358 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002363 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365 * Upon completion, the mutex is available and does not have an owner.
2366 *
2367 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002368 *
2369 * @return N/A
2370 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372
2373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 * This routine locks @a mutex. If the mutex is locked by another thread,
2377 * the calling thread waits until the mutex becomes available or until
2378 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * A thread is permitted to lock a mutex it has already locked. The operation
2381 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @param mutex Address of the mutex.
2384 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002385 * or one of the special values K_NO_WAIT and K_FOREVER.
2386 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002387 * @retval 0 Mutex locked.
2388 * @retval -EBUSY Returned without waiting.
2389 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390 */
Kumar Galacc334c72017-04-21 10:55:34 -05002391extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392
2393/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * This routine unlocks @a mutex. The mutex must already be locked by the
2397 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 *
2399 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 * thread.
2402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
2405 * @return N/A
2406 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407extern void k_mutex_unlock(struct k_mutex *mutex);
2408
Allan Stephensc98da842016-11-11 15:45:03 -05002409/**
2410 * @} end defgroup mutex_apis
2411 */
2412
2413/**
2414 * @cond INTERNAL_HIDDEN
2415 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416
2417struct k_sem {
2418 _wait_q_t wait_q;
2419 unsigned int count;
2420 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002421 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422
Anas Nashif2f203c22016-12-18 06:57:45 -05002423 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424};
2425
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002426#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002427 { \
2428 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2429 .count = initial_count, \
2430 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002431 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002432 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002433 }
2434
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002435#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2436
Allan Stephensc98da842016-11-11 15:45:03 -05002437/**
2438 * INTERNAL_HIDDEN @endcond
2439 */
2440
2441/**
2442 * @defgroup semaphore_apis Semaphore APIs
2443 * @ingroup kernel_apis
2444 * @{
2445 */
2446
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * @param sem Address of the semaphore.
2453 * @param initial_count Initial semaphore count.
2454 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002455 *
2456 * @return N/A
2457 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2459 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002460
2461/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002462 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002464 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2467 *
2468 * @param sem Address of the semaphore.
2469 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002470 * or one of the special values K_NO_WAIT and K_FOREVER.
2471 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002472 * @note When porting code from the nanokernel legacy API to the new API, be
2473 * careful with the return value of this function. The return value is the
2474 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2475 * non-zero means failure, while the nano_sem_take family returns 1 for success
2476 * and 0 for failure.
2477 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002478 * @retval 0 Semaphore taken.
2479 * @retval -EBUSY Returned without waiting.
2480 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002481 */
Kumar Galacc334c72017-04-21 10:55:34 -05002482extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002483
2484/**
2485 * @brief Give a semaphore.
2486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * This routine gives @a sem, unless the semaphore is already at its maximum
2488 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002493 *
2494 * @return N/A
2495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496extern void k_sem_give(struct k_sem *sem);
2497
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002498/**
2499 * @brief Reset a semaphore's count to zero.
2500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002504 *
2505 * @return N/A
2506 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002507static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508{
2509 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510}
2511
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512/**
2513 * @brief Get a semaphore's count.
2514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002521static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522{
2523 return sem->count;
2524}
2525
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002530 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002531 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @param name Name of the semaphore.
2534 * @param initial_count Initial semaphore count.
2535 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002536 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002538 struct k_sem name \
2539 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002540 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541
Allan Stephensc98da842016-11-11 15:45:03 -05002542/**
2543 * @} end defgroup semaphore_apis
2544 */
2545
2546/**
2547 * @defgroup alert_apis Alert APIs
2548 * @ingroup kernel_apis
2549 * @{
2550 */
2551
Allan Stephens5eceb852016-11-16 10:16:30 -05002552/**
2553 * @typedef k_alert_handler_t
2554 * @brief Alert handler function type.
2555 *
2556 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002557 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002558 * and is only invoked if the alert has been initialized with one.
2559 *
2560 * @param alert Address of the alert.
2561 *
2562 * @return 0 if alert has been consumed; non-zero if alert should pend.
2563 */
2564typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002565
2566/**
2567 * @} end defgroup alert_apis
2568 */
2569
2570/**
2571 * @cond INTERNAL_HIDDEN
2572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002574#define K_ALERT_DEFAULT NULL
2575#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002577struct k_alert {
2578 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579 atomic_t send_count;
2580 struct k_work work_item;
2581 struct k_sem sem;
2582
Anas Nashif2f203c22016-12-18 06:57:45 -05002583 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584};
2585
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002586extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002588#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002590 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002592 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2593 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002594 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595 }
2596
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002597#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2598
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002599/**
Allan Stephensc98da842016-11-11 15:45:03 -05002600 * INTERNAL_HIDDEN @endcond
2601 */
2602
2603/**
2604 * @addtogroup alert_apis
2605 * @{
2606 */
2607
2608/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002609 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002610 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002611 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002612 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002613 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @param name Name of the alert.
2616 * @param alert_handler Action to take when alert is sent. Specify either
2617 * the address of a function to be invoked by the system workqueue
2618 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2619 * K_ALERT_DEFAULT (which causes the alert to pend).
2620 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002622#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002623 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002624 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002625 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002626 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @param alert Address of the alert.
2634 * @param handler Action to take when alert is sent. Specify either the address
2635 * of a function to be invoked by the system workqueue thread,
2636 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2637 * K_ALERT_DEFAULT (which causes the alert to pend).
2638 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
2640 * @return N/A
2641 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002642extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2643 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644
2645/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2651 *
2652 * @param alert Address of the alert.
2653 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 * or one of the special values K_NO_WAIT and K_FOREVER.
2655 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002656 * @retval 0 Alert received.
2657 * @retval -EBUSY Returned without waiting.
2658 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002659 */
Kumar Galacc334c72017-04-21 10:55:34 -05002660extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661
2662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * This routine signals @a alert. The action specified for @a alert will
2666 * be taken, which may trigger the execution of an alert handler function
2667 * and/or cause the alert to pend (assuming the alert has not reached its
2668 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @note Can be called by ISRs.
2671 *
2672 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 *
2674 * @return N/A
2675 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002676extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677
2678/**
Allan Stephensc98da842016-11-11 15:45:03 -05002679 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680 */
2681
Allan Stephensc98da842016-11-11 15:45:03 -05002682/**
2683 * @cond INTERNAL_HIDDEN
2684 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685
2686struct k_msgq {
2687 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002688 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002689 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 char *buffer_start;
2691 char *buffer_end;
2692 char *read_ptr;
2693 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002694 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695
Anas Nashif2f203c22016-12-18 06:57:45 -05002696 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697};
2698
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002699#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700 { \
2701 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002702 .max_msgs = q_max_msgs, \
2703 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002705 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 .read_ptr = q_buffer, \
2707 .write_ptr = q_buffer, \
2708 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002709 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710 }
2711
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002712#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2713
Peter Mitsis1da807e2016-10-06 11:36:59 -04002714/**
Allan Stephensc98da842016-11-11 15:45:03 -05002715 * INTERNAL_HIDDEN @endcond
2716 */
2717
2718/**
2719 * @defgroup msgq_apis Message Queue APIs
2720 * @ingroup kernel_apis
2721 * @{
2722 */
2723
2724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002725 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2728 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002729 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2730 * message is similarly aligned to this boundary, @a q_msg_size must also be
2731 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * The message queue can be accessed outside the module where it is defined
2734 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002735 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002736 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * @param q_name Name of the message queue.
2739 * @param q_msg_size Message size (in bytes).
2740 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002741 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002742 */
2743#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2744 static char __noinit __aligned(q_align) \
2745 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002746 struct k_msgq q_name \
2747 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002748 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002749 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750
Peter Mitsisd7a37502016-10-13 11:37:40 -04002751/**
2752 * @brief Initialize a message queue.
2753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * This routine initializes a message queue object, prior to its first use.
2755 *
Allan Stephensda827222016-11-09 14:23:58 -06002756 * The message queue's ring buffer must contain space for @a max_msgs messages,
2757 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2758 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2759 * that each message is similarly aligned to this boundary, @a q_msg_size
2760 * must also be a multiple of N.
2761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * @param q Address of the message queue.
2763 * @param buffer Pointer to ring buffer that holds queued messages.
2764 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002765 * @param max_msgs Maximum number of messages that can be queued.
2766 *
2767 * @return N/A
2768 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002769extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002770 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002771
2772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002773 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002776 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002777 * @note Can be called by ISRs.
2778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * @param q Address of the message queue.
2780 * @param data Pointer to the message.
2781 * @param timeout Waiting period to add the message (in milliseconds),
2782 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002783 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002784 * @retval 0 Message sent.
2785 * @retval -ENOMSG Returned without waiting or queue purged.
2786 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002787 */
Kumar Galacc334c72017-04-21 10:55:34 -05002788extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002789
2790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002791 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * This routine receives a message from message queue @a q in a "first in,
2794 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002795 *
Allan Stephensc98da842016-11-11 15:45:03 -05002796 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @param q Address of the message queue.
2799 * @param data Address of area to hold the received message.
2800 * @param timeout Waiting period to receive the message (in milliseconds),
2801 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002802 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002803 * @retval 0 Message received.
2804 * @retval -ENOMSG Returned without waiting.
2805 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002806 */
Kumar Galacc334c72017-04-21 10:55:34 -05002807extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002808
2809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002810 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * This routine discards all unreceived messages in a message queue's ring
2813 * buffer. Any threads that are blocked waiting to send a message to the
2814 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002817 *
2818 * @return N/A
2819 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820extern void k_msgq_purge(struct k_msgq *q);
2821
Peter Mitsis67be2492016-10-07 11:44:34 -04002822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * This routine returns the number of unused entries in a message queue's
2826 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @param q Address of the message queue.
2829 *
2830 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002831 */
Kumar Galacc334c72017-04-21 10:55:34 -05002832static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002833{
2834 return q->max_msgs - q->used_msgs;
2835}
2836
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * @param q Address of the message queue.
2843 *
2844 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002845 */
Kumar Galacc334c72017-04-21 10:55:34 -05002846static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847{
2848 return q->used_msgs;
2849}
2850
Allan Stephensc98da842016-11-11 15:45:03 -05002851/**
2852 * @} end defgroup msgq_apis
2853 */
2854
2855/**
2856 * @defgroup mem_pool_apis Memory Pool APIs
2857 * @ingroup kernel_apis
2858 * @{
2859 */
2860
Andy Ross73cb9582017-05-09 10:42:39 -07002861/* Note on sizing: the use of a 20 bit field for block means that,
2862 * assuming a reasonable minimum block size of 16 bytes, we're limited
2863 * to 16M of memory managed by a single pool. Long term it would be
2864 * good to move to a variable bit size based on configuration.
2865 */
2866struct k_mem_block_id {
2867 u32_t pool : 8;
2868 u32_t level : 4;
2869 u32_t block : 20;
2870};
2871
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002873 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002874 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875};
2876
Allan Stephensc98da842016-11-11 15:45:03 -05002877/**
2878 * @} end defgroup mem_pool_apis
2879 */
2880
2881/**
2882 * @defgroup mailbox_apis Mailbox APIs
2883 * @ingroup kernel_apis
2884 * @{
2885 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002886
2887struct k_mbox_msg {
2888 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002889 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002890 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002891 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002892 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002893 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 /** sender's message data buffer */
2895 void *tx_data;
2896 /** internal use only - needed for legacy API support */
2897 void *_rx_data;
2898 /** message data block descriptor */
2899 struct k_mem_block tx_block;
2900 /** source thread id */
2901 k_tid_t rx_source_thread;
2902 /** target thread id */
2903 k_tid_t tx_target_thread;
2904 /** internal use only - thread waiting on send (may be a dummy) */
2905 k_tid_t _syncing_thread;
2906#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2907 /** internal use only - semaphore used during asynchronous send */
2908 struct k_sem *_async_sem;
2909#endif
2910};
2911
Allan Stephensc98da842016-11-11 15:45:03 -05002912/**
2913 * @cond INTERNAL_HIDDEN
2914 */
2915
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916struct k_mbox {
2917 _wait_q_t tx_msg_queue;
2918 _wait_q_t rx_msg_queue;
2919
Anas Nashif2f203c22016-12-18 06:57:45 -05002920 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921};
2922
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002923#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002924 { \
2925 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2926 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002927 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928 }
2929
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002930#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2931
Peter Mitsis12092702016-10-14 12:57:23 -04002932/**
Allan Stephensc98da842016-11-11 15:45:03 -05002933 * INTERNAL_HIDDEN @endcond
2934 */
2935
2936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002940 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002941 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002944 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002946 struct k_mbox name \
2947 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002948 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949
Peter Mitsis12092702016-10-14 12:57:23 -04002950/**
2951 * @brief Initialize a mailbox.
2952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * This routine initializes a mailbox object, prior to its first use.
2954 *
2955 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002956 *
2957 * @return N/A
2958 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959extern void k_mbox_init(struct k_mbox *mbox);
2960
Peter Mitsis12092702016-10-14 12:57:23 -04002961/**
2962 * @brief Send a mailbox message in a synchronous manner.
2963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * This routine sends a message to @a mbox and waits for a receiver to both
2965 * receive and process it. The message data may be in a buffer, in a memory
2966 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * @param mbox Address of the mailbox.
2969 * @param tx_msg Address of the transmit message descriptor.
2970 * @param timeout Waiting period for the message to be received (in
2971 * milliseconds), or one of the special values K_NO_WAIT
2972 * and K_FOREVER. Once the message has been received,
2973 * this routine waits as long as necessary for the message
2974 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002975 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002976 * @retval 0 Message sent.
2977 * @retval -ENOMSG Returned without waiting.
2978 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002979 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002980extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002981 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002982
Peter Mitsis12092702016-10-14 12:57:23 -04002983/**
2984 * @brief Send a mailbox message in an asynchronous manner.
2985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine sends a message to @a mbox without waiting for a receiver
2987 * to process it. The message data may be in a buffer, in a memory pool block,
2988 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2989 * will be given when the message has been both received and completely
2990 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * @param mbox Address of the mailbox.
2993 * @param tx_msg Address of the transmit message descriptor.
2994 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002995 *
2996 * @return N/A
2997 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002998extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 struct k_sem *sem);
3000
Peter Mitsis12092702016-10-14 12:57:23 -04003001/**
3002 * @brief Receive a mailbox message.
3003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * This routine receives a message from @a mbox, then optionally retrieves
3005 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * @param mbox Address of the mailbox.
3008 * @param rx_msg Address of the receive message descriptor.
3009 * @param buffer Address of the buffer to receive data, or NULL to defer data
3010 * retrieval and message disposal until later.
3011 * @param timeout Waiting period for a message to be received (in
3012 * milliseconds), or one of the special values K_NO_WAIT
3013 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003014 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003015 * @retval 0 Message received.
3016 * @retval -ENOMSG Returned without waiting.
3017 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003018 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003019extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003020 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003021
3022/**
3023 * @brief Retrieve mailbox message data into a buffer.
3024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * This routine completes the processing of a received message by retrieving
3026 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003027 *
3028 * Alternatively, this routine can be used to dispose of a received message
3029 * without retrieving its data.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @param rx_msg Address of the receive message descriptor.
3032 * @param buffer Address of the buffer to receive data, or NULL to discard
3033 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003034 *
3035 * @return N/A
3036 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003037extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003038
3039/**
3040 * @brief Retrieve mailbox message data into a memory pool block.
3041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003042 * This routine completes the processing of a received message by retrieving
3043 * its data into a memory pool block, then disposing of the message.
3044 * The memory pool block that results from successful retrieval must be
3045 * returned to the pool once the data has been processed, even in cases
3046 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003047 *
3048 * Alternatively, this routine can be used to dispose of a received message
3049 * without retrieving its data. In this case there is no need to return a
3050 * memory pool block to the pool.
3051 *
3052 * This routine allocates a new memory pool block for the data only if the
3053 * data is not already in one. If a new block cannot be allocated, the routine
3054 * returns a failure code and the received message is left unchanged. This
3055 * permits the caller to reattempt data retrieval at a later time or to dispose
3056 * of the received message without retrieving its data.
3057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @param rx_msg Address of a receive message descriptor.
3059 * @param pool Address of memory pool, or NULL to discard data.
3060 * @param block Address of the area to hold memory pool block info.
3061 * @param timeout Waiting period to wait for a memory pool block (in
3062 * milliseconds), or one of the special values K_NO_WAIT
3063 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003064 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003065 * @retval 0 Data retrieved.
3066 * @retval -ENOMEM Returned without waiting.
3067 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003068 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003069extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003070 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003071 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072
Allan Stephensc98da842016-11-11 15:45:03 -05003073/**
3074 * @} end defgroup mailbox_apis
3075 */
3076
3077/**
3078 * @cond INTERNAL_HIDDEN
3079 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080
3081struct k_pipe {
3082 unsigned char *buffer; /* Pipe buffer: may be NULL */
3083 size_t size; /* Buffer size */
3084 size_t bytes_used; /* # bytes used in buffer */
3085 size_t read_index; /* Where in buffer to read from */
3086 size_t write_index; /* Where in buffer to write */
3087
3088 struct {
3089 _wait_q_t readers; /* Reader wait queue */
3090 _wait_q_t writers; /* Writer wait queue */
3091 } wait_q;
3092
Anas Nashif2f203c22016-12-18 06:57:45 -05003093 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094};
3095
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003096#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 { \
3098 .buffer = pipe_buffer, \
3099 .size = pipe_buffer_size, \
3100 .bytes_used = 0, \
3101 .read_index = 0, \
3102 .write_index = 0, \
3103 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3104 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003105 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106 }
3107
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003108#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3109
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003110/**
Allan Stephensc98da842016-11-11 15:45:03 -05003111 * INTERNAL_HIDDEN @endcond
3112 */
3113
3114/**
3115 * @defgroup pipe_apis Pipe APIs
3116 * @ingroup kernel_apis
3117 * @{
3118 */
3119
3120/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003124 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003125 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @param name Name of the pipe.
3128 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3129 * or zero if no ring buffer is used.
3130 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003132#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3133 static unsigned char __noinit __aligned(pipe_align) \
3134 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003135 struct k_pipe name \
3136 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003137 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * This routine initializes a pipe object, prior to its first use.
3143 *
3144 * @param pipe Address of the pipe.
3145 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3146 * is used.
3147 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3148 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149 *
3150 * @return N/A
3151 */
3152extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3153 size_t size);
3154
3155/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * @param pipe Address of the pipe.
3161 * @param data Address of data to write.
3162 * @param bytes_to_write Size of data (in bytes).
3163 * @param bytes_written Address of area to hold the number of bytes written.
3164 * @param min_xfer Minimum number of bytes to write.
3165 * @param timeout Waiting period to wait for the data to be written (in
3166 * milliseconds), or one of the special values K_NO_WAIT
3167 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003169 * @retval 0 At least @a min_xfer bytes of data were written.
3170 * @retval -EIO Returned without waiting; zero data bytes were written.
3171 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003174extern int k_pipe_put(struct k_pipe *pipe, void *data,
3175 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003176 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177
3178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003183 * @param pipe Address of the pipe.
3184 * @param data Address to place the data read from pipe.
3185 * @param bytes_to_read Maximum number of data bytes to read.
3186 * @param bytes_read Address of area to hold the number of bytes read.
3187 * @param min_xfer Minimum number of data bytes to read.
3188 * @param timeout Waiting period to wait for the data to be read (in
3189 * milliseconds), or one of the special values K_NO_WAIT
3190 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003192 * @retval 0 At least @a min_xfer bytes of data were read.
3193 * @retval -EIO Returned without waiting; zero data bytes were read.
3194 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003197extern int k_pipe_get(struct k_pipe *pipe, void *data,
3198 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003199 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003200
3201/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * This routine writes the data contained in a memory block to @a pipe.
3205 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209 * @param block Memory block containing data to send
3210 * @param size Number of data bytes in memory block to send
3211 * @param sem Semaphore to signal upon completion (else NULL)
3212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214 */
3215extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3216 size_t size, struct k_sem *sem);
3217
3218/**
Allan Stephensc98da842016-11-11 15:45:03 -05003219 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220 */
3221
Allan Stephensc98da842016-11-11 15:45:03 -05003222/**
3223 * @cond INTERNAL_HIDDEN
3224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003226struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003228 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003229 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230 char *buffer;
3231 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003232 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233
Anas Nashif2f203c22016-12-18 06:57:45 -05003234 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235};
3236
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003237#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003238 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239 { \
3240 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003241 .num_blocks = slab_num_blocks, \
3242 .block_size = slab_block_size, \
3243 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244 .free_list = NULL, \
3245 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003246 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 }
3248
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003249#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3250
3251
Peter Mitsis578f9112016-10-07 13:50:31 -04003252/**
Allan Stephensc98da842016-11-11 15:45:03 -05003253 * INTERNAL_HIDDEN @endcond
3254 */
3255
3256/**
3257 * @defgroup mem_slab_apis Memory Slab APIs
3258 * @ingroup kernel_apis
3259 * @{
3260 */
3261
3262/**
Allan Stephensda827222016-11-09 14:23:58 -06003263 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003264 *
Allan Stephensda827222016-11-09 14:23:58 -06003265 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003267 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3268 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003269 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003270 *
Allan Stephensda827222016-11-09 14:23:58 -06003271 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003273 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003274 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * @param name Name of the memory slab.
3277 * @param slab_block_size Size of each memory block (in bytes).
3278 * @param slab_num_blocks Number memory blocks.
3279 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003280 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003281#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3282 char __noinit __aligned(slab_align) \
3283 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3284 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003285 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003286 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003287 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003289/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003290 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003293 *
Allan Stephensda827222016-11-09 14:23:58 -06003294 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3295 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3296 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3297 * To ensure that each memory block is similarly aligned to this boundary,
3298 * @a slab_block_size must also be a multiple of N.
3299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003300 * @param slab Address of the memory slab.
3301 * @param buffer Pointer to buffer used for the memory blocks.
3302 * @param block_size Size of each memory block (in bytes).
3303 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003304 *
3305 * @return N/A
3306 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003307extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003308 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003309
3310/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @param slab Address of the memory slab.
3316 * @param mem Pointer to block address area.
3317 * @param timeout Maximum time to wait for operation to complete
3318 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3319 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003320 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003321 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003323 * @retval -ENOMEM Returned without waiting.
3324 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003325 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003326extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003327 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003328
3329/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * This routine releases a previously allocated memory block back to its
3333 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * @param slab Address of the memory slab.
3336 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003337 *
3338 * @return N/A
3339 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003340extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003341
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * This routine gets the number of memory blocks that are currently
3346 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003351 */
Kumar Galacc334c72017-04-21 10:55:34 -05003352static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003354 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003355}
3356
Peter Mitsisc001aa82016-10-13 13:53:37 -04003357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * This routine gets the number of memory blocks that are currently
3361 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003366 */
Kumar Galacc334c72017-04-21 10:55:34 -05003367static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003368{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003369 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003370}
3371
Allan Stephensc98da842016-11-11 15:45:03 -05003372/**
3373 * @} end defgroup mem_slab_apis
3374 */
3375
3376/**
3377 * @cond INTERNAL_HIDDEN
3378 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379
Andy Ross73cb9582017-05-09 10:42:39 -07003380struct k_mem_pool_lvl {
3381 union {
3382 u32_t *bits_p;
3383 u32_t bits;
3384 };
3385 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003386};
3387
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003388struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003389 void *buf;
3390 size_t max_sz;
3391 u16_t n_max;
3392 u8_t n_levels;
3393 u8_t max_inline_level;
3394 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003395 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396};
3397
Andy Ross73cb9582017-05-09 10:42:39 -07003398#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399
Andy Ross73cb9582017-05-09 10:42:39 -07003400#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3401
3402#define _MPOOL_LVLS(maxsz, minsz) \
3403 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3404 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3405 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3406 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3407 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3408 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3409 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3410 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3411 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3412 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3413 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3414 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3415 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3416 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3417 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3418 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3419
3420/* Rounds the needed bits up to integer multiples of u32_t */
3421#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3422 ((((n_max) << (2*(l))) + 31) / 32)
3423
3424/* One word gets stored free unioned with the pointer, otherwise the
3425 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003426 */
Andy Ross73cb9582017-05-09 10:42:39 -07003427#define _MPOOL_LBIT_WORDS(n_max, l) \
3428 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3429 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003430
Andy Ross73cb9582017-05-09 10:42:39 -07003431/* How many bytes for the bitfields of a single level? */
3432#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3433 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3434 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003435
Andy Ross73cb9582017-05-09 10:42:39 -07003436/* Size of the bitmap array that follows the buffer in allocated memory */
3437#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3438 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3439 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3440 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3441 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3442 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3443 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3444 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3445 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3446 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3447 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3448 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3449 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3450 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3451 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3452 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3453 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003454
3455/**
Allan Stephensc98da842016-11-11 15:45:03 -05003456 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003457 */
3458
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003459/**
Allan Stephensc98da842016-11-11 15:45:03 -05003460 * @addtogroup mem_pool_apis
3461 * @{
3462 */
3463
3464/**
3465 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003467 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3468 * long. The memory pool allows blocks to be repeatedly partitioned into
3469 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003470 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003471 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003472 * If the pool is to be accessed outside the module where it is defined, it
3473 * can be declared via
3474 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003475 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003477 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003478 * @param minsz Size of the smallest blocks in the pool (in bytes).
3479 * @param maxsz Size of the largest blocks in the pool (in bytes).
3480 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003482 */
Andy Ross73cb9582017-05-09 10:42:39 -07003483#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3484 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3485 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3486 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3487 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3488 .buf = _mpool_buf_##name, \
3489 .max_sz = maxsz, \
3490 .n_max = nmax, \
3491 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3492 .levels = _mpool_lvls_##name, \
3493 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003494
Peter Mitsis937042c2016-10-13 13:18:26 -04003495/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003498 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * @param pool Address of the memory pool.
3501 * @param block Pointer to block descriptor for the allocated memory.
3502 * @param size Amount of memory to allocate (in bytes).
3503 * @param timeout Maximum time to wait for operation to complete
3504 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3505 * or K_FOREVER to wait as long as necessary.
3506 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003507 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003509 * @retval -ENOMEM Returned without waiting.
3510 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003511 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003512extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003513 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003514
3515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * This routine releases a previously allocated memory block back to its
3519 * memory pool.
3520 *
3521 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003522 *
3523 * @return N/A
3524 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003525extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003526
3527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003529 *
Andy Ross73cb9582017-05-09 10:42:39 -07003530 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 *
Andy Ross73cb9582017-05-09 10:42:39 -07003532 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003533 *
3534 * @return N/A
3535 */
Andy Ross73cb9582017-05-09 10:42:39 -07003536static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003537
3538/**
Allan Stephensc98da842016-11-11 15:45:03 -05003539 * @} end addtogroup mem_pool_apis
3540 */
3541
3542/**
3543 * @defgroup heap_apis Heap Memory Pool APIs
3544 * @ingroup kernel_apis
3545 * @{
3546 */
3547
3548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003552 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003557 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003558extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003559
3560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003562 *
3563 * This routine provides traditional free() semantics. The memory being
3564 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003565 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003566 * If @a ptr is NULL, no operation is performed.
3567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003569 *
3570 * @return N/A
3571 */
3572extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003573
Allan Stephensc98da842016-11-11 15:45:03 -05003574/**
3575 * @} end defgroup heap_apis
3576 */
3577
Benjamin Walshacc68c12017-01-29 18:57:45 -05003578/* polling API - PRIVATE */
3579
Benjamin Walshb0179862017-02-02 16:39:57 -05003580#ifdef CONFIG_POLL
3581#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3582#else
3583#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3584#endif
3585
Benjamin Walshacc68c12017-01-29 18:57:45 -05003586/* private - implementation data created as needed, per-type */
3587struct _poller {
3588 struct k_thread *thread;
3589};
3590
3591/* private - types bit positions */
3592enum _poll_types_bits {
3593 /* can be used to ignore an event */
3594 _POLL_TYPE_IGNORE,
3595
3596 /* to be signaled by k_poll_signal() */
3597 _POLL_TYPE_SIGNAL,
3598
3599 /* semaphore availability */
3600 _POLL_TYPE_SEM_AVAILABLE,
3601
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003602 /* queue/fifo/lifo data availability */
3603 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003604
3605 _POLL_NUM_TYPES
3606};
3607
3608#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3609
3610/* private - states bit positions */
3611enum _poll_states_bits {
3612 /* default state when creating event */
3613 _POLL_STATE_NOT_READY,
3614
Benjamin Walshacc68c12017-01-29 18:57:45 -05003615 /* signaled by k_poll_signal() */
3616 _POLL_STATE_SIGNALED,
3617
3618 /* semaphore is available */
3619 _POLL_STATE_SEM_AVAILABLE,
3620
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003621 /* data is available to read on queue/fifo/lifo */
3622 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003623
3624 _POLL_NUM_STATES
3625};
3626
3627#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3628
3629#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003630 (32 - (0 \
3631 + 8 /* tag */ \
3632 + _POLL_NUM_TYPES \
3633 + _POLL_NUM_STATES \
3634 + 1 /* modes */ \
3635 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003636
3637#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3638#error overflow of 32-bit word in struct k_poll_event
3639#endif
3640
3641/* end of polling API - PRIVATE */
3642
3643
3644/**
3645 * @defgroup poll_apis Async polling APIs
3646 * @ingroup kernel_apis
3647 * @{
3648 */
3649
3650/* Public polling API */
3651
3652/* public - values for k_poll_event.type bitfield */
3653#define K_POLL_TYPE_IGNORE 0
3654#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3655#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003656#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3657#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003658
3659/* public - polling modes */
3660enum k_poll_modes {
3661 /* polling thread does not take ownership of objects when available */
3662 K_POLL_MODE_NOTIFY_ONLY = 0,
3663
3664 K_POLL_NUM_MODES
3665};
3666
3667/* public - values for k_poll_event.state bitfield */
3668#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003669#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3670#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003671#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3672#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003673
3674/* public - poll signal object */
3675struct k_poll_signal {
3676 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003677 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003678
3679 /*
3680 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3681 * user resets it to 0.
3682 */
3683 unsigned int signaled;
3684
3685 /* custom result value passed to k_poll_signal() if needed */
3686 int result;
3687};
3688
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003689#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003690 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003691 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003692 .signaled = 0, \
3693 .result = 0, \
3694 }
3695
3696struct k_poll_event {
3697 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003698 sys_dnode_t _node;
3699
3700 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003701 struct _poller *poller;
3702
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003703 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003704 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003705
Benjamin Walshacc68c12017-01-29 18:57:45 -05003706 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003707 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003708
3709 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003710 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003711
3712 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003713 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003714
3715 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003716 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003717
3718 /* per-type data */
3719 union {
3720 void *obj;
3721 struct k_poll_signal *signal;
3722 struct k_sem *sem;
3723 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003724 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003725 };
3726};
3727
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003728#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003729 { \
3730 .poller = NULL, \
3731 .type = event_type, \
3732 .state = K_POLL_STATE_NOT_READY, \
3733 .mode = event_mode, \
3734 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003735 { .obj = event_obj }, \
3736 }
3737
3738#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3739 event_tag) \
3740 { \
3741 .type = event_type, \
3742 .tag = event_tag, \
3743 .state = K_POLL_STATE_NOT_READY, \
3744 .mode = event_mode, \
3745 .unused = 0, \
3746 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003747 }
3748
3749/**
3750 * @brief Initialize one struct k_poll_event instance
3751 *
3752 * After this routine is called on a poll event, the event it ready to be
3753 * placed in an event array to be passed to k_poll().
3754 *
3755 * @param event The event to initialize.
3756 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3757 * values. Only values that apply to the same object being polled
3758 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3759 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003760 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761 * @param obj Kernel object or poll signal.
3762 *
3763 * @return N/A
3764 */
3765
Kumar Galacc334c72017-04-21 10:55:34 -05003766extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003767 int mode, void *obj);
3768
3769/**
3770 * @brief Wait for one or many of multiple poll events to occur
3771 *
3772 * This routine allows a thread to wait concurrently for one or many of
3773 * multiple poll events to have occurred. Such events can be a kernel object
3774 * being available, like a semaphore, or a poll signal event.
3775 *
3776 * When an event notifies that a kernel object is available, the kernel object
3777 * is not "given" to the thread calling k_poll(): it merely signals the fact
3778 * that the object was available when the k_poll() call was in effect. Also,
3779 * all threads trying to acquire an object the regular way, i.e. by pending on
3780 * the object, have precedence over the thread polling on the object. This
3781 * means that the polling thread will never get the poll event on an object
3782 * until the object becomes available and its pend queue is empty. For this
3783 * reason, the k_poll() call is more effective when the objects being polled
3784 * only have one thread, the polling thread, trying to acquire them.
3785 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003786 * When k_poll() returns 0, the caller should loop on all the events that were
3787 * passed to k_poll() and check the state field for the values that were
3788 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003789 *
3790 * Before being reused for another call to k_poll(), the user has to reset the
3791 * state field to K_POLL_STATE_NOT_READY.
3792 *
3793 * @param events An array of pointers to events to be polled for.
3794 * @param num_events The number of events in the array.
3795 * @param timeout Waiting period for an event to be ready (in milliseconds),
3796 * or one of the special values K_NO_WAIT and K_FOREVER.
3797 *
3798 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003799 * @retval -EAGAIN Waiting period timed out.
3800 */
3801
3802extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003803 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003804
3805/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003806 * @brief Initialize a poll signal object.
3807 *
3808 * Ready a poll signal object to be signaled via k_poll_signal().
3809 *
3810 * @param signal A poll signal.
3811 *
3812 * @return N/A
3813 */
3814
3815extern void k_poll_signal_init(struct k_poll_signal *signal);
3816
3817/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003818 * @brief Signal a poll signal object.
3819 *
3820 * This routine makes ready a poll signal, which is basically a poll event of
3821 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3822 * made ready to run. A @a result value can be specified.
3823 *
3824 * The poll signal contains a 'signaled' field that, when set by
3825 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3826 * be reset by the user before being passed again to k_poll() or k_poll() will
3827 * consider it being signaled, and will return immediately.
3828 *
3829 * @param signal A poll signal.
3830 * @param result The value to store in the result field of the signal.
3831 *
3832 * @retval 0 The signal was delivered successfully.
3833 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3834 */
3835
3836extern int k_poll_signal(struct k_poll_signal *signal, int result);
3837
3838/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003839extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003840
3841/**
3842 * @} end defgroup poll_apis
3843 */
3844
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003845/**
3846 * @brief Make the CPU idle.
3847 *
3848 * This function makes the CPU idle until an event wakes it up.
3849 *
3850 * In a regular system, the idle thread should be the only thread responsible
3851 * for making the CPU idle and triggering any type of power management.
3852 * However, in some more constrained systems, such as a single-threaded system,
3853 * the only thread would be responsible for this if needed.
3854 *
3855 * @return N/A
3856 */
3857extern void k_cpu_idle(void);
3858
3859/**
3860 * @brief Make the CPU idle in an atomic fashion.
3861 *
3862 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3863 * must be done atomically before making the CPU idle.
3864 *
3865 * @param key Interrupt locking key obtained from irq_lock().
3866 *
3867 * @return N/A
3868 */
3869extern void k_cpu_atomic_idle(unsigned int key);
3870
Kumar Galacc334c72017-04-21 10:55:34 -05003871extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003872
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873#include <arch/cpu.h>
3874
Andrew Boiecdb94d62017-04-18 15:22:05 -07003875#ifdef _ARCH_EXCEPT
3876/* This archtecture has direct support for triggering a CPU exception */
3877#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3878#else
3879
3880#include <misc/printk.h>
3881
3882/* NOTE: This is the implementation for arches that do not implement
3883 * _ARCH_EXCEPT() to generate a real CPU exception.
3884 *
3885 * We won't have a real exception frame to determine the PC value when
3886 * the oops occurred, so print file and line number before we jump into
3887 * the fatal error handler.
3888 */
3889#define _k_except_reason(reason) do { \
3890 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3891 _NanoFatalErrorHandler(reason, &_default_esf); \
3892 CODE_UNREACHABLE; \
3893 } while (0)
3894
3895#endif /* _ARCH__EXCEPT */
3896
3897/**
3898 * @brief Fatally terminate a thread
3899 *
3900 * This should be called when a thread has encountered an unrecoverable
3901 * runtime condition and needs to terminate. What this ultimately
3902 * means is determined by the _fatal_error_handler() implementation, which
3903 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3904 *
3905 * If this is called from ISR context, the default system fatal error handler
3906 * will treat it as an unrecoverable system error, just like k_panic().
3907 */
3908#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3909
3910/**
3911 * @brief Fatally terminate the system
3912 *
3913 * This should be called when the Zephyr kernel has encountered an
3914 * unrecoverable runtime condition and needs to terminate. What this ultimately
3915 * means is determined by the _fatal_error_handler() implementation, which
3916 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3917 */
3918#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3919
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003920/*
3921 * private APIs that are utilized by one or more public APIs
3922 */
3923
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003924#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003925extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003926#else
3927#define _init_static_threads() do { } while ((0))
3928#endif
3929
3930extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003931extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932
Andrew Boiedc5d9352017-06-02 12:56:47 -07003933/* arch/cpu.h may declare an architecture or platform-specific macro
3934 * for properly declaring stacks, compatible with MMU/MPU constraints if
3935 * enabled
3936 */
3937#ifdef _ARCH_THREAD_STACK_DEFINE
3938#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3939#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3940 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3941#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3942#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003943static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3944{
3945 return _ARCH_THREAD_STACK_BUFFER(sym);
3946}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003947#else
3948/**
3949 * @brief Declare a toplevel thread stack memory region
3950 *
3951 * This declares a region of memory suitable for use as a thread's stack.
3952 *
3953 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3954 * 'noinit' section so that it isn't zeroed at boot
3955 *
Andrew Boie507852a2017-07-25 18:47:07 -07003956 * The declared symbol will always be a k_thread_stack_t which can be passed to
3957 * k_thread_create, but should otherwise not be manipulated. If the buffer
3958 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003959 *
3960 * It is legal to precede this definition with the 'static' keyword.
3961 *
3962 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3963 * parameter of k_thread_create(), it may not be the same as the
3964 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3965 *
3966 * @param sym Thread stack symbol name
3967 * @param size Size of the stack memory region
3968 */
3969#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003970 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003971
3972/**
3973 * @brief Declare a toplevel array of thread stack memory regions
3974 *
3975 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3976 * definition for additional details and constraints.
3977 *
3978 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3979 * 'noinit' section so that it isn't zeroed at boot
3980 *
3981 * @param sym Thread stack symbol name
3982 * @param nmemb Number of stacks to declare
3983 * @param size Size of the stack memory region
3984 */
3985
3986#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003987 struct _k_thread_stack_element __noinit \
3988 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003989
3990/**
3991 * @brief Declare an embedded stack memory region
3992 *
3993 * Used for stacks embedded within other data structures. Use is highly
3994 * discouraged but in some cases necessary. For memory protection scenarios,
3995 * it is very important that any RAM preceding this member not be writable
3996 * by threads else a stack overflow will lead to silent corruption. In other
3997 * words, the containing data structure should live in RAM owned by the kernel.
3998 *
3999 * @param sym Thread stack symbol name
4000 * @param size Size of the stack memory region
4001 */
4002#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004003 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004004
4005/**
4006 * @brief Return the size in bytes of a stack memory region
4007 *
4008 * Convenience macro for passing the desired stack size to k_thread_create()
4009 * since the underlying implementation may actually create something larger
4010 * (for instance a guard area).
4011 *
4012 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004013 * passed to K_THREAD_STACK_DEFINE.
4014 *
4015 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4016 * it is not guaranteed to return the original value since each array
4017 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004018 *
4019 * @param sym Stack memory symbol
4020 * @return Size of the stack
4021 */
4022#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4023
4024/**
4025 * @brief Get a pointer to the physical stack buffer
4026 *
4027 * Convenience macro to get at the real underlying stack buffer used by
4028 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4029 * This is really only intended for diagnostic tools which want to examine
4030 * stack memory contents.
4031 *
4032 * @param sym Declared stack symbol name
4033 * @return The buffer itself, a char *
4034 */
Andrew Boie507852a2017-07-25 18:47:07 -07004035static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4036{
4037 return (char *)sym;
4038}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004039
4040#endif /* _ARCH_DECLARE_STACK */
4041
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004042#ifdef __cplusplus
4043}
4044#endif
4045
Andrew Boiee004dec2016-11-07 09:01:19 -08004046#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4047/*
4048 * Define new and delete operators.
4049 * At this moment, the operators do nothing since objects are supposed
4050 * to be statically allocated.
4051 */
4052inline void operator delete(void *ptr)
4053{
4054 (void)ptr;
4055}
4056
4057inline void operator delete[](void *ptr)
4058{
4059 (void)ptr;
4060}
4061
4062inline void *operator new(size_t size)
4063{
4064 (void)size;
4065 return NULL;
4066}
4067
4068inline void *operator new[](size_t size)
4069{
4070 (void)size;
4071 return NULL;
4072}
4073
4074/* Placement versions of operator new and delete */
4075inline void operator delete(void *ptr1, void *ptr2)
4076{
4077 (void)ptr1;
4078 (void)ptr2;
4079}
4080
4081inline void operator delete[](void *ptr1, void *ptr2)
4082{
4083 (void)ptr1;
4084 (void)ptr2;
4085}
4086
4087inline void *operator new(size_t size, void *ptr)
4088{
4089 (void)size;
4090 return ptr;
4091}
4092
4093inline void *operator new[](size_t size, void *ptr)
4094{
4095 (void)size;
4096 return ptr;
4097}
4098
4099#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4100
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004101#endif /* !_ASMLANGUAGE */
4102
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004103#endif /* _kernel__h_ */