check in new files...
[IRC.git] / Robust / src / Runtime / STM / inlinestm.h
1 #ifndef INLINESTM_H
2 #define INLINESTM_H
3 #ifdef DELAYCOMP
4
5 #ifndef READSET
6 #define CHECKREADS(x) 0
7 #endif
8
9 #define LIGHTWEIGHTCOMMIT(commitmethod, primitives, locals, params, label) \
10   if (GETLOCKS()||CHECKREADS()) {                                       \
11     if (unlikely(needtocollect)) checkcollect(&___locals___);           \
12     goto label;                                                         \
13   }                                                                     \
14   ptrstack.maxcount=0;                                                  \
15   primstack.count=0;                                                    \
16   branchstack.count=0;                                                  \
17   commitmethod(params, locals, primitives);                             \
18   RELEASELOCKS();                                                       \
19   FREELIST();
20
21 #ifdef READSET
22 static inline int CHECKREADS() {
23   rdchashlistnode_t *rd_curr=rd_c_list;
24   int retval=0;
25   rdchashlistnode_t *ptr=rd_c_table;
26   rdchashlistnode_t *top=&ptr[rd_c_size];
27
28   while(likely(rd_curr!=NULL)) {
29     unsigned int version=rd_curr->version;
30     struct ___Object___ * objptr=rd_curr->key;
31     objheader_t *header=(objheader_t *)(((char *)objptr)-sizeof(objheader_t));
32     if(likely(header->lock>0)) {//doesn't matter what type of lock...
33       if(unlikely(version!=header->version)) {
34         retval=1;break;
35       }
36     } else {
37       if(likely(version==header->version)) {
38         dchashlistnode_t *node = &dc_c_table[(((unsigned INTPTR)objptr) & dc_c_mask)>>4];
39         do {
40           if(node->key == objptr) {
41             goto nextloop;
42           }
43           node = node->next;
44         } while(node!=NULL);
45         retval=1;break;
46       }
47     }
48   nextloop:
49     if (likely(rd_curr>=ptr&&rd_curr<top)) {
50       //zero in list
51       rd_curr->key=NULL;
52       rd_curr->next=NULL;
53     }
54     rd_curr=rd_curr->lnext;
55   }
56
57   if (unlikely(retval)) {
58     while(likely(rd_curr!=NULL)) {
59       if (likely(rd_curr>=ptr&&rd_curr<top)) {
60         //zero in list
61         rd_curr->key=NULL;
62         rd_curr->next=NULL;
63       }
64       rd_curr=rd_curr->lnext;
65     }
66     while(rd_c_structs->next!=NULL) {
67       rdcliststruct_t *next=rd_c_structs->next;
68       free(rd_c_structs);
69       rd_c_structs=next;
70     }
71     rd_c_structs->num = 0;
72     rd_c_numelements = 0;
73     rd_c_list=NULL;
74     
75     lwreset(NULL);
76     return 1;
77   }
78
79   while(rd_c_structs->next!=NULL) {
80     rdcliststruct_t *next=rd_c_structs->next;
81     free(rd_c_structs);
82     rd_c_structs=next;
83   }
84   rd_c_structs->num = 0;
85   rd_c_numelements = 0;
86   rd_c_list=NULL;
87
88   return 0;
89 }
90 #endif
91
92 static inline void FREELIST() {
93   dchashlistnode_t *ptr = dc_c_table;
94   dchashlistnode_t *top=&ptr[dc_c_size];
95   dchashlistnode_t *tmpptr=dc_c_list;
96   while(tmpptr!=NULL) {
97     dchashlistnode_t *next=tmpptr->lnext;
98     if (tmpptr>=ptr&&tmpptr<top) {
99       /*zero in list    */
100       tmpptr->key=NULL;
101       tmpptr->next=NULL;
102     }
103     tmpptr=next;
104   }
105   while(dc_c_structs->next!=NULL) {
106     dcliststruct_t *next=dc_c_structs->next;
107     free(dc_c_structs);
108     dc_c_structs=next;
109   }
110   dc_c_structs->num = 0;
111   dc_c_numelements = 0;
112   dc_c_list=NULL;
113 }
114
115 static inline void RELEASELOCKS() {
116   dchashlistnode_t *dc_curr = dc_c_list;
117   while(likely(dc_curr!=NULL)) {
118     struct ___Object___ * objptr=dc_curr->key;
119     objheader_t *header=&((objheader_t *)objptr)[-1];
120 #ifdef STMARRAY
121     if (objptr->type>=NUMCLASSES) {
122       rwwrite_unlock(&header->lock);
123     } else {
124 #endif
125       write_unlock(&header->lock);
126 #ifdef STMARRAY
127     }
128 #endif
129     dc_curr=dc_curr->lnext;
130   }
131   primstack.count=0;
132   ptrstack.count=0;
133   branchstack.count=0;                                
134 }
135
136 static inline int GETLOCKS() {
137   dchashlistnode_t *dc_curr = dc_c_list;
138   while(likely(dc_curr!=NULL)) {
139     struct ___Object___ * objptr=dc_curr->key;
140     objheader_t *header=&((objheader_t *)objptr)[-1];
141 #ifdef STMARRAY
142     if (objptr->type>=NUMCLASSES) {
143       if (unlikely(!rwwrite_trylock(&header->lock))) {
144 #ifdef READSET
145         rd_t_chashreset();
146 #endif
147         lwreset(dc_curr);
148         return 1;
149       }
150     } else 
151 #endif
152     if(unlikely(!write_trylock(&header->lock))) {
153 #ifdef READSET
154       rd_t_chashreset();
155 #endif
156       lwreset(dc_curr);
157       return 1;
158     }
159     dc_curr=dc_curr->lnext;
160   }
161   return 0;
162 }
163
164 #endif
165 #endif