Commit a4ff33f0 authored by Shivaji's avatar Shivaji

added xchange atomic

parent 4b423db7
...@@ -5,9 +5,11 @@ ...@@ -5,9 +5,11 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <stdatomic.h>
#define MAX_SIZE 10 #define MAX_SIZE 10
#define ALLOTMEMORY (KV *)malloc(sizeof(KV)*MAX_SIZE)
#define ALLOTMEMORY (KV *)malloc(sizeof(KV)*MAX_SIZE)
// static unsigned _Atomic active[10];
typedef enum{ typedef enum{
FALSE,TRUE FALSE,TRUE
}bool; }bool;
...@@ -18,7 +20,10 @@ struct KV{ ...@@ -18,7 +20,10 @@ struct KV{
bool valid; bool valid;
bool modified; bool modified;
int lock; int lock;
// unsigned _Atomic lock;
}; };
int lockers[10];
typedef struct KV KV; typedef struct KV KV;
struct queue{ struct queue{
...@@ -104,10 +109,10 @@ void delet(char *key) ...@@ -104,10 +109,10 @@ void delet(char *key)
{ {
if(array[i]->key == key) if(array[i]->key == key)
{ {
while(cas(&(array[i]->lock),0,1) == FALSE) ; while(atomic_exchange_explicit(&(array[i]->lock),1,0) == 1);
remove_element_from_deque(key); remove_element_from_deque(key);
array[i]->valid = FALSE; array[i]->valid = FALSE;
cas(&(array[i]->lock),1,0); atomic_exchange_explicit(&(array[i]->lock),0,1);
break; break;
} }
} }
...@@ -134,13 +139,13 @@ void put(char *key, char *value) ...@@ -134,13 +139,13 @@ void put(char *key, char *value)
// replacment from cache // replacment from cache
} }
while(cas(&(array[indx]->lock),0,1) == FALSE) ; while(atomic_exchange_explicit(&(array[indx]->lock),1,0) == 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);
cas(&(array[indx]->lock),1,0); atomic_exchange_explicit(&(array[indx]->lock),0,1);
} }
char* get(char *key) char* get(char *key)
...@@ -149,10 +154,10 @@ char* get(char *key) ...@@ -149,10 +154,10 @@ char* get(char *key)
{ {
if(array[i]->key == key) if(array[i]->key == key)
{ {
while(cas(&(array[i]->lock),0,1) == FALSE); while(atomic_exchange_explicit(&(array[i]->lock),1,0) == 1);
remove_element_from_deque(key); remove_element_from_deque(key);
insert_into_queue(key); insert_into_queue(key);
cas(&(array[i]->lock),1,0); atomic_exchange_explicit(&(array[i]->lock),0,1);
return array[i]->value; return array[i]->value;
} }
} }
...@@ -166,6 +171,7 @@ int main() ...@@ -166,6 +171,7 @@ int main()
{ {
array[j] = (KV *)malloc(sizeof(KV)); array[j] = (KV *)malloc(sizeof(KV));
array[j]->valid = FALSE; array[j]->valid = FALSE;
array[j]->lock = 0;
} }
char key1[250]="Hello world1"; char key1[250]="Hello world1";
char value1[256] = "THIS IS Vlaue1"; char value1[256] = "THIS IS Vlaue1";
......
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