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