Move sleep slicing from musl's __wait to the lower level emscripten_futex_wait. NFC#26471
Move sleep slicing from musl's __wait to the lower level emscripten_futex_wait. NFC#26471sbc100 wants to merge 1 commit intoemscripten-core:mainfrom
__wait to the lower level emscripten_futex_wait. NFC#26471Conversation
65227c2 to
dc4a6db
Compare
| double max_ms_slice_to_sleep = emscripten_is_main_runtime_thread() ? 1 : 100; | ||
| // Break up sleeping so that we process proxied work at regular intervals. | ||
| // TODO(sbc): This should be remove and/or moved down into | ||
| // `emscripten_futex_wait`. |
There was a problem hiding this comment.
@tlively do you think we can just remove this loop? i.e. to background threads actually want to be be running proxied work when they call emscripten_thread_sleep?
There was a problem hiding this comment.
I'm not sure. It looks like the ProxyWorker used by the WasmFS OPFS backend does not need this: https://github.com/emscripten-core/emscripten/blob/main/system/lib/wasmfs/thread_utils.h#L67. I don't know about all the users of the system proxying queue, though.
There was a problem hiding this comment.
Oh, what about dlopen? Will that depend on sleeping threads waking up to perform loading work?
There was a problem hiding this comment.
No, the dlopen handling is done down in emscripten_futex_wait at the entry and exit from the wait.
The contract is that you can sleep as long as you want, as long you syncronize your loaded code as soon as you wake up.
There was a problem hiding this comment.
i.e. a thread that is "sleeping" is considered "up-to-date" from the POV of the "sync-the-world" algorithm.
dc4a6db to
c83cec6
Compare
c83cec6 to
56c46cb
Compare
| double max_ms_slice_to_sleep = is_runtime_thread ? 1 : 100; | ||
|
|
||
| while (*addr==val) { | ||
| if (is_runtime_thread || pthread_self()->cancelasync == PTHREAD_CANCEL_ASYNCHRONOUS) { |
There was a problem hiding this comment.
I get the second removal here (cancellation) but what about is_runtime_thread?
There was a problem hiding this comment.
I moved that down into emscripten_futex_wait too.
There was a problem hiding this comment.
See the comment that starts 1. We are the main runtime thread.
d025d13 to
ee3f1b4
Compare
|
I had gemini CLI go over potentially issues with this PR and it found a few subtle issues with the slicing.. all of which are now fixed. |
ee3f1b4 to
dba24ed
Compare
| // 1. We are the main runtime thread. In this case we need to be able to | ||
| // process proxied events from workers. Note that this is not always | ||
| // the same as being the main browser thread. For example, when running | ||
| // under node or when launching and emscripten-built program in a Web |
There was a problem hiding this comment.
| // under node or when launching and emscripten-built program in a Web | |
| // under node or when launching an emscripten-built program in a Web |
| // process proxied events from workers. Note that this is not always | ||
| // the same as being the main browser thread. For example, when running | ||
| // under node or when launching and emscripten-built program in a Web | ||
| // Worker. This this case we limit our wait slices to 1ms intervals. |
There was a problem hiding this comment.
| // Worker. This this case we limit our wait slices to 1ms intervals. | |
| // Worker. In this case we limit our wait slices to 1ms intervals. |
| double max_ms_slice_to_sleep = emscripten_is_main_runtime_thread() ? 1 : 100; | ||
| // Break up sleeping so that we process proxied work at regular intervals. | ||
| // TODO(sbc): This should be remove and/or moved down into | ||
| // `emscripten_futex_wait`. |
There was a problem hiding this comment.
Oh, what about dlopen? Will that depend on sleeping threads waking up to perform loading work?
…n_futex_wait`. NFC This means we only need to do this breaking up on our `wait` operations in a single place. It also means that other users the `emscripten_futex_wait` API don't break pthread proxying or async cancellation. The moved code is only included in pthread-enabled builds so should not effect Wasm Workers builders. This change also paves the way for enabling musl's `__wait` to work with `WASM_WORKERS`.
dba24ed to
a44b167
Compare
This reduces the number of places we need to breaking up our
waitoperation. It also means that other users theemscripten_futex_waitAPI don't break pthread proxying or async cancellation.This change removes one more place where we were erroneously calling
pthread_self()in the Wasm Workers build of libc, so this change also makes the code less broken in the Wasm Worker case.Needed as part of #26487