mirror of
https://github.com/Tencent/MMKV.git
synced 2026-10-17 04:30:56 +08:00
tidy Win32 inter-process lock
This commit is contained in:
+27
-80
@@ -26,30 +26,14 @@
|
||||
#endif
|
||||
|
||||
namespace mmkv {
|
||||
#ifndef MMKV_WIN32
|
||||
static uint32_t LockType2FlockType(LockType lockType) {
|
||||
switch (lockType) {
|
||||
case SharedLockType:
|
||||
return LOCK_SH;
|
||||
case ExclusiveLockType:
|
||||
return LOCK_EX;
|
||||
}
|
||||
return LOCK_EX;
|
||||
|
||||
bool FileLock::lock(LockType lockType) {
|
||||
return doLock(lockType, true);
|
||||
}
|
||||
#else
|
||||
static DWORD LockType2Flag(LockType lockType) {
|
||||
DWORD flag = 0;
|
||||
switch (lockType) {
|
||||
case SharedLockType:
|
||||
flag = 0;
|
||||
break;
|
||||
case ExclusiveLockType:
|
||||
flag = LOCKFILE_EXCLUSIVE_LOCK;
|
||||
break;
|
||||
}
|
||||
return flag;
|
||||
|
||||
bool FileLock::try_lock(LockType lockType) {
|
||||
return doLock(lockType, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
bool FileLock::doLock(LockType lockType, bool wait) {
|
||||
if (!isFileLockValid()) {
|
||||
@@ -74,8 +58,21 @@ bool FileLock::doLock(LockType lockType, bool wait) {
|
||||
unLockFirstIfNeeded = true;
|
||||
}
|
||||
}
|
||||
return platformLock(lockType, wait, unLockFirstIfNeeded);
|
||||
}
|
||||
|
||||
#ifndef MMKV_WIN32
|
||||
static uint32_t LockType2FlockType(LockType lockType) {
|
||||
switch (lockType) {
|
||||
case SharedLockType:
|
||||
return LOCK_SH;
|
||||
case ExclusiveLockType:
|
||||
return LOCK_EX;
|
||||
}
|
||||
return LOCK_EX;
|
||||
}
|
||||
|
||||
bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded) {
|
||||
auto realLockType = LockType2FlockType(lockType);
|
||||
int cmd = wait ? realLockType : (realLockType | LOCK_NB);
|
||||
if (unLockFirstIfNeeded) {
|
||||
@@ -99,39 +96,19 @@ bool FileLock::doLock(LockType lockType, bool wait) {
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
auto realLockType = LockType2Flag(lockType);
|
||||
auto flag = wait ? realLockType : (realLockType | LOCKFILE_FAIL_IMMEDIATELY);
|
||||
if (unLockFirstIfNeeded) {
|
||||
/* try exclusive-lock above shared-lock will always fail in Win32
|
||||
auto ret = LockFileEx(m_fd, realLockType | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &m_overLapped);
|
||||
if (ret) {
|
||||
return true;
|
||||
}*/
|
||||
// lets be gentleman: unlock my shared-lock to prevent deadlock
|
||||
auto ret = UnlockFileEx(m_fd, 0, 1, 0, &m_overLapped);
|
||||
if (!ret) {
|
||||
MMKVError("fail to try unlock first fd=%p, error:%d", m_fd, GetLastError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto ret = LockFileEx(m_fd, flag, 0, 1, 0, &m_overLapped);
|
||||
if (!ret) {
|
||||
MMKVError("fail to lock fd=%p, error:%d", m_fd, GetLastError());
|
||||
bool FileLock::platformUnLock(LockType lockType, bool unlockToSharedLock) {
|
||||
int cmd = unlockToSharedLock ? LOCK_SH : LOCK_UN;
|
||||
auto ret = flock(m_fd, cmd);
|
||||
if (ret != 0) {
|
||||
MMKVError("fail to unlock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool FileLock::lock(LockType lockType) {
|
||||
return doLock(lockType, true);
|
||||
}
|
||||
|
||||
bool FileLock::try_lock(LockType lockType) {
|
||||
return doLock(lockType, false);
|
||||
}
|
||||
|
||||
bool FileLock::unlock(LockType lockType) {
|
||||
if (!isFileLockValid()) {
|
||||
@@ -161,37 +138,7 @@ bool FileLock::unlock(LockType lockType) {
|
||||
unlockToSharedLock = true;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef MMKV_WIN32
|
||||
int cmd = unlockToSharedLock ? LOCK_SH : LOCK_UN;
|
||||
auto ret = flock(m_fd, cmd);
|
||||
if (ret != 0) {
|
||||
MMKVError("fail to unlock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
/* quote from MSDN:
|
||||
* If the same range is locked with an exclusive and a shared lock,
|
||||
* two unlock operations are necessary to unlock the region;
|
||||
* the first unlock operation unlocks the exclusive lock,
|
||||
* the second unlock operation unlocks the shared lock.
|
||||
*/
|
||||
if (unlockToSharedLock) {
|
||||
auto flag = LockType2Flag(SharedLockType);
|
||||
if (!LockFileEx(m_fd, flag, 0, 1, 0, &m_overLapped)) {
|
||||
MMKVError("fail to roll back to shared-lock, error:%d", GetLastError());
|
||||
}
|
||||
}
|
||||
auto ret = UnlockFileEx(m_fd, 0, 1, 0, &m_overLapped);
|
||||
if (!ret) {
|
||||
MMKVError("fail to unlock fd=%p, error:%d", m_fd, GetLastError());
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return platformUnLock(lockType, unlockToSharedLock);
|
||||
}
|
||||
|
||||
} // namespace mmkv
|
||||
|
||||
Reference in New Issue
Block a user