Skip to content

Commit 4dfe1d6

Browse files
committed
Avoid global thread list in WASM (cherry-picked from bbc1faa)
1 parent 0f9d8d7 commit 4dfe1d6

File tree

5 files changed

+12
-55
lines changed

5 files changed

+12
-55
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ static void nodtor(void *dummy)
1717
{
1818
}
1919

20+
#ifndef __EMSCRIPTEN__ // XXX Emscripten: There's no global thread list in WASM
2021
static void dummy_0(void)
2122
{
2223
}
2324

2425
weak_alias(dummy_0, __tl_lock);
2526
weak_alias(dummy_0, __tl_unlock);
27+
#endif
2628

2729
int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
2830
{
@@ -52,15 +54,23 @@ int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
5254
int __pthread_key_delete(pthread_key_t k)
5355
{
5456
sigset_t set;
57+
#ifdef __EMSCRIPTEN__ // XXX Emscripten: There's no global thread list in WASM
58+
pthread_t self = __pthread_self();
59+
#else
5560
pthread_t self = __pthread_self(), td=self;
61+
#endif
5662

5763
__block_app_sigs(&set);
5864
__pthread_rwlock_wrlock(&key_lock);
5965

66+
#ifdef __EMSCRIPTEN__ // XXX Emscripten: There's no global thread list in WASM
67+
self->tsd[k] = 0;
68+
#else
6069
__tl_lock();
6170
do td->tsd[k] = 0;
6271
while ((td=td->next)!=self);
6372
__tl_unlock();
73+
#endif
6474

6575
keys[k] = 0;
6676

system/lib/pthread/library_pthread.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,5 @@ void __emscripten_init_main_thread(void) {
843843
// tid is always non-zero.
844844
__main_pthread.tid = getpid();
845845
__main_pthread.locale = &libc.global_locale;
846-
__main_pthread.next = __main_pthread.prev = &__main_pthread;
847846
__main_pthread.tsd = (void **)__pthread_tsd_main;
848847
}

system/lib/pthread/pthread_create.c

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,6 @@ static long dummy_getpid() {
6868
}
6969
weak_alias(dummy_getpid, __syscall_getpid);
7070

71-
static int tl_lock_count;
72-
static int tl_lock_waiters;
73-
74-
volatile int __thread_list_lock;
75-
76-
void __tl_lock(void) {
77-
int tid = __pthread_self()->tid;
78-
int val = __thread_list_lock;
79-
if (val == tid) {
80-
tl_lock_count++;
81-
return;
82-
}
83-
while ((val = a_cas(&__thread_list_lock, 0, tid)))
84-
__wait(&__thread_list_lock, &tl_lock_waiters, val, 0);
85-
}
86-
87-
void __tl_unlock(void) {
88-
if (tl_lock_count) {
89-
tl_lock_count--;
90-
return;
91-
}
92-
a_store(&__thread_list_lock, 0);
93-
if (tl_lock_waiters) __wake(&__thread_list_lock, 1, 0);
94-
}
95-
9671
int __pthread_create(pthread_t* restrict res,
9772
const pthread_attr_t* restrict attrp,
9873
void* (*entry)(void*),
@@ -120,8 +95,6 @@ int __pthread_create(pthread_t* restrict res,
12095
libc.threaded = 1;
12196
}
12297

123-
struct pthread *self = __pthread_self();
124-
12598
// Allocate thread block (pthread_t structure).
12699
struct pthread *new = malloc(sizeof(struct pthread));
127100
// zero-initialize thread structure.
@@ -141,23 +114,8 @@ int __pthread_create(pthread_t* restrict res,
141114
new->tsd = malloc(PTHREAD_KEYS_MAX * sizeof(void*));
142115
memset(new->tsd, 0, PTHREAD_KEYS_MAX * sizeof(void*));
143116

144-
//printf("start __pthread_create: %p\n", self);
145-
int rtn = __pthread_create_js(new, attrp, entry, arg);
146-
if (rtn != 0)
147-
return rtn;
148-
149-
__tl_lock();
150-
151-
new->next = self->next;
152-
new->prev = self;
153-
new->next->prev = new;
154-
new->prev->next = new;
155-
156-
__tl_unlock();
157-
158117
*res = new;
159-
//printf("done __pthread_create self=%p next=%p prev=%p new=%p\n", self, self->next, self->prev, new);
160-
return 0;
118+
return __pthread_create_js(new, attrp, entry, arg);
161119
}
162120

163121
static void free_tls_data() {
@@ -186,14 +144,6 @@ void _emscripten_thread_exit(void* result) {
186144

187145
free_tls_data();
188146

189-
__tl_lock();
190-
191-
self->next->prev = self->prev;
192-
self->prev->next = self->next;
193-
self->prev = self->next = self;
194-
195-
__tl_unlock();
196-
197147
if (self == emscripten_main_browser_thread_id()) {
198148
exit(0);
199149
return;

tests/other/metadce/minimal_main_Oz_USE_PTHREADS_PROXY_TO_PTHREAD.funcs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ $__pthread_self_internal
1010
$__pthread_setcancelstate
1111
$__stdio_write
1212
$__timedwait
13-
$__tl_lock
14-
$__tl_unlock
1513
$__wasi_syscall_ret
1614
$__wasm_call_ctors
1715
$__wasm_init_memory
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17993
1+
17573

0 commit comments

Comments
 (0)