changes
[IRC.git] / Robust / src / Runtime / DSTM / interface / abortreaders.c
1 #include "clookup.h"
2 #include "abortreaders.h"
3
4 chashtable_t * aborttable;
5 pthread_mutex_t aborttablelock;
6 struct readerlist *freelist;
7
8 void initreaderlist() {
9   pthread_mutex_init(&aborttablelock, NULL);
10   aborttable=chashCreate(CHASH_SIZE, CLOADFACTOR);
11   freelist=NULL;
12 }
13
14 void addtransaction(unsigned int oid, struct transrecord * trans) {
15   struct readerlist * rl;
16   int i;
17   if (pthread_mutex_trylock(&aborttablelock)!=0)
18     return;
19   rl=(struct readerlist *)chashSearch(aborttable, oid);
20   if (rl==NULL) {
21     if (freelist==NULL)
22       rl=calloc(1,sizeof(struct readerlist ));
23     else {
24       rl=freelist;
25       freelist=rl->next;
26       memset(rl,0, sizeof(struct readerlist));
27     }
28     chashInsert(rl, oid, rl);
29   }
30   while(rl->numreaders==READERSIZE) {
31     if (rl->next!=NULL)
32       rl=rl->next;
33     else {
34       rl->next=calloc(1,sizeof(struct readerlist));
35       rl=rl->next;
36     }
37   }
38   rl->numreaders++;
39   for(i=0;i<READERSIZE;i++) {
40     if (rl->array[i]==NULL) {
41       rl->array[i]=trans;
42       pthread_mutex_unlock(&aborttablelock);
43       return;
44     }
45   }
46   pthread_mutex_unlock(&aborttablelock);
47   printf("ERROR in addtransaction\n");
48 }
49
50 void removetransaction(unsigned int oidarray[], unsigned int numoids) {
51   int i,j;
52   pthread_mutex_lock(&aborttablelock);
53   for(i=0;i<numoids;i++) {
54     unsigned int oid=oidarray[i];
55     struct readerlist *rl=chashRemove2(table, oid);
56     struct readerlist *tmp;
57     do {
58       count=rl->numreaders;
59       for(int j=0;count;j++) {
60         struct transrecord *trans=rl->array[j];
61         if (trans!=NULL) {
62           trans->abort=1;//It's okay to set our own abort flag...it is
63                          //too late to abort us
64           count--;
65         }
66       }
67       tmp=rl;
68       rl=rl->next;
69       tmp->next=freelist;
70       freelist=tmp;
71     } while(rl!=NULL);
72   }
73   pthread_mutex_unlock(&aborttablelock);
74 }
75
76 void removeaborttransaction(unsigned int oidarray[], unsigned int numoids, struct transrecord * trans) {
77   int i,j;
78   pthread_mutex_lock(&aborttablelock);
79   for(i=0;i<numoids;i++) {
80     unsigned int oid=oidarray[i];
81     struct readerlist * rl=chashSearch(aborttable, oid);
82     
83     struct readerlist *first=rl;
84     while(1) {
85       for(j=0;j<READERSIZE;j++) {
86         if (rl->array[j]==trans) {
87           rl->array[j]=NULL;
88           if ((--rl->numreaders)==0) {
89             if (first==rl) {
90               chashRemove2(table, oid);
91               if (rl->next!=NULL) 
92                 chashInsert(table, oid, rl->next);
93               rl->next=freelist;
94               freelist=rl;
95             } else {
96               first->next=rl->next;
97               rl->next=freelist;
98               freelist=rl;
99             }
100           }
101           goto nextitem;
102         }
103       }
104       first=rl;
105       rl=rl->next;
106     }
107   nextitem:
108   }
109   pthread_mutex_unlock(&aborttablelock);
110 }
111