public override bool TryLock(TimeSpan durationToWait)
{
var caller = Thread.CurrentThread;
lock (this)
{
if (GetHold(caller)) return true;
if (durationToWait.Ticks <= 0)
return false;
var deadline = DateTime.UtcNow.Add(durationToWait);
try
{
for (;;)
{
Monitor.Wait(this, durationToWait);
if (_owner == null)
{
_owner = caller;
_holds = 1;
return true;
}
durationToWait = deadline.Subtract(DateTime.UtcNow);
if (durationToWait.Ticks <= 0)
return false;
}
}
catch (ThreadInterruptedException e)
{
if (_owner == null) Monitor.Pulse(this);
throw e.PreserveStackTrace();
}
}
}