mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
SinkImpl: Keep enabled count rather than just boolean.
This allows e.g. multiple HTTP streams to individually call Enable() and Disable().
This commit is contained in:
@@ -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<SourceImpl> source) {
|
||||
std::lock_guard<std::mutex> 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();
|
||||
}
|
||||
|
||||
@@ -33,18 +33,14 @@ class SinkImpl {
|
||||
|
||||
void Enable() {
|
||||
std::lock_guard<std::mutex> 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<std::mutex> 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<SourceImpl> source);
|
||||
@@ -58,7 +54,7 @@ class SinkImpl {
|
||||
std::string m_name;
|
||||
mutable std::mutex m_mutex;
|
||||
std::shared_ptr<SourceImpl> m_source;
|
||||
bool m_enabled{false};
|
||||
int m_enabledCount{0};
|
||||
};
|
||||
|
||||
} // namespace cs
|
||||
|
||||
Reference in New Issue
Block a user