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 | |
Benjamin Walsh | fab8d92 | 2016-11-08 15:36:36 -0500 | [diff] [blame] | 67 | #define K_IDLE_PRIO K_LOWEST_THREAD_PRIO |
| 68 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 69 | #define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO) |
| 70 | #define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1) |
| 71 | |
| 72 | typedef sys_dlist_t _wait_q_t; |
| 73 | |
| 74 | #ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS |
| 75 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next |
| 76 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL, |
| 77 | #else |
| 78 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT |
| 79 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) |
| 80 | #endif |
| 81 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 82 | #define tcs k_thread |
| 83 | struct k_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 84 | struct k_mutex; |
| 85 | struct k_sem; |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 86 | struct k_alert; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 87 | struct k_msgq; |
| 88 | struct k_mbox; |
| 89 | struct k_pipe; |
| 90 | struct k_fifo; |
| 91 | struct k_lifo; |
| 92 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 93 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 94 | struct k_mem_pool; |
| 95 | struct k_timer; |
| 96 | |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 97 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 98 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 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); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 106 | |
| 107 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 108 | * @defgroup thread_apis Thread APIs |
| 109 | * @ingroup kernel_apis |
| 110 | * @{ |
| 111 | */ |
| 112 | |
| 113 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 114 | * @brief Spawn a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 115 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 116 | * This routine initializes a thread, then schedules it for execution. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 117 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 118 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 119 | * If the newly spawned thread does not have a delayed start the kernel |
| 120 | * scheduler may preempt the current thread to allow the new thread to |
| 121 | * execute. |
| 122 | * |
| 123 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 124 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 125 | * them using "|" (the logical OR operator). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 126 | * |
| 127 | * @param stack Pointer to the stack space. |
| 128 | * @param stack_size Stack size in bytes. |
| 129 | * @param entry Thread entry function. |
| 130 | * @param p1 1st entry point parameter. |
| 131 | * @param p2 2nd entry point parameter. |
| 132 | * @param p3 3rd entry point parameter. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 133 | * @param prio Thread priority. |
| 134 | * @param options Thread options. |
| 135 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 136 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 137 | * @return ID of new thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 138 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 139 | extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size, |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 140 | void (*entry)(void *, void *, void *), |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 141 | void *p1, void *p2, void *p3, |
| 142 | int32_t prio, uint32_t options, int32_t delay); |
| 143 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 144 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 145 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 146 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 147 | * This routine puts the current thread to sleep for @a duration |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 148 | * milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 149 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 150 | * @param duration Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 151 | * |
| 152 | * @return N/A |
| 153 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 154 | extern void k_sleep(int32_t duration); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 155 | |
| 156 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 157 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 158 | * |
| 159 | * This routine causes the current thread to execute a "do nothing" loop for |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 160 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 161 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 162 | * @return N/A |
| 163 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 164 | extern void k_busy_wait(uint32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 165 | |
| 166 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 167 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 168 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 169 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 170 | * thread of the same or higher priority. If there are no other ready threads |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 171 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 172 | * |
| 173 | * @return N/A |
| 174 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 175 | extern void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 176 | |
| 177 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 178 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 179 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 180 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 181 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 182 | * If @a thread is not currently sleeping, the routine has no effect. |
| 183 | * |
| 184 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 185 | * |
| 186 | * @return N/A |
| 187 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 188 | extern void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 189 | |
| 190 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 191 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 192 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 193 | * @return ID of current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 194 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 195 | extern k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 196 | |
| 197 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 198 | * @brief Cancel thread performing a delayed start. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 199 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 200 | * This routine prevents @a thread from executing if it has not yet started |
| 201 | * execution. The thread must be re-spawned before it will execute. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 202 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 203 | * @param thread ID of thread to cancel. |
| 204 | * |
| 205 | * @retval 0 if successful. |
| 206 | * @retval -EINVAL if the thread has already started executing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 207 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 208 | extern int k_thread_cancel(k_tid_t thread); |
| 209 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 210 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 211 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 212 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 213 | * This routine permanently stops execution of @a thread. The thread is taken |
| 214 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 215 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 216 | * thread might currently own (such as mutexes or memory blocks) are not |
| 217 | * released. It is the responsibility of the caller of this routine to ensure |
| 218 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 219 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 220 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 221 | * |
| 222 | * @return N/A |
| 223 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 224 | extern void k_thread_abort(k_tid_t thread); |
| 225 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 226 | /** |
| 227 | * @cond INTERNAL_HIDDEN |
| 228 | */ |
| 229 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 230 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 231 | #define _THREAD_TIMEOUT_INIT(obj) \ |
| 232 | (obj).nano_timeout = { \ |
| 233 | .node = { {0}, {0} }, \ |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 234 | .thread = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 235 | .wait_q = NULL, \ |
| 236 | .delta_ticks_from_prev = -1, \ |
| 237 | }, |
| 238 | #else |
| 239 | #define _THREAD_TIMEOUT_INIT(obj) |
| 240 | #endif |
| 241 | |
| 242 | #ifdef CONFIG_ERRNO |
| 243 | #define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0, |
| 244 | #else |
| 245 | #define _THREAD_ERRNO_INIT(obj) |
| 246 | #endif |
| 247 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 248 | struct _static_thread_data { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 249 | union { |
| 250 | char *init_stack; |
| 251 | struct k_thread *thread; |
| 252 | }; |
| 253 | unsigned int init_stack_size; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 254 | void (*init_entry)(void *, void *, void *); |
| 255 | void *init_p1; |
| 256 | void *init_p2; |
| 257 | void *init_p3; |
| 258 | int init_prio; |
| 259 | uint32_t init_options; |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 260 | int32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 261 | void (*init_abort)(void); |
| 262 | uint32_t init_groups; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 263 | }; |
| 264 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 265 | #define _THREAD_INITIALIZER(stack, stack_size, \ |
| 266 | entry, p1, p2, p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 267 | prio, options, delay, abort, groups) \ |
| 268 | { \ |
| 269 | .init_stack = (stack), \ |
| 270 | .init_stack_size = (stack_size), \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 271 | .init_entry = (void (*)(void *, void *, void *))entry, \ |
| 272 | .init_p1 = (void *)p1, \ |
| 273 | .init_p2 = (void *)p2, \ |
| 274 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 275 | .init_prio = (prio), \ |
| 276 | .init_options = (options), \ |
| 277 | .init_delay = (delay), \ |
| 278 | .init_abort = (abort), \ |
| 279 | .init_groups = (groups), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 282 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 283 | * INTERNAL_HIDDEN @endcond |
| 284 | */ |
| 285 | |
| 286 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 287 | * @brief Statically define and initialize a thread. |
| 288 | * |
| 289 | * The thread may be scheduled for immediate execution or a delayed start. |
| 290 | * |
| 291 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 292 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 293 | * them using "|" (the logical OR operator). |
| 294 | * |
| 295 | * The ID of the thread can be accessed using: |
| 296 | * |
| 297 | * extern const k_tid_t @a name; |
| 298 | * |
| 299 | * @param name Name of the thread. |
| 300 | * @param stack_size Stack size in bytes. |
| 301 | * @param entry Thread entry function. |
| 302 | * @param p1 1st entry point parameter. |
| 303 | * @param p2 2nd entry point parameter. |
| 304 | * @param p3 3rd entry point parameter. |
| 305 | * @param prio Thread priority. |
| 306 | * @param options Thread options. |
| 307 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 308 | * |
| 309 | * @internal It has been observed that the x86 compiler by default aligns |
| 310 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 311 | * wasting space. To work around this, force a 4-byte alignment. |
| 312 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 313 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 314 | entry, p1, p2, p3, \ |
| 315 | prio, options, delay) \ |
| 316 | char __noinit __stack _k_thread_obj_##name[stack_size]; \ |
| 317 | struct _static_thread_data _k_thread_data_##name __aligned(4) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 318 | __in_section(_static_thread_data, static, name) = \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 319 | _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \ |
| 320 | entry, p1, p2, p3, prio, options, delay, \ |
Allan Stephens | 8809502 | 2016-10-26 14:15:08 -0500 | [diff] [blame] | 321 | NULL, 0); \ |
| 322 | const k_tid_t name = (k_tid_t)_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 323 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 324 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 325 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 326 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 327 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 328 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 329 | * @param thread ID of thread whose priority is needed. |
| 330 | * |
| 331 | * @return Priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 332 | */ |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 333 | extern int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 334 | |
| 335 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 336 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 337 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 338 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 339 | * |
| 340 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 341 | * set to: |
| 342 | * |
| 343 | * - If its priority is raised above the priority of the caller of this |
| 344 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 345 | * |
| 346 | * - If the caller operates on itself, it lowers its priority below that of |
| 347 | * other threads in the system, and the caller is preemptible, the thread of |
| 348 | * highest priority will be scheduled in. |
| 349 | * |
| 350 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 351 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 352 | * highest priority. |
| 353 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 354 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 355 | * @param prio New priority. |
| 356 | * |
| 357 | * @warning Changing the priority of a thread currently involved in mutex |
| 358 | * priority inheritance may result in undefined behavior. |
| 359 | * |
| 360 | * @return N/A |
| 361 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 362 | extern void k_thread_priority_set(k_tid_t thread, int prio); |
| 363 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 364 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 365 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 366 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 367 | * This routine prevents the kernel scheduler from making @a thread the |
| 368 | * current thread. All other internal operations on @a thread are still |
| 369 | * performed; for example, any timeout it is waiting on keeps ticking, |
| 370 | * kernel objects it is waiting on are still handed to it, etc. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 371 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 372 | * If @a thread is already suspended, the routine has no effect. |
| 373 | * |
| 374 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 375 | * |
| 376 | * @return N/A |
| 377 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 378 | extern void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 379 | |
| 380 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 381 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 382 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 383 | * This routine allows the kernel scheduler to make @a thread the current |
| 384 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 385 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 386 | * If @a thread is not currently suspended, the routine has no effect. |
| 387 | * |
| 388 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 389 | * |
| 390 | * @return N/A |
| 391 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 392 | extern void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 393 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 394 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 395 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 396 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 397 | * This routine specifies how the scheduler will perform time slicing of |
| 398 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 399 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 400 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 401 | * ensures that no thread runs for more than the specified time limit |
| 402 | * before other threads of that priority are given a chance to execute. |
| 403 | * Any thread whose priority is higher than @a prio is exempted, and may |
| 404 | * execute as long as desired without being pre-empted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 405 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 406 | * Time slicing only limits the maximum amount of time a thread may continuously |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 407 | * execute. Once the scheduler selects a thread for execution, there is no |
| 408 | * minimum guaranteed time the thread will execute before threads of greater or |
| 409 | * equal priority are scheduled. |
| 410 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 411 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 412 | * for execution, this routine has no effect; the thread is immediately |
| 413 | * rescheduled after the slice period expires. |
| 414 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 415 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 416 | * |
| 417 | * @param slice Maximum time slice length (in milliseconds). |
| 418 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 419 | * |
| 420 | * @return N/A |
| 421 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 422 | extern void k_sched_time_slice_set(int32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 423 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 424 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 425 | * @} end defgroup thread_apis |
| 426 | */ |
| 427 | |
| 428 | /** |
| 429 | * @addtogroup isr_apis |
| 430 | * @{ |
| 431 | */ |
| 432 | |
| 433 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 434 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 435 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 436 | * This routine allows the caller to customize its actions, depending on |
| 437 | * whether it is a thread or an ISR. |
| 438 | * |
| 439 | * @note Can be called by ISRs. |
| 440 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 441 | * @return 0 if invoked by a thread. |
| 442 | * @return Non-zero if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 443 | */ |
Benjamin Walsh | c7ba8b1 | 2016-11-08 16:12:59 -0500 | [diff] [blame] | 444 | extern int k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 445 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 446 | /** |
| 447 | * @brief Determine if code is running in a preemptible thread. |
| 448 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 449 | * This routine allows the caller to customize its actions, depending on |
| 450 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 451 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 452 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 453 | * - The code is running in a thread, not at ISR. |
| 454 | * - The thread's priority is in the preemptible range. |
| 455 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 456 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 457 | * @note Can be called by ISRs. |
| 458 | * |
| 459 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 460 | * @return Non-zero if invoked by a preemptible thread. |
| 461 | */ |
| 462 | extern int k_is_preempt_thread(void); |
| 463 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 464 | /** |
| 465 | * @} end addtogroup isr_apis |
| 466 | */ |
| 467 | |
| 468 | /** |
| 469 | * @addtogroup thread_apis |
| 470 | * @{ |
| 471 | */ |
| 472 | |
| 473 | /** |
| 474 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 475 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 476 | * This routine prevents the current thread from being preempted by another |
| 477 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 478 | * If the thread subsequently performs an operation that makes it unready, |
| 479 | * it will be context switched out in the normal manner. When the thread |
| 480 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 481 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 482 | * This routine can be called recursively. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 483 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 484 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 485 | * when the operation being performed can be safely interrupted by ISRs. |
| 486 | * However, if the amount of processing involved is very small, better |
| 487 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 488 | * |
| 489 | * @return N/A |
| 490 | */ |
| 491 | extern void k_sched_lock(void); |
| 492 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 493 | /** |
| 494 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 495 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 496 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 497 | * A thread must call the routine once for each time it called k_sched_lock() |
| 498 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 499 | * |
| 500 | * @return N/A |
| 501 | */ |
| 502 | extern void k_sched_unlock(void); |
| 503 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 504 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 505 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 506 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 507 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 508 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 509 | * Custom data is not used by the kernel itself, and is freely available |
| 510 | * for a thread to use as it sees fit. It can be used as a framework |
| 511 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 512 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 513 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 514 | * |
| 515 | * @return N/A |
| 516 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 517 | extern void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 518 | |
| 519 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 520 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 521 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 522 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 523 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 524 | * @return Current custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 525 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 526 | extern void *k_thread_custom_data_get(void); |
| 527 | |
| 528 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 529 | * @} end addtogroup thread_apis |
| 530 | */ |
| 531 | |
| 532 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 533 | * kernel timing |
| 534 | */ |
| 535 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 536 | #include <sys_clock.h> |
| 537 | |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 538 | /* Convenience helpers to convert durations into milliseconds */ |
| 539 | #define K_MSEC(ms) (ms) |
| 540 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
| 541 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
| 542 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 543 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 544 | /** |
| 545 | * @cond INTERNAL_HIDDEN |
| 546 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 547 | |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 548 | /* added tick needed to account for tick in progress */ |
| 549 | #define _TICK_ALIGN 1 |
| 550 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 551 | static int64_t __ticks_to_ms(int64_t ticks) |
| 552 | { |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 553 | #if CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 554 | return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec; |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 555 | #else |
| 556 | __ASSERT(ticks == 0, ""); |
| 557 | return 0; |
| 558 | #endif |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 559 | } |
| 560 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 561 | /* timeouts */ |
| 562 | |
| 563 | struct _timeout; |
| 564 | typedef void (*_timeout_func_t)(struct _timeout *t); |
| 565 | |
| 566 | struct _timeout { |
| 567 | sys_dlist_t node; |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 568 | struct k_thread *thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 569 | sys_dlist_t *wait_q; |
| 570 | int32_t delta_ticks_from_prev; |
| 571 | _timeout_func_t func; |
| 572 | }; |
| 573 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 574 | /** |
| 575 | * INTERNAL_HIDDEN @endcond |
| 576 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 577 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 578 | /** |
| 579 | * @cond INTERNAL_HIDDEN |
| 580 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 581 | |
| 582 | struct k_timer { |
| 583 | /* |
| 584 | * _timeout structure must be first here if we want to use |
| 585 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 586 | * list of free timers |
| 587 | */ |
| 588 | struct _timeout timeout; |
| 589 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 590 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 591 | _wait_q_t wait_q; |
| 592 | |
| 593 | /* runs in ISR context */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 594 | void (*expiry_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 595 | |
| 596 | /* runs in the context of the thread that calls k_timer_stop() */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 597 | void (*stop_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 598 | |
| 599 | /* timer period */ |
| 600 | int32_t period; |
| 601 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 602 | /* timer status */ |
| 603 | uint32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 604 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 605 | /* used to support legacy timer APIs */ |
| 606 | void *_legacy_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 607 | |
| 608 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer); |
| 609 | }; |
| 610 | |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 611 | #define K_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 612 | { \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 613 | .timeout.delta_ticks_from_prev = -1, \ |
| 614 | .timeout.wait_q = NULL, \ |
| 615 | .timeout.thread = NULL, \ |
| 616 | .timeout.func = _timer_expiration_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 617 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 618 | .expiry_fn = expiry, \ |
| 619 | .stop_fn = stop, \ |
| 620 | .status = 0, \ |
| 621 | ._legacy_data = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 622 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 623 | } |
| 624 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 625 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 626 | * INTERNAL_HIDDEN @endcond |
| 627 | */ |
| 628 | |
| 629 | /** |
| 630 | * @defgroup timer_apis Timer APIs |
| 631 | * @ingroup kernel_apis |
| 632 | * @{ |
| 633 | */ |
| 634 | |
| 635 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 636 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 637 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 638 | * The timer can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 639 | * |
| 640 | * extern struct k_timer @a name; |
| 641 | * |
| 642 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 643 | * @param expiry_fn Function to invoke each time the timer expires. |
| 644 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 645 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 646 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 647 | struct k_timer name \ |
| 648 | __in_section(_k_timer, static, name) = \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 649 | K_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 650 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 651 | /** |
| 652 | * @brief Initialize a timer. |
| 653 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 654 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 655 | * |
| 656 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 657 | * @param expiry_fn Function to invoke each time the timer expires. |
| 658 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 659 | * |
| 660 | * @return N/A |
| 661 | */ |
| 662 | extern void k_timer_init(struct k_timer *timer, |
| 663 | void (*expiry_fn)(struct k_timer *), |
| 664 | void (*stop_fn)(struct k_timer *)); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 665 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 666 | /** |
| 667 | * @brief Start a timer. |
| 668 | * |
| 669 | * This routine starts a timer, and resets its status to zero. The timer |
| 670 | * begins counting down using the specified duration and period values. |
| 671 | * |
| 672 | * Attempting to start a timer that is already running is permitted. |
| 673 | * The timer's status is reset to zero and the timer begins counting down |
| 674 | * using the new duration and period values. |
| 675 | * |
| 676 | * @param timer Address of timer. |
| 677 | * @param duration Initial timer duration (in milliseconds). |
| 678 | * @param period Timer period (in milliseconds). |
| 679 | * |
| 680 | * @return N/A |
| 681 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 682 | extern void k_timer_start(struct k_timer *timer, |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 683 | int32_t duration, int32_t period); |
| 684 | |
| 685 | /** |
| 686 | * @brief Stop a timer. |
| 687 | * |
| 688 | * This routine stops a running timer prematurely. The timer's stop function, |
| 689 | * if one exists, is invoked by the caller. |
| 690 | * |
| 691 | * Attempting to stop a timer that is not running is permitted, but has no |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 692 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 693 | * |
| 694 | * @param timer Address of timer. |
| 695 | * |
| 696 | * @return N/A |
| 697 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 698 | extern void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 699 | |
| 700 | /** |
| 701 | * @brief Read timer status. |
| 702 | * |
| 703 | * This routine reads the timer's status, which indicates the number of times |
| 704 | * it has expired since its status was last read. |
| 705 | * |
| 706 | * Calling this routine resets the timer's status to zero. |
| 707 | * |
| 708 | * @param timer Address of timer. |
| 709 | * |
| 710 | * @return Timer status. |
| 711 | */ |
| 712 | extern uint32_t k_timer_status_get(struct k_timer *timer); |
| 713 | |
| 714 | /** |
| 715 | * @brief Synchronize thread to timer expiration. |
| 716 | * |
| 717 | * This routine blocks the calling thread until the timer's status is non-zero |
| 718 | * (indicating that it has expired at least once since it was last examined) |
| 719 | * or the timer is stopped. If the timer status is already non-zero, |
| 720 | * or the timer is already stopped, the caller continues without waiting. |
| 721 | * |
| 722 | * Calling this routine resets the timer's status to zero. |
| 723 | * |
| 724 | * This routine must not be used by interrupt handlers, since they are not |
| 725 | * allowed to block. |
| 726 | * |
| 727 | * @param timer Address of timer. |
| 728 | * |
| 729 | * @return Timer status. |
| 730 | */ |
| 731 | extern uint32_t k_timer_status_sync(struct k_timer *timer); |
| 732 | |
| 733 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 734 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 735 | * |
| 736 | * This routine computes the (approximate) time remaining before a running |
| 737 | * timer next expires. If the timer is not running, it returns zero. |
| 738 | * |
| 739 | * @param timer Address of timer. |
| 740 | * |
| 741 | * @return Remaining time (in milliseconds). |
| 742 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 743 | extern int32_t k_timer_remaining_get(struct k_timer *timer); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 744 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 745 | /** |
| 746 | * @} end defgroup timer_apis |
| 747 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 748 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 749 | /** |
| 750 | * @defgroup clock_apis Kernel Clock APIs |
| 751 | * @ingroup kernel_apis |
| 752 | * @{ |
| 753 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 754 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 755 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 756 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 757 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 758 | * This routine returns the elapsed time since the system booted, |
| 759 | * in milliseconds. |
| 760 | * |
| 761 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 762 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 763 | extern int64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 764 | |
| 765 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 766 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 767 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 768 | * This routine returns the lower 32-bits of the elapsed time since the system |
| 769 | * booted, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 770 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 771 | * This routine can be more efficient than k_uptime_get(), as it reduces the |
| 772 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 773 | * cannot hold a system uptime time larger than approximately 50 days, so the |
| 774 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 775 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 776 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 777 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 778 | extern uint32_t k_uptime_get_32(void); |
| 779 | |
| 780 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 781 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 782 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 783 | * This routine computes the elapsed time between the current system uptime |
| 784 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 785 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 786 | * @param reftime Pointer to a reference time, which is updated to the current |
| 787 | * uptime upon return. |
| 788 | * |
| 789 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 790 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 791 | extern int64_t k_uptime_delta(int64_t *reftime); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 792 | |
| 793 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 794 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 795 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 796 | * This routine computes the elapsed time between the current system uptime |
| 797 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 798 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 799 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 800 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 801 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 802 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 803 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 804 | * @param reftime Pointer to a reference time, which is updated to the current |
| 805 | * uptime upon return. |
| 806 | * |
| 807 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 808 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 809 | extern uint32_t k_uptime_delta_32(int64_t *reftime); |
| 810 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 811 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 812 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 813 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 814 | * This routine returns the current time, as measured by the system's hardware |
| 815 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 816 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 817 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 818 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 819 | extern uint32_t k_cycle_get_32(void); |
| 820 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 821 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 822 | * @} end defgroup clock_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 823 | */ |
| 824 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 825 | /** |
| 826 | * @cond INTERNAL_HIDDEN |
| 827 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 828 | |
| 829 | struct k_fifo { |
| 830 | _wait_q_t wait_q; |
| 831 | sys_slist_t data_q; |
| 832 | |
| 833 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo); |
| 834 | }; |
| 835 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 836 | #define K_FIFO_INITIALIZER(obj) \ |
| 837 | { \ |
| 838 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 839 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
| 840 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * INTERNAL_HIDDEN @endcond |
| 845 | */ |
| 846 | |
| 847 | /** |
| 848 | * @defgroup fifo_apis Fifo APIs |
| 849 | * @ingroup kernel_apis |
| 850 | * @{ |
| 851 | */ |
| 852 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 853 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 854 | * @brief Initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 855 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 856 | * This routine initializes a fifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 857 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 858 | * @param fifo Address of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 859 | * |
| 860 | * @return N/A |
| 861 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 862 | extern void k_fifo_init(struct k_fifo *fifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 863 | |
| 864 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 865 | * @brief Add an element to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 866 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 867 | * This routine adds a data item to @a fifo. A fifo data item must be |
| 868 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 869 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 870 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 871 | * @note Can be called by ISRs. |
| 872 | * |
| 873 | * @param fifo Address of the fifo. |
| 874 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 875 | * |
| 876 | * @return N/A |
| 877 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 878 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 879 | |
| 880 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 881 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 882 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 883 | * This routine adds a list of data items to @a fifo in one operation. |
| 884 | * The data items must be in a singly-linked list, with the first 32 bits |
| 885 | * each data item pointing to the next data item; the list must be |
| 886 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 887 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 888 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 889 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 890 | * @param fifo Address of the fifo. |
| 891 | * @param head Pointer to first node in singly-linked list. |
| 892 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 893 | * |
| 894 | * @return N/A |
| 895 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 896 | extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 897 | |
| 898 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 899 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 900 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 901 | * This routine adds a list of data items to @a fifo in one operation. |
| 902 | * The data items must be in a singly-linked list implemented using a |
| 903 | * sys_slist_t object. Upon completion, the sys_slist_t object is invalid |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 904 | * and must be re-initialized via sys_slist_init(). |
| 905 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 906 | * @note Can be called by ISRs. |
| 907 | * |
| 908 | * @param fifo Address of the fifo. |
| 909 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 910 | * |
| 911 | * @return N/A |
| 912 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 913 | extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 914 | |
| 915 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 916 | * @brief Get an element from a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 917 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 918 | * This routine removes a data item from @a fifo in a "first in, first out" |
| 919 | * manner. The first 32 bits of the data item are reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 920 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 921 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 922 | * |
| 923 | * @param fifo Address of the fifo. |
| 924 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 925 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 926 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 927 | * @return Address of the data item if successful. |
| 928 | * @retval NULL if returned without waiting or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 929 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 930 | extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); |
| 931 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 932 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 933 | * @brief Statically define and initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 934 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 935 | * The fifo can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 936 | * |
| 937 | * extern struct k_fifo @a name; |
| 938 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 939 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 940 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 941 | #define K_FIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 942 | struct k_fifo name \ |
| 943 | __in_section(_k_fifo, static, name) = \ |
| 944 | K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 945 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 946 | /** |
| 947 | * @} end defgroup fifo_apis |
| 948 | */ |
| 949 | |
| 950 | /** |
| 951 | * @cond INTERNAL_HIDDEN |
| 952 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 953 | |
| 954 | struct k_lifo { |
| 955 | _wait_q_t wait_q; |
| 956 | void *list; |
| 957 | |
| 958 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo); |
| 959 | }; |
| 960 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 961 | #define K_LIFO_INITIALIZER(obj) \ |
| 962 | { \ |
| 963 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 964 | .list = NULL, \ |
| 965 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * INTERNAL_HIDDEN @endcond |
| 970 | */ |
| 971 | |
| 972 | /** |
| 973 | * @defgroup lifo_apis Lifo APIs |
| 974 | * @ingroup kernel_apis |
| 975 | * @{ |
| 976 | */ |
| 977 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 978 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 979 | * @brief Initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 980 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 981 | * This routine initializes a lifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 982 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 983 | * @param lifo Address of the lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 984 | * |
| 985 | * @return N/A |
| 986 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 987 | extern void k_lifo_init(struct k_lifo *lifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 988 | |
| 989 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 990 | * @brief Add an element to a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 991 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 992 | * This routine adds a data item to @a lifo. A lifo data item must be |
| 993 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 994 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 995 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 996 | * @note Can be called by ISRs. |
| 997 | * |
| 998 | * @param lifo Address of the lifo. |
| 999 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1000 | * |
| 1001 | * @return N/A |
| 1002 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1003 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1004 | |
| 1005 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1006 | * @brief Get an element from a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1007 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1008 | * This routine removes a data item from @a lifo in a "last in, first out" |
| 1009 | * manner. The first 32 bits of the data item are reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1010 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1011 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1012 | * |
| 1013 | * @param lifo Address of the lifo. |
| 1014 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1015 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1016 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1017 | * @return Address of the data item if successful. |
| 1018 | * @retval NULL if returned without waiting or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1019 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1020 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 1021 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1022 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1023 | * @brief Statically define and initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1024 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1025 | * The lifo can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1026 | * |
| 1027 | * extern struct k_lifo @a name; |
| 1028 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1029 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1030 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1031 | #define K_LIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1032 | struct k_lifo name \ |
| 1033 | __in_section(_k_lifo, static, name) = \ |
| 1034 | K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1035 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1036 | /** |
| 1037 | * @} end defgroup lifo_apis |
| 1038 | */ |
| 1039 | |
| 1040 | /** |
| 1041 | * @cond INTERNAL_HIDDEN |
| 1042 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1043 | |
| 1044 | struct k_stack { |
| 1045 | _wait_q_t wait_q; |
| 1046 | uint32_t *base, *next, *top; |
| 1047 | |
| 1048 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack); |
| 1049 | }; |
| 1050 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1051 | #define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
| 1052 | { \ |
| 1053 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1054 | .base = stack_buffer, \ |
| 1055 | .next = stack_buffer, \ |
| 1056 | .top = stack_buffer + stack_num_entries, \ |
| 1057 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * INTERNAL_HIDDEN @endcond |
| 1062 | */ |
| 1063 | |
| 1064 | /** |
| 1065 | * @defgroup stack_apis Stack APIs |
| 1066 | * @ingroup kernel_apis |
| 1067 | * @{ |
| 1068 | */ |
| 1069 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1070 | /** |
| 1071 | * @brief Initialize a stack. |
| 1072 | * |
| 1073 | * This routine initializes a stack object, prior to its first use. |
| 1074 | * |
| 1075 | * @param stack Address of the stack. |
| 1076 | * @param buffer Address of array used to hold stacked values. |
| 1077 | * @param num_entries Maximum number of values that can be stacked. |
| 1078 | * |
| 1079 | * @return N/A |
| 1080 | */ |
Allan Stephens | 018cd9a | 2016-10-07 15:13:24 -0500 | [diff] [blame] | 1081 | extern void k_stack_init(struct k_stack *stack, |
| 1082 | uint32_t *buffer, int num_entries); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1083 | |
| 1084 | /** |
| 1085 | * @brief Push an element onto a stack. |
| 1086 | * |
| 1087 | * This routine adds a 32-bit value @a data to @a stack. |
| 1088 | * |
| 1089 | * @note Can be called by ISRs. |
| 1090 | * |
| 1091 | * @param stack Address of the stack. |
| 1092 | * @param data Value to push onto the stack. |
| 1093 | * |
| 1094 | * @return N/A |
| 1095 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1096 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1097 | |
| 1098 | /** |
| 1099 | * @brief Pop an element from a stack. |
| 1100 | * |
| 1101 | * This routine removes a 32-bit value from @a stack in a "last in, first out" |
| 1102 | * manner and stores the value in @a data. |
| 1103 | * |
| 1104 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1105 | * |
| 1106 | * @param stack Address of the stack. |
| 1107 | * @param data Address of area to hold the value popped from the stack. |
| 1108 | * @param timeout Waiting period to obtain a value (in milliseconds), |
| 1109 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1110 | * |
| 1111 | * @retval 0 if successful. |
| 1112 | * @retval -EBUSY if returned without waiting. |
| 1113 | * @retval -EAGAIN if waiting period timed out. |
| 1114 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1115 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 1116 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1117 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1118 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1119 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1120 | * The stack can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1121 | * |
| 1122 | * extern struct k_stack @a name; |
| 1123 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1124 | * @param name Name of the stack. |
| 1125 | * @param stack_num_entries Maximum number of values that can be stacked. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1126 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1127 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 1128 | uint32_t __noinit \ |
| 1129 | _k_stack_buf_##name[stack_num_entries]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1130 | struct k_stack name \ |
| 1131 | __in_section(_k_stack, static, name) = \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1132 | K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
| 1133 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1134 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1135 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1136 | * @} end defgroup stack_apis |
| 1137 | */ |
| 1138 | |
| 1139 | /** |
| 1140 | * @defgroup workqueue_apis Workqueue Thread APIs |
| 1141 | * @ingroup kernel_apis |
| 1142 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1143 | */ |
| 1144 | |
| 1145 | struct k_work; |
| 1146 | |
| 1147 | typedef void (*k_work_handler_t)(struct k_work *); |
| 1148 | |
| 1149 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1150 | * A workqueue is a thread that executes @ref k_work items that are |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1151 | * queued to it. This is useful for drivers which need to schedule |
| 1152 | * execution of code which might sleep from ISR context. The actual |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1153 | * thread identifier is not stored in the structure in order to save |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1154 | * space. |
| 1155 | */ |
| 1156 | struct k_work_q { |
| 1157 | struct k_fifo fifo; |
| 1158 | }; |
| 1159 | |
| 1160 | /** |
| 1161 | * @brief Work flags. |
| 1162 | */ |
| 1163 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1164 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1165 | }; |
| 1166 | |
| 1167 | /** |
| 1168 | * @brief An item which can be scheduled on a @ref k_work_q. |
| 1169 | */ |
| 1170 | struct k_work { |
| 1171 | void *_reserved; /* Used by k_fifo implementation. */ |
| 1172 | k_work_handler_t handler; |
| 1173 | atomic_t flags[1]; |
| 1174 | }; |
| 1175 | |
| 1176 | /** |
| 1177 | * @brief Statically initialize work item |
| 1178 | */ |
| 1179 | #define K_WORK_INITIALIZER(work_handler) \ |
| 1180 | { \ |
| 1181 | ._reserved = NULL, \ |
| 1182 | .handler = work_handler, \ |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1183 | .flags = { 0 } \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * @brief Dynamically initialize work item |
| 1188 | */ |
| 1189 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 1190 | { |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1191 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1192 | work->handler = handler; |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * @brief Submit a work item to a workqueue. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1197 | * |
| 1198 | * This procedure schedules a work item to be processed. |
| 1199 | * In the case where the work item has already been submitted and is pending |
| 1200 | * execution, calling this function will result in a no-op. In this case, the |
| 1201 | * work item must not be modified externally (e.g. by the caller of this |
| 1202 | * function), since that could cause the work item to be processed in a |
| 1203 | * corrupted state. |
| 1204 | * |
| 1205 | * @param work_q to schedule the work item |
| 1206 | * @param work work item |
| 1207 | * |
| 1208 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1209 | */ |
| 1210 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 1211 | struct k_work *work) |
| 1212 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1213 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1214 | k_fifo_put(&work_q->fifo, work); |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /** |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1219 | * @brief Check if work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1220 | * |
| 1221 | * @param work Work item to query |
| 1222 | * |
| 1223 | * @return K_WORK_STATE_PENDING if pending, 0 if not |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1224 | */ |
| 1225 | static inline int k_work_pending(struct k_work *work) |
| 1226 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1227 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1231 | * @brief Start a new workqueue. |
| 1232 | * |
| 1233 | * This routine must not be called from an ISR. |
| 1234 | * |
| 1235 | * @param work_q Pointer to Work queue |
| 1236 | * @param stack Pointer to work queue thread's stack |
| 1237 | * @param stack_size Size of the work queue thread's stack |
| 1238 | * @param prio Priority of the work queue's thread |
| 1239 | * |
| 1240 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1241 | */ |
Allan Stephens | 904cf97 | 2016-10-07 13:59:23 -0500 | [diff] [blame] | 1242 | extern void k_work_q_start(struct k_work_q *work_q, char *stack, |
| 1243 | unsigned stack_size, unsigned prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1244 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1245 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1246 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1247 | /** |
| 1248 | * @brief An item which can be scheduled on a @ref k_work_q with a delay |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1249 | */ |
| 1250 | struct k_delayed_work { |
| 1251 | struct k_work work; |
| 1252 | struct _timeout timeout; |
| 1253 | struct k_work_q *work_q; |
| 1254 | }; |
| 1255 | |
| 1256 | /** |
| 1257 | * @brief Initialize delayed work |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1258 | * |
| 1259 | * Initialize a delayed work item. |
| 1260 | * |
| 1261 | * @param work Delayed work item |
| 1262 | * @param handler Routine invoked when processing delayed work item |
| 1263 | * |
| 1264 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1265 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1266 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 1267 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1268 | |
| 1269 | /** |
| 1270 | * @brief Submit a delayed work item to a workqueue. |
| 1271 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1272 | * This routine schedules a work item to be processed after a delay. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1273 | * Once the delay has passed, the work item is submitted to the work queue: |
| 1274 | * at this point, it is no longer possible to cancel it. Once the work item's |
| 1275 | * handler is about to be executed, the work is considered complete and can be |
| 1276 | * resubmitted. |
| 1277 | * |
| 1278 | * Care must be taken if the handler blocks or yield as there is no implicit |
| 1279 | * mutual exclusion mechanism. Such usage is not recommended and if necessary, |
| 1280 | * it should be explicitly done between the submitter and the handler. |
| 1281 | * |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1282 | * @param work_q Workqueue to schedule the work item |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1283 | * @param work Delayed work item |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1284 | * @param delay Delay before scheduling the work item (in milliseconds) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1285 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1286 | * @return 0 in case of success, or negative value in case of error. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1287 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1288 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 1289 | struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1290 | int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1291 | |
| 1292 | /** |
| 1293 | * @brief Cancel a delayed work item |
| 1294 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1295 | * This routine cancels a scheduled work item. If the work has been completed |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1296 | * or is idle, this will do nothing. The only case where this can fail is when |
| 1297 | * the work has been submitted to the work queue, but the handler has not run |
| 1298 | * yet. |
| 1299 | * |
| 1300 | * @param work Delayed work item to be canceled |
| 1301 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1302 | * @return 0 in case of success, or negative value in case of error. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1303 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1304 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1305 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1306 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1307 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1308 | extern struct k_work_q k_sys_work_q; |
| 1309 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1310 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1311 | * @brief Submit a work item to the system workqueue. |
| 1312 | * |
| 1313 | * @ref k_work_submit_to_queue |
| 1314 | * |
| 1315 | * When using the system workqueue it is not recommended to block or yield |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1316 | * on the handler since its thread is shared system wide it may cause |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1317 | * unexpected behavior. |
| 1318 | */ |
| 1319 | static inline void k_work_submit(struct k_work *work) |
| 1320 | { |
| 1321 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 1322 | } |
| 1323 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1324 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1325 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1326 | * @brief Submit a delayed work item to the system workqueue. |
| 1327 | * |
| 1328 | * @ref k_delayed_work_submit_to_queue |
| 1329 | * |
| 1330 | * When using the system workqueue it is not recommended to block or yield |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1331 | * on the handler since its thread is shared system wide it may cause |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1332 | * unexpected behavior. |
| 1333 | */ |
| 1334 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1335 | int32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1336 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1337 | 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] | 1338 | } |
| 1339 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1340 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1341 | |
| 1342 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1343 | * @} end defgroup workqueue_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1344 | */ |
| 1345 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1346 | /** |
| 1347 | * @cond INTERNAL_HIDDEN |
| 1348 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1349 | |
| 1350 | struct k_mutex { |
| 1351 | _wait_q_t wait_q; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 1352 | struct k_thread *owner; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1353 | uint32_t lock_count; |
| 1354 | int owner_orig_prio; |
| 1355 | #ifdef CONFIG_OBJECT_MONITOR |
| 1356 | int num_lock_state_changes; |
| 1357 | int num_conflicts; |
| 1358 | #endif |
| 1359 | |
| 1360 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex); |
| 1361 | }; |
| 1362 | |
| 1363 | #ifdef CONFIG_OBJECT_MONITOR |
| 1364 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 1365 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 1366 | #else |
| 1367 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 1368 | #endif |
| 1369 | |
| 1370 | #define K_MUTEX_INITIALIZER(obj) \ |
| 1371 | { \ |
| 1372 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1373 | .owner = NULL, \ |
| 1374 | .lock_count = 0, \ |
| 1375 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 1376 | _MUTEX_INIT_OBJECT_MONITOR \ |
| 1377 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1378 | } |
| 1379 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1380 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1381 | * INTERNAL_HIDDEN @endcond |
| 1382 | */ |
| 1383 | |
| 1384 | /** |
| 1385 | * @defgroup mutex_apis Mutex APIs |
| 1386 | * @ingroup kernel_apis |
| 1387 | * @{ |
| 1388 | */ |
| 1389 | |
| 1390 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1391 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1392 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1393 | * The mutex can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1394 | * |
| 1395 | * extern struct k_mutex @a name; |
| 1396 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1397 | * @param name Name of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1398 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1399 | #define K_MUTEX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1400 | struct k_mutex name \ |
| 1401 | __in_section(_k_mutex, static, name) = \ |
| 1402 | K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1403 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1404 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1405 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1406 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1407 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1408 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1409 | * Upon completion, the mutex is available and does not have an owner. |
| 1410 | * |
| 1411 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1412 | * |
| 1413 | * @return N/A |
| 1414 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1415 | extern void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1416 | |
| 1417 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1418 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1419 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1420 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 1421 | * the calling thread waits until the mutex becomes available or until |
| 1422 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1423 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1424 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 1425 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1426 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1427 | * @param mutex Address of the mutex. |
| 1428 | * @param timeout Waiting period to lock the mutex (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1429 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1430 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1431 | * @retval 0 if successful. |
| 1432 | * @retval -EBUSY if returned without waiting. |
| 1433 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1434 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1435 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1436 | |
| 1437 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1438 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1439 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1440 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 1441 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1442 | * |
| 1443 | * The mutex cannot be claimed by another thread until it has been unlocked by |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1444 | * the calling thread as many times as it was previously locked by that |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1445 | * thread. |
| 1446 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1447 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1448 | * |
| 1449 | * @return N/A |
| 1450 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1451 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 1452 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1453 | /** |
| 1454 | * @} end defgroup mutex_apis |
| 1455 | */ |
| 1456 | |
| 1457 | /** |
| 1458 | * @cond INTERNAL_HIDDEN |
| 1459 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1460 | |
| 1461 | struct k_sem { |
| 1462 | _wait_q_t wait_q; |
| 1463 | unsigned int count; |
| 1464 | unsigned int limit; |
| 1465 | |
| 1466 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem); |
| 1467 | }; |
| 1468 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1469 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 1470 | { \ |
| 1471 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1472 | .count = initial_count, \ |
| 1473 | .limit = count_limit, \ |
| 1474 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * INTERNAL_HIDDEN @endcond |
| 1479 | */ |
| 1480 | |
| 1481 | /** |
| 1482 | * @defgroup semaphore_apis Semaphore APIs |
| 1483 | * @ingroup kernel_apis |
| 1484 | * @{ |
| 1485 | */ |
| 1486 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1487 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1488 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1489 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1490 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1491 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1492 | * @param sem Address of the semaphore. |
| 1493 | * @param initial_count Initial semaphore count. |
| 1494 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1495 | * |
| 1496 | * @return N/A |
| 1497 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1498 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 1499 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1500 | |
| 1501 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1502 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1503 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1504 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1505 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1506 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1507 | * |
| 1508 | * @param sem Address of the semaphore. |
| 1509 | * @param timeout Waiting period to take the semaphore (in milliseconds), |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1510 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1511 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1512 | * @retval 0 if successful. |
| 1513 | * @retval -EBUSY if returned without waiting. |
| 1514 | * @retval -EAGAIN if waiting period timed out. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1515 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1516 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1517 | |
| 1518 | /** |
| 1519 | * @brief Give a semaphore. |
| 1520 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1521 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 1522 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1523 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1524 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1525 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1526 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1527 | * |
| 1528 | * @return N/A |
| 1529 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1530 | extern void k_sem_give(struct k_sem *sem); |
| 1531 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1532 | /** |
| 1533 | * @brief Reset a semaphore's count to zero. |
| 1534 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1535 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1536 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1537 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1538 | * |
| 1539 | * @return N/A |
| 1540 | */ |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame] | 1541 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1542 | { |
| 1543 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1546 | /** |
| 1547 | * @brief Get a semaphore's count. |
| 1548 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1549 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1550 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1551 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1552 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1553 | * @return Current semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1554 | */ |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 1555 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1556 | { |
| 1557 | return sem->count; |
| 1558 | } |
| 1559 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1560 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1561 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1562 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1563 | * The semaphore can be accessed outside the module where it is defined using: |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1564 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1565 | * extern struct k_sem @a name; |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1566 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1567 | * @param name Name of the semaphore. |
| 1568 | * @param initial_count Initial semaphore count. |
| 1569 | * @param count_limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1570 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1571 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1572 | struct k_sem name \ |
| 1573 | __in_section(_k_sem, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1574 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 1575 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1576 | /** |
| 1577 | * @} end defgroup semaphore_apis |
| 1578 | */ |
| 1579 | |
| 1580 | /** |
| 1581 | * @defgroup alert_apis Alert APIs |
| 1582 | * @ingroup kernel_apis |
| 1583 | * @{ |
| 1584 | */ |
| 1585 | |
| 1586 | typedef int (*k_alert_handler_t)(struct k_alert *); |
| 1587 | |
| 1588 | /** |
| 1589 | * @} end defgroup alert_apis |
| 1590 | */ |
| 1591 | |
| 1592 | /** |
| 1593 | * @cond INTERNAL_HIDDEN |
| 1594 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1595 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1596 | #define K_ALERT_DEFAULT NULL |
| 1597 | #define K_ALERT_IGNORE ((void *)(-1)) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1598 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1599 | struct k_alert { |
| 1600 | k_alert_handler_t handler; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1601 | atomic_t send_count; |
| 1602 | struct k_work work_item; |
| 1603 | struct k_sem sem; |
| 1604 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1605 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1606 | }; |
| 1607 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1608 | extern void _alert_deliver(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1609 | |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1610 | #define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1611 | { \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1612 | .handler = (k_alert_handler_t)alert_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1613 | .send_count = ATOMIC_INIT(0), \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1614 | .work_item = K_WORK_INITIALIZER(_alert_deliver), \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1615 | .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1616 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1617 | } |
| 1618 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1619 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1620 | * INTERNAL_HIDDEN @endcond |
| 1621 | */ |
| 1622 | |
| 1623 | /** |
| 1624 | * @addtogroup alert_apis |
| 1625 | * @{ |
| 1626 | */ |
| 1627 | |
| 1628 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1629 | * @brief Statically define and initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1630 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1631 | * The alert is to be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1632 | * |
| 1633 | * extern struct k_alert @a name; |
| 1634 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1635 | * @param name Name of the alert. |
| 1636 | * @param alert_handler Action to take when alert is sent. Specify either |
| 1637 | * the address of a function to be invoked by the system workqueue |
| 1638 | * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 1639 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 1640 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1641 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1642 | #define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1643 | struct k_alert name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1644 | __in_section(_k_alert, static, name) = \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1645 | K_ALERT_INITIALIZER(name, alert_handler, \ |
| 1646 | max_num_pending_alerts) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1647 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1648 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1649 | * @brief Initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1650 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1651 | * This routine initializes an alert object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1652 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1653 | * @param alert Address of the alert. |
| 1654 | * @param handler Action to take when alert is sent. Specify either the address |
| 1655 | * of a function to be invoked by the system workqueue thread, |
| 1656 | * K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 1657 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 1658 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1659 | * |
| 1660 | * @return N/A |
| 1661 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1662 | extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler, |
| 1663 | unsigned int max_num_pending_alerts); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1664 | |
| 1665 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1666 | * @brief Receive an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1667 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1668 | * This routine receives a pending alert for @a alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1669 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1670 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1671 | * |
| 1672 | * @param alert Address of the alert. |
| 1673 | * @param timeout Waiting period to receive the alert (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1674 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1675 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1676 | * @retval 0 if successful. |
| 1677 | * @retval -EBUSY if returned without waiting. |
| 1678 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1679 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1680 | extern int k_alert_recv(struct k_alert *alert, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1681 | |
| 1682 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1683 | * @brief Signal an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1684 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1685 | * This routine signals @a alert. The action specified for @a alert will |
| 1686 | * be taken, which may trigger the execution of an alert handler function |
| 1687 | * and/or cause the alert to pend (assuming the alert has not reached its |
| 1688 | * maximum number of pending alerts). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1689 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1690 | * @note Can be called by ISRs. |
| 1691 | * |
| 1692 | * @param alert Address of the alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1693 | * |
| 1694 | * @return N/A |
| 1695 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1696 | extern void k_alert_send(struct k_alert *alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1697 | |
| 1698 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1699 | * @} end addtogroup alert_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1700 | */ |
| 1701 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1702 | /** |
| 1703 | * @cond INTERNAL_HIDDEN |
| 1704 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1705 | |
| 1706 | struct k_msgq { |
| 1707 | _wait_q_t wait_q; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1708 | size_t msg_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1709 | uint32_t max_msgs; |
| 1710 | char *buffer_start; |
| 1711 | char *buffer_end; |
| 1712 | char *read_ptr; |
| 1713 | char *write_ptr; |
| 1714 | uint32_t used_msgs; |
| 1715 | |
| 1716 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); |
| 1717 | }; |
| 1718 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1719 | #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] | 1720 | { \ |
| 1721 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1722 | .max_msgs = q_max_msgs, \ |
| 1723 | .msg_size = q_msg_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1724 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1725 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1726 | .read_ptr = q_buffer, \ |
| 1727 | .write_ptr = q_buffer, \ |
| 1728 | .used_msgs = 0, \ |
| 1729 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1730 | } |
| 1731 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1732 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1733 | * INTERNAL_HIDDEN @endcond |
| 1734 | */ |
| 1735 | |
| 1736 | /** |
| 1737 | * @defgroup msgq_apis Message Queue APIs |
| 1738 | * @ingroup kernel_apis |
| 1739 | * @{ |
| 1740 | */ |
| 1741 | |
| 1742 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1743 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1744 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1745 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 1746 | * each of which is @a q_msg_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1747 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 1748 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 1749 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1750 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1751 | * The message queue can be accessed outside the module where it is defined |
| 1752 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1753 | * |
| 1754 | * extern struct k_msgq @a name; |
| 1755 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1756 | * @param q_name Name of the message queue. |
| 1757 | * @param q_msg_size Message size (in bytes). |
| 1758 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1759 | * @param q_align Alignment of the message queue's ring buffer. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1760 | */ |
| 1761 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 1762 | static char __noinit __aligned(q_align) \ |
| 1763 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1764 | struct k_msgq q_name \ |
| 1765 | __in_section(_k_msgq, static, q_name) = \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1766 | K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
| 1767 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1768 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1769 | /** |
| 1770 | * @brief Initialize a message queue. |
| 1771 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1772 | * This routine initializes a message queue object, prior to its first use. |
| 1773 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1774 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 1775 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 1776 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 1777 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 1778 | * must also be a multiple of N. |
| 1779 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1780 | * @param q Address of the message queue. |
| 1781 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 1782 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1783 | * @param max_msgs Maximum number of messages that can be queued. |
| 1784 | * |
| 1785 | * @return N/A |
| 1786 | */ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1787 | extern void k_msgq_init(struct k_msgq *q, char *buffer, |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1788 | size_t msg_size, uint32_t max_msgs); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1789 | |
| 1790 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1791 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1792 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1793 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1794 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 1795 | * @note Can be called by ISRs. |
| 1796 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1797 | * @param q Address of the message queue. |
| 1798 | * @param data Pointer to the message. |
| 1799 | * @param timeout Waiting period to add the message (in milliseconds), |
| 1800 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1801 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1802 | * @retval 0 if successful. |
| 1803 | * @retval -ENOMSG if returned without waiting or after a queue purge. |
| 1804 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1805 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1806 | 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] | 1807 | |
| 1808 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1809 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1810 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1811 | * This routine receives a message from message queue @a q in a "first in, |
| 1812 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1813 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1814 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 1815 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1816 | * @param q Address of the message queue. |
| 1817 | * @param data Address of area to hold the received message. |
| 1818 | * @param timeout Waiting period to receive the message (in milliseconds), |
| 1819 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1820 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1821 | * @retval 0 if successful. |
| 1822 | * @retval -ENOMSG if returned without waiting. |
| 1823 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1824 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1825 | 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] | 1826 | |
| 1827 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1828 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1829 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1830 | * This routine discards all unreceived messages in a message queue's ring |
| 1831 | * buffer. Any threads that are blocked waiting to send a message to the |
| 1832 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1833 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1834 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1835 | * |
| 1836 | * @return N/A |
| 1837 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1838 | extern void k_msgq_purge(struct k_msgq *q); |
| 1839 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1840 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1841 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1842 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1843 | * This routine returns the number of unused entries in a message queue's |
| 1844 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1845 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1846 | * @param q Address of the message queue. |
| 1847 | * |
| 1848 | * @return Number of unused ring buffer entries. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1849 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1850 | 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] | 1851 | { |
| 1852 | return q->max_msgs - q->used_msgs; |
| 1853 | } |
| 1854 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1855 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1856 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1857 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1858 | * This routine returns the number of messages in a message queue's ring buffer. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1859 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1860 | * @param q Address of the message queue. |
| 1861 | * |
| 1862 | * @return Number of messages. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1863 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1864 | 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] | 1865 | { |
| 1866 | return q->used_msgs; |
| 1867 | } |
| 1868 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1869 | /** |
| 1870 | * @} end defgroup msgq_apis |
| 1871 | */ |
| 1872 | |
| 1873 | /** |
| 1874 | * @defgroup mem_pool_apis Memory Pool APIs |
| 1875 | * @ingroup kernel_apis |
| 1876 | * @{ |
| 1877 | */ |
| 1878 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1879 | struct k_mem_block { |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 1880 | struct k_mem_pool *pool_id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1881 | void *addr_in_pool; |
| 1882 | void *data; |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1883 | size_t req_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1884 | }; |
| 1885 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1886 | /** |
| 1887 | * @} end defgroup mem_pool_apis |
| 1888 | */ |
| 1889 | |
| 1890 | /** |
| 1891 | * @defgroup mailbox_apis Mailbox APIs |
| 1892 | * @ingroup kernel_apis |
| 1893 | * @{ |
| 1894 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1895 | |
| 1896 | struct k_mbox_msg { |
| 1897 | /** internal use only - needed for legacy API support */ |
| 1898 | uint32_t _mailbox; |
| 1899 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 1900 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1901 | /** application-defined information value */ |
| 1902 | uint32_t info; |
| 1903 | /** sender's message data buffer */ |
| 1904 | void *tx_data; |
| 1905 | /** internal use only - needed for legacy API support */ |
| 1906 | void *_rx_data; |
| 1907 | /** message data block descriptor */ |
| 1908 | struct k_mem_block tx_block; |
| 1909 | /** source thread id */ |
| 1910 | k_tid_t rx_source_thread; |
| 1911 | /** target thread id */ |
| 1912 | k_tid_t tx_target_thread; |
| 1913 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 1914 | k_tid_t _syncing_thread; |
| 1915 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1916 | /** internal use only - semaphore used during asynchronous send */ |
| 1917 | struct k_sem *_async_sem; |
| 1918 | #endif |
| 1919 | }; |
| 1920 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1921 | /** |
| 1922 | * @cond INTERNAL_HIDDEN |
| 1923 | */ |
| 1924 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1925 | struct k_mbox { |
| 1926 | _wait_q_t tx_msg_queue; |
| 1927 | _wait_q_t rx_msg_queue; |
| 1928 | |
| 1929 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox); |
| 1930 | }; |
| 1931 | |
| 1932 | #define K_MBOX_INITIALIZER(obj) \ |
| 1933 | { \ |
| 1934 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 1935 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
| 1936 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1937 | } |
| 1938 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1939 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 1940 | * INTERNAL_HIDDEN @endcond |
| 1941 | */ |
| 1942 | |
| 1943 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1944 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1945 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1946 | * The mailbox is to be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1947 | * |
| 1948 | * extern struct k_mbox @a name; |
| 1949 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1950 | * @param name Name of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1951 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1952 | #define K_MBOX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1953 | struct k_mbox name \ |
| 1954 | __in_section(_k_mbox, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1955 | K_MBOX_INITIALIZER(name) \ |
| 1956 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1957 | /** |
| 1958 | * @brief Initialize a mailbox. |
| 1959 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1960 | * This routine initializes a mailbox object, prior to its first use. |
| 1961 | * |
| 1962 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1963 | * |
| 1964 | * @return N/A |
| 1965 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1966 | extern void k_mbox_init(struct k_mbox *mbox); |
| 1967 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1968 | /** |
| 1969 | * @brief Send a mailbox message in a synchronous manner. |
| 1970 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1971 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 1972 | * receive and process it. The message data may be in a buffer, in a memory |
| 1973 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1974 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1975 | * @param mbox Address of the mailbox. |
| 1976 | * @param tx_msg Address of the transmit message descriptor. |
| 1977 | * @param timeout Waiting period for the message to be received (in |
| 1978 | * milliseconds), or one of the special values K_NO_WAIT |
| 1979 | * and K_FOREVER. Once the message has been received, |
| 1980 | * this routine waits as long as necessary for the message |
| 1981 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1982 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1983 | * @retval 0 if successful. |
| 1984 | * @retval -ENOMSG if returned without waiting. |
| 1985 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1986 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1987 | 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] | 1988 | int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1989 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1990 | /** |
| 1991 | * @brief Send a mailbox message in an asynchronous manner. |
| 1992 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1993 | * This routine sends a message to @a mbox without waiting for a receiver |
| 1994 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 1995 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 1996 | * will be given when the message has been both received and completely |
| 1997 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1998 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1999 | * @param mbox Address of the mailbox. |
| 2000 | * @param tx_msg Address of the transmit message descriptor. |
| 2001 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2002 | * |
| 2003 | * @return N/A |
| 2004 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2005 | 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] | 2006 | struct k_sem *sem); |
| 2007 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2008 | /** |
| 2009 | * @brief Receive a mailbox message. |
| 2010 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2011 | * This routine receives a message from @a mbox, then optionally retrieves |
| 2012 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2013 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2014 | * @param mbox Address of the mailbox. |
| 2015 | * @param rx_msg Address of the receive message descriptor. |
| 2016 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 2017 | * retrieval and message disposal until later. |
| 2018 | * @param timeout Waiting period for a message to be received (in |
| 2019 | * milliseconds), or one of the special values K_NO_WAIT |
| 2020 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2021 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2022 | * @retval 0 if successful. |
| 2023 | * @retval -ENOMSG if returned without waiting. |
| 2024 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2025 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2026 | 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] | 2027 | void *buffer, int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2028 | |
| 2029 | /** |
| 2030 | * @brief Retrieve mailbox message data into a buffer. |
| 2031 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2032 | * This routine completes the processing of a received message by retrieving |
| 2033 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2034 | * |
| 2035 | * Alternatively, this routine can be used to dispose of a received message |
| 2036 | * without retrieving its data. |
| 2037 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2038 | * @param rx_msg Address of the receive message descriptor. |
| 2039 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 2040 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2041 | * |
| 2042 | * @return N/A |
| 2043 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2044 | 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] | 2045 | |
| 2046 | /** |
| 2047 | * @brief Retrieve mailbox message data into a memory pool block. |
| 2048 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2049 | * This routine completes the processing of a received message by retrieving |
| 2050 | * its data into a memory pool block, then disposing of the message. |
| 2051 | * The memory pool block that results from successful retrieval must be |
| 2052 | * returned to the pool once the data has been processed, even in cases |
| 2053 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2054 | * |
| 2055 | * Alternatively, this routine can be used to dispose of a received message |
| 2056 | * without retrieving its data. In this case there is no need to return a |
| 2057 | * memory pool block to the pool. |
| 2058 | * |
| 2059 | * This routine allocates a new memory pool block for the data only if the |
| 2060 | * data is not already in one. If a new block cannot be allocated, the routine |
| 2061 | * returns a failure code and the received message is left unchanged. This |
| 2062 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 2063 | * of the received message without retrieving its data. |
| 2064 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2065 | * @param rx_msg Address of a receive message descriptor. |
| 2066 | * @param pool Address of memory pool, or NULL to discard data. |
| 2067 | * @param block Address of the area to hold memory pool block info. |
| 2068 | * @param timeout Waiting period to wait for a memory pool block (in |
| 2069 | * milliseconds), or one of the special values K_NO_WAIT |
| 2070 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2071 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2072 | * @retval 0 if successful. |
| 2073 | * @retval -ENOMEM if returned without waiting. |
| 2074 | * @retval -EAGAIN if waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2075 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2076 | 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] | 2077 | struct k_mem_pool *pool, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2078 | struct k_mem_block *block, int32_t timeout); |
| 2079 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2080 | /** |
| 2081 | * @} end defgroup mailbox_apis |
| 2082 | */ |
| 2083 | |
| 2084 | /** |
| 2085 | * @cond INTERNAL_HIDDEN |
| 2086 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2087 | |
| 2088 | struct k_pipe { |
| 2089 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 2090 | size_t size; /* Buffer size */ |
| 2091 | size_t bytes_used; /* # bytes used in buffer */ |
| 2092 | size_t read_index; /* Where in buffer to read from */ |
| 2093 | size_t write_index; /* Where in buffer to write */ |
| 2094 | |
| 2095 | struct { |
| 2096 | _wait_q_t readers; /* Reader wait queue */ |
| 2097 | _wait_q_t writers; /* Writer wait queue */ |
| 2098 | } wait_q; |
| 2099 | |
| 2100 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe); |
| 2101 | }; |
| 2102 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2103 | #define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2104 | { \ |
| 2105 | .buffer = pipe_buffer, \ |
| 2106 | .size = pipe_buffer_size, \ |
| 2107 | .bytes_used = 0, \ |
| 2108 | .read_index = 0, \ |
| 2109 | .write_index = 0, \ |
| 2110 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 2111 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
| 2112 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 2113 | } |
| 2114 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2115 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2116 | * INTERNAL_HIDDEN @endcond |
| 2117 | */ |
| 2118 | |
| 2119 | /** |
| 2120 | * @defgroup pipe_apis Pipe APIs |
| 2121 | * @ingroup kernel_apis |
| 2122 | * @{ |
| 2123 | */ |
| 2124 | |
| 2125 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2126 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2127 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2128 | * The pipe can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2129 | * |
| 2130 | * extern struct k_pipe @a name; |
| 2131 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2132 | * @param name Name of the pipe. |
| 2133 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 2134 | * or zero if no ring buffer is used. |
| 2135 | * @param pipe_align Alignment of the pipe's ring buffer (power of 2). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2136 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2137 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
| 2138 | static unsigned char __noinit __aligned(pipe_align) \ |
| 2139 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2140 | struct k_pipe name \ |
| 2141 | __in_section(_k_pipe, static, name) = \ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2142 | K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2143 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2144 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2145 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2146 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2147 | * This routine initializes a pipe object, prior to its first use. |
| 2148 | * |
| 2149 | * @param pipe Address of the pipe. |
| 2150 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 2151 | * is used. |
| 2152 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 2153 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2154 | * |
| 2155 | * @return N/A |
| 2156 | */ |
| 2157 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 2158 | size_t size); |
| 2159 | |
| 2160 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2161 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2162 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2163 | * This routine writes up to @a bytes_to_write bytes of data to @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2164 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2165 | * @param pipe Address of the pipe. |
| 2166 | * @param data Address of data to write. |
| 2167 | * @param bytes_to_write Size of data (in bytes). |
| 2168 | * @param bytes_written Address of area to hold the number of bytes written. |
| 2169 | * @param min_xfer Minimum number of bytes to write. |
| 2170 | * @param timeout Waiting period to wait for the data to be written (in |
| 2171 | * milliseconds), or one of the special values K_NO_WAIT |
| 2172 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2173 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2174 | * @retval 0 if at least @a min_xfer data bytes were written. |
| 2175 | * @retval -EIO if returned without waiting; zero data bytes were written. |
| 2176 | * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer |
| 2177 | * minus one data bytes were written. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2178 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2179 | extern int k_pipe_put(struct k_pipe *pipe, void *data, |
| 2180 | size_t bytes_to_write, size_t *bytes_written, |
| 2181 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2182 | |
| 2183 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2184 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2185 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2186 | * This routine reads up to @a bytes_to_read bytes of data from @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2187 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2188 | * @param pipe Address of the pipe. |
| 2189 | * @param data Address to place the data read from pipe. |
| 2190 | * @param bytes_to_read Maximum number of data bytes to read. |
| 2191 | * @param bytes_read Address of area to hold the number of bytes read. |
| 2192 | * @param min_xfer Minimum number of data bytes to read. |
| 2193 | * @param timeout Waiting period to wait for the data to be read (in |
| 2194 | * milliseconds), or one of the special values K_NO_WAIT |
| 2195 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2196 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2197 | * @retval 0 if at least @a min_xfer data bytes were read. |
| 2198 | * @retval -EIO if returned without waiting; zero data bytes were read. |
| 2199 | * @retval -EAGAIN if waiting period timed out; between zero and @a min_xfer |
| 2200 | * minus one data bytes were read. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2201 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2202 | extern int k_pipe_get(struct k_pipe *pipe, void *data, |
| 2203 | size_t bytes_to_read, size_t *bytes_read, |
| 2204 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2205 | |
| 2206 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2207 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2208 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2209 | * This routine writes the data contained in a memory block to @a pipe. |
| 2210 | * Once all of the data in the block has been written to the pipe, it will |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2211 | * free the memory block @a block and give the semaphore @a sem (if specified). |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2212 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2213 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2214 | * @param block Memory block containing data to send |
| 2215 | * @param size Number of data bytes in memory block to send |
| 2216 | * @param sem Semaphore to signal upon completion (else NULL) |
| 2217 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2218 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2219 | */ |
| 2220 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 2221 | size_t size, struct k_sem *sem); |
| 2222 | |
| 2223 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2224 | * @} end defgroup pipe_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2225 | */ |
| 2226 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2227 | /** |
| 2228 | * @cond INTERNAL_HIDDEN |
| 2229 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2230 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2231 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2232 | _wait_q_t wait_q; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2233 | uint32_t num_blocks; |
| 2234 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2235 | char *buffer; |
| 2236 | char *free_list; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2237 | uint32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2238 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2239 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2240 | }; |
| 2241 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2242 | #define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
| 2243 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2244 | { \ |
| 2245 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2246 | .num_blocks = slab_num_blocks, \ |
| 2247 | .block_size = slab_block_size, \ |
| 2248 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2249 | .free_list = NULL, \ |
| 2250 | .num_used = 0, \ |
| 2251 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 2252 | } |
| 2253 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2254 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2255 | * INTERNAL_HIDDEN @endcond |
| 2256 | */ |
| 2257 | |
| 2258 | /** |
| 2259 | * @defgroup mem_slab_apis Memory Slab APIs |
| 2260 | * @ingroup kernel_apis |
| 2261 | * @{ |
| 2262 | */ |
| 2263 | |
| 2264 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2265 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2266 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2267 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2268 | * that are @a slab_block_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2269 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 2270 | * aligned to this boundary, @a slab_block_size must also be a multiple of |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2271 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2272 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2273 | * The memory slab can be accessed outside the module where it is defined |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2274 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2275 | * |
| 2276 | * extern struct k_mem_slab @a name; |
| 2277 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2278 | * @param name Name of the memory slab. |
| 2279 | * @param slab_block_size Size of each memory block (in bytes). |
| 2280 | * @param slab_num_blocks Number memory blocks. |
| 2281 | * @param slab_align Alignment of the memory slab's buffer (power of 2). |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2282 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2283 | #define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \ |
| 2284 | char __noinit __aligned(slab_align) \ |
| 2285 | _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \ |
| 2286 | struct k_mem_slab name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2287 | __in_section(_k_mem_slab, static, name) = \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2288 | K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
| 2289 | slab_block_size, slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2290 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2291 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2292 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2293 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2294 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2295 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2296 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 2297 | * that are @a slab_block_size bytes long. The buffer must be aligned to an |
| 2298 | * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...). |
| 2299 | * To ensure that each memory block is similarly aligned to this boundary, |
| 2300 | * @a slab_block_size must also be a multiple of N. |
| 2301 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2302 | * @param slab Address of the memory slab. |
| 2303 | * @param buffer Pointer to buffer used for the memory blocks. |
| 2304 | * @param block_size Size of each memory block (in bytes). |
| 2305 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2306 | * |
| 2307 | * @return N/A |
| 2308 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2309 | extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer, |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2310 | size_t block_size, uint32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2311 | |
| 2312 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2313 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2314 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2315 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2316 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2317 | * @param slab Address of the memory slab. |
| 2318 | * @param mem Pointer to block address area. |
| 2319 | * @param timeout Maximum time to wait for operation to complete |
| 2320 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2321 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2322 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2323 | * @retval 0 if successful. The block address area pointed at by @a mem |
| 2324 | * is set to the starting address of the memory block. |
| 2325 | * @retval -ENOMEM if failed immediately. |
| 2326 | * @retval -EAGAIN if timed out. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2327 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2328 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
| 2329 | int32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2330 | |
| 2331 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2332 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2333 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2334 | * This routine releases a previously allocated memory block back to its |
| 2335 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2336 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2337 | * @param slab Address of the memory slab. |
| 2338 | * @param mem Pointer to block address area (as set by k_mem_slab_alloc()). |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2339 | * |
| 2340 | * @return N/A |
| 2341 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2342 | extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2343 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2344 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2345 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2346 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2347 | * This routine gets the number of memory blocks that are currently |
| 2348 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2349 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2350 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2351 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2352 | * @return Number of allocated memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2353 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2354 | static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2355 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2356 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2357 | } |
| 2358 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2359 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2360 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2361 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2362 | * This routine gets the number of memory blocks that are currently |
| 2363 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2364 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2365 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2366 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2367 | * @return Number of unallocated memory blocks. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2368 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2369 | static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab) |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2370 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2371 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2372 | } |
| 2373 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2374 | /** |
| 2375 | * @} end defgroup mem_slab_apis |
| 2376 | */ |
| 2377 | |
| 2378 | /** |
| 2379 | * @cond INTERNAL_HIDDEN |
| 2380 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2381 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2382 | /* |
| 2383 | * Memory pool requires a buffer and two arrays of structures for the |
| 2384 | * memory block accounting: |
| 2385 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 2386 | * status of four blocks of memory. |
| 2387 | */ |
| 2388 | struct k_mem_pool_quad_block { |
| 2389 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 2390 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 2391 | allocated */ |
| 2392 | }; |
| 2393 | /* |
| 2394 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 2395 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 2396 | * block size is 4 times less than the previous one and thus requires 4 times |
| 2397 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 2398 | * memory blocks. |
| 2399 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2400 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2401 | /* |
| 2402 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 2403 | * k_mem_pool_quad_block structures |
| 2404 | */ |
| 2405 | struct k_mem_pool_block_set { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2406 | size_t block_size; /* memory block size */ |
| 2407 | 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] | 2408 | struct k_mem_pool_quad_block *quad_block; |
| 2409 | int count; |
| 2410 | }; |
| 2411 | |
| 2412 | /* Memory pool descriptor */ |
| 2413 | struct k_mem_pool { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2414 | size_t max_block_size; |
| 2415 | size_t min_block_size; |
| 2416 | uint32_t nr_of_maxblocks; |
| 2417 | uint32_t nr_of_block_sets; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2418 | struct k_mem_pool_block_set *block_set; |
| 2419 | char *bufblock; |
| 2420 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2421 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool); |
| 2422 | }; |
| 2423 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2424 | #ifdef CONFIG_ARM |
| 2425 | #define _SECTION_TYPE_SIGN "%" |
| 2426 | #else |
| 2427 | #define _SECTION_TYPE_SIGN "@" |
| 2428 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2429 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2430 | /* |
| 2431 | * Static memory pool initialization |
| 2432 | */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2433 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2434 | /* |
| 2435 | * Use .altmacro to be able to recalculate values and pass them as string |
| 2436 | * arguments when calling assembler macros resursively |
| 2437 | */ |
| 2438 | __asm__(".altmacro\n\t"); |
| 2439 | |
| 2440 | /* |
| 2441 | * Recursively calls a macro |
| 2442 | * The followig global symbols need to be initialized: |
| 2443 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2444 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2445 | * Notes: |
| 2446 | * Global symbols are used due the fact that assembler macro allows only |
| 2447 | * one argument be passed with the % conversion |
| 2448 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 2449 | * is used instead of / 4. |
| 2450 | * n_max argument needs to go first in the invoked macro, as some |
| 2451 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 2452 | * if \name goes first |
| 2453 | */ |
| 2454 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 2455 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 2456 | " __memory_pool_min_block_size\n\t\t" |
| 2457 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 2458 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 2459 | ".endif\n\t" |
| 2460 | ".endm\n"); |
| 2461 | |
| 2462 | /* |
| 2463 | * Build quad blocks |
| 2464 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 2465 | * structures and recursively calls itself for the next array, 4 times |
| 2466 | * larger. |
| 2467 | * The followig global symbols need to be initialized: |
| 2468 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2469 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2470 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 2471 | */ |
| 2472 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2473 | ".balign 4\n\t" |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2474 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 2475 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 2476 | ".if \\n_max % 4\n\t\t" |
| 2477 | ".skip __memory_pool_quad_block_size\n\t" |
| 2478 | ".endif\n\t" |
| 2479 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 2480 | ".endm\n"); |
| 2481 | |
| 2482 | /* |
| 2483 | * Build block sets and initialize them |
| 2484 | * Macro initializes the k_mem_pool_block_set structure and |
| 2485 | * recursively calls itself for the next one. |
| 2486 | * The followig global symbols need to be initialized: |
| 2487 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2488 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2489 | * __memory_pool_block_set_count, the number of the elements in the |
| 2490 | * block set array must be set to 0. Macro calculates it's real |
| 2491 | * value. |
| 2492 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 2493 | * structures, _build_quad_blocks must be called prior it. |
| 2494 | */ |
| 2495 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 2496 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 2497 | ".if \\n_max % 4\n\t\t" |
| 2498 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 2499 | ".else\n\t\t" |
| 2500 | ".int \\n_max >> 2\n\t" |
| 2501 | ".endif\n\t" |
| 2502 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 2503 | ".int 0\n\t" /* count */ |
| 2504 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 2505 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 2506 | ".endm\n"); |
| 2507 | |
| 2508 | /* |
| 2509 | * Build a memory pool structure and initialize it |
| 2510 | * Macro uses __memory_pool_block_set_count global symbol, |
| 2511 | * block set addresses and buffer address, it may be called only after |
| 2512 | * _build_block_set |
| 2513 | */ |
| 2514 | __asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t" |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2515 | ".pushsection ._k_mem_pool.static.\\name,\"aw\"," |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2516 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 2517 | ".globl \\name\n\t" |
| 2518 | "\\name:\n\t" |
| 2519 | ".int \\max_size\n\t" /* max_block_size */ |
| 2520 | ".int \\min_size\n\t" /* min_block_size */ |
| 2521 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 2522 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 2523 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 2524 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 2525 | ".int 0\n\t" /* wait_q->head */ |
| 2526 | ".int 0\n\t" /* wait_q->next */ |
| 2527 | ".popsection\n\t" |
| 2528 | ".endm\n"); |
| 2529 | |
| 2530 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 2531 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2532 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 2533 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 2534 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2535 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 2536 | STRINGIFY(name) "\n\t"); \ |
| 2537 | __asm__(".popsection\n\t") |
| 2538 | |
| 2539 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 2540 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 2541 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2542 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2543 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2544 | __asm__(".balign 4\n\t"); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2545 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 2546 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 2547 | STRINGIFY(name) "\n\t"); \ |
| 2548 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 2549 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 2550 | __asm__(".popsection\n\t"); \ |
| 2551 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 2552 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 2553 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2554 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \ |
| 2555 | char __noinit __aligned(align) \ |
| 2556 | _mem_pool_buffer_##name[(max_size) * (n_max)] |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2557 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2558 | /* |
| 2559 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 2560 | * to __memory_pool_quad_block_size absolute symbol. |
| 2561 | * This function does not get called, but compiler calculates the value and |
| 2562 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 2563 | */ |
| 2564 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 2565 | { |
| 2566 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
| 2567 | #ifdef CONFIG_NIOS2 |
| 2568 | "__memory_pool_quad_block_size = %0\n\t" |
| 2569 | #else |
| 2570 | "__memory_pool_quad_block_size = %c0\n\t" |
| 2571 | #endif |
| 2572 | : |
| 2573 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 2574 | } |
| 2575 | |
| 2576 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2577 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2578 | */ |
| 2579 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2580 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2581 | * @addtogroup mem_pool_apis |
| 2582 | * @{ |
| 2583 | */ |
| 2584 | |
| 2585 | /** |
| 2586 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2587 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2588 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 2589 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 2590 | * quarters, down to blocks of @a min_size bytes long. The buffer is aligned |
| 2591 | * to a @a align -byte boundary. To ensure that the minimum sized blocks are |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2592 | * similarly aligned to this boundary, @a min_size must also be a multiple of |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2593 | * @a align. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2594 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2595 | * If the pool is to be accessed outside the module where it is defined, it |
| 2596 | * can be declared via |
| 2597 | * |
| 2598 | * extern struct k_mem_pool @a name; |
| 2599 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2600 | * @param name Name of the memory pool. |
| 2601 | * @param min_size Size of the smallest blocks in the pool (in bytes). |
| 2602 | * @param max_size Size of the largest blocks in the pool (in bytes). |
| 2603 | * @param n_max Number of maximum sized blocks in the pool. |
| 2604 | * @param align Alignment of the pool's buffer (power of 2). |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2605 | */ |
| 2606 | #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] | 2607 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 2608 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2609 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2610 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 2611 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 2612 | extern struct k_mem_pool name |
| 2613 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2614 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2615 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2616 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2617 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2618 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2619 | * @param pool Address of the memory pool. |
| 2620 | * @param block Pointer to block descriptor for the allocated memory. |
| 2621 | * @param size Amount of memory to allocate (in bytes). |
| 2622 | * @param timeout Maximum time to wait for operation to complete |
| 2623 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2624 | * or K_FOREVER to wait as long as necessary. |
| 2625 | * |
| 2626 | * @retval 0 if successful. The @a data field of the block descriptor |
| 2627 | * is set to the starting address of the memory block. |
| 2628 | * @retval -ENOMEM if unable to allocate a memory block. |
| 2629 | * @retval -EAGAIN if timed out. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2630 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2631 | 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] | 2632 | size_t size, int32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2633 | |
| 2634 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2635 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2636 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2637 | * This routine releases a previously allocated memory block back to its |
| 2638 | * memory pool. |
| 2639 | * |
| 2640 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2641 | * |
| 2642 | * @return N/A |
| 2643 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2644 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2645 | |
| 2646 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2647 | * @brief Defragment a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2648 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2649 | * This routine instructs a memory pool to concatenate unused memory blocks |
| 2650 | * into larger blocks wherever possible. Manually defragmenting the memory |
| 2651 | * pool may speed up future allocations of memory blocks by eliminating the |
| 2652 | * need for the memory pool to perform an automatic partial defragmentation. |
| 2653 | * |
| 2654 | * @param pool Address of the memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2655 | * |
| 2656 | * @return N/A |
| 2657 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2658 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2659 | |
| 2660 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2661 | * @} end addtogroup mem_pool_apis |
| 2662 | */ |
| 2663 | |
| 2664 | /** |
| 2665 | * @defgroup heap_apis Heap Memory Pool APIs |
| 2666 | * @ingroup kernel_apis |
| 2667 | * @{ |
| 2668 | */ |
| 2669 | |
| 2670 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2671 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2672 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2673 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2674 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2675 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2676 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2677 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2678 | * @return Address of the allocated memory if successful; otherwise NULL. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2679 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2680 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2681 | |
| 2682 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2683 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2684 | * |
| 2685 | * This routine provides traditional free() semantics. The memory being |
| 2686 | * returned must have been allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2687 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2688 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2689 | * |
| 2690 | * @return N/A |
| 2691 | */ |
| 2692 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2693 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame^] | 2694 | /** |
| 2695 | * @} end defgroup heap_apis |
| 2696 | */ |
| 2697 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2698 | /* |
| 2699 | * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to |
| 2700 | * hook into the device subsystem, which itself uses nanokernel semaphores, |
| 2701 | * and thus currently requires the definition of nano_sem. |
| 2702 | */ |
| 2703 | #include <legacy.h> |
| 2704 | #include <arch/cpu.h> |
| 2705 | |
| 2706 | /* |
| 2707 | * private APIs that are utilized by one or more public APIs |
| 2708 | */ |
| 2709 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2710 | extern int _is_thread_essential(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2711 | extern void _init_static_threads(void); |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 2712 | extern void _timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2713 | |
| 2714 | #ifdef __cplusplus |
| 2715 | } |
| 2716 | #endif |
| 2717 | |
Andrew Boie | e004dec | 2016-11-07 09:01:19 -0800 | [diff] [blame] | 2718 | #if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) |
| 2719 | /* |
| 2720 | * Define new and delete operators. |
| 2721 | * At this moment, the operators do nothing since objects are supposed |
| 2722 | * to be statically allocated. |
| 2723 | */ |
| 2724 | inline void operator delete(void *ptr) |
| 2725 | { |
| 2726 | (void)ptr; |
| 2727 | } |
| 2728 | |
| 2729 | inline void operator delete[](void *ptr) |
| 2730 | { |
| 2731 | (void)ptr; |
| 2732 | } |
| 2733 | |
| 2734 | inline void *operator new(size_t size) |
| 2735 | { |
| 2736 | (void)size; |
| 2737 | return NULL; |
| 2738 | } |
| 2739 | |
| 2740 | inline void *operator new[](size_t size) |
| 2741 | { |
| 2742 | (void)size; |
| 2743 | return NULL; |
| 2744 | } |
| 2745 | |
| 2746 | /* Placement versions of operator new and delete */ |
| 2747 | inline void operator delete(void *ptr1, void *ptr2) |
| 2748 | { |
| 2749 | (void)ptr1; |
| 2750 | (void)ptr2; |
| 2751 | } |
| 2752 | |
| 2753 | inline void operator delete[](void *ptr1, void *ptr2) |
| 2754 | { |
| 2755 | (void)ptr1; |
| 2756 | (void)ptr2; |
| 2757 | } |
| 2758 | |
| 2759 | inline void *operator new(size_t size, void *ptr) |
| 2760 | { |
| 2761 | (void)size; |
| 2762 | return ptr; |
| 2763 | } |
| 2764 | |
| 2765 | inline void *operator new[](size_t size, void *ptr) |
| 2766 | { |
| 2767 | (void)size; |
| 2768 | return ptr; |
| 2769 | } |
| 2770 | |
| 2771 | #endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */ |
| 2772 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2773 | #endif /* _kernel__h_ */ |