New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ThreadPool concurrency refactoring #2220
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -54,7 +54,10 @@ def initialize(min, max, *extra, &block) | |
@reaper = nil | ||
|
||
@mutex.synchronize do | ||
@min.times { spawn_thread } | ||
@min.times do | ||
spawn_thread | ||
@not_full.wait(@mutex) | ||
end | ||
end | ||
|
||
@clean_thread_locals = false | ||
|
@@ -72,7 +75,7 @@ def self.clean_thread_locals | |
# How many objects have yet to be processed by the pool? | ||
# | ||
def backlog | ||
@mutex.synchronize { @todo.size } | ||
with_mutex { @todo.size } | ||
end | ||
|
||
def pool_capacity | ||
|
@@ -99,20 +102,13 @@ def spawn_thread | |
while true | ||
work = nil | ||
|
||
continue = true | ||
|
||
mutex.synchronize do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clever change and much more readable |
||
while todo.empty? | ||
if @trim_requested > 0 | ||
@trim_requested -= 1 | ||
continue = false | ||
not_full.signal | ||
break | ||
end | ||
|
||
if @shutdown | ||
continue = false | ||
break | ||
@spawned -= 1 | ||
@workers.delete th | ||
Thread.exit | ||
end | ||
|
||
@waiting += 1 | ||
|
@@ -121,11 +117,9 @@ def spawn_thread | |
@waiting -= 1 | ||
end | ||
|
||
work = todo.shift if continue | ||
work = todo.shift | ||
end | ||
|
||
break unless continue | ||
|
||
if @clean_thread_locals | ||
ThreadPool.clean_thread_locals | ||
end | ||
|
@@ -136,11 +130,6 @@ def spawn_thread | |
STDERR.puts "Error reached top of thread-pool: #{e.message} (#{e.class})" | ||
end | ||
end | ||
|
||
mutex.synchronize do | ||
@spawned -= 1 | ||
@workers.delete th | ||
end | ||
end | ||
|
||
@workers << th | ||
|
@@ -150,9 +139,15 @@ def spawn_thread | |
|
||
private :spawn_thread | ||
|
||
def with_mutex(&block) | ||
@mutex.owned? ? | ||
yield : | ||
@mutex.synchronize(&block) | ||
end | ||
|
||
# Add +work+ to the todo list for a Thread to pickup and process. | ||
def <<(work) | ||
@mutex.synchronize do | ||
with_mutex do | ||
if @shutdown | ||
raise "Unable to add work while shutting down" | ||
end | ||
|
@@ -197,7 +192,7 @@ def <<(work) | |
# Returns the current number of busy threads, or +nil+ if shutting down. | ||
# | ||
def wait_until_not_full | ||
@mutex.synchronize do | ||
with_mutex do | ||
while true | ||
return if @shutdown | ||
|
||
|
@@ -213,13 +208,14 @@ def wait_until_not_full | |
end | ||
end | ||
|
||
# If too many threads are in the pool, tell one to finish go ahead | ||
# If there are any free threads in the pool, tell one to go ahead | ||
# and exit. If +force+ is true, then a trim request is requested | ||
# even if all threads are being utilized. | ||
# | ||
def trim(force=false) | ||
@mutex.synchronize do | ||
if (force or @waiting > 0) and @spawned - @trim_requested > @min | ||
with_mutex do | ||
free = @waiting - @todo.size | ||
if (force or free > 0) and @spawned - @trim_requested > @min | ||
@trim_requested += 1 | ||
@not_empty.signal | ||
end | ||
|
@@ -229,7 +225,7 @@ def trim(force=false) | |
# If there are dead threads in the pool make them go away while decreasing | ||
# spawned counter so that new healthy threads could be created again. | ||
def reap | ||
@mutex.synchronize do | ||
with_mutex do | ||
dead_workers = @workers.reject(&:alive?) | ||
|
||
dead_workers.each do |worker| | ||
|
@@ -283,8 +279,9 @@ def auto_reap!(timeout=5) | |
# Tell all threads in the pool to exit and wait for them to finish. | ||
# | ||
def shutdown(timeout=-1) | ||
threads = @mutex.synchronize do | ||
threads = with_mutex do | ||
@shutdown = true | ||
@trim_requested = @spawned | ||
@not_empty.broadcast | ||
@not_full.broadcast | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the trim bugfix could also be mentioned