blob: 7650758c714d23c61c6761ac19ab0eae24ddbefc [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
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040097typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098
99/* threads/scheduler/execution contexts */
100
101enum execution_context_types {
102 K_ISR = 0,
103 K_COOP_THREAD,
104 K_PREEMPT_THREAD,
105};
106
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
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400141#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400142#define _THREAD_TIMEOUT_INIT(obj) \
143 (obj).nano_timeout = { \
144 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400145 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400146 .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
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400159struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400160 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), \
Allan Stephensea6cfd12016-10-05 13:35:38 -0500180 .init_entry = (void (*)(void *, void *, void *))entry, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400181 .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.
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400195 * _static_thread_data structure sise needs to be kept 32-bit aligned as well
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400196 */
Allan Stephensea6cfd12016-10-05 13:35:38 -0500197#define K_THREAD_DEFINE(name, stack_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400198 entry, p1, p2, p3, \
199 abort, prio, groups) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400200 char __noinit __stack _k_thread_obj_##name[stack_size]; \
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400201 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400202 __in_section(_k_task_list, private, task) = \
203 K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
204 entry, p1, p2, p3, abort, prio, groups)
205
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400206/* extern int k_thread_prio_get(k_tid_t thread); in sched.h */
207extern void k_thread_priority_set(k_tid_t thread, int prio);
208
Benjamin Walsh71d52282016-09-29 10:49:48 -0400209extern void k_thread_suspend(k_tid_t thread);
210extern void k_thread_resume(k_tid_t thread);
211extern void k_thread_abort_handler_set(void (*handler)(void));
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212#if 0
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400213extern int k_thread_entry_set(k_tid_t thread,
214 void (*entry)(void*, void*, void*);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400215#endif
216
217extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400218
219extern int k_am_in_isr(void);
220
221extern void k_thread_custom_data_set(void *value);
222extern void *k_thread_custom_data_get(void);
223
224/**
225 * kernel timing
226 */
227
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400228#include <sys_clock.h>
229
230/* private internal time manipulation (users should never play with ticks) */
231
232static int64_t __ticks_to_ms(int64_t ticks)
233{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400234#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400235 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400236#else
237 __ASSERT(ticks == 0, "");
238 return 0;
239#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400240}
241
242
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400243/* timeouts */
244
245struct _timeout;
246typedef void (*_timeout_func_t)(struct _timeout *t);
247
248struct _timeout {
249 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400250 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400251 sys_dlist_t *wait_q;
252 int32_t delta_ticks_from_prev;
253 _timeout_func_t func;
254};
255
256/* timers */
257
258struct k_timer {
259 /*
260 * _timeout structure must be first here if we want to use
261 * dynamic timer allocation. timeout.node is used in the double-linked
262 * list of free timers
263 */
264 struct _timeout timeout;
265
266 /* wait queue for the threads waiting on this timer */
267 _wait_q_t wait_q;
268
269 /* runs in ISR context */
270 void (*handler)(void *);
271 void *handler_arg;
272
273 /* runs in the context of the thread that calls k_timer_stop() */
274 void (*stop_handler)(void *);
275 void *stop_handler_arg;
276
277 /* timer period */
278 int32_t period;
279
280 /* user supplied data pointer returned to the thread*/
281 void *user_data;
282
283 /* user supplied data pointer */
284 void *user_data_internal;
285
286 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
287};
288
289#define K_TIMER_INITIALIZER(obj) \
290 { \
291 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
292 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
293 }
294
295#define K_TIMER_DEFINE(name) \
296 struct k_timer name = K_TIMER_INITIALIZER(name)
297
298extern void k_timer_init(struct k_timer *timer, void *data);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700299
300#if (CONFIG_NUM_DYNAMIC_TIMERS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400301extern struct k_timer *k_timer_alloc(void);
302extern void k_timer_free(struct k_timer *timer);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700303#endif
304
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400305extern void k_timer_start(struct k_timer *timer,
306 int32_t duration, int32_t period,
307 void (*handler)(void *), void *handler_arg,
308 void (*stop_handler)(void *), void *stop_handler_arg);
309extern void k_timer_restart(struct k_timer *timer, int32_t duration,
310 int32_t period);
311extern void k_timer_stop(struct k_timer *timer);
312extern int k_timer_test(struct k_timer *timer, void **data, int wait);
313extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400314
315
316/**
317 * @brief Get the time elapsed since the system booted (uptime)
318 *
319 * @return The current uptime of the system in ms
320 */
321
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400322extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400323
324/**
325 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
326 *
327 * This function is potentially less onerous in both the time it takes to
328 * execute, the interrupt latency it introduces and the amount of 64-bit math
329 * it requires than k_uptime_get(), but it only provides an uptime value of
330 * 32-bits. The user must handle possible rollovers/spillovers.
331 *
332 * At a rate of increment of 1000 per second, it rolls over approximately every
333 * 50 days.
334 *
335 * @return The current uptime of the system in ms
336 */
337
338extern uint32_t k_uptime_get_32(void);
339
340/**
341 * @brief Get the difference between a reference time and the current uptime
342 *
343 * @param reftime A pointer to a reference time. It is updated with the current
344 * uptime upon return.
345 *
346 * @return The delta between the reference time and the current uptime.
347 */
348
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400349extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400350
351/**
352 * @brief Get the difference between a reference time and the current uptime
353 *
354 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
355 * k_uptime_get_32().
356 *
357 * @param reftime A pointer to a reference time. It is updated with the current
358 * uptime upon return.
359 *
360 * @return The delta between the reference time and the current uptime.
361 */
362
363extern uint32_t k_uptime_delta_32(int64_t *reftime);
364
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400365extern bool k_timer_pool_is_empty(void);
366
367extern uint32_t k_cycle_get_32(void);
368
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400369/**
370 * data transfers (basic)
371 */
372
373/* fifos */
374
375struct k_fifo {
376 _wait_q_t wait_q;
377 sys_slist_t data_q;
378
379 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
380};
381
382extern void k_fifo_init(struct k_fifo *fifo);
383extern void k_fifo_put(struct k_fifo *fifo, void *data);
384extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
385extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
386extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
387
388#define K_FIFO_INITIALIZER(obj) \
389 { \
390 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400391 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400392 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
393 }
394
395#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400396 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400397
398/* lifos */
399
400struct k_lifo {
401 _wait_q_t wait_q;
402 void *list;
403
404 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
405};
406
407extern void k_lifo_init(struct k_lifo *lifo);
408extern void k_lifo_put(struct k_lifo *lifo, void *data);
409extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
410
411#define K_LIFO_INITIALIZER(obj) \
412 { \
413 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
414 .list = NULL, \
415 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
416 }
417
418#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400419 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400420
421/* stacks */
422
423struct k_stack {
424 _wait_q_t wait_q;
425 uint32_t *base, *next, *top;
426
427 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
428};
429
430extern void k_stack_init(struct k_stack *stack, int num_entries);
431extern void k_stack_init_with_buffer(struct k_stack *stack, int num_entries,
432 uint32_t *buffer);
433extern void k_stack_push(struct k_stack *stack, uint32_t data);
434extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
435
436#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
437 { \
438 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
439 .base = stack_buffer, \
440 .next = stack_buffer, \
441 .top = stack_buffer + stack_num_entries, \
442 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
443 }
444
445#define K_STACK_DEFINE(name, stack_num_entries) \
446 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400447 struct k_stack name = \
448 K_STACK_INITIALIZER(name, stack_num_entries, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400449 _k_stack_buf_##name); \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400450
451#define K_STACK_SIZE(stack_num_entries) \
452 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
453
454/**
455 * workqueues
456 */
457
458struct k_work;
459
460typedef void (*k_work_handler_t)(struct k_work *);
461
462/**
463 * A workqueue is a fiber that executes @ref k_work items that are
464 * queued to it. This is useful for drivers which need to schedule
465 * execution of code which might sleep from ISR context. The actual
466 * fiber identifier is not stored in the structure in order to save
467 * space.
468 */
469struct k_work_q {
470 struct k_fifo fifo;
471};
472
473/**
474 * @brief Work flags.
475 */
476enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300477 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400478};
479
480/**
481 * @brief An item which can be scheduled on a @ref k_work_q.
482 */
483struct k_work {
484 void *_reserved; /* Used by k_fifo implementation. */
485 k_work_handler_t handler;
486 atomic_t flags[1];
487};
488
489/**
490 * @brief Statically initialize work item
491 */
492#define K_WORK_INITIALIZER(work_handler) \
493 { \
494 ._reserved = NULL, \
495 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300496 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400497 }
498
499/**
500 * @brief Dynamically initialize work item
501 */
502static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
503{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300504 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400505 work->handler = handler;
506}
507
508/**
509 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300510 *
511 * This procedure schedules a work item to be processed.
512 * In the case where the work item has already been submitted and is pending
513 * execution, calling this function will result in a no-op. In this case, the
514 * work item must not be modified externally (e.g. by the caller of this
515 * function), since that could cause the work item to be processed in a
516 * corrupted state.
517 *
518 * @param work_q to schedule the work item
519 * @param work work item
520 *
521 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400522 */
523static inline void k_work_submit_to_queue(struct k_work_q *work_q,
524 struct k_work *work)
525{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300526 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400527 k_fifo_put(&work_q->fifo, work);
528 }
529}
530
531/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300532 * @brief Check if work item is pending.
533 */
534static inline int k_work_pending(struct k_work *work)
535{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300536 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300537}
538
539/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400540 * @brief Start a new workqueue. This routine can be called from either
541 * fiber or task context.
542 */
543extern void k_work_q_start(struct k_work_q *work_q,
544 const struct k_thread_config *config);
545
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400546#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400547
548 /*
549 * @brief An item which can be scheduled on a @ref k_work_q with a
550 * delay.
551 */
552struct k_delayed_work {
553 struct k_work work;
554 struct _timeout timeout;
555 struct k_work_q *work_q;
556};
557
558/**
559 * @brief Initialize delayed work
560 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400561extern void k_delayed_work_init(struct k_delayed_work *work,
562 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400563
564/**
565 * @brief Submit a delayed work item to a workqueue.
566 *
567 * This procedure schedules a work item to be processed after a delay.
568 * Once the delay has passed, the work item is submitted to the work queue:
569 * at this point, it is no longer possible to cancel it. Once the work item's
570 * handler is about to be executed, the work is considered complete and can be
571 * resubmitted.
572 *
573 * Care must be taken if the handler blocks or yield as there is no implicit
574 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
575 * it should be explicitly done between the submitter and the handler.
576 *
577 * @param work_q to schedule the work item
578 * @param work Delayed work item
579 * @param ticks Ticks to wait before scheduling the work item
580 *
581 * @return 0 in case of success or negative value in case of error.
582 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400583extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
584 struct k_delayed_work *work,
585 int32_t ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400586
587/**
588 * @brief Cancel a delayed work item
589 *
590 * This procedure cancels a scheduled work item. If the work has been completed
591 * or is idle, this will do nothing. The only case where this can fail is when
592 * the work has been submitted to the work queue, but the handler has not run
593 * yet.
594 *
595 * @param work Delayed work item to be canceled
596 *
597 * @return 0 in case of success or negative value in case of error.
598 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400599extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400600
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400601#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400602
603#if defined(CONFIG_SYSTEM_WORKQUEUE)
604
605extern struct k_work_q k_sys_work_q;
606
607/*
608 * @brief Submit a work item to the system workqueue.
609 *
610 * @ref k_work_submit_to_queue
611 *
612 * When using the system workqueue it is not recommended to block or yield
613 * on the handler since its fiber is shared system wide it may cause
614 * unexpected behavior.
615 */
616static inline void k_work_submit(struct k_work *work)
617{
618 k_work_submit_to_queue(&k_sys_work_q, work);
619}
620
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400621#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622/*
623 * @brief Submit a delayed work item to the system workqueue.
624 *
625 * @ref k_delayed_work_submit_to_queue
626 *
627 * When using the system workqueue it is not recommended to block or yield
628 * on the handler since its fiber is shared system wide it may cause
629 * unexpected behavior.
630 */
631static inline int k_delayed_work_submit(struct k_delayed_work *work,
632 int ticks)
633{
634 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
635}
636
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400637#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400638#endif /* CONFIG_SYSTEM_WORKQUEUE */
639
640/**
641 * synchronization
642 */
643
644/* mutexes */
645
646struct k_mutex {
647 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400648 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400649 uint32_t lock_count;
650 int owner_orig_prio;
651#ifdef CONFIG_OBJECT_MONITOR
652 int num_lock_state_changes;
653 int num_conflicts;
654#endif
655
656 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
657};
658
659#ifdef CONFIG_OBJECT_MONITOR
660#define _MUTEX_INIT_OBJECT_MONITOR \
661 .num_lock_state_changes = 0, .num_conflicts = 0,
662#else
663#define _MUTEX_INIT_OBJECT_MONITOR
664#endif
665
666#define K_MUTEX_INITIALIZER(obj) \
667 { \
668 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
669 .owner = NULL, \
670 .lock_count = 0, \
671 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
672 _MUTEX_INIT_OBJECT_MONITOR \
673 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
674 }
675
676#define K_MUTEX_DEFINE(name) \
677 struct k_mutex name = K_MUTEX_INITIALIZER(name)
678
679extern void k_mutex_init(struct k_mutex *mutex);
680extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
681extern void k_mutex_unlock(struct k_mutex *mutex);
682
683/* semaphores */
684
685struct k_sem {
686 _wait_q_t wait_q;
687 unsigned int count;
688 unsigned int limit;
689
690 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
691};
692
693extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
694 unsigned int limit);
695extern int k_sem_take(struct k_sem *sem, int32_t timeout);
696extern void k_sem_give(struct k_sem *sem);
697
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400698static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699{
700 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400701}
702
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200703static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400704{
705 return sem->count;
706}
707
Peter Mitsis45403672016-09-09 14:24:06 -0400708#ifdef CONFIG_SEMAPHORE_GROUPS
709/**
710 * @brief Take the first available semaphore
711 *
712 * Given a list of semaphore pointers, this routine will attempt to take one
713 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
714 * semaphore is identified by @a sem (set to NULL on error).
715 *
716 * Be aware that the more semaphores specified in the group, the more stack
717 * space is required by the waiting thread.
718 *
719 * @param sem_array Array of semaphore pointers terminated by a K_END entry
720 * @param sem Identifies the semaphore that was taken
721 * @param timeout Maximum number of milliseconds to wait
722 *
723 * @retval 0 A semaphore was successfully taken
724 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
725 * @retval -EAGAIN Time out occurred while waiting for semaphore
726 */
727
728extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
729 int32_t timeout);
730
731/**
732 * @brief Give all the semaphores in the group
733 *
734 * This routine will give each semaphore in the array of semaphore pointers.
735 *
736 * @param sem_array Array of semaphore pointers terminated by a K_END entry
737 *
738 * @return N/A
739 */
740extern void k_sem_group_give(struct k_sem *sem_array[]);
741
742/**
743 * @brief Reset the count to zero on each semaphore in the array
744 *
745 * This routine resets the count of each semaphore in the group to zero.
746 * Note that it does NOT have any impact on any thread that might have
747 * been previously pending on any of the semaphores.
748 *
749 * @param sem_array Array of semaphore pointers terminated by a K_END entry
750 *
751 * @return N/A
752 */
753extern void k_sem_group_reset(struct k_sem *sem_array[]);
754#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400755
756#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
757 { \
758 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
759 .count = initial_count, \
760 .limit = count_limit, \
761 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
762 }
763
764#define K_SEM_DEFINE(name, initial_count, count_limit) \
765 struct k_sem name = \
766 K_SEM_INITIALIZER(name, initial_count, count_limit)
767
768/* events */
769
770#define K_EVT_DEFAULT NULL
771#define K_EVT_IGNORE ((void *)(-1))
772
773typedef int (*k_event_handler_t)(struct k_event *);
774
775struct k_event {
776 k_event_handler_t handler;
777 atomic_t send_count;
778 struct k_work work_item;
779 struct k_sem sem;
780
781 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
782};
783
784extern void _k_event_deliver(struct k_work *work);
785
786#define K_EVENT_INITIALIZER(obj, event_handler) \
787 { \
788 .handler = (k_event_handler_t)event_handler, \
789 .send_count = ATOMIC_INIT(0), \
790 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
791 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
792 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
793 }
794
795#define K_EVENT_DEFINE(name, event_handler) \
796 struct k_event name \
797 __in_section(_k_event_list, event, name) = \
798 K_EVENT_INITIALIZER(name, event_handler)
799
800extern void k_event_init(struct k_event *event, k_event_handler_t handler);
801extern int k_event_recv(struct k_event *event, int32_t timeout);
802extern void k_event_send(struct k_event *event);
803
804/**
805 * data transfers (complex)
806 */
807
808/* message queues */
809
810struct k_msgq {
811 _wait_q_t wait_q;
812 uint32_t msg_size;
813 uint32_t max_msgs;
814 char *buffer_start;
815 char *buffer_end;
816 char *read_ptr;
817 char *write_ptr;
818 uint32_t used_msgs;
819
820 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
821};
822
823#define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \
824 { \
825 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
826 .max_msgs = q_depth, \
827 .msg_size = q_width, \
828 .buffer_start = q_buffer, \
829 .buffer_end = q_buffer + (q_depth * q_width), \
830 .read_ptr = q_buffer, \
831 .write_ptr = q_buffer, \
832 .used_msgs = 0, \
833 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
834 }
835
836#define K_MSGQ_DEFINE(name, q_depth, q_width) \
837 static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \
838 struct k_msgq name = \
839 K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name)
840
841#define K_MSGQ_SIZE(q_depth, q_width) \
842 ((sizeof(struct k_msgq)) + ((q_width) * (q_depth)))
843
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400844extern void k_msgq_init(struct k_msgq *q, uint32_t msg_size,
845 uint32_t max_msgs, char *buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400846extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
847extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
848extern void k_msgq_purge(struct k_msgq *q);
849
850static inline int k_msgq_num_used_get(struct k_msgq *q)
851{
852 return q->used_msgs;
853}
854
855struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400856 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400857 void *addr_in_pool;
858 void *data;
859 uint32_t req_size;
860};
861
862/* mailboxes */
863
864struct k_mbox_msg {
865 /** internal use only - needed for legacy API support */
866 uint32_t _mailbox;
867 /** size of message (in bytes) */
868 uint32_t size;
869 /** application-defined information value */
870 uint32_t info;
871 /** sender's message data buffer */
872 void *tx_data;
873 /** internal use only - needed for legacy API support */
874 void *_rx_data;
875 /** message data block descriptor */
876 struct k_mem_block tx_block;
877 /** source thread id */
878 k_tid_t rx_source_thread;
879 /** target thread id */
880 k_tid_t tx_target_thread;
881 /** internal use only - thread waiting on send (may be a dummy) */
882 k_tid_t _syncing_thread;
883#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
884 /** internal use only - semaphore used during asynchronous send */
885 struct k_sem *_async_sem;
886#endif
887};
888
889struct k_mbox {
890 _wait_q_t tx_msg_queue;
891 _wait_q_t rx_msg_queue;
892
893 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
894};
895
896#define K_MBOX_INITIALIZER(obj) \
897 { \
898 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
899 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
900 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
901 }
902
903#define K_MBOX_DEFINE(name) \
904 struct k_mbox name = \
905 K_MBOX_INITIALIZER(name) \
906
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907extern void k_mbox_init(struct k_mbox *mbox);
908
909extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
910 int32_t timeout);
911extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
912 struct k_sem *sem);
913
914extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *msg,
915 void *buffer, int32_t timeout);
916extern void k_mbox_data_get(struct k_mbox_msg *msg, void *buffer);
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400917extern int k_mbox_data_block_get(struct k_mbox_msg *msg,
918 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400919 struct k_mem_block *block, int32_t timeout);
920
921/* pipes */
922
923struct k_pipe {
924 unsigned char *buffer; /* Pipe buffer: may be NULL */
925 size_t size; /* Buffer size */
926 size_t bytes_used; /* # bytes used in buffer */
927 size_t read_index; /* Where in buffer to read from */
928 size_t write_index; /* Where in buffer to write */
929
930 struct {
931 _wait_q_t readers; /* Reader wait queue */
932 _wait_q_t writers; /* Writer wait queue */
933 } wait_q;
934
935 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
936};
937
938#define K_PIPE_INITIALIZER(obj, pipe_buffer_size, pipe_buffer) \
939 { \
940 .buffer = pipe_buffer, \
941 .size = pipe_buffer_size, \
942 .bytes_used = 0, \
943 .read_index = 0, \
944 .write_index = 0, \
945 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
946 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
947 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
948 }
949
950#define K_PIPE_DEFINE(name, pipe_buffer_size) \
951 static unsigned char __noinit _k_pipe_buf_##name[pipe_buffer_size]; \
952 struct k_pipe name = \
953 K_PIPE_INITIALIZER(name, pipe_buffer_size, _k_pipe_buf_##name)
954
955#define K_PIPE_SIZE(buffer_size) (sizeof(struct k_pipe) + buffer_size)
956
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400957/**
958 * @brief Runtime initialization of a pipe
959 *
960 * @param pipe Pointer to pipe to initialize
961 * @param buffer Pointer to buffer to use for pipe's ring buffer
962 * @param size Size of the pipe's ring buffer
963 *
964 * @return N/A
965 */
966extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
967 size_t size);
968
969/**
970 * @brief Put a message into the specified pipe
971 *
972 * This routine synchronously adds a message into the pipe specified by
973 * @a pipe. It will wait up to @a timeout for the pipe to accept
974 * @a num_bytes_to_write bytes of data. If by @a timeout, the pipe could not
975 * accept @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
976 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
977 *
978 * @param pipe Pointer to the pipe
979 * @param buffer Data to put into the pipe
980 * @param num_bytes_to_write Desired number of bytes to put into the pipe
981 * @param num_bytes_written Number of bytes the pipe accepted
982 * @param min_bytes Minimum number of bytes accepted for success
983 * @param timeout Maximum number of milliseconds to wait
984 *
985 * @retval 0 At least @a min_bytes were sent
986 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
987 * @retval -EAGAIN Fewer than @a min_bytes were sent
988 */
989extern int k_pipe_put(struct k_pipe *pipe, void *buffer,
990 size_t num_bytes_to_write, size_t *num_bytes_written,
991 size_t min_bytes, int32_t timeout);
992
993/**
994 * @brief Get a message from the specified pipe
995 *
996 * This routine synchronously retrieves a message from the pipe specified by
997 * @a pipe. It will wait up to @a timeout to retrieve @a num_bytes_to_read
998 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
999 * @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
1000 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1001 *
1002 * @param pipe Pointer to the pipe
1003 * @param buffer Location to place retrieved data
1004 * @param num_bytes_to_read Desired number of bytes to retrieve from the pipe
1005 * @param num_bytes_read Number of bytes retrieved from the pipe
1006 * @param min_bytes Minimum number of bytes retrieved for success
1007 * @param timeout Maximum number of milliseconds to wait
1008 *
1009 * @retval 0 At least @a min_bytes were transferred
1010 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
1011 * @retval -EAGAIN Fewer than @a min_bytes were retrieved
1012 */
1013extern int k_pipe_get(struct k_pipe *pipe, void *buffer,
1014 size_t num_bytes_to_read, size_t *num_bytes_read,
1015 size_t min_bytes, int32_t timeout);
1016
1017/**
1018 * @brief Send a message to the specified pipe
1019 *
1020 * This routine asynchronously sends a message from the pipe specified by
1021 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1022 * free the memory block @a block and give the semaphore @a sem (if specified).
1023 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1024 * at any given time.
1025 *
1026 * @param pipe Pointer to the pipe
1027 * @param block Memory block containing data to send
1028 * @param size Number of data bytes in memory block to send
1029 * @param sem Semaphore to signal upon completion (else NULL)
1030 *
1031 * @retval N/A
1032 */
1033extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1034 size_t size, struct k_sem *sem);
1035
1036/**
1037 * memory management
1038 */
1039
1040/* memory maps */
1041
1042struct k_mem_map {
1043 _wait_q_t wait_q;
1044 int num_blocks;
1045 int block_size;
1046 char *buffer;
1047 char *free_list;
1048 int num_used;
1049
1050 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1051};
1052
1053#define K_MEM_MAP_INITIALIZER(obj, map_num_blocks, map_block_size, \
1054 map_buffer) \
1055 { \
1056 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1057 .num_blocks = map_num_blocks, \
1058 .block_size = map_block_size, \
1059 .buffer = map_buffer, \
1060 .free_list = NULL, \
1061 .num_used = 0, \
1062 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1063 }
1064
1065#define K_MEM_MAP_DEFINE(name, map_num_blocks, map_block_size) \
1066 char _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1067 struct k_mem_map name \
1068 __in_section(_k_mem_map_ptr, private, mem_map) = \
1069 K_MEM_MAP_INITIALIZER(name, map_num_blocks, \
1070 map_block_size, _k_mem_map_buf_##name)
1071
1072#define K_MEM_MAP_SIZE(map_num_blocks, map_block_size) \
1073 (sizeof(struct k_mem_map) + ((map_num_blocks) * (map_block_size)))
1074
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001075extern void k_mem_map_init(struct k_mem_map *map, int num_blocks,
1076 int block_size, void *buffer);
1077extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
1078extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1079
1080static inline int k_mem_map_num_used_get(struct k_mem_map *map)
1081{
1082 return map->num_used;
1083}
1084
1085/* memory pools */
1086
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001087/*
1088 * Memory pool requires a buffer and two arrays of structures for the
1089 * memory block accounting:
1090 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1091 * status of four blocks of memory.
1092 */
1093struct k_mem_pool_quad_block {
1094 char *mem_blocks; /* pointer to the first of four memory blocks */
1095 uint32_t mem_status; /* four bits. If bit is set, memory block is
1096 allocated */
1097};
1098/*
1099 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1100 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1101 * block size is 4 times less than the previous one and thus requires 4 times
1102 * bigger array of k_mem_pool_quad_block structures to keep track of the
1103 * memory blocks.
1104 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001106/*
1107 * The array of k_mem_pool_block_set keeps the information of each array of
1108 * k_mem_pool_quad_block structures
1109 */
1110struct k_mem_pool_block_set {
1111 int block_size; /* memory block size */
1112 int nr_of_entries; /* nr of quad block structures in the array */
1113 struct k_mem_pool_quad_block *quad_block;
1114 int count;
1115};
1116
1117/* Memory pool descriptor */
1118struct k_mem_pool {
1119 int max_block_size;
1120 int min_block_size;
1121 int nr_of_maxblocks;
1122 int nr_of_block_sets;
1123 struct k_mem_pool_block_set *block_set;
1124 char *bufblock;
1125 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001126 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1127};
1128
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001129#ifdef CONFIG_ARM
1130#define _SECTION_TYPE_SIGN "%"
1131#else
1132#define _SECTION_TYPE_SIGN "@"
1133#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001134
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001135/*
1136 * Static memory pool initialization
1137 */
1138/*
1139 * Use .altmacro to be able to recalculate values and pass them as string
1140 * arguments when calling assembler macros resursively
1141 */
1142__asm__(".altmacro\n\t");
1143
1144/*
1145 * Recursively calls a macro
1146 * The followig global symbols need to be initialized:
1147 * __memory_pool_max_block_size - maximal size of the memory block
1148 * __memory_pool_min_block_size - minimal size of the memory block
1149 * Notes:
1150 * Global symbols are used due the fact that assembler macro allows only
1151 * one argument be passed with the % conversion
1152 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1153 * is used instead of / 4.
1154 * n_max argument needs to go first in the invoked macro, as some
1155 * assemblers concatenate \name and %(\n_max * 4) arguments
1156 * if \name goes first
1157 */
1158__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1159 ".ifge __memory_pool_max_block_size >> 2 -"
1160 " __memory_pool_min_block_size\n\t\t"
1161 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1162 "\\macro_name %(\\n_max * 4) \\name\n\t"
1163 ".endif\n\t"
1164 ".endm\n");
1165
1166/*
1167 * Build quad blocks
1168 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1169 * structures and recursively calls itself for the next array, 4 times
1170 * larger.
1171 * The followig global symbols need to be initialized:
1172 * __memory_pool_max_block_size - maximal size of the memory block
1173 * __memory_pool_min_block_size - minimal size of the memory block
1174 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1175 */
1176__asm__(".macro _build_quad_blocks n_max, name\n\t"
1177 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1178 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1179 ".if \\n_max % 4\n\t\t"
1180 ".skip __memory_pool_quad_block_size\n\t"
1181 ".endif\n\t"
1182 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1183 ".endm\n");
1184
1185/*
1186 * Build block sets and initialize them
1187 * Macro initializes the k_mem_pool_block_set structure and
1188 * recursively calls itself for the next one.
1189 * The followig global symbols need to be initialized:
1190 * __memory_pool_max_block_size - maximal size of the memory block
1191 * __memory_pool_min_block_size - minimal size of the memory block
1192 * __memory_pool_block_set_count, the number of the elements in the
1193 * block set array must be set to 0. Macro calculates it's real
1194 * value.
1195 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1196 * structures, _build_quad_blocks must be called prior it.
1197 */
1198__asm__(".macro _build_block_set n_max, name\n\t"
1199 ".int __memory_pool_max_block_size\n\t" /* block_size */
1200 ".if \\n_max % 4\n\t\t"
1201 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1202 ".else\n\t\t"
1203 ".int \\n_max >> 2\n\t"
1204 ".endif\n\t"
1205 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1206 ".int 0\n\t" /* count */
1207 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1208 "__do_recurse _build_block_set \\name \\n_max\n\t"
1209 ".endm\n");
1210
1211/*
1212 * Build a memory pool structure and initialize it
1213 * Macro uses __memory_pool_block_set_count global symbol,
1214 * block set addresses and buffer address, it may be called only after
1215 * _build_block_set
1216 */
1217__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1218 ".pushsection ._k_memory_pool,\"aw\","
1219 _SECTION_TYPE_SIGN "progbits\n\t"
1220 ".globl \\name\n\t"
1221 "\\name:\n\t"
1222 ".int \\max_size\n\t" /* max_block_size */
1223 ".int \\min_size\n\t" /* min_block_size */
1224 ".int \\n_max\n\t" /* nr_of_maxblocks */
1225 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1226 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1227 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1228 ".int 0\n\t" /* wait_q->head */
1229 ".int 0\n\t" /* wait_q->next */
1230 ".popsection\n\t"
1231 ".endm\n");
1232
1233#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1234 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1235 _SECTION_TYPE_SIGN "progbits\n\t"); \
1236 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1237 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1238 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1239 STRINGIFY(name) "\n\t"); \
1240 __asm__(".popsection\n\t")
1241
1242#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1243 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1244 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1245 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1246 _SECTION_TYPE_SIGN "progbits\n\t"); \
1247 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1248 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1249 STRINGIFY(name) "\n\t"); \
1250 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1251 __asm__(".int __memory_pool_block_set_count\n\t"); \
1252 __asm__(".popsection\n\t"); \
1253 extern uint32_t _mem_pool_block_set_count_##name; \
1254 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1255
1256#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max) \
1257 char __noinit _mem_pool_buffer_##name[(max_size) * (n_max)]
1258
1259#define K_MEMORY_POOL_DEFINE(name, min_size, max_size, n_max) \
1260 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1261 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
1262 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max); \
1263 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1264 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1265 extern struct k_mem_pool name
1266
1267/*
1268 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1269 * to __memory_pool_quad_block_size absolute symbol.
1270 * This function does not get called, but compiler calculates the value and
1271 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1272 */
1273static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1274{
1275 __asm__(".globl __memory_pool_quad_block_size\n\t"
1276 "__memory_pool_quad_block_size = %c0\n\t"
1277 :
1278 : "n"(sizeof(struct k_mem_pool_quad_block)));
1279}
1280
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001281#define K_MEM_POOL_SIZE(max_block_size, num_max_blocks) \
1282 (sizeof(struct k_mem_pool) + ((max_block_size) * (num_max_blocks)))
1283
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001284extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285 int size, int32_t timeout);
1286extern void k_mem_pool_free(struct k_mem_block *block);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001287extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001288extern void *k_malloc(uint32_t size);
1289extern void k_free(void *p);
1290
1291/*
1292 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1293 * hook into the device subsystem, which itself uses nanokernel semaphores,
1294 * and thus currently requires the definition of nano_sem.
1295 */
1296#include <legacy.h>
1297#include <arch/cpu.h>
1298
1299/*
1300 * private APIs that are utilized by one or more public APIs
1301 */
1302
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001304extern void _init_static_threads(void);
1305
1306#ifdef __cplusplus
1307}
1308#endif
1309
1310#endif /* _kernel__h_ */