]> git.proxmox.com Git - wasi-libc.git/commitdiff
__init_tp: Initialize TID to non-zero value (#360)
authorYAMAMOTO Takashi <yamamoto@midokura.com>
Fri, 6 Jan 2023 19:35:27 +0000 (04:35 +0900)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Wed, 2 Aug 2023 10:24:08 +0000 (12:24 +0200)
This fixes TID-based locking used within libc.

Also, initialize detach_state.

cf. https://github.com/WebAssembly/wasi-threads/pull/16

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

index 5e32f5446ec7e53fa70dd12388fd25470a9cac96..ece8d24e86004cca92d96abcbe3ce8fa9182a5d1 100644 (file)
@@ -70,6 +70,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;