1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

engine: optimize turbo state update logic

This commit is contained in:
NotAShelf 2025-05-18 04:24:14 +03:00
parent 535a045e8b
commit 899d24d5c6
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF

View file

@ -94,8 +94,24 @@ impl TurboHysteresis {
/// Update the turbo state for hysteresis /// Update the turbo state for hysteresis
fn update_state(&self, new_state: bool) { fn update_state(&self, new_state: bool) {
q // First store the new state, then mark as initialized
// With this, any thread seeing initialized=true will also see the correct state
self.previous_state.store(new_state, Ordering::Release); self.previous_state.store(new_state, Ordering::Release);
self.initialized.store(true, Ordering::Release);
// Already initialized, no need for compare_exchange
if self.initialized.load(Ordering::Relaxed) {
return;
}
// Otherwise, try to set initialized=true (but only if it was false)
self.initialized
.compare_exchange(
false, // expected: not initialized
true, // desired: mark as initialized
Ordering::Release, // success ordering: release for memory visibility
Ordering::Relaxed, // failure ordering: we don't care about the current value on failure
)
.ok(); // Ignore the result - if it fails, it means another thread already initialized it
} }
} }