Fix race conditions in test class used throughout the std::thread tests.

The test class 'G' reads and writes to the same static variables in its
constructor, destructor and call operator. When threads are
constructed using `std::thread t((G()))` there is a race condition between the
destruction of the temporary and the execution of `G::operator()()`.

The fix is to simply create the input before creating the thread.

llvm-svn: 233946
This commit is contained in:
Eric Fiselier 2015-04-02 21:12:17 +00:00
parent 4453d2185c
commit 9a37bc91d2
10 changed files with 34 additions and 15 deletions

View File

@ -45,7 +45,8 @@ bool G::op_run = false;
int main()
{
{
std::thread t0((G()));
G g;
std::thread t0(g);
std::thread::id id0 = t0.get_id();
std::thread t1;
std::thread::id id1 = t1.get_id();

View File

@ -48,13 +48,16 @@ int main()
{
assert(G::n_alive == 0);
assert(!G::op_run);
std::thread t0(G(), 5, 5.5);
{
G g;
std::thread t0(g, 5, 5.5);
std::thread::id id = t0.get_id();
std::thread t1;
t1 = std::move(t0);
assert(t1.get_id() == id);
assert(t0.get_id() == std::thread::id());
t1.join();
}
assert(G::n_alive == 0);
assert(G::op_run);
}

View File

@ -13,6 +13,9 @@
// memory is not freed. This will cause ASAN to fail.
// XFAIL: asan
// NOTE: TSAN will report this test as leaking a thread.
// XFAIL: tsan
// <thread>
// class thread
@ -62,14 +65,13 @@ void f1()
int main()
{
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
std::set_terminate(f1);
{
std::thread t0(G(), 5, 5.5);
G g;
std::thread t0(g, 5, 5.5);
std::thread::id id = t0.get_id();
std::thread t1;
t0 = std::move(t1);
assert(false);
}
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
}

View File

@ -55,16 +55,18 @@ int main()
{
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
{
assert(G::n_alive == 0);
G g;
assert(G::n_alive == 1);
assert(!G::op_run);
std::thread t0(G(), 5, 5.5);
std::thread t0(g, 5, 5.5);
std::thread::id id = t0.get_id();
std::thread t1 = std::move(t0);
assert(t1.get_id() == id);
assert(t0.get_id() == std::thread::id());
t1.join();
assert(G::n_alive == 0);
assert(G::n_alive == 1);
assert(G::op_run);
}
assert(G::n_alive == 0);
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
}

View File

@ -9,6 +9,9 @@
//
// UNSUPPORTED: libcpp-has-no-threads
// NOTE: TSAN will report this test as leaking a thread.
// XFAIL: tsan
// <thread>
// class thread
@ -53,8 +56,11 @@ int main()
{
assert(G::n_alive == 0);
assert(!G::op_run);
std::thread t((G()));
std::this_thread::sleep_for(std::chrono::milliseconds(250));
G g;
{
std::thread t(g);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
assert(false);
}

View File

@ -45,7 +45,8 @@ bool G::op_run = false;
int main()
{
{
std::thread t0((G()));
G g;
std::thread t0(g);
std::thread::id id0 = t0.get_id();
std::thread t1;
std::thread::id id1 = t1.get_id();

View File

@ -45,7 +45,8 @@ bool G::op_run = false;
int main()
{
{
std::thread t0((G()));
G g;
std::thread t0(g);
assert(t0.joinable());
t0.join();
assert(!t0.joinable());

View File

@ -45,7 +45,8 @@ bool G::op_run = false;
int main()
{
{
std::thread t0((G()));
G g;
std::thread t0(g);
assert(t0.joinable());
t0.join();
assert(!t0.joinable());

View File

@ -45,7 +45,8 @@ bool G::op_run = false;
int main()
{
{
std::thread t0((G()));
G g;
std::thread t0(g);
pthread_t pid = t0.native_handle();
assert(pid != 0);
t0.join();

View File

@ -45,7 +45,8 @@ bool G::op_run = false;
int main()
{
{
std::thread t0((G()));
G g;
std::thread t0(g);
std::thread::id id0 = t0.get_id();
std::thread t1;
std::thread::id id1 = t1.get_id();