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
# File 'lib/rest-core/thread_pool.rb', line 12

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

Instance Method Details

#<<(task) ⇒ Object



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

def << task
  queue << task
  condv.signal
end

#clearObject



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

def clear
  queue.clear
end

#pop(mutex, timeout = 60) ⇒ Object



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

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

#sizeObject



17
18
19
# File 'lib/rest-core/thread_pool.rb', line 17

def size
  @queue.size
end