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