blob: c42cff9dd9e95af511b3278fb3a15a9c71de188e [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
67#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
68#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
69
70typedef sys_dlist_t _wait_q_t;
71
72#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
73#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
74#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
75#else
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
77#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
78#endif
79
80#define k_thread tcs
81struct tcs;
82struct k_mutex;
83struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040084struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040091struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040092struct k_mem_pool;
93struct k_timer;
94
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040095typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
97/* threads/scheduler/execution contexts */
98
99enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
106extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
107 void (*entry)(void *, void *, void*),
108 void *p1, void *p2, void *p3,
109 int32_t prio, uint32_t options, int32_t delay);
110
111extern void k_sleep(int32_t duration);
112extern void k_busy_wait(uint32_t usec_to_wait);
113extern void k_yield(void);
114extern void k_wakeup(k_tid_t thread);
115extern k_tid_t k_current_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116extern int k_thread_cancel(k_tid_t thread);
117
118extern void k_thread_abort(k_tid_t thread);
119
120#define K_THREAD_GROUP_EXE 0x1
121#define K_THREAD_GROUP_SYS 0x2
122#define K_THREAD_GROUP_FPU 0x4
123
124/* XXX - doesn't work because CONFIG_ARCH is a string */
125#if 0
126/* arch-specific groups */
127#if CONFIG_ARCH == "x86"
128#define K_THREAD_GROUP_SSE 0x4
129#endif
130#endif
131
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400132#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400133#define _THREAD_TIMEOUT_INIT(obj) \
134 (obj).nano_timeout = { \
135 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400136 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400137 .wait_q = NULL, \
138 .delta_ticks_from_prev = -1, \
139 },
140#else
141#define _THREAD_TIMEOUT_INIT(obj)
142#endif
143
144#ifdef CONFIG_ERRNO
145#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
146#else
147#define _THREAD_ERRNO_INIT(obj)
148#endif
149
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400150struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151 union {
152 char *init_stack;
153 struct k_thread *thread;
154 };
155 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500156 void (*init_entry)(void *, void *, void *);
157 void *init_p1;
158 void *init_p2;
159 void *init_p3;
160 int init_prio;
161 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400162 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500163 void (*init_abort)(void);
164 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400165};
166
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400167#define _THREAD_INITIALIZER(stack, stack_size, \
168 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500169 prio, options, delay, abort, groups) \
170 { \
171 .init_stack = (stack), \
172 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400173 .init_entry = (void (*)(void *, void *, void *))entry, \
174 .init_p1 = (void *)p1, \
175 .init_p2 = (void *)p2, \
176 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500177 .init_prio = (prio), \
178 .init_options = (options), \
179 .init_delay = (delay), \
180 .init_abort = (abort), \
181 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400182 }
183
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400184/**
Allan Stephens6cfe1322016-10-26 10:16:51 -0500185 * @brief Define a static thread.
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400186 *
187 * @internal It has been observed that the x86 compiler by default aligns
188 * these _static_thread_data structures to 32-byte boundaries, thereby
189 * wasting space. To work around this, force a 4-byte alignment.
190 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500191#define K_THREAD_DEFINE(name, stack_size, \
192 entry, p1, p2, p3, \
193 prio, options, delay) \
194 char __noinit __stack _k_thread_obj_##name[stack_size]; \
195 struct _static_thread_data _k_thread_data_##name __aligned(4) \
196 __in_section(_k_task_list, private, task) = \
197 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
198 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500199 NULL, 0); \
200 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400201
Allan Stephens399d0ad2016-10-07 13:41:34 -0500202extern int k_thread_priority_get(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400203extern void k_thread_priority_set(k_tid_t thread, int prio);
204
Benjamin Walsh71d52282016-09-29 10:49:48 -0400205extern void k_thread_suspend(k_tid_t thread);
206extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400207
208extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400209
210extern int k_am_in_isr(void);
211
212extern void k_thread_custom_data_set(void *value);
213extern void *k_thread_custom_data_get(void);
214
215/**
216 * kernel timing
217 */
218
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400219#include <sys_clock.h>
220
221/* private internal time manipulation (users should never play with ticks) */
222
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500223/* added tick needed to account for tick in progress */
224#define _TICK_ALIGN 1
225
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400226static int64_t __ticks_to_ms(int64_t ticks)
227{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400228#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400229 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400230#else
231 __ASSERT(ticks == 0, "");
232 return 0;
233#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400234}
235
236
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400237/* timeouts */
238
239struct _timeout;
240typedef void (*_timeout_func_t)(struct _timeout *t);
241
242struct _timeout {
243 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400244 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245 sys_dlist_t *wait_q;
246 int32_t delta_ticks_from_prev;
247 _timeout_func_t func;
248};
249
Allan Stephens45bfa372016-10-12 12:39:42 -0500250
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400251/* timers */
252
253struct k_timer {
254 /*
255 * _timeout structure must be first here if we want to use
256 * dynamic timer allocation. timeout.node is used in the double-linked
257 * list of free timers
258 */
259 struct _timeout timeout;
260
Allan Stephens45bfa372016-10-12 12:39:42 -0500261 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400262 _wait_q_t wait_q;
263
264 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500265 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400266
267 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500268 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400269
270 /* timer period */
271 int32_t period;
272
Allan Stephens45bfa372016-10-12 12:39:42 -0500273 /* timer status */
274 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400275
Allan Stephens45bfa372016-10-12 12:39:42 -0500276 /* used to support legacy timer APIs */
277 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400278
279 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
280};
281
282#define K_TIMER_INITIALIZER(obj) \
283 { \
284 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
285 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
286 }
287
288#define K_TIMER_DEFINE(name) \
289 struct k_timer name = K_TIMER_INITIALIZER(name)
290
Allan Stephens45bfa372016-10-12 12:39:42 -0500291/**
292 * @brief Initialize a timer.
293 *
294 * This routine must be called before the timer is used.
295 *
296 * @param timer Address of timer.
297 * @param expiry_fn Function to invoke each time timer expires.
298 * @param stop_fn Function to invoke if timer is stopped while running.
299 *
300 * @return N/A
301 */
302extern void k_timer_init(struct k_timer *timer,
303 void (*expiry_fn)(struct k_timer *),
304 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700305
Allan Stephens45bfa372016-10-12 12:39:42 -0500306/**
307 * @brief Start a timer.
308 *
309 * This routine starts a timer, and resets its status to zero. The timer
310 * begins counting down using the specified duration and period values.
311 *
312 * Attempting to start a timer that is already running is permitted.
313 * The timer's status is reset to zero and the timer begins counting down
314 * using the new duration and period values.
315 *
316 * @param timer Address of timer.
317 * @param duration Initial timer duration (in milliseconds).
318 * @param period Timer period (in milliseconds).
319 *
320 * @return N/A
321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400322extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500323 int32_t duration, int32_t period);
324
325/**
326 * @brief Stop a timer.
327 *
328 * This routine stops a running timer prematurely. The timer's stop function,
329 * if one exists, is invoked by the caller.
330 *
331 * Attempting to stop a timer that is not running is permitted, but has no
332 * effect on the timer since it is already stopped.
333 *
334 * @param timer Address of timer.
335 *
336 * @return N/A
337 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400338extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500339
340/**
341 * @brief Read timer status.
342 *
343 * This routine reads the timer's status, which indicates the number of times
344 * it has expired since its status was last read.
345 *
346 * Calling this routine resets the timer's status to zero.
347 *
348 * @param timer Address of timer.
349 *
350 * @return Timer status.
351 */
352extern uint32_t k_timer_status_get(struct k_timer *timer);
353
354/**
355 * @brief Synchronize thread to timer expiration.
356 *
357 * This routine blocks the calling thread until the timer's status is non-zero
358 * (indicating that it has expired at least once since it was last examined)
359 * or the timer is stopped. If the timer status is already non-zero,
360 * or the timer is already stopped, the caller continues without waiting.
361 *
362 * Calling this routine resets the timer's status to zero.
363 *
364 * This routine must not be used by interrupt handlers, since they are not
365 * allowed to block.
366 *
367 * @param timer Address of timer.
368 *
369 * @return Timer status.
370 */
371extern uint32_t k_timer_status_sync(struct k_timer *timer);
372
373/**
374 * @brief Get timer remaining before next timer expiration.
375 *
376 * This routine computes the (approximate) time remaining before a running
377 * timer next expires. If the timer is not running, it returns zero.
378 *
379 * @param timer Address of timer.
380 *
381 * @return Remaining time (in milliseconds).
382 */
383
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400384extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400385
386
Allan Stephens45bfa372016-10-12 12:39:42 -0500387/* kernel clocks */
388
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400389/**
390 * @brief Get the time elapsed since the system booted (uptime)
391 *
392 * @return The current uptime of the system in ms
393 */
394
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400395extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400396
397/**
398 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
399 *
400 * This function is potentially less onerous in both the time it takes to
401 * execute, the interrupt latency it introduces and the amount of 64-bit math
402 * it requires than k_uptime_get(), but it only provides an uptime value of
403 * 32-bits. The user must handle possible rollovers/spillovers.
404 *
405 * At a rate of increment of 1000 per second, it rolls over approximately every
406 * 50 days.
407 *
408 * @return The current uptime of the system in ms
409 */
410
411extern uint32_t k_uptime_get_32(void);
412
413/**
414 * @brief Get the difference between a reference time and the current uptime
415 *
416 * @param reftime A pointer to a reference time. It is updated with the current
417 * uptime upon return.
418 *
419 * @return The delta between the reference time and the current uptime.
420 */
421
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400422extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400423
424/**
425 * @brief Get the difference between a reference time and the current uptime
426 *
427 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
428 * k_uptime_get_32().
429 *
430 * @param reftime A pointer to a reference time. It is updated with the current
431 * uptime upon return.
432 *
433 * @return The delta between the reference time and the current uptime.
434 */
435
436extern uint32_t k_uptime_delta_32(int64_t *reftime);
437
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400438extern uint32_t k_cycle_get_32(void);
439
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400440/**
441 * data transfers (basic)
442 */
443
444/* fifos */
445
446struct k_fifo {
447 _wait_q_t wait_q;
448 sys_slist_t data_q;
449
450 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
451};
452
453extern void k_fifo_init(struct k_fifo *fifo);
454extern void k_fifo_put(struct k_fifo *fifo, void *data);
455extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
456extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
457extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
458
459#define K_FIFO_INITIALIZER(obj) \
460 { \
461 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400462 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400463 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
464 }
465
466#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400467 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400468
469/* lifos */
470
471struct k_lifo {
472 _wait_q_t wait_q;
473 void *list;
474
475 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
476};
477
478extern void k_lifo_init(struct k_lifo *lifo);
479extern void k_lifo_put(struct k_lifo *lifo, void *data);
480extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
481
482#define K_LIFO_INITIALIZER(obj) \
483 { \
484 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
485 .list = NULL, \
486 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
487 }
488
489#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400490 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400491
492/* stacks */
493
494struct k_stack {
495 _wait_q_t wait_q;
496 uint32_t *base, *next, *top;
497
498 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
499};
500
Allan Stephens018cd9a2016-10-07 15:13:24 -0500501extern void k_stack_init(struct k_stack *stack,
502 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503extern void k_stack_push(struct k_stack *stack, uint32_t data);
504extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
505
Peter Mitsis602e6a82016-10-17 11:48:43 -0400506#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400507 { \
508 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
509 .base = stack_buffer, \
510 .next = stack_buffer, \
511 .top = stack_buffer + stack_num_entries, \
512 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
513 }
514
Peter Mitsis602e6a82016-10-17 11:48:43 -0400515#define K_STACK_DEFINE(name, stack_num_entries) \
516 uint32_t __noinit \
517 _k_stack_buf_##name[stack_num_entries]; \
518 struct k_stack name = \
519 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
520 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400521
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400522/**
523 * workqueues
524 */
525
526struct k_work;
527
528typedef void (*k_work_handler_t)(struct k_work *);
529
530/**
531 * A workqueue is a fiber that executes @ref k_work items that are
532 * queued to it. This is useful for drivers which need to schedule
533 * execution of code which might sleep from ISR context. The actual
534 * fiber identifier is not stored in the structure in order to save
535 * space.
536 */
537struct k_work_q {
538 struct k_fifo fifo;
539};
540
541/**
542 * @brief Work flags.
543 */
544enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300545 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400546};
547
548/**
549 * @brief An item which can be scheduled on a @ref k_work_q.
550 */
551struct k_work {
552 void *_reserved; /* Used by k_fifo implementation. */
553 k_work_handler_t handler;
554 atomic_t flags[1];
555};
556
557/**
558 * @brief Statically initialize work item
559 */
560#define K_WORK_INITIALIZER(work_handler) \
561 { \
562 ._reserved = NULL, \
563 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300564 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400565 }
566
567/**
568 * @brief Dynamically initialize work item
569 */
570static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
571{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300572 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400573 work->handler = handler;
574}
575
576/**
577 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300578 *
579 * This procedure schedules a work item to be processed.
580 * In the case where the work item has already been submitted and is pending
581 * execution, calling this function will result in a no-op. In this case, the
582 * work item must not be modified externally (e.g. by the caller of this
583 * function), since that could cause the work item to be processed in a
584 * corrupted state.
585 *
586 * @param work_q to schedule the work item
587 * @param work work item
588 *
589 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400590 */
591static inline void k_work_submit_to_queue(struct k_work_q *work_q,
592 struct k_work *work)
593{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300594 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595 k_fifo_put(&work_q->fifo, work);
596 }
597}
598
599/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300600 * @brief Check if work item is pending.
601 */
602static inline int k_work_pending(struct k_work *work)
603{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300604 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300605}
606
607/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400608 * @brief Start a new workqueue. This routine can be called from either
609 * fiber or task context.
610 */
Allan Stephens904cf972016-10-07 13:59:23 -0500611extern void k_work_q_start(struct k_work_q *work_q, char *stack,
612 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400613
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400614#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400615
616 /*
617 * @brief An item which can be scheduled on a @ref k_work_q with a
618 * delay.
619 */
620struct k_delayed_work {
621 struct k_work work;
622 struct _timeout timeout;
623 struct k_work_q *work_q;
624};
625
626/**
627 * @brief Initialize delayed work
628 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400629extern void k_delayed_work_init(struct k_delayed_work *work,
630 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400631
632/**
633 * @brief Submit a delayed work item to a workqueue.
634 *
635 * This procedure schedules a work item to be processed after a delay.
636 * Once the delay has passed, the work item is submitted to the work queue:
637 * at this point, it is no longer possible to cancel it. Once the work item's
638 * handler is about to be executed, the work is considered complete and can be
639 * resubmitted.
640 *
641 * Care must be taken if the handler blocks or yield as there is no implicit
642 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
643 * it should be explicitly done between the submitter and the handler.
644 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500645 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400646 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500647 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400648 *
649 * @return 0 in case of success or negative value in case of error.
650 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400651extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
652 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500653 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400654
655/**
656 * @brief Cancel a delayed work item
657 *
658 * This procedure cancels a scheduled work item. If the work has been completed
659 * or is idle, this will do nothing. The only case where this can fail is when
660 * the work has been submitted to the work queue, but the handler has not run
661 * yet.
662 *
663 * @param work Delayed work item to be canceled
664 *
665 * @return 0 in case of success or negative value in case of error.
666 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400667extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400669#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400670
671#if defined(CONFIG_SYSTEM_WORKQUEUE)
672
673extern struct k_work_q k_sys_work_q;
674
675/*
676 * @brief Submit a work item to the system workqueue.
677 *
678 * @ref k_work_submit_to_queue
679 *
680 * When using the system workqueue it is not recommended to block or yield
681 * on the handler since its fiber is shared system wide it may cause
682 * unexpected behavior.
683 */
684static inline void k_work_submit(struct k_work *work)
685{
686 k_work_submit_to_queue(&k_sys_work_q, work);
687}
688
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400689#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400690/*
691 * @brief Submit a delayed work item to the system workqueue.
692 *
693 * @ref k_delayed_work_submit_to_queue
694 *
695 * When using the system workqueue it is not recommended to block or yield
696 * on the handler since its fiber is shared system wide it may cause
697 * unexpected behavior.
698 */
699static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500700 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400701{
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500702 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400703}
704
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400705#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400706#endif /* CONFIG_SYSTEM_WORKQUEUE */
707
708/**
709 * synchronization
710 */
711
712/* mutexes */
713
714struct k_mutex {
715 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400716 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400717 uint32_t lock_count;
718 int owner_orig_prio;
719#ifdef CONFIG_OBJECT_MONITOR
720 int num_lock_state_changes;
721 int num_conflicts;
722#endif
723
724 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
725};
726
727#ifdef CONFIG_OBJECT_MONITOR
728#define _MUTEX_INIT_OBJECT_MONITOR \
729 .num_lock_state_changes = 0, .num_conflicts = 0,
730#else
731#define _MUTEX_INIT_OBJECT_MONITOR
732#endif
733
734#define K_MUTEX_INITIALIZER(obj) \
735 { \
736 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
737 .owner = NULL, \
738 .lock_count = 0, \
739 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
740 _MUTEX_INIT_OBJECT_MONITOR \
741 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
742 }
743
744#define K_MUTEX_DEFINE(name) \
745 struct k_mutex name = K_MUTEX_INITIALIZER(name)
746
747extern void k_mutex_init(struct k_mutex *mutex);
748extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
749extern void k_mutex_unlock(struct k_mutex *mutex);
750
751/* semaphores */
752
753struct k_sem {
754 _wait_q_t wait_q;
755 unsigned int count;
756 unsigned int limit;
757
758 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
759};
760
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400761/**
762 * @brief Initialize a semaphore object.
763 *
764 * An initial count and a count limit can be specified. The count will never go
765 * over the count limit if the semaphore is given multiple times without being
766 * taken.
767 *
768 * Cannot be called from ISR.
769 *
770 * @param sem Pointer to a semaphore object.
771 * @param initial_count Initial count.
772 * @param limit Highest value the count can take during operation.
773 *
774 * @return N/A
775 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400776extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
777 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400778
779/**
780 * @brief Take a semaphore, possibly pending if not available.
781 *
782 * The current execution context tries to obtain the semaphore. If the
783 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
784 * the context will pend.
785 *
786 * @param sem Pointer to a semaphore object.
787 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
788 * or one of the special values K_NO_WAIT and K_FOREVER.
789 *
790 * @warning If it is called from the context of an ISR, then the only legal
791 * value for @a timeout is K_NO_WAIT.
792 *
793 * @retval 0 When semaphore is obtained successfully.
794 * @retval -EAGAIN When timeout expires.
795 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
796 *
797 * @sa K_NO_WAIT, K_FOREVER
798 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400799extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400800
801/**
802 * @brief Give a semaphore.
803 *
804 * Increase the semaphore's internal count by 1, up to its limit, if no thread
805 * is waiting on the semaphore; otherwise, wake up the first thread in the
806 * semaphore's waiting queue.
807 *
808 * If the latter case, and if the current context is preemptible, the thread
809 * that is taken off the wait queue will be scheduled in and will preempt the
810 * current thread.
811 *
812 * @param sem Pointer to a semaphore object.
813 *
814 * @return N/A
815 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400816extern void k_sem_give(struct k_sem *sem);
817
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400818/**
819 * @brief Reset a semaphore's count to zero.
820 *
821 * The only effect is that the count is set to zero. There is no other
822 * side-effect to calling this function.
823 *
824 * @param sem Pointer to a semaphore object.
825 *
826 * @return N/A
827 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400828static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400829{
830 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400831}
832
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400833/**
834 * @brief Get a semaphore's count.
835 *
836 * Note there is no guarantee the count has not changed by the time this
837 * function returns.
838 *
839 * @param sem Pointer to a semaphore object.
840 *
841 * @return The current semaphore count.
842 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200843static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400844{
845 return sem->count;
846}
847
Peter Mitsis45403672016-09-09 14:24:06 -0400848#ifdef CONFIG_SEMAPHORE_GROUPS
849/**
850 * @brief Take the first available semaphore
851 *
852 * Given a list of semaphore pointers, this routine will attempt to take one
853 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
854 * semaphore is identified by @a sem (set to NULL on error).
855 *
856 * Be aware that the more semaphores specified in the group, the more stack
857 * space is required by the waiting thread.
858 *
859 * @param sem_array Array of semaphore pointers terminated by a K_END entry
860 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400861 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
862 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -0400863 *
864 * @retval 0 A semaphore was successfully taken
865 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
866 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400867 *
868 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -0400869 */
870
871extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
872 int32_t timeout);
873
874/**
875 * @brief Give all the semaphores in the group
876 *
877 * This routine will give each semaphore in the array of semaphore pointers.
878 *
879 * @param sem_array Array of semaphore pointers terminated by a K_END entry
880 *
881 * @return N/A
882 */
883extern void k_sem_group_give(struct k_sem *sem_array[]);
884
885/**
886 * @brief Reset the count to zero on each semaphore in the array
887 *
888 * This routine resets the count of each semaphore in the group to zero.
889 * Note that it does NOT have any impact on any thread that might have
890 * been previously pending on any of the semaphores.
891 *
892 * @param sem_array Array of semaphore pointers terminated by a K_END entry
893 *
894 * @return N/A
895 */
896extern void k_sem_group_reset(struct k_sem *sem_array[]);
897#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400898
899#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
900 { \
901 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
902 .count = initial_count, \
903 .limit = count_limit, \
904 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
905 }
906
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400907/**
908 * @def K_SEM_DEFINE
909 *
910 * @brief Statically define and initialize a global semaphore.
911 *
912 * Create a global semaphore named @name. It is initialized as if k_sem_init()
913 * was called on it. If the semaphore is to be accessed outside the module
914 * where it is defined, it can be declared via
915 *
916 * extern struct k_sem @name;
917 *
918 * @param name Name of the semaphore variable.
919 * @param initial_count Initial count.
920 * @param count_limit Highest value the count can take during operation.
921 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400922#define K_SEM_DEFINE(name, initial_count, count_limit) \
923 struct k_sem name = \
924 K_SEM_INITIALIZER(name, initial_count, count_limit)
925
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400926/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400927
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400928#define K_ALERT_DEFAULT NULL
929#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400931typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400932
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400933struct k_alert {
934 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400935 atomic_t send_count;
936 struct k_work work_item;
937 struct k_sem sem;
938
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400939 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400940};
941
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400942extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400944#define K_ALERT_INITIALIZER(obj, alert_handler) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400946 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400947 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400948 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
950 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
951 }
952
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400953#define K_ALERT_DEFINE(name, alert_handler) \
954 struct k_alert name \
955 __in_section(_k_event_list, alert, name) = \
956 K_ALERT_INITIALIZER(name, alert_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400957
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400958extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler);
959extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
960extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400961
962/**
963 * data transfers (complex)
964 */
965
966/* message queues */
967
968struct k_msgq {
969 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -0400970 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971 uint32_t max_msgs;
972 char *buffer_start;
973 char *buffer_end;
974 char *read_ptr;
975 char *write_ptr;
976 uint32_t used_msgs;
977
978 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
979};
980
Peter Mitsis1da807e2016-10-06 11:36:59 -0400981#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400982 { \
983 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -0400984 .max_msgs = q_max_msgs, \
985 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400986 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -0400987 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400988 .read_ptr = q_buffer, \
989 .write_ptr = q_buffer, \
990 .used_msgs = 0, \
991 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
992 }
993
Peter Mitsis1da807e2016-10-06 11:36:59 -0400994/**
995 * @brief Define a message queue
996 *
997 * This declares and initializes a message queue whose buffer is aligned to
998 * a @a q_align -byte boundary. The new message queue can be passed to the
999 * kernel's message queue functions.
1000 *
1001 * Note that for each of the mesages in the message queue to be aligned to
1002 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1003 *
1004 * @param q_name Name of the message queue
1005 * @param q_msg_size The size in bytes of each message
1006 * @param q_max_msgs Maximum number of messages the queue can hold
1007 * @param q_align Alignment of the message queue's buffer (power of 2)
1008 */
1009#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1010 static char __noinit __aligned(q_align) \
1011 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
1012 struct k_msgq q_name = \
1013 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1014 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001015
Peter Mitsisd7a37502016-10-13 11:37:40 -04001016/**
1017 * @brief Initialize a message queue.
1018 *
1019 * @param q Pointer to the message queue object.
1020 * @param buffer Pointer to memory area that holds queued messages.
1021 * @param msg_size Message size, in bytes.
1022 * @param max_msgs Maximum number of messages that can be queued.
1023 *
1024 * @return N/A
1025 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001026extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001027 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001028
1029/**
1030 * @brief Add a message to a message queue.
1031 *
1032 * This routine adds an item to the message queue. When the message queue is
1033 * full, the routine will wait either for space to become available, or until
1034 * the specified time limit is reached.
1035 *
1036 * @param q Pointer to the message queue object.
1037 * @param data Pointer to message data area.
1038 * @param timeout Number of milliseconds to wait until space becomes available
1039 * to add the message into the message queue, or one of the
1040 * special values K_NO_WAIT and K_FOREVER.
1041 *
1042 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1043 * -EAGAIN if timed out
1044 *
1045 * @sa K_NO_WAIT, K_FOREVER
1046 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001048
1049/**
1050 * @brief Obtain a message from a message queue.
1051 *
1052 * This routine fetches the oldest item from the message queue. When the message
1053 * queue is found empty, the routine will wait either until an item is added to
1054 * the message queue or until the specified time limit is reached.
1055 *
1056 * @param q Pointer to the message queue object.
1057 * @param data Pointer to message data area.
1058 * @param timeout Number of milliseconds to wait to obtain message, or one of
1059 * the special values K_NO_WAIT and K_FOREVER.
1060 *
1061 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1062 *
1063 * @sa K_NO_WAIT, K_FOREVER
1064 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001065extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001066
1067/**
1068 * @brief Purge contents of a message queue.
1069 *
1070 * Discards all messages currently in the message queue, and cancels
1071 * any "add message" operations initiated by waiting threads.
1072 *
1073 * @param q Pointer to the message queue object.
1074 *
1075 * @return N/A
1076 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077extern void k_msgq_purge(struct k_msgq *q);
1078
Peter Mitsis67be2492016-10-07 11:44:34 -04001079/**
1080 * @brief Get the number of unused messages
1081 *
1082 * @param q Message queue to query
1083 *
1084 * @return Number of unused messages
1085 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001086static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001087{
1088 return q->max_msgs - q->used_msgs;
1089}
1090
Peter Mitsisd7a37502016-10-13 11:37:40 -04001091/**
1092 * @brief Get the number of used messages
1093 *
1094 * @param q Message queue to query
1095 *
1096 * @return Number of used messages
1097 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001098static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099{
1100 return q->used_msgs;
1101}
1102
1103struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001104 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105 void *addr_in_pool;
1106 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001107 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001108};
1109
1110/* mailboxes */
1111
1112struct k_mbox_msg {
1113 /** internal use only - needed for legacy API support */
1114 uint32_t _mailbox;
1115 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001116 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117 /** application-defined information value */
1118 uint32_t info;
1119 /** sender's message data buffer */
1120 void *tx_data;
1121 /** internal use only - needed for legacy API support */
1122 void *_rx_data;
1123 /** message data block descriptor */
1124 struct k_mem_block tx_block;
1125 /** source thread id */
1126 k_tid_t rx_source_thread;
1127 /** target thread id */
1128 k_tid_t tx_target_thread;
1129 /** internal use only - thread waiting on send (may be a dummy) */
1130 k_tid_t _syncing_thread;
1131#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1132 /** internal use only - semaphore used during asynchronous send */
1133 struct k_sem *_async_sem;
1134#endif
1135};
1136
1137struct k_mbox {
1138 _wait_q_t tx_msg_queue;
1139 _wait_q_t rx_msg_queue;
1140
1141 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1142};
1143
1144#define K_MBOX_INITIALIZER(obj) \
1145 { \
1146 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1147 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1148 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1149 }
1150
Peter Mitsis12092702016-10-14 12:57:23 -04001151/**
1152 * @brief Define a mailbox
1153 *
1154 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001155 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001156 *
1157 * @param name Name of the mailbox
1158 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159#define K_MBOX_DEFINE(name) \
1160 struct k_mbox name = \
1161 K_MBOX_INITIALIZER(name) \
1162
Peter Mitsis12092702016-10-14 12:57:23 -04001163/**
1164 * @brief Initialize a mailbox.
1165 *
1166 * @param mbox Pointer to the mailbox object
1167 *
1168 * @return N/A
1169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170extern void k_mbox_init(struct k_mbox *mbox);
1171
Peter Mitsis12092702016-10-14 12:57:23 -04001172/**
1173 * @brief Send a mailbox message in a synchronous manner.
1174 *
1175 * Sends a message to a mailbox and waits for a receiver to process it.
1176 * The message data may be in a buffer, in a memory pool block, or non-existent
1177 * (i.e. empty message).
1178 *
1179 * @param mbox Pointer to the mailbox object.
1180 * @param tx_msg Pointer to transmit message descriptor.
1181 * @param timeout Maximum time (milliseconds) to wait for the message to be
1182 * received (although not necessarily completely processed).
1183 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1184 * as necessary.
1185 *
1186 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1187 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001188extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001190
1191#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1192/**
1193 * @brief Send a mailbox message in an asynchronous manner.
1194 *
1195 * Sends a message to a mailbox without waiting for a receiver to process it.
1196 * The message data may be in a buffer, in a memory pool block, or non-existent
1197 * (i.e. an empty message). Optionally, the specified semaphore will be given
1198 * by the mailbox when the message has been both received and disposed of
1199 * by the receiver.
1200 *
1201 * @param mbox Pointer to the mailbox object.
1202 * @param tx_msg Pointer to transmit message descriptor.
1203 * @param sem Semaphore identifier, or NULL if none specified.
1204 *
1205 * @return N/A
1206 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001207extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001208 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001209#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001210
Peter Mitsis12092702016-10-14 12:57:23 -04001211/**
1212 * @brief Receive a mailbox message.
1213 *
1214 * Receives a message from a mailbox, then optionally retrieves its data
1215 * and disposes of the message.
1216 *
1217 * @param mbox Pointer to the mailbox object.
1218 * @param rx_msg Pointer to receive message descriptor.
1219 * @param buffer Pointer to buffer to receive data.
1220 * (Use NULL to defer data retrieval and message disposal until later.)
1221 * @param timeout Maximum time (milliseconds) to wait for a message.
1222 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1223 * necessary.
1224 *
1225 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1226 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001227extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001228 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001229
1230/**
1231 * @brief Retrieve mailbox message data into a buffer.
1232 *
1233 * Completes the processing of a received message by retrieving its data
1234 * into a buffer, then disposing of the message.
1235 *
1236 * Alternatively, this routine can be used to dispose of a received message
1237 * without retrieving its data.
1238 *
1239 * @param rx_msg Pointer to receive message descriptor.
1240 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1241 *
1242 * @return N/A
1243 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001244extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001245
1246/**
1247 * @brief Retrieve mailbox message data into a memory pool block.
1248 *
1249 * Completes the processing of a received message by retrieving its data
1250 * into a memory pool block, then disposing of the message. The memory pool
1251 * block that results from successful retrieval must be returned to the pool
1252 * once the data has been processed, even in cases where zero bytes of data
1253 * are retrieved.
1254 *
1255 * Alternatively, this routine can be used to dispose of a received message
1256 * without retrieving its data. In this case there is no need to return a
1257 * memory pool block to the pool.
1258 *
1259 * This routine allocates a new memory pool block for the data only if the
1260 * data is not already in one. If a new block cannot be allocated, the routine
1261 * returns a failure code and the received message is left unchanged. This
1262 * permits the caller to reattempt data retrieval at a later time or to dispose
1263 * of the received message without retrieving its data.
1264 *
1265 * @param rx_msg Pointer to receive message descriptor.
1266 * @param pool Memory pool identifier. (Use NULL to discard data.)
1267 * @param block Pointer to area to hold memory pool block info.
1268 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1269 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1270 * necessary.
1271 *
1272 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1273 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001274extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001275 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001276 struct k_mem_block *block, int32_t timeout);
1277
1278/* pipes */
1279
1280struct k_pipe {
1281 unsigned char *buffer; /* Pipe buffer: may be NULL */
1282 size_t size; /* Buffer size */
1283 size_t bytes_used; /* # bytes used in buffer */
1284 size_t read_index; /* Where in buffer to read from */
1285 size_t write_index; /* Where in buffer to write */
1286
1287 struct {
1288 _wait_q_t readers; /* Reader wait queue */
1289 _wait_q_t writers; /* Writer wait queue */
1290 } wait_q;
1291
1292 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1293};
1294
Peter Mitsise5d9c582016-10-14 14:44:57 -04001295#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296 { \
1297 .buffer = pipe_buffer, \
1298 .size = pipe_buffer_size, \
1299 .bytes_used = 0, \
1300 .read_index = 0, \
1301 .write_index = 0, \
1302 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1303 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1304 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1305 }
1306
Peter Mitsise5d9c582016-10-14 14:44:57 -04001307#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1308 static unsigned char __noinit __aligned(pipe_align) \
1309 _k_pipe_buf_##name[pipe_buffer_size]; \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001310 struct k_pipe name = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001311 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001313/**
1314 * @brief Runtime initialization of a pipe
1315 *
1316 * @param pipe Pointer to pipe to initialize
1317 * @param buffer Pointer to buffer to use for pipe's ring buffer
1318 * @param size Size of the pipe's ring buffer
1319 *
1320 * @return N/A
1321 */
1322extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1323 size_t size);
1324
1325/**
1326 * @brief Put a message into the specified pipe
1327 *
1328 * This routine synchronously adds a message into the pipe specified by
1329 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001330 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1331 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001332 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1333 *
1334 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001335 * @param data Data to put into the pipe
1336 * @param bytes_to_write Desired number of bytes to put into the pipe
1337 * @param bytes_written Number of bytes the pipe accepted
1338 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001339 * @param timeout Maximum number of milliseconds to wait
1340 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001341 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001342 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001343 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001345extern int k_pipe_put(struct k_pipe *pipe, void *data,
1346 size_t bytes_to_write, size_t *bytes_written,
1347 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348
1349/**
1350 * @brief Get a message from the specified pipe
1351 *
1352 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001353 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001354 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001355 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001356 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1357 *
1358 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001359 * @param data Location to place retrieved data
1360 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1361 * @param bytes_read Number of bytes retrieved from the pipe
1362 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001363 * @param timeout Maximum number of milliseconds to wait
1364 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001365 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001366 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001367 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001368 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001369extern int k_pipe_get(struct k_pipe *pipe, void *data,
1370 size_t bytes_to_read, size_t *bytes_read,
1371 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001372
Peter Mitsis2fef0232016-10-14 14:53:44 -04001373#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374/**
1375 * @brief Send a message to the specified pipe
1376 *
1377 * This routine asynchronously sends a message from the pipe specified by
1378 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1379 * free the memory block @a block and give the semaphore @a sem (if specified).
1380 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1381 * at any given time.
1382 *
1383 * @param pipe Pointer to the pipe
1384 * @param block Memory block containing data to send
1385 * @param size Number of data bytes in memory block to send
1386 * @param sem Semaphore to signal upon completion (else NULL)
1387 *
1388 * @retval N/A
1389 */
1390extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1391 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001392#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393
1394/**
1395 * memory management
1396 */
1397
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001398/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001400struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001402 uint32_t num_blocks;
1403 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404 char *buffer;
1405 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001406 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001408 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001409};
1410
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001411#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
1412 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413 { \
1414 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001415 .num_blocks = slab_num_blocks, \
1416 .block_size = slab_block_size, \
1417 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418 .free_list = NULL, \
1419 .num_used = 0, \
1420 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1421 }
1422
Peter Mitsis578f9112016-10-07 13:50:31 -04001423/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001424 * @brief Define a memory slab
Peter Mitsis578f9112016-10-07 13:50:31 -04001425 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001426 * This declares and initializes a memory slab whose buffer is aligned to
1427 * a @a slab_align -byte boundary. The new memory slab can be passed to the
1428 * kernel's memory slab functions.
Peter Mitsis578f9112016-10-07 13:50:31 -04001429 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001430 * Note that for each of the blocks in the memory slab to be aligned to
1431 * @a slab_align bytes, then @a slab_block_size must be a multiple of
1432 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04001433 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001434 * @param name Name of the memory slab
1435 * @param slab_block_size Size of each block in the buffer (in bytes)
1436 * @param slab_num_blocks Number blocks in the buffer
1437 * @param slab_align Alignment of the memory slab's buffer (power of 2)
Peter Mitsis578f9112016-10-07 13:50:31 -04001438 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001439#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
1440 char __noinit __aligned(slab_align) \
1441 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
1442 struct k_mem_slab name \
1443 __in_section(_k_mem_map_ptr, private, mem_slab) = \
1444 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
1445 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001447/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001448 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001449 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001450 * Initializes the memory slab and creates its list of free blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001451 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001452 * @param slab Pointer to the memory slab object
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001453 * @param buffer Pointer to buffer used for the blocks.
1454 * @param block_size Size of each block, in bytes.
1455 * @param num_blocks Number of blocks.
1456 *
1457 * @return N/A
1458 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001459extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04001460 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001461
1462/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001463 * @brief Allocate a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001464 *
1465 * Takes a block from the list of unused blocks.
1466 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001467 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001468 * @param mem Pointer to area to receive block address.
1469 * @param timeout Maximum time (milliseconds) to wait for allocation to
1470 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait
1471 * as long as necessary.
1472 *
1473 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1474 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001475extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
1476 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001477
1478/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001479 * @brief Free a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001480 *
1481 * Gives block to a waiting thread if there is one, otherwise returns it to
1482 * the list of unused blocks.
1483 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001484 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001485 * @param mem Pointer to area to containing block address.
1486 *
1487 * @return N/A
1488 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001489extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001490
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001491/**
1492 * @brief Get the number of used memory blocks
1493 *
1494 * This routine gets the current number of used memory blocks in the
1495 * specified pool. It should be used for stats purposes only as that
1496 * value may potentially be out-of-date by the time it is used.
1497 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001498 * @param slab Memory slab to query
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001499 *
1500 * @return Number of used memory blocks
1501 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001502static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001504 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505}
1506
Peter Mitsisc001aa82016-10-13 13:53:37 -04001507/**
1508 * @brief Get the number of unused memory blocks
1509 *
1510 * This routine gets the current number of unused memory blocks in the
1511 * specified pool. It should be used for stats purposes only as that value
1512 * may potentially be out-of-date by the time it is used.
1513 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001514 * @param slab Memory slab to query
Peter Mitsisc001aa82016-10-13 13:53:37 -04001515 *
1516 * @return Number of unused memory blocks
1517 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001518static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04001519{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001520 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04001521}
1522
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001523/* memory pools */
1524
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001525/*
1526 * Memory pool requires a buffer and two arrays of structures for the
1527 * memory block accounting:
1528 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1529 * status of four blocks of memory.
1530 */
1531struct k_mem_pool_quad_block {
1532 char *mem_blocks; /* pointer to the first of four memory blocks */
1533 uint32_t mem_status; /* four bits. If bit is set, memory block is
1534 allocated */
1535};
1536/*
1537 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1538 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1539 * block size is 4 times less than the previous one and thus requires 4 times
1540 * bigger array of k_mem_pool_quad_block structures to keep track of the
1541 * memory blocks.
1542 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001543
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001544/*
1545 * The array of k_mem_pool_block_set keeps the information of each array of
1546 * k_mem_pool_quad_block structures
1547 */
1548struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04001549 size_t block_size; /* memory block size */
1550 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001551 struct k_mem_pool_quad_block *quad_block;
1552 int count;
1553};
1554
1555/* Memory pool descriptor */
1556struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04001557 size_t max_block_size;
1558 size_t min_block_size;
1559 uint32_t nr_of_maxblocks;
1560 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001561 struct k_mem_pool_block_set *block_set;
1562 char *bufblock;
1563 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001564 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1565};
1566
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001567#ifdef CONFIG_ARM
1568#define _SECTION_TYPE_SIGN "%"
1569#else
1570#define _SECTION_TYPE_SIGN "@"
1571#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001572
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001573/*
1574 * Static memory pool initialization
1575 */
1576/*
1577 * Use .altmacro to be able to recalculate values and pass them as string
1578 * arguments when calling assembler macros resursively
1579 */
1580__asm__(".altmacro\n\t");
1581
1582/*
1583 * Recursively calls a macro
1584 * The followig global symbols need to be initialized:
1585 * __memory_pool_max_block_size - maximal size of the memory block
1586 * __memory_pool_min_block_size - minimal size of the memory block
1587 * Notes:
1588 * Global symbols are used due the fact that assembler macro allows only
1589 * one argument be passed with the % conversion
1590 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1591 * is used instead of / 4.
1592 * n_max argument needs to go first in the invoked macro, as some
1593 * assemblers concatenate \name and %(\n_max * 4) arguments
1594 * if \name goes first
1595 */
1596__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1597 ".ifge __memory_pool_max_block_size >> 2 -"
1598 " __memory_pool_min_block_size\n\t\t"
1599 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1600 "\\macro_name %(\\n_max * 4) \\name\n\t"
1601 ".endif\n\t"
1602 ".endm\n");
1603
1604/*
1605 * Build quad blocks
1606 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1607 * structures and recursively calls itself for the next array, 4 times
1608 * larger.
1609 * The followig global symbols need to be initialized:
1610 * __memory_pool_max_block_size - maximal size of the memory block
1611 * __memory_pool_min_block_size - minimal size of the memory block
1612 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1613 */
1614__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04001615 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001616 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1617 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1618 ".if \\n_max % 4\n\t\t"
1619 ".skip __memory_pool_quad_block_size\n\t"
1620 ".endif\n\t"
1621 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1622 ".endm\n");
1623
1624/*
1625 * Build block sets and initialize them
1626 * Macro initializes the k_mem_pool_block_set structure and
1627 * recursively calls itself for the next one.
1628 * The followig global symbols need to be initialized:
1629 * __memory_pool_max_block_size - maximal size of the memory block
1630 * __memory_pool_min_block_size - minimal size of the memory block
1631 * __memory_pool_block_set_count, the number of the elements in the
1632 * block set array must be set to 0. Macro calculates it's real
1633 * value.
1634 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1635 * structures, _build_quad_blocks must be called prior it.
1636 */
1637__asm__(".macro _build_block_set n_max, name\n\t"
1638 ".int __memory_pool_max_block_size\n\t" /* block_size */
1639 ".if \\n_max % 4\n\t\t"
1640 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1641 ".else\n\t\t"
1642 ".int \\n_max >> 2\n\t"
1643 ".endif\n\t"
1644 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1645 ".int 0\n\t" /* count */
1646 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1647 "__do_recurse _build_block_set \\name \\n_max\n\t"
1648 ".endm\n");
1649
1650/*
1651 * Build a memory pool structure and initialize it
1652 * Macro uses __memory_pool_block_set_count global symbol,
1653 * block set addresses and buffer address, it may be called only after
1654 * _build_block_set
1655 */
1656__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1657 ".pushsection ._k_memory_pool,\"aw\","
1658 _SECTION_TYPE_SIGN "progbits\n\t"
1659 ".globl \\name\n\t"
1660 "\\name:\n\t"
1661 ".int \\max_size\n\t" /* max_block_size */
1662 ".int \\min_size\n\t" /* min_block_size */
1663 ".int \\n_max\n\t" /* nr_of_maxblocks */
1664 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1665 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1666 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1667 ".int 0\n\t" /* wait_q->head */
1668 ".int 0\n\t" /* wait_q->next */
1669 ".popsection\n\t"
1670 ".endm\n");
1671
1672#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1673 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1674 _SECTION_TYPE_SIGN "progbits\n\t"); \
1675 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1676 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1677 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1678 STRINGIFY(name) "\n\t"); \
1679 __asm__(".popsection\n\t")
1680
1681#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1682 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1683 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1684 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1685 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04001686 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001687 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1688 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1689 STRINGIFY(name) "\n\t"); \
1690 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1691 __asm__(".int __memory_pool_block_set_count\n\t"); \
1692 __asm__(".popsection\n\t"); \
1693 extern uint32_t _mem_pool_block_set_count_##name; \
1694 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1695
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001696#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
1697 char __noinit __aligned(align) \
1698 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001699
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001700/**
1701 * @brief Define a memory pool
1702 *
1703 * This declares and initializes a memory pool whose buffer is aligned to
1704 * a @a align -byte boundary. The new memory pool can be passed to the
1705 * kernel's memory pool functions.
1706 *
1707 * Note that for each of the minimum sized blocks to be aligned to @a align
1708 * bytes, then @a min_size must be a multiple of @a align.
1709 *
1710 * @param name Name of the memory pool
1711 * @param min_size Minimum block size in the pool
1712 * @param max_size Maximum block size in the pool
1713 * @param n_max Number of maximum sized blocks in the pool
1714 * @param align Alignment of the memory pool's buffer
1715 */
1716#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001717 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1718 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001719 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001720 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1721 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1722 extern struct k_mem_pool name
1723
1724/*
1725 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1726 * to __memory_pool_quad_block_size absolute symbol.
1727 * This function does not get called, but compiler calculates the value and
1728 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1729 */
1730static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1731{
1732 __asm__(".globl __memory_pool_quad_block_size\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07001733#ifdef CONFIG_NIOS2
1734 "__memory_pool_quad_block_size = %0\n\t"
1735#else
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001736 "__memory_pool_quad_block_size = %c0\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07001737#endif
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001738 :
1739 : "n"(sizeof(struct k_mem_pool_quad_block)));
1740}
1741
Peter Mitsis937042c2016-10-13 13:18:26 -04001742/**
1743 * @brief Allocate memory from a memory pool
1744 *
1745 * @param pool Pointer to the memory pool object
1746 * @param block Pointer to the allocated memory's block descriptor
1747 * @param size Minimum number of bytes to allocate
1748 * @param timeout Maximum time (milliseconds) to wait for operation to
1749 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
1750 * to wait as long as necessary.
1751 *
1752 * @return 0 on success, -ENOMEM on failure
1753 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001754extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04001755 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04001756
1757/**
1758 * @brief Return previously allocated memory to its memory pool
1759 *
1760 * @param block Pointer to allocated memory's block descriptor
1761 *
1762 * @return N/A
1763 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001764extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04001765
1766/**
1767 * @brief Defragment the specified memory pool
1768 *
1769 * @param pool Pointer to the memory pool object
1770 *
1771 * @return N/A
1772 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001773extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04001774
1775/**
Allan Stephens480a1312016-10-13 15:44:48 -05001776 * @brief Allocate memory from heap
Peter Mitsis937042c2016-10-13 13:18:26 -04001777 *
Allan Stephens480a1312016-10-13 15:44:48 -05001778 * This routine provides traditional malloc() semantics. The memory is
1779 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04001780 *
1781 * @param size Size of memory requested by the caller (in bytes)
1782 *
1783 * @return Address of the allocated memory on success; otherwise NULL
1784 */
Peter Mitsis5f399242016-10-13 13:26:25 -04001785extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04001786
1787/**
Allan Stephens480a1312016-10-13 15:44:48 -05001788 * @brief Free memory allocated from heap
1789 *
1790 * This routine provides traditional free() semantics. The memory being
1791 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04001792 *
1793 * @param ptr Pointer to previously allocated memory
1794 *
1795 * @return N/A
1796 */
1797extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001798
1799/*
1800 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1801 * hook into the device subsystem, which itself uses nanokernel semaphores,
1802 * and thus currently requires the definition of nano_sem.
1803 */
1804#include <legacy.h>
1805#include <arch/cpu.h>
1806
1807/*
1808 * private APIs that are utilized by one or more public APIs
1809 */
1810
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001811extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812extern void _init_static_threads(void);
1813
1814#ifdef __cplusplus
1815}
1816#endif
1817
1818#endif /* _kernel__h_ */