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);
202 int a=mprotect((downpage(&ptrstack.array[MAXPOINTERS])), 4096, PROT_NONE);
205 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
208 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
210 perror("branchstack");
213 trec=calloc(1, sizeof(threadrec_t));
215 lockedobjs=calloc(1, sizeof(struct objlist));
216 objlockscope = calloc(1, sizeof(objlockstate_t));
217 pthread_mutex_init(&lockedobjstore, NULL);
220 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
221 typesCausingAbort[i].numaccess = 0;
222 typesCausingAbort[i].numabort = 0;
223 typesCausingAbort[i].numtrans = 0;
229 struct listitem *litem=malloc(sizeof(struct listitem));
230 pthread_setspecific(litemkey, litem);
237 //Add our litem to list of threads
246 #if defined(THREADS)||defined(STM)
247 void initthread(struct ___Thread___ * ___this___) {
249 struct sigaction sig;
251 sig.sa_handler=(void *)errorhandler;
252 sig.sa_flags=SA_RESTART;
253 sigemptyset(&sig.sa_mask);
255 /* Catch bus errors, segmentation faults, and floating point exceptions*/
256 sigaction(SIGBUS,&sig,0);
257 sigaction(SIGSEGV,&sig,0);
258 sigaction(SIGFPE,&sig,0);
261 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
262 //Add our litem to list of threads
264 struct listitem litem;
265 pthread_setspecific(litemkey, &litem);
268 pthread_mutex_lock(&gclistlock);
273 pthread_mutex_unlock(&gclistlock);
276 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
278 newobjs=calloc(1, sizeof(struct objlist));
280 trec=calloc(1, sizeof(threadrec_t));
282 lockedobjs=calloc(1, sizeof(struct objlist));
284 t_cache = objstrCreate(1048576);
286 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
288 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
291 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
295 int a=mprotect(downpage(&ptrstack.array[MAXPOINTERS]), 4096, PROT_NONE);
298 a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
301 a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
303 perror("branchstack");
305 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
306 objstrDelete(t_cache);
307 objstrDelete(t_reserve);
314 ___this___=(struct ___Thread___ *) p[2];
316 ___Thread______staticStart____L___Thread___(___this___);
318 ___this___->___finished___=1;
319 pthread_mutex_lock(&joinlock);
320 pthread_cond_signal(&joincond);
321 pthread_mutex_unlock(&joinlock);
323 pthread_mutex_lock(&gclistlock);
325 pthread_setspecific(threadlocks, litem.locklist);
327 if (litem.prev==NULL) {
330 litem.prev->next=litem.next;
332 if (litem.next!=NULL) {
333 litem.next->prev=litem.prev;
336 pthread_cond_signal(&gccond);
337 pthread_mutex_unlock(&gclistlock);
341 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
342 #if defined(THREADS)||defined(STM)
344 stopforgc((struct garbagelist *)___params___);
347 usleep(___millis___);
348 #if defined(THREADS)||defined(STM)
355 #if defined(DSTM)|| defined(THREADS)||defined(STM)
356 void CALL00(___Thread______yield____) {
363 // return if the machine is dead
364 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
365 return getStatus(___mid___);
368 int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
374 /* Add thread join capability */
375 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
376 unsigned int *oidarray;
377 unsigned short *versionarray, version;
379 /* Add transaction to check if thread finished for join operation */
382 ptr = transRead((unsigned int) VAR(___this___));
383 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
384 #ifdef THREADJOINDEBUG
385 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
387 if(p->___threadDone___ == 1) {
388 #ifdef THREADJOINDEBUG
389 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
395 else if( checkiftheMachineDead(p->___mid___) == 0) {
396 printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
402 version = (ptr-1)->version;
403 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
404 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
408 oidarray[0] = (unsigned int) VAR(___this___);
410 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
411 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
415 versionarray[0] = version;
416 /* Request Notification */
418 stopforgc((struct garbagelist *)___params___);
422 reqNotify(oidarray, versionarray, 1,p->___mid___);
424 reqNotify(oidarray, versionarray, 1);
438 #if defined(THREADS)||defined(STM)
439 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
440 pthread_mutex_lock(&joinlock);
441 while(!VAR(___this___)->___finished___) {
443 stopforgc((struct garbagelist *)___params___);
445 pthread_cond_wait(&joincond, &joinlock);
450 pthread_mutex_unlock(&joinlock);
453 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
456 pthread_attr_t nattr;
458 pthread_mutex_lock(&gclistlock);
460 pthread_mutex_unlock(&gclistlock);
461 pthread_attr_init(&nattr);
462 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
464 pthread_attr_getstacksize(&nattr, &stacksize);
465 printf("STACKSIZE=%u\n",stacksize);
467 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
471 /* This next statement will likely not work on many machines */
473 pthread_attr_destroy(&nattr);
478 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
479 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
484 void globalDestructor(void *value) {
486 pthread_setspecific(oidval, NULL);
489 void initDSMthread(int *ptr) {
496 int p[]={1, 0 /* NULL */, oid};
498 struct listitem litem;
499 pthread_setspecific(litemkey, &litem);
502 //Add our litem to list of threads
504 pthread_mutex_lock(&gclistlock);
509 pthread_mutex_unlock(&gclistlock);
511 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
513 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
515 threadData = calloc(1, sizeof(unsigned int));
516 *((unsigned int *) threadData) = oid;
517 pthread_setspecific(oidval, threadData);
518 pthread_mutex_lock(&gclistlock);
521 pthread_setspecific(threadlocks, litem.locklist);
523 if (litem.prev==NULL) {
526 litem.prev->next=litem.next;
528 if (litem.next!=NULL) {
529 litem.next->prev=litem.prev;
532 pthread_cond_signal(&gccond);
533 pthread_mutex_unlock(&gclistlock);
534 /* Add transaction to check if thread finished for join operation */
539 tmp = transRead((unsigned int) oid);
540 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
541 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
542 if(transCommit()!= 0) {
549 void startDSMthread(int oid, int objType) {
552 pthread_attr_t nattr;
554 pthread_mutex_lock(&gclistlock);
556 pthread_mutex_unlock(&gclistlock);
557 pthread_attr_init(&nattr);
558 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
559 int * ptr=malloc(sizeof(int)*2);
562 pthread_key_create(&oidval, globalDestructor);
565 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
570 pthread_attr_destroy(&nattr);