a5c7842552c8de9331987e0edca23b1acd5d3fa7
[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<signal.h>
8 #include<stdio.h>
9 #include "option.h"
10 #ifdef DSTM
11 #include "dstm.h"
12 #include "prelookup.h"
13 #endif
14
15 extern int classsize[];
16 jmp_buf error_handler;
17 int instructioncount;
18
19 char *options;
20 int injectfailures=0;
21 float failurechance=0;
22 int debugtask=0;
23 int injectinstructionfailures;
24 int failurecount;
25 float instfailurechance=0;
26 int numfailures;
27 int instaccum=0;
28 #ifdef DMALLOC
29 #include "dmalloc.h"
30 #endif
31
32 void exithandler(int sig, siginfo_t *info, void * uap) {
33   exit(0);
34 }
35
36 void initializeexithandler() {
37   struct sigaction sig;
38   sig.sa_sigaction=&exithandler;
39   sig.sa_flags=SA_SIGINFO;
40   sigemptyset(&sig.sa_mask);
41   sigaction(SIGUSR2, &sig, 0);
42 }
43
44
45 /* This function inject failures */
46
47 void injectinstructionfailure() {
48 #ifdef TASK
49   if (injectinstructionfailures) {
50     if (numfailures==0)
51       return;
52     instructioncount=failurecount;    
53     instaccum+=failurecount;
54     if ((((double)random())/RAND_MAX)<instfailurechance) {
55       if (numfailures>0)
56         numfailures--;
57       printf("FAILURE!!! %d\n",numfailures);
58       longjmp(error_handler,11);
59     }
60   }
61 #else
62 #ifdef THREADS
63   if (injectinstructionfailures) {
64     if (numfailures==0)
65       return;
66     instaccum+=failurecount;
67     if ((((double)random())/RAND_MAX)<instfailurechance) {
68       if (numfailures>0)
69         numfailures--;
70       printf("FAILURE!!! %d\n",numfailures);
71       threadexit();
72     }
73   }
74 #endif
75 #endif
76 }
77
78 void CALL11(___System______exit____I,int ___status___, int ___status___) {
79   exit(___status___);
80 }
81
82 long CALL00(___System______currentTimeMillis____) {
83   struct timeval tv; long long retval;
84   gettimeofday(&tv, NULL);
85   retval = tv.tv_sec; /* seconds */
86   retval*=1000; /* milliseconds */
87   retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
88   return retval;
89 }
90
91 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
92     struct ArrayObject * chararray=VAR(___s___)->___value___;
93     int i;
94     int offset=VAR(___s___)->___offset___;
95     for(i=0;i<VAR(___s___)->___count___;i++) {
96         short sc=((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
97         putchar(sc);
98     }
99 }
100
101 #ifdef DSTM
102 void CALL00(___System______clearPrefetchCache____) {
103   prehashClear();
104 }
105 #endif
106
107 /* Object allocation function */
108
109 #ifdef DSTM
110 void * allocate_newglobal(transrecord_t *trans, int type) {
111   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(trans, classsize[type]);
112   v->type=type;
113 #ifdef THREADS
114   v->tid=0;
115   v->lockentry=0;
116   v->lockcount=0;
117 #endif
118   return v;
119 }
120
121 /* Array allocation function */
122
123 struct ArrayObject * allocate_newarrayglobal(transrecord_t *trans, int type, int length) {
124   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(trans, sizeof(struct ArrayObject)+length*classsize[type]);
125   if (length<0) {
126     printf("ERROR: negative array\n");
127     return NULL;
128   }
129   v->type=type;
130   v->___length___=length;
131 #ifdef THREADS
132   v->tid=0;
133   v->lockentry=0;
134   v->lockcount=0;
135 #endif
136   return v;
137 }
138 #endif
139
140
141 #ifdef PRECISE_GC
142 void * allocate_new(void * ptr, int type) {
143   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
144   v->type=type;
145 #ifdef THREADS
146   v->tid=0;
147   v->lockentry=0;
148   v->lockcount=0;
149 #endif
150 #ifdef OPTIONAL
151   v->fses=0;
152 #endif
153   return v;
154 }
155
156 /* Array allocation function */
157
158 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
159   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
160   v->type=type;
161   if (length<0) {
162     printf("ERROR: negative array\n");
163     return NULL;
164   }
165   v->___length___=length;
166 #ifdef THREADS
167   v->tid=0;
168   v->lockentry=0;
169   v->lockcount=0;
170 #endif
171 #ifdef OPTIONAL
172   v->fses=0;
173 #endif
174   return v;
175 }
176
177 #else
178 void * allocate_new(int type) {
179   struct ___Object___ * v=FREEMALLOC(classsize[type]);
180   v->type=type;
181 #ifdef OPTIONAL
182   v->fses=0;
183 #endif
184   return v;
185 }
186
187 /* Array allocation function */
188
189 struct ArrayObject * allocate_newarray(int type, int length) {
190   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
191   v->type=type;
192   v->___length___=length;
193 #ifdef OPTIONAL
194   v->fses=0;
195 #endif
196   return v;
197 }
198 #endif
199
200
201 /* Converts C character arrays into Java strings */
202 #ifdef PRECISE_GC
203 struct ___String___ * NewString(void * ptr, const char *str,int length) {
204 #else
205 struct ___String___ * NewString(const char *str,int length) {
206 #endif
207   int i;
208 #ifdef PRECISE_GC
209   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
210   int ptrarray[]={1, (int) ptr, (int) chararray};
211   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
212   chararray=(struct ArrayObject *) ptrarray[2];
213 #else
214   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
215   struct ___String___ * strobj=allocate_new(STRINGTYPE);
216 #endif
217   strobj->___value___=chararray;
218   strobj->___count___=length;
219   strobj->___offset___=0;
220
221   for(i=0;i<length;i++) {
222     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
223   return strobj;
224 }
225
226 /* Generated code calls this if we fail a bounds check */
227
228 void failedboundschk() {
229 #ifndef TASK
230   printf("Array out of bounds\n");
231 #ifdef THREADS
232   threadexit();
233 #else
234   exit(-1);
235 #endif
236 #else
237   longjmp(error_handler,2);
238 #endif
239 }
240
241 /* Abort task call */
242 void abort_task() {
243 #ifdef TASK
244   longjmp(error_handler,4);
245 #else
246   printf("Aborting\n");
247   exit(-1);
248 #endif
249 }