|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2025 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 | + bool broadcast = false; |
| 20 | + bool mutex_acquired = false; |
| 21 | + lock_owner_id_t caller = lock_get_caller_owner_id(); |
| 22 | + |
| 23 | + // waiter and mtx_core are protected by the cv spin_lock |
| 24 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 25 | + uint64_t current_broadcast = cond->broadcast_count; |
| 26 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 27 | + // There is a valid owner of the condition variable: we are not the |
| 28 | + // first waiter. |
| 29 | + assert(cond->mtx_core.spin_lock == mtx->core.spin_lock); |
| 30 | + |
| 31 | + // wait until it's released |
| 32 | + lock_internal_spin_unlock_with_wait(&cond->core, save); |
| 33 | + do { |
| 34 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 35 | + if (cond->broadcast_count != current_broadcast) { |
| 36 | + // Condition variable was broadcast while we were waiting to |
| 37 | + // own it. |
| 38 | + spin_unlock(cond->core.spin_lock, save); |
| 39 | + broadcast = true; |
| 40 | + break; |
| 41 | + } |
| 42 | + if (!lock_is_owner_id_valid(cond->waiter)) { |
| 43 | + cond->waiter = caller; |
| 44 | + cond->mtx_core = mtx->core; |
| 45 | + spin_unlock(cond->core.spin_lock, save); |
| 46 | + break; |
| 47 | + } |
| 48 | + if (is_at_the_end_of_time(until)) { |
| 49 | + lock_internal_spin_unlock_with_wait(&cond->core, save); |
| 50 | + } else if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save, until)) { |
| 51 | + // timed out |
| 52 | + success = false; |
| 53 | + break; |
| 54 | + } |
| 55 | + } while (true); |
| 56 | + } else { |
| 57 | + cond->waiter = caller; |
| 58 | + cond->mtx_core = mtx->core; |
| 59 | + spin_unlock(cond->core.spin_lock, save); |
| 60 | + } |
| 61 | + |
| 62 | + save = spin_lock_blocking(mtx->core.spin_lock); |
| 63 | + assert(mtx->owner == caller); |
| 64 | + |
| 65 | + if (success && !broadcast) { |
| 66 | + if (cond->signaled) { |
| 67 | + // as an optimization, do not release the mutex. |
| 68 | + cond->signaled = false; |
| 69 | + mutex_acquired = true; |
| 70 | + spin_unlock(mtx->core.spin_lock, save); |
| 71 | + } else { |
| 72 | + // release mutex |
| 73 | + mtx->owner = LOCK_INVALID_OWNER_ID; |
| 74 | + lock_internal_spin_unlock_with_notify(&mtx->core, save); |
| 75 | + do { |
| 76 | + if (cond->signaled) { |
| 77 | + cond->signaled = false; |
| 78 | + if (!lock_is_owner_id_valid(mtx->owner)) { |
| 79 | + // As an optimization, acquire the mutex here |
| 80 | + mtx->owner = caller; |
| 81 | + mutex_acquired = true; |
| 82 | + } |
| 83 | + spin_unlock(mtx->core.spin_lock, save); |
| 84 | + break; |
| 85 | + } |
| 86 | + if (!success) { |
| 87 | + if (!lock_is_owner_id_valid(mtx->owner)) { |
| 88 | + // As an optimization, acquire the mutex here |
| 89 | + mtx->owner = caller; |
| 90 | + mutex_acquired = true; |
| 91 | + } |
| 92 | + spin_unlock(mtx->core.spin_lock, save); |
| 93 | + break; |
| 94 | + } |
| 95 | + if (is_at_the_end_of_time(until)) { |
| 96 | + lock_internal_spin_unlock_with_wait(&mtx->core, save); |
| 97 | + } else if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&mtx->core, save, until)) { |
| 98 | + // timed out |
| 99 | + success = false; |
| 100 | + } |
| 101 | + save = spin_lock_blocking(mtx->core.spin_lock); |
| 102 | + } while (true); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // free the cond var |
| 107 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 108 | + if (cond->waiter == caller) { |
| 109 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 110 | + } |
| 111 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 112 | + |
| 113 | + if (!mutex_acquired) { |
| 114 | + mutex_enter_blocking(mtx); |
| 115 | + } |
| 116 | + |
| 117 | + return success; |
| 118 | +} |
| 119 | + |
| 120 | +bool __time_critical_func(cond_wait_timeout_ms)(cond_t *cond, mutex_t *mtx, uint32_t timeout_ms) { |
| 121 | + return cond_wait_until(cond, mtx, make_timeout_time_ms(timeout_ms)); |
| 122 | +} |
| 123 | + |
| 124 | +bool __time_critical_func(cond_wait_timeout_us)(cond_t *cond, mutex_t *mtx, uint32_t timeout_us) { |
| 125 | + return cond_wait_until(cond, mtx, make_timeout_time_us(timeout_us)); |
| 126 | +} |
| 127 | + |
| 128 | +void __time_critical_func(cond_wait)(cond_t *cond, mutex_t *mtx) { |
| 129 | + cond_wait_until(cond, mtx, at_the_end_of_time); |
| 130 | +} |
| 131 | + |
| 132 | +void __time_critical_func(cond_signal)(cond_t *cond) { |
| 133 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 134 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 135 | + lock_core_t mtx_core = cond->mtx_core; |
| 136 | + // spin_locks can be identical |
| 137 | + if (mtx_core.spin_lock != cond->core.spin_lock) { |
| 138 | + spin_unlock(cond->core.spin_lock, save); |
| 139 | + save = spin_lock_blocking(mtx_core.spin_lock); |
| 140 | + } |
| 141 | + cond->signaled = true; |
| 142 | + lock_internal_spin_unlock_with_notify(&mtx_core, save); |
| 143 | + } else { |
| 144 | + spin_unlock(cond->core.spin_lock, save); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void __time_critical_func(cond_broadcast)(cond_t *cond) { |
| 149 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 150 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 151 | + cond->broadcast_count++; |
| 152 | + lock_core_t mtx_core = cond->mtx_core; |
| 153 | + // spin_locks can be identical |
| 154 | + if (mtx_core.spin_lock != cond->core.spin_lock) { |
| 155 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 156 | + save = spin_lock_blocking(mtx_core.spin_lock); |
| 157 | + } |
| 158 | + cond->signaled = true; |
| 159 | + lock_internal_spin_unlock_with_notify(&mtx_core, save); |
| 160 | + } else { |
| 161 | + spin_unlock(cond->core.spin_lock, save); |
| 162 | + } |
| 163 | +} |
0 commit comments