blob: 8781d6d23af12a1dd2ca1b3f9771ef67302d0961 [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>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
49#define K_FOREVER (-1)
50#define K_NO_WAIT 0
51
52#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
74#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
75#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
77#else
78#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
79#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
80#endif
81
82#define k_thread tcs
83struct tcs;
84struct 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
99/* threads/scheduler/execution contexts */
100
101enum execution_context_types {
102 K_ISR = 0,
103 K_COOP_THREAD,
104 K_PREEMPT_THREAD,
105};
106
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400108
109/**
110 * @brief Initialize and start a thread with an optional delay
111 *
112 * This routine initializes a thread and optionally delays its execution.
113 * It is not ISR-callable.
114 *
115 * If a thread of priority higher than the current thread is spawned, and the
116 * current thread id preemptible, the current thread is preempted by the new
117 * thread.
118 *
119 * @param stack Pointer to the stack space.
120 * @param stack_size Stack size in bytes.
121 * @param entry Thread entry function.
122 * @param p1 1st entry point parameter.
123 * @param p2 2nd entry point parameter.
124 * @param p3 3rd entry point parameter.
125 * @param prio The thread's priority.
126 * @param options Not used currently.
127 * @param delay Duration of execution delay in milliseconds
128 *
129 * @return Kernel thread identifier
130 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
132 void (*entry)(void *, void *, void*),
133 void *p1, void *p2, void *p3,
134 int32_t prio, uint32_t options, int32_t delay);
135
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400136/**
137 * @brief Put the current thread to sleep
138 *
139 * This routine puts the currently thread to sleep for the specified
140 * number of milliseconds.
141 *
142 * @param duration Number of milliseconds the thread is to sleep
143 *
144 * @return N/A
145 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400146extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400147
148/**
149 * @brief Cause the current thread to busy wait
150 *
151 * This routine causes the current thread to execute a "do nothing" loop for
152 * a specified period of microseconds.
153 *
154 * @warning This routine utilizes the system clock, so it must not be invoked
155 * until the system clock is fully operational or while interrupts are
156 * locked.
157 *
158 * @return N/A
159 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400160extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400161
162/**
163 * @brief Yield the current thread
164 *
165 * Calling this routine results in the current thread yielding to another
166 * thread of the same or higher priority. If there are no other ready threads
167 * of the same or higher priority, the routine will return immediately.
168 *
169 * @return N/A
170 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400171extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400172
173/**
174 * @brief Wake the specified thread from sleep
175 *
176 * This routine wakes the thread specified by @a thread from its sleep.
177 *
178 * @param thread Identifies thread to wake
179 *
180 * @return N/A
181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400182extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400183
184/**
185 * @brief Obtain the thread ID of the currently executing thread
186 *
187 * @return Current thread ID
188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400189extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400190
191/**
192 * @brief Cancel a delayed thread start
193 *
194 * @param thread Delayed thread ID
195 *
196 * @retval 0 on success
197 * @retval -EINVAL Thread has already started or not delayed
198 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400199extern int k_thread_cancel(k_tid_t thread);
200
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400201/**
202 * @brief Abort a thread
203 *
204 * Execution of @a thread is immediately permanently cancelled. @a thread is
205 * taken off the ready queue if ready, or out of any wait queues and/or
206 * timeout queues it might be currently queued on. However, objects it might
207 * currently owned, such as mutexes, are not released. It is up to the
208 * subsystems managing the objects to handle this.
209 *
210 * @param thread Thread to abort
211 *
212 * @return N/A
213 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400214extern void k_thread_abort(k_tid_t thread);
215
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400216#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400217#define _THREAD_TIMEOUT_INIT(obj) \
218 (obj).nano_timeout = { \
219 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400220 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400221 .wait_q = NULL, \
222 .delta_ticks_from_prev = -1, \
223 },
224#else
225#define _THREAD_TIMEOUT_INIT(obj)
226#endif
227
228#ifdef CONFIG_ERRNO
229#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
230#else
231#define _THREAD_ERRNO_INIT(obj)
232#endif
233
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400234struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400235 union {
236 char *init_stack;
237 struct k_thread *thread;
238 };
239 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500240 void (*init_entry)(void *, void *, void *);
241 void *init_p1;
242 void *init_p2;
243 void *init_p3;
244 int init_prio;
245 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400246 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500247 void (*init_abort)(void);
248 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249};
250
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400251#define _THREAD_INITIALIZER(stack, stack_size, \
252 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500253 prio, options, delay, abort, groups) \
254 { \
255 .init_stack = (stack), \
256 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400257 .init_entry = (void (*)(void *, void *, void *))entry, \
258 .init_p1 = (void *)p1, \
259 .init_p2 = (void *)p2, \
260 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500261 .init_prio = (prio), \
262 .init_options = (options), \
263 .init_delay = (delay), \
264 .init_abort = (abort), \
265 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400266 }
267
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400268/**
Allan Stephens6cfe1322016-10-26 10:16:51 -0500269 * @brief Define a static thread.
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400270 *
271 * @internal It has been observed that the x86 compiler by default aligns
272 * these _static_thread_data structures to 32-byte boundaries, thereby
273 * wasting space. To work around this, force a 4-byte alignment.
274 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500275#define K_THREAD_DEFINE(name, stack_size, \
276 entry, p1, p2, p3, \
277 prio, options, delay) \
278 char __noinit __stack _k_thread_obj_##name[stack_size]; \
279 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500280 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500281 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
282 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500283 NULL, 0); \
284 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400285
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400286/**
287 * @brief Get a thread's priority
288 *
289 * @param thread ID of thread to query
290 *
291 * @return Specified thread's priority
292 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500293extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400294
295/**
296 * @brief Set the priority of a thread
297 *
298 * This routine immediately changes the priority of the specified thread.
299 *
300 * Rescheduling can occur immediately depending on the priority @a thread is
301 * set to:
302 *
303 * - If its priority is raised above the priority of the caller of this
304 * function, and the caller is preemptible, @a thread will be scheduled in.
305 *
306 * - If the caller operates on itself, it lowers its priority below that of
307 * other threads in the system, and the caller is preemptible, the thread of
308 * highest priority will be scheduled in.
309 *
310 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
311 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
312 * highest priority.
313 *
314 * @param thread Thread whose priority is to be set.
315 * @param prio New priority.
316 *
317 * @warning Changing the priority of a thread currently involved in mutex
318 * priority inheritance may result in undefined behavior.
319 *
320 * @return N/A
321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400322extern void k_thread_priority_set(k_tid_t thread, int prio);
323
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400324/**
325 * @brief Suspend a thread
326 *
327 * Remove @a thread from scheduling decisions. All other internal operations
328 * on @a thread will still be performed: any timeout it is on keeps ticking
329 * and delivered upon expiry, objects it is waiting on are still handed to it,
330 * etc.
331 *
332 * @param thread Thread to suspend
333 *
334 * @return N/A
335 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400336extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400337
338/**
339 * @brief Resume a previously suspended thread
340 *
341 * Resume using @a thread in scheduling decisions.
342 *
343 * @param thread Thread to resume
344 *
345 * @return N/A
346 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400347extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400348
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400349/**
350 * @brief Set time-slicing period and scope
351 *
352 * This routine controls how thread time slicing is performed by the scheduler
353 * on preemptible threads; it specifes the maximum time slice length (in
354 * milliseconds) and the highest thread priority level for which time slicing
355 * is performed.
356 *
357 * To enable time slicing, a non-zero time slice length must be specified.
358 * The scheduler then ensures that no executing thread runs for more than the
359 * specified number of milliseconds before giving other threads of that priority
360 * a chance to execute. (However, any thread whose priority is higher than the
361 * specified thread priority level is exempted, and may execute as long as
362 * desired without being pre-empted due to time slicing.)
363 *
364 * Time slicing limits only the maximum amount of time a thread may continuously
365 * execute. Once the scheduler selects a thread for execution, there is no
366 * minimum guaranteed time the thread will execute before threads of greater or
367 * equal priority are scheduled.
368 *
369 * When the currently-executing thread is the only one of that priority eligible
370 * for execution, this routine has no effect; the thread is immediately
371 * rescheduled after the slice period expires.
372 *
373 * To disable timeslicing, call the API with both parameters set to zero.
374 *
375 * @return N/A
376 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400377extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400379/**
380 * @brief Determine if code is running at interrupt level
381 *
382 * @return 0 if invoked by a thread, or non-zero if invoked by an ISR
383 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384extern int k_am_in_isr(void);
385
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386/**
387 * @brief Set thread's custom data
388 *
389 * This routine sets the custom data value for the current thread. Custom
390 * data is not used by the kernel itself, and is freely available for the
391 * thread to use as it sees fit.
392 *
393 * This provides a skeleton upon which to build thread-local storage.
394 *
395 * @param value New value to set the thread's custom data to.
396 *
397 * @return N/A
398 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400399extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400400
401/**
402 * @brief Get thread's custom data
403 *
404 * This function returns the custom data value for the current thread.
405 *
406 * @return current custom data value
407 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400408extern void *k_thread_custom_data_get(void);
409
410/**
411 * kernel timing
412 */
413
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400414#include <sys_clock.h>
415
416/* private internal time manipulation (users should never play with ticks) */
417
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500418/* added tick needed to account for tick in progress */
419#define _TICK_ALIGN 1
420
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400421static int64_t __ticks_to_ms(int64_t ticks)
422{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400423#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400424 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400425#else
426 __ASSERT(ticks == 0, "");
427 return 0;
428#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400429}
430
431
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400432/* timeouts */
433
434struct _timeout;
435typedef void (*_timeout_func_t)(struct _timeout *t);
436
437struct _timeout {
438 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400439 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400440 sys_dlist_t *wait_q;
441 int32_t delta_ticks_from_prev;
442 _timeout_func_t func;
443};
444
Allan Stephens45bfa372016-10-12 12:39:42 -0500445
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400446/* timers */
447
448struct k_timer {
449 /*
450 * _timeout structure must be first here if we want to use
451 * dynamic timer allocation. timeout.node is used in the double-linked
452 * list of free timers
453 */
454 struct _timeout timeout;
455
Allan Stephens45bfa372016-10-12 12:39:42 -0500456 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400457 _wait_q_t wait_q;
458
459 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500460 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461
462 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500463 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400464
465 /* timer period */
466 int32_t period;
467
Allan Stephens45bfa372016-10-12 12:39:42 -0500468 /* timer status */
469 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400470
Allan Stephens45bfa372016-10-12 12:39:42 -0500471 /* used to support legacy timer APIs */
472 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400473
474 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
475};
476
Allan Stephens1342adb2016-11-03 13:54:53 -0500477#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400478 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500479 .timeout.delta_ticks_from_prev = -1, \
480 .timeout.wait_q = NULL, \
481 .timeout.thread = NULL, \
482 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400483 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500484 .expiry_fn = expiry, \
485 .stop_fn = stop, \
486 .status = 0, \
487 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400488 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
489 }
490
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491/**
492 * @brief Statically define a timer and initialize it
493 *
494 * If the timer is to be accessed outside the module where it is defined, it
495 * can be declared via
496 *
497 * extern struct k_timer @a name;
498 *
499 * @param name Name of the timer variable.
Allan Stephens1342adb2016-11-03 13:54:53 -0500500 * @param expiry_fn Function to invoke each time timer expires.
501 * @param stop_fn Function to invoke if timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400502 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500503#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500504 struct k_timer name \
505 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500506 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400507
Allan Stephens45bfa372016-10-12 12:39:42 -0500508/**
509 * @brief Initialize a timer.
510 *
511 * This routine must be called before the timer is used.
512 *
513 * @param timer Address of timer.
514 * @param expiry_fn Function to invoke each time timer expires.
515 * @param stop_fn Function to invoke if timer is stopped while running.
516 *
517 * @return N/A
518 */
519extern void k_timer_init(struct k_timer *timer,
520 void (*expiry_fn)(struct k_timer *),
521 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700522
Allan Stephens45bfa372016-10-12 12:39:42 -0500523/**
524 * @brief Start a timer.
525 *
526 * This routine starts a timer, and resets its status to zero. The timer
527 * begins counting down using the specified duration and period values.
528 *
529 * Attempting to start a timer that is already running is permitted.
530 * The timer's status is reset to zero and the timer begins counting down
531 * using the new duration and period values.
532 *
533 * @param timer Address of timer.
534 * @param duration Initial timer duration (in milliseconds).
535 * @param period Timer period (in milliseconds).
536 *
537 * @return N/A
538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400539extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500540 int32_t duration, int32_t period);
541
542/**
543 * @brief Stop a timer.
544 *
545 * This routine stops a running timer prematurely. The timer's stop function,
546 * if one exists, is invoked by the caller.
547 *
548 * Attempting to stop a timer that is not running is permitted, but has no
549 * effect on the timer since it is already stopped.
550 *
551 * @param timer Address of timer.
552 *
553 * @return N/A
554 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400555extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500556
557/**
558 * @brief Read timer status.
559 *
560 * This routine reads the timer's status, which indicates the number of times
561 * it has expired since its status was last read.
562 *
563 * Calling this routine resets the timer's status to zero.
564 *
565 * @param timer Address of timer.
566 *
567 * @return Timer status.
568 */
569extern uint32_t k_timer_status_get(struct k_timer *timer);
570
571/**
572 * @brief Synchronize thread to timer expiration.
573 *
574 * This routine blocks the calling thread until the timer's status is non-zero
575 * (indicating that it has expired at least once since it was last examined)
576 * or the timer is stopped. If the timer status is already non-zero,
577 * or the timer is already stopped, the caller continues without waiting.
578 *
579 * Calling this routine resets the timer's status to zero.
580 *
581 * This routine must not be used by interrupt handlers, since they are not
582 * allowed to block.
583 *
584 * @param timer Address of timer.
585 *
586 * @return Timer status.
587 */
588extern uint32_t k_timer_status_sync(struct k_timer *timer);
589
590/**
591 * @brief Get timer remaining before next timer expiration.
592 *
593 * This routine computes the (approximate) time remaining before a running
594 * timer next expires. If the timer is not running, it returns zero.
595 *
596 * @param timer Address of timer.
597 *
598 * @return Remaining time (in milliseconds).
599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400600extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400601
602
Allan Stephens45bfa372016-10-12 12:39:42 -0500603/* kernel clocks */
604
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400605/**
606 * @brief Get the time elapsed since the system booted (uptime)
607 *
608 * @return The current uptime of the system in ms
609 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400610extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400611
612/**
613 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
614 *
615 * This function is potentially less onerous in both the time it takes to
616 * execute, the interrupt latency it introduces and the amount of 64-bit math
617 * it requires than k_uptime_get(), but it only provides an uptime value of
618 * 32-bits. The user must handle possible rollovers/spillovers.
619 *
620 * At a rate of increment of 1000 per second, it rolls over approximately every
621 * 50 days.
622 *
623 * @return The current uptime of the system in ms
624 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400625extern uint32_t k_uptime_get_32(void);
626
627/**
628 * @brief Get the difference between a reference time and the current uptime
629 *
630 * @param reftime A pointer to a reference time. It is updated with the current
631 * uptime upon return.
632 *
633 * @return The delta between the reference time and the current uptime.
634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400635extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400636
637/**
638 * @brief Get the difference between a reference time and the current uptime
639 *
640 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
641 * k_uptime_get_32().
642 *
643 * @param reftime A pointer to a reference time. It is updated with the current
644 * uptime upon return.
645 *
646 * @return The delta between the reference time and the current uptime.
647 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400648extern uint32_t k_uptime_delta_32(int64_t *reftime);
649
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650/**
651 * @brief Read the platform's timer hardware
652 *
653 * This routine returns the current time in terms of timer hardware clock
654 * cycles.
655 *
656 * @return up counter of elapsed clock cycles
657 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400658extern uint32_t k_cycle_get_32(void);
659
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400660/**
661 * data transfers (basic)
662 */
663
664/* fifos */
665
666struct k_fifo {
667 _wait_q_t wait_q;
668 sys_slist_t data_q;
669
670 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
671};
672
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673/**
674 * @brief Initialize a kernel FIFO object.
675 *
676 * This routine initializes a kernel FIFO object structure. It must not be
677 * called from an ISR.
678 *
679 * @param fifo FIFO to initialize.
680 *
681 * @return N/A
682 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684
685/**
686 * @brief Add an element to the end of a FIFO.
687 *
688 * This routine adds an element to the end of a FIFO. FIFO data items must be
689 * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of
690 * each item for use as a pointer to the next data item in the FIFO's link
691 * list. Each data item added to the FIFO must include and reserve these first
692 * 32 bits.
693 *
694 * @param fifo FIFO on which to interact.
695 * @param data Data to send.
696 *
697 * @return N/A
698 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700
701/**
702 * @brief Atomically add a list of elements to the end of a FIFO.
703 *
704 * This routine adds a list of elements in one shot to the end of a FIFO
705 * object. If threads are pending on the FIFO object, they become ready to run.
706 * If this API is called from a preemptible thread, the highest priority one
707 * will preempt the running thread once the put operation is complete.
708 *
709 * If enough threads are waiting on the FIFO, the address of each element given
710 * to threads is returned to the waiting thread. The remaining elements are
711 * linked to the end of the list.
712 *
713 * The list must be a singly-linked list, where each element only has a pointer
714 * to the next one. The list must be NULL-terminated.
715 *
716 * @param fifo FIFO on which to interact.
717 * @param head head of singly-linked list
718 * @param tail tail of singly-linked list
719 *
720 * @return N/A
721 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400723
724/**
725 * @brief Atomically add a list of elements to the end of a FIFO.
726 *
727 * See k_fifo_put_list for the description of the behaviour.
728 *
729 * It takes a pointer to a sys_slist_t object instead of the head and tail of
730 * a custom singly-linked list. The sys_slist_t object is invalid afterwards
731 * and must be re-initialized via sys_slist_init().
732 *
733 * @param fifo FIFO on which to interact.
734 * @param list pointer to singly-linked list
735 *
736 * @return N/A
737 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400738extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739
740/**
741 * @brief Get an element from the head of a FIFO.
742 *
743 * If no element is available, the function returns NULL. The first word in
744 * the element contains invalid data because its memory location was used to
745 * store a pointer to the next element in the linked list.
746 *
747 * @param fifo FIFO on which to interact.
748 * @param timeout Number of milliseconds to wait for item if FIFO is empty,
749 * or one of the special values K_NO_WAIT and K_FOREVER.
750 *
751 * @warning If it is to be called from the context of an ISR, then @a
752 * timeout must be set to K_NO_WAIT.
753 *
754 * @return Pointer to head element in the list when available.
755 * NULL Otherwise.
756 *
757 * @sa K_NO_WAIT, K_FOREVER
758 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
760
761#define K_FIFO_INITIALIZER(obj) \
762 { \
763 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400764 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400765 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
766 }
767
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768/**
769 * @brief Statically define a FIFO and initialize it
770 *
771 * If the FIFO is to be accessed outside the module where it is defined, it
772 * can be declared via
773 *
774 * extern struct k_fifo @a name;
775 *
776 * @param name Name of the FIFO variable.
777 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500779 struct k_fifo name \
780 __in_section(_k_fifo, static, name) = \
781 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400782
783/* lifos */
784
785struct k_lifo {
786 _wait_q_t wait_q;
787 void *list;
788
789 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
790};
791
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792/**
793 * @brief Initialize a kernel linked list LIFO object.
794 *
795 * This routine initializes a kernel LIFO object structure. It must not be
796 * called from an ISR.
797 *
798 * @param lifo LIFO to initialize.
799 *
800 * @return N/A
801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400802extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803
804/**
805 * @brief Prepend an element to a LIFO
806 *
807 * This routine prepends an element to a LIFO. LIFO data items must be
808 * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of
809 * each item for use as a pointer to the next data item in the LIFO's link
810 * list. Each data item added to the LIFO must include and reserve these first
811 * 32 bits.
812 *
813 * @param lifo LIFO on which to interact.
814 * @param data Data to send.
815 *
816 * @return N/A
817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400818extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819
820/**
821 * @brief Get the first element from a LIFO.
822 *
823 * If no element is available, the function returns NULL. The first word in
824 * the element contains invalid data because its memory location was used to
825 * store a pointer to the next element in the linked list.
826 *
827 * @param lifo LIFO on which to interact.
828 * @param timeout Number of milliseconds to wait for item if LIFO is empty,
829 * or one of the special values K_NO_WAIT and K_FOREVER.
830 *
831 * @warning If it is to be called from the context of an ISR, then @a
832 * timeout must be set to K_NO_WAIT.
833 *
834 * @return Pointer to head element in the list when available.
835 * NULL Otherwise.
836 *
837 * @sa K_NO_WAIT, K_FOREVER
838 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400839extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
840
841#define K_LIFO_INITIALIZER(obj) \
842 { \
843 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
844 .list = NULL, \
845 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
846 }
847
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848/**
849 * @brief Statically define a LIFO and initialize it
850 *
851 * If the LIFO is to be accessed outside the module where it is defined, it
852 * can be declared via
853 *
854 * extern struct k_lifo @a name;
855 *
856 * @param name Name of the LIFO variable.
857 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400858#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500859 struct k_lifo name \
860 __in_section(_k_lifo, static, name) = \
861 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400862
863/* stacks */
864
865struct k_stack {
866 _wait_q_t wait_q;
867 uint32_t *base, *next, *top;
868
869 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
870};
871
Allan Stephens018cd9a2016-10-07 15:13:24 -0500872extern void k_stack_init(struct k_stack *stack,
873 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874extern void k_stack_push(struct k_stack *stack, uint32_t data);
875extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
876
Peter Mitsis602e6a82016-10-17 11:48:43 -0400877#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400878 { \
879 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
880 .base = stack_buffer, \
881 .next = stack_buffer, \
882 .top = stack_buffer + stack_num_entries, \
883 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
884 }
885
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886/**
887 * @brief Statically define a stack object and initialize it
888 *
889 * If the stack is to be accessed outside the module where it is defined, it
890 * can be declared via
891 *
892 * extern struct k_stack @a name;
893 *
894 * @param name Name of the stack object variable.
895 * @param stack_num_entries Number of entries in the stack object
896 */
Peter Mitsis602e6a82016-10-17 11:48:43 -0400897#define K_STACK_DEFINE(name, stack_num_entries) \
898 uint32_t __noinit \
899 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500900 struct k_stack name \
901 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -0400902 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
903 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400905/**
906 * workqueues
907 */
908
909struct k_work;
910
911typedef void (*k_work_handler_t)(struct k_work *);
912
913/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400914 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400915 * queued to it. This is useful for drivers which need to schedule
916 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400918 * space.
919 */
920struct k_work_q {
921 struct k_fifo fifo;
922};
923
924/**
925 * @brief Work flags.
926 */
927enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300928 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400929};
930
931/**
932 * @brief An item which can be scheduled on a @ref k_work_q.
933 */
934struct k_work {
935 void *_reserved; /* Used by k_fifo implementation. */
936 k_work_handler_t handler;
937 atomic_t flags[1];
938};
939
940/**
941 * @brief Statically initialize work item
942 */
943#define K_WORK_INITIALIZER(work_handler) \
944 { \
945 ._reserved = NULL, \
946 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300947 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400948 }
949
950/**
951 * @brief Dynamically initialize work item
952 */
953static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
954{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300955 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400956 work->handler = handler;
957}
958
959/**
960 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300961 *
962 * This procedure schedules a work item to be processed.
963 * In the case where the work item has already been submitted and is pending
964 * execution, calling this function will result in a no-op. In this case, the
965 * work item must not be modified externally (e.g. by the caller of this
966 * function), since that could cause the work item to be processed in a
967 * corrupted state.
968 *
969 * @param work_q to schedule the work item
970 * @param work work item
971 *
972 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400973 */
974static inline void k_work_submit_to_queue(struct k_work_q *work_q,
975 struct k_work *work)
976{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300977 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400978 k_fifo_put(&work_q->fifo, work);
979 }
980}
981
982/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300983 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
985 * @param work Work item to query
986 *
987 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300988 */
989static inline int k_work_pending(struct k_work *work)
990{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300991 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300992}
993
994/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 * @brief Start a new workqueue.
996 *
997 * This routine must not be called from an ISR.
998 *
999 * @param work_q Pointer to Work queue
1000 * @param stack Pointer to work queue thread's stack
1001 * @param stack_size Size of the work queue thread's stack
1002 * @param prio Priority of the work queue's thread
1003 *
1004 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001005 */
Allan Stephens904cf972016-10-07 13:59:23 -05001006extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1007 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001008
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001009#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001010
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011/**
1012 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001013 */
1014struct k_delayed_work {
1015 struct k_work work;
1016 struct _timeout timeout;
1017 struct k_work_q *work_q;
1018};
1019
1020/**
1021 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 *
1023 * Initialize a delayed work item.
1024 *
1025 * @param work Delayed work item
1026 * @param handler Routine invoked when processing delayed work item
1027 *
1028 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001030extern void k_delayed_work_init(struct k_delayed_work *work,
1031 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001032
1033/**
1034 * @brief Submit a delayed work item to a workqueue.
1035 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037 * Once the delay has passed, the work item is submitted to the work queue:
1038 * at this point, it is no longer possible to cancel it. Once the work item's
1039 * handler is about to be executed, the work is considered complete and can be
1040 * resubmitted.
1041 *
1042 * Care must be taken if the handler blocks or yield as there is no implicit
1043 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1044 * it should be explicitly done between the submitter and the handler.
1045 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001046 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001048 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001049 *
1050 * @return 0 in case of success or negative value in case of error.
1051 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001052extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1053 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001054 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055
1056/**
1057 * @brief Cancel a delayed work item
1058 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001060 * or is idle, this will do nothing. The only case where this can fail is when
1061 * the work has been submitted to the work queue, but the handler has not run
1062 * yet.
1063 *
1064 * @param work Delayed work item to be canceled
1065 *
1066 * @return 0 in case of success or negative value in case of error.
1067 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001068extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001070#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072extern struct k_work_q k_sys_work_q;
1073
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001075 * @brief Submit a work item to the system workqueue.
1076 *
1077 * @ref k_work_submit_to_queue
1078 *
1079 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001081 * unexpected behavior.
1082 */
1083static inline void k_work_submit(struct k_work *work)
1084{
1085 k_work_submit_to_queue(&k_sys_work_q, work);
1086}
1087
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001088#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001090 * @brief Submit a delayed work item to the system workqueue.
1091 *
1092 * @ref k_delayed_work_submit_to_queue
1093 *
1094 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096 * unexpected behavior.
1097 */
1098static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001099 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001100{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001101 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102}
1103
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001104#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105
1106/**
1107 * synchronization
1108 */
1109
1110/* mutexes */
1111
1112struct k_mutex {
1113 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001114 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001115 uint32_t lock_count;
1116 int owner_orig_prio;
1117#ifdef CONFIG_OBJECT_MONITOR
1118 int num_lock_state_changes;
1119 int num_conflicts;
1120#endif
1121
1122 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1123};
1124
1125#ifdef CONFIG_OBJECT_MONITOR
1126#define _MUTEX_INIT_OBJECT_MONITOR \
1127 .num_lock_state_changes = 0, .num_conflicts = 0,
1128#else
1129#define _MUTEX_INIT_OBJECT_MONITOR
1130#endif
1131
1132#define K_MUTEX_INITIALIZER(obj) \
1133 { \
1134 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1135 .owner = NULL, \
1136 .lock_count = 0, \
1137 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1138 _MUTEX_INIT_OBJECT_MONITOR \
1139 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1140 }
1141
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142/**
1143 * @brief Statically define a mutex object and initialize it
1144 *
1145 * If the mutex is to be accessed outside the module where it is defined, it
1146 * can be declared via
1147 *
1148 * extern struct k_mutex @a name;
1149 *
1150 * @param name Name of the mutex object variable.
1151 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001152#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001153 struct k_mutex name \
1154 __in_section(_k_mutex, static, name) = \
1155 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001156
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157/**
1158 * @brief Initialize a mutex
1159 *
1160 * Upon initialization, the mutex is available and does not have an owner.
1161 *
1162 * @param mutex Mutex to initialize
1163 *
1164 * @return N/A
1165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167
1168/**
1169 * @brief Lock a mutex
1170 *
1171 * This routine locks mutex @a mutex. When the mutex is locked by another
1172 * thread, the thread calling this function will either wait until the mutex
1173 * becomes available, or until a specified timeout expires.
1174 *
1175 * A thread is permitted to lock a mutex it has already locked; in such a case,
1176 * this routine immediately succeeds and the lock count is increased by 1.
1177 *
1178 * @param mutex Pointer to a mutex object.
1179 * @param timeout Number of milliseconds to wait if mutex is unavailable,
1180 * or one of the special values K_NO_WAIT and K_FOREVER.
1181 *
1182 * @retval 0 When semaphore is obtained successfully.
1183 * @retval -EBUSY Failed to immediately lock mutex when @a timeout is K_NO_WAIT.
1184 * @retval -EAGAIN When timeout expires.
1185 *
1186 * @sa K_NO_WAIT, K_FOREVER
1187 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001189
1190/**
1191 * @brief Unlock a mutex
1192 *
1193 * This routine unlocks mutex @a mutex. The mutex must already be locked by the
1194 * requesting thread.
1195 *
1196 * The mutex cannot be claimed by another thread until it has been unlocked by
1197 * the requesting thread as many times as it was previously locked by that
1198 * thread.
1199 *
1200 * @param mutex Mutex name.
1201 *
1202 * @return N/A
1203 */
1204
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001205extern void k_mutex_unlock(struct k_mutex *mutex);
1206
1207/* semaphores */
1208
1209struct k_sem {
1210 _wait_q_t wait_q;
1211 unsigned int count;
1212 unsigned int limit;
1213
1214 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1215};
1216
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001217/**
1218 * @brief Initialize a semaphore object.
1219 *
1220 * An initial count and a count limit can be specified. The count will never go
1221 * over the count limit if the semaphore is given multiple times without being
1222 * taken.
1223 *
1224 * Cannot be called from ISR.
1225 *
1226 * @param sem Pointer to a semaphore object.
1227 * @param initial_count Initial count.
1228 * @param limit Highest value the count can take during operation.
1229 *
1230 * @return N/A
1231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1233 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001234
1235/**
1236 * @brief Take a semaphore, possibly pending if not available.
1237 *
1238 * The current execution context tries to obtain the semaphore. If the
1239 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
1240 * the context will pend.
1241 *
1242 * @param sem Pointer to a semaphore object.
1243 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
1244 * or one of the special values K_NO_WAIT and K_FOREVER.
1245 *
1246 * @warning If it is called from the context of an ISR, then the only legal
1247 * value for @a timeout is K_NO_WAIT.
1248 *
1249 * @retval 0 When semaphore is obtained successfully.
1250 * @retval -EAGAIN When timeout expires.
1251 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
1252 *
1253 * @sa K_NO_WAIT, K_FOREVER
1254 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001255extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001256
1257/**
1258 * @brief Give a semaphore.
1259 *
1260 * Increase the semaphore's internal count by 1, up to its limit, if no thread
1261 * is waiting on the semaphore; otherwise, wake up the first thread in the
1262 * semaphore's waiting queue.
1263 *
1264 * If the latter case, and if the current context is preemptible, the thread
1265 * that is taken off the wait queue will be scheduled in and will preempt the
1266 * current thread.
1267 *
1268 * @param sem Pointer to a semaphore object.
1269 *
1270 * @return N/A
1271 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001272extern void k_sem_give(struct k_sem *sem);
1273
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001274/**
1275 * @brief Reset a semaphore's count to zero.
1276 *
1277 * The only effect is that the count is set to zero. There is no other
1278 * side-effect to calling this function.
1279 *
1280 * @param sem Pointer to a semaphore object.
1281 *
1282 * @return N/A
1283 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001284static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285{
1286 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287}
1288
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001289/**
1290 * @brief Get a semaphore's count.
1291 *
1292 * Note there is no guarantee the count has not changed by the time this
1293 * function returns.
1294 *
1295 * @param sem Pointer to a semaphore object.
1296 *
1297 * @return The current semaphore count.
1298 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001299static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001300{
1301 return sem->count;
1302}
1303
Peter Mitsis45403672016-09-09 14:24:06 -04001304#ifdef CONFIG_SEMAPHORE_GROUPS
1305/**
1306 * @brief Take the first available semaphore
1307 *
1308 * Given a list of semaphore pointers, this routine will attempt to take one
1309 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
1310 * semaphore is identified by @a sem (set to NULL on error).
1311 *
1312 * Be aware that the more semaphores specified in the group, the more stack
1313 * space is required by the waiting thread.
1314 *
1315 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1316 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001317 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
1318 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -04001319 *
1320 * @retval 0 A semaphore was successfully taken
1321 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
1322 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001323 *
1324 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -04001325 */
1326
1327extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
1328 int32_t timeout);
1329
1330/**
1331 * @brief Give all the semaphores in the group
1332 *
1333 * This routine will give each semaphore in the array of semaphore pointers.
1334 *
1335 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1336 *
1337 * @return N/A
1338 */
1339extern void k_sem_group_give(struct k_sem *sem_array[]);
1340
1341/**
1342 * @brief Reset the count to zero on each semaphore in the array
1343 *
1344 * This routine resets the count of each semaphore in the group to zero.
1345 * Note that it does NOT have any impact on any thread that might have
1346 * been previously pending on any of the semaphores.
1347 *
1348 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1349 *
1350 * @return N/A
1351 */
1352extern void k_sem_group_reset(struct k_sem *sem_array[]);
1353#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354
1355#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1356 { \
1357 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1358 .count = initial_count, \
1359 .limit = count_limit, \
1360 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1361 }
1362
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001363/**
1364 * @def K_SEM_DEFINE
1365 *
1366 * @brief Statically define and initialize a global semaphore.
1367 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 * Create a global semaphore named @a name. It is initialized as if k_sem_init()
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001369 * was called on it. If the semaphore is to be accessed outside the module
1370 * where it is defined, it can be declared via
1371 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001373 *
1374 * @param name Name of the semaphore variable.
1375 * @param initial_count Initial count.
1376 * @param count_limit Highest value the count can take during operation.
1377 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001379 struct k_sem name \
1380 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001381 K_SEM_INITIALIZER(name, initial_count, count_limit)
1382
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001383/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001385#define K_ALERT_DEFAULT NULL
1386#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001388typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001389
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001390struct k_alert {
1391 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392 atomic_t send_count;
1393 struct k_work work_item;
1394 struct k_sem sem;
1395
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001396 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397};
1398
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001399extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001401#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001403 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001405 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001406 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1408 }
1409
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001410/**
1411 * @brief Statically define and initialize a global alert
1412 *
1413 * Create a global alert named @a name. It is initialized as if k_alert_init()
1414 * was called on it. If the alert is to be accessed outside the module
1415 * where it is defined, it can be declared via
1416 *
1417 * extern struct k_alert @a name;
1418 *
1419 * @param name Alert name
1420 * @param alert_handler Handler to invoke after the delivery of the alert
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001421 * @param max_num_pending_alerts Maximum number of concurrent pending alerts
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001422 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001423#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001424 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001425 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001426 K_ALERT_INITIALIZER(name, alert_handler, \
1427 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429/**
1430 * @brief Initialize an alert object.
1431 *
1432 * This routine initializes a kernel alert object structure. It must not be
1433 * called from an ISR.
1434 *
1435 * @param alert Pointer to the alert object
1436 * @param handler Routine to invoke after delivery of alert
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001437 * @param max_num_pending_alerts Maximum number of concurrent pending alerts
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001438 *
1439 * @return N/A
1440 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001441extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1442 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001443
1444/**
1445 * @brief Receive an alert
1446 *
1447 * The current execution context tries to receive the alert. If the
1448 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
1449 * the context will pend.
1450 *
1451 * @param alert Pointer to a alert object.
1452 * @param timeout Number of milliseconds to wait if alert is unavailable,
1453 * or one of the special values K_NO_WAIT and K_FOREVER.
1454 *
1455 * @warning If it is called from the context of an ISR, then the only legal
1456 * value for @a timeout is K_NO_WAIT.
1457 *
1458 * @retval 0 When alert is received successfully.
1459 * @retval -EAGAIN When timeout expires.
1460 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
1461 *
1462 * @sa K_NO_WAIT, K_FOREVER
1463 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001464extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465
1466/**
1467 * @brief Signal an alert
1468 *
1469 * This routine signals the specified alert. If an alert handler is installed
1470 * for that alert, it will run. If no alert handler is installed, any thread
1471 * waiting on the alert is released.
1472 *
1473 * @param alert Alert to signal
1474 *
1475 * @return N/A
1476 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001477extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001478
1479/**
1480 * data transfers (complex)
1481 */
1482
1483/* message queues */
1484
1485struct k_msgq {
1486 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001487 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001488 uint32_t max_msgs;
1489 char *buffer_start;
1490 char *buffer_end;
1491 char *read_ptr;
1492 char *write_ptr;
1493 uint32_t used_msgs;
1494
1495 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1496};
1497
Peter Mitsis1da807e2016-10-06 11:36:59 -04001498#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499 { \
1500 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001501 .max_msgs = q_max_msgs, \
1502 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001504 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505 .read_ptr = q_buffer, \
1506 .write_ptr = q_buffer, \
1507 .used_msgs = 0, \
1508 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1509 }
1510
Peter Mitsis1da807e2016-10-06 11:36:59 -04001511/**
1512 * @brief Define a message queue
1513 *
1514 * This declares and initializes a message queue whose buffer is aligned to
1515 * a @a q_align -byte boundary. The new message queue can be passed to the
1516 * kernel's message queue functions.
1517 *
1518 * Note that for each of the mesages in the message queue to be aligned to
1519 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1520 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001521 * If the message queue is to be accessed outside the module where it is
1522 * defined, it can be declared via
1523 *
1524 * extern struct k_msgq @a name;
1525 *
Peter Mitsis1da807e2016-10-06 11:36:59 -04001526 * @param q_name Name of the message queue
1527 * @param q_msg_size The size in bytes of each message
1528 * @param q_max_msgs Maximum number of messages the queue can hold
1529 * @param q_align Alignment of the message queue's buffer (power of 2)
1530 */
1531#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1532 static char __noinit __aligned(q_align) \
1533 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001534 struct k_msgq q_name \
1535 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001536 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1537 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538
Peter Mitsisd7a37502016-10-13 11:37:40 -04001539/**
1540 * @brief Initialize a message queue.
1541 *
1542 * @param q Pointer to the message queue object.
1543 * @param buffer Pointer to memory area that holds queued messages.
1544 * @param msg_size Message size, in bytes.
1545 * @param max_msgs Maximum number of messages that can be queued.
1546 *
1547 * @return N/A
1548 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001549extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001550 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001551
1552/**
1553 * @brief Add a message to a message queue.
1554 *
1555 * This routine adds an item to the message queue. When the message queue is
1556 * full, the routine will wait either for space to become available, or until
1557 * the specified time limit is reached.
1558 *
1559 * @param q Pointer to the message queue object.
1560 * @param data Pointer to message data area.
1561 * @param timeout Number of milliseconds to wait until space becomes available
1562 * to add the message into the message queue, or one of the
1563 * special values K_NO_WAIT and K_FOREVER.
1564 *
1565 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1566 * -EAGAIN if timed out
1567 *
1568 * @sa K_NO_WAIT, K_FOREVER
1569 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001571
1572/**
1573 * @brief Obtain a message from a message queue.
1574 *
1575 * This routine fetches the oldest item from the message queue. When the message
1576 * queue is found empty, the routine will wait either until an item is added to
1577 * the message queue or until the specified time limit is reached.
1578 *
1579 * @param q Pointer to the message queue object.
1580 * @param data Pointer to message data area.
1581 * @param timeout Number of milliseconds to wait to obtain message, or one of
1582 * the special values K_NO_WAIT and K_FOREVER.
1583 *
1584 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1585 *
1586 * @sa K_NO_WAIT, K_FOREVER
1587 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001588extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001589
1590/**
1591 * @brief Purge contents of a message queue.
1592 *
1593 * Discards all messages currently in the message queue, and cancels
1594 * any "add message" operations initiated by waiting threads.
1595 *
1596 * @param q Pointer to the message queue object.
1597 *
1598 * @return N/A
1599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001600extern void k_msgq_purge(struct k_msgq *q);
1601
Peter Mitsis67be2492016-10-07 11:44:34 -04001602/**
1603 * @brief Get the number of unused messages
1604 *
1605 * @param q Message queue to query
1606 *
1607 * @return Number of unused messages
1608 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001609static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001610{
1611 return q->max_msgs - q->used_msgs;
1612}
1613
Peter Mitsisd7a37502016-10-13 11:37:40 -04001614/**
1615 * @brief Get the number of used messages
1616 *
1617 * @param q Message queue to query
1618 *
1619 * @return Number of used messages
1620 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001621static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001622{
1623 return q->used_msgs;
1624}
1625
1626struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001627 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001628 void *addr_in_pool;
1629 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001630 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631};
1632
1633/* mailboxes */
1634
1635struct k_mbox_msg {
1636 /** internal use only - needed for legacy API support */
1637 uint32_t _mailbox;
1638 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001639 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001640 /** application-defined information value */
1641 uint32_t info;
1642 /** sender's message data buffer */
1643 void *tx_data;
1644 /** internal use only - needed for legacy API support */
1645 void *_rx_data;
1646 /** message data block descriptor */
1647 struct k_mem_block tx_block;
1648 /** source thread id */
1649 k_tid_t rx_source_thread;
1650 /** target thread id */
1651 k_tid_t tx_target_thread;
1652 /** internal use only - thread waiting on send (may be a dummy) */
1653 k_tid_t _syncing_thread;
1654#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1655 /** internal use only - semaphore used during asynchronous send */
1656 struct k_sem *_async_sem;
1657#endif
1658};
1659
1660struct k_mbox {
1661 _wait_q_t tx_msg_queue;
1662 _wait_q_t rx_msg_queue;
1663
1664 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1665};
1666
1667#define K_MBOX_INITIALIZER(obj) \
1668 { \
1669 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1670 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1671 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1672 }
1673
Peter Mitsis12092702016-10-14 12:57:23 -04001674/**
1675 * @brief Define a mailbox
1676 *
1677 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001678 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001679 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001680 * If the mailbox is to be accessed outside the module where it is defined, it
1681 * can be declared via
1682 *
1683 * extern struct k_mbox @a name;
1684 *
Peter Mitsis12092702016-10-14 12:57:23 -04001685 * @param name Name of the mailbox
1686 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001687#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001688 struct k_mbox name \
1689 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001690 K_MBOX_INITIALIZER(name) \
1691
Peter Mitsis12092702016-10-14 12:57:23 -04001692/**
1693 * @brief Initialize a mailbox.
1694 *
1695 * @param mbox Pointer to the mailbox object
1696 *
1697 * @return N/A
1698 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001699extern void k_mbox_init(struct k_mbox *mbox);
1700
Peter Mitsis12092702016-10-14 12:57:23 -04001701/**
1702 * @brief Send a mailbox message in a synchronous manner.
1703 *
1704 * Sends a message to a mailbox and waits for a receiver to process it.
1705 * The message data may be in a buffer, in a memory pool block, or non-existent
1706 * (i.e. empty message).
1707 *
1708 * @param mbox Pointer to the mailbox object.
1709 * @param tx_msg Pointer to transmit message descriptor.
1710 * @param timeout Maximum time (milliseconds) to wait for the message to be
1711 * received (although not necessarily completely processed).
1712 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1713 * as necessary.
1714 *
1715 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1716 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001717extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001719
1720#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1721/**
1722 * @brief Send a mailbox message in an asynchronous manner.
1723 *
1724 * Sends a message to a mailbox without waiting for a receiver to process it.
1725 * The message data may be in a buffer, in a memory pool block, or non-existent
1726 * (i.e. an empty message). Optionally, the specified semaphore will be given
1727 * by the mailbox when the message has been both received and disposed of
1728 * by the receiver.
1729 *
1730 * @param mbox Pointer to the mailbox object.
1731 * @param tx_msg Pointer to transmit message descriptor.
1732 * @param sem Semaphore identifier, or NULL if none specified.
1733 *
1734 * @return N/A
1735 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001736extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001738#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001739
Peter Mitsis12092702016-10-14 12:57:23 -04001740/**
1741 * @brief Receive a mailbox message.
1742 *
1743 * Receives a message from a mailbox, then optionally retrieves its data
1744 * and disposes of the message.
1745 *
1746 * @param mbox Pointer to the mailbox object.
1747 * @param rx_msg Pointer to receive message descriptor.
1748 * @param buffer Pointer to buffer to receive data.
1749 * (Use NULL to defer data retrieval and message disposal until later.)
1750 * @param timeout Maximum time (milliseconds) to wait for a message.
1751 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1752 * necessary.
1753 *
1754 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1755 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001756extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001757 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001758
1759/**
1760 * @brief Retrieve mailbox message data into a buffer.
1761 *
1762 * Completes the processing of a received message by retrieving its data
1763 * into a buffer, then disposing of the message.
1764 *
1765 * Alternatively, this routine can be used to dispose of a received message
1766 * without retrieving its data.
1767 *
1768 * @param rx_msg Pointer to receive message descriptor.
1769 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1770 *
1771 * @return N/A
1772 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001773extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001774
1775/**
1776 * @brief Retrieve mailbox message data into a memory pool block.
1777 *
1778 * Completes the processing of a received message by retrieving its data
1779 * into a memory pool block, then disposing of the message. The memory pool
1780 * block that results from successful retrieval must be returned to the pool
1781 * once the data has been processed, even in cases where zero bytes of data
1782 * are retrieved.
1783 *
1784 * Alternatively, this routine can be used to dispose of a received message
1785 * without retrieving its data. In this case there is no need to return a
1786 * memory pool block to the pool.
1787 *
1788 * This routine allocates a new memory pool block for the data only if the
1789 * data is not already in one. If a new block cannot be allocated, the routine
1790 * returns a failure code and the received message is left unchanged. This
1791 * permits the caller to reattempt data retrieval at a later time or to dispose
1792 * of the received message without retrieving its data.
1793 *
1794 * @param rx_msg Pointer to receive message descriptor.
1795 * @param pool Memory pool identifier. (Use NULL to discard data.)
1796 * @param block Pointer to area to hold memory pool block info.
1797 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1798 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1799 * necessary.
1800 *
1801 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1802 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001803extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001804 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805 struct k_mem_block *block, int32_t timeout);
1806
1807/* pipes */
1808
1809struct k_pipe {
1810 unsigned char *buffer; /* Pipe buffer: may be NULL */
1811 size_t size; /* Buffer size */
1812 size_t bytes_used; /* # bytes used in buffer */
1813 size_t read_index; /* Where in buffer to read from */
1814 size_t write_index; /* Where in buffer to write */
1815
1816 struct {
1817 _wait_q_t readers; /* Reader wait queue */
1818 _wait_q_t writers; /* Writer wait queue */
1819 } wait_q;
1820
1821 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1822};
1823
Peter Mitsise5d9c582016-10-14 14:44:57 -04001824#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825 { \
1826 .buffer = pipe_buffer, \
1827 .size = pipe_buffer_size, \
1828 .bytes_used = 0, \
1829 .read_index = 0, \
1830 .write_index = 0, \
1831 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1832 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1833 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1834 }
1835
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836/**
1837 * @brief Define a pipe
1838 *
1839 * This declares and initializes a pipe. The new pipe can be passed to
1840 * the kernel's pipe functions.
1841 *
1842 * If the pipe is to be accessed outside the module where it is defined, it
1843 * can be declared via
1844 *
1845 * extern struct k_pipe @a name;
1846 *
1847 * @param name Name of the mailbox
1848 * @param pipe_buffer_size Size of the pipe's buffer (may be zero)
1849 * @param pipe_align Alignment of the pipe's buffer
1850 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001851#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1852 static unsigned char __noinit __aligned(pipe_align) \
1853 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001854 struct k_pipe name \
1855 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001856 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001857
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858/**
1859 * @brief Runtime initialization of a pipe
1860 *
1861 * @param pipe Pointer to pipe to initialize
1862 * @param buffer Pointer to buffer to use for pipe's ring buffer
1863 * @param size Size of the pipe's ring buffer
1864 *
1865 * @return N/A
1866 */
1867extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1868 size_t size);
1869
1870/**
1871 * @brief Put a message into the specified pipe
1872 *
1873 * This routine synchronously adds a message into the pipe specified by
1874 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001875 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1876 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1878 *
1879 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001880 * @param data Data to put into the pipe
1881 * @param bytes_to_write Desired number of bytes to put into the pipe
1882 * @param bytes_written Number of bytes the pipe accepted
1883 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884 * @param timeout Maximum number of milliseconds to wait
1885 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001886 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001888 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001889 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001890extern int k_pipe_put(struct k_pipe *pipe, void *data,
1891 size_t bytes_to_write, size_t *bytes_written,
1892 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001893
1894/**
1895 * @brief Get a message from the specified pipe
1896 *
1897 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001898 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001899 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001900 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1902 *
1903 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001904 * @param data Location to place retrieved data
1905 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1906 * @param bytes_read Number of bytes retrieved from the pipe
1907 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001908 * @param timeout Maximum number of milliseconds to wait
1909 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001910 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001912 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001914extern int k_pipe_get(struct k_pipe *pipe, void *data,
1915 size_t bytes_to_read, size_t *bytes_read,
1916 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001917
Peter Mitsis2fef0232016-10-14 14:53:44 -04001918#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001919/**
1920 * @brief Send a message to the specified pipe
1921 *
1922 * This routine asynchronously sends a message from the pipe specified by
1923 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1924 * free the memory block @a block and give the semaphore @a sem (if specified).
1925 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1926 * at any given time.
1927 *
1928 * @param pipe Pointer to the pipe
1929 * @param block Memory block containing data to send
1930 * @param size Number of data bytes in memory block to send
1931 * @param sem Semaphore to signal upon completion (else NULL)
1932 *
1933 * @retval N/A
1934 */
1935extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1936 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001937#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938
1939/**
1940 * memory management
1941 */
1942
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001943/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001945struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001947 uint32_t num_blocks;
1948 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949 char *buffer;
1950 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001951 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001953 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954};
1955
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001956#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
1957 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001958 { \
1959 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001960 .num_blocks = slab_num_blocks, \
1961 .block_size = slab_block_size, \
1962 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963 .free_list = NULL, \
1964 .num_used = 0, \
1965 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1966 }
1967
Peter Mitsis578f9112016-10-07 13:50:31 -04001968/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969 * @brief Define a memory slab allocator
Peter Mitsis578f9112016-10-07 13:50:31 -04001970 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 * This declares and initializes a slab allocator whose buffer is aligned to
1972 * a @a slab_align -byte boundary. The new slab allocator can be passed to the
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001973 * kernel's memory slab functions.
Peter Mitsis578f9112016-10-07 13:50:31 -04001974 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001975 * Note that for each of the blocks in the memory slab to be aligned to
1976 * @a slab_align bytes, then @a slab_block_size must be a multiple of
1977 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04001978 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979 * If the slab allocator is to be accessed outside the module where it is
1980 * defined, it can be declared via
1981 *
1982 * extern struct k_mem_slab @a name;
1983 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001984 * @param name Name of the memory slab
1985 * @param slab_block_size Size of each block in the buffer (in bytes)
1986 * @param slab_num_blocks Number blocks in the buffer
1987 * @param slab_align Alignment of the memory slab's buffer (power of 2)
Peter Mitsis578f9112016-10-07 13:50:31 -04001988 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001989#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
1990 char __noinit __aligned(slab_align) \
1991 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
1992 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001993 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001994 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
1995 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001996
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001997/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001998 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001999 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002000 * Initializes the memory slab and creates its list of free blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002001 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002002 * @param slab Pointer to the memory slab object
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002003 * @param buffer Pointer to buffer used for the blocks.
2004 * @param block_size Size of each block, in bytes.
2005 * @param num_blocks Number of blocks.
2006 *
2007 * @return N/A
2008 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002009extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002010 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002011
2012/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002013 * @brief Allocate a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002014 *
2015 * Takes a block from the list of unused blocks.
2016 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002017 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002018 * @param mem Pointer to area to receive block address.
2019 * @param timeout Maximum time (milliseconds) to wait for allocation to
2020 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait
2021 * as long as necessary.
2022 *
2023 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
2024 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002025extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2026 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002027
2028/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002029 * @brief Free a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002030 *
2031 * Gives block to a waiting thread if there is one, otherwise returns it to
2032 * the list of unused blocks.
2033 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002034 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002035 * @param mem Pointer to area to containing block address.
2036 *
2037 * @return N/A
2038 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002039extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002040
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002041/**
2042 * @brief Get the number of used memory blocks
2043 *
2044 * This routine gets the current number of used memory blocks in the
2045 * specified pool. It should be used for stats purposes only as that
2046 * value may potentially be out-of-date by the time it is used.
2047 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002048 * @param slab Memory slab to query
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002049 *
2050 * @return Number of used memory blocks
2051 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002052static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002054 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055}
2056
Peter Mitsisc001aa82016-10-13 13:53:37 -04002057/**
2058 * @brief Get the number of unused memory blocks
2059 *
2060 * This routine gets the current number of unused memory blocks in the
2061 * specified pool. It should be used for stats purposes only as that value
2062 * may potentially be out-of-date by the time it is used.
2063 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002064 * @param slab Memory slab to query
Peter Mitsisc001aa82016-10-13 13:53:37 -04002065 *
2066 * @return Number of unused memory blocks
2067 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002068static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002069{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002070 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002071}
2072
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002073/* memory pools */
2074
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002075/*
2076 * Memory pool requires a buffer and two arrays of structures for the
2077 * memory block accounting:
2078 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2079 * status of four blocks of memory.
2080 */
2081struct k_mem_pool_quad_block {
2082 char *mem_blocks; /* pointer to the first of four memory blocks */
2083 uint32_t mem_status; /* four bits. If bit is set, memory block is
2084 allocated */
2085};
2086/*
2087 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2088 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2089 * block size is 4 times less than the previous one and thus requires 4 times
2090 * bigger array of k_mem_pool_quad_block structures to keep track of the
2091 * memory blocks.
2092 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002094/*
2095 * The array of k_mem_pool_block_set keeps the information of each array of
2096 * k_mem_pool_quad_block structures
2097 */
2098struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002099 size_t block_size; /* memory block size */
2100 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002101 struct k_mem_pool_quad_block *quad_block;
2102 int count;
2103};
2104
2105/* Memory pool descriptor */
2106struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002107 size_t max_block_size;
2108 size_t min_block_size;
2109 uint32_t nr_of_maxblocks;
2110 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002111 struct k_mem_pool_block_set *block_set;
2112 char *bufblock;
2113 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2115};
2116
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002117#ifdef CONFIG_ARM
2118#define _SECTION_TYPE_SIGN "%"
2119#else
2120#define _SECTION_TYPE_SIGN "@"
2121#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002123/*
2124 * Static memory pool initialization
2125 */
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002126/**
2127 * @cond internal
2128 * Make Doxygen skip assembler macros
2129 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002130/*
2131 * Use .altmacro to be able to recalculate values and pass them as string
2132 * arguments when calling assembler macros resursively
2133 */
2134__asm__(".altmacro\n\t");
2135
2136/*
2137 * Recursively calls a macro
2138 * The followig global symbols need to be initialized:
2139 * __memory_pool_max_block_size - maximal size of the memory block
2140 * __memory_pool_min_block_size - minimal size of the memory block
2141 * Notes:
2142 * Global symbols are used due the fact that assembler macro allows only
2143 * one argument be passed with the % conversion
2144 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2145 * is used instead of / 4.
2146 * n_max argument needs to go first in the invoked macro, as some
2147 * assemblers concatenate \name and %(\n_max * 4) arguments
2148 * if \name goes first
2149 */
2150__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2151 ".ifge __memory_pool_max_block_size >> 2 -"
2152 " __memory_pool_min_block_size\n\t\t"
2153 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2154 "\\macro_name %(\\n_max * 4) \\name\n\t"
2155 ".endif\n\t"
2156 ".endm\n");
2157
2158/*
2159 * Build quad blocks
2160 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2161 * structures and recursively calls itself for the next array, 4 times
2162 * larger.
2163 * The followig global symbols need to be initialized:
2164 * __memory_pool_max_block_size - maximal size of the memory block
2165 * __memory_pool_min_block_size - minimal size of the memory block
2166 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2167 */
2168__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002169 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002170 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2171 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2172 ".if \\n_max % 4\n\t\t"
2173 ".skip __memory_pool_quad_block_size\n\t"
2174 ".endif\n\t"
2175 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2176 ".endm\n");
2177
2178/*
2179 * Build block sets and initialize them
2180 * Macro initializes the k_mem_pool_block_set structure and
2181 * recursively calls itself for the next one.
2182 * The followig global symbols need to be initialized:
2183 * __memory_pool_max_block_size - maximal size of the memory block
2184 * __memory_pool_min_block_size - minimal size of the memory block
2185 * __memory_pool_block_set_count, the number of the elements in the
2186 * block set array must be set to 0. Macro calculates it's real
2187 * value.
2188 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2189 * structures, _build_quad_blocks must be called prior it.
2190 */
2191__asm__(".macro _build_block_set n_max, name\n\t"
2192 ".int __memory_pool_max_block_size\n\t" /* block_size */
2193 ".if \\n_max % 4\n\t\t"
2194 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2195 ".else\n\t\t"
2196 ".int \\n_max >> 2\n\t"
2197 ".endif\n\t"
2198 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2199 ".int 0\n\t" /* count */
2200 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2201 "__do_recurse _build_block_set \\name \\n_max\n\t"
2202 ".endm\n");
2203
2204/*
2205 * Build a memory pool structure and initialize it
2206 * Macro uses __memory_pool_block_set_count global symbol,
2207 * block set addresses and buffer address, it may be called only after
2208 * _build_block_set
2209 */
2210__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002211 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002212 _SECTION_TYPE_SIGN "progbits\n\t"
2213 ".globl \\name\n\t"
2214 "\\name:\n\t"
2215 ".int \\max_size\n\t" /* max_block_size */
2216 ".int \\min_size\n\t" /* min_block_size */
2217 ".int \\n_max\n\t" /* nr_of_maxblocks */
2218 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2219 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2220 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2221 ".int 0\n\t" /* wait_q->head */
2222 ".int 0\n\t" /* wait_q->next */
2223 ".popsection\n\t"
2224 ".endm\n");
2225
2226#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2227 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2228 _SECTION_TYPE_SIGN "progbits\n\t"); \
2229 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2230 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2231 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2232 STRINGIFY(name) "\n\t"); \
2233 __asm__(".popsection\n\t")
2234
2235#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2236 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2237 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2238 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2239 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002240 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002241 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2242 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2243 STRINGIFY(name) "\n\t"); \
2244 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2245 __asm__(".int __memory_pool_block_set_count\n\t"); \
2246 __asm__(".popsection\n\t"); \
2247 extern uint32_t _mem_pool_block_set_count_##name; \
2248 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2249
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002250#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2251 char __noinit __aligned(align) \
2252 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002253
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002254/*
2255 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2256 * to __memory_pool_quad_block_size absolute symbol.
2257 * This function does not get called, but compiler calculates the value and
2258 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2259 */
2260static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2261{
2262 __asm__(".globl __memory_pool_quad_block_size\n\t"
2263#ifdef CONFIG_NIOS2
2264 "__memory_pool_quad_block_size = %0\n\t"
2265#else
2266 "__memory_pool_quad_block_size = %c0\n\t"
2267#endif
2268 :
2269 : "n"(sizeof(struct k_mem_pool_quad_block)));
2270}
2271
2272/**
2273 * @endcond
2274 * End of assembler macros that Doxygen has to skip
2275 */
2276
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002277/**
2278 * @brief Define a memory pool
2279 *
2280 * This declares and initializes a memory pool whose buffer is aligned to
2281 * a @a align -byte boundary. The new memory pool can be passed to the
2282 * kernel's memory pool functions.
2283 *
2284 * Note that for each of the minimum sized blocks to be aligned to @a align
2285 * bytes, then @a min_size must be a multiple of @a align.
2286 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 * If the pool is to be accessed outside the module where it is defined, it
2288 * can be declared via
2289 *
2290 * extern struct k_mem_pool @a name;
2291 *
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002292 * @param name Name of the memory pool
2293 * @param min_size Minimum block size in the pool
2294 * @param max_size Maximum block size in the pool
2295 * @param n_max Number of maximum sized blocks in the pool
2296 * @param align Alignment of the memory pool's buffer
2297 */
2298#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002299 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2300 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002301 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002302 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2303 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2304 extern struct k_mem_pool name
2305
Peter Mitsis937042c2016-10-13 13:18:26 -04002306/**
2307 * @brief Allocate memory from a memory pool
2308 *
2309 * @param pool Pointer to the memory pool object
2310 * @param block Pointer to the allocated memory's block descriptor
2311 * @param size Minimum number of bytes to allocate
2312 * @param timeout Maximum time (milliseconds) to wait for operation to
2313 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
2314 * to wait as long as necessary.
2315 *
2316 * @return 0 on success, -ENOMEM on failure
2317 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002318extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002319 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002320
2321/**
2322 * @brief Return previously allocated memory to its memory pool
2323 *
2324 * @param block Pointer to allocated memory's block descriptor
2325 *
2326 * @return N/A
2327 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002329
2330/**
2331 * @brief Defragment the specified memory pool
2332 *
2333 * @param pool Pointer to the memory pool object
2334 *
2335 * @return N/A
2336 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002337extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002338
2339/**
Allan Stephens480a1312016-10-13 15:44:48 -05002340 * @brief Allocate memory from heap
Peter Mitsis937042c2016-10-13 13:18:26 -04002341 *
Allan Stephens480a1312016-10-13 15:44:48 -05002342 * This routine provides traditional malloc() semantics. The memory is
2343 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002344 *
2345 * @param size Size of memory requested by the caller (in bytes)
2346 *
2347 * @return Address of the allocated memory on success; otherwise NULL
2348 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002349extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002350
2351/**
Allan Stephens480a1312016-10-13 15:44:48 -05002352 * @brief Free memory allocated from heap
2353 *
2354 * This routine provides traditional free() semantics. The memory being
2355 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002356 *
2357 * @param ptr Pointer to previously allocated memory
2358 *
2359 * @return N/A
2360 */
2361extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002362
2363/*
2364 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2365 * hook into the device subsystem, which itself uses nanokernel semaphores,
2366 * and thus currently requires the definition of nano_sem.
2367 */
2368#include <legacy.h>
2369#include <arch/cpu.h>
2370
2371/*
2372 * private APIs that are utilized by one or more public APIs
2373 */
2374
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002377extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378
2379#ifdef __cplusplus
2380}
2381#endif
2382
Andrew Boiee004dec2016-11-07 09:01:19 -08002383#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
2384/*
2385 * Define new and delete operators.
2386 * At this moment, the operators do nothing since objects are supposed
2387 * to be statically allocated.
2388 */
2389inline void operator delete(void *ptr)
2390{
2391 (void)ptr;
2392}
2393
2394inline void operator delete[](void *ptr)
2395{
2396 (void)ptr;
2397}
2398
2399inline void *operator new(size_t size)
2400{
2401 (void)size;
2402 return NULL;
2403}
2404
2405inline void *operator new[](size_t size)
2406{
2407 (void)size;
2408 return NULL;
2409}
2410
2411/* Placement versions of operator new and delete */
2412inline void operator delete(void *ptr1, void *ptr2)
2413{
2414 (void)ptr1;
2415 (void)ptr2;
2416}
2417
2418inline void operator delete[](void *ptr1, void *ptr2)
2419{
2420 (void)ptr1;
2421 (void)ptr2;
2422}
2423
2424inline void *operator new(size_t size, void *ptr)
2425{
2426 (void)size;
2427 return ptr;
2428}
2429
2430inline void *operator new[](size_t size, void *ptr)
2431{
2432 (void)size;
2433 return ptr;
2434}
2435
2436#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
2437
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438#endif /* _kernel__h_ */