Sha256: 73001073df5c45a71331cbff8b183675a980632f1dc0c22e1a63bfcb219e2ccd
Contents?: true
Size: 1.11 KB
Versions: 13
Compression:
Stored size: 1.11 KB
Contents
module Workhorse # Abstraction layer of a simple thread pool implementation used by the worker. class Pool attr_reader :mutex def initialize(size) @size = size @executor = Concurrent::ThreadPoolExecutor.new( min_threads: 0, max_threads: @size, max_queue: 0, fallback_policy: :abort, auto_terminate: false ) @mutex = Mutex.new @active_threads = Concurrent::AtomicFixnum.new(0) end # Posts a new work unit to the pool. def post mutex.synchronize do if idle.zero? fail 'All threads are busy.' end active_threads = @active_threads active_threads.increment @executor.post do begin yield ensure active_threads.decrement end end end end # Returns the number of idle threads. def idle @size - @active_threads.value end def wait @executor.wait_for_termination end # Shuts down the pool and waits for termination. def shutdown @executor.shutdown wait end end end
Version data entries
13 entries across 13 versions & 1 rubygems