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