e5224677e8b21249fe49a4fb6298b91ac0b947a2
[cacao.git] / threads / 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 #ifdef USE_THREADS
18
19 #include "../global.h"
20 #include "sysdep/defines.h"
21
22 #define WAITFOREVER     -1
23
24 #if defined(USE_INTERNAL_THREADS)
25
26 struct _thread;
27
28 typedef struct _iMux {
29     struct _thread *holder;
30     int count;
31     struct _thread *muxWaiters;
32 } iMux;
33
34 typedef struct _iCv {
35         struct _thread*         cvWaiters;
36         struct _iMux*           mux;
37 } iCv;
38
39 #define MAX_MUTEXES             256
40
41 typedef struct _mutexHashEntry
42 {
43     java_objectheader *object;
44     iMux mutex;
45     struct _mutexHashEntry *next;
46     int conditionCount;
47 } mutexHashEntry;
48
49 #define MUTEX_HASH_TRASH_BITS                3
50 #define MUTEX_HASH_SIGN_BITS                10
51
52 #define MUTEX_HASH_TABLE_SIZE             1024
53 #define MUTEX_OVERFLOW_TABLE_SIZE         1024
54 /*
55 #define MAX_MUTEX_HASH_TABLE_SIZE        65536
56 */
57
58 /*
59 #define MUTEX_USE_THRESHOLD               1024
60 */
61
62 extern long mutexHashMask;
63 extern int mutexHashTableSize;
64 extern mutexHashEntry *mutexHashTable;
65
66 extern mutexHashEntry *mutexOverflowTable;
67 extern int mutexOverflowTableSize;
68 extern mutexHashEntry *firstFreeOverflowEntry;
69
70 #define MUTEX_HASH_MASK                  ((MUTEX_HASH_TABLE_SIZE - 1) << 3)
71
72 #if 0
73 #define MUTEX_HASH_VALUE(a)      ((((long)(a)) & MUTEX_HASH_MASK) >> MUTEX_HASH_TRASH_BITS)
74 #else
75 #define MUTEX_HASH_VALUE(a)      (( (((long)(a)) ^ ((long)(a) >> MUTEX_HASH_SIGN_BITS)) & mutexHashMask) >> MUTEX_HASH_TRASH_BITS)
76 #endif
77 #define MUTEX_HASH_SUCCESSOR(h)                  (((h) + 7) & (mutexHashTableSize - 1))
78
79 typedef struct _conditionHashEntry
80 {
81     java_objectheader *object;
82     iCv condition;
83 } conditionHashEntry;
84
85 #define CONDITION_HASH_TABLE_SIZE                1024
86
87 #define CONDITION_HASH_VALUE(a)                  ((((long)(a)) & conditionHashMask) >> 3)
88 #define CONDITION_HASH_SUCCESSOR(h)              (((h) + 7) & (conditionHashTableSize - 1))
89
90 typedef struct
91 {
92     bool free;
93     java_objectheader *object;
94     iMux mutex;
95     iCv condition;
96 } object_mutex;
97
98 extern void initLocks (void);
99
100 mutexHashEntry* conditionLockedMutexForObject (java_objectheader *object);
101
102 void reorderConditionHashTable (int begin);
103 iCv* conditionForObject (java_objectheader *object);
104 iCv* addConditionForObject (java_objectheader *object);
105 void removeConditionForObject (java_objectheader *object);
106
107 /*
108  * use these functions only outside critical sections (intsEnable/intsRestore).
109  */
110
111 void signal_cond_for_object (java_objectheader *obj);
112 void broadcast_cond_for_object (java_objectheader *obj);
113 void wait_cond_for_object (java_objectheader *obj, s8 time);
114 void lock_mutex_for_object (java_objectheader *obj);
115 void unlock_mutex_for_object (java_objectheader *obj);
116
117 void lock_mutex (iMux*);
118 void unlock_mutex (iMux*);
119 void wait_cond (iMux*, iCv*, s8);
120 void signal_cond (iCv*);
121 void broadcast_cond (iCv*);
122
123 /*
124  * use these internal functions only in critical sections. blockInts must be exactly
125  * 1.
126  */
127 void internal_lock_mutex (iMux*);
128 void internal_unlock_mutex (iMux*);
129 void internal_wait_cond (iMux*, iCv*, s8);
130 void internal_signal_cond (iCv*);
131 void internal_broadcast_cond (iCv*);
132
133 void internal_lock_mutex_for_object (java_objectheader *obj);
134 void internal_unlock_mutex_for_object (java_objectheader *obj);
135 void internal_broadcast_cond_for_object (java_objectheader *obj);
136
137 #endif
138
139 #endif /* USE_THREADS */
140
141 #endif /* __locks_h */