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