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