Class: Dnsruby::QueryControl::Queue

Inherits:
Queue
  • Object
show all
Defined in:
lib/dnsruby/query_control.rb

Overview

A normal Queue whose producers also wake deadline/cancellation waiters. Keeping the notification alongside push avoids polling and works on Ruby versions which do not support Queue#pop(timeout:).

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Queue

Returns a new instance of Queue.



16
17
18
19
20
# File 'lib/dnsruby/query_control.rb', line 16

def initialize
  super
  @wait_mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Method Details

#close ⇒ Object



32
33
34
35
36
37
38
# File 'lib/dnsruby/query_control.rb', line 32

def close
  @wait_mutex.synchronize do
    super
    @condition.broadcast
  end
  self
end

#pop_with_control(control) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dnsruby/query_control.rb', line 44

def pop_with_control(control)
  control.add_waiter(self)
  @wait_mutex.synchronize do
    loop do
      control.check!
      begin
        return pop(true)
      rescue ThreadError
        return nil if closed?
        remaining = control.deadline && control.deadline - TransportClock.now
        @condition.wait(@wait_mutex, remaining && [remaining, 0].max)
      end
    end
  end
ensure
  control.remove_waiter(self)
end

#push(value) ⇒ Object Also known as: <<, enq



22
23
24
25
26
27
28
# File 'lib/dnsruby/query_control.rb', line 22

def push(value)
  @wait_mutex.synchronize do
    super
    @condition.broadcast
  end
  self
end

#wake_waiters ⇒ Object



40
41
42
# File 'lib/dnsruby/query_control.rb', line 40

def wake_waiters
  @wait_mutex.synchronize { @condition.broadcast }
end