Skip to content

Commit 79e872c

Browse files
authored
Make use of musl's __pthread_tsd_size. NFC (#16947)
1 parent 592f67f commit 79e872c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

system/lib/pthread/pthread_create.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ static int dummy_getpid(void) {
7171
}
7272
weak_alias(dummy_getpid, __syscall_getpid);
7373

74+
/* pthread_key_create.c overrides this */
75+
static volatile size_t dummy = 0;
76+
weak_alias(dummy, __pthread_tsd_size);
77+
7478
int __pthread_create(pthread_t* restrict res,
7579
const pthread_attr_t* restrict attrp,
7680
void* (*entry)(void*),
@@ -116,8 +120,10 @@ int __pthread_create(pthread_t* restrict res,
116120
new->locale = &libc.global_locale;
117121

118122
// Allocate memory for thread-local storage and initialize it to zero.
119-
new->tsd = malloc(PTHREAD_KEYS_MAX * sizeof(void*));
120-
memset(new->tsd, 0, PTHREAD_KEYS_MAX * sizeof(void*));
123+
if (__pthread_tsd_size) {
124+
new->tsd = malloc(__pthread_tsd_size);
125+
memset(new->tsd, 0, __pthread_tsd_size);
126+
}
121127

122128
new->detach_state = DT_JOINABLE;
123129

@@ -237,8 +243,10 @@ void _emscripten_thread_exit(void* result) {
237243

238244
// We have the call the buildin free here since lsan handling for this thread
239245
// gets shut down during __pthread_tsd_run_dtors.
240-
emscripten_builtin_free(self->tsd);
241-
self->tsd = NULL;
246+
if (self->tsd) {
247+
emscripten_builtin_free(self->tsd);
248+
self->tsd = NULL;
249+
}
242250

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

0 commit comments

Comments
 (0)