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

Method platformLock

Core/InterProcessLock.cpp:104–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102}
103
104bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
105# ifdef MMKV_ANDROID
106 if (m_useFcntlLock) {
107 return fcntlLock(lockType, wait, unLockFirstIfNeeded, tryAgain);
108 }
109# endif
110 auto realLockType = LockType2FlockType(lockType);
111 auto cmd = wait ? realLockType : (realLockType | LOCK_NB);
112 if (unLockFirstIfNeeded) {
113 // try lock
114 auto ret = flock(m_fd, realLockType | LOCK_NB);
115 if (ret == 0) {
116 return true;
117 }
118 // let's be gentleman: unlock my shared-lock to prevent deadlock
119 ret = flock(m_fd, LOCK_UN);
120 if (ret != 0) {
121 MMKVError("fail to try unlock first fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
122 }
123 }
124
125 auto ret = flock(m_fd, cmd);
126 if (ret != 0) {
127 if (tryAgain) {
128 *tryAgain = (errno == EWOULDBLOCK);
129 }
130 if (wait) {
131 MMKVError("fail to lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
132 }
133 // try recover my shared-lock
134 if (unLockFirstIfNeeded) {
135 ret = flock(m_fd, LockType2FlockType(SharedLockType));
136 if (ret != 0) {
137 // let's hope this never happen
138 MMKVError("fail to recover shared-lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
139 }
140 }
141 return false;
142 } else {
143 return true;
144 }
145}
146
147bool FileLock::platformUnLock(bool unlockToSharedLock) {
148# ifdef MMKV_ANDROID

Callers

nothing calls this directly

Calls 2

flockClass · 0.85
LockType2FlockTypeFunction · 0.70

Tested by

no test coverage detected