10 #include "methodheaders.h"
14 #include <DSTM/interface_recovery/dstm.h>
15 #include <DSTM/interface_recovery/llookup.h>
17 #include <DSTM/interface/dstm.h>
18 #include <DSTM/interface/llookup.h>
35 pthread_mutex_t gclock;
36 pthread_mutex_t gclistlock;
37 pthread_cond_t gccond;
38 pthread_mutex_t objlock;
39 pthread_cond_t objcond;
41 pthread_mutex_t atomiclock;
43 pthread_mutex_t joinlock;
44 pthread_cond_t joincond;
45 pthread_key_t threadlocks;
46 pthread_mutex_t threadnotifylock;
47 pthread_cond_t threadnotifycond;
50 #if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
52 extern __thread struct listitem litem;
54 pthread_key_t litemkey;
56 extern struct listitem * list;
62 unsigned int oidvalue;
68 struct lockvector *lptr=(struct lockvector *) pthread_getspecific(threadlocks);
70 struct lockvector *lptr=&lvector;
72 for(lptr->index--;lptr->index>=0;lptr->index--) {
73 if (lptr->locks[lptr->index].islastlock) {
74 struct ___Object___ *ll=lptr->locks[lptr->index].object;
81 pthread_mutex_lock(&objlock); //wake everyone up
82 pthread_cond_broadcast(&objcond);
83 pthread_mutex_unlock(&objlock);
85 pthread_mutex_lock(&gclistlock);
88 if (litem.prev==NULL) {
91 litem.prev->next=litem.next;
93 if (litem.next!=NULL) {
94 litem.next->prev=litem.prev;
98 struct listitem *litem=pthread_getspecific(litemkey);
99 if (litem->prev==NULL) {
102 litem->prev->next=litem->next;
104 if (litem->next!=NULL) {
105 litem->next->prev=litem->prev;
110 pthread_cond_signal(&gccond);
111 pthread_mutex_unlock(&gclistlock);
113 /* Add transaction to check if thread finished for join operation */
114 value = pthread_getspecific(oidval);
115 oidvalue = *((unsigned int *)value);
120 ptr = transRead(oidvalue);
121 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
122 p->___threadDone___ = 1;
123 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
124 if(transCommit() != 0) {
132 void threadhandler(int sig, struct sigcontext ctx) {
137 printf("We just took sig=%d\n",sig);
139 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
140 nptrs = backtrace(buffer, 100);
142 buffer[1]=(void *)ctx.rip;
144 buffer[1]=(void *)ctx.eip;
147 strings = backtrace_symbols(buffer, nptrs);
148 if (strings == NULL) {
149 perror("backtrace_symbols");
153 for (j = 0; j < nptrs; j++)
154 printf("%s\n", strings[j]);
159 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
161 void initializethreads() {
162 struct sigaction sig;
165 pthread_mutex_init(&atomiclock, NULL);
167 pthread_mutex_init(&gclock, NULL);
168 pthread_mutex_init(&gclistlock, NULL);
169 pthread_cond_init(&gccond, NULL);
170 pthread_mutex_init(&objlock,NULL);
171 pthread_cond_init(&objcond,NULL);
172 pthread_mutex_init(&joinlock,NULL);
173 pthread_cond_init(&joincond,NULL);
174 pthread_key_create(&threadlocks, NULL);
176 pthread_key_create(&litem, NULL);
179 initializeexithandler();
184 //deprecated use of sighandler, but apparently still works
186 sig.sa_handler=(void *)errorhandler;
189 sig.sa_handler=(void *)threadhandler;
191 sig.sa_flags=SA_RESTART;
192 sigemptyset(&sig.sa_mask);
194 /* Catch bus errors, segmentation faults, and floating point exceptions*/
195 sigaction(SIGBUS,&sig,0);
196 sigaction(SIGSEGV,&sig,0);
197 sigaction(SIGFPE,&sig,0);
198 signal(SIGPIPE, SIG_IGN);
200 newobjs=calloc(1, sizeof(struct objlist));
201 t_cache = objstrCreate(1048576);
203 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
205 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
208 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
212 #if defined(STMARRAY)&&!defined(DUALVIEW)
215 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
218 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
221 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
223 perror("branchstack");
224 #if defined(STMARRAY)&&!defined(DUALVIEW)
225 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
227 perror("arraystack");
231 trec=calloc(1, sizeof(threadrec_t));
233 lockedobjs=calloc(1, sizeof(struct objlist));
234 objlockscope = calloc(1, sizeof(objlockstate_t));
235 pthread_mutex_init(&lockedobjstore, NULL);
238 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
239 typesCausingAbort[i].numaccess = 0;
240 typesCausingAbort[i].numabort = 0;
241 typesCausingAbort[i].numtrans = 0;
247 struct listitem *litem=malloc(sizeof(struct listitem));
248 pthread_setspecific(litemkey, litem);
255 //Add our litem to list of threads
267 #if defined(THREADS)||defined(STM)
268 void initthread(struct ___Thread___ * ___this___) {
276 struct sigaction sig;
278 sig.sa_handler=(void *)errorhandler;
279 sig.sa_flags=SA_RESTART;
280 sigemptyset(&sig.sa_mask);
282 /* Catch bus errors, segmentation faults, and floating point exceptions*/
283 sigaction(SIGBUS,&sig,0);
284 sigaction(SIGSEGV,&sig,0);
285 sigaction(SIGFPE,&sig,0);
288 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
289 //Add our litem to list of threads
291 struct listitem litem;
292 pthread_setspecific(litemkey, &litem);
293 struct lockvector lvector;
294 pthread_setspecific(threadlocks, &lvector);
296 litem.lvector=&lvector;
298 pthread_mutex_lock(&gclistlock);
303 pthread_mutex_unlock(&gclistlock);
305 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
307 newobjs=calloc(1, sizeof(struct objlist));
309 trec=calloc(1, sizeof(threadrec_t));
311 lockedobjs=calloc(1, sizeof(struct objlist));
313 t_cache = objstrCreate(1048576);
315 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
317 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
320 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
324 #if defined(STMARRAY)&&!defined(DUALVIEW)
327 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
330 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
333 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
335 perror("branchstack");
336 #if defined(STMARRAY)&!defined(DUALVIEW)
337 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
339 perror("arraystack");
342 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
343 objstrDelete(t_cache);
344 objstrDelete(t_reserve);
351 ___this___=(struct ___Thread___ *) p[2];
353 ___Thread______staticStart____L___Thread___(___this___);
355 ___this___->___finished___=1;
356 pthread_mutex_lock(&joinlock);
357 pthread_cond_signal(&joincond);
358 pthread_mutex_unlock(&joinlock);
360 pthread_mutex_lock(&gclistlock);
361 if (litem.prev==NULL) {
364 litem.prev->next=litem.next;
366 if (litem.next!=NULL) {
367 litem.next->prev=litem.prev;
370 pthread_cond_signal(&gccond);
371 pthread_mutex_unlock(&gclistlock);
375 #ifdef D___Thread______sleep____J
376 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
377 #if defined(THREADS)||defined(STM)
379 stopforgc((struct garbagelist *)___params___);
382 usleep(___millis___*1000);
383 #if defined(THREADS)||defined(STM)
391 #ifdef D___Thread______yield____
392 void CALL00(___Thread______yield____) {
397 #ifdef D___Thread______abort____
398 void CALL00(___Thread______abort____) {
400 _longjmp(aborttrans,1);
407 // return if the machine is dead
408 #ifdef D___Thread______nativeGetStatus____I
409 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
410 return getStatus(___mid___);
414 #ifdef D___Thread______nativeGetStatus____I
415 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
422 /* Add thread join capability */
423 #ifdef D___Thread______join____
424 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
425 unsigned int *oidarray;
426 unsigned short *versionarray, version;
428 /* Add transaction to check if thread finished for join operation */
431 ptr = transRead((unsigned int) VAR(___this___));
432 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
433 #ifdef THREADJOINDEBUG
434 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
436 if(p->___threadDone___ == 1) {
437 #ifdef THREADJOINDEBUG
438 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
444 else if( checkiftheMachineDead(p->___mid___) == 0) {
445 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
451 version = (ptr-1)->version;
452 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
453 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
457 oidarray[0] = (unsigned int) VAR(___this___);
459 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
460 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
464 versionarray[0] = version;
465 /* Request Notification */
467 stopforgc((struct garbagelist *)___params___);
471 reqNotify(oidarray, versionarray, 1,p->___mid___);
473 reqNotify(oidarray, versionarray, 1);
488 #if defined(THREADS)||defined(STM)
489 #ifdef D___Thread______nativeJoin____
490 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
491 pthread_mutex_lock(&joinlock);
492 while(!VAR(___this___)->___finished___) {
494 stopforgc((struct garbagelist *)___params___);
496 pthread_cond_wait(&joincond, &joinlock);
501 pthread_mutex_unlock(&joinlock);
505 #ifdef D___Thread______nativeCreate____
506 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
509 pthread_attr_t nattr;
511 pthread_mutex_lock(&gclistlock);
513 pthread_mutex_unlock(&gclistlock);
514 pthread_attr_init(&nattr);
515 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
517 pthread_attr_getstacksize(&nattr, &stacksize);
519 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
523 /* This next statement will likely not work on many machines */
525 pthread_attr_destroy(&nattr);
531 #ifdef D___Thread______start____I
532 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
533 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
539 void globalDestructor(void *value) {
541 pthread_setspecific(oidval, NULL);
544 void initDSMthread(int *ptr) {
551 int p[]={1, 0 /* NULL */, oid};
553 struct listitem litem;
554 pthread_setspecific(litemkey, &litem);
557 //Add our litem to list of threads
559 pthread_mutex_lock(&gclistlock);
564 pthread_mutex_unlock(&gclistlock);
566 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
568 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
570 threadData = calloc(1, sizeof(unsigned int));
571 *((unsigned int *) threadData) = oid;
572 pthread_setspecific(oidval, threadData);
573 pthread_mutex_lock(&gclistlock);
576 pthread_setspecific(threadlocks, litem.locklist);
578 if (litem.prev==NULL) {
581 litem.prev->next=litem.next;
583 if (litem.next!=NULL) {
584 litem.next->prev=litem.prev;
587 pthread_cond_signal(&gccond);
588 pthread_mutex_unlock(&gclistlock);
589 /* Add transaction to check if thread finished for join operation */
594 tmp = transRead((unsigned int) oid);
595 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
596 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
597 if(transCommit()!= 0) {
604 void startDSMthread(int oid, int objType) {
607 pthread_attr_t nattr;
609 // printf("%s -> oid : %u\n",__func__,oid);
611 pthread_mutex_lock(&gclistlock);
613 pthread_mutex_unlock(&gclistlock);
614 pthread_attr_init(&nattr);
615 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
616 int * ptr=malloc(sizeof(int)*2);
619 pthread_key_create(&oidval, globalDestructor);
622 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
627 pthread_attr_destroy(&nattr);