From 6fdfe4ff4c2697c77528d66546342d00d427c24a Mon Sep 17 00:00:00 2001 From: Vitor Sessak Date: Thu, 3 Sep 2026 12:27:04 +0000 Subject: [PATCH] Provide std::mutex fallback for omp_mutex_t when OpenMP is disabled When compiling without OpenMP, provide standard library std::mutex fallback wrapper functions to allow compiling omp_mutex_t locks. --- cpp/src/utilities/omp_helpers.hpp | 37 ++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/cpp/src/utilities/omp_helpers.hpp b/cpp/src/utilities/omp_helpers.hpp index 3fd31d169e..22a8164a5e 100644 --- a/cpp/src/utilities/omp_helpers.hpp +++ b/cpp/src/utilities/omp_helpers.hpp @@ -24,13 +24,47 @@ std::pair calculate_index_range(i_t k, double total, double n) } // namespace cuopt #ifdef _OPENMP - #include +#else +#include +#endif #include #include namespace cuopt { +#ifndef _OPENMP +class omp_mutex_t { + public: + omp_mutex_t() : mutex(new std::mutex()) { } + + omp_mutex_t(const omp_mutex_t&) = delete; + + omp_mutex_t(omp_mutex_t&& other) { *this = std::move(other); } + + omp_mutex_t& operator=(const omp_mutex_t&) = delete; + + omp_mutex_t& operator=(omp_mutex_t&& other) + { + if (&other != this) { + mutex = std::move(other.mutex); + } + return *this; + } + + void lock() { mutex->lock(); } + + void unlock() { mutex->unlock(); } + + bool try_lock() { return mutex->try_lock(); } + + private: + std::unique_ptr mutex; +}; +#endif // !_OPENMP + +#ifdef _OPENMP + // Wrapper of omp_lock_t. Optionally, you can provide a hint as defined in // https://www.openmp.org/spec-html/5.1/openmpse39.html#x224-2570003.9 class omp_mutex_t { @@ -67,6 +101,7 @@ class omp_mutex_t { private: std::unique_ptr mutex; }; +#endif // _OPENMP // Empty class with the same methods as `omp_mutex_t`. This is mainly used for cleanly disabling // the `omp_mutex_t` via type alias (`lock` and `unlock` are replaced by NOOPs).