libcryfs/ParallelAccessStore.h

199 lines
8.1 KiB
C
Raw Normal View History

2015-04-16 14:52:06 +02:00
#ifndef MESSMER_PARALLELACCESSSTORE_IMPLEMENTATIONS_PARALLELACCESS_PARALLELACCESSSTORE_H_
#define MESSMER_PARALLELACCESSSTORE_IMPLEMENTATIONS_PARALLELACCESS_PARALLELACCESSSTORE_H_
#include <mutex>
#include <memory>
#include <map>
2015-04-17 17:39:07 +02:00
#include <unordered_map>
2015-04-16 14:52:06 +02:00
#include <future>
#include <cassert>
#include <type_traits>
#include <messmer/cpp-utils/macros.h>
#include "ParallelAccessBaseStore.h"
#include <messmer/cpp-utils/assert/assert.h>
2015-04-16 14:52:06 +02:00
//TODO Refactor
//TODO Test cases
namespace parallelaccessstore {
template<class Resource, class ResourceRef, class Key>
class ParallelAccessStore {
public:
2015-06-26 13:44:12 +02:00
explicit ParallelAccessStore(cpputils::unique_ref<ParallelAccessBaseStore<Resource, Key>> baseStore);
~ParallelAccessStore() {
ASSERT(_openResources.size() == 0, "Still resources open when trying to destruct");
ASSERT(_resourcesToRemove.size() == 0, "Still resources to remove when trying to destruct");
};
2015-04-16 14:52:06 +02:00
class ResourceRefBase {
public:
//TODO Better way to initialize
ResourceRefBase(): _parallelAccessStore(nullptr), _key(Key::Null()) {}
void init(ParallelAccessStore *parallelAccessStore, const Key &key) {
_parallelAccessStore = parallelAccessStore;
2015-04-16 14:52:06 +02:00
_key = key;
}
virtual ~ResourceRefBase() {
_parallelAccessStore->release(_key);
2015-04-16 14:52:06 +02:00
}
private:
ParallelAccessStore *_parallelAccessStore;
2015-04-16 14:52:06 +02:00
//TODO We're storing Key twice (here and in the base resource). Rather use getKey() on the base resource if possible somehow.
Key _key;
};
2015-10-14 15:01:52 +02:00
bool isOpened(const Key &key) const;
2015-06-26 13:44:12 +02:00
cpputils::unique_ref<ResourceRef> add(const Key &key, cpputils::unique_ref<Resource> resource);
template<class ActualResourceRef>
cpputils::unique_ref<ActualResourceRef> add(const Key &key, cpputils::unique_ref<Resource> resource, std::function<cpputils::unique_ref<ActualResourceRef>(Resource*)> createResourceRef);
2015-06-26 13:44:12 +02:00
boost::optional<cpputils::unique_ref<ResourceRef>> load(const Key &key);
boost::optional<cpputils::unique_ref<ResourceRef>> load(const Key &key, std::function<cpputils::unique_ref<ResourceRef>(Resource*)> createResourceRef);
2015-06-26 13:44:12 +02:00
void remove(const Key &key, cpputils::unique_ref<ResourceRef> block);
2015-04-16 14:52:06 +02:00
private:
class OpenResource {
public:
2015-06-26 13:44:12 +02:00
OpenResource(cpputils::unique_ref<Resource> resource): _resource(std::move(resource)), _refCount(0) {}
2015-04-16 14:52:06 +02:00
Resource *getReference() {
++_refCount;
return _resource.get();
}
void releaseReference() {
--_refCount;
}
bool refCountIsZero() const {
return 0 == _refCount;
}
2015-06-26 13:44:12 +02:00
cpputils::unique_ref<Resource> moveResourceOut() {
2015-04-16 14:52:06 +02:00
return std::move(_resource);
}
private:
2015-06-26 13:44:12 +02:00
cpputils::unique_ref<Resource> _resource;
2015-04-16 14:52:06 +02:00
uint32_t _refCount;
};
2015-10-14 15:01:52 +02:00
mutable std::mutex _mutex;
2015-06-26 13:44:12 +02:00
cpputils::unique_ref<ParallelAccessBaseStore<Resource, Key>> _baseStore;
2015-04-16 14:52:06 +02:00
2015-04-17 17:39:07 +02:00
std::unordered_map<Key, OpenResource> _openResources;
2015-06-26 13:44:12 +02:00
std::map<Key, std::promise<cpputils::unique_ref<Resource>>> _resourcesToRemove;
2015-04-16 14:52:06 +02:00
template<class ActualResourceRef>
cpputils::unique_ref<ActualResourceRef> _add(const Key &key, cpputils::unique_ref<Resource> resource, std::function<cpputils::unique_ref<ActualResourceRef>(Resource*)> createResourceRef);
2015-04-16 14:52:06 +02:00
void release(const Key &key);
friend class CachedResource;
DISALLOW_COPY_AND_ASSIGN(ParallelAccessStore);
};
template<class Resource, class ResourceRef, class Key>
2015-06-26 13:44:12 +02:00
ParallelAccessStore<Resource, ResourceRef, Key>::ParallelAccessStore(cpputils::unique_ref<ParallelAccessBaseStore<Resource, Key>> baseStore)
2015-04-16 14:52:06 +02:00
: _mutex(),
_baseStore(std::move(baseStore)),
_openResources(),
_resourcesToRemove() {
static_assert(std::is_base_of<ResourceRefBase, ResourceRef>::value, "ResourceRef must inherit from ResourceRefBase");
}
2015-10-14 15:01:52 +02:00
template<class Resource, class ResourceRef, class Key>
bool ParallelAccessStore<Resource, ResourceRef, Key>::isOpened(const Key &key) const {
std::lock_guard<std::mutex> lock(_mutex);
return _openResources.find(key) != _openResources.end();
};
2015-04-16 14:52:06 +02:00
template<class Resource, class ResourceRef, class Key>
2015-06-26 13:44:12 +02:00
cpputils::unique_ref<ResourceRef> ParallelAccessStore<Resource, ResourceRef, Key>::add(const Key &key, cpputils::unique_ref<Resource> resource) {
return add<ResourceRef>(key, std::move(resource), [] (Resource *resource) {
return cpputils::make_unique_ref<ResourceRef>(resource);
});
2015-04-16 14:52:06 +02:00
}
template<class Resource, class ResourceRef, class Key>
template<class ActualResourceRef>
cpputils::unique_ref<ActualResourceRef> ParallelAccessStore<Resource, ResourceRef, Key>::add(const Key &key, cpputils::unique_ref<Resource> resource, std::function<cpputils::unique_ref<ActualResourceRef>(Resource*)> createResourceRef) {
static_assert(std::is_base_of<ResourceRef, ActualResourceRef>::value, "Wrong ResourceRef type");
std::lock_guard<std::mutex> lock(_mutex);
return _add<ActualResourceRef>(key, std::move(resource), createResourceRef);
2015-04-16 14:52:06 +02:00
}
template<class Resource, class ResourceRef, class Key>
template<class ActualResourceRef>
cpputils::unique_ref<ActualResourceRef> ParallelAccessStore<Resource, ResourceRef, Key>::_add(const Key &key, cpputils::unique_ref<Resource> resource, std::function<cpputils::unique_ref<ActualResourceRef>(Resource*)> createResourceRef) {
static_assert(std::is_base_of<ResourceRef, ActualResourceRef>::value, "Wrong ResourceRef type");
auto insertResult = _openResources.emplace(key, std::move(resource));
2015-10-14 15:01:52 +02:00
ASSERT(true == insertResult.second, "Inserting failed. Already exists.");
auto resourceRef = createResourceRef(insertResult.first->second.getReference());
2015-04-16 14:52:06 +02:00
resourceRef->init(this, key);
return resourceRef;
2015-04-16 14:52:06 +02:00
}
template<class Resource, class ResourceRef, class Key>
2015-06-26 13:44:12 +02:00
boost::optional<cpputils::unique_ref<ResourceRef>> ParallelAccessStore<Resource, ResourceRef, Key>::load(const Key &key) {
return load(key, [] (Resource *res) {
return cpputils::make_unique_ref<ResourceRef>(res);
});
};
template<class Resource, class ResourceRef, class Key>
boost::optional<cpputils::unique_ref<ResourceRef>> ParallelAccessStore<Resource, ResourceRef, Key>::load(const Key &key, std::function<cpputils::unique_ref<ResourceRef>(Resource*)> createResourceRef) {
2015-10-14 15:01:52 +02:00
//TODO This lock doesn't allow loading different blocks in parallel. Can we only lock the requested key?
2015-04-16 14:52:06 +02:00
std::lock_guard<std::mutex> lock(_mutex);
auto found = _openResources.find(key);
if (found == _openResources.end()) {
2015-06-26 13:44:12 +02:00
auto resource = _baseStore->loadFromBaseStore(key);
if (resource == boost::none) {
return boost::none;
}
return _add(key, std::move(*resource), createResourceRef);
2015-04-16 14:52:06 +02:00
} else {
auto resourceRef = createResourceRef(found->second.getReference());
resourceRef->init(this, key);
return std::move(resourceRef);
2015-04-16 14:52:06 +02:00
}
}
template<class Resource, class ResourceRef, class Key>
2015-06-26 13:44:12 +02:00
void ParallelAccessStore<Resource, ResourceRef, Key>::remove(const Key &key, cpputils::unique_ref<ResourceRef> resource) {
2015-10-14 15:01:52 +02:00
std::future<cpputils::unique_ref<Resource>> resourceToRemoveFuture;
{
std::lock_guard <std::mutex> lock(_mutex); // TODO Lock needed for _resourcesToRemove?
auto insertResult = _resourcesToRemove.emplace(key, std::promise < cpputils::unique_ref < Resource >> ());
ASSERT(true == insertResult.second, "Inserting failed");
resourceToRemoveFuture = insertResult.first->second.get_future();
}
cpputils::destruct(std::move(resource));
2015-04-16 14:52:06 +02:00
//Wait for last resource user to release it
2015-10-14 15:01:52 +02:00
auto resourceToRemove = resourceToRemoveFuture.get();
std::lock_guard<std::mutex> lock(_mutex); // TODO Just added this as a precaution on a whim, but I seriously need to rethink locking here.
2015-04-16 14:52:06 +02:00
_resourcesToRemove.erase(key); //TODO Is this erase causing a race condition?
_baseStore->removeFromBaseStore(std::move(resourceToRemove));
}
template<class Resource, class ResourceRef, class Key>
void ParallelAccessStore<Resource, ResourceRef, Key>::release(const Key &key) {
std::lock_guard<std::mutex> lock(_mutex);
auto found = _openResources.find(key);
ASSERT(found != _openResources.end(), "Didn't find key");
2015-04-16 14:52:06 +02:00
found->second.releaseReference();
if (found->second.refCountIsZero()) {
auto foundToRemove = _resourcesToRemove.find(key);
if (foundToRemove != _resourcesToRemove.end()) {
foundToRemove->second.set_value(found->second.moveResourceOut());
}
_openResources.erase(found);
}
}
}
#endif