1 #include "threadnotify.h"
3 notifyhashtable_t nlookup; //Global hash table
5 void insNode(threadlist_t *head, unsigned int threadid, unsigned int mid) {
8 if((head = calloc(1, sizeof(threadlist_t))) == NULL) {
9 printf("Calloc Error %s, %d,\n", __FILE__, __LINE__);
12 head->threadid = threadid;
16 if((ptr = calloc(1, sizeof(threadlist_t))) == NULL) {
17 printf("Calloc Error %s, %d,\n", __FILE__, __LINE__);
20 ptr->threadid = threadid;
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 unsigned int notifyhashCreate(unsigned int size, float loadfactor) {
43 notifylistnode_t *nodes;
45 // Allocate space for the hash table
46 if((nodes = calloc(size, sizeof(notifylistnode_t))) == NULL) {
47 printf("Calloc error %s %d\n", __FILE__, __LINE__);
51 nlookup.table = nodes;
53 nlookup.numelements = 0; // Initial number of elements in the hash
54 nlookup.loadfactor = loadfactor;
55 //Initialize the pthread_mutex variable
56 pthread_mutex_init(&nlookup.locktable, NULL);
60 // Assign to tids to bins inside hash table
61 unsigned int notifyhashFunction(unsigned int tid) {
62 return( tid % (nlookup.size));
65 // Insert threadcond and threadid mapping into the hash table
66 unsigned int notifyhashInsert(unsigned int tid, pthread_cond_t threadcond) {
69 notifylistnode_t *ptr, *node;
71 if (nlookup.numelements > (nlookup.loadfactor * nlookup.size)) {
73 newsize = 2 * nlookup.size + 1;
74 pthread_mutex_lock(&nlookup.locktable);
75 notifyhashResize(newsize);
76 pthread_mutex_unlock(&nlookup.locktable);
79 nlookup.numelements++;
81 index = notifyhashFunction(tid);
83 printf("DEBUG -> index = %d, threadid = %d\n", index, tid);
85 pthread_mutex_lock(&nlookup.locktable);
86 if(ptr[index].next == NULL && ptr[index].threadid == 0) { // Insert at the first position in the hashtable
87 ptr[index].threadid = tid;
88 ptr[index].threadcond = threadcond;
89 } else { // Insert in the beginning of linked list
90 if ((node = calloc(1, sizeof(notifylistnode_t))) == NULL) {
91 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
92 pthread_mutex_unlock(&nlookup.locktable);
96 node->threadcond = threadcond;
97 node->next = ptr[index].next;
98 ptr[index].next = node;
100 pthread_mutex_unlock(&nlookup.locktable);
104 // Return pthread_cond_t threadcond for a given threadid in the hash table
105 pthread_cond_t notifyhashSearch(unsigned int tid) {
107 notifylistnode_t *ptr, *node;
108 pthread_cond_t tmp = PTHREAD_COND_INITIALIZER;
110 ptr = nlookup.table; // Address of the beginning of hash table
111 index = notifyhashFunction(tid);
113 pthread_mutex_lock(&nlookup.locktable);
114 while(node != NULL) {
115 if(node->threadid == tid) {
116 pthread_mutex_unlock(&nlookup.locktable);
117 return node->threadcond;
121 pthread_mutex_unlock(&nlookup.locktable);
125 // Remove an entry from the hash table
126 unsigned int notifyhashRemove(unsigned int tid) {
128 notifylistnode_t *curr, *prev;
129 notifylistnode_t *ptr, *node;
132 index = notifyhashFunction(tid);
135 pthread_cond_t tmp = PTHREAD_COND_INITIALIZER;
136 pthread_mutex_lock(&nlookup.locktable);
137 for (; 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
142 curr->threadcond = tmp;
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->threadcond = curr->next->threadcond;
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].threadcond = curr->threadcond;
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->threadcond = curr->threadcond;
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