From 669cf4c9010ccd906e9b4431faec06a519e249bb Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 31 Oct 2024 17:54:44 +1300 Subject: [PATCH] Add support for `Fiber::Scheduler#blocking_operation_wait`. --- lib/async/scheduler.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/async/scheduler.rb b/lib/async/scheduler.rb index 63e3d4d..c21c30b 100644 --- a/lib/async/scheduler.rb +++ b/lib/async/scheduler.rb @@ -338,6 +338,16 @@ def process_wait(pid, flags) return @selector.process_wait(Fiber.current, pid, flags) end + def blocking_operation_wait(work) + thread = Thread.new(&work) + + thread.join + + thread = nil + ensure + thread&.kill + end + # Run one iteration of the event loop. # # When terminating the event loop, we already know we are finished. So we don't need to check the task tree. This is a logical requirement because `run_once` ignores transient tasks. For example, a single top level transient task is not enough to keep the reactor running, but during termination we must still process it in order to terminate child tasks.