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