Skip to content

Commit

Permalink
Merge branch 'develop' into feature/issue-102
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Jan 19, 2023
2 parents 86ff47f + 3e6af15 commit 4bca94f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
17 changes: 4 additions & 13 deletions include/boost/system/detail/error_category_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand All @@ -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 )
{
Expand Down
88 changes: 88 additions & 0 deletions include/boost/system/detail/mutex.hpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4bca94f

Please sign in to comment.