9 #include <DSTM/interface/dstm.h>
10 #include <DSTM/interface/llookup.h>
16 pthread_mutex_t gclock;
17 pthread_mutex_t gclistlock;
18 pthread_cond_t gccond;
19 pthread_mutex_t objlock;
20 pthread_cond_t objcond;
22 pthread_mutex_t joinlock;
23 pthread_cond_t joincond;
24 pthread_key_t threadlocks;
25 pthread_mutex_t threadnotifylock;
26 pthread_cond_t threadnotifycond;
32 unsigned int oidvalue;
35 struct ___Object___ *ll=pthread_getspecific(threadlocks);
37 struct ___Object___ *llnext=ll->___nextlockobject___;
38 ll->___nextlockobject___=NULL;
39 ll->___prevlockobject___=NULL;
41 ll->tid=0; //unlock it
44 pthread_mutex_lock(&objlock); //wake everyone up
45 pthread_cond_broadcast(&objcond);
46 pthread_mutex_unlock(&objlock);
48 pthread_mutex_lock(&gclistlock);
50 pthread_cond_signal(&gccond);
51 pthread_mutex_unlock(&gclistlock);
53 /* Add transaction to check if thread finished for join operation */
54 value = pthread_getspecific(oidval);
55 oidvalue = *((unsigned int *)value);
60 ptr = transRead(oidvalue);
61 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
62 p->___threadDone___ = 1;
63 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
64 if(transCommit() != 0) {
72 void threadhandler(int sig, siginfo_t *info, void *uap) {
74 printf("sig=%d\n",sig);
80 void initializethreads() {
83 pthread_mutex_init(&gclock, NULL);
84 pthread_mutex_init(&gclistlock, NULL);
85 pthread_cond_init(&gccond, NULL);
86 pthread_mutex_init(&objlock,NULL);
87 pthread_cond_init(&objcond,NULL);
88 pthread_mutex_init(&joinlock,NULL);
89 pthread_cond_init(&joincond,NULL);
90 pthread_key_create(&threadlocks, NULL);
92 initializeexithandler();
94 sig.sa_sigaction=&threadhandler;
95 sig.sa_flags=SA_SIGINFO;
96 sigemptyset(&sig.sa_mask);
98 /* Catch bus errors, segmentation faults, and floating point exceptions*/
99 sigaction(SIGBUS,&sig,0);
100 sigaction(SIGSEGV,&sig,0);
101 sigaction(SIGFPE,&sig,0);
102 signal(SIGPIPE, SIG_IGN);
106 void initthread(struct ___Thread___ * ___this___) {
108 int p[]={1, (int) NULL, (int) ___this___};
109 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
110 ___this___=(struct ___Thread___ *) p[2];
112 ___Thread______staticStart____L___Thread___(___this___);
114 ___this___->___finished___=1;
115 pthread_mutex_lock(&joinlock);
116 pthread_cond_signal(&joincond);
117 pthread_mutex_unlock(&joinlock);
119 pthread_mutex_lock(&gclistlock);
121 pthread_cond_signal(&gccond);
122 pthread_mutex_unlock(&gclistlock);
126 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
129 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
132 usleep(___millis___);
140 #if defined(DSTM)|| defined(THREADS)
141 void CALL00(___Thread______yield____) {
147 /* Add thread join capability */
148 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
149 unsigned int *oidarray;
150 unsigned short *versionarray, version;
152 /* Add transaction to check if thread finished for join operation */
155 ptr = transRead((unsigned int) VAR(___this___));
156 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
157 #ifdef THREADJOINDEBUG
158 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
160 if(p->___threadDone___ == 1) {
161 #ifdef THREADJOINDEBUG
162 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
168 version = (ptr-1)->version;
169 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
170 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
174 oidarray[0] = (unsigned int) VAR(___this___);
176 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
177 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
181 versionarray[0] = version;
182 /* Request Notification */
184 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
186 reqNotify(oidarray, versionarray, 1);
200 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
201 pthread_mutex_lock(&joinlock);
202 while(!VAR(___this___)->___finished___)
203 pthread_cond_wait(&joincond, &joinlock);
204 pthread_mutex_unlock(&joinlock);
207 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
210 pthread_attr_t nattr;
212 pthread_mutex_lock(&gclistlock);
214 pthread_mutex_unlock(&gclistlock);
215 pthread_attr_init(&nattr);
216 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
219 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
223 /* This next statement will likely not work on many machines */
225 pthread_attr_destroy(&nattr);
230 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
231 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
236 void globalDestructor(void *value) {
238 pthread_setspecific(oidval, NULL);
241 void initDSMthread(int *ptr) {
248 int p[]={1, 0 /* NULL */, oid};
249 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
251 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
253 threadData = calloc(1, sizeof(unsigned int));
254 *((unsigned int *) threadData) = oid;
255 pthread_setspecific(oidval, threadData);
256 pthread_mutex_lock(&gclistlock);
258 pthread_cond_signal(&gccond);
259 pthread_mutex_unlock(&gclistlock);
260 /* Add transaction to check if thread finished for join operation */
265 tmp = transRead((unsigned int) oid);
266 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
267 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
268 if(transCommit()!= 0) {
275 void startDSMthread(int oid, int objType) {
278 pthread_attr_t nattr;
280 pthread_mutex_lock(&gclistlock);
282 pthread_mutex_unlock(&gclistlock);
283 pthread_attr_init(&nattr);
284 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
285 int * ptr=malloc(sizeof(int)*2);
288 pthread_key_create(&oidval, globalDestructor);
290 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
295 pthread_attr_destroy(&nattr);