changes.
[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       ObjectPtr 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     ThreadPtr p = (ThreadPtr) ptr;
126     p->___threadDone___ = 1;
127     *((unsigned int *)&((ObjectPtr) 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 #ifdef D___Thread______nativeCreate____
307 void initthread(ThreadPtr ___this___) {
308 #ifdef AFFINITY
309   set_affinity();
310 #endif
311 #ifdef EVENTMONITOR
312   createmonitor();
313 #endif
314 #ifdef SANDBOX
315   struct sigaction sig;
316   abortenabled=0;
317   sig.sa_handler=(void *)errorhandler;
318   sig.sa_flags=SA_RESTART;
319   sigemptyset(&sig.sa_mask);
320
321   /* Catch bus errors, segmentation faults, and floating point exceptions*/
322   sigaction(SIGBUS,&sig,0);
323   sigaction(SIGSEGV,&sig,0);
324   sigaction(SIGFPE,&sig,0);
325 #endif
326 #ifdef PRECISE_GC
327   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
328   //Add our litem to list of threads
329 #ifdef MAC
330   struct listitem litem;
331   struct lockvector lvector;
332   char * memorybase=NULL;
333   char * memorytop=NULL;
334   pthread_setspecific(litemkey, &litem);
335   pthread_setspecific(memorybasekey, &memorybase);
336   pthread_setspecific(memorytopkey, &memorytop);
337   pthread_setspecific(threadlocks, &lvector);
338 #endif
339 #ifdef JNI
340   litem.jnirefs=&jnirefs;
341   jnirefs=NULL;
342 #endif
343   litem.lvector=&lvector;
344   lvector.index=0;
345   litem.prev=NULL;
346   pthread_mutex_lock(&gclistlock);
347 #ifdef MAC
348   pthread_setspecific(macthreadid, (void *)((long)(++threadcounter)));
349 #else
350   mythreadid=++threadcounter;
351 #endif
352   litem.next=list;
353   if(list!=NULL)
354     list->prev=&litem;
355   list=&litem;
356   pthread_mutex_unlock(&gclistlock);
357 #ifdef THREADS
358   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
359 #else
360   newobjs=calloc(1, sizeof(struct objlist));
361 #ifdef STMSTATS
362   trec=calloc(1, sizeof(threadrec_t));
363   trec->blocked = 0;
364   lockedobjs=calloc(1, sizeof(struct objlist));
365 #endif
366   t_cache = objstrCreate(1048576);
367   t_reserve=NULL;
368   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
369 #ifdef READSET
370   rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
371 #endif
372 #ifdef DELAYCOMP
373   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
374   ptrstack.count=0;
375   primstack.count=0;
376   branchstack.count=0;
377 #if defined(STMARRAY)&&!defined(DUALVIEW)
378   arraystack.count=0;
379 #endif
380   int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
381   if (a==-1)
382     perror("ptrstack");
383   a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
384   if (a==-1)
385     perror("primstack");
386   a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
387   if (a==-1)
388     perror("branchstack");
389 #if defined(STMARRAY)&!defined(DUALVIEW)
390   a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
391   if (a==-1)
392     perror("arraystack");
393 #endif
394 #endif
395   ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
396   objstrDelete(t_cache);
397   objstrDelete(t_reserve);
398   t_chashDelete();
399   free(newobjs);
400 #ifdef STMSTATS
401   free(lockedobjs);
402 #endif
403 #endif
404   ___this___=(ThreadPtr) p[2];
405 #else
406   ___Thread______staticStart____L___Thread___(___this___);
407 #endif
408   ___this___->___finished___=1;
409   pthread_mutex_lock(&joinlock);
410   pthread_cond_signal(&joincond);
411   pthread_mutex_unlock(&joinlock);
412
413   pthread_mutex_lock(&gclistlock);
414   if (litem.prev==NULL) {
415     list=litem.next;
416   } else {
417     litem.prev->next=litem.next;
418   }
419   if (litem.next!=NULL) {
420     litem.next->prev=litem.prev;
421   }
422   threadcount--;
423   pthread_cond_signal(&gccond);
424   pthread_mutex_unlock(&gclistlock);
425 }
426 #endif
427 #endif
428
429 #ifdef D___Thread______sleep____J
430 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
431 #if defined(THREADS)||defined(STM)
432 #ifdef PRECISE_GC
433   stopforgc((struct garbagelist *)___params___);
434 #endif
435 #endif
436   usleep(___millis___*1000);
437 #if defined(THREADS)||defined(STM)
438 #ifdef PRECISE_GC
439   restartaftergc();
440 #endif
441 #endif
442 }
443 #endif
444
445 #ifdef D___Thread______yield____
446 void CALL00(___Thread______yield____) {
447   pthread_yield();
448 }
449 #endif
450
451 #ifdef D___Thread______abort____
452 void CALL00(___Thread______abort____) {
453 #ifdef SANDBOX
454   _longjmp(aborttrans,1);
455 #endif
456 }
457 #endif
458
459 #ifdef DSTM
460 #ifdef RECOVERY
461 // return if the machine is dead
462 #ifdef D___Thread______nativeGetStatus____I
463 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
464   return getStatus(___mid___);
465 }
466 #endif
467 #else
468 #ifdef D___Thread______nativeGetStatus____I
469 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
470   return 0;
471 }
472 #endif
473 #endif
474 #endif
475 #ifdef DSTM
476 /* Add thread join capability */
477 #ifdef D___Thread______join____
478 void CALL01(___Thread______join____, ThreadPtr ___this___) {
479   unsigned int *oidarray;
480   unsigned short *versionarray, version;
481   objheader_t *ptr;
482   /* Add transaction to check if thread finished for join operation */
483 transstart:
484   transStart();
485   ptr = transRead((unsigned int) VAR(___this___));
486   ThreadPtr p = (ThreadPtr) ptr;
487 #ifdef THREADJOINDEBUG
488   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
489 #endif
490   if(p->___threadDone___ == 1) {
491 #ifdef THREADJOINDEBUG
492     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
493 #endif
494     transAbort();
495     return;
496   }
497 #ifdef RECOVERY
498   else if( checkiftheMachineDead(p->___mid___) == 0) {
499     printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
500     transAbort();
501     return;
502   }
503 #endif
504   else {
505     version = (ptr-1)->version;
506     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
507       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
508       return;
509     }
510
511     oidarray[0] = (unsigned int) VAR(___this___);
512
513     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
514       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
515       free(oidarray);
516       return;
517     }
518     versionarray[0] = version;
519     /* Request Notification */
520 #ifdef PRECISE_GC
521     stopforgc((struct garbagelist *)___params___);
522 #endif
523
524 #ifdef RECOVERY
525     reqNotify(oidarray, versionarray, 1,p->___mid___);
526 #else
527     reqNotify(oidarray, versionarray, 1);
528 #endif
529 #ifdef PRECISE_GC
530     restartaftergc();
531 #endif
532     free(oidarray);
533     free(versionarray);
534     transAbort();
535     goto transstart;
536   }
537   return;
538 }
539 #endif
540 #endif
541
542 #if defined(THREADS)||defined(STM)
543 #ifdef D___Thread______nativeJoin____
544 void CALL01(___Thread______nativeJoin____, ThreadPtr ___this___) {
545   pthread_mutex_lock(&joinlock);
546   while(!VAR(___this___)->___finished___) {
547 #ifdef PRECISE_GC
548     stopforgc((struct garbagelist *)___params___);
549 #endif
550     pthread_cond_wait(&joincond, &joinlock);
551 #ifdef PRECISE_GC
552     restartaftergc();
553 #endif
554   }
555   pthread_mutex_unlock(&joinlock);
556 }
557 #endif
558
559 #ifdef D___Thread______nativeCreate____
560 void CALL01(___Thread______nativeCreate____, ThreadPtr ___this___) {
561   pthread_t thread;
562   int retval;
563   pthread_attr_t nattr;
564
565   pthread_mutex_lock(&gclistlock);
566   threadcount++;
567   pthread_mutex_unlock(&gclistlock);
568   pthread_attr_init(&nattr);
569   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
570   INTPTR stacksize;
571   pthread_attr_getstacksize(&nattr, &stacksize);
572   do {
573     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
574     if (retval!=0)
575       usleep(1);
576   } while(retval!=0);
577   /* This next statement will likely not work on many machines */
578
579   pthread_attr_destroy(&nattr);
580 }
581 #endif
582 #endif
583
584 #ifdef DSTM
585 #ifdef D___Thread______start____I
586 void CALL12(___Thread______start____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
587   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
588 }
589 #endif
590 #endif
591
592 #ifdef DSTM
593 void globalDestructor(void *value) {
594   free(value);
595   pthread_setspecific(oidval, NULL);
596 }
597
598 void initDSMthread(int *ptr) {
599   objheader_t *tmp;
600   void *threadData;
601   int oid=ptr[0];
602   int type=ptr[1];
603   free(ptr);
604 #ifdef PRECISE_GC
605   int p[]={1, 0 /* NULL */, oid};
606 #ifdef MAC
607   struct listitem litem;
608   pthread_setspecific(litemkey, &litem);
609 #endif
610
611   //Add our litem to list of threads
612   litem.prev=NULL;
613   pthread_mutex_lock(&gclistlock);
614   litem.next=list;
615   if(list!=NULL)
616     list->prev=&litem;
617   list=&litem;
618   pthread_mutex_unlock(&gclistlock);
619
620   ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
621 #else
622   ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
623 #endif
624   threadData = calloc(1, sizeof(unsigned int));
625   *((unsigned int *) threadData) = oid;
626   pthread_setspecific(oidval, threadData);
627   pthread_mutex_lock(&gclistlock);
628
629 #ifdef THREADS
630   pthread_setspecific(threadlocks, litem.locklist);
631 #endif
632   if (litem.prev==NULL) {
633     list=litem.next;
634   } else {
635     litem.prev->next=litem.next;
636   }
637   if (litem.next!=NULL) {
638     litem.next->prev=litem.prev;
639   }
640   threadcount--;
641   pthread_cond_signal(&gccond);
642   pthread_mutex_unlock(&gclistlock);
643   /* Add transaction to check if thread finished for join operation */
644   goto transstart;
645 transstart:
646   {
647     transStart();
648     tmp  = transRead((unsigned int) oid);
649     ((ThreadPtr)tmp)->___threadDone___ = 1;
650     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
651     if(transCommit()!= 0) {
652       goto transstart;
653     }
654   }
655   pthread_exit(NULL);
656 }
657
658 void startDSMthread(int oid, int objType) {
659   pthread_t thread;
660   int retval;
661   pthread_attr_t nattr;
662
663 //  printf("%s -> oid : %u\n",__func__,oid);
664
665   pthread_mutex_lock(&gclistlock);
666   threadcount++;
667   pthread_mutex_unlock(&gclistlock);
668   pthread_attr_init(&nattr);
669   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
670   int * ptr=malloc(sizeof(int)*2);
671   ptr[0]=oid;
672   ptr[1]=objType;
673   pthread_key_create(&oidval, globalDestructor);
674
675   do {
676     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
677     if (retval!=0)
678       usleep(1);
679   } while(retval!=0);
680
681   pthread_attr_destroy(&nattr);
682 }
683
684 #endif