bool timedlock(int64_t milliseconds) const { #if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 200112L PROFILE_MUTEX_START_LOCK(); struct timespec ts; Util::toTimespec(ts, milliseconds + Util::currentTime()); int ret = pthread_mutex_timedlock(&pthread_mutex_, &ts); if (ret == 0) { PROFILE_MUTEX_LOCKED(); return true; } PROFILE_MUTEX_NOT_LOCKED(); return false; #else /* Otherwise follow solution used by Mono for Android */ struct timespec sleepytime, now, to; /* This is just to avoid a completely busy wait */ sleepytime.tv_sec = 0; sleepytime.tv_nsec = 10000000L; /* 10ms */ Util::toTimespec(to, milliseconds + Util::currentTime()); while ((trylock()) == false) { Util::toTimespec(now, Util::currentTime()); if (now.tv_sec >= to.tv_sec && now.tv_nsec >= to.tv_nsec) { return false; } nanosleep(&sleepytime, NULL); } return true; #endif }
bool timedlock(int64_t milliseconds) const { #if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 200112L PROFILE_MUTEX_START_LOCK(); struct timespec ts; Util::toTimespec(ts, milliseconds); int ret = pthread_mutex_timedlock(&pthread_mutex_, &ts); if (ret == 0) { PROFILE_MUTEX_LOCKED(); return true; } PROFILE_MUTEX_NOT_LOCKED(); return false; #else // If pthread_mutex_timedlock isn't supported, the safest thing to do // is just do a nonblocking trylock. return trylock(); #endif }
void acquireWrite() const { PROFILE_MUTEX_START_LOCK(); pthread_rwlock_wrlock(&rw_lock_); PROFILE_MUTEX_LOCKED(); }
void acquireRead() const { PROFILE_MUTEX_START_LOCK(); pthread_rwlock_rdlock(&rw_lock_); PROFILE_MUTEX_NOT_LOCKED(); // not exclusive, so use not-locked path }
void lock() const { PROFILE_MUTEX_START_LOCK(); pthread_mutex_lock(&pthread_mutex_); PROFILE_MUTEX_LOCKED(); }