Class: Rpush::Daemon::InterruptibleSleep
- Inherits:
-
Object
- Object
- Rpush::Daemon::InterruptibleSleep
- Defined in:
- lib/rpush/daemon/interruptible_sleep.rb
Instance Method Summary collapse
-
#initialize(duration) ⇒ InterruptibleSleep
constructor
A new instance of InterruptibleSleep.
- #sleep ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #wakeup ⇒ Object
Constructor Details
#initialize(duration) ⇒ InterruptibleSleep
Returns a new instance of InterruptibleSleep.
6 7 8 9 10 11 12 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 6 def initialize(duration) @duration = duration @obj = Object.new @obj.extend(MonitorMixin) @condition = @obj.new_cond @stop = false end |
Instance Method Details
#sleep ⇒ Object
14 15 16 17 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 14 def sleep return if @stop @obj.synchronize { @condition.wait(100_000) } end |
#start ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 19 def start @stop = false @thread = Thread.new do loop do break if @stop Kernel.sleep(@duration) wakeup end end end |
#stop ⇒ Object
31 32 33 34 35 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 31 def stop @stop = true wakeup @thread.kill if @thread end |
#wakeup ⇒ Object
37 38 39 |
# File 'lib/rpush/daemon/interruptible_sleep.rb', line 37 def wakeup @obj.synchronize { @condition.signal } end |