summaryrefslogtreecommitdiff
path: root/test/fiber/scheduler.rb
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-09-21 09:54:08 +1200
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-09-21 11:48:44 +1200
commit70f08f1eed1df4579fef047d28fc3c807183fcfa (patch)
tree75b19e3db5bcb6367f000764bf7f5096fc28e0ed /test/fiber/scheduler.rb
parent596173155a15b6d4a7b04bdaf9218b3e756a0683 (diff)
Make `Thread#join` non-blocking.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3558
Diffstat (limited to 'test/fiber/scheduler.rb')
-rw-r--r--test/fiber/scheduler.rb22
1 files changed, 16 insertions, 6 deletions
diff --git a/test/fiber/scheduler.rb b/test/fiber/scheduler.rb
index 10854aac2c..d93d0f106d 100644
--- a/test/fiber/scheduler.rb
+++ b/test/fiber/scheduler.rb
@@ -22,7 +22,7 @@ class Scheduler
@closed = false
@lock = Mutex.new
- @locking = 0
+ @blocking = 0
@ready = []
end
@@ -47,7 +47,7 @@ class Scheduler
def run
@urgent = IO.pipe
- while @readable.any? or @writable.any? or @waiting.any? or @locking.positive?
+ while @readable.any? or @writable.any? or @waiting.any? or @blocking.positive?
# Can only handle file descriptors up to 1024...
readable, writable = IO.select(@readable.keys + [@urgent.first], @writable.keys, [], next_timeout)
@@ -142,12 +142,22 @@ class Scheduler
end
# Used when blocking on synchronization (Mutex#lock, Queue#pop, SizedQueue#push, ...)
- def block(blocker)
- # p [__method__, blocker]
- @locking += 1
+ def block(blocker, timeout = nil)
+ # p [__method__, blocker, timeout]
+ @blocking += 1
+
+ if timeout
+ @waiting[Fiber.current] = current_time + timeout
+ end
+
Fiber.yield
ensure
- @locking -= 1
+ @blocking -= 1
+
+ # Remove from @waiting in the case #unblock was called before the timeout expired:
+ if timeout
+ @waiting.delete(Fiber.current)
+ end
end
# Used when synchronization wakes up a previously-blocked fiber (Mutex#unlock, Queue#push, ...).