Thread safety in assertions is no longer experimental

This commit is contained in:
Martin Hořeňovský
2025-12-28 21:02:50 +01:00
parent eb3811c555
commit 970ec144f5
8 changed files with 21 additions and 17 deletions
+2 -2
View File
@@ -76,8 +76,8 @@ expand_template(
"#cmakedefine CATCH_CONFIG_WINDOWS_SEH": "", "#cmakedefine CATCH_CONFIG_WINDOWS_SEH": "",
"#cmakedefine CATCH_CONFIG_USE_BUILTIN_CONSTANT_P": "", "#cmakedefine CATCH_CONFIG_USE_BUILTIN_CONSTANT_P": "",
"#cmakedefine CATCH_CONFIG_NO_USE_BUILTIN_CONSTANT_P": "", "#cmakedefine CATCH_CONFIG_NO_USE_BUILTIN_CONSTANT_P": "",
"#cmakedefine CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS": "", "#cmakedefine CATCH_CONFIG_THREAD_SAFE_ASSERTIONS": "",
"#cmakedefine CATCH_CONFIG_NO_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS": "", "#cmakedefine CATCH_CONFIG_NO_THREAD_SAFE_ASSERTIONS": "",
}, },
template = "src/catch2/catch_user_config.hpp.in", template = "src/catch2/catch_user_config.hpp.in",
) )
+1 -1
View File
@@ -45,7 +45,7 @@ set(_OverridableOptions
"EXPERIMENTAL_STATIC_ANALYSIS_SUPPORT" "EXPERIMENTAL_STATIC_ANALYSIS_SUPPORT"
"USE_BUILTIN_CONSTANT_P" "USE_BUILTIN_CONSTANT_P"
"DEPRECATION_ANNOTATIONS" "DEPRECATION_ANNOTATIONS"
"EXPERIMENTAL_THREAD_SAFE_ASSERTIONS" "THREAD_SAFE_ASSERTIONS"
) )
foreach(OptionName ${_OverridableOptions}) foreach(OptionName ${_OverridableOptions})
+8 -4
View File
@@ -17,7 +17,7 @@
[Disabling deprecation warnings](#disabling-deprecation-warnings)<br> [Disabling deprecation warnings](#disabling-deprecation-warnings)<br>
[Overriding Catch's debug break (`-b`)](#overriding-catchs-debug-break--b)<br> [Overriding Catch's debug break (`-b`)](#overriding-catchs-debug-break--b)<br>
[Static analysis support](#static-analysis-support)<br> [Static analysis support](#static-analysis-support)<br>
[Experimental thread safety](#experimental-thread-safety)<br> [Thread safety in assertions (and messages)](#thread-safety-in-assertions-and-messages)<br>
Catch2 is designed to "just work" as much as possible, and most of the Catch2 is designed to "just work" as much as possible, and most of the
configuration options below are changed automatically during compilation, configuration options below are changed automatically during compilation,
@@ -316,17 +316,21 @@ no backwards compatibility guarantees._
are not meant to be runnable, only "scannable". are not meant to be runnable, only "scannable".
## Experimental thread safety
<a id="experimental-thread-safety"></a>
## Thread safety in assertions (and messages)
> Introduced in Catch2 3.9.0 > Introduced in Catch2 3.9.0
> Made non-experimental in Catch2 vX.Y.Z
Catch2 can optionally support thread-safe assertions, that means, multiple Catch2 can optionally support thread-safe assertions, that means, multiple
user-spawned threads can use the assertion macros at the same time. Due user-spawned threads can use the assertion macros at the same time. Due
to the performance cost this imposes even on single-threaded usage, Catch2 to the performance cost this imposes even on single-threaded usage, Catch2
defaults to non-thread-safe assertions. defaults to non-thread-safe assertions.
CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS // enables thread safe assertions CATCH_CONFIG_THREAD_SAFE_ASSERTIONS // enables thread safe assertions
CATCH_CONFIG_NO_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS // force-disables thread safe assertions CATCH_CONFIG_NO_THREAD_SAFE_ASSERTIONS // force-disables thread safe assertions
See [the documentation on thread safety in Catch2](thread-safety.md#top) See [the documentation on thread safety in Catch2](thread-safety.md#top)
for details on which macros are safe and other notes. for details on which macros are safe and other notes.
+1 -1
View File
@@ -20,7 +20,7 @@ test case macros is not thread-safe. The way sections define paths through
the test is incompatible with user spawning threads arbitrarily, so this the test is incompatible with user spawning threads arbitrarily, so this
limitation is here to stay. limitation is here to stay.
**Important: thread safety in Catch2 is [opt-in](configuration.md#experimental-thread-safety)** **Important: thread safety in Catch2 is [opt-in](configuration.md#thread-safety)**
## Using assertion macros from spawned threads ## Using assertion macros from spawned threads
+5 -5
View File
@@ -196,12 +196,12 @@
#endif #endif
#cmakedefine CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS #cmakedefine CATCH_CONFIG_THREAD_SAFE_ASSERTIONS
#cmakedefine CATCH_CONFIG_NO_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS #cmakedefine CATCH_CONFIG_NO_THREAD_SAFE_ASSERTIONS
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS ) && \ #if defined( CATCH_CONFIG_THREAD_SAFE_ASSERTIONS ) && \
defined( CATCH_CONFIG_NO_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS ) defined( CATCH_CONFIG_NO_THREAD_SAFE_ASSERTIONS )
# error Cannot force EXPERIMENTAL_THREAD_SAFE_ASSERTIONS to both ON and OFF # error Cannot force THREAD_SAFE_ASSERTIONS to both ON and OFF
#endif #endif
+1 -1
View File
@@ -10,7 +10,7 @@
#include <catch2/catch_user_config.hpp> #include <catch2/catch_user_config.hpp>
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS ) #if defined( CATCH_CONFIG_THREAD_SAFE_ASSERTIONS )
#define CATCH_INTERNAL_THREAD_LOCAL thread_local #define CATCH_INTERNAL_THREAD_LOCAL thread_local
#else #else
#define CATCH_INTERNAL_THREAD_LOCAL #define CATCH_INTERNAL_THREAD_LOCAL
+2 -2
View File
@@ -10,7 +10,7 @@
#include <catch2/catch_user_config.hpp> #include <catch2/catch_user_config.hpp>
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS ) #if defined( CATCH_CONFIG_THREAD_SAFE_ASSERTIONS )
# include <atomic> # include <atomic>
# include <mutex> # include <mutex>
#endif #endif
@@ -19,7 +19,7 @@
namespace Catch { namespace Catch {
namespace Detail { namespace Detail {
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS ) #if defined( CATCH_CONFIG_THREAD_SAFE_ASSERTIONS )
using Mutex = std::mutex; using Mutex = std::mutex;
using LockGuard = std::lock_guard<std::mutex>; using LockGuard = std::lock_guard<std::mutex>;
struct AtomicCounts { struct AtomicCounts {
+1 -1
View File
@@ -571,7 +571,7 @@ add_executable(ThreadSafetyTests
${TESTS_DIR}/X94-ThreadSafetyTests.cpp ${TESTS_DIR}/X94-ThreadSafetyTests.cpp
) )
target_link_libraries(ThreadSafetyTests Catch2_buildall_interface) target_link_libraries(ThreadSafetyTests Catch2_buildall_interface)
target_compile_definitions(ThreadSafetyTests PUBLIC CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS) target_compile_definitions(ThreadSafetyTests PUBLIC CATCH_CONFIG_THREAD_SAFE_ASSERTIONS)
add_test(NAME ThreadSafetyTests::ScopedMessagesAndAssertions add_test(NAME ThreadSafetyTests::ScopedMessagesAndAssertions
COMMAND ThreadSafetyTests -r compact "Failed REQUIRE in the main thread is fine" COMMAND ThreadSafetyTests -r compact "Failed REQUIRE in the main thread is fine"