Commit 1162d1cd authored by Shivaji's avatar Shivaji

Merge branch 'lru-sample' into 'master'

Lru sample

See merge request !2
parents e7b03d37 584623de
...@@ -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=0;
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,10 +71,12 @@ void insert_into_queue(char *key) ...@@ -65,10 +71,12 @@ 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()
{ {
while(ATOMIC_TEST_AND_SET(&(lock),1) == 1);
queue *temp = qu; queue *temp = qu;
qu = qu->next; qu = qu->next;
assert(temp != NULL); assert(temp != NULL);
...@@ -79,9 +87,11 @@ int remove_front_element() ...@@ -79,9 +87,11 @@ 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;
} }
} }
CLEAR(&(lock),0);
} }
int find_empty_location() int find_empty_location()
...@@ -89,9 +99,13 @@ int find_empty_location() ...@@ -89,9 +99,13 @@ int find_empty_location()
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();
return indx;
} }
int cache_del(char *key) int cache_del(char *key)
...@@ -121,7 +135,6 @@ void cache_put(char *key, char *value) ...@@ -121,7 +135,6 @@ void cache_put(char *key, char *value)
if(array[i]->key!=NULL) { if(array[i]->key!=NULL) {
if(strcmp(array[i]->key , key) == 0) if(strcmp(array[i]->key , key) == 0)
{ {
printf("ENTERING INTO QUEUE\n");
indx = i; indx = i;
remove_element_from_deque(key); remove_element_from_deque(key);
break; break;
......
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