From ff0ba068460c7d01ed3db5cf6d23c26b7eff3e3d Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 29 Jun 2016 16:42:43 -0700 Subject: [PATCH] Add mutex to fix race condition --- src/blockstore/implementations/caching/CachingBlockStore.cpp | 4 ++++ src/blockstore/implementations/caching/CachingBlockStore.h | 1 + .../implementations/versioncounting/VersionCountingBlock.h | 3 +-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/blockstore/implementations/caching/CachingBlockStore.cpp b/src/blockstore/implementations/caching/CachingBlockStore.cpp index b9b96bef..546cacbf 100644 --- a/src/blockstore/implementations/caching/CachingBlockStore.cpp +++ b/src/blockstore/implementations/caching/CachingBlockStore.cpp @@ -13,6 +13,8 @@ using boost::optional; using cpputils::unique_ref; using cpputils::make_unique_ref; using boost::none; +using std::mutex; +using std::unique_lock; namespace blockstore { namespace caching { @@ -96,10 +98,12 @@ void CachingBlockStore::forEachBlock(std::function callback) } void CachingBlockStore::registerNewBlock(NewBlock *newBlock) { + unique_lock lock(_newBlocksMutex); _newBlocks.insert(newBlock); } void CachingBlockStore::unregisterNewBlock(NewBlock *newBlock) { + unique_lock lock(_newBlocksMutex); _newBlocks.erase(newBlock); } diff --git a/src/blockstore/implementations/caching/CachingBlockStore.h b/src/blockstore/implementations/caching/CachingBlockStore.h index ef5febe7..5b1a865a 100644 --- a/src/blockstore/implementations/caching/CachingBlockStore.h +++ b/src/blockstore/implementations/caching/CachingBlockStore.h @@ -38,6 +38,7 @@ private: cpputils::unique_ref _baseBlockStore; std::unordered_set _newBlocks; // List of all new blocks that aren't in the base store yet. Cache, 1000> _cache; + std::mutex _newBlocksMutex; DISALLOW_COPY_AND_ASSIGN(CachingBlockStore); }; diff --git a/src/blockstore/implementations/versioncounting/VersionCountingBlock.h b/src/blockstore/implementations/versioncounting/VersionCountingBlock.h index 9b41e525..3c9f929a 100644 --- a/src/blockstore/implementations/versioncounting/VersionCountingBlock.h +++ b/src/blockstore/implementations/versioncounting/VersionCountingBlock.h @@ -55,6 +55,7 @@ private: cpputils::Data _dataWithHeader; uint64_t _version; bool _dataChanged; + std::mutex _mutex; void _storeToBaseBlock(); static cpputils::Data _prependHeaderToData(uint32_t myClientId, uint64_t version, cpputils::Data data); @@ -66,8 +67,6 @@ private: // This header is prepended to blocks to allow future versions to have compatibility. static constexpr uint16_t FORMAT_VERSION_HEADER = 0; - std::mutex _mutex; - DISALLOW_COPY_AND_ASSIGN(VersionCountingBlock); public: