Class: RestCore::ThreadPool::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-core/thread_pool.rb

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



12
13
14
15
16
# File 'lib/rest-core/thread_pool.rb', line 12

def initialize
  @queue = []
  @mutex = Mutex.new
  @condv = ConditionVariable.new
end

Instance Method Details

#<<(task) ⇒ Object



18
19
20
21
22
23
# File 'lib/rest-core/thread_pool.rb', line 18

def << task
  mutex.synchronize do
    queue << task
    condv.signal
  end
end

#clearObject



36
37
38
# File 'lib/rest-core/thread_pool.rb', line 36

def clear
  queue.clear
end

#pop(timeout = 60) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/rest-core/thread_pool.rb', line 25

def pop timeout=60
  mutex.synchronize do
    if queue.empty?
      condv.wait(mutex, timeout)
      queue.shift || lambda{ |_| false } # shutdown idle workers
    else
      queue.shift
    end
  end
end