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 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 105 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
| 106 | extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size, |
| 107 | void (*entry)(void *, void *, void*), |
| 108 | void *p1, void *p2, void *p3, |
| 109 | int32_t prio, uint32_t options, int32_t delay); |
| 110 | |
| 111 | extern void k_sleep(int32_t duration); |
| 112 | extern void k_busy_wait(uint32_t usec_to_wait); |
| 113 | extern void k_yield(void); |
| 114 | extern void k_wakeup(k_tid_t thread); |
| 115 | extern k_tid_t k_current_get(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 116 | extern int k_thread_cancel(k_tid_t thread); |
| 117 | |
| 118 | extern void k_thread_abort(k_tid_t thread); |
| 119 | |
| 120 | #define K_THREAD_GROUP_EXE 0x1 |
| 121 | #define K_THREAD_GROUP_SYS 0x2 |
| 122 | #define K_THREAD_GROUP_FPU 0x4 |
| 123 | |
| 124 | /* XXX - doesn't work because CONFIG_ARCH is a string */ |
| 125 | #if 0 |
| 126 | /* arch-specific groups */ |
| 127 | #if CONFIG_ARCH == "x86" |
| 128 | #define K_THREAD_GROUP_SSE 0x4 |
| 129 | #endif |
| 130 | #endif |
| 131 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 132 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 133 | #define _THREAD_TIMEOUT_INIT(obj) \ |
| 134 | (obj).nano_timeout = { \ |
| 135 | .node = { {0}, {0} }, \ |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 136 | .thread = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 137 | .wait_q = NULL, \ |
| 138 | .delta_ticks_from_prev = -1, \ |
| 139 | }, |
| 140 | #else |
| 141 | #define _THREAD_TIMEOUT_INIT(obj) |
| 142 | #endif |
| 143 | |
| 144 | #ifdef CONFIG_ERRNO |
| 145 | #define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0, |
| 146 | #else |
| 147 | #define _THREAD_ERRNO_INIT(obj) |
| 148 | #endif |
| 149 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 150 | struct _static_thread_data { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 151 | uint32_t init_groups; |
| 152 | int init_prio; |
| 153 | void (*init_entry)(void *, void *, void *); |
| 154 | void *init_p1; |
| 155 | void *init_p2; |
| 156 | void *init_p3; |
| 157 | void (*init_abort)(void); |
| 158 | union { |
| 159 | char *init_stack; |
| 160 | struct k_thread *thread; |
| 161 | }; |
| 162 | unsigned int init_stack_size; |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 163 | int32_t init_delay; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 164 | }; |
| 165 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 166 | /* |
| 167 | * Common macro used by both K_THREAD_INITIALIZER() |
| 168 | * and _MDEF_THREAD_INITIALIZER(). |
| 169 | */ |
| 170 | #define _THREAD_INITIALIZER(stack, stack_size, \ |
| 171 | entry, p1, p2, p3, \ |
| 172 | abort, prio) \ |
| 173 | .init_prio = (prio), \ |
| 174 | .init_entry = (void (*)(void *, void *, void *))entry, \ |
| 175 | .init_p1 = (void *)p1, \ |
| 176 | .init_p2 = (void *)p2, \ |
| 177 | .init_p3 = (void *)p3, \ |
| 178 | .init_abort = abort, \ |
| 179 | .init_stack = (stack), \ |
| 180 | .init_stack_size = (stack_size), |
| 181 | |
| 182 | /** |
| 183 | * @brief Thread initializer macro |
| 184 | * |
| 185 | * This macro is to only be used with statically defined threads that were not |
| 186 | * defined in the MDEF file. As such the associated threads can not belong to |
| 187 | * any thread group. |
| 188 | */ |
| 189 | #define K_THREAD_INITIALIZER(stack, stack_size, \ |
| 190 | entry, p1, p2, p3, \ |
| 191 | abort, prio, delay) \ |
| 192 | { \ |
| 193 | _THREAD_INITIALIZER(stack, stack_size, \ |
| 194 | entry, p1, p2, p3, \ |
| 195 | abort, prio) \ |
| 196 | .init_groups = 0, \ |
| 197 | .init_delay = (delay), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 200 | /** |
| 201 | * @brief Thread initializer macro |
| 202 | * |
| 203 | * This macro is to only be used with statically defined threads that were |
| 204 | * defined with legacy APIs (including the MDEF file). As such the associated |
| 205 | * threads may belong to one or more thread groups. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 206 | */ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 207 | #define _MDEF_THREAD_INITIALIZER(stack, stack_size, \ |
| 208 | entry, p1, p2, p3, \ |
| 209 | abort, prio, groups) \ |
| 210 | { \ |
| 211 | _THREAD_INITIALIZER(stack, stack_size, \ |
| 212 | entry, p1, p2, p3, \ |
| 213 | abort, prio) \ |
| 214 | .init_groups = (groups), \ |
| 215 | .init_delay = K_FOREVER, \ |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @brief Define thread initializer and initialize it. |
| 220 | * |
| 221 | * @internal It has been observed that the x86 compiler by default aligns |
| 222 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 223 | * wasting space. To work around this, force a 4-byte alignment. |
| 224 | */ |
| 225 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 226 | entry, p1, p2, p3, \ |
Allan Stephens | 06aefdb | 2016-10-21 15:14:59 -0500 | [diff] [blame] | 227 | prio, options, delay) \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 228 | char __noinit __stack _k_thread_obj_##name[stack_size]; \ |
| 229 | struct _static_thread_data _k_thread_data_##name __aligned(4) \ |
| 230 | __in_section(_k_task_list, private, task) = \ |
| 231 | K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \ |
| 232 | entry, p1, p2, p3, abort, prio, delay) |
| 233 | |
| 234 | /** |
| 235 | * @brief Define thread initializer for MDEF defined thread and initialize it. |
| 236 | * |
| 237 | * @ref K_THREAD_DEFINE |
| 238 | */ |
| 239 | #define _MDEF_THREAD_DEFINE(name, stack_size, \ |
| 240 | entry, p1, p2, p3, \ |
| 241 | abort, prio, groups) \ |
| 242 | char __noinit __stack _k_thread_obj_##name[stack_size]; \ |
| 243 | struct _static_thread_data _k_thread_data_##name __aligned(4) \ |
| 244 | __in_section(_k_task_list, private, task) = \ |
| 245 | _MDEF_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 246 | entry, p1, p2, p3, abort, prio, groups) |
| 247 | |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 248 | extern int k_thread_priority_get(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 249 | extern void k_thread_priority_set(k_tid_t thread, int prio); |
| 250 | |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 251 | extern void k_thread_suspend(k_tid_t thread); |
| 252 | extern void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 253 | |
| 254 | extern void k_sched_time_slice_set(int32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 255 | |
| 256 | extern int k_am_in_isr(void); |
| 257 | |
| 258 | extern void k_thread_custom_data_set(void *value); |
| 259 | extern void *k_thread_custom_data_get(void); |
| 260 | |
| 261 | /** |
| 262 | * kernel timing |
| 263 | */ |
| 264 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 265 | #include <sys_clock.h> |
| 266 | |
| 267 | /* private internal time manipulation (users should never play with ticks) */ |
| 268 | |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 269 | /* added tick needed to account for tick in progress */ |
| 270 | #define _TICK_ALIGN 1 |
| 271 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 272 | static int64_t __ticks_to_ms(int64_t ticks) |
| 273 | { |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 274 | #if CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 275 | return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec; |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 276 | #else |
| 277 | __ASSERT(ticks == 0, ""); |
| 278 | return 0; |
| 279 | #endif |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 283 | /* timeouts */ |
| 284 | |
| 285 | struct _timeout; |
| 286 | typedef void (*_timeout_func_t)(struct _timeout *t); |
| 287 | |
| 288 | struct _timeout { |
| 289 | sys_dlist_t node; |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 290 | struct k_thread *thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 291 | sys_dlist_t *wait_q; |
| 292 | int32_t delta_ticks_from_prev; |
| 293 | _timeout_func_t func; |
| 294 | }; |
| 295 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 296 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 297 | /* timers */ |
| 298 | |
| 299 | struct k_timer { |
| 300 | /* |
| 301 | * _timeout structure must be first here if we want to use |
| 302 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 303 | * list of free timers |
| 304 | */ |
| 305 | struct _timeout timeout; |
| 306 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 307 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 308 | _wait_q_t wait_q; |
| 309 | |
| 310 | /* runs in ISR context */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 311 | void (*expiry_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 312 | |
| 313 | /* runs in the context of the thread that calls k_timer_stop() */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 314 | void (*stop_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 315 | |
| 316 | /* timer period */ |
| 317 | int32_t period; |
| 318 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 319 | /* timer status */ |
| 320 | uint32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 321 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 322 | /* used to support legacy timer APIs */ |
| 323 | void *_legacy_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 324 | |
| 325 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer); |
| 326 | }; |
| 327 | |
| 328 | #define K_TIMER_INITIALIZER(obj) \ |
| 329 | { \ |
| 330 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 331 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 332 | } |
| 333 | |
| 334 | #define K_TIMER_DEFINE(name) \ |
| 335 | struct k_timer name = K_TIMER_INITIALIZER(name) |
| 336 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 337 | /** |
| 338 | * @brief Initialize a timer. |
| 339 | * |
| 340 | * This routine must be called before the timer is used. |
| 341 | * |
| 342 | * @param timer Address of timer. |
| 343 | * @param expiry_fn Function to invoke each time timer expires. |
| 344 | * @param stop_fn Function to invoke if timer is stopped while running. |
| 345 | * |
| 346 | * @return N/A |
| 347 | */ |
| 348 | extern void k_timer_init(struct k_timer *timer, |
| 349 | void (*expiry_fn)(struct k_timer *), |
| 350 | void (*stop_fn)(struct k_timer *)); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 351 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 352 | /** |
| 353 | * @brief Start a timer. |
| 354 | * |
| 355 | * This routine starts a timer, and resets its status to zero. The timer |
| 356 | * begins counting down using the specified duration and period values. |
| 357 | * |
| 358 | * Attempting to start a timer that is already running is permitted. |
| 359 | * The timer's status is reset to zero and the timer begins counting down |
| 360 | * using the new duration and period values. |
| 361 | * |
| 362 | * @param timer Address of timer. |
| 363 | * @param duration Initial timer duration (in milliseconds). |
| 364 | * @param period Timer period (in milliseconds). |
| 365 | * |
| 366 | * @return N/A |
| 367 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 368 | extern void k_timer_start(struct k_timer *timer, |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 369 | int32_t duration, int32_t period); |
| 370 | |
| 371 | /** |
| 372 | * @brief Stop a timer. |
| 373 | * |
| 374 | * This routine stops a running timer prematurely. The timer's stop function, |
| 375 | * if one exists, is invoked by the caller. |
| 376 | * |
| 377 | * Attempting to stop a timer that is not running is permitted, but has no |
| 378 | * effect on the timer since it is already stopped. |
| 379 | * |
| 380 | * @param timer Address of timer. |
| 381 | * |
| 382 | * @return N/A |
| 383 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 384 | extern void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 385 | |
| 386 | /** |
| 387 | * @brief Read timer status. |
| 388 | * |
| 389 | * This routine reads the timer's status, which indicates the number of times |
| 390 | * it has expired since its status was last read. |
| 391 | * |
| 392 | * Calling this routine resets the timer's status to zero. |
| 393 | * |
| 394 | * @param timer Address of timer. |
| 395 | * |
| 396 | * @return Timer status. |
| 397 | */ |
| 398 | extern uint32_t k_timer_status_get(struct k_timer *timer); |
| 399 | |
| 400 | /** |
| 401 | * @brief Synchronize thread to timer expiration. |
| 402 | * |
| 403 | * This routine blocks the calling thread until the timer's status is non-zero |
| 404 | * (indicating that it has expired at least once since it was last examined) |
| 405 | * or the timer is stopped. If the timer status is already non-zero, |
| 406 | * or the timer is already stopped, the caller continues without waiting. |
| 407 | * |
| 408 | * Calling this routine resets the timer's status to zero. |
| 409 | * |
| 410 | * This routine must not be used by interrupt handlers, since they are not |
| 411 | * allowed to block. |
| 412 | * |
| 413 | * @param timer Address of timer. |
| 414 | * |
| 415 | * @return Timer status. |
| 416 | */ |
| 417 | extern uint32_t k_timer_status_sync(struct k_timer *timer); |
| 418 | |
| 419 | /** |
| 420 | * @brief Get timer remaining before next timer expiration. |
| 421 | * |
| 422 | * This routine computes the (approximate) time remaining before a running |
| 423 | * timer next expires. If the timer is not running, it returns zero. |
| 424 | * |
| 425 | * @param timer Address of timer. |
| 426 | * |
| 427 | * @return Remaining time (in milliseconds). |
| 428 | */ |
| 429 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 430 | extern int32_t k_timer_remaining_get(struct k_timer *timer); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 431 | |
| 432 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 433 | /* kernel clocks */ |
| 434 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 435 | /** |
| 436 | * @brief Get the time elapsed since the system booted (uptime) |
| 437 | * |
| 438 | * @return The current uptime of the system in ms |
| 439 | */ |
| 440 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 441 | extern int64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 442 | |
| 443 | /** |
| 444 | * @brief Get the lower 32-bit of time elapsed since the system booted (uptime) |
| 445 | * |
| 446 | * This function is potentially less onerous in both the time it takes to |
| 447 | * execute, the interrupt latency it introduces and the amount of 64-bit math |
| 448 | * it requires than k_uptime_get(), but it only provides an uptime value of |
| 449 | * 32-bits. The user must handle possible rollovers/spillovers. |
| 450 | * |
| 451 | * At a rate of increment of 1000 per second, it rolls over approximately every |
| 452 | * 50 days. |
| 453 | * |
| 454 | * @return The current uptime of the system in ms |
| 455 | */ |
| 456 | |
| 457 | extern uint32_t k_uptime_get_32(void); |
| 458 | |
| 459 | /** |
| 460 | * @brief Get the difference between a reference time and the current uptime |
| 461 | * |
| 462 | * @param reftime A pointer to a reference time. It is updated with the current |
| 463 | * uptime upon return. |
| 464 | * |
| 465 | * @return The delta between the reference time and the current uptime. |
| 466 | */ |
| 467 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 468 | extern int64_t k_uptime_delta(int64_t *reftime); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * @brief Get the difference between a reference time and the current uptime |
| 472 | * |
| 473 | * The 32-bit version of k_uptime_delta(). It has the same perks and issues as |
| 474 | * k_uptime_get_32(). |
| 475 | * |
| 476 | * @param reftime A pointer to a reference time. It is updated with the current |
| 477 | * uptime upon return. |
| 478 | * |
| 479 | * @return The delta between the reference time and the current uptime. |
| 480 | */ |
| 481 | |
| 482 | extern uint32_t k_uptime_delta_32(int64_t *reftime); |
| 483 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 484 | extern uint32_t k_cycle_get_32(void); |
| 485 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 486 | /** |
| 487 | * data transfers (basic) |
| 488 | */ |
| 489 | |
| 490 | /* fifos */ |
| 491 | |
| 492 | struct k_fifo { |
| 493 | _wait_q_t wait_q; |
| 494 | sys_slist_t data_q; |
| 495 | |
| 496 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo); |
| 497 | }; |
| 498 | |
| 499 | extern void k_fifo_init(struct k_fifo *fifo); |
| 500 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
| 501 | extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail); |
| 502 | extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list); |
| 503 | extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); |
| 504 | |
| 505 | #define K_FIFO_INITIALIZER(obj) \ |
| 506 | { \ |
| 507 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 9091e5d | 2016-09-30 10:42:47 -0400 | [diff] [blame] | 508 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 509 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 510 | } |
| 511 | |
| 512 | #define K_FIFO_DEFINE(name) \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 513 | struct k_fifo name = K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 514 | |
| 515 | /* lifos */ |
| 516 | |
| 517 | struct k_lifo { |
| 518 | _wait_q_t wait_q; |
| 519 | void *list; |
| 520 | |
| 521 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo); |
| 522 | }; |
| 523 | |
| 524 | extern void k_lifo_init(struct k_lifo *lifo); |
| 525 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
| 526 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 527 | |
| 528 | #define K_LIFO_INITIALIZER(obj) \ |
| 529 | { \ |
| 530 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 531 | .list = NULL, \ |
| 532 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 533 | } |
| 534 | |
| 535 | #define K_LIFO_DEFINE(name) \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 536 | struct k_lifo name = K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 537 | |
| 538 | /* stacks */ |
| 539 | |
| 540 | struct k_stack { |
| 541 | _wait_q_t wait_q; |
| 542 | uint32_t *base, *next, *top; |
| 543 | |
| 544 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack); |
| 545 | }; |
| 546 | |
Allan Stephens | 018cd9a | 2016-10-07 15:13:24 -0500 | [diff] [blame] | 547 | extern void k_stack_init(struct k_stack *stack, |
| 548 | uint32_t *buffer, int num_entries); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 549 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
| 550 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 551 | |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 552 | #define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 553 | { \ |
| 554 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 555 | .base = stack_buffer, \ |
| 556 | .next = stack_buffer, \ |
| 557 | .top = stack_buffer + stack_num_entries, \ |
| 558 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 559 | } |
| 560 | |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 561 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 562 | uint32_t __noinit \ |
| 563 | _k_stack_buf_##name[stack_num_entries]; \ |
| 564 | struct k_stack name = \ |
| 565 | K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
| 566 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 567 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 568 | /** |
| 569 | * workqueues |
| 570 | */ |
| 571 | |
| 572 | struct k_work; |
| 573 | |
| 574 | typedef void (*k_work_handler_t)(struct k_work *); |
| 575 | |
| 576 | /** |
| 577 | * A workqueue is a fiber that executes @ref k_work items that are |
| 578 | * queued to it. This is useful for drivers which need to schedule |
| 579 | * execution of code which might sleep from ISR context. The actual |
| 580 | * fiber identifier is not stored in the structure in order to save |
| 581 | * space. |
| 582 | */ |
| 583 | struct k_work_q { |
| 584 | struct k_fifo fifo; |
| 585 | }; |
| 586 | |
| 587 | /** |
| 588 | * @brief Work flags. |
| 589 | */ |
| 590 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 591 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | /** |
| 595 | * @brief An item which can be scheduled on a @ref k_work_q. |
| 596 | */ |
| 597 | struct k_work { |
| 598 | void *_reserved; /* Used by k_fifo implementation. */ |
| 599 | k_work_handler_t handler; |
| 600 | atomic_t flags[1]; |
| 601 | }; |
| 602 | |
| 603 | /** |
| 604 | * @brief Statically initialize work item |
| 605 | */ |
| 606 | #define K_WORK_INITIALIZER(work_handler) \ |
| 607 | { \ |
| 608 | ._reserved = NULL, \ |
| 609 | .handler = work_handler, \ |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 610 | .flags = { 0 } \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
| 614 | * @brief Dynamically initialize work item |
| 615 | */ |
| 616 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 617 | { |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 618 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 619 | work->handler = handler; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * @brief Submit a work item to a workqueue. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 624 | * |
| 625 | * This procedure schedules a work item to be processed. |
| 626 | * In the case where the work item has already been submitted and is pending |
| 627 | * execution, calling this function will result in a no-op. In this case, the |
| 628 | * work item must not be modified externally (e.g. by the caller of this |
| 629 | * function), since that could cause the work item to be processed in a |
| 630 | * corrupted state. |
| 631 | * |
| 632 | * @param work_q to schedule the work item |
| 633 | * @param work work item |
| 634 | * |
| 635 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 636 | */ |
| 637 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 638 | struct k_work *work) |
| 639 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 640 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 641 | k_fifo_put(&work_q->fifo, work); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 646 | * @brief Check if work item is pending. |
| 647 | */ |
| 648 | static inline int k_work_pending(struct k_work *work) |
| 649 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 650 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 654 | * @brief Start a new workqueue. This routine can be called from either |
| 655 | * fiber or task context. |
| 656 | */ |
Allan Stephens | 904cf97 | 2016-10-07 13:59:23 -0500 | [diff] [blame] | 657 | extern void k_work_q_start(struct k_work_q *work_q, char *stack, |
| 658 | unsigned stack_size, unsigned prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 659 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 660 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 661 | |
| 662 | /* |
| 663 | * @brief An item which can be scheduled on a @ref k_work_q with a |
| 664 | * delay. |
| 665 | */ |
| 666 | struct k_delayed_work { |
| 667 | struct k_work work; |
| 668 | struct _timeout timeout; |
| 669 | struct k_work_q *work_q; |
| 670 | }; |
| 671 | |
| 672 | /** |
| 673 | * @brief Initialize delayed work |
| 674 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 675 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 676 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 677 | |
| 678 | /** |
| 679 | * @brief Submit a delayed work item to a workqueue. |
| 680 | * |
| 681 | * This procedure schedules a work item to be processed after a delay. |
| 682 | * Once the delay has passed, the work item is submitted to the work queue: |
| 683 | * at this point, it is no longer possible to cancel it. Once the work item's |
| 684 | * handler is about to be executed, the work is considered complete and can be |
| 685 | * resubmitted. |
| 686 | * |
| 687 | * Care must be taken if the handler blocks or yield as there is no implicit |
| 688 | * mutual exclusion mechanism. Such usage is not recommended and if necessary, |
| 689 | * it should be explicitly done between the submitter and the handler. |
| 690 | * |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 691 | * @param work_q Workqueue to schedule the work item |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 692 | * @param work Delayed work item |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 693 | * @param delay Delay before scheduling the work item (in milliseconds) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 694 | * |
| 695 | * @return 0 in case of success or negative value in case of error. |
| 696 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 697 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 698 | struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 699 | int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 700 | |
| 701 | /** |
| 702 | * @brief Cancel a delayed work item |
| 703 | * |
| 704 | * This procedure cancels a scheduled work item. If the work has been completed |
| 705 | * or is idle, this will do nothing. The only case where this can fail is when |
| 706 | * the work has been submitted to the work queue, but the handler has not run |
| 707 | * yet. |
| 708 | * |
| 709 | * @param work Delayed work item to be canceled |
| 710 | * |
| 711 | * @return 0 in case of success or negative value in case of error. |
| 712 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 713 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 714 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 715 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 716 | |
| 717 | #if defined(CONFIG_SYSTEM_WORKQUEUE) |
| 718 | |
| 719 | extern struct k_work_q k_sys_work_q; |
| 720 | |
| 721 | /* |
| 722 | * @brief Submit a work item to the system workqueue. |
| 723 | * |
| 724 | * @ref k_work_submit_to_queue |
| 725 | * |
| 726 | * When using the system workqueue it is not recommended to block or yield |
| 727 | * on the handler since its fiber is shared system wide it may cause |
| 728 | * unexpected behavior. |
| 729 | */ |
| 730 | static inline void k_work_submit(struct k_work *work) |
| 731 | { |
| 732 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 733 | } |
| 734 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 735 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 736 | /* |
| 737 | * @brief Submit a delayed work item to the system workqueue. |
| 738 | * |
| 739 | * @ref k_delayed_work_submit_to_queue |
| 740 | * |
| 741 | * When using the system workqueue it is not recommended to block or yield |
| 742 | * on the handler since its fiber is shared system wide it may cause |
| 743 | * unexpected behavior. |
| 744 | */ |
| 745 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 746 | int32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 747 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 748 | return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 749 | } |
| 750 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 751 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 752 | #endif /* CONFIG_SYSTEM_WORKQUEUE */ |
| 753 | |
| 754 | /** |
| 755 | * synchronization |
| 756 | */ |
| 757 | |
| 758 | /* mutexes */ |
| 759 | |
| 760 | struct k_mutex { |
| 761 | _wait_q_t wait_q; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 762 | struct k_thread *owner; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 763 | uint32_t lock_count; |
| 764 | int owner_orig_prio; |
| 765 | #ifdef CONFIG_OBJECT_MONITOR |
| 766 | int num_lock_state_changes; |
| 767 | int num_conflicts; |
| 768 | #endif |
| 769 | |
| 770 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex); |
| 771 | }; |
| 772 | |
| 773 | #ifdef CONFIG_OBJECT_MONITOR |
| 774 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 775 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 776 | #else |
| 777 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 778 | #endif |
| 779 | |
| 780 | #define K_MUTEX_INITIALIZER(obj) \ |
| 781 | { \ |
| 782 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 783 | .owner = NULL, \ |
| 784 | .lock_count = 0, \ |
| 785 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 786 | _MUTEX_INIT_OBJECT_MONITOR \ |
| 787 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 788 | } |
| 789 | |
| 790 | #define K_MUTEX_DEFINE(name) \ |
| 791 | struct k_mutex name = K_MUTEX_INITIALIZER(name) |
| 792 | |
| 793 | extern void k_mutex_init(struct k_mutex *mutex); |
| 794 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
| 795 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 796 | |
| 797 | /* semaphores */ |
| 798 | |
| 799 | struct k_sem { |
| 800 | _wait_q_t wait_q; |
| 801 | unsigned int count; |
| 802 | unsigned int limit; |
| 803 | |
| 804 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem); |
| 805 | }; |
| 806 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 807 | /** |
| 808 | * @brief Initialize a semaphore object. |
| 809 | * |
| 810 | * An initial count and a count limit can be specified. The count will never go |
| 811 | * over the count limit if the semaphore is given multiple times without being |
| 812 | * taken. |
| 813 | * |
| 814 | * Cannot be called from ISR. |
| 815 | * |
| 816 | * @param sem Pointer to a semaphore object. |
| 817 | * @param initial_count Initial count. |
| 818 | * @param limit Highest value the count can take during operation. |
| 819 | * |
| 820 | * @return N/A |
| 821 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 822 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 823 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 824 | |
| 825 | /** |
| 826 | * @brief Take a semaphore, possibly pending if not available. |
| 827 | * |
| 828 | * The current execution context tries to obtain the semaphore. If the |
| 829 | * semaphore is unavailable and a timeout other than K_NO_WAIT is specified, |
| 830 | * the context will pend. |
| 831 | * |
| 832 | * @param sem Pointer to a semaphore object. |
| 833 | * @param timeout Number of milliseconds to wait if semaphore is unavailable, |
| 834 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 835 | * |
| 836 | * @warning If it is called from the context of an ISR, then the only legal |
| 837 | * value for @a timeout is K_NO_WAIT. |
| 838 | * |
| 839 | * @retval 0 When semaphore is obtained successfully. |
| 840 | * @retval -EAGAIN When timeout expires. |
| 841 | * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT. |
| 842 | * |
| 843 | * @sa K_NO_WAIT, K_FOREVER |
| 844 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 845 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 846 | |
| 847 | /** |
| 848 | * @brief Give a semaphore. |
| 849 | * |
| 850 | * Increase the semaphore's internal count by 1, up to its limit, if no thread |
| 851 | * is waiting on the semaphore; otherwise, wake up the first thread in the |
| 852 | * semaphore's waiting queue. |
| 853 | * |
| 854 | * If the latter case, and if the current context is preemptible, the thread |
| 855 | * that is taken off the wait queue will be scheduled in and will preempt the |
| 856 | * current thread. |
| 857 | * |
| 858 | * @param sem Pointer to a semaphore object. |
| 859 | * |
| 860 | * @return N/A |
| 861 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 862 | extern void k_sem_give(struct k_sem *sem); |
| 863 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 864 | /** |
| 865 | * @brief Reset a semaphore's count to zero. |
| 866 | * |
| 867 | * The only effect is that the count is set to zero. There is no other |
| 868 | * side-effect to calling this function. |
| 869 | * |
| 870 | * @param sem Pointer to a semaphore object. |
| 871 | * |
| 872 | * @return N/A |
| 873 | */ |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame] | 874 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 875 | { |
| 876 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 879 | /** |
| 880 | * @brief Get a semaphore's count. |
| 881 | * |
| 882 | * Note there is no guarantee the count has not changed by the time this |
| 883 | * function returns. |
| 884 | * |
| 885 | * @param sem Pointer to a semaphore object. |
| 886 | * |
| 887 | * @return The current semaphore count. |
| 888 | */ |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 889 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 890 | { |
| 891 | return sem->count; |
| 892 | } |
| 893 | |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 894 | #ifdef CONFIG_SEMAPHORE_GROUPS |
| 895 | /** |
| 896 | * @brief Take the first available semaphore |
| 897 | * |
| 898 | * Given a list of semaphore pointers, this routine will attempt to take one |
| 899 | * of them, waiting up to a maximum of @a timeout ms to do so. The taken |
| 900 | * semaphore is identified by @a sem (set to NULL on error). |
| 901 | * |
| 902 | * Be aware that the more semaphores specified in the group, the more stack |
| 903 | * space is required by the waiting thread. |
| 904 | * |
| 905 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 906 | * @param sem Identifies the semaphore that was taken |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 907 | * @param timeout Number of milliseconds to wait if semaphores are unavailable, |
| 908 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 909 | * |
| 910 | * @retval 0 A semaphore was successfully taken |
| 911 | * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT) |
| 912 | * @retval -EAGAIN Time out occurred while waiting for semaphore |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 913 | * |
| 914 | * @sa K_NO_WAIT, K_FOREVER |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 915 | */ |
| 916 | |
| 917 | extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem, |
| 918 | int32_t timeout); |
| 919 | |
| 920 | /** |
| 921 | * @brief Give all the semaphores in the group |
| 922 | * |
| 923 | * This routine will give each semaphore in the array of semaphore pointers. |
| 924 | * |
| 925 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 926 | * |
| 927 | * @return N/A |
| 928 | */ |
| 929 | extern void k_sem_group_give(struct k_sem *sem_array[]); |
| 930 | |
| 931 | /** |
| 932 | * @brief Reset the count to zero on each semaphore in the array |
| 933 | * |
| 934 | * This routine resets the count of each semaphore in the group to zero. |
| 935 | * Note that it does NOT have any impact on any thread that might have |
| 936 | * been previously pending on any of the semaphores. |
| 937 | * |
| 938 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 939 | * |
| 940 | * @return N/A |
| 941 | */ |
| 942 | extern void k_sem_group_reset(struct k_sem *sem_array[]); |
| 943 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 944 | |
| 945 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 946 | { \ |
| 947 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 948 | .count = initial_count, \ |
| 949 | .limit = count_limit, \ |
| 950 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 951 | } |
| 952 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 953 | /** |
| 954 | * @def K_SEM_DEFINE |
| 955 | * |
| 956 | * @brief Statically define and initialize a global semaphore. |
| 957 | * |
| 958 | * Create a global semaphore named @name. It is initialized as if k_sem_init() |
| 959 | * was called on it. If the semaphore is to be accessed outside the module |
| 960 | * where it is defined, it can be declared via |
| 961 | * |
| 962 | * extern struct k_sem @name; |
| 963 | * |
| 964 | * @param name Name of the semaphore variable. |
| 965 | * @param initial_count Initial count. |
| 966 | * @param count_limit Highest value the count can take during operation. |
| 967 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 968 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
| 969 | struct k_sem name = \ |
| 970 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 971 | |
| 972 | /* events */ |
| 973 | |
| 974 | #define K_EVT_DEFAULT NULL |
| 975 | #define K_EVT_IGNORE ((void *)(-1)) |
| 976 | |
| 977 | typedef int (*k_event_handler_t)(struct k_event *); |
| 978 | |
| 979 | struct k_event { |
| 980 | k_event_handler_t handler; |
| 981 | atomic_t send_count; |
| 982 | struct k_work work_item; |
| 983 | struct k_sem sem; |
| 984 | |
| 985 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event); |
| 986 | }; |
| 987 | |
| 988 | extern void _k_event_deliver(struct k_work *work); |
| 989 | |
| 990 | #define K_EVENT_INITIALIZER(obj, event_handler) \ |
| 991 | { \ |
| 992 | .handler = (k_event_handler_t)event_handler, \ |
| 993 | .send_count = ATOMIC_INIT(0), \ |
| 994 | .work_item = K_WORK_INITIALIZER(_k_event_deliver), \ |
| 995 | .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \ |
| 996 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 997 | } |
| 998 | |
| 999 | #define K_EVENT_DEFINE(name, event_handler) \ |
| 1000 | struct k_event name \ |
| 1001 | __in_section(_k_event_list, event, name) = \ |
| 1002 | K_EVENT_INITIALIZER(name, event_handler) |
| 1003 | |
| 1004 | extern void k_event_init(struct k_event *event, k_event_handler_t handler); |
| 1005 | extern int k_event_recv(struct k_event *event, int32_t timeout); |
| 1006 | extern void k_event_send(struct k_event *event); |
| 1007 | |
| 1008 | /** |
| 1009 | * data transfers (complex) |
| 1010 | */ |
| 1011 | |
| 1012 | /* message queues */ |
| 1013 | |
| 1014 | struct k_msgq { |
| 1015 | _wait_q_t wait_q; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1016 | size_t msg_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1017 | uint32_t max_msgs; |
| 1018 | char *buffer_start; |
| 1019 | char *buffer_end; |
| 1020 | char *read_ptr; |
| 1021 | char *write_ptr; |
| 1022 | uint32_t used_msgs; |
| 1023 | |
| 1024 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); |
| 1025 | }; |
| 1026 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1027 | #define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1028 | { \ |
| 1029 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1030 | .max_msgs = q_max_msgs, \ |
| 1031 | .msg_size = q_msg_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1032 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1033 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1034 | .read_ptr = q_buffer, \ |
| 1035 | .write_ptr = q_buffer, \ |
| 1036 | .used_msgs = 0, \ |
| 1037 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1038 | } |
| 1039 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1040 | /** |
| 1041 | * @brief Define a message queue |
| 1042 | * |
| 1043 | * This declares and initializes a message queue whose buffer is aligned to |
| 1044 | * a @a q_align -byte boundary. The new message queue can be passed to the |
| 1045 | * kernel's message queue functions. |
| 1046 | * |
| 1047 | * Note that for each of the mesages in the message queue to be aligned to |
| 1048 | * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align. |
| 1049 | * |
| 1050 | * @param q_name Name of the message queue |
| 1051 | * @param q_msg_size The size in bytes of each message |
| 1052 | * @param q_max_msgs Maximum number of messages the queue can hold |
| 1053 | * @param q_align Alignment of the message queue's buffer (power of 2) |
| 1054 | */ |
| 1055 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 1056 | static char __noinit __aligned(q_align) \ |
| 1057 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 1058 | struct k_msgq q_name = \ |
| 1059 | K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
| 1060 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1061 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1062 | /** |
| 1063 | * @brief Initialize a message queue. |
| 1064 | * |
| 1065 | * @param q Pointer to the message queue object. |
| 1066 | * @param buffer Pointer to memory area that holds queued messages. |
| 1067 | * @param msg_size Message size, in bytes. |
| 1068 | * @param max_msgs Maximum number of messages that can be queued. |
| 1069 | * |
| 1070 | * @return N/A |
| 1071 | */ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1072 | extern void k_msgq_init(struct k_msgq *q, char *buffer, |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1073 | size_t msg_size, uint32_t max_msgs); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1074 | |
| 1075 | /** |
| 1076 | * @brief Add a message to a message queue. |
| 1077 | * |
| 1078 | * This routine adds an item to the message queue. When the message queue is |
| 1079 | * full, the routine will wait either for space to become available, or until |
| 1080 | * the specified time limit is reached. |
| 1081 | * |
| 1082 | * @param q Pointer to the message queue object. |
| 1083 | * @param data Pointer to message data area. |
| 1084 | * @param timeout Number of milliseconds to wait until space becomes available |
| 1085 | * to add the message into the message queue, or one of the |
| 1086 | * special values K_NO_WAIT and K_FOREVER. |
| 1087 | * |
| 1088 | * @return 0 if successful, -ENOMSG if failed immediately or after queue purge, |
| 1089 | * -EAGAIN if timed out |
| 1090 | * |
| 1091 | * @sa K_NO_WAIT, K_FOREVER |
| 1092 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1093 | extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1094 | |
| 1095 | /** |
| 1096 | * @brief Obtain a message from a message queue. |
| 1097 | * |
| 1098 | * This routine fetches the oldest item from the message queue. When the message |
| 1099 | * queue is found empty, the routine will wait either until an item is added to |
| 1100 | * the message queue or until the specified time limit is reached. |
| 1101 | * |
| 1102 | * @param q Pointer to the message queue object. |
| 1103 | * @param data Pointer to message data area. |
| 1104 | * @param timeout Number of milliseconds to wait to obtain message, or one of |
| 1105 | * the special values K_NO_WAIT and K_FOREVER. |
| 1106 | * |
| 1107 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 1108 | * |
| 1109 | * @sa K_NO_WAIT, K_FOREVER |
| 1110 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1111 | extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1112 | |
| 1113 | /** |
| 1114 | * @brief Purge contents of a message queue. |
| 1115 | * |
| 1116 | * Discards all messages currently in the message queue, and cancels |
| 1117 | * any "add message" operations initiated by waiting threads. |
| 1118 | * |
| 1119 | * @param q Pointer to the message queue object. |
| 1120 | * |
| 1121 | * @return N/A |
| 1122 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1123 | extern void k_msgq_purge(struct k_msgq *q); |
| 1124 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1125 | /** |
| 1126 | * @brief Get the number of unused messages |
| 1127 | * |
| 1128 | * @param q Message queue to query |
| 1129 | * |
| 1130 | * @return Number of unused messages |
| 1131 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1132 | static inline uint32_t k_msgq_num_free_get(struct k_msgq *q) |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1133 | { |
| 1134 | return q->max_msgs - q->used_msgs; |
| 1135 | } |
| 1136 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1137 | /** |
| 1138 | * @brief Get the number of used messages |
| 1139 | * |
| 1140 | * @param q Message queue to query |
| 1141 | * |
| 1142 | * @return Number of used messages |
| 1143 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1144 | static inline uint32_t k_msgq_num_used_get(struct k_msgq *q) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1145 | { |
| 1146 | return q->used_msgs; |
| 1147 | } |
| 1148 | |
| 1149 | struct k_mem_block { |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 1150 | struct k_mem_pool *pool_id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1151 | void *addr_in_pool; |
| 1152 | void *data; |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1153 | size_t req_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1154 | }; |
| 1155 | |
| 1156 | /* mailboxes */ |
| 1157 | |
| 1158 | struct k_mbox_msg { |
| 1159 | /** internal use only - needed for legacy API support */ |
| 1160 | uint32_t _mailbox; |
| 1161 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 1162 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1163 | /** application-defined information value */ |
| 1164 | uint32_t info; |
| 1165 | /** sender's message data buffer */ |
| 1166 | void *tx_data; |
| 1167 | /** internal use only - needed for legacy API support */ |
| 1168 | void *_rx_data; |
| 1169 | /** message data block descriptor */ |
| 1170 | struct k_mem_block tx_block; |
| 1171 | /** source thread id */ |
| 1172 | k_tid_t rx_source_thread; |
| 1173 | /** target thread id */ |
| 1174 | k_tid_t tx_target_thread; |
| 1175 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 1176 | k_tid_t _syncing_thread; |
| 1177 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1178 | /** internal use only - semaphore used during asynchronous send */ |
| 1179 | struct k_sem *_async_sem; |
| 1180 | #endif |
| 1181 | }; |
| 1182 | |
| 1183 | struct k_mbox { |
| 1184 | _wait_q_t tx_msg_queue; |
| 1185 | _wait_q_t rx_msg_queue; |
| 1186 | |
| 1187 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox); |
| 1188 | }; |
| 1189 | |
| 1190 | #define K_MBOX_INITIALIZER(obj) \ |
| 1191 | { \ |
| 1192 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 1193 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
| 1194 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1195 | } |
| 1196 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1197 | /** |
| 1198 | * @brief Define a mailbox |
| 1199 | * |
| 1200 | * This declares and initializes a mailbox. The new mailbox can be passed to |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1201 | * the kernel's mailbox functions. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1202 | * |
| 1203 | * @param name Name of the mailbox |
| 1204 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1205 | #define K_MBOX_DEFINE(name) \ |
| 1206 | struct k_mbox name = \ |
| 1207 | K_MBOX_INITIALIZER(name) \ |
| 1208 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1209 | /** |
| 1210 | * @brief Initialize a mailbox. |
| 1211 | * |
| 1212 | * @param mbox Pointer to the mailbox object |
| 1213 | * |
| 1214 | * @return N/A |
| 1215 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1216 | extern void k_mbox_init(struct k_mbox *mbox); |
| 1217 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1218 | /** |
| 1219 | * @brief Send a mailbox message in a synchronous manner. |
| 1220 | * |
| 1221 | * Sends a message to a mailbox and waits for a receiver to process it. |
| 1222 | * The message data may be in a buffer, in a memory pool block, or non-existent |
| 1223 | * (i.e. empty message). |
| 1224 | * |
| 1225 | * @param mbox Pointer to the mailbox object. |
| 1226 | * @param tx_msg Pointer to transmit message descriptor. |
| 1227 | * @param timeout Maximum time (milliseconds) to wait for the message to be |
| 1228 | * received (although not necessarily completely processed). |
| 1229 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long |
| 1230 | * as necessary. |
| 1231 | * |
| 1232 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 1233 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1234 | 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] | 1235 | int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1236 | |
| 1237 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1238 | /** |
| 1239 | * @brief Send a mailbox message in an asynchronous manner. |
| 1240 | * |
| 1241 | * Sends a message to a mailbox without waiting for a receiver to process it. |
| 1242 | * The message data may be in a buffer, in a memory pool block, or non-existent |
| 1243 | * (i.e. an empty message). Optionally, the specified semaphore will be given |
| 1244 | * by the mailbox when the message has been both received and disposed of |
| 1245 | * by the receiver. |
| 1246 | * |
| 1247 | * @param mbox Pointer to the mailbox object. |
| 1248 | * @param tx_msg Pointer to transmit message descriptor. |
| 1249 | * @param sem Semaphore identifier, or NULL if none specified. |
| 1250 | * |
| 1251 | * @return N/A |
| 1252 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1253 | 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] | 1254 | struct k_sem *sem); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1255 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1256 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1257 | /** |
| 1258 | * @brief Receive a mailbox message. |
| 1259 | * |
| 1260 | * Receives a message from a mailbox, then optionally retrieves its data |
| 1261 | * and disposes of the message. |
| 1262 | * |
| 1263 | * @param mbox Pointer to the mailbox object. |
| 1264 | * @param rx_msg Pointer to receive message descriptor. |
| 1265 | * @param buffer Pointer to buffer to receive data. |
| 1266 | * (Use NULL to defer data retrieval and message disposal until later.) |
| 1267 | * @param timeout Maximum time (milliseconds) to wait for a message. |
| 1268 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as |
| 1269 | * necessary. |
| 1270 | * |
| 1271 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 1272 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1273 | 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] | 1274 | void *buffer, int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1275 | |
| 1276 | /** |
| 1277 | * @brief Retrieve mailbox message data into a buffer. |
| 1278 | * |
| 1279 | * Completes the processing of a received message by retrieving its data |
| 1280 | * into a buffer, then disposing of the message. |
| 1281 | * |
| 1282 | * Alternatively, this routine can be used to dispose of a received message |
| 1283 | * without retrieving its data. |
| 1284 | * |
| 1285 | * @param rx_msg Pointer to receive message descriptor. |
| 1286 | * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.) |
| 1287 | * |
| 1288 | * @return N/A |
| 1289 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1290 | 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] | 1291 | |
| 1292 | /** |
| 1293 | * @brief Retrieve mailbox message data into a memory pool block. |
| 1294 | * |
| 1295 | * Completes the processing of a received message by retrieving its data |
| 1296 | * into a memory pool block, then disposing of the message. The memory pool |
| 1297 | * block that results from successful retrieval must be returned to the pool |
| 1298 | * once the data has been processed, even in cases where zero bytes of data |
| 1299 | * are retrieved. |
| 1300 | * |
| 1301 | * Alternatively, this routine can be used to dispose of a received message |
| 1302 | * without retrieving its data. In this case there is no need to return a |
| 1303 | * memory pool block to the pool. |
| 1304 | * |
| 1305 | * This routine allocates a new memory pool block for the data only if the |
| 1306 | * data is not already in one. If a new block cannot be allocated, the routine |
| 1307 | * returns a failure code and the received message is left unchanged. This |
| 1308 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 1309 | * of the received message without retrieving its data. |
| 1310 | * |
| 1311 | * @param rx_msg Pointer to receive message descriptor. |
| 1312 | * @param pool Memory pool identifier. (Use NULL to discard data.) |
| 1313 | * @param block Pointer to area to hold memory pool block info. |
| 1314 | * @param timeout Maximum time (milliseconds) to wait for a memory pool block. |
| 1315 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as |
| 1316 | * necessary. |
| 1317 | * |
| 1318 | * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out |
| 1319 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1320 | 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] | 1321 | struct k_mem_pool *pool, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1322 | struct k_mem_block *block, int32_t timeout); |
| 1323 | |
| 1324 | /* pipes */ |
| 1325 | |
| 1326 | struct k_pipe { |
| 1327 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 1328 | size_t size; /* Buffer size */ |
| 1329 | size_t bytes_used; /* # bytes used in buffer */ |
| 1330 | size_t read_index; /* Where in buffer to read from */ |
| 1331 | size_t write_index; /* Where in buffer to write */ |
| 1332 | |
| 1333 | struct { |
| 1334 | _wait_q_t readers; /* Reader wait queue */ |
| 1335 | _wait_q_t writers; /* Writer wait queue */ |
| 1336 | } wait_q; |
| 1337 | |
| 1338 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe); |
| 1339 | }; |
| 1340 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1341 | #define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1342 | { \ |
| 1343 | .buffer = pipe_buffer, \ |
| 1344 | .size = pipe_buffer_size, \ |
| 1345 | .bytes_used = 0, \ |
| 1346 | .read_index = 0, \ |
| 1347 | .write_index = 0, \ |
| 1348 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 1349 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
| 1350 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1351 | } |
| 1352 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1353 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
| 1354 | static unsigned char __noinit __aligned(pipe_align) \ |
| 1355 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1356 | struct k_pipe name = \ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1357 | K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1358 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1359 | /** |
| 1360 | * @brief Runtime initialization of a pipe |
| 1361 | * |
| 1362 | * @param pipe Pointer to pipe to initialize |
| 1363 | * @param buffer Pointer to buffer to use for pipe's ring buffer |
| 1364 | * @param size Size of the pipe's ring buffer |
| 1365 | * |
| 1366 | * @return N/A |
| 1367 | */ |
| 1368 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 1369 | size_t size); |
| 1370 | |
| 1371 | /** |
| 1372 | * @brief Put a message into the specified pipe |
| 1373 | * |
| 1374 | * This routine synchronously adds a message into the pipe specified by |
| 1375 | * @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] | 1376 | * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not |
| 1377 | * 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] | 1378 | * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER. |
| 1379 | * |
| 1380 | * @param pipe Pointer to the pipe |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1381 | * @param data Data to put into the pipe |
| 1382 | * @param bytes_to_write Desired number of bytes to put into the pipe |
| 1383 | * @param bytes_written Number of bytes the pipe accepted |
| 1384 | * @param min_xfer Minimum number of bytes accepted for success |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1385 | * @param timeout Maximum number of milliseconds to wait |
| 1386 | * |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1387 | * @retval 0 At least @a min_xfer were sent |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1388 | * @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] | 1389 | * @retval -EAGAIN Fewer than @a min_xfer were sent |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1390 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1391 | extern int k_pipe_put(struct k_pipe *pipe, void *data, |
| 1392 | size_t bytes_to_write, size_t *bytes_written, |
| 1393 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1394 | |
| 1395 | /** |
| 1396 | * @brief Get a message from the specified pipe |
| 1397 | * |
| 1398 | * This routine synchronously retrieves a message from the pipe specified by |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1399 | * @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] | 1400 | * 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] | 1401 | * @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] | 1402 | * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER. |
| 1403 | * |
| 1404 | * @param pipe Pointer to the pipe |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1405 | * @param data Location to place retrieved data |
| 1406 | * @param bytes_to_read Desired number of bytes to retrieve from the pipe |
| 1407 | * @param bytes_read Number of bytes retrieved from the pipe |
| 1408 | * @param min_xfer Minimum number of bytes retrieved for success |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1409 | * @param timeout Maximum number of milliseconds to wait |
| 1410 | * |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1411 | * @retval 0 At least @a min_xfer were transferred |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1412 | * @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] | 1413 | * @retval -EAGAIN Fewer than @a min_xfer were retrieved |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1414 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1415 | extern int k_pipe_get(struct k_pipe *pipe, void *data, |
| 1416 | size_t bytes_to_read, size_t *bytes_read, |
| 1417 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1418 | |
Peter Mitsis | 2fef023 | 2016-10-14 14:53:44 -0400 | [diff] [blame] | 1419 | #if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1420 | /** |
| 1421 | * @brief Send a message to the specified pipe |
| 1422 | * |
| 1423 | * This routine asynchronously sends a message from the pipe specified by |
| 1424 | * @a pipe. Once all @a size bytes have been accepted by the pipe, it will |
| 1425 | * free the memory block @a block and give the semaphore @a sem (if specified). |
| 1426 | * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight |
| 1427 | * at any given time. |
| 1428 | * |
| 1429 | * @param pipe Pointer to the pipe |
| 1430 | * @param block Memory block containing data to send |
| 1431 | * @param size Number of data bytes in memory block to send |
| 1432 | * @param sem Semaphore to signal upon completion (else NULL) |
| 1433 | * |
| 1434 | * @retval N/A |
| 1435 | */ |
| 1436 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 1437 | size_t size, struct k_sem *sem); |
Peter Mitsis | 2fef023 | 2016-10-14 14:53:44 -0400 | [diff] [blame] | 1438 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1439 | |
| 1440 | /** |
| 1441 | * memory management |
| 1442 | */ |
| 1443 | |
| 1444 | /* memory maps */ |
| 1445 | |
| 1446 | struct k_mem_map { |
| 1447 | _wait_q_t wait_q; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1448 | uint32_t num_blocks; |
| 1449 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1450 | char *buffer; |
| 1451 | char *free_list; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1452 | uint32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1453 | |
| 1454 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map); |
| 1455 | }; |
| 1456 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1457 | #define K_MEM_MAP_INITIALIZER(obj, map_buffer, map_block_size, map_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1458 | { \ |
| 1459 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1460 | .num_blocks = map_num_blocks, \ |
| 1461 | .block_size = map_block_size, \ |
| 1462 | .buffer = map_buffer, \ |
| 1463 | .free_list = NULL, \ |
| 1464 | .num_used = 0, \ |
| 1465 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1466 | } |
| 1467 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1468 | /** |
| 1469 | * @brief Define a memory map |
| 1470 | * |
| 1471 | * This declares and initializes a memory map whose buffer is aligned to |
| 1472 | * a @a map_align -byte boundary. The new memory map can be passed to the |
| 1473 | * kernel's memory map functions. |
| 1474 | * |
| 1475 | * Note that for each of the blocks in the memory map to be aligned to |
| 1476 | * @a map_align bytes, then @a map_block_size must be a multiple of |
| 1477 | * @a map_align. |
| 1478 | * |
| 1479 | * @param name Name of the memory map |
| 1480 | * @param map_block_size Size of each block in the buffer (in bytes) |
| 1481 | * @param map_num_blocks Number blocks in the buffer |
| 1482 | * @param map_align Alignment of the memory map's buffer (power of 2) |
| 1483 | */ |
| 1484 | #define K_MEM_MAP_DEFINE(name, map_block_size, map_num_blocks, map_align) \ |
Allan Stephens | 35ffaff | 2016-10-21 14:31:37 -0500 | [diff] [blame] | 1485 | char __noinit __aligned(map_align) \ |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1486 | _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \ |
| 1487 | struct k_mem_map name \ |
| 1488 | __in_section(_k_mem_map_ptr, private, mem_map) = \ |
| 1489 | K_MEM_MAP_INITIALIZER(name, _k_mem_map_buf_##name, \ |
| 1490 | map_block_size, map_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1491 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1492 | /** |
| 1493 | * @brief Initialize a memory map. |
| 1494 | * |
| 1495 | * Initializes the memory map and creates its list of free blocks. |
| 1496 | * |
| 1497 | * @param map Pointer to the memory map object |
| 1498 | * @param buffer Pointer to buffer used for the blocks. |
| 1499 | * @param block_size Size of each block, in bytes. |
| 1500 | * @param num_blocks Number of blocks. |
| 1501 | * |
| 1502 | * @return N/A |
| 1503 | */ |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1504 | extern void k_mem_map_init(struct k_mem_map *map, void *buffer, |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1505 | size_t block_size, uint32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1506 | |
| 1507 | /** |
| 1508 | * @brief Allocate a memory map block. |
| 1509 | * |
| 1510 | * Takes a block from the list of unused blocks. |
| 1511 | * |
| 1512 | * @param map Pointer to memory map object. |
| 1513 | * @param mem Pointer to area to receive block address. |
| 1514 | * @param timeout Maximum time (milliseconds) to wait for allocation to |
| 1515 | * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait |
| 1516 | * as long as necessary. |
| 1517 | * |
| 1518 | * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out |
| 1519 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1520 | extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1521 | |
| 1522 | /** |
| 1523 | * @brief Free a memory map block. |
| 1524 | * |
| 1525 | * Gives block to a waiting thread if there is one, otherwise returns it to |
| 1526 | * the list of unused blocks. |
| 1527 | * |
| 1528 | * @param map Pointer to memory map object. |
| 1529 | * @param mem Pointer to area to containing block address. |
| 1530 | * |
| 1531 | * @return N/A |
| 1532 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1533 | extern void k_mem_map_free(struct k_mem_map *map, void **mem); |
| 1534 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1535 | /** |
| 1536 | * @brief Get the number of used memory blocks |
| 1537 | * |
| 1538 | * This routine gets the current number of used memory blocks in the |
| 1539 | * specified pool. It should be used for stats purposes only as that |
| 1540 | * value may potentially be out-of-date by the time it is used. |
| 1541 | * |
| 1542 | * @param map Memory map to query |
| 1543 | * |
| 1544 | * @return Number of used memory blocks |
| 1545 | */ |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1546 | static inline uint32_t k_mem_map_num_used_get(struct k_mem_map *map) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1547 | { |
| 1548 | return map->num_used; |
| 1549 | } |
| 1550 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 1551 | /** |
| 1552 | * @brief Get the number of unused memory blocks |
| 1553 | * |
| 1554 | * This routine gets the current number of unused memory blocks in the |
| 1555 | * specified pool. It should be used for stats purposes only as that value |
| 1556 | * may potentially be out-of-date by the time it is used. |
| 1557 | * |
| 1558 | * @param map Memory map to query |
| 1559 | * |
| 1560 | * @return Number of unused memory blocks |
| 1561 | */ |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1562 | static inline uint32_t k_mem_map_num_free_get(struct k_mem_map *map) |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 1563 | { |
| 1564 | return map->num_blocks - map->num_used; |
| 1565 | } |
| 1566 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1567 | /* memory pools */ |
| 1568 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1569 | /* |
| 1570 | * Memory pool requires a buffer and two arrays of structures for the |
| 1571 | * memory block accounting: |
| 1572 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 1573 | * status of four blocks of memory. |
| 1574 | */ |
| 1575 | struct k_mem_pool_quad_block { |
| 1576 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 1577 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 1578 | allocated */ |
| 1579 | }; |
| 1580 | /* |
| 1581 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 1582 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 1583 | * block size is 4 times less than the previous one and thus requires 4 times |
| 1584 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 1585 | * memory blocks. |
| 1586 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1587 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1588 | /* |
| 1589 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 1590 | * k_mem_pool_quad_block structures |
| 1591 | */ |
| 1592 | struct k_mem_pool_block_set { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1593 | size_t block_size; /* memory block size */ |
| 1594 | uint32_t nr_of_entries; /* nr of quad block structures in the array */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1595 | struct k_mem_pool_quad_block *quad_block; |
| 1596 | int count; |
| 1597 | }; |
| 1598 | |
| 1599 | /* Memory pool descriptor */ |
| 1600 | struct k_mem_pool { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1601 | size_t max_block_size; |
| 1602 | size_t min_block_size; |
| 1603 | uint32_t nr_of_maxblocks; |
| 1604 | uint32_t nr_of_block_sets; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1605 | struct k_mem_pool_block_set *block_set; |
| 1606 | char *bufblock; |
| 1607 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1608 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool); |
| 1609 | }; |
| 1610 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1611 | #ifdef CONFIG_ARM |
| 1612 | #define _SECTION_TYPE_SIGN "%" |
| 1613 | #else |
| 1614 | #define _SECTION_TYPE_SIGN "@" |
| 1615 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1616 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1617 | /* |
| 1618 | * Static memory pool initialization |
| 1619 | */ |
| 1620 | /* |
| 1621 | * Use .altmacro to be able to recalculate values and pass them as string |
| 1622 | * arguments when calling assembler macros resursively |
| 1623 | */ |
| 1624 | __asm__(".altmacro\n\t"); |
| 1625 | |
| 1626 | /* |
| 1627 | * Recursively calls a macro |
| 1628 | * The followig global symbols need to be initialized: |
| 1629 | * __memory_pool_max_block_size - maximal size of the memory block |
| 1630 | * __memory_pool_min_block_size - minimal size of the memory block |
| 1631 | * Notes: |
| 1632 | * Global symbols are used due the fact that assembler macro allows only |
| 1633 | * one argument be passed with the % conversion |
| 1634 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 1635 | * is used instead of / 4. |
| 1636 | * n_max argument needs to go first in the invoked macro, as some |
| 1637 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 1638 | * if \name goes first |
| 1639 | */ |
| 1640 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 1641 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 1642 | " __memory_pool_min_block_size\n\t\t" |
| 1643 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 1644 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 1645 | ".endif\n\t" |
| 1646 | ".endm\n"); |
| 1647 | |
| 1648 | /* |
| 1649 | * Build quad blocks |
| 1650 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 1651 | * structures and recursively calls itself for the next array, 4 times |
| 1652 | * larger. |
| 1653 | * The followig global symbols need to be initialized: |
| 1654 | * __memory_pool_max_block_size - maximal size of the memory block |
| 1655 | * __memory_pool_min_block_size - minimal size of the memory block |
| 1656 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 1657 | */ |
| 1658 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 1659 | ".balign 4\n\t" |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1660 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 1661 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 1662 | ".if \\n_max % 4\n\t\t" |
| 1663 | ".skip __memory_pool_quad_block_size\n\t" |
| 1664 | ".endif\n\t" |
| 1665 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 1666 | ".endm\n"); |
| 1667 | |
| 1668 | /* |
| 1669 | * Build block sets and initialize them |
| 1670 | * Macro initializes the k_mem_pool_block_set structure and |
| 1671 | * recursively calls itself for the next one. |
| 1672 | * The followig global symbols need to be initialized: |
| 1673 | * __memory_pool_max_block_size - maximal size of the memory block |
| 1674 | * __memory_pool_min_block_size - minimal size of the memory block |
| 1675 | * __memory_pool_block_set_count, the number of the elements in the |
| 1676 | * block set array must be set to 0. Macro calculates it's real |
| 1677 | * value. |
| 1678 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 1679 | * structures, _build_quad_blocks must be called prior it. |
| 1680 | */ |
| 1681 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 1682 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 1683 | ".if \\n_max % 4\n\t\t" |
| 1684 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 1685 | ".else\n\t\t" |
| 1686 | ".int \\n_max >> 2\n\t" |
| 1687 | ".endif\n\t" |
| 1688 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 1689 | ".int 0\n\t" /* count */ |
| 1690 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 1691 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 1692 | ".endm\n"); |
| 1693 | |
| 1694 | /* |
| 1695 | * Build a memory pool structure and initialize it |
| 1696 | * Macro uses __memory_pool_block_set_count global symbol, |
| 1697 | * block set addresses and buffer address, it may be called only after |
| 1698 | * _build_block_set |
| 1699 | */ |
| 1700 | __asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t" |
| 1701 | ".pushsection ._k_memory_pool,\"aw\"," |
| 1702 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 1703 | ".globl \\name\n\t" |
| 1704 | "\\name:\n\t" |
| 1705 | ".int \\max_size\n\t" /* max_block_size */ |
| 1706 | ".int \\min_size\n\t" /* min_block_size */ |
| 1707 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 1708 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 1709 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 1710 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 1711 | ".int 0\n\t" /* wait_q->head */ |
| 1712 | ".int 0\n\t" /* wait_q->next */ |
| 1713 | ".popsection\n\t" |
| 1714 | ".endm\n"); |
| 1715 | |
| 1716 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 1717 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 1718 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 1719 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 1720 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 1721 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 1722 | STRINGIFY(name) "\n\t"); \ |
| 1723 | __asm__(".popsection\n\t") |
| 1724 | |
| 1725 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 1726 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 1727 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 1728 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 1729 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 1730 | __asm__(".balign 4\n\t"); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1731 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 1732 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 1733 | STRINGIFY(name) "\n\t"); \ |
| 1734 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 1735 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 1736 | __asm__(".popsection\n\t"); \ |
| 1737 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 1738 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 1739 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 1740 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \ |
| 1741 | char __noinit __aligned(align) \ |
| 1742 | _mem_pool_buffer_##name[(max_size) * (n_max)] |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1743 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 1744 | /** |
| 1745 | * @brief Define a memory pool |
| 1746 | * |
| 1747 | * This declares and initializes a memory pool whose buffer is aligned to |
| 1748 | * a @a align -byte boundary. The new memory pool can be passed to the |
| 1749 | * kernel's memory pool functions. |
| 1750 | * |
| 1751 | * Note that for each of the minimum sized blocks to be aligned to @a align |
| 1752 | * bytes, then @a min_size must be a multiple of @a align. |
| 1753 | * |
| 1754 | * @param name Name of the memory pool |
| 1755 | * @param min_size Minimum block size in the pool |
| 1756 | * @param max_size Maximum block size in the pool |
| 1757 | * @param n_max Number of maximum sized blocks in the pool |
| 1758 | * @param align Alignment of the memory pool's buffer |
| 1759 | */ |
| 1760 | #define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1761 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 1762 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 1763 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1764 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 1765 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 1766 | extern struct k_mem_pool name |
| 1767 | |
| 1768 | /* |
| 1769 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 1770 | * to __memory_pool_quad_block_size absolute symbol. |
| 1771 | * This function does not get called, but compiler calculates the value and |
| 1772 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 1773 | */ |
| 1774 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 1775 | { |
| 1776 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
Andrew Boie | 431607c | 2016-10-25 11:47:52 -0700 | [diff] [blame^] | 1777 | #ifdef CONFIG_NIOS2 |
| 1778 | "__memory_pool_quad_block_size = %0\n\t" |
| 1779 | #else |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1780 | "__memory_pool_quad_block_size = %c0\n\t" |
Andrew Boie | 431607c | 2016-10-25 11:47:52 -0700 | [diff] [blame^] | 1781 | #endif |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1782 | : |
| 1783 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 1784 | } |
| 1785 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1786 | /** |
| 1787 | * @brief Allocate memory from a memory pool |
| 1788 | * |
| 1789 | * @param pool Pointer to the memory pool object |
| 1790 | * @param block Pointer to the allocated memory's block descriptor |
| 1791 | * @param size Minimum number of bytes to allocate |
| 1792 | * @param timeout Maximum time (milliseconds) to wait for operation to |
| 1793 | * complete. Use K_NO_WAIT to return immediately, or K_FOREVER |
| 1794 | * to wait as long as necessary. |
| 1795 | * |
| 1796 | * @return 0 on success, -ENOMEM on failure |
| 1797 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1798 | extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block, |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1799 | size_t size, int32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1800 | |
| 1801 | /** |
| 1802 | * @brief Return previously allocated memory to its memory pool |
| 1803 | * |
| 1804 | * @param block Pointer to allocated memory's block descriptor |
| 1805 | * |
| 1806 | * @return N/A |
| 1807 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1808 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1809 | |
| 1810 | /** |
| 1811 | * @brief Defragment the specified memory pool |
| 1812 | * |
| 1813 | * @param pool Pointer to the memory pool object |
| 1814 | * |
| 1815 | * @return N/A |
| 1816 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1817 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1818 | |
| 1819 | /** |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 1820 | * @brief Allocate memory from heap |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1821 | * |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 1822 | * This routine provides traditional malloc() semantics. The memory is |
| 1823 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1824 | * |
| 1825 | * @param size Size of memory requested by the caller (in bytes) |
| 1826 | * |
| 1827 | * @return Address of the allocated memory on success; otherwise NULL |
| 1828 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1829 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1830 | |
| 1831 | /** |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 1832 | * @brief Free memory allocated from heap |
| 1833 | * |
| 1834 | * This routine provides traditional free() semantics. The memory being |
| 1835 | * returned must have been allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 1836 | * |
| 1837 | * @param ptr Pointer to previously allocated memory |
| 1838 | * |
| 1839 | * @return N/A |
| 1840 | */ |
| 1841 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1842 | |
| 1843 | /* |
| 1844 | * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to |
| 1845 | * hook into the device subsystem, which itself uses nanokernel semaphores, |
| 1846 | * and thus currently requires the definition of nano_sem. |
| 1847 | */ |
| 1848 | #include <legacy.h> |
| 1849 | #include <arch/cpu.h> |
| 1850 | |
| 1851 | /* |
| 1852 | * private APIs that are utilized by one or more public APIs |
| 1853 | */ |
| 1854 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1855 | extern int _is_thread_essential(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1856 | extern void _init_static_threads(void); |
| 1857 | |
| 1858 | #ifdef __cplusplus |
| 1859 | } |
| 1860 | #endif |
| 1861 | |
| 1862 | #endif /* _kernel__h_ */ |