guard stuff
[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 }
109
110 #ifdef THREADS
111 void initthread(struct ___Thread___ * ___this___) {
112 #ifdef PRECISE_GC
113   int p[]={1, (int) NULL, (int) ___this___};
114   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
115   ___this___=(struct ___Thread___ *) p[2];
116 #else
117   ___Thread______staticStart____L___Thread___(___this___);
118 #endif
119   ___this___->___finished___=1;
120   pthread_mutex_lock(&joinlock);
121   pthread_cond_signal(&joincond);
122   pthread_mutex_unlock(&joinlock);
123
124   pthread_mutex_lock(&gclistlock);
125   threadcount--;
126   pthread_cond_signal(&gccond);
127   pthread_mutex_unlock(&gclistlock);
128 }
129 #endif
130
131 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
132 #ifdef THREADS
133 #ifdef PRECISE_GC
134   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
135 #endif
136 #endif
137   usleep(___millis___);
138 #ifdef THREADS
139 #ifdef PRECISE_GC
140   restartaftergc(tmp);
141 #endif
142 #endif
143 }
144
145 #if defined(DSTM)|| defined(THREADS)
146 void CALL00(___Thread______yield____) {
147   pthread_yield();
148 }
149 #endif
150
151 #ifdef DSTM
152 /* Add thread join capability */
153 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
154   unsigned int *oidarray;
155   unsigned short *versionarray, version;
156   objheader_t *ptr;
157   /* Add transaction to check if thread finished for join operation */
158 transstart:
159   transStart();
160   ptr = transRead((unsigned int) VAR(___this___));
161   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
162 #ifdef THREADJOINDEBUG
163   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
164 #endif
165   if(p->___threadDone___ == 1) {
166 #ifdef THREADJOINDEBUG
167     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
168 #endif
169     transAbort();
170     return;
171   } else {
172
173     version = (ptr-1)->version;
174     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
175       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
176       return;
177     }
178
179     oidarray[0] = (unsigned int) VAR(___this___);
180
181     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
182       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
183       free(oidarray);
184       return;
185     }
186     versionarray[0] = version;
187     /* Request Notification */
188 #ifdef PRECISE_GC
189     struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
190 #endif
191     reqNotify(oidarray, versionarray, 1);
192 #ifdef PRECISE_GC
193     restartaftergc(tmp);
194 #endif
195     free(oidarray);
196     free(versionarray);
197     transAbort();
198     goto transstart;
199   }
200   return;
201 }
202 #endif
203
204 #ifdef THREADS
205 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
206   pthread_mutex_lock(&joinlock);
207   while(!VAR(___this___)->___finished___)
208     pthread_cond_wait(&joincond, &joinlock);
209   pthread_mutex_unlock(&joinlock);
210 }
211
212 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
213   pthread_t thread;
214   int retval;
215   pthread_attr_t nattr;
216
217   pthread_mutex_lock(&gclistlock);
218   threadcount++;
219   pthread_mutex_unlock(&gclistlock);
220   pthread_attr_init(&nattr);
221   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
222
223   do {
224     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
225     if (retval!=0)
226       usleep(1);
227   } while(retval!=0);
228   /* This next statement will likely not work on many machines */
229
230   pthread_attr_destroy(&nattr);
231 }
232 #endif
233
234 #ifdef DSTM
235 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
236   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
237 }
238 #endif
239
240 #ifdef DSTM
241 void globalDestructor(void *value) {
242   free(value);
243   pthread_setspecific(oidval, NULL);
244 }
245
246 void initDSMthread(int *ptr) {
247   objheader_t *tmp;
248   void *threadData;
249   int oid=ptr[0];
250   int type=ptr[1];
251   free(ptr);
252 #ifdef PRECISE_GC
253   int p[]={1, 0 /* NULL */, oid};
254   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
255 #else
256   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
257 #endif
258   threadData = calloc(1, sizeof(unsigned int));
259   *((unsigned int *) threadData) = oid;
260   pthread_setspecific(oidval, threadData);
261   pthread_mutex_lock(&gclistlock);
262   threadcount--;
263   pthread_cond_signal(&gccond);
264   pthread_mutex_unlock(&gclistlock);
265   /* Add transaction to check if thread finished for join operation */
266   goto transstart;
267 transstart:
268   {
269     transStart();
270     tmp  = transRead((unsigned int) oid);
271     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
272     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
273     if(transCommit()!= 0) {
274       goto transstart;
275     }
276   }
277   pthread_exit(NULL);
278 }
279
280 void startDSMthread(int oid, int objType) {
281   pthread_t thread;
282   int retval;
283   pthread_attr_t nattr;
284
285   pthread_mutex_lock(&gclistlock);
286   threadcount++;
287   pthread_mutex_unlock(&gclistlock);
288   pthread_attr_init(&nattr);
289   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
290   int * ptr=malloc(sizeof(int)*2);
291   ptr[0]=oid;
292   ptr[1]=objType;
293   pthread_key_create(&oidval, globalDestructor);
294   do {
295     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
296     if (retval!=0)
297       usleep(1);
298   } while(retval!=0);
299
300   pthread_attr_destroy(&nattr);
301 }
302
303 #endif