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