Skip to content

Commit e951ffb

Browse files
zcfhwuminghui
authored and
wuminghui
committed
Fix compiler optimize thread local variable access
`bthread_usleep`/`bthread_yield` contains access to TLS variables. In LTO mode, exceptions may occur due to cross-module optimization. For example, bthread_usleep is inlined into WatchConnections, the compiler(clang-17.0.6) cache the address outside the loop, triggering the error mentioned in #2156.
1 parent df0fbdc commit e951ffb

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/bthread/bthread.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pthread_mutex_t g_task_control_mutex = PTHREAD_MUTEX_INITIALIZER;
7070
TaskControl* g_task_control = NULL;
7171

7272
extern BAIDU_THREAD_LOCAL TaskGroup* tls_task_group;
73+
EXTERN_BAIDU_VOLATILE_THREAD_LOCAL(TaskGroup*, tls_task_group);
7374
extern void (*g_worker_startfn)();
7475
extern void (*g_tagged_worker_startfn)(bthread_tag_t);
7576
extern void* (*g_create_span_func)();
@@ -521,15 +522,15 @@ int bthread_timer_del(bthread_timer_t id) {
521522
}
522523

523524
int bthread_usleep(uint64_t microseconds) {
524-
bthread::TaskGroup* g = bthread::tls_task_group;
525+
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
525526
if (NULL != g && !g->is_current_pthread_task()) {
526527
return bthread::TaskGroup::usleep(&g, microseconds);
527528
}
528529
return ::usleep(microseconds);
529530
}
530531

531532
int bthread_yield(void) {
532-
bthread::TaskGroup* g = bthread::tls_task_group;
533+
bthread::TaskGroup* g = bthread::BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_task_group);
533534
if (NULL != g && !g->is_current_pthread_task()) {
534535
bthread::TaskGroup::yield(&g);
535536
return 0;

0 commit comments

Comments
 (0)