Skip to content

Commit d419781

Browse files
author
MarcoFalke
committed
Merge #14985: test: Remove thread_local from test_bitcoin
fa61202 test: Add comment to g_insecure_rand_ctx (MarcoFalke) fa0d3c4 test: Undo thread_local g_insecure_rand_ctx (MarcoFalke) Pull request description: `thread_local` seems to be highly controversial according to the discussion in #14953, so remove it again from the tests. Also remove boost::thread_group in the test that uses it, since I am touching it anyway. Tree-SHA512: 977c1f597e3cfbd0e97d0b037d998fdbc701f62e9a2f57e02dbe1727b63ae8ff478dbd9d3d6dc4ffdfa23f2058b331f04949d51f23a8f55b41ecb75f088f1cbe
2 parents f055389 + fa61202 commit d419781

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/test/test_bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
2424

25+
FastRandomContext g_insecure_rand_ctx;
26+
2527
void CConnmanTest::AddNode(CNode& node)
2628
{
2729
LOCK(g_connman->cs_vNodes);
@@ -37,8 +39,6 @@ void CConnmanTest::ClearNodes()
3739
g_connman->vNodes.clear();
3840
}
3941

40-
thread_local FastRandomContext g_insecure_rand_ctx;
41-
4242
std::ostream& operator<<(std::ostream& os, const uint256& num)
4343
{
4444
os << num.ToString();

src/test/test_bitcoin.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
2626
return stream << static_cast<typename std::underlying_type<T>::type>(e);
2727
}
2828

29-
thread_local extern FastRandomContext g_insecure_rand_ctx;
29+
/**
30+
* This global and the helpers that use it are not thread-safe.
31+
*
32+
* If thread-safety is needed, the global could be made thread_local (given
33+
* that thread_local is supported on all architectures we support) or a
34+
* per-thread instance could be used in the multi-threaded test.
35+
*/
36+
extern FastRandomContext g_insecure_rand_ctx;
3037

3138
static inline void SeedInsecureRand(bool deterministic = false)
3239
{

src/test/validation_block_tests.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,13 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
152152
// create a bunch of threads that repeatedly process a block generated above at random
153153
// this will create parallelism and randomness inside validation - the ValidationInterface
154154
// will subscribe to events generated during block validation and assert on ordering invariance
155-
boost::thread_group threads;
155+
std::vector<std::thread> threads;
156156
for (int i = 0; i < 10; i++) {
157-
threads.create_thread([&blocks]() {
157+
threads.emplace_back([&blocks]() {
158158
bool ignored;
159+
FastRandomContext insecure;
159160
for (int i = 0; i < 1000; i++) {
160-
auto block = blocks[InsecureRandRange(blocks.size() - 1)];
161+
auto block = blocks[insecure.randrange(blocks.size() - 1)];
161162
ProcessNewBlock(Params(), block, true, &ignored);
162163
}
163164

@@ -171,7 +172,9 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
171172
});
172173
}
173174

174-
threads.join_all();
175+
for (auto& t : threads) {
176+
t.join();
177+
}
175178
while (GetMainSignals().CallbacksPending() > 0) {
176179
MilliSleep(100);
177180
}

0 commit comments

Comments
 (0)