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 void initthread(struct ___Thread___ * ___this___) {
314 struct sigaction sig;
316 sig.sa_handler=(void *)errorhandler;
317 sig.sa_flags=SA_RESTART;
318 sigemptyset(&sig.sa_mask);
320 /* Catch bus errors, segmentation faults, and floating point exceptions*/
321 sigaction(SIGBUS,&sig,0);
322 sigaction(SIGSEGV,&sig,0);
323 sigaction(SIGFPE,&sig,0);
326 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
327 //Add our litem to list of threads
329 struct listitem litem;
330 struct lockvector lvector;
331 char * memorybase=NULL;
332 char * memorytop=NULL;
333 pthread_setspecific(litemkey, &litem);
334 pthread_setspecific(memorybasekey, &memorybase);
335 pthread_setspecific(memorytopkey, &memorytop);
336 pthread_setspecific(threadlocks, &lvector);
339 litem.jnirefs=&jnirefs;
342 litem.lvector=&lvector;
345 pthread_mutex_lock(&gclistlock);
347 pthread_setspecific(macthreadid, (void *)((long)(++threadcounter)));
349 mythreadid=++threadcounter;
355 pthread_mutex_unlock(&gclistlock);
357 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
359 newobjs=calloc(1, sizeof(struct objlist));
361 trec=calloc(1, sizeof(threadrec_t));
363 lockedobjs=calloc(1, sizeof(struct objlist));
365 t_cache = objstrCreate(1048576);
367 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
369 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
372 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
376 #if defined(STMARRAY)&&!defined(DUALVIEW)
379 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
382 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
385 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
387 perror("branchstack");
388 #if defined(STMARRAY)&!defined(DUALVIEW)
389 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
391 perror("arraystack");
394 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
395 objstrDelete(t_cache);
396 objstrDelete(t_reserve);
403 ___this___=(struct ___Thread___ *) p[2];
405 ___Thread______staticStart____L___Thread___(___this___);
407 ___this___->___finished___=1;
408 pthread_mutex_lock(&joinlock);
409 pthread_cond_signal(&joincond);
410 pthread_mutex_unlock(&joinlock);
412 pthread_mutex_lock(&gclistlock);
413 if (litem.prev==NULL) {
416 litem.prev->next=litem.next;
418 if (litem.next!=NULL) {
419 litem.next->prev=litem.prev;
422 pthread_cond_signal(&gccond);
423 pthread_mutex_unlock(&gclistlock);
427 #ifdef D___Thread______sleep____J
428 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
429 #if defined(THREADS)||defined(STM)
431 stopforgc((struct garbagelist *)___params___);
434 usleep(___millis___*1000);
435 #if defined(THREADS)||defined(STM)
443 #ifdef D___Thread______yield____
444 void CALL00(___Thread______yield____) {
449 #ifdef D___Thread______abort____
450 void CALL00(___Thread______abort____) {
452 _longjmp(aborttrans,1);
459 // return if the machine is dead
460 #ifdef D___Thread______nativeGetStatus____I
461 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
462 return getStatus(___mid___);
466 #ifdef D___Thread______nativeGetStatus____I
467 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
474 /* Add thread join capability */
475 #ifdef D___Thread______join____
476 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
477 unsigned int *oidarray;
478 unsigned short *versionarray, version;
480 /* Add transaction to check if thread finished for join operation */
483 ptr = transRead((unsigned int) VAR(___this___));
484 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
485 #ifdef THREADJOINDEBUG
486 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
488 if(p->___threadDone___ == 1) {
489 #ifdef THREADJOINDEBUG
490 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
496 else if( checkiftheMachineDead(p->___mid___) == 0) {
497 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
503 version = (ptr-1)->version;
504 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
505 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
509 oidarray[0] = (unsigned int) VAR(___this___);
511 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
512 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
516 versionarray[0] = version;
517 /* Request Notification */
519 stopforgc((struct garbagelist *)___params___);
523 reqNotify(oidarray, versionarray, 1,p->___mid___);
525 reqNotify(oidarray, versionarray, 1);
540 #if defined(THREADS)||defined(STM)
541 #ifdef D___Thread______nativeJoin____
542 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
543 pthread_mutex_lock(&joinlock);
544 while(!VAR(___this___)->___finished___) {
546 stopforgc((struct garbagelist *)___params___);
548 pthread_cond_wait(&joincond, &joinlock);
553 pthread_mutex_unlock(&joinlock);
557 #ifdef D___Thread______nativeCreate____
558 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
561 pthread_attr_t nattr;
563 pthread_mutex_lock(&gclistlock);
565 pthread_mutex_unlock(&gclistlock);
566 pthread_attr_init(&nattr);
567 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
569 pthread_attr_getstacksize(&nattr, &stacksize);
571 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
575 /* This next statement will likely not work on many machines */
577 pthread_attr_destroy(&nattr);
583 #ifdef D___Thread______start____I
584 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
585 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
591 void globalDestructor(void *value) {
593 pthread_setspecific(oidval, NULL);
596 void initDSMthread(int *ptr) {
603 int p[]={1, 0 /* NULL */, oid};
605 struct listitem litem;
606 pthread_setspecific(litemkey, &litem);
609 //Add our litem to list of threads
611 pthread_mutex_lock(&gclistlock);
616 pthread_mutex_unlock(&gclistlock);
618 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
620 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
622 threadData = calloc(1, sizeof(unsigned int));
623 *((unsigned int *) threadData) = oid;
624 pthread_setspecific(oidval, threadData);
625 pthread_mutex_lock(&gclistlock);
628 pthread_setspecific(threadlocks, litem.locklist);
630 if (litem.prev==NULL) {
633 litem.prev->next=litem.next;
635 if (litem.next!=NULL) {
636 litem.next->prev=litem.prev;
639 pthread_cond_signal(&gccond);
640 pthread_mutex_unlock(&gclistlock);
641 /* Add transaction to check if thread finished for join operation */
646 tmp = transRead((unsigned int) oid);
647 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
648 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
649 if(transCommit()!= 0) {
656 void startDSMthread(int oid, int objType) {
659 pthread_attr_t nattr;
661 // printf("%s -> oid : %u\n",__func__,oid);
663 pthread_mutex_lock(&gclistlock);
665 pthread_mutex_unlock(&gclistlock);
666 pthread_attr_init(&nattr);
667 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
668 int * ptr=malloc(sizeof(int)*2);
671 pthread_key_create(&oidval, globalDestructor);
674 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
679 pthread_attr_destroy(&nattr);