MCPcopy Create free account
hub / github.com/apache/brpc / pthread_mutex_lock_impl

Function pthread_mutex_lock_impl

src/bthread/mutex.cpp:870–920  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

868
869template <typename Mutex>
870BUTIL_FORCE_INLINE int pthread_mutex_lock_impl(Mutex* mutex, const struct timespec* abstime) {
871 // Don't change behavior of lock when profiler is off.
872 if (!g_cp ||
873 // collecting code including backtrace() and submit() may call
874 // pthread_mutex_lock and cause deadlock. Don't sample.
875 tls_inside_lock) {
876 return pthread_mutex_lock_internal(mutex, abstime);
877 }
878 // Don't slow down non-contended locks.
879 int rc = pthread_mutex_trylock_internal(mutex);
880 if (rc != EBUSY) {
881 return rc;
882 }
883 // Ask bvar::Collector if this (contended) locking should be sampled
884 const size_t sampling_range = bvar::is_collectable(&g_cp_sl);
885
886 bthread_contention_site_t* csite = NULL;
887#ifndef DONT_SPEEDUP_PTHREAD_CONTENTION_PROFILER_WITH_TLS
888 TLSPthreadContentionSites& fast_alt = tls_csites;
889 if (fast_alt.cp_version != g_cp_version) {
890 fast_alt.cp_version = g_cp_version;
891 fast_alt.count = 0;
892 }
893 if (fast_alt.count < TLS_MAX_COUNT) {
894 MutexAndContentionSite& entry = fast_alt.list[fast_alt.count++];
895 entry.mutex = mutex;
896 csite = &entry.csite;
897 if (!bvar::is_sampling_range_valid(sampling_range)) {
898 make_contention_site_invalid(&entry.csite);
899 return pthread_mutex_lock_internal(mutex, abstime);
900 }
901 }
902#endif
903 if (!bvar::is_sampling_range_valid(sampling_range)) { // don't sample
904 return pthread_mutex_lock_internal(mutex, abstime);
905 }
906 // Lock and monitor the waiting time.
907 const int64_t start_ns = butil::cpuwide_time_ns();
908 rc = pthread_mutex_lock_internal(mutex, abstime);
909 if (!rc) { // Inside lock
910 if (!csite) {
911 csite = add_pthread_contention_site(mutex);
912 if (csite == NULL) {
913 return rc;
914 }
915 }
916 csite->duration_ns = butil::cpuwide_time_ns() - start_ns;
917 csite->sampling_range = sampling_range;
918 } // else rare
919 return rc;
920}
921
922template <typename Mutex>
923BUTIL_FORCE_INLINE int pthread_mutex_trylock_impl(Mutex* mutex) {

Callers 4

lockMethod · 0.85
timed_lockMethod · 0.85
pthread_mutex_lockFunction · 0.85

Calls 7

is_collectableFunction · 0.85
is_sampling_range_validFunction · 0.85
cpuwide_time_nsFunction · 0.85

Tested by

no test coverage detected