-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Disallow blocking on the main thread by default #9579
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
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
0263dae
wip [ci skip] need link to good length docs page
kripken b8f47c0
Merge remote-tracking branch 'origin/incoming' into pj
kripken 9197b79
[ci skip]
kripken ac6d56c
Merge remote-tracking branch 'origin/incoming' into pj
kripken 37d78fb
wip [ci skip]
kripken 68e1a88
[ci skip]
kripken 27b72cc
test
kripken 2e5e63e
more tests
kripken 905e668
more
kripken b24df05
fix
kripken 37f275e
wip [ci skip]
kripken 12c1915
fix
kripken f254984
Merge remote-tracking branch 'origin/incoming' into pj
kripken 479058c
text
kripken b366dc8
fix
kripken 5ae5bbe
fix typo
kripken 586cc93
Merge remote-tracking branch 'origin/incoming' into pj
kripken b21d2c7
feedback
kripken 9d8c79b
docs
kripken 7a0dc20
support pthread_tryjoin_np
kripken ec5c4ad
document pthread_tryjoin_np
kripken 6247193
Merge remote-tracking branch 'origin/incoming' into pj
kripken b34922c
Merge remote-tracking branch 'origin/incoming' into pj
kripken 6000e20
emscripten_block_on_main_thread => emscripten_check_blocking_allowed
kripken 3529e3f
mention pthread_tryjoin_np in changelog [ci skip]
kripken 9f34886
Merge branch 'incoming' into pj
kripken 18ffe28
Merge remote-tracking branch 'origin/incoming' into pj
kripken fa2882f
just warn for now
kripken 3158218
flake8
kripken a59669f
Merge remote-tracking branch 'origin/incoming' into pj
kripken a0ebf2f
feedback [ci skip]
kripken b3bfc83
tryjoin without a pool
kripken 345e0e7
Make sure we test failed tryjoins
kripken 2bc7ce4
text nit [ci skip]
kripken 24318b2
more
kripken 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| .. Asyncify: | ||
| .. _Asyncify: | ||
|
|
||
| ======================== | ||
| Asyncify | ||
|
|
||
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
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
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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright 2019 The Emscripten Authors. All rights reserved. | ||
| // Emscripten is available under two separate licenses, the MIT license and the | ||
| // University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <assert.h> | ||
| #include <emscripten.h> | ||
| #include <pthread.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include <atomic> | ||
|
|
||
| pthread_t thread; | ||
|
|
||
| std::atomic<int> tries; | ||
|
|
||
| static const int EXPECTED_TRIES = 7; | ||
|
|
||
| void loop() { | ||
| void* retval; | ||
| printf("try...\n"); | ||
| if (pthread_tryjoin_np(thread, &retval) == 0) { | ||
|
kripken marked this conversation as resolved.
|
||
| emscripten_cancel_main_loop(); | ||
| assert(tries.load() == EXPECTED_TRIES); | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(2); | ||
| #endif | ||
| } | ||
| tries++; | ||
| } | ||
|
|
||
| void *ThreadMain(void *arg) { | ||
| #ifdef TRY_JOIN | ||
| // Delay to force the main thread to try and fail a few times before | ||
| // succeeding. | ||
| while (tries.load() < EXPECTED_TRIES) {} | ||
| #endif | ||
| pthread_exit((void*)0); | ||
| } | ||
|
|
||
| pthread_t CreateThread() { | ||
| pthread_t ret; | ||
| int rc = pthread_create(&ret, NULL, ThreadMain, (void*)0); | ||
| assert(rc == 0); | ||
| return ret; | ||
| } | ||
|
|
||
| int main() { | ||
| if (!emscripten_has_threading_support()) { | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(0); | ||
| #endif | ||
| printf("Skipped: Threading is not supported.\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| int x = EM_ASM_INT({ | ||
| onerror = function(e) { | ||
| var message = e.toString(); | ||
| var success = message.indexOf("Blocking on the main thread is not allowed by default. See https://emscripten.org/docs/porting/pthreads.html#blocking-on-the-main-browser-thread") >= 0; | ||
| if (success && !Module.reported) { | ||
| Module.reported = true; | ||
| console.log("reporting success"); | ||
| // manually REPORT_RESULT; we shouldn't call back into native code at this point | ||
| var xhr = new XMLHttpRequest(); | ||
| xhr.open("GET", "http://localhost:8888/report_result?0"); | ||
| xhr.send(); | ||
| } | ||
| }; | ||
| return 0; | ||
| }); | ||
|
|
||
| thread = CreateThread(); | ||
| #ifdef TRY_JOIN | ||
| emscripten_set_main_loop(loop, 0, 0); | ||
| #else | ||
| int status; | ||
| // This should fail on the main thread. | ||
| puts("trying to block..."); | ||
| pthread_join(thread, (void**)&status); | ||
| puts("blocked ok."); | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(1); | ||
| #endif | ||
| #endif // TRY_JOIN | ||
| } | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2019 The Emscripten Authors. All rights reserved. | ||
| // Emscripten is available under two separate licenses, the MIT license and the | ||
| // University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <assert.h> | ||
| #include <emscripten.h> | ||
| #include <pthread.h> | ||
| #include <stdio.h> | ||
|
|
||
| int main() { | ||
| if (!emscripten_has_threading_support()) | ||
| { | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(0); | ||
| #endif | ||
| printf("Skipped: Threading is not supported.\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| int x = EM_ASM_INT({ | ||
| onerror = function(e) { | ||
| var message = e.toString(); | ||
| var success = message.indexOf("Blocking on the main thread is not allowed by default. See https://emscripten.org/docs/porting/pthreads.html#blocking-on-the-main-browser-thread") >= 0; | ||
| if (success && !Module.reported) { | ||
| Module.reported = true; | ||
| console.log("reporting success"); | ||
| // manually REPORT_RESULT; we shouldn't call back into native code at this point | ||
| var xhr = new XMLHttpRequest(); | ||
| xhr.open("GET", "http://localhost:8888/report_result?0"); | ||
| xhr.send(); | ||
| } | ||
| }; | ||
| return 0; | ||
| }); | ||
|
|
||
| // This should fail on the main thread. | ||
| puts("trying to block..."); | ||
| pthread_cond_t cv = PTHREAD_COND_INITIALIZER; | ||
| pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; | ||
| pthread_cond_wait(&cv, &lock); | ||
| puts("blocked ok."); | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(1); | ||
| #endif | ||
| } | ||
|
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.