Skip to content

Commit 7229c36

Browse files
authored
Merge pull request #3084 from zhoukangsheng/fix_bthread_id_unlock_failed
fix: fix bthread_id_unlock failed in aarch64 cpu(fix issue #3083)
2 parents cd22e48 + c1ac17a commit 7229c36

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

src/butil/object_pool_inl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
415415
}
416416

417417
inline LocalPool* get_or_new_local_pool() {
418-
LocalPool* lp = _local_pool;
418+
LocalPool* lp = BAIDU_GET_VOLATILE_THREAD_LOCAL(_local_pool);
419419
if (BAIDU_LIKELY(lp != NULL)) {
420420
return lp;
421421
}
@@ -424,7 +424,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
424424
return NULL;
425425
}
426426
BAIDU_SCOPED_LOCK(_change_thread_mutex); //avoid race with clear()
427-
_local_pool = lp;
427+
BAIDU_SET_VOLATILE_THREAD_LOCAL(_local_pool, lp);
428428
butil::thread_atexit(LocalPool::delete_local_pool, lp);
429429
_nlocal.fetch_add(1, butil::memory_order_relaxed);
430430
return lp;
@@ -523,7 +523,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
523523

524524
static butil::static_atomic<ObjectPool*> _singleton;
525525
static pthread_mutex_t _singleton_mutex;
526-
static BAIDU_THREAD_LOCAL LocalPool* _local_pool;
526+
STATIC_MEMBER_BAIDU_VOLATILE_THREAD_LOCAL(LocalPool*, _local_pool);
527527
static butil::static_atomic<long> _nlocal;
528528
static butil::static_atomic<size_t> _ngroup;
529529
static pthread_mutex_t _block_group_mutex;

src/butil/resource_pool_inl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
419419
}
420420

421421
inline LocalPool* get_or_new_local_pool() {
422-
LocalPool* lp = _local_pool;
422+
LocalPool* lp = BAIDU_GET_VOLATILE_THREAD_LOCAL(_local_pool);
423423
if (lp != NULL) {
424424
return lp;
425425
}
@@ -428,7 +428,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
428428
return NULL;
429429
}
430430
BAIDU_SCOPED_LOCK(_change_thread_mutex); //avoid race with clear()
431-
_local_pool = lp;
431+
BAIDU_SET_VOLATILE_THREAD_LOCAL(_local_pool, lp);
432432
butil::thread_atexit(LocalPool::delete_local_pool, lp);
433433
_nlocal.fetch_add(1, butil::memory_order_relaxed);
434434
return lp;
@@ -524,7 +524,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
524524

525525
static butil::static_atomic<ResourcePool*> _singleton;
526526
static pthread_mutex_t _singleton_mutex;
527-
static BAIDU_THREAD_LOCAL LocalPool* _local_pool;
527+
STATIC_MEMBER_BAIDU_VOLATILE_THREAD_LOCAL(LocalPool*, _local_pool);
528528
static butil::static_atomic<long> _nlocal;
529529
static butil::static_atomic<size_t> _ngroup;
530530
static pthread_mutex_t _block_group_mutex;

src/butil/thread_local.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@
4646
var_name = v; \
4747
}
4848

49+
#define STATIC_MEMBER_BAIDU_VOLATILE_THREAD_LOCAL(type, var_name) \
50+
static BAIDU_THREAD_LOCAL type var_name; \
51+
static __attribute__((noinline, unused)) type get_##var_name(void) { \
52+
asm volatile(""); \
53+
return var_name; \
54+
} \
55+
static __attribute__((noinline, unused)) type *get_ptr_##var_name(void) { \
56+
type *ptr = &var_name; \
57+
asm volatile("" : "+rm"(ptr)); \
58+
return ptr; \
59+
} \
60+
static __attribute__((noinline, unused)) void set_##var_name(type v) { \
61+
asm volatile(""); \
62+
var_name = v; \
63+
}
64+
4965
#if (defined (__aarch64__) && defined (__GNUC__)) || defined(__clang__)
5066
// GNU compiler under aarch and Clang compiler is incorrectly caching the
5167
// address of thread_local variables across a suspend-point. The following

0 commit comments

Comments
 (0)