diff --git a/CMakeLists.txt b/CMakeLists.txt index 54b4c55..c99c66a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.24) project(tatami_hdf5 - VERSION 2.0.1 + VERSION 2.0.2 DESCRIPTION "tatami bindings for HDF5" LANGUAGES CXX) diff --git a/include/tatami_hdf5/serialize.hpp b/include/tatami_hdf5/serialize.hpp index b16b485..2dfc019 100644 --- a/include/tatami_hdf5/serialize.hpp +++ b/include/tatami_hdf5/serialize.hpp @@ -6,15 +6,15 @@ * @brief Default locking for serial access. */ -/** - * @cond - */ #ifndef TATAMI_HDF5_PARALLEL_LOCK #include "subpar/subpar.hpp" #ifndef SUBPAR_USES_OPENMP -#include #include +#include namespace tatami_hdf5 { +// We put this in a separate function instead of a static function variable +// inside serialize(), as we would get a different mutex for each instance +// of serialize() created with a different Function_. inline auto& get_default_hdf5_lock() { static std::mutex hdf5_lock; return hdf5_lock; @@ -22,9 +22,6 @@ inline auto& get_default_hdf5_lock() { } #endif #endif -/** - * @endcond - */ namespace tatami_hdf5 { @@ -48,15 +45,18 @@ template void serialize(Function_ f) { #ifdef TATAMI_HDF5_PARALLEL_LOCK TATAMI_HDF5_PARALLEL_LOCK(f); -#elif defined(SUBPAR_USES_OPENMP) +#else +#ifdef SUBPAR_USES_OPENMP #pragma omp critical(hdf5) { f(); } #else - std::lock_guard thing(get_hdf5_lock()); + auto& h5lock = get_default_hdf5_lock(); + std::lock_guard thing(h5lock); f(); #endif +#endif } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dab29ee..3934c30 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -51,6 +51,15 @@ endmacro() decorate_test(libtest) +add_executable( + cuslocktest + src/write_compressed_sparse_matrix.cpp + src/DenseMatrix.cpp + src/CompressedSparseMatrix.cpp +) +decorate_test(cuslocktest) +target_compile_definitions(cuslocktest PRIVATE TATAMI_HDF5_TEST_PARALLEL_ONLY=1 TATAMI_HDF5_TEST_CUSTOM_LOCK=1) + find_package(OpenMP) if(OpenMP_FOUND) add_executable( @@ -59,7 +68,6 @@ if(OpenMP_FOUND) src/DenseMatrix.cpp src/CompressedSparseMatrix.cpp ) - decorate_test(omptest) target_link_libraries(omptest OpenMP::OpenMP_CXX) target_compile_definitions(omptest PRIVATE TATAMI_HDF5_TEST_PARALLEL_ONLY=1) diff --git a/tests/src/custom_parallel.h b/tests/src/custom_parallel.h index b80ff31..e5e597c 100644 --- a/tests/src/custom_parallel.h +++ b/tests/src/custom_parallel.h @@ -6,6 +6,7 @@ // OpenMP critical regions instead of using this mutex-based lock. #ifndef _OPENMP +#ifdef TATAMI_HDF5_TEST_CUSTOM_LOCK #include #include @@ -24,3 +25,4 @@ void hdf5_serialize(Function f) { #endif #endif +#endif