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