GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_mutex.h Lines: 53 53 100.0 %
Date: 2019-02-13 22:28:58 Branches: 2 4 50.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_MUTEX_H_
2
#define SRC_NODE_MUTEX_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "util.h"
7
#include "uv.h"
8
9
namespace node {
10
11
template <typename Traits> class ConditionVariableBase;
12
template <typename Traits> class MutexBase;
13
struct LibuvMutexTraits;
14
15
using ConditionVariable = ConditionVariableBase<LibuvMutexTraits>;
16
using Mutex = MutexBase<LibuvMutexTraits>;
17
18
template <typename Traits>
19
class MutexBase {
20
 public:
21
  inline MutexBase();
22
  inline ~MutexBase();
23
  inline void Lock();
24
  inline void Unlock();
25
26
  class ScopedLock;
27
  class ScopedUnlock;
28
29
  class ScopedLock {
30
   public:
31
    inline explicit ScopedLock(const MutexBase& mutex);
32
    inline explicit ScopedLock(const ScopedUnlock& scoped_unlock);
33
    inline ~ScopedLock();
34
35
   private:
36
    template <typename> friend class ConditionVariableBase;
37
    friend class ScopedUnlock;
38
    const MutexBase& mutex_;
39
    DISALLOW_COPY_AND_ASSIGN(ScopedLock);
40
  };
41
42
  class ScopedUnlock {
43
   public:
44
    inline explicit ScopedUnlock(const ScopedLock& scoped_lock);
45
    inline ~ScopedUnlock();
46
47
   private:
48
    friend class ScopedLock;
49
    const MutexBase& mutex_;
50
    DISALLOW_COPY_AND_ASSIGN(ScopedUnlock);
51
  };
52
53
 private:
54
  template <typename> friend class ConditionVariableBase;
55
  mutable typename Traits::MutexT mutex_;
56
  DISALLOW_COPY_AND_ASSIGN(MutexBase);
57
};
58
59
template <typename Traits>
60
class ConditionVariableBase {
61
 public:
62
  using ScopedLock = typename MutexBase<Traits>::ScopedLock;
63
64
  inline ConditionVariableBase();
65
  inline ~ConditionVariableBase();
66
  inline void Broadcast(const ScopedLock&);
67
  inline void Signal(const ScopedLock&);
68
  inline void Wait(const ScopedLock& scoped_lock);
69
70
 private:
71
  typename Traits::CondT cond_;
72
  DISALLOW_COPY_AND_ASSIGN(ConditionVariableBase);
73
};
74
75
struct LibuvMutexTraits {
76
  using CondT = uv_cond_t;
77
  using MutexT = uv_mutex_t;
78
79
48079
  static inline int cond_init(CondT* cond) {
80
48079
    return uv_cond_init(cond);
81
  }
82
83
82387
  static inline int mutex_init(MutexT* mutex) {
84
82387
    return uv_mutex_init(mutex);
85
  }
86
87
120141
  static inline void cond_broadcast(CondT* cond) {
88
120141
    uv_cond_broadcast(cond);
89
120141
  }
90
91
45502
  static inline void cond_destroy(CondT* cond) {
92
45502
    uv_cond_destroy(cond);
93
45502
  }
94
95
189759
  static inline void cond_signal(CondT* cond) {
96
189759
    uv_cond_signal(cond);
97
189759
  }
98
99
171684
  static inline void cond_wait(CondT* cond, MutexT* mutex) {
100
171684
    uv_cond_wait(cond, mutex);
101
171729
  }
102
103
80362
  static inline void mutex_destroy(MutexT* mutex) {
104
80362
    uv_mutex_destroy(mutex);
105
80362
  }
106
107
18793560
  static inline void mutex_lock(MutexT* mutex) {
108
18793560
    uv_mutex_lock(mutex);
109
18794665
  }
110
111
18794506
  static inline void mutex_unlock(MutexT* mutex) {
112
18794506
    uv_mutex_unlock(mutex);
113
18793739
  }
114
};
115
116
template <typename Traits>
117
48079
ConditionVariableBase<Traits>::ConditionVariableBase() {
118
48079
  CHECK_EQ(0, Traits::cond_init(&cond_));
119
48079
}
120
121
template <typename Traits>
122
45502
ConditionVariableBase<Traits>::~ConditionVariableBase() {
123
45502
  Traits::cond_destroy(&cond_);
124
45502
}
125
126
template <typename Traits>
127
120141
void ConditionVariableBase<Traits>::Broadcast(const ScopedLock&) {
128
120141
  Traits::cond_broadcast(&cond_);
129
120141
}
130
131
template <typename Traits>
132
189759
void ConditionVariableBase<Traits>::Signal(const ScopedLock&) {
133
189759
  Traits::cond_signal(&cond_);
134
189759
}
135
136
template <typename Traits>
137
171698
void ConditionVariableBase<Traits>::Wait(const ScopedLock& scoped_lock) {
138
171698
  Traits::cond_wait(&cond_, &scoped_lock.mutex_.mutex_);
139
171729
}
140
141
template <typename Traits>
142
82387
MutexBase<Traits>::MutexBase() {
143
82387
  CHECK_EQ(0, Traits::mutex_init(&mutex_));
144
82386
}
145
146
template <typename Traits>
147
80362
MutexBase<Traits>::~MutexBase() {
148
80362
  Traits::mutex_destroy(&mutex_);
149
80362
}
150
151
template <typename Traits>
152
void MutexBase<Traits>::Lock() {
153
  Traits::mutex_lock(&mutex_);
154
}
155
156
template <typename Traits>
157
void MutexBase<Traits>::Unlock() {
158
  Traits::mutex_unlock(&mutex_);
159
}
160
161
template <typename Traits>
162
18793599
MutexBase<Traits>::ScopedLock::ScopedLock(const MutexBase& mutex)
163
18793599
    : mutex_(mutex) {
164
18793599
  Traits::mutex_lock(&mutex_.mutex_);
165
18794626
}
166
167
template <typename Traits>
168
MutexBase<Traits>::ScopedLock::ScopedLock(const ScopedUnlock& scoped_unlock)
169
    : MutexBase(scoped_unlock.mutex_) {}
170
171
template <typename Traits>
172
18794432
MutexBase<Traits>::ScopedLock::~ScopedLock() {
173
18794432
  Traits::mutex_unlock(&mutex_.mutex_);
174
18793696
}
175
176
template <typename Traits>
177
MutexBase<Traits>::ScopedUnlock::ScopedUnlock(const ScopedLock& scoped_lock)
178
    : mutex_(scoped_lock.mutex_) {
179
  Traits::mutex_unlock(&mutex_.mutex_);
180
}
181
182
template <typename Traits>
183
MutexBase<Traits>::ScopedUnlock::~ScopedUnlock() {
184
  Traits::mutex_lock(&mutex_.mutex_);
185
}
186
187
}  // namespace node
188
189
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
190
191
#endif  // SRC_NODE_MUTEX_H_