Skip to content

Commit 5646280

Browse files
committed
[libc++] Introduce an indirection to create threads in the test suite
We create threads using std::thread in various places in the test suite. However, the usual std::thread constructor may not work on all platforms, e.g. on platforms where passing a stack size is required to create a thread. This commit introduces a simple indirection that makes it easier to tweak how threads are created inside the test suite on various platforms. Note that tests that are purposefully calling std::thread's constructor directly (e.g. because that is what they're testing) were not modified.
1 parent 8e0148d commit 5646280

103 files changed

Lines changed: 318 additions & 269 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎libcxx/test/libcxx/utilities/memory/util.smartptr/race_condition.pass.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <thread>
2121
#include <cassert>
2222

23+
#include "make_test_thread.h"
2324
#include "test_macros.h"
2425

2526
typedef std::shared_ptr<int> Ptr;
@@ -51,7 +52,7 @@ void run_test(Ptr p) {
5152
assert(p.use_count() == 2);
5253
TestRunner r(p);
5354
assert(p.use_count() == 3);
54-
std::thread t1(r); // Start the test thread.
55+
std::thread t1 = support::make_test_thread(r); // Start the test thread.
5556
assert(p.use_count() == 4);
5657
Start = true;
5758
// Run until we witness 25 use count changes via both

‎libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/atomic_wait.pass.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cassert>
2828
#include <thread>
2929

30+
#include "make_test_thread.h"
3031
#include "test_macros.h"
3132
#include "../atomics.types.operations.req/atomic_helpers.h"
3233

@@ -39,23 +40,23 @@ struct TestFn {
3940
std::atomic_init(&t, T(1));
4041
assert(std::atomic_load(&t) == T(1));
4142
std::atomic_wait(&t, T(0));
42-
std::thread t_([&](){
43+
std::thread t1 = support::make_test_thread([&](){
4344
std::atomic_store(&t, T(3));
4445
std::atomic_notify_one(&t);
4546
});
4647
std::atomic_wait(&t, T(1));
47-
t_.join();
48+
t1.join();
4849

4950
volatile A vt;
5051
std::atomic_init(&vt, T(2));
5152
assert(std::atomic_load(&vt) == T(2));
5253
std::atomic_wait(&vt, T(1));
53-
std::thread t2_([&](){
54+
std::thread t2 = support::make_test_thread([&](){
5455
std::atomic_store(&vt, T(4));
5556
std::atomic_notify_one(&vt);
5657
});
5758
std::atomic_wait(&vt, T(2));
58-
t2_.join();
59+
t2.join();
5960
}
6061
};
6162

‎libcxx/test/std/thread/futures/futures.promise/set_exception_at_thread_exit.pass.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <future>
2020
#include <cassert>
2121

22+
#include "make_test_thread.h"
2223
#include "test_macros.h"
2324

2425
void func(std::promise<int> p)
@@ -32,7 +33,7 @@ int main(int, char**)
3233
typedef int T;
3334
std::promise<T> p;
3435
std::future<T> f = p.get_future();
35-
std::thread(func, std::move(p)).detach();
36+
support::make_test_thread(func, std::move(p)).detach();
3637
try
3738
{
3839
f.get();

‎libcxx/test/std/thread/futures/futures.promise/set_lvalue_at_thread_exit.pass.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <memory>
2020
#include <cassert>
2121

22+
#include "make_test_thread.h"
2223
#include "test_macros.h"
2324

2425
int i = 0;
@@ -34,7 +35,7 @@ int main(int, char**)
3435
{
3536
std::promise<int&> p;
3637
std::future<int&> f = p.get_future();
37-
std::thread(func, std::move(p)).detach();
38+
support::make_test_thread(func, std::move(p)).detach();
3839
assert(f.get() == 4);
3940
}
4041

‎libcxx/test/std/thread/futures/futures.promise/set_rvalue_at_thread_exit.pass.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory>
1919
#include <cassert>
2020

21+
#include "make_test_thread.h"
2122
#include "test_macros.h"
2223

2324
void func(std::promise<std::unique_ptr<int>> p)
@@ -30,7 +31,7 @@ int main(int, char**)
3031
{
3132
std::promise<std::unique_ptr<int>> p;
3233
std::future<std::unique_ptr<int>> f = p.get_future();
33-
std::thread(func, std::move(p)).detach();
34+
support::make_test_thread(func, std::move(p)).detach();
3435
assert(*f.get() == 5);
3536
}
3637

‎libcxx/test/std/thread/futures/futures.promise/set_value_at_thread_exit_const.pass.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <future>
1919
#include <cassert>
2020

21+
#include "make_test_thread.h"
2122
#include "test_macros.h"
2223

2324
void func(std::promise<int> p)
@@ -31,7 +32,7 @@ int main(int, char**)
3132
{
3233
std::promise<int> p;
3334
std::future<int> f = p.get_future();
34-
std::thread(func, std::move(p)).detach();
35+
support::make_test_thread(func, std::move(p)).detach();
3536
assert(f.get() == 5);
3637
}
3738

‎libcxx/test/std/thread/futures/futures.promise/set_value_at_thread_exit_void.pass.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <memory>
2020
#include <cassert>
2121

22+
#include "make_test_thread.h"
2223
#include "test_macros.h"
2324

2425
int i = 0;
@@ -34,7 +35,7 @@ int main(int, char**)
3435
{
3536
std::promise<void> p;
3637
std::future<void> f = p.get_future();
37-
std::thread(func, std::move(p)).detach();
38+
support::make_test_thread(func, std::move(p)).detach();
3839
f.get();
3940
assert(i == 1);
4041
}

‎libcxx/test/std/thread/futures/futures.shared_future/get.pass.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <future>
2121
#include <cassert>
2222

23+
#include "make_test_thread.h"
2324
#include "test_macros.h"
2425

2526
void func1(std::promise<int> p)
@@ -68,7 +69,7 @@ int main(int, char**)
6869
{
6970
std::promise<T> p;
7071
std::shared_future<T> f = p.get_future();
71-
std::thread(func1, std::move(p)).detach();
72+
support::make_test_thread(func1, std::move(p)).detach();
7273
assert(f.valid());
7374
assert(f.get() == 3);
7475
assert(f.valid());
@@ -77,7 +78,7 @@ int main(int, char**)
7778
{
7879
std::promise<T> p;
7980
std::shared_future<T> f = p.get_future();
80-
std::thread(func2, std::move(p)).detach();
81+
support::make_test_thread(func2, std::move(p)).detach();
8182
try
8283
{
8384
assert(f.valid());
@@ -97,7 +98,7 @@ int main(int, char**)
9798
{
9899
std::promise<T> p;
99100
std::shared_future<T> f = p.get_future();
100-
std::thread(func3, std::move(p)).detach();
101+
support::make_test_thread(func3, std::move(p)).detach();
101102
assert(f.valid());
102103
assert(f.get() == 5);
103104
assert(f.valid());
@@ -106,7 +107,7 @@ int main(int, char**)
106107
{
107108
std::promise<T> p;
108109
std::shared_future<T> f = p.get_future();
109-
std::thread(func4, std::move(p)).detach();
110+
support::make_test_thread(func4, std::move(p)).detach();
110111
try
111112
{
112113
assert(f.valid());
@@ -126,7 +127,7 @@ int main(int, char**)
126127
{
127128
std::promise<T> p;
128129
std::shared_future<T> f = p.get_future();
129-
std::thread(func5, std::move(p)).detach();
130+
support::make_test_thread(func5, std::move(p)).detach();
130131
assert(f.valid());
131132
f.get();
132133
assert(f.valid());
@@ -135,7 +136,7 @@ int main(int, char**)
135136
{
136137
std::promise<T> p;
137138
std::shared_future<T> f = p.get_future();
138-
std::thread(func6, std::move(p)).detach();
139+
support::make_test_thread(func6, std::move(p)).detach();
139140
try
140141
{
141142
assert(f.valid());

‎libcxx/test/std/thread/futures/futures.shared_future/wait.pass.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <future>
1919
#include <cassert>
2020

21+
#include "make_test_thread.h"
2122
#include "test_macros.h"
2223

2324
void func1(std::promise<int> p)
@@ -49,7 +50,7 @@ int main(int, char**)
4950
typedef int T;
5051
std::promise<T> p;
5152
std::shared_future<T> f = p.get_future();
52-
std::thread(func1, std::move(p)).detach();
53+
support::make_test_thread(func1, std::move(p)).detach();
5354
assert(f.valid());
5455
f.wait();
5556
assert(f.valid());
@@ -63,7 +64,7 @@ int main(int, char**)
6364
typedef int& T;
6465
std::promise<T> p;
6566
std::shared_future<T> f = p.get_future();
66-
std::thread(func3, std::move(p)).detach();
67+
support::make_test_thread(func3, std::move(p)).detach();
6768
assert(f.valid());
6869
f.wait();
6970
assert(f.valid());
@@ -77,7 +78,7 @@ int main(int, char**)
7778
typedef void T;
7879
std::promise<T> p;
7980
std::shared_future<T> f = p.get_future();
80-
std::thread(func5, std::move(p)).detach();
81+
support::make_test_thread(func5, std::move(p)).detach();
8182
assert(f.valid());
8283
f.wait();
8384
assert(f.valid());

‎libcxx/test/std/thread/futures/futures.shared_future/wait_for.pass.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <future>
2121
#include <cassert>
2222

23+
#include "make_test_thread.h"
2324
#include "test_macros.h"
2425

2526
typedef std::chrono::milliseconds ms;
@@ -56,7 +57,7 @@ int main(int, char**)
5657
typedef int T;
5758
std::promise<T> p;
5859
std::shared_future<T> f = p.get_future();
59-
std::thread(func1, std::move(p)).detach();
60+
support::make_test_thread(func1, std::move(p)).detach();
6061
assert(f.valid());
6162
assert(f.wait_for(ms(1)) == std::future_status::timeout);
6263
assert(f.valid());
@@ -69,7 +70,7 @@ int main(int, char**)
6970
typedef int& T;
7071
std::promise<T> p;
7172
std::shared_future<T> f = p.get_future();
72-
std::thread(func3, std::move(p)).detach();
73+
support::make_test_thread(func3, std::move(p)).detach();
7374
assert(f.valid());
7475
assert(f.wait_for(ms(1)) == std::future_status::timeout);
7576
assert(f.valid());
@@ -82,7 +83,7 @@ int main(int, char**)
8283
typedef void T;
8384
std::promise<T> p;
8485
std::shared_future<T> f = p.get_future();
85-
std::thread(func5, std::move(p)).detach();
86+
support::make_test_thread(func5, std::move(p)).detach();
8687
assert(f.valid());
8788
assert(f.wait_for(ms(1)) == std::future_status::timeout);
8889
assert(f.valid());
@@ -97,7 +98,7 @@ int main(int, char**)
9798
std::promise<T> p;
9899
std::shared_future<T> f = p.get_future();
99100
Clock::time_point t0 = Clock::now();
100-
std::thread(func1, std::move(p)).detach();
101+
support::make_test_thread(func1, std::move(p)).detach();
101102
assert(f.valid());
102103
assert(f.wait_for(ms(1)) == std::future_status::timeout);
103104
assert(f.valid());
@@ -113,7 +114,7 @@ int main(int, char**)
113114
std::promise<T> p;
114115
std::shared_future<T> f = p.get_future();
115116
Clock::time_point t0 = Clock::now();
116-
std::thread(func3, std::move(p)).detach();
117+
support::make_test_thread(func3, std::move(p)).detach();
117118
assert(f.valid());
118119
assert(f.wait_for(ms(1)) == std::future_status::timeout);
119120
assert(f.valid());
@@ -129,7 +130,7 @@ int main(int, char**)
129130
std::promise<T> p;
130131
std::shared_future<T> f = p.get_future();
131132
Clock::time_point t0 = Clock::now();
132-
std::thread(func5, std::move(p)).detach();
133+
support::make_test_thread(func5, std::move(p)).detach();
133134
assert(f.valid());
134135
assert(f.wait_for(ms(1)) == std::future_status::timeout);
135136
assert(f.valid());

0 commit comments

Comments
 (0)