mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
rtos/ConditionVariable.h@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* Mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2017-2019 ARM Limited |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kenjiArai | 0:5b88d5760320 | 5 | * of this software and associated documentation files (the "Software"), to deal |
kenjiArai | 0:5b88d5760320 | 6 | * in the Software without restriction, including without limitation the rights |
kenjiArai | 0:5b88d5760320 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenjiArai | 0:5b88d5760320 | 8 | * copies of the Software, and to permit persons to whom the Software is |
kenjiArai | 0:5b88d5760320 | 9 | * furnished to do so, subject to the following conditions: |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * The above copyright notice and this permission notice shall be included in |
kenjiArai | 0:5b88d5760320 | 12 | * all copies or substantial portions of the Software. |
kenjiArai | 0:5b88d5760320 | 13 | * |
kenjiArai | 0:5b88d5760320 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenjiArai | 0:5b88d5760320 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenjiArai | 0:5b88d5760320 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenjiArai | 0:5b88d5760320 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenjiArai | 0:5b88d5760320 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:5b88d5760320 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kenjiArai | 0:5b88d5760320 | 20 | * SOFTWARE. |
kenjiArai | 0:5b88d5760320 | 21 | */ |
kenjiArai | 0:5b88d5760320 | 22 | #ifndef CONDITIONVARIABLE_H |
kenjiArai | 0:5b88d5760320 | 23 | #define CONDITIONVARIABLE_H |
kenjiArai | 0:5b88d5760320 | 24 | |
kenjiArai | 0:5b88d5760320 | 25 | #include <stdint.h> |
kenjiArai | 1:9db0e321a9f4 | 26 | #include "rtos/mbed_rtos_types.h" |
kenjiArai | 0:5b88d5760320 | 27 | #include "rtos/Mutex.h" |
kenjiArai | 0:5b88d5760320 | 28 | #include "rtos/Semaphore.h" |
kenjiArai | 0:5b88d5760320 | 29 | |
kenjiArai | 0:5b88d5760320 | 30 | #include "platform/NonCopyable.h" |
kenjiArai | 0:5b88d5760320 | 31 | |
kenjiArai | 1:9db0e321a9f4 | 32 | #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) |
kenjiArai | 1:9db0e321a9f4 | 33 | |
kenjiArai | 0:5b88d5760320 | 34 | namespace rtos { |
kenjiArai | 1:9db0e321a9f4 | 35 | /** \addtogroup rtos-public-api */ |
kenjiArai | 0:5b88d5760320 | 36 | /** @{*/ |
kenjiArai | 0:5b88d5760320 | 37 | |
kenjiArai | 0:5b88d5760320 | 38 | struct Waiter; |
kenjiArai | 1:9db0e321a9f4 | 39 | /** |
kenjiArai | 1:9db0e321a9f4 | 40 | * \defgroup rtos_ConditionVariable ConditionVariable class |
kenjiArai | 1:9db0e321a9f4 | 41 | * @{ |
kenjiArai | 1:9db0e321a9f4 | 42 | */ |
kenjiArai | 0:5b88d5760320 | 43 | |
kenjiArai | 0:5b88d5760320 | 44 | /** The ConditionVariable class is a synchronization primitive that allows |
kenjiArai | 0:5b88d5760320 | 45 | * threads to wait until a particular condition occurs. |
kenjiArai | 0:5b88d5760320 | 46 | * |
kenjiArai | 0:5b88d5760320 | 47 | * Use the condition variable in conjunction with a mutex to safely wait for |
kenjiArai | 0:5b88d5760320 | 48 | * or notify waiters of condition changes to a resource accessible by multiple |
kenjiArai | 0:5b88d5760320 | 49 | * threads. |
kenjiArai | 0:5b88d5760320 | 50 | * |
kenjiArai | 0:5b88d5760320 | 51 | * The thread that intends to wait on a ConditionVariable must: |
kenjiArai | 0:5b88d5760320 | 52 | * - Acquire a lock on a mutex. |
kenjiArai | 0:5b88d5760320 | 53 | * - Execute `wait`, `wait_for` or `wait_until`. While the thread is waiting, |
kenjiArai | 0:5b88d5760320 | 54 | * the mutex is unlocked. |
kenjiArai | 0:5b88d5760320 | 55 | * - When the condition variable has been notified, or in the case of `wait_for` |
kenjiArai | 0:5b88d5760320 | 56 | * and `wait_until` the timeout expires, the thread is awakened. |
kenjiArai | 0:5b88d5760320 | 57 | * |
kenjiArai | 0:5b88d5760320 | 58 | * The thread that intends to notify a ConditionVariable must: |
kenjiArai | 0:5b88d5760320 | 59 | * - Acquire a lock on the mutex used to construct the condition variable. |
kenjiArai | 0:5b88d5760320 | 60 | * - Execute `notify_one` or `notify_all` on the condition variable. |
kenjiArai | 0:5b88d5760320 | 61 | * |
kenjiArai | 0:5b88d5760320 | 62 | * All threads waiting on the condition variable wake when |
kenjiArai | 0:5b88d5760320 | 63 | * `ConditionVariable::notify_all` is called. |
kenjiArai | 0:5b88d5760320 | 64 | * At least one thread waiting on the condition variable wakes |
kenjiArai | 0:5b88d5760320 | 65 | * when `ConditionVariable::notify_one` is called. |
kenjiArai | 0:5b88d5760320 | 66 | * |
kenjiArai | 0:5b88d5760320 | 67 | * While a thread is waiting for notification of a |
kenjiArai | 0:5b88d5760320 | 68 | * ConditionVariable, it releases the lock held on the mutex. |
kenjiArai | 0:5b88d5760320 | 69 | * The ConditionVariable reacquires the mutex lock before exiting the wait |
kenjiArai | 0:5b88d5760320 | 70 | * function. |
kenjiArai | 0:5b88d5760320 | 71 | * |
kenjiArai | 0:5b88d5760320 | 72 | * #### Unspecified behavior |
kenjiArai | 0:5b88d5760320 | 73 | * - The thread that is unblocked on `ConditionVariable::notify_one` is |
kenjiArai | 0:5b88d5760320 | 74 | * unspecified if there are multiple waiters. |
kenjiArai | 0:5b88d5760320 | 75 | * - When `ConditionVariable::notify_one` or `ConditionVariable::notify_all` is |
kenjiArai | 0:5b88d5760320 | 76 | * called and there are one or more waiters, and one or more threads |
kenjiArai | 0:5b88d5760320 | 77 | * attempting to acquire the condition variable's mutex, the order in which the mutex is |
kenjiArai | 0:5b88d5760320 | 78 | * acquired is unspecified. |
kenjiArai | 0:5b88d5760320 | 79 | * - Spurious notifications (not triggered by the application) can occur. |
kenjiArai | 0:5b88d5760320 | 80 | * |
kenjiArai | 0:5b88d5760320 | 81 | * #### Undefined behavior |
kenjiArai | 0:5b88d5760320 | 82 | * - Calling wait if the mutex is not locked by the current thread is undefined |
kenjiArai | 0:5b88d5760320 | 83 | * behavior. |
kenjiArai | 0:5b88d5760320 | 84 | * - The order in which waiting threads acquire the condition variable's |
kenjiArai | 0:5b88d5760320 | 85 | * mutex after `ConditionVariable::notify_all` is called is undefined. |
kenjiArai | 0:5b88d5760320 | 86 | * - The behavior of `ConditionVariable::wait` and `ConditionVariable::wait_for` |
kenjiArai | 0:5b88d5760320 | 87 | * is undefined if the condition variable's mutex is locked more than once by |
kenjiArai | 0:5b88d5760320 | 88 | * the calling thread. |
kenjiArai | 0:5b88d5760320 | 89 | * |
kenjiArai | 0:5b88d5760320 | 90 | * @note Synchronization level: Thread safe |
kenjiArai | 0:5b88d5760320 | 91 | * |
kenjiArai | 0:5b88d5760320 | 92 | * Example: |
kenjiArai | 0:5b88d5760320 | 93 | * |
kenjiArai | 0:5b88d5760320 | 94 | * @code |
kenjiArai | 0:5b88d5760320 | 95 | * #include "mbed.h" |
kenjiArai | 0:5b88d5760320 | 96 | * |
kenjiArai | 0:5b88d5760320 | 97 | * Mutex mutex; |
kenjiArai | 0:5b88d5760320 | 98 | * ConditionVariable cv(mutex); |
kenjiArai | 0:5b88d5760320 | 99 | * |
kenjiArai | 0:5b88d5760320 | 100 | * // These variables are protected by locking the mutex. |
kenjiArai | 0:5b88d5760320 | 101 | * uint32_t work_count = 0; |
kenjiArai | 0:5b88d5760320 | 102 | * bool done = false; |
kenjiArai | 0:5b88d5760320 | 103 | * |
kenjiArai | 0:5b88d5760320 | 104 | * void worker_thread() |
kenjiArai | 0:5b88d5760320 | 105 | * { |
kenjiArai | 0:5b88d5760320 | 106 | * // Acquire lock on mutex before accessing protected variables and waiting. |
kenjiArai | 0:5b88d5760320 | 107 | * mutex.lock(); |
kenjiArai | 0:5b88d5760320 | 108 | * |
kenjiArai | 0:5b88d5760320 | 109 | * while (done == false) { |
kenjiArai | 0:5b88d5760320 | 110 | * printf("Worker thread: Count: %lu\r\n", work_count); |
kenjiArai | 0:5b88d5760320 | 111 | * |
kenjiArai | 0:5b88d5760320 | 112 | * // Wait for main thread to notify the condition variable. |
kenjiArai | 0:5b88d5760320 | 113 | * printf("Worker thread: Waiting\r\n"); |
kenjiArai | 0:5b88d5760320 | 114 | * cv.wait(); |
kenjiArai | 0:5b88d5760320 | 115 | * } |
kenjiArai | 0:5b88d5760320 | 116 | * |
kenjiArai | 0:5b88d5760320 | 117 | * printf("Worker: Exiting\r\n"); |
kenjiArai | 0:5b88d5760320 | 118 | * |
kenjiArai | 0:5b88d5760320 | 119 | * // The condition variable acquires the lock when exiting the `wait` function. |
kenjiArai | 0:5b88d5760320 | 120 | * // Unlock mutex when exiting the thread. |
kenjiArai | 0:5b88d5760320 | 121 | * mutex.unlock(); |
kenjiArai | 0:5b88d5760320 | 122 | * } |
kenjiArai | 0:5b88d5760320 | 123 | * |
kenjiArai | 0:5b88d5760320 | 124 | * int main() |
kenjiArai | 0:5b88d5760320 | 125 | * { |
kenjiArai | 0:5b88d5760320 | 126 | * Thread thread; |
kenjiArai | 0:5b88d5760320 | 127 | * thread.start(worker_thread); |
kenjiArai | 0:5b88d5760320 | 128 | * |
kenjiArai | 0:5b88d5760320 | 129 | * for (int i = 0; i < 5; i++) { |
kenjiArai | 0:5b88d5760320 | 130 | * // Acquire lock on mutex before modifying variables and notifying. |
kenjiArai | 0:5b88d5760320 | 131 | * mutex.lock(); |
kenjiArai | 0:5b88d5760320 | 132 | * |
kenjiArai | 0:5b88d5760320 | 133 | * // Change count and notify waiters. |
kenjiArai | 0:5b88d5760320 | 134 | * work_count++; |
kenjiArai | 0:5b88d5760320 | 135 | * printf("Main thread: Set count to: %lu\r\n", work_count); |
kenjiArai | 0:5b88d5760320 | 136 | * printf("Main thread: Notifying worker thread\r\n"); |
kenjiArai | 0:5b88d5760320 | 137 | * cv.notify_all(); |
kenjiArai | 0:5b88d5760320 | 138 | * |
kenjiArai | 0:5b88d5760320 | 139 | * // Mutex must be unlocked before the worker thread can acquire it. |
kenjiArai | 0:5b88d5760320 | 140 | * mutex.unlock(); |
kenjiArai | 0:5b88d5760320 | 141 | * |
kenjiArai | 0:5b88d5760320 | 142 | * wait(1.0); |
kenjiArai | 0:5b88d5760320 | 143 | * } |
kenjiArai | 0:5b88d5760320 | 144 | * |
kenjiArai | 0:5b88d5760320 | 145 | * // Change done and notify waiters of this. |
kenjiArai | 0:5b88d5760320 | 146 | * mutex.lock(); |
kenjiArai | 0:5b88d5760320 | 147 | * done = true; |
kenjiArai | 0:5b88d5760320 | 148 | * cv.notify_all(); |
kenjiArai | 0:5b88d5760320 | 149 | * mutex.unlock(); |
kenjiArai | 0:5b88d5760320 | 150 | * |
kenjiArai | 0:5b88d5760320 | 151 | * thread.join(); |
kenjiArai | 0:5b88d5760320 | 152 | * |
kenjiArai | 0:5b88d5760320 | 153 | * printf("Main: Exiting\r\n"); |
kenjiArai | 0:5b88d5760320 | 154 | * } |
kenjiArai | 0:5b88d5760320 | 155 | * @endcode |
kenjiArai | 0:5b88d5760320 | 156 | */ |
kenjiArai | 0:5b88d5760320 | 157 | |
kenjiArai | 0:5b88d5760320 | 158 | class ConditionVariable : private mbed::NonCopyable<ConditionVariable> { |
kenjiArai | 0:5b88d5760320 | 159 | public: |
kenjiArai | 0:5b88d5760320 | 160 | /** Create and initialize a ConditionVariable object. |
kenjiArai | 0:5b88d5760320 | 161 | * |
kenjiArai | 0:5b88d5760320 | 162 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 163 | */ |
kenjiArai | 0:5b88d5760320 | 164 | ConditionVariable(Mutex &mutex); |
kenjiArai | 0:5b88d5760320 | 165 | |
kenjiArai | 0:5b88d5760320 | 166 | /** Wait for a notification. |
kenjiArai | 0:5b88d5760320 | 167 | * |
kenjiArai | 0:5b88d5760320 | 168 | * Wait causes the current thread to block until the condition variable |
kenjiArai | 0:5b88d5760320 | 169 | * receives a notification from another thread. |
kenjiArai | 0:5b88d5760320 | 170 | * |
kenjiArai | 0:5b88d5760320 | 171 | * @note - The thread calling this function must be the owner of the |
kenjiArai | 0:5b88d5760320 | 172 | * ConditionVariable's mutex, and it must be locked exactly once. |
kenjiArai | 0:5b88d5760320 | 173 | * |
kenjiArai | 0:5b88d5760320 | 174 | * @note - Spurious notifications can occur, so the caller of this API |
kenjiArai | 0:5b88d5760320 | 175 | * should check to make sure the condition the caller is waiting on has |
kenjiArai | 0:5b88d5760320 | 176 | * been met. |
kenjiArai | 0:5b88d5760320 | 177 | * |
kenjiArai | 0:5b88d5760320 | 178 | * @note - The current thread releases the lock while inside the wait |
kenjiArai | 0:5b88d5760320 | 179 | * function and reacquires it upon exiting the function. |
kenjiArai | 0:5b88d5760320 | 180 | * |
kenjiArai | 0:5b88d5760320 | 181 | * Example: |
kenjiArai | 0:5b88d5760320 | 182 | * @code |
kenjiArai | 0:5b88d5760320 | 183 | * mutex.lock(); |
kenjiArai | 0:5b88d5760320 | 184 | * |
kenjiArai | 0:5b88d5760320 | 185 | * while (!condition_met) { |
kenjiArai | 0:5b88d5760320 | 186 | * cond.wait(); |
kenjiArai | 0:5b88d5760320 | 187 | * } |
kenjiArai | 0:5b88d5760320 | 188 | * |
kenjiArai | 0:5b88d5760320 | 189 | * function_to_handle_condition(); |
kenjiArai | 0:5b88d5760320 | 190 | * |
kenjiArai | 0:5b88d5760320 | 191 | * mutex.unlock(); |
kenjiArai | 0:5b88d5760320 | 192 | * @endcode |
kenjiArai | 0:5b88d5760320 | 193 | * |
kenjiArai | 0:5b88d5760320 | 194 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 195 | */ |
kenjiArai | 0:5b88d5760320 | 196 | void wait(); |
kenjiArai | 0:5b88d5760320 | 197 | |
kenjiArai | 0:5b88d5760320 | 198 | /** Wait for a notification until the specified time. |
kenjiArai | 0:5b88d5760320 | 199 | * |
kenjiArai | 0:5b88d5760320 | 200 | * Wait until causes the current thread to block until the condition |
kenjiArai | 0:5b88d5760320 | 201 | * variable is notified, or a specific time given by millisec parameter is |
kenjiArai | 0:5b88d5760320 | 202 | * reached. |
kenjiArai | 0:5b88d5760320 | 203 | * |
kenjiArai | 0:5b88d5760320 | 204 | * @param millisec Absolute end time referenced to `Kernel::get_ms_count()` |
kenjiArai | 0:5b88d5760320 | 205 | * @return `true` if a timeout occurred, `false` otherwise. |
kenjiArai | 0:5b88d5760320 | 206 | * |
kenjiArai | 0:5b88d5760320 | 207 | * @note - The thread calling this function must be the owner of the |
kenjiArai | 0:5b88d5760320 | 208 | * ConditionVariable's mutex, and it must be locked exactly once. |
kenjiArai | 0:5b88d5760320 | 209 | * |
kenjiArai | 0:5b88d5760320 | 210 | * @note - Spurious notifications can occur, so the caller of this API |
kenjiArai | 0:5b88d5760320 | 211 | * should check to make sure the condition the caller is waiting on has |
kenjiArai | 0:5b88d5760320 | 212 | * been met. |
kenjiArai | 0:5b88d5760320 | 213 | * |
kenjiArai | 0:5b88d5760320 | 214 | * @note - The current thread releases the lock while inside the wait |
kenjiArai | 0:5b88d5760320 | 215 | * function and reacquires it upon exiting the function. |
kenjiArai | 0:5b88d5760320 | 216 | * |
kenjiArai | 0:5b88d5760320 | 217 | * Example: |
kenjiArai | 0:5b88d5760320 | 218 | * @code |
kenjiArai | 0:5b88d5760320 | 219 | * mutex.lock(); |
kenjiArai | 0:5b88d5760320 | 220 | * uint64_t end_time = Kernel::get_ms_count() + COND_WAIT_TIMEOUT; |
kenjiArai | 0:5b88d5760320 | 221 | * |
kenjiArai | 0:5b88d5760320 | 222 | * while (!condition_met) { |
kenjiArai | 0:5b88d5760320 | 223 | * if (cond.wait_until(end_time)) { |
kenjiArai | 0:5b88d5760320 | 224 | * break; |
kenjiArai | 0:5b88d5760320 | 225 | * } |
kenjiArai | 0:5b88d5760320 | 226 | * } |
kenjiArai | 0:5b88d5760320 | 227 | * |
kenjiArai | 0:5b88d5760320 | 228 | * if (condition_met) { |
kenjiArai | 0:5b88d5760320 | 229 | * function_to_handle_condition(); |
kenjiArai | 0:5b88d5760320 | 230 | * } |
kenjiArai | 0:5b88d5760320 | 231 | * |
kenjiArai | 0:5b88d5760320 | 232 | * mutex.unlock(); |
kenjiArai | 0:5b88d5760320 | 233 | * @endcode |
kenjiArai | 0:5b88d5760320 | 234 | * |
kenjiArai | 0:5b88d5760320 | 235 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 236 | */ |
kenjiArai | 0:5b88d5760320 | 237 | bool wait_until(uint64_t millisec); |
kenjiArai | 0:5b88d5760320 | 238 | |
kenjiArai | 0:5b88d5760320 | 239 | /** Wait for a notification or timeout. |
kenjiArai | 0:5b88d5760320 | 240 | * |
kenjiArai | 0:5b88d5760320 | 241 | * `Wait for` causes the current thread to block until the condition |
kenjiArai | 0:5b88d5760320 | 242 | * variable receives a notification from another thread, or the timeout |
kenjiArai | 0:5b88d5760320 | 243 | * specified by the millisec parameter is reached. |
kenjiArai | 0:5b88d5760320 | 244 | * |
kenjiArai | 0:5b88d5760320 | 245 | * @param millisec Timeout value or osWaitForever in case of no timeout. |
kenjiArai | 0:5b88d5760320 | 246 | * @return `true` if a timeout occurred, `false` otherwise. |
kenjiArai | 0:5b88d5760320 | 247 | * |
kenjiArai | 0:5b88d5760320 | 248 | * @note - The thread calling this function must be the owner of the |
kenjiArai | 0:5b88d5760320 | 249 | * ConditionVariable's mutex, and it must be locked exactly once. |
kenjiArai | 0:5b88d5760320 | 250 | * |
kenjiArai | 0:5b88d5760320 | 251 | * @note - Spurious notifications can occur, so the caller of this API |
kenjiArai | 0:5b88d5760320 | 252 | * should check to make sure the condition the caller is waiting on has |
kenjiArai | 0:5b88d5760320 | 253 | * been met. |
kenjiArai | 0:5b88d5760320 | 254 | * |
kenjiArai | 0:5b88d5760320 | 255 | * @note - The current thread releases the lock while inside the wait |
kenjiArai | 0:5b88d5760320 | 256 | * function and reacquire it upon exiting the function. |
kenjiArai | 0:5b88d5760320 | 257 | * |
kenjiArai | 0:5b88d5760320 | 258 | * Example: |
kenjiArai | 0:5b88d5760320 | 259 | * @code |
kenjiArai | 0:5b88d5760320 | 260 | * mutex.lock(); |
kenjiArai | 0:5b88d5760320 | 261 | * |
kenjiArai | 0:5b88d5760320 | 262 | * while (!condition_met) { |
kenjiArai | 0:5b88d5760320 | 263 | * cond.wait_for(MAX_SLEEP_TIME); |
kenjiArai | 0:5b88d5760320 | 264 | * if (!condition_met) { |
kenjiArai | 0:5b88d5760320 | 265 | * do_other_work_while_condition_false(); |
kenjiArai | 0:5b88d5760320 | 266 | * } |
kenjiArai | 0:5b88d5760320 | 267 | * } |
kenjiArai | 0:5b88d5760320 | 268 | * |
kenjiArai | 0:5b88d5760320 | 269 | * if (condition_met) { |
kenjiArai | 0:5b88d5760320 | 270 | * function_to_handle_condition(); |
kenjiArai | 0:5b88d5760320 | 271 | * } |
kenjiArai | 0:5b88d5760320 | 272 | * |
kenjiArai | 0:5b88d5760320 | 273 | * mutex.unlock(); |
kenjiArai | 0:5b88d5760320 | 274 | * @endcode |
kenjiArai | 0:5b88d5760320 | 275 | * |
kenjiArai | 0:5b88d5760320 | 276 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 277 | */ |
kenjiArai | 0:5b88d5760320 | 278 | bool wait_for(uint32_t millisec); |
kenjiArai | 0:5b88d5760320 | 279 | |
kenjiArai | 0:5b88d5760320 | 280 | /** Notify one waiter on this condition variable that a condition changed. |
kenjiArai | 0:5b88d5760320 | 281 | * |
kenjiArai | 0:5b88d5760320 | 282 | * This function unblocks one of the threads waiting for the condition |
kenjiArai | 0:5b88d5760320 | 283 | * variable. |
kenjiArai | 0:5b88d5760320 | 284 | * |
kenjiArai | 0:5b88d5760320 | 285 | * @note - The thread calling this function must be the owner of the |
kenjiArai | 0:5b88d5760320 | 286 | * ConditionVariable's mutex. |
kenjiArai | 0:5b88d5760320 | 287 | * |
kenjiArai | 0:5b88d5760320 | 288 | * @note - The thread that is unblocked on ConditionVariable::notify_one is |
kenjiArai | 0:5b88d5760320 | 289 | * undefined if there are multiple waiters. |
kenjiArai | 0:5b88d5760320 | 290 | * |
kenjiArai | 0:5b88d5760320 | 291 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 292 | */ |
kenjiArai | 0:5b88d5760320 | 293 | void notify_one(); |
kenjiArai | 0:5b88d5760320 | 294 | |
kenjiArai | 0:5b88d5760320 | 295 | /** Notify all waiters on this condition variable that a condition changed. |
kenjiArai | 0:5b88d5760320 | 296 | * |
kenjiArai | 0:5b88d5760320 | 297 | * This function unblocks all of the threads waiting for the condition |
kenjiArai | 0:5b88d5760320 | 298 | * variable. |
kenjiArai | 0:5b88d5760320 | 299 | * |
kenjiArai | 0:5b88d5760320 | 300 | * @note - The thread calling this function must be the owner of the |
kenjiArai | 0:5b88d5760320 | 301 | * ConditionVariable's mutex. |
kenjiArai | 0:5b88d5760320 | 302 | * |
kenjiArai | 0:5b88d5760320 | 303 | * @note - If there are one or more waiters and one or more threads |
kenjiArai | 0:5b88d5760320 | 304 | * attempting to acquire the condition variable's mutex the order in which |
kenjiArai | 0:5b88d5760320 | 305 | * the mutex is acquired is undefined. |
kenjiArai | 0:5b88d5760320 | 306 | * |
kenjiArai | 0:5b88d5760320 | 307 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 308 | */ |
kenjiArai | 0:5b88d5760320 | 309 | void notify_all(); |
kenjiArai | 0:5b88d5760320 | 310 | |
kenjiArai | 0:5b88d5760320 | 311 | /** ConditionVariable destructor. |
kenjiArai | 0:5b88d5760320 | 312 | * |
kenjiArai | 0:5b88d5760320 | 313 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 314 | */ |
kenjiArai | 0:5b88d5760320 | 315 | ~ConditionVariable(); |
kenjiArai | 0:5b88d5760320 | 316 | |
kenjiArai | 0:5b88d5760320 | 317 | #if !defined(DOXYGEN_ONLY) |
kenjiArai | 0:5b88d5760320 | 318 | protected: |
kenjiArai | 0:5b88d5760320 | 319 | struct Waiter { |
kenjiArai | 0:5b88d5760320 | 320 | Waiter(); |
kenjiArai | 0:5b88d5760320 | 321 | Semaphore sem; |
kenjiArai | 0:5b88d5760320 | 322 | Waiter *prev; |
kenjiArai | 0:5b88d5760320 | 323 | Waiter *next; |
kenjiArai | 0:5b88d5760320 | 324 | bool in_list; |
kenjiArai | 0:5b88d5760320 | 325 | }; |
kenjiArai | 0:5b88d5760320 | 326 | |
kenjiArai | 0:5b88d5760320 | 327 | static void _add_wait_list(Waiter **wait_list, Waiter *waiter); |
kenjiArai | 0:5b88d5760320 | 328 | static void _remove_wait_list(Waiter **wait_list, Waiter *waiter); |
kenjiArai | 0:5b88d5760320 | 329 | Mutex &_mutex; |
kenjiArai | 0:5b88d5760320 | 330 | Waiter *_wait_list; |
kenjiArai | 0:5b88d5760320 | 331 | #endif // !defined(DOXYGEN_ONLY) |
kenjiArai | 0:5b88d5760320 | 332 | }; |
kenjiArai | 0:5b88d5760320 | 333 | |
kenjiArai | 1:9db0e321a9f4 | 334 | /** @}*/ |
kenjiArai | 1:9db0e321a9f4 | 335 | /** @}*/ |
kenjiArai | 1:9db0e321a9f4 | 336 | } // namespace rtos |
kenjiArai | 0:5b88d5760320 | 337 | #endif |
kenjiArai | 0:5b88d5760320 | 338 | |
kenjiArai | 1:9db0e321a9f4 | 339 | #endif |