[libcxx] [test] Make some threading tests more robust

Increase the timeout tolerance if TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT
is set, similarly to how it's done in a couple other tests.

Use `std::this_thread::yield();` instead of busylooping. When multiple
threads are busylooping, it's plausible that not all threads even get
started running before the timeout runs out.

This makes the threading tests succeed if run in Windows runners on
Github Actions.

Differential Revision: https://reviews.llvm.org/D131483
This commit is contained in:
Martin Storsjö 2022-08-09 11:56:46 +03:00
parent 63d88ed5bf
commit 81e55ff473
3 changed files with 25 additions and 4 deletions

View File

@ -36,6 +36,16 @@ typedef Clock::duration duration;
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 @@ void f()
std::shared_lock<std::shared_timed_mutex> 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**)

View File

@ -37,6 +37,16 @@ typedef Clock::duration duration;
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 @@ void f()
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
}

View File

@ -54,7 +54,7 @@ void f()
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);