mirror of
https://github.com/catchorg/Catch2.git
synced 2025-12-16 07:02:12 +01:00
Only use thread_local in builds with thread safety enabled
This commit is contained in:
@@ -140,6 +140,7 @@ set(IMPL_HEADERS
|
|||||||
${SOURCES_DIR}/internal/catch_test_registry.hpp
|
${SOURCES_DIR}/internal/catch_test_registry.hpp
|
||||||
${SOURCES_DIR}/internal/catch_test_spec_parser.hpp
|
${SOURCES_DIR}/internal/catch_test_spec_parser.hpp
|
||||||
${SOURCES_DIR}/internal/catch_textflow.hpp
|
${SOURCES_DIR}/internal/catch_textflow.hpp
|
||||||
|
${SOURCES_DIR}/internal/catch_thread_local.hpp
|
||||||
${SOURCES_DIR}/internal/catch_thread_support.hpp
|
${SOURCES_DIR}/internal/catch_thread_support.hpp
|
||||||
${SOURCES_DIR}/internal/catch_to_string.hpp
|
${SOURCES_DIR}/internal/catch_to_string.hpp
|
||||||
${SOURCES_DIR}/internal/catch_uncaught_exceptions.hpp
|
${SOURCES_DIR}/internal/catch_uncaught_exceptions.hpp
|
||||||
|
|||||||
@@ -122,6 +122,7 @@
|
|||||||
#include <catch2/internal/catch_test_registry.hpp>
|
#include <catch2/internal/catch_test_registry.hpp>
|
||||||
#include <catch2/internal/catch_test_spec_parser.hpp>
|
#include <catch2/internal/catch_test_spec_parser.hpp>
|
||||||
#include <catch2/internal/catch_textflow.hpp>
|
#include <catch2/internal/catch_textflow.hpp>
|
||||||
|
#include <catch2/internal/catch_thread_local.hpp>
|
||||||
#include <catch2/internal/catch_thread_support.hpp>
|
#include <catch2/internal/catch_thread_support.hpp>
|
||||||
#include <catch2/internal/catch_to_string.hpp>
|
#include <catch2/internal/catch_to_string.hpp>
|
||||||
#include <catch2/internal/catch_uncaught_exceptions.hpp>
|
#include <catch2/internal/catch_uncaught_exceptions.hpp>
|
||||||
|
|||||||
@@ -21,6 +21,6 @@ namespace Catch {
|
|||||||
|
|
||||||
// Messages are owned by their individual threads, so the counter should be thread-local as well.
|
// Messages are owned by their individual threads, so the counter should be thread-local as well.
|
||||||
// Alternative consideration: atomic, so threads don't share IDs and things are easier to debug.
|
// Alternative consideration: atomic, so threads don't share IDs and things are easier to debug.
|
||||||
thread_local unsigned int MessageInfo::globalCount = 0;
|
CATCH_INTERNAL_THREAD_LOCAL unsigned int MessageInfo::globalCount = 0;
|
||||||
|
|
||||||
} // end namespace Catch
|
} // end namespace Catch
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
#include <catch2/internal/catch_result_type.hpp>
|
#include <catch2/internal/catch_result_type.hpp>
|
||||||
#include <catch2/internal/catch_source_line_info.hpp>
|
#include <catch2/internal/catch_source_line_info.hpp>
|
||||||
#include <catch2/internal/catch_stringref.hpp>
|
#include <catch2/internal/catch_stringref.hpp>
|
||||||
|
#include <catch2/internal/catch_thread_local.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ namespace Catch {
|
|||||||
return sequence < other.sequence;
|
return sequence < other.sequence;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
static thread_local unsigned int globalCount;
|
static CATCH_INTERNAL_THREAD_LOCAL unsigned int globalCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end namespace Catch
|
} // end namespace Catch
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <catch2/internal/catch_output_redirect.hpp>
|
#include <catch2/internal/catch_output_redirect.hpp>
|
||||||
#include <catch2/internal/catch_assertion_handler.hpp>
|
#include <catch2/internal/catch_assertion_handler.hpp>
|
||||||
#include <catch2/internal/catch_test_failure_exception.hpp>
|
#include <catch2/internal/catch_test_failure_exception.hpp>
|
||||||
|
#include <catch2/internal/catch_thread_local.hpp>
|
||||||
#include <catch2/internal/catch_result_type.hpp>
|
#include <catch2/internal/catch_result_type.hpp>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@@ -175,25 +176,25 @@ namespace Catch {
|
|||||||
// from heap, to avoid consuming too much thread-local storage.
|
// from heap, to avoid consuming too much thread-local storage.
|
||||||
|
|
||||||
// This is used for the "if" part of CHECKED_IF/CHECKED_ELSE
|
// This is used for the "if" part of CHECKED_IF/CHECKED_ELSE
|
||||||
static thread_local bool g_lastAssertionPassed = false;
|
static CATCH_INTERNAL_THREAD_LOCAL bool g_lastAssertionPassed = false;
|
||||||
|
|
||||||
// This is the source location for last encountered macro. It is
|
// This is the source location for last encountered macro. It is
|
||||||
// used to provide the users with more precise location of error
|
// used to provide the users with more precise location of error
|
||||||
// when an unexpected exception/fatal error happens.
|
// when an unexpected exception/fatal error happens.
|
||||||
static thread_local SourceLineInfo g_lastKnownLineInfo("DummyLocation", static_cast<size_t>(-1));
|
static CATCH_INTERNAL_THREAD_LOCAL SourceLineInfo g_lastKnownLineInfo("DummyLocation", static_cast<size_t>(-1));
|
||||||
|
|
||||||
// Should we clear message scopes before sending off the messages to
|
// Should we clear message scopes before sending off the messages to
|
||||||
// reporter? Set in `assertionPassedFastPath` to avoid doing the full
|
// reporter? Set in `assertionPassedFastPath` to avoid doing the full
|
||||||
// clear there for performance reasons.
|
// clear there for performance reasons.
|
||||||
static thread_local bool g_clearMessageScopes = false;
|
static CATCH_INTERNAL_THREAD_LOCAL bool g_clearMessageScopes = false;
|
||||||
|
|
||||||
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
|
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
|
||||||
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
|
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
|
||||||
// Actual messages to be provided to the reporter
|
// Actual messages to be provided to the reporter
|
||||||
static thread_local std::vector<MessageInfo> g_messages;
|
static CATCH_INTERNAL_THREAD_LOCAL std::vector<MessageInfo> g_messages;
|
||||||
|
|
||||||
// Owners for the UNSCOPED_X information macro
|
// Owners for the UNSCOPED_X information macro
|
||||||
static thread_local std::vector<ScopedMessage> g_messageScopes;
|
static CATCH_INTERNAL_THREAD_LOCAL std::vector<ScopedMessage> g_messageScopes;
|
||||||
CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION
|
CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION
|
||||||
|
|
||||||
} // namespace Detail
|
} // namespace Detail
|
||||||
|
|||||||
19
src/catch2/internal/catch_thread_local.hpp
Normal file
19
src/catch2/internal/catch_thread_local.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
// Copyright Catch2 Authors
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// (See accompanying file LICENSE.txt or copy at
|
||||||
|
// https://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
|
#ifndef CATCH_THREAD_LOCAL_HPP_INCLUDED
|
||||||
|
#define CATCH_THREAD_LOCAL_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <catch2/catch_user_config.hpp>
|
||||||
|
|
||||||
|
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS )
|
||||||
|
#define CATCH_INTERNAL_THREAD_LOCAL thread_local
|
||||||
|
#else
|
||||||
|
#define CATCH_INTERNAL_THREAD_LOCAL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // CATCH_THREAD_LOCAL_HPP_INCLUDED
|
||||||
@@ -148,6 +148,7 @@ internal_headers = [
|
|||||||
'internal/catch_test_registry.hpp',
|
'internal/catch_test_registry.hpp',
|
||||||
'internal/catch_test_spec_parser.hpp',
|
'internal/catch_test_spec_parser.hpp',
|
||||||
'internal/catch_textflow.hpp',
|
'internal/catch_textflow.hpp',
|
||||||
|
'internal/catch_thread_local.hpp',
|
||||||
'internal/catch_thread_support.hpp',
|
'internal/catch_thread_support.hpp',
|
||||||
'internal/catch_to_string.hpp',
|
'internal/catch_to_string.hpp',
|
||||||
'internal/catch_uncaught_exceptions.hpp',
|
'internal/catch_uncaught_exceptions.hpp',
|
||||||
|
|||||||
Reference in New Issue
Block a user