1 /*
2 * Copyright (c) 1998, 2021, 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 *
73 inflate_cause_wait = 2,
74 inflate_cause_notify = 3,
75 inflate_cause_hash_code = 4,
76 inflate_cause_jni_enter = 5,
77 inflate_cause_jni_exit = 6,
78 inflate_cause_nof = 7 // Number of causes
79 } InflateCause;
80
81 typedef enum {
82 NOT_ENABLED = 0,
83 FATAL_EXIT = 1,
84 LOG_WARNING = 2
85 } SyncDiagnosticOption;
86
87 // exit must be implemented non-blocking, since the compiler cannot easily handle
88 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
89
90 // This is the "slow path" version of monitor enter and exit.
91 static void enter(Handle obj, BasicLock* lock, JavaThread* current);
92 static void exit(oop obj, BasicLock* lock, JavaThread* current);
93
94 // Used only to handle jni locks or other unmatched monitor enter/exit
95 // Internally they will use heavy weight monitor.
96 static void jni_enter(Handle obj, JavaThread* current);
97 static void jni_exit(oop obj, TRAPS);
98
99 // Handle all interpreter, compiler and jni cases
100 static int wait(Handle obj, jlong millis, TRAPS);
101 static void notify(Handle obj, TRAPS);
102 static void notifyall(Handle obj, TRAPS);
103
104 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
105 static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
106
107 // Special internal-use-only method for use by JVM infrastructure
108 // that needs to wait() on a java-level object but must not respond
109 // to interrupt requests and doesn't timeout.
110 static void wait_uninterruptibly(Handle obj, JavaThread* current);
111
112 // used by classloading to free classloader object lock,
113 // wait on an internal lock, and reclaim original lock
114 // with original recursion count
115 static intx complete_exit(Handle obj, JavaThread* current);
116 static void reenter (Handle obj, intx recursions, JavaThread* current);
117
118 // Inflate light weight monitor to heavy weight monitor
119 static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
120 // This version is only for internal use
121 static void inflate_helper(oop obj);
122 static const char* inflate_cause_name(const InflateCause cause);
123
124 // Returns the identity hash value for an oop
125 // NOTE: It may cause monitor inflation
126 static intptr_t identity_hash_value_for(Handle obj);
127 static intptr_t FastHashCode(Thread* current, oop obj);
128
129 // java.lang.Thread support
130 static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
131
132 static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
133
134 // JNI detach support
135 static void release_monitors_owned_by_thread(JavaThread* current);
136 static void monitors_iterate(MonitorClosure* m, JavaThread* thread);
137
138 // Initialize the gInflationLocks
139 static void initialize();
166 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
167 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
168 int* error_cnt_p);
169 static void do_final_audit_and_print_stats();
170 static void log_in_use_monitor_details(outputStream* out);
171
172 private:
173 friend class SynchronizerTest;
174
175 static MonitorList _in_use_list;
176 static volatile bool _is_async_deflation_requested;
177 static volatile bool _is_final_audit;
178 static jlong _last_async_deflation_time_ns;
179
180 // Support for SynchronizerTest access to GVars fields:
181 static u_char* get_gvars_addr();
182 static u_char* get_gvars_hc_sequence_addr();
183 static size_t get_gvars_size();
184 static u_char* get_gvars_stw_random_addr();
185
186 static void handle_sync_on_value_based_class(Handle obj, JavaThread* current);
187 };
188
189 // ObjectLocker enforces balanced locking and can never throw an
190 // IllegalMonitorStateException. However, a pending exception may
191 // have to pass through, and we must also be able to deal with
192 // asynchronous exceptions. The caller is responsible for checking
193 // the thread's pending exception if needed.
194 class ObjectLocker : public StackObj {
195 private:
196 JavaThread* _thread;
197 Handle _obj;
198 BasicLock _lock;
199 public:
200 ObjectLocker(Handle obj, JavaThread* current);
201 ~ObjectLocker();
202
203 // Monitor behavior
204 void wait(TRAPS) { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
205 void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); }
206 void wait_uninterruptibly(JavaThread* current) { ObjectSynchronizer::wait_uninterruptibly(_obj, current); }
|
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 *
73 inflate_cause_wait = 2,
74 inflate_cause_notify = 3,
75 inflate_cause_hash_code = 4,
76 inflate_cause_jni_enter = 5,
77 inflate_cause_jni_exit = 6,
78 inflate_cause_nof = 7 // Number of causes
79 } InflateCause;
80
81 typedef enum {
82 NOT_ENABLED = 0,
83 FATAL_EXIT = 1,
84 LOG_WARNING = 2
85 } SyncDiagnosticOption;
86
87 // exit must be implemented non-blocking, since the compiler cannot easily handle
88 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
89
90 // This is the "slow path" version of monitor enter and exit.
91 static void enter(Handle obj, BasicLock* lock, JavaThread* current);
92 static void exit(oop obj, BasicLock* lock, JavaThread* current);
93 // Used to enter a monitor for another thread. This requires that the
94 // locking_thread is suspended, and that entering on a potential
95 // inflated monitor may only contend with deflation. That is the obj being
96 // locked on is either already locked by the locking_thread or cannot
97 // escape the locking_thread.
98 static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
99 private:
100 // Shared implementation for enter and enter_for. Performs all but
101 // inflated monitor enter.
102 static bool enter_fast_impl(Handle obj, BasicLock* lock, JavaThread* locking_thread);
103
104 public:
105 // Used only to handle jni locks or other unmatched monitor enter/exit
106 // Internally they will use heavy weight monitor.
107 static void jni_enter(Handle obj, JavaThread* current);
108 static void jni_exit(oop obj, TRAPS);
109
110 // Handle all interpreter, compiler and jni cases
111 static int wait(Handle obj, jlong millis, TRAPS);
112 static void notify(Handle obj, TRAPS);
113 static void notifyall(Handle obj, TRAPS);
114
115 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
116 static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
117
118 // Special internal-use-only method for use by JVM infrastructure
119 // that needs to wait() on a java-level object but must not respond
120 // to interrupt requests and doesn't timeout.
121 static void wait_uninterruptibly(Handle obj, JavaThread* current);
122
123 // used by classloading to free classloader object lock,
124 // wait on an internal lock, and reclaim original lock
125 // with original recursion count
126 static intx complete_exit(Handle obj, JavaThread* current);
127 static void reenter (Handle obj, intx recursions, JavaThread* current);
128
129 // Inflate light weight monitor to heavy weight monitor
130 static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
131 // Used to inflate a monitor as if it was done from the thread JavaThread.
132 static ObjectMonitor* inflate_for(JavaThread* thread, oop obj, const InflateCause cause);
133
134 private:
135 // Shared implementation between the different LockingMode.
136 static ObjectMonitor* inflate_impl(JavaThread* thread, oop obj, const InflateCause cause);
137
138 public:
139 // This version is only for internal use
140 static void inflate_helper(oop obj);
141 static const char* inflate_cause_name(const InflateCause cause);
142
143 // Returns the identity hash value for an oop
144 // NOTE: It may cause monitor inflation
145 static intptr_t identity_hash_value_for(Handle obj);
146 static intptr_t FastHashCode(Thread* current, oop obj);
147
148 // java.lang.Thread support
149 static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
150
151 static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
152
153 // JNI detach support
154 static void release_monitors_owned_by_thread(JavaThread* current);
155 static void monitors_iterate(MonitorClosure* m, JavaThread* thread);
156
157 // Initialize the gInflationLocks
158 static void initialize();
185 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
186 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
187 int* error_cnt_p);
188 static void do_final_audit_and_print_stats();
189 static void log_in_use_monitor_details(outputStream* out);
190
191 private:
192 friend class SynchronizerTest;
193
194 static MonitorList _in_use_list;
195 static volatile bool _is_async_deflation_requested;
196 static volatile bool _is_final_audit;
197 static jlong _last_async_deflation_time_ns;
198
199 // Support for SynchronizerTest access to GVars fields:
200 static u_char* get_gvars_addr();
201 static u_char* get_gvars_hc_sequence_addr();
202 static size_t get_gvars_size();
203 static u_char* get_gvars_stw_random_addr();
204
205 static void handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread);
206 };
207
208 // ObjectLocker enforces balanced locking and can never throw an
209 // IllegalMonitorStateException. However, a pending exception may
210 // have to pass through, and we must also be able to deal with
211 // asynchronous exceptions. The caller is responsible for checking
212 // the thread's pending exception if needed.
213 class ObjectLocker : public StackObj {
214 private:
215 JavaThread* _thread;
216 Handle _obj;
217 BasicLock _lock;
218 public:
219 ObjectLocker(Handle obj, JavaThread* current);
220 ~ObjectLocker();
221
222 // Monitor behavior
223 void wait(TRAPS) { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
224 void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); }
225 void wait_uninterruptibly(JavaThread* current) { ObjectSynchronizer::wait_uninterruptibly(_obj, current); }
|