Made pointer to class method as atomic pointer

This commit is contained in:
bernat 2020-06-19 12:39:19 +02:00
parent 9b716e1698
commit acd85f0624
2 changed files with 4 additions and 6 deletions

View File

@ -13,7 +13,7 @@ namespace carla {
namespace streaming { namespace streaming {
/// A stream represents an unidirectional channel for sending data from server /// A stream represents an unidirectional channel for sending data from server
/// to client. A **single** client can subscribe to this stream using the /// to client. **Multiple** clients can subscribe to this stream using the
/// stream token. If no client is subscribed, the data flushed down the stream /// stream token. If no client is subscribed, the data flushed down the stream
/// is discarded. /// is discarded.
using Stream = detail::Stream<detail::MultiStreamState>; using Stream = detail::Stream<detail::MultiStreamState>;

View File

@ -13,6 +13,7 @@
#include <mutex> #include <mutex>
#include <vector> #include <vector>
#include <atomic>
namespace carla { namespace carla {
namespace streaming { namespace streaming {
@ -25,6 +26,7 @@ namespace detail {
public: public:
using StreamStateBase::StreamStateBase; using StreamStateBase::StreamStateBase;
using WriteMethodPointer = void (MultiStreamState::*)(std::shared_ptr<const tcp::Message>);
MultiStreamState(const token_type &token) : MultiStreamState(const token_type &token) :
StreamStateBase(token), StreamStateBase(token),
@ -52,7 +54,6 @@ namespace detail {
else if (_sessions.size() > 1) { else if (_sessions.size() > 1) {
_writeFunction = &MultiStreamState::WriteMulti; _writeFunction = &MultiStreamState::WriteMulti;
} }
(_writeFunction == &MultiStreamState::WriteSingle) ? log_debug("Pointer to single stream") : log_debug("Pointer to multi stream");
} }
void DisconnectSession(std::shared_ptr<Session> session) final { void DisconnectSession(std::shared_ptr<Session> session) final {
@ -63,7 +64,6 @@ namespace detail {
DEBUG_ASSERT(session == _session.load()); DEBUG_ASSERT(session == _session.load());
_session.store(nullptr); _session.store(nullptr);
_sessions.clear(); _sessions.clear();
_writeFunction = &MultiStreamState::WriteSingle;
} else { } else {
_sessions.erase( _sessions.erase(
std::remove(_sessions.begin(), _sessions.end(), session), std::remove(_sessions.begin(), _sessions.end(), session),
@ -77,7 +77,6 @@ namespace detail {
_writeFunction = &MultiStreamState::WriteSingle; _writeFunction = &MultiStreamState::WriteSingle;
} }
log_debug("Disconnecting multistream sessions:", _sessions.size()); log_debug("Disconnecting multistream sessions:", _sessions.size());
(_writeFunction == &MultiStreamState::WriteSingle) ? log_debug("Pointer to single stream") : log_debug("Pointer to multi stream");
} }
void ClearSessions() final { void ClearSessions() final {
@ -86,7 +85,6 @@ namespace detail {
_session.store(nullptr); _session.store(nullptr);
_writeFunction = &MultiStreamState::WriteSingle; _writeFunction = &MultiStreamState::WriteSingle;
log_debug("Disconnecting all multistream sessions"); log_debug("Disconnecting all multistream sessions");
(_writeFunction == &MultiStreamState::WriteSingle) ? log_debug("Pointer to single stream") : log_debug("Pointer to multi stream");
} }
void WriteSingle(std::shared_ptr<const tcp::Message> message) { void WriteSingle(std::shared_ptr<const tcp::Message> message) {
@ -114,7 +112,7 @@ namespace detail {
// if there are more than one session, we use vector of sessions with mutex // if there are more than one session, we use vector of sessions with mutex
std::vector<std::shared_ptr<Session>> _sessions; std::vector<std::shared_ptr<Session>> _sessions;
// write function pointer (by default write single stream) // write function pointer (by default write single stream)
void (MultiStreamState::*_writeFunction)(std::shared_ptr<const tcp::Message>); std::atomic<WriteMethodPointer> _writeFunction;
}; };
} // namespace detail } // namespace detail