blob: 4a3fb25d14dbb0329996de596b5be6beabc4989d [file] [log] [blame]
#include "aos/mutex/mutex.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
namespace aos {
// Lock and Unlock use the return values of mutex_lock/mutex_unlock
// to determine whether the lock/unlock succeeded.
bool Mutex::Lock() {
const int ret = mutex_grab(&impl_);
if (ret == 0) {
return false;
} else if (ret == 1) {
return true;
} else {
LOG(FATAL) << "mutex_grab(" << &impl_ << "(=" << std::hex << impl_.futex
<< ")) failed with " << ret;
}
}
void Mutex::Unlock() { mutex_unlock(&impl_); }
Mutex::State Mutex::TryLock() {
const int ret = mutex_trylock(&impl_);
switch (ret) {
case 0:
return State::kLocked;
case 1:
return State::kOwnerDied;
case 4:
return State::kLockFailed;
default:
LOG(FATAL) << "mutex_trylock(" << &impl_ << "(=" << std::hex
<< impl_.futex << ")) failed with " << ret;
}
}
bool Mutex::OwnedBySelf() const { return mutex_islocked(&impl_); }
} // namespace aos