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