Class: DispatchQueue::TimerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch_queue_rb/internal/timer_pool.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimerPool

Returns a new instance of TimerPool.



17
18
19
20
21
22
23
# File 'lib/dispatch_queue_rb/internal/timer_pool.rb', line 17

def initialize()
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @heap = Heap.new { |a,b| a.eta < b.eta }
  @scheduled_eta = nil
  @thread = nil
end

Class Method Details

.default_poolObject



13
14
15
# File 'lib/dispatch_queue_rb/internal/timer_pool.rb', line 13

def self.default_pool
  @@default_pool
end

Instance Method Details

#dispatch_after(eta, group: nil, target_queue: nil, &task) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dispatch_queue_rb/internal/timer_pool.rb', line 25

def dispatch_after( eta, group:nil, target_queue:nil, &task )
  group.enter() if group
  target_queue ||= Dispatch.default_queue
  eta = Time.now + eta if !(Time === eta)
  continuation = Continuation.new( target_queue:target_queue, group:group, eta:eta, &task )

  @mutex.synchronize do
    @heap.push( continuation )
    if @scheduled_eta.nil? || eta < @scheduled_eta
      @thread = Thread.new { _thread_main() } if @thread.nil?
      @condition.signal()
    end
  end
  target_queue
end