@@ -58,7 +58,7 @@ BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group, NULL);
5858// Sync with TaskMeta::local_storage when a bthread is created or destroyed.
5959// During running, the two fields may be inconsistent, use tls_bls as the
6060// groundtruth.
61- __thread LocalStorage tls_bls = BTHREAD_LOCAL_STORAGE_INITIALIZER;
61+ BAIDU_VOLATILE_THREAD_LOCAL ( LocalStorage, tls_bls, BTHREAD_LOCAL_STORAGE_INITIALIZER) ;
6262
6363// defined in bthread/key.cpp
6464extern void return_keytable (bthread_keytable_pool_t *, KeyTable*);
@@ -79,7 +79,7 @@ void* run_create_span_func() {
7979 if (g_create_span_func) {
8080 return g_create_span_func ();
8181 }
82- return tls_bls.rpcz_parent_span ;
82+ return BAIDU_GET_VOLATILE_THREAD_LOCAL ( tls_bls) .rpcz_parent_span ;
8383}
8484
8585int TaskGroup::get_attr (bthread_t tid, bthread_attr_t * out) {
@@ -372,11 +372,13 @@ void TaskGroup::task_runner(intptr_t skip_remained) {
372372 // Clean tls variables, must be done before changing version_butex
373373 // otherwise another thread just joined this thread may not see side
374374 // effects of destructing tls variables.
375- KeyTable* kt = tls_bls.keytable ;
375+ LocalStorage* tls_bls_ptr = BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL (tls_bls);
376+ KeyTable* kt = tls_bls_ptr->keytable ;
376377 if (kt != NULL ) {
377378 return_keytable (m->attr .keytable_pool , kt);
378379 // After deletion: tls may be set during deletion.
379- tls_bls.keytable = NULL ;
380+ tls_bls_ptr = BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL (tls_bls);
381+ tls_bls_ptr->keytable = NULL ;
380382 m->local_storage .keytable = NULL ; // optional
381383 }
382384
@@ -697,8 +699,8 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta, bool cur_ending) {
697699 if (__builtin_expect (next_meta != cur_meta, 1 )) {
698700 g->_cur_meta = next_meta;
699701 // Switch tls_bls
700- cur_meta->local_storage = tls_bls;
701- tls_bls = next_meta->local_storage ;
702+ cur_meta->local_storage = BAIDU_GET_VOLATILE_THREAD_LOCAL ( tls_bls) ;
703+ BAIDU_SET_VOLATILE_THREAD_LOCAL ( tls_bls, next_meta->local_storage ) ;
702704
703705 // Logging must be done after switching the local storage, since the logging lib
704706 // use bthread local storage internally, or will cause memory leak.
0 commit comments