Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BreakneckLock #1906

Merged
merged 1 commit into from
Jan 21, 2024
Merged

Fix BreakneckLock #1906

merged 1 commit into from
Jan 21, 2024

Conversation

ZingBallyhoo
Copy link
Contributor

Summary of the PR

Fixes the implementation of BreakneckLock (only used in SDL2 event code).
Found this issue when trying to figure out why the SDL2 platform implementation had a low update rate for inputs. (first frame locking fails but actually acquires, second frame "succeeds" to acquire the already acquired lock)

Consider the following code: (similar to platform impl)

var l = new BreakneckLock();
var taken = false;
Console.Out.WriteLine($"IsAcquired before: {l.IsAcquired}");
l.TryEnter(ref taken);
Console.Out.WriteLine($"---- TryEnter ----");
Console.Out.WriteLine($"taken after: {taken}");
Console.Out.WriteLine($"IsAcquired after: {l.IsAcquired}");
taken = false;
l.TryEnter(ref taken);
Console.Out.WriteLine($"---- TryEnter ----");
Console.Out.WriteLine($"taken after 2: {taken}");
Console.Out.WriteLine($"IsAcquired after 2: {l.IsAcquired}");
taken = false;
l.TryEnter(ref taken);
Console.Out.WriteLine($"---- TryEnter ----");
Console.Out.WriteLine($"taken after 3: {taken}");
Console.Out.WriteLine($"IsAcquired after 3: {l.IsAcquired}");

Current implementation:

IsAcquired before: False
---- TryEnter ----    
taken after: False    
IsAcquired after: True
---- TryEnter ----
taken after 2: True
IsAcquired after 2: True
---- TryEnter ----
taken after 3: True
IsAcquired after 3: True

Fixed implementation:

IsAcquired before: False
---- TryEnter ----
taken after: True
IsAcquired after: True
---- TryEnter ----
taken after 2: False
IsAcquired after 2: True
---- TryEnter ----
taken after 3: False
IsAcquired after 3: True

Further Comments

The bug was added in this commit john-h-k/SpinLockSlim@53de583

@ZingBallyhoo ZingBallyhoo requested a review from a team as a code owner January 20, 2024 23:04
@Perksey
Copy link
Member

Perksey commented Jan 20, 2024

cc @john-h-k as the original author

Copy link
Member

@Perksey Perksey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks for your contribution!

@Perksey Perksey enabled auto-merge (squash) January 20, 2024 23:46
@Perksey Perksey merged commit 38d616b into dotnet:main Jan 21, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants