Skip to content

Commit af2d3e1

Browse files
committed
bthread: convert ADD/SUB_TLS_PTHREAD_LOCK_COUNT macros to inline functions
Replace the function-like macros ADD_TLS_PTHREAD_LOCK_COUNT and SUB_TLS_PTHREAD_LOCK_COUNT with inline functions AddTlsPthreadLockCount() and SubTlsPthreadLockCount() (with matching no-op definitions in the BRPC_DEBUG_BTHREAD_SCHE_SAFETY=0 branch). Macros lack type safety and scoping, so encapsulating the logic in regular functions is clearer and safer while keeping the same behavior. All call sites are updated accordingly.
1 parent e5ff754 commit af2d3e1

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

src/bthread/mutex.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,13 @@ BAIDU_VOLATILE_THREAD_LOCAL(bool, tls_warn_up, false);
538538
// `tls_pthread_lock_count' is inaccurate, so this feature cannot be used.
539539
BAIDU_VOLATILE_THREAD_LOCAL(int, tls_pthread_lock_count, 0);
540540

541-
#define ADD_TLS_PTHREAD_LOCK_COUNT \
542-
++(*BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(tls_pthread_lock_count))
543-
#define SUB_TLS_PTHREAD_LOCK_COUNT \
544-
--(*BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(tls_pthread_lock_count))
541+
inline void AddTlsPthreadLockCount() {
542+
++(*BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(tls_pthread_lock_count));
543+
}
544+
545+
inline void SubTlsPthreadLockCount() {
546+
--(*BAIDU_GET_PTR_VOLATILE_THREAD_LOCAL(tls_pthread_lock_count));
547+
}
545548

546549
void CheckBthreadScheSafety() {
547550
if (BAIDU_LIKELY(0 == BAIDU_GET_VOLATILE_THREAD_LOCAL(tls_pthread_lock_count))) {
@@ -554,8 +557,8 @@ void CheckBthreadScheSafety() {
554557
<< " pthread locks.";
555558
}
556559
#else
557-
#define ADD_TLS_PTHREAD_LOCK_COUNT ((void)0)
558-
#define SUB_TLS_PTHREAD_LOCK_COUNT ((void)0)
560+
inline void AddTlsPthreadLockCount() {}
561+
inline void SubTlsPthreadLockCount() {}
559562
void CheckBthreadScheSafety() {}
560563
#endif // BRPC_DEBUG_BTHREAD_SCHE_SAFETY
561564

@@ -817,7 +820,7 @@ BUTIL_FORCE_INLINE int pthread_mutex_lock_internal(pthread_mutex_t* mutex,
817820
}
818821
}
819822
if (0 == rc) {
820-
ADD_TLS_PTHREAD_LOCK_COUNT;
823+
AddTlsPthreadLockCount();
821824
}
822825
return rc;
823826
}
@@ -829,7 +832,7 @@ BUTIL_FORCE_INLINE int pthread_mutex_lock_internal(pthread_mutex_t* mutex,
829832
int rc = sys_pthread_mutex_lock(mutex);
830833
if (0 == rc) {
831834
SYS_PTHREAD_MUTEX_SET_OWNER;
832-
ADD_TLS_PTHREAD_LOCK_COUNT;
835+
AddTlsPthreadLockCount();
833836
}
834837
return rc;
835838
}
@@ -840,14 +843,14 @@ BUTIL_FORCE_INLINE int pthread_mutex_trylock_internal(pthread_mutex_t* mutex) {
840843
if (0 == rc) {
841844
FIND_SYS_PTHREAD_MUTEX_OWNER_MAP_ENTRY(mutex);
842845
SYS_PTHREAD_MUTEX_SET_OWNER;
843-
ADD_TLS_PTHREAD_LOCK_COUNT;
846+
AddTlsPthreadLockCount();
844847
}
845848
return rc;
846849
}
847850

848851
BUTIL_FORCE_INLINE int pthread_mutex_unlock_internal(pthread_mutex_t* mutex) {
849852
SYS_PTHREAD_MUTEX_RESET_OWNER(mutex);
850-
SUB_TLS_PTHREAD_LOCK_COUNT;
853+
SubTlsPthreadLockCount();
851854
return sys_pthread_mutex_unlock(mutex);
852855
}
853856
#endif // NO_PTHREAD_MUTEX_HOOK
@@ -1129,7 +1132,7 @@ int FastPthreadMutex::lock_contended(const struct timespec* abstime) {
11291132
}
11301133
}
11311134
PTHREAD_MUTEX_SET_OWNER(_owner);
1132-
ADD_TLS_PTHREAD_LOCK_COUNT;
1135+
AddTlsPthreadLockCount();
11331136
return 0;
11341137
}
11351138

@@ -1147,7 +1150,7 @@ bool FastPthreadMutex::try_lock() {
11471150
bool lock = !split->locked.exchange(1, butil::memory_order_acquire);
11481151
if (lock) {
11491152
PTHREAD_MUTEX_SET_OWNER(_owner);
1150-
ADD_TLS_PTHREAD_LOCK_COUNT;
1153+
AddTlsPthreadLockCount();
11511154
}
11521155
return lock;
11531156
}
@@ -1160,7 +1163,7 @@ bool FastPthreadMutex::timed_lock(const struct timespec* abstime) {
11601163
}
11611164

11621165
void FastPthreadMutex::unlock() {
1163-
SUB_TLS_PTHREAD_LOCK_COUNT;
1166+
SubTlsPthreadLockCount();
11641167
MUTEX_RESET_OWNER_COMMON(_owner);
11651168
auto whole = (butil::atomic<unsigned>*)&_futex;
11661169
const unsigned prev = whole->exchange(0, butil::memory_order_release);

0 commit comments

Comments
 (0)