9 #include <DSTM/interface/dstm.h>
10 #include <DSTM/interface/llookup.h>
14 pthread_mutex_t gclock;
15 pthread_mutex_t gclistlock;
16 pthread_cond_t gccond;
17 pthread_mutex_t objlock;
18 pthread_cond_t objcond;
19 pthread_key_t threadlocks;
20 pthread_mutex_t threadnotifylock;
21 pthread_cond_t threadnotifycond;
27 transrecord_t * trans;
28 unsigned int oidvalue;
31 struct ___Object___ *ll=pthread_getspecific(threadlocks);
33 struct ___Object___ *llnext=ll->___nextlockobject___;
34 ll->___nextlockobject___=NULL;
35 ll->___prevlockobject___=NULL;
37 ll->tid=0; //unlock it
40 pthread_mutex_lock(&objlock);//wake everyone up
41 pthread_cond_broadcast(&objcond);
42 pthread_mutex_unlock(&objlock);
44 pthread_mutex_lock(&gclistlock);
46 pthread_cond_signal(&gccond);
47 pthread_mutex_unlock(&gclistlock);
49 /* Add transaction to check if thread finished for join operation */
50 value = pthread_getspecific(oidval);
51 oidvalue = *((unsigned int *)value);
55 transrecord_t * trans = transStart();
56 ptr = transRead(trans, oidvalue);
57 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
58 p->___threadDone___ = 1;
59 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
60 if(transCommit(trans) != 0) {
68 void threadhandler(int sig, siginfo_t *info, void *uap) {
70 printf("sig=%d\n",sig);
76 void initializethreads() {
79 pthread_mutex_init(&gclock, NULL);
80 pthread_mutex_init(&gclistlock, NULL);
81 pthread_cond_init(&gccond, NULL);
82 pthread_mutex_init(&objlock,NULL);
83 pthread_cond_init(&objcond,NULL);
84 pthread_key_create(&threadlocks, NULL);
86 initializeexithandler();
88 sig.sa_sigaction=&threadhandler;
89 sig.sa_flags=SA_SIGINFO;
90 sigemptyset(&sig.sa_mask);
92 /* Catch bus errors, segmentation faults, and floating point exceptions*/
93 sigaction(SIGBUS,&sig,0);
94 sigaction(SIGSEGV,&sig,0);
95 sigaction(SIGFPE,&sig,0);
96 signal(SIGPIPE, SIG_IGN);
100 void initthread(struct ___Thread___ * ___this___) {
102 struct ___Thread______staticStart____L___Thread____params p={1, NULL, ___this___};
103 ___Thread______staticStart____L___Thread___(&p);
105 ___Thread______staticStart____L___Thread___(___this___);
107 pthread_mutex_lock(&gclistlock);
109 pthread_cond_signal(&gccond);
110 pthread_mutex_unlock(&gclistlock);
114 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
117 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
120 usleep(___millis___);
129 /* Add thread join capability */
130 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
131 unsigned int *oidarray;
132 unsigned short *versionarray, version;
133 transrecord_t *trans;
135 /* Add transaction to check if thread finished for join operation */
138 trans = transStart();
139 ptr = transRead(trans, (unsigned int) VAR(___this___));
140 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
141 if(p->___threadDone___ == 1) {
145 version = (ptr-1)->version;
146 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
147 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
151 oidarray[0] = (unsigned int) VAR(___this___);
153 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
154 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
158 versionarray[0] = version;
159 /* Request Notification */
161 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
163 reqNotify(oidarray, versionarray, 1);
177 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
180 pthread_attr_t nattr;
182 pthread_mutex_lock(&gclistlock);
184 pthread_mutex_unlock(&gclistlock);
185 pthread_attr_init(&nattr);
186 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
189 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
194 pthread_attr_destroy(&nattr);
199 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
200 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
205 void globalDestructor(void *value) {
207 pthread_setspecific(oidval, NULL);
210 void initDSMthread(int *ptr) {
212 transrecord_t * trans;
218 int p[]={1, 0 /* NULL */, oid};
219 ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
221 ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
223 threadData = calloc(1, sizeof(unsigned int));
224 *((unsigned int *) threadData) = oid;
225 pthread_setspecific(oidval, threadData);
226 pthread_mutex_lock(&gclistlock);
228 pthread_cond_signal(&gccond);
229 pthread_mutex_unlock(&gclistlock);
230 /* Add transaction to check if thread finished for join operation */
234 transrecord_t * trans = transStart();
235 tmp = transRead(trans, (unsigned int) oid);
236 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
237 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
238 if(transCommit(trans)!= 0) {
245 void startDSMthread(int oid, int objType) {
248 pthread_attr_t nattr;
250 pthread_mutex_lock(&gclistlock);
252 pthread_mutex_unlock(&gclistlock);
253 pthread_attr_init(&nattr);
254 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
255 int * ptr=malloc(sizeof(int)*2);
258 pthread_key_create(&oidval, globalDestructor);
260 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
265 pthread_attr_destroy(&nattr);