Skip to content

Commit 33a9cf1

Browse files
authored
Make use of new emscripten_dbg helper function. NFC (#18640)
Also use a helper macro similar to the one used in dynlink.c. `emscripten_dbg` automatically prefixed the current thread ID so we don't need to include pthread_self explicitly.
1 parent 008017b commit 33a9cf1

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

system/lib/pthread/emscripten_tls_init.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <emscripten/heap.h>
99
#include <emscripten/threading.h>
10+
#include <emscripten/console.h>
1011
#include <stdlib.h>
1112
#include <malloc.h>
1213

@@ -15,7 +16,9 @@
1516
// Uncomment to trace TLS allocations.
1617
// #define DEBUG_TLS
1718
#ifdef DEBUG_TLS
18-
#include <stdio.h>
19+
#define dbg(fmt, ...) _emscripten_dbgf(fmt, ##__VA_ARGS__)
20+
#else
21+
#define dbg(fmt, ...)
1922
#endif
2023

2124
// linker-generated symbol that loads static TLS data at the given location.
@@ -49,9 +52,7 @@ static int needs_dynamic_alloc(void) {
4952
void _emscripten_tls_free() {
5053
void* tls_block = __builtin_wasm_tls_base();
5154
if (tls_block && needs_dynamic_alloc()) {
52-
#ifdef DEBUG_TLS
53-
_emscripten_errf("tls free: thread=%p dso=%p <- %p", pthread_self(), &__dso_handle, tls_block);
54-
#endif
55+
dbg("tls free: dso=%p <- %p", &__dso_handle, tls_block);
5556
emscripten_builtin_free(tls_block);
5657
}
5758
}
@@ -69,9 +70,7 @@ void* _emscripten_tls_init(void) {
6970
set_needs_dynamic_alloc();
7071
tls_block = emscripten_builtin_memalign(__builtin_wasm_tls_align(), tls_size);
7172
}
72-
#ifdef DEBUG_TLS
73-
_emscripten_errf("tls init: size=%zu thread=%p dso=%p -> %p:%p", tls_size, pthread_self(), &__dso_handle, tls_block, ((char*)tls_block)+tls_size);
74-
#endif
73+
dbg("tls init: size=%zu dso=%p -> %p:%p", tls_size, &__dso_handle, tls_block, ((char*)tls_block)+tls_size);
7574
__wasm_init_tls(tls_block);
7675
return tls_block;
7776
}

system/lib/pthread/pthread_create.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
#include <threads.h>
1616
#include <unistd.h>
1717
#include <emscripten/heap.h>
18+
#include <emscripten/threading.h>
1819

1920
#define STACK_ALIGN 16
2021
#define TSD_ALIGN (sizeof(void*))
2122

2223
// Comment this line to enable tracing of thread creation and destruction:
2324
// #define PTHREAD_DEBUG
25+
#ifdef PTHREAD_DEBUG
26+
#define dbg(fmt, ...) _emscripten_dbgf(fmt, ##__VA_ARGS__)
27+
#else
28+
#define dbg(fmt, ...)
29+
#endif
2430

2531
// See musl's pthread_create.c
2632

@@ -218,10 +224,14 @@ int __pthread_create(pthread_t* restrict res,
218224
#endif
219225

220226
struct pthread *self = __pthread_self();
221-
#ifdef PTHREAD_DEBUG
222-
_emscripten_errf("start __pthread_create: self=%p new=%p new_end=%p stack=%p->%p stack_size=%zu tls_base=%p",
223-
self, new, new+1, (char*)new->stack - new->stack_size, new->stack, new->stack_size, new->tls_base);
224-
#endif
227+
dbg("start __pthread_create: new=%p new_end=%p stack=%p->%p "
228+
"stack_size=%zu tls_base=%p",
229+
new,
230+
new + 1,
231+
(char*)new->stack - new->stack_size,
232+
new->stack,
233+
new->stack_size,
234+
new->tls_base);
225235

226236
// thread may already be running/exited after the _pthread_create_js call below
227237
__tl_lock();
@@ -250,13 +260,14 @@ int __pthread_create(pthread_t* restrict res,
250260
new->next = new->prev = new;
251261

252262
__tl_unlock();
253-
263+
254264
return rtn;
255265
}
256266

257-
#ifdef PTHREAD_DEBUG
258-
_emscripten_errf("done __pthread_create self=%p next=%p prev=%p new=%p", self, self->next, self->prev, new);
259-
#endif
267+
dbg("done __pthread_create next=%p prev=%p new=%p",
268+
self->next,
269+
self->prev,
270+
new);
260271
*res = new;
261272
return 0;
262273
}
@@ -278,9 +289,7 @@ void _emscripten_thread_free_data(pthread_t t) {
278289
// musl normally allocates this using mmap). This region
279290
// includes the pthread structure itself.
280291
unsigned char* block = t->map_base;
281-
#ifdef PTHREAD_DEBUG
282-
_emscripten_errf("_emscripten_thread_free_data thread=%p map_base=%p", t, block);
283-
#endif
292+
dbg("_emscripten_thread_free_data thread=%p map_base=%p", t, block);
284293
// To aid in debugging, set the entire region to zero.
285294
memset(block, 0, sizeof(struct pthread));
286295
emscripten_builtin_free(block);

0 commit comments

Comments
 (0)