2 #include "abortreaders.h"
4 chashtable_t * aborttable;
5 pthread_mutex_t aborttablelock;
6 struct readerlist *freelist;
8 void initreaderlist() {
9 pthread_mutex_init(&aborttablelock, NULL);
10 aborttable=chashCreate(CHASH_SIZE, CLOADFACTOR);
14 void addtransaction(unsigned int oid) {
15 struct readerlist * rl;
17 if (pthread_mutex_trylock(&aborttablelock)!=0)
19 rl=(struct readerlist *)chashSearch(aborttable, oid);
22 rl=calloc(1,sizeof(struct readerlist ));
26 memset(rl,0, sizeof(struct readerlist));
28 chashInsert(aborttable, oid, rl);
30 while(rl->numreaders==READERSIZE) {
34 rl->next=calloc(1,sizeof(struct readerlist));
39 for(i=0; i<READERSIZE; i++) {
40 if (rl->array[i]==NULL) {
41 rl->array[i]=&t_abort;
42 pthread_mutex_unlock(&aborttablelock);
46 pthread_mutex_unlock(&aborttablelock);
47 printf("ERROR in addtransaction\n");
50 void removetransaction(unsigned int oidarray[], unsigned int numoids) {
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;
60 int count=rl->numreaders;
62 for(j=0; count; j++) {
63 int *t_abort=rl->array[j];
65 *t_abort=1; //It's okay to set our own abort flag...it is
66 //too late to abort us
76 pthread_mutex_unlock(&aborttablelock);
79 void removethisreadtransaction(unsigned char* oidverread, unsigned int numoids) {
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));
88 for(j=0; j<READERSIZE; j++) {
89 if (rl->array[j]==&t_abort) {
91 if ((--rl->numreaders)==0) {
93 chashRemove2(aborttable, oid);
95 chashInsert(aborttable, oid, rl->next);
113 pthread_mutex_unlock(&aborttablelock);
116 void removetransactionhash() {
117 chashlistnode_t *ptr=c_table;
119 pthread_mutex_lock(&aborttablelock);
120 for(i=0; i<c_size; i++) {
121 chashlistnode_t *curr=&ptr[i];
123 unsigned int oid=curr->key;
126 struct readerlist * rl=chashSearch(aborttable, oid);
127 struct readerlist *first=rl;
129 for(j=0; j<READERSIZE; j++) {
130 if (rl->array[j]==&t_abort) {
132 if ((--rl->numreaders)==0) {
134 chashRemove2(aborttable, oid);
136 chashInsert(aborttable, oid, rl->next);
140 first->next=rl->next;
155 pthread_mutex_unlock(&aborttablelock);
159 void removethistransaction(unsigned int oidarray[], unsigned int numoids) {
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);
166 struct readerlist *first=rl;
168 for(j=0; j<READERSIZE; j++) {
169 if (rl->array[j]==&t_abort) {
171 if ((--rl->numreaders)==0) {
173 chashRemove2(aborttable, oid);
175 chashInsert(aborttable, oid, rl->next);
179 first->next=rl->next;
193 pthread_mutex_unlock(&aborttablelock);