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