Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group, NULL);
// Sync with TaskMeta::local_storage when a bthread is created or destroyed.
// During running, the two fields may be inconsistent, use tls_bls as the
// groundtruth.
__thread LocalStorage tls_bls = BTHREAD_LOCAL_STORAGE_INITIALIZER;
BAIDU_VOLATILE_THREAD_LOCAL(LocalStorage, tls_bls, BTHREAD_LOCAL_STORAGE_INITIALIZER);

// defined in bthread/key.cpp
extern void return_keytable(bthread_keytable_pool_t*, KeyTable*);
Expand All @@ -79,7 +79,7 @@ void* run_create_span_func() {
if (g_create_span_func) {
return g_create_span_func();
}
return tls_bls.rpcz_parent_span;
return BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls).rpcz_parent_span;
}

int TaskGroup::get_attr(bthread_t tid, bthread_attr_t* out) {
Expand Down Expand Up @@ -372,11 +372,13 @@ void TaskGroup::task_runner(intptr_t skip_remained) {
// Clean tls variables, must be done before changing version_butex
// otherwise another thread just joined this thread may not see side
// effects of destructing tls variables.
KeyTable* kt = tls_bls.keytable;
LocalStorage* tls_bls_ptr = BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(tls_bls);
KeyTable* kt = tls_bls_ptr->keytable;
if (kt != NULL) {
return_keytable(m->attr.keytable_pool, kt);
// After deletion: tls may be set during deletion.
tls_bls.keytable = NULL;
tls_bls_ptr = BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(tls_bls);
tls_bls_ptr->keytable = NULL;
Comment thread
BiteTheDDDDt marked this conversation as resolved.
m->local_storage.keytable = NULL; // optional
}

Expand Down Expand Up @@ -697,8 +699,8 @@ void TaskGroup::sched_to(TaskGroup** pg, TaskMeta* next_meta, bool cur_ending) {
if (__builtin_expect(next_meta != cur_meta, 1)) {
g->_cur_meta = next_meta;
// Switch tls_bls
cur_meta->local_storage = tls_bls;
tls_bls = next_meta->local_storage;
cur_meta->local_storage = BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_bls);
BAIDU_SET_VOLATILE_THREAD_LOCAL(tls_bls, next_meta->local_storage);

// Logging must be done after switching the local storage, since the logging lib
// use bthread local storage internally, or will cause memory leak.
Expand Down
2 changes: 1 addition & 1 deletion src/butil/thread_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
extern void set_##var_name(type v)
#else
#define BAIDU_GET_VOLATILE_THREAD_LOCAL(var_name) var_name
#define BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(var_name) &##var_name
#define BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(var_name) &var_name
#define BAIDU_SET_VOLATILE_THREAD_LOCAL(var_name, value) var_name = value
#define EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(type, var_name) \
extern BAIDU_THREAD_LOCAL type var_name
Expand Down
Loading