Sha256: 71143bc73380c9a25c35c24deaf899ca1534d92341ef3de2a19414840fa13927

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'monitor'

module Rpush
  module Daemon
    class InterruptibleSleep
      def initialize(duration)
        @duration = duration
        @stop = false

        @wakeup_obj = Object.new
        @wakeup_obj.extend(MonitorMixin)
        @wakeup_condition = @wakeup_obj.new_cond

        @sleep_obj = Object.new
        @sleep_obj.extend(MonitorMixin)
        @sleep_condition = @sleep_obj.new_cond
      end

      def sleep
        return if @stop
        goto_sleep
        wait_for_wakeup
      end

      def start
        @stop = false

        @thread = Thread.new do
          loop do
            wait_for_sleeper
            break if @stop
            Kernel.sleep(@duration)
            wakeup
          end
        end
      end

      def stop
        @stop = true
        wakeup
        @thread.kill if @thread
      end

      def wakeup
        @wakeup_obj.synchronize { @wakeup_condition.signal }
      end

      private

      def goto_sleep
        @sleep_obj.synchronize { @sleep_condition.signal }
      end

      def wait_for_wakeup
        @wakeup_obj.synchronize { @wakeup_condition.wait(@duration * 2) }
      end

      def wait_for_sleeper
        @sleep_obj.synchronize { @sleep_condition.wait }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rpush-2.3.2-java lib/rpush/daemon/interruptible_sleep.rb
rpush-2.3.2 lib/rpush/daemon/interruptible_sleep.rb