drop locks if a thread crashes
[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___)->___prevlockobject___=NULL;
33         VAR(___this___)->___nextlockobject___=(struct ___Object___ *)pthread_getspecific(threadlocks);
34         VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___);
35         pthread_setspecific(threadlocks, VAR(___this___));
36         VAR(___this___)->lockcount=1;
37         VAR(___this___)->tid=self;
38         pthread_mutex_unlock(&objlock);
39         break;
40       }
41       {
42 #ifdef PRECISEGC
43         struct listitem *tmp=stopforgc(stackptr);
44 #endif
45         pthread_cond_wait(&objcond, &objlock);
46 #ifdef PRECISEGC
47         restartaftergc(tmp);
48 #endif
49       }
50     }
51   }
52 }
53
54 int CALL01(___Object______MonitorExit____, struct ___Object___ * ___this___) {
55   pthread_t self=pthread_self();
56   if (self==VAR(___this___)->tid) {
57     VAR(___this___)->lockcount--;
58     if (VAR(___this___)->lockcount==0) {
59       if (VAR(___this___)->___prevlockobject___==NULL) {
60         pthread_setspecific(threadlocks, VAR(___this___)->___nextlockobject___);
61       } else
62         VAR(___this___)->___prevlockobject___->___nextlockobject___=VAR(___this___)->___nextlockobject___;
63       if (VAR(___this___)->___nextlockobject___!=NULL)
64         VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___)->___prevlockobject___;
65       VAR(___this___)->lockentry=NULL;
66       VAR(___this___)->tid=0;
67     }
68     pthread_mutex_lock(&objlock);
69     pthread_cond_broadcast(&objcond);
70     pthread_mutex_unlock(&objlock);
71   } else {
72     printf("ERROR...UNLOCKING LOCK WE DON'T HAVE\n");
73     exit(-1);
74   }
75 }
76 #endif