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