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