InterProcessLock.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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() { 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. explicit FileLock(MMKVFileHandle_t fd, bool isAshmem = false);
  57. # endif // MMKV_ANDROID
  58. # else // defined(MMKV_WIN32)
  59. explicit FileLock(MMKVFileHandle_t fd) : m_fd(fd), m_overLapped{}, m_sharedLockCount(0), m_exclusiveLockCount(0) {}
  60. # endif // MMKV_WIN32
  61. bool lock(LockType lockType);
  62. bool try_lock(LockType lockType, bool *tryAgain);
  63. bool unlock(LockType lockType);
  64. // just forbid it for possibly misuse
  65. explicit FileLock(const FileLock &other) = delete;
  66. FileLock &operator=(const FileLock &other) = delete;
  67. };
  68. class InterProcessLock {
  69. FileLock *m_fileLock;
  70. LockType m_lockType;
  71. public:
  72. InterProcessLock(FileLock *fileLock, LockType lockType)
  73. : m_fileLock(fileLock), m_lockType(lockType), m_enable(true) {
  74. MMKV_ASSERT(m_fileLock);
  75. }
  76. bool m_enable;
  77. void lock() {
  78. if (m_enable) {
  79. m_fileLock->lock(m_lockType);
  80. }
  81. }
  82. bool try_lock(bool *tryAgain = nullptr) {
  83. if (m_enable) {
  84. return m_fileLock->try_lock(m_lockType, tryAgain);
  85. }
  86. return false;
  87. }
  88. void unlock() {
  89. if (m_enable) {
  90. m_fileLock->unlock(m_lockType);
  91. }
  92. }
  93. };
  94. } // namespace mmkv
  95. #endif // __cplusplus
  96. #endif // MMKV_INTERPROCESSLOCK_H