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