FST  openfst-1.8.3
OpenFst Library
lock.h
Go to the documentation of this file.
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 
20 #ifndef FST_LOCK_H_
21 #define FST_LOCK_H_
22 
23 #ifdef OPENFST_HAS_ABSL
24 
25 namespace fst {
26 
27 using Mutex;
28 using MutexLock;
29 using ReaderMutexLock;
30 
31 } // namespace fst
32 
33 #else // OPENFST_HAS_ABSL
34 
35 #include <shared_mutex> // NOLINT(build/c++14)
36 
37 namespace fst {
38 
39 class Mutex {
40  public:
41  Mutex() = default;
42 
43  inline void Lock() { mu_.lock(); }
44  inline void Unlock() { mu_.unlock(); }
45 
46  inline void ReaderLock() { mu_.lock_shared(); }
47  inline void ReaderUnlock() { mu_.unlock_shared(); }
48 
49  private:
50  std::shared_mutex mu_;
51 
52  Mutex(const Mutex &) = delete;
53  Mutex &operator=(const Mutex &) = delete;
54 };
55 
56 class MutexLock {
57  public:
58  explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
59  ~MutexLock() { mu_->Unlock(); }
60 
61  private:
62  Mutex *const mu_;
63 
64  MutexLock(const MutexLock &) = delete;
65  MutexLock &operator=(const MutexLock &) = delete;
66 };
67 
69  public:
70  explicit ReaderMutexLock(Mutex *mu) : mu_(mu) { mu_->ReaderLock(); }
71  ~ReaderMutexLock() { mu_->ReaderUnlock(); }
72 
73  private:
74  Mutex *const mu_;
75 
76  ReaderMutexLock(const ReaderMutexLock &) = delete;
77  ReaderMutexLock &operator=(const ReaderMutexLock &) = delete;
78 };
79 
80 } // namespace fst
81 
82 #endif // OPENFST_HAS_ABSL
83 
84 #endif // FST_LOCK_H_
void Lock()
Definition: lock.h:43
~MutexLock()
Definition: lock.h:59
void ReaderLock()
Definition: lock.h:46
MutexLock(Mutex *mu)
Definition: lock.h:58
Mutex()=default
ReaderMutexLock(Mutex *mu)
Definition: lock.h:70
void ReaderUnlock()
Definition: lock.h:47
void Unlock()
Definition: lock.h:44