|
| 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 | + // Release the mutex but without restoring interrupts and notify. |
| 38 | + mtx->owner = LOCK_INVALID_OWNER_ID; |
| 39 | + if (!same_spinlock) { |
| 40 | + spin_unlock_unsafe(mtx->core.spin_lock); |
| 41 | + } |
| 42 | + |
| 43 | + uint64_t current_broadcast = cond->broadcast_count; |
| 44 | + |
| 45 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 46 | + // There is a valid owner of the condition variable: we are not the |
| 47 | + // first waiter. |
| 48 | + // First iteration: notify |
| 49 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 50 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 51 | + // Further iterations: wait |
| 52 | + do { |
| 53 | + if (!lock_is_owner_id_valid(cond->waiter)) { |
| 54 | + break; |
| 55 | + } |
| 56 | + if (cond->broadcast_count != current_broadcast) { |
| 57 | + break; |
| 58 | + } |
| 59 | + if (is_at_the_end_of_time(until)) { |
| 60 | + lock_internal_spin_unlock_with_wait(&cond->core, save); |
| 61 | + } else { |
| 62 | + if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save, until)) { |
| 63 | + // timed out |
| 64 | + success = false; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 69 | + } while (true); |
| 70 | + } else { |
| 71 | + // Notify to finish release of mutex |
| 72 | + __sev(); |
| 73 | + } |
| 74 | + |
| 75 | + if (success && cond->broadcast_count == current_broadcast) { |
| 76 | + cond->waiter = caller; |
| 77 | + |
| 78 | + // Wait for the signal |
| 79 | + do { |
| 80 | + if (cond->signaled) { |
| 81 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 82 | + cond->signaled = false; |
| 83 | + break; |
| 84 | + } |
| 85 | + if (is_at_the_end_of_time(until)) { |
| 86 | + lock_internal_spin_unlock_with_wait(&cond->core, save); |
| 87 | + } else { |
| 88 | + if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save, until)) { |
| 89 | + // timed out |
| 90 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 91 | + success = false; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + save = spin_lock_blocking(cond->core.spin_lock); |
| 96 | + } while (true); |
| 97 | + } |
| 98 | + |
| 99 | + // We got the signal (or timed out) |
| 100 | + // Acquire the mutex spin lock and release the core spin lock. |
| 101 | + if (!same_spinlock) { |
| 102 | + spin_lock_unsafe_blocking(mtx->core.spin_lock); |
| 103 | + spin_unlock_unsafe(cond->core.spin_lock); |
| 104 | + } |
| 105 | + |
| 106 | + if (lock_is_owner_id_valid(mtx->owner)) { |
| 107 | + // Another core holds the mutex. |
| 108 | + // First iteration: notify |
| 109 | + lock_internal_spin_unlock_with_notify(&mtx->core, save); |
| 110 | + save = spin_lock_blocking(mtx->core.spin_lock); |
| 111 | + // Further iterations: wait |
| 112 | + do { |
| 113 | + if (!lock_is_owner_id_valid(mtx->owner)) { |
| 114 | + break; |
| 115 | + } |
| 116 | + // We always wait for the mutex. |
| 117 | + lock_internal_spin_unlock_with_wait(&mtx->core, save); |
| 118 | + save = spin_lock_blocking(mtx->core.spin_lock); |
| 119 | + } while (true); |
| 120 | + } else { |
| 121 | + // Notify to finish release of condition variable |
| 122 | + __sev(); |
| 123 | + } |
| 124 | + |
| 125 | + // Eventually hold the mutex. |
| 126 | + mtx->owner = caller; |
| 127 | + spin_unlock(mtx->core.spin_lock, save); |
| 128 | + |
| 129 | + return success; |
| 130 | +} |
| 131 | + |
| 132 | +bool __time_critical_func(cond_wait_timeout_ms)(cond_t *cond, mutex_t *mtx, uint32_t timeout_ms) { |
| 133 | + return cond_wait_until(cond, mtx, make_timeout_time_ms(timeout_ms)); |
| 134 | +} |
| 135 | + |
| 136 | +bool __time_critical_func(cond_wait_timeout_us)(cond_t *cond, mutex_t *mtx, uint32_t timeout_us) { |
| 137 | + return cond_wait_until(cond, mtx, make_timeout_time_us(timeout_us)); |
| 138 | +} |
| 139 | + |
| 140 | +void __time_critical_func(cond_wait)(cond_t *cond, mutex_t *mtx) { |
| 141 | + cond_wait_until(cond, mtx, at_the_end_of_time); |
| 142 | +} |
| 143 | + |
| 144 | +void __time_critical_func(cond_signal)(cond_t *cond) { |
| 145 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 146 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 147 | + // We have a waiter, we can signal. |
| 148 | + cond->signaled = true; |
| 149 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 150 | + } else { |
| 151 | + spin_unlock(cond->core.spin_lock, save); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +void __time_critical_func(cond_broadcast)(cond_t *cond) { |
| 156 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 157 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 158 | + // We have a waiter, we can broadcast. |
| 159 | + cond->signaled = true; |
| 160 | + cond->broadcast_count++; |
| 161 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 162 | + } else { |
| 163 | + spin_unlock(cond->core.spin_lock, save); |
| 164 | + } |
| 165 | +} |
0 commit comments