changes
[IRC.git] / Robust / src / Runtime / STM / tm.h
1 #ifndef _TM_H_
2 #define _TM_H_
3 #include "runtime.h"
4 /* ==================
5  * Control Messages
6  * ==================
7  */
8 #define TRANS_SOFT_ABORT    12
9 #define TRANS_ABORT         13
10 #define TRANS_COMMIT        14
11 #define TRANS_ABORT_RETRY   15
12
13 /* ========================
14  * Library header files
15  * ========================
16  */
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <pthread.h>
21 #include <sys/time.h>
22 #include <errno.h>
23 #include "stmlookup.h"
24 #include "stmlock.h"
25
26 /* ==================================
27  * Bit designation for status field
28  * of object header
29  * ==================================
30  */
31 #define DIRTY 0x01
32 #define NEW   0x02
33 #define LOCK  0x04
34
35 #ifdef COMPILER
36 #include "structdefs.h"
37
38 typedef struct threadrec {
39   int blocked;
40 } threadrec_t;
41
42 typedef struct objheader {
43   unsigned int version;
44   unsigned int lock;          /* reader and writer lock for object header */
45 #ifdef STMSTATS
46   int abortCount;             /* track how many times does this object cause abort */
47   int accessCount;            /* track how many times is this object accessed */
48   threadrec_t *trec;           /* some thread that locked this object */
49   int riskyflag;              /* track how risky is the object */
50   pthread_mutex_t *objlock;    /* lock this object */
51   int padding;
52 #endif
53 } objheader_t;
54
55 #define OID(x) \
56   (*((void **)&((struct ___Object___ *)(((char *) x) + sizeof(objheader_t)))->___objlocation___))
57
58 #define COMPOID(x) \
59   ((((void *) x )!=NULL) ? (*((void **)&((struct ___Object___ *) x)->___objlocation___)) : NULL)
60
61 #define STATUS(x) \
62   *((unsigned int *) &(((struct ___Object___ *)(((char *) x) + sizeof(objheader_t)))->___objstatus___))
63
64 #define STATUSPTR(x) \
65   ((unsigned int *) &(((struct ___Object___ *)(((char *) x) + sizeof(objheader_t)))->___objstatus___))
66
67 #define TYPE(x) \
68   ((struct ___Object___ *)((char *) x + sizeof(objheader_t)))->type
69
70 #define GETSIZE(size, x) { \
71     int type=TYPE(x); \
72     if (type<NUMCLASSES) { \
73       size=classsize[type]; \
74     } else { \
75       size=classsize[type]*((struct ArrayObject *)&((objheader_t *)x)[1])->___length___+sizeof(struct ArrayObject); \
76     } \
77 }
78
79 #else
80 #define OID(x) x->oid
81 #define TYPE(x) x->type
82 #define STATUS(x) x->status
83 #define STATUSPTR(x) &x->status
84 #define GETSIZE(size, x) size=classsize[TYPE(x)]
85 #endif
86
87
88 /* ================================
89  * Constants
90  * ================================
91  */
92 #define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
93 #define MAXABORTS 2
94 #define NEED_LOCK_THRESHOLD 0.020000
95 #define OSUSED(x) (((unsigned INTPTR)(x)->top)-((unsigned INTPTR) (x+1)))
96 #define OSFREE(x) ((x)->size-OSUSED(x))
97
98 #define TRANSREAD(x,y,z) { \
99     void * inputvalue; \
100     if ((inputvalue=y)==NULL) x=NULL;\
101          else { \
102            chashlistnode_t * cnodetmp=&c_table[(((unsigned INTPTR)inputvalue)&c_mask)>>4]; \
103            do { \
104              if (cnodetmp->key==inputvalue) {x=cnodetmp->val; break;} \
105              cnodetmp=cnodetmp->next; \
106              if (cnodetmp==NULL) {if (((struct ___Object___*)inputvalue)->___objstatus___&NEW) {x=inputvalue; break;} else \
107                                   {x=transRead(inputvalue,z); asm volatile ("" : "=m" (c_table),"=m" (c_mask)); break;}} \
108            } while(1); \
109          }}
110
111 #define TRANSREADRD(x,y) { \
112     void * inputvalue; \
113     if ((inputvalue=y)==NULL) x=NULL;\
114          else { \
115            chashlistnode_t * cnodetmp=&c_table[(((unsigned INTPTR)inputvalue)&c_mask)>>4]; \
116            do { \
117              if (cnodetmp->key==inputvalue) {x=cnodetmp->val; break;} \
118              cnodetmp=cnodetmp->next; \
119              if (cnodetmp==NULL) {if (((struct ___Object___*)inputvalue)->___objstatus___&NEW) {x=inputvalue; break;} else \
120                                     {x=inputvalue;rd_t_chashInsertOnce(inputvalue, ((objheader_t *)inputvalue)[-1].version); break;}} \
121            } while(1); \
122          }}
123
124 /* =================================
125  * Data structures
126  * =================================
127  */
128 typedef struct objstr {
129   unsigned int size;       //this many bytes are allocated after this header
130   void *top;
131   struct objstr *next;
132 } objstr_t;
133
134 #define MAXOBJLIST 512
135 struct objlist {
136   int offset;
137   void * objs[MAXOBJLIST];
138   struct objlist * next;
139 };
140
141 extern __thread struct objlist * newobjs;
142 extern __thread objstr_t *t_cache;
143 extern __thread objstr_t *t_reserve;
144 #ifdef STMSTATS
145 typedef struct objlockstate {
146   int offset;
147   pthread_mutex_t lock[MAXOBJLIST];
148   struct objlockstate *next;
149 } objlockstate_t;
150 extern __thread threadrec_t *trec;
151 extern __thread struct objlist * lockedobjs;
152 extern objlockstate_t *objlockscope;
153 extern __thread int t_objnumcount;
154 pthread_mutex_t lockedobjstore;
155
156 typedef struct objtypestat {
157   int numabort;         
158   int numaccess;
159   int numtrans;    //num of transactions that accessed this object type and aborted
160 } objtypestat_t;
161
162 /* Variables for probability model */
163 #define FACTOR 3
164 #endif
165
166
167 /***********************************
168  * Global Variables for statistics
169  **********************************/
170 #ifdef TRANSSTATS
171 extern int numTransCommit;
172 extern int numTransAbort;
173 extern int nSoftAbort;
174 extern int nSoftAbortAbort;
175 extern int nSoftAbortCommit;
176 #endif
177
178 #ifdef STMSTATS
179 extern objtypestat_t typesCausingAbort[];
180 #endif
181
182
183 /* ================================
184  * Functions used
185  * ================================
186  */
187 int stmStartup();
188 void objstrReset();
189 void objstrDelete(objstr_t *store);
190 objstr_t *objstrCreate(unsigned int size);
191 void transStart();
192 #ifdef STMARRAY
193 objheader_t *transCreateObj(void * ptr, unsigned int size, int bytelength);
194 #else
195 objheader_t *transCreateObj(void * ptr, unsigned int size);
196 #endif
197 unsigned int getNewOID(void);
198 void *objstrAlloc(unsigned int size);
199 __attribute__((pure)) void *transRead(void *, void *);
200 #ifdef READSET
201 __attribute__((pure)) void *transReadOnly(void *);
202 #endif
203 #ifdef DELAYCOMP
204 int transCommit(void (*commitmethod)(void *, void *, void *), void * primitives, void * locals, void * params);
205 int traverseCache(void (*commitmethod)(void *, void *, void *), void * primitives, void * locals, void * params);
206 int alttraverseCache(void (*commitmethod)(void *, void *, void *), void * primitives, void * locals, void * params);
207 void transCommitProcess(struct garbagelist *, int, int, void (*commitmethod)(void *, void *, void *), void * primitives, void * locals, void * params);
208 #else
209 int transCommit();
210 int traverseCache();
211 int alttraverseCache();
212 void transCommitProcess(struct garbagelist *, int);
213 #endif
214 int altalttraverseCache();
215 void transAbortProcess(struct garbagelist *, int);
216 void randomdelay(int);
217 #if defined(STMSTATS)||defined(SOFTABORT)
218 int getTotalAbortCount(int, int, void *, int, void*, int*, int*, int, objheader_t*, int*);
219 int getTotalAbortCount2(void *, int, void *, int *, int*, int, objheader_t*, int*);
220 int getReadAbortCount(int, int, void*, int*, int*, int, objheader_t*, int*);
221 #endif
222 #ifdef STMSTATS
223 objheader_t * needLock(objheader_t *, void *);
224 #endif
225 #ifdef SANDBOX
226 #include "sandbox.h"
227 #endif
228
229 //STM Macros
230 #ifdef STMSTATS
231 #define DEBUGSTMSTAT(args...)
232 #else
233 #define DEBUGSTMSTAT(args...)
234 #endif
235
236 #ifdef STMDEBUG
237 #define DEBUGSTM(x...) printf(x);
238 #else
239 #define DEBUGSTM(x...);
240 #endif
241
242 #ifdef STATDEBUG
243 #define DEBUGSTATS(x...) printf(x);
244 #else
245 #define DEBUGSTATS(x...);
246 #endif
247
248 #ifdef STMSTATS
249 /* Thread variable for locking/unlocking */
250 extern __thread threadrec_t *trec;
251 extern __thread struct objlist * lockedobjs;
252 extern __thread int t_objnumcount;
253 #endif
254
255 #define likely(x) x
256 #define unlikely(x) x
257
258 extern void * curr_heapbase;
259 extern void * curr_heapptr;
260 extern void * curr_heaptop;
261
262 struct fixedlist {
263   int size;
264   void * next;
265   void * array[200];
266 };
267
268 #ifdef STMARRAY
269 #include "array.h"
270 #endif
271
272 #endif