blob: ee87848e7d5db7326fc3f51914ef017c13fc797a [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;
84struct k_event;
85struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
91struct k_mem_map;
92struct 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 uint32_t init_groups;
152 int init_prio;
153 void (*init_entry)(void *, void *, void *);
154 void *init_p1;
155 void *init_p2;
156 void *init_p3;
157 void (*init_abort)(void);
158 union {
159 char *init_stack;
160 struct k_thread *thread;
161 };
162 unsigned int init_stack_size;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400163 int32_t init_delay;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400164};
165
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400166/*
167 * Common macro used by both K_THREAD_INITIALIZER()
168 * and _MDEF_THREAD_INITIALIZER().
169 */
170#define _THREAD_INITIALIZER(stack, stack_size, \
171 entry, p1, p2, p3, \
172 abort, prio) \
173 .init_prio = (prio), \
174 .init_entry = (void (*)(void *, void *, void *))entry, \
175 .init_p1 = (void *)p1, \
176 .init_p2 = (void *)p2, \
177 .init_p3 = (void *)p3, \
178 .init_abort = abort, \
179 .init_stack = (stack), \
180 .init_stack_size = (stack_size),
181
182/**
183 * @brief Thread initializer macro
184 *
185 * This macro is to only be used with statically defined threads that were not
186 * defined in the MDEF file. As such the associated threads can not belong to
187 * any thread group.
188 */
189#define K_THREAD_INITIALIZER(stack, stack_size, \
190 entry, p1, p2, p3, \
191 abort, prio, delay) \
192 { \
193 _THREAD_INITIALIZER(stack, stack_size, \
194 entry, p1, p2, p3, \
195 abort, prio) \
196 .init_groups = 0, \
197 .init_delay = (delay), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400198 }
199
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400200/**
201 * @brief Thread initializer macro
202 *
203 * This macro is to only be used with statically defined threads that were
204 * defined with legacy APIs (including the MDEF file). As such the associated
205 * threads may belong to one or more thread groups.
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206 */
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400207#define _MDEF_THREAD_INITIALIZER(stack, stack_size, \
208 entry, p1, p2, p3, \
209 abort, prio, groups) \
210 { \
211 _THREAD_INITIALIZER(stack, stack_size, \
212 entry, p1, p2, p3, \
213 abort, prio) \
214 .init_groups = (groups), \
215 .init_delay = K_FOREVER, \
216 }
217
218/**
219 * @brief Define thread initializer and initialize it.
220 *
221 * @internal It has been observed that the x86 compiler by default aligns
222 * these _static_thread_data structures to 32-byte boundaries, thereby
223 * wasting space. To work around this, force a 4-byte alignment.
224 */
225#define K_THREAD_DEFINE(name, stack_size, \
226 entry, p1, p2, p3, \
Allan Stephens06aefdb2016-10-21 15:14:59 -0500227 prio, options, delay) \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400228 char __noinit __stack _k_thread_obj_##name[stack_size]; \
229 struct _static_thread_data _k_thread_data_##name __aligned(4) \
230 __in_section(_k_task_list, private, task) = \
231 K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
232 entry, p1, p2, p3, abort, prio, delay)
233
234/**
235 * @brief Define thread initializer for MDEF defined thread and initialize it.
236 *
237 * @ref K_THREAD_DEFINE
238 */
239#define _MDEF_THREAD_DEFINE(name, stack_size, \
240 entry, p1, p2, p3, \
241 abort, prio, groups) \
242 char __noinit __stack _k_thread_obj_##name[stack_size]; \
243 struct _static_thread_data _k_thread_data_##name __aligned(4) \
244 __in_section(_k_task_list, private, task) = \
245 _MDEF_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400246 entry, p1, p2, p3, abort, prio, groups)
247
Allan Stephens399d0ad2016-10-07 13:41:34 -0500248extern int k_thread_priority_get(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249extern void k_thread_priority_set(k_tid_t thread, int prio);
250
Benjamin Walsh71d52282016-09-29 10:49:48 -0400251extern void k_thread_suspend(k_tid_t thread);
252extern void k_thread_resume(k_tid_t thread);
253extern void k_thread_abort_handler_set(void (*handler)(void));
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400254
255extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400256
257extern int k_am_in_isr(void);
258
259extern void k_thread_custom_data_set(void *value);
260extern void *k_thread_custom_data_get(void);
261
262/**
263 * kernel timing
264 */
265
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400266#include <sys_clock.h>
267
268/* private internal time manipulation (users should never play with ticks) */
269
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500270/* added tick needed to account for tick in progress */
271#define _TICK_ALIGN 1
272
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400273static int64_t __ticks_to_ms(int64_t ticks)
274{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400275#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400276 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400277#else
278 __ASSERT(ticks == 0, "");
279 return 0;
280#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400281}
282
283
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400284/* timeouts */
285
286struct _timeout;
287typedef void (*_timeout_func_t)(struct _timeout *t);
288
289struct _timeout {
290 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400291 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400292 sys_dlist_t *wait_q;
293 int32_t delta_ticks_from_prev;
294 _timeout_func_t func;
295};
296
Allan Stephens45bfa372016-10-12 12:39:42 -0500297
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400298/* timers */
299
300struct k_timer {
301 /*
302 * _timeout structure must be first here if we want to use
303 * dynamic timer allocation. timeout.node is used in the double-linked
304 * list of free timers
305 */
306 struct _timeout timeout;
307
Allan Stephens45bfa372016-10-12 12:39:42 -0500308 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400309 _wait_q_t wait_q;
310
311 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500312 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400313
314 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500315 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400316
317 /* timer period */
318 int32_t period;
319
Allan Stephens45bfa372016-10-12 12:39:42 -0500320 /* timer status */
321 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400322
Allan Stephens45bfa372016-10-12 12:39:42 -0500323 /* used to support legacy timer APIs */
324 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400325
326 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
327};
328
329#define K_TIMER_INITIALIZER(obj) \
330 { \
331 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
332 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
333 }
334
335#define K_TIMER_DEFINE(name) \
336 struct k_timer name = K_TIMER_INITIALIZER(name)
337
Allan Stephens45bfa372016-10-12 12:39:42 -0500338/**
339 * @brief Initialize a timer.
340 *
341 * This routine must be called before the timer is used.
342 *
343 * @param timer Address of timer.
344 * @param expiry_fn Function to invoke each time timer expires.
345 * @param stop_fn Function to invoke if timer is stopped while running.
346 *
347 * @return N/A
348 */
349extern void k_timer_init(struct k_timer *timer,
350 void (*expiry_fn)(struct k_timer *),
351 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700352
Allan Stephens45bfa372016-10-12 12:39:42 -0500353/**
354 * @brief Start a timer.
355 *
356 * This routine starts a timer, and resets its status to zero. The timer
357 * begins counting down using the specified duration and period values.
358 *
359 * Attempting to start a timer that is already running is permitted.
360 * The timer's status is reset to zero and the timer begins counting down
361 * using the new duration and period values.
362 *
363 * @param timer Address of timer.
364 * @param duration Initial timer duration (in milliseconds).
365 * @param period Timer period (in milliseconds).
366 *
367 * @return N/A
368 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400369extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500370 int32_t duration, int32_t period);
371
372/**
373 * @brief Stop a timer.
374 *
375 * This routine stops a running timer prematurely. The timer's stop function,
376 * if one exists, is invoked by the caller.
377 *
378 * Attempting to stop a timer that is not running is permitted, but has no
379 * effect on the timer since it is already stopped.
380 *
381 * @param timer Address of timer.
382 *
383 * @return N/A
384 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400385extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500386
387/**
388 * @brief Read timer status.
389 *
390 * This routine reads the timer's status, which indicates the number of times
391 * it has expired since its status was last read.
392 *
393 * Calling this routine resets the timer's status to zero.
394 *
395 * @param timer Address of timer.
396 *
397 * @return Timer status.
398 */
399extern uint32_t k_timer_status_get(struct k_timer *timer);
400
401/**
402 * @brief Synchronize thread to timer expiration.
403 *
404 * This routine blocks the calling thread until the timer's status is non-zero
405 * (indicating that it has expired at least once since it was last examined)
406 * or the timer is stopped. If the timer status is already non-zero,
407 * or the timer is already stopped, the caller continues without waiting.
408 *
409 * Calling this routine resets the timer's status to zero.
410 *
411 * This routine must not be used by interrupt handlers, since they are not
412 * allowed to block.
413 *
414 * @param timer Address of timer.
415 *
416 * @return Timer status.
417 */
418extern uint32_t k_timer_status_sync(struct k_timer *timer);
419
420/**
421 * @brief Get timer remaining before next timer expiration.
422 *
423 * This routine computes the (approximate) time remaining before a running
424 * timer next expires. If the timer is not running, it returns zero.
425 *
426 * @param timer Address of timer.
427 *
428 * @return Remaining time (in milliseconds).
429 */
430
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400431extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400432
433
Allan Stephens45bfa372016-10-12 12:39:42 -0500434/* kernel clocks */
435
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400436/**
437 * @brief Get the time elapsed since the system booted (uptime)
438 *
439 * @return The current uptime of the system in ms
440 */
441
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400442extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400443
444/**
445 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
446 *
447 * This function is potentially less onerous in both the time it takes to
448 * execute, the interrupt latency it introduces and the amount of 64-bit math
449 * it requires than k_uptime_get(), but it only provides an uptime value of
450 * 32-bits. The user must handle possible rollovers/spillovers.
451 *
452 * At a rate of increment of 1000 per second, it rolls over approximately every
453 * 50 days.
454 *
455 * @return The current uptime of the system in ms
456 */
457
458extern uint32_t k_uptime_get_32(void);
459
460/**
461 * @brief Get the difference between a reference time and the current uptime
462 *
463 * @param reftime A pointer to a reference time. It is updated with the current
464 * uptime upon return.
465 *
466 * @return The delta between the reference time and the current uptime.
467 */
468
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400469extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400470
471/**
472 * @brief Get the difference between a reference time and the current uptime
473 *
474 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
475 * k_uptime_get_32().
476 *
477 * @param reftime A pointer to a reference time. It is updated with the current
478 * uptime upon return.
479 *
480 * @return The delta between the reference time and the current uptime.
481 */
482
483extern uint32_t k_uptime_delta_32(int64_t *reftime);
484
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400485extern uint32_t k_cycle_get_32(void);
486
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400487/**
488 * data transfers (basic)
489 */
490
491/* fifos */
492
493struct k_fifo {
494 _wait_q_t wait_q;
495 sys_slist_t data_q;
496
497 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
498};
499
500extern void k_fifo_init(struct k_fifo *fifo);
501extern void k_fifo_put(struct k_fifo *fifo, void *data);
502extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
503extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
504extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
505
506#define K_FIFO_INITIALIZER(obj) \
507 { \
508 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400509 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400510 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
511 }
512
513#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400514 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515
516/* lifos */
517
518struct k_lifo {
519 _wait_q_t wait_q;
520 void *list;
521
522 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
523};
524
525extern void k_lifo_init(struct k_lifo *lifo);
526extern void k_lifo_put(struct k_lifo *lifo, void *data);
527extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
528
529#define K_LIFO_INITIALIZER(obj) \
530 { \
531 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
532 .list = NULL, \
533 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
534 }
535
536#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400537 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400538
539/* stacks */
540
541struct k_stack {
542 _wait_q_t wait_q;
543 uint32_t *base, *next, *top;
544
545 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
546};
547
Allan Stephens018cd9a2016-10-07 15:13:24 -0500548extern void k_stack_init(struct k_stack *stack,
549 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400550extern void k_stack_push(struct k_stack *stack, uint32_t data);
551extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
552
Peter Mitsis602e6a82016-10-17 11:48:43 -0400553#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400554 { \
555 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
556 .base = stack_buffer, \
557 .next = stack_buffer, \
558 .top = stack_buffer + stack_num_entries, \
559 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
560 }
561
Peter Mitsis602e6a82016-10-17 11:48:43 -0400562#define K_STACK_DEFINE(name, stack_num_entries) \
563 uint32_t __noinit \
564 _k_stack_buf_##name[stack_num_entries]; \
565 struct k_stack name = \
566 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
567 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400568
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400569/**
570 * workqueues
571 */
572
573struct k_work;
574
575typedef void (*k_work_handler_t)(struct k_work *);
576
577/**
578 * A workqueue is a fiber that executes @ref k_work items that are
579 * queued to it. This is useful for drivers which need to schedule
580 * execution of code which might sleep from ISR context. The actual
581 * fiber identifier is not stored in the structure in order to save
582 * space.
583 */
584struct k_work_q {
585 struct k_fifo fifo;
586};
587
588/**
589 * @brief Work flags.
590 */
591enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300592 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593};
594
595/**
596 * @brief An item which can be scheduled on a @ref k_work_q.
597 */
598struct k_work {
599 void *_reserved; /* Used by k_fifo implementation. */
600 k_work_handler_t handler;
601 atomic_t flags[1];
602};
603
604/**
605 * @brief Statically initialize work item
606 */
607#define K_WORK_INITIALIZER(work_handler) \
608 { \
609 ._reserved = NULL, \
610 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300611 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400612 }
613
614/**
615 * @brief Dynamically initialize work item
616 */
617static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
618{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300619 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400620 work->handler = handler;
621}
622
623/**
624 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300625 *
626 * This procedure schedules a work item to be processed.
627 * In the case where the work item has already been submitted and is pending
628 * execution, calling this function will result in a no-op. In this case, the
629 * work item must not be modified externally (e.g. by the caller of this
630 * function), since that could cause the work item to be processed in a
631 * corrupted state.
632 *
633 * @param work_q to schedule the work item
634 * @param work work item
635 *
636 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400637 */
638static inline void k_work_submit_to_queue(struct k_work_q *work_q,
639 struct k_work *work)
640{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300641 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400642 k_fifo_put(&work_q->fifo, work);
643 }
644}
645
646/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300647 * @brief Check if work item is pending.
648 */
649static inline int k_work_pending(struct k_work *work)
650{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300651 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300652}
653
654/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400655 * @brief Start a new workqueue. This routine can be called from either
656 * fiber or task context.
657 */
Allan Stephens904cf972016-10-07 13:59:23 -0500658extern void k_work_q_start(struct k_work_q *work_q, char *stack,
659 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400660
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400661#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400662
663 /*
664 * @brief An item which can be scheduled on a @ref k_work_q with a
665 * delay.
666 */
667struct k_delayed_work {
668 struct k_work work;
669 struct _timeout timeout;
670 struct k_work_q *work_q;
671};
672
673/**
674 * @brief Initialize delayed work
675 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400676extern void k_delayed_work_init(struct k_delayed_work *work,
677 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400678
679/**
680 * @brief Submit a delayed work item to a workqueue.
681 *
682 * This procedure schedules a work item to be processed after a delay.
683 * Once the delay has passed, the work item is submitted to the work queue:
684 * at this point, it is no longer possible to cancel it. Once the work item's
685 * handler is about to be executed, the work is considered complete and can be
686 * resubmitted.
687 *
688 * Care must be taken if the handler blocks or yield as there is no implicit
689 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
690 * it should be explicitly done between the submitter and the handler.
691 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500692 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400693 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500694 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400695 *
696 * @return 0 in case of success or negative value in case of error.
697 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400698extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
699 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500700 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400701
702/**
703 * @brief Cancel a delayed work item
704 *
705 * This procedure cancels a scheduled work item. If the work has been completed
706 * or is idle, this will do nothing. The only case where this can fail is when
707 * the work has been submitted to the work queue, but the handler has not run
708 * yet.
709 *
710 * @param work Delayed work item to be canceled
711 *
712 * @return 0 in case of success or negative value in case of error.
713 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400714extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400715
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400716#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400717
718#if defined(CONFIG_SYSTEM_WORKQUEUE)
719
720extern struct k_work_q k_sys_work_q;
721
722/*
723 * @brief Submit a work item to the system workqueue.
724 *
725 * @ref k_work_submit_to_queue
726 *
727 * When using the system workqueue it is not recommended to block or yield
728 * on the handler since its fiber is shared system wide it may cause
729 * unexpected behavior.
730 */
731static inline void k_work_submit(struct k_work *work)
732{
733 k_work_submit_to_queue(&k_sys_work_q, work);
734}
735
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400736#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400737/*
738 * @brief Submit a delayed work item to the system workqueue.
739 *
740 * @ref k_delayed_work_submit_to_queue
741 *
742 * When using the system workqueue it is not recommended to block or yield
743 * on the handler since its fiber is shared system wide it may cause
744 * unexpected behavior.
745 */
746static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500747 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400748{
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500749 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400750}
751
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400752#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753#endif /* CONFIG_SYSTEM_WORKQUEUE */
754
755/**
756 * synchronization
757 */
758
759/* mutexes */
760
761struct k_mutex {
762 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400763 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400764 uint32_t lock_count;
765 int owner_orig_prio;
766#ifdef CONFIG_OBJECT_MONITOR
767 int num_lock_state_changes;
768 int num_conflicts;
769#endif
770
771 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
772};
773
774#ifdef CONFIG_OBJECT_MONITOR
775#define _MUTEX_INIT_OBJECT_MONITOR \
776 .num_lock_state_changes = 0, .num_conflicts = 0,
777#else
778#define _MUTEX_INIT_OBJECT_MONITOR
779#endif
780
781#define K_MUTEX_INITIALIZER(obj) \
782 { \
783 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
784 .owner = NULL, \
785 .lock_count = 0, \
786 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
787 _MUTEX_INIT_OBJECT_MONITOR \
788 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
789 }
790
791#define K_MUTEX_DEFINE(name) \
792 struct k_mutex name = K_MUTEX_INITIALIZER(name)
793
794extern void k_mutex_init(struct k_mutex *mutex);
795extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
796extern void k_mutex_unlock(struct k_mutex *mutex);
797
798/* semaphores */
799
800struct k_sem {
801 _wait_q_t wait_q;
802 unsigned int count;
803 unsigned int limit;
804
805 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
806};
807
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400808/**
809 * @brief Initialize a semaphore object.
810 *
811 * An initial count and a count limit can be specified. The count will never go
812 * over the count limit if the semaphore is given multiple times without being
813 * taken.
814 *
815 * Cannot be called from ISR.
816 *
817 * @param sem Pointer to a semaphore object.
818 * @param initial_count Initial count.
819 * @param limit Highest value the count can take during operation.
820 *
821 * @return N/A
822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400823extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
824 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400825
826/**
827 * @brief Take a semaphore, possibly pending if not available.
828 *
829 * The current execution context tries to obtain the semaphore. If the
830 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
831 * the context will pend.
832 *
833 * @param sem Pointer to a semaphore object.
834 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
835 * or one of the special values K_NO_WAIT and K_FOREVER.
836 *
837 * @warning If it is called from the context of an ISR, then the only legal
838 * value for @a timeout is K_NO_WAIT.
839 *
840 * @retval 0 When semaphore is obtained successfully.
841 * @retval -EAGAIN When timeout expires.
842 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
843 *
844 * @sa K_NO_WAIT, K_FOREVER
845 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400846extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400847
848/**
849 * @brief Give a semaphore.
850 *
851 * Increase the semaphore's internal count by 1, up to its limit, if no thread
852 * is waiting on the semaphore; otherwise, wake up the first thread in the
853 * semaphore's waiting queue.
854 *
855 * If the latter case, and if the current context is preemptible, the thread
856 * that is taken off the wait queue will be scheduled in and will preempt the
857 * current thread.
858 *
859 * @param sem Pointer to a semaphore object.
860 *
861 * @return N/A
862 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400863extern void k_sem_give(struct k_sem *sem);
864
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400865/**
866 * @brief Reset a semaphore's count to zero.
867 *
868 * The only effect is that the count is set to zero. There is no other
869 * side-effect to calling this function.
870 *
871 * @param sem Pointer to a semaphore object.
872 *
873 * @return N/A
874 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400875static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400876{
877 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400878}
879
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400880/**
881 * @brief Get a semaphore's count.
882 *
883 * Note there is no guarantee the count has not changed by the time this
884 * function returns.
885 *
886 * @param sem Pointer to a semaphore object.
887 *
888 * @return The current semaphore count.
889 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200890static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891{
892 return sem->count;
893}
894
Peter Mitsis45403672016-09-09 14:24:06 -0400895#ifdef CONFIG_SEMAPHORE_GROUPS
896/**
897 * @brief Take the first available semaphore
898 *
899 * Given a list of semaphore pointers, this routine will attempt to take one
900 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
901 * semaphore is identified by @a sem (set to NULL on error).
902 *
903 * Be aware that the more semaphores specified in the group, the more stack
904 * space is required by the waiting thread.
905 *
906 * @param sem_array Array of semaphore pointers terminated by a K_END entry
907 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400908 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
909 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -0400910 *
911 * @retval 0 A semaphore was successfully taken
912 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
913 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400914 *
915 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -0400916 */
917
918extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
919 int32_t timeout);
920
921/**
922 * @brief Give all the semaphores in the group
923 *
924 * This routine will give each semaphore in the array of semaphore pointers.
925 *
926 * @param sem_array Array of semaphore pointers terminated by a K_END entry
927 *
928 * @return N/A
929 */
930extern void k_sem_group_give(struct k_sem *sem_array[]);
931
932/**
933 * @brief Reset the count to zero on each semaphore in the array
934 *
935 * This routine resets the count of each semaphore in the group to zero.
936 * Note that it does NOT have any impact on any thread that might have
937 * been previously pending on any of the semaphores.
938 *
939 * @param sem_array Array of semaphore pointers terminated by a K_END entry
940 *
941 * @return N/A
942 */
943extern void k_sem_group_reset(struct k_sem *sem_array[]);
944#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945
946#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
947 { \
948 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
949 .count = initial_count, \
950 .limit = count_limit, \
951 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
952 }
953
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400954/**
955 * @def K_SEM_DEFINE
956 *
957 * @brief Statically define and initialize a global semaphore.
958 *
959 * Create a global semaphore named @name. It is initialized as if k_sem_init()
960 * was called on it. If the semaphore is to be accessed outside the module
961 * where it is defined, it can be declared via
962 *
963 * extern struct k_sem @name;
964 *
965 * @param name Name of the semaphore variable.
966 * @param initial_count Initial count.
967 * @param count_limit Highest value the count can take during operation.
968 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400969#define K_SEM_DEFINE(name, initial_count, count_limit) \
970 struct k_sem name = \
971 K_SEM_INITIALIZER(name, initial_count, count_limit)
972
973/* events */
974
975#define K_EVT_DEFAULT NULL
976#define K_EVT_IGNORE ((void *)(-1))
977
978typedef int (*k_event_handler_t)(struct k_event *);
979
980struct k_event {
981 k_event_handler_t handler;
982 atomic_t send_count;
983 struct k_work work_item;
984 struct k_sem sem;
985
986 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
987};
988
989extern void _k_event_deliver(struct k_work *work);
990
991#define K_EVENT_INITIALIZER(obj, event_handler) \
992 { \
993 .handler = (k_event_handler_t)event_handler, \
994 .send_count = ATOMIC_INIT(0), \
995 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
996 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
997 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
998 }
999
1000#define K_EVENT_DEFINE(name, event_handler) \
1001 struct k_event name \
1002 __in_section(_k_event_list, event, name) = \
1003 K_EVENT_INITIALIZER(name, event_handler)
1004
1005extern void k_event_init(struct k_event *event, k_event_handler_t handler);
1006extern int k_event_recv(struct k_event *event, int32_t timeout);
1007extern void k_event_send(struct k_event *event);
1008
1009/**
1010 * data transfers (complex)
1011 */
1012
1013/* message queues */
1014
1015struct k_msgq {
1016 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001017 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001018 uint32_t max_msgs;
1019 char *buffer_start;
1020 char *buffer_end;
1021 char *read_ptr;
1022 char *write_ptr;
1023 uint32_t used_msgs;
1024
1025 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1026};
1027
Peter Mitsis1da807e2016-10-06 11:36:59 -04001028#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029 { \
1030 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001031 .max_msgs = q_max_msgs, \
1032 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001034 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001035 .read_ptr = q_buffer, \
1036 .write_ptr = q_buffer, \
1037 .used_msgs = 0, \
1038 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1039 }
1040
Peter Mitsis1da807e2016-10-06 11:36:59 -04001041/**
1042 * @brief Define a message queue
1043 *
1044 * This declares and initializes a message queue whose buffer is aligned to
1045 * a @a q_align -byte boundary. The new message queue can be passed to the
1046 * kernel's message queue functions.
1047 *
1048 * Note that for each of the mesages in the message queue to be aligned to
1049 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1050 *
1051 * @param q_name Name of the message queue
1052 * @param q_msg_size The size in bytes of each message
1053 * @param q_max_msgs Maximum number of messages the queue can hold
1054 * @param q_align Alignment of the message queue's buffer (power of 2)
1055 */
1056#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1057 static char __noinit __aligned(q_align) \
1058 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
1059 struct k_msgq q_name = \
1060 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1061 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001062
Peter Mitsisd7a37502016-10-13 11:37:40 -04001063/**
1064 * @brief Initialize a message queue.
1065 *
1066 * @param q Pointer to the message queue object.
1067 * @param buffer Pointer to memory area that holds queued messages.
1068 * @param msg_size Message size, in bytes.
1069 * @param max_msgs Maximum number of messages that can be queued.
1070 *
1071 * @return N/A
1072 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001073extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001074 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001075
1076/**
1077 * @brief Add a message to a message queue.
1078 *
1079 * This routine adds an item to the message queue. When the message queue is
1080 * full, the routine will wait either for space to become available, or until
1081 * the specified time limit is reached.
1082 *
1083 * @param q Pointer to the message queue object.
1084 * @param data Pointer to message data area.
1085 * @param timeout Number of milliseconds to wait until space becomes available
1086 * to add the message into the message queue, or one of the
1087 * special values K_NO_WAIT and K_FOREVER.
1088 *
1089 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1090 * -EAGAIN if timed out
1091 *
1092 * @sa K_NO_WAIT, K_FOREVER
1093 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001094extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001095
1096/**
1097 * @brief Obtain a message from a message queue.
1098 *
1099 * This routine fetches the oldest item from the message queue. When the message
1100 * queue is found empty, the routine will wait either until an item is added to
1101 * the message queue or until the specified time limit is reached.
1102 *
1103 * @param q Pointer to the message queue object.
1104 * @param data Pointer to message data area.
1105 * @param timeout Number of milliseconds to wait to obtain message, or one of
1106 * the special values K_NO_WAIT and K_FOREVER.
1107 *
1108 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1109 *
1110 * @sa K_NO_WAIT, K_FOREVER
1111 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001113
1114/**
1115 * @brief Purge contents of a message queue.
1116 *
1117 * Discards all messages currently in the message queue, and cancels
1118 * any "add message" operations initiated by waiting threads.
1119 *
1120 * @param q Pointer to the message queue object.
1121 *
1122 * @return N/A
1123 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001124extern void k_msgq_purge(struct k_msgq *q);
1125
Peter Mitsis67be2492016-10-07 11:44:34 -04001126/**
1127 * @brief Get the number of unused messages
1128 *
1129 * @param q Message queue to query
1130 *
1131 * @return Number of unused messages
1132 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001133static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001134{
1135 return q->max_msgs - q->used_msgs;
1136}
1137
Peter Mitsisd7a37502016-10-13 11:37:40 -04001138/**
1139 * @brief Get the number of used messages
1140 *
1141 * @param q Message queue to query
1142 *
1143 * @return Number of used messages
1144 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001145static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001146{
1147 return q->used_msgs;
1148}
1149
1150struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001151 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001152 void *addr_in_pool;
1153 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001154 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155};
1156
1157/* mailboxes */
1158
1159struct k_mbox_msg {
1160 /** internal use only - needed for legacy API support */
1161 uint32_t _mailbox;
1162 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001163 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001164 /** application-defined information value */
1165 uint32_t info;
1166 /** sender's message data buffer */
1167 void *tx_data;
1168 /** internal use only - needed for legacy API support */
1169 void *_rx_data;
1170 /** message data block descriptor */
1171 struct k_mem_block tx_block;
1172 /** source thread id */
1173 k_tid_t rx_source_thread;
1174 /** target thread id */
1175 k_tid_t tx_target_thread;
1176 /** internal use only - thread waiting on send (may be a dummy) */
1177 k_tid_t _syncing_thread;
1178#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1179 /** internal use only - semaphore used during asynchronous send */
1180 struct k_sem *_async_sem;
1181#endif
1182};
1183
1184struct k_mbox {
1185 _wait_q_t tx_msg_queue;
1186 _wait_q_t rx_msg_queue;
1187
1188 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1189};
1190
1191#define K_MBOX_INITIALIZER(obj) \
1192 { \
1193 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1194 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1195 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1196 }
1197
Peter Mitsis12092702016-10-14 12:57:23 -04001198/**
1199 * @brief Define a mailbox
1200 *
1201 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001202 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001203 *
1204 * @param name Name of the mailbox
1205 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206#define K_MBOX_DEFINE(name) \
1207 struct k_mbox name = \
1208 K_MBOX_INITIALIZER(name) \
1209
Peter Mitsis12092702016-10-14 12:57:23 -04001210/**
1211 * @brief Initialize a mailbox.
1212 *
1213 * @param mbox Pointer to the mailbox object
1214 *
1215 * @return N/A
1216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217extern void k_mbox_init(struct k_mbox *mbox);
1218
Peter Mitsis12092702016-10-14 12:57:23 -04001219/**
1220 * @brief Send a mailbox message in a synchronous manner.
1221 *
1222 * Sends a message to a mailbox and waits for a receiver to process it.
1223 * The message data may be in a buffer, in a memory pool block, or non-existent
1224 * (i.e. empty message).
1225 *
1226 * @param mbox Pointer to the mailbox object.
1227 * @param tx_msg Pointer to transmit message descriptor.
1228 * @param timeout Maximum time (milliseconds) to wait for the message to be
1229 * received (although not necessarily completely processed).
1230 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1231 * as necessary.
1232 *
1233 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1234 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001235extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001236 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001237
1238#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1239/**
1240 * @brief Send a mailbox message in an asynchronous manner.
1241 *
1242 * Sends a message to a mailbox without waiting for a receiver to process it.
1243 * The message data may be in a buffer, in a memory pool block, or non-existent
1244 * (i.e. an empty message). Optionally, the specified semaphore will be given
1245 * by the mailbox when the message has been both received and disposed of
1246 * by the receiver.
1247 *
1248 * @param mbox Pointer to the mailbox object.
1249 * @param tx_msg Pointer to transmit message descriptor.
1250 * @param sem Semaphore identifier, or NULL if none specified.
1251 *
1252 * @return N/A
1253 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001254extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001255 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001256#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001257
Peter Mitsis12092702016-10-14 12:57:23 -04001258/**
1259 * @brief Receive a mailbox message.
1260 *
1261 * Receives a message from a mailbox, then optionally retrieves its data
1262 * and disposes of the message.
1263 *
1264 * @param mbox Pointer to the mailbox object.
1265 * @param rx_msg Pointer to receive message descriptor.
1266 * @param buffer Pointer to buffer to receive data.
1267 * (Use NULL to defer data retrieval and message disposal until later.)
1268 * @param timeout Maximum time (milliseconds) to wait for a message.
1269 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1270 * necessary.
1271 *
1272 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1273 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001274extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001276
1277/**
1278 * @brief Retrieve mailbox message data into a buffer.
1279 *
1280 * Completes the processing of a received message by retrieving its data
1281 * into a buffer, then disposing of the message.
1282 *
1283 * Alternatively, this routine can be used to dispose of a received message
1284 * without retrieving its data.
1285 *
1286 * @param rx_msg Pointer to receive message descriptor.
1287 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1288 *
1289 * @return N/A
1290 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001291extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001292
1293/**
1294 * @brief Retrieve mailbox message data into a memory pool block.
1295 *
1296 * Completes the processing of a received message by retrieving its data
1297 * into a memory pool block, then disposing of the message. The memory pool
1298 * block that results from successful retrieval must be returned to the pool
1299 * once the data has been processed, even in cases where zero bytes of data
1300 * are retrieved.
1301 *
1302 * Alternatively, this routine can be used to dispose of a received message
1303 * without retrieving its data. In this case there is no need to return a
1304 * memory pool block to the pool.
1305 *
1306 * This routine allocates a new memory pool block for the data only if the
1307 * data is not already in one. If a new block cannot be allocated, the routine
1308 * returns a failure code and the received message is left unchanged. This
1309 * permits the caller to reattempt data retrieval at a later time or to dispose
1310 * of the received message without retrieving its data.
1311 *
1312 * @param rx_msg Pointer to receive message descriptor.
1313 * @param pool Memory pool identifier. (Use NULL to discard data.)
1314 * @param block Pointer to area to hold memory pool block info.
1315 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1316 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1317 * necessary.
1318 *
1319 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1320 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001321extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001322 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001323 struct k_mem_block *block, int32_t timeout);
1324
1325/* pipes */
1326
1327struct k_pipe {
1328 unsigned char *buffer; /* Pipe buffer: may be NULL */
1329 size_t size; /* Buffer size */
1330 size_t bytes_used; /* # bytes used in buffer */
1331 size_t read_index; /* Where in buffer to read from */
1332 size_t write_index; /* Where in buffer to write */
1333
1334 struct {
1335 _wait_q_t readers; /* Reader wait queue */
1336 _wait_q_t writers; /* Writer wait queue */
1337 } wait_q;
1338
1339 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1340};
1341
Peter Mitsise5d9c582016-10-14 14:44:57 -04001342#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001343 { \
1344 .buffer = pipe_buffer, \
1345 .size = pipe_buffer_size, \
1346 .bytes_used = 0, \
1347 .read_index = 0, \
1348 .write_index = 0, \
1349 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1350 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1351 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1352 }
1353
Peter Mitsise5d9c582016-10-14 14:44:57 -04001354#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1355 static unsigned char __noinit __aligned(pipe_align) \
1356 _k_pipe_buf_##name[pipe_buffer_size]; \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001357 struct k_pipe name = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001358 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001359
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001360/**
1361 * @brief Runtime initialization of a pipe
1362 *
1363 * @param pipe Pointer to pipe to initialize
1364 * @param buffer Pointer to buffer to use for pipe's ring buffer
1365 * @param size Size of the pipe's ring buffer
1366 *
1367 * @return N/A
1368 */
1369extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1370 size_t size);
1371
1372/**
1373 * @brief Put a message into the specified pipe
1374 *
1375 * This routine synchronously adds a message into the pipe specified by
1376 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001377 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1378 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1380 *
1381 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001382 * @param data Data to put into the pipe
1383 * @param bytes_to_write Desired number of bytes to put into the pipe
1384 * @param bytes_written Number of bytes the pipe accepted
1385 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001386 * @param timeout Maximum number of milliseconds to wait
1387 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001388 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001389 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001390 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001392extern int k_pipe_put(struct k_pipe *pipe, void *data,
1393 size_t bytes_to_write, size_t *bytes_written,
1394 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395
1396/**
1397 * @brief Get a message from the specified pipe
1398 *
1399 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001400 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001402 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1404 *
1405 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001406 * @param data Location to place retrieved data
1407 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1408 * @param bytes_read Number of bytes retrieved from the pipe
1409 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410 * @param timeout Maximum number of milliseconds to wait
1411 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001412 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001414 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001416extern int k_pipe_get(struct k_pipe *pipe, void *data,
1417 size_t bytes_to_read, size_t *bytes_read,
1418 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001419
Peter Mitsis2fef0232016-10-14 14:53:44 -04001420#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001421/**
1422 * @brief Send a message to the specified pipe
1423 *
1424 * This routine asynchronously sends a message from the pipe specified by
1425 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1426 * free the memory block @a block and give the semaphore @a sem (if specified).
1427 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1428 * at any given time.
1429 *
1430 * @param pipe Pointer to the pipe
1431 * @param block Memory block containing data to send
1432 * @param size Number of data bytes in memory block to send
1433 * @param sem Semaphore to signal upon completion (else NULL)
1434 *
1435 * @retval N/A
1436 */
1437extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1438 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001439#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440
1441/**
1442 * memory management
1443 */
1444
1445/* memory maps */
1446
1447struct k_mem_map {
1448 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001449 uint32_t num_blocks;
1450 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001451 char *buffer;
1452 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001453 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454
1455 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1456};
1457
Peter Mitsis578f9112016-10-07 13:50:31 -04001458#define K_MEM_MAP_INITIALIZER(obj, map_buffer, map_block_size, map_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459 { \
1460 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1461 .num_blocks = map_num_blocks, \
1462 .block_size = map_block_size, \
1463 .buffer = map_buffer, \
1464 .free_list = NULL, \
1465 .num_used = 0, \
1466 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1467 }
1468
Peter Mitsis578f9112016-10-07 13:50:31 -04001469/**
1470 * @brief Define a memory map
1471 *
1472 * This declares and initializes a memory map whose buffer is aligned to
1473 * a @a map_align -byte boundary. The new memory map can be passed to the
1474 * kernel's memory map functions.
1475 *
1476 * Note that for each of the blocks in the memory map to be aligned to
1477 * @a map_align bytes, then @a map_block_size must be a multiple of
1478 * @a map_align.
1479 *
1480 * @param name Name of the memory map
1481 * @param map_block_size Size of each block in the buffer (in bytes)
1482 * @param map_num_blocks Number blocks in the buffer
1483 * @param map_align Alignment of the memory map's buffer (power of 2)
1484 */
1485#define K_MEM_MAP_DEFINE(name, map_block_size, map_num_blocks, map_align) \
Allan Stephens35ffaff2016-10-21 14:31:37 -05001486 char __noinit __aligned(map_align) \
Peter Mitsis578f9112016-10-07 13:50:31 -04001487 _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1488 struct k_mem_map name \
1489 __in_section(_k_mem_map_ptr, private, mem_map) = \
1490 K_MEM_MAP_INITIALIZER(name, _k_mem_map_buf_##name, \
1491 map_block_size, map_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001492
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001493/**
1494 * @brief Initialize a memory map.
1495 *
1496 * Initializes the memory map and creates its list of free blocks.
1497 *
1498 * @param map Pointer to the memory map object
1499 * @param buffer Pointer to buffer used for the blocks.
1500 * @param block_size Size of each block, in bytes.
1501 * @param num_blocks Number of blocks.
1502 *
1503 * @return N/A
1504 */
Peter Mitsis578f9112016-10-07 13:50:31 -04001505extern void k_mem_map_init(struct k_mem_map *map, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04001506 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001507
1508/**
1509 * @brief Allocate a memory map block.
1510 *
1511 * Takes a block from the list of unused blocks.
1512 *
1513 * @param map Pointer to memory map object.
1514 * @param mem Pointer to area to receive block address.
1515 * @param timeout Maximum time (milliseconds) to wait for allocation to
1516 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait
1517 * as long as necessary.
1518 *
1519 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1520 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001521extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001522
1523/**
1524 * @brief Free a memory map block.
1525 *
1526 * Gives block to a waiting thread if there is one, otherwise returns it to
1527 * the list of unused blocks.
1528 *
1529 * @param map Pointer to memory map object.
1530 * @param mem Pointer to area to containing block address.
1531 *
1532 * @return N/A
1533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001534extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1535
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04001536/**
1537 * @brief Get the number of used memory blocks
1538 *
1539 * This routine gets the current number of used memory blocks in the
1540 * specified pool. It should be used for stats purposes only as that
1541 * value may potentially be out-of-date by the time it is used.
1542 *
1543 * @param map Memory map to query
1544 *
1545 * @return Number of used memory blocks
1546 */
Peter Mitsisfb02d572016-10-13 16:55:45 -04001547static inline uint32_t k_mem_map_num_used_get(struct k_mem_map *map)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001548{
1549 return map->num_used;
1550}
1551
Peter Mitsisc001aa82016-10-13 13:53:37 -04001552/**
1553 * @brief Get the number of unused memory blocks
1554 *
1555 * This routine gets the current number of unused memory blocks in the
1556 * specified pool. It should be used for stats purposes only as that value
1557 * may potentially be out-of-date by the time it is used.
1558 *
1559 * @param map Memory map to query
1560 *
1561 * @return Number of unused memory blocks
1562 */
Peter Mitsisfb02d572016-10-13 16:55:45 -04001563static inline uint32_t k_mem_map_num_free_get(struct k_mem_map *map)
Peter Mitsisc001aa82016-10-13 13:53:37 -04001564{
1565 return map->num_blocks - map->num_used;
1566}
1567
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001568/* memory pools */
1569
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001570/*
1571 * Memory pool requires a buffer and two arrays of structures for the
1572 * memory block accounting:
1573 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1574 * status of four blocks of memory.
1575 */
1576struct k_mem_pool_quad_block {
1577 char *mem_blocks; /* pointer to the first of four memory blocks */
1578 uint32_t mem_status; /* four bits. If bit is set, memory block is
1579 allocated */
1580};
1581/*
1582 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1583 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1584 * block size is 4 times less than the previous one and thus requires 4 times
1585 * bigger array of k_mem_pool_quad_block structures to keep track of the
1586 * memory blocks.
1587 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001588
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001589/*
1590 * The array of k_mem_pool_block_set keeps the information of each array of
1591 * k_mem_pool_quad_block structures
1592 */
1593struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04001594 size_t block_size; /* memory block size */
1595 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001596 struct k_mem_pool_quad_block *quad_block;
1597 int count;
1598};
1599
1600/* Memory pool descriptor */
1601struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04001602 size_t max_block_size;
1603 size_t min_block_size;
1604 uint32_t nr_of_maxblocks;
1605 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001606 struct k_mem_pool_block_set *block_set;
1607 char *bufblock;
1608 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001609 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1610};
1611
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001612#ifdef CONFIG_ARM
1613#define _SECTION_TYPE_SIGN "%"
1614#else
1615#define _SECTION_TYPE_SIGN "@"
1616#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001617
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001618/*
1619 * Static memory pool initialization
1620 */
1621/*
1622 * Use .altmacro to be able to recalculate values and pass them as string
1623 * arguments when calling assembler macros resursively
1624 */
1625__asm__(".altmacro\n\t");
1626
1627/*
1628 * Recursively calls a macro
1629 * The followig global symbols need to be initialized:
1630 * __memory_pool_max_block_size - maximal size of the memory block
1631 * __memory_pool_min_block_size - minimal size of the memory block
1632 * Notes:
1633 * Global symbols are used due the fact that assembler macro allows only
1634 * one argument be passed with the % conversion
1635 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1636 * is used instead of / 4.
1637 * n_max argument needs to go first in the invoked macro, as some
1638 * assemblers concatenate \name and %(\n_max * 4) arguments
1639 * if \name goes first
1640 */
1641__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1642 ".ifge __memory_pool_max_block_size >> 2 -"
1643 " __memory_pool_min_block_size\n\t\t"
1644 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1645 "\\macro_name %(\\n_max * 4) \\name\n\t"
1646 ".endif\n\t"
1647 ".endm\n");
1648
1649/*
1650 * Build quad blocks
1651 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1652 * structures and recursively calls itself for the next array, 4 times
1653 * larger.
1654 * The followig global symbols need to be initialized:
1655 * __memory_pool_max_block_size - maximal size of the memory block
1656 * __memory_pool_min_block_size - minimal size of the memory block
1657 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1658 */
1659__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04001660 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001661 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1662 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1663 ".if \\n_max % 4\n\t\t"
1664 ".skip __memory_pool_quad_block_size\n\t"
1665 ".endif\n\t"
1666 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1667 ".endm\n");
1668
1669/*
1670 * Build block sets and initialize them
1671 * Macro initializes the k_mem_pool_block_set structure and
1672 * recursively calls itself for the next one.
1673 * The followig global symbols need to be initialized:
1674 * __memory_pool_max_block_size - maximal size of the memory block
1675 * __memory_pool_min_block_size - minimal size of the memory block
1676 * __memory_pool_block_set_count, the number of the elements in the
1677 * block set array must be set to 0. Macro calculates it's real
1678 * value.
1679 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1680 * structures, _build_quad_blocks must be called prior it.
1681 */
1682__asm__(".macro _build_block_set n_max, name\n\t"
1683 ".int __memory_pool_max_block_size\n\t" /* block_size */
1684 ".if \\n_max % 4\n\t\t"
1685 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1686 ".else\n\t\t"
1687 ".int \\n_max >> 2\n\t"
1688 ".endif\n\t"
1689 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1690 ".int 0\n\t" /* count */
1691 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1692 "__do_recurse _build_block_set \\name \\n_max\n\t"
1693 ".endm\n");
1694
1695/*
1696 * Build a memory pool structure and initialize it
1697 * Macro uses __memory_pool_block_set_count global symbol,
1698 * block set addresses and buffer address, it may be called only after
1699 * _build_block_set
1700 */
1701__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1702 ".pushsection ._k_memory_pool,\"aw\","
1703 _SECTION_TYPE_SIGN "progbits\n\t"
1704 ".globl \\name\n\t"
1705 "\\name:\n\t"
1706 ".int \\max_size\n\t" /* max_block_size */
1707 ".int \\min_size\n\t" /* min_block_size */
1708 ".int \\n_max\n\t" /* nr_of_maxblocks */
1709 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1710 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1711 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1712 ".int 0\n\t" /* wait_q->head */
1713 ".int 0\n\t" /* wait_q->next */
1714 ".popsection\n\t"
1715 ".endm\n");
1716
1717#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1718 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1719 _SECTION_TYPE_SIGN "progbits\n\t"); \
1720 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1721 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1722 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1723 STRINGIFY(name) "\n\t"); \
1724 __asm__(".popsection\n\t")
1725
1726#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1727 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1728 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1729 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1730 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04001731 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001732 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1733 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1734 STRINGIFY(name) "\n\t"); \
1735 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1736 __asm__(".int __memory_pool_block_set_count\n\t"); \
1737 __asm__(".popsection\n\t"); \
1738 extern uint32_t _mem_pool_block_set_count_##name; \
1739 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1740
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001741#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
1742 char __noinit __aligned(align) \
1743 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001744
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001745/**
1746 * @brief Define a memory pool
1747 *
1748 * This declares and initializes a memory pool whose buffer is aligned to
1749 * a @a align -byte boundary. The new memory pool can be passed to the
1750 * kernel's memory pool functions.
1751 *
1752 * Note that for each of the minimum sized blocks to be aligned to @a align
1753 * bytes, then @a min_size must be a multiple of @a align.
1754 *
1755 * @param name Name of the memory pool
1756 * @param min_size Minimum block size in the pool
1757 * @param max_size Maximum block size in the pool
1758 * @param n_max Number of maximum sized blocks in the pool
1759 * @param align Alignment of the memory pool's buffer
1760 */
1761#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001762 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1763 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001764 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001765 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1766 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1767 extern struct k_mem_pool name
1768
1769/*
1770 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1771 * to __memory_pool_quad_block_size absolute symbol.
1772 * This function does not get called, but compiler calculates the value and
1773 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1774 */
1775static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1776{
1777 __asm__(".globl __memory_pool_quad_block_size\n\t"
1778 "__memory_pool_quad_block_size = %c0\n\t"
1779 :
1780 : "n"(sizeof(struct k_mem_pool_quad_block)));
1781}
1782
Peter Mitsis937042c2016-10-13 13:18:26 -04001783/**
1784 * @brief Allocate memory from a memory pool
1785 *
1786 * @param pool Pointer to the memory pool object
1787 * @param block Pointer to the allocated memory's block descriptor
1788 * @param size Minimum number of bytes to allocate
1789 * @param timeout Maximum time (milliseconds) to wait for operation to
1790 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
1791 * to wait as long as necessary.
1792 *
1793 * @return 0 on success, -ENOMEM on failure
1794 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001795extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04001796 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04001797
1798/**
1799 * @brief Return previously allocated memory to its memory pool
1800 *
1801 * @param block Pointer to allocated memory's block descriptor
1802 *
1803 * @return N/A
1804 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04001806
1807/**
1808 * @brief Defragment the specified memory pool
1809 *
1810 * @param pool Pointer to the memory pool object
1811 *
1812 * @return N/A
1813 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001814extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04001815
1816/**
1817 * @brief Allocate memory from heap pool
1818 *
1819 * This routine provides traditional malloc semantics; internally it uses
1820 * the memory pool APIs on a dedicated HEAP pool
1821 *
1822 * @param size Size of memory requested by the caller (in bytes)
1823 *
1824 * @return Address of the allocated memory on success; otherwise NULL
1825 */
Peter Mitsis5f399242016-10-13 13:26:25 -04001826extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04001827
1828/**
1829 * @brief Free memory allocated through k_malloc()
1830 *
1831 * @param ptr Pointer to previously allocated memory
1832 *
1833 * @return N/A
1834 */
1835extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001836
1837/*
1838 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1839 * hook into the device subsystem, which itself uses nanokernel semaphores,
1840 * and thus currently requires the definition of nano_sem.
1841 */
1842#include <legacy.h>
1843#include <arch/cpu.h>
1844
1845/*
1846 * private APIs that are utilized by one or more public APIs
1847 */
1848
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001849extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001850extern void _init_static_threads(void);
1851
1852#ifdef __cplusplus
1853}
1854#endif
1855
1856#endif /* _kernel__h_ */