-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better pruning for RubyThreadPoolExecutor
- Loading branch information
Showing
8 changed files
with
128 additions
and
107 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
lib/concurrent-ruby/concurrent/collection/ruby_timeout_queue.rb
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,44 @@ | ||
module Concurrent | ||
module Collection | ||
# @!visibility private | ||
# @!macro ruby_timeout_queue | ||
class RubyTimeoutQueue < ::Queue | ||
def initialize(*args) | ||
if RUBY_VERSION >= '3.2' | ||
raise 'RubyTimeoutQueue is not needed on Ruby 3.2 or later, use ::Queue instead' | ||
end | ||
|
||
super(*args) | ||
|
||
@mutex = Mutex.new | ||
@cond_var = ConditionVariable.new | ||
end | ||
|
||
def push(obj) | ||
super(obj).tap { @mutex.synchronize { @cond_var.signal } } | ||
end | ||
alias_method :enq, :push | ||
alias_method :<<, :push | ||
|
||
def pop(non_block = false, timeout: nil) | ||
if non_block && timeout | ||
raise ArgumentError, "can't set a timeout if non_block is enabled" | ||
end | ||
|
||
if non_block | ||
super(true) | ||
elsif timeout && empty? | ||
if @mutex.synchronize { @cond_var.wait(@mutex, timeout) } | ||
super(false) | ||
else | ||
nil | ||
end | ||
else | ||
super(false) | ||
end | ||
end | ||
alias_method :deq, :pop | ||
alias_method :shift, :pop | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
lib/concurrent-ruby/concurrent/collection/timeout_queue.rb
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,18 @@ | ||
module Concurrent | ||
module Collection | ||
# @!visibility private | ||
# @!macro internal_implementation_note | ||
TimeoutQueueImplementation = if RUBY_VERSION >= '3.2' | ||
::Queue | ||
else | ||
require 'concurrent/collection/ruby_timeout_queue' | ||
RubyTimeoutQueue | ||
end | ||
private_constant :TimeoutQueueImplementation | ||
|
||
# @!visibility private | ||
# @!macro timeout_queue | ||
class TimeoutQueue < TimeoutQueueImplementation | ||
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
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
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
Oops, something went wrong.