···2727};
28282929// since we still need to handle some calls after pthread_terminate is called and libpthread unwinds its TLS right before calling pthread_terminate,
3030-// we have to use a slightly hackier technique: using one of the system's reserved but unused TLS keys.
3131-// key 200 seems like a good fit; it's in the reserved region but it's not currently listed in `pthread/tsd_private.h` as being in-use by anything.
3030+// we have to use a slightly hackier technique: using one of the system's reserved but unused TLS keys; we use one from the range we currently reserve
3131+// for Darling.
32323333-#define __PTK_XTRACE_TLS 200
3333+#include <darling/emulation/tsd.h>
34343535// TODO: also perform TLS cleanup for other threads when doing a fork
36363737extern "C"
3838void xtrace_tls_thread_cleanup(void) {
3939- tls_table_t table = (tls_table_t)_pthread_getspecific_direct(__PTK_XTRACE_TLS);
3939+ tls_table_t table = (tls_table_t)_pthread_getspecific_direct(__PTK_DARLING_XTRACE_TLS);
4040 if (!table) {
4141 xtrace_tls_debug("no table to cleanup for this thread");
4242 return;
···5858void* xtrace_tls(void* key, size_t size, bool* created, xtrace_tls_destructor_f destructor) {
5959 xtrace_tls_debug("looking up tls variable for key %p", key);
60606161- tls_table_t table = (tls_table_t)_pthread_getspecific_direct(__PTK_XTRACE_TLS);
6161+ tls_table_t table = (tls_table_t)_pthread_getspecific_direct(__PTK_DARLING_XTRACE_TLS);
62626363 xtrace_tls_debug("got %p as table pointer from pthread", table);
6464···7070 xtrace_abort("xtrace: failed TLS table memory allocation");
7171 }
7272 table->size = 0;
7373- _pthread_setspecific_direct(__PTK_XTRACE_TLS, table);
7373+ _pthread_setspecific_direct(__PTK_DARLING_XTRACE_TLS, table);
7474 }
75757676 // check if the key is already present