Class: Abid::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/abid/waiter.rb

Overview

non-block waiter

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeWaiter

Returns a new instance of Waiter.



7
8
9
10
11
12
13
# File 'lib/abid/waiter.rb', line 7

def initialize
  @cv = ConditionVariable.new
  @mutex = Mutex.new
  @queue = MultiRBTree.new
  @thread = nil
  @error = nil
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/abid/waiter.rb', line 38

def alive?
  @thread.alive?
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/abid/waiter.rb', line 34

def empty?
  @queue.empty?
end

#shutdown(error = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/abid/waiter.rb', line 25

def shutdown(error = nil)
  error ||= RuntimeError.new('waiter is shutting down')
  @mutex.synchronize do
    @error = error
    @queue.each { |_, e| e.ivar.fail(error) }
    @queue.clear
  end
end

#wait(interval: 5, timeout: 60, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/abid/waiter.rb', line 15

def wait(interval: 5, timeout: 60, &block)
  run_thread

  ivar = Concurrent::IVar.new
  now = Time.now.to_f
  next_time = now + interval
  push(Entry.new(ivar, now, next_time, interval, timeout, block))
  ivar
end