Skip to content

Commit c786023

Browse files
committed
[wasm64] Fix _emscripten_run_callback_on_thread under wasm64
The only test we have for this was in `test_interactive.py`. I ran it locally to ensure it now works. Fixes: #21851
1 parent b941b7b commit c786023

File tree

7 files changed

+34
-27
lines changed

7 files changed

+34
-27
lines changed

system/include/emscripten/threading_legacy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ typedef struct em_queued_call em_queued_call;
114114
#define EM_FUNC_SIG_II (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(1) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I))
115115
#define EM_FUNC_SIG_III (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(2) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_I))
116116
#define EM_FUNC_SIG_IIII (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(3) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(2, EM_FUNC_SIG_PARAM_I))
117+
#define EM_FUNC_SIG_IIPP (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(3) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_P) | EM_FUNC_SIG_SET_PARAM(2, EM_FUNC_SIG_PARAM_P))
117118
#define EM_FUNC_SIG_IIIII (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(4) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(2, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(3, EM_FUNC_SIG_PARAM_I))
118119
#define EM_FUNC_SIG_IIIIII (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(5) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(2, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(3, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(4, EM_FUNC_SIG_PARAM_I))
119120
#define EM_FUNC_SIG_IIIIIII (EM_FUNC_SIG_RETURN_VALUE_I | EM_FUNC_SIG_WITH_N_PARAMETERS(6) | EM_FUNC_SIG_SET_PARAM(0, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(1, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(2, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(3, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(4, EM_FUNC_SIG_PARAM_I) | EM_FUNC_SIG_SET_PARAM(5, EM_FUNC_SIG_PARAM_I))

system/lib/pthread/proxying_legacy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ typedef int (*em_func_ij)(uint64_t);
8989
typedef uint64_t (*em_func_ji)(int);
9090
typedef int (*em_func_ijj)(uint64_t, uint64_t);
9191
typedef int (*em_func_iij)(int, uint64_t);
92+
typedef int (*em_func_iijj)(int, uint64_t, uint64_t);
9293
typedef uint64_t (*em_func_jjj)(uint64_t, uint64_t);
9394
typedef void (*em_func_vij)(int, uint64_t);
9495
typedef void (*em_func_viij)(int, int, uint64_t);
@@ -249,6 +250,9 @@ static void _do_call(void* arg) {
249250
case EM_FUNC_SIG_IP:
250251
q->returnValue.i = ((em_func_ij)q->functionPtr)(q->args[0].j);
251252
break;
253+
case EM_FUNC_SIG_IIPP:
254+
q->returnValue.i = ((em_func_iijj)q->functionPtr)(q->args[0].i, q->args[1].j, q->args[2].j);
255+
break;
252256
case EM_FUNC_SIG_PI:
253257
q->returnValue.j = ((em_func_ji)q->functionPtr)(q->args[0].i);
254258
break;

test/emscripten_hide_mouse.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
EM_BOOL mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData) {
77
printf("Mouse click on canvas.\n");
8-
#ifdef REPORT_RESULT
9-
REPORT_RESULT(0);
10-
#endif
8+
emscripten_force_exit(0);
119
return 0;
1210
}
1311

test/html5_event_callback_in_two_threads.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33
#include <emscripten/threading.h>
44
#include <emscripten/key_codes.h>
55
#include <stdio.h>
6+
#include <stdbool.h>
67
#include <stdlib.h>
78
#include <memory.h>
89
#include <assert.h>
910

1011
pthread_t application_thread_id = 0;
1112
pthread_t main_runtime_thread_id = 0;
1213

13-
volatile int saw_keydown_event_on_enter_key_on_application_thread = 0;
14-
volatile int saw_keydown_event_on_enter_key_on_main_runtime_thread = 0;
15-
volatile int saw_keypress_event_on_enter_key = 0;
16-
17-
void ReportResult(int code) {
18-
printf("Test finished with code: %d\n", code);
19-
#ifdef REPORT_RESULT
20-
REPORT_RESULT(code);
21-
#endif
22-
exit(code);
23-
}
14+
_Atomic int saw_keydown_event_on_enter_key_on_application_thread = 0;
15+
_Atomic int saw_keydown_event_on_enter_key_on_main_runtime_thread = 0;
16+
_Atomic int saw_keypress_event_on_enter_key = 0;
2417

2518
EM_BOOL keydown_callback_on_application_thread(int eventType, const EmscriptenKeyboardEvent *e, void *userData) {
2619
int dom_pk_code = emscripten_compute_dom_pk_code(e->code);
@@ -41,7 +34,7 @@ EM_BOOL keydown_callback_on_main_runtime_thread(int eventType, const EmscriptenK
4134
#if __EMSCRIPTEN_PTHREADS__
4235
EmscriptenKeyboardEvent *duplicatedEventStruct = malloc(sizeof(*e));
4336
memcpy(duplicatedEventStruct, e, sizeof(*e));
44-
emscripten_dispatch_to_thread(application_thread_id, EM_FUNC_SIG_IIII, keydown_callback_on_application_thread, duplicatedEventStruct, eventType, duplicatedEventStruct, userData);
37+
emscripten_dispatch_to_thread(application_thread_id, EM_FUNC_SIG_IIPP, keydown_callback_on_application_thread, duplicatedEventStruct, eventType, duplicatedEventStruct, userData);
4538
#else
4639
keydown_callback_on_application_thread(eventType, e, userData);
4740
#endif
@@ -63,7 +56,7 @@ EM_BOOL keypress_callback_on_application_thread(int eventType, const EmscriptenK
6356
if (dom_pk_code == DOM_PK_ENTER) {
6457
saw_keypress_event_on_enter_key = 1;
6558
printf("Test failed! KeyPress event came through even though it was suppressed in KeyDown handler!\n");
66-
ReportResult(12345); // FAIL
59+
assert(false);
6760
}
6861
return 0;
6962
}
@@ -76,18 +69,18 @@ EM_BOOL keyup_callback_on_application_thread(int eventType, const EmscriptenKeyb
7669
if (dom_pk_code == DOM_PK_ENTER) {
7770
if (!saw_keydown_event_on_enter_key_on_application_thread) {
7871
printf("Test failed! KeyUp event came through, but a KeyDown event should have first been processed on the application thread!\n");
79-
ReportResult(12346); // FAIL
72+
assert(false);
8073
}
8174
if (!saw_keydown_event_on_enter_key_on_main_runtime_thread) {
8275
printf("Test failed! KeyUp event came through, but a KeyDown event should have first been processed on the main runtime thread!\n");
83-
ReportResult(12347); // FAIL
76+
assert(false);
8477
}
8578
if (saw_keypress_event_on_enter_key) {
8679
printf("Test failed! KeyUp event came through, but a KeyPress event was first seen, suppressing it did not work!\n");
87-
ReportResult(12348); // FAIL
80+
assert(false);
8881
}
8982
printf("Test passed!\n");
90-
ReportResult(1); // PASS
83+
exit(0);
9184
}
9285
return 0;
9386
}

test/test_browser.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,8 +2571,6 @@ def test_html5_core(self, opts):
25712571
if self.is_wasm64():
25722572
if '-sMIN_CHROME_VERSION=0' in opts:
25732573
self.skipTest('wasm64 does not support older browsers')
2574-
if '-sPROXY_TO_PTHREAD' in opts:
2575-
self.skipTest('_emscripten_set_keypress_callback_on_thread broken under wasm64')
25762574
if '-sHTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS=0' in opts:
25772575
# In this mode an exception can be thrown by the browser, and we don't
25782576
# want the test to fail in that case so we override the error handling.

test/test_interactive.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,16 @@ def test_html5_callbacks_on_calling_thread(self):
247247
})
248248
def test_html5_event_callback_in_two_threads(self, args):
249249
# TODO: Make this automatic by injecting enter key press in e.g. shell html file.
250-
self.btest('html5_event_callback_in_two_threads.c', expected='1', args=args)
250+
self.btest_exit('html5_event_callback_in_two_threads.c', args=args)
251251

252-
# Test that emscripten_hide_mouse() is callable from pthreads (and proxies to main thread to obtain the proper window.devicePixelRatio value).
253-
def test_emscripten_hide_mouse(self):
254-
for args in [[], ['-pthread']]:
255-
self.btest('emscripten_hide_mouse.c', expected='0', args=args)
252+
# Test that emscripten_hide_mouse() is callable from pthreads (and proxies to main
253+
# thread to obtain the proper window.devicePixelRatio value).
254+
@parameterized({
255+
'': ([],),
256+
'threads': (['-pthread'],),
257+
})
258+
def test_emscripten_hide_mouse(self, args):
259+
self.btest('emscripten_hide_mouse.c', expected='0', args=args)
256260

257261
# Tests that WebGL can be run on another thread after first having run it on one thread (and that thread has exited). The intent of this is to stress graceful deinit semantics, so that it is not possible to "taint" a Canvas
258262
# to a bad state after a rendering thread in a program quits and restarts. (perhaps e.g. between level loads, or subsystem loads/restarts or something like that)
@@ -283,3 +287,11 @@ def test_audio_worklet_tone_generator(self):
283287
# Tests that AUDIO_WORKLET+MINIMAL_RUNTIME+MODULARIZE combination works together.
284288
def test_audio_worklet_modularize(self):
285289
self.btest('webaudio/audioworklet.c', expected='0', args=['-sAUDIO_WORKLET', '-sWASM_WORKERS', '-sMINIMAL_RUNTIME', '-sMODULARIZE'])
290+
291+
292+
class interactive64(interactive):
293+
def setUp(self):
294+
super().setUp()
295+
self.set_setting('MEMORY64')
296+
self.emcc_args.append('-Wno-experimental')
297+
self.require_wasm64()

tools/emscripten.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,7 @@ def create_pointer_conversion_wrappers(metadata):
10761076
'emscripten_main_runtime_thread_id': 'p',
10771077
'_emscripten_set_offscreencanvas_size_on_thread': '_pp__',
10781078
'fileno': '_p',
1079+
'_emscripten_run_callback_on_thread': '_pp_pp',
10791080
}
10801081

10811082
for function in settings.SIGNATURE_CONVERSIONS:

0 commit comments

Comments
 (0)