Bug fixed in BuildCode.
[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 struct parameterwrapper * objectqueues[NUMCLASSES];
26 struct genhashtable * failedtasks;
27 struct taskparamdescriptor * currtpd;
28 struct RuntimeHash * forward;
29 struct RuntimeHash * reverse;
30 struct RuntimeHash * exitfsesdata;
31
32 int main(int argc, char **argv) {
33 #ifdef BOEHM_GC
34   GC_init(); // Initialize the garbage collector
35 #endif
36 #ifdef CONSCHECK
37   initializemmap();
38 #endif
39   processOptions();
40   initializeexithandler();
41   /* Create table for failed tasks */
42   failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
43                                    (int (*)(void *,void *)) &comparetpd);
44   /* Create queue of active tasks */
45   activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
46                                    (int (*)(void *,void *)) &comparetpd);
47   
48   exitfsesdata=allocateRuntimeHash(10);
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   flagorand(startupobject,1,0xFFFFFFFF);
84 }
85
86 int hashCodetpd(struct taskparamdescriptor *ftd) {
87   int hash=(int)ftd->task;
88   int i;                                                
89   for(i=0;i<ftd->numParameters;i++){ 
90     hash^=(int)ftd->parameterArray[i];
91   }
92   return hash;
93 }
94
95 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
96   int i;
97   if (ftd1->task!=ftd2->task)
98     return 0;
99   for(i=0;i<ftd1->numParameters;i++)
100     if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
101       return 0;
102   return 1;
103 }
104
105 /* This function sets a tag. */
106 #ifdef PRECISE_GC
107 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
108 #else
109 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
110 #endif
111   struct ___Object___ * tagptr=obj->___tags___;
112   if (tagptr==NULL) {
113     obj->___tags___=(struct ___Object___ *)tagd;
114   } else {
115     /* Have to check if it is already set */
116     if (tagptr->type==TAGTYPE) {
117       struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
118       if (td==tagd)
119         return;
120 #ifdef PRECISE_GC
121       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
122       struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
123       obj=(struct ___Object___ *)ptrarray[2];
124       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
125       td=(struct ___TagDescriptor___ *) obj->___tags___;
126 #else
127       struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
128 #endif
129       ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
130       ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
131       obj->___tags___=(struct ___Object___ *) ao;
132       ao->___cachedCode___=2;
133     } else {
134       /* Array Case */
135       int i;
136       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
137       for(i=0;i<ao->___cachedCode___;i++) {
138         struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
139         if (td==tagd)
140           return;
141       }
142       if (ao->___cachedCode___<ao->___length___) {
143         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
144         ao->___cachedCode___++;
145       } else {
146 #ifdef PRECISE_GC
147         int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
148         struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
149         obj=(struct ___Object___ *)ptrarray[2];
150         tagd=(struct ___TagDescriptor___ *) ptrarray[3];
151         ao=(struct ArrayObject *)obj->___tags___;
152 #else
153         struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
154 #endif
155         aonew->___cachedCode___=ao->___length___+1;
156         for(i=0;i<ao->___length___;i++) {
157           ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
158         }
159         ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
160       }
161     }
162   }
163
164   {
165     struct ___Object___ * tagset=tagd->flagptr;
166     
167     if(tagset==NULL) {
168       tagd->flagptr=obj;
169     } else if (tagset->type!=OBJECTARRAYTYPE) {
170 #ifdef PRECISE_GC
171       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
172       struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
173       obj=(struct ___Object___ *)ptrarray[2];
174       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
175 #else
176       struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
177 #endif
178       ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
179       ARRAYSET(ao, struct ___Object___ *, 1, obj);
180       ao->___cachedCode___=2;
181       tagd->flagptr=(struct ___Object___ *)ao;
182     } else {
183       struct ArrayObject *ao=(struct ArrayObject *) tagset;
184       if (ao->___cachedCode___<ao->___length___) {
185         ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
186       } else {
187         int i;
188 #ifdef PRECISE_GC
189         int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
190         struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
191         obj=(struct ___Object___ *)ptrarray[2];
192         tagd=(struct ___TagDescriptor___ *)ptrarray[3];
193         ao=(struct ArrayObject *)tagd->flagptr;
194 #else
195         struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
196 #endif
197         aonew->___cachedCode___=ao->___cachedCode___+1;
198         for(i=0;i<ao->___length___;i++) {
199           ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
200         }
201         ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
202         tagd->flagptr=(struct ___Object___ *) ao;
203       }
204     }
205   }
206 }
207
208 /* This function clears a tag. */
209 #ifdef PRECISE_GC
210 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
211 #else
212 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
213 #endif
214   /* We'll assume that tag is alway there.
215      Need to statically check for this of course. */
216   struct ___Object___ * tagptr=obj->___tags___;
217
218   if (tagptr->type==TAGTYPE) {
219     if ((struct ___TagDescriptor___ *)tagptr==tagd)
220       obj->___tags___=NULL;
221     else
222       printf("ERROR 1 in tagclear\n");
223   } else {
224     struct ArrayObject *ao=(struct ArrayObject *) tagptr;
225     int i;
226     for(i=0;i<ao->___cachedCode___;i++) {
227       struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
228       if (td==tagd) {
229         ao->___cachedCode___--;
230         if (i<ao->___cachedCode___)
231           ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
232         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
233         if (ao->___cachedCode___==0)
234           obj->___tags___=NULL;
235         goto PROCESSCLEAR;
236       }
237     }
238     printf("ERROR 2 in tagclear\n");
239   }
240  PROCESSCLEAR:
241   {
242     struct ___Object___ *tagset=tagd->flagptr;
243     if (tagset->type!=OBJECTARRAYTYPE) {
244       if (tagset==obj)
245         tagd->flagptr=NULL;
246       else
247         printf("ERROR 3 in tagclear\n");
248     } else {
249       struct ArrayObject *ao=(struct ArrayObject *) tagset;
250       int i;
251       for(i=0;i<ao->___cachedCode___;i++) {
252         struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
253         if (tobj==obj) {
254           ao->___cachedCode___--;
255           if (i<ao->___cachedCode___)
256             ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
257           ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
258           if (ao->___cachedCode___==0)
259             tagd->flagptr=NULL;
260           goto ENDCLEAR;
261         }
262       }
263       printf("ERROR 4 in tagclear\n");
264     }
265   }
266  ENDCLEAR:
267   return;
268   
269 }
270  
271 /* This function allocates a new tag. */
272 #ifdef PRECISE_GC
273 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
274   struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
275 #else
276 struct ___TagDescriptor___ * allocate_tag(int index) {
277   struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
278 #endif
279   v->type=TAGTYPE;
280   v->flag=index;
281   return v;
282
283
284
285
286 /* This function updates the flag for object ptr.  It or's the flag
287    with the or mask and and's it with the andmask. */
288
289 void flagbody(struct ___Object___ *ptr, int flag);
290 #ifdef OPTIONAL
291 void enqueueoptional(struct ___Object___ * currobj);
292
293 struct optionaltaskdescriptor *** makeintersectionotd(int num, struct fsanalysiswrapper ** wrapperarray, int *result){
294   int i,j,k;
295   (*result)=0;
296   struct optionaltaskdescriptor *** bigtmparray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **)*maxotd);
297   struct fsanalysiswrapper * tmpwrapper;
298   struct fsanalysiswrapper * firstwrapper = wrapperarray[0];/*we are sure that num>0*/
299   /*we check if the otd of the first wrapper is contained in all others*/
300   for(i=0; i<firstwrapper->numoptionaltaskdescriptors; i++){
301     struct optionaltaskdescriptor ** tmparray = RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * num);
302     struct optionaltaskdescriptor * otd = firstwrapper->optionaltaskdescriptorarray[i];
303     tmparray[0]=otd;
304     for(j=1; j<num; j++){
305       tmpwrapper = wrapperarray[j];
306       for(k=0; k<tmpwrapper->numoptionaltaskdescriptors; k++){
307         struct optionaltaskdescriptor * tmpotd=tmpwrapper->optionaltaskdescriptorarray[k];
308         if(otd->task->name == tmpotd->task->name){
309           tmparray[j]=tmpotd;
310           goto nextwrapper;
311         }
312         RUNFREE(tmparray);
313         goto nextotd;
314       }
315     nextwrapper:
316       ;
317     }
318     bigtmparray[(*result)]=tmparray;
319     (*result)++;          
320   nextotd:
321     ;
322   }
323   
324   {/*now allocate the good size for otdarray and put the otds*/
325     struct optionaltaskdescriptor *** otdarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * (*result));
326     for(i=0; i<(*result); i++)     
327       otdarray[i]=bigtmparray[i];
328     
329     RUNFREE(bigtmparray);
330     return otdarray;
331   }
332 }
333 #endif
334  
335
336 void flagorand(void * ptr, int ormask, int andmask) {
337   int oldflag=((int *)ptr)[1];
338   int flag=ormask|oldflag;
339 #ifdef OPTIONAL
340   struct ___Object___ * obj = (struct ___Object___ *)ptr;
341   if(obj->failedstatus==1){/*store the information about exitfses*/
342     int i,j,counter=0, offset=0;
343     for(i=0; i<obj->numotds; i++){
344       counter+=obj->otds[i]->numenterflags;
345     }
346     obj->numexitfses=counter;
347     if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
348     obj->exitfses= RUNMALLOC(sizeof(int) * counter);
349     for(i=0; i<obj->numotds; i++){
350       for(j=0; j<obj->otds[i]->numenterflags; j++){
351         oldflag=obj->otds[i]->enterflags[j];
352         flag=ormask|oldflag;
353         flag&=andmask;
354         obj->exitfses[j+offset]=flag;
355       }
356       offset+=obj->otds[i]->numenterflags;
357     }
358     enqueueoptional(ptr);
359   }
360   else
361 #endif
362     {
363     flag&=andmask;
364     // Not sure why this was necessary
365     //  if (flag==oldflag) /* Don't do anything */
366     //  return;
367     //else 
368     flagbody(ptr, flag);
369     }
370 }
371  
372 void intflagorand(void * ptr, int ormask, int andmask) {
373   int oldflag=((int *)ptr)[1];
374   int flag=ormask|oldflag;
375 #ifdef OPTIONAL
376   struct ___Object___ * obj = (struct ___Object___ *)ptr;
377   if(obj->failedstatus==1){/*store the information about exitfses*/
378     int i,j,counter=0, offset=0;
379     for(i=0; i<obj->numotds; i++){
380       counter+=obj->otds[i]->numenterflags;
381     }
382     obj->numexitfses=counter;
383     if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
384     obj->exitfses= RUNMALLOC(sizeof(int) * counter);
385     for(i=0; i<obj->numotds; i++){
386       for(j=0; j<obj->otds[i]->numenterflags; j++){
387         oldflag=obj->otds[i]->enterflags[j];
388         flag=ormask|oldflag;
389         flag&=andmask;
390         obj->exitfses[j+offset]=flag;
391       }
392       offset+=obj->otds[i]->numenterflags;
393     }
394     enqueueoptional(ptr);
395   }
396   else
397 #endif
398     {
399     flag&=andmask;
400     if (flag==oldflag) /* Don't do anything */
401       return;
402     else flagbody(ptr, flag);
403     }
404 }
405
406 void flagorandinit(void * ptr, int ormask, int andmask) {
407   int oldflag=((int *)ptr)[1];
408   int flag=ormask|oldflag;
409 #ifdef OPTIONAL
410   struct ___Object___ * obj = (struct ___Object___ *)ptr;
411   if(obj->failedstatus==1){/*store the information about exitfses*/
412     int i,j,counter=0, offset=0;
413     for(i=0; i<obj->numotds; i++){
414       counter+=obj->otds[i]->numenterflags;
415     }
416     obj->numexitfses=counter;
417     if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
418     obj->exitfses= RUNMALLOC(sizeof(int) * counter);
419     for(i=0; i<obj->numotds; i++){
420       for(j=0; j<obj->otds[i]->numenterflags; j++){
421         oldflag=obj->otds[i]->enterflags[j];
422         flag=ormask|oldflag;
423         flag&=andmask;
424         obj->exitfses[j+offset]=flag;
425       }
426       offset+=obj->otds[i]->numenterflags;
427     }
428     enqueueoptional(ptr);
429   }
430   else
431 #endif
432     {
433     flag&=andmask;
434     flagbody(ptr,flag);
435     }
436 }
437
438 #ifdef OPTIONAL
439  removeoptionalfromqueues(int hashcode, struct ___Object___ * currobj, struct parameterwrapper * flagptr){/*find a better way to free the useless instances of the object*/
440    while(flagptr!=NULL) {
441      struct ___Object___ *temp=NULL;
442      struct parameterwrapper *ptr;
443      struct RuntimeNode * node = flagptr->objectset->listhead;
444      while(node!=NULL){
445        temp=(struct ___Object___ *)node->key;
446        if(temp->failedstatus==1 && temp->hashcode==currobj->hashcode){
447          if(temp!=currobj){
448            RuntimeHashget(flagptr->objectset, (int)temp, (int *) &ptr);
449            RuntimeHashremove(flagptr->objectset, (int)temp, (int) ptr);//remove from wrapper
450            //delete the fields that wont be removed by the GC.
451            if(temp->exitfses!=NULL) RUNFREE(temp->exitfses);
452            if(temp->otds!=NULL) RUNFREE(temp->otds);
453            goto nextwrapper;
454          }
455          else{
456            //remove from wrapper
457            RuntimeHashget(flagptr->objectset, (int)temp, (int *) &ptr);
458            RuntimeHashremove(flagptr->objectset, (int)temp, (int) ptr);
459            goto nextwrapper;
460          }
461        }
462        node=node->next;
463      }
464    nextwrapper:
465      ;
466      flagptr=flagptr->next;
467    }
468  } 
469 #endif
470  
471  void flagbody(struct ___Object___ *ptr, int flag) {
472    struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
473    ptr->flag=flag;
474    
475    /*Remove object from all queues */
476    while(flagptr!=NULL) {
477      struct parameterwrapper *next;
478      struct ___Object___ * tag=ptr->___tags___;
479      RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
480      RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
481      flagptr=next;
482    }
483    
484    {
485      struct QueueItem *tmpptr;
486      struct parameterwrapper * parameter=objectqueues[ptr->type];
487      int i;
488      struct parameterwrapper * prevptr=NULL;
489      struct ___Object___ *tagptr=ptr->___tags___;
490      
491      /* Outer loop iterates through all parameter queues an object of
492         this type could be in.  */
493      
494      while(parameter!=NULL) {
495        /* Check tags */
496        if (parameter->numbertags>0) {
497          if (tagptr==NULL)
498            goto nextloop;//that means the object has no tag but that param needs tag
499          else if(tagptr->type==TAGTYPE) {//one tag
500            struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
501            for(i=0;i<parameter->numbertags;i++) {
502              //slotid is parameter->tagarray[2*i];
503              int tagid=parameter->tagarray[2*i+1];
504              if (tagid!=tagptr->flag)
505                goto nextloop; /*We don't have this tag */         
506            }
507          } else {//multiple tags
508            struct ArrayObject * ao=(struct ArrayObject *) tagptr;
509            for(i=0;i<parameter->numbertags;i++) {
510              //slotid is parameter->tagarray[2*i];
511              int tagid=parameter->tagarray[2*i+1];
512              int j;
513              for(j=0;j<ao->___cachedCode___;j++) {
514                if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
515                  goto foundtag;
516              }
517              goto nextloop;
518            foundtag:
519              ;
520            }
521          }
522        }
523        
524        /* Check flags */
525        for(i=0;i<parameter->numberofterms;i++) {
526          int andmask=parameter->intarray[i*2];
527          int checkmask=parameter->intarray[i*2+1];
528          if ((flag&andmask)==checkmask) {
529            enqueuetasks(parameter, prevptr, ptr);
530            prevptr=parameter;
531            break;
532          }
533        }
534      nextloop:
535        parameter=parameter->next;
536      }
537      ptr->flagptr=prevptr;
538    }
539  }
540  
541 #ifdef OPTIONAL
542  
543  void enqueueoptional(struct ___Object___ * currobj){
544    
545    struct classanalysiswrapper * classwrapper=NULL; 
546    struct fsanalysiswrapper * fswrapper=NULL;
547    int counter=0;
548    int numoptionaltaskdescriptors=0;
549    struct optionaltaskdescriptor *** optionaltaskdescriptorarray=NULL;
550    struct fsanalysiswrapper ** goodfswrappersarray=NULL;
551    int numgoodfswrappers=0;
552 #ifdef DEBUG
553    if(currobj->numexitfses==0)
554      printf("Handling failed object\nType : %i\nFlag : 0x%x\n", currobj->type, currobj->flag);
555    else{
556      printf("Handling failed object\nType : %i\n", currobj->type);
557      int fscount=0;
558      for(fscount=0; fscount<currobj->numexitfses; fscount++)
559        printf("Flag : 0x%x\n", currobj->exitfses[fscount]);
560    }
561    struct ___Object___ * tagptr = currobj->___tags___;
562    if(tagptr!=NULL){
563      if(tagptr->type==TAGTYPE) {
564        printf("Tag : %i\n", tagptr->flag);}
565      else {
566        struct ArrayObject * ao=(struct ArrayObject *) tagptr;
567        int numbertags = ao->___length___;
568        for(counter=0; counter<numbertags; counter++){
569          printf("Tag : %i\n", ao[counter].flag);
570        } 
571      }
572    }
573 #endif    
574    /*set the object as failed*/
575    currobj->failedstatus = 1;
576    
577    /*test what optionaltaskdescriptors are available,
578      find the class corresponding*/
579    
580    for(counter = 0; counter<numclasses; counter++){
581      classwrapper = classanalysiswrapperarray[counter];
582      if(classwrapper == NULL){
583        fprintf(stderr, "ERROR : \"struct classanalysiswrapper * classwraper\" is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
584        goto nothingtodo;
585      }
586      /*check object type*/
587      if( currobj->type == classwrapper->type)
588        goto classchecked;
589    }
590 #ifdef DEBUG
591    printf("No task will use this parameter as optional\n");
592 #endif
593    removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
594    goto nothingtodo;
595  classchecked:
596    ;
597 #ifdef DEBUG
598    printf("Found the class, search through fses\n");
599 #endif        
600    /*search through fses*/
601    goodfswrappersarray = RUNMALLOC(sizeof(struct fsanalysiswrapper *) * classwrapper->numfsanalysiswrappers); /*max number of good fswrappers*/
602    if(goodfswrappersarray == NULL){
603      fprintf(stderr, "ERROR : \"struct fsanalysiswrapper ** goodfswrappersarray\" is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
604      removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
605      goto nothingtodo;
606    }
607    for(counter = 0; counter<classwrapper->numfsanalysiswrappers; counter++){
608      /*test the FS of the object (include flags and tags)*/
609      fswrapper = classwrapper->fsanalysiswrapperarray[counter];
610      if(fswrapper == NULL){
611        fprintf(stderr, "ERROR : struct fsanalysiswrapper * is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
612        removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
613        goto nothingtodo;
614      }
615      /*check tags*/
616      struct ___Object___ * tagptr = currobj->___tags___;
617      if(fswrapper->numtags>0){
618        if (tagptr==NULL)
619          goto nextloop;//that means the object has no tag but that param needs tag
620        else if(tagptr->type==TAGTYPE) {//one tag
621          if(fswrapper->numtags!=1) goto nextloop;//we don't have the right number of tags
622          struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
623          if (fswrapper->tags[0]!=tagptr->flag)
624            goto nextloop; 
625        }          
626        else {//multiple tags
627          struct ArrayObject * ao=(struct ArrayObject *) tagptr;
628          int tag_counter=0;
629          if(ao->___length___!=fswrapper->numtags) goto nextloop;//we don't have the right number of tags
630          for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
631            int tagid=fswrapper->tags[tag_counter];
632            int j;
633            for(j=0;j<ao->___cachedCode___;j++) {
634              if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
635                goto foundtag;
636            }
637            goto nextloop;
638          foundtag:
639            ;                  ;
640          }
641        }
642      }
643      
644      //check flags
645      if(currobj->numexitfses==0){/*first time the method is invoqued*/
646        if( currobj->flag == fswrapper->flags){
647          int otdc;
648          optionaltaskdescriptorarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **) * fswrapper->numoptionaltaskdescriptors);
649          numoptionaltaskdescriptors = fswrapper->numoptionaltaskdescriptors;
650          for(otdc = 0; otdc<fswrapper->numoptionaltaskdescriptors; otdc++){
651            struct optionaltaskdescriptor ** tmpptr = RUNMALLOC(sizeof(struct optionaltaskdescriptor *));
652            tmpptr[0] = fswrapper->optionaltaskdescriptorarray[otdc];
653            optionaltaskdescriptorarray[otdc] = tmpptr;
654          }
655          numgoodfswrappers=1;
656          goto fschecked;
657        }
658      }
659      else if(currobj->numexitfses==1){/*one fs exit*/
660        if(currobj->exitfses[0] == fswrapper->flags){
661          int otdc;
662          optionaltaskdescriptorarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **) * fswrapper->numoptionaltaskdescriptors);
663          numoptionaltaskdescriptors = fswrapper->numoptionaltaskdescriptors;
664          for(otdc = 0; otdc<fswrapper->numoptionaltaskdescriptors; otdc++){
665            struct optionaltaskdescriptor ** tmpptr = RUNMALLOC(sizeof(struct optionaltaskdescriptor *));
666            tmpptr[0] = fswrapper->optionaltaskdescriptorarray[otdc];
667            optionaltaskdescriptorarray[otdc] = tmpptr;
668          }
669          numgoodfswrappers=1;
670          goto fschecked;
671        }
672      }
673      else{
674        int fscount=0;
675        for(fscount=0; fscount<currobj->numexitfses; fscount++){
676          
677          if( currobj->exitfses[fscount] == fswrapper->flags ){/*see if the fswraper correspond to one of the fses*/
678            goodfswrappersarray[numgoodfswrappers]=fswrapper;
679            numgoodfswrappers++;
680          }
681        }
682        if(counter==classwrapper->numfsanalysiswrappers-1) goto makeintersection; /*last fswrapper*/
683      }
684    nextloop:
685      ;
686    }
687  nofs:
688    ;
689 #ifdef DEBUG
690    printf("FS not found, Nothing more to do\n");
691 #endif
692    removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
693    goto nothingtodo;
694  makeintersection:
695    ;
696    if(numgoodfswrappers==0 || numgoodfswrappers==1) goto nofs; /*nothing has been found, we expect more than one wrapper for multiple flags*/
697    optionaltaskdescriptorarray = makeintersectionotd(numgoodfswrappers, goodfswrappersarray, &numoptionaltaskdescriptors);
698    if(optionaltaskdescriptorarray==NULL){
699      fprintf(stderr, "ERROR : struct optionaltaskdescriptor ** is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
700      goto nothingtodo;
701    }
702    
703   fschecked:
704    ;
705 #ifdef DEBUG
706    printf("FS(es) found, intersection created, %i potential tasks :\n", numoptionaltaskdescriptors);
707    
708    
709    /*find the parameterwrapper corresponding to the potential task*/
710    for(counter = 0; counter<numoptionaltaskdescriptors; counter++){
711      struct optionaltaskdescriptor ** tmpptr = optionaltaskdescriptorarray[counter];
712      printf("Task %s\n", tmpptr[0]->task->name);
713    }
714    
715 #endif
716    {            
717      struct parameterwrapper * prevptr = NULL;
718      struct parameterwrapper * flagptr = objectqueues[currobj->type];
719      removeoptionalfromqueues(currobj->hashcode,currobj, flagptr);
720      /*process for each otd*/
721      for(counter = 0; counter<numoptionaltaskdescriptors; counter++){
722        struct parameterwrapper * parameter = objectqueues[currobj->type];
723        struct optionaltaskdescriptor ** tmpptr = optionaltaskdescriptorarray[counter];
724        struct optionaltaskdescriptor * currotd = tmpptr[0];
725        int otd_counter=0;
726        while(parameter->task != currotd->task)
727          parameter=parameter->next;
728 #ifdef DEBUG
729        printf("found parameterwrapper for task : %s\n", parameter->task->name);
730 #endif
731        
732        struct ___Object___ * newobj = RUNMALLOC(sizeof(struct ___Object___));
733        (*newobj)=(*currobj);
734        newobj->numotds=numgoodfswrappers;
735        newobj->otds=RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * numgoodfswrappers);
736        for(otd_counter=0; otd_counter<numgoodfswrappers; otd_counter++){
737          newobj->otds[otd_counter]=tmpptr[otd_counter];
738        }
739        enqueuetasks(parameter, prevptr, newobj);
740        prevptr = parameter;
741        
742      nextotd:
743        ;
744      }
745      
746    }
747  nothingtodo:
748    
749    /*if(currobj->exitfses!=NULL) RUNFREE(currobj->exitfses);
750      if(currobj->otds!=NULL) RUNFREE(currobj->otds);*///there has been a problem just before the program exit, maybe due to the GC ?
751    RUNFREE(optionaltaskdescriptorarray);
752    ;
753  }
754
755  /*we need to check if the object is optional, in this case, test the predicate*/
756  /*here is the code for predicate checking*/
757  /*The code has not been tested. I don't even know if it is working or efficient but it is a lead...
758    if(currotd->numpredicatemembers == 0){
759    printf("this task can be fired\n");
760    goto enqueuetask;
761    }
762    else{
763    int pred_counter;
764    int predicatetrue = 0;
765    for(pred_counter = 0; pred_counter<currotd->numpredicatemembers; pred_counter++){
766    struct predicatemember * currpred = currotd->predicatememberarray[pred_counter];
767    printf("predicate type : %i\n", currpred->type);
768    
769    //test if the predicate member is true
770    struct parameterwrapper * paramwrapper = objectqueues[currpred->type];
771    while(paramwrapper!=NULL){
772    struct RuntimeIterator * it = allocateRuntimeIterator(paramwrapper->objectset->listhead);
773    do{
774    struct ___Object___ * obj = (struct ___Object___ *)Runkey(it);
775    printf("obj type : %i\n", obj->type);
776    if(obj->type == currpred->type){
777    //test the predicate
778    printf("predicate to test\n");
779    //only if good
780    goto enqueuetask;
781    }
782    Runnext(it);
783    }while(RunhasNext(it));
784    paramwrapper=paramwrapper->next;
785    }
786    printf("not the good predicate");
787    //goto endanalysis
788    }
789    //the predicate members have to be all true
790    }*/
791  
792 #endif
793  
794  void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
795   void * taskpointerarray[MAXTASKPARAMS];
796   int j;
797   int numparams=parameter->task->numParameters;
798   int numiterators=parameter->task->numTotal-1;
799
800   struct taskdescriptor * task=parameter->task;
801   
802   RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);//this add the object to parameterwrapper
803   
804   /* Add enqueued object to parameter vector */
805   taskpointerarray[parameter->slot]=ptr;
806
807   /* Reset iterators */
808   for(j=0;j<numiterators;j++) {
809     toiReset(&parameter->iterators[j]);
810   }
811
812   /* Find initial state */
813   for(j=0;j<numiterators;j++) {
814   backtrackinit:
815     if(toiHasNext(&parameter->iterators[j], taskpointerarray))
816       toiNext(&parameter->iterators[j], taskpointerarray);
817     else if (j>0) {
818       /* Need to backtrack */
819       toiReset(&parameter->iterators[j]);
820       j--;
821       goto backtrackinit;
822     } else {
823       /* Nothing to enqueue */
824       return;
825     }
826   }
827
828   
829   while(1) {
830     /* Enqueue current state */
831     int launch = 0;
832     struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
833     tpd->task=task;
834     tpd->numParameters=numiterators+1;
835     tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
836     for(j=0;j<=numiterators;j++){
837 #ifdef OPTIONAL
838 #ifdef DEBUG
839       struct ___Object___ * obj = (struct ___Object___ *)taskpointerarray[j];
840       if(obj->failedstatus==1)
841         printf("parameter %i used as optional for task %s\n", obj->type, task->name);
842 #endif
843 #endif
844       tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
845     }
846     /* Enqueue task */
847     if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
848       genputtable(activetasks, tpd, tpd);
849     } else {
850       RUNFREE(tpd->parameterArray);
851       RUNFREE(tpd);
852     }
853     
854     /* This loop iterates to the next parameter combination */
855     if (numiterators==0)
856       return;
857
858     for(j=numiterators-1; j<numiterators;j++) {
859     backtrackinc:
860       if(toiHasNext(&parameter->iterators[j], taskpointerarray))
861         toiNext(&parameter->iterators[j], taskpointerarray);
862       else if (j>0) {
863         /* Need to backtrack */
864         toiReset(&parameter->iterators[j]);
865         j--;
866         goto backtrackinc;
867       } else {
868         /* Nothing more to enqueue */
869         return;
870       }
871     }
872   }
873 }
874  
875 /* Handler for signals. The signals catch null pointer errors and
876    arithmatic errors. */
877
878 void myhandler(int sig, siginfo_t *info, void *uap) {
879   sigset_t toclear;
880 #ifdef DEBUG
881   printf("sig=%d\n",sig);
882   printf("signal\n");
883 #endif
884   sigemptyset(&toclear);
885   sigaddset(&toclear, sig);
886   sigprocmask(SIG_UNBLOCK, &toclear,NULL); 
887   longjmp(error_handler,1);
888 }
889
890 fd_set readfds;
891 int maxreadfd;
892 struct RuntimeHash *fdtoobject;
893
894 void addreadfd(int fd) {
895   if (fd>=maxreadfd)
896     maxreadfd=fd+1;
897   FD_SET(fd, &readfds);
898 }
899
900 void removereadfd(int fd) {
901   FD_CLR(fd, &readfds);
902   if (maxreadfd==(fd+1)) {
903     maxreadfd--;
904     while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
905       maxreadfd--;
906   }
907 }
908
909 #ifdef PRECISE_GC
910 #define OFFSET 2
911 #else
912 #define OFFSET 0
913 #endif
914
915 void executetasks() {
916   void * taskpointerarray[MAXTASKPARAMS+OFFSET];
917
918   /* Set up signal handlers */
919   struct sigaction sig;
920   sig.sa_sigaction=&myhandler;
921   sig.sa_flags=SA_SIGINFO;
922   sigemptyset(&sig.sa_mask);
923
924   /* Catch bus errors, segmentation faults, and floating point exceptions*/
925   sigaction(SIGBUS,&sig,0);
926   sigaction(SIGSEGV,&sig,0);
927   sigaction(SIGFPE,&sig,0);
928   sigaction(SIGPIPE,&sig,0);
929
930   /* Zero fd set */
931   FD_ZERO(&readfds);
932   maxreadfd=0;
933   fdtoobject=allocateRuntimeHash(100);
934
935   /* Map first block of memory to protected, anonymous page */
936   mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
937
938   newtask:
939   while((hashsize(activetasks)>0)||(maxreadfd>0)) {
940
941     /* Check if any filedescriptors have IO pending */
942     if (maxreadfd>0) {
943       int i;
944       struct timeval timeout={0,0};
945       fd_set tmpreadfds;
946       int numselect;
947       tmpreadfds=readfds;
948       numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
949       if (numselect>0) {
950         /* Process ready fd's */
951         int fd;
952         for(fd=0;fd<maxreadfd;fd++) {
953           if (FD_ISSET(fd, &tmpreadfds)) {
954             /* Set ready flag on object */
955             void * objptr;
956             //      printf("Setting fd %d\n",fd);
957             if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
958               intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
959             }
960           }
961         }
962       }
963     }
964
965     /* See if there are any active tasks */
966     if (hashsize(activetasks)>0) {
967       int i;
968       currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
969       genfreekey(activetasks, currtpd);
970       
971       /* Check if this task has failed, allow a task that contains optional objects to fire */
972       if (gencontains(failedtasks, currtpd)) {
973         // Free up task parameter descriptor
974         RUNFREE(currtpd->parameterArray);
975         RUNFREE(currtpd);
976         goto newtask;
977       }
978       int numparams=currtpd->task->numParameters;
979       int numtotal=currtpd->task->numTotal;
980       
981       /* Make sure that the parameters are still in the queues */
982       for(i=0;i<numparams;i++) {
983         void * parameter=currtpd->parameterArray[i];
984         struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
985         struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
986         int j;
987         /* Check that object is still in queue */
988 #ifdef OPTIONAL
989         struct ___Object___ * obj = (struct ___Object___ *)parameter;
990         if(obj->failedstatus==1){
991           struct ___Object___ *temp=NULL;
992           struct parameterwrapper * ptr;
993           struct RuntimeNode * node = pw->objectset->listhead;
994           while(node!=NULL){
995             temp=(struct ___Object___ *)node->key;
996             if(temp->failedstatus==1 && temp->hashcode==obj->hashcode){
997               if(temp==obj)
998                 goto parameterpresent;
999             }
1000             node=node->next;
1001           }
1002           RUNFREE(currtpd->parameterArray);
1003           RUNFREE(currtpd);
1004           goto newtask;
1005         }
1006         else
1007 #endif
1008         {
1009           if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
1010             RUNFREE(currtpd->parameterArray);
1011             RUNFREE(currtpd);
1012             goto newtask;
1013           }
1014         }
1015       parameterpresent:
1016         ;
1017         /* Check that object still has necessary tags */
1018         for(j=0;j<pd->numbertags;j++) {
1019           int slotid=pd->tagarray[2*j]+numparams;
1020           struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1021           if (!containstag(parameter, tagd)) {
1022             RUNFREE(currtpd->parameterArray);
1023             RUNFREE(currtpd);
1024             goto newtask;
1025           }
1026         }
1027         
1028         taskpointerarray[i+OFFSET]=parameter;
1029       }
1030       /* Copy the tags */
1031       for(;i<numtotal;i++) {
1032         taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1033       }
1034
1035       {
1036         /* Checkpoint the state */
1037         forward=allocateRuntimeHash(100);
1038         reverse=allocateRuntimeHash(100);
1039         void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1040         int x;
1041         if (x=setjmp(error_handler)) {
1042           int counter;
1043           /* Recover */
1044 #ifdef DEBUG
1045           printf("Fatal Error=%d, Recovering!\n",x);
1046 #endif
1047           genputtable(failedtasks,currtpd,currtpd);
1048           restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1049
1050 #ifdef OPTIONAL
1051 #ifdef DEBUG      
1052           printf("%i object(s) restored\n", currtpd->task->numParameters);
1053 #endif 
1054           
1055           for(counter=0; counter<currtpd->task->numParameters; counter++){
1056             //remove the object from the previous parameterwrapper (maybe not necessary)
1057             //do a new instance of the object. It allows the restored object to be used by other tasks as a non optional arg.
1058             struct ___Object___ * currobj = RUNMALLOC(sizeof(struct ___Object___));
1059             (*currobj)=(*(struct ___Object___ *)currtpd->parameterArray[counter]);
1060             currobj->numexitfses = 0;
1061             currobj->exitfses = NULL;
1062             currobj->otds=NULL;
1063             currobj->hashcode=(int)currobj;
1064             enqueueoptional( currobj );
1065           }
1066 #endif
1067           freeRuntimeHash(forward);
1068           freeRuntimeHash(reverse);
1069           freemalloc();
1070           forward=NULL;
1071           reverse=NULL;
1072         } else {
1073           if (injectfailures) {
1074             if ((((double)random())/RAND_MAX)<failurechance) {
1075               printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1076               longjmp(error_handler,10);
1077             }
1078           }
1079           /* Actually call task */
1080 #ifdef PRECISE_GC
1081           ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
1082           taskpointerarray[1]=NULL;
1083 #endif
1084           if(debugtask){
1085             printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1086             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1087             printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1088           } else
1089             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1090             
1091           freeRuntimeHash(forward);
1092           freeRuntimeHash(reverse);
1093           freemalloc();
1094           // Free up task parameter descriptor
1095           RUNFREE(currtpd->parameterArray);
1096           RUNFREE(currtpd);
1097           forward=NULL;
1098           reverse=NULL;
1099         }
1100       }
1101     }
1102   }
1103 }
1104  
1105 /* This function processes an objects tags */
1106 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1107   int i;
1108   
1109   for(i=0;i<pd->numbertags;i++) {
1110     int slotid=pd->tagarray[2*i];
1111     int tagid=pd->tagarray[2*i+1];
1112     
1113     if (statusarray[slotid+numparams]==0) {
1114       parameter->iterators[*iteratorcount].istag=1;
1115       parameter->iterators[*iteratorcount].tagid=tagid;
1116       parameter->iterators[*iteratorcount].slot=slotid+numparams;
1117       parameter->iterators[*iteratorcount].tagobjectslot=index;
1118       statusarray[slotid+numparams]=1;
1119       (*iteratorcount)++;
1120     }
1121   }
1122 }
1123
1124
1125 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1126   int i;
1127   int tagcount=0;
1128   struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1129
1130   parameter->iterators[*iteratorcount].istag=0;
1131   parameter->iterators[*iteratorcount].slot=index;
1132   parameter->iterators[*iteratorcount].objectset=objectset;
1133   statusarray[index]=1;
1134
1135   for(i=0;i<pd->numbertags;i++) {
1136     int slotid=pd->tagarray[2*i];
1137     int tagid=pd->tagarray[2*i+1];
1138     if (statusarray[slotid+numparams]!=0) {
1139       /* This tag has already been enqueued, use it to narrow search */
1140       parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1141       tagcount++;
1142     }
1143   }
1144   parameter->iterators[*iteratorcount].numtags=tagcount;
1145
1146   (*iteratorcount)++;
1147 }
1148
1149 /* This function builds the iterators for a task & parameter */
1150
1151 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1152   int statusarray[MAXTASKPARAMS];
1153   int i;
1154   int numparams=task->numParameters;
1155   int iteratorcount=0;
1156   for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1157
1158   statusarray[index]=1; /* Initial parameter */
1159   /* Process tags for initial iterator */
1160   
1161   processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1162   
1163   while(1) {
1164   loopstart:
1165     /* Check for objects with existing tags */
1166     for(i=0;i<numparams;i++) {
1167       if (statusarray[i]==0) {
1168         struct parameterdescriptor *pd=task->descriptorarray[i];
1169         int j;
1170         for(j=0;j<pd->numbertags;j++) {
1171           int slotid=pd->tagarray[2*j];
1172           if(statusarray[slotid+numparams]!=0) {
1173             processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1174             processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1175             goto loopstart;
1176           }
1177         }
1178       }
1179     }
1180
1181     /* Next do objects w/ unbound tags*/
1182
1183     for(i=0;i<numparams;i++) {
1184       if (statusarray[i]==0) {
1185         struct parameterdescriptor *pd=task->descriptorarray[i];
1186         if (pd->numbertags>0) {
1187           processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1188           processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1189           goto loopstart;
1190         }
1191       }
1192     }
1193
1194     /* Nothing with a tag enqueued */
1195
1196     for(i=0;i<numparams;i++) {
1197       if (statusarray[i]==0) {
1198         struct parameterdescriptor *pd=task->descriptorarray[i];
1199         processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1200         processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1201         goto loopstart;
1202       }
1203     }
1204
1205     /* Nothing left */
1206     return;
1207   }
1208 }
1209
1210
1211  
1212
1213 /* This function processes the task information to create queues for
1214    each parameter type. */
1215
1216 void processtasks() {
1217   int i;
1218   for(i=0;i<numtasks;i++) {
1219     struct taskdescriptor * task=taskarray[i];
1220     int j;
1221
1222     for(j=0;j<task->numParameters;j++) {
1223       struct parameterdescriptor *param=task->descriptorarray[j];
1224       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1225       struct parameterwrapper ** ptr=&objectqueues[param->type];
1226
1227       param->queue=parameter;
1228       parameter->objectset=allocateRuntimeHash(10);
1229       parameter->numberofterms=param->numberterms;
1230       parameter->intarray=param->intarray;
1231       parameter->numbertags=param->numbertags;
1232       parameter->tagarray=param->tagarray;
1233       parameter->task=task;
1234       /* Link new queue in */
1235       while((*ptr)!=NULL)
1236         ptr=&((*ptr)->next);
1237       (*ptr)=parameter;
1238     }
1239
1240     /* Build iterators for parameters */
1241     for(j=0;j<task->numParameters;j++) {
1242       struct parameterdescriptor *param=task->descriptorarray[j];
1243       struct parameterwrapper *parameter=param->queue;      
1244       parameter->slot=j;
1245       builditerators(task, j, parameter);
1246     }
1247   }
1248 }
1249
1250 void toiReset(struct tagobjectiterator * it) {
1251   if (it->istag) {
1252     it->tagobjindex=0;
1253   } else if (it->numtags>0) {
1254     it->tagobjindex=0;
1255   } else {
1256     RuntimeHashiterator(it->objectset, &it->it);
1257   }
1258 }
1259
1260 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
1261   if (it->istag) {
1262     /* Iterate tag */
1263     /* Get object with tags */
1264     struct ___Object___ *obj=objectarray[it->tagobjectslot];
1265     struct ___Object___ *tagptr=obj->___tags___;
1266     if (tagptr->type==TAGTYPE) {
1267       if ((it->tagobjindex==0)&& /* First object */
1268           (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1269         return 1;
1270       else
1271         return 0;
1272     } else {
1273       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1274       int tagindex=it->tagobjindex;
1275       for(;tagindex<ao->___cachedCode___;tagindex++) {
1276         struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1277         if (td->flag==it->tagid) {
1278           it->tagobjindex=tagindex; /* Found right type of tag */
1279           return 1;
1280         }
1281       }
1282       return 0;
1283     }
1284   } else if (it->numtags>0) {
1285     /* Use tags to locate appropriate objects */
1286     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1287     struct ___Object___ *objptr=tag->flagptr;
1288     int i;
1289     if (objptr->type!=OBJECTARRAYTYPE) {
1290       if (it->tagobjindex>0)
1291         return 0;
1292       if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
1293         return 0;
1294       for(i=1;i<it->numtags;i++) {
1295         struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1296         if (!containstag(objptr,tag2))
1297           return 0;
1298       }
1299       return 1;
1300     } else {
1301       struct ArrayObject *ao=(struct ArrayObject *) objptr;
1302       int tagindex;
1303       int i;
1304       for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1305         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1306         if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
1307           continue;
1308         for(i=1;i<it->numtags;i++) {
1309           struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1310           if (!containstag(objptr,tag2))
1311             goto nexttag;
1312         }
1313         return 1;
1314       nexttag:
1315         ;
1316       }
1317       it->tagobjindex=tagindex;
1318       return 0;
1319     }
1320   } else {
1321     return RunhasNext(&it->it);
1322   }
1323 }
1324
1325 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1326   int j;
1327   struct ___Object___ * objptr=tag->flagptr;
1328   if (objptr->type==OBJECTARRAYTYPE) {
1329     struct ArrayObject *ao=(struct ArrayObject *)objptr;
1330     for(j=0;j<ao->___cachedCode___;j++) {
1331       if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1332         return 1;
1333     }
1334     return 0;
1335   } else
1336     return objptr==ptr;
1337 }
1338
1339 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
1340   /* hasNext has all of the intelligence */
1341   if(it->istag) {
1342     /* Iterate tag */
1343     /* Get object with tags */
1344     struct ___Object___ *obj=objectarray[it->tagobjectslot];
1345     struct ___Object___ *tagptr=obj->___tags___;
1346     if (tagptr->type==TAGTYPE) {
1347       it->tagobjindex++;
1348       objectarray[it->slot]=tagptr;
1349     } else {
1350       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1351       objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1352     }
1353   } else if (it->numtags>0) {
1354     /* Use tags to locate appropriate objects */
1355     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1356     struct ___Object___ *objptr=tag->flagptr;
1357     if (objptr->type!=OBJECTARRAYTYPE) {
1358       it->tagobjindex++;
1359       objectarray[it->slot]=objptr;
1360     } else {
1361       struct ArrayObject *ao=(struct ArrayObject *) objptr;
1362       objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1363     }
1364   } else {
1365     /* Iterate object */
1366     objectarray[it->slot]=(void *)Runkey(&it->it);
1367     Runnext(&it->it);
1368   }
1369 }
1370
1371
1372 #endif