Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Sep 5, 2024
1 parent c53a911 commit 8f49ee6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static int PollWasiEventLoopUntilResolved(Task<int> mainTask)
{
while (!mainTask.IsCompleted)
{
WasiEventLoop.DispatchWasiEventLoop();
ThreadPoolWorkQueue.Dispatch();
}
var exception = mainTask.Exception;
if (exception is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,10 +906,7 @@ internal static bool Dispatch()
// thread because it sees a Determining or Scheduled stage, and the current thread is the last thread processing
// work items, the current thread must either see the work item queued by the enqueuer, or it must see a stage of
// Scheduled, and try to dequeue again or request another thread.
#if !TARGET_WASI
// TODO https://github.com/dotnet/runtime/issues/104803
Debug.Assert(workQueue._separated.queueProcessingStage == QueueProcessingStage.Scheduled);
#endif
workQueue._separated.queueProcessingStage = QueueProcessingStage.Determining;
Interlocked.MemoryBarrier();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static class WasiEventLoop
// it will be leaked and stay in this list forever.
// it will also keep the Pollable handle alive and prevent it from being disposed
private static readonly List<PollableHolder> s_pollables = new();
private static bool s_tasksCanceled;
private static bool s_checkScheduled;

internal static Task RegisterWasiPollableHandle(int handle, CancellationToken cancellationToken)
{
Expand All @@ -29,18 +29,24 @@ internal static Task RegisterWasiPollable(Pollable pollable, CancellationToken c
// this will register the pollable holder into s_pollables
var holder = new PollableHolder(pollable, cancellationToken);
s_pollables.Add(holder);

ScheduleCheck();

return holder.taskCompletionSource.Task;
}

// this is not thread safe
internal static void DispatchWasiEventLoop()
internal static void ScheduleCheck()
{
ThreadPoolWorkQueue.Dispatch();
if (s_tasksCanceled)
if (!s_checkScheduled && s_pollables.Count > 0)
{
s_tasksCanceled = false;
return;
s_checkScheduled = true;
ThreadPool.UnsafeQueueUserWorkItem(BlockOnPollables, null);
}
}

internal static void BlockOnPollables(object? _)
{
s_checkScheduled = false;

var holders = new List<PollableHolder>(s_pollables.Count);
var pending = new List<Pollable>(s_pollables.Count);
Expand Down Expand Up @@ -73,6 +79,7 @@ internal static void DispatchWasiEventLoop()
s_pollables.Add(holder);
}
}
ScheduleCheck();
}
}

Expand Down Expand Up @@ -120,11 +127,6 @@ private static void CancelAndDispose(object? s)
return;
}

// Tell event loop to exit early, giving the application a
// chance to quit if the task(s) it is interested in have
// completed.
s_tasksCanceled = true;

// it will be removed from s_pollables on the next run
self.isDisposed = true;
self.pollable.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static partial class ThreadPool
{
// Indicates whether the thread pool should yield the thread from the dispatch loop to the runtime periodically so that
// the runtime may use the thread for processing other work
internal static bool YieldFromDispatchLoop => false;
internal static bool YieldFromDispatchLoop => true;

private const bool IsWorkerTrackingEnabledInConfig = false;

Expand Down

0 comments on commit 8f49ee6

Please sign in to comment.