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: 60 60 100.0 %
Date: 2019-02-26 22:23:30 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
47821
  static inline int cond_init(CondT* cond) {
80
47821
    return uv_cond_init(cond);
81
  }
82
83
86709
  static inline int mutex_init(MutexT* mutex) {
84
86709
    return uv_mutex_init(mutex);
85
  }
86
87
103682
  static inline void cond_broadcast(CondT* cond) {
88
103682
    uv_cond_broadcast(cond);
89
103682
  }
90
91
45408
  static inline void cond_destroy(CondT* cond) {
92
45408
    uv_cond_destroy(cond);
93
45408
  }
94
95
167640
  static inline void cond_signal(CondT* cond) {
96
167640
    uv_cond_signal(cond);
97
167640
  }
98
99
151872
  static inline void cond_wait(CondT* cond, MutexT* mutex) {
100
151872
    uv_cond_wait(cond, mutex);
101
151931
  }
102
103
84765
  static inline void mutex_destroy(MutexT* mutex) {
104
84765
    uv_mutex_destroy(mutex);
105
84766
  }
106
107
19497809
  static inline void mutex_lock(MutexT* mutex) {
108
19497809
    uv_mutex_lock(mutex);
109
19498944
  }
110
111
19498699
  static inline void mutex_unlock(MutexT* mutex) {
112
19498699
    uv_mutex_unlock(mutex);
113
19497992
  }
114
};
115
116
template <typename Traits>
117
47821
ConditionVariableBase<Traits>::ConditionVariableBase() {
118
47821
  CHECK_EQ(0, Traits::cond_init(&cond_));
119
47821
}
120
121
template <typename Traits>
122
45408
ConditionVariableBase<Traits>::~ConditionVariableBase() {
123
45408
  Traits::cond_destroy(&cond_);
124
45408
}
125
126
template <typename Traits>
127
103682
void ConditionVariableBase<Traits>::Broadcast(const ScopedLock&) {
128
103682
  Traits::cond_broadcast(&cond_);
129
103682
}
130
131
template <typename Traits>
132
167640
void ConditionVariableBase<Traits>::Signal(const ScopedLock&) {
133
167640
  Traits::cond_signal(&cond_);
134
167640
}
135
136
template <typename Traits>
137
151871
void ConditionVariableBase<Traits>::Wait(const ScopedLock& scoped_lock) {
138
151871
  Traits::cond_wait(&cond_, &scoped_lock.mutex_.mutex_);
139
151931
}
140
141
template <typename Traits>
142
86709
MutexBase<Traits>::MutexBase() {
143
86709
  CHECK_EQ(0, Traits::mutex_init(&mutex_));
144
86712
}
145
146
template <typename Traits>
147
84765
MutexBase<Traits>::~MutexBase() {
148
84765
  Traits::mutex_destroy(&mutex_);
149
84766
}
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
19497782
MutexBase<Traits>::ScopedLock::ScopedLock(const MutexBase& mutex)
163
19497782
    : mutex_(mutex) {
164
19497782
  Traits::mutex_lock(&mutex_.mutex_);
165
19498811
}
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
19498518
MutexBase<Traits>::ScopedLock::~ScopedLock() {
173
19498518
  Traits::mutex_unlock(&mutex_.mutex_);
174
19497756
}
175
176
template <typename Traits>
177
123
MutexBase<Traits>::ScopedUnlock::ScopedUnlock(const ScopedLock& scoped_lock)
178
123
    : mutex_(scoped_lock.mutex_) {
179
123
  Traits::mutex_unlock(&mutex_.mutex_);
180
123
}
181
182
template <typename Traits>
183
123
MutexBase<Traits>::ScopedUnlock::~ScopedUnlock() {
184
123
  Traits::mutex_lock(&mutex_.mutex_);
185
123
}
186
187
}  // namespace node
188
189
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
190
191
#endif  // SRC_NODE_MUTEX_H_