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