start of new file
[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 #include <DSTM/interface/dstm.h>
10 #include <DSTM/interface/llookup.h>
11
12 #ifndef RAW
13 #include <stdio.h>
14 #endif
15 int threadcount;
16 pthread_mutex_t gclock;
17 pthread_mutex_t gclistlock;
18 pthread_cond_t gccond;
19 pthread_mutex_t objlock;
20 pthread_cond_t objcond;
21
22 pthread_mutex_t joinlock;
23 pthread_cond_t joincond;
24 pthread_key_t threadlocks;
25 pthread_mutex_t threadnotifylock;
26 pthread_cond_t threadnotifycond;
27 pthread_key_t oidval;
28
29 void threadexit() {
30   objheader_t* ptr;
31   void *value;
32   transrecord_t * trans;
33   unsigned int oidvalue;
34
35 #ifdef THREADS
36   struct ___Object___ *ll=pthread_getspecific(threadlocks);
37   while(ll!=NULL) {
38     struct ___Object___ *llnext=ll->___nextlockobject___;    
39     ll->___nextlockobject___=NULL;
40     ll->___prevlockobject___=NULL;
41     ll->lockcount=0;
42     ll->tid=0; //unlock it
43     ll=llnext;
44   }
45   pthread_mutex_lock(&objlock);//wake everyone up
46   pthread_cond_broadcast(&objcond);
47   pthread_mutex_unlock(&objlock);
48 #endif
49   pthread_mutex_lock(&gclistlock);
50   threadcount--;
51   pthread_cond_signal(&gccond);
52   pthread_mutex_unlock(&gclistlock);
53 #ifdef DSTM
54   /* Add transaction to check if thread finished for join operation */
55   value = pthread_getspecific(oidval);
56   oidvalue = *((unsigned int *)value);
57   goto transstart;
58 transstart:
59   {
60     transrecord_t * trans = transStart();
61     ptr = transRead(trans, oidvalue);
62     struct ___Thread___ *p = (struct ___Thread___ *) ptr;
63     p->___threadDone___ = 1;
64     *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
65     if(transCommit(trans) != 0) {
66       goto transstart;
67     }
68   }
69 #endif  
70   pthread_exit(NULL);
71 }
72
73 void threadhandler(int sig, siginfo_t *info, void *uap) {
74 #ifdef DEBUG
75   printf("sig=%d\n",sig);
76   printf("signal\n");
77 #endif
78   threadexit();
79 }
80
81 void initializethreads() {
82   struct sigaction sig;
83   threadcount=1;
84   pthread_mutex_init(&gclock, NULL);
85   pthread_mutex_init(&gclistlock, NULL);
86   pthread_cond_init(&gccond, NULL);
87   pthread_mutex_init(&objlock,NULL);
88   pthread_cond_init(&objcond,NULL);
89   pthread_mutex_init(&joinlock,NULL);
90   pthread_cond_init(&joincond,NULL);
91   pthread_key_create(&threadlocks, NULL);
92   processOptions();
93   initializeexithandler();
94
95   sig.sa_sigaction=&threadhandler;
96   sig.sa_flags=SA_SIGINFO;
97   sigemptyset(&sig.sa_mask);
98
99   /* Catch bus errors, segmentation faults, and floating point exceptions*/
100   sigaction(SIGBUS,&sig,0);
101   sigaction(SIGSEGV,&sig,0);
102   sigaction(SIGFPE,&sig,0);
103   signal(SIGPIPE, SIG_IGN);
104 }
105
106 #ifdef THREADS
107 void initthread(struct ___Thread___ * ___this___) {
108 #ifdef PRECISE_GC
109   int p[]={1, (int) NULL, (int) ___this___};
110   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
111   ___this___=(struct ___Thread___ *) p[2];
112 #else
113   ___Thread______staticStart____L___Thread___(___this___);
114 #endif
115   ___this___->___finished___=1;
116   pthread_mutex_lock(&joinlock);
117   pthread_cond_signal(&joincond);
118   pthread_mutex_unlock(&joinlock);
119
120   pthread_mutex_lock(&gclistlock);
121   threadcount--;
122   pthread_cond_signal(&gccond);
123   pthread_mutex_unlock(&gclistlock);
124 }
125 #endif
126
127 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
128 #ifdef THREADS
129 #ifdef PRECISE_GC
130   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
131 #endif
132 #endif
133   usleep(___millis___);  
134 #ifdef THREADS
135 #ifdef PRECISE_GC
136   restartaftergc(tmp);
137 #endif
138 #endif
139 }
140
141 #if defined(DSTM)|| defined(THREADS)
142 void CALL00(___Thread______yield____) {
143   pthread_yield();
144 }
145 #endif
146
147 #ifdef DSTM
148 /* Add thread join capability */
149 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
150   unsigned int *oidarray;
151   unsigned short *versionarray, version;
152   transrecord_t *trans;
153   objheader_t *ptr;
154   /* Add transaction to check if thread finished for join operation */
155   goto transstart;
156 transstart:
157   trans = transStart();
158   ptr = transRead(trans, (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(trans);
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(trans);
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   transrecord_t * trans;
247   void *threadData;
248   int oid=ptr[0];
249   int type=ptr[1];
250   free(ptr);
251 #ifdef PRECISE_GC
252   int p[]={1, 0 /* NULL */, oid};
253   ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
254 #else
255   ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
256 #endif
257   threadData = calloc(1, sizeof(unsigned int));
258   *((unsigned int *) threadData) = oid;
259   pthread_setspecific(oidval, threadData);
260   pthread_mutex_lock(&gclistlock);
261   threadcount--;
262   pthread_cond_signal(&gccond);
263   pthread_mutex_unlock(&gclistlock);
264   /* Add transaction to check if thread finished for join operation */
265   goto transstart;
266 transstart:
267   {
268     transrecord_t * trans = transStart();
269     tmp  = transRead(trans, (unsigned int) oid);
270     ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
271     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
272     if(transCommit(trans)!= 0) {
273       goto transstart;
274     }
275   }
276   pthread_exit(NULL);
277 }
278
279 void startDSMthread(int oid, int objType) {
280         pthread_t thread;
281         int retval;
282         pthread_attr_t nattr;
283
284         pthread_mutex_lock(&gclistlock);
285         threadcount++;
286         pthread_mutex_unlock(&gclistlock);
287   pthread_attr_init(&nattr);
288   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
289   int * ptr=malloc(sizeof(int)*2);
290   ptr[0]=oid;
291   ptr[1]=objType;
292   pthread_key_create(&oidval, globalDestructor);
293   do {
294     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
295     if (retval!=0)
296       usleep(1);
297   } while(retval!=0);
298
299   pthread_attr_destroy(&nattr);
300 }
301
302 #endif