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 transrecord_t * trans;
23 pthread_key_t oid;
24
25 void threadexit() {
26   objheader_t* ptr;
27   void *value;
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(oid);
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   if(transCommit(trans) != 0) {
59           transAbort(trans);
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 /* Add thread join capability */
127 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
128   printf("DEBUG -> Inside thread join\n");
129 #ifdef DSTM
130   pthread_t thread;
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 transstart:
137   trans = transStart();
138   ptr = transRead(trans, (unsigned int) VAR(___this___));
139   struct ___Thread___ *p = (struct ___Thread___ *) ptr;
140   if(p->___threadDone___ == 1) {
141           transAbort(trans);
142           return;
143   } else {
144           version = (ptr-1)->version;
145           if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
146                   printf("Calloc error %s, %d\n", __FILE__, __LINE__);
147                   return;
148           }
149
150           oidarray[0] = (unsigned int) VAR(___this___);
151
152           if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
153                   printf("Calloc error %s, %d\n", __FILE__, __LINE__);
154                   free(oidarray);
155                   return;
156           }
157           versionarray[0] = version;
158           /* Request Notification */
159           reqNotify(oidarray, versionarray, 1); 
160           free(oidarray);
161           free(versionarray);
162           transAbort(trans);
163           goto transstart;
164   }
165   return;
166 #endif
167 }
168
169 #ifdef THREADS
170 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
171   pthread_t thread;
172   int retval;
173   pthread_attr_t nattr;
174
175   pthread_mutex_lock(&gclistlock);
176   threadcount++;
177   pthread_mutex_unlock(&gclistlock);
178   pthread_attr_init(&nattr);
179   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
180   
181   do {
182     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
183     if (retval!=0)
184       usleep(1);
185   } while(retval!=0);
186
187   pthread_attr_destroy(&nattr);
188 }
189 #endif
190
191 #ifdef DSTM
192 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
193   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
194 }
195 #endif
196
197 #ifdef DSTM
198 void globalDestructor(void *value) {
199         free(value);
200         pthread_setspecific(oid, NULL);
201 }
202
203 void initDSMthread(int *ptr) {
204   objheader_t *tmp;     
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(oid, 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___ *t = (struct ___Thread___ *) tmp;
228   t->___threadDone___ = 1;
229   if(transCommit(trans)!= 0) {
230           transAbort(trans);
231           goto transstart;
232   }
233   pthread_exit(NULL);
234 }
235
236 void startDSMthread(int oid, int objType) {
237   pthread_t thread;
238   int retval;
239   pthread_attr_t nattr;
240
241   pthread_mutex_lock(&gclistlock);
242   threadcount++;
243   pthread_mutex_unlock(&gclistlock);
244   pthread_attr_init(&nattr);
245   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
246   int * ptr=malloc(sizeof(int)*2);
247   ptr[0]=oid;
248   ptr[1]=objType;
249   pthread_key_create(&oid, globalDestructor);
250   do {
251     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
252     if (retval!=0)
253       usleep(1);
254   } while(retval!=0);
255
256   pthread_attr_destroy(&nattr);
257 }
258
259 #endif