Skip to content

Commit 593dce4

Browse files
committedFeb 15, 2015
Move util functions into util.cc.
1 parent d981762 commit 593dce4

File tree

3 files changed

+118
-81
lines changed

3 files changed

+118
-81
lines changed
 

‎format.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# limitations under the License.
1515

1616
cd $(dirname $0)
17-
clang-format-3.5 -style=Google -i stenotype/*.{cc,h} stenotype/afl/index_bin.cc
17+
clang-format-3.5 -style=Google -i stenotype/*.{cc,h}

‎stenotype/util.cc

+90
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,94 @@ std::string Dirname(const std::string& filename) {
4141
return std::string(dirname(copy));
4242
}
4343

44+
void Barrier::Block() {
45+
std::unique_lock<std::mutex> lock(mu_);
46+
count_++;
47+
if (count_ >= threads_) {
48+
lock.unlock();
49+
cond_.notify_all();
50+
} else {
51+
while (count_ < threads_) {
52+
cond_.wait(lock);
53+
}
54+
}
55+
}
56+
57+
void Notification::WaitForNotification() {
58+
std::unique_lock<std::mutex> lock(mu_);
59+
while (waiting_) {
60+
cond_.wait(lock);
61+
}
62+
}
63+
64+
void Notification::Notify() {
65+
mu_.lock();
66+
CHECK(waiting_);
67+
waiting_ = false;
68+
mu_.unlock();
69+
cond_.notify_all();
70+
}
71+
72+
void ProducerConsumerQueue::Put(void* val) {
73+
CHECK(val != NULL);
74+
CHECK(!closed_);
75+
std::unique_lock<std::mutex> lock(mu_);
76+
d_.push_back(val);
77+
lock.unlock();
78+
cond_.notify_one();
79+
}
80+
81+
void* ProducerConsumerQueue::Get() {
82+
std::unique_lock<std::mutex> lock(mu_);
83+
while (d_.empty() && !closed_) {
84+
cond_.wait(lock);
85+
}
86+
if (d_.empty() && closed_) {
87+
return NULL;
88+
}
89+
void* ret = d_.front();
90+
d_.pop_front();
91+
return ret;
92+
}
93+
94+
void ProducerConsumerQueue::Close() {
95+
std::unique_lock<std::mutex> lock(mu_);
96+
closed_ = true;
97+
lock.unlock();
98+
cond_.notify_all();
99+
}
100+
101+
void Watchdog::Watch() {
102+
auto last = ctr_;
103+
while (true) {
104+
auto now = GetCurrentTimeMicros();
105+
auto recheck = now + kNumMicrosPerSecond * seconds_;
106+
for (; last == ctr_ && !done_ && now < recheck;
107+
now = GetCurrentTimeMicros()) {
108+
SleepForSeconds(std::min(1.0, double(seconds_) / 10));
109+
}
110+
if (done_) {
111+
return;
112+
} else if (last != ctr_) {
113+
LOG(V2) << "Fed watchdog: " << description_;
114+
last = ctr_;
115+
continue;
116+
}
117+
LOG(FATAL) << "WATCHDOG FAILURE: " << description_;
118+
}
119+
}
120+
121+
Watchdog::Watchdog(std::string description, int seconds)
122+
: description_(description), seconds_(seconds), ctr_(0), done_(false) {
123+
t_ = new std::thread(&Watchdog::Watch, this);
124+
}
125+
126+
Watchdog::~Watchdog() {
127+
done_ = true;
128+
t_->join();
129+
delete t_;
130+
}
131+
132+
void Watchdog::Feed() { ctr_++; }
133+
44134
} // namespace st

‎stenotype/util.h

+27-80
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,9 @@ class Barrier {
241241
public:
242242
explicit Barrier(int threads) : threads_(threads), count_(0) {}
243243
~Barrier() {}
244-
void Block() {
245-
std::unique_lock<std::mutex> lock(mu_);
246-
if (++count_ >= threads_) {
247-
lock.unlock();
248-
cond_.notify_all();
249-
} else {
250-
while (count_ < threads_) {
251-
cond_.wait(lock);
252-
}
253-
}
254-
}
244+
245+
// The first N-1 calls to Block block until the Nth call.
246+
void Block();
255247

256248
private:
257249
int threads_;
@@ -267,19 +259,12 @@ class Notification {
267259
public:
268260
Notification() : waiting_(true) {}
269261
~Notification() {}
270-
void WaitForNotification() {
271-
std::unique_lock<std::mutex> lock(mu_);
272-
while (waiting_) {
273-
cond_.wait(lock);
274-
}
275-
}
276-
void Notify() {
277-
mu_.lock();
278-
CHECK(waiting_);
279-
waiting_ = false;
280-
mu_.unlock();
281-
cond_.notify_all();
282-
}
262+
263+
// Block until Notify is called.
264+
void WaitForNotification();
265+
266+
// Unblock WaitForNotification calls.
267+
void Notify();
283268

284269
private:
285270
bool waiting_;
@@ -295,31 +280,14 @@ class ProducerConsumerQueue {
295280
ProducerConsumerQueue() : closed_(false) {}
296281
~ProducerConsumerQueue() {}
297282

298-
void Put(void* val) {
299-
CHECK(!closed_);
300-
std::unique_lock<std::mutex> lock(mu_);
301-
d_.push_back(val);
302-
lock.unlock();
303-
cond_.notify_one();
304-
}
305-
void* Get() {
306-
std::unique_lock<std::mutex> lock(mu_);
307-
while (d_.empty() && !closed_) {
308-
cond_.wait(lock);
309-
}
310-
if (d_.empty() && closed_) {
311-
return NULL;
312-
}
313-
void* ret = d_.front();
314-
d_.pop_front();
315-
return ret;
316-
}
317-
void Close() {
318-
std::unique_lock<std::mutex> lock(mu_);
319-
closed_ = true;
320-
lock.unlock();
321-
cond_.notify_all();
322-
}
283+
// Add value onto the queue. Must not be NULL.
284+
void Put(void* val);
285+
286+
// Get value off of the queue. Gets NULL if queue is closed.
287+
void* Get();
288+
289+
// Close queue. All subsequent Get calls will immediately return NULL.
290+
void Close();
323291

324292
private:
325293
std::mutex mu_;
@@ -375,40 +343,19 @@ inline std::string UnhiddenFile(const std::string& dirname, int64_t micros) {
375343
// }
376344
// // dog falls out of scope, its thread is canceled and it quietly goes away.
377345
class Watchdog {
378-
private:
379-
void Watch() {
380-
auto last = ctr_;
381-
while (true) {
382-
auto now = GetCurrentTimeMicros();
383-
auto recheck = now + kNumMicrosPerSecond * seconds_;
384-
for (; last == ctr_ && !done_ && now < recheck;
385-
now = GetCurrentTimeMicros()) {
386-
SleepForSeconds(std::min(1.0, double(seconds_) / 10));
387-
}
388-
if (done_) {
389-
return;
390-
} else if (last != ctr_) {
391-
LOG(V2) << "Fed watchdog: " << description_;
392-
last = ctr_;
393-
continue;
394-
}
395-
LOG(FATAL) << "WATCHDOG FAILURE: " << description_;
396-
}
397-
}
398-
399346
public:
400-
Watchdog(std::string description, int seconds)
401-
: description_(description), seconds_(seconds), ctr_(0), done_(false) {
402-
t_ = new std::thread(&Watchdog::Watch, this);
403-
}
404-
~Watchdog() {
405-
done_ = true;
406-
t_->join();
407-
delete t_;
408-
}
409-
void Feed() { ctr_++; }
347+
// Create a watchdog that crashes if it hasn't been fed after X seconds.
348+
Watchdog(std::string description, int seconds);
349+
350+
// Constructor stops the watchdog.
351+
~Watchdog();
352+
353+
// Feed the watchdog.
354+
void Feed();
410355

411356
private:
357+
void Watch();
358+
412359
std::thread* t_;
413360
std::string description_;
414361
int seconds_;

0 commit comments

Comments
 (0)
Please sign in to comment.