Added some locks against race conditions

This commit is contained in:
Sebastian Messmer 2015-10-05 18:54:16 +02:00
parent 78dbe6ea24
commit 810c2c5b48
3 changed files with 10 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include "ciphers/Cipher.h" #include "ciphers/Cipher.h"
#include <messmer/cpp-utils/assert/assert.h> #include <messmer/cpp-utils/assert/assert.h>
#include <mutex>
namespace blockstore { namespace blockstore {
namespace encrypted { namespace encrypted {
@ -50,6 +51,8 @@ private:
static cpputils::Data _prependKeyHeaderToData(const Key &key, cpputils::Data data); static cpputils::Data _prependKeyHeaderToData(const Key &key, cpputils::Data data);
static bool _keyHeaderIsCorrect(const Key &key, const cpputils::Data &data); static bool _keyHeaderIsCorrect(const Key &key, const cpputils::Data &data);
std::mutex _mutex;
DISALLOW_COPY_AND_ASSIGN(EncryptedBlock); DISALLOW_COPY_AND_ASSIGN(EncryptedBlock);
}; };
@ -114,6 +117,7 @@ EncryptedBlock<Cipher>::EncryptedBlock(cpputils::unique_ref<Block> baseBlock, co
template<class Cipher> template<class Cipher>
EncryptedBlock<Cipher>::~EncryptedBlock() { EncryptedBlock<Cipher>::~EncryptedBlock() {
std::unique_lock<std::mutex> lock(_mutex);
_encryptToBaseBlock(); _encryptToBaseBlock();
} }
@ -131,6 +135,7 @@ void EncryptedBlock<Cipher>::write(const void *source, uint64_t offset, uint64_t
template<class Cipher> template<class Cipher>
void EncryptedBlock<Cipher>::flush() { void EncryptedBlock<Cipher>::flush() {
std::unique_lock<std::mutex> lock(_mutex);
_encryptToBaseBlock(); _encryptToBaseBlock();
return _baseBlock->flush(); return _baseBlock->flush();
} }
@ -151,6 +156,7 @@ void EncryptedBlock<Cipher>::_encryptToBaseBlock() {
template<class Cipher> template<class Cipher>
cpputils::unique_ref<Block> EncryptedBlock<Cipher>::releaseBlock() { cpputils::unique_ref<Block> EncryptedBlock<Cipher>::releaseBlock() {
std::unique_lock<std::mutex> lock(_mutex);
_encryptToBaseBlock(); _encryptToBaseBlock();
return std::move(_baseBlock); return std::move(_baseBlock);
} }

View File

@ -91,6 +91,7 @@ void OnDiskBlock::_storeToDisk() const {
} }
void OnDiskBlock::flush() { void OnDiskBlock::flush() {
std::unique_lock<std::mutex> lock(_mutex);
if (_dataChanged) { if (_dataChanged) {
_storeToDisk(); _storeToDisk();
_dataChanged = false; _dataChanged = false;

View File

@ -8,6 +8,7 @@
#include <iostream> #include <iostream>
#include <messmer/cpp-utils/pointer/unique_ref.h> #include <messmer/cpp-utils/pointer/unique_ref.h>
#include <mutex>
namespace blockstore { namespace blockstore {
namespace ondisk { namespace ondisk {
@ -37,6 +38,8 @@ private:
void _fillDataWithZeroes(); void _fillDataWithZeroes();
void _storeToDisk() const; void _storeToDisk() const;
std::mutex _mutex;
DISALLOW_COPY_AND_ASSIGN(OnDiskBlock); DISALLOW_COPY_AND_ASSIGN(OnDiskBlock);
}; };