Commit 4b423db7 authored by Shivaji's avatar Shivaji

added initial cas

parent 481b3be8
#include <iostream> #include <stdio.h>
#include <string>
#include <vector>
#include <pthread.h> #include <pthread.h>
#include <map> #include <stdlib.h>
#include <bits/stdc++.h> #define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_SIZE 10
#define ALLOTMEMORY (KV *)malloc(sizeof(KV)*MAX_SIZE)
typedef enum{
FALSE,TRUE
}bool;
struct KV{ struct KV{
char *key; char *key;
char *value; char *value;
bool valid; bool valid;
bool modified; bool modified;
int lock;
}; };
typedef struct KV KV; typedef struct KV KV;
using namespace std; struct queue{
char *key;
int MAX_SIZE=10; struct queue *next;
map<char *,int> mp; };
vector<KV *> stor;
deque<char *> dq;
typedef struct queue queue;
KV *array[MAX_SIZE];
queue *qu = 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)
{ {
for (auto it = dq.begin(); it != dq.end(); it++) queue *present = qu , *previous=NULL;
while(present->next != NULL)
{ {
if(*it == key) if(present->key == key)
{ {
dq.erase(it); if(previous == NULL)
break; qu = qu->next;
if(last == present)
last = previous;
if(previous)
previous->next = present->next;
free(present);
return;
} }
previous = present;
present = present->next;
}
}
void insert_into_queue(char *key)
{
queue *temp = (queue *)malloc(sizeof(queue));
temp->key = key;
temp->next = NULL;
if(qu == NULL)
{
qu = temp;
last = temp;
}
else
{
last->next = temp;
last = temp;
} }
} }
...@@ -38,65 +93,79 @@ int find_empty_location() ...@@ -38,65 +93,79 @@ int find_empty_location()
{ {
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<MAX_SIZE;i++)
{ {
if(stor[i]->valid == 0) if(array[i]->valid == FALSE)
return i; return i;
} }
} }
void delet(char *key) void delet(char *key)
{ {
remove_element_from_deque(key); for(int i=0;i<MAX_SIZE;i++)
int indx = mp[key]; {
mp.erase(key); if(array[i]->key == key)
stor[indx]->valid = 0; {
while(cas(&(array[i]->lock),0,1) == FALSE) ;
remove_element_from_deque(key);
array[i]->valid = FALSE;
cas(&(array[i]->lock),1,0);
break;
}
}
//TODO remove key from file also //TODO remove key from file also
} }
void put(char *key, char *value) void put(char *key, char *value)
{ {
int indx=-1; int indx=-1;
if(mp.find(key) != mp.end()) for(int i=0;i<MAX_SIZE;i++)
{ {
indx = mp[key]; if(array[i]->key == key)
remove_element_from_deque(key); {
indx = i;
remove_element_from_deque(key);
break;
}
} }
else
if(indx == -1)
{ {
indx = find_empty_location(); indx = find_empty_location();
mp[key] = indx;
// TODO should write to file if modified is true // TODO should write to file if modified is true
// replacment from cache // replacment from cache
} }
stor[indx]->key = key; while(cas(&(array[indx]->lock),0,1) == FALSE) ;
stor[indx]->value = value; array[indx]->key = key;
stor[indx]->valid = 1; array[indx]->value = value;
stor[indx]->modified = 1; array[indx]->valid = TRUE;
dq.push_front(key); array[indx]->modified = TRUE;
insert_into_queue(key);
cas(&(array[indx]->lock),1,0);
} }
char* get(char *key) char* get(char *key)
{ {
if(mp.find(key) != mp.end()) for(int i=0;i<MAX_SIZE;i++)
{ {
int indx = mp[key]; if(array[i]->key == key)
remove_element_from_deque(key); {
dq.push_front(key); while(cas(&(array[i]->lock),0,1) == FALSE);
return stor[indx]->value; remove_element_from_deque(key);
insert_into_queue(key);
cas(&(array[i]->lock),1,0);
return array[i]->value;
}
} }
return (char *)"NOT FOUND"; return (char *)"NOT FOUND";
} }
int main() int main()
{ {
// stor.resize(10);
stor.resize(10);
int i=0; int i=0;
for(int j=0;j<MAX_SIZE;j++) for(int j=0;j<MAX_SIZE;j++)
{ {
stor[j] = (KV *)malloc(sizeof(KV)); array[j] = (KV *)malloc(sizeof(KV));
stor[j]->valid = false; array[j]->valid = FALSE;
} }
char key1[250]="Hello world1"; char key1[250]="Hello world1";
char value1[256] = "THIS IS Vlaue1"; char value1[256] = "THIS IS Vlaue1";
...@@ -126,61 +195,86 @@ int main() ...@@ -126,61 +195,86 @@ int main()
temp1->key = key1; temp1->key = key1;
temp1->value = value1; temp1->value = value1;
temp1->valid = 1; temp1->valid = TRUE;
temp2->key = key2; temp2->key = key2;
temp2->value = value2; temp2->value = value2;
temp2->valid = 1; temp2->valid = TRUE;
temp3->key = key3; temp3->key = key3;
temp3->value = value3; temp3->value = value3;
temp3->valid = 1; temp3->valid = TRUE;
temp4->key = key4; temp4->key = key4;
temp4->value = value4; temp4->value = value4;
temp4->valid = 1; temp4->valid = TRUE;
temp5->key = key5; temp5->key = key5;
temp5->value = value5; temp5->value = value5;
temp5->valid = 1; temp5->valid = TRUE;
temp6->key = key6; temp6->key = key6;
temp6->value = value6; temp6->value = value6;
temp6->valid = 1; temp6->valid = TRUE;
stor[i++]=(temp1); array[i++] = (temp1);
stor[i++] = (temp2); array[i++] = (temp2);
stor[i++] = (temp3); array[i++] = (temp3);
stor[i++] = (temp4); array[i++] = (temp4);
stor[i++] = (temp5); array[i++] = (temp5);
stor[i++] = (temp6); array[i++] = (temp6);
mp.insert({key6,5}); qu = (queue *)malloc(sizeof(queue));
mp.insert({key1,0}); qu->key = temp1->key;
mp.insert({key2,1}); qu->next = NULL;
mp.insert({key3,2});
mp.insert({key4,3}); queue *te = qu;
mp.insert({key5,4}); te->next = (queue *)malloc(sizeof(queue));
te = te->next;
cout << mp.size() << endl; te->key = temp1->key;
cout << stor[0]->key << " " << stor[0]->value << endl; te->next = NULL;
cout << stor[1]->key << " " << stor[1]->value << endl;
cout << stor[2]->key << " " << stor[2]->value << endl; te->next = (queue *)malloc(sizeof(queue));
cout << stor[3]->key << " " << stor[3]->value << endl; te = te->next;
cout << stor[4]->key << " " << stor[4]->value << endl; te->key = temp2->key;
cout << stor[5]->key << " " << stor[5]->value << endl; te->next = NULL;
cout << get(stor[5]->key) << endl; te->next = (queue *)malloc(sizeof(queue));
put(stor[5]->key, (char *)"This is a good"); te = te->next;
cout << get(stor[5]->key) << endl; te->key = temp3->key;
delet(stor[5]->key); te->next = NULL;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp4->key;
te->next = NULL;
te->next = (queue *)malloc(sizeof(queue));
te = te->next;
te->key = temp5->key;
te->next = NULL;
last = te;
printf("%s " " %s\n", array[0]->key, array[0]->value);
printf("%s " " %s\n", array[1]->key, array[1]->value);
printf("%s " " %s\n", array[2]->key, array[2]->value);
printf("%s " " %s\n", array[3]->key, array[3]->value);
printf("%s " " %s\n", array[4]->key, array[4]->value);
printf("%s " " %s\n", array[5]->key, array[5]->value);
printf("%s\n",get(array[5]->key));
put(array[5]->key, (char *)"This is a good");
printf("%s\n",get(array[5]->key));
delet(array[5]->key);
delet(stor[0]->key); delet(array[0]->key);
put((char *)"Hello World7", (char *)"THis is vaue7"); put((char *)"Hello World7", (char *)"THis is vaue7");
cout << stor[0]->key << " " << stor[0]->value << "\n"; printf("%s %s\n",array[0]->key, array[0]->value);
return 0; return 0;
} }
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