Skip to content

Commit d427034

Browse files
committed
Only use thread_local in builds with thread safety enabled
1 parent 1e46360 commit d427034

7 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ set(IMPL_HEADERS
140140
${SOURCES_DIR}/internal/catch_test_registry.hpp
141141
${SOURCES_DIR}/internal/catch_test_spec_parser.hpp
142142
${SOURCES_DIR}/internal/catch_textflow.hpp
143+
${SOURCES_DIR}/internal/catch_thread_local.hpp
143144
${SOURCES_DIR}/internal/catch_thread_support.hpp
144145
${SOURCES_DIR}/internal/catch_to_string.hpp
145146
${SOURCES_DIR}/internal/catch_uncaught_exceptions.hpp

src/catch2/catch_all.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#include <catch2/internal/catch_test_registry.hpp>
123123
#include <catch2/internal/catch_test_spec_parser.hpp>
124124
#include <catch2/internal/catch_textflow.hpp>
125+
#include <catch2/internal/catch_thread_local.hpp>
125126
#include <catch2/internal/catch_thread_support.hpp>
126127
#include <catch2/internal/catch_to_string.hpp>
127128
#include <catch2/internal/catch_uncaught_exceptions.hpp>

src/catch2/internal/catch_message_info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ namespace Catch {
2121

2222
// Messages are owned by their individual threads, so the counter should be thread-local as well.
2323
// Alternative consideration: atomic, so threads don't share IDs and things are easier to debug.
24-
thread_local unsigned int MessageInfo::globalCount = 0;
24+
CATCH_INTERNAL_THREAD_LOCAL unsigned int MessageInfo::globalCount = 0;
2525

2626
} // end namespace Catch

src/catch2/internal/catch_message_info.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <catch2/internal/catch_result_type.hpp>
1313
#include <catch2/internal/catch_source_line_info.hpp>
1414
#include <catch2/internal/catch_stringref.hpp>
15+
#include <catch2/internal/catch_thread_local.hpp>
1516

1617
#include <string>
1718

@@ -38,7 +39,7 @@ namespace Catch {
3839
return sequence < other.sequence;
3940
}
4041
private:
41-
static thread_local unsigned int globalCount;
42+
static CATCH_INTERNAL_THREAD_LOCAL unsigned int globalCount;
4243
};
4344

4445
} // end namespace Catch

src/catch2/internal/catch_run_context.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <catch2/internal/catch_output_redirect.hpp>
2121
#include <catch2/internal/catch_assertion_handler.hpp>
2222
#include <catch2/internal/catch_test_failure_exception.hpp>
23+
#include <catch2/internal/catch_thread_local.hpp>
2324
#include <catch2/internal/catch_result_type.hpp>
2425

2526
#include <cassert>
@@ -175,25 +176,25 @@ namespace Catch {
175176
// from heap, to avoid consuming too much thread-local storage.
176177

177178
// This is used for the "if" part of CHECKED_IF/CHECKED_ELSE
178-
static thread_local bool g_lastAssertionPassed = false;
179+
static CATCH_INTERNAL_THREAD_LOCAL bool g_lastAssertionPassed = false;
179180

180181
// This is the source location for last encountered macro. It is
181182
// used to provide the users with more precise location of error
182183
// when an unexpected exception/fatal error happens.
183-
static thread_local SourceLineInfo g_lastKnownLineInfo("DummyLocation", static_cast<size_t>(-1));
184+
static CATCH_INTERNAL_THREAD_LOCAL SourceLineInfo g_lastKnownLineInfo("DummyLocation", static_cast<size_t>(-1));
184185

185186
// Should we clear message scopes before sending off the messages to
186187
// reporter? Set in `assertionPassedFastPath` to avoid doing the full
187188
// clear there for performance reasons.
188-
static thread_local bool g_clearMessageScopes = false;
189+
static CATCH_INTERNAL_THREAD_LOCAL bool g_clearMessageScopes = false;
189190

190191
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
191192
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
192193
// Actual messages to be provided to the reporter
193-
static thread_local std::vector<MessageInfo> g_messages;
194+
static CATCH_INTERNAL_THREAD_LOCAL std::vector<MessageInfo> g_messages;
194195

195196
// Owners for the UNSCOPED_X information macro
196-
static thread_local std::vector<ScopedMessage> g_messageScopes;
197+
static CATCH_INTERNAL_THREAD_LOCAL std::vector<ScopedMessage> g_messageScopes;
197198
CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION
198199

199200
} // namespace Detail
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
// Copyright Catch2 Authors
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE.txt or copy at
5+
// https://www.boost.org/LICENSE_1_0.txt)
6+
7+
// SPDX-License-Identifier: BSL-1.0
8+
#ifndef CATCH_THREAD_LOCAL_HPP_INCLUDED
9+
#define CATCH_THREAD_LOCAL_HPP_INCLUDED
10+
11+
#include <catch2/catch_user_config.hpp>
12+
13+
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS )
14+
#define CATCH_INTERNAL_THREAD_LOCAL thread_local
15+
#else
16+
#define CATCH_INTERNAL_THREAD_LOCAL
17+
#endif
18+
19+
#endif // CATCH_THREAD_LOCAL_HPP_INCLUDED

src/catch2/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ internal_headers = [
148148
'internal/catch_test_registry.hpp',
149149
'internal/catch_test_spec_parser.hpp',
150150
'internal/catch_textflow.hpp',
151+
'internal/catch_thread_local.hpp',
151152
'internal/catch_thread_support.hpp',
152153
'internal/catch_to_string.hpp',
153154
'internal/catch_uncaught_exceptions.hpp',

0 commit comments

Comments
 (0)