Skip to content

Commit

Permalink
now compiles
Browse files Browse the repository at this point in the history
fixed releaseAccess, allocate, deAlloc
  • Loading branch information
iNomaD committed Dec 26, 2017
1 parent 4f4a787 commit 9bd81bd
Showing 1 changed file with 84 additions and 66 deletions.
150 changes: 84 additions & 66 deletions cds/container/wf_hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,88 +94,33 @@ class WfHashtable
};

int P;
Hashtable** H; // 1..2P
std::atomic<Hashtable*>* H; // 1..2P
int currInd; // 1..2P = index of the currently valid hashtable
int* busy; // 1..2P = number of processes that are using a hashtable
Hashtable* next; // 1..2P = next hashtable to which the contents of hashtable H[i] is being copied
int* prot; // 1..2P = is used to guard the variables busy[i], next[i] and H[i]
// against being reused for a new table, before all processes have discarded these

public:
WfHashtable(int P){
this->P = P;
this->H = new Hashtable[2*P];
this->busy = new int[2*P];
this->prot = new int[2*P];
};

~WfHashtable(){
for(int i=0; i<2*P; ++i){
delete H[i];
}
delete H;
delete busy;
delete prot;
}

WfHashtableProcess* getProcess(){
WfHashtableProcess* process = new WfHashtableProcess(this);
return process;
}

template <typename KEY, typename T>
class WfHashtableProcess {
protected
protected:
WfHashtable* wh;
int index; // 1..2P

public:

WfHashtableProcess(WfHashtable* wh){
this->wh = wh;
getAccess();
}

~WfHashtableProcess(){}

// ----------- ACCESS METHODS -----------

void getAccess() {
while (true) {
index = wh->currInd;
wh->prot[index] ++;
if (index == wh->currInd) {
wh->busy[index]++;
if (index == wh->currInd) {
return;
}
else {
releaseAccess(index);
}
}
else {
wh->prot[index]--;
}
}
}

void releaseAccess(int i) {
Hashtable* h;
h = wh->H[i];
wh->busy[i]--;
if (h != 0 && wh->busy[i] == 0) {
if (atomic_compare_exchange_strong(wh->H[i], h, 0)) { // ATOMIC
deAlloc(i);//change h
}
}
wh->prot[i]--;
~WfHashtableProcess(){
releaseAccess(index);
}

// ----------- HASHTABLE METHODS -----------

int key(int a, int l, int n) {
return a % l;
// ?????? ????? Inc(a,l,n-1)
}

T* find(int a) {
EValue<KEY,T> r;
int n, l, k;
Expand Down Expand Up @@ -312,16 +257,64 @@ class WfHashtable
h.occ++;
}
}

protected:

// ----------- ACCESS METHODS -----------

void getAccess() {
while (true) {
index = wh->currInd;
wh->prot[index] ++;
if (index == wh->currInd) {
wh->busy[index]++;
if (index == wh->currInd) {
return;
}
else {
releaseAccess(index);
}
}
else {
wh->prot[index]--;
}
}
}

void releaseAccess(int i) {
Hashtable* h;
h = wh->H[i];
wh->busy[i]--;
if (h != NULL && wh->busy[i] == 0) {
Hashtable* null_ptr = NULL;
if (std::atomic_compare_exchange_strong(&wh->H[i], &h, null_ptr)) {
deAlloc(i);
}
}
wh->prot[i]--;
}

int key(int a, int l, int n) {
return a % l;
// ?????? ????? Inc(a,l,n-1)
}

// ----------- HEAP methods -----------
protected:
Hashtable* allocate(int i,int s, int b) {

return atomic_exchange(H[i], new Hashtable(s, b));//atomic

void allocate(int i, int s, int b) {
Hashtable* tmp = new Hashtable(s, b)
std::atomic_exchange(&wh->H[i], tmp);
if (wh->H[i] != tmp) delete tmp;
}

void deAlloc(int h) {
atomic_exchange(H[h], NULL);//atomic
Hashtable* tmp = wh->H[h];
if (tmp != NULL) {
atomic_exchange(&wh->H[h], NULL);
if (tmp != NULL) {
delete tmp;
}
}
}

void newTable() {
Expand Down Expand Up @@ -430,6 +423,31 @@ class WfHashtable
}
};

// ----------- WfHashtable API -----------

typedef WfHashtableProcess<KEY, T> process;

WfHashtable(int P) {
this->P = P;
this->H = new std::atomic<Hashtable*>[2 * P];
this->busy = new int[2 * P];
this->prot = new int[2 * P];
};

~WfHashtable() {
for (int i = 0; i<2 * P; ++i) {
delete H[i];
}
delete H;
delete busy;
delete prot;
}

WfHashtableProcess<KEY, T>* getProcess() {
WfHashtableProcess<KEY, T>* process = new WfHashtableProcess<KEY, T>(this);
return process;
}

};
}}

Expand Down

0 comments on commit 9bd81bd

Please sign in to comment.