class Reserver

Constants

WakeUp

Attributes

__getobj__[R]
thread_class[R]
time[R]

Public Class Methods

new(time, thread: Thread, &proc) click to toggle source
# File core/lib/reserver.rb, line 15
def initialize(time, thread: Thread, &proc)
  raise ArgumentError.new('Block necessary for Reserver.new') unless block_given?
  @proc = proc
  @thread_class = thread
  case
  when time.is_a?(Time)
    @time = time.freeze
  when time.is_a?(String)
    @time = (Time.parse time).freeze
  when time.is_a?(Integer)
    @time = (Time.new + time).freeze
  else
    raise ArgumentError.new('first argument must be Integer, String or Time')
  end
  Reserver.register(self)
end
register(new) click to toggle source
# File core/lib/reserver.rb, line 46
def register(new)
  atomic do
    (@reservers ||= SortedSet.new) << new
    waiter.run end end
unregister(reserver) click to toggle source
# File core/lib/reserver.rb, line 51
def unregister(reserver)
  @reservers.delete(reserver)
end
waiter() click to toggle source
# File core/lib/reserver.rb, line 55
def waiter
  atomic do
    @waiter = nil if @waiter and not @waiter.alive?
    @waiter ||= Thread.new do
      while !@reservers.empty?
        begin
          reserver = @reservers.first
          sleep_time = reserver.time - Time.now
          if sleep_time <= 0
            @reservers.delete reserver
            reserver.thread_class.new(&reserver)
          else
            Timeout.timeout(1 + sleep_time / 2, WakeUp){ Thread.stop } end
        rescue WakeUp
          ;
        rescue Exception => e
          warn e end end end end end

Public Instance Methods

call() click to toggle source
# File core/lib/reserver.rb, line 32
def call
  @proc.call
  self end
cancel() click to toggle source
# File core/lib/reserver.rb, line 39
def cancel
  Reserver.unregister(self)
end
to_proc() click to toggle source
# File core/lib/reserver.rb, line 36
def to_proc
  @proc end