/// <summary> /// Lock a ref. /// </summary> /// <param name="r">The ref to lock.</param> /// <returns>The current value of the ref.</returns> object Lock(Ref r) { bool unlocked = false; try { r.EnterWriteLock(); if (r.CurrentValPoint() > _readPoint) throw _retryex; Info refinfo = r.TInfo; // write lock conflict if (refinfo != null && refinfo != _info && refinfo.IsRunning) { if (!barge(refinfo)) { r.ExitWriteLock(); unlocked = true; // stop prior to blocking stop(RETRY); lock (refinfo) { if (refinfo.IsRunning) { try { Monitor.Wait(refinfo, LOCK_WAIT_MSECS); } catch (ThreadInterruptedException) { } } } throw _retryex; } } r.TInfo = _info; return r.TryGetVal(); } finally { if (!unlocked) { r.ExitWriteLock(); } } }