1 #include "threadnotify.h"
3 notifyhashtable_t nlookup; //Global hash table
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) {
11 if((head = calloc(1, sizeof(threadlist_t))) == NULL) {
12 printf("Calloc Error %s, %d,\n", __FILE__, __LINE__);
15 head->threadid = threadid;
19 if((ptr = calloc(1, sizeof(threadlist_t))) == NULL) {
20 printf("Calloc Error %s, %d,\n", __FILE__, __LINE__);
23 ptr->threadid = threadid;
32 /* This function displays the linked list of threads waiting on update notification
34 void display(threadlist_t *head) {
37 printf("No thread is waiting\n");
42 printf("The threadid waiting is = %d\n", ptr->threadid);
43 printf("The mid on which thread present = %d\n", ptr->mid);
49 /* This function creates a new hash table that stores a mapping between the threadid and
50 * a pointer to the thread notify data */
51 unsigned int notifyhashCreate(unsigned int size, float loadfactor) {
52 notifylistnode_t *nodes;
54 // Allocate space for the hash table
55 if((nodes = calloc(size, sizeof(notifylistnode_t))) == NULL) {
56 printf("Calloc error %s %d\n", __FILE__, __LINE__);
60 nlookup.table = nodes;
62 nlookup.numelements = 0; // Initial number of elements in the hash
63 nlookup.loadfactor = loadfactor;
64 //Initialize the pthread_mutex variable
65 pthread_mutex_init(&nlookup.locktable, NULL);
69 // Assign to tids to bins inside hash table
70 unsigned int notifyhashFunction(unsigned int tid) {
71 return( tid % (nlookup.size));
74 // Insert pointer to the notify data and threadid mapping into the hash table
75 unsigned int notifyhashInsert(unsigned int tid, notifydata_t *ndata) {
78 notifylistnode_t *ptr, *node, *tmp;
81 if (nlookup.numelements > (nlookup.loadfactor * nlookup.size)) {
83 newsize = 2 * nlookup.size + 1;
84 pthread_mutex_lock(&nlookup.locktable);
85 notifyhashResize(newsize);
86 pthread_mutex_unlock(&nlookup.locktable);
90 nlookup.numelements++;
92 index = notifyhashFunction(tid);
94 printf("DEBUG -> index = %d, threadid = %d\n", index, tid);
96 pthread_mutex_lock(&nlookup.locktable);
97 if(ptr[index].next == NULL && ptr[index].threadid == 0) { // Insert at the first position in the hashtable
98 ptr[index].threadid = tid;
99 ptr[index].ndata = ndata;
100 } else { // Insert in the beginning of linked list
101 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
102 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
103 pthread_mutex_unlock(&nlookup.locktable);
106 node->threadid = tid;
108 node->next = ptr[index].next;
109 ptr[index].next = node;
111 pthread_mutex_unlock(&nlookup.locktable);
114 index = notifyhashFunction(tid);
115 pthread_mutex_lock(&nlookup.locktable);
116 if(ptr[index].next == NULL && ptr[index].threadid == 0) { // Insert at the first position in the hashtable
117 ptr[index].threadid = tid;
118 ptr[index].ndata = ndata;
122 if(tmp->threadid == tid) {
129 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
130 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
131 pthread_mutex_unlock(&nlookup.locktable);
134 node->threadid = tid;
136 node->next = ptr[index].next;
137 ptr[index].next = node;
140 pthread_mutex_unlock(&nlookup.locktable);
145 // Return pointer to thread notify data for a given threadid in the hash table
146 notifydata_t *notifyhashSearch(unsigned int tid) {
148 notifylistnode_t *ptr, *node;
150 ptr = nlookup.table; // Address of the beginning of hash table
151 index = notifyhashFunction(tid);
153 pthread_mutex_lock(&nlookup.locktable);
154 while(node != NULL) {
155 if(node->threadid == tid) {
156 pthread_mutex_unlock(&nlookup.locktable);
161 pthread_mutex_unlock(&nlookup.locktable);
165 // Remove an entry from the hash table
166 unsigned int notifyhashRemove(unsigned int tid) {
168 notifylistnode_t *curr, *prev, *ptr, *node;
171 index = notifyhashFunction(tid);
174 pthread_mutex_lock(&nlookup.locktable);
175 for (; curr != NULL; curr = curr->next) {
176 if (curr->threadid == tid) { // Find a match in the hash table
177 nlookup.numelements--; // Decrement the number of elements in the global hashtable
178 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of notifylistnode_t
181 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first bin item with a linked list of notifylistnode_t connected
182 curr->threadid = curr->next->threadid;
183 curr->ndata = curr->next->ndata;
185 curr->next = curr->next->next;
187 } else { // Regular delete from linked listed
188 prev->next = curr->next;
191 pthread_mutex_unlock(&nlookup.locktable);
196 pthread_mutex_unlock(&nlookup.locktable);
201 unsigned int notifyhashResize(unsigned int newsize) {
202 notifylistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next notifyhashlistnodes in a linked list
203 unsigned int oldsize;
204 int isfirst; // Keeps track of the first element in the notifylistnode_t for each bin in hashtable
206 notifylistnode_t *newnode;
209 oldsize = nlookup.size;
211 if((node = calloc(newsize, sizeof(notifylistnode_t))) == NULL) {
212 printf("Calloc error %s %d\n", __FILE__, __LINE__);
216 nlookup.table = node; //Update the global hashtable upon resize()
217 nlookup.size = newsize;
218 nlookup.numelements = 0;
220 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
223 while (curr != NULL) { //Inner loop to go through linked lists
224 if (curr->threadid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
225 break; //threadid = threadcond =0 for element if not present within the hash table
228 index = notifyhashFunction(curr->threadid);
230 printf("DEBUG(resize) -> index = %d, threadid = %d\n", index, curr->threadid);
232 // Insert into the new table
233 if(nlookup.table[index].next == NULL && nlookup.table[index].threadid == 0) {
234 nlookup.table[index].threadid = curr->threadid;
235 nlookup.table[index].ndata = curr->ndata;
236 nlookup.numelements++;
238 if((newnode = calloc(1, sizeof(notifylistnode_t))) == NULL) {
239 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
242 newnode->threadid = curr->threadid;
243 newnode->ndata = curr->ndata;
244 newnode->next = nlookup.table[index].next;
245 nlookup.table[index].next = newnode;
246 nlookup.numelements++;
249 //free the linked list of notifylistnode_t if not the first element in the hash table
259 free(ptr); //Free the memory of the old hash table