MCPcopy Create free account
hub / github.com/Tencent/MMKV / platformLock

Method platformLock

Core/InterProcessLock_Win32.cpp:41–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39}
40
41bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
42 auto realLockType = LockType2Flag(lockType);
43 auto flag = wait ? realLockType : (realLockType | LOCKFILE_FAIL_IMMEDIATELY);
44 if (unLockFirstIfNeeded) {
45 /* try exclusive-lock above shared-lock will always fail in Win32
46 auto ret = LockFileEx(m_fd, realLockType | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &m_overLapped);
47 if (ret) {
48 return true;
49 }*/
50 // let's be gentleman: unlock my shared-lock to prevent deadlock
51 auto ret = UnlockFileEx(m_fd, 0, 1, 0, &m_overLapped);
52 if (!ret) {
53 auto lastError = GetLastError();
54 if (lastError != ERROR_NOT_LOCKED) {
55 MMKVError("fail to try unlock first fd=%p, error:%d", m_fd, lastError);
56 }
57 }
58 }
59
60 auto ret = LockFileEx(m_fd, flag, 0, 1, 0, &m_overLapped);
61 if (!ret) {
62 if (tryAgain) {
63 *tryAgain = (GetLastError() == ERROR_LOCK_VIOLATION);
64 }
65 if (wait) {
66 MMKVError("fail to lock fd=%p, error:%d", m_fd, GetLastError());
67 }
68 // try recover my shared-lock
69 if (unLockFirstIfNeeded) {
70 ret = LockFileEx(m_fd, LockType2Flag(SharedLockType), 0, 1, 0, &m_overLapped);
71 if (!ret) {
72 // let's hope this never happen
73 MMKVError("fail to recover shared-lock fd=%p, error:%d", m_fd, GetLastError());
74 }
75 }
76 return false;
77 } else {
78 return true;
79 }
80}
81
82bool FileLock::platformUnLock(bool unlockToSharedLock) {
83 /* quote from MSDN:

Callers

nothing calls this directly

Calls 1

LockType2FlagFunction · 0.85

Tested by

no test coverage detected