InterProcessLock_Win32.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2019 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "InterProcessLock.h"
  21. #ifdef MMKV_WIN32
  22. # include "MMKVLog.h"
  23. namespace mmkv {
  24. static DWORD LockType2Flag(LockType lockType) {
  25. DWORD flag = 0;
  26. switch (lockType) {
  27. case SharedLockType:
  28. flag = 0;
  29. break;
  30. case ExclusiveLockType:
  31. flag = LOCKFILE_EXCLUSIVE_LOCK;
  32. break;
  33. }
  34. return flag;
  35. }
  36. bool FileLock::platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
  37. auto realLockType = LockType2Flag(lockType);
  38. auto flag = wait ? realLockType : (realLockType | LOCKFILE_FAIL_IMMEDIATELY);
  39. if (unLockFirstIfNeeded) {
  40. /* try exclusive-lock above shared-lock will always fail in Win32
  41. auto ret = LockFileEx(m_fd, realLockType | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &m_overLapped);
  42. if (ret) {
  43. return true;
  44. }*/
  45. // let's be gentleman: unlock my shared-lock to prevent deadlock
  46. auto ret = UnlockFileEx(m_fd, 0, 1, 0, &m_overLapped);
  47. if (!ret) {
  48. auto lastError = GetLastError();
  49. if (lastError != ERROR_NOT_LOCKED) {
  50. MMKVError("fail to try unlock first fd=%p, error:%d", m_fd, lastError);
  51. }
  52. }
  53. }
  54. auto ret = LockFileEx(m_fd, flag, 0, 1, 0, &m_overLapped);
  55. if (!ret) {
  56. if (tryAgain) {
  57. *tryAgain = (GetLastError() == ERROR_LOCK_VIOLATION);
  58. }
  59. if (wait) {
  60. MMKVError("fail to lock fd=%p, error:%d", m_fd, GetLastError());
  61. }
  62. // try recover my shared-lock
  63. if (unLockFirstIfNeeded) {
  64. ret = LockFileEx(m_fd, LockType2Flag(SharedLockType), 0, 1, 0, &m_overLapped);
  65. if (!ret) {
  66. // let's hope this never happen
  67. MMKVError("fail to recover shared-lock fd=%p, error:%d", m_fd, GetLastError());
  68. }
  69. }
  70. return false;
  71. } else {
  72. return true;
  73. }
  74. }
  75. bool FileLock::platformUnLock(bool unlockToSharedLock) {
  76. /* quote from MSDN:
  77. * If the same range is locked with an exclusive and a shared lock,
  78. * two unlock operations are necessary to unlock the region;
  79. * the first unlock operation unlocks the exclusive lock,
  80. * the second unlock operation unlocks the shared lock.
  81. */
  82. if (unlockToSharedLock) {
  83. auto flag = LockType2Flag(SharedLockType);
  84. if (!LockFileEx(m_fd, flag, 0, 1, 0, &m_overLapped)) {
  85. MMKVError("fail to roll back to shared-lock, error:%d", GetLastError());
  86. }
  87. }
  88. auto ret = UnlockFileEx(m_fd, 0, 1, 0, &m_overLapped);
  89. if (!ret) {
  90. auto lastError = GetLastError();
  91. if (lastError != ERROR_NOT_LOCKED) {
  92. MMKVError("fail to unlock fd=%p, error:%d", m_fd, lastError);
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. } // namespace mmkv
  99. #endif // MMKV_WIN32