|
274 | 274 | # include <TargetConditionals.h>
|
275 | 275 | #endif
|
276 | 276 |
|
277 |
| -#include <iostream> // NOLINT |
| 277 | +#include <condition_variable> // NOLINT |
| 278 | +#include <iostream> |
278 | 279 | #include <locale>
|
279 | 280 | #include <memory>
|
280 |
| -#include <string> // NOLINT |
| 281 | +#include <mutex> // NOLINT |
| 282 | +#include <string> |
281 | 283 | #include <tuple>
|
282 |
| -#include <vector> // NOLINT |
| 284 | +#include <vector> |
283 | 285 |
|
284 | 286 | #include "gtest/internal/custom/gtest-port.h"
|
285 | 287 | #include "gtest/internal/gtest-port-arch.h"
|
@@ -1172,58 +1174,8 @@ inline void SleepMilliseconds(int n) {
|
1172 | 1174 | };
|
1173 | 1175 | nanosleep(&time, nullptr);
|
1174 | 1176 | }
|
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 |
1226 | 1177 |
|
| 1178 | +# elif GTEST_OS_WINDOWS |
1227 | 1179 | GTEST_API_ void SleepMilliseconds(int n);
|
1228 | 1180 |
|
1229 | 1181 | // Provides leak-safe Windows kernel handle ownership.
|
@@ -1255,22 +1207,45 @@ class GTEST_API_ AutoHandle {
|
1255 | 1207 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
|
1256 | 1208 | };
|
1257 | 1209 |
|
| 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 |
1258 | 1217 | // Allows a controller thread to pause execution of newly created
|
1259 | 1218 | // threads until notified. Instances of this class must be created
|
1260 | 1219 | // and destroyed in the controller thread.
|
1261 | 1220 | //
|
1262 | 1221 | // This class is only for testing Google Test's own constructs. Do not
|
1263 | 1222 | // use it in user tests, either directly or indirectly.
|
| 1223 | +// TODO(b/203539622): Replace unconditionally with absl::Notification. |
1264 | 1224 | class GTEST_API_ Notification {
|
1265 | 1225 | public:
|
1266 |
| - Notification(); |
1267 |
| - void Notify(); |
1268 |
| - void WaitForNotification(); |
| 1226 | + Notification() : notified_(false) {} |
| 1227 | + Notification(const Notification&) = delete; |
| 1228 | + Notification& operator=(const Notification&) = delete; |
1269 | 1229 |
|
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 | + } |
1272 | 1237 |
|
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_; |
1274 | 1249 | };
|
1275 | 1250 | # endif // GTEST_HAS_NOTIFICATION_
|
1276 | 1251 |
|
|
0 commit comments