Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal static bool DoTransactionalTryLock<TSessionFunctionsWrapper, TTransacti
// We can't start each retry with a full timeout because we might always fail if someone is not unlocking (e.g. another thread hangs
// somehow while holding a lock, or the current thread has issued two lock calls on two key sets and the second tries to lock one in
// the first, and so on). So set the timeout high enough to accommodate as many retries as you want.
var startTime = DateTime.UtcNow;
var startTimestamp = Stopwatch.GetTimestamp();

Retry:
var prevBucketIndex = -1L;
Expand All @@ -141,7 +141,8 @@ internal static bool DoTransactionalTryLock<TSessionFunctionsWrapper, TTransacti
DoTransactionalUnlock(clientSession, keys[..keyIdx]);

// Lock failure is the only place we check the timeout. If we've exceeded that, or if we've had a cancellation, return false.
if (cancellationToken.IsCancellationRequested || DateTime.UtcNow.Ticks - startTime.Ticks > timeout.Ticks)
// A negative timeout (e.g. Timeout.InfiniteTimeSpan) means wait indefinitely until cancellation.
if (cancellationToken.IsCancellationRequested || (timeout >= TimeSpan.Zero && Stopwatch.GetElapsedTime(startTimestamp) > timeout))
return false;

// No cancellation and we're within the timeout. We've released our locks so this refresh will let other threads advance
Expand All @@ -161,7 +162,7 @@ internal static bool DoManualTryPromoteLock<TSessionFunctionsWrapper, TTransacti
where TSessionFunctionsWrapper : ISessionFunctionsWrapper<TInput, TOutput, TContext, TStoreFunctions, TAllocator>
where TTransactionalKey : ITransactionalKey
{
var startTime = DateTime.UtcNow;
var startTimestamp = Stopwatch.GetTimestamp();
while (true)
{
if (clientSession.store.InternalPromoteLock(key.KeyHash))
Expand All @@ -173,8 +174,9 @@ internal static bool DoManualTryPromoteLock<TSessionFunctionsWrapper, TTransacti
return true;
}

// CancellationToken can accompany either of the other two mechanisms
if (cancellationToken.IsCancellationRequested || DateTime.UtcNow.Ticks - startTime.Ticks > timeout.Ticks)
// CancellationToken can accompany either of the other two mechanisms.
// A negative timeout (e.g. Timeout.InfiniteTimeSpan) means wait indefinitely until cancellation.
if (cancellationToken.IsCancellationRequested || (timeout >= TimeSpan.Zero && Stopwatch.GetElapsedTime(startTimestamp) > timeout))
break; // out of the retry loop

// Lock failed, must retry
Expand Down
Loading