Commit bb0f32ef authored by Shivaji's avatar Shivaji

added atomic locks

parent a4ff33f0
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <stdatomic.h> #include <stdatomic.h>
#define MAX_SIZE 10 #define ATOMIC_TEST_AND_SET __atomic_test_and_set
#define CLEAR __atomic_clear
#define MAX_SIZE 10
#define ALLOTMEMORY (KV *)malloc(sizeof(KV)*MAX_SIZE)
// static unsigned _Atomic active[10];
typedef enum{ typedef enum{
FALSE,TRUE FALSE,TRUE
}bool; }bool;
...@@ -19,11 +19,10 @@ struct KV{ ...@@ -19,11 +19,10 @@ struct KV{
char *value; char *value;
bool valid; bool valid;
bool modified; bool modified;
int lock; unsigned _Atomic lock;
// unsigned _Atomic lock; //int lock;
}; };
int lockers[10];
typedef struct KV KV; typedef struct KV KV;
struct queue{ struct queue{
...@@ -37,24 +36,7 @@ KV *array[MAX_SIZE]; ...@@ -37,24 +36,7 @@ KV *array[MAX_SIZE];
queue *qu = NULL; queue *qu = NULL;
queue *last=NULL; queue *last=NULL;
// int compare_and_swap(int* reg, int oldval, int newval)
// {
// ATOMIC();
// int old_reg_val = *reg;
// if (old_reg_val == oldval)
// *reg = newval;
// END_ATOMIC();
// return old_reg_val;
// }
bool cas( int *p, int old, int new ) {
if( *p == old ) {
*p = new;
return TRUE;
} else {
return FALSE;
}
}
void remove_element_from_deque(char *key) void remove_element_from_deque(char *key)
{ {
...@@ -109,10 +91,10 @@ void delet(char *key) ...@@ -109,10 +91,10 @@ void delet(char *key)
{ {
if(array[i]->key == key) if(array[i]->key == key)
{ {
while(atomic_exchange_explicit(&(array[i]->lock),1,0) == 1); while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1);
remove_element_from_deque(key); remove_element_from_deque(key);
array[i]->valid = FALSE; array[i]->valid = FALSE;
atomic_exchange_explicit(&(array[i]->lock),0,1); CLEAR(&(array[i]->lock),0);
break; break;
} }
} }
...@@ -139,13 +121,13 @@ void put(char *key, char *value) ...@@ -139,13 +121,13 @@ void put(char *key, char *value)
// replacment from cache // replacment from cache
} }
while(atomic_exchange_explicit(&(array[indx]->lock),1,0) == 1); while(ATOMIC_TEST_AND_SET(&(array[indx]->lock),1) == 1);
array[indx]->key = key; array[indx]->key = key;
array[indx]->value = value; array[indx]->value = value;
array[indx]->valid = TRUE; array[indx]->valid = TRUE;
array[indx]->modified = TRUE; array[indx]->modified = TRUE;
insert_into_queue(key); insert_into_queue(key);
atomic_exchange_explicit(&(array[indx]->lock),0,1); CLEAR(&(array[indx]->lock),0);
} }
char* get(char *key) char* get(char *key)
...@@ -154,10 +136,10 @@ char* get(char *key) ...@@ -154,10 +136,10 @@ char* get(char *key)
{ {
if(array[i]->key == key) if(array[i]->key == key)
{ {
while(atomic_exchange_explicit(&(array[i]->lock),1,0) == 1); while(ATOMIC_TEST_AND_SET(&(array[i]->lock),1) == 1);
remove_element_from_deque(key); remove_element_from_deque(key);
insert_into_queue(key); insert_into_queue(key);
atomic_exchange_explicit(&(array[i]->lock),0,1); CLEAR(&(array[i]->lock),0);
return array[i]->value; return array[i]->value;
} }
} }
...@@ -202,26 +184,32 @@ int main() ...@@ -202,26 +184,32 @@ int main()
temp1->key = key1; temp1->key = key1;
temp1->value = value1; temp1->value = value1;
temp1->valid = TRUE; temp1->valid = TRUE;
temp1->lock = ATOMIC_VAR_INIT(0);
temp2->key = key2; temp2->key = key2;
temp2->value = value2; temp2->value = value2;
temp2->valid = TRUE; temp2->valid = TRUE;
temp2->lock = ATOMIC_VAR_INIT(0);
temp3->key = key3; temp3->key = key3;
temp3->value = value3; temp3->value = value3;
temp3->valid = TRUE; temp3->valid = TRUE;
temp3->lock = ATOMIC_VAR_INIT(0);
temp4->key = key4; temp4->key = key4;
temp4->value = value4; temp4->value = value4;
temp4->valid = TRUE; temp4->valid = TRUE;
temp4->lock = ATOMIC_VAR_INIT(0);
temp5->key = key5; temp5->key = key5;
temp5->value = value5; temp5->value = value5;
temp5->valid = TRUE; temp5->valid = TRUE;
temp5->lock = ATOMIC_VAR_INIT(0);
temp6->key = key6; temp6->key = key6;
temp6->value = value6; temp6->value = value6;
temp6->valid = TRUE; temp6->valid = TRUE;
temp6->lock = ATOMIC_VAR_INIT(0);
array[i++] = (temp1); array[i++] = (temp1);
......
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