ceb8eb5565a9f20ef68820a173bbbeb7a83f5610
[cacao.git] / src / threads / green / locks.h
1 /*
2  * locks.h
3  * Manage locking system
4  * This include the mutex's and cv's.
5  *
6  * Copyright (c) 1996 T. J. Wilkinson & Associates, London, UK.
7  *
8  * See the file "license.terms" for information on usage and redistribution
9  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10  *
11  * Written by Tim Wilkinson <tim@tjwassoc.demon.co.uk>, 1996.
12  */
13
14 #ifndef __locks_h
15 #define __locks_h
16
17 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
18
19 #include "global.h"
20
21 #define WAITFOREVER     -1
22
23 struct _thread;
24
25 typedef struct _iMux {
26     struct _thread *holder;
27     int count;
28     struct _thread *muxWaiters;
29 } iMux;
30
31 typedef struct _iCv {
32         struct _thread*         cvWaiters;
33         struct _iMux*           mux;
34 } iCv;
35
36 #define MAX_MUTEXES             256
37
38 typedef struct _mutexHashEntry
39 {
40     java_objectheader *object;
41     iMux mutex;
42     struct _mutexHashEntry *next;
43     int conditionCount;
44 } mutexHashEntry;
45
46 #define MUTEX_HASH_TRASH_BITS                3
47 #define MUTEX_HASH_SIGN_BITS                10
48
49 #define MUTEX_HASH_TABLE_SIZE             1024
50 #define MUTEX_OVERFLOW_TABLE_SIZE         1024
51 /*
52 #define MAX_MUTEX_HASH_TABLE_SIZE        65536
53 */
54
55 /*
56 #define MUTEX_USE_THRESHOLD               1024
57 */
58
59 extern long mutexHashMask;
60 extern int mutexHashTableSize;
61 extern mutexHashEntry *mutexHashTable;
62
63 extern mutexHashEntry *mutexOverflowTable;
64 extern int mutexOverflowTableSize;
65 extern mutexHashEntry *firstFreeOverflowEntry;
66
67 #define MUTEX_HASH_MASK                  ((MUTEX_HASH_TABLE_SIZE - 1) << 3)
68
69 #if 0
70 #define MUTEX_HASH_VALUE(a)      ((((long)(a)) & MUTEX_HASH_MASK) >> MUTEX_HASH_TRASH_BITS)
71 #else
72 #define MUTEX_HASH_VALUE(a)      (( (((long)(a)) ^ ((long)(a) >> MUTEX_HASH_SIGN_BITS)) & mutexHashMask) >> MUTEX_HASH_TRASH_BITS)
73 #endif
74 #define MUTEX_HASH_SUCCESSOR(h)                  (((h) + 7) & (mutexHashTableSize - 1))
75
76 typedef struct _conditionHashEntry
77 {
78     java_objectheader *object;
79     iCv condition;
80 } conditionHashEntry;
81
82 #define CONDITION_HASH_TABLE_SIZE                1024
83
84 #define CONDITION_HASH_VALUE(a)                  ((((long)(a)) & conditionHashMask) >> 3)
85 #define CONDITION_HASH_SUCCESSOR(h)              (((h) + 7) & (conditionHashTableSize - 1))
86
87 typedef struct
88 {
89     bool free;
90     java_objectheader *object;
91     iMux mutex;
92     iCv condition;
93 } object_mutex;
94
95 extern void initLocks (void);
96
97 mutexHashEntry* conditionLockedMutexForObject (java_objectheader *object);
98
99 void reorderConditionHashTable (int begin);
100 iCv* conditionForObject (java_objectheader *object);
101 iCv* addConditionForObject (java_objectheader *object);
102 void removeConditionForObject (java_objectheader *object);
103
104 /*
105  * use these functions only outside critical sections (intsEnable/intsRestore).
106  */
107
108 void signal_cond_for_object (java_objectheader *obj);
109 void broadcast_cond_for_object (java_objectheader *obj);
110 void wait_cond_for_object (java_objectheader *obj, s8 time, s4);
111 void lock_mutex_for_object (java_objectheader *obj);
112 void unlock_mutex_for_object (java_objectheader *obj);
113
114 void lock_mutex (iMux*);
115 void unlock_mutex (iMux*);
116 void wait_cond (iMux*, iCv*, s8);
117 void signal_cond (iCv*);
118 void broadcast_cond (iCv*);
119
120 /*
121  * use these internal functions only in critical sections. blockInts must be exactly
122  * 1.
123  */
124 void internal_lock_mutex (iMux*);
125 void internal_unlock_mutex (iMux*);
126 void internal_wait_cond (iMux*, iCv*, s8);
127 void internal_signal_cond (iCv*);
128 void internal_broadcast_cond (iCv*);
129
130 void internal_lock_mutex_for_object (java_objectheader *obj);
131 void internal_unlock_mutex_for_object (java_objectheader *obj);
132 void internal_broadcast_cond_for_object (java_objectheader *obj);
133
134 #endif /* USE_THREADS */
135
136 #endif /* __locks_h */