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