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;
81 nlookup.numelements++;
85 if(tmp->threadid == tid) {
88 pthread_mutex_unlock(&nlookup.locktable);
94 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
95 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
96 pthread_mutex_unlock(&nlookup.locktable);
101 node->next = ptr[index].next;
102 ptr[index].next = node;
103 nlookup.numelements++;
106 pthread_mutex_unlock(&nlookup.locktable);
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);
125 pthread_mutex_unlock(&nlookup.locktable);
129 // Remove an entry from the hash table
130 unsigned int notifyhashRemove(unsigned int tid) {
131 notifylistnode_t *curr, *prev, *node;
133 notifylistnode_t *ptr = nlookup.table;
134 int index = notifyhashFunction(tid);
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
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;
147 curr->next = curr->next->next;
149 } else { // Regular delete from linked listed
150 prev->next = curr->next;
153 pthread_mutex_unlock(&nlookup.locktable);
158 pthread_mutex_unlock(&nlookup.locktable);
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
168 notifylistnode_t *newnode;
171 oldsize = nlookup.size;
173 if((node = calloc(newsize, sizeof(notifylistnode_t))) == NULL) {
174 printf("Calloc error %s %d\n", __FILE__, __LINE__);
178 nlookup.table = node; //Update the global hashtable upon resize()
179 nlookup.size = newsize;
180 nlookup.numelements = 0;
182 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
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
190 index = notifyhashFunction(curr->threadid);
192 printf("DEBUG(resize) -> index = %d, threadid = %d\n", index, curr->threadid);
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++;
200 if((newnode = calloc(1, sizeof(notifylistnode_t))) == NULL) {
201 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
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++;
211 //free the linked list of notifylistnode_t if not the first element in the hash table
221 free(ptr); //Free the memory of the old hash table