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"
22 #include "instrument.h"
25 struct Queue * activetasks;
26 struct parameterwrapper * objectqueues[NUMCLASSES];
27 struct genhashtable * failedtasks;
29 int main(int argc, char **argv) {
36 /* Allocate startup object */
37 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
38 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
39 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
40 (int (*)(void *,void *)) &comparetpd);
42 activetasks=createQueue();
46 flagorand(startupobject,1,0xFFFFFFFF);
48 /* Build array of strings */
50 startupobject->___parameters___=stringarray;
53 int length=strlen(argv[i]);
54 struct ___String___ *newstring=NewString(argv[i],length);
55 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
61 int hashCodetpd(struct taskparamdescriptor *ftd) {
62 int hash=(int)ftd->task;
64 for(i=0;i<ftd->numParameters;i++) {
65 hash^=(int)ftd->parameterArray[i];
70 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
72 if (ftd1->task!=ftd2->task)
74 for(i=0;i<ftd1->numParameters;i++)
75 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
80 void flagorand(void * ptr, int ormask, int andmask) {
81 int flag=((int *)ptr)[1];
82 struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
86 /*Remove from all queues */
87 while(flagptr!=NULL) {
88 struct RuntimeHash *next;
89 RuntimeHashget(flagptr, (int) ptr, (int *) &next);
90 RuntimeHashremove(flagptr, (int)ptr, (int) next);
95 struct QueueItem *tmpptr;
96 struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
98 struct RuntimeHash * prevptr=NULL;
99 while(parameter!=NULL) {
100 for(i=0;i<parameter->numberofterms;i++) {
101 int andmask=parameter->intarray[i*2];
102 int checkmask=parameter->intarray[i*2+1];
103 if ((flag&andmask)==checkmask) {
104 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
105 prevptr=parameter->objectset;
107 struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
108 void * taskpointerarray[MAXTASKPARAMS];
110 int numparams=parameter->task->numParameters;
112 struct taskdescriptor * task=parameter->task;
114 for(j=0;j<numparams;j++) {
115 struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
117 taskpointerarray[j]=ptr;
120 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
121 if (RunhasNext(&iteratorarray[j]))
122 taskpointerarray[j]=(void *) Runnext(&iteratorarray[j]);
124 break; /* No tasks to dispatch */
127 /* Queue task items... */
130 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
132 tpd->numParameters=numparams;
133 tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
134 for(j=0;j<numparams;j++)
135 tpd->parameterArray[j]=taskpointerarray[j];
137 if (!gencontains(failedtasks, tpd))
138 addNewItem(activetasks, tpd);
140 /* This loop iterates to the next paramter combination */
141 for(j=0;j<numparams;j++) {
143 if ((j+1)==numparams)
147 if (RunhasNext(&iteratorarray[j])) {
148 taskpointerarray[j]=(void *) Runnext(&iteratorarray[j]);
150 } else if ((j+1)!=numparams) {
151 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
162 parameter=parameter->next;
164 ((struct RuntimeHash **)ptr)[2]=prevptr;
168 /* Handler for signals */
169 void myhandler(int sig, struct __siginfo *info, void *uap) {
171 printf("sig=%d\n",sig);
174 longjmp(error_handler,1);
177 void executetasks() {
178 void * taskpointerarray[MAXTASKPARAMS];
180 /* Set up signal handlers */
181 struct sigaction sig;
182 sig.sa_sigaction=&myhandler;
183 sig.sa_flags=SA_SIGINFO;
184 sigemptyset(&sig.sa_mask);
186 /* Catch bus errors, segmentation faults, and floating point exceptions*/
187 sigaction(SIGBUS,&sig,0);
188 sigaction(SIGSEGV,&sig,0);
189 sigaction(SIGFPE,&sig,0);
191 /* Map first block of memory to protected, anonymous page */
192 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
195 while(!isEmpty(activetasks)) {
196 struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
197 struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
199 removeItem(activetasks, qi);
201 for(i=0;i<tpd->task->numParameters;i++) {
202 void * parameter=tpd->parameterArray[i];
203 struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
204 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
205 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
207 taskpointerarray[i]=parameter;
210 struct RuntimeHash * forward=allocateRuntimeHash(100);
211 struct RuntimeHash * reverse=allocateRuntimeHash(100);
212 void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
213 if (setjmp(error_handler)) {
217 printf("Recovering\n");
219 genputtable(failedtasks,tpd,tpd);
220 restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
222 /* Actually call task */
223 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
229 void processtasks() {
231 for(i=0;i<numtasks;i++) {
232 struct taskdescriptor * task=taskarray[i];
235 for(j=0;j<task->numParameters;j++) {
236 struct parameterdescriptor *param=task->descriptorarray[j];
237 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
238 struct parameterwrapper ** ptr=&objectqueues[param->type];
240 param->queue=parameter;
241 parameter->objectset=allocateRuntimeHash(10);
242 parameter->numberofterms=param->numberterms;
243 parameter->intarray=param->intarray;
244 parameter->task=task;
245 /* Link new queue in */
254 int ___Object______hashcode____(struct ___Object___ * ___this___) {
255 return (int) ___this___;
258 void ___System______printString____L___String___(struct ___String___ * s) {
259 struct ArrayObject * chararray=s->___string___;
261 for(i=0;i<chararray->___length___;i++) {
262 short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
267 void * allocate_new(int type) {
268 void * v=FREEMALLOC(classsize[type]);
273 struct ArrayObject * allocate_newarray(int type, int length) {
274 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
276 v->___length___=length;
280 struct ___String___ * NewString(const char *str,int length) {
281 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
282 struct ___String___ * strobj=allocate_new(STRINGTYPE);
284 strobj->___string___=chararray;
285 for(i=0;i<length;i++) {
286 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
290 void failedboundschk() {
292 printf("Array out of bounds\n");
295 longjmp(error_handler,2);
299 void failednullptr() {
301 printf("Dereferenced a null pointer\n");
304 longjmp(error_handler,3);
310 printf("Aborting\n");
313 longjmp(error_handler,4);