Refactor flaky shared_mutex tests

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@246055 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier 2015-08-26 19:04:40 +00:00
parent 3dfc10d4b1
commit db8a5fd864
4 changed files with 39 additions and 22 deletions

View File

@ -6,6 +6,8 @@
// Source Licenses. See LICENSE.TXT for details. // Source Licenses. See LICENSE.TXT for details.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++98, c++03, c++11, c++14
// <shared_mutex> // <shared_mutex>
@ -15,15 +17,9 @@
#include <shared_mutex> #include <shared_mutex>
#include "test_macros.h"
int main() int main()
{ {
#if TEST_STD_VER > 14
std::shared_mutex m0; std::shared_mutex m0;
std::shared_mutex m1; std::shared_mutex m1;
m1 = m0; m1 = m0; // expected-error {{overload resolution selected deleted operator '='}}
#else
# error
#endif
} }

View File

@ -6,6 +6,8 @@
// Source Licenses. See LICENSE.TXT for details. // Source Licenses. See LICENSE.TXT for details.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++98, c++03, c++11, c++14
// <shared_mutex> // <shared_mutex>
@ -15,14 +17,8 @@
#include <shared_mutex> #include <shared_mutex>
#include "test_macros.h"
int main() int main()
{ {
#if TEST_STD_VER > 14
std::shared_mutex m0; std::shared_mutex m0;
std::shared_mutex m1(m0); std::shared_mutex m1(m0); // expected-error {{call to deleted constructor of 'std::shared_mutex'}}
#else
# error
#endif
} }

View File

@ -21,6 +21,7 @@
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <cassert>
#include "test_macros.h"
std::shared_mutex m; std::shared_mutex m;
@ -30,21 +31,32 @@ typedef Clock::duration duration;
typedef std::chrono::milliseconds ms; typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns; 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() void f()
{ {
time_point t0 = Clock::now(); time_point t0 = Clock::now();
m.lock(); m.lock();
time_point t1 = Clock::now(); time_point t1 = Clock::now();
m.unlock(); m.unlock();
ns d = t1 - t0 - ms(250); ns d = t1 - t0 - WaitTime;
assert(d < ms(50)); // within 50ms assert(d < Tolerance); // within tolerance
} }
int main() int main()
{ {
m.lock(); m.lock();
std::thread t(f); std::thread t(f);
std::this_thread::sleep_for(ms(250)); std::this_thread::sleep_for(WaitTime);
m.unlock(); m.unlock();
t.join(); t.join();
} }

View File

@ -22,6 +22,8 @@
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <cassert>
#include "test_macros.h"
std::shared_mutex m; std::shared_mutex m;
typedef std::chrono::system_clock Clock; typedef std::chrono::system_clock Clock;
@ -30,14 +32,25 @@ typedef Clock::duration duration;
typedef std::chrono::milliseconds ms; typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns; 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() void f()
{ {
time_point t0 = Clock::now(); time_point t0 = Clock::now();
m.lock_shared(); m.lock_shared();
time_point t1 = Clock::now(); time_point t1 = Clock::now();
m.unlock_shared(); m.unlock_shared();
ns d = t1 - t0 - ms(250); ns d = t1 - t0 - WaitTime;
assert(d < ms(50)); // within 50ms assert(d < Tolerance); // within tolerance
} }
void g() void g()
@ -47,7 +60,7 @@ void g()
time_point t1 = Clock::now(); time_point t1 = Clock::now();
m.unlock_shared(); m.unlock_shared();
ns d = t1 - t0; ns d = t1 - t0;
assert(d < ms(50)); // within 50ms assert(d < Tolerance); // within tolerance
} }
@ -57,7 +70,7 @@ int main()
std::vector<std::thread> v; std::vector<std::thread> v;
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
v.push_back(std::thread(f)); v.push_back(std::thread(f));
std::this_thread::sleep_for(ms(250)); std::this_thread::sleep_for(WaitTime);
m.unlock(); m.unlock();
for (auto& t : v) for (auto& t : v)
t.join(); t.join();
@ -65,7 +78,7 @@ int main()
for (auto& t : v) for (auto& t : v)
t = std::thread(g); t = std::thread(g);
std::thread q(f); std::thread q(f);
std::this_thread::sleep_for(ms(250)); std::this_thread::sleep_for(WaitTime);
m.unlock_shared(); m.unlock_shared();
for (auto& t : v) for (auto& t : v)
t.join(); t.join();