mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Simplify the implementation of Scheduler#block
* This shows block() with a timeout is similar to #kernel_sleep and also does not need to change `@blocking`.
This commit is contained in:
parent
0fa1c82bfc
commit
2b73e6ba71
1 changed files with 15 additions and 13 deletions
|
@ -103,7 +103,7 @@ class Scheduler
|
||||||
ensure
|
ensure
|
||||||
@closed = true
|
@closed = true
|
||||||
|
|
||||||
# We freeze to detect any inadvertant modifications after the scheduler is closed:
|
# We freeze to detect any unintended modifications after the scheduler is closed:
|
||||||
self.freeze
|
self.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -144,19 +144,21 @@ class Scheduler
|
||||||
# Used when blocking on synchronization (Mutex#lock, Queue#pop, SizedQueue#push, ...)
|
# Used when blocking on synchronization (Mutex#lock, Queue#pop, SizedQueue#push, ...)
|
||||||
def block(blocker, timeout = nil)
|
def block(blocker, timeout = nil)
|
||||||
# p [__method__, blocker, timeout]
|
# p [__method__, blocker, timeout]
|
||||||
@blocking += 1
|
|
||||||
|
|
||||||
if timeout
|
if timeout
|
||||||
@waiting[Fiber.current] = current_time + timeout
|
@waiting[Fiber.current] = current_time + timeout
|
||||||
|
begin
|
||||||
|
Fiber.yield
|
||||||
|
ensure
|
||||||
|
# Remove from @waiting in the case #unblock was called before the timeout expired:
|
||||||
|
@waiting.delete(Fiber.current)
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
@blocking += 1
|
||||||
|
begin
|
||||||
Fiber.yield
|
Fiber.yield
|
||||||
ensure
|
ensure
|
||||||
@blocking -= 1
|
@blocking -= 1
|
||||||
|
end
|
||||||
# Remove from @waiting in the case #unblock was called before the timeout expired:
|
|
||||||
if timeout
|
|
||||||
@waiting.delete(Fiber.current)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue