-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work pool for (more) efficient handling of
blocking_operation_wait
.
- Loading branch information
Showing
3 changed files
with
202 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2024, by Samuel Williams. | ||
|
||
require "etc" | ||
|
||
module Async | ||
# A simple work pool that offloads work to a background thread. | ||
# | ||
# @private | ||
class WorkerPool | ||
class Promise | ||
def initialize(work) | ||
@work = work | ||
@state = :pending | ||
@value = nil | ||
@guard = ::Mutex.new | ||
@condition = ::ConditionVariable.new | ||
@thread = nil | ||
end | ||
|
||
def call | ||
work = nil | ||
|
||
@guard.synchronize do | ||
@thread = ::Thread.current | ||
|
||
return unless work = @work | ||
end | ||
|
||
resolve(work.call) | ||
rescue Exception => error | ||
reject(error) | ||
end | ||
|
||
private def resolve(value) | ||
@guard.synchronize do | ||
@work = nil | ||
@thread = nil | ||
@value = value | ||
@state = :resolved | ||
@condition.broadcast | ||
end | ||
end | ||
|
||
private def reject(error) | ||
@guard.synchronize do | ||
@work = nil | ||
@thread = nil | ||
@value = error | ||
@state = :failed | ||
@condition.broadcast | ||
end | ||
end | ||
|
||
def cancel | ||
@guard.synchronize do | ||
@work = nil | ||
@state = :cancelled | ||
@thread&.raise(Interrupt) | ||
end | ||
end | ||
|
||
def wait | ||
@guard.synchronize do | ||
while @state == :pending | ||
@condition.wait(@guard) | ||
end | ||
|
||
if @state == :failed | ||
raise @value | ||
else | ||
return @value | ||
end | ||
end | ||
end | ||
end | ||
|
||
# A handle to the work being done. | ||
class Worker | ||
def initialize | ||
@work = ::Thread::Queue.new | ||
@thread = ::Thread.new(&method(:run)) | ||
end | ||
|
||
def run | ||
while work = @work.pop | ||
work.call | ||
end | ||
end | ||
|
||
def close | ||
if thread = @thread | ||
@thread = nil | ||
thread.kill | ||
end | ||
end | ||
|
||
# Call the work and notify the scheduler when it is done. | ||
def call(work) | ||
promise = Promise.new(work) | ||
|
||
@work.push(promise) | ||
|
||
promise.wait | ||
end | ||
end | ||
|
||
# Create a new work pool. | ||
# | ||
# @parameter size [Integer] The number of threads to use. | ||
def initialize(size: Etc.nprocessors) | ||
@ready = ::Thread::Queue.new | ||
|
||
size.times do | ||
@ready.push(Worker.new) | ||
end | ||
end | ||
|
||
# Close the work pool. Kills all outstanding work. | ||
def close | ||
if ready = @ready | ||
@ready = nil | ||
ready.close | ||
|
||
while worker = ready.pop | ||
worker.close | ||
end | ||
end | ||
end | ||
|
||
# Offload work to a thread. | ||
# | ||
# @parameter work [Proc] The work to be done. | ||
def call(work) | ||
if ready = @ready | ||
worker = ready.pop | ||
|
||
begin | ||
worker.call(work) | ||
ensure | ||
ready.push(worker) | ||
end | ||
else | ||
raise RuntimeError, "No worker available!" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2022-2024, by Samuel Williams. | ||
# Copyright, 2024, by Patrik Wenger. | ||
|
||
require "async/worker_pool" | ||
require "sus/fixtures/async" | ||
|
||
describe Async::WorkerPool do | ||
include Sus::Fixtures::Async::ReactorContext | ||
|
||
let(:worker_pool) {subject.new(size: 1)} | ||
|
||
it "offloads work to a thread" do | ||
result = worker_pool.call(proc do | ||
Thread.current | ||
end) | ||
|
||
expect(result).not.to be == Thread.current | ||
end | ||
|
||
it "gracefully handles errors" do | ||
expect do | ||
worker_pool.call(proc do | ||
raise ArgumentError, "Oops!" | ||
end) | ||
end.to raise_exception(ArgumentError, message: be == "Oops!") | ||
end | ||
|
||
with "#close" do | ||
it "can be closed" do | ||
worker_pool.close | ||
|
||
expect do | ||
worker_pool.call(proc{}) | ||
end.to raise_exception(RuntimeError) | ||
end | ||
end | ||
end |