Class: QuartzTorrent::InterruptibleSleep

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeInterruptibleSleep

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

#wakeObject

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