saved_handler maintains a work_guard (websocket):

* Tidy up and comment asserts

* `pausation` is renamed to saved_handler

* A work guard on the handler's associated executor
  is maintained for the lifetime of the store handler.
This commit is contained in:
Vinnie Falco
2018-12-09 19:00:04 -08:00
parent 8ea282ee5e
commit b6eb988694
4 changed files with 178 additions and 169 deletions

View File

@@ -4,6 +4,7 @@ Version 198:
* multi_buffer improvements
* static_buffer improvements
* flat_static_buffer_improvements
* saved_handler maintains a work_guard (websocket)
API Changes:

View File

@@ -1,162 +0,0 @@
//
// 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_PAUSATION_HPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
#include <boost/beast/core/detail/allocator.hpp>
#include <boost/asio/associated_allocator.hpp>
#include <boost/assert.hpp>
#include <memory>
#include <utility>
namespace boost {
namespace beast {
namespace websocket {
namespace detail {
// A container that holds a suspended, asynchronous composed
// operation. The contained object may be invoked later to
// resume the operation, or the container may be destroyed.
//
class pausation
{
struct handler
{
handler() = default;
handler(handler &&) = delete;
handler(handler const&) = delete;
virtual ~handler() = default;
virtual void destroy() = 0;
virtual void invoke() = 0;
};
template<class Handler>
class impl : public handler
{
Handler h_;
public:
template<class DeducedHandler>
impl(DeducedHandler&& h)
: h_(std::forward<DeducedHandler>(h))
{
}
void
destroy() override
{
Handler h(std::move(h_));
typename beast::detail::allocator_traits<
net::associated_allocator_t<
Handler>>::template rebind_alloc<impl> alloc{
net::get_associated_allocator(h)};
beast::detail::allocator_traits<
decltype(alloc)>::destroy(alloc, this);
beast::detail::allocator_traits<
decltype(alloc)>::deallocate(alloc, this, 1);
}
void
invoke() override
{
Handler h(std::move(h_));
typename beast::detail::allocator_traits<
net::associated_allocator_t<
Handler>>::template rebind_alloc<impl> alloc{
net::get_associated_allocator(h)};
beast::detail::allocator_traits<
decltype(alloc)>::destroy(alloc, this);
beast::detail::allocator_traits<
decltype(alloc)>::deallocate(alloc, this, 1);
h();
}
};
handler* h_ = nullptr;
public:
pausation() = default;
pausation(pausation const&) = delete;
pausation& operator=(pausation const&) = delete;
~pausation()
{
if(h_)
h_->destroy();
}
pausation(pausation&& other)
{
boost::ignore_unused(other);
BOOST_ASSERT(! other.h_);
}
pausation&
operator=(pausation&& other)
{
boost::ignore_unused(other);
BOOST_ASSERT(! h_);
BOOST_ASSERT(! other.h_);
return *this;
}
template<class CompletionHandler>
void
emplace(CompletionHandler&& handler);
explicit
operator bool() const
{
return h_ != nullptr;
}
bool
maybe_invoke()
{
if(h_)
{
auto const h = h_;
h_ = nullptr;
h->invoke();
return true;
}
return false;
}
};
template<class CompletionHandler>
void
pausation::emplace(CompletionHandler&& handler)
{
BOOST_ASSERT(! h_);
typename beast::detail::allocator_traits<
net::associated_allocator_t<
CompletionHandler>>::template rebind_alloc<
impl<CompletionHandler>> alloc{
net::get_associated_allocator(handler)};
using A = decltype(alloc);
auto const d =
[&alloc](impl<CompletionHandler>* p)
{
beast::detail::allocator_traits<A>::deallocate(alloc, p, 1);
};
std::unique_ptr<impl<CompletionHandler>, decltype(d)> p{
beast::detail::allocator_traits<A>::allocate(alloc, 1), d};
beast::detail::allocator_traits<A>::construct(
alloc, p.get(), std::forward<CompletionHandler>(handler));
h_ = p.release();
}
} // detail
} // websocket
} // beast
} // boost
#endif

View File

@@ -0,0 +1,170 @@
//
// 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_SAVED_HANDLER_HPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_SAVED_HANDLER_HPP
#include <boost/beast/core/detail/allocator.hpp>
#include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp>
#include <boost/asio/executor_work_guard.hpp>
#include <boost/assert.hpp>
#include <memory>
#include <utility>
namespace boost {
namespace beast {
namespace websocket {
namespace detail {
// A container that holds a suspended, asynchronous composed
// operation. The contained object may be invoked later to
// resume the operation, or the container may be destroyed.
//
class saved_handler
{
struct base
{
base() = default;
base(base &&) = delete;
base(base const&) = delete;
virtual void destroy() = 0;
virtual void invoke() = 0;
protected:
~base() = default;
};
template<class Handler>
class impl : public base
{
Handler h_;
net::executor_work_guard<
net::associated_executor_t<Handler>> wg_;
public:
template<class DeducedHandler>
impl(DeducedHandler&& h)
: h_(std::forward<DeducedHandler>(h))
, wg_(net::get_associated_executor(h_))
{
}
void
destroy() override
{
using A = typename beast::detail::allocator_traits<
net::associated_allocator_t<
Handler>>::template rebind_alloc<impl>;
using alloc_traits =
beast::detail::allocator_traits<A>;
Handler h(std::move(h_));
A alloc(net::get_associated_allocator(h));
alloc_traits::destroy(alloc, this);
alloc_traits::deallocate(alloc, this, 1);
}
void
invoke() override
{
using A = typename beast::detail::allocator_traits<
net::associated_allocator_t<
Handler>>::template rebind_alloc<impl>;
using alloc_traits =
beast::detail::allocator_traits<A>;
Handler h(std::move(h_));
A alloc(net::get_associated_allocator(h));
alloc_traits::destroy(alloc, this);
alloc_traits::deallocate(alloc, this, 1);
h();
}
};
base* p_ = nullptr;
public:
saved_handler() = default;
saved_handler(saved_handler const&) = delete;
saved_handler& operator=(saved_handler const&) = delete;
~saved_handler()
{
if(p_)
p_->destroy();
}
saved_handler(saved_handler&& other)
{
boost::ignore_unused(other);
// Moving a saved handler that
// owns an object is illegal
BOOST_ASSERT(! other.p_);
}
saved_handler&
operator=(saved_handler&& other)
{
boost::ignore_unused(other);
// Moving a saved handler that
// owns an object is illegal
BOOST_ASSERT(! p_);
BOOST_ASSERT(! other.p_);
return *this;
}
template<class Handler>
void
emplace(Handler&& handler)
{
// Can't emplace twice without invoke
BOOST_ASSERT(! p_);
using A = typename beast::detail::allocator_traits<
net::associated_allocator_t<Handler>>::template
rebind_alloc<impl<Handler>>;
using alloc_traits =
beast::detail::allocator_traits<A>;
A alloc(net::get_associated_allocator(handler));
auto const d =
[&alloc](impl<Handler>* p)
{
alloc_traits::deallocate(alloc, p, 1);
};
std::unique_ptr<impl<Handler>, decltype(d)> p(
alloc_traits::allocate(alloc, 1), d);
alloc_traits::construct(alloc, p.get(),
std::forward<Handler>(handler));
p_ = p.release();
}
explicit
operator bool() const noexcept
{
return p_ != nullptr;
}
bool
maybe_invoke()
{
if(p_)
{
auto const h = p_;
p_ = nullptr;
h->invoke();
return true;
}
return false;
}
};
} // detail
} // websocket
} // beast
} // boost
#endif

View File

@@ -19,7 +19,7 @@
#include <boost/beast/websocket/detail/frame.hpp>
#include <boost/beast/websocket/detail/hybi13.hpp>
#include <boost/beast/websocket/detail/mask.hpp>
#include <boost/beast/websocket/detail/pausation.hpp>
#include <boost/beast/websocket/detail/saved_handler.hpp>
#include <boost/beast/websocket/detail/pmd_extension.hpp>
#include <boost/beast/websocket/detail/stream_base.hpp>
#include <boost/beast/websocket/detail/utf8_checker.hpp>
@@ -204,12 +204,12 @@ class stream
= 4096;
detail::fh_buffer wr_fb_; // header buffer used for writes
detail::pausation paused_rd_; // paused read op
detail::pausation paused_wr_; // paused write op
detail::pausation paused_ping_; // paused ping op
detail::pausation paused_close_; // paused close op
detail::pausation paused_r_rd_; // paused read op (async read)
detail::pausation paused_r_close_;// paused close op (async read)
detail::saved_handler paused_rd_; // paused read op
detail::saved_handler paused_wr_; // paused write op
detail::saved_handler paused_ping_; // paused ping op
detail::saved_handler paused_close_; // paused close op
detail::saved_handler paused_r_rd_; // paused read op (async read)
detail::saved_handler paused_r_close_;// paused close op (async read)
public:
/// Indicates if the permessage-deflate extension is supported