bug fixes
[IRC.git] / Robust / src / Runtime / thread.c
1 #include "runtime.h"
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include "thread.h"
7 #include "option.h"
8 #include <signal.h>
9
10 #ifdef DSTM
11 #include <DSTM/interface/dstm.h>
12 #include <DSTM/interface/llookup.h>
13 #endif
14
15 #ifndef RAW
16 #include <stdio.h>
17 #endif
18 int threadcount;
19 pthread_mutex_t gclock;
20 pthread_mutex_t gclistlock;
21 pthread_cond_t gccond;
22 pthread_mutex_t objlock;
23 pthread_cond_t objcond;
24
25 pthread_mutex_t joinlock;
26 pthread_cond_t joincond;
27 pthread_key_t threadlocks;
28 pthread_mutex_t threadnotifylock;
29 pthread_cond_t threadnotifycond;
30 pthread_key_t oidval;
31
32 void threadexit() {
33 #ifdef DSTM
34   objheader_t* ptr;
35   unsigned int oidvalue;
36 #endif
37   void *value;
38
39 #ifdef THREADS
40   struct ___Object___ *ll=pthread_getspecific(threadlocks);
41   while(ll!=NULL) {
42     struct ___Object___ *llnext=ll->___nextlockobject___;
43     ll->___nextlockobject___=NULL;
44     ll->___prevlockobject___=NULL;
45     ll->lockcount=0;
46     ll->tid=0; //unlock it
47     ll=llnext;
48   }
49   pthread_mutex_lock(&objlock); //wake everyone up
50   pthread_cond_broadcast(&objcond);
51   pthread_mutex_unlock(&objlock);
52 #endif
53   pthread_mutex_lock(&gclistlock);
54   threadcount--;
55   pthread_cond_signal(&gccond);
56   pthread_mutex_unlock(&gclistlock);
57 #ifdef DSTM
58   /* Add transaction to check if thread finished for join operation */
59   value = pthread_getspecific(oidval);
60   oidvalue = *((unsigned int *)value);
61   goto transstart;
62 transstart:
63   {
64     transStart();
65     ptr = transRead(oidvalue);
66     struct ___Thread___ *p = (struct ___Thread___ *) ptr;
67     p->___threadDone___ = 1;
68     *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
69     if(transCommit() != 0) {
70       goto transstart;
71     }
72   }
73 #endif
74   pthread_exit(NULL);
75 }
76
77 void threadhandler(int sig, siginfo_t *info, void *uap) {
78 #ifdef DEBUG
79   printf("sig=%d\n",sig);
80   printf("signal\n");
81 #endif
82   threadexit();
83 }
84
85 void initializethreads() {
86   struct sigaction sig;
87   threadcount=1;
88   pthread_mutex_init(&gclock, NULL);
89   pthread_mutex_init(&gclistlock, NULL);
90   pthread_cond_init(&gccond, NULL);
91   pthread_mutex_init(&objlock,NULL);
92   pthread_cond_init(&objcond,NULL);
93   pthread_mutex_init(&joinlock,NULL);
94   pthread_cond_init(&joincond,NULL);
95   pthread_key_create(&threadlocks, NULL);
96   processOptions();
97   initializeexithandler();
98
99   sig.sa_sigaction=&threadhandler;
100   sig.sa_flags=SA_SIGINFO;
101   sigemptyset(&sig.sa_mask);
102
103   /* Catch bus errors, segmentation faults, and floating point exceptions*/
104   sigaction(SIGBUS,&sig,0);
105   sigaction(SIGSEGV,&sig,0);
106   sigaction(SIGFPE,&sig,0);
107   signal(SIGPIPE, SIG_IGN);
108 #ifdef STM
109   newobjs=calloc(1, sizeof(struct objlist));
110 #endif
111 }
112
113 #if defined(THREADS)||defined(STM)
114 void initthread(struct ___Thread___ * ___this___) {
115 #ifdef PRECISE_GC
116   int p[]={1, (int) NULL, (int) ___this___};
117 #ifdef THREADS
118   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
119 #else
120   newobjs=calloc(1, sizeof(struct objlist));
121  ___Thread____NN____staticStart____L___Thread___((struct ___Thread____NN____staticStart____L___Thread____params *)p);
122   free(newobjs);
123 #endif
124   ___this___=(struct ___Thread___ *) p[2];
125 #else
126   ___Thread______staticStart____L___Thread___(___this___);
127 #endif
128   ___this___->___finished___=1;
129   pthread_mutex_lock(&joinlock);
130   pthread_cond_signal(&joincond);
131   pthread_mutex_unlock(&joinlock);
132
133   pthread_mutex_lock(&gclistlock);
134   threadcount--;
135   pthread_cond_signal(&gccond);
136   pthread_mutex_unlock(&gclistlock);
137 }
138 #endif
139
140 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
141 #if defined(THREADS)||defined(STM)
142 #ifdef PRECISE_GC
143   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
144 #endif
145 #endif
146   usleep(___millis___);
147 #if defined(THREADS)||defined(STM)
148 #ifdef PRECISE_GC
149   restartaftergc(tmp);
150 #endif
151 #endif
152 }
153
154 #if defined(DSTM)|| defined(THREADS)||defined(STM)
155 void CALL00(___Thread______yield____) {
156   pthread_yield();
157 }
158 #endif
159
160 #ifdef DSTM
161 /* Add thread join capability */
162 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
163   unsigned int *oidarray;
164   unsigned short *versionarray, version;
165   objheader_t *ptr;
166   /* Add transaction to check if thread finished for join operation */
167 transstart:
168   transStart();
169   ptr = transRead((unsigned int) VAR(___this___));
170   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
171 #ifdef THREADJOINDEBUG
172   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
173 #endif
174   if(p->___threadDone___ == 1) {
175 #ifdef THREADJOINDEBUG
176     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
177 #endif
178     transAbort();
179     return;
180   } else {
181
182     version = (ptr-1)->version;
183     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
184       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
185       return;
186     }
187
188     oidarray[0] = (unsigned int) VAR(___this___);
189
190     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
191       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
192       free(oidarray);
193       return;
194     }
195     versionarray[0] = version;
196     /* Request Notification */
197 #ifdef PRECISE_GC
198     struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
199 #endif
200     reqNotify(oidarray, versionarray, 1);
201 #ifdef PRECISE_GC
202     restartaftergc(tmp);
203 #endif
204     free(oidarray);
205     free(versionarray);
206     transAbort();
207     goto transstart;
208   }
209   return;
210 }
211 #endif
212
213 #if defined(THREADS)||defined(STM)
214 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
215 #ifdef PRECISE_GC
216     struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
217 #endif
218   pthread_mutex_lock(&joinlock);
219   while(!VAR(___this___)->___finished___)
220     pthread_cond_wait(&joincond, &joinlock);
221   pthread_mutex_unlock(&joinlock);
222 #ifdef PRECISE_GC
223     restartaftergc(tmp);
224 #endif
225
226 }
227
228 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
229   pthread_t thread;
230   int retval;
231   pthread_attr_t nattr;
232
233   pthread_mutex_lock(&gclistlock);
234   threadcount++;
235   pthread_mutex_unlock(&gclistlock);
236   pthread_attr_init(&nattr);
237   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
238
239   do {
240     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
241     if (retval!=0)
242       usleep(1);
243   } while(retval!=0);
244   /* This next statement will likely not work on many machines */
245
246   pthread_attr_destroy(&nattr);
247 }
248 #endif
249
250 #ifdef DSTM
251 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
252   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
253 }
254 #endif
255
256 #ifdef DSTM
257 void globalDestructor(void *value) {
258   free(value);
259   pthread_setspecific(oidval, NULL);
260 }
261
262 void initDSMthread(int *ptr) {
263   objheader_t *tmp;
264   void *threadData;
265   int oid=ptr[0];
266   int type=ptr[1];
267   free(ptr);
268 #ifdef PRECISE_GC
269   int p[]={1, 0 /* NULL */, oid};
270   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
271 #else
272   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
273 #endif
274   threadData = calloc(1, sizeof(unsigned int));
275   *((unsigned int *) threadData) = oid;
276   pthread_setspecific(oidval, threadData);
277   pthread_mutex_lock(&gclistlock);
278   threadcount--;
279   pthread_cond_signal(&gccond);
280   pthread_mutex_unlock(&gclistlock);
281   /* Add transaction to check if thread finished for join operation */
282   goto transstart;
283 transstart:
284   {
285     transStart();
286     tmp  = transRead((unsigned int) oid);
287     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
288     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
289     if(transCommit()!= 0) {
290       goto transstart;
291     }
292   }
293   pthread_exit(NULL);
294 }
295
296 void startDSMthread(int oid, int objType) {
297   pthread_t thread;
298   int retval;
299   pthread_attr_t nattr;
300
301   pthread_mutex_lock(&gclistlock);
302   threadcount++;
303   pthread_mutex_unlock(&gclistlock);
304   pthread_attr_init(&nattr);
305   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
306   int * ptr=malloc(sizeof(int)*2);
307   ptr[0]=oid;
308   ptr[1]=objType;
309   pthread_key_create(&oidval, globalDestructor);
310   do {
311     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
312     if (retval!=0)
313       usleep(1);
314   } while(retval!=0);
315
316   pthread_attr_destroy(&nattr);
317 }
318
319 #endif