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
6 changes: 3 additions & 3 deletions src/butil/object_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
}

inline LocalPool* get_or_new_local_pool() {
LocalPool* lp = _local_pool;
LocalPool* lp = BAIDU_GET_VOLATILE_THREAD_LOCAL(_local_pool);
if (BAIDU_LIKELY(lp != NULL)) {
return lp;
}
Expand All @@ -424,7 +424,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
return NULL;
}
BAIDU_SCOPED_LOCK(_change_thread_mutex); //avoid race with clear()
_local_pool = lp;
BAIDU_SET_VOLATILE_THREAD_LOCAL(_local_pool, lp);
butil::thread_atexit(LocalPool::delete_local_pool, lp);
_nlocal.fetch_add(1, butil::memory_order_relaxed);
return lp;
Expand Down Expand Up @@ -523,7 +523,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {

static butil::static_atomic<ObjectPool*> _singleton;
static pthread_mutex_t _singleton_mutex;
static BAIDU_THREAD_LOCAL LocalPool* _local_pool;
STATIC_MEMBER_BAIDU_VOLATILE_THREAD_LOCAL(LocalPool*, _local_pool);
static butil::static_atomic<long> _nlocal;
static butil::static_atomic<size_t> _ngroup;
static pthread_mutex_t _block_group_mutex;
Expand Down
6 changes: 3 additions & 3 deletions src/butil/resource_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
}

inline LocalPool* get_or_new_local_pool() {
LocalPool* lp = _local_pool;
LocalPool* lp = BAIDU_GET_VOLATILE_THREAD_LOCAL(_local_pool);
if (lp != NULL) {
return lp;
}
Expand All @@ -428,7 +428,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
return NULL;
}
BAIDU_SCOPED_LOCK(_change_thread_mutex); //avoid race with clear()
_local_pool = lp;
BAIDU_SET_VOLATILE_THREAD_LOCAL(_local_pool, lp);
butil::thread_atexit(LocalPool::delete_local_pool, lp);
_nlocal.fetch_add(1, butil::memory_order_relaxed);
return lp;
Expand Down Expand Up @@ -524,7 +524,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {

static butil::static_atomic<ResourcePool*> _singleton;
static pthread_mutex_t _singleton_mutex;
static BAIDU_THREAD_LOCAL LocalPool* _local_pool;
STATIC_MEMBER_BAIDU_VOLATILE_THREAD_LOCAL(LocalPool*, _local_pool);
static butil::static_atomic<long> _nlocal;
static butil::static_atomic<size_t> _ngroup;
static pthread_mutex_t _block_group_mutex;
Expand Down
16 changes: 16 additions & 0 deletions src/butil/thread_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
var_name = v; \
}

#define STATIC_MEMBER_BAIDU_VOLATILE_THREAD_LOCAL(type, var_name) \
static BAIDU_THREAD_LOCAL type var_name; \
static __attribute__((noinline, unused)) type get_##var_name(void) { \
asm volatile(""); \
return var_name; \
} \
static __attribute__((noinline, unused)) type *get_ptr_##var_name(void) { \
type *ptr = &var_name; \
asm volatile("" : "+rm"(ptr)); \
return ptr; \
} \
static __attribute__((noinline, unused)) void set_##var_name(type v) { \
asm volatile(""); \
var_name = v; \
}

#if (defined (__aarch64__) && defined (__GNUC__)) || defined(__clang__)
// GNU compiler under aarch and Clang compiler is incorrectly caching the
// address of thread_local variables across a suspend-point. The following
Expand Down
Loading