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 litemkey;
60 extern struct listitem * list;
66 unsigned int oidvalue;
72 struct lockvector *lptr=(struct lockvector *) pthread_getspecific(threadlocks);
74 struct lockvector *lptr=&lvector;
76 for(lptr->index--;lptr->index>=0;lptr->index--) {
77 if (lptr->locks[lptr->index].islastlock) {
78 struct ___Object___ *ll=lptr->locks[lptr->index].object;
83 pthread_mutex_lock(&objlock); //wake everyone up
84 pthread_cond_broadcast(&objcond);
85 pthread_mutex_unlock(&objlock);
87 pthread_mutex_lock(&gclistlock);
90 if (litem.prev==NULL) {
93 litem.prev->next=litem.next;
95 if (litem.next!=NULL) {
96 litem.next->prev=litem.prev;
100 struct listitem *litem=pthread_getspecific(litemkey);
101 if (litem->prev==NULL) {
104 litem->prev->next=litem->next;
106 if (litem->next!=NULL) {
107 litem->next->prev=litem->prev;
112 pthread_cond_signal(&gccond);
113 pthread_mutex_unlock(&gclistlock);
115 /* Add transaction to check if thread finished for join operation */
116 value = pthread_getspecific(oidval);
117 oidvalue = *((unsigned int *)value);
122 ptr = transRead(oidvalue);
123 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
124 p->___threadDone___ = 1;
125 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
126 if(transCommit() != 0) {
134 void threadhandler(int sig, struct sigcontext ctx) {
139 printf("We just took sig=%d\n",sig);
141 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
142 nptrs = backtrace(buffer, 100);
144 buffer[1]=(void *)ctx.rip;
146 buffer[1]=(void *)ctx.eip;
149 strings = backtrace_symbols(buffer, nptrs);
150 if (strings == NULL) {
151 perror("backtrace_symbols");
155 for (j = 0; j < nptrs; j++)
156 printf("%s\n", strings[j]);
161 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
163 void initializethreads() {
164 struct sigaction sig;
167 pthread_mutex_init(&atomiclock, NULL);
169 pthread_mutex_init(&gclock, NULL);
170 pthread_mutex_init(&gclistlock, NULL);
171 pthread_cond_init(&gccond, NULL);
172 pthread_mutex_init(&objlock,NULL);
173 pthread_cond_init(&objcond,NULL);
174 pthread_mutex_init(&joinlock,NULL);
175 pthread_cond_init(&joincond,NULL);
177 pthread_key_create(&macthreadid, 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(macthreadid, 0);
256 pthread_setspecific(litemkey, litem);
263 //Add our litem to list of threads
267 litem.lvector=&lvector;
278 #if defined(THREADS)||defined(STM)
281 void initthread(struct ___Thread___ * ___this___) {
289 struct sigaction sig;
291 sig.sa_handler=(void *)errorhandler;
292 sig.sa_flags=SA_RESTART;
293 sigemptyset(&sig.sa_mask);
295 /* Catch bus errors, segmentation faults, and floating point exceptions*/
296 sigaction(SIGBUS,&sig,0);
297 sigaction(SIGSEGV,&sig,0);
298 sigaction(SIGFPE,&sig,0);
301 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
302 //Add our litem to list of threads
304 struct listitem litem;
305 pthread_setspecific(litemkey, &litem);
306 struct lockvector lvector;
307 pthread_setspecific(threadlocks, &lvector);
309 litem.lvector=&lvector;
312 pthread_mutex_lock(&gclistlock);
314 pthread_setspecific(macthreadid, ++threadcounter);
316 mythreadid=++threadcounter;
322 pthread_mutex_unlock(&gclistlock);
324 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
326 newobjs=calloc(1, sizeof(struct objlist));
328 trec=calloc(1, sizeof(threadrec_t));
330 lockedobjs=calloc(1, sizeof(struct objlist));
332 t_cache = objstrCreate(1048576);
334 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
336 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
339 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
343 #if defined(STMARRAY)&&!defined(DUALVIEW)
346 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
349 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
352 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
354 perror("branchstack");
355 #if defined(STMARRAY)&!defined(DUALVIEW)
356 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
358 perror("arraystack");
361 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
362 objstrDelete(t_cache);
363 objstrDelete(t_reserve);
370 ___this___=(struct ___Thread___ *) p[2];
372 ___Thread______staticStart____L___Thread___(___this___);
374 ___this___->___finished___=1;
375 pthread_mutex_lock(&joinlock);
376 pthread_cond_signal(&joincond);
377 pthread_mutex_unlock(&joinlock);
379 pthread_mutex_lock(&gclistlock);
380 if (litem.prev==NULL) {
383 litem.prev->next=litem.next;
385 if (litem.next!=NULL) {
386 litem.next->prev=litem.prev;
389 pthread_cond_signal(&gccond);
390 pthread_mutex_unlock(&gclistlock);
394 #ifdef D___Thread______sleep____J
395 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
396 #if defined(THREADS)||defined(STM)
398 stopforgc((struct garbagelist *)___params___);
401 usleep(___millis___*1000);
402 #if defined(THREADS)||defined(STM)
410 #ifdef D___Thread______yield____
411 void CALL00(___Thread______yield____) {
416 #ifdef D___Thread______abort____
417 void CALL00(___Thread______abort____) {
419 _longjmp(aborttrans,1);
426 // return if the machine is dead
427 #ifdef D___Thread______nativeGetStatus____I
428 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
429 return getStatus(___mid___);
433 #ifdef D___Thread______nativeGetStatus____I
434 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
441 /* Add thread join capability */
442 #ifdef D___Thread______join____
443 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
444 unsigned int *oidarray;
445 unsigned short *versionarray, version;
447 /* Add transaction to check if thread finished for join operation */
450 ptr = transRead((unsigned int) VAR(___this___));
451 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
452 #ifdef THREADJOINDEBUG
453 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
455 if(p->___threadDone___ == 1) {
456 #ifdef THREADJOINDEBUG
457 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
463 else if( checkiftheMachineDead(p->___mid___) == 0) {
464 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
470 version = (ptr-1)->version;
471 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
472 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
476 oidarray[0] = (unsigned int) VAR(___this___);
478 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
479 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
483 versionarray[0] = version;
484 /* Request Notification */
486 stopforgc((struct garbagelist *)___params___);
490 reqNotify(oidarray, versionarray, 1,p->___mid___);
492 reqNotify(oidarray, versionarray, 1);
507 #if defined(THREADS)||defined(STM)
508 #ifdef D___Thread______nativeJoin____
509 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
510 pthread_mutex_lock(&joinlock);
511 while(!VAR(___this___)->___finished___) {
513 stopforgc((struct garbagelist *)___params___);
515 pthread_cond_wait(&joincond, &joinlock);
520 pthread_mutex_unlock(&joinlock);
524 #ifdef D___Thread______nativeCreate____
525 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
528 pthread_attr_t nattr;
530 pthread_mutex_lock(&gclistlock);
532 pthread_mutex_unlock(&gclistlock);
533 pthread_attr_init(&nattr);
534 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
536 pthread_attr_getstacksize(&nattr, &stacksize);
538 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
542 /* This next statement will likely not work on many machines */
544 pthread_attr_destroy(&nattr);
550 #ifdef D___Thread______start____I
551 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
552 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
558 void globalDestructor(void *value) {
560 pthread_setspecific(oidval, NULL);
563 void initDSMthread(int *ptr) {
570 int p[]={1, 0 /* NULL */, oid};
572 struct listitem litem;
573 pthread_setspecific(litemkey, &litem);
576 //Add our litem to list of threads
578 pthread_mutex_lock(&gclistlock);
583 pthread_mutex_unlock(&gclistlock);
585 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
587 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
589 threadData = calloc(1, sizeof(unsigned int));
590 *((unsigned int *) threadData) = oid;
591 pthread_setspecific(oidval, threadData);
592 pthread_mutex_lock(&gclistlock);
595 pthread_setspecific(threadlocks, litem.locklist);
597 if (litem.prev==NULL) {
600 litem.prev->next=litem.next;
602 if (litem.next!=NULL) {
603 litem.next->prev=litem.prev;
606 pthread_cond_signal(&gccond);
607 pthread_mutex_unlock(&gclistlock);
608 /* Add transaction to check if thread finished for join operation */
613 tmp = transRead((unsigned int) oid);
614 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
615 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
616 if(transCommit()!= 0) {
623 void startDSMthread(int oid, int objType) {
626 pthread_attr_t nattr;
628 // printf("%s -> oid : %u\n",__func__,oid);
630 pthread_mutex_lock(&gclistlock);
632 pthread_mutex_unlock(&gclistlock);
633 pthread_attr_init(&nattr);
634 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
635 int * ptr=malloc(sizeof(int)*2);
638 pthread_key_create(&oidval, globalDestructor);
641 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
646 pthread_attr_destroy(&nattr);