FST  openfst-1.7.5
OpenFst Library
lock.h
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // See www.openfst.org for extensive documentation on this weighted
14 // finite-state transducer library.
15 //
16 // Google-compatibility locking declarations and inline definitions.
17 
18 #ifndef FST_LIB_LOCK_H_
19 #define FST_LIB_LOCK_H_
20 
21 #ifdef OPENFST_HAS_ABSL
22 
23 #include "absl/synchronization/mutex.h"
24 
25 namespace fst {
26 
27 using absl::Mutex;
28 using absl::MutexLock;
30 
31 } // namespace fst
32 
33 #else // OPENFST_HAS_ABSL
34 
35 #include <mutex>
36 
37 namespace fst {
38 
39 class Mutex {
40  public:
41  Mutex() {}
42 
43  inline void Lock() { mu_.lock(); }
44 
45  inline void Unlock() { mu_.unlock(); }
46 
47  private:
48  std::mutex mu_;
49 
50  Mutex(const Mutex &) = delete;
51  Mutex &operator=(const Mutex &) = delete;
52 };
53 
54 class MutexLock {
55  public:
56  explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
57 
58  ~MutexLock() { mu_->Unlock(); }
59 
60  private:
61  Mutex *mu_;
62 
63  MutexLock(const MutexLock &) = delete;
64  MutexLock &operator=(const MutexLock &) = delete;
65 };
66 
67 // Currently, we don't use a separate reader lock.
68 // TODO(kbg): Implement this with std::shared_mutex once C++17 becomes widely
69 // available.
71 
72 } // namespace fst
73 
74 #endif // OPENFST_HAS_ABSL
75 
76 #endif // FST_LIB_LOCK_H_
void Lock()
Definition: lock.h:43
~MutexLock()
Definition: lock.h:58
MutexLock(Mutex *mu)
Definition: lock.h:56
MutexLock ReaderMutexLock
Definition: lock.h:70
Mutex()
Definition: lock.h:41
void Unlock()
Definition: lock.h:45