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.
159 lines
4.2 KiB
159 lines
4.2 KiB
5 years ago
|
#if FAT
|
||
|
|
||
|
using System;
|
||
|
|
||
|
namespace LinqInternal.Threading
|
||
|
{
|
||
|
internal sealed partial class CriticalDisposable : IExtendedDisposable
|
||
|
{
|
||
|
private int _status;
|
||
|
|
||
|
[System.Diagnostics.DebuggerNonUserCode]
|
||
|
~CriticalDisposable()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
// Empty
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Dispose(false);
|
||
|
}
|
||
|
catch (Exception exception)
|
||
|
{
|
||
|
// Catch'em all - fields may be partially collected.
|
||
|
GC.KeepAlive(exception);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public bool IsDisposed
|
||
|
{
|
||
|
[System.Diagnostics.DebuggerNonUserCode]
|
||
|
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
|
||
|
{
|
||
|
System.Threading.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);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return whenDisposed.Invoke();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (whenNotDisposed == null)
|
||
|
{
|
||
|
return default(TReturn);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (ThreadingHelper.SpinWaitRelativeSet(ref _status, 1, -1))
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
return whenNotDisposed.Invoke();
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
System.Threading.Interlocked.Decrement(ref _status);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (whenDisposed == null)
|
||
|
{
|
||
|
return default(TReturn);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return whenDisposed.Invoke();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[System.Diagnostics.DebuggerNonUserCode]
|
||
|
private void Dispose(bool disposeManagedResources)
|
||
|
{
|
||
|
GC.KeepAlive(disposeManagedResources);
|
||
|
if (TakeDisposalExecution())
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
_release();
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
_release = null;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private bool TakeDisposalExecution()
|
||
|
{
|
||
|
return _status != -1 && ThreadingHelper.SpinWaitSetUnless(ref _status, -1, 0, -1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|