split garbage collector into three files...
[IRC.git] / Robust / src / Runtime / thread.c
1 #include "runtime.h"
2 #include <sys/types.h>
3 #include <sys/mman.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include "thread.h"
8 #include "option.h"
9 #include <signal.h>
10 #include "methodheaders.h"
11 #ifndef MULTICORE
12 #include "mlp_lock.h"
13 #endif
14
15 #ifdef DSTM
16 #ifdef RECOVERY
17 #include <DSTM/interface_recovery/dstm.h>
18 #include <DSTM/interface_recovery/llookup.h>
19 #else
20 #include <DSTM/interface/dstm.h>
21 #include <DSTM/interface/llookup.h>
22 #endif
23 #endif
24
25 #ifndef RAW
26 #include <stdio.h>
27 #endif
28 #ifdef STM
29 #include "tm.h"
30 #endif
31 #include <execinfo.h>
32 #ifdef EVENTMONITOR
33 #include "monitor.h"
34 #endif
35
36
37 int threadcount;
38 pthread_mutex_t gclock;
39 pthread_mutex_t gclistlock;
40 pthread_cond_t gccond;
41 pthread_mutex_t objlock;
42 pthread_cond_t objcond;
43
44 pthread_mutex_t atomiclock;
45
46 pthread_mutex_t joinlock;
47 pthread_cond_t joincond;
48 pthread_key_t threadlocks;
49 pthread_key_t macthreadid;
50 pthread_mutex_t threadnotifylock;
51 pthread_cond_t threadnotifycond;
52 pthread_key_t oidval;
53
54 #if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
55 #ifndef MAC
56 extern __thread struct listitem litem;
57 #else
58 pthread_key_t memorybasekey;
59 pthread_key_t memorytopkey;
60 pthread_key_t litemkey;
61 #endif
62 extern struct listitem * list;
63 #endif
64
65 void threadexit() {
66 #ifdef DSTM
67   objheader_t* ptr;
68   unsigned int oidvalue;
69 #endif
70   void *value;
71
72 #ifdef THREADS
73 #ifdef MAC
74   struct lockvector *lptr=(struct lockvector *) pthread_getspecific(threadlocks);
75 #else
76   struct lockvector *lptr=&lvector;
77 #endif
78   for(lptr->index--;lptr->index>=0;lptr->index--) {
79     if (lptr->locks[lptr->index].islastlock) {
80       struct ___Object___ *ll=lptr->locks[lptr->index].object;
81       ll->tid=0;
82     }
83   }
84
85   pthread_mutex_lock(&objlock); //wake everyone up
86   pthread_cond_broadcast(&objcond);
87   pthread_mutex_unlock(&objlock);
88 #endif
89   pthread_mutex_lock(&gclistlock);
90
91 #ifndef MAC
92   if (litem.prev==NULL) {
93     list=litem.next;
94   } else {
95     litem.prev->next=litem.next;
96   }
97   if (litem.next!=NULL) {
98     litem.next->prev=litem.prev;
99   }
100 #else
101   {
102     struct listitem *litem=pthread_getspecific(litemkey);
103     if (litem->prev==NULL) {
104       list=litem->next;
105     } else {
106       litem->prev->next=litem->next;
107     }
108     if (litem->next!=NULL) {
109       litem->next->prev=litem->prev;
110     }
111   }
112 #endif
113   threadcount--;
114   pthread_cond_signal(&gccond);
115   pthread_mutex_unlock(&gclistlock);
116 #ifdef DSTM
117   /* Add transaction to check if thread finished for join operation */
118   value = pthread_getspecific(oidval);
119   oidvalue = *((unsigned int *)value);
120   goto transstart;
121 transstart:
122   {
123     transStart();
124     ptr = transRead(oidvalue);
125     struct ___Thread___ *p = (struct ___Thread___ *) ptr;
126     p->___threadDone___ = 1;
127     *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
128     if(transCommit() != 0) {
129       goto transstart;
130     }
131   }
132 #endif
133   pthread_exit(NULL);
134 }
135
136 #ifdef MAC
137 void threadhandler(int sig) {
138   printf("We just took sig=%d\n",sig);
139   printf("signal\n");
140   printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
141   
142   threadexit();
143 }
144 #else
145 void threadhandler(int sig, struct sigcontext ctx) {
146   void *buffer[100];
147   char **strings;
148   int nptrs,j;
149
150   printf("We just took sig=%d\n",sig);
151   printf("signal\n");
152   printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
153   nptrs = backtrace(buffer, 100);
154 #ifdef BIT64
155   buffer[1]=(void *)ctx.rip;
156 #else
157   buffer[1]=(void *)ctx.eip;
158 #endif
159
160   strings = backtrace_symbols(buffer, nptrs);
161   if (strings == NULL) {
162     perror("backtrace_symbols");
163     exit(EXIT_FAILURE);
164   }
165   
166   for (j = 0; j < nptrs; j++)
167     printf("%s\n", strings[j]);
168   
169   threadexit();
170 }
171 #endif
172
173 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
174
175 void initializethreads() {
176   struct sigaction sig;
177   threadcount=1;
178 #ifdef THREADS
179   pthread_mutex_init(&atomiclock, NULL);
180 #endif
181   pthread_mutex_init(&gclock, NULL);
182   pthread_mutex_init(&gclistlock, NULL);
183   pthread_cond_init(&gccond, NULL);
184   pthread_mutex_init(&objlock,NULL);
185   pthread_cond_init(&objcond,NULL);
186   pthread_mutex_init(&joinlock,NULL);
187   pthread_cond_init(&joincond,NULL);
188 #ifdef MAC
189   pthread_key_create(&macthreadid, NULL);
190   pthread_key_create(&threadlocks, NULL);
191   pthread_key_create(&memorybasekey, NULL);
192   pthread_key_create(&memorytopkey, NULL);
193   pthread_key_create(&litemkey, NULL);
194 #endif
195   processOptions();
196   initializeexithandler();
197 #ifdef AFFINITY
198   set_affinity();
199 #endif
200
201   //deprecated use of sighandler, but apparently still works
202 #ifdef SANDBOX
203   sig.sa_handler=(void *)errorhandler;
204   abortenabled=0;
205 #else
206   sig.sa_handler=(void *)threadhandler;
207 #endif
208   sig.sa_flags=SA_RESTART;
209   sigemptyset(&sig.sa_mask);
210
211   /* Catch bus errors, segmentation faults, and floating point exceptions*/
212   sigaction(SIGBUS,&sig,0);
213   sigaction(SIGSEGV,&sig,0);
214   sigaction(SIGFPE,&sig,0);
215   signal(SIGPIPE, SIG_IGN);
216 #ifdef STM
217   newobjs=calloc(1, sizeof(struct objlist));
218   t_cache = objstrCreate(1048576);
219   t_reserve=NULL;
220   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
221 #ifdef READSET
222   rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
223 #endif
224 #ifdef DELAYCOMP
225   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
226   ptrstack.count=0;
227   primstack.count=0;
228   branchstack.count=0;
229 #if defined(STMARRAY)&&!defined(DUALVIEW)
230   arraystack.count=0;
231 #endif
232   int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
233   if (a==-1)
234     perror("ptrstack");
235   a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
236   if (a==-1)
237     perror("primstack");
238   a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
239   if (a==-1)
240     perror("branchstack");
241 #if defined(STMARRAY)&&!defined(DUALVIEW)
242   a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
243   if (a==-1)
244     perror("arraystack");
245 #endif
246 #endif
247 #ifdef STMSTATS
248   trec=calloc(1, sizeof(threadrec_t));
249   trec->blocked = 0;
250   lockedobjs=calloc(1, sizeof(struct objlist));
251   objlockscope = calloc(1, sizeof(objlockstate_t));
252   pthread_mutex_init(&lockedobjstore, NULL);
253   { 
254     int i;
255     for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
256       typesCausingAbort[i].numaccess = 0;
257       typesCausingAbort[i].numabort = 0;
258       typesCausingAbort[i].numtrans = 0;
259     }
260   }
261 #endif
262 #endif
263 #ifdef MAC
264   struct listitem *litem=malloc(sizeof(struct listitem));
265   struct lockvector *lvector=malloc(sizeof(struct lockvector));
266   litem->lvector=lvector;
267   lvector->index=0;
268   pthread_setspecific(threadlocks, lvector);
269   pthread_setspecific(macthreadid, (void *)0);
270   pthread_setspecific(litemkey, litem);
271   char ** memorybase=malloc(sizeof(char *));
272   *memorybase=NULL;
273   pthread_setspecific(memorybasekey, memorybase);
274   char ** memorytop=malloc(sizeof(char *));
275   *memorytop=NULL;
276   pthread_setspecific(memorytopkey, memorytop);
277
278   litem->prev=NULL;
279   litem->next=list;
280   if(list!=NULL)
281     list->prev=litem;
282   list=litem;
283 #else
284   //Add our litem to list of threads
285   mythreadid=0;
286   litem.prev=NULL;
287   litem.next=list;
288   litem.lvector=&lvector;
289 #ifdef JNI
290   litem.jnirefs=&jnirefs;
291   jnirefs=NULL;
292 #endif
293   lvector.index=0;
294   if(list!=NULL)
295     list->prev=&litem;
296   list=&litem;
297 #endif
298 #ifdef EVENTMONITOR
299   createmonitor();
300 #endif
301 }
302
303 #if defined(THREADS)||defined(STM)
304 int threadcounter=0;
305
306 void initthread(struct ___Thread___ * ___this___) {
307 #ifdef AFFINITY
308   set_affinity();
309 #endif
310 #ifdef EVENTMONITOR
311   createmonitor();
312 #endif
313 #ifdef SANDBOX
314   struct sigaction sig;
315   abortenabled=0;
316   sig.sa_handler=(void *)errorhandler;
317   sig.sa_flags=SA_RESTART;
318   sigemptyset(&sig.sa_mask);
319
320   /* Catch bus errors, segmentation faults, and floating point exceptions*/
321   sigaction(SIGBUS,&sig,0);
322   sigaction(SIGSEGV,&sig,0);
323   sigaction(SIGFPE,&sig,0);
324 #endif
325 #ifdef PRECISE_GC
326   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
327   //Add our litem to list of threads
328 #ifdef MAC
329   struct listitem litem;
330   struct lockvector lvector;
331   char * memorybase=NULL;
332   char * memorytop=NULL;
333   pthread_setspecific(litemkey, &litem);
334   pthread_setspecific(memorybasekey, &memorybase);
335   pthread_setspecific(memorytopkey, &memorytop);
336   pthread_setspecific(threadlocks, &lvector);
337 #endif
338 #ifdef JNI
339   litem.jnirefs=&jnirefs;
340   jnirefs=NULL;
341 #endif
342   litem.lvector=&lvector;
343   lvector.index=0;
344   litem.prev=NULL;
345   pthread_mutex_lock(&gclistlock);
346 #ifdef MAC
347   pthread_setspecific(macthreadid, (void *)((long)(++threadcounter)));
348 #else
349   mythreadid=++threadcounter;
350 #endif
351   litem.next=list;
352   if(list!=NULL)
353     list->prev=&litem;
354   list=&litem;
355   pthread_mutex_unlock(&gclistlock);
356 #ifdef THREADS
357   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
358 #else
359   newobjs=calloc(1, sizeof(struct objlist));
360 #ifdef STMSTATS
361   trec=calloc(1, sizeof(threadrec_t));
362   trec->blocked = 0;
363   lockedobjs=calloc(1, sizeof(struct objlist));
364 #endif
365   t_cache = objstrCreate(1048576);
366   t_reserve=NULL;
367   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
368 #ifdef READSET
369   rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
370 #endif
371 #ifdef DELAYCOMP
372   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
373   ptrstack.count=0;
374   primstack.count=0;
375   branchstack.count=0;
376 #if defined(STMARRAY)&&!defined(DUALVIEW)
377   arraystack.count=0;
378 #endif
379   int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
380   if (a==-1)
381     perror("ptrstack");
382   a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
383   if (a==-1)
384     perror("primstack");
385   a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
386   if (a==-1)
387     perror("branchstack");
388 #if defined(STMARRAY)&!defined(DUALVIEW)
389   a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
390   if (a==-1)
391     perror("arraystack");
392 #endif
393 #endif
394  ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
395  objstrDelete(t_cache);
396  objstrDelete(t_reserve);
397  t_chashDelete();
398  free(newobjs);
399 #ifdef STMSTATS
400  free(lockedobjs);
401 #endif
402 #endif
403   ___this___=(struct ___Thread___ *) p[2];
404 #else
405   ___Thread______staticStart____L___Thread___(___this___);
406 #endif
407   ___this___->___finished___=1;
408   pthread_mutex_lock(&joinlock);
409   pthread_cond_signal(&joincond);
410   pthread_mutex_unlock(&joinlock);
411
412   pthread_mutex_lock(&gclistlock);
413   if (litem.prev==NULL) {
414     list=litem.next;
415   } else {
416     litem.prev->next=litem.next;
417   }
418   if (litem.next!=NULL) {
419     litem.next->prev=litem.prev;
420   }
421   threadcount--;
422   pthread_cond_signal(&gccond);
423   pthread_mutex_unlock(&gclistlock);
424 }
425 #endif
426
427 #ifdef D___Thread______sleep____J
428 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
429 #if defined(THREADS)||defined(STM)
430 #ifdef PRECISE_GC
431   stopforgc((struct garbagelist *)___params___);
432 #endif
433 #endif
434   usleep(___millis___*1000);
435 #if defined(THREADS)||defined(STM)
436 #ifdef PRECISE_GC
437   restartaftergc();
438 #endif
439 #endif
440 }
441 #endif
442
443 #ifdef D___Thread______yield____
444 void CALL00(___Thread______yield____) {
445   pthread_yield();
446 }
447 #endif
448
449 #ifdef D___Thread______abort____
450 void CALL00(___Thread______abort____) {
451 #ifdef SANDBOX
452   _longjmp(aborttrans,1);
453 #endif
454 }
455 #endif
456
457 #ifdef DSTM
458 #ifdef RECOVERY
459 // return if the machine is dead
460 #ifdef D___Thread______nativeGetStatus____I
461 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
462   return getStatus(___mid___);
463 }
464 #endif
465 #else 
466 #ifdef D___Thread______nativeGetStatus____I
467 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
468   return 0;
469 }
470 #endif
471 #endif
472 #endif
473 #ifdef DSTM
474 /* Add thread join capability */
475 #ifdef D___Thread______join____
476 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
477   unsigned int *oidarray;
478   unsigned short *versionarray, version;
479   objheader_t *ptr;
480   /* Add transaction to check if thread finished for join operation */
481 transstart:
482   transStart();
483   ptr = transRead((unsigned int) VAR(___this___));
484   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
485 #ifdef THREADJOINDEBUG
486   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
487 #endif
488   if(p->___threadDone___ == 1) {
489 #ifdef THREADJOINDEBUG
490     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
491 #endif
492     transAbort();
493     return;
494   }
495 #ifdef RECOVERY
496   else if( checkiftheMachineDead(p->___mid___) == 0) {
497     printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
498     transAbort();
499     return;
500   }
501 #endif
502   else {
503     version = (ptr-1)->version;
504     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
505       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
506       return;
507     }
508
509     oidarray[0] = (unsigned int) VAR(___this___);
510
511     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
512       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
513       free(oidarray);
514       return;
515     }
516     versionarray[0] = version;
517     /* Request Notification */
518 #ifdef PRECISE_GC
519     stopforgc((struct garbagelist *)___params___);
520 #endif
521
522 #ifdef RECOVERY
523     reqNotify(oidarray, versionarray, 1,p->___mid___);
524 #else
525     reqNotify(oidarray, versionarray, 1);
526 #endif
527 #ifdef PRECISE_GC
528     restartaftergc();
529 #endif
530     free(oidarray);
531     free(versionarray);
532     transAbort();
533     goto transstart;
534   }
535   return;
536 }
537 #endif
538 #endif
539
540 #if defined(THREADS)||defined(STM)
541 #ifdef D___Thread______nativeJoin____
542 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
543   pthread_mutex_lock(&joinlock);
544   while(!VAR(___this___)->___finished___) {
545 #ifdef PRECISE_GC
546   stopforgc((struct garbagelist *)___params___);
547 #endif
548     pthread_cond_wait(&joincond, &joinlock);
549 #ifdef PRECISE_GC
550     restartaftergc();
551 #endif
552   }
553   pthread_mutex_unlock(&joinlock);
554 }
555 #endif
556
557 #ifdef D___Thread______nativeCreate____
558 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
559   pthread_t thread;
560   int retval;
561   pthread_attr_t nattr;
562
563   pthread_mutex_lock(&gclistlock);
564   threadcount++;
565   pthread_mutex_unlock(&gclistlock);
566   pthread_attr_init(&nattr);
567   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
568   INTPTR stacksize;
569   pthread_attr_getstacksize(&nattr, &stacksize);
570   do {
571     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
572     if (retval!=0)
573       usleep(1);
574   } while(retval!=0);
575   /* This next statement will likely not work on many machines */
576
577   pthread_attr_destroy(&nattr);
578 }
579 #endif
580 #endif
581
582 #ifdef DSTM
583 #ifdef D___Thread______start____I
584 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
585   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
586 }
587 #endif
588 #endif
589
590 #ifdef DSTM
591 void globalDestructor(void *value) {
592   free(value);
593   pthread_setspecific(oidval, NULL);
594 }
595
596 void initDSMthread(int *ptr) {
597   objheader_t *tmp;
598   void *threadData;
599   int oid=ptr[0];
600   int type=ptr[1];
601   free(ptr);
602 #ifdef PRECISE_GC
603   int p[]={1, 0 /* NULL */, oid};
604 #ifdef MAC
605   struct listitem litem;
606   pthread_setspecific(litemkey, &litem);
607 #endif
608
609   //Add our litem to list of threads
610   litem.prev=NULL;
611   pthread_mutex_lock(&gclistlock);
612   litem.next=list;
613   if(list!=NULL)
614     list->prev=&litem;
615   list=&litem;
616   pthread_mutex_unlock(&gclistlock);
617
618   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
619 #else
620   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
621 #endif
622   threadData = calloc(1, sizeof(unsigned int));
623   *((unsigned int *) threadData) = oid;
624   pthread_setspecific(oidval, threadData);
625   pthread_mutex_lock(&gclistlock);
626
627 #ifdef THREADS
628   pthread_setspecific(threadlocks, litem.locklist);
629 #endif
630   if (litem.prev==NULL) {
631     list=litem.next;
632   } else {
633     litem.prev->next=litem.next;
634   }
635   if (litem.next!=NULL) {
636     litem.next->prev=litem.prev;
637   }
638   threadcount--;
639   pthread_cond_signal(&gccond);
640   pthread_mutex_unlock(&gclistlock);
641   /* Add transaction to check if thread finished for join operation */
642   goto transstart;
643 transstart:
644   {
645     transStart();
646     tmp  = transRead((unsigned int) oid);
647     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
648     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
649     if(transCommit()!= 0) {
650       goto transstart;
651     }
652   }
653   pthread_exit(NULL);
654 }
655
656 void startDSMthread(int oid, int objType) {
657   pthread_t thread;
658   int retval;
659   pthread_attr_t nattr;
660
661 //  printf("%s -> oid : %u\n",__func__,oid);
662
663   pthread_mutex_lock(&gclistlock);
664   threadcount++;
665   pthread_mutex_unlock(&gclistlock);
666   pthread_attr_init(&nattr);
667   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
668   int * ptr=malloc(sizeof(int)*2);
669   ptr[0]=oid;
670   ptr[1]=objType;
671   pthread_key_create(&oidval, globalDestructor);
672   
673   do {
674     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
675     if (retval!=0)
676       usleep(1);
677   } while(retval!=0);
678
679   pthread_attr_destroy(&nattr);
680 }
681
682 #endif