Class: Sumac::WorkerPool

Inherits:
Object show all
Defined in:
lib/sumac/worker_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(size = 1, duck_types: {}) ⇒ WorkerPool

Returns a new instance of WorkerPool.



4
5
6
7
8
9
# File 'lib/sumac/worker_pool.rb', line 4

def initialize(size = 1, duck_types: {})
  raise 'Error: worker count invalid' if size < 1
  @thread_class = duck_types[:thread] || Thread
  @semaphore = QuackConcurrency::Semaphore.new(size)
  @threads = []
end

Instance Method Details

#joinObject



29
30
31
32
# File 'lib/sumac/worker_pool.rb', line 29

def join
  @threads.each(&:join)
  nil
end

#run(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/sumac/worker_pool.rb', line 19

def run(&block)
  @semaphore.acquire
  @threads << @thread_class.new do
    block.call
    @threads.delete(@thread_class.current)
    @semaphore.release
  end
  nil
end

#sizeObject



11
12
13
# File 'lib/sumac/worker_pool.rb', line 11

def size
  @semaphore.permit_count
end

#size=(new_size) ⇒ Object



15
16
17
# File 'lib/sumac/worker_pool.rb', line 15

def size=(new_size)
  @semaphore.set_permit_count!(new_size)
end