blob: 51c4eed2848f2f411be5ae82e28c2aafccac71c5 [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
105struct k_thread_config {
106 char *stack;
107 unsigned stack_size;
108 unsigned prio;
109};
110
111typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
112extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
113 void (*entry)(void *, void *, void*),
114 void *p1, void *p2, void *p3,
115 int32_t prio, uint32_t options, int32_t delay);
116
117extern void k_sleep(int32_t duration);
118extern void k_busy_wait(uint32_t usec_to_wait);
119extern void k_yield(void);
120extern void k_wakeup(k_tid_t thread);
121extern k_tid_t k_current_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122extern int k_thread_cancel(k_tid_t thread);
123
124extern void k_thread_abort(k_tid_t thread);
125
126#define K_THREAD_GROUP_EXE 0x1
127#define K_THREAD_GROUP_SYS 0x2
128#define K_THREAD_GROUP_FPU 0x4
129
130/* XXX - doesn't work because CONFIG_ARCH is a string */
131#if 0
132/* arch-specific groups */
133#if CONFIG_ARCH == "x86"
134#define K_THREAD_GROUP_SSE 0x4
135#endif
136#endif
137
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400138#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400139#define _THREAD_TIMEOUT_INIT(obj) \
140 (obj).nano_timeout = { \
141 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400142 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400143 .wait_q = NULL, \
144 .delta_ticks_from_prev = -1, \
145 },
146#else
147#define _THREAD_TIMEOUT_INIT(obj)
148#endif
149
150#ifdef CONFIG_ERRNO
151#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
152#else
153#define _THREAD_ERRNO_INIT(obj)
154#endif
155
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400156struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157 uint32_t init_groups;
158 int init_prio;
159 void (*init_entry)(void *, void *, void *);
160 void *init_p1;
161 void *init_p2;
162 void *init_p3;
163 void (*init_abort)(void);
164 union {
165 char *init_stack;
166 struct k_thread *thread;
167 };
168 unsigned int init_stack_size;
169};
170
171#define K_THREAD_INITIALIZER(stack, stack_size, \
172 entry, p1, p2, p3, \
173 abort, prio, groups) \
174 { \
175 .init_groups = (groups), \
176 .init_prio = (prio), \
Allan Stephensea6cfd12016-10-05 13:35:38 -0500177 .init_entry = (void (*)(void *, void *, void *))entry, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400178 .init_p1 = (void *)p1, \
179 .init_p2 = (void *)p2, \
180 .init_p3 = (void *)p3, \
181 .init_abort = abort, \
182 .init_stack = (stack), \
183 .init_stack_size = (stack_size), \
184 }
185
186/*
187 * Define thread initializer object and initialize it
188 * NOTE: For thread group functions thread initializers must be organized
189 * in array and thus should not have gaps between them.
190 * On x86 by default compiler aligns them by 32 byte boundary. To prevent
191 * this 32-bit alignment in specified here.
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400192 * _static_thread_data structure sise needs to be kept 32-bit aligned as well
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400193 */
Allan Stephensea6cfd12016-10-05 13:35:38 -0500194#define K_THREAD_DEFINE(name, stack_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400195 entry, p1, p2, p3, \
196 abort, prio, groups) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400197 char __noinit __stack _k_thread_obj_##name[stack_size]; \
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400198 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400199 __in_section(_k_task_list, private, task) = \
200 K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
201 entry, p1, p2, p3, abort, prio, groups)
202
Allan Stephens399d0ad2016-10-07 13:41:34 -0500203extern int k_thread_priority_get(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204extern void k_thread_priority_set(k_tid_t thread, int prio);
205
Benjamin Walsh71d52282016-09-29 10:49:48 -0400206extern void k_thread_suspend(k_tid_t thread);
207extern void k_thread_resume(k_tid_t thread);
208extern void k_thread_abort_handler_set(void (*handler)(void));
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400209
210extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400211
212extern int k_am_in_isr(void);
213
214extern void k_thread_custom_data_set(void *value);
215extern void *k_thread_custom_data_get(void);
216
217/**
218 * kernel timing
219 */
220
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400221#include <sys_clock.h>
222
223/* private internal time manipulation (users should never play with ticks) */
224
225static int64_t __ticks_to_ms(int64_t ticks)
226{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400227#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400228 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400229#else
230 __ASSERT(ticks == 0, "");
231 return 0;
232#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400233}
234
235
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400236/* timeouts */
237
238struct _timeout;
239typedef void (*_timeout_func_t)(struct _timeout *t);
240
241struct _timeout {
242 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400243 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400244 sys_dlist_t *wait_q;
245 int32_t delta_ticks_from_prev;
246 _timeout_func_t func;
247};
248
249/* timers */
250
251struct k_timer {
252 /*
253 * _timeout structure must be first here if we want to use
254 * dynamic timer allocation. timeout.node is used in the double-linked
255 * list of free timers
256 */
257 struct _timeout timeout;
258
259 /* wait queue for the threads waiting on this timer */
260 _wait_q_t wait_q;
261
262 /* runs in ISR context */
263 void (*handler)(void *);
264 void *handler_arg;
265
266 /* runs in the context of the thread that calls k_timer_stop() */
267 void (*stop_handler)(void *);
268 void *stop_handler_arg;
269
270 /* timer period */
271 int32_t period;
272
273 /* user supplied data pointer returned to the thread*/
274 void *user_data;
275
276 /* user supplied data pointer */
277 void *user_data_internal;
278
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
291extern void k_timer_init(struct k_timer *timer, void *data);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700292
293#if (CONFIG_NUM_DYNAMIC_TIMERS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400294extern struct k_timer *k_timer_alloc(void);
295extern void k_timer_free(struct k_timer *timer);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700296#endif
297
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400298extern void k_timer_start(struct k_timer *timer,
299 int32_t duration, int32_t period,
300 void (*handler)(void *), void *handler_arg,
301 void (*stop_handler)(void *), void *stop_handler_arg);
302extern void k_timer_restart(struct k_timer *timer, int32_t duration,
303 int32_t period);
304extern void k_timer_stop(struct k_timer *timer);
305extern int k_timer_test(struct k_timer *timer, void **data, int wait);
306extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400307
308
309/**
310 * @brief Get the time elapsed since the system booted (uptime)
311 *
312 * @return The current uptime of the system in ms
313 */
314
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400315extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400316
317/**
318 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
319 *
320 * This function is potentially less onerous in both the time it takes to
321 * execute, the interrupt latency it introduces and the amount of 64-bit math
322 * it requires than k_uptime_get(), but it only provides an uptime value of
323 * 32-bits. The user must handle possible rollovers/spillovers.
324 *
325 * At a rate of increment of 1000 per second, it rolls over approximately every
326 * 50 days.
327 *
328 * @return The current uptime of the system in ms
329 */
330
331extern uint32_t k_uptime_get_32(void);
332
333/**
334 * @brief Get the difference between a reference time and the current uptime
335 *
336 * @param reftime A pointer to a reference time. It is updated with the current
337 * uptime upon return.
338 *
339 * @return The delta between the reference time and the current uptime.
340 */
341
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400342extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400343
344/**
345 * @brief Get the difference between a reference time and the current uptime
346 *
347 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
348 * k_uptime_get_32().
349 *
350 * @param reftime A pointer to a reference time. It is updated with the current
351 * uptime upon return.
352 *
353 * @return The delta between the reference time and the current uptime.
354 */
355
356extern uint32_t k_uptime_delta_32(int64_t *reftime);
357
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400358extern bool k_timer_pool_is_empty(void);
359
360extern uint32_t k_cycle_get_32(void);
361
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400362/**
363 * data transfers (basic)
364 */
365
366/* fifos */
367
368struct k_fifo {
369 _wait_q_t wait_q;
370 sys_slist_t data_q;
371
372 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
373};
374
375extern void k_fifo_init(struct k_fifo *fifo);
376extern void k_fifo_put(struct k_fifo *fifo, void *data);
377extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
378extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
379extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
380
381#define K_FIFO_INITIALIZER(obj) \
382 { \
383 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400384 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400385 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
386 }
387
388#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400389 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400390
391/* lifos */
392
393struct k_lifo {
394 _wait_q_t wait_q;
395 void *list;
396
397 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
398};
399
400extern void k_lifo_init(struct k_lifo *lifo);
401extern void k_lifo_put(struct k_lifo *lifo, void *data);
402extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
403
404#define K_LIFO_INITIALIZER(obj) \
405 { \
406 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
407 .list = NULL, \
408 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
409 }
410
411#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400412 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400413
414/* stacks */
415
416struct k_stack {
417 _wait_q_t wait_q;
418 uint32_t *base, *next, *top;
419
420 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
421};
422
423extern void k_stack_init(struct k_stack *stack, int num_entries);
424extern void k_stack_init_with_buffer(struct k_stack *stack, int num_entries,
425 uint32_t *buffer);
426extern void k_stack_push(struct k_stack *stack, uint32_t data);
427extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
428
429#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
430 { \
431 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
432 .base = stack_buffer, \
433 .next = stack_buffer, \
434 .top = stack_buffer + stack_num_entries, \
435 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
436 }
437
438#define K_STACK_DEFINE(name, stack_num_entries) \
439 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400440 struct k_stack name = \
441 K_STACK_INITIALIZER(name, stack_num_entries, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400442 _k_stack_buf_##name); \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400443
444#define K_STACK_SIZE(stack_num_entries) \
445 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
446
447/**
448 * workqueues
449 */
450
451struct k_work;
452
453typedef void (*k_work_handler_t)(struct k_work *);
454
455/**
456 * A workqueue is a fiber that executes @ref k_work items that are
457 * queued to it. This is useful for drivers which need to schedule
458 * execution of code which might sleep from ISR context. The actual
459 * fiber identifier is not stored in the structure in order to save
460 * space.
461 */
462struct k_work_q {
463 struct k_fifo fifo;
464};
465
466/**
467 * @brief Work flags.
468 */
469enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300470 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400471};
472
473/**
474 * @brief An item which can be scheduled on a @ref k_work_q.
475 */
476struct k_work {
477 void *_reserved; /* Used by k_fifo implementation. */
478 k_work_handler_t handler;
479 atomic_t flags[1];
480};
481
482/**
483 * @brief Statically initialize work item
484 */
485#define K_WORK_INITIALIZER(work_handler) \
486 { \
487 ._reserved = NULL, \
488 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300489 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400490 }
491
492/**
493 * @brief Dynamically initialize work item
494 */
495static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
496{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300497 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400498 work->handler = handler;
499}
500
501/**
502 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300503 *
504 * This procedure schedules a work item to be processed.
505 * In the case where the work item has already been submitted and is pending
506 * execution, calling this function will result in a no-op. In this case, the
507 * work item must not be modified externally (e.g. by the caller of this
508 * function), since that could cause the work item to be processed in a
509 * corrupted state.
510 *
511 * @param work_q to schedule the work item
512 * @param work work item
513 *
514 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515 */
516static inline void k_work_submit_to_queue(struct k_work_q *work_q,
517 struct k_work *work)
518{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300519 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400520 k_fifo_put(&work_q->fifo, work);
521 }
522}
523
524/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300525 * @brief Check if work item is pending.
526 */
527static inline int k_work_pending(struct k_work *work)
528{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300529 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300530}
531
532/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533 * @brief Start a new workqueue. This routine can be called from either
534 * fiber or task context.
535 */
536extern void k_work_q_start(struct k_work_q *work_q,
537 const struct k_thread_config *config);
538
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400539#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400540
541 /*
542 * @brief An item which can be scheduled on a @ref k_work_q with a
543 * delay.
544 */
545struct k_delayed_work {
546 struct k_work work;
547 struct _timeout timeout;
548 struct k_work_q *work_q;
549};
550
551/**
552 * @brief Initialize delayed work
553 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400554extern void k_delayed_work_init(struct k_delayed_work *work,
555 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400556
557/**
558 * @brief Submit a delayed work item to a workqueue.
559 *
560 * This procedure schedules a work item to be processed after a delay.
561 * Once the delay has passed, the work item is submitted to the work queue:
562 * at this point, it is no longer possible to cancel it. Once the work item's
563 * handler is about to be executed, the work is considered complete and can be
564 * resubmitted.
565 *
566 * Care must be taken if the handler blocks or yield as there is no implicit
567 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
568 * it should be explicitly done between the submitter and the handler.
569 *
570 * @param work_q to schedule the work item
571 * @param work Delayed work item
572 * @param ticks Ticks to wait before scheduling the work item
573 *
574 * @return 0 in case of success or negative value in case of error.
575 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400576extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
577 struct k_delayed_work *work,
578 int32_t ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579
580/**
581 * @brief Cancel a delayed work item
582 *
583 * This procedure cancels a scheduled work item. If the work has been completed
584 * or is idle, this will do nothing. The only case where this can fail is when
585 * the work has been submitted to the work queue, but the handler has not run
586 * yet.
587 *
588 * @param work Delayed work item to be canceled
589 *
590 * @return 0 in case of success or negative value in case of error.
591 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400592extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400594#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595
596#if defined(CONFIG_SYSTEM_WORKQUEUE)
597
598extern struct k_work_q k_sys_work_q;
599
600/*
601 * @brief Submit a work item to the system workqueue.
602 *
603 * @ref k_work_submit_to_queue
604 *
605 * When using the system workqueue it is not recommended to block or yield
606 * on the handler since its fiber is shared system wide it may cause
607 * unexpected behavior.
608 */
609static inline void k_work_submit(struct k_work *work)
610{
611 k_work_submit_to_queue(&k_sys_work_q, work);
612}
613
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400614#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400615/*
616 * @brief Submit a delayed work item to the system workqueue.
617 *
618 * @ref k_delayed_work_submit_to_queue
619 *
620 * When using the system workqueue it is not recommended to block or yield
621 * on the handler since its fiber is shared system wide it may cause
622 * unexpected behavior.
623 */
624static inline int k_delayed_work_submit(struct k_delayed_work *work,
625 int ticks)
626{
627 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
628}
629
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400630#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400631#endif /* CONFIG_SYSTEM_WORKQUEUE */
632
633/**
634 * synchronization
635 */
636
637/* mutexes */
638
639struct k_mutex {
640 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400641 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400642 uint32_t lock_count;
643 int owner_orig_prio;
644#ifdef CONFIG_OBJECT_MONITOR
645 int num_lock_state_changes;
646 int num_conflicts;
647#endif
648
649 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
650};
651
652#ifdef CONFIG_OBJECT_MONITOR
653#define _MUTEX_INIT_OBJECT_MONITOR \
654 .num_lock_state_changes = 0, .num_conflicts = 0,
655#else
656#define _MUTEX_INIT_OBJECT_MONITOR
657#endif
658
659#define K_MUTEX_INITIALIZER(obj) \
660 { \
661 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
662 .owner = NULL, \
663 .lock_count = 0, \
664 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
665 _MUTEX_INIT_OBJECT_MONITOR \
666 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
667 }
668
669#define K_MUTEX_DEFINE(name) \
670 struct k_mutex name = K_MUTEX_INITIALIZER(name)
671
672extern void k_mutex_init(struct k_mutex *mutex);
673extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
674extern void k_mutex_unlock(struct k_mutex *mutex);
675
676/* semaphores */
677
678struct k_sem {
679 _wait_q_t wait_q;
680 unsigned int count;
681 unsigned int limit;
682
683 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
684};
685
686extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
687 unsigned int limit);
688extern int k_sem_take(struct k_sem *sem, int32_t timeout);
689extern void k_sem_give(struct k_sem *sem);
690
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400691static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400692{
693 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694}
695
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200696static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400697{
698 return sem->count;
699}
700
Peter Mitsis45403672016-09-09 14:24:06 -0400701#ifdef CONFIG_SEMAPHORE_GROUPS
702/**
703 * @brief Take the first available semaphore
704 *
705 * Given a list of semaphore pointers, this routine will attempt to take one
706 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
707 * semaphore is identified by @a sem (set to NULL on error).
708 *
709 * Be aware that the more semaphores specified in the group, the more stack
710 * space is required by the waiting thread.
711 *
712 * @param sem_array Array of semaphore pointers terminated by a K_END entry
713 * @param sem Identifies the semaphore that was taken
714 * @param timeout Maximum number of milliseconds to wait
715 *
716 * @retval 0 A semaphore was successfully taken
717 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
718 * @retval -EAGAIN Time out occurred while waiting for semaphore
719 */
720
721extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
722 int32_t timeout);
723
724/**
725 * @brief Give all the semaphores in the group
726 *
727 * This routine will give each semaphore in the array of semaphore pointers.
728 *
729 * @param sem_array Array of semaphore pointers terminated by a K_END entry
730 *
731 * @return N/A
732 */
733extern void k_sem_group_give(struct k_sem *sem_array[]);
734
735/**
736 * @brief Reset the count to zero on each semaphore in the array
737 *
738 * This routine resets the count of each semaphore in the group to zero.
739 * Note that it does NOT have any impact on any thread that might have
740 * been previously pending on any of the semaphores.
741 *
742 * @param sem_array Array of semaphore pointers terminated by a K_END entry
743 *
744 * @return N/A
745 */
746extern void k_sem_group_reset(struct k_sem *sem_array[]);
747#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400748
749#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
750 { \
751 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
752 .count = initial_count, \
753 .limit = count_limit, \
754 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
755 }
756
757#define K_SEM_DEFINE(name, initial_count, count_limit) \
758 struct k_sem name = \
759 K_SEM_INITIALIZER(name, initial_count, count_limit)
760
761/* events */
762
763#define K_EVT_DEFAULT NULL
764#define K_EVT_IGNORE ((void *)(-1))
765
766typedef int (*k_event_handler_t)(struct k_event *);
767
768struct k_event {
769 k_event_handler_t handler;
770 atomic_t send_count;
771 struct k_work work_item;
772 struct k_sem sem;
773
774 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
775};
776
777extern void _k_event_deliver(struct k_work *work);
778
779#define K_EVENT_INITIALIZER(obj, event_handler) \
780 { \
781 .handler = (k_event_handler_t)event_handler, \
782 .send_count = ATOMIC_INIT(0), \
783 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
784 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
785 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
786 }
787
788#define K_EVENT_DEFINE(name, event_handler) \
789 struct k_event name \
790 __in_section(_k_event_list, event, name) = \
791 K_EVENT_INITIALIZER(name, event_handler)
792
793extern void k_event_init(struct k_event *event, k_event_handler_t handler);
794extern int k_event_recv(struct k_event *event, int32_t timeout);
795extern void k_event_send(struct k_event *event);
796
797/**
798 * data transfers (complex)
799 */
800
801/* message queues */
802
803struct k_msgq {
804 _wait_q_t wait_q;
805 uint32_t msg_size;
806 uint32_t max_msgs;
807 char *buffer_start;
808 char *buffer_end;
809 char *read_ptr;
810 char *write_ptr;
811 uint32_t used_msgs;
812
813 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
814};
815
816#define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \
817 { \
818 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
819 .max_msgs = q_depth, \
820 .msg_size = q_width, \
821 .buffer_start = q_buffer, \
822 .buffer_end = q_buffer + (q_depth * q_width), \
823 .read_ptr = q_buffer, \
824 .write_ptr = q_buffer, \
825 .used_msgs = 0, \
826 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
827 }
828
829#define K_MSGQ_DEFINE(name, q_depth, q_width) \
830 static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \
831 struct k_msgq name = \
832 K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name)
833
834#define K_MSGQ_SIZE(q_depth, q_width) \
835 ((sizeof(struct k_msgq)) + ((q_width) * (q_depth)))
836
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400837extern void k_msgq_init(struct k_msgq *q, uint32_t msg_size,
838 uint32_t max_msgs, char *buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400839extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
840extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
841extern void k_msgq_purge(struct k_msgq *q);
842
843static inline int k_msgq_num_used_get(struct k_msgq *q)
844{
845 return q->used_msgs;
846}
847
848struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400849 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400850 void *addr_in_pool;
851 void *data;
852 uint32_t req_size;
853};
854
855/* mailboxes */
856
857struct k_mbox_msg {
858 /** internal use only - needed for legacy API support */
859 uint32_t _mailbox;
860 /** size of message (in bytes) */
861 uint32_t size;
862 /** application-defined information value */
863 uint32_t info;
864 /** sender's message data buffer */
865 void *tx_data;
866 /** internal use only - needed for legacy API support */
867 void *_rx_data;
868 /** message data block descriptor */
869 struct k_mem_block tx_block;
870 /** source thread id */
871 k_tid_t rx_source_thread;
872 /** target thread id */
873 k_tid_t tx_target_thread;
874 /** internal use only - thread waiting on send (may be a dummy) */
875 k_tid_t _syncing_thread;
876#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
877 /** internal use only - semaphore used during asynchronous send */
878 struct k_sem *_async_sem;
879#endif
880};
881
882struct k_mbox {
883 _wait_q_t tx_msg_queue;
884 _wait_q_t rx_msg_queue;
885
886 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
887};
888
889#define K_MBOX_INITIALIZER(obj) \
890 { \
891 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
892 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
893 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
894 }
895
896#define K_MBOX_DEFINE(name) \
897 struct k_mbox name = \
898 K_MBOX_INITIALIZER(name) \
899
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400900extern void k_mbox_init(struct k_mbox *mbox);
901
902extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
903 int32_t timeout);
904extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
905 struct k_sem *sem);
906
907extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *msg,
908 void *buffer, int32_t timeout);
909extern void k_mbox_data_get(struct k_mbox_msg *msg, void *buffer);
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400910extern int k_mbox_data_block_get(struct k_mbox_msg *msg,
911 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912 struct k_mem_block *block, int32_t timeout);
913
914/* pipes */
915
916struct k_pipe {
917 unsigned char *buffer; /* Pipe buffer: may be NULL */
918 size_t size; /* Buffer size */
919 size_t bytes_used; /* # bytes used in buffer */
920 size_t read_index; /* Where in buffer to read from */
921 size_t write_index; /* Where in buffer to write */
922
923 struct {
924 _wait_q_t readers; /* Reader wait queue */
925 _wait_q_t writers; /* Writer wait queue */
926 } wait_q;
927
928 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
929};
930
931#define K_PIPE_INITIALIZER(obj, pipe_buffer_size, pipe_buffer) \
932 { \
933 .buffer = pipe_buffer, \
934 .size = pipe_buffer_size, \
935 .bytes_used = 0, \
936 .read_index = 0, \
937 .write_index = 0, \
938 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
939 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
940 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
941 }
942
943#define K_PIPE_DEFINE(name, pipe_buffer_size) \
944 static unsigned char __noinit _k_pipe_buf_##name[pipe_buffer_size]; \
945 struct k_pipe name = \
946 K_PIPE_INITIALIZER(name, pipe_buffer_size, _k_pipe_buf_##name)
947
948#define K_PIPE_SIZE(buffer_size) (sizeof(struct k_pipe) + buffer_size)
949
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950/**
951 * @brief Runtime initialization of a pipe
952 *
953 * @param pipe Pointer to pipe to initialize
954 * @param buffer Pointer to buffer to use for pipe's ring buffer
955 * @param size Size of the pipe's ring buffer
956 *
957 * @return N/A
958 */
959extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
960 size_t size);
961
962/**
963 * @brief Put a message into the specified pipe
964 *
965 * This routine synchronously adds a message into the pipe specified by
966 * @a pipe. It will wait up to @a timeout for the pipe to accept
967 * @a num_bytes_to_write bytes of data. If by @a timeout, the pipe could not
968 * accept @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
969 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
970 *
971 * @param pipe Pointer to the pipe
972 * @param buffer Data to put into the pipe
973 * @param num_bytes_to_write Desired number of bytes to put into the pipe
974 * @param num_bytes_written Number of bytes the pipe accepted
975 * @param min_bytes Minimum number of bytes accepted for success
976 * @param timeout Maximum number of milliseconds to wait
977 *
978 * @retval 0 At least @a min_bytes were sent
979 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
980 * @retval -EAGAIN Fewer than @a min_bytes were sent
981 */
982extern int k_pipe_put(struct k_pipe *pipe, void *buffer,
983 size_t num_bytes_to_write, size_t *num_bytes_written,
984 size_t min_bytes, int32_t timeout);
985
986/**
987 * @brief Get a message from the specified pipe
988 *
989 * This routine synchronously retrieves a message from the pipe specified by
990 * @a pipe. It will wait up to @a timeout to retrieve @a num_bytes_to_read
991 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
992 * @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
993 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
994 *
995 * @param pipe Pointer to the pipe
996 * @param buffer Location to place retrieved data
997 * @param num_bytes_to_read Desired number of bytes to retrieve from the pipe
998 * @param num_bytes_read Number of bytes retrieved from the pipe
999 * @param min_bytes Minimum number of bytes retrieved for success
1000 * @param timeout Maximum number of milliseconds to wait
1001 *
1002 * @retval 0 At least @a min_bytes were transferred
1003 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
1004 * @retval -EAGAIN Fewer than @a min_bytes were retrieved
1005 */
1006extern int k_pipe_get(struct k_pipe *pipe, void *buffer,
1007 size_t num_bytes_to_read, size_t *num_bytes_read,
1008 size_t min_bytes, int32_t timeout);
1009
1010/**
1011 * @brief Send a message to the specified pipe
1012 *
1013 * This routine asynchronously sends a message from the pipe specified by
1014 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1015 * free the memory block @a block and give the semaphore @a sem (if specified).
1016 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1017 * at any given time.
1018 *
1019 * @param pipe Pointer to the pipe
1020 * @param block Memory block containing data to send
1021 * @param size Number of data bytes in memory block to send
1022 * @param sem Semaphore to signal upon completion (else NULL)
1023 *
1024 * @retval N/A
1025 */
1026extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1027 size_t size, struct k_sem *sem);
1028
1029/**
1030 * memory management
1031 */
1032
1033/* memory maps */
1034
1035struct k_mem_map {
1036 _wait_q_t wait_q;
1037 int num_blocks;
1038 int block_size;
1039 char *buffer;
1040 char *free_list;
1041 int num_used;
1042
1043 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1044};
1045
1046#define K_MEM_MAP_INITIALIZER(obj, map_num_blocks, map_block_size, \
1047 map_buffer) \
1048 { \
1049 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1050 .num_blocks = map_num_blocks, \
1051 .block_size = map_block_size, \
1052 .buffer = map_buffer, \
1053 .free_list = NULL, \
1054 .num_used = 0, \
1055 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1056 }
1057
1058#define K_MEM_MAP_DEFINE(name, map_num_blocks, map_block_size) \
1059 char _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1060 struct k_mem_map name \
1061 __in_section(_k_mem_map_ptr, private, mem_map) = \
1062 K_MEM_MAP_INITIALIZER(name, map_num_blocks, \
1063 map_block_size, _k_mem_map_buf_##name)
1064
1065#define K_MEM_MAP_SIZE(map_num_blocks, map_block_size) \
1066 (sizeof(struct k_mem_map) + ((map_num_blocks) * (map_block_size)))
1067
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001068extern void k_mem_map_init(struct k_mem_map *map, int num_blocks,
1069 int block_size, void *buffer);
1070extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
1071extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1072
1073static inline int k_mem_map_num_used_get(struct k_mem_map *map)
1074{
1075 return map->num_used;
1076}
1077
1078/* memory pools */
1079
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001080/*
1081 * Memory pool requires a buffer and two arrays of structures for the
1082 * memory block accounting:
1083 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1084 * status of four blocks of memory.
1085 */
1086struct k_mem_pool_quad_block {
1087 char *mem_blocks; /* pointer to the first of four memory blocks */
1088 uint32_t mem_status; /* four bits. If bit is set, memory block is
1089 allocated */
1090};
1091/*
1092 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1093 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1094 * block size is 4 times less than the previous one and thus requires 4 times
1095 * bigger array of k_mem_pool_quad_block structures to keep track of the
1096 * memory blocks.
1097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001099/*
1100 * The array of k_mem_pool_block_set keeps the information of each array of
1101 * k_mem_pool_quad_block structures
1102 */
1103struct k_mem_pool_block_set {
1104 int block_size; /* memory block size */
1105 int nr_of_entries; /* nr of quad block structures in the array */
1106 struct k_mem_pool_quad_block *quad_block;
1107 int count;
1108};
1109
1110/* Memory pool descriptor */
1111struct k_mem_pool {
1112 int max_block_size;
1113 int min_block_size;
1114 int nr_of_maxblocks;
1115 int nr_of_block_sets;
1116 struct k_mem_pool_block_set *block_set;
1117 char *bufblock;
1118 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1120};
1121
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001122#ifdef CONFIG_ARM
1123#define _SECTION_TYPE_SIGN "%"
1124#else
1125#define _SECTION_TYPE_SIGN "@"
1126#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001128/*
1129 * Static memory pool initialization
1130 */
1131/*
1132 * Use .altmacro to be able to recalculate values and pass them as string
1133 * arguments when calling assembler macros resursively
1134 */
1135__asm__(".altmacro\n\t");
1136
1137/*
1138 * Recursively calls a macro
1139 * The followig global symbols need to be initialized:
1140 * __memory_pool_max_block_size - maximal size of the memory block
1141 * __memory_pool_min_block_size - minimal size of the memory block
1142 * Notes:
1143 * Global symbols are used due the fact that assembler macro allows only
1144 * one argument be passed with the % conversion
1145 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1146 * is used instead of / 4.
1147 * n_max argument needs to go first in the invoked macro, as some
1148 * assemblers concatenate \name and %(\n_max * 4) arguments
1149 * if \name goes first
1150 */
1151__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1152 ".ifge __memory_pool_max_block_size >> 2 -"
1153 " __memory_pool_min_block_size\n\t\t"
1154 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1155 "\\macro_name %(\\n_max * 4) \\name\n\t"
1156 ".endif\n\t"
1157 ".endm\n");
1158
1159/*
1160 * Build quad blocks
1161 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1162 * structures and recursively calls itself for the next array, 4 times
1163 * larger.
1164 * The followig global symbols need to be initialized:
1165 * __memory_pool_max_block_size - maximal size of the memory block
1166 * __memory_pool_min_block_size - minimal size of the memory block
1167 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1168 */
1169__asm__(".macro _build_quad_blocks n_max, name\n\t"
1170 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1171 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1172 ".if \\n_max % 4\n\t\t"
1173 ".skip __memory_pool_quad_block_size\n\t"
1174 ".endif\n\t"
1175 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1176 ".endm\n");
1177
1178/*
1179 * Build block sets and initialize them
1180 * Macro initializes the k_mem_pool_block_set structure and
1181 * recursively calls itself for the next one.
1182 * The followig global symbols need to be initialized:
1183 * __memory_pool_max_block_size - maximal size of the memory block
1184 * __memory_pool_min_block_size - minimal size of the memory block
1185 * __memory_pool_block_set_count, the number of the elements in the
1186 * block set array must be set to 0. Macro calculates it's real
1187 * value.
1188 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1189 * structures, _build_quad_blocks must be called prior it.
1190 */
1191__asm__(".macro _build_block_set n_max, name\n\t"
1192 ".int __memory_pool_max_block_size\n\t" /* block_size */
1193 ".if \\n_max % 4\n\t\t"
1194 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1195 ".else\n\t\t"
1196 ".int \\n_max >> 2\n\t"
1197 ".endif\n\t"
1198 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1199 ".int 0\n\t" /* count */
1200 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1201 "__do_recurse _build_block_set \\name \\n_max\n\t"
1202 ".endm\n");
1203
1204/*
1205 * Build a memory pool structure and initialize it
1206 * Macro uses __memory_pool_block_set_count global symbol,
1207 * block set addresses and buffer address, it may be called only after
1208 * _build_block_set
1209 */
1210__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1211 ".pushsection ._k_memory_pool,\"aw\","
1212 _SECTION_TYPE_SIGN "progbits\n\t"
1213 ".globl \\name\n\t"
1214 "\\name:\n\t"
1215 ".int \\max_size\n\t" /* max_block_size */
1216 ".int \\min_size\n\t" /* min_block_size */
1217 ".int \\n_max\n\t" /* nr_of_maxblocks */
1218 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1219 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1220 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1221 ".int 0\n\t" /* wait_q->head */
1222 ".int 0\n\t" /* wait_q->next */
1223 ".popsection\n\t"
1224 ".endm\n");
1225
1226#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1227 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1228 _SECTION_TYPE_SIGN "progbits\n\t"); \
1229 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1230 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1231 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1232 STRINGIFY(name) "\n\t"); \
1233 __asm__(".popsection\n\t")
1234
1235#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1236 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1237 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1238 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1239 _SECTION_TYPE_SIGN "progbits\n\t"); \
1240 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1241 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1242 STRINGIFY(name) "\n\t"); \
1243 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1244 __asm__(".int __memory_pool_block_set_count\n\t"); \
1245 __asm__(".popsection\n\t"); \
1246 extern uint32_t _mem_pool_block_set_count_##name; \
1247 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1248
1249#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max) \
1250 char __noinit _mem_pool_buffer_##name[(max_size) * (n_max)]
1251
1252#define K_MEMORY_POOL_DEFINE(name, min_size, max_size, n_max) \
1253 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1254 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
1255 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max); \
1256 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1257 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1258 extern struct k_mem_pool name
1259
1260/*
1261 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1262 * to __memory_pool_quad_block_size absolute symbol.
1263 * This function does not get called, but compiler calculates the value and
1264 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1265 */
1266static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1267{
1268 __asm__(".globl __memory_pool_quad_block_size\n\t"
1269 "__memory_pool_quad_block_size = %c0\n\t"
1270 :
1271 : "n"(sizeof(struct k_mem_pool_quad_block)));
1272}
1273
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001274#define K_MEM_POOL_SIZE(max_block_size, num_max_blocks) \
1275 (sizeof(struct k_mem_pool) + ((max_block_size) * (num_max_blocks)))
1276
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001277extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278 int size, int32_t timeout);
1279extern void k_mem_pool_free(struct k_mem_block *block);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001280extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001281extern void *k_malloc(uint32_t size);
1282extern void k_free(void *p);
1283
1284/*
1285 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1286 * hook into the device subsystem, which itself uses nanokernel semaphores,
1287 * and thus currently requires the definition of nano_sem.
1288 */
1289#include <legacy.h>
1290#include <arch/cpu.h>
1291
1292/*
1293 * private APIs that are utilized by one or more public APIs
1294 */
1295
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001297extern void _init_static_threads(void);
1298
1299#ifdef __cplusplus
1300}
1301#endif
1302
1303#endif /* _kernel__h_ */