Refactor and fix more flaky shared_mutex tests
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@245918 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
4add27b763
commit
7bcd5704d9
@ -21,6 +21,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
std::shared_timed_mutex m;
|
||||
|
||||
typedef std::chrono::system_clock Clock;
|
||||
@ -29,6 +31,19 @@ typedef Clock::duration duration;
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::nanoseconds ns;
|
||||
|
||||
|
||||
ms WaitTime = ms(250);
|
||||
|
||||
// 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 !TEST_HAS_FEATURE(thread_sanitizer)
|
||||
ms Tolerance = ms(50);
|
||||
#else
|
||||
ms Tolerance = ms(100);
|
||||
#endif
|
||||
|
||||
|
||||
void f()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
|
@ -22,6 +22,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
std::shared_timed_mutex m;
|
||||
|
||||
typedef std::chrono::system_clock Clock;
|
||||
@ -30,14 +32,27 @@ typedef Clock::duration duration;
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::nanoseconds ns;
|
||||
|
||||
|
||||
ms WaitTime = ms(250);
|
||||
|
||||
// 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 !TEST_HAS_FEATURE(thread_sanitizer)
|
||||
ms Tolerance = ms(50);
|
||||
#else
|
||||
ms Tolerance = ms(100);
|
||||
#endif
|
||||
|
||||
|
||||
void f()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
m.lock_shared();
|
||||
time_point t1 = Clock::now();
|
||||
m.unlock_shared();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
void g()
|
||||
@ -47,7 +62,7 @@ void g()
|
||||
time_point t1 = Clock::now();
|
||||
m.unlock_shared();
|
||||
ns d = t1 - t0;
|
||||
assert(d < ms(50)); // within 50ms
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
|
||||
@ -57,7 +72,7 @@ int main()
|
||||
std::vector<std::thread> v;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
v.push_back(std::thread(f));
|
||||
std::this_thread::sleep_for(ms(250));
|
||||
std::this_thread::sleep_for(WaitTime);
|
||||
m.unlock();
|
||||
for (auto& t : v)
|
||||
t.join();
|
||||
@ -65,7 +80,7 @@ int main()
|
||||
for (auto& t : v)
|
||||
t = std::thread(g);
|
||||
std::thread q(f);
|
||||
std::this_thread::sleep_for(ms(250));
|
||||
std::this_thread::sleep_for(WaitTime);
|
||||
m.unlock_shared();
|
||||
for (auto& t : v)
|
||||
t.join();
|
||||
|
@ -22,6 +22,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
std::shared_timed_mutex m;
|
||||
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
@ -30,23 +32,36 @@ typedef Clock::duration duration;
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::nanoseconds ns;
|
||||
|
||||
|
||||
ms WaitTime = ms(250);
|
||||
|
||||
// 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 !TEST_HAS_FEATURE(thread_sanitizer)
|
||||
ms Tolerance = ms(50);
|
||||
#else
|
||||
ms Tolerance = ms(100);
|
||||
#endif
|
||||
|
||||
|
||||
void f1()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_for(ms(300)) == true);
|
||||
assert(m.try_lock_for(WaitTime + Tolerance) == true);
|
||||
time_point t1 = Clock::now();
|
||||
m.unlock();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
void f2()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_for(ms(250)) == false);
|
||||
assert(m.try_lock_for(WaitTime) == false);
|
||||
time_point t1 = Clock::now();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -54,14 +69,14 @@ int main()
|
||||
{
|
||||
m.lock();
|
||||
std::thread t(f1);
|
||||
std::this_thread::sleep_for(ms(250));
|
||||
std::this_thread::sleep_for(WaitTime);
|
||||
m.unlock();
|
||||
t.join();
|
||||
}
|
||||
{
|
||||
m.lock();
|
||||
std::thread t(f2);
|
||||
std::this_thread::sleep_for(ms(300));
|
||||
std::this_thread::sleep_for(WaitTime + Tolerance);
|
||||
m.unlock();
|
||||
t.join();
|
||||
}
|
||||
|
@ -23,6 +23,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
std::shared_timed_mutex m;
|
||||
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
@ -31,23 +33,35 @@ typedef Clock::duration duration;
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::nanoseconds ns;
|
||||
|
||||
ms WaitTime = ms(250);
|
||||
|
||||
// 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 !TEST_HAS_FEATURE(thread_sanitizer)
|
||||
ms Tolerance = ms(50);
|
||||
#else
|
||||
ms Tolerance = ms(100);
|
||||
#endif
|
||||
|
||||
|
||||
void f1()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_shared_for(ms(300)) == true);
|
||||
assert(m.try_lock_shared_for(WaitTime + Tolerance) == true);
|
||||
time_point t1 = Clock::now();
|
||||
m.unlock_shared();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within 50ms
|
||||
}
|
||||
|
||||
void f2()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_shared_for(ms(250)) == false);
|
||||
assert(m.try_lock_shared_for(WaitTime) == false);
|
||||
time_point t1 = Clock::now();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within 50ms
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -57,7 +71,7 @@ int main()
|
||||
std::vector<std::thread> v;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
v.push_back(std::thread(f1));
|
||||
std::this_thread::sleep_for(ms(250));
|
||||
std::this_thread::sleep_for(WaitTime);
|
||||
m.unlock();
|
||||
for (auto& t : v)
|
||||
t.join();
|
||||
@ -67,7 +81,7 @@ int main()
|
||||
std::vector<std::thread> v;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
v.push_back(std::thread(f2));
|
||||
std::this_thread::sleep_for(ms(300));
|
||||
std::this_thread::sleep_for(WaitTime + Tolerance);
|
||||
m.unlock();
|
||||
for (auto& t : v)
|
||||
t.join();
|
||||
|
@ -23,6 +23,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
std::shared_timed_mutex m;
|
||||
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
@ -31,23 +33,35 @@ typedef Clock::duration duration;
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::nanoseconds ns;
|
||||
|
||||
ms WaitTime = ms(250);
|
||||
|
||||
// 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 !TEST_HAS_FEATURE(thread_sanitizer)
|
||||
ms Tolerance = ms(50);
|
||||
#else
|
||||
ms Tolerance = ms(100);
|
||||
#endif
|
||||
|
||||
|
||||
void f1()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_shared_until(Clock::now() + ms(300)) == true);
|
||||
assert(m.try_lock_shared_until(Clock::now() + WaitTime + Tolerance) == true);
|
||||
time_point t1 = Clock::now();
|
||||
m.unlock_shared();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within 50ms
|
||||
}
|
||||
|
||||
void f2()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_shared_until(Clock::now() + ms(250)) == false);
|
||||
assert(m.try_lock_shared_until(Clock::now() + WaitTime) == false);
|
||||
time_point t1 = Clock::now();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -57,7 +71,7 @@ int main()
|
||||
std::vector<std::thread> v;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
v.push_back(std::thread(f1));
|
||||
std::this_thread::sleep_for(ms(250));
|
||||
std::this_thread::sleep_for(WaitTime);
|
||||
m.unlock();
|
||||
for (auto& t : v)
|
||||
t.join();
|
||||
@ -67,7 +81,7 @@ int main()
|
||||
std::vector<std::thread> v;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
v.push_back(std::thread(f2));
|
||||
std::this_thread::sleep_for(ms(300));
|
||||
std::this_thread::sleep_for(WaitTime + Tolerance);
|
||||
m.unlock();
|
||||
for (auto& t : v)
|
||||
t.join();
|
||||
|
@ -32,23 +32,36 @@ typedef Clock::duration duration;
|
||||
typedef std::chrono::milliseconds ms;
|
||||
typedef std::chrono::nanoseconds ns;
|
||||
|
||||
|
||||
ms WaitTime = ms(250);
|
||||
|
||||
// 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 !TEST_HAS_FEATURE(thread_sanitizer)
|
||||
ms Tolerance = ms(50);
|
||||
#else
|
||||
ms Tolerance = ms(100);
|
||||
#endif
|
||||
|
||||
|
||||
void f1()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_until(Clock::now() + ms(300)) == true);
|
||||
assert(m.try_lock_until(Clock::now() + WaitTime + Tolerance) == true);
|
||||
time_point t1 = Clock::now();
|
||||
m.unlock();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
void f2()
|
||||
{
|
||||
time_point t0 = Clock::now();
|
||||
assert(m.try_lock_until(Clock::now() + ms(250)) == false);
|
||||
assert(m.try_lock_until(Clock::now() + WaitTime) == false);
|
||||
time_point t1 = Clock::now();
|
||||
ns d = t1 - t0 - ms(250);
|
||||
assert(d < ms(50)); // within 50ms
|
||||
ns d = t1 - t0 - WaitTime;
|
||||
assert(d < Tolerance); // within tolerance
|
||||
}
|
||||
|
||||
int main()
|
||||
@ -56,14 +69,14 @@ int main()
|
||||
{
|
||||
m.lock();
|
||||
std::thread t(f1);
|
||||
std::this_thread::sleep_for(ms(250));
|
||||
std::this_thread::sleep_for(WaitTime);
|
||||
m.unlock();
|
||||
t.join();
|
||||
}
|
||||
{
|
||||
m.lock();
|
||||
std::thread t(f2);
|
||||
std::this_thread::sleep_for(ms(300));
|
||||
std::this_thread::sleep_for(WaitTime + Tolerance);
|
||||
m.unlock();
|
||||
t.join();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user