Sha256: ce67fd914f43f8d50506c44421a9b3195307b28e1b914429c87686a21f92adda

Contents?: true

Size: 583 Bytes

Versions: 5

Compression:

Stored size: 583 Bytes

Contents

# frozen_string_literal: true

export :process, :setup, :size=, :busy?

@size = 10

def process(&block)
  setup unless @task_queue

  watcher = Gyro::Async.new
  @task_queue << [block, watcher]
  watcher.await
end

def size=(size)
  @size = size
end

def busy?
  !@queue.empty?
end

def setup
  @task_queue = ::Queue.new
  @threads = (1..@size).map { Thread.new { thread_loop } }
end

def thread_loop
  loop { run_queued_task }
end

def run_queued_task
  (block, watcher) = @task_queue.pop
  result = block.()
  watcher.signal!(result)
rescue Exception => e
  watcher.signal!(e)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
polyphony-0.27 lib/polyphony/core/thread_pool.rb
polyphony-0.26 lib/polyphony/core/thread_pool.rb
polyphony-0.25 lib/polyphony/core/thread_pool.rb
polyphony-0.24 lib/polyphony/core/thread_pool.rb
polyphony-0.23 lib/polyphony/core/thread_pool.rb