Skip to content

Commit fcd5d2a

Browse files
committed
Minor cleanups
1 parent d50e5a6 commit fcd5d2a

File tree

13 files changed

+58
-62
lines changed

13 files changed

+58
-62
lines changed

src/library_pthread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ var LibraryPThread = {
269269
worker.on('error', function(e) {
270270
worker.onerror(e);
271271
});
272-
worker.on('detachedExit', function() {
272+
worker.on('exit', function() {
273273
// TODO: update the worker queue?
274274
// See: https://github.com/emscripten-core/emscripten/issues/9763
275275
});

src/struct_info_internal.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"_a_transferredcanvases"
2525
],
2626
"thread_profiler_block": [
27-
"threadStatus",
28-
"currentStatusStartTime",
29-
"timeSpentInStatus",
30-
"name"
27+
"threadStatus",
28+
"currentStatusStartTime",
29+
"timeSpentInStatus",
30+
"name"
3131
]
3232
},
3333
"defines": [

system/lib/libc/musl/src/internal/pthread_impl.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
#include "syscall.h"
1111
#include "atomic.h"
1212
#ifdef __EMSCRIPTEN__
13+
#include <math.h>
1314
#include <emscripten/threading.h>
1415
#include "threading_internal.h"
15-
#endif
16+
#else
1617
#include "futex.h"
18+
#endif
1719

1820
#include "pthread_arch.h"
1921

@@ -186,11 +188,12 @@ hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec
186188
hidden void __wait(volatile int *, volatile int *, int, int);
187189
static inline void __wake(volatile void *addr, int cnt, int priv)
188190
{
189-
if (priv) priv = FUTEX_PRIVATE;
190-
if (cnt<0) cnt = INT_MAX;
191191
#ifdef __EMSCRIPTEN__
192-
emscripten_futex_wake(addr, cnt);
192+
(void)priv;
193+
emscripten_futex_wake(addr, cnt < 0 ? INT_MAX : cnt);
193194
#else
195+
if (priv) priv = FUTEX_PRIVATE;
196+
if (cnt<0) cnt = INT_MAX;
194197
__syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS ||
195198
__syscall(SYS_futex, addr, FUTEX_WAKE, cnt);
196199
#endif

system/lib/libc/musl/src/thread/__timedwait.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
#include <time.h>
33
#include <errno.h>
44
#ifdef __EMSCRIPTEN__
5-
#include <math.h>
65
#include <emscripten/emscripten.h> // for emscripten_get_now()
76
#else
87
#include "futex.h"
9-
#endif
108
#include "syscall.h"
9+
#endif
1110
#include "pthread_impl.h"
1211

1312
#ifndef __EMSCRIPTEN__
@@ -42,7 +41,9 @@ int __timedwait_cp(volatile int *addr, int val,
4241
int r;
4342
struct timespec to, *top=0;
4443

44+
#ifndef __EMSCRIPTEN__
4545
if (priv) priv = FUTEX_PRIVATE;
46+
#endif
4647

4748
if (at) {
4849
if (at->tv_nsec >= 1000000000UL) return EINVAL;

system/lib/libc/musl/src/thread/__wait.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
#ifdef __EMSCRIPTEN__
2-
#include <math.h>
3-
#include <emscripten/threading.h>
4-
#endif
5-
61
#include "pthread_impl.h"
72

83
void __wait(volatile int *addr, volatile int *waiters, int val, int priv)
94
{
105
int spins=100;
6+
#ifndef __EMSCRIPTEN__
117
if (priv) priv = FUTEX_PRIVATE;
8+
#endif
129
while (spins-- && (!waiters || !*waiters)) {
1310
if (*addr==val) a_spin();
1411
else return;

system/lib/libc/musl/src/thread/pthread_barrier_wait.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#ifdef __EMSCRIPTEN__
2-
#include <math.h>
3-
#endif
4-
51
#include "pthread_impl.h"
62

73
static int pshared_barrier_wait(pthread_barrier_t *b)

system/lib/libc/musl/src/thread/pthread_detach.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
static int __pthread_detach(pthread_t t)
55
{
6+
#ifdef __EMSCRIPTEN__
67
// XXX EMSCRIPTEN: Add check for invalid (already joined) thread. Again
78
// for the benefit of the conformance tests.
8-
if (t->self != t)
9-
return ESRCH;
9+
if (t->self != t) return ESRCH;
10+
#endif
1011
/* If the cas fails, detach state is either already-detached
1112
* or exiting/exited, and pthread_join will trap or cleanup. */
1213
if (a_cas(&t->detach_state, DT_JOINABLE, DT_DETACHED) != DT_JOINABLE)
@@ -16,5 +17,6 @@ static int __pthread_detach(pthread_t t)
1617

1718
weak_alias(__pthread_detach, pthread_detach);
1819
weak_alias(__pthread_detach, thrd_detach);
19-
// XXX EMSCRIPTEN: add extra alias for asan.
20+
#ifdef __EMSCRIPTEN__ // XXX Emscripten add an extra alias for ASan/LSan.
2021
weak_alias(__pthread_detach, emscripten_builtin_pthread_detach);
22+
#endif

system/lib/libc/musl/src/thread/pthread_key_create.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include "pthread_impl.h"
22

3-
#ifdef __EMSCRIPTEN__
4-
#include <emscripten.h>
5-
#endif
6-
73
volatile size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
84
void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
95

system/lib/libc/musl/src/thread/thrd_create.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// if we call the internal __pthread_create function here to don't the wrapping
77
// See pthread_create wrapper in compiler-rt/lib/lsan/lsan_interceptors.cpp.
88
#define __pthread_create pthread_create
9-
#else
10-
int __pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict);
119
#endif
1210

1311
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)

system/lib/pthread/library_pthread.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,16 @@
55
* found in the LICENSE file.
66
*/
77

8-
#define _GNU_SOURCE
9-
#include "../internal/libc.h"
10-
#include "../internal/pthread_impl.h"
8+
#include "libc.h"
9+
#include "pthread_impl.h"
1110
#include <assert.h>
12-
#include <dirent.h>
13-
#include <errno.h>
14-
#include <fcntl.h>
1511
#include <math.h>
16-
#include <poll.h>
1712
#include <pthread.h>
1813
#include <stdatomic.h>
1914
#include <stdarg.h>
2015
#include <stdbool.h>
21-
#include <stdlib.h>
22-
#include <sys/ioctl.h>
23-
#include <sys/mman.h>
24-
#include <sys/socket.h>
25-
#include <sys/stat.h>
26-
#include <sys/statvfs.h>
27-
#include <sys/time.h>
28-
#include <termios.h>
2916
#include <threads.h>
3017
#include <unistd.h>
31-
#include <utime.h>
3218

3319
#include <emscripten.h>
3420
#include <emscripten/threading.h>
@@ -834,13 +820,16 @@ void __emscripten_init_main_thread(void) {
834820
__main_pthread.stack = (void*)emscripten_stack_get_base();
835821
__main_pthread.stack_size = emscripten_stack_get_base() - emscripten_stack_get_end();
836822
__main_pthread.detach_state = DT_JOINABLE;
837-
// pthread struct robust_list head should point to itself.
838-
__main_pthread.robust_list.head = &__main_pthread.robust_list.head;
839823
// Main thread ID is always 1. It can't be 0 because musl assumes
840824
// tid is always non-zero.
841825
__main_pthread.tid = getpid();
842826
__main_pthread.locale = &libc.global_locale;
827+
// Initialize thread-specific data area.
828+
__main_pthread.tsd = (void **)__pthread_tsd_main;
829+
// pthread struct robust_list head should point to itself.
830+
__main_pthread.robust_list.head = &__main_pthread.robust_list.head;
831+
// pthread struct prev and next should initially point to itself (see __init_tp),
832+
// this is used by pthread_key_delete for deleting thread-specific data.
843833
// TODO(sbc): Implement circular list of threads
844834
//__main_pthread.next = __main_pthread.prev = &__main_pthread;
845-
__main_pthread.tsd = (void **)__pthread_tsd_main;
846835
}

system/lib/pthread/pthread_create.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* found in the LICENSE file.
66
*/
77

8-
#define _GNU_SOURCE
98
#include "pthread_impl.h"
109
#include "stdio_impl.h"
1110
#include "assert.h"
@@ -22,7 +21,7 @@
2221

2322
// See musl's pthread_create.c
2423

25-
extern int __pthread_create_js(struct pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
24+
extern int __pthread_create_js(pthread_t thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
2625
extern void __set_thread_state(pthread_t ptr, int is_main, int is_runtime, int can_block);
2726
extern int _emscripten_default_pthread_stack_size();
2827
extern void __emscripten_thread_cleanup(pthread_t thread);
@@ -76,7 +75,7 @@ int __pthread_create(pthread_t* restrict res,
7675
void* (*entry)(void*),
7776
void* restrict arg) {
7877
// Note on LSAN: lsan intercepts/wraps calls to pthread_create so any
79-
// allocation we we do here should be considered leaks.
78+
// allocation we do here should be considered as leak.
8079
// See: lsan_interceptors.cpp.
8180
if (!res) {
8281
return EINVAL;
@@ -143,8 +142,6 @@ int __pthread_create(pthread_t* restrict res,
143142
_emscripten_thread_profiler_init(new);
144143
#endif
145144

146-
//printf("start __pthread_create: %p\n", self);
147-
148145
// Set libc.need_locks before calling __pthread_create_js since
149146
// by the time it returns the thread could be running and we
150147
// want libc.need_locks to be set from the moment it starts.
@@ -169,7 +166,6 @@ int __pthread_create(pthread_t* restrict res,
169166
*/
170167

171168
*res = new;
172-
//printf("done __pthread_create self=%p next=%p prev=%p new=%p\n", self, self->next, self->prev, new);
173169
return 0;
174170
}
175171

@@ -202,11 +198,11 @@ static void free_tls_data() {
202198
}
203199

204200
void _emscripten_thread_exit(void* result) {
205-
struct pthread *self = __pthread_self();
201+
pthread_t self = __pthread_self();
206202
assert(self);
207203

208-
self->canceldisable = PTHREAD_CANCEL_DISABLE;
209-
self->cancelasync = PTHREAD_CANCEL_DEFERRED;
204+
self->canceldisable = 1;
205+
self->cancelasync = 0;
210206
self->result = result;
211207

212208
// Run any handlers registered with pthread_cleanup_push
@@ -235,16 +231,16 @@ void _emscripten_thread_exit(void* result) {
235231
return;
236232
}
237233

238-
// We have the call the buildin free here since lsan handling for this thread
234+
// We have the call the builtin free here since lsan handling for this thread
239235
// gets shut down during __pthread_tsd_run_dtors.
240236
emscripten_builtin_free(self->tsd);
241237
self->tsd = NULL;
242238

243239
// Not hosting a pthread anymore in this worker set __pthread_self to NULL
244240
__set_thread_state(NULL, 0, 0, 1);
245241

246-
/* This atomic potentially competes with a concurrent pthread_detach
247-
* call; the loser is responsible for freeing thread resources. */
242+
// This atomic potentially competes with a concurrent pthread_detach
243+
// call; the loser is responsible for freeing thread resources.
248244
int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
249245

250246
if (state == DT_DETACHED) {
@@ -258,7 +254,7 @@ void _emscripten_thread_exit(void* result) {
258254
}
259255
}
260256

261-
// Mark as `no_sanitize("address"` since emscripten_pthread_exit destroys
257+
// Mark as `no_sanitize("address")` since emscripten_pthread_exit destroys
262258
// the current thread and runs its exit handlers. Without this asan injects
263259
// a call to __asan_handle_no_return before emscripten_unwind_to_js_event_loop
264260
// which seem to cause a crash later down the line.

tests/pthread/main_thread_join.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void *ThreadMain(void *arg) {
3333
// succeeding.
3434
while (tries.load() < EXPECTED_TRIES) {}
3535
#endif
36-
pthread_exit((void*)0);
36+
pthread_exit((void*)0);
3737
}
3838

3939
pthread_t CreateThread() {

tests/test_posixtest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
./tests/third_party/posixtestsuite
88
See
99
https://github.com/emscripten-core/posixtestsuite
10+
11+
Verify that it runs properly in musl:
12+
cd tests/third_party/posixtestsuite
13+
docker run -v $(pwd):/app -w /app -it alpine:latest sh -c "\
14+
apk add build-base && \
15+
POSIX_TARGET=conformance/interfaces/[INTERFACE_DIR] make LDFLAGS='-pthread' CFLAGS='-D__EMSCRIPTEN__'"
16+
cat logfile
17+
18+
Where [INTERFACE_DIR] is for e.g.: pthread_detach
1019
"""
1120

1221
import glob
@@ -153,6 +162,15 @@ def f(self):
153162
if name in disabled:
154163
self.skipTest(disabled[name])
155164
args = ['-I' + os.path.join(testsuite_root, 'include'),
165+
# TODO(kleisauke): Run with ASan. Note that this requires matching signatures, i.e:
166+
# void *thread_func() -> void *thread_func(void *unused)
167+
# void *a_thread_func() -> void *a_thread_func(void *unused)
168+
# void *a_thread_function() -> void *a_thread_function(void *unused)
169+
# void a_cleanup_func() -> void a_cleanup_func(void *unused)
170+
# etc, etc.
171+
# '-O0',
172+
# '-g3',
173+
# '-fsanitize=address',
156174
'-Werror',
157175
'-Wno-format-security',
158176
'-Wno-int-conversion',

0 commit comments

Comments
 (0)