From: adash Date: Sat, 10 Mar 2007 02:14:02 +0000 (+0000) Subject: Added pthread support for llookup and mlookup hash X-Git-Tag: preEdgeChange~666 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=15200f7c67c3f7b92d11ac8cca996a87b1f60fa6;p=IRC.git Added pthread support for llookup and mlookup hash changed cachehash to chash modified the test suite --- diff --git a/Robust/src/Runtime/DSTM/interface/clookup.c b/Robust/src/Runtime/DSTM/interface/clookup.c index f1cf8548..4fd11cbe 100644 --- a/Robust/src/Runtime/DSTM/interface/clookup.c +++ b/Robust/src/Runtime/DSTM/interface/clookup.c @@ -1,17 +1,17 @@ #include "clookup.h" -cachehashtable_t *cachehashCreate(unsigned int size, float loadfactor) { - cachehashtable_t *ctable; - cachehashlistnode_t *nodes; +chashtable_t *chashCreate(unsigned int size, float loadfactor) { + chashtable_t *ctable; + chashlistnode_t *nodes; int i; - if((ctable = calloc(1, sizeof(cachehashtable_t))) == NULL) { + if((ctable = calloc(1, sizeof(chashtable_t))) == NULL) { printf("Calloc error %s %d\n", __FILE__, __LINE__); return NULL; } // Allocate space for the hash table - if((nodes = calloc(HASH_SIZE, sizeof(cachehashlistnode_t))) == NULL) { + if((nodes = calloc(size, sizeof(chashlistnode_t))) == NULL) { printf("Calloc error %s %d\n", __FILE__, __LINE__); return NULL; } @@ -25,25 +25,25 @@ cachehashtable_t *cachehashCreate(unsigned int size, float loadfactor) { } //Finds the right bin in the hash table -unsigned int cachehashFunction(cachehashtable_t *table, unsigned int key) { +unsigned int chashFunction(chashtable_t *table, unsigned int key) { return ( key % (table->size)); } //Store objects and their pointers into hash -unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *val) { +unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val) { unsigned int newsize; int index; - cachehashlistnode_t *ptr, *node; + chashlistnode_t *ptr, *node; if(table->numelements > (table->loadfactor * table->size)) { //Resize newsize = 2 * table->size + 1; - cachehashResize(table,newsize); + chashResize(table,newsize); } ptr = table->table; table->numelements++; - index = cachehashFunction(table, key); + index = chashFunction(table, key); #ifdef DEBUG printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val); #endif @@ -51,7 +51,7 @@ unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *va ptr[index].key = key; ptr[index].val = val; } else { // Insert in the beginning of linked list - if ((node = calloc(1, sizeof(cachehashlistnode_t))) == NULL) { + if ((node = calloc(1, sizeof(chashlistnode_t))) == NULL) { printf("Calloc error %s, %d\n", __FILE__, __LINE__); return 1; } @@ -64,12 +64,12 @@ unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *va } // Search for an address for a given oid -void *cachehashSearch(cachehashtable_t *table, unsigned int key) { +void *chashSearch(chashtable_t *table, unsigned int key) { int index; - cachehashlistnode_t *ptr, *node; + chashlistnode_t *ptr, *node; ptr = table->table; - index = cachehashFunction(table, key); + index = chashFunction(table, key); node = &ptr[index]; while(node != NULL) { if(node->key == key) { @@ -80,22 +80,22 @@ void *cachehashSearch(cachehashtable_t *table, unsigned int key) { return NULL; } -unsigned int cachehashRemove(cachehashtable_t *table, unsigned int key) { +unsigned int chashRemove(chashtable_t *table, unsigned int key) { int index; - cachehashlistnode_t *curr, *prev; - cachehashlistnode_t *ptr, *node; + chashlistnode_t *curr, *prev; + chashlistnode_t *ptr, *node; ptr = table->table; - index = cachehashFunction(table,key); + index = chashFunction(table,key); curr = &ptr[index]; for (; curr != NULL; curr = curr->next) { if (curr->key == key) { // Find a match in the hash table table->numelements--; // Decrement the number of elements in the global hashtable - if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of cachehashlistnode_t + if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of chashlistnode_t curr->key = 0; curr->val = NULL; - } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of cachehashlistnode_t connected + } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of chashlistnode_t connected curr->key = curr->next->key; curr->val = curr->next->val; node = curr->next; @@ -112,17 +112,17 @@ unsigned int cachehashRemove(cachehashtable_t *table, unsigned int key) { return 1; } -unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize) { - cachehashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next cachehashlistnodes in a linked list +unsigned int chashResize(chashtable_t *table, unsigned int newsize) { + chashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list unsigned int oldsize; - int isfirst; // Keeps track of the first element in the cachehashlistnode_t for each bin in hashtable + int isfirst; // Keeps track of the first element in the chashlistnode_t for each bin in hashtable int i,index; - cachehashlistnode_t *newnode; + chashlistnode_t *newnode; ptr = table->table; oldsize = table->size; - if((node = calloc(newsize, sizeof(cachehashlistnode_t))) == NULL) { + if((node = calloc(newsize, sizeof(chashlistnode_t))) == NULL) { printf("Calloc error %s %d\n", __FILE__, __LINE__); return 1; } @@ -140,7 +140,7 @@ unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize) { } next = curr->next; - index = cachehashFunction(table, curr->key); + index = chashFunction(table, curr->key); #ifdef DEBUG printf("DEBUG(resize) -> index = %d, key = %d, val = %x\n", index, curr->key, curr->val); #endif @@ -150,7 +150,7 @@ unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize) { table->table[index].val = curr->val; table->numelements++; }else { - if((newnode = calloc(1, sizeof(cachehashlistnode_t))) == NULL) { + if((newnode = calloc(1, sizeof(chashlistnode_t))) == NULL) { printf("Calloc error %s, %d\n", __FILE__, __LINE__); return 1; } @@ -161,7 +161,7 @@ unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize) { table->numelements++; } - //free the linked list of cachehashlistnode_t if not the first element in the hash table + //free the linked list of chashlistnode_t if not the first element in the hash table if (isfirst != 1) { free(curr); } @@ -176,9 +176,9 @@ unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize) { } //Delete the entire hash table -void cachehashDelete(cachehashtable_t *table) { +void chashDelete(chashtable_t *table) { int i, isFirst; - cachehashlistnode_t *ptr, *curr, *next; + chashlistnode_t *ptr, *curr, *next; ptr = table->table; for(i=0 ; isize ; i++) { @@ -196,4 +196,5 @@ void cachehashDelete(cachehashtable_t *table) { free(ptr); free(table); + table = NULL; } diff --git a/Robust/src/Runtime/DSTM/interface/clookup.h b/Robust/src/Runtime/DSTM/interface/clookup.h index 9425102f..8b8df2cf 100644 --- a/Robust/src/Runtime/DSTM/interface/clookup.h +++ b/Robust/src/Runtime/DSTM/interface/clookup.h @@ -7,27 +7,27 @@ #define LOADFACTOR 0.75 #define HASH_SIZE 100 -typedef struct cachehashlistnode { +typedef struct chashlistnode { unsigned int key; void *val; //this can be cast to another type or used to point to a larger structure - struct cachehashlistnode *next; -} cachehashlistnode_t; + struct chashlistnode *next; +} chashlistnode_t; typedef struct cashehashtable { - cachehashlistnode_t *table; // points to beginning of hash table + chashlistnode_t *table; // points to beginning of hash table unsigned int size; unsigned int numelements; float loadfactor; -} cachehashtable_t; +} chashtable_t; /* Prototypes for hash*/ -cachehashtable_t *cachehashCreate(unsigned int size, float loadfactor); -unsigned int cachehashFunction(cachehashtable_t *table, unsigned int key); -unsigned int cachehashInsert(cachehashtable_t *table, unsigned int key, void *val); -void *cachehashSearch(cachehashtable_t *table, unsigned int key); //returns val, NULL if not found -unsigned int cachehashRemove(cachehashtable_t *table, unsigned int key); //returns -1 if not found -unsigned int cachehashResize(cachehashtable_t *table, unsigned int newsize); -void cachehashDelete(cachehashtable_t *table); +chashtable_t *chashCreate(unsigned int size, float loadfactor); +unsigned int chashFunction(chashtable_t *table, unsigned int key); +unsigned int chashInsert(chashtable_t *table, unsigned int key, void *val); +void *chashSearch(chashtable_t *table, unsigned int key); //returns val, NULL if not found +unsigned int chashRemove(chashtable_t *table, unsigned int key); //returns -1 if not found +unsigned int chashResize(chashtable_t *table, unsigned int newsize); +void chashDelete(chashtable_t *table); /* end hash */ #endif diff --git a/Robust/src/Runtime/DSTM/interface/llookup.c b/Robust/src/Runtime/DSTM/interface/llookup.c index 2bcbf75b..02469a14 100644 --- a/Robust/src/Runtime/DSTM/interface/llookup.c +++ b/Robust/src/Runtime/DSTM/interface/llookup.c @@ -4,18 +4,20 @@ llookup hash is an array of lhashlistnode_t oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty => the OID's can be any unsigned int except 0 + + Uses pthreads. compile using -lpthread option ***************************************************************************************************/ #include "llookup.h" lhashtable_t llookup; //Global Hash table -// Creates a hash table with default size HASH_SIZE and an array of lhashlistnode_t +// Creates a hash table with size and an array of lhashlistnode_t unsigned int lhashCreate(unsigned int size, float loadfactor) { lhashlistnode_t *nodes; int i; // Allocate space for the hash table - if((nodes = calloc(HASH_SIZE, sizeof(lhashlistnode_t))) == NULL) { + if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) { printf("Calloc error %s %d\n", __FILE__, __LINE__); return 1; } @@ -24,6 +26,8 @@ unsigned int lhashCreate(unsigned int size, float loadfactor) { llookup.size = size; llookup.numelements = 0; // Initial number of elements in the hash llookup.loadfactor = loadfactor; + //Initialize the pthread_mutex variable + pthread_mutex_init(&llookup.locktable, NULL); return 0; } @@ -41,8 +45,11 @@ unsigned int lhashInsert(unsigned int oid, unsigned int mid) { if (llookup.numelements > (llookup.loadfactor * llookup.size)) { //Resize Table newsize = 2 * llookup.size + 1; + pthread_mutex_lock(&llookup.locktable); lhashResize(newsize); + pthread_mutex_unlock(&llookup.locktable); } + ptr = llookup.table; llookup.numelements++; @@ -50,6 +57,7 @@ unsigned int lhashInsert(unsigned int oid, unsigned int mid) { #ifdef DEBUG printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index); #endif + pthread_mutex_lock(&llookup.locktable); if(ptr[index].next == NULL && ptr[index].oid == 0) { // Insert at the first position in the hashtable ptr[index].oid = oid; ptr[index].mid = mid; @@ -63,6 +71,8 @@ unsigned int lhashInsert(unsigned int oid, unsigned int mid) { node->next = ptr[index].next; ptr[index].next = node; } + + pthread_mutex_unlock(&llookup.locktable); return 0; } @@ -74,12 +84,14 @@ unsigned int lhashSearch(unsigned int oid) { ptr = llookup.table; // Address of the beginning of hash table index = lhashFunction(oid); node = &ptr[index]; + pthread_mutex_lock(&llookup.locktable); while(node != NULL) { if(node->oid == oid) { return node->mid; } node = node->next; } + pthread_mutex_unlock(&llookup.locktable); return 0; } @@ -92,7 +104,8 @@ unsigned int lhashRemove(unsigned int oid) { ptr = llookup.table; index = lhashFunction(oid); curr = &ptr[index]; - + + pthread_mutex_lock(&llookup.locktable); for (; curr != NULL; curr = curr->next) { if (curr->oid == oid) { // Find a match in the hash table llookup.numelements--; // Decrement the number of elements in the global hashtable @@ -113,6 +126,7 @@ unsigned int lhashRemove(unsigned int oid) { } prev = curr; } + pthread_mutex_unlock(&llookup.locktable); return 1; } diff --git a/Robust/src/Runtime/DSTM/interface/llookup.h b/Robust/src/Runtime/DSTM/interface/llookup.h index 719e68cb..7a70b9bd 100644 --- a/Robust/src/Runtime/DSTM/interface/llookup.h +++ b/Robust/src/Runtime/DSTM/interface/llookup.h @@ -3,6 +3,7 @@ #include #include +#include #define LOADFACTOR 0.75 #define HASH_SIZE 100 @@ -18,6 +19,7 @@ typedef struct lhashtable { unsigned int size; unsigned int numelements; float loadfactor; + pthread_mutex_t locktable; } lhashtable_t; unsigned int lhashCreate(unsigned int size, float loadfactor);// returns 0 for success and 0 for failure diff --git a/Robust/src/Runtime/DSTM/interface/mlookup.c b/Robust/src/Runtime/DSTM/interface/mlookup.c index 56e799ce..44317e2a 100644 --- a/Robust/src/Runtime/DSTM/interface/mlookup.c +++ b/Robust/src/Runtime/DSTM/interface/mlookup.c @@ -2,12 +2,13 @@ mhashtable_t mlookup; //Global hash table +// Creates a machine lookup table with size =" size" unsigned int mhashCreate(unsigned int size, float loadfactor) { mhashlistnode_t *nodes; int i; // Allocate space for the hash table - if((nodes = calloc(HASH_SIZE, sizeof(mhashlistnode_t))) == NULL) { + if((nodes = calloc(size, sizeof(mhashlistnode_t))) == NULL) { printf("Calloc error %s %d\n", __FILE__, __LINE__); return 1; } @@ -16,6 +17,8 @@ unsigned int mhashCreate(unsigned int size, float loadfactor) { mlookup.size = size; mlookup.numelements = 0; // Initial number of elements in the hash mlookup.loadfactor = loadfactor; + //Initialize the pthread_mutex variable + pthread_mutex_init(&mlookup.locktable, NULL); return 0; } @@ -33,7 +36,9 @@ unsigned int mhashInsert(unsigned int key, void *val) { if (mlookup.numelements > (mlookup.loadfactor * mlookup.size)) { //Resize Table newsize = 2 * mlookup.size + 1; + pthread_mutex_lock(&mlookup.locktable); mhashResize(newsize); + pthread_mutex_unlock(&mlookup.locktable); } ptr = mlookup.table; mlookup.numelements++; @@ -42,6 +47,7 @@ unsigned int mhashInsert(unsigned int key, void *val) { #ifdef DEBUG printf("DEBUG -> index = %d, key = %d, val = %x\n", index, key, val); #endif + pthread_mutex_lock(&mlookup.locktable); if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable ptr[index].key = key; ptr[index].val = val; @@ -55,6 +61,7 @@ unsigned int mhashInsert(unsigned int key, void *val) { node->next = ptr[index].next; ptr[index].next = node; } + pthread_mutex_unlock(&mlookup.locktable); return 0; } @@ -66,12 +73,14 @@ void *mhashSearch(unsigned int key) { ptr = mlookup.table; // Address of the beginning of hash table index = mhashFunction(key); node = &ptr[index]; + pthread_mutex_lock(&mlookup.locktable); while(node != NULL) { if(node->key == key) { return node->val; } node = node->next; } + pthread_mutex_unlock(&mlookup.locktable); return NULL; } @@ -85,6 +94,7 @@ unsigned int mhashRemove(unsigned int key) { index = mhashFunction(key); curr = &ptr[index]; + pthread_mutex_lock(&mlookup.locktable); for (; curr != NULL; curr = curr->next) { if (curr->key == key) { // Find a match in the hash table mlookup.numelements--; // Decrement the number of elements in the global hashtable @@ -105,6 +115,7 @@ unsigned int mhashRemove(unsigned int key) { } prev = curr; } + pthread_mutex_unlock(&mlookup.locktable); return 1; } diff --git a/Robust/src/Runtime/DSTM/interface/mlookup.h b/Robust/src/Runtime/DSTM/interface/mlookup.h index 9e45b880..f3b0159d 100644 --- a/Robust/src/Runtime/DSTM/interface/mlookup.h +++ b/Robust/src/Runtime/DSTM/interface/mlookup.h @@ -3,6 +3,7 @@ #include #include +#include #define LOADFACTOR 0.75 #define HASH_SIZE 100 @@ -18,6 +19,7 @@ typedef struct mhashtable { unsigned int size; unsigned int numelements; float loadfactor; + pthread_mutex_t locktable; } mhashtable_t; unsigned int mhashCreate(unsigned int size, float loadfactor); diff --git a/Robust/src/Runtime/DSTM/interface/testclookup.c b/Robust/src/Runtime/DSTM/interface/testclookup.c index e958e806..e89074b7 100644 --- a/Robust/src/Runtime/DSTM/interface/testclookup.c +++ b/Robust/src/Runtime/DSTM/interface/testclookup.c @@ -5,54 +5,54 @@ main() { int i; void *val; - cachehashtable_t *ctable; + chashtable_t *ctable; - if (( ctable = cachehashCreate(20, 0.50)) == NULL) { - printf("cachehashCreate error\n"); //creates hashtable + if (( ctable = chashCreate(1000, 0.40)) == NULL) { + printf("chashCreate error\n"); //creates hashtable } - for (i = 1; i <= 10; i++) { // Checks the insert() and resize() - if (cachehashInsert(ctable, 10*i, &i) == 1) - printf("cachehashInsert error\n"); + for (i = 1; i <= 2000; i++) { // Checks the insert() and resize() + if (chashInsert(ctable, 10*i, &i) == 1) + printf("chashInsert error\n"); } - i = cachehashRemove(ctable, 10);//Delete first element in the hashtable + i = chashRemove(ctable, 10);//Delete first element in the hashtable if(i == 1) - printf("cachehashRemove error "); + printf("chashRemove error "); - for (i = 1; i <= 10; i++) { // Check if it can search for all keys in hash table - val = cachehashSearch(ctable, 10*i); + for (i = 1; i <= 2000; i++) { // Check if it can search for all keys in hash table + val = chashSearch(ctable, 10*i); if (val != &i) - printf("cachehashSearch error - val = %d\n", val); + printf("chashSearch error - val = %d\n", val); else - printf("cachehashSearch key = %d val = %x\n",10*i, val); + printf("chashSearch key = %d val = %x\n",10*i, val); } - i = cachehashRemove(ctable, 30); + i = chashRemove(ctable, 30); if(i == 1) - printf("cachehashRemove error\n "); - i = cachehashRemove(ctable, 40); + printf("chashRemove error\n "); + i = chashRemove(ctable, 40); if(i == 1) - printf("cachehashRemove error\n "); - i = cachehashRemove(ctable, 80); + printf("chashRemove error\n "); + i = chashRemove(ctable, 80); if(i == 1) - printf("cachehashRemove error\n "); - i = cachehashRemove(ctable, 100); + printf("chashRemove error\n "); + i = chashRemove(ctable, 100); if(i == 1) - printf("cachehashRemove error\n "); - i = cachehashRemove(ctable, 90); + printf("chashRemove error\n "); + i = chashRemove(ctable, 90); if(i == 1) - printf("cachehashRemove error\n "); + printf("chashRemove error\n "); - for (i = 1; i <= 10; i++) { //Prints all left over elements inside hash after deletion and prints error if element not found in hash - val = cachehashSearch(ctable, 10*i); + for (i = 1; i <= 2000; i++) { //Prints all left over elements inside hash after deletion and prints error if element not found in hash + val = chashSearch(ctable, 10*i); if (val != &i) - printf("cachehashSearch error - val = %d\n", val); + printf("chashSearch error - val = %d\n", val); else - printf("cachehashSearch key = %d val = %x\n",10*i, val); + printf("chashSearch key = %d val = %x\n",10*i, val); } printf("The total number of elements in table : %d\n", ctable->numelements); - + + chashDelete(ctable); } -