From c3d949ddba1e9c5ade93a629a722a782c60e94a0 Mon Sep 17 00:00:00 2001 From: vsonnier Date: Wed, 1 Jun 2016 19:51:01 +0200 Subject: [PATCH] MISC 3: Make the whole BufferType life-cycle and recycling properly guarded against concurrent access --- src/IOThread.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/IOThread.h b/src/IOThread.h index eb4e94d..9d9232b 100644 --- a/src/IOThread.h +++ b/src/IOThread.h @@ -20,6 +20,7 @@ struct map_string_less : public std::binary_function lock(m_mutex); + + BufferType* buf = nullptr; for (outputBuffersI = outputBuffers.begin(); outputBuffersI != outputBuffers.end(); outputBuffersI++) { - if (!buf && (*outputBuffersI)->getRefCount() <= 0) { + if (buf == nullptr && (*outputBuffersI)->getRefCount() <= 0) { buf = (*outputBuffersI); - (*outputBuffersI)->setRefCount(0); + buf->setRefCount(0); } else if ((*outputBuffersI)->getRefCount() <= 0) { (*outputBuffersI)->decRefCount(); } } - if (buf) { + if (buf != nullptr) { if (outputBuffers.back()->getRefCount() < -REBUFFER_GC_LIMIT) { BufferType *ref = outputBuffers.back(); outputBuffers.pop_back(); @@ -87,6 +90,7 @@ public: } void purge() { + std::lock_guard < std::mutex > lock(m_mutex); while (!outputBuffers.empty()) { BufferType *ref = outputBuffers.front(); outputBuffers.pop_front(); @@ -97,6 +101,7 @@ private: std::string bufferId; std::deque outputBuffers; typename std::deque::iterator outputBuffersI; + mutable std::mutex m_mutex; };