class QuartzTorrent::InterruptibleSleep
Class that implements a sleep for a specified number of seconds that can be interrupted. When a caller calls sleep, another thread can call wake to wake the sleeper.
Public Class Methods
new()
click to toggle source
# File lib/quartz_torrent/interruptiblesleep.rb, line 7 def initialize @eventRead, @eventWrite = IO.pipe @eventPending = false @mutex = Mutex.new end
Public Instance Methods
sleep(seconds)
click to toggle source
Sleep.
# File lib/quartz_torrent/interruptiblesleep.rb, line 14 def sleep(seconds) if IO.select([@eventRead], nil, nil, seconds) @eventRead.read(1) end end
wake()
click to toggle source
Wake the sleeper.
# File lib/quartz_torrent/interruptiblesleep.rb, line 21 def wake @mutex.synchronize do @eventWrite.print "X" if ! @eventPending end end