diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp --- a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp @@ -36,6 +36,16 @@ typedef std::chrono::milliseconds ms; typedef std::chrono::nanoseconds ns; + +// Thread sanitizer causes more overhead and will sometimes cause this test +// to fail. To prevent this we give Thread sanitizer more time to complete the +// test. +#if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT) +ms Tolerance = ms(200); +#else +ms Tolerance = ms(200 * 5); +#endif + void f() { time_point t0 = Clock::now(); @@ -56,10 +66,11 @@ std::shared_lock lk(m, std::try_to_lock); if (lk.owns_lock()) break; + std::this_thread::yield(); } time_point t1 = Clock::now(); ns d = t1 - t0 - ms(250); - assert(d < ms(200)); // within 200ms + assert(d < Tolerance); // within tolerance } int main(int, char**) diff --git a/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp --- a/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp +++ b/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp @@ -37,6 +37,16 @@ typedef std::chrono::milliseconds ms; typedef std::chrono::nanoseconds ns; + +// Thread sanitizer causes more overhead and will sometimes cause this test +// to fail. To prevent this we give Thread sanitizer more time to complete the +// test. +#if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT) +ms Tolerance = ms(200); +#else +ms Tolerance = ms(200 * 5); +#endif + void f() { time_point t0 = Clock::now(); @@ -44,11 +54,11 @@ assert(!m.try_lock_shared()); assert(!m.try_lock_shared()); while(!m.try_lock_shared()) - ; + std::this_thread::yield(); time_point t1 = Clock::now(); m.unlock_shared(); ns d = t1 - t0 - ms(250); - assert(d < ms(200)); // within 200ms + assert(d < Tolerance); // within tolerance } diff --git a/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp --- a/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp +++ b/libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared.pass.cpp @@ -54,7 +54,7 @@ assert(!m.try_lock_shared()); assert(!m.try_lock_shared()); while(!m.try_lock_shared()) - ; + std::this_thread::yield(); time_point t1 = Clock::now(); m.unlock_shared(); ns d = t1 - t0 - ms(250);