-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and rename confusing pthreads async functions (#10772)
As noted in #9086, emscripten_async_queue_on_thread is a misleading name since it is only async if calling another thread - if we are already on the same thread, we just do the call immediately. Fixing that by making it actually async isn't an option, since if the current thread never returns to the main event loop, an async event would never happen. We have to fire it synchronously in that case, but we don't know if that is the case or not (it depends on user code). And it would be less efficient for the common case. As a solution emscripten_async_queue_on_thread has been renamed to emscripten_dispatch_to_thread which no longer implies that it is async - the operation is in fact only async if it is sent to another thread, while it is sync if on the same one. A new emscripten_dispatch_to_thread_async function is added which is always async. That is a breaking change for people using that API. I would guess not many have since while it is in threading.h it isn't documented elsewhere. And this PR should help some of those users (including the one that suggested this PR), so overall it seems worth a breaking change. Fixes #9086
- Loading branch information
Showing
9 changed files
with
108 additions
and
32 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2020 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 <stdio.h> | ||
#include <assert.h> | ||
#include <emscripten/threading.h> | ||
|
||
int state = 0; | ||
|
||
void increment() { | ||
state++; | ||
} | ||
|
||
void finish() { | ||
#ifdef REPORT_RESULT | ||
REPORT_RESULT(1); | ||
#endif | ||
} | ||
|
||
int main() { | ||
assert(state == 0); | ||
// This dispatch_to_thread call will be synchronous since we are on the right | ||
// thread already. | ||
int called_now = emscripten_dispatch_to_thread(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &increment, 0); | ||
assert(called_now); | ||
assert(state == 1); | ||
// This async call will actually be async. | ||
emscripten_dispatch_to_thread_async(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &increment, 0); | ||
assert(state == 1); | ||
emscripten_dispatch_to_thread_async(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &finish, 0); | ||
} |
Oops, something went wrong.