mirror of
https://github.com/boostorg/beast.git
synced 2025-07-30 21:07:26 +02:00
Add detail/soft_mutex.hpp
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
Version 205
|
||||
|
||||
* Doc work
|
||||
* Add detail/soft_mutex.hpp
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
112
include/boost/beast/websocket/detail/soft_mutex.hpp
Normal file
112
include/boost/beast/websocket/detail/soft_mutex.hpp
Normal file
@ -0,0 +1,112 @@
|
||||
//
|
||||
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_SOFT_MUTEX_HPP
|
||||
#define BOOST_BEAST_WEBSOCKET_DETAIL_SOFT_MUTEX_HPP
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace websocket {
|
||||
namespace detail {
|
||||
|
||||
// used to order reads, writes in websocket streams
|
||||
|
||||
class soft_mutex
|
||||
{
|
||||
int id_ = 0;
|
||||
|
||||
public:
|
||||
soft_mutex() = default;
|
||||
soft_mutex(soft_mutex const&) = delete;
|
||||
soft_mutex& operator=(soft_mutex const&) = delete;
|
||||
|
||||
soft_mutex(soft_mutex&& other) noexcept
|
||||
: id_(boost::exchange(other.id_, 0))
|
||||
{
|
||||
}
|
||||
|
||||
soft_mutex& operator=(soft_mutex&& other) noexcept
|
||||
{
|
||||
id_ = other.id_;
|
||||
other.id_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// VFALCO I'm not too happy that this function is needed
|
||||
void
|
||||
reset()
|
||||
{
|
||||
id_ = 0;
|
||||
}
|
||||
|
||||
bool
|
||||
is_locked() const noexcept
|
||||
{
|
||||
return id_ != 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool
|
||||
is_locked(T const*) const noexcept
|
||||
{
|
||||
return id_ == T::id;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void
|
||||
lock(T const*)
|
||||
{
|
||||
BOOST_ASSERT(id_ == 0);
|
||||
id_ = T::id;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void
|
||||
unlock(T const*)
|
||||
{
|
||||
BOOST_ASSERT(id_ == T::id);
|
||||
id_ = 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool
|
||||
try_lock(T const*)
|
||||
{
|
||||
// If this assert goes off it means you are attempting to
|
||||
// simultaneously initiate more than one of same asynchronous
|
||||
// operation, which is not allowed. For example, you must wait
|
||||
// for an async_read to complete before performing another
|
||||
// async_read.
|
||||
//
|
||||
BOOST_ASSERT(id_ != T::id);
|
||||
if(id_ != 0)
|
||||
return false;
|
||||
id_ = T::id;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool
|
||||
try_unlock(T const*) noexcept
|
||||
{
|
||||
if(id_ != T::id)
|
||||
return false;
|
||||
id_ = 0;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // detail
|
||||
} // websocket
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif
|
@ -7,8 +7,8 @@
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_WEBSOCKET_STREAM_BASE_HPP
|
||||
#define BOOST_BEAST_WEBSOCKET_STREAM_BASE_HPP
|
||||
#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_STREAM_BASE_HPP
|
||||
#define BOOST_BEAST_WEBSOCKET_DETAIL_STREAM_BASE_HPP
|
||||
|
||||
#include <boost/beast/http/empty_body.hpp>
|
||||
#include <boost/beast/http/message.hpp>
|
||||
@ -43,87 +43,6 @@ namespace beast {
|
||||
namespace websocket {
|
||||
namespace detail {
|
||||
|
||||
// used to order reads and writes
|
||||
class soft_mutex
|
||||
{
|
||||
int id_ = 0;
|
||||
|
||||
public:
|
||||
soft_mutex() = default;
|
||||
soft_mutex(soft_mutex const&) = delete;
|
||||
soft_mutex& operator=(soft_mutex const&) = delete;
|
||||
|
||||
soft_mutex(soft_mutex&& other) noexcept
|
||||
: id_(boost::exchange(other.id_, 0))
|
||||
{
|
||||
}
|
||||
|
||||
soft_mutex& operator=(soft_mutex&& other) noexcept
|
||||
{
|
||||
id_ = other.id_;
|
||||
other.id_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// VFALCO I'm not too happy that this function is needed
|
||||
void reset()
|
||||
{
|
||||
id_ = 0;
|
||||
}
|
||||
|
||||
bool is_locked() const
|
||||
{
|
||||
return id_ != 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool is_locked(T const*) const
|
||||
{
|
||||
return id_ == T::id;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void lock(T const*)
|
||||
{
|
||||
BOOST_ASSERT(id_ == 0);
|
||||
id_ = T::id;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void unlock(T const*)
|
||||
{
|
||||
BOOST_ASSERT(id_ == T::id);
|
||||
id_ = 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool try_lock(T const*)
|
||||
{
|
||||
// If this assert goes off it means you are attempting to
|
||||
// simultaneously initiate more than one of same asynchronous
|
||||
// operation, which is not allowed. For example, you must wait
|
||||
// for an async_read to complete before performing another
|
||||
// async_read.
|
||||
//
|
||||
BOOST_ASSERT(id_ != T::id);
|
||||
if(id_ != 0)
|
||||
return false;
|
||||
id_ = T::id;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool try_unlock(T const*)
|
||||
{
|
||||
if(id_ != T::id)
|
||||
return false;
|
||||
id_ = 0;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
struct stream_prng
|
||||
{
|
||||
bool secure_prng_ = true;
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <boost/beast/websocket/detail/hybi13.hpp>
|
||||
#include <boost/beast/websocket/detail/mask.hpp>
|
||||
#include <boost/beast/websocket/detail/pmd_extension.hpp>
|
||||
#include <boost/beast/websocket/detail/soft_mutex.hpp>
|
||||
#include <boost/beast/websocket/detail/stream_base.hpp>
|
||||
#include <boost/beast/websocket/detail/utf8_checker.hpp>
|
||||
#include <boost/beast/core/saved_handler.hpp>
|
||||
|
Reference in New Issue
Block a user