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>
34 pthread_mutex_t gclock;
35 pthread_mutex_t gclistlock;
36 pthread_cond_t gccond;
37 pthread_mutex_t objlock;
38 pthread_cond_t objcond;
40 pthread_mutex_t atomiclock;
42 pthread_mutex_t joinlock;
43 pthread_cond_t joincond;
44 pthread_key_t threadlocks;
45 pthread_mutex_t threadnotifylock;
46 pthread_cond_t threadnotifycond;
49 #if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
51 extern __thread struct listitem litem;
53 pthread_key_t litemkey;
55 extern struct listitem * list;
61 unsigned int oidvalue;
66 struct ___Object___ *ll=pthread_getspecific(threadlocks);
68 struct ___Object___ *llnext=ll->___nextlockobject___;
69 ll->___nextlockobject___=NULL;
70 ll->___prevlockobject___=NULL;
72 ll->tid=0; //unlock it
75 pthread_mutex_lock(&objlock); //wake everyone up
76 pthread_cond_broadcast(&objcond);
77 pthread_mutex_unlock(&objlock);
79 pthread_mutex_lock(&gclistlock);
81 pthread_setspecific(threadlocks, litem.locklist);
84 if (litem.prev==NULL) {
87 litem.prev->next=litem.next;
89 if (litem.next!=NULL) {
90 litem.next->prev=litem.prev;
94 struct listitem *litem=pthread_getspecific(litemkey);
95 if (litem->prev==NULL) {
98 litem->prev->next=litem->next;
100 if (litem->next!=NULL) {
101 litem->next->prev=litem->prev;
106 pthread_cond_signal(&gccond);
107 pthread_mutex_unlock(&gclistlock);
109 /* Add transaction to check if thread finished for join operation */
110 value = pthread_getspecific(oidval);
111 oidvalue = *((unsigned int *)value);
116 ptr = transRead(oidvalue);
117 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
118 p->___threadDone___ = 1;
119 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
120 if(transCommit() != 0) {
128 void threadhandler(int sig, struct sigcontext ctx) {
133 printf("We just took sig=%d\n",sig);
135 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
136 nptrs = backtrace(buffer, 100);
138 buffer[1]=(void *)ctx.rip;
140 buffer[1]=(void *)ctx.eip;
143 strings = backtrace_symbols(buffer, nptrs);
144 if (strings == NULL) {
145 perror("backtrace_symbols");
149 for (j = 0; j < nptrs; j++)
150 printf("%s\n", strings[j]);
155 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
157 void initializethreads() {
158 struct sigaction sig;
161 pthread_mutex_init(&atomiclock, NULL);
163 pthread_mutex_init(&gclock, NULL);
164 pthread_mutex_init(&gclistlock, NULL);
165 pthread_cond_init(&gccond, NULL);
166 pthread_mutex_init(&objlock,NULL);
167 pthread_cond_init(&objcond,NULL);
168 pthread_mutex_init(&joinlock,NULL);
169 pthread_cond_init(&joincond,NULL);
170 pthread_key_create(&threadlocks, NULL);
172 pthread_key_create(&litem, NULL);
175 initializeexithandler();
180 //deprecated use of sighandler, but apparently still works
182 sig.sa_handler=(void *)errorhandler;
185 sig.sa_handler=(void *)threadhandler;
187 sig.sa_flags=SA_RESTART;
188 sigemptyset(&sig.sa_mask);
190 /* Catch bus errors, segmentation faults, and floating point exceptions*/
191 sigaction(SIGBUS,&sig,0);
192 sigaction(SIGSEGV,&sig,0);
193 sigaction(SIGFPE,&sig,0);
194 signal(SIGPIPE, SIG_IGN);
196 newobjs=calloc(1, sizeof(struct objlist));
197 t_cache = objstrCreate(1048576);
199 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
201 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
204 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
208 #if defined(STMARRAY)&&!defined(DUALVIEW)
211 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
214 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
217 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
219 perror("branchstack");
220 #if defined(STMARRAY)&&!defined(DUALVIEW)
221 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
223 perror("arraystack");
227 trec=calloc(1, sizeof(threadrec_t));
229 lockedobjs=calloc(1, sizeof(struct objlist));
230 objlockscope = calloc(1, sizeof(objlockstate_t));
231 pthread_mutex_init(&lockedobjstore, NULL);
234 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
235 typesCausingAbort[i].numaccess = 0;
236 typesCausingAbort[i].numabort = 0;
237 typesCausingAbort[i].numtrans = 0;
243 struct listitem *litem=malloc(sizeof(struct listitem));
244 pthread_setspecific(litemkey, litem);
251 //Add our litem to list of threads
263 #if defined(THREADS)||defined(STM)
264 void initthread(struct ___Thread___ * ___this___) {
272 struct sigaction sig;
274 sig.sa_handler=(void *)errorhandler;
275 sig.sa_flags=SA_RESTART;
276 sigemptyset(&sig.sa_mask);
278 /* Catch bus errors, segmentation faults, and floating point exceptions*/
279 sigaction(SIGBUS,&sig,0);
280 sigaction(SIGSEGV,&sig,0);
281 sigaction(SIGFPE,&sig,0);
284 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
285 //Add our litem to list of threads
287 struct listitem litem;
288 pthread_setspecific(litemkey, &litem);
291 pthread_mutex_lock(&gclistlock);
296 pthread_mutex_unlock(&gclistlock);
299 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
301 newobjs=calloc(1, sizeof(struct objlist));
303 trec=calloc(1, sizeof(threadrec_t));
305 lockedobjs=calloc(1, sizeof(struct objlist));
307 t_cache = objstrCreate(1048576);
309 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
311 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
314 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
318 #if defined(STMARRAY)&&!defined(DUALVIEW)
321 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
324 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
327 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
329 perror("branchstack");
330 #if defined(STMARRAY)&!defined(DUALVIEW)
331 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
333 perror("arraystack");
336 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
337 objstrDelete(t_cache);
338 objstrDelete(t_reserve);
345 ___this___=(struct ___Thread___ *) p[2];
347 ___Thread______staticStart____L___Thread___(___this___);
349 ___this___->___finished___=1;
350 pthread_mutex_lock(&joinlock);
351 pthread_cond_signal(&joincond);
352 pthread_mutex_unlock(&joinlock);
354 pthread_mutex_lock(&gclistlock);
356 pthread_setspecific(threadlocks, litem.locklist);
358 if (litem.prev==NULL) {
361 litem.prev->next=litem.next;
363 if (litem.next!=NULL) {
364 litem.next->prev=litem.prev;
367 pthread_cond_signal(&gccond);
368 pthread_mutex_unlock(&gclistlock);
372 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
373 #if defined(THREADS)||defined(STM)
375 stopforgc((struct garbagelist *)___params___);
378 usleep(___millis___);
379 #if defined(THREADS)||defined(STM)
386 #if defined(DSTM)|| defined(THREADS)||defined(STM)
387 void CALL00(___Thread______yield____) {
392 #ifdef D___Thread______abort____
393 void CALL00(___Thread______abort____) {
395 _longjmp(aborttrans,1);
402 // return if the machine is dead
403 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
404 return getStatus(___mid___);
407 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
413 /* Add thread join capability */
414 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
415 unsigned int *oidarray;
416 unsigned short *versionarray, version;
418 /* Add transaction to check if thread finished for join operation */
421 ptr = transRead((unsigned int) VAR(___this___));
422 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
423 #ifdef THREADJOINDEBUG
424 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
426 if(p->___threadDone___ == 1) {
427 #ifdef THREADJOINDEBUG
428 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
434 else if( checkiftheMachineDead(p->___mid___) == 0) {
435 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
441 version = (ptr-1)->version;
442 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
443 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
447 oidarray[0] = (unsigned int) VAR(___this___);
449 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
450 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
454 versionarray[0] = version;
455 /* Request Notification */
457 stopforgc((struct garbagelist *)___params___);
461 reqNotify(oidarray, versionarray, 1,p->___mid___);
463 reqNotify(oidarray, versionarray, 1);
477 #if defined(THREADS)||defined(STM)
478 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
479 pthread_mutex_lock(&joinlock);
480 while(!VAR(___this___)->___finished___) {
482 stopforgc((struct garbagelist *)___params___);
484 pthread_cond_wait(&joincond, &joinlock);
489 pthread_mutex_unlock(&joinlock);
492 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
495 pthread_attr_t nattr;
497 pthread_mutex_lock(&gclistlock);
499 pthread_mutex_unlock(&gclistlock);
500 pthread_attr_init(&nattr);
501 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
503 pthread_attr_getstacksize(&nattr, &stacksize);
504 printf("STACKSIZE=%u\n",stacksize);
506 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
510 /* This next statement will likely not work on many machines */
512 pthread_attr_destroy(&nattr);
517 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
518 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
523 void globalDestructor(void *value) {
525 pthread_setspecific(oidval, NULL);
528 void initDSMthread(int *ptr) {
535 int p[]={1, 0 /* NULL */, oid};
537 struct listitem litem;
538 pthread_setspecific(litemkey, &litem);
541 //Add our litem to list of threads
543 pthread_mutex_lock(&gclistlock);
548 pthread_mutex_unlock(&gclistlock);
550 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
552 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
554 threadData = calloc(1, sizeof(unsigned int));
555 *((unsigned int *) threadData) = oid;
556 pthread_setspecific(oidval, threadData);
557 pthread_mutex_lock(&gclistlock);
560 pthread_setspecific(threadlocks, litem.locklist);
562 if (litem.prev==NULL) {
565 litem.prev->next=litem.next;
567 if (litem.next!=NULL) {
568 litem.next->prev=litem.prev;
571 pthread_cond_signal(&gccond);
572 pthread_mutex_unlock(&gclistlock);
573 /* Add transaction to check if thread finished for join operation */
578 tmp = transRead((unsigned int) oid);
579 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
580 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
581 if(transCommit()!= 0) {
588 void startDSMthread(int oid, int objType) {
591 pthread_attr_t nattr;
593 // printf("%s -> oid : %u\n",__func__,oid);
595 pthread_mutex_lock(&gclistlock);
597 pthread_mutex_unlock(&gclistlock);
598 pthread_attr_init(&nattr);
599 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
600 int * ptr=malloc(sizeof(int)*2);
603 pthread_key_create(&oidval, globalDestructor);
606 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
611 pthread_attr_destroy(&nattr);