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