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