From 7f88bd15d11ffc7dbc8bf4303ce05387573e8660 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 13 Oct 2016 00:13:18 -0700 Subject: [PATCH] SinkImpl: Keep enabled count rather than just boolean. This allows e.g. multiple HTTP streams to individually call Enable() and Disable(). --- src/SinkImpl.cpp | 7 +++---- src/SinkImpl.h | 14 +++++--------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/SinkImpl.cpp b/src/SinkImpl.cpp index a48feb1e47..7104a6d390 100644 --- a/src/SinkImpl.cpp +++ b/src/SinkImpl.cpp @@ -15,19 +15,18 @@ SinkImpl::SinkImpl(llvm::StringRef name) : m_name{name} {} SinkImpl::~SinkImpl() { if (m_source) { - if (m_enabled) m_source->DisableSink(); + if (m_enabledCount > 0) m_source->DisableSink(); m_source->RemoveSink(); } } void SinkImpl::SetSource(std::shared_ptr source) { std::lock_guard lock(m_mutex); - bool was_enabled = m_enabled; if (m_source) { - if (m_enabled) m_source->DisableSink(); + if (m_enabledCount > 0) m_source->DisableSink(); m_source->RemoveSink(); } m_source = source; m_source->AddSink(); - if (was_enabled) m_source->EnableSink(); + if (m_enabledCount > 0) m_source->EnableSink(); } diff --git a/src/SinkImpl.h b/src/SinkImpl.h index 716be983cd..95da265068 100644 --- a/src/SinkImpl.h +++ b/src/SinkImpl.h @@ -33,18 +33,14 @@ class SinkImpl { void Enable() { std::lock_guard lock(m_mutex); - if (!m_enabled) { - m_enabled = true; - if (m_source) m_source->EnableSink(); - } + if (m_enabledCount == 0 && m_source) m_source->EnableSink(); + ++m_enabledCount; } void Disable() { std::lock_guard lock(m_mutex); - if (m_enabled) { - m_enabled = false; - if (m_source) m_source->DisableSink(); - } + --m_enabledCount; + if (m_enabledCount == 0 && m_source) m_source->DisableSink(); } void SetSource(std::shared_ptr source); @@ -58,7 +54,7 @@ class SinkImpl { std::string m_name; mutable std::mutex m_mutex; std::shared_ptr m_source; - bool m_enabled{false}; + int m_enabledCount{0}; }; } // namespace cs