|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2023 Paul Guyot <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "pico/cond.h" |
| 8 | + |
| 9 | +void cond_init(cond_t *cond) { |
| 10 | + lock_init(&cond->core, next_striped_spin_lock_num()); |
| 11 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 12 | + cond->broadcast_count = 0; |
| 13 | + cond->signaled = false; |
| 14 | + __mem_fence_release(); |
| 15 | +} |
| 16 | + |
| 17 | +bool __time_critical_func(cond_wait_until)(cond_t *cond, mutex_t *mtx, absolute_time_t until) { |
| 18 | + bool success = true; |
| 19 | + lock_owner_id_t caller = lock_get_caller_owner_id(); |
| 20 | + uint32_t save = save_and_disable_interrupts(); |
| 21 | + // Acquire the mutex spin lock |
| 22 | + spin_lock_unsafe_blocking(mtx->core.spin_lock); |
| 23 | + assert(lock_is_owner_id_valid(mtx->owner)); |
| 24 | + assert(caller == mtx->owner); |
| 25 | + |
| 26 | + // Mutex and cond spin locks can be the same as spin locks are attributed |
| 27 | + // using `next_striped_spin_lock_num()`. To avoid any deadlock, we only |
| 28 | + // acquire the condition variable spin lock if it is different from the |
| 29 | + // mutex spin lock |
| 30 | + bool same_spinlock = mtx->core.spin_lock == cond->core.spin_lock; |
| 31 | + |
| 32 | + // Acquire the condition variable spin_lock |
| 33 | + if (!same_spinlock) { |
| 34 | + spin_lock_unsafe_blocking(cond->core.spin_lock); |
| 35 | + } |
| 36 | + |
| 37 | + mtx->owner = LOCK_INVALID_OWNER_ID; |
| 38 | + |
| 39 | + uint64_t current_broadcast = cond->broadcast_count; |
| 40 | + |
| 41 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 42 | + // Release the mutex but without restoring interrupts and notify. |
| 43 | + if (!same_spinlock) { |
| 44 | + spin_unlock_unsafe(mtx->core.spin_lock); |
| 45 | + } |
| 46 | + |
| 47 | + // There is a valid owner of the condition variable: we are not the |
| 48 | + // first waiter. |
| 49 | + // First iteration: notify |
| 50 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 51 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 52 | + // Further iterations: wait |
| 53 | + do { |
| 54 | + if (!lock_is_owner_id_valid(cond->waiter)) { |
| 55 | + break; |
| 56 | + } |
| 57 | + if (cond->broadcast_count != current_broadcast) { |
| 58 | + break; |
| 59 | + } |
| 60 | + if (is_at_the_end_of_time(until)) { |
| 61 | + lock_internal_spin_unlock_with_wait(&cond->core, save); |
| 62 | + } else { |
| 63 | + if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save, until)) { |
| 64 | + // timed out |
| 65 | + success = false; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 70 | + } while (true); |
| 71 | + } else { |
| 72 | + // Release the mutex but without restoring interrupts |
| 73 | + if (!same_spinlock) { |
| 74 | + uint32_t disabled_ints = save_and_disable_interrupts(); |
| 75 | + lock_internal_spin_unlock_with_notify(&mtx->core, disabled_ints); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (success && cond->broadcast_count == current_broadcast) { |
| 80 | + cond->waiter = caller; |
| 81 | + |
| 82 | + // Wait for the signal |
| 83 | + do { |
| 84 | + if (cond->signaled) { |
| 85 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 86 | + cond->signaled = false; |
| 87 | + break; |
| 88 | + } |
| 89 | + if (is_at_the_end_of_time(until)) { |
| 90 | + lock_internal_spin_unlock_with_wait(&cond->core, save); |
| 91 | + } else { |
| 92 | + if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save, until)) { |
| 93 | + // timed out |
| 94 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 95 | + success = false; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 100 | + } while (true); |
| 101 | + } |
| 102 | + |
| 103 | + // We got the signal (or timed out) |
| 104 | + |
| 105 | + if (lock_is_owner_id_valid(mtx->owner)) { |
| 106 | + // Acquire the mutex spin lock and release the core spin lock. |
| 107 | + if (!same_spinlock) { |
| 108 | + spin_lock_unsafe_blocking(mtx->core.spin_lock); |
| 109 | + spin_unlock_unsafe(cond->core.spin_lock); |
| 110 | + } |
| 111 | + |
| 112 | + // Another core holds the mutex. |
| 113 | + // First iteration: notify |
| 114 | + lock_internal_spin_unlock_with_notify(&mtx->core, save); |
| 115 | + save = spin_lock_blocking(mtx->core.spin_lock); |
| 116 | + // Further iterations: wait |
| 117 | + do { |
| 118 | + if (!lock_is_owner_id_valid(mtx->owner)) { |
| 119 | + break; |
| 120 | + } |
| 121 | + // We always wait for the mutex. |
| 122 | + lock_internal_spin_unlock_with_wait(&mtx->core, save); |
| 123 | + save = spin_lock_blocking(mtx->core.spin_lock); |
| 124 | + } while (true); |
| 125 | + } else { |
| 126 | + // Acquire the mutex spin lock and release the core spin lock |
| 127 | + // with notify but without restoring interrupts |
| 128 | + if (!same_spinlock) { |
| 129 | + spin_lock_unsafe_blocking(mtx->core.spin_lock); |
| 130 | + uint32_t disabled_ints = save_and_disable_interrupts(); |
| 131 | + lock_internal_spin_unlock_with_notify(&cond->core, disabled_ints); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Eventually hold the mutex. |
| 136 | + mtx->owner = caller; |
| 137 | + |
| 138 | + // Restore the interrupts now |
| 139 | + spin_unlock(mtx->core.spin_lock, save); |
| 140 | + |
| 141 | + return success; |
| 142 | +} |
| 143 | + |
| 144 | +bool __time_critical_func(cond_wait_timeout_ms)(cond_t *cond, mutex_t *mtx, uint32_t timeout_ms) { |
| 145 | + return cond_wait_until(cond, mtx, make_timeout_time_ms(timeout_ms)); |
| 146 | +} |
| 147 | + |
| 148 | +bool __time_critical_func(cond_wait_timeout_us)(cond_t *cond, mutex_t *mtx, uint32_t timeout_us) { |
| 149 | + return cond_wait_until(cond, mtx, make_timeout_time_us(timeout_us)); |
| 150 | +} |
| 151 | + |
| 152 | +void __time_critical_func(cond_wait)(cond_t *cond, mutex_t *mtx) { |
| 153 | + cond_wait_until(cond, mtx, at_the_end_of_time); |
| 154 | +} |
| 155 | + |
| 156 | +void __time_critical_func(cond_signal)(cond_t *cond) { |
| 157 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 158 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 159 | + // We have a waiter, we can signal. |
| 160 | + cond->signaled = true; |
| 161 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 162 | + } else { |
| 163 | + spin_unlock(cond->core.spin_lock, save); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void __time_critical_func(cond_broadcast)(cond_t *cond) { |
| 168 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 169 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 170 | + // We have a waiter, we can broadcast. |
| 171 | + cond->signaled = true; |
| 172 | + cond->broadcast_count++; |
| 173 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 174 | + } else { |
| 175 | + spin_unlock(cond->core.spin_lock, save); |
| 176 | + } |
| 177 | +} |
0 commit comments