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   int a=mprotect((downpage(&ptrstack.array[MAXPOINTERS])), 4096, PROT_NONE);
203   if (a==-1)
204     perror("ptrstack");
205   a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
206   if (a==-1)
207     perror("primstack");
208   a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
209   if (a==-1)
210     perror("branchstack");
211 #endif
212 #ifdef STMSTATS
213   trec=calloc(1, sizeof(threadrec_t));
214   trec->blocked = 0;
215   lockedobjs=calloc(1, sizeof(struct objlist));
216   objlockscope = calloc(1, sizeof(objlockstate_t));
217   pthread_mutex_init(&lockedobjstore, NULL);
218   { 
219     int i;
220     for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
221       typesCausingAbort[i].numaccess = 0;
222       typesCausingAbort[i].numabort = 0;
223       typesCausingAbort[i].numtrans = 0;
224     }
225   }
226 #endif
227 #endif
228 #ifdef MAC
229   struct listitem *litem=malloc(sizeof(struct listitem));
230   pthread_setspecific(litemkey, litem);
231   litem->prev=NULL;
232   litem->next=list;
233   if(list!=NULL)
234     list->prev=litem;
235   list=litem;
236 #else
237   //Add our litem to list of threads
238   litem.prev=NULL;
239   litem.next=list;
240   if(list!=NULL)
241     list->prev=&litem;
242   list=&litem;
243 #endif
244 }
245
246 #if defined(THREADS)||defined(STM)
247 void initthread(struct ___Thread___ * ___this___) {
248 #ifdef SANDBOX
249   struct sigaction sig;
250   abortenabled=0;
251   sig.sa_handler=(void *)errorhandler;
252   sig.sa_flags=SA_RESTART;
253   sigemptyset(&sig.sa_mask);
254
255   /* Catch bus errors, segmentation faults, and floating point exceptions*/
256   sigaction(SIGBUS,&sig,0);
257   sigaction(SIGSEGV,&sig,0);
258   sigaction(SIGFPE,&sig,0);
259 #endif
260 #ifdef PRECISE_GC
261   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
262   //Add our litem to list of threads
263 #ifdef MAC
264   struct listitem litem;
265   pthread_setspecific(litemkey, &litem);
266 #endif
267   litem.prev=NULL;
268   pthread_mutex_lock(&gclistlock);
269   litem.next=list;
270   if(list!=NULL)
271     list->prev=&litem;
272   list=&litem;
273   pthread_mutex_unlock(&gclistlock);
274   
275 #ifdef THREADS
276   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
277 #else
278   newobjs=calloc(1, sizeof(struct objlist));
279 #ifdef STMSTATS
280   trec=calloc(1, sizeof(threadrec_t));
281   trec->blocked = 0;
282   lockedobjs=calloc(1, sizeof(struct objlist));
283 #endif
284   t_cache = objstrCreate(1048576);
285   t_reserve=NULL;
286   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
287 #ifdef READSET
288   rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
289 #endif
290 #ifdef DELAYCOMP
291   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
292   ptrstack.count=0;
293   primstack.count=0;
294   branchstack.count=0;
295   int a=mprotect(downpage(&ptrstack.array[MAXPOINTERS]), 4096, PROT_NONE);
296   if (a==-1)
297     perror("ptrstack");
298   a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
299   if (a==-1)
300     perror("primstack");
301   a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
302   if (a==-1)
303     perror("branchstack");
304 #endif
305  ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
306  objstrDelete(t_cache);
307  objstrDelete(t_reserve);
308  t_chashDelete();
309  free(newobjs);
310 #ifdef STMSTATS
311  free(lockedobjs);
312 #endif
313 #endif
314   ___this___=(struct ___Thread___ *) p[2];
315 #else
316   ___Thread______staticStart____L___Thread___(___this___);
317 #endif
318   ___this___->___finished___=1;
319   pthread_mutex_lock(&joinlock);
320   pthread_cond_signal(&joincond);
321   pthread_mutex_unlock(&joinlock);
322
323   pthread_mutex_lock(&gclistlock);
324 #ifdef THREADS
325   pthread_setspecific(threadlocks, litem.locklist);
326 #endif
327   if (litem.prev==NULL) {
328     list=litem.next;
329   } else {
330     litem.prev->next=litem.next;
331   }
332   if (litem.next!=NULL) {
333     litem.next->prev=litem.prev;
334   }
335   threadcount--;
336   pthread_cond_signal(&gccond);
337   pthread_mutex_unlock(&gclistlock);
338 }
339 #endif
340
341 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
342 #if defined(THREADS)||defined(STM)
343 #ifdef PRECISE_GC
344   stopforgc((struct garbagelist *)___params___);
345 #endif
346 #endif
347   usleep(___millis___);
348 #if defined(THREADS)||defined(STM)
349 #ifdef PRECISE_GC
350   restartaftergc();
351 #endif
352 #endif
353 }
354
355 #if defined(DSTM)|| defined(THREADS)||defined(STM)
356 void CALL00(___Thread______yield____) {
357   pthread_yield();
358 }
359 #endif
360
361 #ifdef DSTM
362 #ifdef RECOVERY
363 // return if the machine is dead
364 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
365   return getStatus(___mid___);
366 }
367 #else 
368 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
369   return 0;
370 }
371 #endif
372 #endif
373 #ifdef DSTM
374 /* Add thread join capability */
375 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
376   unsigned int *oidarray;
377   unsigned short *versionarray, version;
378   objheader_t *ptr;
379   /* Add transaction to check if thread finished for join operation */
380 transstart:
381   transStart();
382   ptr = transRead((unsigned int) VAR(___this___));
383   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
384 #ifdef THREADJOINDEBUG
385   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
386 #endif
387   if(p->___threadDone___ == 1) {
388 #ifdef THREADJOINDEBUG
389     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
390 #endif
391     transAbort();
392     return;
393   }
394 #ifdef RECOVERY
395   else if( checkiftheMachineDead(p->___mid___) == 0) {
396     printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
397     transAbort();
398     return;
399   }
400 #endif
401   else {
402     version = (ptr-1)->version;
403     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
404       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
405       return;
406     }
407
408     oidarray[0] = (unsigned int) VAR(___this___);
409
410     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
411       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
412       free(oidarray);
413       return;
414     }
415     versionarray[0] = version;
416     /* Request Notification */
417 #ifdef PRECISE_GC
418     stopforgc((struct garbagelist *)___params___);
419 #endif
420
421 #ifdef RECOVERY
422     reqNotify(oidarray, versionarray, 1,p->___mid___);
423 #else
424     reqNotify(oidarray, versionarray, 1);
425 #endif
426 #ifdef PRECISE_GC
427     restartaftergc();
428 #endif
429     free(oidarray);
430     free(versionarray);
431     transAbort();
432     goto transstart;
433   }
434   return;
435 }
436 #endif
437
438 #if defined(THREADS)||defined(STM)
439 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
440   pthread_mutex_lock(&joinlock);
441   while(!VAR(___this___)->___finished___) {
442 #ifdef PRECISE_GC
443   stopforgc((struct garbagelist *)___params___);
444 #endif
445     pthread_cond_wait(&joincond, &joinlock);
446 #ifdef PRECISE_GC
447     restartaftergc();
448 #endif
449   }
450   pthread_mutex_unlock(&joinlock);
451 }
452
453 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
454   pthread_t thread;
455   int retval;
456   pthread_attr_t nattr;
457
458   pthread_mutex_lock(&gclistlock);
459   threadcount++;
460   pthread_mutex_unlock(&gclistlock);
461   pthread_attr_init(&nattr);
462   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
463   INTPTR stacksize;
464   pthread_attr_getstacksize(&nattr, &stacksize);
465   printf("STACKSIZE=%u\n",stacksize);
466   do {
467     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
468     if (retval!=0)
469       usleep(1);
470   } while(retval!=0);
471   /* This next statement will likely not work on many machines */
472
473   pthread_attr_destroy(&nattr);
474 }
475 #endif
476
477 #ifdef DSTM
478 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
479   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
480 }
481 #endif
482
483 #ifdef DSTM
484 void globalDestructor(void *value) {
485   free(value);
486   pthread_setspecific(oidval, NULL);
487 }
488
489 void initDSMthread(int *ptr) {
490   objheader_t *tmp;
491   void *threadData;
492   int oid=ptr[0];
493   int type=ptr[1];
494   free(ptr);
495 #ifdef PRECISE_GC
496   int p[]={1, 0 /* NULL */, oid};
497 #ifdef MAC
498   struct listitem litem;
499   pthread_setspecific(litemkey, &litem);
500 #endif
501
502   //Add our litem to list of threads
503   litem.prev=NULL;
504   pthread_mutex_lock(&gclistlock);
505   litem.next=list;
506   if(list!=NULL)
507     list->prev=&litem;
508   list=&litem;
509   pthread_mutex_unlock(&gclistlock);
510
511   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
512 #else
513   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
514 #endif
515   threadData = calloc(1, sizeof(unsigned int));
516   *((unsigned int *) threadData) = oid;
517   pthread_setspecific(oidval, threadData);
518   pthread_mutex_lock(&gclistlock);
519
520 #ifdef THREADS
521   pthread_setspecific(threadlocks, litem.locklist);
522 #endif
523   if (litem.prev==NULL) {
524     list=litem.next;
525   } else {
526     litem.prev->next=litem.next;
527   }
528   if (litem.next!=NULL) {
529     litem.next->prev=litem.prev;
530   }
531   threadcount--;
532   pthread_cond_signal(&gccond);
533   pthread_mutex_unlock(&gclistlock);
534   /* Add transaction to check if thread finished for join operation */
535   goto transstart;
536 transstart:
537   {
538     transStart();
539     tmp  = transRead((unsigned int) oid);
540     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
541     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
542     if(transCommit()!= 0) {
543       goto transstart;
544     }
545   }
546   pthread_exit(NULL);
547 }
548
549 void startDSMthread(int oid, int objType) {
550   pthread_t thread;
551   int retval;
552   pthread_attr_t nattr;
553
554   pthread_mutex_lock(&gclistlock);
555   threadcount++;
556   pthread_mutex_unlock(&gclistlock);
557   pthread_attr_init(&nattr);
558   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
559   int * ptr=malloc(sizeof(int)*2);
560   ptr[0]=oid;
561   ptr[1]=objType;
562   pthread_key_create(&oidval, globalDestructor);
563   
564   do {
565     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
566     if (retval!=0)
567       usleep(1);
568   } while(retval!=0);
569
570   pthread_attr_destroy(&nattr);
571 }
572
573 #endif