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