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;
67 struct ___Object___ *ll=pthread_getspecific(threadlocks);
69 struct ___Object___ *llnext=ll->___nextlockobject___;
70 ll->___nextlockobject___=NULL;
71 ll->___prevlockobject___=NULL;
73 ll->tid=0; //unlock it
76 pthread_mutex_lock(&objlock); //wake everyone up
77 pthread_cond_broadcast(&objcond);
78 pthread_mutex_unlock(&objlock);
80 pthread_mutex_lock(&gclistlock);
82 pthread_setspecific(threadlocks, litem.locklist);
85 if (litem.prev==NULL) {
88 litem.prev->next=litem.next;
90 if (litem.next!=NULL) {
91 litem.next->prev=litem.prev;
95 struct listitem *litem=pthread_getspecific(litemkey);
96 if (litem->prev==NULL) {
99 litem->prev->next=litem->next;
101 if (litem->next!=NULL) {
102 litem->next->prev=litem->prev;
107 pthread_cond_signal(&gccond);
108 pthread_mutex_unlock(&gclistlock);
110 /* Add transaction to check if thread finished for join operation */
111 value = pthread_getspecific(oidval);
112 oidvalue = *((unsigned int *)value);
117 ptr = transRead(oidvalue);
118 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
119 p->___threadDone___ = 1;
120 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
121 if(transCommit() != 0) {
129 void threadhandler(int sig, struct sigcontext ctx) {
134 printf("We just took sig=%d\n",sig);
136 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
137 nptrs = backtrace(buffer, 100);
139 buffer[1]=(void *)ctx.rip;
141 buffer[1]=(void *)ctx.eip;
144 strings = backtrace_symbols(buffer, nptrs);
145 if (strings == NULL) {
146 perror("backtrace_symbols");
150 for (j = 0; j < nptrs; j++)
151 printf("%s\n", strings[j]);
156 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
158 void initializethreads() {
159 struct sigaction sig;
162 pthread_mutex_init(&atomiclock, NULL);
164 pthread_mutex_init(&gclock, NULL);
165 pthread_mutex_init(&gclistlock, NULL);
166 pthread_cond_init(&gccond, NULL);
167 pthread_mutex_init(&objlock,NULL);
168 pthread_cond_init(&objcond,NULL);
169 pthread_mutex_init(&joinlock,NULL);
170 pthread_cond_init(&joincond,NULL);
171 pthread_key_create(&threadlocks, NULL);
173 pthread_key_create(&litem, NULL);
176 initializeexithandler();
181 //deprecated use of sighandler, but apparently still works
183 sig.sa_handler=(void *)errorhandler;
186 sig.sa_handler=(void *)threadhandler;
188 sig.sa_flags=SA_RESTART;
189 sigemptyset(&sig.sa_mask);
191 /* Catch bus errors, segmentation faults, and floating point exceptions*/
192 sigaction(SIGBUS,&sig,0);
193 sigaction(SIGSEGV,&sig,0);
194 sigaction(SIGFPE,&sig,0);
195 signal(SIGPIPE, SIG_IGN);
197 newobjs=calloc(1, sizeof(struct objlist));
198 t_cache = objstrCreate(1048576);
200 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
202 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
205 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
209 #if defined(STMARRAY)&&!defined(DUALVIEW)
212 int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
215 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
218 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
220 perror("branchstack");
221 #if defined(STMARRAY)&&!defined(DUALVIEW)
222 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
224 perror("arraystack");
228 trec=calloc(1, sizeof(threadrec_t));
230 lockedobjs=calloc(1, sizeof(struct objlist));
231 objlockscope = calloc(1, sizeof(objlockstate_t));
232 pthread_mutex_init(&lockedobjstore, NULL);
235 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
236 typesCausingAbort[i].numaccess = 0;
237 typesCausingAbort[i].numabort = 0;
238 typesCausingAbort[i].numtrans = 0;
244 struct listitem *litem=malloc(sizeof(struct listitem));
245 pthread_setspecific(litemkey, litem);
252 //Add our litem to list of threads
264 #if defined(THREADS)||defined(STM)
265 void initthread(struct ___Thread___ * ___this___) {
273 struct sigaction sig;
275 sig.sa_handler=(void *)errorhandler;
276 sig.sa_flags=SA_RESTART;
277 sigemptyset(&sig.sa_mask);
279 /* Catch bus errors, segmentation faults, and floating point exceptions*/
280 sigaction(SIGBUS,&sig,0);
281 sigaction(SIGSEGV,&sig,0);
282 sigaction(SIGFPE,&sig,0);
285 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
286 //Add our litem to list of threads
288 struct listitem litem;
289 pthread_setspecific(litemkey, &litem);
292 pthread_mutex_lock(&gclistlock);
297 pthread_mutex_unlock(&gclistlock);
300 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
302 newobjs=calloc(1, sizeof(struct objlist));
304 trec=calloc(1, sizeof(threadrec_t));
306 lockedobjs=calloc(1, sizeof(struct objlist));
308 t_cache = objstrCreate(1048576);
310 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
312 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
315 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
319 #if defined(STMARRAY)&&!defined(DUALVIEW)
322 int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
325 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
328 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
330 perror("branchstack");
331 #if defined(STMARRAY)&!defined(DUALVIEW)
332 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
334 perror("arraystack");
337 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
338 objstrDelete(t_cache);
339 objstrDelete(t_reserve);
346 ___this___=(struct ___Thread___ *) p[2];
348 ___Thread______staticStart____L___Thread___(___this___);
350 ___this___->___finished___=1;
351 pthread_mutex_lock(&joinlock);
352 pthread_cond_signal(&joincond);
353 pthread_mutex_unlock(&joinlock);
355 pthread_mutex_lock(&gclistlock);
357 pthread_setspecific(threadlocks, litem.locklist);
359 if (litem.prev==NULL) {
362 litem.prev->next=litem.next;
364 if (litem.next!=NULL) {
365 litem.next->prev=litem.prev;
368 pthread_cond_signal(&gccond);
369 pthread_mutex_unlock(&gclistlock);
373 #ifdef D___Thread______sleep____J
374 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
375 #if defined(THREADS)||defined(STM)
377 stopforgc((struct garbagelist *)___params___);
380 usleep(___millis___*1000);
381 #if defined(THREADS)||defined(STM)
389 #ifdef D___Thread______yield____
390 void CALL00(___Thread______yield____) {
395 #ifdef D___Thread______abort____
396 void CALL00(___Thread______abort____) {
398 _longjmp(aborttrans,1);
405 // return if the machine is dead
406 #ifdef D___Thread______nativeGetStatus____I
407 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
408 return getStatus(___mid___);
412 #ifdef D___Thread______nativeGetStatus____I
413 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
420 /* Add thread join capability */
421 #ifdef D___Thread______join____
422 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
423 unsigned int *oidarray;
424 unsigned short *versionarray, version;
426 /* Add transaction to check if thread finished for join operation */
429 ptr = transRead((unsigned int) VAR(___this___));
430 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
431 #ifdef THREADJOINDEBUG
432 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
434 if(p->___threadDone___ == 1) {
435 #ifdef THREADJOINDEBUG
436 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
442 else if( checkiftheMachineDead(p->___mid___) == 0) {
443 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
449 version = (ptr-1)->version;
450 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
451 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
455 oidarray[0] = (unsigned int) VAR(___this___);
457 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
458 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
462 versionarray[0] = version;
463 /* Request Notification */
465 stopforgc((struct garbagelist *)___params___);
469 reqNotify(oidarray, versionarray, 1,p->___mid___);
471 reqNotify(oidarray, versionarray, 1);
486 #if defined(THREADS)||defined(STM)
487 #ifdef D___Thread______nativeJoin____
488 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
489 pthread_mutex_lock(&joinlock);
490 while(!VAR(___this___)->___finished___) {
492 stopforgc((struct garbagelist *)___params___);
494 pthread_cond_wait(&joincond, &joinlock);
499 pthread_mutex_unlock(&joinlock);
503 #ifdef D___Thread______nativeCreate____
504 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
507 pthread_attr_t nattr;
509 pthread_mutex_lock(&gclistlock);
511 pthread_mutex_unlock(&gclistlock);
512 pthread_attr_init(&nattr);
513 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
515 pthread_attr_getstacksize(&nattr, &stacksize);
517 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
521 /* This next statement will likely not work on many machines */
523 pthread_attr_destroy(&nattr);
529 #ifdef D___Thread______start____I
530 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
531 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
537 void globalDestructor(void *value) {
539 pthread_setspecific(oidval, NULL);
542 void initDSMthread(int *ptr) {
549 int p[]={1, 0 /* NULL */, oid};
551 struct listitem litem;
552 pthread_setspecific(litemkey, &litem);
555 //Add our litem to list of threads
557 pthread_mutex_lock(&gclistlock);
562 pthread_mutex_unlock(&gclistlock);
564 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
566 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
568 threadData = calloc(1, sizeof(unsigned int));
569 *((unsigned int *) threadData) = oid;
570 pthread_setspecific(oidval, threadData);
571 pthread_mutex_lock(&gclistlock);
574 pthread_setspecific(threadlocks, litem.locklist);
576 if (litem.prev==NULL) {
579 litem.prev->next=litem.next;
581 if (litem.next!=NULL) {
582 litem.next->prev=litem.prev;
585 pthread_cond_signal(&gccond);
586 pthread_mutex_unlock(&gclistlock);
587 /* Add transaction to check if thread finished for join operation */
592 tmp = transRead((unsigned int) oid);
593 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
594 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
595 if(transCommit()!= 0) {
602 void startDSMthread(int oid, int objType) {
605 pthread_attr_t nattr;
607 // printf("%s -> oid : %u\n",__func__,oid);
609 pthread_mutex_lock(&gclistlock);
611 pthread_mutex_unlock(&gclistlock);
612 pthread_attr_init(&nattr);
613 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
614 int * ptr=malloc(sizeof(int)*2);
617 pthread_key_create(&oidval, globalDestructor);
620 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
625 pthread_attr_destroy(&nattr);