new class names. build output code for optional tasks.
[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 "optionalstruct.h"
10 #include <sys/select.h>
11 #include <sys/types.h>
12 #include <sys/mman.h>
13 #include <string.h>
14 #include <signal.h>
15
16 extern int injectfailures;
17 extern float failurechance;
18 extern int debugtask;
19 extern int instaccum;
20
21 #ifdef CONSCHECK
22 #include "instrument.h"
23 #endif
24
25 struct genhashtable * activetasks;
26 struct parameterwrapper * objectqueues[NUMCLASSES];
27 struct genhashtable * failedtasks;
28 struct taskparamdescriptor * currtpd;
29 struct RuntimeHash * forward;
30 struct RuntimeHash * reverse;
31
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
50   /* Process task information */
51   processtasks();
52
53   /* Create startup object */
54   createstartupobject(argc, argv);
55
56   /* Start executing the tasks */
57   executetasks();
58 }
59
60 void createstartupobject(int argc, char ** argv) {
61   int i;
62   
63   /* Allocate startup object     */
64 #ifdef PRECISE_GC
65   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
66   struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1); 
67 #else
68   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
69   struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1); 
70 #endif
71   /* Build array of strings */
72   startupobject->___parameters___=stringarray;
73   for(i=1;i<argc;i++) {
74     int length=strlen(argv[i]);
75 #ifdef PRECISE_GC
76     struct ___String___ *newstring=NewString(NULL, argv[i],length);
77 #else
78     struct ___String___ *newstring=NewString(argv[i],length);
79 #endif
80     ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
81   }
82   
83   /* Set initialized flag for startup object */
84   flagorand(startupobject,1,0xFFFFFFFF);
85 }
86
87 int hashCodetpd(struct taskparamdescriptor *ftd) {
88   int hash=(int)ftd->task;
89   int i;
90   for(i=0;i<ftd->numParameters;i++) {
91     hash^=(int)ftd->parameterArray[i];
92   }
93   return hash;
94 }
95
96 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
97   int i;
98   if (ftd1->task!=ftd2->task)
99     return 0;
100   for(i=0;i<ftd1->numParameters;i++)
101     if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
102       return 0;
103   return 1;
104 }
105
106
107 /* This function sets a tag. */
108 #ifdef PRECISE_GC
109 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
110 #else
111 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
112 #endif
113   struct ___Object___ * tagptr=obj->___tags___;
114   if (tagptr==NULL) {
115     obj->___tags___=(struct ___Object___ *)tagd;
116   } else {
117     /* Have to check if it is already set */
118     if (tagptr->type==TAGTYPE) {
119       struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
120       if (td==tagd)
121         return;
122 #ifdef PRECISE_GC
123       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
124       struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
125       obj=(struct ___Object___ *)ptrarray[2];
126       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
127       td=(struct ___TagDescriptor___ *) obj->___tags___;
128 #else
129       struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
130 #endif
131       ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
132       ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
133       obj->___tags___=(struct ___Object___ *) ao;
134       ao->___cachedCode___=2;
135     } else {
136       /* Array Case */
137       int i;
138       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
139       for(i=0;i<ao->___cachedCode___;i++) {
140         struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
141         if (td==tagd)
142           return;
143       }
144       if (ao->___cachedCode___<ao->___length___) {
145         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
146         ao->___cachedCode___++;
147       } else {
148 #ifdef PRECISE_GC
149         int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
150         struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
151         obj=(struct ___Object___ *)ptrarray[2];
152         tagd=(struct ___TagDescriptor___ *) ptrarray[3];
153         ao=(struct ArrayObject *)obj->___tags___;
154 #else
155         struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
156 #endif
157         aonew->___cachedCode___=ao->___length___+1;
158         for(i=0;i<ao->___length___;i++) {
159           ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
160         }
161         ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
162       }
163     }
164   }
165
166   {
167     struct ___Object___ * tagset=tagd->flagptr;
168     
169     if(tagset==NULL) {
170       tagd->flagptr=obj;
171     } else if (tagset->type!=OBJECTARRAYTYPE) {
172 #ifdef PRECISE_GC
173       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
174       struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
175       obj=(struct ___Object___ *)ptrarray[2];
176       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
177 #else
178       struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
179 #endif
180       ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
181       ARRAYSET(ao, struct ___Object___ *, 1, obj);
182       ao->___cachedCode___=2;
183       tagd->flagptr=(struct ___Object___ *)ao;
184     } else {
185       struct ArrayObject *ao=(struct ArrayObject *) tagset;
186       if (ao->___cachedCode___<ao->___length___) {
187         ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
188       } else {
189         int i;
190 #ifdef PRECISE_GC
191         int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
192         struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
193         obj=(struct ___Object___ *)ptrarray[2];
194         tagd=(struct ___TagDescriptor___ *)ptrarray[3];
195         ao=(struct ArrayObject *)tagd->flagptr;
196 #else
197         struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
198 #endif
199         aonew->___cachedCode___=ao->___cachedCode___+1;
200         for(i=0;i<ao->___length___;i++) {
201           ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
202         }
203         ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
204         tagd->flagptr=(struct ___Object___ *) ao;
205       }
206     }
207   }
208 }
209
210 /* This function clears a tag. */
211 #ifdef PRECISE_GC
212 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
213 #else
214 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
215 #endif
216   /* We'll assume that tag is alway there.
217      Need to statically check for this of course. */
218   struct ___Object___ * tagptr=obj->___tags___;
219
220   if (tagptr->type==TAGTYPE) {
221     if ((struct ___TagDescriptor___ *)tagptr==tagd)
222       obj->___tags___=NULL;
223     else
224       printf("ERROR 1 in tagclear\n");
225   } else {
226     struct ArrayObject *ao=(struct ArrayObject *) tagptr;
227     int i;
228     for(i=0;i<ao->___cachedCode___;i++) {
229       struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
230       if (td==tagd) {
231         ao->___cachedCode___--;
232         if (i<ao->___cachedCode___)
233           ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
234         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
235         if (ao->___cachedCode___==0)
236           obj->___tags___=NULL;
237         goto PROCESSCLEAR;
238       }
239     }
240     printf("ERROR 2 in tagclear\n");
241   }
242  PROCESSCLEAR:
243   {
244     struct ___Object___ *tagset=tagd->flagptr;
245     if (tagset->type!=OBJECTARRAYTYPE) {
246       if (tagset==obj)
247         tagd->flagptr=NULL;
248       else
249         printf("ERROR 3 in tagclear\n");
250     } else {
251       struct ArrayObject *ao=(struct ArrayObject *) tagset;
252       int i;
253       for(i=0;i<ao->___cachedCode___;i++) {
254         struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
255         if (tobj==obj) {
256           ao->___cachedCode___--;
257           if (i<ao->___cachedCode___)
258             ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
259           ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
260           if (ao->___cachedCode___==0)
261             tagd->flagptr=NULL;
262           goto ENDCLEAR;
263         }
264       }
265       printf("ERROR 4 in tagclear\n");
266     }
267   }
268  ENDCLEAR:
269   return;
270   
271 }
272  
273 /* This function allocates a new tag. */
274 #ifdef PRECISE_GC
275 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
276   struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
277 #else
278 struct ___TagDescriptor___ * allocate_tag(int index) {
279   struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
280 #endif
281   v->type=TAGTYPE;
282   v->flag=index;
283   return v;
284
285
286
287
288 /* This function updates the flag for object ptr.  It or's the flag
289    with the or mask and and's it with the andmask. */
290
291 void flagbody(struct ___Object___ *ptr, int flag);
292
293 void flagorand(void * ptr, int ormask, int andmask) {
294   int oldflag=((int *)ptr)[1];
295   int flag=ormask|oldflag;
296   flag&=andmask;
297   // Not sure why this was necessary
298   //  if (flag==oldflag) /* Don't do anything */
299   //  return;
300   //else 
301   flagbody(ptr, flag);
302 }
303
304 void intflagorand(void * ptr, int ormask, int andmask) {
305   int oldflag=((int *)ptr)[1];
306   int flag=ormask|oldflag;
307   flag&=andmask;
308   if (flag==oldflag) /* Don't do anything */
309     return;
310   else flagbody(ptr, flag);
311 }
312
313 void flagorandinit(void * ptr, int ormask, int andmask) {
314   int oldflag=((int *)ptr)[1];
315   int flag=ormask|oldflag;
316   flag&=andmask;
317   flagbody(ptr,flag);
318 }
319
320 void flagbody(struct ___Object___ *ptr, int flag) {
321   struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
322   ptr->flag=flag;
323   
324   /*Remove object from all queues */
325   while(flagptr!=NULL) {
326     struct parameterwrapper *next;
327     struct ___Object___ * tag=ptr->___tags___;
328     RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
329     RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
330     flagptr=next;
331   }
332   
333   {
334     struct QueueItem *tmpptr;
335     struct parameterwrapper * parameter=objectqueues[ptr->type];
336     int i;
337     struct parameterwrapper * prevptr=NULL;
338     struct ___Object___ *tagptr=ptr->___tags___;
339       
340     /* Outer loop iterates through all parameter queues an object of
341        this type could be in.  */
342
343     while(parameter!=NULL) {
344       /* Check tags */
345       if (parameter->numbertags>0) {
346         if (tagptr==NULL)
347           goto nextloop;//that means the object has no tag but that param needs tag
348         else if(tagptr->type==TAGTYPE) {//one tag
349           struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
350           for(i=0;i<parameter->numbertags;i++) {
351             //slotid is parameter->tagarray[2*i];
352             int tagid=parameter->tagarray[2*i+1];
353             if (tagid!=tagptr->flag)
354               goto nextloop; /*We don't have this tag */          
355           }
356         } else {//multiple tags
357           struct ArrayObject * ao=(struct ArrayObject *) tagptr;
358           for(i=0;i<parameter->numbertags;i++) {
359             //slotid is parameter->tagarray[2*i];
360             int tagid=parameter->tagarray[2*i+1];
361             int j;
362             for(j=0;j<ao->___cachedCode___;j++) {
363               if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
364                 goto foundtag;
365             }
366             goto nextloop;
367           foundtag:
368             ;
369           }
370         }
371       }
372
373       /* Check flags */
374       for(i=0;i<parameter->numberofterms;i++) {
375         int andmask=parameter->intarray[i*2];
376         int checkmask=parameter->intarray[i*2+1];
377         if ((flag&andmask)==checkmask) {
378           enqueuetasks(parameter, prevptr, ptr);
379           prevptr=parameter;
380           break;
381         }
382       }
383     nextloop:
384       parameter=parameter->next;
385     }
386     ptr->flagptr=prevptr;
387   }
388 }
389   
390 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
391   void * taskpointerarray[MAXTASKPARAMS];
392   int j;
393   int numparams=parameter->task->numParameters;
394   int numiterators=parameter->task->numTotal-1;
395
396   struct taskdescriptor * task=parameter->task;
397   
398   RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);//this add the object to parameterwrapper
399   
400   /* Add enqueued object to parameter vector */
401   taskpointerarray[parameter->slot]=ptr;
402
403   /* Reset iterators */
404   for(j=0;j<numiterators;j++) {
405     toiReset(&parameter->iterators[j]);
406   }
407
408   /* Find initial state */
409   for(j=0;j<numiterators;j++) {
410   backtrackinit:
411     if(toiHasNext(&parameter->iterators[j], taskpointerarray))
412       toiNext(&parameter->iterators[j], taskpointerarray);
413     else if (j>0) {
414       /* Need to backtrack */
415       toiReset(&parameter->iterators[j]);
416       j--;
417       goto backtrackinit;
418     } else {
419       /* Nothing to enqueue */
420       return;
421     }
422   }
423
424   
425   while(1) {
426     /* Enqueue current state */
427     struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
428     tpd->task=task;
429     tpd->numParameters=numiterators+1;
430     tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
431     for(j=0;j<=numiterators;j++)
432       tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
433     
434     /* Enqueue task */
435     if (!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd)) {
436       genputtable(activetasks, tpd, tpd);
437     } else {
438       RUNFREE(tpd->parameterArray);
439       RUNFREE(tpd);
440     }
441     
442     /* This loop iterates to the next parameter combination */
443     if (numiterators==0)
444       return;
445
446     for(j=numiterators-1; j<numiterators;j++) {
447     backtrackinc:
448       if(toiHasNext(&parameter->iterators[j], taskpointerarray))
449         toiNext(&parameter->iterators[j], taskpointerarray);
450       else if (j>0) {
451         /* Need to backtrack */
452         toiReset(&parameter->iterators[j]);
453         j--;
454         goto backtrackinc;
455       } else {
456         /* Nothing more to enqueue */
457         return;
458       }
459     }
460   }
461 }
462  
463 /* Handler for signals. The signals catch null pointer errors and
464    arithmatic errors. */
465
466 void myhandler(int sig, siginfo_t *info, void *uap) {
467 #ifdef DEBUG
468   printf("sig=%d\n",sig);
469   printf("signal\n");
470 #endif
471   longjmp(error_handler,1);
472 }
473
474 fd_set readfds;
475 int maxreadfd;
476 struct RuntimeHash *fdtoobject;
477
478 void addreadfd(int fd) {
479   if (fd>=maxreadfd)
480     maxreadfd=fd+1;
481   FD_SET(fd, &readfds);
482 }
483
484 void removereadfd(int fd) {
485   FD_CLR(fd, &readfds);
486   if (maxreadfd==(fd+1)) {
487     maxreadfd--;
488     while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
489       maxreadfd--;
490   }
491 }
492
493 #ifdef PRECISE_GC
494 #define OFFSET 2
495 #else
496 #define OFFSET 0
497 #endif
498
499 void executetasks() {
500   void * taskpointerarray[MAXTASKPARAMS+OFFSET];
501
502   /* Set up signal handlers */
503   struct sigaction sig;
504   sig.sa_sigaction=&myhandler;
505   sig.sa_flags=SA_SIGINFO;
506   sigemptyset(&sig.sa_mask);
507
508   /* Catch bus errors, segmentation faults, and floating point exceptions*/
509   sigaction(SIGBUS,&sig,0);
510   sigaction(SIGSEGV,&sig,0);
511   sigaction(SIGFPE,&sig,0);
512   sigaction(SIGPIPE,&sig,0);
513
514   /* Zero fd set */
515   FD_ZERO(&readfds);
516   maxreadfd=0;
517   fdtoobject=allocateRuntimeHash(100);
518
519   /* Map first block of memory to protected, anonymous page */
520   mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
521
522   newtask:
523   while((hashsize(activetasks)>0)||(maxreadfd>0)) {
524
525     /* Check if any filedescriptors have IO pending */
526     if (maxreadfd>0) {
527       int i;
528       struct timeval timeout={0,0};
529       fd_set tmpreadfds;
530       int numselect;
531       tmpreadfds=readfds;
532       numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
533       if (numselect>0) {
534         /* Process ready fd's */
535         int fd;
536         for(fd=0;fd<maxreadfd;fd++) {
537           if (FD_ISSET(fd, &tmpreadfds)) {
538             /* Set ready flag on object */
539             void * objptr;
540             //      printf("Setting fd %d\n",fd);
541             if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
542               intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
543             }
544           }
545         }
546       }
547     }
548
549     /* See if there are any active tasks */
550     if (hashsize(activetasks)>0) {
551       int i;
552       currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
553       genfreekey(activetasks, currtpd);
554
555       /* Check if this task has failed */
556       if (gencontains(failedtasks, currtpd)) {
557         // Free up task parameter descriptor
558         RUNFREE(currtpd->parameterArray);
559         RUNFREE(currtpd);
560         goto newtask;
561       }
562       int numparams=currtpd->task->numParameters;
563       int numtotal=currtpd->task->numTotal;
564
565       /* Make sure that the parameters are still in the queues */
566       for(i=0;i<numparams;i++) {
567         void * parameter=currtpd->parameterArray[i];
568         struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
569         struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
570         int j;
571         /* Check that object is still in queue */
572         if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
573           RUNFREE(currtpd->parameterArray);
574           RUNFREE(currtpd);
575           goto newtask;
576         }
577         /* Check that object still has necessary tags */
578         for(j=0;j<pd->numbertags;j++) {
579           int slotid=pd->tagarray[2*j]+numparams;
580           struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
581           if (!containstag(parameter, tagd)) {
582             RUNFREE(currtpd->parameterArray);
583             RUNFREE(currtpd);
584             goto newtask;
585           }
586         }
587         
588         taskpointerarray[i+OFFSET]=parameter;
589       }
590       /* Copy the tags */
591       for(;i<numtotal;i++) {
592         taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
593       }
594
595       {
596         /* Checkpoint the state */
597         forward=allocateRuntimeHash(100);
598         reverse=allocateRuntimeHash(100);
599         void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
600         int x;
601         if (x=setjmp(error_handler)) {
602           /* Recover */
603           int h;
604 #ifdef DEBUG
605           printf("Fatal Error=%d, Recovering!\n",x);
606 #endif
607           genputtable(failedtasks,currtpd,currtpd);
608           restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
609           /*where we have to insert the code for optional tasks
610             all the pointers I need are in currtpd->parameterArray */
611           freeRuntimeHash(forward);
612           freeRuntimeHash(reverse);
613           freemalloc();
614           forward=NULL;
615           reverse=NULL;
616         } else {
617           if (injectfailures) {
618             if ((((double)random())/RAND_MAX)<failurechance) {
619               printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
620               longjmp(error_handler,10);
621             }
622           }
623           /* Actually call task */
624 #ifdef PRECISE_GC
625           ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
626           taskpointerarray[1]=NULL;
627 #endif
628
629           if (debugtask) {
630             printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
631             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
632             printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
633           } else
634             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
635           freeRuntimeHash(forward);
636           freeRuntimeHash(reverse);
637           freemalloc();
638           // Free up task parameter descriptor
639           RUNFREE(currtpd->parameterArray);
640           RUNFREE(currtpd);
641           forward=NULL;
642           reverse=NULL;
643         }
644       }
645     }
646   }
647 }
648
649 /* This function processes an objects tags */
650 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
651   int i;
652
653   for(i=0;i<pd->numbertags;i++) {
654     int slotid=pd->tagarray[2*i];
655     int tagid=pd->tagarray[2*i+1];
656     
657     if (statusarray[slotid+numparams]==0) {
658       parameter->iterators[*iteratorcount].istag=1;
659       parameter->iterators[*iteratorcount].tagid=tagid;
660       parameter->iterators[*iteratorcount].slot=slotid+numparams;
661       parameter->iterators[*iteratorcount].tagobjectslot=index;
662       statusarray[slotid+numparams]=1;
663       (*iteratorcount)++;
664     }
665   }
666 }
667
668
669 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
670   int i;
671   int tagcount=0;
672   struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
673
674   parameter->iterators[*iteratorcount].istag=0;
675   parameter->iterators[*iteratorcount].slot=index;
676   parameter->iterators[*iteratorcount].objectset=objectset;
677   statusarray[index]=1;
678
679   for(i=0;i<pd->numbertags;i++) {
680     int slotid=pd->tagarray[2*i];
681     int tagid=pd->tagarray[2*i+1];
682     if (statusarray[slotid+numparams]!=0) {
683       /* This tag has already been enqueued, use it to narrow search */
684       parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
685       tagcount++;
686     }
687   }
688   parameter->iterators[*iteratorcount].numtags=tagcount;
689
690   (*iteratorcount)++;
691 }
692
693 /* This function builds the iterators for a task & parameter */
694
695 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
696   int statusarray[MAXTASKPARAMS];
697   int i;
698   int numparams=task->numParameters;
699   int iteratorcount=0;
700   for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
701
702   statusarray[index]=1; /* Initial parameter */
703   /* Process tags for initial iterator */
704   
705   processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
706   
707   while(1) {
708   loopstart:
709     /* Check for objects with existing tags */
710     for(i=0;i<numparams;i++) {
711       if (statusarray[i]==0) {
712         struct parameterdescriptor *pd=task->descriptorarray[i];
713         int j;
714         for(j=0;j<pd->numbertags;j++) {
715           int slotid=pd->tagarray[2*j];
716           if(statusarray[slotid+numparams]!=0) {
717             processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
718             processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
719             goto loopstart;
720           }
721         }
722       }
723     }
724
725     /* Next do objects w/ unbound tags*/
726
727     for(i=0;i<numparams;i++) {
728       if (statusarray[i]==0) {
729         struct parameterdescriptor *pd=task->descriptorarray[i];
730         if (pd->numbertags>0) {
731           processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
732           processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
733           goto loopstart;
734         }
735       }
736     }
737
738     /* Nothing with a tag enqueued */
739
740     for(i=0;i<numparams;i++) {
741       if (statusarray[i]==0) {
742         struct parameterdescriptor *pd=task->descriptorarray[i];
743         processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
744         processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
745         goto loopstart;
746       }
747     }
748
749     /* Nothing left */
750     return;
751   }
752 }
753
754
755  
756
757 /* This function processes the task information to create queues for
758    each parameter type. */
759
760 void processtasks() {
761   int i;
762   for(i=0;i<numtasks;i++) {
763     struct taskdescriptor * task=taskarray[i];
764     int j;
765
766     for(j=0;j<task->numParameters;j++) {
767       struct parameterdescriptor *param=task->descriptorarray[j];
768       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
769       struct parameterwrapper ** ptr=&objectqueues[param->type];
770
771       param->queue=parameter;
772       parameter->objectset=allocateRuntimeHash(10);
773       parameter->numberofterms=param->numberterms;
774       parameter->intarray=param->intarray;
775       parameter->numbertags=param->numbertags;
776       parameter->tagarray=param->tagarray;
777       parameter->task=task;
778       /* Link new queue in */
779       while((*ptr)!=NULL)
780         ptr=&((*ptr)->next);
781       (*ptr)=parameter;
782     }
783
784     /* Build iterators for parameters */
785     for(j=0;j<task->numParameters;j++) {
786       struct parameterdescriptor *param=task->descriptorarray[j];
787       struct parameterwrapper *parameter=param->queue;      
788       parameter->slot=j;
789       builditerators(task, j, parameter);
790     }
791   }
792 }
793
794 void toiReset(struct tagobjectiterator * it) {
795   if (it->istag) {
796     it->tagobjindex=0;
797   } else if (it->numtags>0) {
798     it->tagobjindex=0;
799   } else {
800     RuntimeHashiterator(it->objectset, &it->it);
801   }
802 }
803
804 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
805   if (it->istag) {
806     /* Iterate tag */
807     /* Get object with tags */
808     struct ___Object___ *obj=objectarray[it->tagobjectslot];
809     struct ___Object___ *tagptr=obj->___tags___;
810     if (tagptr->type==TAGTYPE) {
811       if ((it->tagobjindex==0)&& /* First object */
812           (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
813         return 1;
814       else
815         return 0;
816     } else {
817       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
818       int tagindex=it->tagobjindex;
819       for(;tagindex<ao->___cachedCode___;tagindex++) {
820         struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
821         if (td->flag==it->tagid) {
822           it->tagobjindex=tagindex; /* Found right type of tag */
823           return 1;
824         }
825       }
826       return 0;
827     }
828   } else if (it->numtags>0) {
829     /* Use tags to locate appropriate objects */
830     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
831     struct ___Object___ *objptr=tag->flagptr;
832     int i;
833     if (objptr->type!=OBJECTARRAYTYPE) {
834       if (it->tagobjindex>0)
835         return 0;
836       if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
837         return 0;
838       for(i=1;i<it->numtags;i++) {
839         struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
840         if (!containstag(objptr,tag2))
841           return 0;
842       }
843       return 1;
844     } else {
845       struct ArrayObject *ao=(struct ArrayObject *) objptr;
846       int tagindex;
847       int i;
848       for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
849         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
850         if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
851           continue;
852         for(i=1;i<it->numtags;i++) {
853           struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
854           if (!containstag(objptr,tag2))
855             goto nexttag;
856         }
857         return 1;
858       nexttag:
859         ;
860       }
861       it->tagobjindex=tagindex;
862       return 0;
863     }
864   } else {
865     return RunhasNext(&it->it);
866   }
867 }
868
869 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
870   int j;
871   struct ___Object___ * objptr=tag->flagptr;
872   if (objptr->type==OBJECTARRAYTYPE) {
873     struct ArrayObject *ao=(struct ArrayObject *)objptr;
874     for(j=0;j<ao->___cachedCode___;j++) {
875       if (ptr==ARRAYGET(ao, struct ___Object___*, j))
876         return 1;
877     }
878     return 0;
879   } else
880     return objptr==ptr;
881 }
882
883 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
884   /* hasNext has all of the intelligence */
885   if(it->istag) {
886     /* Iterate tag */
887     /* Get object with tags */
888     struct ___Object___ *obj=objectarray[it->tagobjectslot];
889     struct ___Object___ *tagptr=obj->___tags___;
890     if (tagptr->type==TAGTYPE) {
891       it->tagobjindex++;
892       objectarray[it->slot]=tagptr;
893     } else {
894       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
895       objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
896     }
897   } else if (it->numtags>0) {
898     /* Use tags to locate appropriate objects */
899     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
900     struct ___Object___ *objptr=tag->flagptr;
901     if (objptr->type!=OBJECTARRAYTYPE) {
902       it->tagobjindex++;
903       objectarray[it->slot]=objptr;
904     } else {
905       struct ArrayObject *ao=(struct ArrayObject *) objptr;
906       objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
907     }
908   } else {
909     /* Iterate object */
910     objectarray[it->slot]=(void *)Runkey(&it->it);
911     Runnext(&it->it);
912   }
913 }
914
915
916 #endif