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