13 #include <DSTM/interface_recovery/dstm.h>
14 #include <DSTM/interface_recovery/llookup.h>
16 #include <DSTM/interface/dstm.h>
17 #include <DSTM/interface/llookup.h>
31 pthread_mutex_t gclock;
32 pthread_mutex_t gclistlock;
33 pthread_cond_t gccond;
34 pthread_mutex_t objlock;
35 pthread_cond_t objcond;
37 pthread_mutex_t atomiclock;
39 pthread_mutex_t joinlock;
40 pthread_cond_t joincond;
41 pthread_key_t threadlocks;
42 pthread_mutex_t threadnotifylock;
43 pthread_cond_t threadnotifycond;
46 #if defined(THREADS) || defined(DSTM) || defined(STM)
48 extern __thread struct listitem litem;
50 pthread_key_t litemkey;
52 extern struct listitem * list;
58 unsigned int oidvalue;
63 struct ___Object___ *ll=pthread_getspecific(threadlocks);
65 struct ___Object___ *llnext=ll->___nextlockobject___;
66 ll->___nextlockobject___=NULL;
67 ll->___prevlockobject___=NULL;
69 ll->tid=0; //unlock it
72 pthread_mutex_lock(&objlock); //wake everyone up
73 pthread_cond_broadcast(&objcond);
74 pthread_mutex_unlock(&objlock);
76 pthread_mutex_lock(&gclistlock);
78 pthread_setspecific(threadlocks, litem.locklist);
81 if (litem.prev==NULL) {
84 litem.prev->next=litem.next;
86 if (litem.next!=NULL) {
87 litem.next->prev=litem.prev;
91 struct listitem *litem=pthread_getspecific(litemkey);
92 if (litem->prev==NULL) {
95 litem->prev->next=litem->next;
97 if (litem->next!=NULL) {
98 litem->next->prev=litem->prev;
103 pthread_cond_signal(&gccond);
104 pthread_mutex_unlock(&gclistlock);
106 /* Add transaction to check if thread finished for join operation */
107 value = pthread_getspecific(oidval);
108 oidvalue = *((unsigned int *)value);
113 ptr = transRead(oidvalue);
114 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
115 p->___threadDone___ = 1;
116 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
117 if(transCommit() != 0) {
125 void threadhandler(int sig, struct sigcontext ctx) {
130 printf("We just took sig=%d\n",sig);
132 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
133 nptrs = backtrace(buffer, 100);
135 buffer[1]=(void *)ctx.rip;
137 buffer[1]=(void *)ctx.eip;
140 strings = backtrace_symbols(buffer, nptrs);
141 if (strings == NULL) {
142 perror("backtrace_symbols");
146 for (j = 0; j < nptrs; j++)
147 printf("%s\n", strings[j]);
152 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
154 void initializethreads() {
155 struct sigaction sig;
158 pthread_mutex_init(&atomiclock, NULL);
160 pthread_mutex_init(&gclock, NULL);
161 pthread_mutex_init(&gclistlock, NULL);
162 pthread_cond_init(&gccond, NULL);
163 pthread_mutex_init(&objlock,NULL);
164 pthread_cond_init(&objcond,NULL);
165 pthread_mutex_init(&joinlock,NULL);
166 pthread_cond_init(&joincond,NULL);
167 pthread_key_create(&threadlocks, NULL);
169 pthread_key_create(&litem, NULL);
172 initializeexithandler();
177 //deprecated use of sighandler, but apparently still works
179 sig.sa_handler=(void *)errorhandler;
182 sig.sa_handler=(void *)threadhandler;
184 sig.sa_flags=SA_RESTART;
185 sigemptyset(&sig.sa_mask);
187 /* Catch bus errors, segmentation faults, and floating point exceptions*/
188 sigaction(SIGBUS,&sig,0);
189 sigaction(SIGSEGV,&sig,0);
190 sigaction(SIGFPE,&sig,0);
191 signal(SIGPIPE, SIG_IGN);
193 newobjs=calloc(1, sizeof(struct objlist));
194 t_cache = objstrCreate(1048576);
196 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
198 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
201 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
205 #if defined(STMARRAY)&&!defined(DUALVIEW)
208 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
211 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
214 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
216 perror("branchstack");
217 #if defined(STMARRAY)&&!defined(DUALVIEW)
218 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
220 perror("arraystack");
224 trec=calloc(1, sizeof(threadrec_t));
226 lockedobjs=calloc(1, sizeof(struct objlist));
227 objlockscope = calloc(1, sizeof(objlockstate_t));
228 pthread_mutex_init(&lockedobjstore, NULL);
231 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
232 typesCausingAbort[i].numaccess = 0;
233 typesCausingAbort[i].numabort = 0;
234 typesCausingAbort[i].numtrans = 0;
240 struct listitem *litem=malloc(sizeof(struct listitem));
241 pthread_setspecific(litemkey, litem);
248 //Add our litem to list of threads
257 #if defined(THREADS)||defined(STM)
258 void initthread(struct ___Thread___ * ___this___) {
263 struct sigaction sig;
265 sig.sa_handler=(void *)errorhandler;
266 sig.sa_flags=SA_RESTART;
267 sigemptyset(&sig.sa_mask);
269 /* Catch bus errors, segmentation faults, and floating point exceptions*/
270 sigaction(SIGBUS,&sig,0);
271 sigaction(SIGSEGV,&sig,0);
272 sigaction(SIGFPE,&sig,0);
275 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
276 //Add our litem to list of threads
278 struct listitem litem;
279 pthread_setspecific(litemkey, &litem);
282 pthread_mutex_lock(&gclistlock);
287 pthread_mutex_unlock(&gclistlock);
290 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
292 newobjs=calloc(1, sizeof(struct objlist));
294 trec=calloc(1, sizeof(threadrec_t));
296 lockedobjs=calloc(1, sizeof(struct objlist));
298 t_cache = objstrCreate(1048576);
300 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
302 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
305 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
309 #if defined(STMARRAY)&&!defined(DUALVIEW)
312 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
315 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
318 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
320 perror("branchstack");
321 #if defined(STMARRAY)&!defined(DUALVIEW)
322 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
324 perror("arraystack");
327 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
328 objstrDelete(t_cache);
329 objstrDelete(t_reserve);
336 ___this___=(struct ___Thread___ *) p[2];
338 ___Thread______staticStart____L___Thread___(___this___);
340 ___this___->___finished___=1;
341 pthread_mutex_lock(&joinlock);
342 pthread_cond_signal(&joincond);
343 pthread_mutex_unlock(&joinlock);
345 pthread_mutex_lock(&gclistlock);
347 pthread_setspecific(threadlocks, litem.locklist);
349 if (litem.prev==NULL) {
352 litem.prev->next=litem.next;
354 if (litem.next!=NULL) {
355 litem.next->prev=litem.prev;
358 pthread_cond_signal(&gccond);
359 pthread_mutex_unlock(&gclistlock);
363 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
364 #if defined(THREADS)||defined(STM)
366 stopforgc((struct garbagelist *)___params___);
369 usleep(___millis___);
370 #if defined(THREADS)||defined(STM)
377 #if defined(DSTM)|| defined(THREADS)||defined(STM)
378 void CALL00(___Thread______yield____) {
383 #ifdef D___Thread______abort____
384 void CALL00(___Thread______abort____) {
386 _longjmp(aborttrans,1);
393 // return if the machine is dead
394 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
395 return getStatus(___mid___);
398 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
404 /* Add thread join capability */
405 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
406 unsigned int *oidarray;
407 unsigned short *versionarray, version;
409 /* Add transaction to check if thread finished for join operation */
412 ptr = transRead((unsigned int) VAR(___this___));
413 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
414 #ifdef THREADJOINDEBUG
415 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
417 if(p->___threadDone___ == 1) {
418 #ifdef THREADJOINDEBUG
419 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
425 else if( checkiftheMachineDead(p->___mid___) == 0) {
426 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
432 version = (ptr-1)->version;
433 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
434 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
438 oidarray[0] = (unsigned int) VAR(___this___);
440 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
441 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
445 versionarray[0] = version;
446 /* Request Notification */
448 stopforgc((struct garbagelist *)___params___);
452 reqNotify(oidarray, versionarray, 1,p->___mid___);
454 reqNotify(oidarray, versionarray, 1);
468 #if defined(THREADS)||defined(STM)
469 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
470 pthread_mutex_lock(&joinlock);
471 while(!VAR(___this___)->___finished___) {
473 stopforgc((struct garbagelist *)___params___);
475 pthread_cond_wait(&joincond, &joinlock);
480 pthread_mutex_unlock(&joinlock);
483 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
486 pthread_attr_t nattr;
488 pthread_mutex_lock(&gclistlock);
490 pthread_mutex_unlock(&gclistlock);
491 pthread_attr_init(&nattr);
492 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
494 pthread_attr_getstacksize(&nattr, &stacksize);
495 printf("STACKSIZE=%u\n",stacksize);
497 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
501 /* This next statement will likely not work on many machines */
503 pthread_attr_destroy(&nattr);
508 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
509 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
514 void globalDestructor(void *value) {
516 pthread_setspecific(oidval, NULL);
519 void initDSMthread(int *ptr) {
526 int p[]={1, 0 /* NULL */, oid};
528 struct listitem litem;
529 pthread_setspecific(litemkey, &litem);
532 //Add our litem to list of threads
534 pthread_mutex_lock(&gclistlock);
539 pthread_mutex_unlock(&gclistlock);
541 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
543 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
545 threadData = calloc(1, sizeof(unsigned int));
546 *((unsigned int *) threadData) = oid;
547 pthread_setspecific(oidval, threadData);
548 pthread_mutex_lock(&gclistlock);
551 pthread_setspecific(threadlocks, litem.locklist);
553 if (litem.prev==NULL) {
556 litem.prev->next=litem.next;
558 if (litem.next!=NULL) {
559 litem.next->prev=litem.prev;
562 pthread_cond_signal(&gccond);
563 pthread_mutex_unlock(&gclistlock);
564 /* Add transaction to check if thread finished for join operation */
569 tmp = transRead((unsigned int) oid);
570 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
571 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
572 if(transCommit()!= 0) {
579 void startDSMthread(int oid, int objType) {
582 pthread_attr_t nattr;
584 pthread_mutex_lock(&gclistlock);
586 pthread_mutex_unlock(&gclistlock);
587 pthread_attr_init(&nattr);
588 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
589 int * ptr=malloc(sizeof(int)*2);
592 pthread_key_create(&oidval, globalDestructor);
595 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
600 pthread_attr_destroy(&nattr);