Skip to content

Commit 5a255d5

Browse files
authored
__init_tp: Initialize TID to non-zero value (#360)
This fixes TID-based locking used within libc. Also, initialize detach_state. cf. WebAssembly/wasi-threads#16
1 parent 35fee1d commit 5a255d5

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
@@ -70,6 +70,18 @@ int __init_tp(void *p)
7070
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
7171
#else
7272
setup_default_stack_size();
73+
td->detach_state = DT_JOINABLE;
74+
/*
75+
* Initialize the TID to a value which doesn't conflict with
76+
* host-allocated TIDs, so that TID-based locks can work.
77+
*
78+
* Note:
79+
* - Host-allocated TIDs range from 1 to 0x1fffffff. (inclusive)
80+
* - __tl_lock and __lockfile uses TID 0 as "unlocked".
81+
* - __lockfile relies on the fact the most significant two bits
82+
* of TIDs are 0.
83+
*/
84+
td->tid = 0x3fffffff;
7385
#endif
7486
td->locale = &libc.global_locale;
7587
td->robust_list.head = &td->robust_list.head;

0 commit comments

Comments
 (0)