diff --git a/cache/coherence_multi.hpp b/cache/coherence_multi.hpp index 28973d5..786d36e 100644 --- a/cache/coherence_multi.hpp +++ b/cache/coherence_multi.hpp @@ -160,6 +160,7 @@ class OuterCohPortMultiThreadT : public OPUC bool hit = cache->hit(addr, &ai, &s, &w, Priority::probe); if(hit){ std::tie(meta, data) = cache->access_line(ai, s, w); + cache->lock_line(ai, s, w); /** It is possible that higher priority behaviors have caused the meta to change, so need check again */ if(!meta->is_valid() || meta->addr(s) != addr){ cache->unlock_line(ai, s, w); diff --git a/cache/metadata.hpp b/cache/metadata.hpp index bcad6d7..4d075e7 100644 --- a/cache/metadata.hpp +++ b/cache/metadata.hpp @@ -5,10 +5,9 @@ #include #include "util/concept_macro.hpp" #include - -#ifndef NDEBUG +#include +#include #include -#endif class CMDataBase { @@ -220,11 +219,35 @@ using MetadataDirectory = MetadataMixer; template requires C_DERIVE class MetaLock : public MT { std::mutex mtx; + +#ifndef NDEBUG + // verify no double lock or unlock + std::hash hasher; + std::atomic locked; +#endif + public: MetaLock() : MT() {} virtual ~MetaLock() {} - virtual void lock() { mtx.lock(); } - virtual void unlock() { mtx.unlock(); } + virtual void lock() { +#ifndef NDEBUG + uint64_t thread_id = hasher(std::this_thread::get_id()); + assert(locked.load() != thread_id || 0 == + "This cache line has already be locked by this thread and should not be locked by this thread again!"); +#endif + mtx.lock(); +#ifndef NDEBUG + locked = thread_id; +#endif + } + virtual void unlock() { +#ifndef NDEBUG + assert(locked.load() != 0 || 0 == + "This cache line has already be unlocked and should not be unlocked again!"); + locked = 0; +#endif + mtx.unlock(); + } }; #endif diff --git a/util/monitor.hpp b/util/monitor.hpp index c278fc9..a99e595 100644 --- a/util/monitor.hpp +++ b/util/monitor.hpp @@ -240,8 +240,8 @@ class SimpleTracerMT : public SimpleTracer { PrintPool pool; std::thread print_thread; + std::hash hasher; virtual void print(std::string& msg) { - std::hash hasher; uint16_t id = hasher(std::this_thread::get_id()); std::string msg_ext = (boost::format("thread %04x: %s") % id % msg).str(); pool.add(msg_ext); diff --git a/util/multithread.hpp b/util/multithread.hpp index 1df8f81..07fa7cb 100644 --- a/util/multithread.hpp +++ b/util/multithread.hpp @@ -108,5 +108,4 @@ class PendingXact { }; - #endif