| | |
| | | |
| | | bool FMutex::try_lock() |
| | | { |
| | | if (flock(fd_, LOCK_EX | LOCK_NB) == 0) { |
| | | if (mtx_.try_lock()) { |
| | | if (mtx_.try_lock()) { |
| | | if (flock(fd_, LOCK_EX | LOCK_NB) == 0) { |
| | | return true; |
| | | } else { |
| | | flock(fd_, LOCK_UN); |
| | | mtx_.unlock(); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | void FMutex::lock() |
| | | { |
| | | //Note: the lock order affects performance a lot, |
| | | // locking fd_ first is about 100 times faster than locking mtx_ first. |
| | | flock(fd_, LOCK_EX); |
| | | mtx_.lock(); |
| | | flock(fd_, LOCK_EX); |
| | | } |
| | | void FMutex::unlock() |
| | | { |
| | | mtx_.unlock(); |
| | | flock(fd_, LOCK_UN); |
| | | mtx_.unlock(); |
| | | } |
| | | |
| | | } // namespace robust |