From 330f78f3334bcdcdb4cc2ab2dbf66604e0224d71 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 21 五月 2021 16:21:45 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/valib/bhshmq
---
src/robust.cpp | 13 +++++++++----
1 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/src/robust.cpp b/src/robust.cpp
index 08d2073..4654652 100644
--- a/src/robust.cpp
+++ b/src/robust.cpp
@@ -36,24 +36,29 @@
bool FMutex::try_lock()
{
if (flock(fd_, LOCK_EX | LOCK_NB) == 0) {
+ ++count_;
if (mtx_.try_lock()) {
return true;
} else {
- flock(fd_, LOCK_UN);
+ if (--count_ == 0) {
+ flock(fd_, LOCK_UN);
+ }
}
}
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);
+ ++count_;
mtx_.lock();
}
void FMutex::unlock()
{
mtx_.unlock();
- flock(fd_, LOCK_UN);
+ if (--count_ == 0) {
+ flock(fd_, LOCK_UN);
+ }
}
+
} // namespace robust
\ No newline at end of file
--
Gitblit v1.8.0