Skip to content

Commit

Permalink
move timeoutMs == 0 check to PrepareAsyncWaitCore
Browse files Browse the repository at this point in the history
make PrepareAsyncWaitCore static and remove a redundant argument
  • Loading branch information
lambdageek committed Apr 20, 2023
1 parent 8662099 commit 0a9d8f7
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private sealed record WaitEntry (LowLevelLifoAsyncWaitSemaphore Semaphore, Actio

public void PrepareAsyncWait(int timeoutMs, Action<LowLevelLifoAsyncWaitSemaphore, object?> onSuccess, Action<LowLevelLifoAsyncWaitSemaphore, object?> onTimeout, object? state)
{
//FIXME(ak): the async wait never spins. Shoudl we spin a little?
Debug.Assert(timeoutMs >= -1);

// Try to acquire the semaphore or
Expand Down Expand Up @@ -112,16 +111,12 @@ private void PrepareAsyncWaitForSignal(int timeoutMs, Action<LowLevelLifoAsyncWa

_onWait();

if (timeoutMs == 0) {
onTimeout (this, state);
return;
}
WaitEntry we = new WaitEntry(this, onSuccess, onTimeout, state)
{
TimeoutMs = timeoutMs,
StartWaitTicks = timeoutMs != -1 ? Environment.TickCount : 0,
};
PrepareAsyncWaitCore(timeoutMs, we);
PrepareAsyncWaitCore(we);
// on success calls InternalAsyncWaitSuccess, on timeout calls InternalAsyncWaitTimeout
}

Expand Down Expand Up @@ -180,17 +175,23 @@ private static void InternalAsyncWaitSuccess(LowLevelLifoAsyncWaitSemaphore self
we.TimeoutMs = 0;
we.StartWaitTicks = endWaitTicks;
}
self.PrepareAsyncWaitCore (we.TimeoutMs, we);
PrepareAsyncWaitCore (we);
// on success calls InternalAsyncWaitSuccess, on timeout calls InternalAsyncWaitTimeout
}

private void PrepareAsyncWaitCore(int timeout_ms, WaitEntry internalWaitEntry)
private static void PrepareAsyncWaitCore(WaitEntry internalWaitEntry)
{
int timeoutMs = internalWaitEntry.TimeoutMs;
LowLevelLifoAsyncWaitSemaphore semaphore = internalWaitEntry.Semaphore;
if (timeoutMs == 0) {
internalWaitEntry.OnTimeout (semaphore, internalWaitEntry.State);
return;
}
GCHandle gchandle = GCHandle.Alloc (internalWaitEntry);
unsafe {
delegate* unmanaged<IntPtr, IntPtr, void> successCallback = &SuccessCallback;
delegate* unmanaged<IntPtr, IntPtr, void> timeoutCallback = &TimeoutCallback;
PrepareAsyncWaitInternal (lifo_semaphore, timeout_ms, successCallback, timeoutCallback, GCHandle.ToIntPtr(gchandle));
PrepareAsyncWaitInternal (semaphore.lifo_semaphore, timeoutMs, successCallback, timeoutCallback, GCHandle.ToIntPtr(gchandle));
}
}

Expand Down

0 comments on commit 0a9d8f7

Please sign in to comment.