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);
193 pthread_key_create(&litemkey, NULL);
196 initializeexithandler();
201 //deprecated use of sighandler, but apparently still works
203 sig.sa_handler=(void *)errorhandler;
206 sig.sa_handler=(void *)threadhandler;
208 sig.sa_flags=SA_RESTART;
209 sigemptyset(&sig.sa_mask);
211 /* Catch bus errors, segmentation faults, and floating point exceptions*/
212 sigaction(SIGBUS,&sig,0);
213 sigaction(SIGSEGV,&sig,0);
214 sigaction(SIGFPE,&sig,0);
215 signal(SIGPIPE, SIG_IGN);
217 newobjs=calloc(1, sizeof(struct objlist));
218 t_cache = objstrCreate(1048576);
220 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
222 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
225 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
229 #if defined(STMARRAY)&&!defined(DUALVIEW)
232 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
235 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
238 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
240 perror("branchstack");
241 #if defined(STMARRAY)&&!defined(DUALVIEW)
242 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
244 perror("arraystack");
248 trec=calloc(1, sizeof(threadrec_t));
250 lockedobjs=calloc(1, sizeof(struct objlist));
251 objlockscope = calloc(1, sizeof(objlockstate_t));
252 pthread_mutex_init(&lockedobjstore, NULL);
255 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
256 typesCausingAbort[i].numaccess = 0;
257 typesCausingAbort[i].numabort = 0;
258 typesCausingAbort[i].numtrans = 0;
264 struct listitem *litem=malloc(sizeof(struct listitem));
265 struct lockvector *lvector=malloc(sizeof(struct lockvector));
266 litem->lvector=lvector;
268 pthread_setspecific(threadlocks, lvector);
269 pthread_setspecific(macthreadid, (void *)0);
270 pthread_setspecific(litemkey, litem);
271 char ** memorybase=malloc(sizeof(char *));
273 pthread_setspecific(memorybasekey, memorybase);
274 char ** memorytop=malloc(sizeof(char *));
276 pthread_setspecific(memorytopkey, memorytop);
284 //Add our litem to list of threads
288 litem.lvector=&lvector;
290 litem.jnirefs=&jnirefs;
303 #if defined(THREADS)||defined(STM)
306 #ifdef D___Thread______nativeCreate____
307 void initthread(struct ___Thread___ * ___this___) {
315 struct sigaction sig;
317 sig.sa_handler=(void *)errorhandler;
318 sig.sa_flags=SA_RESTART;
319 sigemptyset(&sig.sa_mask);
321 /* Catch bus errors, segmentation faults, and floating point exceptions*/
322 sigaction(SIGBUS,&sig,0);
323 sigaction(SIGSEGV,&sig,0);
324 sigaction(SIGFPE,&sig,0);
327 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
328 //Add our litem to list of threads
330 struct listitem litem;
331 struct lockvector lvector;
332 char * memorybase=NULL;
333 char * memorytop=NULL;
334 pthread_setspecific(litemkey, &litem);
335 pthread_setspecific(memorybasekey, &memorybase);
336 pthread_setspecific(memorytopkey, &memorytop);
337 pthread_setspecific(threadlocks, &lvector);
340 litem.jnirefs=&jnirefs;
343 litem.lvector=&lvector;
346 pthread_mutex_lock(&gclistlock);
348 pthread_setspecific(macthreadid, (void *)((long)(++threadcounter)));
350 mythreadid=++threadcounter;
356 pthread_mutex_unlock(&gclistlock);
358 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
360 newobjs=calloc(1, sizeof(struct objlist));
362 trec=calloc(1, sizeof(threadrec_t));
364 lockedobjs=calloc(1, sizeof(struct objlist));
366 t_cache = objstrCreate(1048576);
368 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
370 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
373 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
377 #if defined(STMARRAY)&&!defined(DUALVIEW)
380 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
383 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
386 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
388 perror("branchstack");
389 #if defined(STMARRAY)&!defined(DUALVIEW)
390 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
392 perror("arraystack");
395 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
396 objstrDelete(t_cache);
397 objstrDelete(t_reserve);
404 ___this___=(struct ___Thread___ *) p[2];
406 ___Thread______staticStart____L___Thread___(___this___);
408 ___this___->___finished___=1;
409 pthread_mutex_lock(&joinlock);
410 pthread_cond_signal(&joincond);
411 pthread_mutex_unlock(&joinlock);
413 pthread_mutex_lock(&gclistlock);
414 if (litem.prev==NULL) {
417 litem.prev->next=litem.next;
419 if (litem.next!=NULL) {
420 litem.next->prev=litem.prev;
423 pthread_cond_signal(&gccond);
424 pthread_mutex_unlock(&gclistlock);
429 #ifdef D___Thread______sleep____J
430 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
431 #if defined(THREADS)||defined(STM)
433 stopforgc((struct garbagelist *)___params___);
436 usleep(___millis___*1000);
437 #if defined(THREADS)||defined(STM)
445 #ifdef D___Thread______yield____
446 void CALL00(___Thread______yield____) {
451 #ifdef D___Thread______abort____
452 void CALL00(___Thread______abort____) {
454 _longjmp(aborttrans,1);
461 // return if the machine is dead
462 #ifdef D___Thread______nativeGetStatus____I
463 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
464 return getStatus(___mid___);
468 #ifdef D___Thread______nativeGetStatus____I
469 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
476 /* Add thread join capability */
477 #ifdef D___Thread______join____
478 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
479 unsigned int *oidarray;
480 unsigned short *versionarray, version;
482 /* Add transaction to check if thread finished for join operation */
485 ptr = transRead((unsigned int) VAR(___this___));
486 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
487 #ifdef THREADJOINDEBUG
488 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
490 if(p->___threadDone___ == 1) {
491 #ifdef THREADJOINDEBUG
492 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
498 else if( checkiftheMachineDead(p->___mid___) == 0) {
499 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
505 version = (ptr-1)->version;
506 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
507 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
511 oidarray[0] = (unsigned int) VAR(___this___);
513 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
514 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
518 versionarray[0] = version;
519 /* Request Notification */
521 stopforgc((struct garbagelist *)___params___);
525 reqNotify(oidarray, versionarray, 1,p->___mid___);
527 reqNotify(oidarray, versionarray, 1);
542 #if defined(THREADS)||defined(STM)
543 #ifdef D___Thread______nativeJoin____
544 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
545 pthread_mutex_lock(&joinlock);
546 while(!VAR(___this___)->___finished___) {
548 stopforgc((struct garbagelist *)___params___);
550 pthread_cond_wait(&joincond, &joinlock);
555 pthread_mutex_unlock(&joinlock);
559 #ifdef D___Thread______nativeCreate____
560 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
563 pthread_attr_t nattr;
565 pthread_mutex_lock(&gclistlock);
567 pthread_mutex_unlock(&gclistlock);
568 pthread_attr_init(&nattr);
569 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
571 pthread_attr_getstacksize(&nattr, &stacksize);
573 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
577 /* This next statement will likely not work on many machines */
579 pthread_attr_destroy(&nattr);
585 #ifdef D___Thread______start____I
586 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
587 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
593 void globalDestructor(void *value) {
595 pthread_setspecific(oidval, NULL);
598 void initDSMthread(int *ptr) {
605 int p[]={1, 0 /* NULL */, oid};
607 struct listitem litem;
608 pthread_setspecific(litemkey, &litem);
611 //Add our litem to list of threads
613 pthread_mutex_lock(&gclistlock);
618 pthread_mutex_unlock(&gclistlock);
620 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
622 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
624 threadData = calloc(1, sizeof(unsigned int));
625 *((unsigned int *) threadData) = oid;
626 pthread_setspecific(oidval, threadData);
627 pthread_mutex_lock(&gclistlock);
630 pthread_setspecific(threadlocks, litem.locklist);
632 if (litem.prev==NULL) {
635 litem.prev->next=litem.next;
637 if (litem.next!=NULL) {
638 litem.next->prev=litem.prev;
641 pthread_cond_signal(&gccond);
642 pthread_mutex_unlock(&gclistlock);
643 /* Add transaction to check if thread finished for join operation */
648 tmp = transRead((unsigned int) oid);
649 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
650 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
651 if(transCommit()!= 0) {
658 void startDSMthread(int oid, int objType) {
661 pthread_attr_t nattr;
663 // printf("%s -> oid : %u\n",__func__,oid);
665 pthread_mutex_lock(&gclistlock);
667 pthread_mutex_unlock(&gclistlock);
668 pthread_attr_init(&nattr);
669 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
670 int * ptr=malloc(sizeof(int)*2);
673 pthread_key_create(&oidval, globalDestructor);
676 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
681 pthread_attr_destroy(&nattr);