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;
20 pthread_mutex_t joinlock;
21 pthread_cond_t joincond;
22 pthread_key_t threadlocks;
23 pthread_mutex_t threadnotifylock;
24 pthread_cond_t threadnotifycond;
30 transrecord_t * trans;
31 unsigned int oidvalue;
34 struct ___Object___ *ll=pthread_getspecific(threadlocks);
36 struct ___Object___ *llnext=ll->___nextlockobject___;
37 ll->___nextlockobject___=NULL;
38 ll->___prevlockobject___=NULL;
40 ll->tid=0; //unlock it
43 pthread_mutex_lock(&objlock);//wake everyone up
44 pthread_cond_broadcast(&objcond);
45 pthread_mutex_unlock(&objlock);
47 pthread_mutex_lock(&gclistlock);
49 pthread_cond_signal(&gccond);
50 pthread_mutex_unlock(&gclistlock);
52 /* Add transaction to check if thread finished for join operation */
53 value = pthread_getspecific(oidval);
54 oidvalue = *((unsigned int *)value);
58 transrecord_t * trans = transStart();
59 ptr = transRead(trans, oidvalue);
60 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
61 p->___threadDone___ = 1;
62 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
63 if(transCommit(trans) != 0) {
71 void threadhandler(int sig, siginfo_t *info, void *uap) {
73 printf("sig=%d\n",sig);
79 void initializethreads() {
82 pthread_mutex_init(&gclock, NULL);
83 pthread_mutex_init(&gclistlock, NULL);
84 pthread_cond_init(&gccond, NULL);
85 pthread_mutex_init(&objlock,NULL);
86 pthread_cond_init(&objcond,NULL);
87 pthread_mutex_init(&joinlock,NULL);
88 pthread_cond_init(&joincond,NULL);
89 pthread_key_create(&threadlocks, NULL);
91 initializeexithandler();
93 sig.sa_sigaction=&threadhandler;
94 sig.sa_flags=SA_SIGINFO;
95 sigemptyset(&sig.sa_mask);
97 /* Catch bus errors, segmentation faults, and floating point exceptions*/
98 sigaction(SIGBUS,&sig,0);
99 sigaction(SIGSEGV,&sig,0);
100 sigaction(SIGFPE,&sig,0);
101 signal(SIGPIPE, SIG_IGN);
105 void initthread(struct ___Thread___ * ___this___) {
107 int p[]={1, (int) NULL, (int) ___this___};
108 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
109 ___this___=(struct ___Thread___ *) p[2];
111 ___Thread______staticStart____L___Thread___(___this___);
113 ___this___->___finished___=1;
114 pthread_mutex_lock(&joinlock);
115 pthread_cond_signal(&joincond);
116 pthread_mutex_unlock(&joinlock);
118 pthread_mutex_lock(&gclistlock);
120 pthread_cond_signal(&gccond);
121 pthread_mutex_unlock(&gclistlock);
125 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
128 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
131 usleep(___millis___);
140 /* Add thread join capability */
141 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
142 unsigned int *oidarray;
143 unsigned short *versionarray, version;
144 transrecord_t *trans;
146 /* Add transaction to check if thread finished for join operation */
149 trans = transStart();
150 ptr = transRead(trans, (unsigned int) VAR(___this___));
151 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
152 if(p->___threadDone___ == 1) {
157 version = (ptr-1)->version;
158 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
159 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
163 oidarray[0] = (unsigned int) VAR(___this___);
165 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
166 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
170 versionarray[0] = version;
171 /* Request Notification */
173 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
175 reqNotify(oidarray, versionarray, 1);
189 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
190 pthread_mutex_lock(&joinlock);
191 while(!VAR(___this___)->___finished___)
192 pthread_cond_wait(&joincond, &joinlock);
193 pthread_mutex_unlock(&joinlock);
196 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
199 pthread_attr_t nattr;
201 pthread_mutex_lock(&gclistlock);
203 pthread_mutex_unlock(&gclistlock);
204 pthread_attr_init(&nattr);
205 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
208 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
212 /* This next statement will likely not work on many machines */
214 pthread_attr_destroy(&nattr);
219 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
220 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
225 void globalDestructor(void *value) {
227 pthread_setspecific(oidval, NULL);
230 void initDSMthread(int *ptr) {
232 transrecord_t * trans;
238 int p[]={1, 0 /* NULL */, oid};
239 ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
241 ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
243 threadData = calloc(1, sizeof(unsigned int));
244 *((unsigned int *) threadData) = oid;
245 pthread_setspecific(oidval, threadData);
246 pthread_mutex_lock(&gclistlock);
248 pthread_cond_signal(&gccond);
249 pthread_mutex_unlock(&gclistlock);
250 /* Add transaction to check if thread finished for join operation */
254 transrecord_t * trans = transStart();
255 tmp = transRead(trans, (unsigned int) oid);
256 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
257 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
258 if(transCommit(trans)!= 0) {
265 void startDSMthread(int oid, int objType) {
268 pthread_attr_t nattr;
270 pthread_mutex_lock(&gclistlock);
272 pthread_mutex_unlock(&gclistlock);
273 pthread_attr_init(&nattr);
274 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
275 int * ptr=malloc(sizeof(int)*2);
278 pthread_key_create(&oidval, globalDestructor);
280 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
285 pthread_attr_destroy(&nattr);