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