30 |
30 |
31 // A Mutex/Monitor is a simple wrapper around a native lock plus condition |
31 // A Mutex/Monitor is a simple wrapper around a native lock plus condition |
32 // variable that supports lock ownership tracking, lock ranking for deadlock |
32 // variable that supports lock ownership tracking, lock ranking for deadlock |
33 // detection and coordinates with the safepoint protocol. |
33 // detection and coordinates with the safepoint protocol. |
34 |
34 |
35 // The default length of monitor name was originally chosen to be 64 to avoid |
35 // The default length of mutex name was originally chosen to be 64 to avoid |
36 // false sharing. Now, PaddedMonitor is available for this purpose. |
36 // false sharing. Now, PaddedMutex and PaddedMonitor are available for this purpose. |
37 // TODO: Check if _name[MONITOR_NAME_LEN] should better get replaced by const char*. |
37 // TODO: Check if _name[MUTEX_NAME_LEN] should better get replaced by const char*. |
38 static const int MONITOR_NAME_LEN = 64; |
38 static const int MUTEX_NAME_LEN = 64; |
39 |
39 |
40 class Monitor : public CHeapObj<mtSynchronizer> { |
40 class Mutex : public CHeapObj<mtSynchronizer> { |
41 |
41 |
42 public: |
42 public: |
43 // A special lock: Is a lock where you are guaranteed not to block while you are |
43 // A special lock: Is a lock where you are guaranteed not to block while you are |
44 // holding it, i.e., no vm operation can happen, taking other (blocking) locks, etc. |
44 // holding it, i.e., no vm operation can happen, taking other (blocking) locks, etc. |
45 // The rank 'access' is similar to 'special' and has the same restrictions on usage. |
45 // The rank 'access' is similar to 'special' and has the same restrictions on usage. |
76 }; |
76 }; |
77 |
77 |
78 protected: // Monitor-Mutex metadata |
78 protected: // Monitor-Mutex metadata |
79 Thread * volatile _owner; // The owner of the lock |
79 Thread * volatile _owner; // The owner of the lock |
80 os::PlatformMonitor _lock; // Native monitor implementation |
80 os::PlatformMonitor _lock; // Native monitor implementation |
81 char _name[MONITOR_NAME_LEN]; // Name of mutex/monitor |
81 char _name[MUTEX_NAME_LEN]; // Name of mutex/monitor |
82 |
82 |
83 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode) |
83 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode) |
84 #ifndef PRODUCT |
84 #ifndef PRODUCT |
85 bool _allow_vm_block; |
85 bool _allow_vm_block; |
86 DEBUG_ONLY(int _rank;) // rank (to avoid/detect potential deadlocks) |
86 DEBUG_ONLY(int _rank;) // rank (to avoid/detect potential deadlocks) |
87 DEBUG_ONLY(Monitor * _next;) // Used by a Thread to link up owned locks |
87 DEBUG_ONLY(Mutex* _next;) // Used by a Thread to link up owned locks |
88 DEBUG_ONLY(Thread* _last_owner;) // the last thread to own the lock |
88 DEBUG_ONLY(Thread* _last_owner;) // the last thread to own the lock |
89 DEBUG_ONLY(static bool contains(Monitor * locks, Monitor * lock);) |
89 DEBUG_ONLY(static bool contains(Mutex* locks, Mutex* lock);) |
90 DEBUG_ONLY(static Monitor * get_least_ranked_lock(Monitor * locks);) |
90 DEBUG_ONLY(static Mutex* get_least_ranked_lock(Mutex* locks);) |
91 DEBUG_ONLY(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);) |
91 DEBUG_ONLY(Mutex* get_least_ranked_lock_besides_this(Mutex* locks);) |
92 #endif |
92 #endif |
93 |
93 |
94 void set_owner_implementation(Thread* owner) PRODUCT_RETURN; |
94 void set_owner_implementation(Thread* owner) PRODUCT_RETURN; |
95 void check_prelock_state (Thread* thread, bool safepoint_check) PRODUCT_RETURN; |
95 void check_prelock_state (Thread* thread, bool safepoint_check) PRODUCT_RETURN; |
96 void check_block_state (Thread* thread) PRODUCT_RETURN; |
96 void check_block_state (Thread* thread) PRODUCT_RETURN; |
97 void check_safepoint_state (Thread* thread, bool safepoint_check) NOT_DEBUG_RETURN; |
97 void check_safepoint_state (Thread* thread, bool safepoint_check) NOT_DEBUG_RETURN; |
98 void assert_owner (Thread* expected) NOT_DEBUG_RETURN; |
98 void assert_owner (Thread* expected) NOT_DEBUG_RETURN; |
99 void assert_wait_lock_state (Thread* self) NOT_DEBUG_RETURN; |
|
100 |
99 |
101 public: |
100 public: |
102 enum { |
101 enum { |
103 _allow_vm_block_flag = true, |
102 _allow_vm_block_flag = true, |
104 _as_suspend_equivalent_flag = true |
103 _as_suspend_equivalent_flag = true |
128 _safepoint_check_flag, |
127 _safepoint_check_flag, |
129 _no_safepoint_check_flag |
128 _no_safepoint_check_flag |
130 }; |
129 }; |
131 |
130 |
132 enum SafepointCheckRequired { |
131 enum SafepointCheckRequired { |
133 _safepoint_check_never, // Monitors with this value will cause errors |
132 _safepoint_check_never, // Mutexes with this value will cause errors |
134 // when acquired by a JavaThread with a safepoint check. |
133 // when acquired by a JavaThread with a safepoint check. |
135 _safepoint_check_sometimes, // A couple of special locks are acquired by JavaThreads sometimes |
134 _safepoint_check_sometimes, // A couple of special locks are acquired by JavaThreads sometimes |
136 // with and sometimes without safepoint checks. These |
135 // with and sometimes without safepoint checks. These |
137 // locks will not produce errors when locked. |
136 // locks will not produce errors when locked. |
138 _safepoint_check_always // Monitors with this value will cause errors |
137 _safepoint_check_always // Mutexes with this value will cause errors |
139 // when acquired by a JavaThread without a safepoint check. |
138 // when acquired by a JavaThread without a safepoint check. |
140 }; |
139 }; |
141 |
140 |
142 NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;) |
141 NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;) |
143 |
142 |
144 public: |
143 public: |
145 Monitor(int rank, const char *name, bool allow_vm_block = false, |
144 Mutex(int rank, const char *name, bool allow_vm_block = false, |
146 SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
145 SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
147 ~Monitor(); |
146 ~Mutex(); |
|
147 |
|
148 void lock(); // prints out warning if VM thread blocks |
|
149 void lock(Thread *thread); // overloaded with current thread |
|
150 void unlock(); |
|
151 bool is_locked() const { return _owner != NULL; } |
|
152 |
|
153 bool try_lock(); // Like lock(), but unblocking. It returns false instead |
|
154 |
|
155 void release_for_safepoint(); |
|
156 |
|
157 // Lock without safepoint check. Should ONLY be used by safepoint code and other code |
|
158 // that is guaranteed not to block while running inside the VM. |
|
159 void lock_without_safepoint_check(); |
|
160 void lock_without_safepoint_check(Thread* self); |
|
161 |
|
162 // Current owner - not not MT-safe. Can only be used to guarantee that |
|
163 // the current running thread owns the lock |
|
164 Thread* owner() const { return _owner; } |
|
165 bool owned_by_self() const; |
|
166 |
|
167 const char *name() const { return _name; } |
|
168 |
|
169 void print_on_error(outputStream* st) const; |
|
170 |
|
171 #ifndef PRODUCT |
|
172 void print_on(outputStream* st) const; |
|
173 void print() const { print_on(::tty); } |
|
174 DEBUG_ONLY(int rank() const { return _rank; }) |
|
175 bool allow_vm_block() { return _allow_vm_block; } |
|
176 |
|
177 DEBUG_ONLY(Mutex *next() const { return _next; }) |
|
178 DEBUG_ONLY(void set_next(Mutex *next) { _next = next; }) |
|
179 #endif |
|
180 |
|
181 void set_owner(Thread* owner) { |
|
182 #ifndef PRODUCT |
|
183 set_owner_implementation(owner); |
|
184 DEBUG_ONLY(void verify_mutex(Thread* thr);) |
|
185 #else |
|
186 _owner = owner; |
|
187 #endif |
|
188 } |
|
189 }; |
|
190 |
|
191 class Monitor : public Mutex { |
|
192 void assert_wait_lock_state (Thread* self) NOT_DEBUG_RETURN; |
|
193 public: |
|
194 Monitor(int rank, const char *name, bool allow_vm_block = false, |
|
195 SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
|
196 // default destructor |
148 |
197 |
149 // Wait until monitor is notified (or times out). |
198 // Wait until monitor is notified (or times out). |
150 // Defaults are to make safepoint checks, wait time is forever (i.e., |
199 // Defaults are to make safepoint checks, wait time is forever (i.e., |
151 // zero), and not a suspend-equivalent condition. Returns true if wait |
200 // zero), and not a suspend-equivalent condition. Returns true if wait |
152 // times out; otherwise returns false. |
201 // times out; otherwise returns false. |
153 bool wait(long timeout = 0, |
202 bool wait(long timeout = 0, |
154 bool as_suspend_equivalent = !_as_suspend_equivalent_flag); |
203 bool as_suspend_equivalent = !_as_suspend_equivalent_flag); |
155 bool wait_without_safepoint_check(long timeout = 0); |
204 bool wait_without_safepoint_check(long timeout = 0); |
156 void notify(); |
205 void notify(); |
157 void notify_all(); |
206 void notify_all(); |
158 |
207 }; |
159 |
208 |
160 void lock(); // prints out warning if VM thread blocks |
|
161 void lock(Thread *thread); // overloaded with current thread |
|
162 void unlock(); |
|
163 bool is_locked() const { return _owner != NULL; } |
|
164 |
|
165 bool try_lock(); // Like lock(), but unblocking. It returns false instead |
|
166 |
|
167 void release_for_safepoint(); |
|
168 |
|
169 // Lock without safepoint check. Should ONLY be used by safepoint code and other code |
|
170 // that is guaranteed not to block while running inside the VM. |
|
171 void lock_without_safepoint_check(); |
|
172 void lock_without_safepoint_check(Thread* self); |
|
173 |
|
174 // Current owner - not not MT-safe. Can only be used to guarantee that |
|
175 // the current running thread owns the lock |
|
176 Thread* owner() const { return _owner; } |
|
177 bool owned_by_self() const; |
|
178 |
|
179 const char *name() const { return _name; } |
|
180 |
|
181 void print_on_error(outputStream* st) const; |
|
182 |
|
183 #ifndef PRODUCT |
|
184 void print_on(outputStream* st) const; |
|
185 void print() const { print_on(::tty); } |
|
186 DEBUG_ONLY(int rank() const { return _rank; }) |
|
187 bool allow_vm_block() { return _allow_vm_block; } |
|
188 |
|
189 DEBUG_ONLY(Monitor *next() const { return _next; }) |
|
190 DEBUG_ONLY(void set_next(Monitor *next) { _next = next; }) |
|
191 #endif |
|
192 |
|
193 void set_owner(Thread* owner) { |
|
194 #ifndef PRODUCT |
|
195 set_owner_implementation(owner); |
|
196 DEBUG_ONLY(void verify_Monitor(Thread* thr);) |
|
197 #else |
|
198 _owner = owner; |
|
199 #endif |
|
200 } |
|
201 |
|
202 }; |
|
203 |
|
204 class PaddedMonitor : public Monitor { |
|
205 enum { |
|
206 CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Monitor), |
|
207 PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1 |
|
208 }; |
|
209 char _padding[PADDING_LEN]; |
|
210 public: |
|
211 PaddedMonitor(int rank, const char *name, bool allow_vm_block = false, |
|
212 SafepointCheckRequired safepoint_check_required = _safepoint_check_always) : |
|
213 Monitor(rank, name, allow_vm_block, safepoint_check_required) {}; |
|
214 }; |
|
215 |
|
216 // Normally we'd expect Monitor to extend Mutex in the sense that a monitor |
|
217 // constructed from pthreads primitives might extend a mutex by adding |
|
218 // a condvar and some extra metadata. In fact this was the case until J2SE7. |
|
219 // |
|
220 // Currently, however, the base object is a monitor. Monitor contains all the |
|
221 // logic for wait(), notify(), etc. Mutex extends monitor and restricts the |
|
222 // visibility of wait(), notify(), and notify_all(). |
|
223 // |
|
224 // Another viable alternative would have been to have Monitor extend Mutex and |
|
225 // implement all the normal mutex and wait()-notify() logic in Mutex base class. |
|
226 // The wait()-notify() facility would be exposed via special protected member functions |
|
227 // (e.g., _Wait() and _Notify()) in Mutex. Monitor would extend Mutex and expose wait() |
|
228 // as a call to _Wait(). That is, the public wait() would be a wrapper for the protected |
|
229 // _Wait(). |
|
230 // |
|
231 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead. |
|
232 // After all, monitors are sufficient for Java-level synchronization. At one point in time |
|
233 // there may have been some benefit to having distinct mutexes and monitors, but that time |
|
234 // has passed. |
|
235 // |
|
236 |
|
237 class Mutex : public Monitor { // degenerate Monitor |
|
238 public: |
|
239 Mutex(int rank, const char *name, bool allow_vm_block = false, |
|
240 SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
|
241 // default destructor |
|
242 private: |
|
243 void notify(); |
|
244 void notify_all(); |
|
245 bool wait(long timeout, bool as_suspend_equivalent); |
|
246 bool wait_without_safepoint_check(long timeout); |
|
247 }; |
|
248 |
209 |
249 class PaddedMutex : public Mutex { |
210 class PaddedMutex : public Mutex { |
250 enum { |
211 enum { |
251 CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Mutex), |
212 CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Mutex), |
252 PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1 |
213 PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1 |