Skip to content

Commit 6085102

Browse files
authored
Minor pthread cleanups. NFC (#26856)
Split out from #13007.
1 parent c505862 commit 6085102

11 files changed

Lines changed: 21 additions & 43 deletions

File tree

src/struct_info_internal.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"_a_transferredcanvases"
1616
],
1717
"thread_profiler_block": [
18-
"threadStatus",
19-
"timeSpentInStatus",
20-
"name"
18+
"threadStatus",
19+
"timeSpentInStatus",
20+
"name"
2121
]
2222
}
2323
},

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
#include <time.h>
33
#include <errno.h>
44
#ifdef __EMSCRIPTEN__
5-
#include <math.h>
6-
#include <emscripten/threading.h>
7-
#include <emscripten/emscripten.h>
5+
#include <math.h> // for INFINITY
86
#else
97
#include "futex.h"
10-
#endif
118
#include "syscall.h"
9+
#endif
1210
#include "pthread_impl.h"
1311

1412
#ifndef __EMSCRIPTEN__

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#ifdef __EMSCRIPTEN__
2-
#include <math.h>
3-
#include <emscripten/threading.h>
2+
#include <math.h> // for INFINITY
43
#endif
54

65
#include "pthread_impl.h"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifdef __EMSCRIPTEN__
2-
#include <assert.h> // For INFINITY
3-
#include <math.h> // For INFINITY
2+
#include <assert.h> // for assert
3+
#include <math.h> // for INFINITY
44
#endif
55

66
#include "pthread_impl.h"

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ static int __pthread_detach(pthread_t t)
3232

3333
weak_alias(__pthread_detach, pthread_detach);
3434
weak_alias(__pthread_detach, thrd_detach);
35-
// XXX EMSCRIPTEN: add extra alias for asan.
35+
#ifdef __EMSCRIPTEN__ // XXX Emscripten add an extra alias for ASan/LSan.
3636
weak_alias(__pthread_detach, emscripten_builtin_pthread_detach);
37+
#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,10 +1,6 @@
11
#include "pthread_impl.h"
22
#include "fork_impl.h"
33

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

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: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,13 @@
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>
15-
#include <poll.h>
1611
#include <pthread.h>
1712
#include <stdarg.h>
18-
#include <stdlib.h>
19-
#include <sys/ioctl.h>
20-
#include <sys/mman.h>
21-
#include <sys/socket.h>
22-
#include <sys/stat.h>
23-
#include <sys/statvfs.h>
24-
#include <sys/time.h>
25-
#include <termios.h>
2613
#include <threads.h>
2714
#include <unistd.h>
28-
#include <utime.h>
2915

3016
#include <emscripten.h>
3117
#include <emscripten/proxying.h>
@@ -136,6 +122,7 @@ void _emscripten_init_main_thread(void) {
136122
// pthread struct prev and next should initially point to itself (see __init_tp),
137123
// this is used by pthread_key_delete for deleting thread-specific data.
138124
__main_pthread.next = __main_pthread.prev = &__main_pthread;
125+
// Initialize thread-specific data area.
139126
__main_pthread.tsd = (void **)__pthread_tsd_main;
140127

141128
_emscripten_init_main_thread_js(&__main_pthread);

system/lib/pthread/pthread_create.c

Lines changed: 6 additions & 7 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"
@@ -108,7 +107,7 @@ int __pthread_create(pthread_t* restrict res,
108107
void* (*entry)(void*),
109108
void* restrict arg) {
110109
// Note on LSAN: lsan intercepts/wraps calls to pthread_create so any
111-
// allocation we do here should be considered leaks.
110+
// allocations we do here should not be considered leaks.
112111
// See: lsan_interceptors.cpp.
113112
if (!res) {
114113
return EINVAL;
@@ -292,10 +291,10 @@ void _emscripten_thread_free_data(pthread_t t) {
292291
}
293292

294293
void _emscripten_thread_exit(void* result) {
295-
struct pthread *self = __pthread_self();
294+
pthread_t self = __pthread_self();
296295
assert(self);
297296

298-
self->canceldisable = PTHREAD_CANCEL_DISABLE;
297+
self->canceldisable = 1;
299298
self->cancelasync = 0;
300299
self->result = result;
301300

@@ -344,8 +343,8 @@ void _emscripten_thread_exit(void* result) {
344343
// Not hosting a pthread anymore in this worker set __pthread_self to NULL
345344
__set_thread_state(NULL, 0, 0, 1);
346345

347-
/* This atomic potentially competes with a concurrent pthread_detach
348-
* call; the loser is responsible for freeing thread resources. */
346+
// This atomic potentially competes with a concurrent pthread_detach
347+
// call; the loser is responsible for freeing thread resources.
349348
int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
350349

351350
if (state == DT_DETACHED) {
@@ -363,7 +362,7 @@ void _emscripten_thread_exit(void* result) {
363362
}
364363
}
365364

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

system/lib/pthread/threading_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS expectedS
105105
#endif
106106

107107
int __pthread_kill_js(pthread_t t, int sig);
108-
int __pthread_create_js(struct __pthread *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
108+
int __pthread_create_js(pthread_t thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
109109
int _emscripten_default_pthread_stack_size();
110110
void __set_thread_state(pthread_t ptr, int is_main, int is_runtime, int can_block);
111111

0 commit comments

Comments
 (0)