Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/DynamicData/Internal/SwappableLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,73 @@ public static SwappableLock CreateAndEnter(object gate)
return result;
}

#if NET9_0_OR_GREATER
public static SwappableLock CreateAndEnter(Lock gate)
{
Comment thread
dwcullop marked this conversation as resolved.
Outdated
gate.Enter();
return new SwappableLock() { _lockGate = gate };
}
#endif

public void SwapTo(object gate)
{
#if NET9_0_OR_GREATER
if (_gate is null && _lockGate is null)
throw new InvalidOperationException("Lock is not initialized");
#else
if (_gate is null)
throw new InvalidOperationException("Lock is not initialized");
#endif

var hasNewLock = false;
Monitor.Enter(gate, ref hasNewLock);

#if NET9_0_OR_GREATER
if (_lockGate is not null)
{
_lockGate.Exit();
_lockGate = null;
}
else
#endif
if (_hasLock)
Monitor.Exit(_gate);
{
Monitor.Exit(_gate!);
}

_hasLock = hasNewLock;
_gate = gate;
}

#if NET9_0_OR_GREATER
public void SwapTo(Lock gate)
{
if (_lockGate is null && _gate is null)
throw new InvalidOperationException("Lock is not initialized");

gate.Enter();

Comment thread
dwcullop marked this conversation as resolved.
Outdated
if (_lockGate is not null)
_lockGate.Exit();
else if (_hasLock)
Monitor.Exit(_gate!);

_lockGate = gate;
_hasLock = false;
_gate = null;
}
Comment thread
dwcullop marked this conversation as resolved.
Outdated
#endif

public void Dispose()
{
#if NET9_0_OR_GREATER
if (_lockGate is not null)
{
_lockGate.Exit();
_lockGate = null;
}
else
#endif
if (_hasLock && (_gate is not null))
{
Monitor.Exit(_gate);
Expand All @@ -45,4 +95,8 @@ public void Dispose()

private bool _hasLock;
private object? _gate;

#if NET9_0_OR_GREATER
Comment thread
dwcullop marked this conversation as resolved.
Outdated
private Lock? _lockGate;
#endif
}
Loading