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;
82 pthread_mutex_lock(&objlock); //wake everyone up
83 pthread_cond_broadcast(&objcond);
84 pthread_mutex_unlock(&objlock);
86 pthread_mutex_lock(&gclistlock);
89 if (litem.prev==NULL) {
92 litem.prev->next=litem.next;
94 if (litem.next!=NULL) {
95 litem.next->prev=litem.prev;
99 struct listitem *litem=pthread_getspecific(litemkey);
100 if (litem->prev==NULL) {
103 litem->prev->next=litem->next;
105 if (litem->next!=NULL) {
106 litem->next->prev=litem->prev;
111 pthread_cond_signal(&gccond);
112 pthread_mutex_unlock(&gclistlock);
114 /* Add transaction to check if thread finished for join operation */
115 value = pthread_getspecific(oidval);
116 oidvalue = *((unsigned int *)value);
121 ptr = transRead(oidvalue);
122 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
123 p->___threadDone___ = 1;
124 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
125 if(transCommit() != 0) {
133 void threadhandler(int sig, struct sigcontext ctx) {
138 printf("We just took sig=%d\n",sig);
140 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
141 nptrs = backtrace(buffer, 100);
143 buffer[1]=(void *)ctx.rip;
145 buffer[1]=(void *)ctx.eip;
148 strings = backtrace_symbols(buffer, nptrs);
149 if (strings == NULL) {
150 perror("backtrace_symbols");
154 for (j = 0; j < nptrs; j++)
155 printf("%s\n", strings[j]);
160 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
162 void initializethreads() {
163 struct sigaction sig;
166 pthread_mutex_init(&atomiclock, NULL);
168 pthread_mutex_init(&gclock, NULL);
169 pthread_mutex_init(&gclistlock, NULL);
170 pthread_cond_init(&gccond, NULL);
171 pthread_mutex_init(&objlock,NULL);
172 pthread_cond_init(&objcond,NULL);
173 pthread_mutex_init(&joinlock,NULL);
174 pthread_cond_init(&joincond,NULL);
176 pthread_key_create(&threadlocks, NULL);
177 pthread_key_create(&litem, NULL);
180 initializeexithandler();
185 //deprecated use of sighandler, but apparently still works
187 sig.sa_handler=(void *)errorhandler;
190 sig.sa_handler=(void *)threadhandler;
192 sig.sa_flags=SA_RESTART;
193 sigemptyset(&sig.sa_mask);
195 /* Catch bus errors, segmentation faults, and floating point exceptions*/
196 sigaction(SIGBUS,&sig,0);
197 sigaction(SIGSEGV,&sig,0);
198 sigaction(SIGFPE,&sig,0);
199 signal(SIGPIPE, SIG_IGN);
201 newobjs=calloc(1, sizeof(struct objlist));
202 t_cache = objstrCreate(1048576);
204 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
206 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
209 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
213 #if defined(STMARRAY)&&!defined(DUALVIEW)
216 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
219 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
222 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
224 perror("branchstack");
225 #if defined(STMARRAY)&&!defined(DUALVIEW)
226 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
228 perror("arraystack");
232 trec=calloc(1, sizeof(threadrec_t));
234 lockedobjs=calloc(1, sizeof(struct objlist));
235 objlockscope = calloc(1, sizeof(objlockstate_t));
236 pthread_mutex_init(&lockedobjstore, NULL);
239 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
240 typesCausingAbort[i].numaccess = 0;
241 typesCausingAbort[i].numabort = 0;
242 typesCausingAbort[i].numtrans = 0;
248 struct listitem *litem=malloc(sizeof(struct listitem));
249 struct lockvector *lvector=malloc(sizeof(struct lockvector));
250 litem->lockvector=lvector;
252 pthread_setspecific(threadlocks, lvector);
253 pthread_setspecific(litemkey, litem);
260 //Add our litem to list of threads
263 litem.lvector=&lvector;
274 #if defined(THREADS)||defined(STM)
275 void initthread(struct ___Thread___ * ___this___) {
283 struct sigaction sig;
285 sig.sa_handler=(void *)errorhandler;
286 sig.sa_flags=SA_RESTART;
287 sigemptyset(&sig.sa_mask);
289 /* Catch bus errors, segmentation faults, and floating point exceptions*/
290 sigaction(SIGBUS,&sig,0);
291 sigaction(SIGSEGV,&sig,0);
292 sigaction(SIGFPE,&sig,0);
295 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
296 //Add our litem to list of threads
298 struct listitem litem;
299 pthread_setspecific(litemkey, &litem);
300 struct lockvector lvector;
301 pthread_setspecific(threadlocks, &lvector);
303 litem.lvector=&lvector;
306 pthread_mutex_lock(&gclistlock);
311 pthread_mutex_unlock(&gclistlock);
313 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
315 newobjs=calloc(1, sizeof(struct objlist));
317 trec=calloc(1, sizeof(threadrec_t));
319 lockedobjs=calloc(1, sizeof(struct objlist));
321 t_cache = objstrCreate(1048576);
323 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
325 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
328 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
332 #if defined(STMARRAY)&&!defined(DUALVIEW)
335 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
338 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
341 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
343 perror("branchstack");
344 #if defined(STMARRAY)&!defined(DUALVIEW)
345 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
347 perror("arraystack");
350 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
351 objstrDelete(t_cache);
352 objstrDelete(t_reserve);
359 ___this___=(struct ___Thread___ *) p[2];
361 ___Thread______staticStart____L___Thread___(___this___);
363 ___this___->___finished___=1;
364 pthread_mutex_lock(&joinlock);
365 pthread_cond_signal(&joincond);
366 pthread_mutex_unlock(&joinlock);
368 pthread_mutex_lock(&gclistlock);
369 if (litem.prev==NULL) {
372 litem.prev->next=litem.next;
374 if (litem.next!=NULL) {
375 litem.next->prev=litem.prev;
378 pthread_cond_signal(&gccond);
379 pthread_mutex_unlock(&gclistlock);
383 #ifdef D___Thread______sleep____J
384 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
385 #if defined(THREADS)||defined(STM)
387 stopforgc((struct garbagelist *)___params___);
390 usleep(___millis___*1000);
391 #if defined(THREADS)||defined(STM)
399 #ifdef D___Thread______yield____
400 void CALL00(___Thread______yield____) {
405 #ifdef D___Thread______abort____
406 void CALL00(___Thread______abort____) {
408 _longjmp(aborttrans,1);
415 // return if the machine is dead
416 #ifdef D___Thread______nativeGetStatus____I
417 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
418 return getStatus(___mid___);
422 #ifdef D___Thread______nativeGetStatus____I
423 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
430 /* Add thread join capability */
431 #ifdef D___Thread______join____
432 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
433 unsigned int *oidarray;
434 unsigned short *versionarray, version;
436 /* Add transaction to check if thread finished for join operation */
439 ptr = transRead((unsigned int) VAR(___this___));
440 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
441 #ifdef THREADJOINDEBUG
442 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
444 if(p->___threadDone___ == 1) {
445 #ifdef THREADJOINDEBUG
446 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
452 else if( checkiftheMachineDead(p->___mid___) == 0) {
453 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
459 version = (ptr-1)->version;
460 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
461 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
465 oidarray[0] = (unsigned int) VAR(___this___);
467 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
468 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
472 versionarray[0] = version;
473 /* Request Notification */
475 stopforgc((struct garbagelist *)___params___);
479 reqNotify(oidarray, versionarray, 1,p->___mid___);
481 reqNotify(oidarray, versionarray, 1);
496 #if defined(THREADS)||defined(STM)
497 #ifdef D___Thread______nativeJoin____
498 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
499 pthread_mutex_lock(&joinlock);
500 while(!VAR(___this___)->___finished___) {
502 stopforgc((struct garbagelist *)___params___);
504 pthread_cond_wait(&joincond, &joinlock);
509 pthread_mutex_unlock(&joinlock);
513 #ifdef D___Thread______nativeCreate____
514 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
517 pthread_attr_t nattr;
519 pthread_mutex_lock(&gclistlock);
521 pthread_mutex_unlock(&gclistlock);
522 pthread_attr_init(&nattr);
523 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
525 pthread_attr_getstacksize(&nattr, &stacksize);
527 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
531 /* This next statement will likely not work on many machines */
533 pthread_attr_destroy(&nattr);
539 #ifdef D___Thread______start____I
540 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
541 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
547 void globalDestructor(void *value) {
549 pthread_setspecific(oidval, NULL);
552 void initDSMthread(int *ptr) {
559 int p[]={1, 0 /* NULL */, oid};
561 struct listitem litem;
562 pthread_setspecific(litemkey, &litem);
565 //Add our litem to list of threads
567 pthread_mutex_lock(&gclistlock);
572 pthread_mutex_unlock(&gclistlock);
574 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
576 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
578 threadData = calloc(1, sizeof(unsigned int));
579 *((unsigned int *) threadData) = oid;
580 pthread_setspecific(oidval, threadData);
581 pthread_mutex_lock(&gclistlock);
584 pthread_setspecific(threadlocks, litem.locklist);
586 if (litem.prev==NULL) {
589 litem.prev->next=litem.next;
591 if (litem.next!=NULL) {
592 litem.next->prev=litem.prev;
595 pthread_cond_signal(&gccond);
596 pthread_mutex_unlock(&gclistlock);
597 /* Add transaction to check if thread finished for join operation */
602 tmp = transRead((unsigned int) oid);
603 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
604 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
605 if(transCommit()!= 0) {
612 void startDSMthread(int oid, int objType) {
615 pthread_attr_t nattr;
617 // printf("%s -> oid : %u\n",__func__,oid);
619 pthread_mutex_lock(&gclistlock);
621 pthread_mutex_unlock(&gclistlock);
622 pthread_attr_init(&nattr);
623 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
624 int * ptr=malloc(sizeof(int)*2);
627 pthread_key_create(&oidval, globalDestructor);
630 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
635 pthread_attr_destroy(&nattr);