Skip to content

Commit

Permalink
make the pending finish transaction db work with MT
Browse files Browse the repository at this point in the history
  • Loading branch information
wsong83 committed Jun 27, 2024
1 parent ec1c994 commit 7d362ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
15 changes: 5 additions & 10 deletions cache/coherence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,13 @@ template<class IPUC, bool EnMT> requires C_DERIVE<IPUC, InnerCohPortUncached<EnM
class InnerCohPortT : public IPUC
{
private:
// record the pending finish message from inner caches
// replace the single storage to a set in multi-thread sim
uint64_t addr_pending_finish;
int32_t id_pending_finish;
PendingXact<EnMT> pending_xact; // record the pending finish message from inner caches
protected:
using InnerCohPortBase::coh;
using InnerCohPortBase::outer;
using InnerCohPortBase::policy;
public:
InnerCohPortT(policy_ptr policy) : IPUC(policy), addr_pending_finish(0) {}
InnerCohPortT(policy_ptr policy) : IPUC(policy) {}
virtual ~InnerCohPortT() {}

virtual std::pair<bool, bool> probe_req(uint64_t addr, CMMetadataBase *meta, CMDataBase *data, coh_cmd_t cmd, uint64_t *delay) {
Expand All @@ -329,16 +326,14 @@ class InnerCohPortT : public IPUC

// record pending finish
virtual void finish_record(uint64_t addr, coh_cmd_t outer_cmd) {
addr_pending_finish = addr;
id_pending_finish = outer_cmd.id;
pending_xact.insert(addr, outer_cmd.id);
}

// only forward the finish message recorded by previous acquire
virtual void finish_resp(uint64_t addr, coh_cmd_t outer_cmd) {
assert(addr_pending_finish == 0 || (addr_pending_finish == addr && id_pending_finish == outer_cmd.id));
if(addr_pending_finish == addr && id_pending_finish == outer_cmd.id) {
if(pending_xact.count(addr, outer_cmd.id)) {
outer->finish_req(addr);
addr_pending_finish = 0;
pending_xact.remove(addr, outer_cmd.id);
}
}
};
Expand Down
61 changes: 61 additions & 0 deletions util/multithread.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef CM_UTIL_MULTITHREAD_HPP
#define CM_UTIL_MULTITHREAD_HPP

#include <unordered_map>
#include <cstdint>
#include <cassert>
#include <atomic>
#include <mutex>
#include <condition_variable>
Expand Down Expand Up @@ -48,4 +51,62 @@ class AtomicVar {
}
};

// a database for recoridng the pending transactions
template<bool EnMT>
class PendingXact {
std::unordered_map<uint64_t, uint64_t> db;
std::mutex mtx;
public:
PendingXact() {}
virtual ~PendingXact() {}

void insert(uint64_t addr, uint32_t id) {
std::lock_guard lk(mtx);
if(!db.count(addr)) db[addr] = 0;
assert(0ull == (db[addr] & (1ull << id)) || 0 ==
"The to be inserted <addr, id> pair has already been inserted in the database!");
db[addr] |= (1ull << id);
}

void remove(uint64_t addr, uint32_t id) {
std::lock_guard lk(mtx);
if(db.count(addr)) {
db[addr] &= ~(1ull << id);
if(db[addr] == 0) db.erase(addr);
}
}

bool count(uint64_t addr, uint32_t id) {
std::lock_guard lk(mtx);
return db.count(addr) && (db[addr] & (1ull << id));
}
};

// specialization for non-multithread env
template<>
class PendingXact<false> {

uint64_t addr;
int32_t id;

public:
PendingXact(): addr(0), id(0) {}
virtual ~PendingXact() {}

void insert(uint64_t addr, uint32_t id) {
this->addr = addr;
this->id = id;
}

void remove(uint64_t addr, uint32_t id) {
if(count(addr, id)) this->addr = 0;
}

bool count(uint64_t addr, uint32_t id) {
return this->addr == addr && this->id == id;
}
};



#endif

0 comments on commit 7d362ac

Please sign in to comment.