From 30b7cf91c940eceabc1cdd149f2f2b1d04b07994 Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Mon, 28 Oct 2019 09:53:53 +0000 Subject: [PATCH] Use std::condition_variable_any with timed locking (#2365) --- CMakeLists.txt | 8 ++--- nano/lib/CMakeLists.txt | 5 --- nano/lib/locks.cpp | 15 -------- nano/lib/locks.hpp | 58 +++--------------------------- util/build_prep/bootstrap_boost.sh | 2 +- 5 files changed, 7 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49e6d49d..accfab98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ set (NANO_SECURE_RPC OFF CACHE BOOL "") set (NANO_ROCKSDB OFF CACHE BOOL "") set (NANO_POW_SERVER OFF CACHE BOOL "") set (NANO_WARN_TO_ERR OFF CACHE BOOL "") -set (NANO_TIMED_LOCKS 0 CACHE INTEGER "") +set (NANO_TIMED_LOCKS 0 CACHE STRING "") option (NANO_STACKTRACE_BACKTRACE "Use BOOST_STACKTRACE_USE_BACKTRACE in stacktraces, for POSIX" OFF) if (NANO_STACKTRACE_BACKTRACE) @@ -192,11 +192,7 @@ set(Boost_USE_MULTITHREADED ON) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules") -if (${NANO_TIMED_LOCKS} GREATER 0) - set (timed_locks_boost_libs context fiber) -endif () - -find_package (Boost 1.67.0 REQUIRED COMPONENTS filesystem log log_setup thread program_options system ${timed_locks_boost_libs}) +find_package (Boost 1.67.0 REQUIRED COMPONENTS filesystem log log_setup thread program_options system) if (NANO_ROCKSDB) find_package (RocksDB REQUIRED) diff --git a/nano/lib/CMakeLists.txt b/nano/lib/CMakeLists.txt index 41d8bde0..71f0d699 100644 --- a/nano/lib/CMakeLists.txt +++ b/nano/lib/CMakeLists.txt @@ -10,10 +10,6 @@ else () error ("Unknown platform: ${CMAKE_SYSTEM_NAME}") endif () -if (${NANO_TIMED_LOCKS} GREATER 0) - set (timed_locks_boost_libs Boost::context Boost::fiber) -endif () - add_library (nano_lib ${platform_sources} alarm.hpp @@ -68,7 +64,6 @@ target_link_libraries (nano_lib blake2 ${CRYPTOPP_LIBRARY} ${CMAKE_DL_LIBS} - ${timed_locks_boost_libs} Boost::boost) if (NANO_STACKTRACE_BACKTRACE) diff --git a/nano/lib/locks.cpp b/nano/lib/locks.cpp index 4f85560d..07589727 100644 --- a/nano/lib/locks.cpp +++ b/nano/lib/locks.cpp @@ -179,20 +179,5 @@ void unique_lock::validate () const // Explicit instantiations for allowed types template class unique_lock; - -void condition_variable::notify_one () noexcept -{ - cnd.notify_one (); -} - -void condition_variable::notify_all () noexcept -{ - cnd.notify_all (); -} - -void condition_variable::wait (nano::unique_lock & lk) -{ - cnd.wait (lk); -} } #endif diff --git a/nano/lib/locks.hpp b/nano/lib/locks.hpp index 2c47dc9a..3d22dff4 100644 --- a/nano/lib/locks.hpp +++ b/nano/lib/locks.hpp @@ -1,12 +1,7 @@ #pragma once #include -#if NANO_TIMED_LOCKS > 0 -#ifndef NOMINMAX -#define NOMINMAX -#endif -#include -#endif + #include #include #include @@ -74,59 +69,14 @@ private: void lock_impl (); }; -class condition_variable final -{ -private: - boost::fibers::condition_variable_any cnd; - -public: - condition_variable () = default; - condition_variable (condition_variable const &) = delete; - condition_variable & operator= (condition_variable const &) = delete; - - void notify_one () noexcept; - void notify_all () noexcept; - void wait (nano::unique_lock & lt); - - template - void wait (nano::unique_lock & lt, Pred pred) - { - cnd.wait (lt, pred); - } - - template - void wait_until (nano::unique_lock & lk, std::chrono::time_point const & timeout_time) - { - cnd.wait_until (lk, timeout_time); - } - - template - bool wait_until (nano::unique_lock & lk, std::chrono::time_point const & timeout_time, Pred pred) - { - return cnd.wait_until (lk, timeout_time, pred); - } - - template - void wait_for (nano::unique_lock & lk, - std::chrono::duration const & timeout_duration) - { - cnd.wait_for (lk, timeout_duration); - } - - template - bool wait_for (nano::unique_lock & lk, std::chrono::duration const & timeout_duration, Pred pred) - { - return cnd.wait_for (lk, timeout_duration, pred); - } -}; - #else template using lock_guard = std::lock_guard; template using unique_lock = std::unique_lock; - -using condition_variable = std::condition_variable; #endif + +// For consistency wrapping the less well known _any variant which can be used with any lockable type +using condition_variable = std::condition_variable_any; } diff --git a/util/build_prep/bootstrap_boost.sh b/util/build_prep/bootstrap_boost.sh index 38cc123c..47033144 100755 --- a/util/build_prep/bootstrap_boost.sh +++ b/util/build_prep/bootstrap_boost.sh @@ -26,7 +26,7 @@ while getopts 'hmcCkpvB:' OPT; do exit 0 ;; m) - bootstrapArgs+=('--with-libraries=system,thread,log,filesystem,program_options,context,fiber') + bootstrapArgs+=('--with-libraries=system,thread,log,filesystem,program_options') ;; c) useClang='true'