11 #include <DSTM/interface/dstm.h>
12 #include <DSTM/interface/llookup.h>
19 pthread_mutex_t gclock;
20 pthread_mutex_t gclistlock;
21 pthread_cond_t gccond;
22 pthread_mutex_t objlock;
23 pthread_cond_t objcond;
25 pthread_mutex_t joinlock;
26 pthread_cond_t joincond;
27 pthread_key_t threadlocks;
28 pthread_mutex_t threadnotifylock;
29 pthread_cond_t threadnotifycond;
35 unsigned int oidvalue;
40 struct ___Object___ *ll=pthread_getspecific(threadlocks);
42 struct ___Object___ *llnext=ll->___nextlockobject___;
43 ll->___nextlockobject___=NULL;
44 ll->___prevlockobject___=NULL;
46 ll->tid=0; //unlock it
49 pthread_mutex_lock(&objlock); //wake everyone up
50 pthread_cond_broadcast(&objcond);
51 pthread_mutex_unlock(&objlock);
53 pthread_mutex_lock(&gclistlock);
55 pthread_cond_signal(&gccond);
56 pthread_mutex_unlock(&gclistlock);
58 /* Add transaction to check if thread finished for join operation */
59 value = pthread_getspecific(oidval);
60 oidvalue = *((unsigned int *)value);
65 ptr = transRead(oidvalue);
66 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
67 p->___threadDone___ = 1;
68 *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
69 if(transCommit() != 0) {
77 void threadhandler(int sig, siginfo_t *info, void *uap) {
79 printf("sig=%d\n",sig);
85 void initializethreads() {
88 pthread_mutex_init(&gclock, NULL);
89 pthread_mutex_init(&gclistlock, NULL);
90 pthread_cond_init(&gccond, NULL);
91 pthread_mutex_init(&objlock,NULL);
92 pthread_cond_init(&objcond,NULL);
93 pthread_mutex_init(&joinlock,NULL);
94 pthread_cond_init(&joincond,NULL);
95 pthread_key_create(&threadlocks, NULL);
97 initializeexithandler();
99 sig.sa_sigaction=&threadhandler;
100 sig.sa_flags=SA_SIGINFO;
101 sigemptyset(&sig.sa_mask);
103 /* Catch bus errors, segmentation faults, and floating point exceptions*/
104 sigaction(SIGBUS,&sig,0);
105 sigaction(SIGSEGV,&sig,0);
106 sigaction(SIGFPE,&sig,0);
107 signal(SIGPIPE, SIG_IGN);
110 #if defined(THREADS)||defined(STM)
111 void initthread(struct ___Thread___ * ___this___) {
113 int p[]={1, (int) NULL, (int) ___this___};
115 ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
117 newobjs=calloc(1, sizeof(struct objlist));
118 ___Thread____NN____staticStart____L___Thread___((struct ___Thread____NN____staticStart____L___Thread____params *)p);
121 ___this___=(struct ___Thread___ *) p[2];
123 ___Thread______staticStart____L___Thread___(___this___);
125 ___this___->___finished___=1;
126 pthread_mutex_lock(&joinlock);
127 pthread_cond_signal(&joincond);
128 pthread_mutex_unlock(&joinlock);
130 pthread_mutex_lock(&gclistlock);
132 pthread_cond_signal(&gccond);
133 pthread_mutex_unlock(&gclistlock);
137 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
138 #if defined(THREADS)||defined(STM)
140 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
143 usleep(___millis___);
144 #if defined(THREADS)||defined(STM)
151 #if defined(DSTM)|| defined(THREADS)||defined(STM)
152 void CALL00(___Thread______yield____) {
158 /* Add thread join capability */
159 void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
160 unsigned int *oidarray;
161 unsigned short *versionarray, version;
163 /* Add transaction to check if thread finished for join operation */
166 ptr = transRead((unsigned int) VAR(___this___));
167 struct ___Thread___ *p = (struct ___Thread___ *) ptr;
168 #ifdef THREADJOINDEBUG
169 printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
171 if(p->___threadDone___ == 1) {
172 #ifdef THREADJOINDEBUG
173 printf("Thread oid = %x is done\n", (unsigned int) VAR(___this___));
179 version = (ptr-1)->version;
180 if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
181 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
185 oidarray[0] = (unsigned int) VAR(___this___);
187 if((versionarray = calloc(1, sizeof(unsigned short))) == NULL) {
188 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
192 versionarray[0] = version;
193 /* Request Notification */
195 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
197 reqNotify(oidarray, versionarray, 1);
210 #if defined(THREADS)||defined(STM)
211 void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
213 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
215 pthread_mutex_lock(&joinlock);
216 while(!VAR(___this___)->___finished___)
217 pthread_cond_wait(&joincond, &joinlock);
218 pthread_mutex_unlock(&joinlock);
225 void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
228 pthread_attr_t nattr;
230 pthread_mutex_lock(&gclistlock);
232 pthread_mutex_unlock(&gclistlock);
233 pthread_attr_init(&nattr);
234 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
237 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
241 /* This next statement will likely not work on many machines */
243 pthread_attr_destroy(&nattr);
248 void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
249 startRemoteThread((unsigned int)VAR(___this___), ___mid___);
254 void globalDestructor(void *value) {
256 pthread_setspecific(oidval, NULL);
259 void initDSMthread(int *ptr) {
266 int p[]={1, 0 /* NULL */, oid};
267 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
269 ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
271 threadData = calloc(1, sizeof(unsigned int));
272 *((unsigned int *) threadData) = oid;
273 pthread_setspecific(oidval, threadData);
274 pthread_mutex_lock(&gclistlock);
276 pthread_cond_signal(&gccond);
277 pthread_mutex_unlock(&gclistlock);
278 /* Add transaction to check if thread finished for join operation */
283 tmp = transRead((unsigned int) oid);
284 ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
285 *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
286 if(transCommit()!= 0) {
293 void startDSMthread(int oid, int objType) {
296 pthread_attr_t nattr;
298 pthread_mutex_lock(&gclistlock);
300 pthread_mutex_unlock(&gclistlock);
301 pthread_attr_init(&nattr);
302 pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
303 int * ptr=malloc(sizeof(int)*2);
306 pthread_key_create(&oidval, globalDestructor);
308 retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread, ptr);
313 pthread_attr_destroy(&nattr);