15
15
#include <threads.h>
16
16
#include <unistd.h>
17
17
#include <emscripten/heap.h>
18
+ #include <emscripten/threading.h>
18
19
19
20
#define STACK_ALIGN 16
20
21
#define TSD_ALIGN (sizeof(void*))
21
22
22
23
// Comment this line to enable tracing of thread creation and destruction:
23
24
// #define PTHREAD_DEBUG
25
+ #ifdef PTHREAD_DEBUG
26
+ #define dbg (fmt , ...) _emscripten_dbgf(fmt, ##__VA_ARGS__)
27
+ #else
28
+ #define dbg (fmt , ...)
29
+ #endif
24
30
25
31
// See musl's pthread_create.c
26
32
@@ -218,10 +224,14 @@ int __pthread_create(pthread_t* restrict res,
218
224
#endif
219
225
220
226
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 );
225
235
226
236
// thread may already be running/exited after the _pthread_create_js call below
227
237
__tl_lock ();
@@ -250,13 +260,14 @@ int __pthread_create(pthread_t* restrict res,
250
260
new -> next = new -> prev = new ;
251
261
252
262
__tl_unlock ();
253
-
263
+
254
264
return rtn ;
255
265
}
256
266
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 );
260
271
* res = new ;
261
272
return 0 ;
262
273
}
@@ -278,9 +289,7 @@ void _emscripten_thread_free_data(pthread_t t) {
278
289
// musl normally allocates this using mmap). This region
279
290
// includes the pthread structure itself.
280
291
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 );
284
293
// To aid in debugging, set the entire region to zero.
285
294
memset (block , 0 , sizeof (struct pthread ));
286
295
emscripten_builtin_free (block );
0 commit comments