#include "base/synchronization/lock_impl.h"
#include <pthread.h>
#include <atomic>
#include <cstdint>
#include <ostream>
#include <string>
#include "base/check_op.h"
#include "base/posix/safe_strerror.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/lock_metrics_recorder.h"
#include "base/synchronization/synchronization_buildflags.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/background_thread_pool_field_trial.h"
#endif
#if BUILDFLAG(IS_ANDROID)
extern "C" {
int __attribute__((weak)) pthread_mutexattr_setprotocol(
pthread_mutexattr_t* _Nonnull __attr,
int __protocol);
}
#endif
namespace base {
namespace internal {
namespace {
#if BUILDFLAG(ENABLE_MUTEX_PRIORITY_INHERITANCE) && BUILDFLAG(IS_ANDROID)
bool IsMutexPriorityInheritanceEnabled() {
return
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
(pthread_mutexattr_setprotocol != nullptr) &&
#pragma clang diagnostic pop
base::android::BackgroundThreadPoolFieldTrial::
ShouldUsePriorityInheritanceLocks();
}
#endif
#if DCHECK_IS_ON()
const char* AdditionalHintForSystemErrorCode(int error_code) {
switch (error_code) {
case EINVAL:
return "Hint: This is often related to a use-after-free.";
default:
return "";
}
}
#endif
std::string SystemErrorCodeToString(int error_code) {
#if DCHECK_IS_ON()
return base::safe_strerror(error_code) + ". " +
AdditionalHintForSystemErrorCode(error_code);
#else
return std::string();
#endif
}
}
#if DCHECK_IS_ON()
void dcheck_trylock_result(int rv) {
DCHECK(rv == 0 || rv == EBUSY)
<< ". " << base::internal::SystemErrorCodeToString(rv);
}
void dcheck_unlock_result(int rv) {
DCHECK_EQ(rv, 0) << ". " << strerror(rv);
}
#endif
#if BUILDFLAG(IS_FUCHSIA)
#define PRIORITY_INHERITANCE_LOCKS_POSSIBLE() 0
#else
#define PRIORITY_INHERITANCE_LOCKS_POSSIBLE() 1
#endif
LockImpl::LockImpl() {
pthread_mutexattr_t mta;
int rv = pthread_mutexattr_init(&mta);
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
#if PRIORITY_INHERITANCE_LOCKS_POSSIBLE()
if (PriorityInheritanceAvailable()) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT);
#pragma clang diagnostic pop
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
}
#endif
#ifndef NDEBUG
rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK);
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
#endif
rv = pthread_mutex_init(&native_handle_, &mta);
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
rv = pthread_mutexattr_destroy(&mta);
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
}
LockImpl::~LockImpl() {
int rv = pthread_mutex_destroy(&native_handle_);
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
}
void LockImpl::LockInternal() {
LockMetricsRecorder::ScopedLockAcquisitionTimer timer;
int rv = pthread_mutex_lock(&native_handle_);
DCHECK_EQ(rv, 0) << ". " << SystemErrorCodeToString(rv);
}
bool LockImpl::PriorityInheritanceAvailable() {
#if BUILDFLAG(ENABLE_MUTEX_PRIORITY_INHERITANCE) && BUILDFLAG(IS_ANDROID)
return IsMutexPriorityInheritanceEnabled();
#elif PRIORITY_INHERITANCE_LOCKS_POSSIBLE() && BUILDFLAG(IS_APPLE)
return true;
#else
return false;
#endif
}
}
bool KernelSupportsPriorityInheritanceFutex() {
#if BUILDFLAG(IS_ANDROID)
static bool supports_pi_futex = [] {
auto kernel_version = SysInfo::KernelVersionNumber::Current();
return (kernel_version > SysInfo::KernelVersionNumber(6, 12, 13)) ||
((kernel_version > SysInfo::KernelVersionNumber(6, 6, 29)) &&
(kernel_version < SysInfo::KernelVersionNumber(6, 6, INT32_MAX))) ||
((kernel_version > SysInfo::KernelVersionNumber(6, 1, 75)) &&
(kernel_version < SysInfo::KernelVersionNumber(6, 1, INT32_MAX)));
}();
return supports_pi_futex;
#else
return false;
#endif
}
}