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