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  *
 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/growableArray.hpp"
 33 
 34 class LogStream;
 35 class ObjectMonitor;
 36 class ThreadsList;
 37 
 38 class MonitorList {
 39   friend class VMStructs;
 40 
 41 private:
 42   ObjectMonitor* volatile _head;
 43   volatile size_t _count;
 44   volatile size_t _max;
 45 
 46 public:
 47   void add(ObjectMonitor* monitor);
 48   size_t unlink_deflated(Thread* current, LogStream* ls, elapsedTimer* timer_p,
 49                          GrowableArray<ObjectMonitor*>* unlinked_list);
 50   size_t count() const;
 51   size_t max() const;
 52 
 53   class Iterator;
 54   Iterator iterator() const;
 55 };
 56 
 57 class MonitorList::Iterator {
 58   ObjectMonitor* _current;
 59 
 60 public:
 61   Iterator(ObjectMonitor* head) : _current(head) {}
 62   bool has_next() const { return _current != NULL; }
 63   ObjectMonitor* next();
 64 };
 65 
 66 class ObjectSynchronizer : AllStatic {
 67   friend class VMStructs;
 68 
 69  public:
 70   typedef enum {
 71     inflate_cause_vm_internal = 0,
 72     inflate_cause_monitor_enter = 1,
 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   static markWord safe_load_mark(oop obj);
125 
126   // Returns the identity hash value for an oop
127   // NOTE: It may cause monitor inflation
128   static intptr_t identity_hash_value_for(Handle obj);
129   static intptr_t FastHashCode(Thread* current, oop obj);
130 
131   static markWord stable_mark(const oop obj);
132 
133   // java.lang.Thread support
134   static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
135 
136   static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
137 
138   // JNI detach support
139   static void release_monitors_owned_by_thread(JavaThread* current);
140   static void monitors_iterate(MonitorClosure* m, JavaThread* thread);
141 
142   // Initialize the gInflationLocks
143   static void initialize();
144 
145   // GC: we current use aggressive monitor deflation policy
146   // Basically we try to deflate all monitors that are not busy.
147   static size_t deflate_idle_monitors();
148 
149   // Deflate idle monitors:
150   static void chk_for_block_req(JavaThread* current, const char* op_name,
151                                 const char* cnt_name, size_t cnt, LogStream* ls,
152                                 elapsedTimer* timer_p);
153   static size_t deflate_monitor_list(Thread* current, LogStream* ls,
154                                      elapsedTimer* timer_p);
155   static size_t in_use_list_ceiling();
156   static void dec_in_use_list_ceiling();
157   static void inc_in_use_list_ceiling();
158   static void set_in_use_list_ceiling(size_t new_value);
159   static bool is_async_deflation_needed();
160   static bool is_async_deflation_requested() { return _is_async_deflation_requested; }
161   static bool is_final_audit() { return _is_final_audit; }
162   static void set_is_final_audit() { _is_final_audit = true; }
163   static jlong last_async_deflation_time_ns() { return _last_async_deflation_time_ns; }
164   static bool request_deflate_idle_monitors();  // for whitebox test support
165   static void set_is_async_deflation_requested(bool new_value) { _is_async_deflation_requested = new_value; }
166   static jlong time_since_last_async_deflation_ms();
167 
168   // debugging
169   static void audit_and_print_stats(bool on_exit);
170   static void chk_in_use_list(outputStream* out, int* error_cnt_p);
171   static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
172                                int* error_cnt_p);
173   static void do_final_audit_and_print_stats();
174   static void log_in_use_monitor_details(outputStream* out);
175 
176  private:
177   friend class SynchronizerTest;
178 
179   static MonitorList _in_use_list;
180   static volatile bool _is_async_deflation_requested;
181   static volatile bool _is_final_audit;
182   static jlong         _last_async_deflation_time_ns;
183 
184   // Support for SynchronizerTest access to GVars fields:
185   static u_char* get_gvars_addr();
186   static u_char* get_gvars_hc_sequence_addr();
187   static size_t get_gvars_size();
188   static u_char* get_gvars_stw_random_addr();
189 
190   static void handle_sync_on_value_based_class(Handle obj, JavaThread* current);
191 };
192 
193 // ObjectLocker enforces balanced locking and can never throw an
194 // IllegalMonitorStateException. However, a pending exception may
195 // have to pass through, and we must also be able to deal with
196 // asynchronous exceptions. The caller is responsible for checking
197 // the thread's pending exception if needed.
198 class ObjectLocker : public StackObj {
199  private:
200   JavaThread* _thread;
201   Handle      _obj;
202   BasicLock   _lock;
203  public:
204   ObjectLocker(Handle obj, JavaThread* current);
205   ~ObjectLocker();
206 
207   // Monitor behavior
208   void wait(TRAPS)  { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
209   void notify_all(TRAPS)  { ObjectSynchronizer::notifyall(_obj, CHECK); }
210   void wait_uninterruptibly(JavaThread* current) { ObjectSynchronizer::wait_uninterruptibly(_obj, current); }
211 };
212 
213 #endif // SHARE_RUNTIME_SYNCHRONIZER_HPP