Repository navigation
Decide and apply a Toggle under one lock, so concurrent toggles each flip once [patch] - #31
Merged
Merged
Conversation
…flip once [patch] Toggle read IsActive, then called SetActive, then read IsActive again, each under its own acquisition of the gate. Two threads toggling an inactive controller could both read "off", both ask for "on", and both return true, leaving it on after two flips. SetActive now takes a function from the current state to the wanted one and runs it inside the lock that applies the change, returning the state it left. Toggle returns that instead of re-reading. Fixes #30. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4T6sUBiRNom98NuYi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4T6sUBiRNom98NuYi
|
This was referenced Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #30
What was wrong
KeepAwakeController.Toggle()readIsActive, calledSetActive(!IsActive), and then readIsActiveagain, takinggateseparately each time. When two threads toggled an inactive controller, both could read "off" and both ask for "on". Both calls then returnedtrue, and the controller stayed on after two flips.Change
SetActivenow takes aFunc<bool, bool>from the current state to the state wanted. It evaluates that inside the lock that applies the change, and returns the state it left.Enable/Disablepass constant functions.Togglepassescurrent => !currentand returns the result, instead of re-readingIsActive.StateChangedis still raised after the lock is released.Tests
FakeSleepBlockergains anAcquireDelaythat widens the race window.Toggle_ConcurrentCallsEachFlipOnceAndReportTheirOwnResult: 200 trials of 4 threads released together by aBarrier. Each trial asserts the final state isN % 2 == 1, and that the return values contain exactly ⌈N/2⌉trueand ⌊N/2⌋false.Generated by Claude Code