diff options
| author | ptitSeb <seebastien.chev@gmail.com> | 2023-10-16 12:40:22 +0200 |
|---|---|---|
| committer | ptitSeb <seebastien.chev@gmail.com> | 2023-10-16 12:40:22 +0200 |
| commit | d51560211c804f0af08a1f7cb184f74264b085de (patch) | |
| tree | cb367d84cf6f1c7114656cac1f4e6895a1f177a6 /src/libtools/threads.c | |
| parent | 1b620b31d57ff4fa1205cd088e92d908d84cdebf (diff) | |
| download | box64-d51560211c804f0af08a1f7cb184f74264b085de.tar.gz box64-d51560211c804f0af08a1f7cb184f74264b085de.zip | |
Rollback to previous way of handling tls key destructors
Diffstat (limited to 'src/libtools/threads.c')
| -rw-r--r-- | src/libtools/threads.c | 72 |
1 files changed, 28 insertions, 44 deletions
diff --git a/src/libtools/threads.c b/src/libtools/threads.c index ed0d2b0b..a5fe55f7 100644 --- a/src/libtools/threads.c +++ b/src/libtools/threads.c @@ -62,15 +62,6 @@ typedef struct x64_unwind_buff_s { void *__pad[4]; } x64_unwind_buff_t __attribute__((__aligned__)); -typedef struct my_tls_keys_s { - pthread_key_t key; - uintptr_t f; -} my_tls_keys_t; - -static int keys_cap = 0; -static int keys_size = 0; -static my_tls_keys_t *keys = NULL; - typedef void(*vFv_t)(); KHASH_MAP_INIT_INT64(threadstack, threadstack_t*) @@ -144,25 +135,14 @@ typedef struct emuthread_s { x64_unwind_buff_t **cancels; } emuthread_t; +static pthread_key_t thread_key; + static void emuthread_destroy(void* p) { emuthread_t *et = (emuthread_t*)p; if(!et) return; void* ptr; - // check all tls keys - int end = 4; - while(end) { - int still = 0; - for(int i=0; i<keys_size; ++i) { - ptr = pthread_getspecific(keys[i].key); - if(ptr) { - still = 1; - RunFunctionWithEmu(et->emu, 0, keys[i].f, 1, ptr); - } - } - /*if(still) --end; else*/ end=0; - } // check tlsdata if (my_context && (ptr = pthread_getspecific(my_context->tlskey)) != NULL) free_tlsdatasize(ptr); @@ -170,6 +150,7 @@ static void emuthread_destroy(void* p) if(et) { FreeX64Emu(&et->emu); box_free(et); + pthread_setspecific(thread_key, NULL); } } @@ -189,8 +170,6 @@ static void emuthread_cancel(void* p) et->cancel_size = et->cancel_cap = 0; } -static pthread_key_t thread_key; - void thread_set_emu(x64emu_t* emu) { emuthread_t *et = (emuthread_t*)pthread_getspecific(thread_key); @@ -627,6 +606,29 @@ static void* findcleanup_routineFct(void* fct) return NULL; } +// key_dtor +#define GO(A) \ +static uintptr_t my_key_dtor_fct_##A = 0; \ +static void my_key_dtor_##A(void* a) \ +{ \ + RunFunction(my_key_dtor_fct_##A, 1, a);\ +} +SUPER() +#undef GO +static void* findkey_dtorFct(void* fct) +{ + if(!fct) return fct; + if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct); + #define GO(A) if(my_key_dtor_fct_##A == (uintptr_t)fct) return my_key_dtor_##A; + SUPER() + #undef GO + #define GO(A) if(my_key_dtor_fct_##A == 0) {my_key_dtor_fct_##A = (uintptr_t)fct; return my_key_dtor_##A; } + SUPER() + #undef GO + printf_log(LOG_NONE, "Warning, no more slot for pthread key_dtor callback\n"); + return NULL; +} + #undef SUPER // custom implementation of pthread_once... @@ -655,15 +657,7 @@ EXPORT int my___pthread_once(x64emu_t* emu, void* once, void* cb) __attribute__( EXPORT int my_pthread_key_create(x64emu_t* emu, pthread_key_t* key, void* dtor) { (void)emu; - int ret = pthread_key_create(key, NULL/*findkey_destructorFct(dtor)*/); - if(!ret && dtor) { - if(keys_cap==keys_size) { - keys_cap += 8; - keys = box_realloc(keys, sizeof(my_tls_keys_t)*keys_cap); - } - keys[keys_size].f = (uintptr_t)dtor; - keys[keys_size++].key = *key; - } + int ret = pthread_key_create(key, findkey_dtorFct(dtor)); return ret; } EXPORT int my___pthread_key_create(x64emu_t* emu, pthread_key_t* key, void* dtor) __attribute__((alias("my_pthread_key_create"))); @@ -671,18 +665,10 @@ EXPORT int my___pthread_key_create(x64emu_t* emu, pthread_key_t* key, void* dtor EXPORT int my_pthread_key_delete(x64emu_t* emu, pthread_key_t key) { int ret = pthread_key_delete(key); - if(ret) { - for(int i=keys_size-1; i>=0; --i) - if(keys[i].key == key) { - if(i!=keys_size-1) - memmove(keys+i, keys+i+1, sizeof(my_tls_keys_t)*(keys_size-(i+1))); - --keys_size; - } - } return ret; } -pthread_cond_t* alignCond(pthread_cond_t* pc) +static pthread_cond_t* alignCond(pthread_cond_t* pc) { #ifndef NOALIGN if((uintptr_t)pc&7) @@ -1098,8 +1084,6 @@ void clean_current_emuthread() void fini_pthread_helper(box64context_t* context) { - box_free(keys); - keys_size = keys_cap = 0; CleanStackSize(context); clean_current_emuthread(); } |