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