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 joinlock;
30 pthread_cond_t joincond;
31 pthread_key_t threadlocks;
32 pthread_mutex_t threadnotifylock;
33 pthread_cond_t threadnotifycond;
36 #if defined(THREADS) || defined(DSTM) || defined(STM)
38 extern __thread struct listitem litem;
40 pthread_key_t litemkey;
42 extern struct listitem * list;
48 unsigned int oidvalue;
53 struct ___Object___ *ll=pthread_getspecific(threadlocks);
55 struct ___Object___ *llnext=ll->___nextlockobject___;
56 ll->___nextlockobject___=NULL;
57 ll->___prevlockobject___=NULL;
59 ll->tid=0; //unlock it
62 pthread_mutex_lock(&objlock); //wake everyone up
63 pthread_cond_broadcast(&objcond);
64 pthread_mutex_unlock(&objlock);
66 pthread_mutex_lock(&gclistlock);
68 pthread_setspecific(threadlocks, litem->locklist);
71 if (litem.prev==NULL) {
74 litem.prev->next=litem.next;
76 if (litem.next!=NULL) {
77 litem.next->prev=litem.prev;
81 struct listitem *litem=pthread_getspecific(litemkey);
82 if (litem->prev==NULL) {
85 litem->prev->next=litem->next;
87 if (litem->next!=NULL) {
88 litem->next->prev=litem->prev;
93 pthread_cond_signal(&gccond);
94 pthread_mutex_unlock(&gclistlock);
96 /* Add transaction to check if thread finished for join operation */
97 value = pthread_getspecific(oidval);
98 oidvalue = *((unsigned int *)value);
103 ptr = transRead(oidvalue);
104 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
105 p->___threadDone___ = 1;
106 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
107 if(transCommit() != 0) {
115 void threadhandler(int sig, siginfo_t *info, void *uap) {
117 printf("sig=%d\n",sig);
123 void initializethreads() {
124 struct sigaction sig;
126 pthread_mutex_init(&gclock, NULL);
127 pthread_mutex_init(&gclistlock, NULL);
128 pthread_cond_init(&gccond, NULL);
129 pthread_mutex_init(&objlock,NULL);
130 pthread_cond_init(&objcond,NULL);
131 pthread_mutex_init(&joinlock,NULL);
132 pthread_cond_init(&joincond,NULL);
133 pthread_key_create(&threadlocks, NULL);
135 pthread_key_create(&litem, NULL);
138 initializeexithandler();
140 sig.sa_sigaction=&threadhandler;
141 sig.sa_flags=SA_SIGINFO;
142 sigemptyset(&sig.sa_mask);
144 /* Catch bus errors, segmentation faults, and floating point exceptions*/
145 sigaction(SIGBUS,&sig,0);
146 sigaction(SIGSEGV,&sig,0);
147 sigaction(SIGFPE,&sig,0);
148 signal(SIGPIPE, SIG_IGN);
150 newobjs=calloc(1, sizeof(struct objlist));
151 t_cache = objstrCreate(1048576);
153 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
155 trec=calloc(1, sizeof(threadrec_t));
157 lockedobjs=calloc(1, sizeof(struct objlist));
158 objlockscope = calloc(1, sizeof(objlockstate_t));
159 pthread_mutex_init(&lockedobjstore, NULL);
162 for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
163 typesCausingAbort[i] = 0;
169 struct listitem *litem=malloc(sizeof(struct listitem));
170 pthread_setspecific(litemkey, litem);
177 //Add our litem to list of threads
186 #if defined(THREADS)||defined(STM)
187 void initthread(struct ___Thread___ * ___this___) {
189 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
190 //Add our litem to list of threads
192 struct listitem litem;
193 pthread_setspecific(litemkey, &litem);
196 pthread_mutex_lock(&gclistlock);
201 pthread_mutex_unlock(&gclistlock);
204 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
206 newobjs=calloc(1, sizeof(struct objlist));
208 trec=calloc(1, sizeof(threadrec_t));
210 lockedobjs=calloc(1, sizeof(struct objlist));
212 t_cache = objstrCreate(1048576);
214 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
215 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
216 objstrDelete(t_cache);
217 objstrDelete(t_reserve);
224 ___this___=(struct ___Thread___ *) p[2];
226 ___Thread______staticStart____L___Thread___(___this___);
228 ___this___->___finished___=1;
229 pthread_mutex_lock(&joinlock);
230 pthread_cond_signal(&joincond);
231 pthread_mutex_unlock(&joinlock);
233 pthread_mutex_lock(&gclistlock);
235 pthread_setspecific(threadlocks, litem->locklist);
237 if (litem.prev==NULL) {
240 litem.prev->next=litem.next;
242 if (litem.next!=NULL) {
243 litem.next->prev=litem.prev;
246 pthread_cond_signal(&gccond);
247 pthread_mutex_unlock(&gclistlock);
251 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
252 #if defined(THREADS)||defined(STM)
254 stopforgc((struct garbagelist *)___params___);
257 usleep(___millis___);
258 #if defined(THREADS)||defined(STM)
265 #if defined(DSTM)|| defined(THREADS)||defined(STM)
266 void CALL00(___Thread______yield____) {
272 /* Add thread join capability */
273 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
274 unsigned int *oidarray;
275 unsigned short *versionarray, version;
277 /* Add transaction to check if thread finished for join operation */
280 ptr = transRead((unsigned int) VAR(___this___));
281 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
282 #ifdef THREADJOINDEBUG
283 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
285 if(p->___threadDone___ == 1) {
286 #ifdef THREADJOINDEBUG
287 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
293 version = (ptr-1)->version;
294 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
295 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
299 oidarray[0] = (unsigned int) VAR(___this___);
301 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
302 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
306 versionarray[0] = version;
307 /* Request Notification */
309 stopforgc((struct garbagelist *)___params___);
311 reqNotify(oidarray, versionarray, 1);
324 #if defined(THREADS)||defined(STM)
325 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
326 pthread_mutex_lock(&joinlock);
327 while(!VAR(___this___)->___finished___) {
329 stopforgc((struct garbagelist *)___params___);
331 pthread_cond_wait(&joincond, &joinlock);
336 pthread_mutex_unlock(&joinlock);
339 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
342 pthread_attr_t nattr;
344 pthread_mutex_lock(&gclistlock);
346 pthread_mutex_unlock(&gclistlock);
347 pthread_attr_init(&nattr);
348 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
351 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
355 /* This next statement will likely not work on many machines */
357 pthread_attr_destroy(&nattr);
362 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
363 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
368 void globalDestructor(void *value) {
370 pthread_setspecific(oidval, NULL);
373 void initDSMthread(int *ptr) {
380 int p[]={1, 0 /* NULL */, oid};
382 struct listitem litem;
383 pthread_setspecific(litemkey, &litem);
386 //Add our litem to list of threads
388 pthread_mutex_lock(&gclistlock);
393 pthread_mutex_unlock(&gclistlock);
395 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
397 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
399 threadData = calloc(1, sizeof(unsigned int));
400 *((unsigned int *) threadData) = oid;
401 pthread_setspecific(oidval, threadData);
402 pthread_mutex_lock(&gclistlock);
405 pthread_setspecific(threadlocks, litem->locklist);
407 if (litem.prev==NULL) {
410 litem.prev->next=litem.next;
412 if (litem.next!=NULL) {
413 litem.next->prev=litem.prev;
416 pthread_cond_signal(&gccond);
417 pthread_mutex_unlock(&gclistlock);
418 /* Add transaction to check if thread finished for join operation */
423 tmp = transRead((unsigned int) oid);
424 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
425 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
426 if(transCommit()!= 0) {
433 void startDSMthread(int oid, int objType) {
436 pthread_attr_t nattr;
438 pthread_mutex_lock(&gclistlock);
440 pthread_mutex_unlock(&gclistlock);
441 pthread_attr_init(&nattr);
442 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
443 int * ptr=malloc(sizeof(int)*2);
446 pthread_key_create(&oidval, globalDestructor);
448 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
453 pthread_attr_destroy(&nattr);