adding a test case
[IRC.git] / Robust / src / Runtime / DSTM / interface / threadnotify.c
1 #include "threadnotify.h"
2
3 notifyhashtable_t nlookup; //Global hash table
4
5 /* This function creates a new node in the linked list of threads waiting
6  * for an update notification from a particular object.
7  * This takes in the head of the linked list and inserts the new node to it */
8 threadlist_t *insNode(threadlist_t *head, unsigned int threadid, unsigned int mid) {
9   threadlist_t *ptr;
10   if(head == NULL) {
11     head = malloc(sizeof(threadlist_t));
12     head->threadid = threadid;
13     head->mid = mid;
14     head->next = NULL;
15   } else {
16     ptr = malloc(sizeof(threadlist_t));
17     ptr->threadid = threadid;
18     ptr->mid = mid;
19     ptr->next = head;
20     head = ptr;
21   }
22   return head;
23 }
24
25 /* This function displays the linked list of threads waiting on update notification
26  * from an object */
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 /* This function creates a new hash table that stores a mapping between the threadid and
43  * a pointer to the thread notify data */
44 unsigned int notifyhashCreate(unsigned int size, float loadfactor) {
45   notifylistnode_t *nodes = calloc(size, sizeof(notifylistnode_t));
46   nlookup.table = nodes;
47   nlookup.size = size;
48   nlookup.numelements = 0; // Initial number of elements in the hash
49   nlookup.loadfactor = loadfactor;
50   //Initialize the pthread_mutex variable
51   pthread_mutex_init(&nlookup.locktable, NULL);
52   return 0;
53 }
54
55 // Assign to tids to bins inside hash table
56 unsigned int notifyhashFunction(unsigned int tid) {
57   return( tid % (nlookup.size));
58 }
59
60 // Insert pointer to the notify data and threadid mapping into the hash table
61 unsigned int notifyhashInsert(unsigned int tid, notifydata_t *ndata) {
62   unsigned int newsize;
63   int index;
64   notifylistnode_t *ptr, *node, *tmp;
65   int isFound = 0;
66
67   if (nlookup.numelements > (nlookup.loadfactor * nlookup.size)) {
68     //Resize Table
69     newsize = 2 * nlookup.size + 1;
70     pthread_mutex_lock(&nlookup.locktable);
71     notifyhashResize(newsize);
72     pthread_mutex_unlock(&nlookup.locktable);
73   }
74   ptr = nlookup.table;
75   index = notifyhashFunction(tid);
76   pthread_mutex_lock(&nlookup.locktable);
77   if(ptr[index].next == NULL && ptr[index].threadid == 0) {
78     // Insert at the first position in the hashtable
79     ptr[index].threadid = tid;
80     ptr[index].ndata = ndata;
81     nlookup.numelements++;
82   } else {
83     tmp = &ptr[index];
84     while(tmp != NULL) {
85       if(tmp->threadid == tid) {
86         isFound = 1;
87         tmp->ndata = ndata;
88         pthread_mutex_unlock(&nlookup.locktable);
89         return 0;
90       }
91       tmp = tmp->next;
92     }
93     if(!isFound) {
94       if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
95         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
96         pthread_mutex_unlock(&nlookup.locktable);
97         return 1;
98       }
99       node->threadid = tid;
100       node->ndata = ndata;
101       node->next = ptr[index].next;
102       ptr[index].next = node;
103       nlookup.numelements++;
104     }
105   }
106   pthread_mutex_unlock(&nlookup.locktable);
107
108   return 0;
109 }
110
111 // Return pointer to thread notify data for a given threadid in the hash table
112 notifydata_t  *notifyhashSearch(unsigned int tid) {
113   // Address of the beginning of hash table
114   notifylistnode_t *ptr = nlookup.table;
115   int index = notifyhashFunction(tid);
116   pthread_mutex_lock(&nlookup.locktable);
117   notifylistnode_t * node = &ptr[index];
118   while(node != NULL) {
119     if(node->threadid == tid) {
120       pthread_mutex_unlock(&nlookup.locktable);
121       return node->ndata;
122     }
123     node = node->next;
124   }
125   pthread_mutex_unlock(&nlookup.locktable);
126   return NULL;
127 }
128
129 // Remove an entry from the hash table
130 unsigned int notifyhashRemove(unsigned int tid) {
131   notifylistnode_t *curr, *prev, *node;
132
133   notifylistnode_t *ptr = nlookup.table;
134   int index = notifyhashFunction(tid);
135
136   pthread_mutex_lock(&nlookup.locktable);
137   for (curr = &ptr[index]; 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->ndata = NULL;
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->ndata = curr->next->ndata;
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].ndata = curr->ndata;
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->ndata = curr->ndata;
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   ptr = NULL;
223   return 0;
224 }