Fix emscripten_set_immediate when called from a pthread#27008
Open
sbc100 wants to merge 1 commit into
Open
Conversation
cf27cfa to
a602c5e
Compare
We have special handling for the `setimmediate` message in the pthreads code to forward it back to the worker. Without this change, `emscripten_set_immediate` (which uses `emSetImmediate` under the hood) was using `_si` as its message ID and was not getting forwarded, causing immediate callbacks to never run in workers. To fix this while avoiding event listener interference when both `emscripten_set_immediate` and the `EM_TIMING_SETIMMEDIATE` main loop are active in the same thread, we use unique message IDs for each polyfill (`em-setimmediate` and `ml-setimmediate`) and update the pthreads forwarding logic to support both. Also update `emSetImmediate` to check if it is running in a worker and omit the target origin argument when calling `postMessage`, as the worker version of `postMessage` does not support it. This matches the logic already present in the `EM_TIMING_SETIMMEDIATE` main loop implementation. Add a pthread variant to `test_emscripten_set_immediate` to prevent regressions.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
We have special handling for the
setimmediatemessage in the pthreads code. Without this change,emscripten_set_immediate(which usesemSetImmediateunder the hood) was using_sias its message ID. This meant that when called from a worker, the message sent to the main thread was not recognized and therefore not forwarded back to the worker, causing the immediate callback to never run.By unifying both to use
setimmediate, we can reuse the existing pthreads forwarding logic.Also update
emSetImmediateto check if it is running in a worker and omit the target origin argument when callingpostMessage, as the worker version ofpostMessagedoes not support it. This matches the logic already present in theEM_TIMING_SETIMMEDIATEmain loop implementation.Add a pthread variant to
test_emscripten_set_immediateto prevent regressions.