InterProcessLock_Android.cpp 3.2 KB

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