Skip to content

Commit

Permalink
Add support for Fiber::Scheduler#blocking_operation_wait. (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Nov 21, 2024
1 parent b6bfc3c commit c317990
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,25 @@ def process_wait(pid, flags)
return @selector.process_wait(Fiber.current, pid, flags)
end

# Wait for the given work to be executed.
#
# @public Since *Async v2.19* and *Ruby v3.4*.
# @asynchronous May be non-blocking.
#
# @parameter work [Proc] The work to execute on a background thread.
# @returns [Object] The result of the work.
def blocking_operation_wait(work)
thread = Thread.new(&work)

result = thread.join

thread = nil

return result
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.
Expand Down
13 changes: 13 additions & 0 deletions test/io/buffer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

describe IO::Buffer do
it "can copy a large buffer (releasing the GVL)" do
source = IO::Buffer.new(1024 * 1024 * 10)
destination = IO::Buffer.new(source.size)

source.copy(destination)
end
end

0 comments on commit c317990

Please sign in to comment.