< prev index next >

src/hotspot/share/runtime/synchronizer.hpp

Print this page

  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  *

 75     inflate_cause_wait = 2,
 76     inflate_cause_notify = 3,
 77     inflate_cause_hash_code = 4,
 78     inflate_cause_jni_enter = 5,
 79     inflate_cause_jni_exit = 6,
 80     inflate_cause_nof = 7 // Number of causes
 81   } InflateCause;
 82 
 83   typedef enum {
 84     NOT_ENABLED    = 0,
 85     FATAL_EXIT     = 1,
 86     LOG_WARNING    = 2
 87   } SyncDiagnosticOption;
 88 
 89   // exit must be implemented non-blocking, since the compiler cannot easily handle
 90   // deoptimization at monitor exit. Hence, it does not take a Handle argument.
 91 
 92   // This is the "slow path" version of monitor enter and exit.
 93   static void enter(Handle obj, BasicLock* lock, JavaThread* current);
 94   static void exit(oop obj, BasicLock* lock, JavaThread* current);










 95 

 96   // Used only to handle jni locks or other unmatched monitor enter/exit
 97   // Internally they will use heavy weight monitor.
 98   static void jni_enter(Handle obj, JavaThread* current);
 99   static void jni_exit(oop obj, TRAPS);
100 
101   // Handle all interpreter, compiler and jni cases
102   static int  wait(Handle obj, jlong millis, TRAPS);
103   static void notify(Handle obj, TRAPS);
104   static void notifyall(Handle obj, TRAPS);
105 
106   static bool quick_notify(oopDesc* obj, JavaThread* current, bool All);
107   static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
108 
109   // Special internal-use-only method for use by JVM infrastructure
110   // that needs to wait() on a java-level object but that can't risk
111   // throwing unexpected InterruptedExecutionExceptions.
112   static void waitUninterruptibly(Handle obj, jlong Millis, TRAPS);
113 
114   // Inflate light weight monitor to heavy weight monitor
115   static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);








116   // This version is only for internal use
117   static void inflate_helper(oop obj);
118   static const char* inflate_cause_name(const InflateCause cause);
119 
120   // Returns the identity hash value for an oop
121   // NOTE: It may cause monitor inflation
122   static intptr_t FastHashCode(Thread* current, oop obj);
123 
124   // java.lang.Thread support
125   static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
126 
127   static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
128 
129   // JNI detach support
130   static void release_monitors_owned_by_thread(JavaThread* current);
131 
132   // Iterate over all ObjectMonitors.
133   template <typename Function>
134   static void monitors_iterate(Function function);
135 

176   static void chk_in_use_list(outputStream* out, int* error_cnt_p);
177   static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
178                                int* error_cnt_p);
179   static void do_final_audit_and_print_stats();
180   static void log_in_use_monitor_details(outputStream* out, bool log_all);
181 
182  private:
183   friend class SynchronizerTest;
184 
185   static MonitorList _in_use_list;
186   static volatile bool _is_async_deflation_requested;
187   static volatile bool _is_final_audit;
188   static jlong         _last_async_deflation_time_ns;
189 
190   // Support for SynchronizerTest access to GVars fields:
191   static u_char* get_gvars_addr();
192   static u_char* get_gvars_hc_sequence_addr();
193   static size_t get_gvars_size();
194   static u_char* get_gvars_stw_random_addr();
195 
196   static void handle_sync_on_value_based_class(Handle obj, JavaThread* current);
197 };
198 
199 // ObjectLocker enforces balanced locking and can never throw an
200 // IllegalMonitorStateException. However, a pending exception may
201 // have to pass through, and we must also be able to deal with
202 // asynchronous exceptions. The caller is responsible for checking
203 // the thread's pending exception if needed.
204 class ObjectLocker : public StackObj {
205  private:
206   JavaThread* _thread;
207   Handle      _obj;
208   BasicLock   _lock;
209  public:
210   ObjectLocker(Handle obj, JavaThread* current);
211   ~ObjectLocker();
212 
213   // Monitor behavior
214   void wait(TRAPS)  { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
215   void wait_uninterruptibly(TRAPS)  { ObjectSynchronizer::waitUninterruptibly(_obj, 0, CHECK); } // wait forever
216   void notify_all(TRAPS)  { ObjectSynchronizer::notifyall(_obj, CHECK); }

  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  *

 75     inflate_cause_wait = 2,
 76     inflate_cause_notify = 3,
 77     inflate_cause_hash_code = 4,
 78     inflate_cause_jni_enter = 5,
 79     inflate_cause_jni_exit = 6,
 80     inflate_cause_nof = 7 // Number of causes
 81   } InflateCause;
 82 
 83   typedef enum {
 84     NOT_ENABLED    = 0,
 85     FATAL_EXIT     = 1,
 86     LOG_WARNING    = 2
 87   } SyncDiagnosticOption;
 88 
 89   // exit must be implemented non-blocking, since the compiler cannot easily handle
 90   // deoptimization at monitor exit. Hence, it does not take a Handle argument.
 91 
 92   // This is the "slow path" version of monitor enter and exit.
 93   static void enter(Handle obj, BasicLock* lock, JavaThread* current);
 94   static void exit(oop obj, BasicLock* lock, JavaThread* current);
 95   // Used to enter a monitor for another thread. This requires that the
 96   // locking_thread is suspended, and that entering on a potential
 97   // inflated monitor may only contend with deflation. That is the obj being
 98   // locked on is either already locked by the locking_thread or cannot
 99   // escape the locking_thread.
100   static void enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread);
101 private:
102   // Shared implementation for enter and enter_for. Performs all but
103   // inflated monitor enter.
104   static bool enter_fast_impl(Handle obj, BasicLock* lock, JavaThread* locking_thread);
105 
106 public:
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, JavaThread* current, BasicLock* Lock);
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   // Inflate light weight monitor to heavy weight monitor
126   static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
127   // Used to inflate a monitor as if it was done from the thread JavaThread.
128   static ObjectMonitor* inflate_for(JavaThread* thread, oop obj, const InflateCause cause);
129 
130 private:
131   // Shared implementation between the different LockingMode.
132   static ObjectMonitor* inflate_impl(JavaThread* thread, oop obj, const InflateCause cause);
133 
134 public:
135   // This version is only for internal use
136   static void inflate_helper(oop obj);
137   static const char* inflate_cause_name(const InflateCause cause);
138 
139   // Returns the identity hash value for an oop
140   // NOTE: It may cause monitor inflation
141   static intptr_t FastHashCode(Thread* current, oop obj);
142 
143   // java.lang.Thread support
144   static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
145 
146   static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
147 
148   // JNI detach support
149   static void release_monitors_owned_by_thread(JavaThread* current);
150 
151   // Iterate over all ObjectMonitors.
152   template <typename Function>
153   static void monitors_iterate(Function function);
154 

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  private:
202   friend class SynchronizerTest;
203 
204   static MonitorList _in_use_list;
205   static volatile bool _is_async_deflation_requested;
206   static volatile bool _is_final_audit;
207   static jlong         _last_async_deflation_time_ns;
208 
209   // Support for SynchronizerTest access to GVars fields:
210   static u_char* get_gvars_addr();
211   static u_char* get_gvars_hc_sequence_addr();
212   static size_t get_gvars_size();
213   static u_char* get_gvars_stw_random_addr();
214 
215   static void handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread);
216 };
217 
218 // ObjectLocker enforces balanced locking and can never throw an
219 // IllegalMonitorStateException. However, a pending exception may
220 // have to pass through, and we must also be able to deal with
221 // asynchronous exceptions. The caller is responsible for checking
222 // the thread's pending exception if needed.
223 class ObjectLocker : public StackObj {
224  private:
225   JavaThread* _thread;
226   Handle      _obj;
227   BasicLock   _lock;
228  public:
229   ObjectLocker(Handle obj, JavaThread* current);
230   ~ObjectLocker();
231 
232   // Monitor behavior
233   void wait(TRAPS)  { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
234   void wait_uninterruptibly(TRAPS)  { ObjectSynchronizer::waitUninterruptibly(_obj, 0, CHECK); } // wait forever
235   void notify_all(TRAPS)  { ObjectSynchronizer::notifyall(_obj, CHECK); }
< prev index next >