352637d10e7a019fce3cb936fa3d4e0aea050be1
[IRC.git] / Robust / src / Runtime / object.c
1 #include "object.h"
2 #include "stdio.h"
3 #include "stdlib.h"
4
5 #ifdef THREADS
6 #include "thread.h"
7 #endif
8
9 int CALL01(___Object______hashCode____, struct ___Object___ * ___this___) {
10   return (int) VAR(___this___);
11 }
12
13 int CALL01(___Object______getType____, struct ___Object___ * ___this___) {
14   return ((int *)VAR(___this___))[0];
15 }
16
17 #ifdef THREADS
18 int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) {
19   pthread_t self=pthread_self();
20   if (self==VAR(___this___)->tid) {
21     VAR(___this___)->lockcount++;
22   } else {
23 #ifdef PRECISEGC
24     struct listitem *tmp=stopforgc(stackptr);
25 #endif
26     pthread_mutex_lock(&objlock);
27 #ifdef PRECISEGC
28     restartaftergc(tmp);
29 #endif
30     while(1) {
31       if (VAR(___this___)->tid==0) {
32         VAR(___this___)->lockcount=1;
33         VAR(___this___)->tid=self;
34         pthread_mutex_unlock(&objlock);
35         break;
36       }
37       {
38 #ifdef PRECISEGC
39         struct listitem *tmp=stopforgc(stackptr);
40 #endif
41         pthread_cond_wait(&objcond, &objlock);
42 #ifdef PRECISEGC
43         restartaftergc(tmp);
44 #endif
45       }
46     }
47   }
48 }
49
50 int CALL01(___Object______MonitorExit____, struct ___Object___ * ___this___) {
51   pthread_t self=pthread_self();
52   if (self==VAR(___this___)->tid) {
53     VAR(___this___)->lockcount--;
54     if (VAR(___this___)->lockcount==0)
55       VAR(___this___)->tid=0;
56     pthread_mutex_lock(&objlock);
57     pthread_cond_broadcast(&objcond);
58     pthread_mutex_unlock(&objlock);
59   } else {
60     printf("ERROR...UNLOCKING LOCK WE DON'T HAVE\n");
61     exit(-1);
62   }
63 }
64 #endif