mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-05 06:57:46 +00:00
Added thirdparty: boost library
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
||||
#ifndef BOOST_COMPAT_DETAIL_THROW_SYSTEM_ERROR_HPP_INCLUDED
|
||||
#define BOOST_COMPAT_DETAIL_THROW_SYSTEM_ERROR_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/throw_exception.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <system_error>
|
||||
|
||||
namespace boost {
|
||||
namespace compat {
|
||||
namespace detail {
|
||||
|
||||
BOOST_NORETURN BOOST_NOINLINE inline void throw_system_error( std::errc e, boost::source_location const& loc = BOOST_CURRENT_LOCATION )
|
||||
{
|
||||
boost::throw_exception( std::system_error( std::make_error_code( e ) ), loc );
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace compat
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_COMPAT_DETAIL_THROW_SYSTEM_ERROR_HPP_INCLUDED
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#ifndef BOOST_COMPAT_LATCH_HPP_INCLUDED
|
||||
#define BOOST_COMPAT_LATCH_HPP_INCLUDED
|
||||
|
||||
// Copyright 2023 Peter Dimov.
|
||||
// Copyright 2023 Christian Mazakas.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
#include <climits>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace compat
|
||||
{
|
||||
|
||||
class latch {
|
||||
private:
|
||||
std::ptrdiff_t n_;
|
||||
mutable std::mutex m_;
|
||||
mutable std::condition_variable cv_;
|
||||
|
||||
public:
|
||||
explicit latch(std::ptrdiff_t expected) : n_{expected}, m_{}, cv_{} {
|
||||
BOOST_ASSERT(n_ >= 0);
|
||||
BOOST_ASSERT(n_ <= max());
|
||||
}
|
||||
|
||||
latch(latch const &) = delete;
|
||||
latch &operator=(latch const &) = delete;
|
||||
|
||||
~latch() = default;
|
||||
|
||||
void count_down(std::ptrdiff_t n = 1) {
|
||||
std::unique_lock<std::mutex> lk(m_);
|
||||
count_down_and_notify(lk, n);
|
||||
}
|
||||
|
||||
bool try_wait() const noexcept {
|
||||
std::unique_lock<std::mutex> lk(m_);
|
||||
return is_ready();
|
||||
}
|
||||
|
||||
void wait() const {
|
||||
std::unique_lock<std::mutex> lk(m_);
|
||||
wait_impl(lk);
|
||||
}
|
||||
|
||||
void arrive_and_wait(std::ptrdiff_t n = 1) {
|
||||
std::unique_lock<std::mutex> lk(m_);
|
||||
bool should_wait = count_down_and_notify(lk, n);
|
||||
if (should_wait) {
|
||||
wait_impl(lk);
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr std::ptrdiff_t max() noexcept { return PTRDIFF_MAX; }
|
||||
|
||||
private:
|
||||
bool is_ready() const { return n_ == 0; }
|
||||
|
||||
bool count_down_and_notify(std::unique_lock<std::mutex> &lk,
|
||||
std::ptrdiff_t n) {
|
||||
BOOST_ASSERT(n <= n_);
|
||||
n_ -= n;
|
||||
if (n_ == 0) {
|
||||
lk.unlock();
|
||||
cv_.notify_all();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wait_impl(std::unique_lock<std::mutex> &lk) const {
|
||||
cv_.wait(lk, [this] { return this->is_ready(); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace compat
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_COMPAT_LATCH_HPP_INCLUDED
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
#ifndef BOOST_COMPAT_SHARED_LOCK_HPP_INCLUDED
|
||||
#define BOOST_COMPAT_SHARED_LOCK_HPP_INCLUDED
|
||||
|
||||
// Copyright 2023 Christian Mazakas.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/compat/detail/throw_system_error.hpp>
|
||||
|
||||
#include <memory> // std::addressof
|
||||
#include <mutex> // std::defer_lock_t
|
||||
#include <system_error> // std::errc
|
||||
|
||||
namespace boost {
|
||||
namespace compat {
|
||||
|
||||
template <class Mutex>
|
||||
class shared_lock;
|
||||
|
||||
template <class Mutex>
|
||||
void swap( shared_lock<Mutex>& x, shared_lock<Mutex>& y ) noexcept;
|
||||
|
||||
template <class Mutex>
|
||||
class shared_lock {
|
||||
private:
|
||||
Mutex* pm_ = nullptr;
|
||||
bool owns_ = false;
|
||||
|
||||
public:
|
||||
using mutex_type = Mutex;
|
||||
|
||||
shared_lock() noexcept = default;
|
||||
|
||||
explicit shared_lock( mutex_type& m ) : pm_( std::addressof( m ) ) { lock(); }
|
||||
|
||||
shared_lock( mutex_type& m, std::defer_lock_t ) noexcept
|
||||
: pm_( std::addressof( m ) ) {}
|
||||
|
||||
shared_lock( mutex_type& m, std::try_to_lock_t )
|
||||
: pm_( std::addressof( m ) ) {
|
||||
try_lock();
|
||||
}
|
||||
|
||||
shared_lock( mutex_type& m, std::adopt_lock_t )
|
||||
: pm_( std::addressof( m ) ), owns_{ true } {}
|
||||
|
||||
~shared_lock() {
|
||||
if ( owns_ ) {
|
||||
unlock();
|
||||
}
|
||||
}
|
||||
|
||||
shared_lock( const shared_lock& ) = delete;
|
||||
shared_lock& operator=( const shared_lock& ) = delete;
|
||||
|
||||
shared_lock( shared_lock&& u ) noexcept {
|
||||
pm_ = u.pm_;
|
||||
owns_ = u.owns_;
|
||||
|
||||
u.pm_ = nullptr;
|
||||
u.owns_ = false;
|
||||
}
|
||||
|
||||
shared_lock& operator=( shared_lock&& u ) noexcept {
|
||||
shared_lock( std::move( u ) ).swap( *this );
|
||||
return *this;
|
||||
}
|
||||
|
||||
void lock() {
|
||||
if ( !pm_ ) {
|
||||
detail::throw_system_error( std::errc::operation_not_permitted );
|
||||
}
|
||||
|
||||
if ( owns_lock() ) {
|
||||
detail::throw_system_error( std::errc::resource_deadlock_would_occur );
|
||||
}
|
||||
|
||||
pm_->lock_shared();
|
||||
owns_ = true;
|
||||
}
|
||||
|
||||
bool try_lock() {
|
||||
if ( !pm_ ) {
|
||||
detail::throw_system_error( std::errc::operation_not_permitted );
|
||||
}
|
||||
|
||||
if ( owns_lock() ) {
|
||||
detail::throw_system_error( std::errc::resource_deadlock_would_occur );
|
||||
}
|
||||
|
||||
bool b = pm_->try_lock_shared();
|
||||
owns_ = b;
|
||||
return b;
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
if ( !pm_ || !owns_ ) {
|
||||
detail::throw_system_error( std::errc::operation_not_permitted );
|
||||
}
|
||||
|
||||
pm_->unlock_shared();
|
||||
owns_ = false;
|
||||
}
|
||||
|
||||
void swap( shared_lock& u ) noexcept {
|
||||
std::swap( pm_, u.pm_ );
|
||||
std::swap( owns_, u.owns_ );
|
||||
}
|
||||
|
||||
mutex_type* release() noexcept {
|
||||
mutex_type* pm = pm_;
|
||||
pm_ = nullptr;
|
||||
owns_ = false;
|
||||
return pm;
|
||||
}
|
||||
|
||||
mutex_type* mutex() const noexcept { return pm_; }
|
||||
|
||||
bool owns_lock() const noexcept { return owns_; }
|
||||
explicit operator bool() const noexcept { return owns_; }
|
||||
};
|
||||
|
||||
template <class Mutex>
|
||||
void swap( shared_lock<Mutex>& x, shared_lock<Mutex>& y ) noexcept {
|
||||
x.swap( y );
|
||||
}
|
||||
|
||||
} // namespace compat
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_COMPAT_SHARED_LOCK_HPP_INCLUDED
|
||||
Reference in New Issue
Block a user