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