8582a0165f6d9988436bba512ab545390852455b
[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 struct primitivelist *pl;
142
143 void initializethreads() {
144   struct sigaction sig;
145   threadcount=1;
146 #ifdef THREADS
147   pthread_mutex_init(&atomiclock, NULL);
148 #endif
149   pthread_mutex_init(&gclock, NULL);
150   pthread_mutex_init(&gclistlock, NULL);
151   pthread_cond_init(&gccond, NULL);
152   pthread_mutex_init(&objlock,NULL);
153   pthread_cond_init(&objcond,NULL);
154   pthread_mutex_init(&joinlock,NULL);
155   pthread_cond_init(&joincond,NULL);
156   pthread_key_create(&threadlocks, NULL);
157 #ifdef MAC
158   pthread_key_create(&litem, NULL);
159 #endif
160   processOptions();
161   initializeexithandler();
162
163   //deprecated use of sighandler, but apparently still works
164   sig.sa_handler=(void *)threadhandler;
165   sig.sa_flags=SA_RESTART;
166   sigemptyset(&sig.sa_mask);
167
168   /* Catch bus errors, segmentation faults, and floating point exceptions*/
169   sigaction(SIGBUS,&sig,0);
170   sigaction(SIGSEGV,&sig,0);
171   sigaction(SIGFPE,&sig,0);
172   signal(SIGPIPE, SIG_IGN);
173 #ifdef STM
174   newobjs=calloc(1, sizeof(struct objlist));
175   t_cache = objstrCreate(1048576);
176   t_reserve=NULL;
177   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
178 #ifdef DELAYCOMP
179   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
180   ptrstack.count=0;
181   primstack.count=0;
182   pl=&primstack;
183 #endif
184 #ifdef STMSTATS
185   trec=calloc(1, sizeof(threadrec_t));
186   trec->blocked = 0;
187   lockedobjs=calloc(1, sizeof(struct objlist));
188   objlockscope = calloc(1, sizeof(objlockstate_t));
189   pthread_mutex_init(&lockedobjstore, NULL);
190   { 
191     int i;
192     for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
193       typesCausingAbort[i] = 0;
194     }
195   }
196 #endif
197 #endif
198 #ifdef MAC
199   struct listitem *litem=malloc(sizeof(struct listitem));
200   pthread_setspecific(litemkey, litem);
201   litem->prev=NULL;
202   litem->next=list;
203   if(list!=NULL)
204     list->prev=litem;
205   list=litem;
206 #else
207   //Add our litem to list of threads
208   litem.prev=NULL;
209   litem.next=list;
210   if(list!=NULL)
211     list->prev=&litem;
212   list=&litem;
213 #endif
214 }
215
216 #if defined(THREADS)||defined(STM)
217 void initthread(struct ___Thread___ * ___this___) {
218 #ifdef PRECISE_GC
219   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
220   //Add our litem to list of threads
221 #ifdef MAC
222   struct listitem litem;
223   pthread_setspecific(litemkey, &litem);
224 #endif
225   litem.prev=NULL;
226   pthread_mutex_lock(&gclistlock);
227   litem.next=list;
228   if(list!=NULL)
229     list->prev=&litem;
230   list=&litem;
231   pthread_mutex_unlock(&gclistlock);
232   
233 #ifdef THREADS
234   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
235 #else
236   newobjs=calloc(1, sizeof(struct objlist));
237 #ifdef STMSTATS
238   trec=calloc(1, sizeof(threadrec_t));
239   trec->blocked = 0;
240   lockedobjs=calloc(1, sizeof(struct objlist));
241 #endif
242   t_cache = objstrCreate(1048576);
243   t_reserve=NULL;
244   t_chashCreate(CHASH_SIZE, CLOADFACTOR);
245 #ifdef DELAYCOMP
246   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
247   ptrstack.count=0;
248   primstack.count=0;
249 #endif
250  ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
251  objstrDelete(t_cache);
252  objstrDelete(t_reserve);
253  t_chashDelete();
254  free(newobjs);
255 #ifdef STMSTATS
256  free(lockedobjs);
257 #endif
258 #endif
259   ___this___=(struct ___Thread___ *) p[2];
260 #else
261   ___Thread______staticStart____L___Thread___(___this___);
262 #endif
263   ___this___->___finished___=1;
264   pthread_mutex_lock(&joinlock);
265   pthread_cond_signal(&joincond);
266   pthread_mutex_unlock(&joinlock);
267
268   pthread_mutex_lock(&gclistlock);
269 #ifdef THREADS
270   pthread_setspecific(threadlocks, litem.locklist);
271 #endif
272   if (litem.prev==NULL) {
273     list=litem.next;
274   } else {
275     litem.prev->next=litem.next;
276   }
277   if (litem.next!=NULL) {
278     litem.next->prev=litem.prev;
279   }
280   threadcount--;
281   pthread_cond_signal(&gccond);
282   pthread_mutex_unlock(&gclistlock);
283 }
284 #endif
285
286 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
287 #if defined(THREADS)||defined(STM)
288 #ifdef PRECISE_GC
289   stopforgc((struct garbagelist *)___params___);
290 #endif
291 #endif
292   usleep(___millis___);
293 #if defined(THREADS)||defined(STM)
294 #ifdef PRECISE_GC
295   restartaftergc();
296 #endif
297 #endif
298 }
299
300 #if defined(DSTM)|| defined(THREADS)||defined(STM)
301 void CALL00(___Thread______yield____) {
302   pthread_yield();
303 }
304 #endif
305
306 #ifdef DSTM
307 /* Add thread join capability */
308 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
309   unsigned int *oidarray;
310   unsigned short *versionarray, version;
311   objheader_t *ptr;
312   /* Add transaction to check if thread finished for join operation */
313 transstart:
314   transStart();
315   ptr = transRead((unsigned int) VAR(___this___));
316   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
317 #ifdef THREADJOINDEBUG
318   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
319 #endif
320   if(p->___threadDone___ == 1) {
321 #ifdef THREADJOINDEBUG
322     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
323 #endif
324     transAbort();
325     return;
326   } else {
327
328     version = (ptr-1)->version;
329     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
330       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
331       return;
332     }
333
334     oidarray[0] = (unsigned int) VAR(___this___);
335
336     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
337       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
338       free(oidarray);
339       return;
340     }
341     versionarray[0] = version;
342     /* Request Notification */
343 #ifdef PRECISE_GC
344     stopforgc((struct garbagelist *)___params___);
345 #endif
346     reqNotify(oidarray, versionarray, 1);
347 #ifdef PRECISE_GC
348     restartaftergc();
349 #endif
350     free(oidarray);
351     free(versionarray);
352     transAbort();
353     goto transstart;
354   }
355   return;
356 }
357 #endif
358
359 #if defined(THREADS)||defined(STM)
360 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
361   pthread_mutex_lock(&joinlock);
362   while(!VAR(___this___)->___finished___) {
363 #ifdef PRECISE_GC
364   stopforgc((struct garbagelist *)___params___);
365 #endif
366     pthread_cond_wait(&joincond, &joinlock);
367 #ifdef PRECISE_GC
368     restartaftergc();
369 #endif
370   }
371   pthread_mutex_unlock(&joinlock);
372 }
373
374 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
375   pthread_t thread;
376   int retval;
377   pthread_attr_t nattr;
378
379   pthread_mutex_lock(&gclistlock);
380   threadcount++;
381   pthread_mutex_unlock(&gclistlock);
382   pthread_attr_init(&nattr);
383   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
384
385   do {
386     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
387     if (retval!=0)
388       usleep(1);
389   } while(retval!=0);
390   /* This next statement will likely not work on many machines */
391
392   pthread_attr_destroy(&nattr);
393 }
394 #endif
395
396 #ifdef DSTM
397 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
398   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
399 }
400 #endif
401
402 #ifdef DSTM
403 void globalDestructor(void *value) {
404   free(value);
405   pthread_setspecific(oidval, NULL);
406 }
407
408 void initDSMthread(int *ptr) {
409   objheader_t *tmp;
410   void *threadData;
411   int oid=ptr[0];
412   int type=ptr[1];
413   free(ptr);
414 #ifdef PRECISE_GC
415   int p[]={1, 0 /* NULL */, oid};
416 #ifdef MAC
417   struct listitem litem;
418   pthread_setspecific(litemkey, &litem);
419 #endif
420
421   //Add our litem to list of threads
422   litem.prev=NULL;
423   pthread_mutex_lock(&gclistlock);
424   litem.next=list;
425   if(list!=NULL)
426     list->prev=&litem;
427   list=&litem;
428   pthread_mutex_unlock(&gclistlock);
429
430   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
431 #else
432   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
433 #endif
434   threadData = calloc(1, sizeof(unsigned int));
435   *((unsigned int *) threadData) = oid;
436   pthread_setspecific(oidval, threadData);
437   pthread_mutex_lock(&gclistlock);
438
439 #ifdef THREADS
440   pthread_setspecific(threadlocks, litem.locklist);
441 #endif
442   if (litem.prev==NULL) {
443     list=litem.next;
444   } else {
445     litem.prev->next=litem.next;
446   }
447   if (litem.next!=NULL) {
448     litem.next->prev=litem.prev;
449   }
450   threadcount--;
451   pthread_cond_signal(&gccond);
452   pthread_mutex_unlock(&gclistlock);
453   /* Add transaction to check if thread finished for join operation */
454   goto transstart;
455 transstart:
456   {
457     transStart();
458     tmp  = transRead((unsigned int) oid);
459     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
460     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
461     if(transCommit()!= 0) {
462       goto transstart;
463     }
464   }
465   pthread_exit(NULL);
466 }
467
468 void startDSMthread(int oid, int objType) {
469   pthread_t thread;
470   int retval;
471   pthread_attr_t nattr;
472
473   pthread_mutex_lock(&gclistlock);
474   threadcount++;
475   pthread_mutex_unlock(&gclistlock);
476   pthread_attr_init(&nattr);
477   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
478   int * ptr=malloc(sizeof(int)*2);
479   ptr[0]=oid;
480   ptr[1]=objType;
481   pthread_key_create(&oidval, globalDestructor);
482   do {
483     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
484     if (retval!=0)
485       usleep(1);
486   } while(retval!=0);
487
488   pthread_attr_destroy(&nattr);
489 }
490
491 #endif