You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

  1. // Copyright 2005-2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the 'License');
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an 'AS IS' BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // See www.openfst.org for extensive documentation on this weighted
  16. // finite-state transducer library.
  17. //
  18. // Google-compatibility locking declarations and inline definitions.
  19. #ifndef FST_LOCK_H_
  20. #define FST_LOCK_H_
  21. #ifdef OPENFST_HAS_ABSL
  22. namespace fst {
  23. using Mutex;
  24. using MutexLock;
  25. using ReaderMutexLock;
  26. } // namespace fst
  27. #else // OPENFST_HAS_ABSL
  28. #include <shared_mutex> // NOLINT(build/c++14)
  29. namespace fst {
  30. class Mutex {
  31. public:
  32. Mutex() = default;
  33. inline void Lock() { mu_.lock(); }
  34. inline void Unlock() { mu_.unlock(); }
  35. inline void ReaderLock() { mu_.lock_shared(); }
  36. inline void ReaderUnlock() { mu_.unlock_shared(); }
  37. private:
  38. std::shared_mutex mu_;
  39. Mutex(const Mutex &) = delete;
  40. Mutex &operator=(const Mutex &) = delete;
  41. };
  42. class MutexLock {
  43. public:
  44. explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
  45. ~MutexLock() { mu_->Unlock(); }
  46. private:
  47. Mutex *const mu_;
  48. MutexLock(const MutexLock &) = delete;
  49. MutexLock &operator=(const MutexLock &) = delete;
  50. };
  51. class ReaderMutexLock {
  52. public:
  53. explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
  54. ~ReaderMutexLock() { mu_->ReaderUnlock(); }
  55. private:
  56. Mutex *const mu_;
  57. ReaderMutexLock(const ReaderMutexLock &) = delete;
  58. ReaderMutexLock &operator=(const ReaderMutexLock &) = delete;
  59. };
  60. } // namespace fst
  61. #endif // OPENFST_HAS_ABSL
  62. #endif // FST_LOCK_H_