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