Skip to content

Commit f503588

Browse files
derekmauroCJ-Johnson
authored andcommitted
Googletest export
Replace the multiple implementations of Notification with a single portable implementation. The also removes the awkward loop with sleep in Notification and will allow the removal of SleepMilliseconds. PiperOrigin-RevId: 405399733
1 parent 16f637f commit f503588

File tree

2 files changed

+35
-77
lines changed

2 files changed

+35
-77
lines changed

googletest/include/gtest/internal/gtest-port.h

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,14 @@
274274
# include <TargetConditionals.h>
275275
#endif
276276

277-
#include <iostream> // NOLINT
277+
#include <condition_variable> // NOLINT
278+
#include <iostream>
278279
#include <locale>
279280
#include <memory>
280-
#include <string> // NOLINT
281+
#include <mutex> // NOLINT
282+
#include <string>
281283
#include <tuple>
282-
#include <vector> // NOLINT
284+
#include <vector>
283285

284286
#include "gtest/internal/custom/gtest-port.h"
285287
#include "gtest/internal/gtest-port-arch.h"
@@ -1172,58 +1174,8 @@ inline void SleepMilliseconds(int n) {
11721174
};
11731175
nanosleep(&time, nullptr);
11741176
}
1175-
# endif // GTEST_HAS_PTHREAD
1176-
1177-
# if GTEST_HAS_NOTIFICATION_
1178-
// Notification has already been imported into the namespace.
1179-
// Nothing to do here.
1180-
1181-
# elif GTEST_HAS_PTHREAD
1182-
// Allows a controller thread to pause execution of newly created
1183-
// threads until notified. Instances of this class must be created
1184-
// and destroyed in the controller thread.
1185-
//
1186-
// This class is only for testing Google Test's own constructs. Do not
1187-
// use it in user tests, either directly or indirectly.
1188-
class Notification {
1189-
public:
1190-
Notification() : notified_(false) {
1191-
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr));
1192-
}
1193-
~Notification() {
1194-
pthread_mutex_destroy(&mutex_);
1195-
}
1196-
1197-
// Notifies all threads created with this notification to start. Must
1198-
// be called from the controller thread.
1199-
void Notify() {
1200-
pthread_mutex_lock(&mutex_);
1201-
notified_ = true;
1202-
pthread_mutex_unlock(&mutex_);
1203-
}
1204-
1205-
// Blocks until the controller thread notifies. Must be called from a test
1206-
// thread.
1207-
void WaitForNotification() {
1208-
for (;;) {
1209-
pthread_mutex_lock(&mutex_);
1210-
const bool notified = notified_;
1211-
pthread_mutex_unlock(&mutex_);
1212-
if (notified)
1213-
break;
1214-
SleepMilliseconds(10);
1215-
}
1216-
}
1217-
1218-
private:
1219-
pthread_mutex_t mutex_;
1220-
bool notified_;
1221-
1222-
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1223-
};
1224-
1225-
# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
12261177

1178+
# elif GTEST_OS_WINDOWS
12271179
GTEST_API_ void SleepMilliseconds(int n);
12281180

12291181
// Provides leak-safe Windows kernel handle ownership.
@@ -1255,22 +1207,45 @@ class GTEST_API_ AutoHandle {
12551207
GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
12561208
};
12571209

1210+
# endif
1211+
1212+
# if GTEST_HAS_NOTIFICATION_
1213+
// Notification has already been imported into the namespace.
1214+
// Nothing to do here.
1215+
1216+
# else
12581217
// Allows a controller thread to pause execution of newly created
12591218
// threads until notified. Instances of this class must be created
12601219
// and destroyed in the controller thread.
12611220
//
12621221
// This class is only for testing Google Test's own constructs. Do not
12631222
// use it in user tests, either directly or indirectly.
1223+
// TODO(b/203539622): Replace unconditionally with absl::Notification.
12641224
class GTEST_API_ Notification {
12651225
public:
1266-
Notification();
1267-
void Notify();
1268-
void WaitForNotification();
1226+
Notification() : notified_(false) {}
1227+
Notification(const Notification&) = delete;
1228+
Notification& operator=(const Notification&) = delete;
12691229

1270-
private:
1271-
AutoHandle event_;
1230+
// Notifies all threads created with this notification to start. Must
1231+
// be called from the controller thread.
1232+
void Notify() {
1233+
std::lock_guard<std::mutex> lock(mu_);
1234+
notified_ = true;
1235+
cv_.notify_all();
1236+
}
12721237

1273-
GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1238+
// Blocks until the controller thread notifies. Must be called from a test
1239+
// thread.
1240+
void WaitForNotification() {
1241+
std::unique_lock<std::mutex> lock(mu_);
1242+
cv_.wait(lock, [this]() { return notified_; });
1243+
}
1244+
1245+
private:
1246+
std::mutex mu_;
1247+
std::condition_variable cv_;
1248+
bool notified_;
12741249
};
12751250
# endif // GTEST_HAS_NOTIFICATION_
12761251

googletest/src/gtest-port.cc

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,6 @@ bool AutoHandle::IsCloseable() const {
322322
return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE;
323323
}
324324

325-
Notification::Notification()
326-
: event_(::CreateEvent(nullptr, // Default security attributes.
327-
TRUE, // Do not reset automatically.
328-
FALSE, // Initially unset.
329-
nullptr)) { // Anonymous event.
330-
GTEST_CHECK_(event_.Get() != nullptr);
331-
}
332-
333-
void Notification::Notify() {
334-
GTEST_CHECK_(::SetEvent(event_.Get()) != FALSE);
335-
}
336-
337-
void Notification::WaitForNotification() {
338-
GTEST_CHECK_(
339-
::WaitForSingleObject(event_.Get(), INFINITE) == WAIT_OBJECT_0);
340-
}
341-
342325
Mutex::Mutex()
343326
: owner_thread_id_(0),
344327
type_(kDynamic),

0 commit comments

Comments
 (0)