1 /*
2 * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
123 inflate_cause_wait = 2,
124 inflate_cause_notify = 3,
125 inflate_cause_hash_code = 4,
126 inflate_cause_jni_enter = 5,
127 inflate_cause_jni_exit = 6,
128 inflate_cause_nof = 7 // Number of causes
129 } InflateCause;
130
131 typedef enum {
132 NOT_ENABLED = 0,
133 FATAL_EXIT = 1,
134 LOG_WARNING = 2
135 } SyncDiagnosticOption;
136
137 // exit must be implemented non-blocking, since the compiler cannot easily handle
138 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
139
140 // This is the "slow path" version of monitor enter and exit.
141 static void enter(Handle obj, BasicLock* lock, JavaThread* current);
142 static void exit(oop obj, BasicLock* lock, JavaThread* current);
143
144 // Used only to handle jni locks or other unmatched monitor enter/exit
145 // Internally they will use heavy weight monitor.
146 static void jni_enter(Handle obj, JavaThread* current);
147 static void jni_exit(oop obj, TRAPS);
148
149 // Handle all interpreter, compiler and jni cases
150 static int wait(Handle obj, jlong millis, TRAPS);
151 static void notify(Handle obj, TRAPS);
152 static void notifyall(Handle obj, TRAPS);
153
154 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
155 static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
156
157 // Inflate light weight monitor to heavy weight monitor
158 static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
159 // This version is only for internal use
160 static void inflate_helper(oop obj);
161 static const char* inflate_cause_name(const InflateCause cause);
162
163 // Returns the identity hash value for an oop
164 // NOTE: It may cause monitor inflation
165 static intptr_t FastHashCode(Thread* current, oop obj);
166
167 // java.lang.Thread support
168 static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
169
170 static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
171
172 // JNI detach support
173 static void release_monitors_owned_by_thread(JavaThread* current);
174
175 // Iterate ObjectMonitors where the owner == thread; this does NOT include
176 // ObjectMonitors where owner is set to a stack lock address in thread:
177 //
178 // This version of monitors_iterate() works with the in-use monitor list.
213 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
214 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
215 int* error_cnt_p);
216 static void do_final_audit_and_print_stats();
217 static void log_in_use_monitor_details(outputStream* out);
218
219 private:
220 friend class SynchronizerTest;
221
222 static MonitorList _in_use_list;
223 static volatile bool _is_async_deflation_requested;
224 static volatile bool _is_final_audit;
225 static jlong _last_async_deflation_time_ns;
226
227 // Support for SynchronizerTest access to GVars fields:
228 static u_char* get_gvars_addr();
229 static u_char* get_gvars_hc_sequence_addr();
230 static size_t get_gvars_size();
231 static u_char* get_gvars_stw_random_addr();
232
233 static void handle_sync_on_value_based_class(Handle obj, JavaThread* current);
234 };
235
236 // ObjectLocker enforces balanced locking and can never throw an
237 // IllegalMonitorStateException. However, a pending exception may
238 // have to pass through, and we must also be able to deal with
239 // asynchronous exceptions. The caller is responsible for checking
240 // the thread's pending exception if needed.
241 class ObjectLocker : public StackObj {
242 private:
243 JavaThread* _thread;
244 Handle _obj;
245 BasicLock _lock;
246 public:
247 ObjectLocker(Handle obj, JavaThread* current);
248 ~ObjectLocker();
249
250 // Monitor behavior
251 void wait(TRAPS) { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
252 void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); }
253 };
|
1 /*
2 * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
123 inflate_cause_wait = 2,
124 inflate_cause_notify = 3,
125 inflate_cause_hash_code = 4,
126 inflate_cause_jni_enter = 5,
127 inflate_cause_jni_exit = 6,
128 inflate_cause_nof = 7 // Number of causes
129 } InflateCause;
130
131 typedef enum {
132 NOT_ENABLED = 0,
133 FATAL_EXIT = 1,
134 LOG_WARNING = 2
135 } SyncDiagnosticOption;
136
137 // exit must be implemented non-blocking, since the compiler cannot easily handle
138 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
139
140 // This is the "slow path" version of monitor enter and exit.
141 static void enter(Handle obj, BasicLock* lock, JavaThread* current);
142 static void exit(oop obj, BasicLock* lock, JavaThread* current);
143 // Used to enter a monitor for another thread. This requires that the
144 // locking_thread is suspended, and that entering on a potential
145 // inflated monitor may only contend with deflation. That is the obj being
146 // locked on is either already locked by the locking_thread or cannot
147 // escape the locking_thread.
148 static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
149 private:
150 // Shared implementation for enter and enter_for. Performs all but
151 // inflated monitor enter.
152 static bool enter_fast_impl(Handle obj, BasicLock* lock, JavaThread* locking_thread);
153
154 public:
155 // Used only to handle jni locks or other unmatched monitor enter/exit
156 // Internally they will use heavy weight monitor.
157 static void jni_enter(Handle obj, JavaThread* current);
158 static void jni_exit(oop obj, TRAPS);
159
160 // Handle all interpreter, compiler and jni cases
161 static int wait(Handle obj, jlong millis, TRAPS);
162 static void notify(Handle obj, TRAPS);
163 static void notifyall(Handle obj, TRAPS);
164
165 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
166 static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
167
168 // Inflate light weight monitor to heavy weight monitor
169 static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
170 // Used to inflate a monitor as if it was done from the thread JavaThread.
171 static ObjectMonitor* inflate_for(JavaThread* thread, oop obj, const InflateCause cause);
172
173 private:
174 // Shared implementation between the different LockingMode.
175 static ObjectMonitor* inflate_impl(JavaThread* thread, oop obj, const InflateCause cause);
176
177 public:
178 // This version is only for internal use
179 static void inflate_helper(oop obj);
180 static const char* inflate_cause_name(const InflateCause cause);
181
182 // Returns the identity hash value for an oop
183 // NOTE: It may cause monitor inflation
184 static intptr_t FastHashCode(Thread* current, oop obj);
185
186 // java.lang.Thread support
187 static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
188
189 static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
190
191 // JNI detach support
192 static void release_monitors_owned_by_thread(JavaThread* current);
193
194 // Iterate ObjectMonitors where the owner == thread; this does NOT include
195 // ObjectMonitors where owner is set to a stack lock address in thread:
196 //
197 // This version of monitors_iterate() works with the in-use monitor list.
232 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
233 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
234 int* error_cnt_p);
235 static void do_final_audit_and_print_stats();
236 static void log_in_use_monitor_details(outputStream* out);
237
238 private:
239 friend class SynchronizerTest;
240
241 static MonitorList _in_use_list;
242 static volatile bool _is_async_deflation_requested;
243 static volatile bool _is_final_audit;
244 static jlong _last_async_deflation_time_ns;
245
246 // Support for SynchronizerTest access to GVars fields:
247 static u_char* get_gvars_addr();
248 static u_char* get_gvars_hc_sequence_addr();
249 static size_t get_gvars_size();
250 static u_char* get_gvars_stw_random_addr();
251
252 static void handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread);
253 };
254
255 // ObjectLocker enforces balanced locking and can never throw an
256 // IllegalMonitorStateException. However, a pending exception may
257 // have to pass through, and we must also be able to deal with
258 // asynchronous exceptions. The caller is responsible for checking
259 // the thread's pending exception if needed.
260 class ObjectLocker : public StackObj {
261 private:
262 JavaThread* _thread;
263 Handle _obj;
264 BasicLock _lock;
265 public:
266 ObjectLocker(Handle obj, JavaThread* current);
267 ~ObjectLocker();
268
269 // Monitor behavior
270 void wait(TRAPS) { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
271 void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); }
272 };
|