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 head = malloc(sizeof(threadlist_t));
12 head->threadid = threadid;
16 ptr = malloc(sizeof(threadlist_t));
17 ptr->threadid = threadid;
25 /* This function displays the linked list of threads waiting on update notification
27 void display(threadlist_t *head) {
30 printf("No thread is waiting\n");
35 printf("The threadid waiting is = %d\n", ptr->threadid);
36 printf("The mid on which thread present = %d\n", ptr->mid);
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;
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);
55 // Assign to tids to bins inside hash table
56 unsigned int notifyhashFunction(unsigned int tid) {
57 return( tid % (nlookup.size));
60 // Insert pointer to the notify data and threadid mapping into the hash table
61 unsigned int notifyhashInsert(unsigned int tid, notifydata_t *ndata) {
64 notifylistnode_t *ptr, *node, *tmp;
67 if (nlookup.numelements > (nlookup.loadfactor * nlookup.size)) {
69 newsize = 2 * nlookup.size + 1;
70 pthread_mutex_lock(&nlookup.locktable);
71 notifyhashResize(newsize);
72 pthread_mutex_unlock(&nlookup.locktable);
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;
84 if(tmp->threadid == tid) {
91 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
92 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
93 pthread_mutex_unlock(&nlookup.locktable);
98 node->next = ptr[index].next;
99 ptr[index].next = node;
102 pthread_mutex_unlock(&nlookup.locktable);
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);
121 pthread_mutex_unlock(&nlookup.locktable);
125 // Remove an entry from the hash table
126 unsigned int notifyhashRemove(unsigned int tid) {
127 notifylistnode_t *curr, *prev, *node;
129 notifylistnode_t *ptr = nlookup.table;
130 int index = notifyhashFunction(tid);
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
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;
143 curr->next = curr->next->next;
145 } else { // Regular delete from linked listed
146 prev->next = curr->next;
149 pthread_mutex_unlock(&nlookup.locktable);
154 pthread_mutex_unlock(&nlookup.locktable);
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
164 notifylistnode_t *newnode;
167 oldsize = nlookup.size;
169 if((node = calloc(newsize, sizeof(notifylistnode_t))) == NULL) {
170 printf("Calloc error %s %d\n", __FILE__, __LINE__);
174 nlookup.table = node; //Update the global hashtable upon resize()
175 nlookup.size = newsize;
176 nlookup.numelements = 0;
178 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
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
186 index = notifyhashFunction(curr->threadid);
188 printf("DEBUG(resize) -> index = %d, threadid = %d\n", index, curr->threadid);
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++;
196 if((newnode = calloc(1, sizeof(notifylistnode_t))) == NULL) {
197 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
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++;
207 //free the linked list of notifylistnode_t if not the first element in the hash table
217 free(ptr); //Free the memory of the old hash table