|
| 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 | + lock_owner_id_t caller = lock_get_caller_owner_id(); |
| 20 | + |
| 21 | + // Try to acquire the condition variable, i.e. be the first waiter. |
| 22 | + // Acquire the condition variable lock first. |
| 23 | + uint32_t save1 = spin_lock_blocking(cond->core.spin_lock); |
| 24 | + uint64_t current_broadcast = cond->broadcast_count; |
| 25 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 26 | + // There is a valid owner of the condition variable: we are not the |
| 27 | + // first waiter. |
| 28 | + lock_internal_spin_unlock_with_notify(&cond->core, save1); |
| 29 | + |
| 30 | + // Release the mutex |
| 31 | + mutex_exit(mtx); |
| 32 | + |
| 33 | + do { |
| 34 | + uint32_t save2 = 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 | + lock_internal_spin_unlock_with_notify(&cond->core, save2); |
| 39 | + break; |
| 40 | + } |
| 41 | + if (!lock_is_owner_id_valid(cond->waiter)) { |
| 42 | + cond->waiter = caller; |
| 43 | + lock_internal_spin_unlock_with_notify(&cond->core, save2); |
| 44 | + break; |
| 45 | + } |
| 46 | + if (is_at_the_end_of_time(until)) { |
| 47 | + lock_internal_spin_unlock_with_wait(&cond->core, save2); |
| 48 | + } else if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save2, until)) { |
| 49 | + // timed out |
| 50 | + success = false; |
| 51 | + break; |
| 52 | + } |
| 53 | + } while (true); |
| 54 | + } else { |
| 55 | + cond->waiter = caller; |
| 56 | + lock_internal_spin_unlock_with_notify(&cond->core, save1); |
| 57 | + |
| 58 | + // Release the mutex |
| 59 | + mutex_exit(mtx); |
| 60 | + } |
| 61 | + |
| 62 | + if (success) { |
| 63 | + // Wait for the signal |
| 64 | + do { |
| 65 | + uint32_t save3 = spin_lock_blocking(cond->core.spin_lock); |
| 66 | + if (cond->signaled) { |
| 67 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 68 | + cond->signaled = false; |
| 69 | + lock_internal_spin_unlock_with_notify(&cond->core, save3); |
| 70 | + break; |
| 71 | + } |
| 72 | + if (!success) { |
| 73 | + cond->waiter = LOCK_INVALID_OWNER_ID; |
| 74 | + lock_internal_spin_unlock_with_notify(&cond->core, save3); |
| 75 | + break; |
| 76 | + } |
| 77 | + if (is_at_the_end_of_time(until)) { |
| 78 | + lock_internal_spin_unlock_with_wait(&cond->core, save3); |
| 79 | + } else if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&cond->core, save3, until)) { |
| 80 | + // timed out |
| 81 | + success = false; |
| 82 | + } |
| 83 | + } while (true); |
| 84 | + } |
| 85 | + |
| 86 | + // Acquire the mutex |
| 87 | + mutex_enter_blocking(mtx); |
| 88 | + |
| 89 | + return success; |
| 90 | +} |
| 91 | + |
| 92 | +bool __time_critical_func(cond_wait_timeout_ms)(cond_t *cond, mutex_t *mtx, uint32_t timeout_ms) { |
| 93 | + return cond_wait_until(cond, mtx, make_timeout_time_ms(timeout_ms)); |
| 94 | +} |
| 95 | + |
| 96 | +bool __time_critical_func(cond_wait_timeout_us)(cond_t *cond, mutex_t *mtx, uint32_t timeout_us) { |
| 97 | + return cond_wait_until(cond, mtx, make_timeout_time_us(timeout_us)); |
| 98 | +} |
| 99 | + |
| 100 | +void __time_critical_func(cond_wait)(cond_t *cond, mutex_t *mtx) { |
| 101 | + cond_wait_until(cond, mtx, at_the_end_of_time); |
| 102 | +} |
| 103 | + |
| 104 | +void __time_critical_func(cond_signal)(cond_t *cond) { |
| 105 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 106 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 107 | + // We have a waiter, we can signal. |
| 108 | + cond->signaled = true; |
| 109 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 110 | + } else { |
| 111 | + spin_unlock(cond->core.spin_lock, save); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void __time_critical_func(cond_broadcast)(cond_t *cond) { |
| 116 | + uint32_t save = spin_lock_blocking(cond->core.spin_lock); |
| 117 | + if (lock_is_owner_id_valid(cond->waiter)) { |
| 118 | + // We have a waiter, we can broadcast. |
| 119 | + cond->signaled = true; |
| 120 | + cond->broadcast_count++; |
| 121 | + lock_internal_spin_unlock_with_notify(&cond->core, save); |
| 122 | + } else { |
| 123 | + spin_unlock(cond->core.spin_lock, save); |
| 124 | + } |
| 125 | +} |
0 commit comments