guard header files
[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 int threadcount;
19 pthread_mutex_t gclock;
20 pthread_mutex_t gclistlock;
21 pthread_cond_t gccond;
22 pthread_mutex_t objlock;
23 pthread_cond_t objcond;
24
25 pthread_mutex_t joinlock;
26 pthread_cond_t joincond;
27 pthread_key_t threadlocks;
28 pthread_mutex_t threadnotifylock;
29 pthread_cond_t threadnotifycond;
30 pthread_key_t oidval;
31
32 void threadexit() {
33   objheader_t* ptr;
34   void *value;
35   unsigned int oidvalue;
36
37 #ifdef THREADS
38   struct ___Object___ *ll=pthread_getspecific(threadlocks);
39   while(ll!=NULL) {
40     struct ___Object___ *llnext=ll->___nextlockobject___;
41     ll->___nextlockobject___=NULL;
42     ll->___prevlockobject___=NULL;
43     ll->lockcount=0;
44     ll->tid=0; //unlock it
45     ll=llnext;
46   }
47   pthread_mutex_lock(&objlock); //wake everyone up
48   pthread_cond_broadcast(&objcond);
49   pthread_mutex_unlock(&objlock);
50 #endif
51   pthread_mutex_lock(&gclistlock);
52   threadcount--;
53   pthread_cond_signal(&gccond);
54   pthread_mutex_unlock(&gclistlock);
55 #ifdef DSTM
56   /* Add transaction to check if thread finished for join operation */
57   value = pthread_getspecific(oidval);
58   oidvalue = *((unsigned int *)value);
59   goto transstart;
60 transstart:
61   {
62     transStart();
63     ptr = transRead(oidvalue);
64     struct ___Thread___ *p = (struct ___Thread___ *) ptr;
65     p->___threadDone___ = 1;
66     *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
67     if(transCommit() != 0) {
68       goto transstart;
69     }
70   }
71 #endif
72   pthread_exit(NULL);
73 }
74
75 void threadhandler(int sig, siginfo_t *info, void *uap) {
76 #ifdef DEBUG
77   printf("sig=%d\n",sig);
78   printf("signal\n");
79 #endif
80   threadexit();
81 }
82
83 void initializethreads() {
84   struct sigaction sig;
85   threadcount=1;
86   pthread_mutex_init(&gclock, NULL);
87   pthread_mutex_init(&gclistlock, NULL);
88   pthread_cond_init(&gccond, NULL);
89   pthread_mutex_init(&objlock,NULL);
90   pthread_cond_init(&objcond,NULL);
91   pthread_mutex_init(&joinlock,NULL);
92   pthread_cond_init(&joincond,NULL);
93   pthread_key_create(&threadlocks, NULL);
94   processOptions();
95   initializeexithandler();
96
97   sig.sa_sigaction=&threadhandler;
98   sig.sa_flags=SA_SIGINFO;
99   sigemptyset(&sig.sa_mask);
100
101   /* Catch bus errors, segmentation faults, and floating point exceptions*/
102   sigaction(SIGBUS,&sig,0);
103   sigaction(SIGSEGV,&sig,0);
104   sigaction(SIGFPE,&sig,0);
105   signal(SIGPIPE, SIG_IGN);
106 }
107
108 #ifdef THREADS
109 void initthread(struct ___Thread___ * ___this___) {
110 #ifdef PRECISE_GC
111   int p[]={1, (int) NULL, (int) ___this___};
112   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
113   ___this___=(struct ___Thread___ *) p[2];
114 #else
115   ___Thread______staticStart____L___Thread___(___this___);
116 #endif
117   ___this___->___finished___=1;
118   pthread_mutex_lock(&joinlock);
119   pthread_cond_signal(&joincond);
120   pthread_mutex_unlock(&joinlock);
121
122   pthread_mutex_lock(&gclistlock);
123   threadcount--;
124   pthread_cond_signal(&gccond);
125   pthread_mutex_unlock(&gclistlock);
126 }
127 #endif
128
129 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
130 #ifdef THREADS
131 #ifdef PRECISE_GC
132   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
133 #endif
134 #endif
135   usleep(___millis___);
136 #ifdef THREADS
137 #ifdef PRECISE_GC
138   restartaftergc(tmp);
139 #endif
140 #endif
141 }
142
143 #if defined(DSTM)|| defined(THREADS)
144 void CALL00(___Thread______yield____) {
145   pthread_yield();
146 }
147 #endif
148
149 #ifdef DSTM
150 /* Add thread join capability */
151 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
152   unsigned int *oidarray;
153   unsigned short *versionarray, version;
154   objheader_t *ptr;
155   /* Add transaction to check if thread finished for join operation */
156 transstart:
157   transStart();
158   ptr = transRead((unsigned int) VAR(___this___));
159   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
160 #ifdef THREADJOINDEBUG
161   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
162 #endif
163   if(p->___threadDone___ == 1) {
164 #ifdef THREADJOINDEBUG
165     printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
166 #endif
167     transAbort();
168     return;
169   } else {
170
171     version = (ptr-1)->version;
172     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
173       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
174       return;
175     }
176
177     oidarray[0] = (unsigned int) VAR(___this___);
178
179     if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
180       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
181       free(oidarray);
182       return;
183     }
184     versionarray[0] = version;
185     /* Request Notification */
186 #ifdef PRECISE_GC
187     struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
188 #endif
189     reqNotify(oidarray, versionarray, 1);
190 #ifdef PRECISE_GC
191     restartaftergc(tmp);
192 #endif
193     free(oidarray);
194     free(versionarray);
195     transAbort();
196     goto transstart;
197   }
198   return;
199 }
200 #endif
201
202 #ifdef THREADS
203 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
204   pthread_mutex_lock(&joinlock);
205   while(!VAR(___this___)->___finished___)
206     pthread_cond_wait(&joincond, &joinlock);
207   pthread_mutex_unlock(&joinlock);
208 }
209
210 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
211   pthread_t thread;
212   int retval;
213   pthread_attr_t nattr;
214
215   pthread_mutex_lock(&gclistlock);
216   threadcount++;
217   pthread_mutex_unlock(&gclistlock);
218   pthread_attr_init(&nattr);
219   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
220
221   do {
222     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
223     if (retval!=0)
224       usleep(1);
225   } while(retval!=0);
226   /* This next statement will likely not work on many machines */
227
228   pthread_attr_destroy(&nattr);
229 }
230 #endif
231
232 #ifdef DSTM
233 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
234   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
235 }
236 #endif
237
238 #ifdef DSTM
239 void globalDestructor(void *value) {
240   free(value);
241   pthread_setspecific(oidval, NULL);
242 }
243
244 void initDSMthread(int *ptr) {
245   objheader_t *tmp;
246   void *threadData;
247   int oid=ptr[0];
248   int type=ptr[1];
249   free(ptr);
250 #ifdef PRECISE_GC
251   int p[]={1, 0 /* NULL */, oid};
252   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
253 #else
254   ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
255 #endif
256   threadData = calloc(1, sizeof(unsigned int));
257   *((unsigned int *) threadData) = oid;
258   pthread_setspecific(oidval, threadData);
259   pthread_mutex_lock(&gclistlock);
260   threadcount--;
261   pthread_cond_signal(&gccond);
262   pthread_mutex_unlock(&gclistlock);
263   /* Add transaction to check if thread finished for join operation */
264   goto transstart;
265 transstart:
266   {
267     transStart();
268     tmp  = transRead((unsigned int) oid);
269     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
270     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
271     if(transCommit()!= 0) {
272       goto transstart;
273     }
274   }
275   pthread_exit(NULL);
276 }
277
278 void startDSMthread(int oid, int objType) {
279   pthread_t thread;
280   int retval;
281   pthread_attr_t nattr;
282
283   pthread_mutex_lock(&gclistlock);
284   threadcount++;
285   pthread_mutex_unlock(&gclistlock);
286   pthread_attr_init(&nattr);
287   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
288   int * ptr=malloc(sizeof(int)*2);
289   ptr[0]=oid;
290   ptr[1]=objType;
291   pthread_key_create(&oidval, globalDestructor);
292   do {
293     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
294     if (retval!=0)
295       usleep(1);
296   } while(retval!=0);
297
298   pthread_attr_destroy(&nattr);
299 }
300
301 #endif