forked from boostorg/system
Add system/detail/mutex.hpp, use it in error_category_impl.hpp
This commit is contained in:
@ -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 <boost/system/detail/std_category_impl.hpp>
|
||||
#include <boost/system/detail/mutex.hpp>
|
||||
#include <new>
|
||||
|
||||
#if !defined(BOOST_SYSTEM_DISABLE_THREADS)
|
||||
# include <mutex>
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace system
|
||||
{
|
||||
|
||||
#if !defined(BOOST_SYSTEM_DISABLE_THREADS)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<class = void> struct stdcat_mx_holder
|
||||
{
|
||||
static std::mutex mx_;
|
||||
static mutex mx_;
|
||||
};
|
||||
|
||||
template<class T> std::mutex stdcat_mx_holder<T>::mx_;
|
||||
template<class T> mutex stdcat_mx_holder<T>::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<std::mutex> lk( boost::system::detail::stdcat_mx_holder<>::mx_ );
|
||||
#endif
|
||||
system::detail::lock_guard<system::detail::mutex> lk( system::detail::stdcat_mx_holder<>::mx_ );
|
||||
|
||||
if( sc_init_.load( std::memory_order_acquire ) == 0 )
|
||||
{
|
||||
|
88
include/boost/system/detail/mutex.hpp
Normal file
88
include/boost/system/detail/mutex.hpp
Normal file
@ -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 <boost/config.hpp>
|
||||
|
||||
#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 <mutex>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace system
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
using std::mutex;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace system
|
||||
} // namespace boost
|
||||
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace system
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<class Mtx> 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
|
Reference in New Issue
Block a user