Skip to content

Commit 31e9690

Browse files
committed
Return ENOTSUP from pthread_create when threads are not available.
See WebAssembly/wasi-libc#716
1 parent 80d31d0 commit 31e9690

File tree

7 files changed

+25
-18
lines changed

7 files changed

+25
-18
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ See docs/process.md for more on how version tagging works.
2020

2121
4.0.24 (in development)
2222
-----------------------
23+
- Calling pthread_create in a single-threaded build will now return ENOTSUP
24+
rather then EAGAIN. (#26105)
2325
- compiler-rt and libunwind were updated to LLVM 21.1.8. (#26036 and #26045)
2426
- A new `-sEXECUTABLE` setting was added which adds a #! line to the resulting
2527
JavaScript and makes it executable. This setting defaults to true when the

system/lib/pthread/library_pthread_stub.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ int pthread_barrier_destroy(pthread_barrier_t* mutex) { return 0; }
9292
int pthread_barrier_wait(pthread_barrier_t* mutex) { return 0; }
9393

9494
int __pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) {
95-
return EAGAIN;
95+
// ENOTSUP, while not mentioned in the pthread_create docs, does better
96+
// describe the situation.
97+
// See https://github.com/WebAssembly/wasi-libc/pull/716 for discussion
98+
// on this error code vs, for example, EAGAIN.
99+
return ENOTSUP;
96100
}
97101

98102
weak_alias(__pthread_create, emscripten_builtin_pthread_create);

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"a.out.js": 244848,
3-
"a.out.nodebug.wasm": 577875,
4-
"total": 822723,
3+
"a.out.nodebug.wasm": 577876,
4+
"total": 822724,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/other/test_pthread_stub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ void* start_pthread(void* arg) {
2222
}
2323

2424
#define CHECK(X, EXPECTED) rtn = X; if (rtn != EXPECTED) printf(#X " returned %s\n", strerror(rtn)); assert(rtn == EXPECTED)
25-
#define CHECK_FAIL(X) CHECK(X, EAGAIN)
25+
#define CHECK_FAIL(X) CHECK(X, ENOTSUP)
2626
#define CHECK_SUCCESS(X) CHECK(X, 0)
2727

2828
#define CHECK_C11(X, expected) rtn = X; if (rtn != expected) printf(#X " returned %d\n", rtn); assert(rtn == expected)
2929
#define CHECK_C11_SUCCESS(X) CHECK_C11(X, thrd_success)
30-
#define CHECK_C11_FAIL(X) CHECK_C11(X, thrd_nomem)
30+
#define CHECK_C11_FAIL(X) CHECK_C11(X, thrd_error)
3131

3232
void test_c11_threads() {
3333
printf("test_c11_threads\n");
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
#include <assert.h>
1212
#include <errno.h>
1313

14-
void *ThreadMain(void *arg)
15-
{
16-
pthread_exit(NULL);
14+
void *ThreadMain(void *arg) {
15+
pthread_exit(NULL);
1716
}
1817

19-
int main()
20-
{
21-
pthread_t thread;
22-
int rc = pthread_create(&thread, NULL, ThreadMain, NULL);
23-
pthread_join(thread, NULL);
18+
int main() {
19+
pthread_t thread;
20+
int rc = pthread_create(&thread, NULL, ThreadMain, NULL);
21+
pthread_join(thread, NULL);
2422

25-
if (emscripten_has_threading_support()) assert(rc == 0);
26-
else assert(rc == EAGAIN);
23+
if (emscripten_has_threading_support()) {
24+
assert(rc == 0);
25+
} else {
26+
assert(rc == ENOTSUP);
27+
}
2728

28-
return 0;
29+
return 0;
2930
}

test/test_browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3911,7 +3911,7 @@ def test_pthread_file_io(self):
39113911
'mt': (['-pthread', '-sPTHREAD_POOL_SIZE=8'],),
39123912
})
39133913
def test_pthread_supported(self, args):
3914-
self.btest_exit('pthread/test_pthread_supported.cpp', cflags=['-O3'] + args)
3914+
self.btest_exit('pthread/test_pthread_supported.c', cflags=['-O3'] + args)
39153915

39163916
def test_pthread_dispatch_after_exit(self):
39173917
self.btest_exit('pthread/test_pthread_dispatch_after_exit.c', cflags=['-pthread'])

test/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15095,7 +15095,7 @@ def test_libcxx_errors(self):
1509515095

1509615096
# Since we are building without -pthread the thread constructor will fail,
1509715097
# and in debug mode at least we expect to see the error message from libc++
15098-
expected = 'system_error was thrown in -fno-exceptions mode with error 6 and message "thread constructor failed"'
15098+
expected = 'system_error was thrown in -fno-exceptions mode with error 138 and message "thread constructor failed"'
1509915099
self.do_runf('main.cpp', expected, assert_returncode=NON_ZERO)
1510015100

1510115101
def test_parsetools_make_removed_fs_assert(self):

0 commit comments

Comments
 (0)