Skip to content

Commit 3042235

Browse files
authored
Improve tests for pthreads+dylink (#16525)
test_pthread_dlsym.c: Explicit syncing of code is not necessary in this test. The libraries should be loaded in the same order on every thread regardless. test_pthread_dlsym.c: Be a little more hostile by avoiding pthread constructs and just spinning. This mean the explicit code sync is more likely to actaully be needed, even in the face of syncronization happing if futex_wait (which I'm porposing to add as followup).
1 parent 70e39ab commit 3042235

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

tests/core/pthread/test_pthread_dlopen.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ static sidey_func_type p_side_func_address;
1515
static int* expected_data_addr;
1616
static func_t expected_func_addr;
1717

18-
static pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
19-
static pthread_mutex_t ready_mutex = PTHREAD_MUTEX_INITIALIZER;
18+
static atomic_bool started = false;
2019
static atomic_bool ready = false;
2120

2221
static void* thread_main() {
23-
while (!ready) {
24-
pthread_mutex_lock(&ready_mutex);
25-
pthread_cond_wait(&ready_cond, &ready_mutex);
26-
pthread_mutex_unlock(&ready_mutex);
27-
}
28-
2922
printf("in thread_main\n");
23+
started = true;
24+
// Spin until the main thread has loaded the side module
25+
while (!ready) {}
26+
27+
// Without this explict sync we get "invalid index into function table" below
3028
_emscripten_thread_sync_code();
3129

3230
printf("calling p_side_data_address=%p\n", p_side_data_address);
@@ -50,8 +48,10 @@ int main() {
5048
int rc = pthread_create(&t, NULL, thread_main, NULL);
5149
assert(rc == 0);
5250

53-
printf("loading dylib\n");
51+
// Spin until the thread has started
52+
while (!started) {}
5453

54+
printf("loading dylib\n");
5555
void* handle = dlopen("liblib.so", RTLD_NOW|RTLD_GLOBAL);
5656
if (!handle) {
5757
printf("dlerror: %s\n", dlerror());
@@ -71,10 +71,7 @@ int main() {
7171
printf("p_side_func_address -> %p\n", expected_func_addr);
7272
assert(expected_func_addr() == 43);
7373

74-
pthread_mutex_lock(&ready_mutex);
7574
ready = true;
76-
pthread_cond_signal(&ready_cond);
77-
pthread_mutex_unlock(&ready_mutex);
7875

7976
printf("joining\n");
8077
rc = pthread_join(t, NULL);

tests/core/pthread/test_pthread_dlsym.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <dlfcn.h>
33
#include <pthread.h>
44
#include <stdio.h>
5-
#include <emscripten/threading.h>
65

76
typedef int (*func_t)();
87

@@ -53,7 +52,6 @@ static void test_order2() {
5352

5453
static void* thread_main() {
5554
printf("in thread_main\n");
56-
_emscripten_thread_sync_code();
5755
test_order2();
5856
printf("thread_main done\n");
5957
return 0;

tests/test_core.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8860,7 +8860,6 @@ def test_pthread_dlopen(self):
88608860

88618861
self.prep_dlfcn_main()
88628862
self.set_setting('EXIT_RUNTIME')
8863-
self.set_setting('PTHREAD_POOL_SIZE', 2)
88648863
self.set_setting('PROXY_TO_PTHREAD')
88658864
self.do_runf(test_file('core/pthread/test_pthread_dlopen.c'))
88668865

@@ -8873,7 +8872,6 @@ def test_pthread_dlsym(self):
88738872

88748873
self.prep_dlfcn_main()
88758874
self.set_setting('EXIT_RUNTIME')
8876-
self.set_setting('PTHREAD_POOL_SIZE', 2)
88778875
self.set_setting('PROXY_TO_PTHREAD')
88788876
self.do_runf(test_file('core/pthread/test_pthread_dlsym.c'))
88798877

0 commit comments

Comments
 (0)