Skip to content

Commit 62576d5

Browse files
Locking for Notification class.
git-svn-id: http://googletest.googlecode.com/svn/trunk@610 861a406c-534a-0410-8894-cb66d6ee9925
1 parent 4dcb99d commit 62576d5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

include/gtest/internal/gtest-port.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,22 +1102,37 @@ inline void SleepMilliseconds(int n) {
11021102
// use it in user tests, either directly or indirectly.
11031103
class Notification {
11041104
public:
1105-
Notification() : notified_(false) {}
1105+
Notification() : notified_(false) {
1106+
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1107+
}
1108+
~Notification() {
1109+
pthread_mutex_destroy(&mutex_);
1110+
}
11061111

11071112
// Notifies all threads created with this notification to start. Must
11081113
// be called from the controller thread.
1109-
void Notify() { notified_ = true; }
1114+
void Notify() {
1115+
pthread_mutex_lock(&mutex_);
1116+
notified_ = true;
1117+
pthread_mutex_unlock(&mutex_);
1118+
}
11101119

11111120
// Blocks until the controller thread notifies. Must be called from a test
11121121
// thread.
11131122
void WaitForNotification() {
1114-
while (!notified_) {
1123+
for (;;) {
1124+
pthread_mutex_lock(&mutex_);
1125+
const bool notified = notified_;
1126+
pthread_mutex_unlock(&mutex_);
1127+
if (notified)
1128+
break;
11151129
SleepMilliseconds(10);
11161130
}
11171131
}
11181132

11191133
private:
1120-
volatile bool notified_;
1134+
pthread_mutex_t mutex_;
1135+
bool notified_;
11211136

11221137
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
11231138
};

0 commit comments

Comments
 (0)