1 /*
2 * Copyright (c) 1998, 2025, 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 *
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/hashTable.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 MonitorList() : _head(nullptr), _count(0), _max(0) {};
51 void add(ObjectMonitor* monitor);
52 size_t unlink_deflated(size_t deflated_count,
53 GrowableArray<ObjectMonitor*>* unlinked_list,
54 ObjectMonitorDeflationSafepointer* safepointer);
55 size_t count() const;
56 size_t max() const;
57
58 class Iterator;
59 Iterator iterator() const;
60 };
61
62 class MonitorList::Iterator {
63 ObjectMonitor* _current;
64
65 public:
66 Iterator(ObjectMonitor* head) : _current(head) {}
67 bool has_next() const { return _current != nullptr; }
68 ObjectMonitor* next();
69 };
70
71 class ObjectSynchronizer : AllStatic {
72 friend class VMStructs;
73 friend class ObjectMonitorDeflationLogging;
74 friend class WhiteBox;
75
76 public:
77 typedef enum {
78 inflate_cause_vm_internal = 0,
79 inflate_cause_monitor_enter = 1,
80 inflate_cause_wait = 2,
81 inflate_cause_notify = 3,
82 inflate_cause_jni_enter = 4,
83 inflate_cause_jni_exit = 5,
84 inflate_cause_nof = 6 // Number of causes
85 } InflateCause;
86
87 typedef enum {
88 NOT_ENABLED = 0,
89 FATAL_EXIT = 1,
90 LOG_WARNING = 2
91 } SyncDiagnosticOption;
92
93 // exit must be implemented non-blocking, since the compiler cannot easily handle
94 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
95
96 // This is the "slow path" version of monitor enter and exit.
97 static void enter(Handle obj, BasicLock* lock, JavaThread* current);
98 static void exit(oop obj, BasicLock* lock, JavaThread* current);
99
100 // Used to enter a monitor for another thread. This requires that the
101 // locking_thread is suspended, and that entering on a potential
102 // inflated monitor may only contend with deflation. That is the obj being
103 // locked on is either already locked by the locking_thread or cannot
104 // escape the locking_thread.
105 static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
106
107 // Used only to handle jni locks or other unmatched monitor enter/exit
108 // Internally they will use heavy weight monitor.
109 static void jni_enter(Handle obj, JavaThread* current);
110 static void jni_exit(oop obj, TRAPS);
111
112 // Handle all interpreter, compiler and jni cases
113 static int wait(Handle obj, jlong millis, TRAPS);
114 static void notify(Handle obj, TRAPS);
115 static void notifyall(Handle obj, TRAPS);
116
117 static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
118 static bool quick_enter(oop obj, BasicLock* Lock, JavaThread* current);
119
120 // Special internal-use-only method for use by JVM infrastructure
121 // that needs to wait() on a java-level object but that can't risk
122 // throwing unexpected InterruptedExecutionExceptions.
123 static void waitUninterruptibly(Handle obj, jlong Millis, TRAPS);
124
125 public:
126 static const char* inflate_cause_name(const InflateCause cause);
127
128 static ObjectMonitor* read_monitor(markWord mark);
129 static ObjectMonitor* read_monitor(Thread* current, oop obj);
130 static ObjectMonitor* read_monitor(Thread* current, oop obj, markWord mark);
131
132 // Returns the identity hash value for an oop
133 // NOTE: It may cause monitor inflation
134 static intptr_t FastHashCode(Thread* current, oop obj);
135
136 // NOTE: May not cause monitor inflation
137 static uint32_t get_hash(markWord mark, oop obj);
138 // For CDS path.
139 static uint32_t get_hash(markWord mark, oop obj, Klass* klass);
140
141 // java.lang.Thread support
142 static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
143
144 static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
145
146 // JNI detach support
147 static void release_monitors_owned_by_thread(JavaThread* current);
148
149 // Iterate over all ObjectMonitors.
150 template <typename Function>
151 static void monitors_iterate(Function function);
152
153 // Iterate ObjectMonitors owned by any thread and where the owner `filter`
154 // returns true.
155 template <typename OwnerFilter>
156 static void owned_monitors_iterate_filtered(MonitorClosure* closure, OwnerFilter filter);
157
158 // Iterate ObjectMonitors where the owner is thread; this does NOT include
159 // ObjectMonitors where the owner is anonymous.
160 static void owned_monitors_iterate(MonitorClosure* m, JavaThread* thread);
161
162 // Iterate ObjectMonitors where the owner is vthread.
163 static void owned_monitors_iterate(MonitorClosure* m, oop vthread);
164
165 // Iterate ObjectMonitors owned by any thread.
166 static void owned_monitors_iterate(MonitorClosure* closure);
167
168 // Initialize the gInflationLocks
169 static void initialize();
170
171 // We currently use aggressive monitor deflation policy;
172 // basically we try to deflate all monitors that are not busy.
173 static size_t deflate_idle_monitors();
174
175 // Deflate idle monitors:
176 static size_t deflate_monitor_list(ObjectMonitorDeflationSafepointer* safepointer);
177 static size_t in_use_list_count();
178 static size_t in_use_list_max();
179 static size_t in_use_list_ceiling();
180 static void dec_in_use_list_ceiling();
181 static void inc_in_use_list_ceiling();
182 static void set_in_use_list_ceiling(size_t new_value);
183 static bool is_async_deflation_needed();
184 static bool is_async_deflation_requested() { return _is_async_deflation_requested; }
185 static bool is_final_audit() { return _is_final_audit; }
186 static void set_is_final_audit() { _is_final_audit = true; }
187 static jlong last_async_deflation_time_ns() { return _last_async_deflation_time_ns; }
188 static void request_deflate_idle_monitors();
189 static bool request_deflate_idle_monitors_from_wb(); // for whitebox test support
190 static void set_is_async_deflation_requested(bool new_value) { _is_async_deflation_requested = new_value; }
191 static jlong time_since_last_async_deflation_ms();
192
193 // debugging
194 static void audit_and_print_stats(outputStream* out, bool on_exit);
195 static void chk_in_use_list(outputStream* out, int* error_cnt_p);
196 static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
197 int* error_cnt_p);
198 static void do_final_audit_and_print_stats();
199 static void log_in_use_monitor_details(outputStream* out, bool log_all);
200
201 static intptr_t get_next_hash(Thread* current, oop obj);
202
203 private:
204 friend class SynchronizerTest;
205
206 static MonitorList _in_use_list;
207 static volatile bool _is_async_deflation_requested;
208 static volatile bool _is_final_audit;
209 static jlong _last_async_deflation_time_ns;
210
211 // Support for SynchronizerTest access to GVars fields:
212 static u_char* get_gvars_addr();
213 static u_char* get_gvars_hc_sequence_addr();
214 static size_t get_gvars_size();
215 static u_char* get_gvars_stw_random_addr();
216
217 static void handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread);
218
219 static ObjectMonitor* get_or_insert_monitor_from_table(oop object, JavaThread* current, bool* inserted);
220 static ObjectMonitor* get_or_insert_monitor(oop object, JavaThread* current, ObjectSynchronizer::InflateCause cause);
221
222 static ObjectMonitor* add_monitor(JavaThread* current, ObjectMonitor* monitor, oop obj);
223 static bool remove_monitor(Thread* current, ObjectMonitor* monitor, oop obj);
224
225 static void deflate_mark_word(oop object);
226
227 static void ensure_lock_stack_space(JavaThread* current);
228
229 class CacheSetter;
230 class LockStackInflateContendedLocks;
231 class VerifyThreadState;
232
233 static void create_om_table();
234
235 public:
236 static bool needs_resize();
237 static bool resize_table(JavaThread* current);
238
239 private:
240 static inline bool fast_lock_try_enter(oop obj, LockStack& lock_stack, JavaThread* current);
241 static bool fast_lock_spin_enter(oop obj, LockStack& lock_stack, JavaThread* current, bool observed_deflation);
242
243 public:
244 static ObjectMonitor* inflate_into_object_header(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, Thread* current);
245 static ObjectMonitor* inflate_locked_or_imse(oop object, ObjectSynchronizer::InflateCause cause, TRAPS);
246 static ObjectMonitor* inflate_fast_locked_object(oop object, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current);
247 static ObjectMonitor* inflate_and_enter(oop object, BasicLock* lock, ObjectSynchronizer::InflateCause cause, JavaThread* locking_thread, JavaThread* current);
248
249 static void deflate_monitor(Thread* current, oop obj, ObjectMonitor* monitor);
250
251 static ObjectMonitor* get_monitor_from_table(Thread* current, oop obj);
252
253 static bool contains_monitor(Thread* current, ObjectMonitor* monitor);
254
255 static bool quick_enter_internal(oop obj, BasicLock* Lock, JavaThread* current);
256 };
257
258 // ObjectLocker enforces balanced locking and can never throw an
259 // IllegalMonitorStateException. However, a pending exception may
260 // have to pass through, and we must also be able to deal with
261 // asynchronous exceptions. The caller is responsible for checking
262 // the thread's pending exception if needed.
263 // When using ObjectLocker the top native frames in the stack will
264 // not be seen in case we attempt preemption, since we start walking
265 // from the last Java anchor, so we disable it with NoPreemptMark.
266 class ObjectLocker : public StackObj {
267 private:
268 JavaThread* _thread;
269 Handle _obj;
270 BasicLock _lock;
271 NoPreemptMark _npm;
272 bool _skip_exit;
273 public:
274 ObjectLocker(Handle obj, TRAPS);
275 ~ObjectLocker();
276
277 // Monitor behavior
278 void wait_uninterruptibly(TRAPS);
279 void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); }
280 };
281
282 // Interface to visit monitors
283 class ObjectMonitorsView {
284 public:
285 // Visit monitors that belong to the given thread
286 virtual void visit(MonitorClosure* closure, JavaThread* thread) = 0;
287 };
288
289 #endif // SHARE_RUNTIME_SYNCHRONIZER_HPP