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();
174 //deprecated use of sighandler, but apparently still works
176 sig.sa_handler=(void *)errorhandler;
179 sig.sa_handler=(void *)threadhandler;
181 sig.sa_flags=SA_RESTART;
182 sigemptyset(&sig.sa_mask);
184 /* Catch bus errors, segmentation faults, and floating point exceptions*/
185 sigaction(SIGBUS,&sig,0);
186 sigaction(SIGSEGV,&sig,0);
187 sigaction(SIGFPE,&sig,0);
188 signal(SIGPIPE, SIG_IGN);
190 newobjs=calloc(1, sizeof(struct objlist));
191 t_cache = objstrCreate(1048576);
193 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
195 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
198 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
205 int a=mprotect((downpage(&ptrstack.buffer[0])), 4096, PROT_NONE);
208 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
211 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
213 perror("branchstack");
215 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
217 perror("arraystack");
221 trec=calloc(1, sizeof(threadrec_t));
223 lockedobjs=calloc(1, sizeof(struct objlist));
224 objlockscope = calloc(1, sizeof(objlockstate_t));
225 pthread_mutex_init(&lockedobjstore, NULL);
228 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
229 typesCausingAbort[i].numaccess = 0;
230 typesCausingAbort[i].numabort = 0;
231 typesCausingAbort[i].numtrans = 0;
237 struct listitem *litem=malloc(sizeof(struct listitem));
238 pthread_setspecific(litemkey, litem);
245 //Add our litem to list of threads
254 #if defined(THREADS)||defined(STM)
255 void initthread(struct ___Thread___ * ___this___) {
257 struct sigaction sig;
259 sig.sa_handler=(void *)errorhandler;
260 sig.sa_flags=SA_RESTART;
261 sigemptyset(&sig.sa_mask);
263 /* Catch bus errors, segmentation faults, and floating point exceptions*/
264 sigaction(SIGBUS,&sig,0);
265 sigaction(SIGSEGV,&sig,0);
266 sigaction(SIGFPE,&sig,0);
269 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
270 //Add our litem to list of threads
272 struct listitem litem;
273 pthread_setspecific(litemkey, &litem);
276 pthread_mutex_lock(&gclistlock);
281 pthread_mutex_unlock(&gclistlock);
284 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
286 newobjs=calloc(1, sizeof(struct objlist));
288 trec=calloc(1, sizeof(threadrec_t));
290 lockedobjs=calloc(1, sizeof(struct objlist));
292 t_cache = objstrCreate(1048576);
294 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
296 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
299 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
306 int a=mprotect(downpage(&ptrstack.buffer[0]), 4096, PROT_NONE);
309 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
312 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
314 perror("branchstack");
316 a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
318 perror("arraystack");
321 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
322 objstrDelete(t_cache);
323 objstrDelete(t_reserve);
330 ___this___=(struct ___Thread___ *) p[2];
332 ___Thread______staticStart____L___Thread___(___this___);
334 ___this___->___finished___=1;
335 pthread_mutex_lock(&joinlock);
336 pthread_cond_signal(&joincond);
337 pthread_mutex_unlock(&joinlock);
339 pthread_mutex_lock(&gclistlock);
341 pthread_setspecific(threadlocks, litem.locklist);
343 if (litem.prev==NULL) {
346 litem.prev->next=litem.next;
348 if (litem.next!=NULL) {
349 litem.next->prev=litem.prev;
352 pthread_cond_signal(&gccond);
353 pthread_mutex_unlock(&gclistlock);
357 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
358 #if defined(THREADS)||defined(STM)
360 stopforgc((struct garbagelist *)___params___);
363 usleep(___millis___);
364 #if defined(THREADS)||defined(STM)
371 #if defined(DSTM)|| defined(THREADS)||defined(STM)
372 void CALL00(___Thread______yield____) {
379 // return if the machine is dead
380 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
381 return getStatus(___mid___);
384 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
390 /* Add thread join capability */
391 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
392 unsigned int *oidarray;
393 unsigned short *versionarray, version;
395 /* Add transaction to check if thread finished for join operation */
398 ptr = transRead((unsigned int) VAR(___this___));
399 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
400 #ifdef THREADJOINDEBUG
401 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
403 if(p->___threadDone___ == 1) {
404 #ifdef THREADJOINDEBUG
405 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
411 else if( checkiftheMachineDead(p->___mid___) == 0) {
412 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
418 version = (ptr-1)->version;
419 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
420 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
424 oidarray[0] = (unsigned int) VAR(___this___);
426 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
427 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
431 versionarray[0] = version;
432 /* Request Notification */
434 stopforgc((struct garbagelist *)___params___);
438 reqNotify(oidarray, versionarray, 1,p->___mid___);
440 reqNotify(oidarray, versionarray, 1);
454 #if defined(THREADS)||defined(STM)
455 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
456 pthread_mutex_lock(&joinlock);
457 while(!VAR(___this___)->___finished___) {
459 stopforgc((struct garbagelist *)___params___);
461 pthread_cond_wait(&joincond, &joinlock);
466 pthread_mutex_unlock(&joinlock);
469 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
472 pthread_attr_t nattr;
474 pthread_mutex_lock(&gclistlock);
476 pthread_mutex_unlock(&gclistlock);
477 pthread_attr_init(&nattr);
478 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
480 pthread_attr_getstacksize(&nattr, &stacksize);
481 printf("STACKSIZE=%u\n",stacksize);
483 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
487 /* This next statement will likely not work on many machines */
489 pthread_attr_destroy(&nattr);
494 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
495 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
500 void globalDestructor(void *value) {
502 pthread_setspecific(oidval, NULL);
505 void initDSMthread(int *ptr) {
512 int p[]={1, 0 /* NULL */, oid};
514 struct listitem litem;
515 pthread_setspecific(litemkey, &litem);
518 //Add our litem to list of threads
520 pthread_mutex_lock(&gclistlock);
525 pthread_mutex_unlock(&gclistlock);
527 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
529 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
531 threadData = calloc(1, sizeof(unsigned int));
532 *((unsigned int *) threadData) = oid;
533 pthread_setspecific(oidval, threadData);
534 pthread_mutex_lock(&gclistlock);
537 pthread_setspecific(threadlocks, litem.locklist);
539 if (litem.prev==NULL) {
542 litem.prev->next=litem.next;
544 if (litem.next!=NULL) {
545 litem.next->prev=litem.prev;
548 pthread_cond_signal(&gccond);
549 pthread_mutex_unlock(&gclistlock);
550 /* Add transaction to check if thread finished for join operation */
555 tmp = transRead((unsigned int) oid);
556 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
557 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
558 if(transCommit()!= 0) {
565 void startDSMthread(int oid, int objType) {
568 pthread_attr_t nattr;
570 pthread_mutex_lock(&gclistlock);
572 pthread_mutex_unlock(&gclistlock);
573 pthread_attr_init(&nattr);
574 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
575 int * ptr=malloc(sizeof(int)*2);
578 pthread_key_create(&oidval, globalDestructor);
581 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
586 pthread_attr_destroy(&nattr);