-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add emscripten_queue_microtask() API #25741
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
base: main
Are you sure you want to change the base?
Changes from 2 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 |
|---|---|---|
|
|
@@ -466,6 +466,8 @@ int emscripten_request_animation_frame(bool (*cb)(double time, void *userData), | |
| void emscripten_cancel_animation_frame(int requestAnimationFrameId); | ||
| void emscripten_request_animation_frame_loop(bool (*cb)(double time, void *userData), void *userData); | ||
|
|
||
| void emscripten_queue_microtask(void (*cb)(void *userData), void *userData); | ||
|
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. This should probably go in |
||
|
|
||
| double emscripten_date_now(void); | ||
| double emscripten_performance_now(void); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| // Test emscripten_queue_microtask() behavior | ||
|
|
||
| #include <stdio.h> | ||
| #include <assert.h> | ||
| #include <emscripten.h> | ||
| #include <emscripten/html5.h> | ||
|
|
||
| void cb(void *userData) { | ||
| printf("cb\n"); | ||
| assert(userData == (void*)42); | ||
| emscripten_force_exit(0); | ||
| } | ||
|
|
||
| int main() { | ||
| emscripten_queue_microtask(cb, (void*)42); | ||
| emscripten_exit_with_live_runtime(); | ||
| return 99; // We won't reach here, but return non-zero value to guard against refactors that might exit() with this value. | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5016,6 +5016,13 @@ def test_emscripten_request_animation_frame_loop(self): | |
| def test_request_animation_frame(self): | ||
| self.btest_exit('test_request_animation_frame.c') | ||
|
|
||
| def test_emscripten_queue_microtask(self): | ||
| self.btest_exit('emscripten_queue_microtask.c') | ||
|
|
||
| @requires_shared_array_buffer | ||
| def test_emscripten_queue_microtask_pthread(self): | ||
|
||
| self.btest_exit('emscripten_queue_microtask.c', cflags=['-pthread', '-sPROXY_TO_PTHREAD']) | ||
|
|
||
| @requires_shared_array_buffer | ||
| def test_emscripten_set_timeout(self): | ||
| self.btest_exit('emscripten_set_timeout.c', cflags=['-pthread', '-sPROXY_TO_PTHREAD']) | ||
|
|
||
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.
This probably needs to be wrapped in
callUserCallback.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.. Both the push and pop callback and callUserCallback are nice for some uses, but result in bloat :/
Added these, maybe callUserCallback() can be made zero-cost some day. (Or maybe it is so at least with Closure, not sure)
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.
We do need
callUserCallbackwhenever we enter user code from the event loop otherwise lots of things can break. A classic one is callingexit()from within the callback.IIRC the overhead of
callUserCallbackin MINIMAL_RUNTIME, where we don't have the same requirements is very close to zero. If you want to make reduce it to absolutely zero that seems like a good thing, but unrelated to this specific change.