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