From 3d6829698f13b3ebd39960eb801937d9e7e69076 Mon Sep 17 00:00:00 2001 From: Ethan Slattery Date: Wed, 17 Apr 2024 21:07:42 -0700 Subject: [PATCH 1/2] feature switch for thread local variables and multi-threading thread local variables will trigger a memory allocation on a bare metal system. use a macro to define the variable as thread local. Bare metal by definition has no threads and so needs no qualifier. --- CMakeLists.txt | 1 + include/snitch/snitch_config.hpp.config | 9 +++++++++ meson_options.txt | 1 + src/snitch_test_data.cpp | 2 +- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fbbef831..9c7789d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(SNITCH_MAX_PATH_LENGTH 1024 CACHE STRING "Maximum length of a file # Feature toggles. set(SNITCH_DEFINE_MAIN ON CACHE BOOL "Define main() in snitch -- disable to provide your own main() function.") set(SNITCH_WITH_EXCEPTIONS ON CACHE BOOL "Use exceptions in snitch implementation -- will be forced OFF if exceptions are not available.") +set(SNITCH_WITH_MULTITHREADING ON CACHE BOOL "Multi-threading support and thread local variables -- disable if needed.") set(SNITCH_WITH_TIMINGS ON CACHE BOOL "Measure the time taken by each test case -- disable to speed up tests.") set(SNITCH_WITH_SHORTHAND_MACROS ON CACHE BOOL "Use short names for test macros -- disable if this causes conflicts.") set(SNITCH_CONSTEXPR_FLOAT_USE_BITCAST ON CACHE BOOL "Use std::bit_cast if available to implement exact constexpr float-to-string conversion.") diff --git a/include/snitch/snitch_config.hpp.config b/include/snitch/snitch_config.hpp.config index f4fe4c9b..7e78888b 100644 --- a/include/snitch/snitch_config.hpp.config +++ b/include/snitch/snitch_config.hpp.config @@ -77,6 +77,9 @@ #if !defined(SNITCH_WITH_CATCH2_REPORTER) #cmakedefine01 SNITCH_WITH_CATCH2_REPORTER #endif +#if !defined(SNITCH_WITH_MULTITHREADING) +#cmakedefine01 SNITCH_WITH_MULTITHREADING +#endif #if !defined(SNITCH_SHARED_LIBRARY) #cmakedefine01 SNITCH_SHARED_LIBRARY #endif @@ -97,6 +100,12 @@ # define SNITCH_WITH_EXCEPTIONS 0 #endif +#if defined(SNITCH_WITH_MULTITHREADING) +# define SNITCH_THREAD_LOCAL thread_local +#else +# define SNITCH_THREAD_LOCAL +#endif + #if !defined(__cpp_lib_bit_cast) # undef SNITCH_CONSTEXPR_FLOAT_USE_BITCAST # define SNITCH_CONSTEXPR_FLOAT_USE_BITCAST 0 diff --git a/meson_options.txt b/meson_options.txt index f763c89d..0592828f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -15,6 +15,7 @@ option('max_path_length' ,type: 'integer' ,value: 1024, description: 'M # Feature toggles. option('define_main' ,type: 'boolean' ,value: true, description: 'Define main() in snitch -- disable to provide your own main() function.') option('with_exceptions' ,type: 'boolean' ,value: true, description: 'Use exceptions in snitch implementation -- will be forced OFF if exceptions are not available.') +option('with_multithreading' ,type: 'boolean', value: true, description: 'Multi-threading support and thread local variables -- disable if needed.') option('with_timings' ,type: 'boolean' ,value: true, description: 'Measure the time taken by each test case -- disable to speed up tests.') option('with_shorthand_macros' ,type: 'boolean' ,value: true, description: 'Use short names for test macros -- disable if this causes conflicts.') option('constexpr_float_use_bitcast' ,type: 'boolean' ,value: true, description: 'Use std::bit_cast if available to implement exact constexpr float-to-string conversion.') diff --git a/src/snitch_test_data.cpp b/src/snitch_test_data.cpp index 731e0802..87d7b3b5 100644 --- a/src/snitch_test_data.cpp +++ b/src/snitch_test_data.cpp @@ -6,7 +6,7 @@ namespace snitch::impl { namespace { -thread_local test_state* thread_current_test = nullptr; +SNITCH_THREAD_LOCAL test_state* thread_current_test = nullptr; } test_state& get_current_test() noexcept { From 73f4935bfcaa96c9d8a303ffaddb469183ea0bc6 Mon Sep 17 00:00:00 2001 From: Ethan Slattery Date: Sat, 27 Apr 2024 12:58:01 -0700 Subject: [PATCH 2/2] cleanup for multi-threading option in CMake and Meson Cleanup of option documentation, usage, and meson configuration. Based on code review in merge request from cschreib. --- CMakeLists.txt | 2 +- include/snitch/snitch_config.hpp.config | 2 +- meson_options.txt | 2 +- snitch/meson.build | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c7789d7..ba301444 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ set(SNITCH_MAX_PATH_LENGTH 1024 CACHE STRING "Maximum length of a file # Feature toggles. set(SNITCH_DEFINE_MAIN ON CACHE BOOL "Define main() in snitch -- disable to provide your own main() function.") set(SNITCH_WITH_EXCEPTIONS ON CACHE BOOL "Use exceptions in snitch implementation -- will be forced OFF if exceptions are not available.") -set(SNITCH_WITH_MULTITHREADING ON CACHE BOOL "Multi-threading support and thread local variables -- disable if needed.") +set(SNITCH_WITH_MULTITHREADING ON CACHE BOOL "Make the testing framework thread-safe -- disable if multithreading is not needed.") set(SNITCH_WITH_TIMINGS ON CACHE BOOL "Measure the time taken by each test case -- disable to speed up tests.") set(SNITCH_WITH_SHORTHAND_MACROS ON CACHE BOOL "Use short names for test macros -- disable if this causes conflicts.") set(SNITCH_CONSTEXPR_FLOAT_USE_BITCAST ON CACHE BOOL "Use std::bit_cast if available to implement exact constexpr float-to-string conversion.") diff --git a/include/snitch/snitch_config.hpp.config b/include/snitch/snitch_config.hpp.config index 7e78888b..2573f126 100644 --- a/include/snitch/snitch_config.hpp.config +++ b/include/snitch/snitch_config.hpp.config @@ -100,7 +100,7 @@ # define SNITCH_WITH_EXCEPTIONS 0 #endif -#if defined(SNITCH_WITH_MULTITHREADING) +#if SNITCH_WITH_MULTITHREADING # define SNITCH_THREAD_LOCAL thread_local #else # define SNITCH_THREAD_LOCAL diff --git a/meson_options.txt b/meson_options.txt index 0592828f..cf889630 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -15,7 +15,7 @@ option('max_path_length' ,type: 'integer' ,value: 1024, description: 'M # Feature toggles. option('define_main' ,type: 'boolean' ,value: true, description: 'Define main() in snitch -- disable to provide your own main() function.') option('with_exceptions' ,type: 'boolean' ,value: true, description: 'Use exceptions in snitch implementation -- will be forced OFF if exceptions are not available.') -option('with_multithreading' ,type: 'boolean', value: true, description: 'Multi-threading support and thread local variables -- disable if needed.') +option('with_multithreading' ,type: 'boolean', value: true, description: 'Make the testing framework thread-safe -- disable if multithreading is not needed.') option('with_timings' ,type: 'boolean' ,value: true, description: 'Measure the time taken by each test case -- disable to speed up tests.') option('with_shorthand_macros' ,type: 'boolean' ,value: true, description: 'Use short names for test macros -- disable if this causes conflicts.') option('constexpr_float_use_bitcast' ,type: 'boolean' ,value: true, description: 'Use std::bit_cast if available to implement exact constexpr float-to-string conversion.') diff --git a/snitch/meson.build b/snitch/meson.build index 29710ba6..9349f3a9 100644 --- a/snitch/meson.build +++ b/snitch/meson.build @@ -34,6 +34,7 @@ conf_data = configuration_data({ 'SNITCH_DEFINE_MAIN' : get_option('define_main').to_int(), 'SNITCH_WITH_EXCEPTIONS' : get_option('with_exceptions').to_int(), + 'SNITCH_WITH_MULTITHREADING' : get_option('with_multithreading').to_int(), 'SNITCH_WITH_TIMINGS' : get_option('with_timings').to_int(), 'SNITCH_WITH_SHORTHAND_MACROS' : get_option('with_shorthand_macros').to_int(), 'SNITCH_CONSTEXPR_FLOAT_USE_BITCAST' : get_option('constexpr_float_use_bitcast').to_int(),