上海虹口龙之梦项目
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.
 
 
 
 

69 lines
1.5 KiB

using System;
using System.Collections;
namespace UniRx
{
public sealed class MultipleAssignmentDisposable : IDisposable, ICancelable
{
static readonly BooleanDisposable True = new BooleanDisposable(true);
object gate = new object();
IDisposable current;
public bool IsDisposed
{
get
{
lock (gate)
{
return current == True;
}
}
}
public IDisposable Disposable
{
get
{
lock (gate)
{
return (current == True)
? UniRx.Disposable.Empty
: current;
}
}
set
{
var shouldDispose = false;
lock (gate)
{
shouldDispose = (current == True);
if (!shouldDispose)
{
current = value;
}
}
if (shouldDispose && value != null)
{
value.Dispose();
}
}
}
public void Dispose()
{
IDisposable old = null;
lock (gate)
{
if (current != True)
{
old = current;
current = True;
}
}
if (old != null) old.Dispose();
}
}
}