Skip to content

Commit 090cdcf

Browse files
authored
Remove redundant and error-prone call to .Reset() in our AutoResetEvents (#68)
1 parent a9cfcd6 commit 090cdcf

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

src/DurableEngine/OrchestrationInvoker.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal Hashtable Invoke(IPowerShellServices powerShellServices)
7676
while (true)
7777
{
7878
// block this thread until user-code thread (the PS orchestrator) invokes a DF CmdLet or completes.
79-
var orchestratorReturned = context.SharedMemory.YieldToUserCodeThread(orchestratorReturnedHandle);
79+
var orchestratorReturned = context.SharedMemory.WaitForInvokerThreadTurn(orchestratorReturnedHandle);
8080
if (orchestratorReturned)
8181
{
8282
// The PS orchestrator has a return value, there's no more DF APIs to await.
@@ -109,6 +109,12 @@ internal Hashtable Invoke(IPowerShellServices powerShellServices)
109109
await task.GetDTFxTask();
110110
} // Exceptions are ignored at this point, they will be re-surfaced by the PS code if left unhandled.
111111
catch { }
112+
113+
// Wake up user-code thread. For a small moment, both the user code thread and the invoker thread
114+
// will be running at the same time.
115+
// However, the invoker thread will block itself again at the start of the next loop until the user-code
116+
// thread yields control.
117+
context.SharedMemory.WakeUserCodeThread();
112118
}
113119
};
114120

src/DurableEngine/SharedMemory.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,16 @@ public void YieldToInvokerThread()
4040
{
4141
// Wake invoker thread.
4242
invokerThreadTurn.Set();
43-
44-
// Block user-code thread.
45-
userCodeThreadTurn.Reset();
4643
userCodeThreadTurn.WaitOne();
4744
}
4845

4946
/// <summary>
50-
/// Blocks Orchestration-invoker thread, wakes up user-code thread.
51-
/// This is usually used after the invoker has a result for the PS orchestrator.
47+
/// Blocks Orchestration-invoker thread until the user-code thread completes or yields.
5248
/// </summary>
5349
/// <param name="completionHandle">The WaitHandle tracking if the user-code thread completed.</param>
5450
/// <returns>True if the user-code thread completed, False if it requests an API to be awaited.</returns>
55-
public bool YieldToUserCodeThread(WaitHandle completionHandle)
51+
public bool WaitForInvokerThreadTurn(WaitHandle completionHandle)
5652
{
57-
// Wake user-code thread
58-
userCodeThreadTurn.Set();
59-
60-
// Get invoker thread ready to block
61-
invokerThreadTurn.Reset();
6253

6354
// Wake up when either the user-code returns, or when we're yielded-to for `await`'ing.
6455
var index = WaitHandle.WaitAny(new[] { completionHandle, invokerThreadTurn });
@@ -67,13 +58,12 @@ public bool YieldToUserCodeThread(WaitHandle completionHandle)
6758
}
6859

6960
/// <summary>
70-
/// Blocks user code thread if the orchestrator-invoker thread is currently running.
71-
/// This guarantees that the user-code thread and the orchestration-invoker thread run one
72-
/// at a time after this point.
61+
/// Wakes up the user-code thread without blocking the invoker thread.
62+
/// The invoker thread should block itself afterwards to prevent races.
7363
/// </summary>
74-
public void GuaranteeUserCodeTurn()
64+
public void WakeUserCodeThread()
7565
{
76-
userCodeThreadTurn.WaitOne();
66+
userCodeThreadTurn.Set();
7767
}
7868
}
7969
}

src/DurableEngine/Tasks/DurableTask.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ internal OrchestrationAction GetOrCreateAction()
4949
/// <param name="writeErr">Function to write an exception to the pipeline.</param>
5050
public void Execute(Action<object> write, Action<ErrorRecord> writeErr)
5151
{
52-
// Ensure that a DurableTask in the usercode thread
53-
// only executes while the orchestration-invoker thread is blocked.
54-
OrchestrationContext.SharedMemory.GuaranteeUserCodeTurn();
55-
5652
DurableTask task = this;
5753

5854
if (NoWait)

0 commit comments

Comments
 (0)