From 489ef888d942ecd40063b0e074b2901a2c1754bb Mon Sep 17 00:00:00 2001 From: dmauro Date: Mon, 25 Oct 2021 13:40:39 -0400 Subject: [PATCH] Googletest export Remove GoogleTest's SleepMilliseconds function. It is only used in tests and a portable implementation is available. PiperOrigin-RevId: 405437102 --- googletest/include/gtest/internal/gtest-port.h | 16 +--------------- googletest/src/gtest-port.cc | 4 ---- googletest/test/googletest-port-test.cc | 12 ++++++++---- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index de00483d..63ef13c1 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -1163,21 +1163,8 @@ void ClearInjectableArgvs(); // Defines synchronization primitives. #if GTEST_IS_THREADSAFE -# if GTEST_HAS_PTHREAD -// Sleeps for (roughly) n milliseconds. This function is only for testing -// Google Test's own constructs. Don't use it in user tests, either -// directly or indirectly. -inline void SleepMilliseconds(int n) { - const timespec time = { - 0, // 0 seconds. - n * 1000L * 1000L, // And n ms. - }; - nanosleep(&time, nullptr); -} - -# elif GTEST_OS_WINDOWS -GTEST_API_ void SleepMilliseconds(int n); +# if GTEST_OS_WINDOWS // Provides leak-safe Windows kernel handle ownership. // Used in death tests and in threading support. class GTEST_API_ AutoHandle { @@ -1206,7 +1193,6 @@ class GTEST_API_ AutoHandle { GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle); }; - # endif # if GTEST_HAS_NOTIFICATION_ diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index 6faa8b90..f63625b5 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -280,10 +280,6 @@ size_t GetThreadCount() { #if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS -void SleepMilliseconds(int n) { - ::Sleep(static_cast(n)); -} - AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {} diff --git a/googletest/test/googletest-port-test.cc b/googletest/test/googletest-port-test.cc index 16d30c46..b14e1f76 100644 --- a/googletest/test/googletest-port-test.cc +++ b/googletest/test/googletest-port-test.cc @@ -36,8 +36,10 @@ # include #endif // GTEST_OS_MAC +#include // NOLINT #include #include +#include // NOLINT #include // For std::pair and std::make_pair. #include @@ -333,7 +335,7 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) { break; } - SleepMilliseconds(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } // Retry if an arbitrary other thread was created or destroyed. @@ -1050,7 +1052,7 @@ class AtomicCounterWithMutex { int temp = value_; { // We need to put up a memory barrier to prevent reads and writes to - // value_ rearranged with the call to SleepMilliseconds when observed + // value_ rearranged with the call to sleep_for when observed // from other threads. #if GTEST_HAS_PTHREAD // On POSIX, locking a mutex puts up a memory barrier. We cannot use @@ -1061,7 +1063,8 @@ class AtomicCounterWithMutex { pthread_mutex_init(&memory_barrier_mutex, nullptr)); GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&memory_barrier_mutex)); - SleepMilliseconds(static_cast(random_.Generate(30))); + std::this_thread::sleep_for( + std::chrono::milliseconds(random_.Generate(30))); GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&memory_barrier_mutex)); GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&memory_barrier_mutex)); @@ -1069,7 +1072,8 @@ class AtomicCounterWithMutex { // On Windows, performing an interlocked access puts up a memory barrier. volatile LONG dummy = 0; ::InterlockedIncrement(&dummy); - SleepMilliseconds(static_cast(random_.Generate(30))); + std::this_thread::sleep_for( + std::chrono::milliseconds(random_.Generate(30))); ::InterlockedIncrement(&dummy); #else # error "Memory barrier not implemented on this platform."