Class: UringMachine::BlockingOperationThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/uringmachine/fiber_scheduler.rb

Overview

Implements a worker thread pool for running blocking operations. Worker threads are started as needed. Worker thread count is limited to the number of CPU cores available.

Instance Method Summary collapse

Constructor Details

#initialize(max_workers = Etc.nprocessors) ⇒ void

Initializes a new worker pool.

Parameters:

  • max_workers (Integer) (defaults to: Etc.nprocessors)

    maximum worker thread count



18
19
20
21
22
23
24
25
26
# File 'lib/uringmachine/fiber_scheduler.rb', line 18

def initialize(max_workers = Etc.nprocessors)
  @max_workers = max_workers
  @pending_count = 0
  @worker_count = 0

  @worker_mutex = UM::Mutex.new
  @job_queue = UM::Queue.new
  @workers = []
end

Instance Method Details

#process(machine, job) ⇒ any

Processes a request by submitting it to the job queue and waiting for the return value. Starts a worker if needed.

Parameters:

  • machine (UringMachine)

    machine

  • job (any)

    callable job object

Returns:

  • (any)

    return value



34
35
36
37
38
39
40
41
# File 'lib/uringmachine/fiber_scheduler.rb', line 34

def process(machine, job)
  queue = Fiber.current.mailbox
  if @worker_count == 0 || (@pending_count > 0 && @worker_count < @max_workers)
    start_worker(machine)
  end
  machine.push(@job_queue, [queue, job])
  machine.shift(queue)
end