Skip to content

Commit

Permalink
__init_tp: Initialize TID to non-zero value (WebAssembly#360)
Browse files Browse the repository at this point in the history
This fixes TID-based locking used within libc.

Also, initialize detach_state.

cf. WebAssembly/wasi-threads#16
  • Loading branch information
yamt authored and john-sharratt committed Mar 5, 2023
1 parent ab1971f commit 98688e1
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libc-top-half/musl/src/env/__init_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ int __init_tp(void *p)
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
#else
setup_default_stack_size();
td->detach_state = DT_JOINABLE;
/*
* Initialize the TID to a value which doesn't conflict with
* host-allocated TIDs, so that TID-based locks can work.
*
* Note:
* - Host-allocated TIDs range from 1 to 0x1fffffff. (inclusive)
* - __tl_lock and __lockfile uses TID 0 as "unlocked".
* - __lockfile relies on the fact the most significant two bits
* of TIDs are 0.
*/
td->tid = 0x3fffffff;
#endif
td->locale = &libc.global_locale;
td->robust_list.head = &td->robust_list.head;
Expand Down

0 comments on commit 98688e1

Please sign in to comment.