blob: 12c62c17bb76ded7bbc200a11e71fcb851e1335d [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050035#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050036#include <kernel_version.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Anas Nashif61f4b242016-11-18 10:53:59 -050042#ifdef CONFIG_KERNEL_DEBUG
43#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040044#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
45#else
46#define K_DEBUG(fmt, ...)
47#endif
48
49#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
50#define K_PRIO_PREEMPT(x) (x)
51
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
Benjamin Walshfab8d922016-11-08 15:36:36 -050067#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
Anas Nashif2f203c22016-12-18 06:57:45 -050074#ifdef CONFIG_OBJECT_TRACING
75#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
76#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#else
Anas Nashif2f203c22016-12-18 06:57:45 -050078#define _OBJECT_TRACING_INIT
79#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050082#define tcs k_thread
83struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084struct k_mutex;
85struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040086struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040093struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094struct k_mem_pool;
95struct k_timer;
96
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400105/**
Allan Stephensc98da842016-11-11 15:45:03 -0500106 * @defgroup thread_apis Thread APIs
107 * @ingroup kernel_apis
108 * @{
109 */
110
111/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500112 * @typedef k_thread_entry_t
113 * @brief Thread entry point function type.
114 *
115 * A thread's entry point function is invoked when the thread starts executing.
116 * Up to 3 argument values can be passed to the function.
117 *
118 * The thread terminates execution permanently if the entry point function
119 * returns. The thread is responsible for releasing any shared resources
120 * it may own (such as mutexes and dynamically allocated memory), prior to
121 * returning.
122 *
123 * @param p1 First argument.
124 * @param p2 Second argument.
125 * @param p3 Third argument.
126 *
127 * @return N/A
128 */
129typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
130
131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500132 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500134 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500136 * The new thread may be scheduled for immediate execution or a delayed start.
137 * If the newly spawned thread does not have a delayed start the kernel
138 * scheduler may preempt the current thread to allow the new thread to
139 * execute.
140 *
141 * Thread options are architecture-specific, and can include K_ESSENTIAL,
142 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
143 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400144 *
145 * @param stack Pointer to the stack space.
146 * @param stack_size Stack size in bytes.
147 * @param entry Thread entry function.
148 * @param p1 1st entry point parameter.
149 * @param p2 2nd entry point parameter.
150 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500151 * @param prio Thread priority.
152 * @param options Thread options.
153 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500155 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400156 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500157extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500158 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400159 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500160 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400161
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500163 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400164 *
Allan Stephensc98da842016-11-11 15:45:03 -0500165 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500166 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500168 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400169 *
170 * @return N/A
171 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400172extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400173
174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500175 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400176 *
177 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500178 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400179 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400180 * @return N/A
181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400182extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400183
184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500185 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500187 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500189 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400190 *
191 * @return N/A
192 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400193extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400194
195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500196 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500198 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500200 * If @a thread is not currently sleeping, the routine has no effect.
201 *
202 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400203 *
204 * @return N/A
205 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400207
208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500209 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500211 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400212 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400213extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400214
215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500216 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500218 * This routine prevents @a thread from executing if it has not yet started
219 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500221 * @param thread ID of thread to cancel.
222 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500223 * @retval 0 Thread spawning cancelled.
224 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400226extern int k_thread_cancel(k_tid_t thread);
227
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400228/**
Allan Stephensc98da842016-11-11 15:45:03 -0500229 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500231 * This routine permanently stops execution of @a thread. The thread is taken
232 * off all kernel queues it is part of (i.e. the ready queue, the timeout
233 * queue, or a kernel object wait queue). However, any kernel resources the
234 * thread might currently own (such as mutexes or memory blocks) are not
235 * released. It is the responsibility of the caller of this routine to ensure
236 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500238 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400239 *
240 * @return N/A
241 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400242extern void k_thread_abort(k_tid_t thread);
243
Allan Stephensc98da842016-11-11 15:45:03 -0500244/**
245 * @cond INTERNAL_HIDDEN
246 */
247
Benjamin Walshd211a522016-12-06 11:44:01 -0500248/* timeout has timed out and is not on _timeout_q anymore */
249#define _EXPIRED (-2)
250
251/* timeout is not in use */
252#define _INACTIVE (-1)
253
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400254#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400255#define _THREAD_TIMEOUT_INIT(obj) \
256 (obj).nano_timeout = { \
257 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400258 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500260 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261 },
262#else
263#define _THREAD_TIMEOUT_INIT(obj)
264#endif
265
266#ifdef CONFIG_ERRNO
267#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
268#else
269#define _THREAD_ERRNO_INIT(obj)
270#endif
271
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400272struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400273 union {
274 char *init_stack;
275 struct k_thread *thread;
276 };
277 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500278 void (*init_entry)(void *, void *, void *);
279 void *init_p1;
280 void *init_p2;
281 void *init_p3;
282 int init_prio;
283 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400284 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500285 void (*init_abort)(void);
286 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287};
288
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400289#define _THREAD_INITIALIZER(stack, stack_size, \
290 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500291 prio, options, delay, abort, groups) \
292 { \
293 .init_stack = (stack), \
294 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400295 .init_entry = (void (*)(void *, void *, void *))entry, \
296 .init_p1 = (void *)p1, \
297 .init_p2 = (void *)p2, \
298 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500299 .init_prio = (prio), \
300 .init_options = (options), \
301 .init_delay = (delay), \
302 .init_abort = (abort), \
303 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400304 }
305
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400306/**
Allan Stephensc98da842016-11-11 15:45:03 -0500307 * INTERNAL_HIDDEN @endcond
308 */
309
310/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500311 * @brief Statically define and initialize a thread.
312 *
313 * The thread may be scheduled for immediate execution or a delayed start.
314 *
315 * Thread options are architecture-specific, and can include K_ESSENTIAL,
316 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
317 * them using "|" (the logical OR operator).
318 *
319 * The ID of the thread can be accessed using:
320 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500321 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500322 *
323 * @param name Name of the thread.
324 * @param stack_size Stack size in bytes.
325 * @param entry Thread entry function.
326 * @param p1 1st entry point parameter.
327 * @param p2 2nd entry point parameter.
328 * @param p3 3rd entry point parameter.
329 * @param prio Thread priority.
330 * @param options Thread options.
331 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400332 *
333 * @internal It has been observed that the x86 compiler by default aligns
334 * these _static_thread_data structures to 32-byte boundaries, thereby
335 * wasting space. To work around this, force a 4-byte alignment.
336 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500337#define K_THREAD_DEFINE(name, stack_size, \
338 entry, p1, p2, p3, \
339 prio, options, delay) \
340 char __noinit __stack _k_thread_obj_##name[stack_size]; \
341 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500342 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500343 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
344 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500345 NULL, 0); \
346 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400347
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500349 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * @param thread ID of thread whose priority is needed.
354 *
355 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400356 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500357extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400358
359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500360 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500362 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400363 *
364 * Rescheduling can occur immediately depending on the priority @a thread is
365 * set to:
366 *
367 * - If its priority is raised above the priority of the caller of this
368 * function, and the caller is preemptible, @a thread will be scheduled in.
369 *
370 * - If the caller operates on itself, it lowers its priority below that of
371 * other threads in the system, and the caller is preemptible, the thread of
372 * highest priority will be scheduled in.
373 *
374 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
375 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
376 * highest priority.
377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500378 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379 * @param prio New priority.
380 *
381 * @warning Changing the priority of a thread currently involved in mutex
382 * priority inheritance may result in undefined behavior.
383 *
384 * @return N/A
385 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400386extern void k_thread_priority_set(k_tid_t thread, int prio);
387
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500389 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500391 * This routine prevents the kernel scheduler from making @a thread the
392 * current thread. All other internal operations on @a thread are still
393 * performed; for example, any timeout it is waiting on keeps ticking,
394 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500396 * If @a thread is already suspended, the routine has no effect.
397 *
398 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400399 *
400 * @return N/A
401 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400402extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400403
404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500405 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500407 * This routine allows the kernel scheduler to make @a thread the current
408 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500410 * If @a thread is not currently suspended, the routine has no effect.
411 *
412 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400413 *
414 * @return N/A
415 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400416extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400417
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400418/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500419 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500421 * This routine specifies how the scheduler will perform time slicing of
422 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500424 * To enable time slicing, @a slice must be non-zero. The scheduler
425 * ensures that no thread runs for more than the specified time limit
426 * before other threads of that priority are given a chance to execute.
427 * Any thread whose priority is higher than @a prio is exempted, and may
428 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500430 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431 * execute. Once the scheduler selects a thread for execution, there is no
432 * minimum guaranteed time the thread will execute before threads of greater or
433 * equal priority are scheduled.
434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500435 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400436 * for execution, this routine has no effect; the thread is immediately
437 * rescheduled after the slice period expires.
438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500439 * To disable timeslicing, set both @a slice and @a prio to zero.
440 *
441 * @param slice Maximum time slice length (in milliseconds).
442 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400443 *
444 * @return N/A
445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400446extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400447
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448/**
Allan Stephensc98da842016-11-11 15:45:03 -0500449 * @} end defgroup thread_apis
450 */
451
452/**
453 * @addtogroup isr_apis
454 * @{
455 */
456
457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500458 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400459 *
Allan Stephensc98da842016-11-11 15:45:03 -0500460 * This routine allows the caller to customize its actions, depending on
461 * whether it is a thread or an ISR.
462 *
463 * @note Can be called by ISRs.
464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500465 * @return 0 if invoked by a thread.
466 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500468extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400469
Benjamin Walsh445830d2016-11-10 15:54:27 -0500470/**
471 * @brief Determine if code is running in a preemptible thread.
472 *
Allan Stephensc98da842016-11-11 15:45:03 -0500473 * This routine allows the caller to customize its actions, depending on
474 * whether it can be preempted by another thread. The routine returns a 'true'
475 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500476 *
Allan Stephensc98da842016-11-11 15:45:03 -0500477 * - The code is running in a thread, not at ISR.
478 * - The thread's priority is in the preemptible range.
479 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500480 *
Allan Stephensc98da842016-11-11 15:45:03 -0500481 * @note Can be called by ISRs.
482 *
483 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500484 * @return Non-zero if invoked by a preemptible thread.
485 */
486extern int k_is_preempt_thread(void);
487
Allan Stephensc98da842016-11-11 15:45:03 -0500488/**
489 * @} end addtogroup isr_apis
490 */
491
492/**
493 * @addtogroup thread_apis
494 * @{
495 */
496
497/**
498 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500499 *
Allan Stephensc98da842016-11-11 15:45:03 -0500500 * This routine prevents the current thread from being preempted by another
501 * thread by instructing the scheduler to treat it as a cooperative thread.
502 * If the thread subsequently performs an operation that makes it unready,
503 * it will be context switched out in the normal manner. When the thread
504 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500505 *
Allan Stephensc98da842016-11-11 15:45:03 -0500506 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500507 *
Allan Stephensc98da842016-11-11 15:45:03 -0500508 * @note k_sched_lock() and k_sched_unlock() should normally be used
509 * when the operation being performed can be safely interrupted by ISRs.
510 * However, if the amount of processing involved is very small, better
511 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500512 *
513 * @return N/A
514 */
515extern void k_sched_lock(void);
516
Allan Stephensc98da842016-11-11 15:45:03 -0500517/**
518 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500519 *
Allan Stephensc98da842016-11-11 15:45:03 -0500520 * This routine reverses the effect of a previous call to k_sched_lock().
521 * A thread must call the routine once for each time it called k_sched_lock()
522 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500523 *
524 * @return N/A
525 */
526extern void k_sched_unlock(void);
527
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500533 * Custom data is not used by the kernel itself, and is freely available
534 * for a thread to use as it sees fit. It can be used as a framework
535 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500537 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538 *
539 * @return N/A
540 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400541extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400542
543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500544 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500546 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500548 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400549 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400550extern void *k_thread_custom_data_get(void);
551
552/**
Allan Stephensc98da842016-11-11 15:45:03 -0500553 * @} end addtogroup thread_apis
554 */
555
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400556#include <sys_clock.h>
557
Allan Stephensc2f15a42016-11-17 12:24:22 -0500558/**
559 * @addtogroup clock_apis
560 * @{
561 */
562
563/**
564 * @brief Generate null timeout delay.
565 *
566 * This macro generates a timeout delay that that instructs a kernel API
567 * not to wait if the requested operation cannot be performed immediately.
568 *
569 * @return Timeout delay value.
570 */
571#define K_NO_WAIT 0
572
573/**
574 * @brief Generate timeout delay from milliseconds.
575 *
576 * This macro generates a timeout delay that that instructs a kernel API
577 * to wait up to @a ms milliseconds to perform the requested operation.
578 *
579 * @param ms Duration in milliseconds.
580 *
581 * @return Timeout delay value.
582 */
Johan Hedberg14471692016-11-13 10:52:15 +0200583#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500584
585/**
586 * @brief Generate timeout delay from seconds.
587 *
588 * This macro generates a timeout delay that that instructs a kernel API
589 * to wait up to @a s seconds to perform the requested operation.
590 *
591 * @param s Duration in seconds.
592 *
593 * @return Timeout delay value.
594 */
Johan Hedberg14471692016-11-13 10:52:15 +0200595#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500596
597/**
598 * @brief Generate timeout delay from minutes.
599 *
600 * This macro generates a timeout delay that that instructs a kernel API
601 * to wait up to @a m minutes to perform the requested operation.
602 *
603 * @param m Duration in minutes.
604 *
605 * @return Timeout delay value.
606 */
Johan Hedberg14471692016-11-13 10:52:15 +0200607#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500608
609/**
610 * @brief Generate timeout delay from hours.
611 *
612 * This macro generates a timeout delay that that instructs a kernel API
613 * to wait up to @a h hours to perform the requested operation.
614 *
615 * @param h Duration in hours.
616 *
617 * @return Timeout delay value.
618 */
Johan Hedberg14471692016-11-13 10:52:15 +0200619#define K_HOURS(h) K_MINUTES((h) * 60)
620
Allan Stephensc98da842016-11-11 15:45:03 -0500621/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500622 * @brief Generate infinite timeout delay.
623 *
624 * This macro generates a timeout delay that that instructs a kernel API
625 * to wait as long as necessary to perform the requested operation.
626 *
627 * @return Timeout delay value.
628 */
629#define K_FOREVER (-1)
630
631/**
632 * @} end addtogroup clock_apis
633 */
634
635/**
Allan Stephensc98da842016-11-11 15:45:03 -0500636 * @cond INTERNAL_HIDDEN
637 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400638
Benjamin Walsh62092182016-12-20 14:39:08 -0500639/* kernel clocks */
640
641#if (sys_clock_ticks_per_sec == 1000) || \
642 (sys_clock_ticks_per_sec == 500) || \
643 (sys_clock_ticks_per_sec == 250) || \
644 (sys_clock_ticks_per_sec == 125) || \
645 (sys_clock_ticks_per_sec == 100) || \
646 (sys_clock_ticks_per_sec == 50) || \
647 (sys_clock_ticks_per_sec == 25) || \
648 (sys_clock_ticks_per_sec == 20) || \
649 (sys_clock_ticks_per_sec == 10) || \
650 (sys_clock_ticks_per_sec == 1)
651
652 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
653#else
654 /* yields horrible 64-bit math on many architectures: try to avoid */
655 #define _NON_OPTIMIZED_TICKS_PER_SEC
656#endif
657
658#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
659extern int32_t _ms_to_ticks(int32_t ms);
660#else
661static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
662{
663 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
664}
665#endif
666
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500667/* added tick needed to account for tick in progress */
668#define _TICK_ALIGN 1
669
Benjamin Walsh62092182016-12-20 14:39:08 -0500670static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400671{
Benjamin Walsh62092182016-12-20 14:39:08 -0500672#ifdef CONFIG_SYS_CLOCK_EXISTS
673
674#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400675 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400676#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500677 return (uint64_t)ticks * _ms_per_tick;
678#endif
679
680#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400681 __ASSERT(ticks == 0, "");
682 return 0;
683#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400684}
685
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400686/* timeouts */
687
688struct _timeout;
689typedef void (*_timeout_func_t)(struct _timeout *t);
690
691struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500692 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400693 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694 sys_dlist_t *wait_q;
695 int32_t delta_ticks_from_prev;
696 _timeout_func_t func;
697};
698
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200699extern int32_t _timeout_remaining_get(struct _timeout *timeout);
700
Allan Stephensc98da842016-11-11 15:45:03 -0500701/**
702 * INTERNAL_HIDDEN @endcond
703 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500704
Allan Stephensc98da842016-11-11 15:45:03 -0500705/**
706 * @cond INTERNAL_HIDDEN
707 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400708
709struct k_timer {
710 /*
711 * _timeout structure must be first here if we want to use
712 * dynamic timer allocation. timeout.node is used in the double-linked
713 * list of free timers
714 */
715 struct _timeout timeout;
716
Allan Stephens45bfa372016-10-12 12:39:42 -0500717 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400718 _wait_q_t wait_q;
719
720 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500721 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722
723 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500724 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400725
726 /* timer period */
727 int32_t period;
728
Allan Stephens45bfa372016-10-12 12:39:42 -0500729 /* timer status */
730 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400731
Allan Stephens45bfa372016-10-12 12:39:42 -0500732 /* used to support legacy timer APIs */
733 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400734
Anas Nashif2f203c22016-12-18 06:57:45 -0500735 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400736};
737
Allan Stephens1342adb2016-11-03 13:54:53 -0500738#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400739 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500740 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500741 .timeout.wait_q = NULL, \
742 .timeout.thread = NULL, \
743 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400744 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500745 .expiry_fn = expiry, \
746 .stop_fn = stop, \
747 .status = 0, \
748 ._legacy_data = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500749 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400750 }
751
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752/**
Allan Stephensc98da842016-11-11 15:45:03 -0500753 * INTERNAL_HIDDEN @endcond
754 */
755
756/**
757 * @defgroup timer_apis Timer APIs
758 * @ingroup kernel_apis
759 * @{
760 */
761
762/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500763 * @typedef k_timer_expiry_t
764 * @brief Timer expiry function type.
765 *
766 * A timer's expiry function is executed by the system clock interrupt handler
767 * each time the timer expires. The expiry function is optional, and is only
768 * invoked if the timer has been initialized with one.
769 *
770 * @param timer Address of timer.
771 *
772 * @return N/A
773 */
774typedef void (*k_timer_expiry_t)(struct k_timer *timer);
775
776/**
777 * @typedef k_timer_stop_t
778 * @brief Timer stop function type.
779 *
780 * A timer's stop function is executed if the timer is stopped prematurely.
781 * The function runs in the context of the thread that stops the timer.
782 * The stop function is optional, and is only invoked if the timer has been
783 * initialized with one.
784 *
785 * @param timer Address of timer.
786 *
787 * @return N/A
788 */
789typedef void (*k_timer_stop_t)(struct k_timer *timer);
790
791/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500796 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
798 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @param expiry_fn Function to invoke each time the timer expires.
800 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500802#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500803 struct k_timer name \
804 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500805 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400806
Allan Stephens45bfa372016-10-12 12:39:42 -0500807/**
808 * @brief Initialize a timer.
809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500811 *
812 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * @param expiry_fn Function to invoke each time the timer expires.
814 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500815 *
816 * @return N/A
817 */
818extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500819 k_timer_expiry_t expiry_fn,
820 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700821
Allan Stephens45bfa372016-10-12 12:39:42 -0500822/**
823 * @brief Start a timer.
824 *
825 * This routine starts a timer, and resets its status to zero. The timer
826 * begins counting down using the specified duration and period values.
827 *
828 * Attempting to start a timer that is already running is permitted.
829 * The timer's status is reset to zero and the timer begins counting down
830 * using the new duration and period values.
831 *
832 * @param timer Address of timer.
833 * @param duration Initial timer duration (in milliseconds).
834 * @param period Timer period (in milliseconds).
835 *
836 * @return N/A
837 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400838extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500839 int32_t duration, int32_t period);
840
841/**
842 * @brief Stop a timer.
843 *
844 * This routine stops a running timer prematurely. The timer's stop function,
845 * if one exists, is invoked by the caller.
846 *
847 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500849 *
850 * @param timer Address of timer.
851 *
852 * @return N/A
853 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400854extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500855
856/**
857 * @brief Read timer status.
858 *
859 * This routine reads the timer's status, which indicates the number of times
860 * it has expired since its status was last read.
861 *
862 * Calling this routine resets the timer's status to zero.
863 *
864 * @param timer Address of timer.
865 *
866 * @return Timer status.
867 */
868extern uint32_t k_timer_status_get(struct k_timer *timer);
869
870/**
871 * @brief Synchronize thread to timer expiration.
872 *
873 * This routine blocks the calling thread until the timer's status is non-zero
874 * (indicating that it has expired at least once since it was last examined)
875 * or the timer is stopped. If the timer status is already non-zero,
876 * or the timer is already stopped, the caller continues without waiting.
877 *
878 * Calling this routine resets the timer's status to zero.
879 *
880 * This routine must not be used by interrupt handlers, since they are not
881 * allowed to block.
882 *
883 * @param timer Address of timer.
884 *
885 * @return Timer status.
886 */
887extern uint32_t k_timer_status_sync(struct k_timer *timer);
888
889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500891 *
892 * This routine computes the (approximate) time remaining before a running
893 * timer next expires. If the timer is not running, it returns zero.
894 *
895 * @param timer Address of timer.
896 *
897 * @return Remaining time (in milliseconds).
898 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200899static inline int32_t k_timer_remaining_get(struct k_timer *timer)
900{
901 return _timeout_remaining_get(&timer->timeout);
902}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400903
Allan Stephensc98da842016-11-11 15:45:03 -0500904/**
905 * @} end defgroup timer_apis
906 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400907
Allan Stephensc98da842016-11-11 15:45:03 -0500908/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500909 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500910 * @{
911 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500912
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * This routine returns the elapsed time since the system booted,
917 * in milliseconds.
918 *
919 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400920 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400921extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400922
923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500924 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * This routine returns the lower 32-bits of the elapsed time since the system
927 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 * This routine can be more efficient than k_uptime_get(), as it reduces the
930 * need for interrupt locking and 64-bit math. However, the 32-bit result
931 * cannot hold a system uptime time larger than approximately 50 days, so the
932 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400935 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400936extern uint32_t k_uptime_get_32(void);
937
938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * This routine computes the elapsed time between the current system uptime
942 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * @param reftime Pointer to a reference time, which is updated to the current
945 * uptime upon return.
946 *
947 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400948 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400950
951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * This routine computes the elapsed time between the current system uptime
955 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500957 * This routine can be more efficient than k_uptime_delta(), as it reduces the
958 * need for interrupt locking and 64-bit math. However, the 32-bit result
959 * cannot hold an elapsed time larger than approximately 50 days, so the
960 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @param reftime Pointer to a reference time, which is updated to the current
963 * uptime upon return.
964 *
965 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400966 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400967extern uint32_t k_uptime_delta_32(int64_t *reftime);
968
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500970 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * This routine returns the current time, as measured by the system's hardware
973 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977extern uint32_t k_cycle_get_32(void);
978
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400979/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500980 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400981 */
982
Allan Stephensc98da842016-11-11 15:45:03 -0500983/**
984 * @cond INTERNAL_HIDDEN
985 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400986
987struct k_fifo {
988 _wait_q_t wait_q;
989 sys_slist_t data_q;
990
Anas Nashif2f203c22016-12-18 06:57:45 -0500991 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992};
993
Allan Stephensc98da842016-11-11 15:45:03 -0500994#define K_FIFO_INITIALIZER(obj) \
995 { \
996 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
997 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -0500998 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -0500999 }
1000
1001/**
1002 * INTERNAL_HIDDEN @endcond
1003 */
1004
1005/**
1006 * @defgroup fifo_apis Fifo APIs
1007 * @ingroup kernel_apis
1008 * @{
1009 */
1010
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001017 *
1018 * @return N/A
1019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021
1022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * This routine adds a data item to @a fifo. A fifo data item must be
1026 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1027 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @note Can be called by ISRs.
1030 *
1031 * @param fifo Address of the fifo.
1032 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 *
1034 * @return N/A
1035 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001036extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037
1038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * This routine adds a list of data items to @a fifo in one operation.
1042 * The data items must be in a singly-linked list, with the first 32 bits
1043 * each data item pointing to the next data item; the list must be
1044 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @param fifo Address of the fifo.
1049 * @param head Pointer to first node in singly-linked list.
1050 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 *
1052 * @return N/A
1053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001054extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055
1056/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001057 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001059 * This routine adds a list of data items to @a fifo in one operation.
1060 * The data items must be in a singly-linked list implemented using a
1061 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062 * and must be re-initialized via sys_slist_init().
1063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001064 * @note Can be called by ISRs.
1065 *
1066 * @param fifo Address of the fifo.
1067 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
1069 * @return N/A
1070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072
1073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * This routine removes a data item from @a fifo in a "first in, first out"
1077 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1080 *
1081 * @param fifo Address of the fifo.
1082 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083 * or one of the special values K_NO_WAIT and K_FOREVER.
1084 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001085 * @return Address of the data item if successful; NULL if returned
1086 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001087 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001088extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1089
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001091 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001095 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001100 struct k_fifo name \
1101 __in_section(_k_fifo, static, name) = \
1102 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001103
Allan Stephensc98da842016-11-11 15:45:03 -05001104/**
1105 * @} end defgroup fifo_apis
1106 */
1107
1108/**
1109 * @cond INTERNAL_HIDDEN
1110 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001111
1112struct k_lifo {
1113 _wait_q_t wait_q;
1114 void *list;
1115
Anas Nashif2f203c22016-12-18 06:57:45 -05001116 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117};
1118
Allan Stephensc98da842016-11-11 15:45:03 -05001119#define K_LIFO_INITIALIZER(obj) \
1120 { \
1121 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1122 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001123 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001124 }
1125
1126/**
1127 * INTERNAL_HIDDEN @endcond
1128 */
1129
1130/**
1131 * @defgroup lifo_apis Lifo APIs
1132 * @ingroup kernel_apis
1133 * @{
1134 */
1135
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001141 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
1143 * @return N/A
1144 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001145extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146
1147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001148 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * This routine adds a data item to @a lifo. A lifo data item must be
1151 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1152 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @note Can be called by ISRs.
1155 *
1156 * @param lifo Address of the lifo.
1157 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 *
1159 * @return N/A
1160 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162
1163/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001166 * This routine removes a data item from @a lifo in a "last in, first out"
1167 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1170 *
1171 * @param lifo Address of the lifo.
1172 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 * or one of the special values K_NO_WAIT and K_FOREVER.
1174 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001175 * @return Address of the data item if successful; NULL if returned
1176 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001178extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1179
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001185 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001187 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001190 struct k_lifo name \
1191 __in_section(_k_lifo, static, name) = \
1192 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193
Allan Stephensc98da842016-11-11 15:45:03 -05001194/**
1195 * @} end defgroup lifo_apis
1196 */
1197
1198/**
1199 * @cond INTERNAL_HIDDEN
1200 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201
1202struct k_stack {
1203 _wait_q_t wait_q;
1204 uint32_t *base, *next, *top;
1205
Anas Nashif2f203c22016-12-18 06:57:45 -05001206 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001207};
1208
Allan Stephensc98da842016-11-11 15:45:03 -05001209#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1210 { \
1211 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1212 .base = stack_buffer, \
1213 .next = stack_buffer, \
1214 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001215 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001216 }
1217
1218/**
1219 * INTERNAL_HIDDEN @endcond
1220 */
1221
1222/**
1223 * @defgroup stack_apis Stack APIs
1224 * @ingroup kernel_apis
1225 * @{
1226 */
1227
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228/**
1229 * @brief Initialize a stack.
1230 *
1231 * This routine initializes a stack object, prior to its first use.
1232 *
1233 * @param stack Address of the stack.
1234 * @param buffer Address of array used to hold stacked values.
1235 * @param num_entries Maximum number of values that can be stacked.
1236 *
1237 * @return N/A
1238 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001239extern void k_stack_init(struct k_stack *stack,
1240 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241
1242/**
1243 * @brief Push an element onto a stack.
1244 *
1245 * This routine adds a 32-bit value @a data to @a stack.
1246 *
1247 * @note Can be called by ISRs.
1248 *
1249 * @param stack Address of the stack.
1250 * @param data Value to push onto the stack.
1251 *
1252 * @return N/A
1253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001254extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001255
1256/**
1257 * @brief Pop an element from a stack.
1258 *
1259 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1260 * manner and stores the value in @a data.
1261 *
1262 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1263 *
1264 * @param stack Address of the stack.
1265 * @param data Address of area to hold the value popped from the stack.
1266 * @param timeout Waiting period to obtain a value (in milliseconds),
1267 * or one of the special values K_NO_WAIT and K_FOREVER.
1268 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001269 * @retval 0 Element popped from stack.
1270 * @retval -EBUSY Returned without waiting.
1271 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1274
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001275/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001278 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001280 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * @param name Name of the stack.
1283 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001285#define K_STACK_DEFINE(name, stack_num_entries) \
1286 uint32_t __noinit \
1287 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001288 struct k_stack name \
1289 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001290 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1291 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001292
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001293/**
Allan Stephensc98da842016-11-11 15:45:03 -05001294 * @} end defgroup stack_apis
1295 */
1296
Allan Stephens6bba9b02016-11-16 14:56:54 -05001297struct k_work;
1298
Allan Stephensc98da842016-11-11 15:45:03 -05001299/**
1300 * @defgroup workqueue_apis Workqueue Thread APIs
1301 * @ingroup kernel_apis
1302 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303 */
1304
Allan Stephens6bba9b02016-11-16 14:56:54 -05001305/**
1306 * @typedef k_work_handler_t
1307 * @brief Work item handler function type.
1308 *
1309 * A work item's handler function is executed by a workqueue's thread
1310 * when the work item is processed by the workqueue.
1311 *
1312 * @param work Address of the work item.
1313 *
1314 * @return N/A
1315 */
1316typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001317
1318/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001319 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001320 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001321
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001322struct k_work_q {
1323 struct k_fifo fifo;
1324};
1325
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001326enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001327 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328};
1329
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330struct k_work {
1331 void *_reserved; /* Used by k_fifo implementation. */
1332 k_work_handler_t handler;
1333 atomic_t flags[1];
1334};
1335
Allan Stephens6bba9b02016-11-16 14:56:54 -05001336struct k_delayed_work {
1337 struct k_work work;
1338 struct _timeout timeout;
1339 struct k_work_q *work_q;
1340};
1341
1342extern struct k_work_q k_sys_work_q;
1343
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001345 * INTERNAL_HIDDEN @endcond
1346 */
1347
1348/**
1349 * @brief Initialize a statically-defined work item.
1350 *
1351 * This macro can be used to initialize a statically-defined workqueue work
1352 * item, prior to its first use. For example,
1353 *
1354 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1355 *
1356 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001357 */
1358#define K_WORK_INITIALIZER(work_handler) \
1359 { \
1360 ._reserved = NULL, \
1361 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001362 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001363 }
1364
1365/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001366 * @brief Initialize a work item.
1367 *
1368 * This routine initializes a workqueue work item, prior to its first use.
1369 *
1370 * @param work Address of work item.
1371 * @param handler Function to invoke each time work item is processed.
1372 *
1373 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374 */
1375static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1376{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001377 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378 work->handler = handler;
1379}
1380
1381/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001382 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001383 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001384 * This routine submits work item @a work to be processed by workqueue
1385 * @a work_q. If the work item is already pending in the workqueue's queue
1386 * as a result of an earlier submission, this routine has no effect on the
1387 * work item. If the work item has already been processed, or is currently
1388 * being processed, its work is considered complete and the work item can be
1389 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001390 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001391 * @warning
1392 * A submitted work item must not be modified until it has been processed
1393 * by the workqueue.
1394 *
1395 * @note Can be called by ISRs.
1396 *
1397 * @param work_q Address of workqueue.
1398 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001399 *
1400 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401 */
1402static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1403 struct k_work *work)
1404{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001405 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406 k_fifo_put(&work_q->fifo, work);
1407 }
1408}
1409
1410/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001411 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001412 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001413 * This routine indicates if work item @a work is pending in a workqueue's
1414 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001415 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001416 * @note Can be called by ISRs.
1417 *
1418 * @param work Address of work item.
1419 *
1420 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001421 */
1422static inline int k_work_pending(struct k_work *work)
1423{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001424 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001425}
1426
1427/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001428 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001430 * This routine starts workqueue @a work_q. The workqueue spawns its work
1431 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001432 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001433 * @param work_q Address of workqueue.
1434 * @param stack Pointer to work queue thread's stack space.
1435 * @param stack_size Size of the work queue thread's stack (in bytes).
1436 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001437 *
1438 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001439 */
Allan Stephens904cf972016-10-07 13:59:23 -05001440extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001441 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001442
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001443/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001444 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001446 * This routine initializes a workqueue delayed work item, prior to
1447 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001449 * @param work Address of delayed work item.
1450 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
1452 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001454extern void k_delayed_work_init(struct k_delayed_work *work,
1455 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456
1457/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001458 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001460 * This routine schedules work item @a work to be processed by workqueue
1461 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1462 * an asychronous countdown for the work item and then returns to the caller.
1463 * Only when the countdown completes is the work item actually submitted to
1464 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001466 * Submitting a previously submitted delayed work item that is still
1467 * counting down cancels the existing submission and restarts the countdown
1468 * using the new delay. If the work item is currently pending on the
1469 * workqueue's queue because the countdown has completed it is too late to
1470 * resubmit the item, and resubmission fails without impacting the work item.
1471 * If the work item has already been processed, or is currently being processed,
1472 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001473 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001474 * @warning
1475 * A delayed work item must not be modified until it has been processed
1476 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001477 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001478 * @note Can be called by ISRs.
1479 *
1480 * @param work_q Address of workqueue.
1481 * @param work Address of delayed work item.
1482 * @param delay Delay before submitting the work item (in milliseconds).
1483 *
1484 * @retval 0 Work item countdown started.
1485 * @retval -EINPROGRESS Work item is already pending.
1486 * @retval -EINVAL Work item is being processed or has completed its work.
1487 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001488 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001489extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1490 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001491 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001492
1493/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001494 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001495 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001496 * This routine cancels the submission of delayed work item @a work.
1497 * A delayed work item can only be cancelled while its countdown is still
1498 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001500 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001502 * @param work Address of delayed work item.
1503 *
1504 * @retval 0 Work item countdown cancelled.
1505 * @retval -EINPROGRESS Work item is already pending.
1506 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001508extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001509
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001510/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001511 * @brief Submit a work item to the system workqueue.
1512 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001513 * This routine submits work item @a work to be processed by the system
1514 * workqueue. If the work item is already pending in the workqueue's queue
1515 * as a result of an earlier submission, this routine has no effect on the
1516 * work item. If the work item has already been processed, or is currently
1517 * being processed, its work is considered complete and the work item can be
1518 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001519 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001520 * @warning
1521 * Work items submitted to the system workqueue should avoid using handlers
1522 * that block or yield since this may prevent the system workqueue from
1523 * processing other work items in a timely manner.
1524 *
1525 * @note Can be called by ISRs.
1526 *
1527 * @param work Address of work item.
1528 *
1529 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530 */
1531static inline void k_work_submit(struct k_work *work)
1532{
1533 k_work_submit_to_queue(&k_sys_work_q, work);
1534}
1535
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537 * @brief Submit a delayed work item to the system workqueue.
1538 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001539 * This routine schedules work item @a work to be processed by the system
1540 * workqueue after a delay of @a delay milliseconds. The routine initiates
1541 * an asychronous countdown for the work item and then returns to the caller.
1542 * Only when the countdown completes is the work item actually submitted to
1543 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001545 * Submitting a previously submitted delayed work item that is still
1546 * counting down cancels the existing submission and restarts the countdown
1547 * using the new delay. If the work item is currently pending on the
1548 * workqueue's queue because the countdown has completed it is too late to
1549 * resubmit the item, and resubmission fails without impacting the work item.
1550 * If the work item has already been processed, or is currently being processed,
1551 * its work is considered complete and the work item can be resubmitted.
1552 *
1553 * @warning
1554 * Work items submitted to the system workqueue should avoid using handlers
1555 * that block or yield since this may prevent the system workqueue from
1556 * processing other work items in a timely manner.
1557 *
1558 * @note Can be called by ISRs.
1559 *
1560 * @param work Address of delayed work item.
1561 * @param delay Delay before submitting the work item (in milliseconds).
1562 *
1563 * @retval 0 Work item countdown started.
1564 * @retval -EINPROGRESS Work item is already pending.
1565 * @retval -EINVAL Work item is being processed or has completed its work.
1566 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001567 */
1568static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001569 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001571 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572}
1573
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001574/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001575 * @brief Get time remaining before a delayed work gets scheduled.
1576 *
1577 * This routine computes the (approximate) time remaining before a
1578 * delayed work gets executed. If the delayed work is not waiting to be
1579 * schedules, it returns zero.
1580 *
1581 * @param work Delayed work item.
1582 *
1583 * @return Remaining time (in milliseconds).
1584 */
1585static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1586{
1587 return _timeout_remaining_get(&work->timeout);
1588}
1589
1590/**
Allan Stephensc98da842016-11-11 15:45:03 -05001591 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001592 */
1593
Allan Stephensc98da842016-11-11 15:45:03 -05001594/**
1595 * @cond INTERNAL_HIDDEN
1596 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001597
1598struct k_mutex {
1599 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001600 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601 uint32_t lock_count;
1602 int owner_orig_prio;
1603#ifdef CONFIG_OBJECT_MONITOR
1604 int num_lock_state_changes;
1605 int num_conflicts;
1606#endif
1607
Anas Nashif2f203c22016-12-18 06:57:45 -05001608 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001609};
1610
1611#ifdef CONFIG_OBJECT_MONITOR
1612#define _MUTEX_INIT_OBJECT_MONITOR \
1613 .num_lock_state_changes = 0, .num_conflicts = 0,
1614#else
1615#define _MUTEX_INIT_OBJECT_MONITOR
1616#endif
1617
1618#define K_MUTEX_INITIALIZER(obj) \
1619 { \
1620 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1621 .owner = NULL, \
1622 .lock_count = 0, \
1623 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1624 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001625 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001626 }
1627
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001628/**
Allan Stephensc98da842016-11-11 15:45:03 -05001629 * INTERNAL_HIDDEN @endcond
1630 */
1631
1632/**
1633 * @defgroup mutex_apis Mutex APIs
1634 * @ingroup kernel_apis
1635 * @{
1636 */
1637
1638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001643 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001645 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001648 struct k_mutex name \
1649 __in_section(_k_mutex, static, name) = \
1650 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001651
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * Upon completion, the mutex is available and does not have an owner.
1658 *
1659 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660 *
1661 * @return N/A
1662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001664
1665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001666 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine locks @a mutex. If the mutex is locked by another thread,
1669 * the calling thread waits until the mutex becomes available or until
1670 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * A thread is permitted to lock a mutex it has already locked. The operation
1673 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001675 * @param mutex Address of the mutex.
1676 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001677 * or one of the special values K_NO_WAIT and K_FOREVER.
1678 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001679 * @retval 0 Mutex locked.
1680 * @retval -EBUSY Returned without waiting.
1681 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001683extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684
1685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * This routine unlocks @a mutex. The mutex must already be locked by the
1689 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001690 *
1691 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693 * thread.
1694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001695 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001696 *
1697 * @return N/A
1698 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001699extern void k_mutex_unlock(struct k_mutex *mutex);
1700
Allan Stephensc98da842016-11-11 15:45:03 -05001701/**
1702 * @} end defgroup mutex_apis
1703 */
1704
1705/**
1706 * @cond INTERNAL_HIDDEN
1707 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001708
1709struct k_sem {
1710 _wait_q_t wait_q;
1711 unsigned int count;
1712 unsigned int limit;
1713
Anas Nashif2f203c22016-12-18 06:57:45 -05001714 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001715};
1716
Allan Stephensc98da842016-11-11 15:45:03 -05001717#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1718 { \
1719 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1720 .count = initial_count, \
1721 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001722 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001723 }
1724
1725/**
1726 * INTERNAL_HIDDEN @endcond
1727 */
1728
1729/**
1730 * @defgroup semaphore_apis Semaphore APIs
1731 * @ingroup kernel_apis
1732 * @{
1733 */
1734
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @param sem Address of the semaphore.
1741 * @param initial_count Initial semaphore count.
1742 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001743 *
1744 * @return N/A
1745 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1747 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001748
1749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1755 *
1756 * @param sem Address of the semaphore.
1757 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001758 * or one of the special values K_NO_WAIT and K_FOREVER.
1759 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001760 * @note When porting code from the nanokernel legacy API to the new API, be
1761 * careful with the return value of this function. The return value is the
1762 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1763 * non-zero means failure, while the nano_sem_take family returns 1 for success
1764 * and 0 for failure.
1765 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001766 * @retval 0 Semaphore taken.
1767 * @retval -EBUSY Returned without waiting.
1768 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001769 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001770extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001771
1772/**
1773 * @brief Give a semaphore.
1774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * This routine gives @a sem, unless the semaphore is already at its maximum
1776 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001781 *
1782 * @return N/A
1783 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001784extern void k_sem_give(struct k_sem *sem);
1785
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001786/**
1787 * @brief Reset a semaphore's count to zero.
1788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001789 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001792 *
1793 * @return N/A
1794 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001795static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001796{
1797 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798}
1799
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001800/**
1801 * @brief Get a semaphore's count.
1802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001805 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001808 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001809static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001810{
1811 return sem->count;
1812}
1813
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001818 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001819 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @param name Name of the semaphore.
1822 * @param initial_count Initial semaphore count.
1823 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001826 struct k_sem name \
1827 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001828 K_SEM_INITIALIZER(name, initial_count, count_limit)
1829
Allan Stephensc98da842016-11-11 15:45:03 -05001830/**
1831 * @} end defgroup semaphore_apis
1832 */
1833
1834/**
1835 * @defgroup alert_apis Alert APIs
1836 * @ingroup kernel_apis
1837 * @{
1838 */
1839
Allan Stephens5eceb852016-11-16 10:16:30 -05001840/**
1841 * @typedef k_alert_handler_t
1842 * @brief Alert handler function type.
1843 *
1844 * An alert's alert handler function is invoked by the system workqueue
1845 * when the alert is signalled. The alert handler function is optional,
1846 * and is only invoked if the alert has been initialized with one.
1847 *
1848 * @param alert Address of the alert.
1849 *
1850 * @return 0 if alert has been consumed; non-zero if alert should pend.
1851 */
1852typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001853
1854/**
1855 * @} end defgroup alert_apis
1856 */
1857
1858/**
1859 * @cond INTERNAL_HIDDEN
1860 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001861
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001862#define K_ALERT_DEFAULT NULL
1863#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001864
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001865struct k_alert {
1866 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001867 atomic_t send_count;
1868 struct k_work work_item;
1869 struct k_sem sem;
1870
Anas Nashif2f203c22016-12-18 06:57:45 -05001871 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001872};
1873
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001874extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001875
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001876#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001878 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001880 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001881 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001882 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883 }
1884
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885/**
Allan Stephensc98da842016-11-11 15:45:03 -05001886 * INTERNAL_HIDDEN @endcond
1887 */
1888
1889/**
1890 * @addtogroup alert_apis
1891 * @{
1892 */
1893
1894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001895 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001897 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001899 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001901 * @param name Name of the alert.
1902 * @param alert_handler Action to take when alert is sent. Specify either
1903 * the address of a function to be invoked by the system workqueue
1904 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1905 * K_ALERT_DEFAULT (which causes the alert to pend).
1906 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001907 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001908#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001909 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001910 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001911 K_ALERT_INITIALIZER(name, alert_handler, \
1912 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @param alert Address of the alert.
1920 * @param handler Action to take when alert is sent. Specify either the address
1921 * of a function to be invoked by the system workqueue thread,
1922 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1923 * K_ALERT_DEFAULT (which causes the alert to pend).
1924 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
1926 * @return N/A
1927 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001928extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1929 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930
1931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001932 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001936 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1937 *
1938 * @param alert Address of the alert.
1939 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001940 * or one of the special values K_NO_WAIT and K_FOREVER.
1941 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001942 * @retval 0 Alert received.
1943 * @retval -EBUSY Returned without waiting.
1944 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001946extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947
1948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001949 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001951 * This routine signals @a alert. The action specified for @a alert will
1952 * be taken, which may trigger the execution of an alert handler function
1953 * and/or cause the alert to pend (assuming the alert has not reached its
1954 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * @note Can be called by ISRs.
1957 *
1958 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959 *
1960 * @return N/A
1961 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001962extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963
1964/**
Allan Stephensc98da842016-11-11 15:45:03 -05001965 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966 */
1967
Allan Stephensc98da842016-11-11 15:45:03 -05001968/**
1969 * @cond INTERNAL_HIDDEN
1970 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971
1972struct k_msgq {
1973 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001974 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975 uint32_t max_msgs;
1976 char *buffer_start;
1977 char *buffer_end;
1978 char *read_ptr;
1979 char *write_ptr;
1980 uint32_t used_msgs;
1981
Anas Nashif2f203c22016-12-18 06:57:45 -05001982 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983};
1984
Peter Mitsis1da807e2016-10-06 11:36:59 -04001985#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001986 { \
1987 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001988 .max_msgs = q_max_msgs, \
1989 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001991 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992 .read_ptr = q_buffer, \
1993 .write_ptr = q_buffer, \
1994 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001995 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996 }
1997
Peter Mitsis1da807e2016-10-06 11:36:59 -04001998/**
Allan Stephensc98da842016-11-11 15:45:03 -05001999 * INTERNAL_HIDDEN @endcond
2000 */
2001
2002/**
2003 * @defgroup msgq_apis Message Queue APIs
2004 * @ingroup kernel_apis
2005 * @{
2006 */
2007
2008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002011 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2012 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002013 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2014 * message is similarly aligned to this boundary, @a q_msg_size must also be
2015 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * The message queue can be accessed outside the module where it is defined
2018 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002020 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002022 * @param q_name Name of the message queue.
2023 * @param q_msg_size Message size (in bytes).
2024 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002025 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002026 */
2027#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2028 static char __noinit __aligned(q_align) \
2029 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002030 struct k_msgq q_name \
2031 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002032 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2033 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034
Peter Mitsisd7a37502016-10-13 11:37:40 -04002035/**
2036 * @brief Initialize a message queue.
2037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * This routine initializes a message queue object, prior to its first use.
2039 *
Allan Stephensda827222016-11-09 14:23:58 -06002040 * The message queue's ring buffer must contain space for @a max_msgs messages,
2041 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2042 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2043 * that each message is similarly aligned to this boundary, @a q_msg_size
2044 * must also be a multiple of N.
2045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002046 * @param q Address of the message queue.
2047 * @param buffer Pointer to ring buffer that holds queued messages.
2048 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002049 * @param max_msgs Maximum number of messages that can be queued.
2050 *
2051 * @return N/A
2052 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002053extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002054 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002055
2056/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002059 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002060 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002061 * @note Can be called by ISRs.
2062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * @param q Address of the message queue.
2064 * @param data Pointer to the message.
2065 * @param timeout Waiting period to add the message (in milliseconds),
2066 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002067 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002068 * @retval 0 Message sent.
2069 * @retval -ENOMSG Returned without waiting or queue purged.
2070 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002071 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002073
2074/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * This routine receives a message from message queue @a q in a "first in,
2078 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002079 *
Allan Stephensc98da842016-11-11 15:45:03 -05002080 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082 * @param q Address of the message queue.
2083 * @param data Address of area to hold the received message.
2084 * @param timeout Waiting period to receive the message (in milliseconds),
2085 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002086 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002087 * @retval 0 Message received.
2088 * @retval -ENOMSG Returned without waiting.
2089 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002092
2093/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * This routine discards all unreceived messages in a message queue's ring
2097 * buffer. Any threads that are blocked waiting to send a message to the
2098 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002101 *
2102 * @return N/A
2103 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104extern void k_msgq_purge(struct k_msgq *q);
2105
Peter Mitsis67be2492016-10-07 11:44:34 -04002106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * This routine returns the number of unused entries in a message queue's
2110 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112 * @param q Address of the message queue.
2113 *
2114 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002115 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002116static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002117{
2118 return q->max_msgs - q->used_msgs;
2119}
2120
Peter Mitsisd7a37502016-10-13 11:37:40 -04002121/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002122 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * @param q Address of the message queue.
2127 *
2128 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002129 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002130static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131{
2132 return q->used_msgs;
2133}
2134
Allan Stephensc98da842016-11-11 15:45:03 -05002135/**
2136 * @} end defgroup msgq_apis
2137 */
2138
2139/**
2140 * @defgroup mem_pool_apis Memory Pool APIs
2141 * @ingroup kernel_apis
2142 * @{
2143 */
2144
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002145struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002146 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147 void *addr_in_pool;
2148 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002149 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150};
2151
Allan Stephensc98da842016-11-11 15:45:03 -05002152/**
2153 * @} end defgroup mem_pool_apis
2154 */
2155
2156/**
2157 * @defgroup mailbox_apis Mailbox APIs
2158 * @ingroup kernel_apis
2159 * @{
2160 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161
2162struct k_mbox_msg {
2163 /** internal use only - needed for legacy API support */
2164 uint32_t _mailbox;
2165 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002166 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167 /** application-defined information value */
2168 uint32_t info;
2169 /** sender's message data buffer */
2170 void *tx_data;
2171 /** internal use only - needed for legacy API support */
2172 void *_rx_data;
2173 /** message data block descriptor */
2174 struct k_mem_block tx_block;
2175 /** source thread id */
2176 k_tid_t rx_source_thread;
2177 /** target thread id */
2178 k_tid_t tx_target_thread;
2179 /** internal use only - thread waiting on send (may be a dummy) */
2180 k_tid_t _syncing_thread;
2181#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2182 /** internal use only - semaphore used during asynchronous send */
2183 struct k_sem *_async_sem;
2184#endif
2185};
2186
Allan Stephensc98da842016-11-11 15:45:03 -05002187/**
2188 * @cond INTERNAL_HIDDEN
2189 */
2190
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191struct k_mbox {
2192 _wait_q_t tx_msg_queue;
2193 _wait_q_t rx_msg_queue;
2194
Anas Nashif2f203c22016-12-18 06:57:45 -05002195 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196};
2197
2198#define K_MBOX_INITIALIZER(obj) \
2199 { \
2200 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2201 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002202 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203 }
2204
Peter Mitsis12092702016-10-14 12:57:23 -04002205/**
Allan Stephensc98da842016-11-11 15:45:03 -05002206 * INTERNAL_HIDDEN @endcond
2207 */
2208
2209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002210 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002212 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002214 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002216 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002217 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002219 struct k_mbox name \
2220 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 K_MBOX_INITIALIZER(name) \
2222
Peter Mitsis12092702016-10-14 12:57:23 -04002223/**
2224 * @brief Initialize a mailbox.
2225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002226 * This routine initializes a mailbox object, prior to its first use.
2227 *
2228 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002229 *
2230 * @return N/A
2231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232extern void k_mbox_init(struct k_mbox *mbox);
2233
Peter Mitsis12092702016-10-14 12:57:23 -04002234/**
2235 * @brief Send a mailbox message in a synchronous manner.
2236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * This routine sends a message to @a mbox and waits for a receiver to both
2238 * receive and process it. The message data may be in a buffer, in a memory
2239 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * @param mbox Address of the mailbox.
2242 * @param tx_msg Address of the transmit message descriptor.
2243 * @param timeout Waiting period for the message to be received (in
2244 * milliseconds), or one of the special values K_NO_WAIT
2245 * and K_FOREVER. Once the message has been received,
2246 * this routine waits as long as necessary for the message
2247 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002248 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002249 * @retval 0 Message sent.
2250 * @retval -ENOMSG Returned without waiting.
2251 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002252 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002253extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002255
Peter Mitsis12092702016-10-14 12:57:23 -04002256/**
2257 * @brief Send a mailbox message in an asynchronous manner.
2258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002259 * This routine sends a message to @a mbox without waiting for a receiver
2260 * to process it. The message data may be in a buffer, in a memory pool block,
2261 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2262 * will be given when the message has been both received and completely
2263 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * @param mbox Address of the mailbox.
2266 * @param tx_msg Address of the transmit message descriptor.
2267 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002268 *
2269 * @return N/A
2270 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002271extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272 struct k_sem *sem);
2273
Peter Mitsis12092702016-10-14 12:57:23 -04002274/**
2275 * @brief Receive a mailbox message.
2276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002277 * This routine receives a message from @a mbox, then optionally retrieves
2278 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002280 * @param mbox Address of the mailbox.
2281 * @param rx_msg Address of the receive message descriptor.
2282 * @param buffer Address of the buffer to receive data, or NULL to defer data
2283 * retrieval and message disposal until later.
2284 * @param timeout Waiting period for a message to be received (in
2285 * milliseconds), or one of the special values K_NO_WAIT
2286 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002287 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002288 * @retval 0 Message received.
2289 * @retval -ENOMSG Returned without waiting.
2290 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002291 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002292extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002294
2295/**
2296 * @brief Retrieve mailbox message data into a buffer.
2297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002298 * This routine completes the processing of a received message by retrieving
2299 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002300 *
2301 * Alternatively, this routine can be used to dispose of a received message
2302 * without retrieving its data.
2303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002304 * @param rx_msg Address of the receive message descriptor.
2305 * @param buffer Address of the buffer to receive data, or NULL to discard
2306 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002307 *
2308 * @return N/A
2309 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002310extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002311
2312/**
2313 * @brief Retrieve mailbox message data into a memory pool block.
2314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * This routine completes the processing of a received message by retrieving
2316 * its data into a memory pool block, then disposing of the message.
2317 * The memory pool block that results from successful retrieval must be
2318 * returned to the pool once the data has been processed, even in cases
2319 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002320 *
2321 * Alternatively, this routine can be used to dispose of a received message
2322 * without retrieving its data. In this case there is no need to return a
2323 * memory pool block to the pool.
2324 *
2325 * This routine allocates a new memory pool block for the data only if the
2326 * data is not already in one. If a new block cannot be allocated, the routine
2327 * returns a failure code and the received message is left unchanged. This
2328 * permits the caller to reattempt data retrieval at a later time or to dispose
2329 * of the received message without retrieving its data.
2330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * @param rx_msg Address of a receive message descriptor.
2332 * @param pool Address of memory pool, or NULL to discard data.
2333 * @param block Address of the area to hold memory pool block info.
2334 * @param timeout Waiting period to wait for a memory pool block (in
2335 * milliseconds), or one of the special values K_NO_WAIT
2336 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002337 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002338 * @retval 0 Data retrieved.
2339 * @retval -ENOMEM Returned without waiting.
2340 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002341 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002342extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002343 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344 struct k_mem_block *block, int32_t timeout);
2345
Allan Stephensc98da842016-11-11 15:45:03 -05002346/**
2347 * @} end defgroup mailbox_apis
2348 */
2349
2350/**
2351 * @cond INTERNAL_HIDDEN
2352 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353
2354struct k_pipe {
2355 unsigned char *buffer; /* Pipe buffer: may be NULL */
2356 size_t size; /* Buffer size */
2357 size_t bytes_used; /* # bytes used in buffer */
2358 size_t read_index; /* Where in buffer to read from */
2359 size_t write_index; /* Where in buffer to write */
2360
2361 struct {
2362 _wait_q_t readers; /* Reader wait queue */
2363 _wait_q_t writers; /* Writer wait queue */
2364 } wait_q;
2365
Anas Nashif2f203c22016-12-18 06:57:45 -05002366 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367};
2368
Peter Mitsise5d9c582016-10-14 14:44:57 -04002369#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370 { \
2371 .buffer = pipe_buffer, \
2372 .size = pipe_buffer_size, \
2373 .bytes_used = 0, \
2374 .read_index = 0, \
2375 .write_index = 0, \
2376 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2377 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002378 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 }
2380
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381/**
Allan Stephensc98da842016-11-11 15:45:03 -05002382 * INTERNAL_HIDDEN @endcond
2383 */
2384
2385/**
2386 * @defgroup pipe_apis Pipe APIs
2387 * @ingroup kernel_apis
2388 * @{
2389 */
2390
2391/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002396 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * @param name Name of the pipe.
2399 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2400 * or zero if no ring buffer is used.
2401 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002403#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2404 static unsigned char __noinit __aligned(pipe_align) \
2405 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002406 struct k_pipe name \
2407 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002408 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 * This routine initializes a pipe object, prior to its first use.
2414 *
2415 * @param pipe Address of the pipe.
2416 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2417 * is used.
2418 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2419 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420 *
2421 * @return N/A
2422 */
2423extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2424 size_t size);
2425
2426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * @param pipe Address of the pipe.
2432 * @param data Address of data to write.
2433 * @param bytes_to_write Size of data (in bytes).
2434 * @param bytes_written Address of area to hold the number of bytes written.
2435 * @param min_xfer Minimum number of bytes to write.
2436 * @param timeout Waiting period to wait for the data to be written (in
2437 * milliseconds), or one of the special values K_NO_WAIT
2438 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002440 * @retval 0 At least @a min_xfer bytes of data were written.
2441 * @retval -EIO Returned without waiting; zero data bytes were written.
2442 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002445extern int k_pipe_put(struct k_pipe *pipe, void *data,
2446 size_t bytes_to_write, size_t *bytes_written,
2447 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002448
2449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454 * @param pipe Address of the pipe.
2455 * @param data Address to place the data read from pipe.
2456 * @param bytes_to_read Maximum number of data bytes to read.
2457 * @param bytes_read Address of area to hold the number of bytes read.
2458 * @param min_xfer Minimum number of data bytes to read.
2459 * @param timeout Waiting period to wait for the data to be read (in
2460 * milliseconds), or one of the special values K_NO_WAIT
2461 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002463 * @retval 0 At least @a min_xfer bytes of data were read.
2464 * @retval -EIO Returned without waiting; zero data bytes were read.
2465 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002468extern int k_pipe_get(struct k_pipe *pipe, void *data,
2469 size_t bytes_to_read, size_t *bytes_read,
2470 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471
2472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * This routine writes the data contained in a memory block to @a pipe.
2476 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480 * @param block Memory block containing data to send
2481 * @param size Number of data bytes in memory block to send
2482 * @param sem Semaphore to signal upon completion (else NULL)
2483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485 */
2486extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2487 size_t size, struct k_sem *sem);
2488
2489/**
Allan Stephensc98da842016-11-11 15:45:03 -05002490 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491 */
2492
Allan Stephensc98da842016-11-11 15:45:03 -05002493/**
2494 * @cond INTERNAL_HIDDEN
2495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002497struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002499 uint32_t num_blocks;
2500 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501 char *buffer;
2502 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002503 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504
Anas Nashif2f203c22016-12-18 06:57:45 -05002505 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506};
2507
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002508#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2509 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510 { \
2511 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002512 .num_blocks = slab_num_blocks, \
2513 .block_size = slab_block_size, \
2514 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515 .free_list = NULL, \
2516 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002517 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002518 }
2519
Peter Mitsis578f9112016-10-07 13:50:31 -04002520/**
Allan Stephensc98da842016-11-11 15:45:03 -05002521 * INTERNAL_HIDDEN @endcond
2522 */
2523
2524/**
2525 * @defgroup mem_slab_apis Memory Slab APIs
2526 * @ingroup kernel_apis
2527 * @{
2528 */
2529
2530/**
Allan Stephensda827222016-11-09 14:23:58 -06002531 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002532 *
Allan Stephensda827222016-11-09 14:23:58 -06002533 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002535 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2536 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002537 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002538 *
Allan Stephensda827222016-11-09 14:23:58 -06002539 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002542 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @param name Name of the memory slab.
2545 * @param slab_block_size Size of each memory block (in bytes).
2546 * @param slab_num_blocks Number memory blocks.
2547 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002548 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002549#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2550 char __noinit __aligned(slab_align) \
2551 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2552 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002553 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002554 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2555 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002557/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002558 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002561 *
Allan Stephensda827222016-11-09 14:23:58 -06002562 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2563 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2564 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2565 * To ensure that each memory block is similarly aligned to this boundary,
2566 * @a slab_block_size must also be a multiple of N.
2567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @param slab Address of the memory slab.
2569 * @param buffer Pointer to buffer used for the memory blocks.
2570 * @param block_size Size of each memory block (in bytes).
2571 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002572 *
2573 * @return N/A
2574 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002575extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002576 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002577
2578/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * @param slab Address of the memory slab.
2584 * @param mem Pointer to block address area.
2585 * @param timeout Maximum time to wait for operation to complete
2586 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2587 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002588 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002589 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002591 * @retval -ENOMEM Returned without waiting.
2592 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002593 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002594extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2595 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002596
2597/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * This routine releases a previously allocated memory block back to its
2601 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002603 * @param slab Address of the memory slab.
2604 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002605 *
2606 * @return N/A
2607 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002608extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613 * This routine gets the number of memory blocks that are currently
2614 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002618 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002619 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002620static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002622 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002623}
2624
Peter Mitsisc001aa82016-10-13 13:53:37 -04002625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * This routine gets the number of memory blocks that are currently
2629 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002634 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002635static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002636{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002637 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002638}
2639
Allan Stephensc98da842016-11-11 15:45:03 -05002640/**
2641 * @} end defgroup mem_slab_apis
2642 */
2643
2644/**
2645 * @cond INTERNAL_HIDDEN
2646 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002648/*
2649 * Memory pool requires a buffer and two arrays of structures for the
2650 * memory block accounting:
2651 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2652 * status of four blocks of memory.
2653 */
2654struct k_mem_pool_quad_block {
2655 char *mem_blocks; /* pointer to the first of four memory blocks */
2656 uint32_t mem_status; /* four bits. If bit is set, memory block is
2657 allocated */
2658};
2659/*
2660 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2661 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2662 * block size is 4 times less than the previous one and thus requires 4 times
2663 * bigger array of k_mem_pool_quad_block structures to keep track of the
2664 * memory blocks.
2665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002667/*
2668 * The array of k_mem_pool_block_set keeps the information of each array of
2669 * k_mem_pool_quad_block structures
2670 */
2671struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002672 size_t block_size; /* memory block size */
2673 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002674 struct k_mem_pool_quad_block *quad_block;
2675 int count;
2676};
2677
2678/* Memory pool descriptor */
2679struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002680 size_t max_block_size;
2681 size_t min_block_size;
2682 uint32_t nr_of_maxblocks;
2683 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002684 struct k_mem_pool_block_set *block_set;
2685 char *bufblock;
2686 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002687 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688};
2689
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002690#ifdef CONFIG_ARM
2691#define _SECTION_TYPE_SIGN "%"
2692#else
2693#define _SECTION_TYPE_SIGN "@"
2694#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002696/*
2697 * Static memory pool initialization
2698 */
Allan Stephensc98da842016-11-11 15:45:03 -05002699
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002700/*
2701 * Use .altmacro to be able to recalculate values and pass them as string
2702 * arguments when calling assembler macros resursively
2703 */
2704__asm__(".altmacro\n\t");
2705
2706/*
2707 * Recursively calls a macro
2708 * The followig global symbols need to be initialized:
2709 * __memory_pool_max_block_size - maximal size of the memory block
2710 * __memory_pool_min_block_size - minimal size of the memory block
2711 * Notes:
2712 * Global symbols are used due the fact that assembler macro allows only
2713 * one argument be passed with the % conversion
2714 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2715 * is used instead of / 4.
2716 * n_max argument needs to go first in the invoked macro, as some
2717 * assemblers concatenate \name and %(\n_max * 4) arguments
2718 * if \name goes first
2719 */
2720__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2721 ".ifge __memory_pool_max_block_size >> 2 -"
2722 " __memory_pool_min_block_size\n\t\t"
2723 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2724 "\\macro_name %(\\n_max * 4) \\name\n\t"
2725 ".endif\n\t"
2726 ".endm\n");
2727
2728/*
2729 * Build quad blocks
2730 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2731 * structures and recursively calls itself for the next array, 4 times
2732 * larger.
2733 * The followig global symbols need to be initialized:
2734 * __memory_pool_max_block_size - maximal size of the memory block
2735 * __memory_pool_min_block_size - minimal size of the memory block
2736 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2737 */
2738__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002739 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002740 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2741 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2742 ".if \\n_max % 4\n\t\t"
2743 ".skip __memory_pool_quad_block_size\n\t"
2744 ".endif\n\t"
2745 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2746 ".endm\n");
2747
2748/*
2749 * Build block sets and initialize them
2750 * Macro initializes the k_mem_pool_block_set structure and
2751 * recursively calls itself for the next one.
2752 * The followig global symbols need to be initialized:
2753 * __memory_pool_max_block_size - maximal size of the memory block
2754 * __memory_pool_min_block_size - minimal size of the memory block
2755 * __memory_pool_block_set_count, the number of the elements in the
2756 * block set array must be set to 0. Macro calculates it's real
2757 * value.
2758 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2759 * structures, _build_quad_blocks must be called prior it.
2760 */
2761__asm__(".macro _build_block_set n_max, name\n\t"
2762 ".int __memory_pool_max_block_size\n\t" /* block_size */
2763 ".if \\n_max % 4\n\t\t"
2764 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2765 ".else\n\t\t"
2766 ".int \\n_max >> 2\n\t"
2767 ".endif\n\t"
2768 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2769 ".int 0\n\t" /* count */
2770 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2771 "__do_recurse _build_block_set \\name \\n_max\n\t"
2772 ".endm\n");
2773
2774/*
2775 * Build a memory pool structure and initialize it
2776 * Macro uses __memory_pool_block_set_count global symbol,
2777 * block set addresses and buffer address, it may be called only after
2778 * _build_block_set
2779 */
2780__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002781 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002782 _SECTION_TYPE_SIGN "progbits\n\t"
2783 ".globl \\name\n\t"
2784 "\\name:\n\t"
2785 ".int \\max_size\n\t" /* max_block_size */
2786 ".int \\min_size\n\t" /* min_block_size */
2787 ".int \\n_max\n\t" /* nr_of_maxblocks */
2788 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2789 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2790 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2791 ".int 0\n\t" /* wait_q->head */
2792 ".int 0\n\t" /* wait_q->next */
2793 ".popsection\n\t"
2794 ".endm\n");
2795
2796#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2797 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2798 _SECTION_TYPE_SIGN "progbits\n\t"); \
2799 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2800 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2801 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2802 STRINGIFY(name) "\n\t"); \
2803 __asm__(".popsection\n\t")
2804
2805#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2806 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2807 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2808 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2809 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002810 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002811 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2812 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2813 STRINGIFY(name) "\n\t"); \
2814 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2815 __asm__(".int __memory_pool_block_set_count\n\t"); \
2816 __asm__(".popsection\n\t"); \
2817 extern uint32_t _mem_pool_block_set_count_##name; \
2818 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2819
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002820#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2821 char __noinit __aligned(align) \
2822 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002823
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002824/*
2825 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2826 * to __memory_pool_quad_block_size absolute symbol.
2827 * This function does not get called, but compiler calculates the value and
2828 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2829 */
2830static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2831{
2832 __asm__(".globl __memory_pool_quad_block_size\n\t"
2833#ifdef CONFIG_NIOS2
2834 "__memory_pool_quad_block_size = %0\n\t"
2835#else
2836 "__memory_pool_quad_block_size = %c0\n\t"
2837#endif
2838 :
2839 : "n"(sizeof(struct k_mem_pool_quad_block)));
2840}
2841
2842/**
Allan Stephensc98da842016-11-11 15:45:03 -05002843 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002844 */
2845
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002846/**
Allan Stephensc98da842016-11-11 15:45:03 -05002847 * @addtogroup mem_pool_apis
2848 * @{
2849 */
2850
2851/**
2852 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2855 * long. The memory pool allows blocks to be repeatedly partitioned into
2856 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2857 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002858 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002860 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 * If the pool is to be accessed outside the module where it is defined, it
2862 * can be declared via
2863 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002864 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @param name Name of the memory pool.
2867 * @param min_size Size of the smallest blocks in the pool (in bytes).
2868 * @param max_size Size of the largest blocks in the pool (in bytes).
2869 * @param n_max Number of maximum sized blocks in the pool.
2870 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002871 */
2872#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002873 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2874 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002875 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002876 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2877 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2878 extern struct k_mem_pool name
2879
Peter Mitsis937042c2016-10-13 13:18:26 -04002880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param pool Address of the memory pool.
2886 * @param block Pointer to block descriptor for the allocated memory.
2887 * @param size Amount of memory to allocate (in bytes).
2888 * @param timeout Maximum time to wait for operation to complete
2889 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2890 * or K_FOREVER to wait as long as necessary.
2891 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002892 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002894 * @retval -ENOMEM Returned without waiting.
2895 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002896 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002897extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002898 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002899
2900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * This routine releases a previously allocated memory block back to its
2904 * memory pool.
2905 *
2906 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002907 *
2908 * @return N/A
2909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002911
2912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * This routine instructs a memory pool to concatenate unused memory blocks
2916 * into larger blocks wherever possible. Manually defragmenting the memory
2917 * pool may speed up future allocations of memory blocks by eliminating the
2918 * need for the memory pool to perform an automatic partial defragmentation.
2919 *
2920 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002921 *
2922 * @return N/A
2923 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002924extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002925
2926/**
Allan Stephensc98da842016-11-11 15:45:03 -05002927 * @} end addtogroup mem_pool_apis
2928 */
2929
2930/**
2931 * @defgroup heap_apis Heap Memory Pool APIs
2932 * @ingroup kernel_apis
2933 * @{
2934 */
2935
2936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002940 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002945 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002946extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002947
2948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002950 *
2951 * This routine provides traditional free() semantics. The memory being
2952 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002953 *
Anas Nashif345fdd52016-12-20 08:36:04 -05002954 * If @a ptr is NULL, no operation is performed.
2955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002957 *
2958 * @return N/A
2959 */
2960extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961
Allan Stephensc98da842016-11-11 15:45:03 -05002962/**
2963 * @} end defgroup heap_apis
2964 */
2965
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05002966/**
2967 * @brief Make the CPU idle.
2968 *
2969 * This function makes the CPU idle until an event wakes it up.
2970 *
2971 * In a regular system, the idle thread should be the only thread responsible
2972 * for making the CPU idle and triggering any type of power management.
2973 * However, in some more constrained systems, such as a single-threaded system,
2974 * the only thread would be responsible for this if needed.
2975 *
2976 * @return N/A
2977 */
2978extern void k_cpu_idle(void);
2979
2980/**
2981 * @brief Make the CPU idle in an atomic fashion.
2982 *
2983 * Similar to k_cpu_idle(), but called with interrupts locked if operations
2984 * must be done atomically before making the CPU idle.
2985 *
2986 * @param key Interrupt locking key obtained from irq_lock().
2987 *
2988 * @return N/A
2989 */
2990extern void k_cpu_atomic_idle(unsigned int key);
2991
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992/*
2993 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2994 * hook into the device subsystem, which itself uses nanokernel semaphores,
2995 * and thus currently requires the definition of nano_sem.
2996 */
2997#include <legacy.h>
2998#include <arch/cpu.h>
2999
3000/*
3001 * private APIs that are utilized by one or more public APIs
3002 */
3003
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003004#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003006#else
3007#define _init_static_threads() do { } while ((0))
3008#endif
3009
3010extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003011extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012
3013#ifdef __cplusplus
3014}
3015#endif
3016
Andrew Boiee004dec2016-11-07 09:01:19 -08003017#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3018/*
3019 * Define new and delete operators.
3020 * At this moment, the operators do nothing since objects are supposed
3021 * to be statically allocated.
3022 */
3023inline void operator delete(void *ptr)
3024{
3025 (void)ptr;
3026}
3027
3028inline void operator delete[](void *ptr)
3029{
3030 (void)ptr;
3031}
3032
3033inline void *operator new(size_t size)
3034{
3035 (void)size;
3036 return NULL;
3037}
3038
3039inline void *operator new[](size_t size)
3040{
3041 (void)size;
3042 return NULL;
3043}
3044
3045/* Placement versions of operator new and delete */
3046inline void operator delete(void *ptr1, void *ptr2)
3047{
3048 (void)ptr1;
3049 (void)ptr2;
3050}
3051
3052inline void operator delete[](void *ptr1, void *ptr2)
3053{
3054 (void)ptr1;
3055 (void)ptr2;
3056}
3057
3058inline void *operator new(size_t size, void *ptr)
3059{
3060 (void)size;
3061 return ptr;
3062}
3063
3064inline void *operator new[](size_t size, void *ptr)
3065{
3066 (void)size;
3067 return ptr;
3068}
3069
3070#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3071
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072#endif /* _kernel__h_ */