-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Move emscripten_futex_wake to native code. NFC #15766
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
Merged
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2021 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 <errno.h> | ||
| #include <limits.h> | ||
| #include <atomic.h> | ||
| #include <emscripten/threading.h> | ||
|
|
||
| int _emscripten_thread_supports_atomics_wait(void); | ||
|
|
||
| // Stores the memory address that the main thread is waiting on, if any. If | ||
| // the main thread is waiting, we wake it up before waking up any workers. | ||
| void* _emscripten_main_thread_futex; | ||
|
|
||
| // Returns the number of threads (>= 0) woken up, or the value -EINVAL on error. | ||
| // Pass count == INT_MAX to wake up all threads. | ||
| int emscripten_futex_wake(volatile void *addr, int count) { | ||
| if (!addr || (((intptr_t)addr) & 3) != 0 || count < 0) { | ||
| return -EINVAL; | ||
| } | ||
| if (count == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| // See if main thread is waiting on this address? If so, wake it up by resetting its wake location to zero. | ||
| // Note that this is not a fair procedure, since we always wake main thread first before any workers, so | ||
| // this scheme does not adhere to real queue-based waiting. | ||
| int main_thread_woken = 0; | ||
| if (a_cas_p(&_emscripten_main_thread_futex, (void*)addr, 0) == addr) { | ||
| // We only use __emscripten_main_thread_futex on the main browser thread, | ||
| // where we cannot block while we wait. Therefore we should only see it set | ||
| // from other threads (that should always support waiting), and not on the | ||
| // main thread itself. In other words, the main thread must never try to | ||
| // wake itself up! | ||
| assert(_emscripten_thread_supports_atomics_wait()); | ||
| --count; | ||
| main_thread_woken = 1; | ||
| if (count <= 0) { | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| // Wake any workers waiting on this address. | ||
| int ret = __builtin_wasm_memory_atomic_notify((int*)addr, count); | ||
| assert(ret >= 0); | ||
| return ret + main_thread_woken; | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ G | |
| H | ||
| I | ||
| J | ||
| K | ||
| p | ||
| q | ||
| r | ||
| s | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -13,4 +13,3 @@ a.l | |
| a.m | ||
| a.n | ||
| a.o | ||
| a.p | ||
2 changes: 1 addition & 1 deletion
2
tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.jssize
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 @@ | ||
| 49636 | ||
| 48861 |
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 |
|---|---|---|
|
|
@@ -13,4 +13,3 @@ l | |
| m | ||
| n | ||
| o | ||
| p | ||
2 changes: 1 addition & 1 deletion
2
tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.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 @@ | ||
| 16846 | ||
| 16911 |
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.
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.
At some point we might want to audit our code to try to make it work correctly from audio worklets, which also can't block.
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.
Yeah.. this mechanism assumes only one (busy-looping) main thread I think, so it would need to be revisited.
Uh oh!
There was an error while loading. Please reload this page.
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 guess I came across the audioworklet issue:
It would be absolutely great, if this gets fixed. Because the app I am using with Emscripten seems to rely on that. And I am a bit lost with fixing it by myself.