bug fix in scheduling
[IRC.git] / Robust / src / Runtime / multicoretask.c
1 #ifdef TASK
2 #include "runtime.h"
3 #include "multicoreruntime.h"
4 #include "runtime_arch.h"
5 #include "GenericHashtable.h"
6
7 //  data structures for task invocation
8 struct genhashtable * activetasks;
9 struct taskparamdescriptor * currtpd;
10
11 // specific functions used inside critical sections
12 void enqueueObject_I(void * ptr, 
13                                  struct parameterwrapper ** queues, 
14                                                                                  int length);
15 int enqueuetasks_I(struct parameterwrapper *parameter, 
16                                struct parameterwrapper *prevptr, 
17                                                                          struct ___Object___ *ptr, 
18                                                                          int * enterflags, 
19                                                                          int numenterflags);
20
21 #ifdef MULTICORE_GC
22 inline __attribute__((always_inline)) 
23 void setupsmemmode(void) {
24 #ifdef SMEML
25         bamboo_smem_mode = SMEMLOCAL;
26 #elif defined SMEMF
27         bamboo_smem_mode = SMEMFIXED;
28 #elif defined SMEMM
29         bamboo_smem_mode = SMEMMIXED;
30 #elif defined SMEMG
31         bamboo_smem_mode = SMEMGLOBAL;
32 #else
33         // defaultly using local mode
34         bamboo_smem_mode = SMEMLOCAL;
35 #endif
36 } // void setupsmemmode(void)
37 #endif
38
39 inline __attribute__((always_inline)) 
40 void initruntimedata() {
41         int i;
42         // initialize the arrays
43   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
44     // startup core to initialize corestatus[]
45     for(i = 0; i < NUMCORES; ++i) {
46       corestatus[i] = 1;
47       numsendobjs[i] = 0; 
48       numreceiveobjs[i] = 0;
49 #ifdef PROFILE
50                         // initialize the profile data arrays
51                         profilestatus[i] = 1;
52 #endif
53 #ifdef MULTICORE_GC
54                         gccorestatus[i] = 1;
55                         gcnumsendobjs[i] = 0; 
56       gcnumreceiveobjs[i] = 0;
57                         gcloads[i] = 0;
58                         gcrequiredmems[i] = 0;
59                         gcstopblock[i] = 0;
60                         gcfilledblocks[i] = 0;
61 #endif
62     } // for(i = 0; i < NUMCORES; ++i)
63                 numconfirm = 0;
64                 waitconfirm = false; 
65                 
66                 // TODO for test
67                 total_num_t6 = 0;
68   }
69
70   busystatus = true;
71   self_numsendobjs = 0;
72   self_numreceiveobjs = 0;
73
74   for(i = 0; i < BAMBOO_MSG_BUF_LENGTH; ++i) {
75     msgdata[i] = -1;
76   }
77   msgtype = -1;
78   msgdataindex = 0;
79   msglength = BAMBOO_MSG_BUF_LENGTH;
80   for(i = 0; i < BAMBOO_OUT_BUF_LENGTH; ++i) {
81     outmsgdata[i] = -1;
82   }
83   outmsgindex = 0;
84   outmsglast = 0;
85   outmsgleft = 0;
86   isMsgHanging = false;
87   isMsgSending = false;
88
89   smemflag = true;
90   bamboo_cur_msp = NULL;
91   bamboo_smem_size = 0;
92         totransobjqueue = createQueue();
93
94 #ifdef MULTICORE_GC
95         gcflag = false;
96         gcprocessing = false;
97         gcphase = FINISHPHASE;
98         gccurr_heaptop = 0;
99         gcself_numsendobjs = 0;
100         gcself_numreceiveobjs = 0;
101         gcmarkedptrbound = 0;
102         gcpointertbl = allocateRuntimeHash(20);
103         gcobj2map = 0;
104         gcmappedobj = 0;
105         gcismapped = false;
106         gcnumlobjs = 0;
107         gcheaptop = 0;
108         gctopcore = 0;
109         gctopblock = 0;
110         gcmovestartaddr = 0;
111         gctomove = false;
112         gcmovepending = 0;
113         gcblock2fill = 0;
114         gcsbstarttbl = BAMBOO_BASE_VA;
115         gcsmemtbl = RUNMALLOC_I(sizeof(int)*gcnumblock);
116 #else
117         // create the lock table, lockresult table and obj queue
118   locktable.size = 20;
119   locktable.bucket = 
120                 (struct RuntimeNode **) RUNMALLOC_I(sizeof(struct RuntimeNode *)*20);
121   /* Set allocation blocks*/
122   locktable.listhead=NULL;
123   locktable.listtail=NULL;
124   /*Set data counts*/
125   locktable.numelements = 0;
126   lockobj = 0;
127   lock2require = 0;
128   lockresult = 0;
129   lockflag = false;
130         lockRedirectTbl = allocateRuntimeHash(20);
131   objRedirectLockTbl = allocateRuntimeHash(20);
132 #endif
133 #ifndef INTERRUPT
134   reside = false;
135 #endif  
136   objqueue.head = NULL;
137   objqueue.tail = NULL;
138
139         currtpd = NULL;
140
141 #ifdef PROFILE
142   stall = false;
143   //isInterrupt = true;
144   totalexetime = -1;
145   taskInfoIndex = 0;
146   taskInfoOverflow = false;
147   /*interruptInfoIndex = 0;
148   interruptInfoOverflow = false;*/
149 #endif
150 }
151
152 inline __attribute__((always_inline))
153 void disruntimedata() {
154 #ifdef MULTICORE_GC
155         freeRuntimeHash(gcpointertbl);
156 #else
157         freeRuntimeHash(lockRedirectTbl);
158         freeRuntimeHash(objRedirectLockTbl);
159         RUNFREE(locktable.bucket);
160 #endif
161         genfreehashtable(activetasks);
162         if(currtpd != NULL) {
163                 RUNFREE(currtpd->parameterArray);
164                 RUNFREE(currtpd);
165                 currtpd = NULL;
166         }
167 }
168
169 inline __attribute__((always_inline))
170 bool checkObjQueue() {
171         bool rflag = false;
172         struct transObjInfo * objInfo = NULL;
173         int grount = 0;
174
175 #ifdef PROFILE
176 #ifdef ACCURATEPROFILE
177         bool isChecking = false;
178         if(!isEmpty(&objqueue)) {
179                 profileTaskStart("objqueue checking");
180                 isChecking = true;
181         } // if(!isEmpty(&objqueue))
182 #endif
183 #endif
184
185         while(!isEmpty(&objqueue)) {
186                 void * obj = NULL;
187                 BAMBOO_START_CRITICAL_SECTION_OBJ_QUEUE();
188 #ifdef DEBUG
189                 BAMBOO_DEBUGPRINT(0xf001);
190 #endif
191 #ifdef PROFILE
192                 //isInterrupt = false;
193 #endif 
194 #ifdef DEBUG
195                 BAMBOO_DEBUGPRINT(0xeee1);
196 #endif
197                 rflag = true;
198                 objInfo = (struct transObjInfo *)getItem(&objqueue); 
199                 obj = objInfo->objptr;
200 #ifdef DEBUG
201                 BAMBOO_DEBUGPRINT_REG((int)obj);
202 #endif
203                 // grab lock and flush the obj
204                 grount = 0;
205                 getwritelock_I(obj);
206                 while(!lockflag) {
207                         BAMBOO_WAITING_FOR_LOCK();
208                 } // while(!lockflag)
209                 grount = lockresult;
210 #ifdef DEBUG
211                 BAMBOO_DEBUGPRINT_REG(grount);
212 #endif
213
214                 lockresult = 0;
215                 lockobj = 0;
216                 lock2require = 0;
217                 lockflag = false;
218 #ifndef INTERRUPT
219                 reside = false;
220 #endif
221
222                 if(grount == 1) {
223                         int k = 0;
224                         // flush the object
225 #ifdef CACHEFLUSH
226                         BAMBOO_CACHE_FLUSH_RANGE((int)obj,sizeof(int));
227                         BAMBOO_CACHE_FLUSH_RANGE((int)obj, 
228                                         classsize[((struct ___Object___ *)obj)->type]);
229 #endif
230                         // enqueue the object
231                         for(k = 0; k < objInfo->length; ++k) {
232                                 int taskindex = objInfo->queues[2 * k];
233                                 int paramindex = objInfo->queues[2 * k + 1];
234                                 struct parameterwrapper ** queues = 
235                                         &(paramqueues[BAMBOO_NUM_OF_CORE][taskindex][paramindex]);
236 #ifdef DEBUG
237                                 BAMBOO_DEBUGPRINT_REG(taskindex);
238                                 BAMBOO_DEBUGPRINT_REG(paramindex);
239                                 struct ___Object___ * tmpptr = (struct ___Object___ *)obj;
240                                 tprintf("Process %x(%d): receive obj %x(%lld), ptrflag %x\n", 
241                                                                 BAMBOO_NUM_OF_CORE, BAMBOO_NUM_OF_CORE, (int)obj, 
242                                                                 (long)obj, tmpptr->flag);
243 #endif
244                                 enqueueObject_I(obj, queues, 1);
245 #ifdef DEBUG                             
246                                 BAMBOO_DEBUGPRINT_REG(hashsize(activetasks));
247 #endif
248                         } // for(k = 0; k < objInfo->length; ++k)
249                         releasewritelock_I(obj);
250                         RUNFREE(objInfo->queues);
251                         RUNFREE(objInfo);
252                 } else {
253                         // can not get lock
254                         // put it at the end of the queue if no update version in the queue
255                         struct QueueItem * qitem = getHead(&objqueue);
256                         struct QueueItem * prev = NULL;
257                         while(qitem != NULL) {
258                                 struct transObjInfo * tmpinfo = 
259                                         (struct transObjInfo *)(qitem->objectptr);
260                                 if(tmpinfo->objptr == obj) {
261                                         // the same object in the queue, which should be enqueued
262                                         // recently. Current one is outdate, do not re-enqueue it
263                                         RUNFREE(objInfo->queues);
264                                         RUNFREE(objInfo);
265                                         goto objqueuebreak;
266                                 } else {
267                                         prev = qitem;
268                                 } // if(tmpinfo->objptr == obj)
269                                 qitem = getNextQueueItem(prev);
270                         } // while(qitem != NULL)
271                         // try to execute active tasks already enqueued first
272                         addNewItem_I(&objqueue, objInfo);
273 #ifdef PROFILE
274                         //isInterrupt = true;
275 #endif
276 objqueuebreak:
277                         BAMBOO_CLOSE_CRITICAL_SECTION_OBJ_QUEUE();
278 #ifdef DEBUG
279                         BAMBOO_DEBUGPRINT(0xf000);
280 #endif
281                         break;
282                 } // if(grount == 1)
283                 BAMBOO_CLOSE_CRITICAL_SECTION_OBJ_QUEUE();
284 #ifdef DEBUG
285                 BAMBOO_DEBUGPRINT(0xf000);
286 #endif
287         } // while(!isEmpty(&objqueue))
288
289 #ifdef PROFILE
290 #ifdef ACCURATEPROFILE
291         if(isChecking) {
292                 profileTaskEnd();
293         } // if(isChecking)
294 #endif
295 #endif
296
297 #ifdef DEBUG
298         BAMBOO_DEBUGPRINT(0xee02);
299 #endif
300         return rflag;
301 }
302
303 inline __attribute__((always_inline))
304 void checkCoreStatus() {
305         bool allStall = false;
306         int i = 0;
307         int sumsendobj = 0;
308         if((!waitconfirm) || 
309                         (waitconfirm && (numconfirm == 0))) {
310 #ifdef DEBUG
311                 BAMBOO_DEBUGPRINT(0xee04);
312                 BAMBOO_DEBUGPRINT_REG(waitconfirm);
313 #endif
314                 BAMBOO_START_CRITICAL_SECTION_STATUS();
315 #ifdef DEBUG
316                 BAMBOO_DEBUGPRINT(0xf001);
317 #endif
318                 corestatus[BAMBOO_NUM_OF_CORE] = 0;
319                 numsendobjs[BAMBOO_NUM_OF_CORE] = self_numsendobjs;
320                 numreceiveobjs[BAMBOO_NUM_OF_CORE] = self_numreceiveobjs;
321                 // check the status of all cores
322                 allStall = true;
323 #ifdef DEBUG
324                 BAMBOO_DEBUGPRINT_REG(NUMCORES);
325 #endif
326                 for(i = 0; i < NUMCORES; ++i) {
327 #ifdef DEBUG
328                         BAMBOO_DEBUGPRINT(0xe000 + corestatus[i]);
329 #endif
330                         if(corestatus[i] != 0) {
331                                 allStall = false;
332                                 break;
333                         }
334                 } // for(i = 0; i < NUMCORES; ++i)
335                 if(allStall) {
336                         // check if the sum of send objs and receive obj are the same
337                         // yes->check if the info is the latest; no->go on executing
338                         sumsendobj = 0;
339                         for(i = 0; i < NUMCORES; ++i) {
340                                 sumsendobj += numsendobjs[i];
341 #ifdef DEBUG
342                                 BAMBOO_DEBUGPRINT(0xf000 + numsendobjs[i]);
343 #endif
344                         } // for(i = 0; i < NUMCORES; ++i)      
345                         for(i = 0; i < NUMCORES; ++i) {
346                                 sumsendobj -= numreceiveobjs[i];
347 #ifdef DEBUG
348                                 BAMBOO_DEBUGPRINT(0xf000 + numreceiveobjs[i]);
349 #endif
350                         } // for(i = 0; i < NUMCORES; ++i)
351                         if(0 == sumsendobj) {
352                                 if(!waitconfirm) {
353                                         // the first time found all cores stall
354                                         // send out status confirm msg to all other cores
355                                         // reset the corestatus array too
356 #ifdef DEBUG
357                                         BAMBOO_DEBUGPRINT(0xee05);
358 #endif
359                                         corestatus[BAMBOO_NUM_OF_CORE] = 1;
360                                         for(i = 1; i < NUMCORES; ++i) { 
361                                                 corestatus[i] = 1;
362                                                 // send status confirm msg to core i
363                                                 send_msg_1(i, STATUSCONFIRM);
364                                         } // for(i = 1; i < NUMCORES; ++i)
365                                         waitconfirm = true;
366                                         numconfirm = NUMCORES - 1;
367                                 } else {
368                                         // all the core status info are the latest
369                                         // terminate; for profiling mode, send request to all
370                                         // other cores to pour out profiling data
371 #ifdef DEBUG
372                                         BAMBOO_DEBUGPRINT(0xee06);
373 #endif                                            
374                          
375 #ifdef USEIO
376                                         totalexetime = BAMBOO_GET_EXE_TIME();
377 #else
378                                         BAMBOO_DEBUGPRINT(BAMBOO_GET_EXE_TIME());
379                                         BAMBOO_DEBUGPRINT_REG(total_num_t6); // TODO for test
380                                         BAMBOO_DEBUGPRINT(0xbbbbbbbb);
381 #endif
382                                         // profile mode, send msgs to other cores to request pouring
383                                         // out progiling data
384 #ifdef PROFILE
385                                         BAMBOO_CLOSE_CRITICAL_SECTION_STATUS();
386 #ifdef DEBUG
387                                         BAMBOO_DEBUGPRINT(0xf000);
388 #endif
389                                         for(i = 1; i < NUMCORES; ++i) {
390                                                 // send profile request msg to core i
391                                                 send_msg_2(i, PROFILEOUTPUT, totalexetime);
392                                         } // for(i = 1; i < NUMCORES; ++i)
393                                         // pour profiling data on startup core
394                                         outputProfileData();
395                                         while(true) {
396                                                 BAMBOO_START_CRITICAL_SECTION_STATUS();
397 #ifdef DEBUG
398                                                 BAMBOO_DEBUGPRINT(0xf001);
399 #endif
400                                                 profilestatus[BAMBOO_NUM_OF_CORE] = 0;
401                                                 // check the status of all cores
402                                                 allStall = true;
403 #ifdef DEBUG
404                                                 BAMBOO_DEBUGPRINT_REG(NUMCORES);
405 #endif  
406                                                 for(i = 0; i < NUMCORES; ++i) {
407 #ifdef DEBUG
408                                                         BAMBOO_DEBUGPRINT(0xe000 + profilestatus[i]);
409 #endif
410                                                         if(profilestatus[i] != 0) {
411                                                                 allStall = false;
412                                                                 break;
413                                                         }
414                                                 }  // for(i = 0; i < NUMCORES; ++i)
415                                                 if(!allStall) {
416                                                         int halt = 100;
417                                                         BAMBOO_CLOSE_CRITICAL_SECTION_STATUS();
418 #ifdef DEBUG
419                                                         BAMBOO_DEBUGPRINT(0xf000);
420 #endif
421                                                         while(halt--) {
422                                                         }
423                                                 } else {
424                                                         break;
425                                                 } // if(!allStall)
426                                         } // while(true)
427 #endif
428                                         disruntimedata();
429                                         terminate(); // All done.
430                                 } // if(!waitconfirm)
431                         } else {
432                                 // still some objects on the fly on the network
433                                 // reset the waitconfirm and numconfirm
434 #ifdef DEBUG
435                                         BAMBOO_DEBUGPRINT(0xee07);
436 #endif
437                                 waitconfirm = false;
438                                 numconfirm = 0;
439                         } //  if(0 == sumsendobj)
440                 } else {
441                         // not all cores are stall, keep on waiting
442 #ifdef DEBUG
443                         BAMBOO_DEBUGPRINT(0xee08);
444 #endif
445                         waitconfirm = false;
446                         numconfirm = 0;
447                 } //  if(allStall)
448                 BAMBOO_CLOSE_CRITICAL_SECTION_STATUS();
449 #ifdef DEBUG
450                 BAMBOO_DEBUGPRINT(0xf000);
451 #endif
452         } // if((!waitconfirm) ||
453 }
454
455 // main function for each core
456 inline void run(void * arg) {
457   int i = 0;
458   int argc = 1;
459   char ** argv = NULL;
460   bool sendStall = false;
461   bool isfirst = true;
462   bool tocontinue = false;
463
464   corenum = BAMBOO_GET_NUM_OF_CORE();
465 #ifdef DEBUG
466   BAMBOO_DEBUGPRINT(0xeeee);
467   BAMBOO_DEBUGPRINT_REG(corenum);
468   BAMBOO_DEBUGPRINT(STARTUPCORE);
469 #endif
470
471         // initialize runtime data structures
472         initruntimedata();
473
474   // other architecture related initialization
475   initialization();
476   initCommunication();
477
478   initializeexithandler();
479
480   // main process of the execution module
481   if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
482         // non-executing cores, only processing communications
483     activetasks = NULL;
484 /*#ifdef PROFILE
485         BAMBOO_DEBUGPRINT(0xee01);
486         BAMBOO_DEBUGPRINT_REG(taskInfoIndex);
487         BAMBOO_DEBUGPRINT_REG(taskInfoOverflow);
488                 profileTaskStart("msg handling");
489         }
490  #endif*/
491 #ifdef PROFILE
492     //isInterrupt = false;
493 #endif
494                 fakeExecution();
495   } else {
496           /* Create queue of active tasks */
497           activetasks=
498                         genallocatehashtable((unsigned int(*) (void *)) &hashCodetpd,
499                            (int(*) (void *,void *)) &comparetpd);
500           
501           /* Process task information */
502           processtasks();
503           
504           if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
505                   /* Create startup object */
506                   createstartupobject(argc, argv);
507           }
508
509 #ifdef DEBUG
510           BAMBOO_DEBUGPRINT(0xee00);
511 #endif
512
513           while(true) {
514 #ifdef MULTICORE_GC
515                         // check if need to do GC
516                         gc(NULL);
517 #endif
518
519                   // check if there are new active tasks can be executed
520                   executetasks();
521                         if(busystatus) {
522                                 sendStall = false;
523                         }
524
525 #ifndef INTERRUPT
526                   while(receiveObject() != -1) {
527                   }
528 #endif  
529
530 #ifdef DEBUG
531                   BAMBOO_DEBUGPRINT(0xee01);
532 #endif  
533                   
534                   // check if there are some pending objects, 
535                         // if yes, enqueue them and executetasks again
536                   tocontinue = checkObjQueue();
537
538                   if(!tocontinue) {
539                           // check if stop
540                           if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
541                                   if(isfirst) {
542 #ifdef DEBUG
543                                           BAMBOO_DEBUGPRINT(0xee03);
544 #endif
545                                           isfirst = false;
546                                   }
547                                         checkCoreStatus();
548                           } else {
549                                   if(!sendStall) {
550 #ifdef DEBUG
551                                           BAMBOO_DEBUGPRINT(0xee09);
552 #endif
553 #ifdef PROFILE
554                                           if(!stall) {
555 #endif
556                                                   if(isfirst) {
557                                                           // wait for some time
558                                                           int halt = 10000;
559 #ifdef DEBUG
560                                                           BAMBOO_DEBUGPRINT(0xee0a);
561 #endif
562                                                           while(halt--) {
563                                                           }
564                                                           isfirst = false;
565                                                   } else {
566                                                           // send StallMsg to startup core
567 #ifdef DEBUG
568                                                           BAMBOO_DEBUGPRINT(0xee0b);
569 #endif
570                                                           // send stall msg
571                                                           send_msg_4(STARTUPCORE, TRANSTALL, BAMBOO_NUM_OF_CORE, 
572                                                                                        self_numsendobjs, self_numreceiveobjs);
573                                                           sendStall = true;
574                                                           isfirst = true;
575                                                           busystatus = false;
576                                                   }
577 #ifdef PROFILE
578                                           }
579 #endif
580                                   } else {
581                                           isfirst = true;
582                                           busystatus = false;
583 #ifdef DEBUG
584                                           BAMBOO_DEBUGPRINT(0xee0c);
585 #endif
586                                   } // if(!sendStall)
587                           } // if(STARTUPCORE == BAMBOO_NUM_OF_CORE) 
588                   } // if(!tocontinue)
589           } // while(true) 
590   } // if(BAMBOO_NUM_OF_CORE > NUMCORES - 1)
591
592 } // run()
593
594 struct ___createstartupobject____I_locals {
595   INTPTR size;
596   void * next;
597   struct  ___StartupObject___ * ___startupobject___;
598   struct ArrayObject * ___stringarray___;
599 }; // struct ___createstartupobject____I_locals
600
601 void createstartupobject(int argc, 
602                                      char ** argv) {
603   int i;
604
605   /* Allocate startup object     */
606 #ifdef MULTICORE_GC
607         struct ___createstartupobject____I_locals ___locals___={2, NULL, NULL, NULL};
608   struct ___StartupObject___ *startupobject=
609                 (struct ___StartupObject___*) allocate_new(&___locals___, STARTUPTYPE);
610         ___locals___.___startupobject___ = startupobject;
611   struct ArrayObject * stringarray=
612                 allocate_newarray(&___locals___, STRINGARRAYTYPE, argc-1);
613         ___locals___.___stringarray___ = stringarray;
614 #else
615   struct ___StartupObject___ *startupobject=
616                 (struct ___StartupObject___*) allocate_new(STARTUPTYPE);
617   struct ArrayObject * stringarray=
618                 allocate_newarray(STRINGARRAYTYPE, argc-1);
619 #endif
620   /* Build array of strings */
621   startupobject->___parameters___=stringarray;
622   for(i=1; i<argc; i++) {
623     int length=strlen(argv[i]);
624 #ifdef MULTICORE_GC
625     struct ___String___ *newstring=NewString(&___locals___, argv[i],length);
626 #else
627     struct ___String___ *newstring=NewString(argv[i],length);
628 #endif
629     ((void **)(((char *)&stringarray->___length___)+sizeof(int)))[i-1]=
630                         newstring;
631   }
632
633   startupobject->version = 0;
634   startupobject->lock = NULL;
635
636   /* Set initialized flag for startup object */
637   flagorandinit(startupobject,1,0xFFFFFFFF);
638   enqueueObject(startupobject, NULL, 0);
639 #ifdef CACHEFLUSH
640   BAMBOO_CACHE_FLUSH_ALL();
641 #endif
642 }
643
644 int hashCodetpd(struct taskparamdescriptor *ftd) {
645   int hash=(int)ftd->task;
646   int i;
647   for(i=0; i<ftd->numParameters; i++) {
648     hash^=(int)ftd->parameterArray[i];
649   }
650   return hash;
651 }
652
653 int comparetpd(struct taskparamdescriptor *ftd1, 
654                            struct taskparamdescriptor *ftd2) {
655   int i;
656   if (ftd1->task!=ftd2->task)
657     return 0;
658   for(i=0; i<ftd1->numParameters; i++)
659     if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
660       return 0;
661   return 1;
662 }
663
664 /* This function sets a tag. */
665 #ifdef MULTICORE_GC
666 void tagset(void *ptr, 
667                         struct ___Object___ * obj, 
668                                                 struct ___TagDescriptor___ * tagd) {
669 #else
670 void tagset(struct ___Object___ * obj, 
671                         struct ___TagDescriptor___ * tagd) {
672 #endif
673   struct ArrayObject * ao=NULL;
674   struct ___Object___ * tagptr=obj->___tags___;
675   if (tagptr==NULL) {
676     obj->___tags___=(struct ___Object___ *)tagd;
677   } else {
678     /* Have to check if it is already set */
679     if (tagptr->type==TAGTYPE) {
680       struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
681       if (td==tagd) {
682         return;
683       }
684 #ifdef MULTICORE_GC
685       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
686       struct ArrayObject * ao=
687                                 allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
688       obj=(struct ___Object___ *)ptrarray[2];
689       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
690       td=(struct ___TagDescriptor___ *) obj->___tags___;
691 #else
692       ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
693 #endif
694
695       ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
696       ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
697       obj->___tags___=(struct ___Object___ *) ao;
698       ao->___cachedCode___=2;
699     } else {
700       /* Array Case */
701       int i;
702       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
703       for(i=0; i<ao->___cachedCode___; i++) {
704         struct ___TagDescriptor___ * td=
705                 ARRAYGET(ao, struct ___TagDescriptor___*, i);
706         if (td==tagd) {
707           return;
708         }
709       }
710       if (ao->___cachedCode___<ao->___length___) {
711         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
712         ao->___cachedCode___++;
713       } else {
714 #ifdef MULTICORE_GC
715         int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
716         struct ArrayObject * aonew=
717                 allocate_newarray(&ptrarray,TAGARRAYTYPE,
718                                               TAGARRAYINTERVAL+ao->___length___);
719         obj=(struct ___Object___ *)ptrarray[2];
720         tagd=(struct ___TagDescriptor___ *) ptrarray[3];
721         ao=(struct ArrayObject *)obj->___tags___;
722 #else
723         struct ArrayObject * aonew=
724                 allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
725 #endif
726
727         aonew->___cachedCode___=ao->___length___+1;
728         for(i=0; i<ao->___length___; i++) {
729           ARRAYSET(aonew, struct ___TagDescriptor___*, i, 
730                                      ARRAYGET(ao, struct ___TagDescriptor___*, i));
731         }
732         ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
733       }
734     }
735   }
736
737   {
738     struct ___Object___ * tagset=tagd->flagptr;
739     if(tagset==NULL) {
740       tagd->flagptr=obj;
741     } else if (tagset->type!=OBJECTARRAYTYPE) {
742 #ifdef MULTICORE_GC
743       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
744       struct ArrayObject * ao=
745                                 allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
746       obj=(struct ___Object___ *)ptrarray[2];
747       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
748 #else
749       struct ArrayObject * ao=
750                                 allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
751 #endif
752       ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
753       ARRAYSET(ao, struct ___Object___ *, 1, obj);
754       ao->___cachedCode___=2;
755       tagd->flagptr=(struct ___Object___ *)ao;
756     } else {
757       struct ArrayObject *ao=(struct ArrayObject *) tagset;
758       if (ao->___cachedCode___<ao->___length___) {
759         ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
760       } else {
761         int i;
762 #ifdef MULTICORE_GC
763         int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
764         struct ArrayObject * aonew=
765                 allocate_newarray(&ptrarray,OBJECTARRAYTYPE,
766                                               OBJECTARRAYINTERVAL+ao->___length___);
767         obj=(struct ___Object___ *)ptrarray[2];
768         tagd=(struct ___TagDescriptor___ *)ptrarray[3];
769         ao=(struct ArrayObject *)tagd->flagptr;
770 #else
771         struct ArrayObject * aonew=
772                 allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
773 #endif
774         aonew->___cachedCode___=ao->___cachedCode___+1;
775         for(i=0; i<ao->___length___; i++) {
776           ARRAYSET(aonew, struct ___Object___*, i, 
777                                      ARRAYGET(ao, struct ___Object___*, i));
778         }
779         ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
780         tagd->flagptr=(struct ___Object___ *) aonew;
781       }
782     }
783   }
784 }
785
786 /* This function clears a tag. */
787 #ifdef MULTICORE_GC
788 void tagclear(void *ptr, 
789                           struct ___Object___ * obj, 
790                                                         struct ___TagDescriptor___ * tagd) {
791 #else
792 void tagclear(struct ___Object___ * obj, 
793                           struct ___TagDescriptor___ * tagd) {
794 #endif
795   /* We'll assume that tag is alway there.
796      Need to statically check for this of course. */
797   struct ___Object___ * tagptr=obj->___tags___;
798
799   if (tagptr->type==TAGTYPE) {
800     if ((struct ___TagDescriptor___ *)tagptr==tagd)
801       obj->___tags___=NULL;
802   } else {
803     struct ArrayObject *ao=(struct ArrayObject *) tagptr;
804     int i;
805     for(i=0; i<ao->___cachedCode___; i++) {
806       struct ___TagDescriptor___ * td=
807                                 ARRAYGET(ao, struct ___TagDescriptor___ *, i);
808       if (td==tagd) {
809         ao->___cachedCode___--;
810         if (i<ao->___cachedCode___)
811           ARRAYSET(ao, struct ___TagDescriptor___ *, i, 
812                                 ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
813         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
814         if (ao->___cachedCode___==0)
815           obj->___tags___=NULL;
816         goto PROCESSCLEAR;
817       }
818     }
819   }
820 PROCESSCLEAR:
821   {
822     struct ___Object___ *tagset=tagd->flagptr;
823     if (tagset->type!=OBJECTARRAYTYPE) {
824       if (tagset==obj)
825         tagd->flagptr=NULL;
826     } else {
827       struct ArrayObject *ao=(struct ArrayObject *) tagset;
828       int i;
829       for(i=0; i<ao->___cachedCode___; i++) {
830         struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
831         if (tobj==obj) {
832           ao->___cachedCode___--;
833           if (i<ao->___cachedCode___)
834             ARRAYSET(ao, struct ___Object___ *, i, 
835                                         ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
836           ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
837           if (ao->___cachedCode___==0)
838             tagd->flagptr=NULL;
839           goto ENDCLEAR;
840         }
841       }
842     }
843   }
844 ENDCLEAR:
845   return;
846 }
847
848 /* This function allocates a new tag. */
849 #ifdef MULTICORE_GC
850 struct ___TagDescriptor___ * allocate_tag(void *ptr, 
851                                                       int index) {
852   struct ___TagDescriptor___ * v=
853                 (struct ___TagDescriptor___ *) FREEMALLOC((struct garbagelist *) ptr, 
854                                                                       classsize[TAGTYPE]);
855 #else
856 struct ___TagDescriptor___ * allocate_tag(int index) {
857   struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
858 #endif
859   v->type=TAGTYPE;
860   v->flag=index;
861   return v;
862 }
863
864
865
866 /* This function updates the flag for object ptr.  It or's the flag
867    with the or mask and and's it with the andmask. */
868
869 void flagbody(struct ___Object___ *ptr, 
870                           int flag, 
871                                                         struct parameterwrapper ** queues, 
872                                                         int length, 
873                                                         bool isnew);
874
875 int flagcomp(const int *val1, const int *val2) {
876   return (*val1)-(*val2);
877 }
878
879 void flagorand(void * ptr, 
880                            int ormask, 
881                                                          int andmask, 
882                                                          struct parameterwrapper ** queues, 
883                                                          int length) {
884   {
885     int oldflag=((int *)ptr)[1];
886     int flag=ormask|oldflag;
887     flag&=andmask;
888     flagbody(ptr, flag, queues, length, false);
889   }
890 }
891
892 bool intflagorand(void * ptr, 
893                               int ormask, 
894                                                                         int andmask) {
895   {
896     int oldflag=((int *)ptr)[1];
897     int flag=ormask|oldflag;
898     flag&=andmask;
899     if (flag==oldflag)   /* Don't do anything */
900       return false;
901     else {
902       flagbody(ptr, flag, NULL, 0, false);
903       return true;
904     }
905   }
906 }
907
908 void flagorandinit(void * ptr, 
909                                int ormask, 
910                                                                          int andmask) {
911   int oldflag=((int *)ptr)[1];
912   int flag=ormask|oldflag;
913   flag&=andmask;
914   flagbody(ptr,flag,NULL,0,true);
915 }
916
917 void flagbody(struct ___Object___ *ptr, 
918                           int flag, 
919                                                         struct parameterwrapper ** vqueues, 
920                                                         int vlength, 
921                                                         bool isnew) {
922   struct parameterwrapper * flagptr = NULL;
923   int i = 0;
924   struct parameterwrapper ** queues = vqueues;
925   int length = vlength;
926   int next;
927   int UNUSED, UNUSED2;
928   int * enterflags = NULL;
929   if((!isnew) && (queues == NULL)) {
930     if(BAMBOO_NUM_OF_CORE < NUMCORES) {
931                 queues = objectqueues[BAMBOO_NUM_OF_CORE][ptr->type];
932                 length = numqueues[BAMBOO_NUM_OF_CORE][ptr->type];
933         } else {
934                 return;
935         }
936   }
937   ptr->flag=flag;
938
939   /*Remove object from all queues */
940   for(i = 0; i < length; ++i) {
941     flagptr = queues[i];
942     ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, 
943                                           (int *) &enterflags, &UNUSED, &UNUSED2);
944     ObjectHashremove(flagptr->objectset, (int)ptr);
945     if (enterflags!=NULL)
946       RUNFREE(enterflags);
947   }
948 }
949
950 void enqueueObject(void * vptr, 
951                                struct parameterwrapper ** vqueues, 
952                                                                          int vlength) {
953         struct ___Object___ *ptr = (struct ___Object___ *)vptr;
954         
955         {
956                 //struct QueueItem *tmpptr;
957                 struct parameterwrapper * parameter=NULL;
958                 int j;
959                 int i;
960                 struct parameterwrapper * prevptr=NULL;
961                 struct ___Object___ *tagptr=NULL;
962                 struct parameterwrapper ** queues = vqueues;
963                 int length = vlength;
964                 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
965                         return;
966                 }
967                 if(queues == NULL) {
968                         queues = objectqueues[BAMBOO_NUM_OF_CORE][ptr->type];
969                         length = numqueues[BAMBOO_NUM_OF_CORE][ptr->type];
970                 }
971                 tagptr=ptr->___tags___;
972
973                 /* Outer loop iterates through all parameter queues an object of
974                    this type could be in.  */
975                 for(j = 0; j < length; ++j) {
976                         parameter = queues[j];     
977                         /* Check tags */
978                         if (parameter->numbertags>0) {
979                                 if (tagptr==NULL)
980                                         goto nextloop; //that means the object has no tag 
981                                                  //but that param needs tag
982                                 else if(tagptr->type==TAGTYPE) { //one tag
983                                         //struct ___TagDescriptor___ * tag=
984                                         //(struct ___TagDescriptor___*) tagptr;  
985                                         for(i=0; i<parameter->numbertags; i++) {
986                                                 //slotid is parameter->tagarray[2*i];
987                                                 int tagid=parameter->tagarray[2*i+1];
988                                                 if (tagid!=tagptr->flag)
989                                                         goto nextloop; /*We don't have this tag */
990                                         }
991                                 } else { //multiple tags
992                                         struct ArrayObject * ao=(struct ArrayObject *) tagptr;
993                                         for(i=0; i<parameter->numbertags; i++) {
994                                                 //slotid is parameter->tagarray[2*i];
995                                                 int tagid=parameter->tagarray[2*i+1];
996                                                 int j;
997                                                 for(j=0; j<ao->___cachedCode___; j++) {
998                                                         if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
999                                                                 goto foundtag;
1000                                                 }
1001                                                 goto nextloop;
1002 foundtag:
1003                                                 ;
1004                                         }
1005                                 }
1006                         }
1007         
1008                         /* Check flags */
1009                         for(i=0; i<parameter->numberofterms; i++) {
1010                                 int andmask=parameter->intarray[i*2];
1011                                 int checkmask=parameter->intarray[i*2+1];
1012                                 if ((ptr->flag&andmask)==checkmask) {
1013                                         enqueuetasks(parameter, prevptr, ptr, NULL, 0);
1014                                         prevptr=parameter;
1015                                         break;
1016                                 }
1017                         }
1018 nextloop:
1019                         ;
1020                 }
1021         }
1022 }
1023
1024 void enqueueObject_I(void * vptr, 
1025                                  struct parameterwrapper ** vqueues, 
1026                                                                                  int vlength) {
1027         struct ___Object___ *ptr = (struct ___Object___ *)vptr;
1028         
1029         {
1030                 //struct QueueItem *tmpptr;
1031                 struct parameterwrapper * parameter=NULL;
1032                 int j;
1033                 int i;
1034                 struct parameterwrapper * prevptr=NULL;
1035                 struct ___Object___ *tagptr=NULL;
1036                 struct parameterwrapper ** queues = vqueues;
1037                 int length = vlength;
1038                 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1039                         return;
1040                 }
1041                 if(queues == NULL) {
1042                         queues = objectqueues[BAMBOO_NUM_OF_CORE][ptr->type];
1043                         length = numqueues[BAMBOO_NUM_OF_CORE][ptr->type];
1044                 }
1045                 tagptr=ptr->___tags___;
1046
1047                 /* Outer loop iterates through all parameter queues an object of
1048                    this type could be in.  */
1049                 for(j = 0; j < length; ++j) {
1050                         parameter = queues[j];     
1051                         /* Check tags */
1052                         if (parameter->numbertags>0) {
1053                                 if (tagptr==NULL)
1054                                         goto nextloop; //that means the object has no tag 
1055                                                  //but that param needs tag
1056                                 else if(tagptr->type==TAGTYPE) { //one tag
1057                                         //struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;         
1058                                         for(i=0; i<parameter->numbertags; i++) {
1059                                                 //slotid is parameter->tagarray[2*i];
1060                                                 int tagid=parameter->tagarray[2*i+1];
1061                                                 if (tagid!=tagptr->flag)
1062                                                         goto nextloop; /*We don't have this tag */
1063                                         }
1064                                 } else { //multiple tags
1065                                         struct ArrayObject * ao=(struct ArrayObject *) tagptr;
1066                                         for(i=0; i<parameter->numbertags; i++) {
1067                                                 //slotid is parameter->tagarray[2*i];
1068                                                 int tagid=parameter->tagarray[2*i+1];
1069                                                 int j;
1070                                                 for(j=0; j<ao->___cachedCode___; j++) {
1071                                                         if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
1072                                                                 goto foundtag;
1073                                                 }
1074                                                 goto nextloop;
1075 foundtag:
1076                                                 ;
1077                                         }
1078                                 }
1079                         }
1080
1081                         /* Check flags */
1082                         for(i=0; i<parameter->numberofterms; i++) {
1083                                 int andmask=parameter->intarray[i*2];
1084                                 int checkmask=parameter->intarray[i*2+1];
1085                                 if ((ptr->flag&andmask)==checkmask) {
1086                                         enqueuetasks_I(parameter, prevptr, ptr, NULL, 0);
1087                                         prevptr=parameter;
1088                                         break;
1089                                 }
1090                         }
1091 nextloop:
1092                         ;
1093                 }
1094         }
1095 }
1096
1097
1098 int * getAliasLock(void ** ptrs, 
1099                                int length, 
1100                                                                          struct RuntimeHash * tbl) {
1101         if(length == 0) {
1102                 return (int*)(RUNMALLOC(sizeof(int)));
1103         } else {
1104                 int i = 0;
1105                 int locks[length];
1106                 int locklen = 0;
1107                 bool redirect = false;
1108                 int redirectlock = 0;
1109                 for(; i < length; i++) {
1110                         struct ___Object___ * ptr = (struct ___Object___ *)(ptrs[i]);
1111                         int lock = 0;
1112                         int j = 0;
1113                         if(ptr->lock == NULL) {
1114                                 lock = (int)(ptr);
1115                         } else {
1116                                 lock = (int)(ptr->lock);
1117                         }
1118                         if(redirect) {
1119                                 if(lock != redirectlock) {
1120                                         RuntimeHashadd(tbl, lock, redirectlock);
1121                                 }
1122                         } else {
1123                                 if(RuntimeHashcontainskey(tbl, lock)) {
1124                                         // already redirected
1125                                         redirect = true;
1126                                         RuntimeHashget(tbl, lock, &redirectlock);
1127                                         for(; j < locklen; j++) {
1128                                                 if(locks[j] != redirectlock) {
1129                                                         RuntimeHashadd(tbl, locks[j], redirectlock);
1130                                                 }
1131                                         }
1132                                 } else {
1133                                         bool insert = true;
1134                                         for(j = 0; j < locklen; j++) {
1135                                                 if(locks[j] == lock) {
1136                                                         insert = false;
1137                                                         break;
1138                                                 } else if(locks[j] > lock) {
1139                                                         break;
1140                                                 }
1141                                         }
1142                                         if(insert) {
1143                                                 int h = locklen;
1144                                                 for(; h > j; h--) {
1145                                                         locks[h] = locks[h-1];
1146                                                 }       
1147                                                 locks[j] = lock;
1148                                                 locklen++;
1149                                         }
1150                                 }
1151                         }
1152                 }
1153                 if(redirect) {
1154                         return (int *)redirectlock;
1155                 } else {
1156                         return (int *)(locks[0]);
1157                 }
1158         }
1159 }
1160
1161 void addAliasLock(void * ptr, 
1162                               int lock) {
1163   struct ___Object___ * obj = (struct ___Object___ *)ptr;
1164   if(((int)ptr != lock) && (obj->lock != (int*)lock)) {
1165     // originally no alias lock associated or have a different alias lock
1166     // flush it as the new one
1167     obj->lock = (int *)lock;
1168   }
1169 }
1170
1171 #ifdef PROFILE
1172 inline void setTaskExitIndex(int index) {
1173         taskInfoArray[taskInfoIndex]->exitIndex = index;
1174 }
1175
1176 inline void addNewObjInfo(void * nobj) {
1177         if(taskInfoArray[taskInfoIndex]->newObjs == NULL) {
1178                 taskInfoArray[taskInfoIndex]->newObjs = createQueue();
1179         }
1180         addNewItem(taskInfoArray[taskInfoIndex]->newObjs, nobj);
1181 }
1182 #endif
1183
1184 #ifdef MULTICORE_GC
1185 struct freeMemItem * findFreeMemChunk(int coren,
1186                                                   int isize,
1187                                                   int * tofindb) {
1188         struct freeMemItem * freemem = bamboo_free_mem_list->head;
1189         struct freeMemItem * prev = NULL;
1190         int i = 0;
1191         int j = 0;
1192         *tofindb = gc_core2block[2*coren+i]+124*j;
1193         // check available shared mem chunks
1194         do {
1195                 int foundsmem = 0;
1196                 switch(bamboo_smem_mode) {
1197                         case SMEMLOCAL: {
1198                                 int startb = freemem->startblock;
1199                                 int endb = freemem->endblock;
1200                                 while(startb > *tofindb) {
1201                                         i++;
1202                                         if(2==i) {
1203                                                 i = 0;
1204                                                 j++;
1205                                         }
1206                                         *tofindb = gc_core2block[2*coren+i]+124*j;
1207                                 } // while(startb > tofindb)
1208                                 if(startb <= *tofindb) {
1209                                         if((endb >= *tofindb) && (freemem->size >= isize)) {
1210                                                 foundsmem = 1;
1211                                         } else if(*tofindb > gcnumblock-1) {
1212                                                 // no more local mem
1213                                                 foundsmem = 2;
1214                                         } // if(endb >= tofindb) 
1215                                 } // if(startb <= tofindb)
1216                                 break;
1217                         }
1218
1219                         case SMEMFIXED: {
1220                                 int startb = freemem->startblock;
1221                                 int endb = freemem->endblock;
1222                                 if(startb <= *tofindb) {
1223                                         if((endb >= *tofindb)  && (freemem->size >= isize)) {
1224                                                 foundsmem = 1;
1225                                         } 
1226                                 } else {
1227                                         // use the global mem
1228                                         if(((startb > NUMCORES-1) && (freemem->size >= isize)) || 
1229                                                         ((endb > NUMCORES-1) && ((freemem->size-
1230                                                                 (gcbaseva+BAMBOO_LARGE_SMEM_BOUND-freemem->ptr))>=isize))) {
1231                                                 foundsmem = 1;
1232                                         }
1233                                 }
1234                                 break;
1235                         }
1236
1237                         case SMEMMIXED: {
1238                                 // TODO not supported yet
1239                                 BAMBOO_EXIT(0xe001);
1240                                 break;
1241                         }
1242
1243                         case SMEMGLOBAL: {
1244                     foundsmem = (freemem->size >= isize);
1245                                 break;
1246                         }
1247                         default:
1248                                 break;
1249                 }
1250
1251                 if(1 == foundsmem) {
1252                         // found one
1253                         break;
1254                 } else if (2 == foundsmem) {
1255                         // terminate, no more mem
1256                         freemem = NULL;
1257                         break;
1258                 }
1259                 prev = freemem;
1260                 freemem = freemem->next;
1261         } while(freemem != NULL);
1262
1263         return freemem;
1264 } // struct freeMemItem * findFreeMemChunk(int, int, int *)
1265
1266 void * localmalloc(int tofindb,
1267                                int isize,
1268                                struct freeMemItem * freemem,
1269                                int * allocsize) {
1270         void * mem = NULL;
1271         int startb = freemem->startblock;
1272         int endb = freemem->endblock;
1273         int tmpptr = gcbaseva+((tofindb<NUMCORES)?tofindb*BAMBOO_SMEM_SIZE_L
1274                 :BAMBOO_LARGE_SMEM_BOUND+(tofindb-NUMCORES)*BAMBOO_SMEM_SIZE);
1275         if((freemem->size+freemem->ptr-tmpptr)>=isize) {
1276                 mem = (tmpptr>freemem->ptr)?((void *)tmpptr):(freemem->ptr);
1277         } else {
1278                 mem = (void *)(freemem->size+freemem->ptr-isize);
1279         }
1280         // check the remaining space in this block
1281         int remain = (int)(mem-gcbaseva);
1282         int bound = (BAMBOO_SMEM_SIZE);
1283         if(remain < BAMBOO_LARGE_SMEM_BOUND) {
1284                 bound = (BAMBOO_SMEM_SIZE_L);
1285         }
1286         remain = bound - remain%bound;
1287         if(remain < isize) {
1288                 // this object acrosses blocks
1289                 *allocsize = isize;
1290         } else {
1291                 // round the asigned block to the end of the current block
1292                 *allocsize = remain;
1293         }
1294         if(freemem->ptr == (int)mem) {
1295                 freemem->ptr = ((void*)freemem->ptr) + (*allocsize);
1296                 freemem->size -= *allocsize;
1297                 BLOCKINDEX(freemem->ptr, &(freemem->startblock));
1298         } else if((freemem->ptr+freemem->size) == ((int)mem+(*allocsize))) {
1299                 freemem->size -= *allocsize;
1300                 BLOCKINDEX(((int)mem)-1, &(freemem->endblock));
1301         } else {
1302                 struct freeMemItem * tmp = 
1303                         (struct freeMemItem *)RUNMALLOC(sizeof(struct freeMemItem));
1304                 tmp->ptr = (int)mem+*allocsize;
1305                 tmp->size = freemem->ptr+freemem->size-(int)mem-*allocsize;
1306                 BLOCKINDEX(tmp->ptr, &(tmp->startblock));
1307                 tmp->endblock = freemem->endblock;
1308                 tmp->next = freemem->next;
1309                 freemem->next = tmp;
1310                 freemem->size = (int)mem - freemem->ptr;
1311                 BLOCKINDEX(((int)mem-1), &(freemem->endblock));
1312         }
1313         return mem;
1314 } // void * localmalloc(int, int, struct freeMemItem *, int *)
1315
1316 void * globalmalloc(int isize,
1317                                 struct freeMemItem * freemem,
1318                                 int * allocsize) {
1319         void * mem = (void *)(freemem->ptr);
1320         // check the remaining space in this block
1321         int remain = (int)(mem-(BAMBOO_BASE_VA));
1322         int bound = (BAMBOO_SMEM_SIZE);
1323         if(remain < BAMBOO_LARGE_SMEM_BOUND) {
1324                 bound = (BAMBOO_SMEM_SIZE_L);
1325         }
1326         remain = bound - remain%bound;
1327         if(remain < isize) {
1328                 // this object acrosses blocks
1329                 *allocsize = isize;
1330         } else {
1331                 // round the asigned block to the end of the current block
1332                 *allocsize = remain;
1333         }
1334         freemem->ptr = ((void*)freemem->ptr) + (*allocsize);
1335         freemem->size -= *allocsize;
1336         return mem;
1337 } // void * globalmalloc(int, struct freeMemItem *, int *)
1338 #endif
1339
1340 // malloc from the shared memory
1341 void * smemalloc(int coren,
1342                              int size, 
1343                              int * allocsize) {
1344         void * mem = NULL;
1345 #ifdef MULTICORE_GC
1346         int isize = size+(BAMBOO_CACHE_LINE_SIZE);
1347         int toallocate = (isize>(BAMBOO_SMEM_SIZE)) ? (isize):(BAMBOO_SMEM_SIZE);
1348         // go through free mem list for suitable chunks
1349         int tofindb = 0;
1350         struct freeMemItem * freemem = findFreeMemChunk(coren, isize, &tofindb);
1351
1352         // allocate shared mem if available
1353         if(freemem != NULL) {
1354                 switch(bamboo_smem_mode) {
1355                         case SMEMLOCAL: {
1356                                 mem = localmalloc(tofindb, isize, freemem, allocsize);
1357                                 break;
1358                         }
1359
1360                         case SMEMFIXED: {
1361                                 int startb = freemem->startblock;
1362                                 int endb = freemem->endblock;
1363                                 if(startb > tofindb) {
1364                                         // malloc on global mem
1365                                         mem = globalmalloc(isize, freemem, allocsize);
1366                                 } else {
1367                                         // malloc on local mem
1368                                         mem = localmalloc(tofindb, isize, freemem, allocsize);
1369                                 }
1370                                 break;
1371                         }
1372
1373                         case SMEMMIXED: {
1374                                 // TODO not supported yet
1375                                 BAMBOO_EXIT(0xe002);
1376                                 break;
1377                         }
1378
1379                         case SMEMGLOBAL: {
1380                                 mem = globalmalloc(isize,freemem, allocsize);
1381                                 break;
1382                         }
1383
1384                         default:
1385                                 break;
1386                 }
1387         } else {
1388 #else
1389         int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size):(BAMBOO_SMEM_SIZE);
1390         mem = mspace_calloc(bamboo_free_msp, 1, toallocate);
1391         *allocsize = toallocate;
1392         if(mem == NULL) {
1393 #endif
1394                 // no enough shared global memory
1395                 *allocsize = 0;
1396 #ifdef MULTICORE_GC
1397                 gcflag = true;
1398                 return NULL;
1399 #else
1400                 BAMBOO_DEBUGPRINT(0xa001);
1401                 BAMBOO_EXIT(0xa001);
1402 #endif
1403         }
1404         return mem;
1405 }  // void * smemalloc(int, int, int)
1406
1407 // receive object transferred from other cores
1408 // or the terminate message from other cores
1409 // Should be invoked in critical sections!!
1410 // NOTICE: following format is for threadsimulate version only
1411 //         RAW version please see previous description
1412 // format: type + object
1413 // type: -1--stall msg
1414 //      !-1--object
1415 // return value: 0--received an object
1416 //               1--received nothing
1417 //               2--received a Stall Msg
1418 //               3--received a lock Msg
1419 //               RAW version: -1 -- received nothing
1420 //                            otherwise -- received msg type
1421 int receiveObject() {
1422   int deny = 0;
1423   
1424 msg:
1425   if(receiveMsg() == -1) {
1426           return -1;
1427   }
1428
1429   if(msgdataindex == msglength) {
1430     // received a whole msg
1431     MSGTYPE type; 
1432     type = msgdata[0];
1433     switch(type) {
1434     case TRANSOBJ: {
1435       // receive a object transfer msg
1436       struct transObjInfo * transObj = 
1437                                 RUNMALLOC_I(sizeof(struct transObjInfo));
1438       int k = 0;
1439 #ifdef DEBUG
1440 #ifndef CLOSE_PRINT
1441                         BAMBOO_DEBUGPRINT(0xe880);
1442 #endif
1443 #endif
1444       if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1445 #ifndef CLOSE_PRINT
1446                                 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1447 #endif
1448                                 BAMBOO_EXIT(0xa002);
1449                         } 
1450       // store the object and its corresponding queue info, enqueue it later
1451       transObj->objptr = (void *)msgdata[2]; 
1452       transObj->length = (msglength - 3) / 2;
1453       transObj->queues = RUNMALLOC_I(sizeof(int)*(msglength - 3));
1454       for(k = 0; k < transObj->length; ++k) {
1455                                 transObj->queues[2*k] = msgdata[3+2*k];
1456 #ifdef DEBUG
1457 #ifndef CLOSE_PRINT
1458                                 BAMBOO_DEBUGPRINT_REG(transObj->queues[2*k]);
1459 #endif
1460 #endif
1461                                 transObj->queues[2*k+1] = msgdata[3+2*k+1];
1462 #ifdef DEBUG
1463 #ifndef CLOSE_PRINT
1464                                 BAMBOO_DEBUGPRINT_REG(transObj->queues[2*k+1]);
1465 #endif
1466 #endif
1467                         }
1468       // check if there is an existing duplicate item
1469       {
1470                                 struct QueueItem * qitem = getHead(&objqueue);
1471                                 struct QueueItem * prev = NULL;
1472                                 while(qitem != NULL) {
1473                                         struct transObjInfo * tmpinfo = 
1474                                                 (struct transObjInfo *)(qitem->objectptr);
1475                                         if(tmpinfo->objptr == transObj->objptr) {
1476                                                 // the same object, remove outdate one
1477                                                 removeItem(&objqueue, qitem);
1478                                                 //break;
1479                                         } else {
1480                                                 prev = qitem;
1481                                         }
1482                                         if(prev == NULL) {
1483                                                 qitem = getHead(&objqueue);
1484                                         } else {
1485                                                 qitem = getNextQueueItem(prev);
1486                                         }
1487                                 }
1488                                 addNewItem_I(&objqueue, (void *)transObj);
1489                         }
1490       ++(self_numreceiveobjs);
1491       break;
1492     }
1493
1494     case TRANSTALL: {
1495       // receive a stall msg
1496       if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1497                   // non startup core can not receive stall msg
1498 #ifndef CLOSE_PRINT
1499                                 BAMBOO_DEBUGPRINT_REG(msgdata[1]);
1500 #endif
1501                                 BAMBOO_EXIT(0xa003);
1502       } 
1503       if(msgdata[1] < NUMCORES) {
1504 #ifdef DEBUG
1505 #ifndef CLOSE_PRINT
1506                                 BAMBOO_DEBUGPRINT(0xe881);
1507 #endif
1508 #endif
1509                                 corestatus[msgdata[1]] = 0;
1510                                 numsendobjs[msgdata[1]] = msgdata[2];
1511                                 numreceiveobjs[msgdata[1]] = msgdata[3];
1512       }
1513       break;
1514     }
1515
1516 // GC version have no lock msgs
1517 #ifndef MULTICORE_GC
1518     case LOCKREQUEST: {
1519       // receive lock request msg, handle it right now
1520       // check to see if there is a lock exist for the required obj
1521                         // msgdata[1] -> lock type
1522                         int data2 = msgdata[2]; // obj pointer
1523       int data3 = msgdata[3]; // lock
1524                         int data4 = msgdata[4]; // request core
1525                         // -1: redirected, 0: approved, 1: denied
1526       deny = processlockrequest(msgdata[1], data3, data2, 
1527                                                               data4, data4, true);  
1528                         if(deny == -1) {
1529                                 // this lock request is redirected
1530                                 break;
1531                         } else {
1532                                 // send response msg
1533                                 // for 32 bit machine, the size is always 4 words
1534                                 int tmp = deny==1?LOCKDENY:LOCKGROUNT;
1535                                 if(isMsgSending) {
1536                                         cache_msg_4(data4, tmp, msgdata[1], data2, data3);
1537                                 } else {
1538                                         send_msg_4(data4, tmp, msgdata[1], data2, data3);
1539                                 }
1540                         }
1541       break;
1542     }
1543
1544     case LOCKGROUNT: {
1545       // receive lock grount msg
1546       if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1547 #ifndef CLOSE_PRINT
1548                                 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1549 #endif
1550                                 BAMBOO_EXIT(0xa004);
1551       } 
1552       if((lockobj == msgdata[2]) && (lock2require == msgdata[3])) {
1553 #ifdef DEBUG
1554 #ifndef CLOSE_PRINT
1555                                 BAMBOO_DEBUGPRINT(0xe882);
1556 #endif
1557 #endif
1558                                 lockresult = 1;
1559                                 lockflag = true;
1560 #ifndef INTERRUPT
1561                                 reside = false;
1562 #endif
1563                         } else {
1564                                 // conflicts on lockresults
1565 #ifndef CLOSE_PRINT
1566                                 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1567 #endif
1568                                 BAMBOO_EXIT(0xa005);
1569       }
1570       break;
1571     }
1572
1573     case LOCKDENY: {
1574       // receive lock deny msg
1575       if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1576 #ifndef CLOSE_PRINT
1577                                 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1578 #endif
1579                                 BAMBOO_EXIT(0xa006);
1580       } 
1581       if((lockobj == msgdata[2]) && (lock2require == msgdata[3])) {
1582 #ifdef DEBUG
1583 #ifndef CLOSE_PRINT
1584                                 BAMBOO_DEBUGPRINT(0xe883);
1585 #endif
1586 #endif
1587                                 lockresult = 0;
1588                                 lockflag = true;
1589 #ifndef INTERRUPT
1590                                 reside = false;
1591 #endif
1592                                 } else {
1593                                 // conflicts on lockresults
1594 #ifndef CLOSE_PRINT
1595                                 BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1596 #endif
1597                                 BAMBOO_EXIT(0xa007);
1598       }
1599       break;
1600     }
1601
1602     case LOCKRELEASE: {
1603       // receive lock release msg
1604                         processlockrelease(msgdata[1], msgdata[2], 0, false);
1605       break;
1606     }
1607 #endif
1608
1609 #ifdef PROFILE
1610     case PROFILEOUTPUT: {
1611       // receive an output profile data request msg
1612       if(BAMBOO_NUM_OF_CORE == STARTUPCORE) {
1613                                 // startup core can not receive profile output finish msg
1614                                 BAMBOO_EXIT(0xa008);
1615       }
1616 #ifdef DEBUG
1617 #ifndef CLOSE_PRINT
1618                         BAMBOO_DEBUGPRINT(0xe885);
1619 #endif
1620 #endif
1621                         stall = true;
1622                         totalexetime = msgdata[1];
1623                         outputProfileData();
1624                         if(isMsgSending) {
1625                                 cache_msg_2(STARTUPCORE, PROFILEFINISH, BAMBOO_NUM_OF_CORE);
1626                         } else {
1627                                 send_msg_2(STARTUPCORE, PROFILEFINISH, BAMBOO_NUM_OF_CORE);
1628                         }
1629       break;
1630     }
1631
1632     case PROFILEFINISH: {
1633       // receive a profile output finish msg
1634       if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1635                                 // non startup core can not receive profile output finish msg
1636 #ifndef CLOSE_PRINT
1637                                 BAMBOO_DEBUGPRINT_REG(msgdata[1]);
1638 #endif
1639                                 BAMBOO_EXIT(0xa009);
1640       }
1641 #ifdef DEBUG
1642 #ifndef CLOSE_PRINT
1643                         BAMBOO_DEBUGPRINT(0xe886);
1644 #endif
1645 #endif
1646                         profilestatus[msgdata[1]] = 0;
1647       break;
1648     }
1649 #endif
1650
1651 // GC version has no lock msgs
1652 #ifndef MULTICORE_GC
1653         case REDIRECTLOCK: {
1654           // receive a redirect lock request msg, handle it right now
1655                 // check to see if there is a lock exist for the required obj
1656           int data1 = msgdata[1]; // lock type
1657           int data2 = msgdata[2]; // obj pointer
1658                 int data3 = msgdata[3]; // redirect lock
1659           int data4 = msgdata[4]; // root request core
1660           int data5 = msgdata[5]; // request core
1661           deny = processlockrequest(msgdata[1], data3, data2, data5, data4, true);
1662           if(deny == -1) {
1663                   // this lock request is redirected
1664                   break;
1665           } else {
1666                   // send response msg
1667                   // for 32 bit machine, the size is always 4 words
1668                   if(isMsgSending) {
1669                           cache_msg_4(data4, deny==1?REDIRECTDENY:REDIRECTGROUNT, 
1670                                                         data1, data2, data3);
1671                   } else {
1672                           send_msg_4(data4, deny==1?REDIRECTDENY:REDIRECTGROUNT, 
1673                                                        data1, data2, data3);
1674                   }
1675           }
1676           break;
1677         }
1678
1679         case REDIRECTGROUNT: {
1680                 // receive a lock grant msg with redirect info
1681                 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1682 #ifndef CLOSE_PRINT
1683                         BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1684 #endif
1685                         BAMBOO_EXIT(0xa00a);
1686                 }
1687                 if(lockobj == msgdata[2]) {
1688 #ifdef DEBUG
1689 #ifndef CLOSE_PRINT
1690                   BAMBOO_DEBUGPRINT(0xe891);
1691 #endif
1692 #endif
1693                   lockresult = 1;
1694                   lockflag = true;
1695                   RuntimeHashadd_I(objRedirectLockTbl, lockobj, msgdata[3]);
1696 #ifndef INTERRUPT
1697                   reside = false;
1698 #endif
1699                 } else {
1700                   // conflicts on lockresults
1701 #ifndef CLOSE_PRINT
1702                   BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1703 #endif
1704                   BAMBOO_EXIT(0xa00b);
1705                 }
1706                 break;
1707         }
1708         
1709         case REDIRECTDENY: {
1710           // receive a lock deny msg with redirect info
1711           if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
1712 #ifndef CLOSE_PRINT
1713                   BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1714 #endif
1715                   BAMBOO_EXIT(0xa00c);
1716           }
1717                 if(lockobj == msgdata[2]) {
1718 #ifdef DEBUG
1719 #ifndef CLOSE_PRINT
1720                   BAMBOO_DEBUGPRINT(0xe892);
1721 #endif
1722 #endif
1723                   lockresult = 0;
1724                   lockflag = true;
1725 #ifndef INTERRUPT
1726                   reside = false;
1727 #endif
1728                 } else {
1729                   // conflicts on lockresults
1730 #ifndef CLOSE_PRINT
1731                   BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1732 #endif
1733                   BAMBOO_EXIT(0xa00d);
1734                 }
1735                 break;
1736         }
1737
1738         case REDIRECTRELEASE: {
1739           // receive a lock release msg with redirect info
1740                 processlockrelease(msgdata[1], msgdata[2], msgdata[3], true);
1741                 break;
1742         }
1743 #endif
1744         
1745         case STATUSCONFIRM: {
1746       // receive a status confirm info
1747           if((BAMBOO_NUM_OF_CORE == STARTUPCORE) 
1748                                 || (BAMBOO_NUM_OF_CORE > NUMCORES - 1)) {
1749                   // wrong core to receive such msg
1750                   BAMBOO_EXIT(0xa00e);
1751                 } else {
1752                   // send response msg
1753 #ifdef DEBUG
1754 #ifndef CLOSE_PRINT
1755                   BAMBOO_DEBUGPRINT(0xe887);
1756 #endif
1757 #endif
1758                   if(isMsgSending) {
1759                           cache_msg_5(STARTUPCORE, STATUSREPORT, 
1760                                                         busystatus?1:0, BAMBOO_NUM_OF_CORE,
1761                                                                                 self_numsendobjs, self_numreceiveobjs);
1762                   } else {
1763                           send_msg_5(STARTUPCORE, STATUSREPORT, 
1764                                                        busystatus?1:0, BAMBOO_NUM_OF_CORE,
1765                                                                          self_numsendobjs, self_numreceiveobjs);
1766                   }
1767                 }
1768           break;
1769         }
1770
1771         case STATUSREPORT: {
1772           // receive a status confirm info
1773           if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1774                   // wrong core to receive such msg
1775 #ifndef CLOSE_PRINT
1776                   BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1777 #endif
1778                   BAMBOO_EXIT(0xa00f);
1779                 } else {
1780 #ifdef DEBUG
1781 #ifndef CLOSE_PRINT
1782                   BAMBOO_DEBUGPRINT(0xe888);
1783 #endif
1784 #endif
1785                   if(waitconfirm) {
1786                           numconfirm--;
1787                   }
1788                   corestatus[msgdata[2]] = msgdata[1];
1789                         numsendobjs[msgdata[2]] = msgdata[3];
1790                         numreceiveobjs[msgdata[2]] = msgdata[4];
1791                 }
1792           break;
1793         }
1794
1795         case TERMINATE: {
1796           // receive a terminate msg
1797 #ifdef DEBUG
1798 #ifndef CLOSE_PRINT
1799                 BAMBOO_DEBUGPRINT(0xe889);
1800 #endif
1801 #endif
1802                 disruntimedata();
1803                 BAMBOO_EXIT(0);
1804           break;
1805         }
1806
1807         case MEMREQUEST: {
1808           // receive a shared memory request msg
1809           if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1810                   // wrong core to receive such msg
1811 #ifndef CLOSE_PRINT
1812                   BAMBOO_DEBUGPRINT_REG(msgdata[2]);
1813 #endif
1814                   BAMBOO_EXIT(0xa010);
1815                 } else {
1816 #ifdef DEBUG
1817 #ifndef CLOSE_PRINT
1818                   BAMBOO_DEBUGPRINT(0xe88a);
1819 #endif
1820 #endif
1821 #ifdef MULTICORE_GC
1822                         if(gcprocessing) {
1823                                 // is currently doing gc, dump this msg
1824                                 break;
1825                         }
1826 #endif
1827                         int allocsize = 0;
1828                   void * mem = smemalloc(msgdata[2], msgdata[1], &allocsize);
1829                         if(mem == NULL) {
1830                                 break;
1831                         }
1832                         // send the start_va to request core
1833                         if(isMsgSending) {
1834                                 cache_msg_3(msgdata[2], MEMRESPONSE, mem, allocsize);
1835                         } else {
1836                                 send_msg_3( msgdata[2], MEMRESPONSE, mem, allocsize);
1837                         } 
1838                 }
1839           break;
1840         }
1841
1842         case MEMRESPONSE: {
1843                 // receive a shared memory response msg
1844 #ifdef DEBUG
1845 #ifndef CLOSE_PRINT
1846           BAMBOO_DEBUGPRINT(0xe88b);
1847 #endif
1848 #endif
1849 #ifdef MULTICORE_GC
1850                 if(gcprocessing) {
1851                         // is currently doing gc, dump this msg
1852                         break;
1853                 }
1854 #endif
1855           if(msgdata[2] == 0) {
1856                   bamboo_smem_size = 0;
1857                   bamboo_cur_msp = 0;
1858           } else {
1859 #ifdef MULTICORE_GC
1860                         // fill header to store the size of this mem block
1861                         (*((int*)msgdata[1])) = msgdata[2];
1862                   bamboo_smem_size = msgdata[2] - BAMBOO_CACHE_LINE_SIZE;
1863                         bamboo_cur_msp = msgdata[1] + BAMBOO_CACHE_LINE_SIZE;
1864 #else
1865                   bamboo_smem_size = msgdata[2];
1866                   bamboo_cur_msp =(void*)(msgdata[1]);
1867 #endif
1868           }
1869           smemflag = true;
1870           break;
1871         }
1872
1873 #ifdef MULTICORE_GC
1874         // GC msgs
1875         case GCSTARTINIT: {
1876                 gcflag = true;
1877                 gcphase = INITPHASE;
1878                 if(!smemflag) {
1879                         // is waiting for response of mem request
1880                         // let it return NULL and start gc
1881                         bamboo_smem_size = 0;
1882                         bamboo_cur_msp = NULL;
1883                         smemflag = true;
1884                 }
1885           break;
1886         }
1887
1888         case GCSTART: {
1889                 // receive a start GC msg
1890 #ifdef DEBUG
1891 #ifndef CLOSE_PRINT
1892           BAMBOO_DEBUGPRINT(0xe88c);
1893 #endif
1894 #endif
1895           // set the GC flag
1896                 gcphase = MARKPHASE;
1897           break;
1898         }
1899
1900         case GCSTARTCOMPACT: {
1901                 // a compact phase start msg
1902                 gcblock2fill = msgdata[1];
1903                 gcphase = COMPACTPHASE;
1904                 break;
1905         }
1906
1907         case GCSTARTFLUSH: {
1908                 // received a flush phase start msg
1909                 gcphase = FLUSHPHASE;
1910                 break;
1911         }
1912         
1913         case GCFINISHINIT: {
1914                 // received a init phase finish msg
1915                 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1916                   // non startup core can not receive this msg
1917 #ifndef CLOSE_PRINT
1918                   BAMBOO_DEBUGPRINT_REG(msgdata[1]);
1919 #endif
1920                   BAMBOO_EXIT(0xb001);
1921                 }
1922 #ifdef DEBUG
1923                 BAMBOO_DEBUGPRINT(0xe88c);
1924                 BAMBOO_DEBUGPRINT_REG(msgdata[1]);
1925 #endif
1926                 if(msgdata[1] < NUMCORES) {
1927                         gccorestatus[msgdata[1]] = 0;
1928                 }
1929         }
1930
1931         case GCFINISHMARK: {
1932                 // received a mark phase finish msg
1933                 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1934                   // non startup core can not receive this msg
1935 #ifndef CLOSE_PRINT
1936                   BAMBOO_DEBUGPRINT_REG(msgdata[1]);
1937 #endif
1938                   BAMBOO_EXIT(0xb002);
1939                 } 
1940                 if(msgdata[1] < NUMCORES) {
1941                         gccorestatus[msgdata[1]] = 0;
1942                         gcnumsendobjs[msgdata[1]] = msgdata[2];
1943                         gcnumreceiveobjs[msgdata[1]] = msgdata[3];
1944                 }
1945           break;
1946         }
1947         
1948         case GCFINISHCOMPACT: {
1949                 // received a compact phase finish msg
1950                 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
1951                   // non startup core can not receive this msg
1952                   // return -1
1953 #ifndef CLOSE_PRINT
1954                   BAMBOO_DEBUGPRINT_REG(msgdata[1]);
1955 #endif
1956                   BAMBOO_EXIT(0xb003);
1957                 }
1958                 int cnum = msgdata[1];
1959                 int filledblocks = msgdata[2];
1960                 int heaptop = msgdata[3];
1961                 int data4 = msgdata[4];
1962                 if(cnum < NUMCORES) {
1963                         if(COMPACTPHASE == gcphase) {
1964                                 gcfilledblocks[cnum] = filledblocks;
1965                                 gcloads[cnum] = heaptop;
1966                         }
1967                         if(data4 > 0) {
1968                                 // ask for more mem
1969                                 int startaddr = 0;
1970                                 int tomove = 0;
1971                                 int dstcore = 0;
1972                                 if(gcfindSpareMem_I(&startaddr, &tomove, &dstcore, data4, cnum)) {
1973                                         if(isMsgSending) {
1974                                                 cache_msg_4(cnum, GCMOVESTART, dstcore, startaddr, tomove);
1975                                         } else {
1976                                                 send_msg_4(cnum, GCMOVESTART, dstcore, startaddr, tomove);
1977                                         }
1978                                 }
1979                         } else {
1980                                 gccorestatus[cnum] = 0;
1981                                 // check if there is pending move request
1982                                 /*if(gcmovepending > 0) {
1983                                         int j;
1984                                         for(j = 0; j < NUMCORES; j++) {
1985                                                 if(gcrequiredmems[j]>0) {
1986                                                         break;
1987                                                 }
1988                                         }
1989                                         if(j < NUMCORES) {
1990                                                 // find match
1991                                                 int tomove = 0;
1992                                                 int startaddr = 0;
1993                                                 gcrequiredmems[j] = assignSpareMem_I(cnum, 
1994                                                                                                                                                                                            gcrequiredmems[j], 
1995                                                                                                                                                                                            &tomove, 
1996                                                                                                                                                                                            &startaddr);
1997                                                 if(STARTUPCORE == j) {
1998                                                         gcdstcore = cnum;
1999                                                         gctomove = true;
2000                                                         gcmovestartaddr = startaddr;
2001                                                         gcblock2fill = tomove;
2002                                                 } else {
2003                                                         if(isMsgSending) {
2004                                                                 cache_msg_4(j, GCMOVESTART, cnum, startaddr, tomove);
2005                                                         } else {
2006                                                                 send_msg_4(j, GCMOVESTART, cnum, startaddr, tomove);
2007                                                         }
2008                                                 } // if(STARTUPCORE == j)
2009                                                 if(gcrequiredmems[j] == 0) {
2010                                                         gcmovepending--;
2011                                                 }
2012                                         } // if(j < NUMCORES)
2013                                 } // if(gcmovepending > 0) */
2014                         } // if(data4>0)
2015                 } // if(cnum < NUMCORES)
2016           break;
2017         }
2018
2019         case GCFINISHFLUSH: {
2020                 // received a flush phase finish msg
2021                 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
2022                   // non startup core can not receive this msg
2023                   // return -1
2024 #ifndef CLOSE_PRINT
2025                   BAMBOO_DEBUGPRINT_REG(msgdata[1]);
2026 #endif
2027                   BAMBOO_EXIT(0xb004);
2028                 } 
2029                 if(msgdata[1] < NUMCORES) {
2030                   gccorestatus[msgdata[1]] = 0;
2031                 }
2032           break;
2033         }
2034
2035         case GCFINISH: {
2036                 // received a GC finish msg
2037                 gcphase = FINISHPHASE;
2038                 break;
2039         }
2040
2041         case GCMARKCONFIRM: {
2042                 // received a marked phase finish confirm request msg
2043                 if((BAMBOO_NUM_OF_CORE == STARTUPCORE) 
2044                                 || (BAMBOO_NUM_OF_CORE > NUMCORES - 1)) {
2045                   // wrong core to receive such msg
2046                   BAMBOO_EXIT(0xb005);
2047                 } else {
2048                   // send response msg
2049                   if(isMsgSending) {
2050                           cache_msg_5(STARTUPCORE, GCMARKREPORT, BAMBOO_NUM_OF_CORE, 
2051                                                         gcbusystatus, gcself_numsendobjs, 
2052                                                                                 gcself_numreceiveobjs);
2053                   } else {
2054                           send_msg_5(STARTUPCORE, GCMARKREPORT, BAMBOO_NUM_OF_CORE, 
2055                                                        gcbusystatus, gcself_numsendobjs, gcself_numreceiveobjs);
2056                   }
2057                 }
2058           break;
2059         }
2060
2061         case GCMARKREPORT: {
2062                 // received a marked phase finish confirm response msg
2063                 if(BAMBOO_NUM_OF_CORE != STARTUPCORE) {
2064                   // wrong core to receive such msg
2065 #ifndef CLOSE_PRINT
2066                   BAMBOO_DEBUGPRINT_REG(msgdata[2]);
2067 #endif
2068                   BAMBOO_EXIT(0xb006);
2069                 } else {
2070                   if(waitconfirm) {
2071                           numconfirm--;
2072                   }
2073                   gccorestatus[msgdata[1]] = msgdata[2];
2074                   gcnumsendobjs[msgdata[1]] = msgdata[3];
2075                   gcnumreceiveobjs[msgdata[1]] = msgdata[4];
2076                 }
2077           break;
2078         }
2079
2080         case GCMARKEDOBJ: {
2081                 // received a markedObj msg
2082                 gc_enqueue_I(msgdata[1]);
2083                 gcself_numreceiveobjs++;
2084                 gcbusystatus = true;
2085                 break;
2086         }
2087
2088         case GCMOVESTART: {
2089                 // received a start moving objs msg
2090                 gctomove = true;
2091                 gcdstcore = msgdata[1];
2092                 gcmovestartaddr = msgdata[2];
2093                 gcblock2fill = msgdata[3];
2094                 break;
2095         }
2096         
2097         case GCMAPREQUEST: {
2098                 // received a mapping info request msg
2099                 void * dstptr = NULL;
2100                 RuntimeHashget(gcpointertbl, msgdata[1], &dstptr);
2101                 if(NULL == dstptr) {
2102                         // no such pointer in this core, something is wrong
2103 #ifdef DEBUG
2104                         BAMBOO_DEBUGPRINT_REG(msgdata[1]);
2105                         BAMBOO_DEBUGPRINT_REG(msgdata[2]);
2106 #endif
2107                         BAMBOO_EXIT(0xb007);
2108                 } else {
2109                         // send back the mapping info
2110                         if(isMsgSending) {
2111                                 cache_msg_3(msgdata[2], GCMAPINFO, msgdata[1], (int)dstptr);
2112                         } else {
2113                                 send_msg_3(msgdata[2], GCMAPINFO, msgdata[1], (int)dstptr);
2114                         }
2115                 }
2116                 break;
2117         }
2118
2119         case GCMAPINFO: {
2120                 // received a mapping info response msg
2121                 if(msgdata[1] != gcobj2map) {
2122                         // obj not matched, something is wrong
2123 #ifdef DEBUG
2124                         BAMBOO_DEBUGPRINT_REG(gcobj2map);
2125                         BAMBOO_DEBUGPRINT_REG(msgdata[1]);
2126 #endif
2127                         BAMBOO_EXIT(0xb008);
2128                 } else {
2129                         gcmappedobj = msgdata[2];
2130                         RuntimeHashadd_I(gcpointertbl, gcobj2map, gcmappedobj);
2131                 }
2132                 gcismapped = true;
2133                 break;
2134         }
2135
2136         case GCLOBJREQUEST: {
2137                 // received a large objs info request msg
2138                 transferMarkResults_I();
2139                 break;
2140         }
2141
2142         case GCLOBJINFO: {
2143                 // received a large objs info response msg
2144                 numconfirm--;
2145
2146                 if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
2147 #ifndef CLOSE_PRINT
2148                         BAMBOO_DEBUGPRINT_REG(msgdata[2]);
2149 #endif
2150                         BAMBOO_EXIT(0xb009);
2151                 } 
2152                 // store the mark result info 
2153                 int cnum = msgdata[2];
2154                 gcloads[cnum] = msgdata[3];
2155                 if(gcheaptop < msgdata[4]) {
2156                         gcheaptop = msgdata[4];
2157                 }
2158                 // large obj info here
2159           for(int k = 5; k < msgdata[1];) {
2160                         int lobj = msgdata[k++];
2161                         int length = msgdata[k++];
2162                         gc_lobjenqueue_I(lobj, length, cnum);
2163                         gcnumlobjs++;
2164                 } // for(int k = 5; k < msgdata[1];)
2165                 break;
2166         }
2167         
2168         case GCLOBJMAPPING: {
2169                 // received a large obj mapping info msg
2170                 RuntimeHashadd_I(gcpointertbl, msgdata[1], msgdata[2]);
2171                 break;
2172         }
2173
2174 #endif
2175
2176         default:
2177                 break;
2178         }
2179         for(msgdataindex--; msgdataindex > 0; --msgdataindex) {
2180                 msgdata[msgdataindex] = -1;
2181         }
2182         msgtype = -1;
2183         msglength = 30;
2184 #ifdef DEBUG
2185 #ifndef CLOSE_PRINT
2186         BAMBOO_DEBUGPRINT(0xe88d);
2187 #endif
2188 #endif
2189
2190         if(BAMBOO_MSG_AVAIL() != 0) {
2191                 goto msg;
2192         }
2193 #ifdef PROFILE
2194 /*if(isInterrupt) {
2195                 profileTaskEnd();
2196         }*/
2197 #endif
2198         return (int)type;
2199 } else {
2200         // not a whole msg
2201 #ifdef DEBUG
2202 #ifndef CLOSE_PRINT
2203         BAMBOO_DEBUGPRINT(0xe88e);
2204 #endif
2205 #endif
2206 #ifdef PROFILE
2207 /*  if(isInterrupt) {
2208                   profileTaskEnd();
2209                 }*/
2210 #endif
2211     return -2;
2212   }
2213 }
2214
2215 int enqueuetasks(struct parameterwrapper *parameter, 
2216                              struct parameterwrapper *prevptr, 
2217                                                                  struct ___Object___ *ptr, 
2218                                                                  int * enterflags, 
2219                                                                  int numenterflags) {
2220   void * taskpointerarray[MAXTASKPARAMS];
2221   int j;
2222   //int numparams=parameter->task->numParameters;
2223   int numiterators=parameter->task->numTotal-1;
2224   int retval=1;
2225
2226   struct taskdescriptor * task=parameter->task;
2227
2228    //this add the object to parameterwrapper
2229    ObjectHashadd(parameter->objectset, (int) ptr, 0, (int) enterflags, 
2230                                    numenterflags, enterflags==NULL);
2231
2232   /* Add enqueued object to parameter vector */
2233   taskpointerarray[parameter->slot]=ptr;
2234
2235   /* Reset iterators */
2236   for(j=0; j<numiterators; j++) {
2237     toiReset(&parameter->iterators[j]);
2238   }
2239
2240   /* Find initial state */
2241   for(j=0; j<numiterators; j++) {
2242 backtrackinit:
2243     if(toiHasNext(&parameter->iterators[j],taskpointerarray OPTARG(failed)))
2244       toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
2245     else if (j>0) {
2246       /* Need to backtrack */
2247       toiReset(&parameter->iterators[j]);
2248       j--;
2249       goto backtrackinit;
2250     } else {
2251       /* Nothing to enqueue */
2252       return retval;
2253     }
2254   }
2255
2256   while(1) {
2257     /* Enqueue current state */
2258     //int launch = 0;
2259     struct taskparamdescriptor *tpd=
2260                         RUNMALLOC(sizeof(struct taskparamdescriptor));
2261     tpd->task=task;
2262     tpd->numParameters=numiterators+1;
2263     tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
2264
2265     for(j=0; j<=numiterators; j++) {
2266                         //store the actual parameters
2267       tpd->parameterArray[j]=taskpointerarray[j]; 
2268     }
2269     /* Enqueue task */
2270     if ((/*!gencontains(failedtasks, tpd)&&*/ 
2271                                         !gencontains(activetasks,tpd))) {
2272                 genputtable(activetasks, tpd, tpd);
2273     } else {
2274       RUNFREE(tpd->parameterArray);
2275       RUNFREE(tpd);
2276     }
2277
2278     /* This loop iterates to the next parameter combination */
2279     if (numiterators==0)
2280       return retval;
2281
2282     for(j=numiterators-1; j<numiterators; j++) {
2283 backtrackinc:
2284       if(toiHasNext(&parameter->iterators[j],taskpointerarray OPTARG(failed)))
2285         toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
2286       else if (j>0) {
2287         /* Need to backtrack */
2288         toiReset(&parameter->iterators[j]);
2289         j--;
2290         goto backtrackinc;
2291       } else {
2292         /* Nothing more to enqueue */
2293         return retval;
2294       }
2295     }
2296   }
2297   return retval;
2298 }
2299
2300 int enqueuetasks_I(struct parameterwrapper *parameter, 
2301                                struct parameterwrapper *prevptr, 
2302                                                                          struct ___Object___ *ptr, 
2303                                                                          int * enterflags, 
2304                                                                          int numenterflags) {
2305   void * taskpointerarray[MAXTASKPARAMS];
2306   int j;
2307   //int numparams=parameter->task->numParameters;
2308   int numiterators=parameter->task->numTotal-1;
2309   int retval=1;
2310   //int addnormal=1;
2311   //int adderror=1;
2312
2313   struct taskdescriptor * task=parameter->task;
2314
2315    //this add the object to parameterwrapper
2316    ObjectHashadd_I(parameter->objectset, (int) ptr, 0, (int) enterflags, 
2317                                      numenterflags, enterflags==NULL);  
2318
2319   /* Add enqueued object to parameter vector */
2320   taskpointerarray[parameter->slot]=ptr;
2321
2322   /* Reset iterators */
2323   for(j=0; j<numiterators; j++) {
2324     toiReset(&parameter->iterators[j]);
2325   }
2326
2327   /* Find initial state */
2328   for(j=0; j<numiterators; j++) {
2329 backtrackinit:
2330     if(toiHasNext(&parameter->iterators[j],taskpointerarray OPTARG(failed)))
2331       toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
2332     else if (j>0) {
2333       /* Need to backtrack */
2334       toiReset(&parameter->iterators[j]);
2335       j--;
2336       goto backtrackinit;
2337     } else {
2338       /* Nothing to enqueue */
2339       return retval;
2340     }
2341   }
2342
2343   while(1) {
2344     /* Enqueue current state */
2345     //int launch = 0;
2346     struct taskparamdescriptor *tpd=
2347                         RUNMALLOC_I(sizeof(struct taskparamdescriptor));
2348     tpd->task=task;
2349     tpd->numParameters=numiterators+1;
2350     tpd->parameterArray=RUNMALLOC_I(sizeof(void *)*(numiterators+1));
2351
2352     for(j=0; j<=numiterators; j++) {
2353                         //store the actual parameters
2354       tpd->parameterArray[j]=taskpointerarray[j]; 
2355     }
2356     /* Enqueue task */
2357     if ((/*!gencontains(failedtasks, tpd)&&*/ 
2358                                         !gencontains(activetasks,tpd))) {
2359                 genputtable_I(activetasks, tpd, tpd);
2360     } else {
2361       RUNFREE(tpd->parameterArray);
2362       RUNFREE(tpd);
2363     }
2364
2365     /* This loop iterates to the next parameter combination */
2366     if (numiterators==0)
2367       return retval;
2368
2369     for(j=numiterators-1; j<numiterators; j++) {
2370 backtrackinc:
2371       if(toiHasNext(&parameter->iterators[j], taskpointerarray OPTARG(failed)))
2372         toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
2373       else if (j>0) {
2374         /* Need to backtrack */
2375         toiReset(&parameter->iterators[j]);
2376         j--;
2377         goto backtrackinc;
2378       } else {
2379         /* Nothing more to enqueue */
2380         return retval;
2381       }
2382     }
2383   }
2384   return retval;
2385 }
2386
2387 #ifdef MULTICORE_GC
2388 #define OFFSET 2
2389 #else
2390 #define OFFSET 0
2391 #endif
2392
2393 int containstag(struct ___Object___ *ptr, 
2394                             struct ___TagDescriptor___ *tag);
2395
2396 #ifndef MULTICORE_GC
2397 void releasewritelock_r(void * lock, void * redirectlock) {
2398   int targetcore = 0;
2399   int reallock = (int)lock;
2400   targetcore = (reallock >> 5) % BAMBOO_TOTALCORE;
2401
2402 #ifdef DEBUG
2403   BAMBOO_DEBUGPRINT(0xe671);
2404   BAMBOO_DEBUGPRINT_REG((int)lock);
2405   BAMBOO_DEBUGPRINT_REG(reallock);
2406   BAMBOO_DEBUGPRINT_REG(targetcore);
2407 #endif
2408
2409   if(targetcore == BAMBOO_NUM_OF_CORE) {
2410         BAMBOO_START_CRITICAL_SECTION_LOCK();
2411 #ifdef DEBUG
2412         BAMBOO_DEBUGPRINT(0xf001);
2413 #endif
2414     // reside on this core
2415     if(!RuntimeHashcontainskey(locktbl, reallock)) {
2416       // no locks for this object, something is wrong
2417       BAMBOO_EXIT(0xa011);
2418     } else {
2419       int rwlock_obj = 0;
2420           struct LockValue * lockvalue = NULL;
2421 #ifdef DEBUG
2422       BAMBOO_DEBUGPRINT(0xe672);
2423 #endif
2424       RuntimeHashget(locktbl, reallock, &rwlock_obj);
2425           lockvalue = (struct LockValue *)rwlock_obj;
2426 #ifdef DEBUG
2427       BAMBOO_DEBUGPRINT_REG(lockvalue->value);
2428 #endif
2429       lockvalue->value++;
2430           lockvalue->redirectlock = (int)redirectlock;
2431 #ifdef DEBUG
2432       BAMBOO_DEBUGPRINT_REG(lockvalue->value);
2433 #endif
2434     }
2435         BAMBOO_CLOSE_CRITICAL_SECTION_LOCK();
2436 #ifdef DEBUG
2437         BAMBOO_DEBUGPRINT(0xf000);
2438 #endif
2439     return;
2440   } else {
2441           // send lock release with redirect info msg
2442           // for 32 bit machine, the size is always 4 words
2443           send_msg_4(targetcore, REDIRECTRELEASE, 1, (int)lock, (int)redirectlock);
2444   }
2445 }
2446 #endif
2447
2448 void executetasks() {
2449   void * taskpointerarray[MAXTASKPARAMS+OFFSET];
2450   int numparams=0;
2451   int numtotal=0;
2452   struct ___Object___ * tmpparam = NULL;
2453   struct parameterdescriptor * pd=NULL;
2454   struct parameterwrapper *pw=NULL;
2455   int j = 0;
2456   int x = 0;
2457   bool islock = true;
2458
2459   struct LockValue locks[MAXTASKPARAMS];
2460   int locklen = 0;
2461   int grount = 0;
2462   int andmask=0;
2463   int checkmask=0;
2464
2465 newtask:
2466   while(hashsize(activetasks)>0) {
2467 #ifdef MULTICORE_GC
2468                 gc(NULL);
2469 #endif
2470 #ifdef DEBUG
2471     BAMBOO_DEBUGPRINT(0xe990);
2472 #endif
2473
2474     /* See if there are any active tasks */
2475     if (hashsize(activetasks)>0) {
2476       int i;
2477 #ifdef PROFILE
2478 #ifdef ACCURATEPROFILE
2479           profileTaskStart("tpd checking");
2480 #endif
2481 #endif
2482           busystatus = true;
2483                 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
2484                 genfreekey(activetasks, currtpd);
2485
2486                 numparams=currtpd->task->numParameters;
2487                 numtotal=currtpd->task->numTotal;
2488
2489           // clear the lockRedirectTbl 
2490                 // (TODO, this table should be empty after all locks are released)
2491           // reset all locks
2492           for(j = 0; j < MAXTASKPARAMS; j++) {
2493                   locks[j].redirectlock = 0;
2494                   locks[j].value = 0;
2495           }
2496           // get all required locks
2497           locklen = 0;
2498           // check which locks are needed
2499           for(i = 0; i < numparams; i++) {
2500                   void * param = currtpd->parameterArray[i];
2501                   int tmplock = 0;
2502                   int j = 0;
2503                   bool insert = true;
2504                   if(((struct ___Object___ *)param)->type == STARTUPTYPE) {
2505                           islock = false;
2506                           taskpointerarray[i+OFFSET]=param;
2507                           goto execute;
2508                   }
2509                   if(((struct ___Object___ *)param)->lock == NULL) {
2510                           tmplock = (int)param;
2511                   } else {
2512                           tmplock = (int)(((struct ___Object___ *)param)->lock);
2513                   }
2514                   // insert into the locks array
2515                   for(j = 0; j < locklen; j++) {
2516                           if(locks[j].value == tmplock) {
2517                                   insert = false;
2518                                   break;
2519                           } else if(locks[j].value > tmplock) {
2520                                   break;
2521                           }
2522                   }
2523                   if(insert) {
2524                           int h = locklen;
2525                           for(; h > j; h--) {
2526                                   locks[h].redirectlock = locks[h-1].redirectlock;
2527                                   locks[h].value = locks[h-1].value;
2528                           }
2529                           locks[j].value = tmplock;
2530                           locks[j].redirectlock = (int)param;
2531                           locklen++;
2532                   }               
2533           } // line 2713: for(i = 0; i < numparams; i++) 
2534           // grab these required locks
2535 #ifdef DEBUG
2536           BAMBOO_DEBUGPRINT(0xe991);
2537 #endif
2538           for(i = 0; i < locklen; i++) {
2539                   int * lock = (int *)(locks[i].redirectlock);
2540                   islock = true;
2541                   // require locks for this parameter if it is not a startup object
2542 #ifdef DEBUG
2543                   BAMBOO_DEBUGPRINT_REG((int)lock);
2544                   BAMBOO_DEBUGPRINT_REG((int)(locks[i].value));
2545 #endif
2546                   getwritelock(lock);
2547                   BAMBOO_START_CRITICAL_SECTION();
2548 #ifdef DEBUG
2549                   BAMBOO_DEBUGPRINT(0xf001);
2550 #endif
2551 #ifdef PROFILE
2552                   //isInterrupt = false;
2553 #endif 
2554                   while(!lockflag) { 
2555                           BAMBOO_WAITING_FOR_LOCK();
2556                   }
2557 #ifndef INTERRUPT
2558                   if(reside) {
2559                           while(BAMBOO_WAITING_FOR_LOCK() != -1) {
2560                           }
2561                   }
2562 #endif
2563                   grount = lockresult;
2564
2565                   lockresult = 0;
2566                   lockobj = 0;
2567                   lock2require = 0;
2568                   lockflag = false;
2569 #ifndef INTERRUPT
2570                   reside = false;
2571 #endif
2572 #ifdef PROFILE
2573                   //isInterrupt = true;
2574 #endif
2575                   BAMBOO_CLOSE_CRITICAL_SECTION();
2576 #ifdef DEBUG
2577                   BAMBOO_DEBUGPRINT(0xf000);
2578 #endif
2579
2580                   if(grount == 0) {
2581                           int j = 0;
2582 #ifdef DEBUG
2583                           BAMBOO_DEBUGPRINT(0xe992);
2584 #endif
2585                           // can not get the lock, try later
2586                           // releas all grabbed locks for previous parameters
2587                           for(j = 0; j < i; ++j) {
2588                                   lock = (int*)(locks[j].redirectlock);
2589                                   releasewritelock(lock);
2590                           }
2591                           genputtable(activetasks, currtpd, currtpd);
2592                           if(hashsize(activetasks) == 1) {
2593                                   // only one task right now, wait a little while before next try
2594                                   int halt = 10000;
2595                                   while(halt--) {
2596                                   }
2597                           }
2598 #ifdef PROFILE
2599 #ifdef ACCURATEPROFILE
2600                           // fail, set the end of the checkTaskInfo
2601                           profileTaskEnd();
2602 #endif
2603 #endif
2604                           goto newtask;
2605                   } // line 2794: if(grount == 0)
2606           } // line 2752:  for(i = 0; i < locklen; i++)
2607
2608 #ifdef DEBUG
2609         BAMBOO_DEBUGPRINT(0xe993);
2610 #endif
2611       /* Make sure that the parameters are still in the queues */
2612       for(i=0; i<numparams; i++) {
2613         void * parameter=currtpd->parameterArray[i];
2614
2615         // flush the object
2616 #ifdef CACHEFLUSH
2617         BAMBOO_CACHE_FLUSH_RANGE((int)parameter, 
2618                         classsize[((struct ___Object___ *)parameter)->type]);
2619 #endif
2620         tmpparam = (struct ___Object___ *)parameter;
2621         pd=currtpd->task->descriptorarray[i];
2622         pw=(struct parameterwrapper *) pd->queue;
2623         /* Check that object is still in queue */
2624         {
2625           if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
2626 #ifdef DEBUG
2627             BAMBOO_DEBUGPRINT(0xe994);
2628 #endif
2629             // release grabbed locks
2630             for(j = 0; j < locklen; ++j) {
2631                 int * lock = (int *)(locks[j].redirectlock);
2632                 releasewritelock(lock);
2633             }
2634             RUNFREE(currtpd->parameterArray);
2635             RUNFREE(currtpd);
2636                         currtpd = NULL;
2637             goto newtask;
2638           }
2639         } // line2865
2640         /* Check if the object's flags still meets requirements */
2641         {
2642           int tmpi = 0;
2643           bool ismet = false;
2644           for(tmpi = 0; tmpi < pw->numberofterms; ++tmpi) {
2645             andmask=pw->intarray[tmpi*2];
2646             checkmask=pw->intarray[tmpi*2+1];
2647             if((((struct ___Object___ *)parameter)->flag&andmask)==checkmask) {
2648               ismet = true;
2649               break;
2650             }
2651           }
2652           if (!ismet) {
2653             // flags are never suitable
2654             // remove this obj from the queue
2655             int next;
2656             int UNUSED, UNUSED2;
2657             int * enterflags;
2658 #ifdef DEBUG
2659             BAMBOO_DEBUGPRINT(0xe995);
2660 #endif
2661             ObjectHashget(pw->objectset, (int) parameter, (int *) &next, 
2662                                                   (int *) &enterflags, &UNUSED, &UNUSED2);
2663             ObjectHashremove(pw->objectset, (int)parameter);
2664             if (enterflags!=NULL)
2665               RUNFREE(enterflags);
2666             // release grabbed locks
2667             for(j = 0; j < locklen; ++j) {
2668                  int * lock = (int *)(locks[j].redirectlock);
2669                 releasewritelock(lock);
2670             }
2671             RUNFREE(currtpd->parameterArray);
2672             RUNFREE(currtpd);
2673                         currtpd = NULL;
2674 #ifdef PROFILE
2675 #ifdef ACCURATEPROFILE
2676             // fail, set the end of the checkTaskInfo
2677                 profileTaskEnd();
2678 #endif
2679 #endif
2680             goto newtask;
2681           } // line 2878: if (!ismet)
2682         } // line 2867
2683 parameterpresent:
2684         ;
2685         /* Check that object still has necessary tags */
2686         for(j=0; j<pd->numbertags; j++) {
2687           int slotid=pd->tagarray[2*j]+numparams;
2688           struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
2689           if (!containstag(parameter, tagd)) {
2690 #ifdef DEBUG
2691             BAMBOO_DEBUGPRINT(0xe996);
2692 #endif
2693                 {
2694                 // release grabbed locks
2695                 int tmpj = 0;
2696             for(tmpj = 0; tmpj < locklen; ++tmpj) {
2697                  int * lock = (int *)(locks[tmpj].redirectlock);
2698                 releasewritelock(lock);
2699             }
2700                 }
2701             RUNFREE(currtpd->parameterArray);
2702             RUNFREE(currtpd);
2703                         currtpd = NULL;
2704             goto newtask;
2705           } // line2911: if (!containstag(parameter, tagd))
2706         } // line 2808: for(j=0; j<pd->numbertags; j++)
2707
2708         taskpointerarray[i+OFFSET]=parameter;
2709       } // line 2824: for(i=0; i<numparams; i++)
2710       /* Copy the tags */
2711       for(; i<numtotal; i++) {
2712         taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
2713       }
2714
2715       {
2716 execute:
2717           /* Actually call task */
2718 #ifdef MULTICORE_GC
2719           ((int *)taskpointerarray)[0]=currtpd->numParameters;
2720           taskpointerarray[1]=NULL;
2721 #endif
2722 #ifdef PROFILE
2723 #ifdef ACCURATEPROFILE
2724           // check finish, set the end of the checkTaskInfo
2725           profileTaskEnd();
2726 #endif
2727           profileTaskStart(currtpd->task->name);
2728 #endif
2729
2730 #ifdef DEBUG
2731                 BAMBOO_DEBUGPRINT(0xe997);
2732 #endif
2733                 ((void(*) (void **))currtpd->task->taskptr)(taskpointerarray);
2734 #ifdef PROFILE
2735 #ifdef ACCURATEPROFILE
2736           // task finish, set the end of the checkTaskInfo
2737           profileTaskEnd();
2738           // new a PostTaskInfo for the post-task execution
2739           profileTaskStart("post task execution");
2740 #endif
2741 #endif
2742 #ifdef DEBUG
2743           BAMBOO_DEBUGPRINT(0xe998);
2744           BAMBOO_DEBUGPRINT_REG(islock);
2745 #endif
2746
2747           if(islock) {
2748 #ifdef DEBUG
2749                   BAMBOO_DEBUGPRINT(0xe999);
2750 #endif
2751             for(i = 0; i < locklen; ++i) {
2752                                 void * ptr = (void *)(locks[i].redirectlock);
2753               int * lock = (int *)(locks[i].value);
2754 #ifdef DEBUG
2755                   BAMBOO_DEBUGPRINT_REG((int)ptr);
2756                   BAMBOO_DEBUGPRINT_REG((int)lock);
2757 #endif
2758 #ifndef MULTICORE_GC
2759                   if(RuntimeHashcontainskey(lockRedirectTbl, (int)lock)) {
2760                           int redirectlock;
2761                           RuntimeHashget(lockRedirectTbl, (int)lock, &redirectlock);
2762                           RuntimeHashremovekey(lockRedirectTbl, (int)lock);
2763                           releasewritelock_r(lock, (int *)redirectlock);
2764                   } else {
2765 #else
2766                                 {
2767 #endif
2768                 releasewritelock(ptr);
2769                   }
2770             }
2771           } // line 3015: if(islock)
2772
2773 #ifdef PROFILE
2774           // post task execution finish, set the end of the postTaskInfo
2775           profileTaskEnd();
2776 #endif
2777
2778           // Free up task parameter descriptor
2779           RUNFREE(currtpd->parameterArray);
2780           RUNFREE(currtpd);
2781                 currtpd = NULL;
2782 #ifdef DEBUG
2783           BAMBOO_DEBUGPRINT(0xe99a);
2784 #endif
2785       } //  
2786     } //  if (hashsize(activetasks)>0)  
2787   } //  while(hashsize(activetasks)>0)
2788 #ifdef DEBUG
2789   BAMBOO_DEBUGPRINT(0xe99b);
2790 #endif
2791 }
2792
2793 /* This function processes an objects tags */
2794 void processtags(struct parameterdescriptor *pd, 
2795                              int index, 
2796                                                                  struct parameterwrapper *parameter, 
2797                                                                  int * iteratorcount, 
2798                                                                  int *statusarray, 
2799                                                                  int numparams) {
2800   int i;
2801
2802   for(i=0; i<pd->numbertags; i++) {
2803     int slotid=pd->tagarray[2*i];
2804     int tagid=pd->tagarray[2*i+1];
2805
2806     if (statusarray[slotid+numparams]==0) {
2807       parameter->iterators[*iteratorcount].istag=1;
2808       parameter->iterators[*iteratorcount].tagid=tagid;
2809       parameter->iterators[*iteratorcount].slot=slotid+numparams;
2810       parameter->iterators[*iteratorcount].tagobjectslot=index;
2811       statusarray[slotid+numparams]=1;
2812       (*iteratorcount)++;
2813     }
2814   }
2815 }
2816
2817
2818 void processobject(struct parameterwrapper *parameter, 
2819                                int index, 
2820                                                                          struct parameterdescriptor *pd, 
2821                                                                          int *iteratorcount, 
2822                                                                          int * statusarray, 
2823                                                                          int numparams) {
2824   int i;
2825   int tagcount=0;
2826   struct ObjectHash * objectset=
2827                 ((struct parameterwrapper *)pd->queue)->objectset;
2828
2829   parameter->iterators[*iteratorcount].istag=0;
2830   parameter->iterators[*iteratorcount].slot=index;
2831   parameter->iterators[*iteratorcount].objectset=objectset;
2832   statusarray[index]=1;
2833
2834   for(i=0; i<pd->numbertags; i++) {
2835     int slotid=pd->tagarray[2*i];
2836     //int tagid=pd->tagarray[2*i+1];
2837     if (statusarray[slotid+numparams]!=0) {
2838       /* This tag has already been enqueued, use it to narrow search */
2839       parameter->iterators[*iteratorcount].tagbindings[tagcount]=
2840                                 slotid+numparams;
2841       tagcount++;
2842     }
2843   }
2844   parameter->iterators[*iteratorcount].numtags=tagcount;
2845
2846   (*iteratorcount)++;
2847 }
2848
2849 /* This function builds the iterators for a task & parameter */
2850
2851 void builditerators(struct taskdescriptor * task, 
2852                                 int index, 
2853                                                                                 struct parameterwrapper * parameter) {
2854   int statusarray[MAXTASKPARAMS];
2855   int i;
2856   int numparams=task->numParameters;
2857   int iteratorcount=0;
2858   for(i=0; i<MAXTASKPARAMS; i++) statusarray[i]=0;
2859
2860   statusarray[index]=1; /* Initial parameter */
2861   /* Process tags for initial iterator */
2862
2863   processtags(task->descriptorarray[index], index, parameter, 
2864                                 &iteratorcount, statusarray, numparams);
2865
2866   while(1) {
2867 loopstart:
2868     /* Check for objects with existing tags */
2869     for(i=0; i<numparams; i++) {
2870       if (statusarray[i]==0) {
2871         struct parameterdescriptor *pd=task->descriptorarray[i];
2872         int j;
2873         for(j=0; j<pd->numbertags; j++) {
2874           int slotid=pd->tagarray[2*j];
2875           if(statusarray[slotid+numparams]!=0) {
2876             processobject(parameter, i, pd, &iteratorcount, statusarray, 
2877                                                   numparams);
2878             processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
2879             goto loopstart;
2880           }
2881         }
2882       }
2883     }
2884
2885     /* Next do objects w/ unbound tags*/
2886
2887     for(i=0; i<numparams; i++) {
2888       if (statusarray[i]==0) {
2889         struct parameterdescriptor *pd=task->descriptorarray[i];
2890         if (pd->numbertags>0) {
2891           processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
2892           processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
2893           goto loopstart;
2894         }
2895       }
2896     }
2897
2898     /* Nothing with a tag enqueued */
2899
2900     for(i=0; i<numparams; i++) {
2901       if (statusarray[i]==0) {
2902         struct parameterdescriptor *pd=task->descriptorarray[i];
2903         processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
2904         processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
2905         goto loopstart;
2906       }
2907     }
2908
2909     /* Nothing left */
2910     return;
2911   }
2912 }
2913
2914 void printdebug() {
2915   int i;
2916   int j;
2917   if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
2918     return;
2919   }
2920   for(i=0; i<numtasks[BAMBOO_NUM_OF_CORE]; i++) {
2921     struct taskdescriptor * task=taskarray[BAMBOO_NUM_OF_CORE][i];
2922 #ifndef RAW 
2923         printf("%s\n", task->name);
2924 #endif
2925     for(j=0; j<task->numParameters; j++) {
2926       struct parameterdescriptor *param=task->descriptorarray[j];
2927       struct parameterwrapper *parameter=param->queue;
2928       struct ObjectHash * set=parameter->objectset;
2929       struct ObjectIterator objit;
2930 #ifndef RAW
2931           printf("  Parameter %d\n", j);
2932 #endif
2933       ObjectHashiterator(set, &objit);
2934       while(ObjhasNext(&objit)) {
2935         struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
2936         struct ___Object___ * tagptr=obj->___tags___;
2937         int nonfailed=Objdata4(&objit);
2938         int numflags=Objdata3(&objit);
2939         int flags=Objdata2(&objit);
2940         Objnext(&objit);
2941 #ifndef RAW
2942         printf("    Contains %lx\n", obj);
2943         printf("      flag=%d\n", obj->flag);
2944 #endif
2945         if (tagptr==NULL) {
2946         } else if (tagptr->type==TAGTYPE) {
2947 #ifndef RAW
2948           printf("      tag=%lx\n",tagptr);
2949 #else
2950           ;
2951 #endif
2952         } else {
2953           int tagindex=0;
2954           struct ArrayObject *ao=(struct ArrayObject *)tagptr;
2955           for(; tagindex<ao->___cachedCode___; tagindex++) {
2956 #ifndef RAW
2957                   printf("      tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, 
2958                                                  tagindex));
2959 #else
2960                   ;
2961 #endif
2962           }
2963         }
2964       }
2965     }
2966   }
2967 }
2968
2969
2970 /* This function processes the task information to create queues for
2971    each parameter type. */
2972
2973 void processtasks() {
2974   int i;
2975   if(BAMBOO_NUM_OF_CORE > NUMCORES - 1) {
2976     return;
2977   }
2978   for(i=0; i<numtasks[BAMBOO_NUM_OF_CORE]; i++) {
2979     struct taskdescriptor * task=taskarray[BAMBOO_NUM_OF_CORE][i];
2980     int j;
2981
2982     /* Build objectsets */
2983     for(j=0; j<task->numParameters; j++) {
2984       struct parameterdescriptor *param=task->descriptorarray[j];
2985       struct parameterwrapper *parameter=param->queue;
2986       parameter->objectset=allocateObjectHash(10);
2987       parameter->task=task;
2988     }
2989
2990     /* Build iterators for parameters */
2991     for(j=0; j<task->numParameters; j++) {
2992       struct parameterdescriptor *param=task->descriptorarray[j];
2993       struct parameterwrapper *parameter=param->queue;
2994       builditerators(task, j, parameter);
2995     }
2996   }
2997 }
2998
2999 void toiReset(struct tagobjectiterator * it) {
3000   if (it->istag) {
3001     it->tagobjindex=0;
3002   } else if (it->numtags>0) {
3003     it->tagobjindex=0;
3004   } else {
3005     ObjectHashiterator(it->objectset, &it->it);
3006   }
3007 }
3008
3009 int toiHasNext(struct tagobjectiterator *it, 
3010                            void ** objectarray OPTARG(int * failed)) {
3011   if (it->istag) {
3012     /* Iterate tag */
3013     /* Get object with tags */
3014     struct ___Object___ *obj=objectarray[it->tagobjectslot];
3015     struct ___Object___ *tagptr=obj->___tags___;
3016     if (tagptr->type==TAGTYPE) {
3017       if ((it->tagobjindex==0)&& /* First object */
3018           (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
3019         return 1;
3020       else
3021         return 0;
3022     } else {
3023       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
3024       int tagindex=it->tagobjindex;
3025       for(; tagindex<ao->___cachedCode___; tagindex++) {
3026         struct ___TagDescriptor___ *td=
3027                 ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
3028         if (td->flag==it->tagid) {
3029           it->tagobjindex=tagindex; /* Found right type of tag */
3030           return 1;
3031         }
3032       }
3033       return 0;
3034     }
3035   } else if (it->numtags>0) {
3036     /* Use tags to locate appropriate objects */
3037     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
3038     struct ___Object___ *objptr=tag->flagptr;
3039     int i;
3040     if (objptr->type!=OBJECTARRAYTYPE) {
3041       if (it->tagobjindex>0)
3042         return 0;
3043       if (!ObjectHashcontainskey(it->objectset, (int) objptr))
3044         return 0;
3045       for(i=1; i<it->numtags; i++) {
3046         struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
3047         if (!containstag(objptr,tag2))
3048           return 0;
3049       }
3050       return 1;
3051     } else {
3052       struct ArrayObject *ao=(struct ArrayObject *) objptr;
3053       int tagindex;
3054       int i;
3055       for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
3056         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
3057         if (!ObjectHashcontainskey(it->objectset, (int) objptr))
3058           continue;
3059         for(i=1; i<it->numtags; i++) {
3060           struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
3061           if (!containstag(objptr,tag2))
3062             goto nexttag;
3063         }
3064         it->tagobjindex=tagindex;
3065         return 1;
3066 nexttag:
3067         ;
3068       }
3069       it->tagobjindex=tagindex;
3070       return 0;
3071     }
3072   } else {
3073     return ObjhasNext(&it->it);
3074   }
3075 }
3076
3077 int containstag(struct ___Object___ *ptr, 
3078                             struct ___TagDescriptor___ *tag) {
3079   int j;
3080   struct ___Object___ * objptr=tag->flagptr;
3081   if (objptr->type==OBJECTARRAYTYPE) {
3082     struct ArrayObject *ao=(struct ArrayObject *)objptr;
3083     for(j=0; j<ao->___cachedCode___; j++) {
3084       if (ptr==ARRAYGET(ao, struct ___Object___*, j)) {
3085         return 1;
3086                         }
3087     }
3088     return 0;
3089   } else {
3090     return objptr==ptr;
3091         }
3092 }
3093
3094 void toiNext(struct tagobjectiterator *it, 
3095                          void ** objectarray OPTARG(int * failed)) {
3096   /* hasNext has all of the intelligence */
3097   if(it->istag) {
3098     /* Iterate tag */
3099     /* Get object with tags */
3100     struct ___Object___ *obj=objectarray[it->tagobjectslot];
3101     struct ___Object___ *tagptr=obj->___tags___;
3102     if (tagptr->type==TAGTYPE) {
3103       it->tagobjindex++;
3104       objectarray[it->slot]=tagptr;
3105     } else {
3106       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
3107       objectarray[it->slot]=
3108                                 ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
3109     }
3110   } else if (it->numtags>0) {
3111     /* Use tags to locate appropriate objects */
3112     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
3113     struct ___Object___ *objptr=tag->flagptr;
3114     if (objptr->type!=OBJECTARRAYTYPE) {
3115       it->tagobjindex++;
3116       objectarray[it->slot]=objptr;
3117     } else {
3118       struct ArrayObject *ao=(struct ArrayObject *) objptr;
3119       objectarray[it->slot]=
3120                                 ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
3121     }
3122   } else {
3123     /* Iterate object */
3124     objectarray[it->slot]=(void *)Objkey(&it->it);
3125     Objnext(&it->it);
3126   }
3127 }
3128 #endif