Skip to content

Commit

Permalink
Feature/single pointer cache ref (#2364)
Browse files Browse the repository at this point in the history
* remove redundant mutex

Signed-off-by: iceseer <iceseer@gmail.com>
  • Loading branch information
iceseer committed Jun 27, 2022
1 parent 01c8119 commit e1129f4
Showing 1 changed file with 29 additions and 51 deletions.
80 changes: 29 additions & 51 deletions libs/cache/single_pointer_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,46 @@
#include <memory>
#include <mutex>

namespace iroha {
namespace cache {

namespace iroha::cache {

/**
* Thread-safely stores and returns shared pointer to an element of template
* type
*/
template <typename DataType>
struct SinglePointerCache final {
/**
* Thread-safely stores and returns shared pointer to an element of template
* type
* Pointer to data type
*/
template <typename DataType>
class SinglePointerCache {
public:
/**
* Pointer to data type
*/
using DataPointer = std::shared_ptr<std::decay_t<DataType>>;

/**
* Insert data to the cache
* @param pointer to the data to be inserted
*/
void insert(DataPointer data);

/**
* Get data from the cache
* @return pointer to the stored data
*/
DataPointer get() const;

/**
* Delete data inside the cache
*/
void release();

private:
DataPointer stored_data_;

mutable std::mutex mutex_;
};

template <typename DataType>
void SinglePointerCache<DataType>::insert(
SinglePointerCache::DataPointer data) {
std::lock_guard<std::mutex> lock(mutex_);
using DataPointer = std::shared_ptr<std::decay_t<DataType>>;

/**
* Insert data to the cache
* @param pointer to the data to be inserted
*/
void insert(DataPointer data) {
stored_data_ = std::move(data);
}

template <typename DataType>
typename SinglePointerCache<DataType>::DataPointer
SinglePointerCache<DataType>::get() const {
std::lock_guard<std::mutex> lock(mutex_);

/**
* Get data from the cache
* @return pointer to the stored data
*/
DataPointer get() const {
return stored_data_;
}

template <typename DataType>
void SinglePointerCache<DataType>::release() {
std::lock_guard<std::mutex> lock(mutex_);

/**
* Delete data inside the cache
*/
void release() {
stored_data_.reset();
}

} // namespace cache
} // namespace iroha
private:
DataPointer stored_data_;
};

} // namespace iroha::cache

#endif // IROHA_SINGLE_POINTER_CACHE_HPP

0 comments on commit e1129f4

Please sign in to comment.