Commit ab71c925 authored by Shivaji's avatar Shivaji

added locking for queue

parent 00b787b3
......@@ -16,15 +16,17 @@
struct KV *array[MAX_SIZE];
struct queue *qu=NULL;
struct queue *last;
unsigned _Atomic lock;
void remove_element_from_deque(char *key)
{
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *present = qu , *previous=NULL;
if(present == NULL)
if(present == NULL){
CLEAR(&(lock),0);
return;
}
if(strcmp(present->key, key) == 0)
{
......@@ -32,6 +34,7 @@ void remove_element_from_deque(char *key)
if(last == present)
last = last->next;
free(present);
CLEAR(&(lock),0);
return;
}
......@@ -43,15 +46,18 @@ void remove_element_from_deque(char *key)
last = previous;
previous->next = present->next;
free(present);
CLEAR(&(lock),0);
return;
}
previous = present;
present = present->next;
}
CLEAR(&(lock),0);
}
void insert_into_queue(char *key)
{
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *temp = (queue *)malloc(sizeof(queue));
memcpy(temp->key, key, KEY_SIZE);
temp->next = NULL;
......@@ -65,6 +71,7 @@ void insert_into_queue(char *key)
last->next = temp;
last = temp;
}
CLEAR(&(lock),0);
}
int remove_front_element()
......@@ -79,6 +86,7 @@ int remove_front_element()
{
// check if it is modified to write to file
array[i]->valid = FALSE;
CLEAR(&(lock),0);
return i;
}
}
......@@ -86,12 +94,18 @@ int remove_front_element()
int find_empty_location()
{
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
for(int i=0;i<MAX_SIZE;i++)
{
if(array[i]->valid == FALSE)
{
CLEAR(&(lock),0);
return i;
}
}
return remove_front_element();
int indx = remove_front_element();
CLEAR(&(lock),0);
return indx;
}
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