You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.6 KiB
144 lines
3.6 KiB
#if FAT |
|
|
|
using System; |
|
using System.Threading; |
|
|
|
namespace LinqInternal.Threading |
|
{ |
|
internal sealed partial class NoReentrantReadWriteLock : IExtendedDisposable |
|
{ |
|
private int _status; |
|
|
|
[System.Diagnostics.DebuggerNonUserCode] |
|
~NoReentrantReadWriteLock() |
|
{ |
|
try |
|
{ |
|
// Empty |
|
} |
|
finally |
|
{ |
|
Dispose(false); |
|
} |
|
} |
|
|
|
public bool IsDisposed |
|
{ |
|
get { return _status == -1; } |
|
} |
|
|
|
[System.Diagnostics.DebuggerNonUserCode] |
|
public void Dispose() |
|
{ |
|
try |
|
{ |
|
Dispose(true); |
|
} |
|
finally |
|
{ |
|
GC.SuppressFinalize(this); |
|
} |
|
} |
|
|
|
[System.Diagnostics.DebuggerNonUserCode] |
|
public void DisposedConditional(Action whenDisposed, Action whenNotDisposed) |
|
{ |
|
if (_status == -1) |
|
{ |
|
if (whenDisposed != null) |
|
{ |
|
whenDisposed.Invoke(); |
|
} |
|
} |
|
else |
|
{ |
|
if (whenNotDisposed != null) |
|
{ |
|
if (ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1)) |
|
{ |
|
try |
|
{ |
|
whenNotDisposed.Invoke(); |
|
} |
|
finally |
|
{ |
|
Interlocked.Decrement(ref _status); |
|
} |
|
} |
|
else |
|
{ |
|
if (whenDisposed != null) |
|
{ |
|
whenDisposed.Invoke(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
[System.Diagnostics.DebuggerNonUserCode] |
|
public TReturn DisposedConditional<TReturn>(Func<TReturn> whenDisposed, Func<TReturn> whenNotDisposed) |
|
{ |
|
if (_status == -1) |
|
{ |
|
if (whenDisposed == null) |
|
{ |
|
return default(TReturn); |
|
} |
|
return whenDisposed.Invoke(); |
|
} |
|
if (whenNotDisposed == null) |
|
{ |
|
return default(TReturn); |
|
} |
|
if (ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1)) |
|
{ |
|
try |
|
{ |
|
return whenNotDisposed.Invoke(); |
|
} |
|
finally |
|
{ |
|
Interlocked.Decrement(ref _status); |
|
} |
|
} |
|
if (whenDisposed == null) |
|
{ |
|
return default(TReturn); |
|
} |
|
return whenDisposed.Invoke(); |
|
} |
|
|
|
[System.Diagnostics.DebuggerNonUserCode] |
|
private void Dispose(bool disposeManagedResources) |
|
{ |
|
if (TakeDisposalExecution()) |
|
{ |
|
try |
|
{ |
|
if (disposeManagedResources) |
|
{ |
|
_freeToRead.Dispose(); |
|
_freeToWrite.Dispose(); |
|
} |
|
} |
|
finally |
|
{ |
|
_freeToRead = null; |
|
_freeToWrite = null; |
|
} |
|
} |
|
} |
|
|
|
private bool TakeDisposalExecution() |
|
{ |
|
if (_status == -1) |
|
{ |
|
return false; |
|
} |
|
return ThreadingHelper.SpinWaitSetUnless(ref _status, -1, 0, -1); |
|
} |
|
} |
|
} |
|
|
|
#endif |