-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Micro-optimize timed wait loops. NFC #26849
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -530,24 +530,23 @@ em_queued_call* emscripten_async_waitable_run_in_main_runtime_thread_( | |
| } | ||
|
|
||
| EMSCRIPTEN_RESULT emscripten_wait_for_call_v(em_queued_call* call, double timeoutMSecs) { | ||
| if (call->operationDone) { | ||
| return EMSCRIPTEN_RESULT_SUCCESS; | ||
| } | ||
|
|
||
| emscripten_set_current_thread_status(EM_THREAD_STATUS_WAITPROXY); | ||
|
|
||
| int r; | ||
| double target = emscripten_get_now() + timeoutMSecs; | ||
| do { | ||
| r = -emscripten_futex_wait(&call->operationDone, 0, timeoutMSecs); | ||
|
|
||
| int done = atomic_load(&call->operationDone); | ||
|
Collaborator
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 don't think we want to remove this line. IIUC that idea with the futex_wait API is that one should only call it once you know you need to block. i.e. its designed as the expensive syscall that you use once you know you need to block, and its common to place it after some amount of spinning (or at least an atomic check like this one).
Collaborator
Author
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. Good point. Added this back as an early return with commit ab6b2bf. |
||
| if (!done) { | ||
| double now = emscripten_get_now(); | ||
| double waitEndTime = now + timeoutMSecs; | ||
| emscripten_set_current_thread_status(EM_THREAD_STATUS_WAITPROXY); | ||
| while (!done && now < waitEndTime) { | ||
| r = emscripten_futex_wait(&call->operationDone, 0, waitEndTime - now); | ||
| done = atomic_load(&call->operationDone); | ||
| now = emscripten_get_now(); | ||
| } | ||
| emscripten_set_current_thread_status(EM_THREAD_STATUS_RUNNING); | ||
| } | ||
| if (done) | ||
| return EMSCRIPTEN_RESULT_SUCCESS; | ||
| else | ||
| return EMSCRIPTEN_RESULT_TIMED_OUT; | ||
| timeoutMSecs = target - emscripten_get_now(); | ||
| } while (r == EINTR && timeoutMSecs > 0); | ||
|
|
||
| emscripten_set_current_thread_status(EM_THREAD_STATUS_RUNNING); | ||
|
|
||
| return r == ETIMEDOUT ? EMSCRIPTEN_RESULT_TIMED_OUT : EMSCRIPTEN_RESULT_SUCCESS; | ||
| } | ||
|
|
||
| EMSCRIPTEN_RESULT emscripten_wait_for_call_i( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.