Commit ab71c925 authored by Shivaji's avatar Shivaji

added locking for queue

parent 00b787b3
...@@ -16,15 +16,17 @@ ...@@ -16,15 +16,17 @@
struct KV *array[MAX_SIZE]; struct KV *array[MAX_SIZE];
struct queue *qu=NULL; struct queue *qu=NULL;
struct queue *last; struct queue *last;
unsigned _Atomic lock;
void remove_element_from_deque(char *key) void remove_element_from_deque(char *key)
{ {
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *present = qu , *previous=NULL; queue *present = qu , *previous=NULL;
if(present == NULL) if(present == NULL){
CLEAR(&(lock),0);
return; return;
}
if(strcmp(present->key, key) == 0) if(strcmp(present->key, key) == 0)
{ {
...@@ -32,6 +34,7 @@ void remove_element_from_deque(char *key) ...@@ -32,6 +34,7 @@ void remove_element_from_deque(char *key)
if(last == present) if(last == present)
last = last->next; last = last->next;
free(present); free(present);
CLEAR(&(lock),0);
return; return;
} }
...@@ -43,15 +46,18 @@ void remove_element_from_deque(char *key) ...@@ -43,15 +46,18 @@ void remove_element_from_deque(char *key)
last = previous; last = previous;
previous->next = present->next; previous->next = present->next;
free(present); free(present);
CLEAR(&(lock),0);
return; return;
} }
previous = present; previous = present;
present = present->next; present = present->next;
} }
CLEAR(&(lock),0);
} }
void insert_into_queue(char *key) void insert_into_queue(char *key)
{ {
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *temp = (queue *)malloc(sizeof(queue)); queue *temp = (queue *)malloc(sizeof(queue));
memcpy(temp->key, key, KEY_SIZE); memcpy(temp->key, key, KEY_SIZE);
temp->next = NULL; temp->next = NULL;
...@@ -65,6 +71,7 @@ void insert_into_queue(char *key) ...@@ -65,6 +71,7 @@ void insert_into_queue(char *key)
last->next = temp; last->next = temp;
last = temp; last = temp;
} }
CLEAR(&(lock),0);
} }
int remove_front_element() int remove_front_element()
...@@ -79,6 +86,7 @@ int remove_front_element() ...@@ -79,6 +86,7 @@ int remove_front_element()
{ {
// check if it is modified to write to file // check if it is modified to write to file
array[i]->valid = FALSE; array[i]->valid = FALSE;
CLEAR(&(lock),0);
return i; return i;
} }
} }
...@@ -86,12 +94,18 @@ int remove_front_element() ...@@ -86,12 +94,18 @@ int remove_front_element()
int find_empty_location() int find_empty_location()
{ {
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<MAX_SIZE;i++)
{ {
if(array[i]->valid == FALSE) if(array[i]->valid == FALSE)
{
CLEAR(&(lock),0);
return i; return i;
}
} }
return remove_front_element(); int indx = remove_front_element();
CLEAR(&(lock),0);
return indx;
} }
int cache_del(char *key) int cache_del(char *key)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment