GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_mutex.h Lines: 53 53 100.0 %
Date: 2019-01-07 12:15:22 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
36798
  static inline int cond_init(CondT* cond) {
80
36798
    return uv_cond_init(cond);
81
  }
82
83
61935
  static inline int mutex_init(MutexT* mutex) {
84
61935
    return uv_mutex_init(mutex);
85
  }
86
87
150161
  static inline void cond_broadcast(CondT* cond) {
88
150161
    uv_cond_broadcast(cond);
89
150161
  }
90
91
33287
  static inline void cond_destroy(CondT* cond) {
92
33287
    uv_cond_destroy(cond);
93
33287
  }
94
95
250660
  static inline void cond_signal(CondT* cond) {
96
250660
    uv_cond_signal(cond);
97
250660
  }
98
99
231851
  static inline void cond_wait(CondT* cond, MutexT* mutex) {
100
231851
    uv_cond_wait(cond, mutex);
101
230529
  }
102
103
59718
  static inline void mutex_destroy(MutexT* mutex) {
104
59718
    uv_mutex_destroy(mutex);
105
59718
  }
106
107
19606294
  static inline void mutex_lock(MutexT* mutex) {
108
19606294
    uv_mutex_lock(mutex);
109
19608062
  }
110
111
19606478
  static inline void mutex_unlock(MutexT* mutex) {
112
19606478
    uv_mutex_unlock(mutex);
113
19605754
  }
114
};
115
116
template <typename Traits>
117
36798
ConditionVariableBase<Traits>::ConditionVariableBase() {
118
36798
  CHECK_EQ(0, Traits::cond_init(&cond_));
119
36798
}
120
121
template <typename Traits>
122
33287
ConditionVariableBase<Traits>::~ConditionVariableBase() {
123
33287
  Traits::cond_destroy(&cond_);
124
33287
}
125
126
template <typename Traits>
127
150161
void ConditionVariableBase<Traits>::Broadcast(const ScopedLock&) {
128
150161
  Traits::cond_broadcast(&cond_);
129
150161
}
130
131
template <typename Traits>
132
250660
void ConditionVariableBase<Traits>::Signal(const ScopedLock&) {
133
250660
  Traits::cond_signal(&cond_);
134
250660
}
135
136
template <typename Traits>
137
231856
void ConditionVariableBase<Traits>::Wait(const ScopedLock& scoped_lock) {
138
231856
  Traits::cond_wait(&cond_, &scoped_lock.mutex_.mutex_);
139
230529
}
140
141
template <typename Traits>
142
61935
MutexBase<Traits>::MutexBase() {
143
61935
  CHECK_EQ(0, Traits::mutex_init(&mutex_));
144
61934
}
145
146
template <typename Traits>
147
59718
MutexBase<Traits>::~MutexBase() {
148
59718
  Traits::mutex_destroy(&mutex_);
149
59718
}
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
19606400
MutexBase<Traits>::ScopedLock::ScopedLock(const MutexBase& mutex)
163
19606400
    : mutex_(mutex) {
164
19606400
  Traits::mutex_lock(&mutex_.mutex_);
165
19608074
}
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
19606396
MutexBase<Traits>::ScopedLock::~ScopedLock() {
173
19606396
  Traits::mutex_unlock(&mutex_.mutex_);
174
19605718
}
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_