start of new file
[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   } else {
82     tmp = &ptr[index];
83     while(tmp != NULL) {
84       if(tmp->threadid == tid) {
85         isFound = 1;
86         tmp->ndata = ndata;
87       }
88       tmp = tmp->next;
89     }
90     if(!isFound) {
91       if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
92         printf("Calloc error %s, %d\n", __FILE__, __LINE__);
93         pthread_mutex_unlock(&nlookup.locktable);
94         return 1;
95       }
96       node->threadid = tid;
97       node->ndata = ndata;
98       node->next = ptr[index].next;
99       ptr[index].next = node;
100     }
101   }
102   pthread_mutex_unlock(&nlookup.locktable);
103   
104   return 0;
105 }
106
107 // Return pointer to thread notify data for a given threadid in the hash table
108 notifydata_t  *notifyhashSearch(unsigned int tid) {
109   // Address of the beginning of hash table     
110   notifylistnode_t *ptr = nlookup.table;        
111   int index = notifyhashFunction(tid);
112   pthread_mutex_lock(&nlookup.locktable);
113   notifylistnode_t * node = &ptr[index];
114   while(node != NULL) {
115     if(node->threadid == tid) {
116       pthread_mutex_unlock(&nlookup.locktable);
117       return node->ndata;
118     }
119     node = node->next;
120   }
121   pthread_mutex_unlock(&nlookup.locktable);
122   return NULL;
123 }
124
125 // Remove an entry from the hash table
126 unsigned int notifyhashRemove(unsigned int tid) {
127   notifylistnode_t *curr, *prev, *node;
128   
129   notifylistnode_t *ptr = nlookup.table;
130   int index = notifyhashFunction(tid);
131   
132   pthread_mutex_lock(&nlookup.locktable);
133   for (curr = &ptr[index]; curr != NULL; curr = curr->next) {
134     if (curr->threadid == tid) {         // Find a match in the hash table
135       nlookup.numelements--;  // Decrement the number of elements in the global hashtable
136       if ((curr == &ptr[index]) && (curr->next == NULL))  { // Delete the first item inside the hashtable with no linked list of notifylistnode_t 
137         curr->threadid = 0;
138         curr->ndata = NULL;
139       } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first bin item with a linked list of notifylistnode_t  connected 
140         curr->threadid = curr->next->threadid;
141         curr->ndata = curr->next->ndata;
142         node = curr->next;
143         curr->next = curr->next->next;
144         free(node);
145       } else {                                          // Regular delete from linked listed    
146         prev->next = curr->next;
147         free(curr);
148       }
149       pthread_mutex_unlock(&nlookup.locktable);
150       return 0;
151     }       
152     prev = curr; 
153   }
154   pthread_mutex_unlock(&nlookup.locktable);
155   return 1;
156 }
157
158 // Resize table
159 unsigned int notifyhashResize(unsigned int newsize) {
160   notifylistnode_t *node, *ptr, *curr, *next;   // curr and next keep track of the current and the next notifyhashlistnodes in a linked list
161   unsigned int oldsize;
162   int isfirst;    // Keeps track of the first element in the notifylistnode_t for each bin in hashtable
163   int i,index;          
164   notifylistnode_t *newnode;            
165
166   ptr = nlookup.table;
167   oldsize = nlookup.size;
168   
169   if((node = calloc(newsize, sizeof(notifylistnode_t))) == NULL) {
170     printf("Calloc error %s %d\n", __FILE__, __LINE__);
171     return 1;
172   }
173   
174   nlookup.table = node;                 //Update the global hashtable upon resize()
175   nlookup.size = newsize;
176   nlookup.numelements = 0;
177   
178   for(i = 0; i < oldsize; i++) {                        //Outer loop for each bin in hash table
179     curr = &ptr[i];
180     isfirst = 1;                        
181     while (curr != NULL) {                      //Inner loop to go through linked lists
182       if (curr->threadid == 0) {                //Exit inner loop if there the first element for a given bin/index is NULL
183         break;                  //threadid = threadcond =0 for element if not present within the hash table
184       }
185       next = curr->next;
186       index = notifyhashFunction(curr->threadid);
187 #ifdef DEBUG
188       printf("DEBUG(resize) -> index = %d, threadid = %d\n", index, curr->threadid);
189 #endif
190       // Insert into the new table
191       if(nlookup.table[index].next == NULL && nlookup.table[index].threadid == 0) { 
192         nlookup.table[index].threadid = curr->threadid;
193         nlookup.table[index].ndata = curr->ndata;
194         nlookup.numelements++;
195       }else { 
196         if((newnode = calloc(1, sizeof(notifylistnode_t))) == NULL) { 
197           printf("Calloc error %s, %d\n", __FILE__, __LINE__);
198           return 1;
199         }       
200         newnode->threadid = curr->threadid;
201         newnode->ndata = curr->ndata;
202         newnode->next = nlookup.table[index].next;
203         nlookup.table[index].next = newnode;    
204         nlookup.numelements++;
205       }       
206       
207       //free the linked list of notifylistnode_t if not the first element in the hash table
208       if (isfirst != 1) {
209         free(curr);
210       } 
211       
212       isfirst = 0;
213       curr = next;
214     }
215   }
216   
217   free(ptr);            //Free the memory of the old hash table 
218   ptr = NULL;
219   return 0;
220 }