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 ptr = transRead(trans, oidvalue);
56 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
57 p->___threadDone___ = 1;
58 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
59 if(transCommit(trans) != 0) {
66 void threadhandler(int sig, siginfo_t *info, void *uap) {
68 printf("sig=%d\n",sig);
74 void initializethreads() {
77 pthread_mutex_init(&gclock, NULL);
78 pthread_mutex_init(&gclistlock, NULL);
79 pthread_cond_init(&gccond, NULL);
80 pthread_mutex_init(&objlock,NULL);
81 pthread_cond_init(&objcond,NULL);
82 pthread_key_create(&threadlocks, NULL);
84 initializeexithandler();
86 sig.sa_sigaction=&threadhandler;
87 sig.sa_flags=SA_SIGINFO;
88 sigemptyset(&sig.sa_mask);
90 /* Catch bus errors, segmentation faults, and floating point exceptions*/
91 sigaction(SIGBUS,&sig,0);
92 sigaction(SIGSEGV,&sig,0);
93 sigaction(SIGFPE,&sig,0);
94 signal(SIGPIPE, SIG_IGN);
98 void initthread(struct ___Thread___ * ___this___) {
100 struct ___Thread______staticStart____L___Thread____params p={1, NULL, ___this___};
101 ___Thread______staticStart____L___Thread___(&p);
103 ___Thread______staticStart____L___Thread___(___this___);
105 pthread_mutex_lock(&gclistlock);
107 pthread_cond_signal(&gccond);
108 pthread_mutex_unlock(&gclistlock);
112 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
115 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
118 usleep(___millis___);
127 /* Add thread join capability */
128 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
129 unsigned int *oidarray;
130 unsigned short *versionarray, version;
131 transrecord_t *trans;
133 /* Add transaction to check if thread finished for join operation */
136 trans = transStart();
137 ptr = transRead(trans, (unsigned int) VAR(___this___));
138 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
139 if(p->___threadDone___ == 1) {
143 version = (ptr-1)->version;
144 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
145 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
149 oidarray[0] = (unsigned int) VAR(___this___);
151 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
152 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
156 versionarray[0] = version;
157 /* Request Notification */
158 reqNotify(oidarray, versionarray, 1);
169 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
172 pthread_attr_t nattr;
174 pthread_mutex_lock(&gclistlock);
176 pthread_mutex_unlock(&gclistlock);
177 pthread_attr_init(&nattr);
178 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
181 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
186 pthread_attr_destroy(&nattr);
191 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
192 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
197 void globalDestructor(void *value) {
199 pthread_setspecific(oidval, NULL);
202 void initDSMthread(int *ptr) {
204 transrecord_t * trans;
210 int p[]={1, 0 /* NULL */, oid};
211 ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
213 ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
215 threadData = calloc(1, sizeof(unsigned int));
216 *((unsigned int *) threadData) = oid;
217 pthread_setspecific(oidval, threadData);
218 pthread_mutex_lock(&gclistlock);
220 pthread_cond_signal(&gccond);
221 pthread_mutex_unlock(&gclistlock);
222 /* Add transaction to check if thread finished for join operation */
225 trans = transStart();
226 tmp = transRead(trans, (unsigned int) oid);
227 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
228 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
229 if(transCommit(trans)!= 0) {
235 void startDSMthread(int oid, int objType) {
238 pthread_attr_t nattr;
240 pthread_mutex_lock(&gclistlock);
242 pthread_mutex_unlock(&gclistlock);
243 pthread_attr_init(&nattr);
244 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
245 int * ptr=malloc(sizeof(int)*2);
248 pthread_key_create(&oidval, globalDestructor);
250 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
255 pthread_attr_destroy(&nattr);