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 void 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;
30 /* This function displays the linked list of threads waiting on update notification
32 void display(threadlist_t *head) {
35 printf("No thread is waiting\n");
40 printf("The threadid waiting is = %d\n", ptr->threadid);
41 printf("The mid on which thread present = %d\n", ptr->mid);
47 /* This function creates a new hash table that stores a mapping between the threadid and
48 * a pointer to the thread notify data */
49 unsigned int notifyhashCreate(unsigned int size, float loadfactor) {
50 notifylistnode_t *nodes;
52 // Allocate space for the hash table
53 if((nodes = calloc(size, sizeof(notifylistnode_t))) == NULL) {
54 printf("Calloc error %s %d\n", __FILE__, __LINE__);
58 nlookup.table = nodes;
60 nlookup.numelements = 0; // Initial number of elements in the hash
61 nlookup.loadfactor = loadfactor;
62 //Initialize the pthread_mutex variable
63 pthread_mutex_init(&nlookup.locktable, NULL);
67 // Assign to tids to bins inside hash table
68 unsigned int notifyhashFunction(unsigned int tid) {
69 return( tid % (nlookup.size));
72 // Insert pointer to the notify data and threadid mapping into the hash table
73 unsigned int notifyhashInsert(unsigned int tid, notifydata_t *ndata) {
76 notifylistnode_t *ptr, *node;
78 if (nlookup.numelements > (nlookup.loadfactor * nlookup.size)) {
80 newsize = 2 * nlookup.size + 1;
81 pthread_mutex_lock(&nlookup.locktable);
82 notifyhashResize(newsize);
83 pthread_mutex_unlock(&nlookup.locktable);
86 nlookup.numelements++;
88 index = notifyhashFunction(tid);
90 printf("DEBUG -> index = %d, threadid = %d\n", index, tid);
92 pthread_mutex_lock(&nlookup.locktable);
93 if(ptr[index].next == NULL && ptr[index].threadid == 0) { // Insert at the first position in the hashtable
94 ptr[index].threadid = tid;
95 ptr[index].ndata = ndata;
96 } else { // Insert in the beginning of linked list
97 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
98 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
99 pthread_mutex_unlock(&nlookup.locktable);
102 node->threadid = tid;
104 node->next = ptr[index].next;
105 ptr[index].next = node;
107 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) {
114 notifylistnode_t *ptr, *node;
116 ptr = nlookup.table; // Address of the beginning of hash table
117 index = notifyhashFunction(tid);
119 pthread_mutex_lock(&nlookup.locktable);
120 while(node != NULL) {
121 if(node->threadid == tid) {
122 pthread_mutex_unlock(&nlookup.locktable);
127 pthread_mutex_unlock(&nlookup.locktable);
131 // Remove an entry from the hash table
132 unsigned int notifyhashRemove(unsigned int tid) {
134 notifylistnode_t *curr, *prev, *ptr, *node;
137 index = notifyhashFunction(tid);
140 pthread_mutex_lock(&nlookup.locktable);
141 for (; curr != NULL; curr = curr->next) {
142 if (curr->threadid == tid) { // Find a match in the hash table
143 nlookup.numelements--; // Decrement the number of elements in the global hashtable
144 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of notifylistnode_t
147 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first bin item with a linked list of notifylistnode_t connected
148 curr->threadid = curr->next->threadid;
149 curr->ndata = curr->next->ndata;
151 curr->next = curr->next->next;
153 } else { // Regular delete from linked listed
154 prev->next = curr->next;
157 pthread_mutex_unlock(&nlookup.locktable);
162 pthread_mutex_unlock(&nlookup.locktable);
167 unsigned int notifyhashResize(unsigned int newsize) {
168 notifylistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next notifyhashlistnodes in a linked list
169 unsigned int oldsize;
170 int isfirst; // Keeps track of the first element in the notifylistnode_t for each bin in hashtable
172 notifylistnode_t *newnode;
175 oldsize = nlookup.size;
177 if((node = calloc(newsize, sizeof(notifylistnode_t))) == NULL) {
178 printf("Calloc error %s %d\n", __FILE__, __LINE__);
182 nlookup.table = node; //Update the global hashtable upon resize()
183 nlookup.size = newsize;
184 nlookup.numelements = 0;
186 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
189 while (curr != NULL) { //Inner loop to go through linked lists
190 if (curr->threadid == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
191 break; //threadid = threadcond =0 for element if not present within the hash table
194 index = notifyhashFunction(curr->threadid);
196 printf("DEBUG(resize) -> index = %d, threadid = %d\n", index, curr->threadid);
198 // Insert into the new table
199 if(nlookup.table[index].next == NULL && nlookup.table[index].threadid == 0) {
200 nlookup.table[index].threadid = curr->threadid;
201 nlookup.table[index].ndata = curr->ndata;
202 nlookup.numelements++;
204 if((newnode = calloc(1, sizeof(notifylistnode_t))) == NULL) {
205 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
208 newnode->threadid = curr->threadid;
209 newnode->ndata = curr->ndata;
210 newnode->next = nlookup.table[index].next;
211 nlookup.table[index].next = newnode;
212 nlookup.numelements++;
215 //free the linked list of notifylistnode_t if not the first element in the hash table
225 free(ptr); //Free the memory of the old hash table