From cd931001489e7a438f6437f06b031a596b578560 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Thu, 27 Apr 2023 11:29:13 +0200 Subject: [PATCH] [K/N] Fix sleeping in RWSpinLockTest ^KT-56233 --- .../runtime/src/main/cpp/MutexTest.cpp | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/kotlin-native/runtime/src/main/cpp/MutexTest.cpp b/kotlin-native/runtime/src/main/cpp/MutexTest.cpp index ee7f7f388bd..a36d8fb40d6 100644 --- a/kotlin-native/runtime/src/main/cpp/MutexTest.cpp +++ b/kotlin-native/runtime/src/main/cpp/MutexTest.cpp @@ -97,7 +97,8 @@ TEST(RWSpinLockTest, LockSharedWhileLocked) { EXPECT_TRUE(done); mutex.unlock_shared(); }); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + // Wait to give the second thread a chance to try to acquire the lock. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); done = true; mutex.unlock(); } @@ -118,7 +119,8 @@ TEST(RWSpinLockTest, LockWhileLockShared) { EXPECT_TRUE(done); mutex.unlock(); }); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + // Wait to give the second thread a chance to try to acquire the lock. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Shared lock is not where one does mutability. But it's okay here. done = true; mutex.unlock_shared(); @@ -142,35 +144,28 @@ TEST(RWSpinLockTest, LockSharedWhileLockSharedWithPendingLock) { mutex.unlock(); }); // Wait for thread_lock to say that it wants to lock the mutex. - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + bool failed_to_lock = false; + for (int i = 0; i < 50; ++i) { // 5s total waiting time. + std::shared_lock guard(mutex, std::try_to_lock); + if (!guard) { + failed_to_lock = true; + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + EXPECT_TRUE(failed_to_lock); ScopedThread thread_lock_shared([&] { mutex.lock_shared(); EXPECT_THAT(state, 2); mutex.unlock_shared(); }); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + // Wait to give thread_lock_shared a chance to try to acquire the lock. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Shared lock is not where one does mutability. But it's okay here. state = 1; mutex.unlock_shared(); } -TEST(RWSpinLockTest, TryLockSharedWhileLockSharedWithPendingLock) { - RWSpinLock mutex; - bool done = false; - mutex.lock_shared(); - ScopedThread thread_lock([&] { - mutex.lock(); - EXPECT_TRUE(done); - mutex.unlock(); - }); - // Wait for thread_lock to say that it wants to lock the mutex. - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - EXPECT_FALSE(mutex.try_lock_shared()); - // Shared lock is not where one does mutability. But it's okay here. - done = true; - mutex.unlock_shared(); -} - TEST(RWSpinLockTest, LockSharedWhileLockSharedWithFailedPendingLock) { RWSpinLock mutex; mutex.lock_shared();