9 #include <DSTM/interface/dstm.h>
10 #include <DSTM/interface/llookup.h>
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;
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;
32 transrecord_t * trans;
33 unsigned int oidvalue;
36 struct ___Object___ *ll=pthread_getspecific(threadlocks);
38 struct ___Object___ *llnext=ll->___nextlockobject___;
39 ll->___nextlockobject___=NULL;
40 ll->___prevlockobject___=NULL;
42 ll->tid=0; //unlock it
45 pthread_mutex_lock(&objlock); //wake everyone up
46 pthread_cond_broadcast(&objcond);
47 pthread_mutex_unlock(&objlock);
49 pthread_mutex_lock(&gclistlock);
51 pthread_cond_signal(&gccond);
52 pthread_mutex_unlock(&gclistlock);
54 /* Add transaction to check if thread finished for join operation */
55 value = pthread_getspecific(oidval);
56 oidvalue = *((unsigned int *)value);
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) {
73 void threadhandler(int sig, siginfo_t *info, void *uap) {
75 printf("sig=%d\n",sig);
81 void initializethreads() {
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);
93 initializeexithandler();
95 sig.sa_sigaction=&threadhandler;
96 sig.sa_flags=SA_SIGINFO;
97 sigemptyset(&sig.sa_mask);
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);
107 void initthread(struct ___Thread___ * ___this___) {
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];
113 ___Thread______staticStart____L___Thread___(___this___);
115 ___this___->___finished___=1;
116 pthread_mutex_lock(&joinlock);
117 pthread_cond_signal(&joincond);
118 pthread_mutex_unlock(&joinlock);
120 pthread_mutex_lock(&gclistlock);
122 pthread_cond_signal(&gccond);
123 pthread_mutex_unlock(&gclistlock);
127 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
130 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
133 usleep(___millis___);
141 #if defined(DSTM)|| defined(THREADS)
142 void CALL00(___Thread______yield____) {
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;
154 /* Add transaction to check if thread finished for join operation */
156 trans = transStart();
157 ptr = transRead(trans, (unsigned int) VAR(___this___));
158 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
159 #ifdef THREADJOINDEBUG
160 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
162 if(p->___threadDone___ == 1) {
163 #ifdef THREADJOINDEBUG
164 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
170 version = (ptr-1)->version;
171 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
172 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
176 oidarray[0] = (unsigned int) VAR(___this___);
178 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
179 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
183 versionarray[0] = version;
184 /* Request Notification */
186 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
188 reqNotify(oidarray, versionarray, 1);
202 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
203 pthread_mutex_lock(&joinlock);
204 while(!VAR(___this___)->___finished___)
205 pthread_cond_wait(&joincond, &joinlock);
206 pthread_mutex_unlock(&joinlock);
209 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
212 pthread_attr_t nattr;
214 pthread_mutex_lock(&gclistlock);
216 pthread_mutex_unlock(&gclistlock);
217 pthread_attr_init(&nattr);
218 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
221 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
225 /* This next statement will likely not work on many machines */
227 pthread_attr_destroy(&nattr);
232 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
233 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
238 void globalDestructor(void *value) {
240 pthread_setspecific(oidval, NULL);
243 void initDSMthread(int *ptr) {
245 transrecord_t * trans;
251 int p[]={1, 0 /* NULL */, oid};
252 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
254 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
256 threadData = calloc(1, sizeof(unsigned int));
257 *((unsigned int *) threadData) = oid;
258 pthread_setspecific(oidval, threadData);
259 pthread_mutex_lock(&gclistlock);
261 pthread_cond_signal(&gccond);
262 pthread_mutex_unlock(&gclistlock);
263 /* Add transaction to check if thread finished for join operation */
267 transrecord_t * trans = transStart();
268 tmp = transRead(trans, (unsigned int) oid);
269 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
270 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
271 if(transCommit(trans)!= 0) {
278 void startDSMthread(int oid, int objType) {
281 pthread_attr_t nattr;
283 pthread_mutex_lock(&gclistlock);
285 pthread_mutex_unlock(&gclistlock);
286 pthread_attr_init(&nattr);
287 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
288 int * ptr=malloc(sizeof(int)*2);
291 pthread_key_create(&oidval, globalDestructor);
293 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
298 pthread_attr_destroy(&nattr);