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_key_t macthreadid;
50 pthread_mutex_t threadnotifylock;
51 pthread_cond_t threadnotifycond;
54 #if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
56 extern __thread struct listitem litem;
58 pthread_key_t memorybasekey;
59 pthread_key_t memorytopkey;
60 pthread_key_t litemkey;
62 extern struct listitem * list;
68 unsigned int oidvalue;
74 struct lockvector *lptr=(struct lockvector *) pthread_getspecific(threadlocks);
76 struct lockvector *lptr=&lvector;
78 for(lptr->index--;lptr->index>=0;lptr->index--) {
79 if (lptr->locks[lptr->index].islastlock) {
80 struct ___Object___ *ll=lptr->locks[lptr->index].object;
85 pthread_mutex_lock(&objlock); //wake everyone up
86 pthread_cond_broadcast(&objcond);
87 pthread_mutex_unlock(&objlock);
89 pthread_mutex_lock(&gclistlock);
92 if (litem.prev==NULL) {
95 litem.prev->next=litem.next;
97 if (litem.next!=NULL) {
98 litem.next->prev=litem.prev;
102 struct listitem *litem=pthread_getspecific(litemkey);
103 if (litem->prev==NULL) {
106 litem->prev->next=litem->next;
108 if (litem->next!=NULL) {
109 litem->next->prev=litem->prev;
114 pthread_cond_signal(&gccond);
115 pthread_mutex_unlock(&gclistlock);
117 /* Add transaction to check if thread finished for join operation */
118 value = pthread_getspecific(oidval);
119 oidvalue = *((unsigned int *)value);
124 ptr = transRead(oidvalue);
125 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
126 p->___threadDone___ = 1;
127 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
128 if(transCommit() != 0) {
137 void threadhandler(int sig) {
138 printf("We just took sig=%d\n",sig);
140 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
145 void threadhandler(int sig, struct sigcontext ctx) {
150 printf("We just took sig=%d\n",sig);
152 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
153 nptrs = backtrace(buffer, 100);
155 buffer[1]=(void *)ctx.rip;
157 buffer[1]=(void *)ctx.eip;
160 strings = backtrace_symbols(buffer, nptrs);
161 if (strings == NULL) {
162 perror("backtrace_symbols");
166 for (j = 0; j < nptrs; j++)
167 printf("%s\n", strings[j]);
173 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
175 void initializethreads() {
176 struct sigaction sig;
179 pthread_mutex_init(&atomiclock, NULL);
181 pthread_mutex_init(&gclock, NULL);
182 pthread_mutex_init(&gclistlock, NULL);
183 pthread_cond_init(&gccond, NULL);
184 pthread_mutex_init(&objlock,NULL);
185 pthread_cond_init(&objcond,NULL);
186 pthread_mutex_init(&joinlock,NULL);
187 pthread_cond_init(&joincond,NULL);
189 pthread_key_create(&macthreadid, NULL);
190 pthread_key_create(&threadlocks, NULL);
191 pthread_key_create(&memorybasekey, NULL);
192 pthread_key_create(&memorytopkey, NULL);
195 initializeexithandler();
200 //deprecated use of sighandler, but apparently still works
202 sig.sa_handler=(void *)errorhandler;
205 sig.sa_handler=(void *)threadhandler;
207 sig.sa_flags=SA_RESTART;
208 sigemptyset(&sig.sa_mask);
210 /* Catch bus errors, segmentation faults, and floating point exceptions*/
211 sigaction(SIGBUS,&sig,0);
212 sigaction(SIGSEGV,&sig,0);
213 sigaction(SIGFPE,&sig,0);
214 signal(SIGPIPE, SIG_IGN);
216 newobjs=calloc(1, sizeof(struct objlist));
217 t_cache = objstrCreate(1048576);
219 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
221 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
224 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
228 #if defined(STMARRAY)&&!defined(DUALVIEW)
231 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
234 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
237 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
239 perror("branchstack");
240 #if defined(STMARRAY)&&!defined(DUALVIEW)
241 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
243 perror("arraystack");
247 trec=calloc(1, sizeof(threadrec_t));
249 lockedobjs=calloc(1, sizeof(struct objlist));
250 objlockscope = calloc(1, sizeof(objlockstate_t));
251 pthread_mutex_init(&lockedobjstore, NULL);
254 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
255 typesCausingAbort[i].numaccess = 0;
256 typesCausingAbort[i].numabort = 0;
257 typesCausingAbort[i].numtrans = 0;
263 struct listitem *litem=malloc(sizeof(struct listitem));
264 struct lockvector *lvector=malloc(sizeof(struct lockvector));
265 litem->lvector=lvector;
267 pthread_setspecific(threadlocks, lvector);
268 pthread_setspecific(macthreadid, (void *)0);
269 pthread_setspecific(litemkey, litem);
270 char ** memorybase=malloc(sizeof(char *));
272 pthread_setspecific(memorybasekey, memorybase);
273 char ** memorytop=malloc(sizeof(char *));
275 pthread_setspecific(memorytopkey, memorytop);
283 //Add our litem to list of threads
287 litem.lvector=&lvector;
298 #if defined(THREADS)||defined(STM)
301 void initthread(struct ___Thread___ * ___this___) {
309 struct sigaction sig;
311 sig.sa_handler=(void *)errorhandler;
312 sig.sa_flags=SA_RESTART;
313 sigemptyset(&sig.sa_mask);
315 /* Catch bus errors, segmentation faults, and floating point exceptions*/
316 sigaction(SIGBUS,&sig,0);
317 sigaction(SIGSEGV,&sig,0);
318 sigaction(SIGFPE,&sig,0);
321 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
322 //Add our litem to list of threads
324 struct listitem litem;
325 struct lockvector lvector;
326 char * memorybase=NULL;
327 char * memorytop=NULL;
328 pthread_setspecific(litemkey, &litem);
329 pthread_setspecific(memorybasekey, &memorybase);
330 pthread_setspecific(memorytopkey, &memorytop);
331 pthread_setspecific(threadlocks, &lvector);
333 litem.lvector=&lvector;
336 pthread_mutex_lock(&gclistlock);
338 pthread_setspecific(macthreadid, (void *)((long)(++threadcounter)));
340 mythreadid=++threadcounter;
346 pthread_mutex_unlock(&gclistlock);
348 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
350 newobjs=calloc(1, sizeof(struct objlist));
352 trec=calloc(1, sizeof(threadrec_t));
354 lockedobjs=calloc(1, sizeof(struct objlist));
356 t_cache = objstrCreate(1048576);
358 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
360 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
363 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
367 #if defined(STMARRAY)&&!defined(DUALVIEW)
370 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
373 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
376 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
378 perror("branchstack");
379 #if defined(STMARRAY)&!defined(DUALVIEW)
380 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
382 perror("arraystack");
385 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
386 objstrDelete(t_cache);
387 objstrDelete(t_reserve);
394 ___this___=(struct ___Thread___ *) p[2];
396 ___Thread______staticStart____L___Thread___(___this___);
398 ___this___->___finished___=1;
399 pthread_mutex_lock(&joinlock);
400 pthread_cond_signal(&joincond);
401 pthread_mutex_unlock(&joinlock);
403 pthread_mutex_lock(&gclistlock);
404 if (litem.prev==NULL) {
407 litem.prev->next=litem.next;
409 if (litem.next!=NULL) {
410 litem.next->prev=litem.prev;
413 pthread_cond_signal(&gccond);
414 pthread_mutex_unlock(&gclistlock);
418 #ifdef D___Thread______sleep____J
419 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
420 #if defined(THREADS)||defined(STM)
422 stopforgc((struct garbagelist *)___params___);
425 usleep(___millis___*1000);
426 #if defined(THREADS)||defined(STM)
434 #ifdef D___Thread______yield____
435 void CALL00(___Thread______yield____) {
440 #ifdef D___Thread______abort____
441 void CALL00(___Thread______abort____) {
443 _longjmp(aborttrans,1);
450 // return if the machine is dead
451 #ifdef D___Thread______nativeGetStatus____I
452 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
453 return getStatus(___mid___);
457 #ifdef D___Thread______nativeGetStatus____I
458 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
465 /* Add thread join capability */
466 #ifdef D___Thread______join____
467 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
468 unsigned int *oidarray;
469 unsigned short *versionarray, version;
471 /* Add transaction to check if thread finished for join operation */
474 ptr = transRead((unsigned int) VAR(___this___));
475 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
476 #ifdef THREADJOINDEBUG
477 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
479 if(p->___threadDone___ == 1) {
480 #ifdef THREADJOINDEBUG
481 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
487 else if( checkiftheMachineDead(p->___mid___) == 0) {
488 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
494 version = (ptr-1)->version;
495 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
496 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
500 oidarray[0] = (unsigned int) VAR(___this___);
502 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
503 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
507 versionarray[0] = version;
508 /* Request Notification */
510 stopforgc((struct garbagelist *)___params___);
514 reqNotify(oidarray, versionarray, 1,p->___mid___);
516 reqNotify(oidarray, versionarray, 1);
531 #if defined(THREADS)||defined(STM)
532 #ifdef D___Thread______nativeJoin____
533 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
534 pthread_mutex_lock(&joinlock);
535 while(!VAR(___this___)->___finished___) {
537 stopforgc((struct garbagelist *)___params___);
539 pthread_cond_wait(&joincond, &joinlock);
544 pthread_mutex_unlock(&joinlock);
548 #ifdef D___Thread______nativeCreate____
549 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
552 pthread_attr_t nattr;
554 pthread_mutex_lock(&gclistlock);
556 pthread_mutex_unlock(&gclistlock);
557 pthread_attr_init(&nattr);
558 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
560 pthread_attr_getstacksize(&nattr, &stacksize);
562 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
566 /* This next statement will likely not work on many machines */
568 pthread_attr_destroy(&nattr);
574 #ifdef D___Thread______start____I
575 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
576 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
582 void globalDestructor(void *value) {
584 pthread_setspecific(oidval, NULL);
587 void initDSMthread(int *ptr) {
594 int p[]={1, 0 /* NULL */, oid};
596 struct listitem litem;
597 pthread_setspecific(litemkey, &litem);
600 //Add our litem to list of threads
602 pthread_mutex_lock(&gclistlock);
607 pthread_mutex_unlock(&gclistlock);
609 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
611 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
613 threadData = calloc(1, sizeof(unsigned int));
614 *((unsigned int *) threadData) = oid;
615 pthread_setspecific(oidval, threadData);
616 pthread_mutex_lock(&gclistlock);
619 pthread_setspecific(threadlocks, litem.locklist);
621 if (litem.prev==NULL) {
624 litem.prev->next=litem.next;
626 if (litem.next!=NULL) {
627 litem.next->prev=litem.prev;
630 pthread_cond_signal(&gccond);
631 pthread_mutex_unlock(&gclistlock);
632 /* Add transaction to check if thread finished for join operation */
637 tmp = transRead((unsigned int) oid);
638 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
639 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
640 if(transCommit()!= 0) {
647 void startDSMthread(int oid, int objType) {
650 pthread_attr_t nattr;
652 // printf("%s -> oid : %u\n",__func__,oid);
654 pthread_mutex_lock(&gclistlock);
656 pthread_mutex_unlock(&gclistlock);
657 pthread_attr_init(&nattr);
658 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
659 int * ptr=malloc(sizeof(int)*2);
662 pthread_key_create(&oidval, globalDestructor);
665 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
670 pthread_attr_destroy(&nattr);