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 *
23 */
24
25 #ifndef SHARE_RUNTIME_SYNCHRONIZER_HPP
26 #define SHARE_RUNTIME_SYNCHRONIZER_HPP
27
28 #include "memory/padded.hpp"
29 #include "oops/markWord.hpp"
30 #include "runtime/basicLock.hpp"
31 #include "runtime/handles.hpp"
32 #include "utilities/resourceHash.hpp"
33
34 template <typename T> class GrowableArray;
35 class LogStream;
36 class ObjectMonitor;
37 class ObjectMonitorDeflationSafepointer;
38 class ThreadsList;
39
40 class MonitorList {
41 friend class VMStructs;
42
43 private:
44 ObjectMonitor* volatile _head;
45 volatile size_t _count;
46 volatile size_t _max;
47
48 public:
49 void add(ObjectMonitor* monitor);
50 size_t unlink_deflated(size_t deflated_count,
51 GrowableArray<ObjectMonitor*>* unlinked_list,
75 inflate_cause_vm_internal = 0,
76 inflate_cause_monitor_enter = 1,
77 inflate_cause_wait = 2,
78 inflate_cause_notify = 3,
79 inflate_cause_hash_code = 4,
80 inflate_cause_jni_enter = 5,
81 inflate_cause_jni_exit = 6,
82 inflate_cause_nof = 7 // Number of causes
83 } InflateCause;
84
85 typedef enum {
86 NOT_ENABLED = 0,
87 FATAL_EXIT = 1,
88 LOG_WARNING = 2
89 } SyncDiagnosticOption;
90
91 // exit must be implemented non-blocking, since the compiler cannot easily handle
92 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
93
94 // This is the "slow path" version of monitor enter and exit.
95 static void enter(Handle obj, BasicLock* lock, JavaThread* current);
96 static void exit(oop obj, BasicLock* lock, JavaThread* current);
97 // Used to enter a monitor for another thread. This requires that the
98 // locking_thread is suspended, and that entering on a potential
99 // inflated monitor may only contend with deflation. That is the obj being
100 // locked on is either already locked by the locking_thread or cannot
101 // escape the locking_thread.
102 static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
103 private:
104 // Shared implementation for enter and enter_for. Performs all but
105 // inflated monitor enter.
106 static bool enter_fast_impl(Handle obj, BasicLock* lock, JavaThread* locking_thread);
107
108 public:
109 // Used only to handle jni locks or other unmatched monitor enter/exit
110 // Internally they will use heavy weight monitor.
111 static void jni_enter(Handle obj, JavaThread* current);
112 static void jni_exit(oop obj, TRAPS);
113
114 // Handle all interpreter, compiler and jni cases
115 static int wait(Handle obj, jlong millis, TRAPS);
116 static void notify(Handle obj, TRAPS);
117 static void notifyall(Handle obj, TRAPS);
118
119 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
120 static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
121
122 // Inflate light weight monitor to heavy weight monitor
123 static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
124 // Used to inflate a monitor as if it was done from the thread JavaThread.
125 static ObjectMonitor* inflate_for(JavaThread* thread, oop obj, const InflateCause cause);
126
127 private:
128 // Shared implementation between the different LockingMode.
129 static ObjectMonitor* inflate_impl(JavaThread* thread, oop obj, const InflateCause cause);
130
131 public:
132 // This version is only for internal use
133 static void inflate_helper(oop obj);
134 static const char* inflate_cause_name(const InflateCause cause);
135
136 // Returns the identity hash value for an oop
137 // NOTE: It may cause monitor inflation
138 static intptr_t FastHashCode(Thread* current, oop obj);
139
140 // java.lang.Thread support
141 static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
142
143 static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
144
145 // JNI detach support
146 static void release_monitors_owned_by_thread(JavaThread* current);
147
148 // Iterate over all ObjectMonitors.
149 template <typename Function>
150 static void monitors_iterate(Function function);
151
152 // Iterate ObjectMonitors owned by any thread and where the owner `filter`
153 // returns true.
154 template <typename OwnerFilter>
155 static void owned_monitors_iterate_filtered(MonitorClosure* closure, OwnerFilter filter);
177 static bool is_async_deflation_needed();
178 static bool is_async_deflation_requested() { return _is_async_deflation_requested; }
179 static bool is_final_audit() { return _is_final_audit; }
180 static void set_is_final_audit() { _is_final_audit = true; }
181 static jlong last_async_deflation_time_ns() { return _last_async_deflation_time_ns; }
182 static void request_deflate_idle_monitors();
183 static bool request_deflate_idle_monitors_from_wb(); // for whitebox test support
184 static void set_is_async_deflation_requested(bool new_value) { _is_async_deflation_requested = new_value; }
185 static jlong time_since_last_async_deflation_ms();
186
187 // debugging
188 static void audit_and_print_stats(outputStream* out, bool on_exit);
189 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
190 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
191 int* error_cnt_p);
192 static void do_final_audit_and_print_stats();
193 static void log_in_use_monitor_details(outputStream* out, bool log_all);
194
195 private:
196 friend class SynchronizerTest;
197
198 static MonitorList _in_use_list;
199 static volatile bool _is_async_deflation_requested;
200 static volatile bool _is_final_audit;
201 static jlong _last_async_deflation_time_ns;
202
203 // Support for SynchronizerTest access to GVars fields:
204 static u_char* get_gvars_addr();
205 static u_char* get_gvars_hc_sequence_addr();
206 static size_t get_gvars_size();
207 static u_char* get_gvars_stw_random_addr();
208
209 static void handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread);
210 };
211
212 // ObjectLocker enforces balanced locking and can never throw an
213 // IllegalMonitorStateException. However, a pending exception may
214 // have to pass through, and we must also be able to deal with
215 // asynchronous exceptions. The caller is responsible for checking
216 // the thread's pending exception if needed.
|
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 *
23 */
24
25 #ifndef SHARE_RUNTIME_SYNCHRONIZER_HPP
26 #define SHARE_RUNTIME_SYNCHRONIZER_HPP
27
28 #include "memory/padded.hpp"
29 #include "oops/markWord.hpp"
30 #include "runtime/basicLock.hpp"
31 #include "runtime/handles.hpp"
32 #include "runtime/javaThread.hpp"
33 #include "utilities/resourceHash.hpp"
34
35 template <typename T> class GrowableArray;
36 class LogStream;
37 class ObjectMonitor;
38 class ObjectMonitorDeflationSafepointer;
39 class ThreadsList;
40
41 class MonitorList {
42 friend class VMStructs;
43
44 private:
45 ObjectMonitor* volatile _head;
46 volatile size_t _count;
47 volatile size_t _max;
48
49 public:
50 void add(ObjectMonitor* monitor);
51 size_t unlink_deflated(size_t deflated_count,
52 GrowableArray<ObjectMonitor*>* unlinked_list,
76 inflate_cause_vm_internal = 0,
77 inflate_cause_monitor_enter = 1,
78 inflate_cause_wait = 2,
79 inflate_cause_notify = 3,
80 inflate_cause_hash_code = 4,
81 inflate_cause_jni_enter = 5,
82 inflate_cause_jni_exit = 6,
83 inflate_cause_nof = 7 // Number of causes
84 } InflateCause;
85
86 typedef enum {
87 NOT_ENABLED = 0,
88 FATAL_EXIT = 1,
89 LOG_WARNING = 2
90 } SyncDiagnosticOption;
91
92 // exit must be implemented non-blocking, since the compiler cannot easily handle
93 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
94
95 // This is the "slow path" version of monitor enter and exit.
96 static inline void enter(Handle obj, BasicLock* lock, JavaThread* current);
97 static inline void exit(oop obj, BasicLock* lock, JavaThread* current);
98
99 // Used to enter a monitor for another thread. This requires that the
100 // locking_thread is suspended, and that entering on a potential
101 // inflated monitor may only contend with deflation. That is the obj being
102 // locked on is either already locked by the locking_thread or cannot
103 // escape the locking_thread.
104 static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
105 private:
106 // Shared implementation for enter and enter_for. Performs all but
107 // inflated monitor enter.
108 static bool enter_fast_impl(Handle obj, BasicLock* lock, JavaThread* locking_thread);
109
110 static bool quick_enter_legacy(oop obj, JavaThread* current, BasicLock* Lock);
111 static void enter_legacy(Handle obj, BasicLock* Lock, JavaThread* current);
112 static void exit_legacy(oop obj, BasicLock* lock, JavaThread* current);
113 public:
114 // Used only to handle jni locks or other unmatched monitor enter/exit
115 // Internally they will use heavy weight monitor.
116 static void jni_enter(Handle obj, JavaThread* current);
117 static void jni_exit(oop obj, TRAPS);
118
119 // Handle all interpreter, compiler and jni cases
120 static int wait(Handle obj, jlong millis, TRAPS);
121 static void notify(Handle obj, TRAPS);
122 static void notifyall(Handle obj, TRAPS);
123
124 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
125 static inline bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
126
127 // Inflate light weight monitor to heavy weight monitor
128 static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
129 // Used to inflate a monitor as if it was done from the thread JavaThread.
130 static ObjectMonitor* inflate_for(JavaThread* thread, oop obj, const InflateCause cause);
131
132 private:
133 // Shared implementation between the different LockingMode.
134 static ObjectMonitor* inflate_impl(oop obj, const InflateCause cause);
135
136 public:
137 // This version is only for internal use
138 static void inflate_helper(oop obj);
139 static const char* inflate_cause_name(const InflateCause cause);
140
141 inline static ObjectMonitor* read_monitor(markWord mark);
142 inline static ObjectMonitor* read_monitor(Thread* current, oop obj, markWord mark);
143
144 // Returns the identity hash value for an oop
145 // NOTE: It may cause monitor inflation
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
156 // Iterate over all ObjectMonitors.
157 template <typename Function>
158 static void monitors_iterate(Function function);
159
160 // Iterate ObjectMonitors owned by any thread and where the owner `filter`
161 // returns true.
162 template <typename OwnerFilter>
163 static void owned_monitors_iterate_filtered(MonitorClosure* closure, OwnerFilter filter);
185 static bool is_async_deflation_needed();
186 static bool is_async_deflation_requested() { return _is_async_deflation_requested; }
187 static bool is_final_audit() { return _is_final_audit; }
188 static void set_is_final_audit() { _is_final_audit = true; }
189 static jlong last_async_deflation_time_ns() { return _last_async_deflation_time_ns; }
190 static void request_deflate_idle_monitors();
191 static bool request_deflate_idle_monitors_from_wb(); // for whitebox test support
192 static void set_is_async_deflation_requested(bool new_value) { _is_async_deflation_requested = new_value; }
193 static jlong time_since_last_async_deflation_ms();
194
195 // debugging
196 static void audit_and_print_stats(outputStream* out, bool on_exit);
197 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
198 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
199 int* error_cnt_p);
200 static void do_final_audit_and_print_stats();
201 static void log_in_use_monitor_details(outputStream* out, bool log_all);
202
203 private:
204 friend class SynchronizerTest;
205 friend class LightweightSynchronizer;
206
207 static MonitorList _in_use_list;
208 static volatile bool _is_async_deflation_requested;
209 static volatile bool _is_final_audit;
210 static jlong _last_async_deflation_time_ns;
211
212 // Support for SynchronizerTest access to GVars fields:
213 static u_char* get_gvars_addr();
214 static u_char* get_gvars_hc_sequence_addr();
215 static size_t get_gvars_size();
216 static u_char* get_gvars_stw_random_addr();
217
218 static void handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread);
219 };
220
221 // ObjectLocker enforces balanced locking and can never throw an
222 // IllegalMonitorStateException. However, a pending exception may
223 // have to pass through, and we must also be able to deal with
224 // asynchronous exceptions. The caller is responsible for checking
225 // the thread's pending exception if needed.
|