barrier bug
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <signal.h>
4 #include "mem.h"
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <stdio.h>
8 #include "option.h"
9 #include "methodheaders.h"
10 #ifdef DSTM
11 #include "dstm.h"
12 #include "prelookup.h"
13 #include "prefetch.h"
14 #endif
15 #ifdef STM
16 #include "tm.h"
17 #include <pthread.h>
18 /* Global barrier for STM */
19 pthread_barrier_t barrier; 
20 pthread_barrierattr_t attr;
21 #endif
22 #include <string.h>
23
24 extern int classsize[];
25 extern int typearray[];
26 extern int typearray2[];
27 jmp_buf error_handler;
28 int instructioncount;
29
30 char *options;
31 int injectfailures=0;
32 float failurechance=0;
33 int errors=0;
34 int debugtask=0;
35 int injectinstructionfailures;
36 int failurecount;
37 float instfailurechance=0;
38 int numfailures;
39 int instaccum=0;
40 #ifdef DMALLOC
41 #include "dmalloc.h"
42 #endif
43
44 int instanceof(struct ___Object___ *ptr, int type) {
45   int i=ptr->type;
46   do {
47     if (i==type)
48       return 1;
49     i=typearray[i];
50   } while(i!=-1);
51   i=ptr->type;
52   if (i>NUMCLASSES) {
53     do {
54       if (i==type)
55         return 1;
56       i=typearray2[i-NUMCLASSES];
57     } while(i!=-1);
58   }
59   return 0;
60 }
61
62 void exithandler(int sig, siginfo_t *info, void * uap) {
63   exit(0);
64 }
65
66 void initializeexithandler() {
67   struct sigaction sig;
68   sig.sa_sigaction=&exithandler;
69   sig.sa_flags=SA_SIGINFO;
70   sigemptyset(&sig.sa_mask);
71   sigaction(SIGUSR2, &sig, 0);
72 }
73
74
75 /* This function inject failures */
76
77 void injectinstructionfailure() {
78 #ifdef TASK
79   if (injectinstructionfailures) {
80     if (numfailures==0)
81       return;
82     instructioncount=failurecount;
83     instaccum+=failurecount;
84     if ((((double)random())/RAND_MAX)<instfailurechance) {
85       if (numfailures>0)
86         numfailures--;
87       printf("FAILURE!!! %d\n",numfailures);
88       longjmp(error_handler,11);
89     }
90   }
91 #else
92 #ifdef THREADS
93   if (injectinstructionfailures) {
94     if (numfailures==0)
95       return;
96     instaccum+=failurecount;
97     if ((((double)random())/RAND_MAX)<instfailurechance) {
98       if (numfailures>0)
99         numfailures--;
100       printf("FAILURE!!! %d\n",numfailures);
101       threadexit();
102     }
103   }
104 #endif
105 #endif
106 }
107
108 void CALL11(___System______exit____I,int ___status___, int ___status___) {
109 #ifdef TRANSSTATS
110   printf("numTransCommit = %d\n", numTransCommit);
111   printf("numTransAbort = %d\n", numTransAbort);
112   printf("nSoftAbort = %d\n", nSoftAbort);
113 #ifdef STM
114   printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
115   printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
116 #endif
117 #endif
118   exit(___status___);
119 }
120
121 #ifdef D___Vector______removeElement_____AR_L___Object____I
122 void CALL12(___Vector______removeElement_____AR_L___Object____I, int ___index___, struct ArrayObject * ___array___, int ___index___) {
123   int length=VAR(___array___)->___length___;
124   char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
125   memmove(offset, offset+sizeof(void *),(length-___index___-1)*sizeof(void *));
126 }
127 #endif
128
129 void CALL11(___System______printI____I,int ___status___, int ___status___) {
130   printf("%d\n",___status___);
131 }
132
133 long long CALL00(___System______currentTimeMillis____) {
134   struct timeval tv; long long retval;
135   gettimeofday(&tv, NULL);
136   retval = tv.tv_sec; /* seconds */
137   retval*=1000; /* milliseconds */
138   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
139   return retval;
140 }
141
142 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
143   struct ArrayObject * chararray=VAR(___s___)->___value___;
144   int i;
145   int offset=VAR(___s___)->___offset___;
146   for(i=0; i<VAR(___s___)->___count___; i++) {
147     short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
148     putchar(sc);
149   }
150 }
151
152 #ifdef DSTM
153 void CALL00(___System______clearPrefetchCache____) {
154   prehashClear();
155 }
156
157 #ifdef RANGEPREFETCH
158 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
159   /* Manual Prefetches to be inserted */
160   //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
161   //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
162   int numoffset=VAR(___offsets___)->___length___;
163   int i;
164   short offArry[numoffset+2];
165   offArry[0] = 0;
166   offArry[1] = 0;
167   for(i = 2; i<(numoffset+2); i++) {
168     offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
169     //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
170   }
171   unsigned int oid;
172   if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
173     oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
174   } else { //even
175     oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
176   }
177   rangePrefetch(oid, (short)(numoffset+2), offArry);
178 }
179 #else
180 void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
181   return;
182 }
183 #endif
184
185 #endif
186
187 #ifdef STM
188 /* STM Barrier constructs */
189 void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
190 #ifdef PRECISE_GC
191   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
192 #endif
193   // Barrier initialization
194   int ret; 
195   if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
196     printf("%s() Could not create a barrier: error %d\n", __func__, errno);
197     perror("");
198     exit(-1);
199   }
200 #ifdef PRECISE_GC
201   restartaftergc(tmp);
202 #endif
203
204
205 void CALL00(___Barrier______enterBarrier____) {
206   // Synchronization point
207   int ret;
208   ret = pthread_barrier_wait(&barrier);
209   if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
210     printf("%s() Could not wait on barrier: error %d\n", __func__, errno);
211     perror("");
212     exit(-1);
213   }
214 }
215 #endif
216
217 /* Object allocation function */
218
219 #ifdef DSTM
220 __attribute__((malloc)) void * allocate_newglobal(int type) {
221   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
222   v->type=type;
223 #ifdef THREADS
224   v->tid=0;
225   v->lockentry=0;
226   v->lockcount=0;
227 #endif
228   return v;
229 }
230
231 /* Array allocation function */
232
233 __attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
234   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
235   if (length<0) {
236     printf("ERROR: negative array\n");
237     return NULL;
238   }
239   v->type=type;
240   v->___length___=length;
241 #ifdef THREADS
242   v->tid=0;
243   v->lockentry=0;
244   v->lockcount=0;
245 #endif
246   return v;
247 }
248 #endif
249
250
251 #ifdef STM
252 // STM Versions of allocation functions
253
254 /* Object allocation function */
255 __attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
256   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
257   v->type=type;
258   v->___objlocation___=v;
259   return v;
260 }
261
262 /* Array allocation function */
263 __attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
264   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
265   if (length<0) {
266     printf("ERROR: negative array\n");
267     return NULL;
268   }
269   v->___objlocation___=(struct ___Object___*)v;
270   v->type=type;
271   v->___length___=length;
272   return v;
273 }
274 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
275   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
276   struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
277   initdsmlocks(&tmp->lock);
278   tmp->version = 1;
279   v->___objlocation___=v;
280   v->type = type;
281   return v;
282 }
283
284 /* Array allocation function */
285
286 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
287   objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
288   struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
289   initdsmlocks(&tmp->lock);
290   tmp->version=1;
291   v->type=type;
292   if (length<0) {
293     printf("ERROR: negative array\n");
294     return NULL;
295   }
296   v->___objlocation___=(struct ___Object___ *)v;
297   v->___length___=length;
298   return v;
299 }
300 #endif
301
302 #ifndef STM
303 #if defined(PRECISE_GC)
304 __attribute__((malloc)) void * allocate_new(void * ptr, int type) {
305   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
306   v->type=type;
307 #ifdef THREADS
308   v->tid=0;
309   v->lockentry=0;
310   v->lockcount=0;
311 #endif
312 #ifdef OPTIONAL
313   v->fses=0;
314 #endif
315   return v;
316 }
317
318 /* Array allocation function */
319
320 __attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
321   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
322   v->type=type;
323   if (length<0) {
324     printf("ERROR: negative array\n");
325     return NULL;
326   }
327   v->___length___=length;
328 #ifdef THREADS
329   v->tid=0;
330   v->lockentry=0;
331   v->lockcount=0;
332 #endif
333 #ifdef OPTIONAL
334   v->fses=0;
335 #endif
336   return v;
337 }
338
339 #else
340 __attribute__((malloc)) void * allocate_new(int type) {
341   struct ___Object___ * v=FREEMALLOC(classsize[type]);
342   v->type=type;
343 #ifdef OPTIONAL
344   v->fses=0;
345 #endif
346   return v;
347 }
348
349 /* Array allocation function */
350
351 __attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
352   __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
353   v->type=type;
354   v->___length___=length;
355 #ifdef OPTIONAL
356   v->fses=0;
357 #endif
358   return v;
359 }
360 #endif
361 #endif
362
363
364 /* Converts C character arrays into Java strings */
365 #ifdef PRECISE_GC
366 __attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
367 #else
368 __attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
369 #endif
370   int i;
371 #ifdef PRECISE_GC
372   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
373   INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
374   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
375   chararray=(struct ArrayObject *) ptrarray[2];
376 #else
377   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
378   struct ___String___ * strobj=allocate_new(STRINGTYPE);
379 #endif
380   strobj->___value___=chararray;
381   strobj->___count___=length;
382   strobj->___offset___=0;
383
384   for(i=0; i<length; i++) {
385     ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
386   }
387   return strobj;
388 }
389
390 /* Generated code calls this if we fail a bounds check */
391
392 void failedboundschk() {
393 #ifndef TASK
394   printf("Array out of bounds\n");
395 #ifdef THREADS
396   threadexit();
397 #else
398   exit(-1);
399 #endif
400 #else
401   longjmp(error_handler,2);
402 #endif
403 }
404
405 /* Abort task call */
406 void abort_task() {
407 #ifdef TASK
408   longjmp(error_handler,4);
409 #else
410   printf("Aborting\n");
411   exit(-1);
412 #endif
413 }