Implementation for thread join and wait and notify design
[IRC.git] / Robust / src / Runtime / DSTM / interface / threadnotify.c
1 #include "threadnotify.h"
2
3 notifyhashtable_t nlookup; //Global hash table
4
5 void insNode(threadlist_t *head, unsigned int threadid, unsigned int mid) {
6         threadlist_t *ptr;
7         if(head == NULL) {
8                 if((head = calloc(1, sizeof(threadlist_t))) == NULL) {
9                         printf("Calloc Error %s, %d,\n", __FILE__, __LINE__);
10                         return;
11                 }
12                 head->threadid = threadid;
13                 head->mid = mid;
14                 head->next = NULL;
15         } else {
16                 if((ptr = calloc(1, sizeof(threadlist_t))) == NULL) {
17                         printf("Calloc Error %s, %d,\n", __FILE__, __LINE__);
18                         return;
19                 }
20                 ptr->threadid = threadid;
21                 ptr->mid = mid;
22                 ptr->next = head;
23                 head = ptr;
24         }
25 }
26
27 void display(threadlist_t *head) {
28         threadlist_t *ptr;
29         if(head == NULL) {
30                 printf("No thread is waiting\n");
31                 return;
32         } else {
33                 while(head != NULL) {
34                         ptr = head;
35                         printf("The threadid waiting is = %d\n", ptr->threadid);
36                         printf("The mid on which thread present = %d\n", ptr->mid);
37                         head = ptr->next;
38                 }
39         }
40 }
41
42 unsigned int notifyhashCreate(unsigned int size, float loadfactor) { 
43         notifylistnode_t *nodes;
44
45         // Allocate space for the hash table 
46         if((nodes = calloc(size, sizeof(notifylistnode_t))) == NULL) {
47                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
48                 return 1;
49         }
50
51         nlookup.table = nodes;
52         nlookup.size = size;
53         nlookup.numelements = 0; // Initial number of elements in the hash
54         nlookup.loadfactor = loadfactor;
55         //Initialize the pthread_mutex variable         
56         pthread_mutex_init(&nlookup.locktable, NULL);
57         return 0;
58 }
59
60 // Assign to tids to bins inside hash table
61 unsigned int notifyhashFunction(unsigned int tid) {
62         return( tid % (nlookup.size));
63 }
64
65 // Insert threadcond and threadid mapping into the hash table
66 unsigned int notifyhashInsert(unsigned int tid, pthread_cond_t threadcond) {
67         unsigned int newsize;
68         int index;
69         notifylistnode_t *ptr, *node;
70
71         if (nlookup.numelements > (nlookup.loadfactor * nlookup.size)) {
72                 //Resize Table
73                 newsize = 2 * nlookup.size + 1;         
74                 pthread_mutex_lock(&nlookup.locktable);
75                 notifyhashResize(newsize);
76                 pthread_mutex_unlock(&nlookup.locktable);
77         }
78         ptr = nlookup.table;
79         nlookup.numelements++;
80
81         index = notifyhashFunction(tid);
82 #ifdef DEBUG
83         printf("DEBUG -> index = %d, threadid = %d\n", index, tid);
84 #endif
85         pthread_mutex_lock(&nlookup.locktable);
86         if(ptr[index].next == NULL && ptr[index].threadid == 0) {       // Insert at the first position in the hashtable
87                 ptr[index].threadid = tid;
88                 ptr[index].threadcond = threadcond;
89         } else {                        // Insert in the beginning of linked list
90                 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
91                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
92                         pthread_mutex_unlock(&nlookup.locktable);
93                         return 1;
94                 }
95                 node->threadid = tid;
96                 node->threadcond = threadcond;
97                 node->next = ptr[index].next;
98                 ptr[index].next = node;
99         }
100         pthread_mutex_unlock(&nlookup.locktable);
101         return 0;
102 }
103
104 // Return pthread_cond_t threadcond for a given threadid in the hash table
105 pthread_cond_t notifyhashSearch(unsigned int tid) {
106         int index;
107         notifylistnode_t *ptr, *node;
108         pthread_cond_t tmp = PTHREAD_COND_INITIALIZER;
109
110         ptr = nlookup.table;    // Address of the beginning of hash table       
111         index = notifyhashFunction(tid);
112         node = &ptr[index];
113         pthread_mutex_lock(&nlookup.locktable);
114         while(node != NULL) {
115                 if(node->threadid == tid) {
116                         pthread_mutex_unlock(&nlookup.locktable);
117                         return node->threadcond;
118                 }
119                 node = node->next;
120         }
121         pthread_mutex_unlock(&nlookup.locktable);
122         return tmp;
123 }
124
125 // Remove an entry from the hash table
126 unsigned int notifyhashRemove(unsigned int tid) {
127         int index;
128         notifylistnode_t *curr, *prev;
129         notifylistnode_t *ptr, *node;
130
131         ptr = nlookup.table;
132         index = notifyhashFunction(tid);
133         curr = &ptr[index];
134
135         pthread_cond_t tmp = PTHREAD_COND_INITIALIZER;
136         pthread_mutex_lock(&nlookup.locktable);
137         for (; curr != NULL; curr = curr->next) {
138                 if (curr->threadid == tid) {         // Find a match in the hash table
139                         nlookup.numelements--;  // Decrement the number of elements in the global hashtable
140                         if ((curr == &ptr[index]) && (curr->next == NULL))  { // Delete the first item inside the hashtable with no linked list of notifylistnode_t 
141                                 curr->threadid = 0;
142                                 curr->threadcond = tmp;
143                         } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first bin item with a linked list of notifylistnode_t  connected 
144                                 curr->threadid = curr->next->threadid;
145                                 curr->threadcond = curr->next->threadcond;
146                                 node = curr->next;
147                                 curr->next = curr->next->next;
148                                 free(node);
149                         } else {                                                // Regular delete from linked listed    
150                                 prev->next = curr->next;
151                                 free(curr);
152                         }
153                         pthread_mutex_unlock(&nlookup.locktable);
154                         return 0;
155                 }       
156                 prev = curr; 
157         }
158         pthread_mutex_unlock(&nlookup.locktable);
159         return 1;
160 }
161
162 // Resize table
163 unsigned int notifyhashResize(unsigned int newsize) {
164         notifylistnode_t *node, *ptr, *curr, *next;     // curr and next keep track of the current and the next notifyhashlistnodes in a linked list
165         unsigned int oldsize;
166         int isfirst;    // Keeps track of the first element in the notifylistnode_t for each bin in hashtable
167         int i,index;    
168         notifylistnode_t *newnode;              
169
170         ptr = nlookup.table;
171         oldsize = nlookup.size;
172
173         if((node = calloc(newsize, sizeof(notifylistnode_t))) == NULL) {
174                 printf("Calloc error %s %d\n", __FILE__, __LINE__);
175                 return 1;
176         }
177
178         nlookup.table = node;           //Update the global hashtable upon resize()
179         nlookup.size = newsize;
180         nlookup.numelements = 0;
181
182         for(i = 0; i < oldsize; i++) {                  //Outer loop for each bin in hash table
183                 curr = &ptr[i];
184                 isfirst = 1;                    
185                 while (curr != NULL) {                  //Inner loop to go through linked lists
186                         if (curr->threadid == 0) {              //Exit inner loop if there the first element for a given bin/index is NULL
187                                 break;                  //threadid = threadcond =0 for element if not present within the hash table
188                         }
189                         next = curr->next;
190                         index = notifyhashFunction(curr->threadid);
191 #ifdef DEBUG
192                         printf("DEBUG(resize) -> index = %d, threadid = %d\n", index, curr->threadid);
193 #endif
194                         // Insert into the new table
195                         if(nlookup.table[index].next == NULL && nlookup.table[index].threadid == 0) { 
196                                 nlookup.table[index].threadid = curr->threadid;
197                                 nlookup.table[index].threadcond = curr->threadcond;
198                                 nlookup.numelements++;
199                         }else { 
200                                 if((newnode = calloc(1, sizeof(notifylistnode_t))) == NULL) { 
201                                         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
202                                         return 1;
203                                 }       
204                                 newnode->threadid = curr->threadid;
205                                 newnode->threadcond = curr->threadcond;
206                                 newnode->next = nlookup.table[index].next;
207                                 nlookup.table[index].next = newnode;    
208                                 nlookup.numelements++;
209                         }       
210
211                         //free the linked list of notifylistnode_t if not the first element in the hash table
212                         if (isfirst != 1) {
213                                 free(curr);
214                         } 
215
216                         isfirst = 0;
217                         curr = next;
218                 }
219         }
220
221         free(ptr);              //Free the memory of the old hash table 
222         return 0;
223 }