2 #include "structdefs.h"
13 extern int classsize[];
14 jmp_buf error_handler;
17 #include "checkpoint.h"
19 #include "SimpleHash.h"
20 #include "GenericHashtable.h"
21 #include <sys/select.h>
22 #include <sys/socket.h>
24 #include <arpa/inet.h>
27 #include "instrument.h"
30 struct Queue * activetasks;
31 struct parameterwrapper * objectqueues[NUMCLASSES];
32 struct genhashtable * failedtasks;
34 int main(int argc, char **argv) {
41 /* Allocate startup object */
42 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
43 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
44 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
45 (int (*)(void *,void *)) &comparetpd);
47 activetasks=createQueue();
51 flagorand(startupobject,1,0xFFFFFFFF);
53 /* Build array of strings */
55 startupobject->___parameters___=stringarray;
58 int length=strlen(argv[i]);
59 struct ___String___ *newstring=NewString(argv[i],length);
60 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
66 int hashCodetpd(struct taskparamdescriptor *ftd) {
67 int hash=(int)ftd->task;
69 for(i=0;i<ftd->numParameters;i++) {
70 hash^=(int)ftd->parameterArray[i];
75 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
77 if (ftd1->task!=ftd2->task)
79 for(i=0;i<ftd1->numParameters;i++)
80 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
85 void flagorand(void * ptr, int ormask, int andmask) {
86 int flag=((int *)ptr)[1];
87 struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
91 /*Remove from all queues */
92 while(flagptr!=NULL) {
93 struct RuntimeHash *next;
94 RuntimeHashget(flagptr, (int) ptr, (int *) &next);
95 RuntimeHashremove(flagptr, (int)ptr, (int) next);
100 struct QueueItem *tmpptr;
101 struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
103 struct RuntimeHash * prevptr=NULL;
104 while(parameter!=NULL) {
105 for(i=0;i<parameter->numberofterms;i++) {
106 int andmask=parameter->intarray[i*2];
107 int checkmask=parameter->intarray[i*2+1];
108 if ((flag&andmask)==checkmask) {
109 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
110 prevptr=parameter->objectset;
112 struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
113 void * taskpointerarray[MAXTASKPARAMS];
115 int numparams=parameter->task->numParameters;
117 struct taskdescriptor * task=parameter->task;
119 for(j=0;j<numparams;j++) {
120 struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
122 taskpointerarray[j]=ptr;
125 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
126 if (RunhasNext(&iteratorarray[j])) {
127 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
128 Runnext(&iteratorarray[j]);
131 break; /* No tasks to dispatch */
135 /* Queue task items... */
138 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
140 tpd->numParameters=numparams;
141 tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
142 for(j=0;j<numparams;j++)
143 tpd->parameterArray[j]=taskpointerarray[j];
145 if (!gencontains(failedtasks, tpd))
146 addNewItem(activetasks, tpd);
148 /* This loop iterates to the next parameter combination */
149 for(j=0;j<numparams;j++) {
151 if ((j+1)==numparams)
155 if (RunhasNext(&iteratorarray[j])) {
156 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
157 Runnext(&iteratorarray[j]);
159 } else if ((j+1)!=numparams) {
160 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
171 parameter=parameter->next;
173 ((struct RuntimeHash **)ptr)[2]=prevptr;
177 /* Handler for signals */
178 void myhandler(int sig, struct __siginfo *info, void *uap) {
180 printf("sig=%d\n",sig);
183 longjmp(error_handler,1);
189 struct RuntimeHash *fdtoobject;
191 void addreadfd(int fd) {
194 FD_SET(fd, &readfds);
197 void removereadfd(int fd) {
198 FD_CLR(fd, &readfds);
199 if (maxreadfd==(fd+1)) {
201 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
206 void executetasks() {
207 void * taskpointerarray[MAXTASKPARAMS];
209 /* Set up signal handlers */
210 struct sigaction sig;
211 sig.sa_sigaction=&myhandler;
212 sig.sa_flags=SA_SIGINFO;
213 sigemptyset(&sig.sa_mask);
215 /* Catch bus errors, segmentation faults, and floating point exceptions*/
216 sigaction(SIGBUS,&sig,0);
217 sigaction(SIGSEGV,&sig,0);
218 sigaction(SIGFPE,&sig,0);
223 fdtoobject=allocateRuntimeHash(100);
225 /* Map first block of memory to protected, anonymous page */
226 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
229 while(!isEmpty(activetasks)||(maxreadfd>0)) {
233 struct timeval timeout={0,0};
236 FD_COPY(&readfds, &tmpreadfds);
237 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
239 /* Process ready fd's */
241 for(fd=0;fd<maxreadfd;fd++) {
242 if (FD_ISSET(fd, &tmpreadfds)) {
243 /* Set ready flag on object */
245 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
246 flagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
253 if (!isEmpty(activetasks)) {
255 struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
256 struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
257 removeItem(activetasks, qi);
259 for(i=0;i<tpd->task->numParameters;i++) {
260 void * parameter=tpd->parameterArray[i];
261 struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
262 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
263 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
265 taskpointerarray[i]=parameter;
268 struct RuntimeHash * forward=allocateRuntimeHash(100);
269 struct RuntimeHash * reverse=allocateRuntimeHash(100);
270 void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
271 if (setjmp(error_handler)) {
275 printf("Recovering\n");
277 genputtable(failedtasks,tpd,tpd);
278 restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
280 /* Actually call task */
281 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
288 void processtasks() {
290 for(i=0;i<numtasks;i++) {
291 struct taskdescriptor * task=taskarray[i];
294 for(j=0;j<task->numParameters;j++) {
295 struct parameterdescriptor *param=task->descriptorarray[j];
296 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
297 struct parameterwrapper ** ptr=&objectqueues[param->type];
299 param->queue=parameter;
300 parameter->objectset=allocateRuntimeHash(10);
301 parameter->numberofterms=param->numberterms;
302 parameter->intarray=param->intarray;
303 parameter->task=task;
304 /* Link new queue in */
314 int ___ServerSocket______createSocket____I(struct ___ServerSocket___ * sock, int port) {
318 struct sockaddr_in sin;
320 bzero (&sin, sizeof (sin));
321 sin.sin_family = AF_INET;
322 sin.sin_port = htons (port);
323 sin.sin_addr.s_addr = htonl (INADDR_ANY);
324 fd=socket(AF_INET, SOCK_STREAM, 0);
328 printf("createSocket error #1\n");
330 longjmp(error_handler,5);
333 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
337 printf("createSocket error #2\n");
339 longjmp(error_handler, 6);
341 fcntl(fd, F_SETFD, 1);
342 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
345 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
349 printf("createSocket error #3\n");
351 longjmp(error_handler, 7);
355 if (listen(fd, 5)<0) {
359 printf("createSocket error #4\n");
361 longjmp(error_handler, 8);
364 /* Store the fd/socket object mapping */
365 RuntimeHashadd(fdtoobject, fd, (int) sock);
370 int ___ServerSocket______nativeaccept____L___Socket___(struct ___ServerSocket___ * serversock, struct ___Socket___ * sock) {
371 struct sockaddr_in sin;
372 unsigned int sinlen=sizeof(sin);
373 int fd=serversock->___fd___;
375 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
381 printf("acceptSocket error #1\n");
383 longjmp(error_handler, 9);
385 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
387 RuntimeHashadd(fdtoobject, newfd, (int) sock);
389 flagorand(serversock,0,0xFFFFFFFE);
394 void ___Socket______nativeWrite_____AR_B(struct ___Socket___ * sock, struct ArrayObject * ao) {
395 int fd=sock->___fd___;
396 int length=ao->___length___;
397 char * charstr=((char *)& ao->___length___)+sizeof(int);
398 int bytewritten=write(fd, charstr, length);
399 if (bytewritten!=length) {
400 printf("ERROR IN NATIVEWRITE\n");
402 flagorand(sock,0,0xFFFFFFFE);
405 int ___Socket______nativeRead_____AR_B(struct ___Socket___ * sock, struct ArrayObject * ao) {
406 int fd=sock->___fd___;
407 int length=ao->___length___;
408 char * charstr=((char *)& ao->___length___)+sizeof(int);
409 int byteread=read(fd, charstr, length);
412 printf("ERROR IN NATIVEREAD\n");
414 flagorand(sock,0,0xFFFFFFFE);
418 void ___Socket______nativeClose____(struct ___Socket___ * sock) {
419 int fd=sock->___fd___;
421 RuntimeHashget(fdtoobject, fd, &data);
422 RuntimeHashremove(fdtoobject, fd, data);
425 flagorand(sock,0,0xFFFFFFFE);
429 int ___Object______hashcode____(struct ___Object___ * ___this___) {
430 return (int) ___this___;
433 void ___System______printString____L___String___(struct ___String___ * s) {
434 struct ArrayObject * chararray=s->___value___;
436 int offset=s->___offset___;
437 for(i=0;i<s->___count___;i++) {
438 short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
443 void * allocate_new(int type) {
444 void * v=FREEMALLOC(classsize[type]);
449 struct ArrayObject * allocate_newarray(int type, int length) {
450 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
452 v->___length___=length;
456 struct ___String___ * NewString(const char *str,int length) {
457 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
458 struct ___String___ * strobj=allocate_new(STRINGTYPE);
460 strobj->___value___=chararray;
461 strobj->___count___=length;
462 strobj->___offset___=0;
464 for(i=0;i<length;i++) {
465 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
469 void failedboundschk() {
471 printf("Array out of bounds\n");
474 longjmp(error_handler,2);
478 void failednullptr() {
480 printf("Dereferenced a null pointer\n");
483 longjmp(error_handler,3);
489 printf("Aborting\n");
492 longjmp(error_handler,4);