Introduce Fiber Scheduler blocking_region hook. (#11963)

This commit is contained in:
Samuel Williams 2024-10-31 17:26:37 +13:00 committed by GitHub
parent 550ac2f2ed
commit 87fb44dff6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
Notes: git 2024-10-31 04:26:56 +00:00
Merged-By: ioquatix <samuel@codeotaku.com>
9 changed files with 118 additions and 12 deletions

View file

@ -309,6 +309,10 @@ class Scheduler
Addrinfo.getaddrinfo(hostname, nil).map(&:ip_address).uniq
end.value
end
def blocking_region(work)
Thread.new(&work).join
end
end
# This scheduler class implements `io_read` and `io_write` hooks which require
@ -321,8 +325,7 @@ class IOBufferScheduler < Scheduler
io.nonblock = true
while true
maximum_size = buffer.size - offset
result = blocking{buffer.read(io, maximum_size, offset)}
result = blocking{buffer.read(io, 0, offset)}
if result > 0
total += result
@ -349,8 +352,7 @@ class IOBufferScheduler < Scheduler
io.nonblock = true
while true
maximum_size = buffer.size - offset
result = blocking{buffer.write(io, maximum_size, offset)}
result = blocking{buffer.write(io, 0, offset)}
if result > 0
total += result
@ -377,8 +379,7 @@ class IOBufferScheduler < Scheduler
io.nonblock = true
while true
maximum_size = buffer.size - offset
result = blocking{buffer.pread(io, from, maximum_size, offset)}
result = blocking{buffer.pread(io, from, 0, offset)}
if result > 0
total += result
@ -406,8 +407,7 @@ class IOBufferScheduler < Scheduler
io.nonblock = true
while true
maximum_size = buffer.size - offset
result = blocking{buffer.pwrite(io, from, maximum_size, offset)}
result = blocking{buffer.pwrite(io, from, 0, offset)}
if result > 0
total += result

View file

@ -153,12 +153,13 @@ class TestFiberIO < Test::Unit::TestCase
Fiber.set_scheduler scheduler
Fiber.schedule do
message = i.read(20)
# We add 1 here, to force the read to block (testing that specific code path).
message = i.read(MESSAGE.bytesize + 1)
i.close
end
Fiber.schedule do
o.write("Hello World")
o.write(MESSAGE)
o.close
end
end

View file

@ -21,7 +21,8 @@ class TestFiberIOBuffer < Test::Unit::TestCase
Fiber.set_scheduler scheduler
Fiber.schedule do
message = i.read(20)
# We add 1 here, to force the read to block (testing that specific code path).
message = i.read(MESSAGE.bytesize + 1)
i.close
end

View file

@ -59,12 +59,14 @@ class TestFiberProcess < Test::Unit::TestCase
def test_fork
omit 'fork not supported' unless Process.respond_to?(:fork)
pid = Process.fork{}
Thread.new do
scheduler = Scheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
pid = Process.fork {}
Process.wait(pid)
assert_predicate $?, :success?