Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[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) {
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(aborttable, 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]=&t_abort;
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(aborttable, oid);
56     struct readerlist *tmp;
57     if (rl==NULL)
58       continue;
59     do {
60       int count=rl->numreaders;
61       int j;
62       for(j=0; count; j++) {
63         int *t_abort=rl->array[j];
64         if (t_abort!=NULL) {
65           *t_abort=1; //It's okay to set our own abort flag...it is
66           //too late to abort us
67           count--;
68         }
69       }
70       tmp=rl;
71       rl=rl->next;
72       tmp->next=freelist;
73       freelist=tmp;
74     } while(rl!=NULL);
75   }
76   pthread_mutex_unlock(&aborttablelock);
77 }
78
79 void removethisreadtransaction(unsigned char* oidverread, unsigned int numoids) {
80   int i,j;
81   pthread_mutex_lock(&aborttablelock);
82   for(i=0; i<numoids; i++) {
83     unsigned int oid=*((unsigned int *)oidverread);
84     struct readerlist * rl=chashSearch(aborttable, oid);
85     struct readerlist *first=rl;
86     oidverread+=(sizeof(unsigned int)+sizeof(unsigned short));
87     while(rl!=NULL) {
88       for(j=0; j<READERSIZE; j++) {
89         if (rl->array[j]==&t_abort) {
90           rl->array[j]=NULL;
91           if ((--rl->numreaders)==0) {
92             if (first==rl) {
93               chashRemove2(aborttable, oid);
94               if (rl->next!=NULL)
95                 chashInsert(aborttable, oid, rl->next);
96               rl->next=freelist;
97               freelist=rl;
98             } else {
99               first->next=rl->next;
100               rl->next=freelist;
101               freelist=rl;
102             }
103           }
104           goto nextitem;
105         }
106       }
107       first=rl;
108       rl=rl->next;
109     }
110 nextitem:
111     ;
112   }
113   pthread_mutex_unlock(&aborttablelock);
114 }
115
116 void removetransactionhash() {
117   chashlistnode_t *ptr=c_table;
118   int i,j;
119   pthread_mutex_lock(&aborttablelock);
120   for(i=0; i<c_size; i++) {
121     chashlistnode_t *curr=&ptr[i];
122     do {
123       unsigned int oid=curr->key;
124       if (oid==0)
125         break;
126       struct readerlist * rl=chashSearch(aborttable, oid);
127       struct readerlist *first=rl;
128       while(rl!=NULL) {
129         for(j=0; j<READERSIZE; j++) {
130           if (rl->array[j]==&t_abort) {
131             rl->array[j]=NULL;
132             if ((--rl->numreaders)==0) {
133               if (first==rl) {
134                 chashRemove2(aborttable, oid);
135                 if (rl->next!=NULL)
136                   chashInsert(aborttable, oid, rl->next);
137                 rl->next=freelist;
138                 freelist=rl;
139               } else {
140                 first->next=rl->next;
141                 rl->next=freelist;
142                 freelist=rl;
143               }
144             }
145             goto nextitem;
146           }
147         }
148         first=rl;
149         rl=rl->next;
150       }
151 nextitem:
152       curr=curr->next;
153     } while(curr!=NULL);
154   }
155   pthread_mutex_unlock(&aborttablelock);
156 }
157
158
159 void removethistransaction(unsigned int oidarray[], unsigned int numoids) {
160   int i,j;
161   pthread_mutex_lock(&aborttablelock);
162   for(i=0; i<numoids; i++) {
163     unsigned int oid=oidarray[i];
164     struct readerlist * rl=chashSearch(aborttable, oid);
165
166     struct readerlist *first=rl;
167     while(rl!=NULL) {
168       for(j=0; j<READERSIZE; j++) {
169         if (rl->array[j]==&t_abort) {
170           rl->array[j]=NULL;
171           if ((--rl->numreaders)==0) {
172             if (first==rl) {
173               chashRemove2(aborttable, oid);
174               if (rl->next!=NULL)
175                 chashInsert(aborttable, oid, rl->next);
176               rl->next=freelist;
177               freelist=rl;
178             } else {
179               first->next=rl->next;
180               rl->next=freelist;
181               freelist=rl;
182             }
183           }
184           goto nextitem;
185         }
186       }
187       first=rl;
188       rl=rl->next;
189     }
190 nextitem:
191     ;
192   }
193   pthread_mutex_unlock(&aborttablelock);
194 }
195