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.


git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@233946 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier 2015-04-02 21:12:17 +00:00
parent 1d55ecf513
commit 6be02cb83f
10 changed files with 34 additions and 15 deletions

View File

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

View File

@ -48,13 +48,16 @@ int main()
{ {
assert(G::n_alive == 0); assert(G::n_alive == 0);
assert(!G::op_run); 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::id id = t0.get_id();
std::thread t1; std::thread t1;
t1 = std::move(t0); t1 = std::move(t0);
assert(t1.get_id() == id); assert(t1.get_id() == id);
assert(t0.get_id() == std::thread::id()); assert(t0.get_id() == std::thread::id());
t1.join(); t1.join();
}
assert(G::n_alive == 0); assert(G::n_alive == 0);
assert(G::op_run); assert(G::op_run);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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