Home | History | Annotate | Download | only in fst
      1 // lock.h
      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 // Author: riley (at) google.com (Michael Riley)
     16 //
     17 // \file
     18 // Google-compatibility locking declarations and inline definitions
     19 //
     20 // Classes and functions here are no-ops (by design); proper locking requires
     21 // actual implementation.
     22 
     23 #ifndef FST_LIB_LOCK_H__
     24 #define FST_LIB_LOCK_H__
     25 
     26 #include <fst/compat.h>  // for DISALLOW_COPY_AND_ASSIGN
     27 
     28 namespace fst {
     29 
     30 using namespace std;
     31 
     32 //
     33 // Single initialization  - single-thread implementation
     34 //
     35 
     36 typedef int FstOnceType;
     37 
     38 static const int FST_ONCE_INIT = 1;
     39 
     40 inline int FstOnceInit(FstOnceType *once, void (*init)(void)) {
     41   if (*once)
     42     (*init)();
     43   *once = 0;
     44   return 0;
     45 }
     46 
     47 //
     48 // Thread locking - single-thread (non-)implementation
     49 //
     50 
     51 class Mutex {
     52  public:
     53   Mutex() {}
     54 
     55  private:
     56   DISALLOW_COPY_AND_ASSIGN(Mutex);
     57 };
     58 
     59 class MutexLock {
     60  public:
     61   MutexLock(Mutex *) {}
     62 
     63  private:
     64   DISALLOW_COPY_AND_ASSIGN(MutexLock);
     65 };
     66 
     67 class ReaderMutexLock {
     68  public:
     69   ReaderMutexLock(Mutex *) {}
     70 
     71  private:
     72   DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock);
     73 };
     74 
     75 // Reference counting - single-thread implementation
     76 class RefCounter {
     77  public:
     78   RefCounter() : count_(1) {}
     79 
     80   int count() const { return count_; }
     81   int Incr() const { return ++count_; }
     82   int Decr() const {  return --count_; }
     83 
     84  private:
     85   mutable int count_;
     86 
     87   DISALLOW_COPY_AND_ASSIGN(RefCounter);
     88 };
     89 
     90 }  // namespace fst
     91 
     92 #endif  // FST_LIB_LOCK_H__
     93