add codes for generating multi-core version binary. Also add options -multicore ...
[IRC.git] / Robust / src / Runtime / task.c
1 #ifdef TASK
2 #include "runtime.h"
3 #include "structdefs.h"
4 #include "mem.h"
5 #include "checkpoint.h"
6 #include "Queue.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
11 #include <sys/mman.h>
12 #include <string.h>
13 #include <signal.h>
14
15 extern int injectfailures;
16 extern float failurechance;
17 extern int debugtask;
18 extern int instaccum;
19
20 #ifdef CONSCHECK
21 #include "instrument.h"
22 #endif
23
24 struct genhashtable * activetasks;
25 #ifndef MULTICORE
26 struct parameterwrapper * objectqueues[NUMCLASSES];
27 #endif
28 struct genhashtable * failedtasks;
29 struct taskparamdescriptor * currtpd;
30 struct RuntimeHash * forward;
31 struct RuntimeHash * reverse;
32
33 int main(int argc, char **argv) {
34 #ifdef BOEHM_GC
35   GC_init(); // Initialize the garbage collector
36 #endif
37 #ifdef CONSCHECK
38   initializemmap();
39 #endif
40   processOptions();
41   initializeexithandler();
42   /* Create table for failed tasks */
43   failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
44                                    (int (*)(void *,void *)) &comparetpd);
45   /* Create queue of active tasks */
46   activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
47                                    (int (*)(void *,void *)) &comparetpd);
48   
49   /* Process task information */
50   processtasks();
51
52   /* Create startup object */
53   createstartupobject(argc, argv);
54
55   /* Start executing the tasks */
56   executetasks();
57 }
58
59 void createstartupobject(int argc, char ** argv) {
60   int i;
61   
62   /* Allocate startup object     */
63 #ifdef PRECISE_GC
64   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
65   struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1); 
66 #else
67   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
68   struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1); 
69 #endif
70   /* Build array of strings */
71   startupobject->___parameters___=stringarray;
72   for(i=1;i<argc;i++) {
73     int length=strlen(argv[i]);
74 #ifdef PRECISE_GC
75     struct ___String___ *newstring=NewString(NULL, argv[i],length);
76 #else
77     struct ___String___ *newstring=NewString(argv[i],length);
78 #endif
79     ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
80   }
81   
82   /* Set initialized flag for startup object */ 
83 #ifdef MULTICORE
84   flagorand(startupobject,1,0xFFFFFFFF,NULL,0);
85   enqueueObject(startupobject, objq4startupobj[corenum], numqueues4startupobj[corenum]);
86 #else
87   flagorand(startupobject,1,0xFFFFFFFF);
88   enqueueObject(startupobject);
89 #endif
90 }
91
92 int hashCodetpd(struct taskparamdescriptor *ftd) {
93   int hash=(int)ftd->task;
94   int i;
95   for(i=0;i<ftd->numParameters;i++){ 
96     hash^=(int)ftd->parameterArray[i];
97   }
98   return hash;
99 }
100
101 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
102   int i;
103   if (ftd1->task!=ftd2->task)
104     return 0;
105   for(i=0;i<ftd1->numParameters;i++)
106     if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
107       return 0;
108 #ifdef OPTIONAL
109   for(i=0;i<ftd1->numParameters;i++) {
110     if(ftd1->failed[i]!=ftd2->failed[i])
111       return 0;
112   }
113 #endif
114   return 1;
115 }
116
117 /* This function sets a tag. */
118 #ifdef PRECISE_GC
119 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
120 #else
121 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
122 #endif
123   struct ___Object___ * tagptr=obj->___tags___;
124   if (tagptr==NULL) {
125     obj->___tags___=(struct ___Object___ *)tagd;
126   } else {
127     /* Have to check if it is already set */
128     if (tagptr->type==TAGTYPE) {
129       struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
130       if (td==tagd)
131         return;
132 #ifdef PRECISE_GC
133       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
134       struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
135       obj=(struct ___Object___ *)ptrarray[2];
136       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
137       td=(struct ___TagDescriptor___ *) obj->___tags___;
138 #else
139       struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
140 #endif
141       ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
142       ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
143       obj->___tags___=(struct ___Object___ *) ao;
144       ao->___cachedCode___=2;
145     } else {
146       /* Array Case */
147       int i;
148       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
149       for(i=0;i<ao->___cachedCode___;i++) {
150         struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
151         if (td==tagd)
152           return;
153       }
154       if (ao->___cachedCode___<ao->___length___) {
155         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
156         ao->___cachedCode___++;
157       } else {
158 #ifdef PRECISE_GC
159         int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
160         struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
161         obj=(struct ___Object___ *)ptrarray[2];
162         tagd=(struct ___TagDescriptor___ *) ptrarray[3];
163         ao=(struct ArrayObject *)obj->___tags___;
164 #else
165         struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
166 #endif
167         aonew->___cachedCode___=ao->___length___+1;
168         for(i=0;i<ao->___length___;i++) {
169           ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
170         }
171         ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
172       }
173     }
174   }
175
176   {
177     struct ___Object___ * tagset=tagd->flagptr;
178     if(tagset==NULL) {
179       tagd->flagptr=obj;
180     } else if (tagset->type!=OBJECTARRAYTYPE) {
181 #ifdef PRECISE_GC
182       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
183       struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
184       obj=(struct ___Object___ *)ptrarray[2];
185       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
186 #else
187       struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
188 #endif
189       ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
190       ARRAYSET(ao, struct ___Object___ *, 1, obj);
191       ao->___cachedCode___=2;
192       tagd->flagptr=(struct ___Object___ *)ao;
193     } else {
194       struct ArrayObject *ao=(struct ArrayObject *) tagset;
195       if (ao->___cachedCode___<ao->___length___) {
196         ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
197       } else {
198         int i;
199 #ifdef PRECISE_GC
200         int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
201         struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
202         obj=(struct ___Object___ *)ptrarray[2];
203         tagd=(struct ___TagDescriptor___ *)ptrarray[3];
204         ao=(struct ArrayObject *)tagd->flagptr;
205 #else
206         struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
207 #endif
208         aonew->___cachedCode___=ao->___cachedCode___+1;
209         for(i=0;i<ao->___length___;i++) {
210           ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
211         }
212         ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
213         tagd->flagptr=(struct ___Object___ *) aonew;
214       }
215     }
216   }
217 }
218
219 /* This function clears a tag. */
220 #ifdef PRECISE_GC
221 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
222 #else
223 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
224 #endif
225   /* We'll assume that tag is alway there.
226      Need to statically check for this of course. */
227   struct ___Object___ * tagptr=obj->___tags___;
228
229   if (tagptr->type==TAGTYPE) {
230     if ((struct ___TagDescriptor___ *)tagptr==tagd)
231       obj->___tags___=NULL;
232     else
233       printf("ERROR 1 in tagclear\n");
234   } else {
235     struct ArrayObject *ao=(struct ArrayObject *) tagptr;
236     int i;
237     for(i=0;i<ao->___cachedCode___;i++) {
238       struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
239       if (td==tagd) {
240         ao->___cachedCode___--;
241         if (i<ao->___cachedCode___)
242           ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
243         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
244         if (ao->___cachedCode___==0)
245           obj->___tags___=NULL;
246         goto PROCESSCLEAR;
247       }
248     }
249     printf("ERROR 2 in tagclear\n");
250   }
251  PROCESSCLEAR:
252   {
253     struct ___Object___ *tagset=tagd->flagptr;
254     if (tagset->type!=OBJECTARRAYTYPE) {
255       if (tagset==obj)
256         tagd->flagptr=NULL;
257       else
258         printf("ERROR 3 in tagclear\n");
259     } else {
260       struct ArrayObject *ao=(struct ArrayObject *) tagset;
261       int i;
262       for(i=0;i<ao->___cachedCode___;i++) {
263         struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
264         if (tobj==obj) {
265           ao->___cachedCode___--;
266           if (i<ao->___cachedCode___)
267             ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
268           ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
269           if (ao->___cachedCode___==0)
270             tagd->flagptr=NULL;
271           goto ENDCLEAR;
272         }
273       }
274       printf("ERROR 4 in tagclear\n");
275     }
276   }
277  ENDCLEAR:
278   return;
279 }
280  
281 /* This function allocates a new tag. */
282 #ifdef PRECISE_GC
283 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
284   struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
285 #else
286 struct ___TagDescriptor___ * allocate_tag(int index) {
287   struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
288 #endif
289   v->type=TAGTYPE;
290   v->flag=index;
291   return v;
292
293
294
295
296 /* This function updates the flag for object ptr.  It or's the flag
297    with the or mask and and's it with the andmask. */
298
299 #ifdef MULTICORE
300 void flagbody(struct ___Object___ *ptr, int flag, struct parameterwrapper ** queues, int length);
301 #else
302 void flagbody(struct ___Object___ *ptr, int flag);
303 #endif
304 #ifdef OPTIONAL
305 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
306 #endif
307  
308  int flagcomp(const int *val1, const int *val2) {
309    return (*val1)-(*val2);
310  } 
311
312 #ifdef MULTICORE
313 void flagorand(void * ptr, int ormask, int andmask, struct parameterwrapper ** queues, int length) {
314 #else
315 void flagorand(void * ptr, int ormask, int andmask) {
316 #endif
317 #ifdef OPTIONAL
318   struct ___Object___ * obj = (struct ___Object___ *)ptr;
319   if(obj->numfses){/*store the information about fses*/
320     int flag, i, j,counter, offset=0;
321     for(i=0;i<obj->numfses;i++) {
322       int oldoffset;
323       counter=obj->fses[offset++];
324       oldoffset=offset;
325       for(j=0;j<counter;j++) {
326         flag=obj->fses[offset];
327         obj->fses[offset++]=(flag|ormask)&andmask;
328       }
329       qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
330     }
331     enqueueoptional(obj, 0, NULL, NULL, 0);
332   }
333   else
334 #endif
335     {
336       int oldflag=((int *)ptr)[1];
337       int flag=ormask|oldflag;
338       flag&=andmask;
339 #ifdef MULTICORE
340           flagbody(ptr, flag, queues, length);
341 #else
342       flagbody(ptr, flag);
343 #endif
344     }
345 }
346  
347 bool intflagorand(void * ptr, int ormask, int andmask) {
348 #ifdef OPTIONAL
349   struct ___Object___ * obj = (struct ___Object___ *)ptr;
350   if(obj->numfses) {/*store the information about fses*/
351     int flag, i, j,counter, offset=0;
352     for(i=0;i<obj->numfses;i++) {
353       int oldoffset;
354       counter=obj->fses[offset++];
355       oldoffset=offset;
356       for(j=0;j<counter;j++) {
357         flag=obj->fses[offset];
358         obj->fses[offset++]=(flag|ormask)&andmask;
359       }
360       qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
361     }
362     enqueueoptional(obj, 0, NULL, NULL, 0);
363   }
364   else
365 #endif
366     {
367       int oldflag=((int *)ptr)[1];
368       int flag=ormask|oldflag;
369       flag&=andmask;
370       if (flag==oldflag) /* Don't do anything */
371         return false;
372       else {
373 #ifdef MULTICORE
374                  flagbody(ptr, flag, NULL, 0);
375 #else
376                  flagbody(ptr, flag);
377 #endif
378                  return true;
379           }
380     }
381 }
382
383 void flagorandinit(void * ptr, int ormask, int andmask) {
384   int oldflag=((int *)ptr)[1];
385   int flag=ormask|oldflag;
386   flag&=andmask;
387 #ifdef MULTICORE
388   flagbody(ptr,flag,NULL,0);
389 #else
390   flagbody(ptr,flag);
391 #endif
392 }
393
394 #ifdef MULTICORE
395 void flagbody(struct ___Object___ *ptr, int flag, struct parameterwrapper ** queues, int length) {
396 #else
397 void flagbody(struct ___Object___ *ptr, int flag) {
398 #endif
399 #ifdef MULTICORE
400   struct parameterwrapper * flagptr = NULL;
401   int i = 0;
402 #else
403   struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
404 #endif
405   ptr->flag=flag;
406   
407   /*Remove object from all queues */
408 #ifdef MULTICORE
409   for(i = 0; i < length; ++i) {
410           flagptr = queues[i];
411 #else
412   while(flagptr!=NULL) {
413 #endif
414 #ifdef MULTICORE
415         int next;
416 #else
417     struct parameterwrapper *next;
418 #endif
419     int UNUSED, UNUSED2;
420     int * enterflags;
421     ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
422     ObjectHashremove(flagptr->objectset, (int)ptr);
423     if (enterflags!=NULL)
424       free(enterflags);
425 #ifdef MULTICORE
426         ;
427 #else
428     flagptr=next;
429 #endif
430   }
431  }
432
433 #ifdef MULTICORE
434  void enqueueObject(void * vptr, struct parameterwrapper ** queues, int length) {
435 #else
436  void enqueueObject(void *vptr) {
437 #endif
438    struct ___Object___ *ptr = (struct ___Object___ *)vptr;
439   
440   {
441     struct QueueItem *tmpptr;
442 #ifdef MULTICORE
443         struct parameterwrapper * parameter=NULL;
444         int j;
445 #else
446     struct parameterwrapper * parameter=objectqueues[ptr->type];
447 #endif
448     int i;
449     struct parameterwrapper * prevptr=NULL;
450     struct ___Object___ *tagptr=ptr->___tags___;
451     
452     /* Outer loop iterates through all parameter queues an object of
453        this type could be in.  */
454     
455 #ifdef MULTICORE
456         for(j = 0; j < length; ++j) {
457                 parameter = queues[j];
458 #else
459     while(parameter!=NULL) {
460 #endif
461       /* Check tags */
462       if (parameter->numbertags>0) {
463         if (tagptr==NULL)
464           goto nextloop;//that means the object has no tag but that param needs tag
465         else if(tagptr->type==TAGTYPE) {//one tag
466           struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
467           for(i=0;i<parameter->numbertags;i++) {
468             //slotid is parameter->tagarray[2*i];
469             int tagid=parameter->tagarray[2*i+1];
470             if (tagid!=tagptr->flag)
471               goto nextloop; /*We don't have this tag */          
472            }
473         } else {//multiple tags
474           struct ArrayObject * ao=(struct ArrayObject *) tagptr;
475           for(i=0;i<parameter->numbertags;i++) {
476             //slotid is parameter->tagarray[2*i];
477             int tagid=parameter->tagarray[2*i+1];
478             int j;
479             for(j=0;j<ao->___cachedCode___;j++) {
480               if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
481                 goto foundtag;
482             }
483             goto nextloop;
484           foundtag:
485             ;
486           }
487         }
488       }
489       
490       /* Check flags */
491       for(i=0;i<parameter->numberofterms;i++) {
492         int andmask=parameter->intarray[i*2];
493         int checkmask=parameter->intarray[i*2+1];
494         if ((ptr->flag&andmask)==checkmask) {
495           enqueuetasks(parameter, prevptr, ptr, NULL, 0);
496           prevptr=parameter;
497           break;
498         }
499       }
500     nextloop:
501 #ifdef MULTICORE
502           ;
503 #else
504       parameter=parameter->next;
505 #endif
506     }
507 #ifdef MULTICORE
508         /*if(prevptr != NULL) {
509           ptr->flagptr=prevptr->arrayindex;
510         } else {
511           ptr->flagptr = 0;
512         }*/
513 #else
514     ptr->flagptr=prevptr;
515 #endif
516   }
517 }
518
519 #ifdef MULTICORE
520
521 void transferObject(void * obj, int targetcore) {
522         int type=((int *)obj)[0];
523  // if (type<NUMCLASSES) {
524     /* We have a normal object */
525     int size=classsize[type];
526
527 #ifdef RAW
528 #endif
529         
530     //struct ___Object___ * newobj=FREEMALLOC(size);
531     //memcpy(newobj, obj, size);
532     //obj->___localcopy___=newobj;
533   //} else {
534     /* We have an array */
535     /*struct ArrayObject *ao=(struct ArrayObject *)obj;
536     int elementsize=classsize[type];
537     int length=ao->___length___;
538     int size=sizeof(struct ArrayObject)+length*elementsize;
539     struct ___Object___ * newobj=FREEMALLOC(size);
540     memcpy(newobj, obj, size);
541     obj->___localcopy___=newobj;
542   }*/
543 }
544
545 #endif
546
547 #ifdef OPTIONAL
548
549 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
550   /* Check Tags */
551   struct ___Object___ * tagptr = currobj->___tags___;
552   if(fswrapper->numtags>0){
553     if (tagptr==NULL)
554       return 0; //that means the object has no tag but that param
555     //needs tag
556     else if(tagptr->type==TAGTYPE) {//one tag
557       if(fswrapper->numtags!=1) 
558         return 0; //we don't have the right number of tags
559       struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
560       if (fswrapper->tags[0]!=tagptr->flag)
561         return 0;
562     } else {  //multiple tags
563       struct ArrayObject * ao=(struct ArrayObject *) tagptr;
564       int tag_counter=0;
565       int foundtag=0;
566       
567       if(ao->___length___!=fswrapper->numtags) 
568         return 0;//we don't have the right number of tags
569       for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
570         int tagid=fswrapper->tags[tag_counter];
571         int j;
572         for(j=0;j<ao->___cachedCode___;j++) {
573           if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
574             return 1;
575         }
576         return 0;
577       }
578     }
579   }
580   return 1;
581 }
582
583 int getlength(int *flist, int len) {
584   int count=0;
585   int i;
586   for(i=0;i<len;i++) {
587     int size=flist[count];
588     count+=1+size;
589   }
590   return count;
591 }
592
593 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
594   int size1=getlength(flist1, len1);
595   int size2=getlength(flist2, len2);
596   int *merge=RUNMALLOC((size1+size2)*sizeof(int));
597   memcpy(merge, flist1, size1*sizeof(int));
598   memcpy(&merge[size1], flist2, size2*sizeof(int));
599   return merge;
600 }
601
602 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
603   int count=0;
604   int i=0;
605   int j=0;
606   while(i<len1||j<len2) {
607     if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
608       if(merge!=NULL) {
609         merge[count]=flist1[i];
610       }
611       i++;
612       count++;
613     } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
614       if(merge!=NULL) {
615         merge[count]=flist2[j];
616       }
617       j++;
618       count++;
619     } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
620       if(merge!=NULL) {
621         merge[count]=flist1[i];
622       }
623       i++;
624       j++;
625       count++;
626     }
627   }
628   return count;
629 }
630
631 /* Merge flags from ftlmerge into ftl. */
632 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
633   int length=0;
634   int i,j;
635   int *mergedlist;
636   int offset=0;
637   for(i=0;i<ftl->numflags;i++) {
638     int len=ftl->flags[offset++];
639     int offsetmerge=0;
640     for(j=0;j<ftlmerge->numflags;j++) {
641       int lenmerge=ftlmerge->flags[offsetmerge++];
642       length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
643       offsetmerge+=lenmerge;
644     }
645     offset+=len;
646   }
647   mergedlist=RUNMALLOC(sizeof(int)*length);
648   
649   offset=0;
650   length=0;
651   for(i=0;i<ftl->numflags;i++) {
652     int len=ftl->flags[offset++];
653     int offsetmerge=0;
654     for(j=0;j<ftlmerge->numflags;j++) {
655       int lenmerge=ftlmerge->flags[offsetmerge++];
656       int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
657       mergedlist[length]=size;
658       length+=size+1;
659     }
660   }
661   RUNFREE(ftl->flags);
662   ftl->flags=mergedlist;
663   ftl->numflags*=ftlmerge->numflags;
664 }
665
666 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
667   struct failedtasklist *tmpptr;
668   while((*andlist)!=NULL) {
669     struct failedtasklist *searchftl=list;
670     while(searchftl!=NULL) {
671       if ((*andlist)->task==searchftl->task&&
672           (*andlist)->index==searchftl->index) {
673         mergeitems(*andlist, searchftl);
674         break;
675       }
676       searchftl=searchftl->next;
677     }
678     if (searchftl==NULL) {
679       //didn't find andlist
680       tmpptr=*andlist;
681       *andlist=(*andlist)->next;//splice item out of list
682       RUNFREE(tmpptr->flags); //free the item
683       RUNFREE(tmpptr);
684     } else {
685       andlist=&((*andlist)->next); //iterate to next item
686     }
687   }
688   //free the list we're searching
689   while(list!=NULL) {
690     tmpptr=list->next;
691     RUNFREE(list->flags);
692     RUNFREE(list);
693     list=tmpptr;
694   }
695 }
696
697 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
698   struct failedtasklist *list=NULL;
699   int i,h;
700   struct fsanalysiswrapper *fswrapper=NULL;
701   for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
702     struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
703     if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
704       //we only match exactly here
705       fswrapper=tmp;
706       break;
707     }
708   }
709   if (fswrapper==NULL)
710     return list;
711   for(i=0;i<fswrapper->numtaskfailures;i++) {
712     int j;
713     struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
714     if (taskfail->task==task&&taskfail->index==index) {
715       int start=0;
716       while(start<taskfail->numoptionaltaskdescriptors) {
717         struct taskdescriptor *currtask=NULL;
718         struct failedtasklist *tmpftl;
719         int currindex;
720         int totallength=0;
721         int *enterflags;
722         int numenterflags, offset;
723         struct parameterwrapper *pw;
724         for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
725           struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
726           if(currtask==NULL) {
727             currtask=otd->task;
728             currindex=otd->index;
729           } else if (currtask!=otd->task||currindex!=otd->index)
730             break;
731           totallength+=otd->numenterflags;
732         }
733         pw=currtask->descriptorarray[currindex]->queue;
734         enterflags=RUNMALLOC(totallength*sizeof(int));
735         numenterflags=j-start;
736         offset=0;
737         for(start;start<j;start++) {
738           struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
739           enterflags[offset++]=otd->numenterflags;
740           memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
741           offset+=otd->numenterflags;
742         }
743         tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
744         tmpftl->next=list;
745         tmpftl->task=currtask;
746         tmpftl->numflags=numenterflags;
747         tmpftl->flags=enterflags;
748         list=tmpftl;
749       }
750     }
751   }
752   return list;
753 }
754
755 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
756   struct failedtasklist *list=NULL;
757   int i,h;
758   int start=0;
759   struct fsanalysiswrapper *fswrapper=NULL;
760   for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
761     struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
762     if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
763       //we only match exactly here
764       fswrapper=tmp;
765       break;
766     }
767   }
768   if(fswrapper==NULL)
769     return NULL;
770
771   while(start<fswrapper->numoptionaltaskdescriptors) {
772     struct taskdescriptor *currtask=NULL;
773     struct failedtasklist *tmpftl;
774     int j;
775     int currindex;
776     int totallength=0;
777     int *enterflags;
778     int numenterflags, offset;
779     struct parameterwrapper *pw;
780     for(j=start;j<fswrapper->numoptionaltaskdescriptors;j++) {
781       struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
782       if(currtask==NULL) {
783         currtask=otd->task;
784         currindex=otd->index;
785       } else if (currtask!=otd->task||currindex!=otd->index)
786         break;
787       totallength+=otd->numenterflags;
788     }
789     pw=currtask->descriptorarray[currindex]->queue;
790     enterflags=RUNMALLOC(totallength*sizeof(int));
791     numenterflags=j-start;
792     offset=0;
793     for(start;start<j;start++) {
794       struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
795       enterflags[offset++]=otd->numenterflags;
796       memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
797       offset+=otd->numenterflags;
798     }
799     tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
800     tmpftl->next=list;
801     tmpftl->task=currtask;
802     tmpftl->numflags=numenterflags;
803     tmpftl->flags=enterflags;
804     list=tmpftl;
805   }
806   return list;
807 }
808
809
810
811 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
812   while(andlist!=NULL) {
813     struct failedtasklist *tmp=andlist;
814     struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
815     struct parmaeterwrapper *next;
816     int * flags;
817     int numflags;
818     int isnonfailed;
819     
820     if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
821       currobj->flagptr=pw;
822     
823     andlist=andlist->next;
824     RUNFREE(tmp);
825   }
826 }
827
828 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
829   struct classanalysiswrapper * classwrapper=NULL; 
830   
831   /*test what optionaltaskdescriptors are available, find the class
832     corresponding*/
833   if (classanalysiswrapperarray[currobj->type]!=NULL) {
834     classwrapper = classanalysiswrapperarray[currobj->type];
835   } else
836     return;
837   
838   if(task!=NULL) { 
839     /* We have a failure */
840     if (failedfses==NULL) {
841       /* Failed in normal state */
842       /*first time the method is invoked*/
843       int i,h;
844       struct fsanalysiswrapper *fswrapper=NULL;
845
846       for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
847         struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
848         if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
849           //we only match exactly here
850           fswrapper=tmp;
851           break;
852         }
853       }
854       if(fswrapper==NULL) //nothing to do in this state
855         return;
856       for(i=0;i<fswrapper->numtaskfailures;i++) {
857         int j;
858         struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
859         if (taskfail->task==task&&taskfail->index==index) {
860           int start=0;
861           while(start<taskfail->numoptionaltaskdescriptors) {
862             struct taskdescriptor *currtask=NULL;
863             int currindex;
864             int totallength=0;
865             int *enterflags;
866             int numenterflags, offset;
867             struct parameterwrapper *pw;
868             for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
869               struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
870               if(currtask==NULL) {
871                 currtask=otd->task;
872                 currindex=otd->index;
873               } else if (currtask!=otd->task||currindex!=otd->index)
874                 break;
875               totallength+=otd->numenterflags;//1 is to store the lengths
876             }
877             pw=currtask->descriptorarray[currindex]->queue;
878             enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
879             numenterflags=j-start;
880
881             offset=0;
882             for(start;start<j;start++) {
883               struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
884               enterflags[offset++]=otd->numenterflags;
885               memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
886               offset+=otd->numenterflags;
887             }
888             //Enqueue this one
889             if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
890               currobj->flagptr=pw;
891           }
892         }
893       }
894     } else {
895       /* Failed in failed state */
896       int i;
897       int offset=0;
898       for(i=0;i<numfailedfses;i++) {
899         int numfses=failedfses[offset++];
900         int j;
901         struct failedtasklist *andlist=NULL;
902         for(j=0;j<numfses;j++) {
903           int flagstate=failedfses[offset++];
904           struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
905           if (andlist==NULL)
906             andlist=currlist;
907           else
908             mergefailedlists(&andlist, currlist);
909         }
910         enqueuelist(currobj, andlist);
911       }
912     }
913   } else {
914     /* No failure, but we are in a failed state */
915     struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
916
917     /*Remove object from all queues */
918     while(flagptr!=NULL) {
919       struct parameterwrapper *next;
920       int UNUSED, UNUSED2;
921       int * enterflags;
922       ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
923       ObjectHashremove(flagptr->objectset, (int)currobj);
924       if (enterflags!=NULL)
925         free(enterflags);
926       flagptr=next;
927     }
928
929     /* Failed in failed state */
930     int i;
931     int offset=0;
932     for(i=0;i<currobj->numfses;i++) {
933       int numfses=currobj->fses[offset++];
934       int j;
935       struct failedtasklist *andlist=NULL;
936       for(j=0;j<numfses;j++) {
937         int flagstate=currobj->fses[offset++];
938         struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
939         if (andlist==NULL)
940           andlist=currlist;
941         else
942           mergefailedlists(&andlist, currlist);
943       }
944       enqueuelist(currobj, andlist);
945     }
946   }
947
948  
949  
950 #endif
951  
952 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
953   void * taskpointerarray[MAXTASKPARAMS];
954 #ifdef OPTIONAL
955   int failed[MAXTASKPARAMS];
956 #endif
957   int j;
958   int numparams=parameter->task->numParameters;
959   int numiterators=parameter->task->numTotal-1;
960   int retval=1;
961   int addnormal=1;
962   int adderror=1;
963
964   struct taskdescriptor * task=parameter->task;
965
966 #ifdef OPTIONAL  
967   if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
968     /* The object is already here...or it with the existing item */
969     int * oldflags;
970     int oldnumflags;
971     int oldptr;
972     int oldstatus;
973     int *mergedflags;
974     ObjectHashget(parameter->objectset, (int) ptr, & oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
975     mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
976     ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
977
978     RUNFREE(oldflags);
979     RUNFREE(enterflags);
980
981     //only add if truly needed
982     if (oldstatus)
983       addnormal=0;
984     if (oldnumflags>0)
985       adderror=0;
986
987     retval=0;
988   } else {
989 #endif
990 #ifdef MULTICORE
991         /*int arrayindex = 0;
992         if(prevptr != NULL) {
993                 arrayindex = prevptr->arrayindex;
994         }*/
995         ObjectHashadd(parameter->objectset, (int) ptr, 0, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
996 #else
997     ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
998 #endif
999 #ifdef OPTIONAL
1000   }
1001 #endif
1002  
1003   /* Add enqueued object to parameter vector */
1004   taskpointerarray[parameter->slot]=ptr;
1005 #ifdef OPTIONAL
1006   failed[parameter->slot]=(enterflags!=NULL);
1007 #endif
1008
1009   /* Reset iterators */
1010   for(j=0;j<numiterators;j++) {
1011     toiReset(&parameter->iterators[j]);
1012   }
1013
1014   /* Find initial state */
1015   for(j=0;j<numiterators;j++) {
1016   backtrackinit:
1017     if(toiHasNext(&parameter->iterators[j], taskpointerarray OPTARG(failed)))
1018       toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
1019     else if (j>0) {
1020       /* Need to backtrack */
1021       toiReset(&parameter->iterators[j]);
1022       j--;
1023       goto backtrackinit;
1024     } else {
1025       /* Nothing to enqueue */
1026       return retval;
1027     }
1028   }
1029
1030   
1031   while(1) {
1032     /* Enqueue current state */
1033     int launch = 0;
1034     struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
1035     tpd->task=task;
1036     tpd->numParameters=numiterators+1;
1037     tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
1038 #ifdef OPTIONAL
1039     tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
1040 #endif
1041     for(j=0;j<=numiterators;j++){
1042       tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
1043 #ifdef OPTIONAL
1044       tpd->failed[j]=failed[j];
1045       if (failed[j]!=0&&failed[j]!=1) {
1046         printf("BAD\n");
1047       }
1048 #endif
1049     }
1050     /* Enqueue task */
1051     if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
1052       genputtable(activetasks, tpd, tpd);
1053     } else {
1054       RUNFREE(tpd->parameterArray);
1055 #ifdef OPTIONAL
1056       RUNFREE(tpd->failed);
1057 #endif
1058       RUNFREE(tpd);
1059     }
1060     
1061     /* This loop iterates to the next parameter combination */
1062     if (numiterators==0)
1063       return retval;
1064
1065     for(j=numiterators-1; j<numiterators;j++) {
1066     backtrackinc:
1067       if(toiHasNext(&parameter->iterators[j], taskpointerarray OPTARG(failed)))
1068         toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
1069       else if (j>0) {
1070         /* Need to backtrack */
1071         toiReset(&parameter->iterators[j]);
1072         j--;
1073         goto backtrackinc;
1074       } else {
1075         /* Nothing more to enqueue */
1076         return retval;
1077       }
1078     }
1079   }
1080   return retval;
1081 }
1082  
1083 /* Handler for signals. The signals catch null pointer errors and
1084    arithmatic errors. */
1085
1086 void myhandler(int sig, siginfo_t *info, void *uap) {
1087   sigset_t toclear;
1088 #ifdef DEBUG
1089   printf("sig=%d\n",sig);
1090   printf("signal\n");
1091 #endif
1092   sigemptyset(&toclear);
1093   sigaddset(&toclear, sig);
1094   sigprocmask(SIG_UNBLOCK, &toclear,NULL); 
1095   longjmp(error_handler,1);
1096 }
1097
1098 fd_set readfds;
1099 int maxreadfd;
1100 struct RuntimeHash *fdtoobject;
1101
1102 void addreadfd(int fd) {
1103   if (fd>=maxreadfd)
1104     maxreadfd=fd+1;
1105   FD_SET(fd, &readfds);
1106 }
1107
1108 void removereadfd(int fd) {
1109   FD_CLR(fd, &readfds);
1110   if (maxreadfd==(fd+1)) {
1111     maxreadfd--;
1112     while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
1113       maxreadfd--;
1114   }
1115 }
1116
1117 #ifdef PRECISE_GC
1118 #define OFFSET 2
1119 #else
1120 #define OFFSET 0
1121 #endif
1122
1123 #ifdef OPTIONAL
1124  int * fsescopy(int *src, int len) {
1125    int *dst;
1126    if (src==NULL)
1127      return NULL;
1128    dst=RUNMALLOC(len*sizeof(int));
1129    memcpy(dst, src, len*sizeof(int));
1130    return dst;
1131  }
1132 #endif
1133
1134 void executetasks() {
1135   void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1136 #ifdef OPTIONAL
1137   int * fsesarray[MAXTASKPARAMS];
1138   int * oldfsesarray[MAXTASKPARAMS];
1139   int numfsesarray[MAXTASKPARAMS];
1140 #endif  
1141
1142   /* Set up signal handlers */
1143   struct sigaction sig;
1144   sig.sa_sigaction=&myhandler;
1145   sig.sa_flags=SA_SIGINFO;
1146   sigemptyset(&sig.sa_mask);
1147
1148   /* Catch bus errors, segmentation faults, and floating point exceptions*/
1149   sigaction(SIGBUS,&sig,0);
1150   sigaction(SIGSEGV,&sig,0);
1151   sigaction(SIGFPE,&sig,0);
1152   sigaction(SIGPIPE,&sig,0);
1153
1154   /* Zero fd set */
1155   FD_ZERO(&readfds);
1156   maxreadfd=0;
1157   fdtoobject=allocateRuntimeHash(100);
1158
1159   /* Map first block of memory to protected, anonymous page */
1160   mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1161
1162   newtask:
1163   while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1164
1165     /* Check if any filedescriptors have IO pending */
1166     if (maxreadfd>0) {
1167       int i;
1168       struct timeval timeout={0,0};
1169       fd_set tmpreadfds;
1170       int numselect;
1171       tmpreadfds=readfds;
1172       numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1173       if (numselect>0) {
1174         /* Process ready fd's */
1175         int fd;
1176         for(fd=0;fd<maxreadfd;fd++) {
1177           if (FD_ISSET(fd, &tmpreadfds)) {
1178             /* Set ready flag on object */
1179             void * objptr;
1180             //      printf("Setting fd %d\n",fd);
1181             if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1182               if(intflagorand(objptr,1,0xFFFFFFFF)) { /* Set the first flag to 1 */
1183 #ifdef MULTICORE
1184                          enqueueObject(objptr, NULL, 0);
1185 #else
1186                          enqueueObject(objptr);
1187 #endif
1188                   }
1189             }
1190           }
1191         }
1192       }
1193     }
1194
1195     /* See if there are any active tasks */
1196     if (hashsize(activetasks)>0) {
1197       int i;
1198       currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1199       genfreekey(activetasks, currtpd);
1200       
1201       /* Check if this task has failed, allow a task that contains optional objects to fire */
1202       if (gencontains(failedtasks, currtpd)) {
1203         // Free up task parameter descriptor
1204         RUNFREE(currtpd->parameterArray);
1205 #ifdef OPTIONAL
1206         RUNFREE(currtpd->failed);
1207 #endif
1208         RUNFREE(currtpd);
1209         goto newtask;
1210       }
1211       int numparams=currtpd->task->numParameters;
1212       int numtotal=currtpd->task->numTotal;
1213       
1214       /* Make sure that the parameters are still in the queues */
1215       for(i=0;i<numparams;i++) {
1216         void * parameter=currtpd->parameterArray[i];
1217         struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1218         struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1219         int j;
1220         /* Check that object is still in queue */
1221 #ifdef OPTIONAL
1222         {
1223           int UNUSED, UNUSED2;
1224           int *flags;
1225           int numflags, isnonfailed;
1226           int failed=currtpd->failed[i];
1227           if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1228             RUNFREE(currtpd->parameterArray);
1229             RUNFREE(currtpd->failed);
1230             RUNFREE(currtpd);
1231             goto newtask;
1232           } else {
1233             if (failed&&(flags!=NULL)) {
1234               //Failed parameter
1235               fsesarray[i]=flags;
1236               numfsesarray[i]=numflags;
1237             } else if (!failed && isnonfailed) {
1238               //Non-failed parameter
1239               fsesarray[i]=NULL;
1240               numfsesarray[i]=0;
1241             } else {
1242               RUNFREE(currtpd->parameterArray);
1243               RUNFREE(currtpd->failed);
1244               RUNFREE(currtpd);
1245               goto newtask;
1246             }
1247           }
1248         }
1249 #else
1250         {
1251           if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1252             RUNFREE(currtpd->parameterArray);
1253             RUNFREE(currtpd);
1254             goto newtask;
1255           }
1256         }
1257 #endif
1258       parameterpresent:
1259         ;
1260         /* Check that object still has necessary tags */
1261         for(j=0;j<pd->numbertags;j++) {
1262           int slotid=pd->tagarray[2*j]+numparams;
1263           struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1264           if (!containstag(parameter, tagd)) {
1265             RUNFREE(currtpd->parameterArray);
1266 #ifdef OPTIONAL
1267             RUNFREE(currtpd->failed);
1268 #endif
1269             RUNFREE(currtpd);
1270             goto newtask;
1271           }
1272         }
1273         
1274         taskpointerarray[i+OFFSET]=parameter;
1275       }
1276       /* Copy the tags */
1277       for(;i<numtotal;i++) {
1278         taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1279       }
1280
1281       {
1282         /* Checkpoint the state */
1283         forward=allocateRuntimeHash(100);
1284         reverse=allocateRuntimeHash(100);
1285         void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1286         int x;
1287         if (x=setjmp(error_handler)) {
1288           int counter;
1289           /* Recover */
1290 #ifdef DEBUG
1291           printf("Fatal Error=%d, Recovering!\n",x);
1292 #endif
1293           genputtable(failedtasks,currtpd,currtpd);
1294           restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1295
1296 #ifdef OPTIONAL
1297           for(counter=0; counter<currtpd->task->numParameters; counter++){
1298             //enqueue as failed
1299             enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1300
1301             //free fses copies
1302             if (fsesarray[counter]!=NULL)
1303               RUNFREE(fsesarray[counter]);
1304           }
1305 #endif
1306           freeRuntimeHash(forward);
1307           freeRuntimeHash(reverse);
1308           freemalloc();
1309           forward=NULL;
1310           reverse=NULL;
1311         } else {
1312           if (injectfailures) {
1313             if ((((double)random())/RAND_MAX)<failurechance) {
1314               printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1315               longjmp(error_handler,10);
1316             }
1317           }
1318           /* Actually call task */
1319 #ifdef PRECISE_GC
1320           ((int *)taskpointerarray)[0]=currtpd->numParameters;
1321           taskpointerarray[1]=NULL;
1322 #endif
1323 #ifdef OPTIONAL
1324           //get the task flags set
1325           for(i=0;i<numparams;i++) {
1326             oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1327             fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1328             ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];         
1329           }
1330 #endif
1331           if(debugtask){
1332             printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1333             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1334             printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1335           } else
1336             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1337
1338 #ifdef OPTIONAL
1339           for(i=0;i<numparams;i++) {
1340             //free old fses
1341             if(oldfsesarray[i]!=NULL)
1342               RUNFREE(oldfsesarray[i]);
1343           }
1344 #endif
1345           
1346           freeRuntimeHash(forward);
1347           freeRuntimeHash(reverse);
1348           freemalloc();
1349           // Free up task parameter descriptor
1350           RUNFREE(currtpd->parameterArray);
1351 #ifdef OPTIONAL
1352           RUNFREE(currtpd->failed);
1353 #endif
1354           RUNFREE(currtpd);
1355           forward=NULL;
1356           reverse=NULL;
1357         }
1358       }
1359     }
1360   }
1361 }
1362  
1363 /* This function processes an objects tags */
1364 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1365   int i;
1366   
1367   for(i=0;i<pd->numbertags;i++) {
1368     int slotid=pd->tagarray[2*i];
1369     int tagid=pd->tagarray[2*i+1];
1370     
1371     if (statusarray[slotid+numparams]==0) {
1372       parameter->iterators[*iteratorcount].istag=1;
1373       parameter->iterators[*iteratorcount].tagid=tagid;
1374       parameter->iterators[*iteratorcount].slot=slotid+numparams;
1375       parameter->iterators[*iteratorcount].tagobjectslot=index;
1376       statusarray[slotid+numparams]=1;
1377       (*iteratorcount)++;
1378     }
1379   }
1380 }
1381
1382
1383 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1384   int i;
1385   int tagcount=0;
1386   struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1387
1388   parameter->iterators[*iteratorcount].istag=0;
1389   parameter->iterators[*iteratorcount].slot=index;
1390   parameter->iterators[*iteratorcount].objectset=objectset;
1391   statusarray[index]=1;
1392
1393   for(i=0;i<pd->numbertags;i++) {
1394     int slotid=pd->tagarray[2*i];
1395     int tagid=pd->tagarray[2*i+1];
1396     if (statusarray[slotid+numparams]!=0) {
1397       /* This tag has already been enqueued, use it to narrow search */
1398       parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1399       tagcount++;
1400     }
1401   }
1402   parameter->iterators[*iteratorcount].numtags=tagcount;
1403
1404   (*iteratorcount)++;
1405 }
1406
1407 /* This function builds the iterators for a task & parameter */
1408
1409 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1410   int statusarray[MAXTASKPARAMS];
1411   int i;
1412   int numparams=task->numParameters;
1413   int iteratorcount=0;
1414   for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1415
1416   statusarray[index]=1; /* Initial parameter */
1417   /* Process tags for initial iterator */
1418   
1419   processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1420   
1421   while(1) {
1422   loopstart:
1423     /* Check for objects with existing tags */
1424     for(i=0;i<numparams;i++) {
1425       if (statusarray[i]==0) {
1426         struct parameterdescriptor *pd=task->descriptorarray[i];
1427         int j;
1428         for(j=0;j<pd->numbertags;j++) {
1429           int slotid=pd->tagarray[2*j];
1430           if(statusarray[slotid+numparams]!=0) {
1431             processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1432             processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1433             goto loopstart;
1434           }
1435         }
1436       }
1437     }
1438
1439     /* Next do objects w/ unbound tags*/
1440
1441     for(i=0;i<numparams;i++) {
1442       if (statusarray[i]==0) {
1443         struct parameterdescriptor *pd=task->descriptorarray[i];
1444         if (pd->numbertags>0) {
1445           processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1446           processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1447           goto loopstart;
1448         }
1449       }
1450     }
1451
1452     /* Nothing with a tag enqueued */
1453
1454     for(i=0;i<numparams;i++) {
1455       if (statusarray[i]==0) {
1456         struct parameterdescriptor *pd=task->descriptorarray[i];
1457         processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1458         processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1459         goto loopstart;
1460       }
1461     }
1462
1463     /* Nothing left */
1464     return;
1465   }
1466 }
1467
1468  void printdebug() {
1469    int i;
1470    int j;
1471 #ifdef MULTICORE
1472    for(i=0;i<numtasks[corenum];i++) {
1473      struct taskdescriptor * task=taskarray[corenum][i];
1474 #else
1475    for(i=0;i<numtasks;i++) {
1476          struct taskdescriptor * task=taskarray[i];
1477 #endif
1478      printf("%s\n", task->name);
1479      for(j=0;j<task->numParameters;j++) {
1480        struct parameterdescriptor *param=task->descriptorarray[j];
1481        struct parameterwrapper *parameter=param->queue;
1482        struct ObjectHash * set=parameter->objectset;
1483        struct ObjectIterator objit;
1484        printf("  Parameter %d\n", j);
1485        ObjectHashiterator(set, &objit);
1486        while(ObjhasNext(&objit)) {
1487          struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1488          struct ___Object___ * tagptr=obj->___tags___;
1489          int nonfailed=Objdata4(&objit);
1490          int numflags=Objdata3(&objit);
1491          int flags=Objdata2(&objit);
1492          Objnext(&objit);
1493          printf("    Contains %lx\n", obj);
1494          printf("      flag=%d\n", obj->flag); 
1495 #ifdef OPTIONAL
1496          printf("      flagsstored=%x\n",flags);
1497          printf("      numflags=%d\n", numflags);
1498          printf("      nonfailed=%d\n",nonfailed);
1499 #endif
1500          if (tagptr==NULL) {
1501          } else if (tagptr->type==TAGTYPE) {
1502            printf("      tag=%lx\n",tagptr);
1503          } else {
1504            int tagindex=0;
1505            struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1506            for(;tagindex<ao->___cachedCode___;tagindex++) {
1507              printf("      tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1508            }
1509          }
1510        }
1511      }
1512    }
1513  }
1514  
1515
1516 /* This function processes the task information to create queues for
1517    each parameter type. */
1518
1519 void processtasks() {
1520   int i;
1521 #ifdef MULTICORE
1522   for(i=0;i<numtasks[corenum];i++) {
1523     struct taskdescriptor * task=taskarray[corenum][i];
1524 #else
1525   for(i=0;i<numtasks;i++) {
1526         struct taskdescriptor * task=taskarray[i];
1527 #endif
1528     int j;
1529
1530 #ifndef MULTICORE
1531     for(j=0;j<task->numParameters;j++) {
1532       struct parameterdescriptor *param=task->descriptorarray[j];
1533       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1534       struct parameterwrapper ** ptr=&objectqueues[param->type];
1535
1536       param->queue=parameter;
1537       parameter->objectset=allocateObjectHash(10);
1538       parameter->numberofterms=param->numberterms;
1539       parameter->intarray=param->intarray;
1540       parameter->numbertags=param->numbertags;
1541       parameter->tagarray=param->tagarray;
1542       parameter->task=task;
1543           parameter->slot=j;
1544       /* Link new queue in */
1545       while((*ptr)!=NULL)
1546         ptr=&((*ptr)->next);
1547       (*ptr)=parameter;
1548     }
1549 #endif
1550
1551     /* Build iterators for parameters */
1552     for(j=0;j<task->numParameters;j++) {
1553       struct parameterdescriptor *param=task->descriptorarray[j];
1554       struct parameterwrapper *parameter=param->queue;
1555 #ifdef MULTICORE
1556           parameter->objectset=allocateObjectHash(10);
1557           parameter->task=task;
1558 #endif
1559       builditerators(task, j, parameter);
1560     }
1561   }
1562 }
1563
1564 void toiReset(struct tagobjectiterator * it) {
1565   if (it->istag) {
1566     it->tagobjindex=0;
1567   } else if (it->numtags>0) {
1568     it->tagobjindex=0;
1569 #ifdef OPTIONAL
1570     it->failedstate=0;
1571 #endif
1572   } else {
1573     ObjectHashiterator(it->objectset, &it->it);
1574 #ifdef OPTIONAL
1575     it->failedstate=0;
1576 #endif
1577   }
1578 }
1579
1580 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1581   if (it->istag) {
1582     /* Iterate tag */
1583     /* Get object with tags */
1584     struct ___Object___ *obj=objectarray[it->tagobjectslot];
1585     struct ___Object___ *tagptr=obj->___tags___;
1586     if (tagptr->type==TAGTYPE) {
1587       if ((it->tagobjindex==0)&& /* First object */
1588           (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1589         return 1;
1590       else
1591         return 0;
1592     } else {
1593       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1594       int tagindex=it->tagobjindex;
1595       for(;tagindex<ao->___cachedCode___;tagindex++) {
1596         struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1597         if (td->flag==it->tagid) {
1598           it->tagobjindex=tagindex; /* Found right type of tag */
1599           return 1;
1600         }
1601       }
1602       return 0;
1603     }
1604   } else if (it->numtags>0) {
1605     /* Use tags to locate appropriate objects */
1606     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1607     struct ___Object___ *objptr=tag->flagptr;
1608     int i;
1609     if (objptr->type!=OBJECTARRAYTYPE) {
1610       if (it->tagobjindex>0)
1611         return 0;
1612       if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1613         return 0;
1614       for(i=1;i<it->numtags;i++) {
1615         struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1616         if (!containstag(objptr,tag2))
1617           return 0;
1618       }
1619 #ifdef OPTIONAL
1620       if (it->failedstate==1) {
1621         int UNUSED, UNUSED2;
1622         int * flags;
1623         int isnonfailed;
1624         ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1625         if (flags!=NULL) {
1626           return 1;
1627         } else {
1628           it->tagobjindex++;
1629           it->failedstate=0;
1630           return 0;
1631         }
1632       } else {
1633         int UNUSED, UNUSED2;
1634         int * flags;
1635         int isnonfailed;
1636         ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1637         if (!isnonfailed) {
1638           it->failedstate=1;
1639         }
1640         return 1;
1641       }
1642 #endif      
1643       return 1;
1644     } else {
1645       struct ArrayObject *ao=(struct ArrayObject *) objptr;
1646       int tagindex;
1647       int i;
1648 #ifdef OPTIONAL
1649       if (it->failedstate==1) {
1650         int UNUSED, UNUSED2;
1651         int * flags;
1652         int isnonfailed;
1653         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1654         ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1655         if (flags!=NULL) {
1656           return 1;
1657         } else {
1658           it->failedstate=0;
1659           it->tagobjindex++;
1660         }
1661       }
1662 #endif
1663       for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1664         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1665         if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1666           continue;
1667         for(i=1;i<it->numtags;i++) {
1668           struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1669           if (!containstag(objptr,tag2))
1670             goto nexttag;
1671         }
1672 #ifdef OPTIONAL
1673         {
1674           int UNUSED, UNUSED2;
1675           int flags, isnonfailed;
1676           struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1677           ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1678           if (!isnonfailed) {
1679             it->failedstate=1;
1680           }
1681         }
1682 #endif
1683         it->tagobjindex=tagindex;
1684         return 1;
1685       nexttag:
1686         ;
1687       }
1688       it->tagobjindex=tagindex;
1689       return 0;
1690     }
1691   } else {
1692 #ifdef OPTIONAL
1693     if (it->failedstate==1) {
1694       if (Objdata2(&it->it))
1695         return 1;
1696       else {
1697         it->failedstate=0;
1698         Objnext(&it->it);
1699       }
1700     }
1701     if (ObjhasNext(&it->it)) {
1702       if (!Objdata4(&it->it)) {
1703         //failed state only
1704         it->failedstate=1;
1705       }
1706       return 1;
1707     } else
1708       return 0;
1709 #else
1710     return ObjhasNext(&it->it);
1711 #endif
1712   }
1713 }
1714
1715 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1716   int j;
1717   struct ___Object___ * objptr=tag->flagptr;
1718   if (objptr->type==OBJECTARRAYTYPE) {
1719     struct ArrayObject *ao=(struct ArrayObject *)objptr;
1720     for(j=0;j<ao->___cachedCode___;j++) {
1721       if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1722         return 1;
1723     }
1724     return 0;
1725   } else
1726     return objptr==ptr;
1727 }
1728
1729 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1730   /* hasNext has all of the intelligence */
1731   if(it->istag) {
1732     /* Iterate tag */
1733     /* Get object with tags */
1734     struct ___Object___ *obj=objectarray[it->tagobjectslot];
1735     struct ___Object___ *tagptr=obj->___tags___;
1736 #ifdef OPTIONAL
1737     failed[it->slot]=0; //have to set it to something
1738 #endif
1739     if (tagptr->type==TAGTYPE) {
1740       it->tagobjindex++;
1741       objectarray[it->slot]=tagptr;
1742     } else {
1743       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1744       objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1745     }
1746   } else if (it->numtags>0) {
1747     /* Use tags to locate appropriate objects */
1748     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1749     struct ___Object___ *objptr=tag->flagptr;
1750     if (objptr->type!=OBJECTARRAYTYPE) {
1751 #ifdef OPTIONAL
1752     failed[it->slot]=it->failedstate;
1753     objectarray[it->slot]=objptr;
1754     if (it->failedstate==0) {
1755       it->failedstate=1;
1756     } else {
1757       it->failedstate=0;
1758       it->tagobjindex++;
1759     }
1760 #else
1761       it->tagobjindex++;
1762       objectarray[it->slot]=objptr;
1763 #endif
1764     } else {
1765       struct ArrayObject *ao=(struct ArrayObject *) objptr;
1766 #ifdef OPTIONAL
1767     failed[it->slot]=it->failedstate;
1768     objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1769     if (it->failedstate==0) {
1770       it->failedstate=1;
1771     } else {
1772       it->failedstate=0;
1773       it->tagobjindex++;
1774     }
1775 #else
1776       objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1777 #endif
1778     }
1779   } else {
1780     /* Iterate object */
1781     objectarray[it->slot]=(void *)Objkey(&it->it);
1782 #ifdef OPTIONAL
1783     failed[it->slot]=it->failedstate;
1784     if (it->failedstate==0) {
1785       it->failedstate=1;
1786     } else {
1787       it->failedstate=0;
1788       Objnext(&it->it);
1789     }
1790 #else
1791     Objnext(&it->it);
1792 #endif
1793   }
1794 }
1795 #endif