add native method for clearing prefetch cache to System.java
[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   clearObjStore();
104   prehashClear();
105 }
106 #endif
107
108 /* Object allocation function */
109
110 #ifdef DSTM
111 void * allocate_newglobal(transrecord_t *trans, int type) {
112   struct ___Object___ * v=(struct ___Object___ *) transCreateObj(trans, classsize[type]);
113   v->type=type;
114 #ifdef THREADS
115   v->tid=0;
116   v->lockentry=0;
117   v->lockcount=0;
118 #endif
119   return v;
120 }
121
122 /* Array allocation function */
123
124 struct ArrayObject * allocate_newarrayglobal(transrecord_t *trans, int type, int length) {
125   struct ArrayObject * v=(struct ArrayObject *)transCreateObj(trans, sizeof(struct ArrayObject)+length*classsize[type]);
126   if (length<0) {
127     printf("ERROR: negative array\n");
128     return NULL;
129   }
130   v->type=type;
131   v->___length___=length;
132 #ifdef THREADS
133   v->tid=0;
134   v->lockentry=0;
135   v->lockcount=0;
136 #endif
137   return v;
138 }
139 #endif
140
141
142 #ifdef PRECISE_GC
143 void * allocate_new(void * ptr, int type) {
144   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
145   v->type=type;
146 #ifdef THREADS
147   v->tid=0;
148   v->lockentry=0;
149   v->lockcount=0;
150 #endif
151 #ifdef OPTIONAL
152   v->fses=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   if (length<0) {
163     printf("ERROR: negative array\n");
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 #ifdef OPTIONAL
173   v->fses=0;
174 #endif
175   return v;
176 }
177
178 #else
179 void * allocate_new(int type) {
180   struct ___Object___ * v=FREEMALLOC(classsize[type]);
181   v->type=type;
182 #ifdef OPTIONAL
183   v->fses=0;
184 #endif
185   return v;
186 }
187
188 /* Array allocation function */
189
190 struct ArrayObject * allocate_newarray(int type, int length) {
191   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
192   v->type=type;
193   v->___length___=length;
194 #ifdef OPTIONAL
195   v->fses=0;
196 #endif
197   return v;
198 }
199 #endif
200
201
202 /* Converts C character arrays into Java strings */
203 #ifdef PRECISE_GC
204 struct ___String___ * NewString(void * ptr, const char *str,int length) {
205 #else
206 struct ___String___ * NewString(const char *str,int length) {
207 #endif
208   int i;
209 #ifdef PRECISE_GC
210   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
211   int ptrarray[]={1, (int) ptr, (int) chararray};
212   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
213   chararray=(struct ArrayObject *) ptrarray[2];
214 #else
215   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
216   struct ___String___ * strobj=allocate_new(STRINGTYPE);
217 #endif
218   strobj->___value___=chararray;
219   strobj->___count___=length;
220   strobj->___offset___=0;
221
222   for(i=0;i<length;i++) {
223     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
224   return strobj;
225 }
226
227 /* Generated code calls this if we fail a bounds check */
228
229 void failedboundschk() {
230 #ifndef TASK
231   printf("Array out of bounds\n");
232 #ifdef THREADS
233   threadexit();
234 #else
235   exit(-1);
236 #endif
237 #else
238   longjmp(error_handler,2);
239 #endif
240 }
241
242 /* Abort task call */
243 void abort_task() {
244 #ifdef TASK
245   longjmp(error_handler,4);
246 #else
247   printf("Aborting\n");
248   exit(-1);
249 #endif
250 }