From 3e6af15652eb6136d68bbb0288c0a89bfc08d77c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 19 Jan 2023 12:04:06 +0200 Subject: [PATCH] Add system/detail/mutex.hpp, use it in error_category_impl.hpp --- .../system/detail/error_category_impl.hpp | 17 +--- include/boost/system/detail/mutex.hpp | 88 +++++++++++++++++++ 2 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 include/boost/system/detail/mutex.hpp diff --git a/include/boost/system/detail/error_category_impl.hpp b/include/boost/system/detail/error_category_impl.hpp index 0c4593f..fae6951 100644 --- a/include/boost/system/detail/error_category_impl.hpp +++ b/include/boost/system/detail/error_category_impl.hpp @@ -98,33 +98,26 @@ inline char const * error_category::message( int ev, char * buffer, std::size_t #if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR) #include +#include #include -#if !defined(BOOST_SYSTEM_DISABLE_THREADS) -# include -#endif - namespace boost { namespace system { -#if !defined(BOOST_SYSTEM_DISABLE_THREADS) - namespace detail { template struct stdcat_mx_holder { - static std::mutex mx_; + static mutex mx_; }; -template std::mutex stdcat_mx_holder::mx_; +template mutex stdcat_mx_holder::mx_; } // namespace detail -#endif - inline void error_category::init_stdcat() const { static_assert( sizeof( stdcat_ ) >= sizeof( boost::system::detail::std_category ), "sizeof(stdcat_) is not enough for std_category" ); @@ -137,9 +130,7 @@ inline void error_category::init_stdcat() const #endif -#if !defined(BOOST_SYSTEM_DISABLE_THREADS) - std::lock_guard lk( boost::system::detail::stdcat_mx_holder<>::mx_ ); -#endif + system::detail::lock_guard lk( system::detail::stdcat_mx_holder<>::mx_ ); if( sc_init_.load( std::memory_order_acquire ) == 0 ) { diff --git a/include/boost/system/detail/mutex.hpp b/include/boost/system/detail/mutex.hpp new file mode 100644 index 0000000..66f13e5 --- /dev/null +++ b/include/boost/system/detail/mutex.hpp @@ -0,0 +1,88 @@ +#ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED +#define BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED + +// Copyright 2023 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt) + +#include + +#if defined(BOOST_SYSTEM_DISABLE_THREADS) + +namespace boost +{ +namespace system +{ +namespace detail +{ + +struct mutex +{ + void lock() + { + } + + void unlock() + { + } +}; + +} // namespace detail +} // namespace system +} // namespace boost + +#else + +#include + +namespace boost +{ +namespace system +{ +namespace detail +{ + +using std::mutex; + +} // namespace detail +} // namespace system +} // namespace boost + +#endif + +namespace boost +{ +namespace system +{ +namespace detail +{ + +template class lock_guard +{ +private: + + Mtx& mtx_; + +private: + + lock_guard( lock_guard const& ); + lock_guard& operator=( lock_guard const& ); + +public: + + explicit lock_guard( Mtx& mtx ): mtx_( mtx ) + { + mtx_.lock(); + } + + ~lock_guard() + { + mtx_.unlock(); + } +}; + +} // namespace detail +} // namespace system +} // namespace boost + +#endif // #ifndef BOOST_SYSTEM_DETAIL_MUTEX_HPP_INCLUDED