FST  openfst-1.7.2
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 #include <mutex>
22 
23 namespace fst {
24 
25 using namespace std;
26 
27 class Mutex {
28  public:
29  Mutex() {}
30 
31  inline void Lock() { mu_.lock(); }
32 
33  inline void Unlock() { mu_.unlock(); }
34 
35  private:
36  std::mutex mu_;
37 
38  Mutex(const Mutex &) = delete;
39  Mutex &operator=(const Mutex &) = delete;
40 };
41 
42 class MutexLock {
43  public:
44  explicit MutexLock(Mutex *mu) : mu_(mu) { mu_->Lock(); }
45 
46  ~MutexLock() { mu_->Unlock(); }
47 
48  private:
49  Mutex *mu_;
50 
51  MutexLock(const MutexLock &) = delete;
52  MutexLock &operator=(const MutexLock &) = delete;
53 };
54 
55 // Currently, we don't use a separate reader lock.
56 // TODO(kbg): Implement this with std::shared_mutex once C++17 becomes widely
57 // available.
59 
60 } // namespace fst
61 
62 #endif // FST_LIB_LOCK_H_
void Lock()
Definition: lock.h:31
~MutexLock()
Definition: lock.h:46
MutexLock(Mutex *mu)
Definition: lock.h:44
Mutex()
Definition: lock.h:29
void Unlock()
Definition: lock.h:33