bug fix
[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 #include <execinfo.h>
22
23
24 int threadcount;
25 pthread_mutex_t gclock;
26 pthread_mutex_t gclistlock;
27 pthread_cond_t gccond;
28 pthread_mutex_t objlock;
29 pthread_cond_t objcond;
30
31 pthread_mutex_t atomiclock;
32
33 pthread_mutex_t joinlock;
34 pthread_cond_t joincond;
35 pthread_key_t threadlocks;
36 pthread_mutex_t threadnotifylock;
37 pthread_cond_t threadnotifycond;
38 pthread_key_t oidval;
39
40 #if defined(THREADS) || defined(DSTM) || defined(STM)
41 #ifndef MAC
42 extern __thread struct listitem litem;
43 #else
44 pthread_key_t litemkey;
45 #endif
46 extern struct listitem * list;
47 #endif
48
49 void threadexit() {
50 #ifdef DSTM
51   objheader_t* ptr;
52   unsigned int oidvalue;
53 #endif
54   void *value;
55
56 #ifdef THREADS
57   struct ___Object___ *ll=pthread_getspecific(threadlocks);
58   while(ll!=NULL) {
59     struct ___Object___ *llnext=ll->___nextlockobject___;
60     ll->___nextlockobject___=NULL;
61     ll->___prevlockobject___=NULL;
62     ll->lockcount=0;
63     ll->tid=0; //unlock it
64     ll=llnext;
65   }
66   pthread_mutex_lock(&objlock); //wake everyone up
67   pthread_cond_broadcast(&objcond);
68   pthread_mutex_unlock(&objlock);
69 #endif
70   pthread_mutex_lock(&gclistlock);
71 #ifdef THREADS
72   pthread_setspecific(threadlocks, litem.locklist);
73 #endif
74 #ifndef MAC
75   if (litem.prev==NULL) {
76     list=litem.next;
77   } else {
78     litem.prev->next=litem.next;
79   }
80   if (litem.next!=NULL) {
81     litem.next->prev=litem.prev;
82   }
83 #else
84   {
85     struct listitem *litem=pthread_getspecific(litemkey);
86     if (litem->prev==NULL) {
87       list=litem->next;
88     } else {
89       litem->prev->next=litem->next;
90     }
91     if (litem->next!=NULL) {
92       litem->next->prev=litem->prev;
93     }
94   }
95 #endif
96   threadcount--;
97   pthread_cond_signal(&gccond);
98   pthread_mutex_unlock(&gclistlock);
99 #ifdef DSTM
100   /* Add transaction to check if thread finished for join operation */
101   value = pthread_getspecific(oidval);
102   oidvalue = *((unsigned int *)value);
103   goto transstart;
104 transstart:
105   {
106     transStart();
107     ptr = transRead(oidvalue);
108     struct ___Thread___ *p = (struct ___Thread___ *) ptr;
109     p->___threadDone___ = 1;
110     *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
111     if(transCommit() != 0) {
112       goto transstart;
113     }
114   }
115 #endif
116   pthread_exit(NULL);
117 }
118
119 void threadhandler(int sig, struct sigcontext ctx) {
120   void *buffer[100];
121   char **strings;
122   int nptrs,j;
123
124   printf("We just took sig=%d\n",sig);
125   printf("signal\n");
126   printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
127   nptrs = backtrace(buffer, 100);
128   buffer[1]=(void *)ctx.eip;
129   strings = backtrace_symbols(buffer, nptrs);
130   if (strings == NULL) {
131     perror("backtrace_symbols");
132     exit(EXIT_FAILURE);
133   }
134   
135   for (j = 0; j < nptrs; j++)
136     printf("%s\n", strings[j]);
137   
138   threadexit();
139 }
140
141 void initializethreads() {
142   struct sigaction sig;
143   threadcount=1;
144 #ifdef THREADS
145   pthread_mutex_init(&atomiclock, NULL);
146 #endif
147   pthread_mutex_init(&gclock, NULL);
148   pthread_mutex_init(&gclistlock, NULL);
149   pthread_cond_init(&gccond, NULL);
150   pthread_mutex_init(&objlock,NULL);
151   pthread_cond_init(&objcond,NULL);
152   pthread_mutex_init(&joinlock,NULL);
153   pthread_cond_init(&joincond,NULL);
154   pthread_key_create(&threadlocks, NULL);
155 #ifdef MAC
156   pthread_key_create(&litem, NULL);
157 #endif
158   processOptions();
159   initializeexithandler();
160
161   //deprecated use of sighandler, but apparently still works
162   sig.sa_handler=(void *)threadhandler;
163   sig.sa_flags=SA_RESTART;
164   sigemptyset(&sig.sa_mask);
165
166   /* Catch bus errors, segmentation faults, and floating point exceptions*/
167   sigaction(SIGBUS,&sig,0);
168   sigaction(SIGSEGV,&sig,0);
169   sigaction(SIGFPE,&sig,0);
170   signal(SIGPIPE, SIG_IGN);
171 #ifdef STM
172   newobjs=calloc(1, sizeof(struct objlist));
173   t_cache = objstrCreate(1048576);
174   t_reserve=NULL;
175   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
176 #ifdef DELAYCOMP
177   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
178 #endif
179 #ifdef STMSTATS
180   trec=calloc(1, sizeof(threadrec_t));
181   trec->blocked = 0;
182   lockedobjs=calloc(1, sizeof(struct objlist));
183   objlockscope = calloc(1, sizeof(objlockstate_t));
184   pthread_mutex_init(&lockedobjstore, NULL);
185   { 
186     int i;
187     for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
188       typesCausingAbort[i] = 0;
189     }
190   }
191 #endif
192 #endif
193 #ifdef MAC
194   struct listitem *litem=malloc(sizeof(struct listitem));
195   pthread_setspecific(litemkey, litem);
196   litem->prev=NULL;
197   litem->next=list;
198   if(list!=NULL)
199     list->prev=litem;
200   list=litem;
201 #else
202   //Add our litem to list of threads
203   litem.prev=NULL;
204   litem.next=list;
205   if(list!=NULL)
206     list->prev=&litem;
207   list=&litem;
208 #endif
209 }
210
211 #if defined(THREADS)||defined(STM)
212 void initthread(struct ___Thread___ * ___this___) {
213 #ifdef PRECISE_GC
214   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
215   //Add our litem to list of threads
216 #ifdef MAC
217   struct listitem litem;
218   pthread_setspecific(litemkey, &litem);
219 #endif
220   litem.prev=NULL;
221   pthread_mutex_lock(&gclistlock);
222   litem.next=list;
223   if(list!=NULL)
224     list->prev=&litem;
225   list=&litem;
226   pthread_mutex_unlock(&gclistlock);
227   
228 #ifdef THREADS
229   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
230 #else
231   newobjs=calloc(1, sizeof(struct objlist));
232 #ifdef STMSTATS
233   trec=calloc(1, sizeof(threadrec_t));
234   trec->blocked = 0;
235   lockedobjs=calloc(1, sizeof(struct objlist));
236 #endif
237   t_cache = objstrCreate(1048576);
238   t_reserve=NULL;
239   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
240 #ifdef DELAYCOMP
241   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
242 #endif
243  ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
244  objstrDelete(t_cache);
245  objstrDelete(t_reserve);
246  t_chashDelete();
247  free(newobjs);
248 #ifdef STMSTATS
249  free(lockedobjs);
250 #endif
251 #endif
252   ___this___=(struct ___Thread___ *) p[2];
253 #else
254   ___Thread______staticStart____L___Thread___(___this___);
255 #endif
256   ___this___->___finished___=1;
257   pthread_mutex_lock(&joinlock);
258   pthread_cond_signal(&joincond);
259   pthread_mutex_unlock(&joinlock);
260
261   pthread_mutex_lock(&gclistlock);
262 #ifdef THREADS
263   pthread_setspecific(threadlocks, litem.locklist);
264 #endif
265   if (litem.prev==NULL) {
266     list=litem.next;
267   } else {
268     litem.prev->next=litem.next;
269   }
270   if (litem.next!=NULL) {
271     litem.next->prev=litem.prev;
272   }
273   threadcount--;
274   pthread_cond_signal(&gccond);
275   pthread_mutex_unlock(&gclistlock);
276 }
277 #endif
278
279 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
280 #if defined(THREADS)||defined(STM)
281 #ifdef PRECISE_GC
282   stopforgc((struct garbagelist *)___params___);
283 #endif
284 #endif
285   usleep(___millis___);
286 #if defined(THREADS)||defined(STM)
287 #ifdef PRECISE_GC
288   restartaftergc();
289 #endif
290 #endif
291 }
292
293 #if defined(DSTM)|| defined(THREADS)||defined(STM)
294 void CALL00(___Thread______yield____) {
295   pthread_yield();
296 }
297 #endif
298
299 #ifdef DSTM
300 /* Add thread join capability */
301 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
302   unsigned int *oidarray;
303   unsigned short *versionarray, version;
304   objheader_t *ptr;
305   /* Add transaction to check if thread finished for join operation */
306 transstart:
307   transStart();
308   ptr = transRead((unsigned int) VAR(___this___));
309   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
310 #ifdef THREADJOINDEBUG
311   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
312 #endif
313   if(p->___threadDone___ == 1) {
314 #ifdef THREADJOINDEBUG
315     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
316 #endif
317     transAbort();
318     return;
319   } else {
320
321     version = (ptr-1)->version;
322     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
323       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
324       return;
325     }
326
327     oidarray[0] = (unsigned int) VAR(___this___);
328
329     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
330       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
331       free(oidarray);
332       return;
333     }
334     versionarray[0] = version;
335     /* Request Notification */
336 #ifdef PRECISE_GC
337     stopforgc((struct garbagelist *)___params___);
338 #endif
339     reqNotify(oidarray, versionarray, 1);
340 #ifdef PRECISE_GC
341     restartaftergc();
342 #endif
343     free(oidarray);
344     free(versionarray);
345     transAbort();
346     goto transstart;
347   }
348   return;
349 }
350 #endif
351
352 #if defined(THREADS)||defined(STM)
353 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
354   pthread_mutex_lock(&joinlock);
355   while(!VAR(___this___)->___finished___) {
356 #ifdef PRECISE_GC
357   stopforgc((struct garbagelist *)___params___);
358 #endif
359     pthread_cond_wait(&joincond, &joinlock);
360 #ifdef PRECISE_GC
361     restartaftergc();
362 #endif
363   }
364   pthread_mutex_unlock(&joinlock);
365 }
366
367 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
368   pthread_t thread;
369   int retval;
370   pthread_attr_t nattr;
371
372   pthread_mutex_lock(&gclistlock);
373   threadcount++;
374   pthread_mutex_unlock(&gclistlock);
375   pthread_attr_init(&nattr);
376   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
377
378   do {
379     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
380     if (retval!=0)
381       usleep(1);
382   } while(retval!=0);
383   /* This next statement will likely not work on many machines */
384
385   pthread_attr_destroy(&nattr);
386 }
387 #endif
388
389 #ifdef DSTM
390 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
391   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
392 }
393 #endif
394
395 #ifdef DSTM
396 void globalDestructor(void *value) {
397   free(value);
398   pthread_setspecific(oidval, NULL);
399 }
400
401 void initDSMthread(int *ptr) {
402   objheader_t *tmp;
403   void *threadData;
404   int oid=ptr[0];
405   int type=ptr[1];
406   free(ptr);
407 #ifdef PRECISE_GC
408   int p[]={1, 0 /* NULL */, oid};
409 #ifdef MAC
410   struct listitem litem;
411   pthread_setspecific(litemkey, &litem);
412 #endif
413
414   //Add our litem to list of threads
415   litem.prev=NULL;
416   pthread_mutex_lock(&gclistlock);
417   litem.next=list;
418   if(list!=NULL)
419     list->prev=&litem;
420   list=&litem;
421   pthread_mutex_unlock(&gclistlock);
422
423   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
424 #else
425   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
426 #endif
427   threadData = calloc(1, sizeof(unsigned int));
428   *((unsigned int *) threadData) = oid;
429   pthread_setspecific(oidval, threadData);
430   pthread_mutex_lock(&gclistlock);
431
432 #ifdef THREADS
433   pthread_setspecific(threadlocks, litem.locklist);
434 #endif
435   if (litem.prev==NULL) {
436     list=litem.next;
437   } else {
438     litem.prev->next=litem.next;
439   }
440   if (litem.next!=NULL) {
441     litem.next->prev=litem.prev;
442   }
443   threadcount--;
444   pthread_cond_signal(&gccond);
445   pthread_mutex_unlock(&gclistlock);
446   /* Add transaction to check if thread finished for join operation */
447   goto transstart;
448 transstart:
449   {
450     transStart();
451     tmp  = transRead((unsigned int) oid);
452     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
453     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
454     if(transCommit()!= 0) {
455       goto transstart;
456     }
457   }
458   pthread_exit(NULL);
459 }
460
461 void startDSMthread(int oid, int objType) {
462   pthread_t thread;
463   int retval;
464   pthread_attr_t nattr;
465
466   pthread_mutex_lock(&gclistlock);
467   threadcount++;
468   pthread_mutex_unlock(&gclistlock);
469   pthread_attr_init(&nattr);
470   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
471   int * ptr=malloc(sizeof(int)*2);
472   ptr[0]=oid;
473   ptr[1]=objType;
474   pthread_key_create(&oidval, globalDestructor);
475   do {
476     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
477     if (retval!=0)
478       usleep(1);
479   } while(retval!=0);
480
481   pthread_attr_destroy(&nattr);
482 }
483
484 #endif