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;
170 #if defined(THREADS)||defined(STM)
171 void initthread(struct ___Thread___ * ___this___) {
173 INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
174 //Add our litem to list of threads
176 struct listitem litem;
177 pthread_setspecific(litemkey, &litem);
180 pthread_mutex_lock(&gclistlock);
185 pthread_mutex_unlock(&gclistlock);
188 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
190 newobjs=calloc(1, sizeof(struct objlist));
192 trec=calloc(1, sizeof(threadrec_t));
194 lockedobjs=calloc(1, sizeof(struct objlist));
196 t_cache = objstrCreate(1048576);
198 t_chashCreate(CHASH_SIZE, CLOADFACTOR);
199 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
200 objstrDelete(t_cache);
201 objstrDelete(t_reserve);
208 ___this___=(struct ___Thread___ *) p[2];
210 ___Thread______staticStart____L___Thread___(___this___);
212 ___this___->___finished___=1;
213 pthread_mutex_lock(&joinlock);
214 pthread_cond_signal(&joincond);
215 pthread_mutex_unlock(&joinlock);
217 pthread_mutex_lock(&gclistlock);
219 pthread_setspecific(threadlocks, litem->locklist);
221 if (litem.prev==NULL) {
224 litem.prev->next=litem.next;
226 if (litem.next!=NULL) {
227 litem.next->prev=litem.prev;
230 pthread_cond_signal(&gccond);
231 pthread_mutex_unlock(&gclistlock);
235 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
236 #if defined(THREADS)||defined(STM)
238 stopforgc((struct garbagelist *)___params___);
241 usleep(___millis___);
242 #if defined(THREADS)||defined(STM)
249 #if defined(DSTM)|| defined(THREADS)||defined(STM)
250 void CALL00(___Thread______yield____) {
256 /* Add thread join capability */
257 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
258 unsigned int *oidarray;
259 unsigned short *versionarray, version;
261 /* Add transaction to check if thread finished for join operation */
264 ptr = transRead((unsigned int) VAR(___this___));
265 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
266 #ifdef THREADJOINDEBUG
267 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
269 if(p->___threadDone___ == 1) {
270 #ifdef THREADJOINDEBUG
271 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
277 version = (ptr-1)->version;
278 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
279 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
283 oidarray[0] = (unsigned int) VAR(___this___);
285 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
286 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
290 versionarray[0] = version;
291 /* Request Notification */
293 stopforgc((struct garbagelist *)___params___);
295 reqNotify(oidarray, versionarray, 1);
308 #if defined(THREADS)||defined(STM)
309 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
311 stopforgc((struct garbagelist *)___params___);
313 pthread_mutex_lock(&joinlock);
314 while(!VAR(___this___)->___finished___)
315 pthread_cond_wait(&joincond, &joinlock);
316 pthread_mutex_unlock(&joinlock);
323 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
326 pthread_attr_t nattr;
328 pthread_mutex_lock(&gclistlock);
330 pthread_mutex_unlock(&gclistlock);
331 pthread_attr_init(&nattr);
332 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
335 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
339 /* This next statement will likely not work on many machines */
341 pthread_attr_destroy(&nattr);
346 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
347 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
352 void globalDestructor(void *value) {
354 pthread_setspecific(oidval, NULL);
357 void initDSMthread(int *ptr) {
364 int p[]={1, 0 /* NULL */, oid};
366 struct listitem litem;
367 pthread_setspecific(litemkey, &litem);
370 //Add our litem to list of threads
372 pthread_mutex_lock(&gclistlock);
377 pthread_mutex_unlock(&gclistlock);
379 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
381 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
383 threadData = calloc(1, sizeof(unsigned int));
384 *((unsigned int *) threadData) = oid;
385 pthread_setspecific(oidval, threadData);
386 pthread_mutex_lock(&gclistlock);
389 pthread_setspecific(threadlocks, litem->locklist);
391 if (litem.prev==NULL) {
394 litem.prev->next=litem.next;
396 if (litem.next!=NULL) {
397 litem.next->prev=litem.prev;
400 pthread_cond_signal(&gccond);
401 pthread_mutex_unlock(&gclistlock);
402 /* Add transaction to check if thread finished for join operation */
407 tmp = transRead((unsigned int) oid);
408 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
409 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
410 if(transCommit()!= 0) {
417 void startDSMthread(int oid, int objType) {
420 pthread_attr_t nattr;
422 pthread_mutex_lock(&gclistlock);
424 pthread_mutex_unlock(&gclistlock);
425 pthread_attr_init(&nattr);
426 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
427 int * ptr=malloc(sizeof(int)*2);
430 pthread_key_create(&oidval, globalDestructor);
432 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
437 pthread_attr_destroy(&nattr);