Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the pending finish transaction db work with MT #101

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading