mirror of
https://github.com/RGBCube/superfreq
synced 2025-07-27 17:07:44 +00:00
engine: fix race condition in turbo state init
This commit is contained in:
parent
ed3ba94f74
commit
13bb434ff1
1 changed files with 6 additions and 6 deletions
|
@ -69,11 +69,8 @@ impl TurboHysteresis {
|
|||
/// Initialize the state with a specific value if not already initialized
|
||||
/// Only one thread should be able to initialize the state
|
||||
fn initialize_with(&self, initial_state: bool) -> bool {
|
||||
// First store the initial state so that it's visible before initialized=true
|
||||
self.previous_state.store(initial_state, Ordering::Release);
|
||||
|
||||
// Try to atomically change initialized from false to true
|
||||
// Now, only one thread can win the initialization race
|
||||
// First, try to atomically change initialized from false to true
|
||||
// Only one thread can win the initialization race
|
||||
match self.initialized.compare_exchange(
|
||||
false, // expected: not initialized
|
||||
true, // desired: mark as initialized
|
||||
|
@ -82,11 +79,14 @@ impl TurboHysteresis {
|
|||
) {
|
||||
Ok(_) => {
|
||||
// We won the race to initialize
|
||||
// Now it's safe to set the initial state since we know we're the only
|
||||
// thread that has successfully marked this as initialized
|
||||
self.previous_state.store(initial_state, Ordering::Release);
|
||||
initial_state
|
||||
}
|
||||
Err(_) => {
|
||||
// Another thread already initialized it.
|
||||
// Read the current state in bitter defeat
|
||||
// Just read the current state value that was set by the winning thread
|
||||
self.previous_state.load(Ordering::Acquire)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue