Refactor shared_timed_mutex tests.

First I removed all of the uses of _LIBCPP_STD_VER and added LIT UNSUPPORTED tags to prevent the tests from being run in older standard dialects.
Second I increased the time tolerances used in some tests when testing with Thread Sanitizer because thread sanitizer make these tests take longer.


git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@245793 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier 2015-08-22 21:24:01 +00:00
parent 2fdc443f37
commit a73d0926fc
21 changed files with 119 additions and 146 deletions

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -20,9 +21,7 @@
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_timed_mutex> ul;
assert(!ul.owns_lock());
assert(ul.mutex() == nullptr);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,16 +19,11 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m0;
std::shared_timed_mutex m1;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_timed_mutex> lk0(m0);
std::shared_lock<std::shared_timed_mutex> lk1(m1);
lk1 = std::move(lk0);
@ -35,5 +31,4 @@ int main()
assert(lk1.owns_lock() == true);
assert(lk0.mutex() == nullptr);
assert(lk0.owns_lock() == false);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,18 +19,13 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
std::shared_lock<std::shared_timed_mutex> lk0(m);
std::shared_lock<std::shared_timed_mutex> lk = std::move(lk0);
assert(lk.mutex() == &m);
assert(lk.owns_lock() == true);
assert(lk0.mutex() == nullptr);
assert(lk0.owns_lock() == false);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -21,9 +22,7 @@
#include <cstdlib>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
#include "test_macros.h"
typedef std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
@ -31,6 +30,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
std::shared_timed_mutex m;
void f()
{
time_point t0 = Clock::now();
@ -39,8 +51,8 @@ void f()
std::shared_lock<std::shared_timed_mutex> ul(m);
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
}
void g()
@ -52,30 +64,30 @@ void g()
t1 = Clock::now();
}
ns d = t1 - t0;
assert(d < ms(50)); // within 50ms
assert(d < Tolerance); // within tolerance
}
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
m.lock();
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));
m.unlock();
for (auto& t : v)
t.join();
m.lock_shared();
for (auto& t : v)
t = std::thread(g);
std::thread q(f);
std::this_thread::sleep_for(ms(250));
m.unlock_shared();
for (auto& t : v)
t.join();
q.join();
#endif // _LIBCPP_STD_VER > 11
{
m.lock();
for (int i = 0; i < 5; ++i)
v.push_back(std::thread(f));
std::this_thread::sleep_for(WaitTime);
m.unlock();
for (auto& t : v)
t.join();
}
{
m.lock_shared();
for (auto& t : v)
t = std::thread(g);
std::thread q(f);
std::this_thread::sleep_for(WaitTime);
m.unlock_shared();
for (auto& t : v)
t.join();
q.join();
}
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -20,11 +21,9 @@
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
m.lock_shared();
std::shared_lock<std::shared_timed_mutex> lk(m, std::adopt_lock);
assert(lk.mutex() == &m);
assert(lk.owns_lock() == true);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -20,10 +21,8 @@
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
assert(lk.mutex() == &m);
assert(lk.owns_lock() == false);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -22,7 +23,7 @@
#include <cstdlib>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#include "test_macros.h"
std::shared_timed_mutex m;
@ -32,37 +33,46 @@ 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();
std::shared_lock<std::shared_timed_mutex> lk(m, ms(300));
std::shared_lock<std::shared_timed_mutex> lk(m, WaitTime + Tolerance);
assert(lk.owns_lock() == true);
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
}
void f2()
{
time_point t0 = Clock::now();
std::shared_lock<std::shared_timed_mutex> lk(m, ms(250));
std::shared_lock<std::shared_timed_mutex> lk(m, WaitTime);
assert(lk.owns_lock() == 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
}
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
m.lock();
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();
@ -72,10 +82,9 @@ 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();
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -22,7 +23,7 @@
#include <cstdlib>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#include "test_macros.h"
std::shared_timed_mutex m;
@ -32,37 +33,46 @@ 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();
std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + ms(300));
std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + WaitTime + Tolerance);
assert(lk.owns_lock() == true);
time_point t1 = Clock::now();
ns d = t1 - t0 - ms(250);
assert(d < ns(50000000)); // within 50ms
ns d = t1 - t0 - WaitTime;
assert(d < Tolerance); // within 50ms
}
void f2()
{
time_point t0 = Clock::now();
std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + ms(250));
std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + WaitTime);
assert(lk.owns_lock() == 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
}
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
{
m.lock();
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();
@ -72,10 +82,9 @@ 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();
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -21,8 +22,6 @@
#include <cstdlib>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
typedef std::chrono::system_clock Clock;
@ -57,11 +56,8 @@ void f()
assert(d < ms(200)); // within 200ms
}
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
m.lock();
std::vector<std::thread> v;
for (int i = 0; i < 5; ++i)
@ -70,5 +66,4 @@ int main()
m.unlock();
for (auto& t : v)
t.join();
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -21,7 +22,7 @@
#include <cstdlib>
#include <cassert>
#if _LIBCPP_STD_VER > 11
#include "test_macros.h"
std::shared_timed_mutex m;
@ -31,6 +32,18 @@ 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(25);
#else
ms Tolerance = ms(75);
#endif
void f()
{
std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
@ -38,8 +51,8 @@ void f()
lk.lock();
time_point t1 = Clock::now();
assert(lk.owns_lock() == true);
ns d = t1 - t0 - ms(250);
assert(d < ms(25)); // within 25ms
ns d = t1 - t0 - WaitTime;
assert(d < Tolerance); // within tolerance
try
{
lk.lock();
@ -62,18 +75,14 @@ void f()
}
}
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
m.lock();
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();
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,8 +19,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
bool try_lock_called = false;
struct mutex
@ -34,11 +33,9 @@ struct mutex
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<mutex> lk(m, std::defer_lock);
assert(lk.try_lock() == true);
assert(try_lock_called == true);
@ -66,5 +63,4 @@ int main()
{
assert(e.code().value() == EPERM);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -19,8 +20,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
bool try_lock_for_called = false;
typedef std::chrono::milliseconds ms;
@ -39,11 +38,8 @@ struct mutex
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<mutex> lk(m, std::defer_lock);
assert(lk.try_lock_for(ms(5)) == true);
assert(try_lock_for_called == true);
@ -71,5 +67,4 @@ int main()
{
assert(e.code().value() == EPERM);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -19,8 +20,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
bool try_lock_until_called = false;
struct mutex
@ -38,11 +37,8 @@ struct mutex
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
typedef std::chrono::steady_clock Clock;
std::shared_lock<mutex> lk(m, std::defer_lock);
assert(lk.try_lock_until(Clock::now()) == true);
@ -71,5 +67,4 @@ int main()
{
assert(e.code().value() == EPERM);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,8 +19,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
bool unlock_called = false;
struct mutex
@ -30,11 +29,8 @@ struct mutex
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<mutex> lk(m);
lk.unlock();
assert(unlock_called == true);
@ -58,5 +54,4 @@ int main()
{
assert(e.code().value() == EPERM);
}
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,8 +19,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct mutex
{
void lock_shared() {}
@ -28,11 +27,8 @@ struct mutex
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<mutex> lk1(m);
std::shared_lock<mutex> lk2;
lk1.swap(lk2);
@ -41,5 +37,4 @@ int main()
assert(lk2.mutex() == &m);
assert(lk2.owns_lock() == true);
static_assert(noexcept(lk1.swap(lk2)), "member swap must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -19,8 +20,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct mutex
{
void lock_shared() {}
@ -29,11 +28,8 @@ struct mutex
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<mutex> lk1(m);
std::shared_lock<mutex> lk2;
swap(lk1, lk2);
@ -42,5 +38,4 @@ int main()
assert(lk2.mutex() == &m);
assert(lk2.owns_lock() == true);
static_assert(noexcept(swap(lk1, lk2)), "non-member swap must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,8 +19,6 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
struct mutex
{
static int lock_count;
@ -33,11 +32,8 @@ int mutex::unlock_count = 0;
mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<mutex> lk(m);
assert(lk.mutex() == &m);
assert(lk.owns_lock() == true);
@ -49,5 +45,4 @@ int main()
assert(mutex::lock_count == 1);
assert(mutex::unlock_count == 0);
static_assert(noexcept(lk.release()), "release must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,15 +19,10 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_timed_mutex> lk0;
assert(lk0.mutex() == nullptr);
std::shared_lock<std::shared_timed_mutex> lk1(m);
@ -34,5 +30,4 @@ int main()
lk1.unlock();
assert(lk1.mutex() == &m);
static_assert(noexcept(lk0.mutex()), "mutex() must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,15 +19,10 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_timed_mutex> lk0;
assert(static_cast<bool>(lk0) == false);
std::shared_lock<std::shared_timed_mutex> lk1(m);
@ -34,5 +30,4 @@ int main()
lk1.unlock();
assert(static_cast<bool>(lk1) == false);
static_assert(noexcept(static_cast<bool>(lk0)), "explicit operator bool() must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -18,15 +19,10 @@
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_timed_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_timed_mutex> lk0;
assert(lk0.owns_lock() == false);
std::shared_lock<std::shared_timed_mutex> lk1(m);
@ -34,5 +30,4 @@ int main()
lk1.unlock();
assert(lk1.owns_lock() == false);
static_assert(noexcept(lk0.owns_lock()), "owns_lock must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -8,6 +8,8 @@
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: libcpp-has-no-threads
// UNSUPPORTED: c++98, c++03, c++11
// <shared_mutex>
@ -24,8 +26,6 @@
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert((std::is_same<std::shared_lock<std::mutex>::mutex_type,
std::mutex>::value), "");
#endif // _LIBCPP_STD_VER > 11
}