Class: QuartzTorrent::InterruptibleSleep
- Inherits:
-
Object
- Object
- QuartzTorrent::InterruptibleSleep
- Defined in:
- lib/quartz_torrent/interruptiblesleep.rb
Overview
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.
Instance Method Summary collapse
-
#initialize ⇒ InterruptibleSleep
constructor
A new instance of InterruptibleSleep.
-
#sleep(seconds) ⇒ Object
Sleep.
-
#wake ⇒ Object
Wake the sleeper.
Constructor Details
#initialize ⇒ InterruptibleSleep
Returns a new instance of InterruptibleSleep.
7 8 9 10 11 |
# File 'lib/quartz_torrent/interruptiblesleep.rb', line 7 def initialize @eventRead, @eventWrite = IO.pipe @eventPending = false @mutex = Mutex.new end |
Instance Method Details
#sleep(seconds) ⇒ Object
Sleep.
14 15 16 17 18 |
# File 'lib/quartz_torrent/interruptiblesleep.rb', line 14 def sleep(seconds) if IO.select([@eventRead], nil, nil, seconds) @eventRead.read(1) end end |
#wake ⇒ Object
Wake the sleeper.
21 22 23 24 25 |
# File 'lib/quartz_torrent/interruptiblesleep.rb', line 21 def wake @mutex.synchronize do @eventWrite.print "X" if ! @eventPending end end |