Added pthread support for llookup and mlookup hash
[IRC.git] / Robust / src / Runtime / DSTM / interface / llookup.c
1 /************************************************************************************************
2   IMP NOTE:
3    All llookup hash function prototypes returns 0 on sucess and 1 otherwise
4    llookup hash is an array of lhashlistnode_t
5    oid = mid = 0 in a given lhashlistnode_t for each bin in the hash table ONLY if the entry is empty =>
6    the OID's can be any unsigned int except 0
7
8    Uses pthreads. compile using -lpthread option
9 ***************************************************************************************************/
10 #include "llookup.h"
11
12 lhashtable_t llookup;           //Global Hash table
13
14 // Creates a hash table with size and an array of lhashlistnode_t 
15 unsigned int lhashCreate(unsigned int size, float loadfactor) {
16         lhashlistnode_t *nodes;
17         int i;
18
19         // Allocate space for the hash table 
20         if((nodes = calloc(size, sizeof(lhashlistnode_t))) == NULL) {
21                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
22                 return 1;
23         }
24         
25         llookup.table = nodes;
26         llookup.size = size;
27         llookup.numelements = 0; // Initial number of elements in the hash
28         llookup.loadfactor = loadfactor;
29         //Initialize the pthread_mutex variable         
30         pthread_mutex_init(&llookup.locktable, NULL);
31         return 0;
32 }
33
34 // Assign to oids to bins inside hash table
35 unsigned int lhashFunction(unsigned int oid) {
36         return( oid % (llookup.size));
37 }
38
39 // Insert oid and mid mapping into the hash table
40 unsigned int lhashInsert(unsigned int oid, unsigned int mid) {
41         unsigned int newsize;
42         int index;
43         lhashlistnode_t *ptr, *node;
44         
45         if (llookup.numelements > (llookup.loadfactor * llookup.size)) {
46                 //Resize Table
47                 newsize = 2 * llookup.size + 1;         
48                 pthread_mutex_lock(&llookup.locktable);
49                 lhashResize(newsize);
50                 pthread_mutex_unlock(&llookup.locktable);
51         }
52         
53         ptr = llookup.table;
54         llookup.numelements++;
55         
56         index = lhashFunction(oid);
57 #ifdef DEBUG
58         printf("DEBUG(insert) oid = %d, mid =%d, index =%d\n",oid,mid, index);
59 #endif
60         pthread_mutex_lock(&llookup.locktable);
61         if(ptr[index].next == NULL && ptr[index].oid == 0) {    // Insert at the first position in the hashtable
62                 ptr[index].oid = oid;
63                 ptr[index].mid = mid;
64         } else {                        // Insert in the linked list
65                 if ((node = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
66                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
67                         return 1;
68                 }
69                 node->oid = oid;
70                 node->mid = mid;
71                 node->next = ptr[index].next;
72                 ptr[index].next = node;
73         }
74         
75         pthread_mutex_unlock(&llookup.locktable);
76         return 0;
77 }
78
79 // Return mid for a given oid in the hash table
80 unsigned int lhashSearch(unsigned int oid) {
81         int index;
82         lhashlistnode_t *ptr, *node;
83
84         ptr = llookup.table;    // Address of the beginning of hash table       
85         index = lhashFunction(oid);
86         node = &ptr[index];
87         pthread_mutex_lock(&llookup.locktable);
88         while(node != NULL) {
89                 if(node->oid == oid) {
90                         return node->mid;
91                 }
92                 node = node->next;
93         }
94         pthread_mutex_unlock(&llookup.locktable);
95         return 0;
96 }
97
98 // Remove an entry from the hash table
99 unsigned int lhashRemove(unsigned int oid) {
100         int index;
101         lhashlistnode_t *curr, *prev;
102         lhashlistnode_t *ptr, *node;
103         
104         ptr = llookup.table;
105         index = lhashFunction(oid);
106         curr = &ptr[index];
107         
108         pthread_mutex_lock(&llookup.locktable);
109         for (; curr != NULL; curr = curr->next) {
110                 if (curr->oid == oid) {         // Find a match in the hash table
111                         llookup.numelements--;  // Decrement the number of elements in the global hashtable
112                         if ((curr == &ptr[index]) && (curr->next == NULL))  { // Delete the first item inside the hashtable with no linked list of lhashlistnode_t 
113                                 curr->oid = 0;
114                                 curr->mid = 0;
115                         } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of lhashlistnode_t  connected 
116                                 curr->oid = curr->next->oid;
117                                 curr->mid = curr->next->mid;
118                                 node = curr->next;
119                                 curr->next = curr->next->next;
120                                 free(node);
121                         } else {                                                // Regular delete from linked listed    
122                                 prev->next = curr->next;
123                                 free(curr);
124                         }
125                         return 0;
126                 }       
127                 prev = curr; 
128         }
129         pthread_mutex_unlock(&llookup.locktable);
130         return 1;
131 }
132
133 // Resize table
134 unsigned int lhashResize(unsigned int newsize) {
135         lhashlistnode_t *node, *ptr, *curr, *next;      // curr and next keep track of the current and the next lhashlistnodes in a linked list
136         unsigned int oldsize;
137         int isfirst;    // Keeps track of the first element in the lhashlistnode_t for each bin in hashtable
138         int i,index;    
139         lhashlistnode_t *newnode;               
140         
141         ptr = llookup.table;
142         oldsize = llookup.size;
143         
144         if((node = calloc(newsize, sizeof(lhashlistnode_t))) == NULL) {
145                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
146                 return 1;
147         }
148
149         llookup.table = node;           //Update the global hashtable upon resize()
150         llookup.size = newsize;
151         llookup.numelements = 0;
152
153         for(i = 0; i < oldsize; i++) {                  //Outer loop for each bin in hash table
154                 curr = &ptr[i];
155                 isfirst = 1;                    
156                 while (curr != NULL) {                  //Inner loop to go through linked lists
157                         if (curr->oid == 0) {           //Exit inner loop if there the first element for a given bin/index is NULL
158                                 break;                  //oid = mid =0 for element if not present within the hash table
159                         }
160                         next = curr->next;
161                         index = lhashFunction(curr->oid);
162                         // Insert into the new table
163                         if(llookup.table[index].next == NULL && llookup.table[index].oid == 0) {
164                                 llookup.table[index].oid = curr->oid;
165                                 llookup.table[index].mid = curr->mid;
166                                 llookup.numelements++;
167                         }else {
168                                 if((newnode = calloc(1, sizeof(lhashlistnode_t))) == NULL) {
169                                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
170                                         return 1;
171                                 }
172                                 newnode->oid = curr->oid;
173                                 newnode->mid = curr->mid;
174                                 newnode->next = llookup.table[index].next;
175                                 llookup.table[index].next = newnode;    
176                                 llookup.numelements++;
177                         }
178                         
179                         //free the linked list of lhashlistnode_t if not the first element in the hash table
180                         if (isfirst != 1) {
181                                 free(curr);
182                         } 
183                         
184                         isfirst = 0;
185                         curr = next;
186
187                 }
188         }
189
190         free(ptr);              //Free the memory of the old hash table 
191         return 0;
192 }