3 #include "structdefs.h"
5 #include "checkpoint.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
15 extern int injectfailures;
16 extern float failurechance;
21 #include "instrument.h"
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;
31 int main(int argc, char **argv) {
33 GC_init(); // Initialize the garbage collector
39 initializeexithandler();
40 /* Create table for failed tasks */
41 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
42 (int (*)(void *,void *)) &comparetpd);
43 /* Create queue of active tasks */
44 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
45 (int (*)(void *,void *)) &comparetpd);
47 /* Process task information */
50 /* Create startup object */
51 createstartupobject(argc, argv);
53 /* Start executing the tasks */
57 void createstartupobject(int argc, char ** argv) {
60 /* Allocate startup object */
62 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
63 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
65 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
66 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
68 /* Build array of strings */
69 startupobject->___parameters___=stringarray;
71 int length=strlen(argv[i]);
73 struct ___String___ *newstring=NewString(NULL, argv[i],length);
75 struct ___String___ *newstring=NewString(argv[i],length);
77 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
80 /* Set initialized flag for startup object */
81 flagorand(startupobject,1,0xFFFFFFFF);
84 int hashCodetpd(struct taskparamdescriptor *ftd) {
85 int hash=(int)ftd->task;
87 for(i=0;i<ftd->numParameters;i++){
88 hash^=(int)ftd->parameterArray[i];
93 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
95 if (ftd1->task!=ftd2->task)
97 for(i=0;i<ftd1->numParameters;i++)
98 if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
103 /* This function sets a tag. */
105 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
107 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
109 struct ___Object___ * tagptr=obj->___tags___;
111 obj->___tags___=(struct ___Object___ *)tagd;
113 /* Have to check if it is already set */
114 if (tagptr->type==TAGTYPE) {
115 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
119 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
120 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
121 obj=(struct ___Object___ *)ptrarray[2];
122 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
123 td=(struct ___TagDescriptor___ *) obj->___tags___;
125 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
127 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
128 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
129 obj->___tags___=(struct ___Object___ *) ao;
130 ao->___cachedCode___=2;
134 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
135 for(i=0;i<ao->___cachedCode___;i++) {
136 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
140 if (ao->___cachedCode___<ao->___length___) {
141 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
142 ao->___cachedCode___++;
145 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
146 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
147 obj=(struct ___Object___ *)ptrarray[2];
148 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
149 ao=(struct ArrayObject *)obj->___tags___;
151 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
153 aonew->___cachedCode___=ao->___length___+1;
154 for(i=0;i<ao->___length___;i++) {
155 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
157 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
163 struct ___Object___ * tagset=tagd->flagptr;
167 } else if (tagset->type!=OBJECTARRAYTYPE) {
169 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
170 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
171 obj=(struct ___Object___ *)ptrarray[2];
172 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
174 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
176 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
177 ARRAYSET(ao, struct ___Object___ *, 1, obj);
178 ao->___cachedCode___=2;
179 tagd->flagptr=(struct ___Object___ *)ao;
181 struct ArrayObject *ao=(struct ArrayObject *) tagset;
182 if (ao->___cachedCode___<ao->___length___) {
183 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
187 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
188 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
189 obj=(struct ___Object___ *)ptrarray[2];
190 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
191 ao=(struct ArrayObject *)tagd->flagptr;
193 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
195 aonew->___cachedCode___=ao->___cachedCode___+1;
196 for(i=0;i<ao->___length___;i++) {
197 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
199 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
200 tagd->flagptr=(struct ___Object___ *) ao;
206 /* This function clears a tag. */
208 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
210 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
212 /* We'll assume that tag is alway there.
213 Need to statically check for this of course. */
214 struct ___Object___ * tagptr=obj->___tags___;
216 if (tagptr->type==TAGTYPE) {
217 if ((struct ___TagDescriptor___ *)tagptr==tagd)
218 obj->___tags___=NULL;
220 printf("ERROR 1 in tagclear\n");
222 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
224 for(i=0;i<ao->___cachedCode___;i++) {
225 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
227 ao->___cachedCode___--;
228 if (i<ao->___cachedCode___)
229 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
230 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
231 if (ao->___cachedCode___==0)
232 obj->___tags___=NULL;
236 printf("ERROR 2 in tagclear\n");
240 struct ___Object___ *tagset=tagd->flagptr;
241 if (tagset->type!=OBJECTARRAYTYPE) {
245 printf("ERROR 3 in tagclear\n");
247 struct ArrayObject *ao=(struct ArrayObject *) tagset;
249 for(i=0;i<ao->___cachedCode___;i++) {
250 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
252 ao->___cachedCode___--;
253 if (i<ao->___cachedCode___)
254 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
255 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
256 if (ao->___cachedCode___==0)
261 printf("ERROR 4 in tagclear\n");
269 /* This function allocates a new tag. */
271 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
272 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
274 struct ___TagDescriptor___ * allocate_tag(int index) {
275 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
284 /* This function updates the flag for object ptr. It or's the flag
285 with the or mask and and's it with the andmask. */
287 void flagbody(struct ___Object___ *ptr, int flag);
289 void enqueueoptional(struct ___Object___ * currobj);
291 struct optionaltaskdescriptor *** makeintersectionotd(int num, struct fsanalysiswrapper ** wrapperarray, int *result){
294 struct optionaltaskdescriptor *** bigtmparray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **)*maxotd);
295 struct fsanalysiswrapper * tmpwrapper;
296 struct fsanalysiswrapper * firstwrapper = wrapperarray[0];/*we are sure that num>0*/
297 /*we check if the otd of the first wrapper is contained in all others*/
298 for(i=0; i<firstwrapper->numoptionaltaskdescriptors; i++){
299 struct optionaltaskdescriptor ** tmparray = RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * num);
300 struct optionaltaskdescriptor * otd = firstwrapper->optionaltaskdescriptorarray[i];
302 for(j=1; j<num; j++){
303 tmpwrapper = wrapperarray[j];
304 for(k=0; k<tmpwrapper->numoptionaltaskdescriptors; k++){
305 struct optionaltaskdescriptor * tmpotd=tmpwrapper->optionaltaskdescriptorarray[k];
306 if(otd->task->name == tmpotd->task->name){
316 bigtmparray[(*result)]=tmparray;
322 {/*now allocate the good size for otdarray and put the otds*/
323 struct optionaltaskdescriptor *** otdarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * (*result));
324 for(i=0; i<(*result); i++)
325 otdarray[i]=bigtmparray[i];
327 RUNFREE(bigtmparray);
334 void flagorand(void * ptr, int ormask, int andmask) {
335 int oldflag=((int *)ptr)[1];
336 int flag=ormask|oldflag;
338 struct ___Object___ * obj = (struct ___Object___ *)ptr;
339 if(obj->failedstatus==1){/*store the information about exitfses*/
340 int i,j,counter=0, offset=0;
341 for(i=0; i<obj->numotds; i++){
342 counter+=obj->otds[i]->numenterflags;
344 obj->numexitfses=counter;
345 if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
346 obj->exitfses= RUNMALLOC(sizeof(int) * counter);
347 for(i=0; i<obj->numotds; i++){
348 for(j=0; j<obj->otds[i]->numenterflags; j++){
349 oldflag=obj->otds[i]->enterflags[j];
352 obj->exitfses[j+offset]=flag;
354 offset+=obj->otds[i]->numenterflags;
356 enqueueoptional(ptr);
366 void intflagorand(void * ptr, int ormask, int andmask) {
367 int oldflag=((int *)ptr)[1];
368 int flag=ormask|oldflag;
370 struct ___Object___ * obj = (struct ___Object___ *)ptr;
371 if(obj->failedstatus==1) {/*store the information about exitfses*/
372 int i,j,counter=0, offset=0;
373 for(i=0; i<obj->numotds; i++) {
374 counter+=obj->otds[i]->numenterflags;
376 obj->numexitfses=counter;
377 if(obj->exitfses!=NULL)
378 RUNFREE(obj->exitfses);
379 obj->exitfses= RUNMALLOC(sizeof(int) * counter);
380 for(i=0; i<obj->numotds; i++) {
381 for(j=0; j<obj->otds[i]->numenterflags; j++){
382 oldflag=obj->otds[i]->enterflags[j];
385 obj->exitfses[j+offset]=flag;
387 offset+=obj->otds[i]->numenterflags;
389 enqueueoptional(ptr);
395 if (flag==oldflag) /* Don't do anything */
397 else flagbody(ptr, flag);
401 void flagorandinit(void * ptr, int ormask, int andmask) {
402 int oldflag=((int *)ptr)[1];
403 int flag=ormask|oldflag;
405 struct ___Object___ * obj = (struct ___Object___ *)ptr;
406 if(obj->failedstatus==1){/*store the information about exitfses*/
407 int i,j,counter=0, offset=0;
408 for(i=0; i<obj->numotds; i++){
409 counter+=obj->otds[i]->numenterflags;
411 obj->numexitfses=counter;
412 if(obj->exitfses!=NULL) RUNFREE(obj->exitfses);
413 obj->exitfses= RUNMALLOC(sizeof(int) * counter);
414 for(i=0; i<obj->numotds; i++){
415 for(j=0; j<obj->otds[i]->numenterflags; j++){
416 oldflag=obj->otds[i]->enterflags[j];
419 obj->exitfses[j+offset]=flag;
421 offset+=obj->otds[i]->numenterflags;
423 enqueueoptional(ptr);
434 removeoptionalfromqueues(int hashcode, struct ___Object___ * currobj, struct parameterwrapper * flagptr){/*find a better way to free the useless instances of the object*/
435 while(flagptr!=NULL) {
436 struct ___Object___ *temp=NULL;
437 struct parameterwrapper *ptr;
438 struct ObjectNode * node = flagptr->objectset->listhead;
440 temp=(struct ___Object___ *)node->key;
441 if(temp->failedstatus==1 && temp->hashcode==currobj->hashcode){
443 ObjectHashremove(flagptr->objectset, (int)temp);//remove from wrapper
444 //delete the fields that wont be removed by the GC.
445 if(temp->exitfses!=NULL) RUNFREE(temp->exitfses);
446 if(temp->otds!=NULL) RUNFREE(temp->otds);
450 //remove from wrapper
451 ObjectHashremove(flagptr->objectset, (int)temp);
459 flagptr=flagptr->next;
464 void flagbody(struct ___Object___ *ptr, int flag) {
465 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
468 /*Remove object from all queues */
469 while(flagptr!=NULL) {
470 struct parameterwrapper *next;
471 struct ___Object___ * tag=ptr->___tags___;
473 ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, &FIXME);
474 ObjectHashremove(flagptr->objectset, (int)ptr);
479 struct QueueItem *tmpptr;
480 struct parameterwrapper * parameter=objectqueues[ptr->type];
482 struct parameterwrapper * prevptr=NULL;
483 struct ___Object___ *tagptr=ptr->___tags___;
485 /* Outer loop iterates through all parameter queues an object of
486 this type could be in. */
488 while(parameter!=NULL) {
490 if (parameter->numbertags>0) {
492 goto nextloop;//that means the object has no tag but that param needs tag
493 else if(tagptr->type==TAGTYPE) {//one tag
494 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
495 for(i=0;i<parameter->numbertags;i++) {
496 //slotid is parameter->tagarray[2*i];
497 int tagid=parameter->tagarray[2*i+1];
498 if (tagid!=tagptr->flag)
499 goto nextloop; /*We don't have this tag */
501 } else {//multiple tags
502 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
503 for(i=0;i<parameter->numbertags;i++) {
504 //slotid is parameter->tagarray[2*i];
505 int tagid=parameter->tagarray[2*i+1];
507 for(j=0;j<ao->___cachedCode___;j++) {
508 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
519 for(i=0;i<parameter->numberofterms;i++) {
520 int andmask=parameter->intarray[i*2];
521 int checkmask=parameter->intarray[i*2+1];
522 if ((flag&andmask)==checkmask) {
523 enqueuetasks(parameter, prevptr, ptr);
529 parameter=parameter->next;
531 ptr->flagptr=prevptr;
537 void enqueueoptional(struct ___Object___ * currobj){
539 struct classanalysiswrapper * classwrapper=NULL;
540 struct fsanalysiswrapper * fswrapper=NULL;
542 int numoptionaltaskdescriptors=0;
543 struct optionaltaskdescriptor *** optionaltaskdescriptorarray=NULL;
544 struct fsanalysiswrapper ** goodfswrappersarray=NULL;
545 int numgoodfswrappers=0;
547 if(currobj->numexitfses==0)
548 printf("Handling failed object\nType : %i\nFlag : 0x%x\n", currobj->type, currobj->flag);
550 printf("Handling failed object\nType : %i\n", currobj->type);
552 for(fscount=0; fscount<currobj->numexitfses; fscount++)
553 printf("Flag : 0x%x\n", currobj->exitfses[fscount]);
555 struct ___Object___ * tagptr = currobj->___tags___;
557 if(tagptr->type==TAGTYPE) {
558 printf("Tag : %i\n", tagptr->flag);}
560 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
561 int numbertags = ao->___length___;
562 for(counter=0; counter<numbertags; counter++){
563 printf("Tag : %i\n", ao[counter].flag);
568 /*set the object as failed*/
569 currobj->failedstatus = 1;
571 /*test what optionaltaskdescriptors are available,
572 find the class corresponding*/
574 for(counter = 0; counter<numclasses; counter++){
575 classwrapper = classanalysiswrapperarray[counter];
576 if(classwrapper == NULL){
577 fprintf(stderr, "ERROR : \"struct classanalysiswrapper * classwraper\" is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
580 /*check object type*/
581 if( currobj->type == classwrapper->type)
585 printf("No task will use this parameter as optional\n");
587 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
592 printf("Found the class, search through fses\n");
594 /*search through fses*/
595 goodfswrappersarray = RUNMALLOC(sizeof(struct fsanalysiswrapper *) * classwrapper->numfsanalysiswrappers); /*max number of good fswrappers*/
596 if(goodfswrappersarray == NULL){
597 fprintf(stderr, "ERROR : \"struct fsanalysiswrapper ** goodfswrappersarray\" is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
598 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
601 for(counter = 0; counter<classwrapper->numfsanalysiswrappers; counter++){
602 /*test the FS of the object (include flags and tags)*/
603 fswrapper = classwrapper->fsanalysiswrapperarray[counter];
604 if(fswrapper == NULL){
605 fprintf(stderr, "ERROR : struct fsanalysiswrapper * is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
606 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
610 struct ___Object___ * tagptr = currobj->___tags___;
611 if(fswrapper->numtags>0){
613 goto nextloop;//that means the object has no tag but that param needs tag
614 else if(tagptr->type==TAGTYPE) {//one tag
615 if(fswrapper->numtags!=1) goto nextloop;//we don't have the right number of tags
616 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
617 if (fswrapper->tags[0]!=tagptr->flag)
620 else {//multiple tags
621 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
623 if(ao->___length___!=fswrapper->numtags) goto nextloop;//we don't have the right number of tags
624 for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
625 int tagid=fswrapper->tags[tag_counter];
627 for(j=0;j<ao->___cachedCode___;j++) {
628 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
639 if(currobj->numexitfses==0){/*first time the method is invoqued*/
640 if( currobj->flag == fswrapper->flags){
642 optionaltaskdescriptorarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **) * fswrapper->numoptionaltaskdescriptors);
643 numoptionaltaskdescriptors = fswrapper->numoptionaltaskdescriptors;
644 for(otdc = 0; otdc<fswrapper->numoptionaltaskdescriptors; otdc++){
645 struct optionaltaskdescriptor ** tmpptr = RUNMALLOC(sizeof(struct optionaltaskdescriptor *));
646 tmpptr[0] = fswrapper->optionaltaskdescriptorarray[otdc];
647 optionaltaskdescriptorarray[otdc] = tmpptr;
653 else if(currobj->numexitfses==1){/*one fs exit*/
654 if(currobj->exitfses[0] == fswrapper->flags){
656 optionaltaskdescriptorarray = RUNMALLOC(sizeof(struct optionaltaskdescriptor **) * fswrapper->numoptionaltaskdescriptors);
657 numoptionaltaskdescriptors = fswrapper->numoptionaltaskdescriptors;
658 for(otdc = 0; otdc<fswrapper->numoptionaltaskdescriptors; otdc++){
659 struct optionaltaskdescriptor ** tmpptr = RUNMALLOC(sizeof(struct optionaltaskdescriptor *));
660 tmpptr[0] = fswrapper->optionaltaskdescriptorarray[otdc];
661 optionaltaskdescriptorarray[otdc] = tmpptr;
669 for(fscount=0; fscount<currobj->numexitfses; fscount++){
671 if( currobj->exitfses[fscount] == fswrapper->flags ){/*see if the fswraper correspond to one of the fses*/
672 goodfswrappersarray[numgoodfswrappers]=fswrapper;
676 if(counter==classwrapper->numfsanalysiswrappers-1) goto makeintersection; /*last fswrapper*/
684 printf("FS not found, Nothing more to do\n");
686 removeoptionalfromqueues(currobj->hashcode,currobj, objectqueues[currobj->type]);
690 if(numgoodfswrappers==0 || numgoodfswrappers==1) goto nofs; /*nothing has been found, we expect more than one wrapper for multiple flags*/
691 optionaltaskdescriptorarray = makeintersectionotd(numgoodfswrappers, goodfswrappersarray, &numoptionaltaskdescriptors);
692 if(optionaltaskdescriptorarray==NULL){
693 fprintf(stderr, "ERROR : struct optionaltaskdescriptor ** is a NULL pointer\n, Analysis has been skipped, check Runtime/task.c, function enqueueoptional\n");
700 printf("FS(es) found, intersection created, %i potential tasks :\n", numoptionaltaskdescriptors);
703 /*find the parameterwrapper corresponding to the potential task*/
704 for(counter = 0; counter<numoptionaltaskdescriptors; counter++){
705 struct optionaltaskdescriptor ** tmpptr = optionaltaskdescriptorarray[counter];
706 printf("Task %s\n", tmpptr[0]->task->name);
711 struct parameterwrapper * prevptr = NULL;
712 struct parameterwrapper * flagptr = objectqueues[currobj->type];
713 removeoptionalfromqueues(currobj->hashcode,currobj, flagptr);
714 /*process for each otd*/
715 for(counter = 0; counter<numoptionaltaskdescriptors; counter++){
716 struct parameterwrapper * parameter = objectqueues[currobj->type];
717 struct optionaltaskdescriptor ** tmpptr = optionaltaskdescriptorarray[counter];
718 struct optionaltaskdescriptor * currotd = tmpptr[0];
720 while(parameter->task != currotd->task)
721 parameter=parameter->next;
723 printf("found parameterwrapper for task : %s\n", parameter->task->name);
726 struct ___Object___ * newobj = RUNMALLOC(sizeof(struct ___Object___));
727 (*newobj)=(*currobj);
728 newobj->numotds=numgoodfswrappers;
729 newobj->otds=RUNMALLOC(sizeof(struct optionaltaskdescriptor *) * numgoodfswrappers);
730 for(otd_counter=0; otd_counter<numgoodfswrappers; otd_counter++){
731 newobj->otds[otd_counter]=tmpptr[otd_counter];
733 enqueuetasks(parameter, prevptr, newobj);
743 /*if(currobj->exitfses!=NULL) RUNFREE(currobj->exitfses);
744 if(currobj->otds!=NULL) RUNFREE(currobj->otds);*///there has been a problem just before the program exit, maybe due to the GC ?
745 RUNFREE(optionaltaskdescriptorarray);
749 /*we need to check if the object is optional, in this case, test the predicate*/
750 /*here is the code for predicate checking*/
751 /*The code has not been tested. I don't even know if it is working or efficient but it is a lead...
752 if(currotd->numpredicatemembers == 0){
753 printf("this task can be fired\n");
758 int predicatetrue = 0;
759 for(pred_counter = 0; pred_counter<currotd->numpredicatemembers; pred_counter++){
760 struct predicatemember * currpred = currotd->predicatememberarray[pred_counter];
761 printf("predicate type : %i\n", currpred->type);
763 //test if the predicate member is true
764 struct parameterwrapper * paramwrapper = objectqueues[currpred->type];
765 while(paramwrapper!=NULL){
766 struct ObjectIterator * it = allocateObjectIterator(paramwrapper->objectset->listhead);
768 struct ___Object___ * obj = (struct ___Object___ *)Objkey(it);
769 printf("obj type : %i\n", obj->type);
770 if(obj->type == currpred->type){
772 printf("predicate to test\n");
777 }while(ObjhasNext(it));
778 paramwrapper=paramwrapper->next;
780 printf("not the good predicate");
783 //the predicate members have to be all true
788 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
789 void * taskpointerarray[MAXTASKPARAMS];
791 int numparams=parameter->task->numParameters;
792 int numiterators=parameter->task->numTotal-1;
794 struct taskdescriptor * task=parameter->task;
796 ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, 0);//this add the object to parameterwrapper
798 /* Add enqueued object to parameter vector */
799 taskpointerarray[parameter->slot]=ptr;
801 /* Reset iterators */
802 for(j=0;j<numiterators;j++) {
803 toiReset(¶meter->iterators[j]);
806 /* Find initial state */
807 for(j=0;j<numiterators;j++) {
809 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
810 toiNext(¶meter->iterators[j], taskpointerarray);
812 /* Need to backtrack */
813 toiReset(¶meter->iterators[j]);
817 /* Nothing to enqueue */
824 /* Enqueue current state */
826 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
828 tpd->numParameters=numiterators+1;
829 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
830 for(j=0;j<=numiterators;j++){
833 struct ___Object___ * obj = (struct ___Object___ *)taskpointerarray[j];
834 if(obj->failedstatus==1)
835 printf("parameter %i used as optional for task %s\n", obj->type, task->name);
838 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
841 if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
842 genputtable(activetasks, tpd, tpd);
844 RUNFREE(tpd->parameterArray);
848 /* This loop iterates to the next parameter combination */
852 for(j=numiterators-1; j<numiterators;j++) {
854 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
855 toiNext(¶meter->iterators[j], taskpointerarray);
857 /* Need to backtrack */
858 toiReset(¶meter->iterators[j]);
862 /* Nothing more to enqueue */
869 /* Handler for signals. The signals catch null pointer errors and
870 arithmatic errors. */
872 void myhandler(int sig, siginfo_t *info, void *uap) {
875 printf("sig=%d\n",sig);
878 sigemptyset(&toclear);
879 sigaddset(&toclear, sig);
880 sigprocmask(SIG_UNBLOCK, &toclear,NULL);
881 longjmp(error_handler,1);
886 struct RuntimeHash *fdtoobject;
888 void addreadfd(int fd) {
891 FD_SET(fd, &readfds);
894 void removereadfd(int fd) {
895 FD_CLR(fd, &readfds);
896 if (maxreadfd==(fd+1)) {
898 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
909 void executetasks() {
910 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
912 /* Set up signal handlers */
913 struct sigaction sig;
914 sig.sa_sigaction=&myhandler;
915 sig.sa_flags=SA_SIGINFO;
916 sigemptyset(&sig.sa_mask);
918 /* Catch bus errors, segmentation faults, and floating point exceptions*/
919 sigaction(SIGBUS,&sig,0);
920 sigaction(SIGSEGV,&sig,0);
921 sigaction(SIGFPE,&sig,0);
922 sigaction(SIGPIPE,&sig,0);
927 fdtoobject=allocateRuntimeHash(100);
929 /* Map first block of memory to protected, anonymous page */
930 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
933 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
935 /* Check if any filedescriptors have IO pending */
938 struct timeval timeout={0,0};
942 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
944 /* Process ready fd's */
946 for(fd=0;fd<maxreadfd;fd++) {
947 if (FD_ISSET(fd, &tmpreadfds)) {
948 /* Set ready flag on object */
950 // printf("Setting fd %d\n",fd);
951 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
952 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
959 /* See if there are any active tasks */
960 if (hashsize(activetasks)>0) {
962 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
963 genfreekey(activetasks, currtpd);
965 /* Check if this task has failed, allow a task that contains optional objects to fire */
966 if (gencontains(failedtasks, currtpd)) {
967 // Free up task parameter descriptor
968 RUNFREE(currtpd->parameterArray);
972 int numparams=currtpd->task->numParameters;
973 int numtotal=currtpd->task->numTotal;
975 /* Make sure that the parameters are still in the queues */
976 for(i=0;i<numparams;i++) {
977 void * parameter=currtpd->parameterArray[i];
978 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
979 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
981 /* Check that object is still in queue */
983 struct ___Object___ * obj = (struct ___Object___ *)parameter;
984 if(obj->failedstatus==1){
985 struct ___Object___ *temp=NULL;
986 struct parameterwrapper * ptr;
987 struct ObjectNode * node = pw->objectset->listhead;
989 temp=(struct ___Object___ *)node->key;
990 if(temp->failedstatus==1 && temp->hashcode==obj->hashcode){
992 goto parameterpresent;
996 RUNFREE(currtpd->parameterArray);
1003 if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1004 RUNFREE(currtpd->parameterArray);
1011 /* Check that object still has necessary tags */
1012 for(j=0;j<pd->numbertags;j++) {
1013 int slotid=pd->tagarray[2*j]+numparams;
1014 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1015 if (!containstag(parameter, tagd)) {
1016 RUNFREE(currtpd->parameterArray);
1022 taskpointerarray[i+OFFSET]=parameter;
1025 for(;i<numtotal;i++) {
1026 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1030 /* Checkpoint the state */
1031 forward=allocateRuntimeHash(100);
1032 reverse=allocateRuntimeHash(100);
1033 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1035 if (x=setjmp(error_handler)) {
1039 printf("Fatal Error=%d, Recovering!\n",x);
1041 genputtable(failedtasks,currtpd,currtpd);
1042 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1046 printf("%i object(s) restored\n", currtpd->task->numParameters);
1049 for(counter=0; counter<currtpd->task->numParameters; counter++){
1050 //remove the object from the previous parameterwrapper (maybe not necessary)
1051 //do a new instance of the object. It allows the restored object to be used by other tasks as a non optional arg.
1052 struct ___Object___ * currobj = RUNMALLOC(sizeof(struct ___Object___));
1053 (*currobj)=(*(struct ___Object___ *)currtpd->parameterArray[counter]);
1054 currobj->numexitfses = 0;
1055 currobj->exitfses = NULL;
1057 currobj->hashcode=(int)currobj;
1058 enqueueoptional( currobj );
1061 freeRuntimeHash(forward);
1062 freeRuntimeHash(reverse);
1067 if (injectfailures) {
1068 if ((((double)random())/RAND_MAX)<failurechance) {
1069 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1070 longjmp(error_handler,10);
1073 /* Actually call task */
1075 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
1076 taskpointerarray[1]=NULL;
1079 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1080 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1081 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1083 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1085 freeRuntimeHash(forward);
1086 freeRuntimeHash(reverse);
1088 // Free up task parameter descriptor
1089 RUNFREE(currtpd->parameterArray);
1099 /* This function processes an objects tags */
1100 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1103 for(i=0;i<pd->numbertags;i++) {
1104 int slotid=pd->tagarray[2*i];
1105 int tagid=pd->tagarray[2*i+1];
1107 if (statusarray[slotid+numparams]==0) {
1108 parameter->iterators[*iteratorcount].istag=1;
1109 parameter->iterators[*iteratorcount].tagid=tagid;
1110 parameter->iterators[*iteratorcount].slot=slotid+numparams;
1111 parameter->iterators[*iteratorcount].tagobjectslot=index;
1112 statusarray[slotid+numparams]=1;
1119 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1122 struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1124 parameter->iterators[*iteratorcount].istag=0;
1125 parameter->iterators[*iteratorcount].slot=index;
1126 parameter->iterators[*iteratorcount].objectset=objectset;
1127 statusarray[index]=1;
1129 for(i=0;i<pd->numbertags;i++) {
1130 int slotid=pd->tagarray[2*i];
1131 int tagid=pd->tagarray[2*i+1];
1132 if (statusarray[slotid+numparams]!=0) {
1133 /* This tag has already been enqueued, use it to narrow search */
1134 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1138 parameter->iterators[*iteratorcount].numtags=tagcount;
1143 /* This function builds the iterators for a task & parameter */
1145 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1146 int statusarray[MAXTASKPARAMS];
1148 int numparams=task->numParameters;
1149 int iteratorcount=0;
1150 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1152 statusarray[index]=1; /* Initial parameter */
1153 /* Process tags for initial iterator */
1155 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1159 /* Check for objects with existing tags */
1160 for(i=0;i<numparams;i++) {
1161 if (statusarray[i]==0) {
1162 struct parameterdescriptor *pd=task->descriptorarray[i];
1164 for(j=0;j<pd->numbertags;j++) {
1165 int slotid=pd->tagarray[2*j];
1166 if(statusarray[slotid+numparams]!=0) {
1167 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1168 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1175 /* Next do objects w/ unbound tags*/
1177 for(i=0;i<numparams;i++) {
1178 if (statusarray[i]==0) {
1179 struct parameterdescriptor *pd=task->descriptorarray[i];
1180 if (pd->numbertags>0) {
1181 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1182 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1188 /* Nothing with a tag enqueued */
1190 for(i=0;i<numparams;i++) {
1191 if (statusarray[i]==0) {
1192 struct parameterdescriptor *pd=task->descriptorarray[i];
1193 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1194 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1207 /* This function processes the task information to create queues for
1208 each parameter type. */
1210 void processtasks() {
1212 for(i=0;i<numtasks;i++) {
1213 struct taskdescriptor * task=taskarray[i];
1216 for(j=0;j<task->numParameters;j++) {
1217 struct parameterdescriptor *param=task->descriptorarray[j];
1218 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1219 struct parameterwrapper ** ptr=&objectqueues[param->type];
1221 param->queue=parameter;
1222 parameter->objectset=allocateObjectHash(10);
1223 parameter->numberofterms=param->numberterms;
1224 parameter->intarray=param->intarray;
1225 parameter->numbertags=param->numbertags;
1226 parameter->tagarray=param->tagarray;
1227 parameter->task=task;
1228 /* Link new queue in */
1230 ptr=&((*ptr)->next);
1234 /* Build iterators for parameters */
1235 for(j=0;j<task->numParameters;j++) {
1236 struct parameterdescriptor *param=task->descriptorarray[j];
1237 struct parameterwrapper *parameter=param->queue;
1239 builditerators(task, j, parameter);
1244 void toiReset(struct tagobjectiterator * it) {
1247 } else if (it->numtags>0) {
1250 ObjectHashiterator(it->objectset, &it->it);
1254 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
1257 /* Get object with tags */
1258 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1259 struct ___Object___ *tagptr=obj->___tags___;
1260 if (tagptr->type==TAGTYPE) {
1261 if ((it->tagobjindex==0)&& /* First object */
1262 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1267 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1268 int tagindex=it->tagobjindex;
1269 for(;tagindex<ao->___cachedCode___;tagindex++) {
1270 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1271 if (td->flag==it->tagid) {
1272 it->tagobjindex=tagindex; /* Found right type of tag */
1278 } else if (it->numtags>0) {
1279 /* Use tags to locate appropriate objects */
1280 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1281 struct ___Object___ *objptr=tag->flagptr;
1283 if (objptr->type!=OBJECTARRAYTYPE) {
1284 if (it->tagobjindex>0)
1286 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1288 for(i=1;i<it->numtags;i++) {
1289 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1290 if (!containstag(objptr,tag2))
1295 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1298 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1299 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1300 if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1302 for(i=1;i<it->numtags;i++) {
1303 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1304 if (!containstag(objptr,tag2))
1311 it->tagobjindex=tagindex;
1315 return ObjhasNext(&it->it);
1319 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1321 struct ___Object___ * objptr=tag->flagptr;
1322 if (objptr->type==OBJECTARRAYTYPE) {
1323 struct ArrayObject *ao=(struct ArrayObject *)objptr;
1324 for(j=0;j<ao->___cachedCode___;j++) {
1325 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1333 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
1334 /* hasNext has all of the intelligence */
1337 /* Get object with tags */
1338 struct ___Object___ *obj=objectarray[it->tagobjectslot];
1339 struct ___Object___ *tagptr=obj->___tags___;
1340 if (tagptr->type==TAGTYPE) {
1342 objectarray[it->slot]=tagptr;
1344 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1345 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1347 } else if (it->numtags>0) {
1348 /* Use tags to locate appropriate objects */
1349 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1350 struct ___Object___ *objptr=tag->flagptr;
1351 if (objptr->type!=OBJECTARRAYTYPE) {
1353 objectarray[it->slot]=objptr;
1355 struct ArrayObject *ao=(struct ArrayObject *) objptr;
1356 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1359 /* Iterate object */
1360 objectarray[it->slot]=(void *)Objkey(&it->it);