Skip to content

Commit 65b550e

Browse files
authored
use BAIDU_VOLATILE_THREAD_LOCAL to declare tls_bls to avoid error compiler optimization (#2934)
* add attribute noinline to tls_bls update * update * fix compile error * update * update * update
1 parent 724e2fd commit 65b550e

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/bthread/task_group.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6464
extern 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

8585
int 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.

src/butil/thread_local.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
extern void set_##var_name(type v)
6161
#else
6262
#define BAIDU_GET_VOLATILE_THREAD_LOCAL(var_name) var_name
63-
#define BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(var_name) &##var_name
63+
#define BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(var_name) &var_name
6464
#define BAIDU_SET_VOLATILE_THREAD_LOCAL(var_name, value) var_name = value
6565
#define EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(type, var_name) \
6666
extern BAIDU_THREAD_LOCAL type var_name

0 commit comments

Comments
 (0)