Skip to content

Commit 98688e1

Browse files
yamtjohn-sharratt
authored andcommitted
__init_tp: Initialize TID to non-zero value (WebAssembly#360)
This fixes TID-based locking used within libc. Also, initialize detach_state. cf. WebAssembly/wasi-threads#16
1 parent ab1971f commit 98688e1

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

libc-top-half/musl/src/env/__init_tls.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ int __init_tp(void *p)
8282
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
8383
#else
8484
setup_default_stack_size();
85+
td->detach_state = DT_JOINABLE;
86+
/*
87+
* Initialize the TID to a value which doesn't conflict with
88+
* host-allocated TIDs, so that TID-based locks can work.
89+
*
90+
* Note:
91+
* - Host-allocated TIDs range from 1 to 0x1fffffff. (inclusive)
92+
* - __tl_lock and __lockfile uses TID 0 as "unlocked".
93+
* - __lockfile relies on the fact the most significant two bits
94+
* of TIDs are 0.
95+
*/
96+
td->tid = 0x3fffffff;
8597
#endif
8698
td->locale = &libc.global_locale;
8799
td->robust_list.head = &td->robust_list.head;

0 commit comments

Comments
 (0)