InterProcessLock.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 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. #ifndef MMKV_INTERPROCESSLOCK_H
  21. #define MMKV_INTERPROCESSLOCK_H
  22. #ifdef __cplusplus
  23. # include "MMKVPredef.h"
  24. # include <fcntl.h>
  25. namespace mmkv {
  26. enum LockType {
  27. SharedLockType,
  28. ExclusiveLockType,
  29. };
  30. // a recursive POSIX file-lock wrapper
  31. // handles lock upgrade & downgrade correctly
  32. class FileLock {
  33. MMKVFileHandle_t m_fd;
  34. size_t m_sharedLockCount;
  35. size_t m_exclusiveLockCount;
  36. bool doLock(LockType lockType, bool wait, bool *tryAgain = nullptr);
  37. bool platformLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain);
  38. bool platformUnLock(bool unLockFirstIfNeeded);
  39. # ifndef MMKV_WIN32
  40. bool isFileLockValid() const { return m_fd >= 0; }
  41. # ifdef MMKV_ANDROID
  42. const bool m_isAshmem;
  43. struct flock m_lockInfo;
  44. bool ashmemLock(LockType lockType, bool wait, bool unLockFirstIfNeeded, bool *tryAgain);
  45. bool ashmemUnLock(bool unLockFirstIfNeeded);
  46. # endif
  47. # else // defined(MMKV_WIN32)
  48. OVERLAPPED m_overLapped;
  49. bool isFileLockValid() { return m_fd != INVALID_HANDLE_VALUE; }
  50. # endif // MMKV_WIN32
  51. public:
  52. # ifndef MMKV_WIN32
  53. # ifndef MMKV_ANDROID
  54. explicit FileLock(MMKVFileHandle_t fd) : m_fd(fd), m_sharedLockCount(0), m_exclusiveLockCount(0) {}
  55. # else
  56. // locking with pos & len only works in ashmem lock type (fcntl)
  57. explicit FileLock(MMKVFileHandle_t fd, bool isAshmem = false, int64_t lockPos = 0, int64_t lockLen = 0);
  58. # endif // MMKV_ANDROID
  59. # else // defined(MMKV_WIN32)
  60. explicit FileLock(MMKVFileHandle_t fd) : m_fd(fd), m_overLapped{}, m_sharedLockCount(0), m_exclusiveLockCount(0) {}
  61. # endif // MMKV_WIN32
  62. ~FileLock();
  63. bool lock(LockType lockType);
  64. bool try_lock(LockType lockType, bool *tryAgain);
  65. bool unlock(LockType lockType);
  66. // unlock all and destroy file lock
  67. void destroyAndUnLock();
  68. // just forbid it for possibly misuse
  69. explicit FileLock(const FileLock &other) = delete;
  70. FileLock &operator=(const FileLock &other) = delete;
  71. };
  72. class InterProcessLock {
  73. FileLock *m_fileLock;
  74. LockType m_lockType;
  75. public:
  76. InterProcessLock(FileLock *fileLock, LockType lockType)
  77. : m_fileLock(fileLock), m_lockType(lockType), m_enable(true) {
  78. MMKV_ASSERT(m_fileLock);
  79. }
  80. bool m_enable;
  81. void lock() {
  82. if (m_enable) {
  83. m_fileLock->lock(m_lockType);
  84. }
  85. }
  86. bool try_lock(bool *tryAgain = nullptr) {
  87. if (m_enable) {
  88. return m_fileLock->try_lock(m_lockType, tryAgain);
  89. }
  90. return false;
  91. }
  92. void unlock() {
  93. if (m_enable) {
  94. m_fileLock->unlock(m_lockType);
  95. }
  96. }
  97. };
  98. } // namespace mmkv
  99. #endif // __cplusplus
  100. #endif // MMKV_INTERPROCESSLOCK_H