forked from Signal65/puma-CodeRabbit
-
Notifications
You must be signed in to change notification settings - Fork 1
ThreadPool concurrency refactoring (#2220) #3
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
Open
MitchLewis930
wants to merge
1
commit into
pr_053_before
Choose a base branch
from
pr_053_after
base: pr_053_before
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||||||||
| 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 | ||||||||||
|
Comment on lines
+217
to
+218
|
||||||||||
| free = @waiting - @todo.size | |
| if (force or free > 0) and @spawned - @trim_requested > @min | |
| free_threads = @waiting - @todo.size | |
| if (force or free_threads > 0) and @spawned - @trim_requested > @min |
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.
Thread cleanup (decrementing @Spawned and removing from @Workers) happens inside the mutex before Thread.exit, but the original code performed cleanup after the thread loop ended. This change means if Thread.exit fails or is interrupted, the cleanup has already occurred, potentially leaving @Spawned and @Workers in an inconsistent state. Consider whether cleanup should remain after the thread exits to ensure accurate accounting.