-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Don't execute the proxy queue while adding to it from same thread. #24565
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
Draft
brendandahl
wants to merge
3
commits into
emscripten-core:main
Choose a base branch
from
brendandahl:proxy-deadlock
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -35,6 +35,8 @@ static em_proxying_queue system_proxying_queue = { | |||||||||
.capacity = 0, | ||||||||||
}; | ||||||||||
|
||||||||||
static _Thread_local int system_queue_in_use = 0; | ||||||||||
|
||||||||||
em_proxying_queue* emscripten_proxy_get_system_queue(void) { | ||||||||||
return &system_proxying_queue; | ||||||||||
} | ||||||||||
|
@@ -110,17 +112,24 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) { | |||||||||
assert(q != NULL); | ||||||||||
assert(pthread_self()); | ||||||||||
|
||||||||||
// Recursion guard to avoid infinite recursion when we arrive here from the | ||||||||||
// pthread_lock call below that executes the system queue. The per-task_queue | ||||||||||
// recursion lock can't catch these recursions because it can only be checked | ||||||||||
// after the lock has been acquired. | ||||||||||
static _Thread_local int executing_system_queue = 0; | ||||||||||
// Below is a recursion and deadlock guard: | ||||||||||
// The recursion guard is to avoid infinite recursion when we arrive here from | ||||||||||
// the pthread_lock call below that executes the system queue. The | ||||||||||
// per-task_queue recursion lock can't catch these recursions because it can | ||||||||||
// only be checked after the lock has been acquired. | ||||||||||
// | ||||||||||
// This also guards against deadlocks when adding to the system queue. When | ||||||||||
// the current thread is adding tasks, it locks the queue, but we can | ||||||||||
// potentially try to execute the queue during the add (from | ||||||||||
// emscripten_yield). This will deadlock the thread, so only try to take the | ||||||||||
Comment on lines
+123
to
+124
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.
Suggested change
|
||||||||||
// lock if the current thread is not using the queue. We then hope the | ||||||||||
// queue is executed later when it is unlocked. | ||||||||||
int is_system_queue = q == &system_proxying_queue; | ||||||||||
if (is_system_queue) { | ||||||||||
if (executing_system_queue) { | ||||||||||
if (system_queue_in_use) { | ||||||||||
return; | ||||||||||
} | ||||||||||
executing_system_queue = 1; | ||||||||||
system_queue_in_use = 1; | ||||||||||
} | ||||||||||
|
||||||||||
pthread_mutex_lock(&q->mutex); | ||||||||||
|
@@ -133,14 +142,21 @@ void emscripten_proxy_execute_queue(em_proxying_queue* q) { | |||||||||
} | ||||||||||
|
||||||||||
if (is_system_queue) { | ||||||||||
executing_system_queue = 0; | ||||||||||
system_queue_in_use = 0; | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
static int do_proxy(em_proxying_queue* q, pthread_t target_thread, task t) { | ||||||||||
assert(q != NULL); | ||||||||||
pthread_mutex_lock(&q->mutex); | ||||||||||
int is_system_queue = q == &system_proxying_queue; | ||||||||||
if (is_system_queue) { | ||||||||||
system_queue_in_use = 1; | ||||||||||
} | ||||||||||
em_task_queue* tasks = get_or_add_tasks_for_thread(q, target_thread); | ||||||||||
if (is_system_queue) { | ||||||||||
system_queue_in_use = 0; | ||||||||||
} | ||||||||||
pthread_mutex_unlock(&q->mutex); | ||||||||||
if (tasks == NULL) { | ||||||||||
return 0; | ||||||||||
|
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 |
---|---|---|
|
@@ -72,6 +72,7 @@ $emscripten_futex_wake | |
$emscripten_stack_get_current | ||
$emscripten_stack_set_limits | ||
$free_ctx | ||
$get_or_add_tasks_for_thread | ||
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. I wonder what happened here. |
||
$get_tasks_for_thread | ||
$init_active_ctxs | ||
$init_file_lock | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
19396 | ||
19446 |
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
2 changes: 1 addition & 1 deletion
2
test/other/codesize/test_codesize_minimal_pthreads_memgrowth.size
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 |
---|---|---|
@@ -1 +1 @@ | ||
19397 | ||
19447 |
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.
We could use
bool
here along withtrue/false
but maybe thats a bigger/unnecessary change.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.
For whatever reason I decided to use
int
instead ofbool
when I wrote all this, so this is at least consistent with the rest of the file.