Locking for Notification class.
This commit is contained in:
parent
4d6f296e8e
commit
cfb40870bc
|
@ -1102,22 +1102,37 @@ inline void SleepMilliseconds(int n) {
|
||||||
// use it in user tests, either directly or indirectly.
|
// use it in user tests, either directly or indirectly.
|
||||||
class Notification {
|
class Notification {
|
||||||
public:
|
public:
|
||||||
Notification() : notified_(false) {}
|
Notification() : notified_(false) {
|
||||||
|
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
|
||||||
|
}
|
||||||
|
~Notification() {
|
||||||
|
pthread_mutex_destroy(&mutex_);
|
||||||
|
}
|
||||||
|
|
||||||
// Notifies all threads created with this notification to start. Must
|
// Notifies all threads created with this notification to start. Must
|
||||||
// be called from the controller thread.
|
// be called from the controller thread.
|
||||||
void Notify() { notified_ = true; }
|
void Notify() {
|
||||||
|
pthread_mutex_lock(&mutex_);
|
||||||
|
notified_ = true;
|
||||||
|
pthread_mutex_unlock(&mutex_);
|
||||||
|
}
|
||||||
|
|
||||||
// Blocks until the controller thread notifies. Must be called from a test
|
// Blocks until the controller thread notifies. Must be called from a test
|
||||||
// thread.
|
// thread.
|
||||||
void WaitForNotification() {
|
void WaitForNotification() {
|
||||||
while (!notified_) {
|
for (;;) {
|
||||||
|
pthread_mutex_lock(&mutex_);
|
||||||
|
const bool notified = notified_;
|
||||||
|
pthread_mutex_unlock(&mutex_);
|
||||||
|
if (notified)
|
||||||
|
break;
|
||||||
SleepMilliseconds(10);
|
SleepMilliseconds(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
volatile bool notified_;
|
pthread_mutex_t mutex_;
|
||||||
|
bool notified_;
|
||||||
|
|
||||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user