11 #include <DSTM/interface/dstm.h>
12 #include <DSTM/interface/llookup.h>
25 pthread_mutex_t gclock;
26 pthread_mutex_t gclistlock;
27 pthread_cond_t gccond;
28 pthread_mutex_t objlock;
29 pthread_cond_t objcond;
31 pthread_mutex_t atomiclock;
33 pthread_mutex_t joinlock;
34 pthread_cond_t joincond;
35 pthread_key_t threadlocks;
36 pthread_mutex_t threadnotifylock;
37 pthread_cond_t threadnotifycond;
40 #if defined(THREADS) || defined(DSTM) || defined(STM)
42 extern __thread struct listitem litem;
44 pthread_key_t litemkey;
46 extern struct listitem * list;
52 unsigned int oidvalue;
57 struct ___Object___ *ll=pthread_getspecific(threadlocks);
59 struct ___Object___ *llnext=ll->___nextlockobject___;
60 ll->___nextlockobject___=NULL;
61 ll->___prevlockobject___=NULL;
63 ll->tid=0; //unlock it
66 pthread_mutex_lock(&objlock); //wake everyone up
67 pthread_cond_broadcast(&objcond);
68 pthread_mutex_unlock(&objlock);
70 pthread_mutex_lock(&gclistlock);
72 pthread_setspecific(threadlocks, litem.locklist);
75 if (litem.prev==NULL) {
78 litem.prev->next=litem.next;
80 if (litem.next!=NULL) {
81 litem.next->prev=litem.prev;
85 struct listitem *litem=pthread_getspecific(litemkey);
86 if (litem->prev==NULL) {
89 litem->prev->next=litem->next;
91 if (litem->next!=NULL) {
92 litem->next->prev=litem->prev;
97 pthread_cond_signal(&gccond);
98 pthread_mutex_unlock(&gclistlock);
100 /* Add transaction to check if thread finished for join operation */
101 value = pthread_getspecific(oidval);
102 oidvalue = *((unsigned int *)value);
107 ptr = transRead(oidvalue);
108 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
109 p->___threadDone___ = 1;
110 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
111 if(transCommit() != 0) {
119 void threadhandler(int sig, struct sigcontext ctx) {
124 printf("We just took sig=%d\n",sig);
126 printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
127 nptrs = backtrace(buffer, 100);
129 buffer[1]=(void *)ctx.rip;
131 buffer[1]=(void *)ctx.eip;
134 strings = backtrace_symbols(buffer, nptrs);
135 if (strings == NULL) {
136 perror("backtrace_symbols");
140 for (j = 0; j < nptrs; j++)
141 printf("%s\n", strings[j]);
146 struct primitivelist *pl;
148 void initializethreads() {
149 struct sigaction sig;
152 pthread_mutex_init(&atomiclock, NULL);
154 pthread_mutex_init(&gclock, NULL);
155 pthread_mutex_init(&gclistlock, NULL);
156 pthread_cond_init(&gccond, NULL);
157 pthread_mutex_init(&objlock,NULL);
158 pthread_cond_init(&objcond,NULL);
159 pthread_mutex_init(&joinlock,NULL);
160 pthread_cond_init(&joincond,NULL);
161 pthread_key_create(&threadlocks, NULL);
163 pthread_key_create(&litem, NULL);
166 initializeexithandler();
168 //deprecated use of sighandler, but apparently still works
169 sig.sa_handler=(void *)threadhandler;
170 sig.sa_flags=SA_RESTART;
171 sigemptyset(&sig.sa_mask);
173 /* Catch bus errors, segmentation faults, and floating point exceptions*/
174 sigaction(SIGBUS,&sig,0);
175 sigaction(SIGSEGV,&sig,0);
176 sigaction(SIGFPE,&sig,0);
177 signal(SIGPIPE, SIG_IGN);
179 newobjs=calloc(1, sizeof(struct objlist));
180 t_cache = objstrCreate(1048576);
182 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
184 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
187 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
193 trec=calloc(1, sizeof(threadrec_t));
195 lockedobjs=calloc(1, sizeof(struct objlist));
196 objlockscope = calloc(1, sizeof(objlockstate_t));
197 pthread_mutex_init(&lockedobjstore, NULL);
200 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
201 typesCausingAbort[i] = 0;
207 struct listitem *litem=malloc(sizeof(struct listitem));
208 pthread_setspecific(litemkey, litem);
215 //Add our litem to list of threads
224 #if defined(THREADS)||defined(STM)
225 void initthread(struct ___Thread___ * ___this___) {
227 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
228 //Add our litem to list of threads
230 struct listitem litem;
231 pthread_setspecific(litemkey, &litem);
234 pthread_mutex_lock(&gclistlock);
239 pthread_mutex_unlock(&gclistlock);
242 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
244 newobjs=calloc(1, sizeof(struct objlist));
246 trec=calloc(1, sizeof(threadrec_t));
248 lockedobjs=calloc(1, sizeof(struct objlist));
250 t_cache = objstrCreate(1048576);
252 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
254 rd_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
257 dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
261 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
262 objstrDelete(t_cache);
263 objstrDelete(t_reserve);
270 ___this___=(struct ___Thread___ *) p[2];
272 ___Thread______staticStart____L___Thread___(___this___);
274 ___this___->___finished___=1;
275 pthread_mutex_lock(&joinlock);
276 pthread_cond_signal(&joincond);
277 pthread_mutex_unlock(&joinlock);
279 pthread_mutex_lock(&gclistlock);
281 pthread_setspecific(threadlocks, litem.locklist);
283 if (litem.prev==NULL) {
286 litem.prev->next=litem.next;
288 if (litem.next!=NULL) {
289 litem.next->prev=litem.prev;
292 pthread_cond_signal(&gccond);
293 pthread_mutex_unlock(&gclistlock);
297 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
298 #if defined(THREADS)||defined(STM)
300 stopforgc((struct garbagelist *)___params___);
303 usleep(___millis___);
304 #if defined(THREADS)||defined(STM)
311 #if defined(DSTM)|| defined(THREADS)||defined(STM)
312 void CALL00(___Thread______yield____) {
318 /* Add thread join capability */
319 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
320 unsigned int *oidarray;
321 unsigned short *versionarray, version;
323 /* Add transaction to check if thread finished for join operation */
326 ptr = transRead((unsigned int) VAR(___this___));
327 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
328 #ifdef THREADJOINDEBUG
329 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
331 if(p->___threadDone___ == 1) {
332 #ifdef THREADJOINDEBUG
333 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
339 version = (ptr-1)->version;
340 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
341 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
345 oidarray[0] = (unsigned int) VAR(___this___);
347 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
348 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
352 versionarray[0] = version;
353 /* Request Notification */
355 stopforgc((struct garbagelist *)___params___);
357 reqNotify(oidarray, versionarray, 1);
370 #if defined(THREADS)||defined(STM)
371 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
372 pthread_mutex_lock(&joinlock);
373 while(!VAR(___this___)->___finished___) {
375 stopforgc((struct garbagelist *)___params___);
377 pthread_cond_wait(&joincond, &joinlock);
382 pthread_mutex_unlock(&joinlock);
385 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
388 pthread_attr_t nattr;
390 pthread_mutex_lock(&gclistlock);
392 pthread_mutex_unlock(&gclistlock);
393 pthread_attr_init(&nattr);
394 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
396 pthread_attr_getstacksize(&nattr, &stacksize);
397 printf("STACKSIZE=%u\n",stacksize);
399 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
403 /* This next statement will likely not work on many machines */
405 pthread_attr_destroy(&nattr);
410 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
411 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
416 void globalDestructor(void *value) {
418 pthread_setspecific(oidval, NULL);
421 void initDSMthread(int *ptr) {
428 int p[]={1, 0 /* NULL */, oid};
430 struct listitem litem;
431 pthread_setspecific(litemkey, &litem);
434 //Add our litem to list of threads
436 pthread_mutex_lock(&gclistlock);
441 pthread_mutex_unlock(&gclistlock);
443 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
445 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
447 threadData = calloc(1, sizeof(unsigned int));
448 *((unsigned int *) threadData) = oid;
449 pthread_setspecific(oidval, threadData);
450 pthread_mutex_lock(&gclistlock);
453 pthread_setspecific(threadlocks, litem.locklist);
455 if (litem.prev==NULL) {
458 litem.prev->next=litem.next;
460 if (litem.next!=NULL) {
461 litem.next->prev=litem.prev;
464 pthread_cond_signal(&gccond);
465 pthread_mutex_unlock(&gclistlock);
466 /* Add transaction to check if thread finished for join operation */
471 tmp = transRead((unsigned int) oid);
472 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
473 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
474 if(transCommit()!= 0) {
481 void startDSMthread(int oid, int objType) {
484 pthread_attr_t nattr;
486 pthread_mutex_lock(&gclistlock);
488 pthread_mutex_unlock(&gclistlock);
489 pthread_attr_init(&nattr);
490 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
491 int * ptr=malloc(sizeof(int)*2);
494 pthread_key_create(&oidval, globalDestructor);
496 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
501 pthread_attr_destroy(&nattr);