-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a shared/reusable implementation for multi-locking within strea…
…m operators, I.E. being able to process upstream notifications and downstream notifications at the same time, with different locks, while still preserving notification order. (#893)
- Loading branch information
1 parent
76fd915
commit a02c6d6
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved. | ||
// Roland Pheasant licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
namespace DynamicData; | ||
|
||
internal ref struct SwappableLock | ||
{ | ||
public static SwappableLock CreateAndEnter(object gate) | ||
{ | ||
var result = new SwappableLock() | ||
{ | ||
_gate = gate | ||
}; | ||
|
||
Monitor.Enter(gate, ref result._hasLock); | ||
|
||
return result; | ||
} | ||
|
||
public void SwapTo(object gate) | ||
{ | ||
if (_gate is null) | ||
throw new InvalidOperationException("Lock is not initialized"); | ||
|
||
var hasNewLock = false; | ||
Monitor.Enter(gate, ref hasNewLock); | ||
|
||
if (_hasLock) | ||
Monitor.Exit(_gate); | ||
|
||
_hasLock = hasNewLock; | ||
_gate = gate; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_hasLock && (_gate is not null)) | ||
{ | ||
Monitor.Exit(_gate); | ||
_hasLock = false; | ||
_gate = null; | ||
} | ||
} | ||
|
||
private bool _hasLock; | ||
private object? _gate; | ||
} |