bug
[IRC.git] / Robust / src / Runtime / object.c
1 #include "object.h"
2 #ifdef MULTICORE
3 #include "runtime_arch.h"
4 #else
5 #include "stdio.h"
6 #endif
7 #include "stdlib.h"
8
9 #ifdef THREADS
10 #include "thread.h"
11 #endif
12 #ifndef MULTICORE
13 #include "mlp_lock.h"
14 #endif
15
16 #ifndef MAC
17 __thread struct lockvector lvector;
18 __thread int mythreadid;
19 #endif
20
21 #ifdef D___Object______nativehashCode____
22 int CALL01(___Object______nativehashCode____, struct ___Object___ * ___this___) {
23   return (int)((INTPTR) VAR(___this___));
24 }
25 #endif
26
27 #ifdef D___Object______hashCode____
28 int CALL01(___Object______hashCode____, struct ___Object___ * ___this___) {
29   if (!VAR(___this___)->___cachedHash___) {
30     VAR(___this___)->___cachedHash___=1;
31     VAR(___this___)->___cachedCode___=(int)((INTPTR)VAR(___this___));
32   }
33   return VAR(___this___)->___cachedCode___;
34 }
35 #endif
36
37 #ifdef D___Object______getType____
38 int CALL01(___Object______getType____, struct ___Object___ * ___this___) {
39   return ((int *)VAR(___this___))[0];
40 }
41 #endif
42
43 #ifdef THREADS
44 #ifdef D___Object______MonitorEnter____
45 void CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) {
46 #ifdef MAC
47   struct lockvector *lptr=(struct lockvector *)pthread_getspecific(threadlocks);
48   int self=pthread_getspecific(macthreadid);
49 #else
50   struct lockvector *lptr=&lvector;
51   int self=mythreadid;
52 #endif
53   struct lockpair *lpair=&lptr->locks[lptr->index++];
54   lpair->object=VAR(___this___);
55
56   if (self==VAR(___this___)->tid) {
57     lpair->islastlock=0;
58   } else {
59     lpair->islastlock=1;
60     while(1) {
61       if (VAR(___this___)->tid==0) {
62         if (CAS32(&VAR(___this___)->tid, 0, self)==0) {
63           return;
64         }
65       }
66       {
67 #ifdef PRECISE_GC
68         if (unlikely(needtocollect))
69           checkcollect((struct garbagelist *)___params___);
70 #endif
71       }
72     }
73   }
74 }
75 #endif
76
77
78 #ifdef D___Object______notify____
79 void CALL01(___Object______notify____, struct ___Object___ * ___this___) {
80   VAR(___this___)->notifycount++;
81 }
82 #endif
83
84 #ifdef D___Object______notifyAll____
85 void CALL01(___Object______notifyAll____, struct ___Object___ * ___this___) {
86   VAR(___this___)->notifycount++;
87 }
88 #endif
89
90 #ifdef D___Object______wait____
91 void CALL01(___Object______wait____, struct ___Object___ * ___this___) {
92 #ifdef MAC
93   int self=pthread_getspecific(macthreadid);
94 #else
95   int self=mythreadid;
96 #endif
97   int notifycount=VAR(___this___)->notifycount;
98   BARRIER();
99   VAR(___this___)->tid=0;
100   BARRIER();
101   
102   while(notifycount==VAR(___this___)->notifycount) {
103 #ifdef PRECISE_GC
104     if (unlikely(needtocollect))
105       checkcollect((struct garbagelist *)___params___);
106 #endif
107   }
108
109   while(1) {
110     if (VAR(___this___)->tid==0) {
111       if (CAS32(&VAR(___this___)->tid, 0, self)==0) {
112         BARRIER();
113         return;
114       }
115     }
116 #ifdef PRECISE_GC
117     if (unlikely(needtocollect))
118       checkcollect((struct garbagelist *)___params___);
119 #endif
120   }
121 }
122 #endif
123
124 #ifdef D___Object______MonitorExit____
125 void CALL01(___Object______MonitorExit____, struct ___Object___ * ___this___) {
126 #ifdef MAC
127   struct lockvector *lptr=(struct lockvector *)pthread_getspecific(threadlocks);
128 #else
129   struct lockvector *lptr=&lvector;
130 #endif
131   struct lockpair *lpair=&lptr->locks[--lptr->index];
132   
133   if (lpair->islastlock) {
134     MBARRIER();
135     lpair->object->tid=0;
136   }
137 }
138 #endif
139 #endif