10 #include "methodheaders.h"
17 #include <DSTM/interface_recovery/dstm.h>
18 #include <DSTM/interface_recovery/llookup.h>
20 #include <DSTM/interface/dstm.h>
21 #include <DSTM/interface/llookup.h>
38 pthread_mutex_t gclock;
39 pthread_mutex_t gclistlock;
40 pthread_cond_t gccond;
41 pthread_mutex_t objlock;
42 pthread_cond_t objcond;
44 pthread_mutex_t atomiclock;
46 pthread_mutex_t joinlock;
47 pthread_cond_t joincond;
48 pthread_key_t threadlocks;
49 pthread_mutex_t threadnotifylock;
50 pthread_cond_t threadnotifycond;
53 #if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
55 extern __thread struct listitem litem;
57 pthread_key_t litemkey;
59 extern struct listitem * list;
65 unsigned int oidvalue;
71 struct lockvector *lptr=(struct lockvector *) pthread_getspecific(threadlocks);
73 struct lockvector *lptr=&lvector;
75 for(lptr->index--;lptr->index>=0;lptr->index--) {
76 if (lptr->locks[lptr->index].islastlock) {
77 struct ___Object___ *ll=lptr->locks[lptr->index].object;
84 pthread_mutex_lock(&objlock); //wake everyone up
85 pthread_cond_broadcast(&objcond);
86 pthread_mutex_unlock(&objlock);
88 pthread_mutex_lock(&gclistlock);
91 if (litem.prev==NULL) {
94 litem.prev->next=litem.next;
96 if (litem.next!=NULL) {
97 litem.next->prev=litem.prev;
101 struct listitem *litem=pthread_getspecific(litemkey);
102 if (litem->prev==NULL) {
105 litem->prev->next=litem->next;
107 if (litem->next!=NULL) {
108 litem->next->prev=litem->prev;
113 pthread_cond_signal(&gccond);
114 pthread_mutex_unlock(&gclistlock);
116 /* Add transaction to check if thread finished for join operation */
117 value = pthread_getspecific(oidval);
118 oidvalue = *((unsigned int *)value);
123 ptr = transRead(oidvalue);
124 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
125 p->___threadDone___ = 1;
126 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
127 if(transCommit() != 0) {
135 void threadhandler(int sig, struct sigcontext ctx) {
140 printf("We just took sig=%d\n",sig);
142 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
143 nptrs = backtrace(buffer, 100);
145 buffer[1]=(void *)ctx.rip;
147 buffer[1]=(void *)ctx.eip;
150 strings = backtrace_symbols(buffer, nptrs);
151 if (strings == NULL) {
152 perror("backtrace_symbols");
156 for (j = 0; j < nptrs; j++)
157 printf("%s\n", strings[j]);
162 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
164 void initializethreads() {
165 struct sigaction sig;
168 pthread_mutex_init(&atomiclock, NULL);
170 pthread_mutex_init(&gclock, NULL);
171 pthread_mutex_init(&gclistlock, NULL);
172 pthread_cond_init(&gccond, NULL);
173 pthread_mutex_init(&objlock,NULL);
174 pthread_cond_init(&objcond,NULL);
175 pthread_mutex_init(&joinlock,NULL);
176 pthread_cond_init(&joincond,NULL);
178 pthread_key_create(&threadlocks, NULL);
179 pthread_key_create(&litem, NULL);
182 initializeexithandler();
187 //deprecated use of sighandler, but apparently still works
189 sig.sa_handler=(void *)errorhandler;
192 sig.sa_handler=(void *)threadhandler;
194 sig.sa_flags=SA_RESTART;
195 sigemptyset(&sig.sa_mask);
197 /* Catch bus errors, segmentation faults, and floating point exceptions*/
198 sigaction(SIGBUS,&sig,0);
199 sigaction(SIGSEGV,&sig,0);
200 sigaction(SIGFPE,&sig,0);
201 signal(SIGPIPE, SIG_IGN);
203 newobjs=calloc(1, sizeof(struct objlist));
204 t_cache = objstrCreate(1048576);
206 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
208 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
211 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
215 #if defined(STMARRAY)&&!defined(DUALVIEW)
218 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
221 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
224 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
226 perror("branchstack");
227 #if defined(STMARRAY)&&!defined(DUALVIEW)
228 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
230 perror("arraystack");
234 trec=calloc(1, sizeof(threadrec_t));
236 lockedobjs=calloc(1, sizeof(struct objlist));
237 objlockscope = calloc(1, sizeof(objlockstate_t));
238 pthread_mutex_init(&lockedobjstore, NULL);
241 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
242 typesCausingAbort[i].numaccess = 0;
243 typesCausingAbort[i].numabort = 0;
244 typesCausingAbort[i].numtrans = 0;
250 struct listitem *litem=malloc(sizeof(struct listitem));
251 struct lockvector *lvector=malloc(sizeof(struct lockvector));
252 litem->lockvector=lvector;
254 pthread_setspecific(threadlocks, lvector);
255 pthread_setspecific(litemkey, litem);
262 //Add our litem to list of threads
265 litem.lvector=&lvector;
276 #if defined(THREADS)||defined(STM)
277 void initthread(struct ___Thread___ * ___this___) {
285 struct sigaction sig;
287 sig.sa_handler=(void *)errorhandler;
288 sig.sa_flags=SA_RESTART;
289 sigemptyset(&sig.sa_mask);
291 /* Catch bus errors, segmentation faults, and floating point exceptions*/
292 sigaction(SIGBUS,&sig,0);
293 sigaction(SIGSEGV,&sig,0);
294 sigaction(SIGFPE,&sig,0);
297 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
298 //Add our litem to list of threads
300 struct listitem litem;
301 pthread_setspecific(litemkey, &litem);
302 struct lockvector lvector;
303 pthread_setspecific(threadlocks, &lvector);
305 litem.lvector=&lvector;
308 pthread_mutex_lock(&gclistlock);
313 pthread_mutex_unlock(&gclistlock);
315 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
317 newobjs=calloc(1, sizeof(struct objlist));
319 trec=calloc(1, sizeof(threadrec_t));
321 lockedobjs=calloc(1, sizeof(struct objlist));
323 t_cache = objstrCreate(1048576);
325 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
327 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
330 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
334 #if defined(STMARRAY)&&!defined(DUALVIEW)
337 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
340 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
343 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
345 perror("branchstack");
346 #if defined(STMARRAY)&!defined(DUALVIEW)
347 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
349 perror("arraystack");
352 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
353 objstrDelete(t_cache);
354 objstrDelete(t_reserve);
361 ___this___=(struct ___Thread___ *) p[2];
363 ___Thread______staticStart____L___Thread___(___this___);
365 ___this___->___finished___=1;
366 pthread_mutex_lock(&joinlock);
367 pthread_cond_signal(&joincond);
368 pthread_mutex_unlock(&joinlock);
370 pthread_mutex_lock(&gclistlock);
371 if (litem.prev==NULL) {
374 litem.prev->next=litem.next;
376 if (litem.next!=NULL) {
377 litem.next->prev=litem.prev;
380 pthread_cond_signal(&gccond);
381 pthread_mutex_unlock(&gclistlock);
385 #ifdef D___Thread______sleep____J
386 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
387 #if defined(THREADS)||defined(STM)
389 stopforgc((struct garbagelist *)___params___);
392 usleep(___millis___*1000);
393 #if defined(THREADS)||defined(STM)
401 #ifdef D___Thread______yield____
402 void CALL00(___Thread______yield____) {
407 #ifdef D___Thread______abort____
408 void CALL00(___Thread______abort____) {
410 _longjmp(aborttrans,1);
417 // return if the machine is dead
418 #ifdef D___Thread______nativeGetStatus____I
419 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
420 return getStatus(___mid___);
424 #ifdef D___Thread______nativeGetStatus____I
425 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
432 /* Add thread join capability */
433 #ifdef D___Thread______join____
434 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
435 unsigned int *oidarray;
436 unsigned short *versionarray, version;
438 /* Add transaction to check if thread finished for join operation */
441 ptr = transRead((unsigned int) VAR(___this___));
442 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
443 #ifdef THREADJOINDEBUG
444 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
446 if(p->___threadDone___ == 1) {
447 #ifdef THREADJOINDEBUG
448 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
454 else if( checkiftheMachineDead(p->___mid___) == 0) {
455 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
461 version = (ptr-1)->version;
462 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
463 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
467 oidarray[0] = (unsigned int) VAR(___this___);
469 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
470 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
474 versionarray[0] = version;
475 /* Request Notification */
477 stopforgc((struct garbagelist *)___params___);
481 reqNotify(oidarray, versionarray, 1,p->___mid___);
483 reqNotify(oidarray, versionarray, 1);
498 #if defined(THREADS)||defined(STM)
499 #ifdef D___Thread______nativeJoin____
500 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
501 pthread_mutex_lock(&joinlock);
502 while(!VAR(___this___)->___finished___) {
504 stopforgc((struct garbagelist *)___params___);
506 pthread_cond_wait(&joincond, &joinlock);
511 pthread_mutex_unlock(&joinlock);
515 #ifdef D___Thread______nativeCreate____
516 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
519 pthread_attr_t nattr;
521 pthread_mutex_lock(&gclistlock);
523 pthread_mutex_unlock(&gclistlock);
524 pthread_attr_init(&nattr);
525 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
527 pthread_attr_getstacksize(&nattr, &stacksize);
529 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
533 /* This next statement will likely not work on many machines */
535 pthread_attr_destroy(&nattr);
541 #ifdef D___Thread______start____I
542 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
543 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
549 void globalDestructor(void *value) {
551 pthread_setspecific(oidval, NULL);
554 void initDSMthread(int *ptr) {
561 int p[]={1, 0 /* NULL */, oid};
563 struct listitem litem;
564 pthread_setspecific(litemkey, &litem);
567 //Add our litem to list of threads
569 pthread_mutex_lock(&gclistlock);
574 pthread_mutex_unlock(&gclistlock);
576 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
578 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
580 threadData = calloc(1, sizeof(unsigned int));
581 *((unsigned int *) threadData) = oid;
582 pthread_setspecific(oidval, threadData);
583 pthread_mutex_lock(&gclistlock);
586 pthread_setspecific(threadlocks, litem.locklist);
588 if (litem.prev==NULL) {
591 litem.prev->next=litem.next;
593 if (litem.next!=NULL) {
594 litem.next->prev=litem.prev;
597 pthread_cond_signal(&gccond);
598 pthread_mutex_unlock(&gclistlock);
599 /* Add transaction to check if thread finished for join operation */
604 tmp = transRead((unsigned int) oid);
605 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
606 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
607 if(transCommit()!= 0) {
614 void startDSMthread(int oid, int objType) {
617 pthread_attr_t nattr;
619 // printf("%s -> oid : %u\n",__func__,oid);
621 pthread_mutex_lock(&gclistlock);
623 pthread_mutex_unlock(&gclistlock);
624 pthread_attr_init(&nattr);
625 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
626 int * ptr=malloc(sizeof(int)*2);
629 pthread_key_create(&oidval, globalDestructor);
632 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
637 pthread_attr_destroy(&nattr);