From ec58dbe57a6d905d8731951549d2633d8a5c9119 Mon Sep 17 00:00:00 2001 From: owent Date: Wed, 25 Oct 2023 20:46:41 +0800 Subject: [PATCH] Update cmake toolset, optimize lifetime of TLS variable. --- atframework/cmake-toolset | 2 +- src/libcopp/utils/uint64_id_allocator.cpp | 27 ++++++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/atframework/cmake-toolset b/atframework/cmake-toolset index c911fe3..cf3486b 160000 --- a/atframework/cmake-toolset +++ b/atframework/cmake-toolset @@ -1 +1 @@ -Subproject commit c911fe34b494dcb3200408803f25787772e5d8c6 +Subproject commit cf3486b91b0de719a8d6215d34f1a776472f61e1 diff --git a/src/libcopp/utils/uint64_id_allocator.cpp b/src/libcopp/utils/uint64_id_allocator.cpp index 8bfdb5c..b197308 100644 --- a/src/libcopp/utils/uint64_id_allocator.cpp +++ b/src/libcopp/utils/uint64_id_allocator.cpp @@ -79,7 +79,22 @@ static void init_pthread_uint64_id_allocator_tls() { (void)pthread_key_create(>_uint64_id_allocator_tls_key, dtor_pthread_uint64_id_allocator_tls); } -uint64_id_allocator_tls_cache_t *get_uint64_id_allocator_tls_cache() { +struct gt_uint64_id_allocator_tls_cache_main_thread_dtor_t { + gt_uint64_id_allocator_tls_cache_main_thread_dtor_t() {} + + ~gt_uint64_id_allocator_tls_cache_main_thread_dtor_t() { + void *cache_ptr = pthread_getspecific(gt_uint64_id_allocator_tls_key); + pthread_setspecific(gt_uint64_id_allocator_tls_key, nullptr); + dtor_pthread_uint64_id_allocator_tls(cache_ptr); + } +}; +static void init_pthread_get_log_tls_main_thread_dtor() { + static gt_uint64_id_allocator_tls_cache_main_thread_dtor_t gt_uint64_id_allocator_tls_cache_main_thread_dtor; + (void)gt_uint64_id_allocator_tls_cache_main_thread_dtor; +} + +static uint64_id_allocator_tls_cache_t *get_uint64_id_allocator_tls_cache() { + init_pthread_get_log_tls_main_thread_dtor(); (void)pthread_once(>_uint64_id_allocator_tls_once, init_pthread_uint64_id_allocator_tls); uint64_id_allocator_tls_cache_t *ret = reinterpret_cast(pthread_getspecific(gt_uint64_id_allocator_tls_key)); @@ -92,16 +107,6 @@ uint64_id_allocator_tls_cache_t *get_uint64_id_allocator_tls_cache() { return ret; } -struct gt_uint64_id_allocator_tls_cache_main_thread_dtor_t { - uint64_id_allocator_tls_cache_t *cache_ptr; - gt_uint64_id_allocator_tls_cache_main_thread_dtor_t() { cache_ptr = get_uint64_id_allocator_tls_cache(); } - - ~gt_uint64_id_allocator_tls_cache_main_thread_dtor_t() { - pthread_setspecific(gt_uint64_id_allocator_tls_key, nullptr); - dtor_pthread_uint64_id_allocator_tls(reinterpret_cast(cache_ptr)); - } -}; -static gt_uint64_id_allocator_tls_cache_main_thread_dtor_t gt_uint64_id_allocator_tls_cache_main_thread_dtor; #else uint64_id_allocator_tls_cache_t *get_uint64_id_allocator_tls_cache() { static thread_local uint64_id_allocator_tls_cache_t ret = {0, 0};