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

#initializevoid

Initializes a new worker pool.



16
17
18
19
20
21
22
23
# File 'lib/uringmachine/fiber_scheduler.rb', line 16

def initialize
  @pending_count = 0
  @worker_count = 0
  @max_workers = Etc.nprocessors
  @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



31
32
33
34
35
36
37
38
# File 'lib/uringmachine/fiber_scheduler.rb', line 31

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