changes for using unique ID instead of obj address for the memory queue
[IRC.git] / Robust / src / Runtime / runtime.h
1 #ifndef RUNTIME
2 #define RUNTIME
3 #ifndef MULTICORE
4 #include <setjmp.h>
5 extern jmp_buf error_handler;
6 extern int instructioncount;
7 extern int failurecount;
8 #endif
9 #ifdef DSTM
10 #ifdef RECOVERY
11 #include "DSTM/interface_recovery/dstm.h"
12 #else
13 #include "DSTM/interface/dstm.h"
14 #endif
15 #endif
16
17 #ifdef AFFINITY
18 void set_affinity();
19 #endif
20
21 #ifndef INTPTR
22 #ifdef BIT64
23 #define INTPTR long
24 #define INTPTRSHIFT 3
25 #else
26 #define INTPTR int
27 #define INTPTRSHIFT 2
28 #endif
29 #endif
30
31 extern void * curr_heapbase;
32 extern void * curr_heaptop;
33
34 #define likely(x) __builtin_expect((x),1)
35 #define unlikely(x) __builtin_expect((x),0)
36
37
38 #define TAGARRAYINTERVAL 10
39 #define OBJECTARRAYINTERVAL 10
40
41 #define ARRAYSET(array, type, index, value) \
42   ((type *)(& (& array->___length___)[1]))[index]=value
43
44 #define ARRAYGET(array, type, index) \
45   ((type *)(& (& array->___length___)[1]))[index]
46
47 #ifdef OPTIONAL
48 #define OPTARG(x) , x
49 #else
50 #define OPTARG(x)
51 #endif
52
53 #ifdef DSTM
54 __attribute__((malloc)) void * allocate_newglobal(int type);
55 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length);
56 #endif
57
58 #ifdef STM
59 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type);
60 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length);
61 #endif
62
63 #ifdef PRECISE_GC
64 #include "garbage.h"
65 #ifdef MLP
66 __attribute__((malloc)) void * allocate_new_oid(void *, int type, int oid);
67 __attribute__((malloc)) void * allocate_new(void *, int type);
68 __attribute__((malloc)) struct ArrayObject * allocate_newarray_oid(void *, int type, int length, int oid);
69 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length);
70 #else
71 __attribute__((malloc)) void * allocate_new(void *, int type);
72 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
73 #endif
74 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
75 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
76 #elif defined MULTICORE_GC
77 __attribute__((malloc)) void * allocate_new(void *, int type);
78 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void *, int type, int length);
79 __attribute__((malloc)) struct ___String___ * NewString(void *, const char *str,int length);
80 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(void *ptr, int index);
81 #else
82 __attribute__((malloc)) void * allocate_new(int type);
83 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length);
84 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length);
85 __attribute__((malloc)) struct ___TagDescriptor___ * allocate_tag(int index);
86 #endif
87
88
89
90 void initializeexithandler();
91 void failedboundschk();
92 void failednullptr();
93 void abort_task();
94 void injectinstructionfailure();
95 void createstartupobject();
96
97 #ifdef PRECISE_GC
98 #define VAR(name) ___params___->name
99 #define CALL00(name) name(struct name ## _params * ___params___)
100 #define CALL01(name, alt) name(struct name ## _params * ___params___)
101 #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___)
102 #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest)
103 #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest)
104 #define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2)
105 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2)
106 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2)
107 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3)
108 #else
109 #define VAR(name) name
110 #define CALL00(name) name()
111 #define CALL01(name, alt) name(alt)
112 #define CALL02(name, alt1, alt2) name(alt1, alt2)
113 #define CALL11(name,rest, alt) name(alt)
114 #define CALL12(name,rest, alt1, alt2) name(alt1, alt2)
115 #define CALL22(name, rest, rest2, alt1, alt2) name(alt1, alt2)
116 #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(alt1, alt2, alt3)
117 #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
118 #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4)
119 #endif
120
121 #ifdef TASK
122 #include "SimpleHash.h"
123 #ifndef MULTICORE
124 #include "chash.h"
125 #include "ObjectHash.h"
126 #include "structdefs.h"
127 #endif
128 #include "task.h"
129 #ifdef OPTIONAL
130 #include "optionalstruct.h"
131 #endif
132
133 #ifdef OPTIONAL
134 struct failedtasklist {
135   struct taskdescriptor *task;
136   int index;
137   int numflags;
138   int *flags;
139   struct failedtasklist *next;
140 };
141 #endif
142
143 #ifdef MULTICORE
144 struct transObjInfo {
145   void * objptr;
146   int targetcore;
147   int * queues;
148   int length;
149 };
150 #endif
151
152 #ifdef FASTCHECK
153 extern struct ___Object___ * ___fcrevert___;
154 #endif
155
156 #ifdef MULTICORE
157 inline void run(void * arg);
158 #ifdef MULTICORE_GC
159 inline void setupsmemmode(void);
160 #endif
161 int receiveObject(int send_port_pending);
162 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length);
163 void flagorandinit(void * ptr, int ormask, int andmask);
164 void enqueueObject(void * ptr, struct parameterwrapper ** queues,int length);
165 #ifdef PROFILE
166 inline void setTaskExitIndex(int index);
167 inline void addNewObjInfo(void * nobj);
168 #endif
169 int * getAliasLock(void ** ptrs, int length, struct RuntimeHash * tbl);
170 void addAliasLock(void * ptr, int lock);
171 void * smemalloc_I(int coren, int size, int * allocsize);
172 #else
173 void flagorand(void * ptr, int ormask, int andmask);
174 void flagorandinit(void * ptr, int ormask, int andmask);
175 void enqueueObject(void * ptr);
176 #endif
177 void executetasks();
178 void processtasks();
179
180 #ifndef MULTICORE
181 struct tagobjectiterator {
182   int istag; /* 0 if object iterator, 1 if tag iterator */
183   struct ObjectIterator it; /* Object iterator */
184   struct ObjectHash * objectset;
185 #ifdef OPTIONAL
186   int failedstate;
187 #endif
188   int slot;
189   int tagobjindex; /* Index for tag or object depending on use */
190   /*if tag we have an object binding */
191   int tagid;
192   int tagobjectslot;
193   /*if object, we may have one or more tag bindings */
194   int numtags;
195   int tagbindings[MAXTASKPARAMS-1]; /* list slots */
196 };
197
198 struct parameterwrapper {
199   struct parameterwrapper *next;
200   struct ObjectHash * objectset;
201   int numberofterms;
202   int * intarray;
203   int numbertags;
204   int * tagarray;
205   struct taskdescriptor * task;
206   int slot;
207   struct tagobjectiterator iterators[MAXTASKPARAMS-1];
208 };
209 #endif
210
211 struct taskparamdescriptor {
212   struct taskdescriptor * task;
213   int numParameters;
214   void ** parameterArray;
215 #ifdef OPTIONAL
216   int * failed;
217 #endif
218 };
219
220 int hashCodetpd(struct taskparamdescriptor *);
221 int comparetpd(struct taskparamdescriptor *, struct taskparamdescriptor *);
222
223 void toiReset(struct tagobjectiterator * it);
224 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
225 void toiNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed));
226 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams);
227 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams);
228 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter);
229 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags);
230
231 #endif
232
233 #if defined(__i386__)
234
235 static __inline__ unsigned long long rdtsc(void)
236 {
237   unsigned long long int x;
238   __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
239   return x;
240 }
241 #elif defined(__x86_64__)
242
243 static __inline__ unsigned long long rdtsc(void)
244 {
245   unsigned hi, lo;
246   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
247   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
248 }
249
250 #elif defined(__powerpc__)
251
252 typedef unsigned long long int unsigned long long;
253
254 static __inline__ unsigned long long rdtsc(void)
255 {
256   unsigned long long int result=0;
257   unsigned long int upper, lower,tmp;
258   __asm__ volatile(
259       "0:                  \n"
260       "\tmftbu   %0           \n"
261       "\tmftb    %1           \n"
262       "\tmftbu   %2           \n"
263       "\tcmpw    %2,%0        \n"
264       "\tbne     0b         \n"
265       : "=r"(upper),"=r"(lower),"=r"(tmp)
266                    );
267   result = upper;
268   result = result<<32;
269   result = result|lower;
270
271   return(result);
272 }
273 #endif
274
275
276 #endif