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