Skip to content

Commit

Permalink
shared mutexes for reads and locks for writes
Browse files Browse the repository at this point in the history
  • Loading branch information
ghoshkaj committed Apr 1, 2018
1 parent e57c949 commit 181c4c7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions include/engine/unpacking_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define UNPACKING_CACHE_HPP

#include <boost/optional/optional_io.hpp>
#include <boost/thread.hpp>

#include "../../third_party/compute_detail/lru_cache.hpp"
#include "util/typedefs.hpp"
Expand All @@ -17,13 +18,14 @@ class UnpackingCache
m_cache;
unsigned m_current_data_timestamp = 0;
std::mutex m_mutex;
boost::shared_mutex m_shared_access;

public:
UnpackingCache(unsigned timestamp) : m_cache(6000000), m_current_data_timestamp(timestamp){};

void Clear(unsigned new_data_timestamp)
{
std::lock_guard<std::mutex> guard(m_mutex);
boost::unique_lock<boost::shared_mutex> lock(m_shared_access);
if (m_current_data_timestamp != new_data_timestamp)
{
m_cache.clear();
Expand All @@ -33,19 +35,20 @@ class UnpackingCache

bool IsEdgeInCache(std::tuple<NodeID, NodeID, std::size_t> edge)
{
std::lock_guard<std::mutex> guard(m_mutex);
boost::shared_lock<boost::shared_mutex> lock(m_shared_access);
return m_cache.contains(edge);
}

void AddEdge(std::tuple<NodeID, NodeID, std::size_t> edge, EdgeDuration duration)
{
boost::unique_lock<boost::shared_mutex> lock(m_shared_access);
std::lock_guard<std::mutex> guard(m_mutex);
m_cache.insert(edge, duration);
}

EdgeDuration GetDuration(std::tuple<NodeID, NodeID, std::size_t> edge)
{
std::lock_guard<std::mutex> guard(m_mutex);
boost::shared_lock<boost::shared_mutex> lock(m_shared_access);
boost::optional<EdgeDuration> duration = m_cache.get(edge);
return *duration ? *duration : MAXIMAL_EDGE_DURATION;
}
Expand Down

0 comments on commit 181c4c7

Please sign in to comment.