11 #include <DSTM/interface/dstm.h>
12 #include <DSTM/interface/llookup.h>
23 pthread_mutex_t gclock;
24 pthread_mutex_t gclistlock;
25 pthread_cond_t gccond;
26 pthread_mutex_t objlock;
27 pthread_cond_t objcond;
29 pthread_mutex_t atomiclock;
31 pthread_mutex_t joinlock;
32 pthread_cond_t joincond;
33 pthread_key_t threadlocks;
34 pthread_mutex_t threadnotifylock;
35 pthread_cond_t threadnotifycond;
38 #if defined(THREADS) || defined(DSTM) || defined(STM)
40 extern __thread struct listitem litem;
42 pthread_key_t litemkey;
44 extern struct listitem * list;
50 unsigned int oidvalue;
55 struct ___Object___ *ll=pthread_getspecific(threadlocks);
57 struct ___Object___ *llnext=ll->___nextlockobject___;
58 ll->___nextlockobject___=NULL;
59 ll->___prevlockobject___=NULL;
61 ll->tid=0; //unlock it
64 pthread_mutex_lock(&objlock); //wake everyone up
65 pthread_cond_broadcast(&objcond);
66 pthread_mutex_unlock(&objlock);
68 pthread_mutex_lock(&gclistlock);
70 pthread_setspecific(threadlocks, litem.locklist);
73 if (litem.prev==NULL) {
76 litem.prev->next=litem.next;
78 if (litem.next!=NULL) {
79 litem.next->prev=litem.prev;
83 struct listitem *litem=pthread_getspecific(litemkey);
84 if (litem->prev==NULL) {
87 litem->prev->next=litem->next;
89 if (litem->next!=NULL) {
90 litem->next->prev=litem->prev;
95 pthread_cond_signal(&gccond);
96 pthread_mutex_unlock(&gclistlock);
98 /* Add transaction to check if thread finished for join operation */
99 value = pthread_getspecific(oidval);
100 oidvalue = *((unsigned int *)value);
105 ptr = transRead(oidvalue);
106 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
107 p->___threadDone___ = 1;
108 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
109 if(transCommit() != 0) {
117 void threadhandler(int sig, siginfo_t *info, void *uap) {
119 printf("sig=%d\n",sig);
125 void initializethreads() {
126 struct sigaction sig;
129 pthread_mutex_init(&atomiclock, NULL);
131 pthread_mutex_init(&gclock, NULL);
132 pthread_mutex_init(&gclistlock, NULL);
133 pthread_cond_init(&gccond, NULL);
134 pthread_mutex_init(&objlock,NULL);
135 pthread_cond_init(&objcond,NULL);
136 pthread_mutex_init(&joinlock,NULL);
137 pthread_cond_init(&joincond,NULL);
138 pthread_key_create(&threadlocks, NULL);
140 pthread_key_create(&litem, NULL);
143 initializeexithandler();
145 sig.sa_sigaction=&threadhandler;
146 sig.sa_flags=SA_SIGINFO;
147 sigemptyset(&sig.sa_mask);
149 /* Catch bus errors, segmentation faults, and floating point exceptions*/
150 sigaction(SIGBUS,&sig,0);
151 sigaction(SIGSEGV,&sig,0);
152 sigaction(SIGFPE,&sig,0);
153 signal(SIGPIPE, SIG_IGN);
155 newobjs=calloc(1, sizeof(struct objlist));
156 t_cache = objstrCreate(1048576);
158 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
160 trec=calloc(1, sizeof(threadrec_t));
162 lockedobjs=calloc(1, sizeof(struct objlist));
163 objlockscope = calloc(1, sizeof(objlockstate_t));
164 pthread_mutex_init(&lockedobjstore, NULL);
167 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
168 typesCausingAbort[i] = 0;
174 struct listitem *litem=malloc(sizeof(struct listitem));
175 pthread_setspecific(litemkey, litem);
182 //Add our litem to list of threads
191 #if defined(THREADS)||defined(STM)
192 void initthread(struct ___Thread___ * ___this___) {
194 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
195 //Add our litem to list of threads
197 struct listitem litem;
198 pthread_setspecific(litemkey, &litem);
201 pthread_mutex_lock(&gclistlock);
206 pthread_mutex_unlock(&gclistlock);
209 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
211 newobjs=calloc(1, sizeof(struct objlist));
213 trec=calloc(1, sizeof(threadrec_t));
215 lockedobjs=calloc(1, sizeof(struct objlist));
217 t_cache = objstrCreate(1048576);
219 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
220 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
221 objstrDelete(t_cache);
222 objstrDelete(t_reserve);
229 ___this___=(struct ___Thread___ *) p[2];
231 ___Thread______staticStart____L___Thread___(___this___);
233 ___this___->___finished___=1;
234 pthread_mutex_lock(&joinlock);
235 pthread_cond_signal(&joincond);
236 pthread_mutex_unlock(&joinlock);
238 pthread_mutex_lock(&gclistlock);
240 pthread_setspecific(threadlocks, litem.locklist);
242 if (litem.prev==NULL) {
245 litem.prev->next=litem.next;
247 if (litem.next!=NULL) {
248 litem.next->prev=litem.prev;
251 pthread_cond_signal(&gccond);
252 pthread_mutex_unlock(&gclistlock);
256 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
257 #if defined(THREADS)||defined(STM)
259 stopforgc((struct garbagelist *)___params___);
262 usleep(___millis___);
263 #if defined(THREADS)||defined(STM)
270 #if defined(DSTM)|| defined(THREADS)||defined(STM)
271 void CALL00(___Thread______yield____) {
277 /* Add thread join capability */
278 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
279 unsigned int *oidarray;
280 unsigned short *versionarray, version;
282 /* Add transaction to check if thread finished for join operation */
285 ptr = transRead((unsigned int) VAR(___this___));
286 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
287 #ifdef THREADJOINDEBUG
288 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
290 if(p->___threadDone___ == 1) {
291 #ifdef THREADJOINDEBUG
292 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
298 version = (ptr-1)->version;
299 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
300 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
304 oidarray[0] = (unsigned int) VAR(___this___);
306 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
307 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
311 versionarray[0] = version;
312 /* Request Notification */
314 stopforgc((struct garbagelist *)___params___);
316 reqNotify(oidarray, versionarray, 1);
329 #if defined(THREADS)||defined(STM)
330 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
331 pthread_mutex_lock(&joinlock);
332 while(!VAR(___this___)->___finished___) {
334 stopforgc((struct garbagelist *)___params___);
336 pthread_cond_wait(&joincond, &joinlock);
341 pthread_mutex_unlock(&joinlock);
344 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
347 pthread_attr_t nattr;
349 pthread_mutex_lock(&gclistlock);
351 pthread_mutex_unlock(&gclistlock);
352 pthread_attr_init(&nattr);
353 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
356 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
360 /* This next statement will likely not work on many machines */
362 pthread_attr_destroy(&nattr);
367 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
368 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
373 void globalDestructor(void *value) {
375 pthread_setspecific(oidval, NULL);
378 void initDSMthread(int *ptr) {
385 int p[]={1, 0 /* NULL */, oid};
387 struct listitem litem;
388 pthread_setspecific(litemkey, &litem);
391 //Add our litem to list of threads
393 pthread_mutex_lock(&gclistlock);
398 pthread_mutex_unlock(&gclistlock);
400 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
402 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
404 threadData = calloc(1, sizeof(unsigned int));
405 *((unsigned int *) threadData) = oid;
406 pthread_setspecific(oidval, threadData);
407 pthread_mutex_lock(&gclistlock);
410 pthread_setspecific(threadlocks, litem.locklist);
412 if (litem.prev==NULL) {
415 litem.prev->next=litem.next;
417 if (litem.next!=NULL) {
418 litem.next->prev=litem.prev;
421 pthread_cond_signal(&gccond);
422 pthread_mutex_unlock(&gclistlock);
423 /* Add transaction to check if thread finished for join operation */
428 tmp = transRead((unsigned int) oid);
429 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
430 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
431 if(transCommit()!= 0) {
438 void startDSMthread(int oid, int objType) {
441 pthread_attr_t nattr;
443 pthread_mutex_lock(&gclistlock);
445 pthread_mutex_unlock(&gclistlock);
446 pthread_attr_init(&nattr);
447 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
448 int * ptr=malloc(sizeof(int)*2);
451 pthread_key_create(&oidval, globalDestructor);
453 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
458 pthread_attr_destroy(&nattr);