session_alloc is thread-safe

fix #1408
This commit is contained in:
Vinnie Falco
2019-01-12 17:42:53 -08:00
parent cf2dbdc0be
commit abb8203cbe
2 changed files with 11 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ Version 203
* Include error code in call to set_option * Include error code in call to set_option
* saved_handler is a public interface * saved_handler is a public interface
* Use new saved_handler in websocket * Use new saved_handler in websocket
* session_alloc is thread-safe
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -18,6 +18,7 @@
#include <boost/intrusive/list.hpp> #include <boost/intrusive/list.hpp>
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <mutex>
#include <utility> #include <utility>
namespace detail { namespace detail {
@@ -79,6 +80,7 @@ protected:
boost::intrusive::constant_time_size< boost::intrusive::constant_time_size<
true>>::type; true>>::type;
std::mutex m_;
std::size_t refs_ = 1; // shared count std::size_t refs_ = 1; // shared count
std::size_t high_ = 0; // highest used std::size_t high_ = 0; // highest used
std::size_t size_ = 0; // size of buf_ std::size_t size_ = 0; // size of buf_
@@ -135,6 +137,7 @@ pool_t::
addref() -> addref() ->
pool_t& pool_t&
{ {
std::lock_guard<std::mutex> lock(m_);
++refs_; ++refs_;
return *this; return *this;
} }
@@ -145,8 +148,11 @@ session_alloc_base<Context>::
pool_t:: pool_t::
release() release()
{ {
if(--refs_) {
return; std::lock_guard<std::mutex> lock(m_);
if(--refs_)
return;
}
delete this; delete this;
} }
@@ -156,6 +162,7 @@ session_alloc_base<Context>::
pool_t:: pool_t::
alloc(std::size_t n) alloc(std::size_t n)
{ {
std::lock_guard<std::mutex> lock(m_);
if(list_.empty() && size_ < high_) if(list_.empty() && size_ < high_)
{ {
if(buf_) if(buf_)
@@ -204,6 +211,7 @@ pool_t::
dealloc(void* pv, std::size_t n) dealloc(void* pv, std::size_t n)
{ {
boost::ignore_unused(n); boost::ignore_unused(n);
std::lock_guard<std::mutex> lock(m_);
auto& e = *(reinterpret_cast<element*>(pv) - 1); auto& e = *(reinterpret_cast<element*>(pv) - 1);
BOOST_ASSERT(e.size() == n); BOOST_ASSERT(e.size() == n);
if( (e.end() > buf_ + size_) || if( (e.end() > buf_ + size_) ||