Skip to content

Commit

Permalink
Optimize locking for cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Neuwirt committed Jul 29, 2019
1 parent 2a7f40e commit a9884bc
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions Source/Miruken/Concurrency/Promise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ public void Cancelled(CancelledCallback cancelled)
{
if (cancelled == null) return;

if (IsCompleted)
{
if (State == PromiseState.Cancelled)
cancelled(_exception as CancelledException);
return;
}

_lock.EnterUpgradeableReadLock();

if (IsCompleted)
Expand All @@ -185,23 +192,23 @@ public void Cancelled(CancelledCallback cancelled)

if (State == PromiseState.Cancelled)
cancelled(_exception as CancelledException);
return;
}
else

_lock.EnterWriteLock();

try
{
_lock.EnterWriteLock();
try
{
_rejected += (ex, s) =>
{
if (ex is CancelledException cancel)
cancelled(cancel);
};
}
finally
_rejected += (ex, s) =>
{
_lock.ExitWriteLock();
_lock.ExitUpgradeableReadLock();
}
if (ex is CancelledException cancel)
cancelled(cancel);
};
}
finally
{
_lock.ExitWriteLock();
_lock.ExitUpgradeableReadLock();
}
}

Expand Down Expand Up @@ -1111,7 +1118,7 @@ protected virtual Promise<R> CreateChild<R>(Promise<R>.PromiseOwner owner)
return new Promise<R>(mode, owner);
}

private void Subscribe(ResolveCallback resolve, RejectCallback reject)
protected void Subscribe(ResolveCallback resolve, RejectCallback reject)
{
if (IsCompleted)
{
Expand Down

0 comments on commit a9884bc

Please sign in to comment.