Class: UringMachine::BlockingOperationThreadPool
- Inherits:
-
Object
- Object
- UringMachine::BlockingOperationThreadPool
- 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
-
#initialize(max_workers = Etc.nprocessors) ⇒ void
constructor
Initializes a new worker pool.
-
#process(machine, job) ⇒ any
Processes a request by submitting it to the job queue and waiting for the return value.
Constructor Details
#initialize(max_workers = Etc.nprocessors) ⇒ void
Initializes a new worker pool.
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.
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 |