InterProcessLock_Android.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_ANDROID
  22. # include "MMKVLog.h"
  23. # include <sys/file.h>
  24. # include <unistd.h>
  25. # include <cassert>
  26. namespace mmkv {
  27. FileLock::FileLock(MMKVFileHandle_t fd, bool isAshmem, int64_t lockPos, int64_t lockLen)
  28. : m_fd(fd), m_sharedLockCount(0), m_exclusiveLockCount(0), m_isAshmem(isAshmem), m_lockInfo() {
  29. m_lockInfo.l_type = F_WRLCK;
  30. m_lockInfo.l_start = lockPos;
  31. m_lockInfo.l_whence = SEEK_SET;
  32. m_lockInfo.l_len = lockLen;
  33. m_lockInfo.l_pid = 0;
  34. }
  35. static short LockType2FlockType(LockType lockType) {
  36. switch (lockType) {
  37. case SharedLockType:
  38. return F_RDLCK;
  39. case ExclusiveLockType:
  40. return F_WRLCK;
  41. }
  42. }
  43. bool FileLock::ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain) {
  44. m_lockInfo.l_type = LockType2FlockType(lockType);
  45. if (unLockFirstIfNeeded) {
  46. // try lock
  47. auto ret = fcntl(m_fd, F_OFD_SETLK, &m_lockInfo);
  48. if (ret == 0) {
  49. return true;
  50. }
  51. // lets be gentleman: unlock my shared-lock to prevent deadlock
  52. auto type = m_lockInfo.l_type;
  53. m_lockInfo.l_type = F_UNLCK;
  54. ret = fcntl(m_fd, F_OFD_SETLK, &m_lockInfo);
  55. if (ret != 0) {
  56. MMKVError("fail to try unlock first fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
  57. }
  58. m_lockInfo.l_type = type;
  59. }
  60. int cmd = wait ? F_OFD_SETLKW : F_OFD_SETLK;
  61. auto ret = fcntl(m_fd, cmd, &m_lockInfo);
  62. if (ret != 0) {
  63. if (tryAgain) {
  64. *tryAgain = (errno == EAGAIN);
  65. }
  66. if (wait) {
  67. MMKVError("fail to lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
  68. }
  69. // try recover my shared-lock
  70. if (unLockFirstIfNeeded) {
  71. m_lockInfo.l_type = LockType2FlockType(SharedLockType);
  72. ret = fcntl(m_fd, cmd, &m_lockInfo);
  73. if (ret != 0) {
  74. // let's hope this never happen
  75. MMKVError("fail to recover shared-lock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
  76. }
  77. }
  78. return false;
  79. } else {
  80. return true;
  81. }
  82. }
  83. bool FileLock::ashmemUnLock(bool unlockToSharedLock) {
  84. m_lockInfo.l_type = static_cast<short>(unlockToSharedLock ? F_RDLCK : F_UNLCK);
  85. auto ret = fcntl(m_fd, F_OFD_SETLK, &m_lockInfo);
  86. if (ret != 0) {
  87. MMKVError("fail to unlock fd=%d, ret=%d, error:%s", m_fd, ret, strerror(errno));
  88. return false;
  89. } else {
  90. return true;
  91. }
  92. }
  93. } // namespace mmkv
  94. #endif // MMKV_ANDROID