61159ecd898f6d42f6f8285e161e98219c23f29a
[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 #endif
19
20 #ifdef D___Object______nativehashCode____
21 int CALL01(___Object______nativehashCode____, struct ___Object___ * ___this___) {
22   return (int)((INTPTR) VAR(___this___));
23 }
24 #endif
25
26 #ifdef D___Object______hashCode____
27 int CALL01(___Object______hashCode____, struct ___Object___ * ___this___) {
28   if (!VAR(___this___)->___cachedHash___) {
29     VAR(___this___)->___cachedHash___=1;
30     VAR(___this___)->___cachedCode___=(int)((INTPTR)VAR(___this___));
31   }
32   return VAR(___this___)->___cachedCode___;
33 }
34 #endif
35
36 #ifdef D___Object______getType____
37 int CALL01(___Object______getType____, struct ___Object___ * ___this___) {
38   return ((int *)VAR(___this___))[0];
39 }
40 #endif
41
42 #ifdef THREADS
43 #ifdef D___Object______MonitorEnter____
44 void CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) {
45 #ifdef MAC
46   struct lockvector *lptr=(struct lockvector *)pthread_getspecific(threadlocks);
47 #else
48   struct lockvector *lptr=&lvector;
49 #endif
50   struct lockpair *lpair=&lptr->locks[lptr->index++];
51   pthread_t self=pthread_self();
52   lpair->object=VAR(___this___);
53
54   if (self==VAR(___this___)->tid) {
55     lpair->islastlock=0;
56   } else {
57     lpair->islastlock=1;
58     while(1) {
59       if (VAR(___this___)->tid==0) {
60         if (CAS32(&VAR(___this___)->tid, 0, self)==0) {
61           return;
62         }
63       }
64       {
65 #ifdef PRECISE_GC
66         if (unlikely(needtocollect))
67           checkcollect((struct garbagelist *)___params___);
68 #endif
69       }
70     }
71   }
72 }
73 #endif
74
75
76 #ifdef D___Object______notify____
77 void CALL01(___Object______notify____, struct ___Object___ * ___this___) {
78   VAR(___this___)->notifycount++;
79 }
80 #endif
81
82 #ifdef D___Object______notifyAll____
83 void CALL01(___Object______notifyAll____, struct ___Object___ * ___this___) {
84   VAR(___this___)->notifycount++;
85 }
86 #endif
87
88 #ifdef D___Object______wait____
89 void CALL01(___Object______wait____, struct ___Object___ * ___this___) {
90   pthread_t self=pthread_self();
91   int notifycount=VAR(___this___)->notifycount;
92   BARRIER();
93   VAR(___this___)->tid=0;
94   BARRIER();
95   
96   while(notifycount==VAR(___this___)->notifycount) {
97 #ifdef PRECISE_GC
98     if (unlikely(needtocollect))
99       checkcollect((struct garbagelist *)___params___);
100 #endif
101   }
102
103   while(1) {
104     if (VAR(___this___)->tid==0) {
105       if (CAS32(&VAR(___this___)->tid, 0, self)==0) {
106         BARRIER();
107         return;
108       }
109     }
110 #ifdef PRECISE_GC
111     if (unlikely(needtocollect))
112       checkcollect((struct garbagelist *)___params___);
113 #endif
114   }
115 }
116 #endif
117
118 #ifdef D___Object______MonitorExit____
119 void CALL01(___Object______MonitorExit____, struct ___Object___ * ___this___) {
120 #ifdef MAC
121   struct lockvector *lptr=(struct lockvector *)pthread_getspecific(threadlocks);
122 #else
123   struct lockvector *lptr=&lvector;
124 #endif
125   struct lockpair *lpair=&lptr->locks[--lptr->index];
126   
127   if (lpair->islastlock) {
128     MBARRIER();
129     lpair->object->tid=0;
130   }
131 }
132 #endif
133 #endif