From b6119e581e6ea9273b188463dc4529c30db140ba Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Tue, 6 Jul 2021 16:51:55 +0200 Subject: uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include `. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added. --- host/include/uhd/transport/bounded_buffer.ipp | 33 ++++++++----------- host/include/uhd/transport/nirio/nirio_fifo.h | 8 ++--- host/include/uhd/transport/nirio/nirio_fifo.ipp | 16 +++++----- .../uhd/transport/nirio/niusrprio_session.h | 6 ++-- .../include/uhd/transport/nirio/rpc/rpc_client.hpp | 10 +++--- host/include/uhd/utils/soft_register.hpp | 37 +++++++++++----------- 6 files changed, 53 insertions(+), 57 deletions(-) (limited to 'host/include') diff --git a/host/include/uhd/transport/bounded_buffer.ipp b/host/include/uhd/transport/bounded_buffer.ipp index 3e15fc03a..122b3d740 100644 --- a/host/include/uhd/transport/bounded_buffer.ipp +++ b/host/include/uhd/transport/bounded_buffer.ipp @@ -11,9 +11,9 @@ #include #include #include -#include #include #include +#include namespace uhd{ namespace transport{ @@ -30,9 +30,8 @@ namespace uhd{ namespace transport{ UHD_INLINE bool push_with_haste(const elem_type &elem) { - boost::mutex::scoped_lock lock(_mutex); - if (_buffer.full()) - { + std::unique_lock lock(_mutex); + if (_buffer.full()) { return false; } _buffer.push_front(elem); @@ -42,15 +41,13 @@ namespace uhd{ namespace transport{ UHD_INLINE bool push_with_pop_on_full(const elem_type &elem) { - boost::mutex::scoped_lock lock(_mutex); - if (_buffer.full()) - { + std::lock_guard lock(_mutex); + if (_buffer.full()) { _buffer.pop_back(); _buffer.push_front(elem); _empty_cond.notify_one(); return false; - } - else { + } else { _buffer.push_front(elem); _empty_cond.notify_one(); return true; @@ -59,7 +56,7 @@ namespace uhd{ namespace transport{ UHD_INLINE void push_with_wait(const elem_type &elem) { - boost::mutex::scoped_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_buffer.full()) { _full_cond.wait(lock, _not_full_fcn); @@ -70,7 +67,7 @@ namespace uhd{ namespace transport{ UHD_INLINE bool push_with_timed_wait(const elem_type &elem, double timeout) { - boost::mutex::scoped_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_buffer.full()) { if (not _full_cond.timed_wait(lock, @@ -86,9 +83,8 @@ namespace uhd{ namespace transport{ UHD_INLINE bool pop_with_haste(elem_type &elem) { - boost::mutex::scoped_lock lock(_mutex); - if (_buffer.empty()) - { + std::unique_lock lock(_mutex); + if (_buffer.empty()) { return false; } this->pop_back(elem); @@ -98,7 +94,7 @@ namespace uhd{ namespace transport{ UHD_INLINE void pop_with_wait(elem_type &elem) { - boost::mutex::scoped_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_buffer.empty()) { _empty_cond.wait(lock, _not_empty_fcn); @@ -109,9 +105,8 @@ namespace uhd{ namespace transport{ UHD_INLINE bool pop_with_timed_wait(elem_type &elem, double timeout) { - boost::mutex::scoped_lock lock(_mutex); - if (_buffer.empty()) - { + std::unique_lock lock(_mutex); + if (_buffer.empty()) { if (not _empty_cond.timed_wait(lock, to_time_dur(timeout), _not_empty_fcn)) { @@ -124,7 +119,7 @@ namespace uhd{ namespace transport{ } private: - boost::mutex _mutex; + std::mutex _mutex; boost::condition _empty_cond, _full_cond; boost::circular_buffer _buffer; diff --git a/host/include/uhd/transport/nirio/nirio_fifo.h b/host/include/uhd/transport/nirio/nirio_fifo.h index 0cd9f1906..71b9563c3 100644 --- a/host/include/uhd/transport/nirio/nirio_fifo.h +++ b/host/include/uhd/transport/nirio/nirio_fifo.h @@ -13,12 +13,12 @@ #include #include #include -#include #include -#include #include +#include +#include +#include #include -#include namespace uhd { namespace niusrprio { @@ -213,7 +213,7 @@ private: //Members size_t _remaining_in_claimed_block; size_t _remaining_acquirable_elements; nirio_driver_iface::rio_mmap_t _mem_map; - boost::recursive_mutex _mutex; + std::recursive_mutex _mutex; niriok_proxy::sptr _riok_proxy_ptr; uint64_t _expected_xfer_count; diff --git a/host/include/uhd/transport/nirio/nirio_fifo.ipp b/host/include/uhd/transport/nirio/nirio_fifo.ipp index 048616b4c..12058aa70 100644 --- a/host/include/uhd/transport/nirio/nirio_fifo.ipp +++ b/host/include/uhd/transport/nirio/nirio_fifo.ipp @@ -61,7 +61,7 @@ nirio_status nirio_fifo::initialize( { nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == UNMAPPED) { @@ -101,7 +101,7 @@ nirio_status nirio_fifo::initialize( template void nirio_fifo::finalize() { - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); //If the FIFO is started, the stop will change the state to MAPPED. stop(); @@ -200,7 +200,7 @@ nirio_status nirio_fifo::start() nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == STARTED) { //Do nothing. Already started. @@ -237,7 +237,7 @@ nirio_status nirio_fifo::stop() nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == STARTED) { @@ -266,7 +266,7 @@ nirio_status nirio_fifo::acquire( nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr || _mem_map.is_null()) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == STARTED) { @@ -320,7 +320,7 @@ nirio_status nirio_fifo::release(const size_t elements) nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == STARTED) { status = _riok_proxy_ptr->grant_fifo( @@ -345,7 +345,7 @@ nirio_status nirio_fifo::read( nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == STARTED) { status = _riok_proxy_ptr->read_fifo( @@ -375,7 +375,7 @@ nirio_status nirio_fifo::write( nirio_status status = NiRio_Status_Success; if (!_riok_proxy_ptr) return NiRio_Status_ResourceNotInitialized; - boost::unique_lock lock(_mutex); + std::unique_lock lock(_mutex); if (_state == STARTED) { status = _riok_proxy_ptr->write_fifo( diff --git a/host/include/uhd/transport/nirio/niusrprio_session.h b/host/include/uhd/transport/nirio/niusrprio_session.h index 183f766cc..432c3cd90 100644 --- a/host/include/uhd/transport/nirio/niusrprio_session.h +++ b/host/include/uhd/transport/nirio/niusrprio_session.h @@ -13,8 +13,8 @@ #include #include #include -#include -#include +#include +#include #include namespace uhd { namespace niusrprio { @@ -103,7 +103,7 @@ private: niriok_proxy::sptr _riok_proxy; nirio_resource_manager _resource_manager; usrprio_rpc::usrprio_rpc_client _rpc_client; - boost::recursive_mutex _session_mutex; + std::recursive_mutex _session_mutex; }; }} // namespace uhd::niusrprio diff --git a/host/include/uhd/transport/nirio/rpc/rpc_client.hpp b/host/include/uhd/transport/nirio/rpc/rpc_client.hpp index 9361b7276..844e36c76 100644 --- a/host/include/uhd/transport/nirio/rpc/rpc_client.hpp +++ b/host/include/uhd/transport/nirio/rpc/rpc_client.hpp @@ -11,9 +11,11 @@ #include #include #include -#include -#include +#include +#include +#include #include +#include namespace uhd { namespace usrprio_rpc { @@ -76,8 +78,8 @@ private: func_xport_buf_t _request; func_xport_buf_t _response; // Synchronization - boost::mutex _mutex; - boost::condition_variable _exec_gate; + std::mutex _mutex; + boost::condition _exec_gate; boost::system::error_code _exec_err; }; diff --git a/host/include/uhd/utils/soft_register.hpp b/host/include/uhd/utils/soft_register.hpp index e38ffedfe..d23d1b8fb 100644 --- a/host/include/uhd/utils/soft_register.hpp +++ b/host/include/uhd/utils/soft_register.hpp @@ -11,12 +11,11 @@ #include #include #include -#include #include -#include -#include #include +#include #include +#include /*! \file soft_register.hpp * Utilities to access and index hardware registers. @@ -329,48 +328,48 @@ public: UHD_INLINE void initialize(wb_iface& iface, bool sync = false) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); soft_register_t::initialize(iface, sync); } UHD_INLINE void set(const soft_reg_field_t field, const reg_data_t value) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); soft_register_t::set(field, value); } UHD_INLINE reg_data_t get(const soft_reg_field_t field) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); return soft_register_t::get(field); } UHD_INLINE void flush() { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); soft_register_t::flush(); } UHD_INLINE void refresh() { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); soft_register_t::refresh(); } UHD_INLINE void write(const soft_reg_field_t field, const reg_data_t value) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); soft_register_t::write(field, value); } UHD_INLINE reg_data_t read(const soft_reg_field_t field) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); return soft_register_t::read(field); } private: - boost::mutex _mutex; + std::mutex _mutex; }; /* @@ -483,7 +482,7 @@ public: */ void initialize(wb_iface& iface, bool sync = false) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); for (soft_register_base* reg : _reglist) { reg->initialize(iface, sync); } @@ -496,7 +495,7 @@ public: */ void flush() { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); for (soft_register_base* reg : _reglist) { reg->flush(); } @@ -509,7 +508,7 @@ public: */ void refresh() { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); for (soft_register_base* reg : _reglist) { reg->refresh(); } @@ -555,7 +554,7 @@ protected: const std::string& name, const visibility_t visible = PRIVATE) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); if (visible == PUBLIC) { // Only add to the map if this register is publicly visible if (not _regmap.insert(regmap_t::value_type(name, ®)).second) { @@ -573,7 +572,7 @@ private: const std::string _name; regmap_t _regmap; // For lookups reglist_t _reglist; // To maintain order - boost::mutex _mutex; + std::mutex _mutex; }; @@ -611,7 +610,7 @@ public: */ void add(soft_regmap_t& regmap) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); _regmaps.push_back(®map); } @@ -620,7 +619,7 @@ public: */ void add(soft_regmap_db_t& db) { - boost::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); if (&db == this) { throw uhd::assertion_error("cannot add regmap db to itself" + _name); } else { @@ -700,7 +699,7 @@ private: const std::string _name; db_t _regmaps; db_t _regmap_dbs; - boost::mutex _mutex; + std::mutex _mutex; }; } // namespace uhd -- cgit v1.2.3