summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjgm@google.com <jgm@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2012-01-27 21:26:58 +0000
committerjgm@google.com <jgm@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2012-01-27 21:26:58 +0000
commit62576d5b3fb1173e4ad1b7fbc98f9d58323dfc64 (patch)
tree67cb21a58f634a2c021726f5a0288683e62b95cb
parent4dcb99d7acb0a551b7122f68a4d15d993f748c9d (diff)
downloadgoogletest-62576d5b3fb1173e4ad1b7fbc98f9d58323dfc64.tar.gz
Locking for Notification class.
git-svn-id: http://googletest.googlecode.com/svn/trunk@610 861a406c-534a-0410-8894-cb66d6ee9925
-rw-r--r--include/gtest/internal/gtest-port.h23
1 files changed, 19 insertions, 4 deletions
diff --git a/include/gtest/internal/gtest-port.h b/include/gtest/internal/gtest-port.h
index 3c8463b..8c96f31 100644
--- a/include/gtest/internal/gtest-port.h
+++ b/include/gtest/internal/gtest-port.h
@@ -1102,22 +1102,37 @@ inline void SleepMilliseconds(int n) {
// use it in user tests, either directly or indirectly.
class Notification {
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
// 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
// thread.
void WaitForNotification() {
- while (!notified_) {
+ for (;;) {
+ pthread_mutex_lock(&mutex_);
+ const bool notified = notified_;
+ pthread_mutex_unlock(&mutex_);
+ if (notified)
+ break;
SleepMilliseconds(10);
}
}
private:
- volatile bool notified_;
+ pthread_mutex_t mutex_;
+ bool notified_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
};