< prev index next >

src/hotspot/share/runtime/synchronizer.hpp

Print this page

 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 ThreadsList;
 38 
 39 // Hash table of void* to a list of ObjectMonitor* owned by the JavaThread.
 40 // The JavaThread's owner key is either a JavaThread* or a stack lock
 41 // address in the JavaThread so we use "void*".
 42 //
 43 class ObjectMonitorsHashtable {
 44  private:
 45   static unsigned int ptr_hash(void* const& s1) {
 46     // 2654435761 = 2^32 * Phi (golden ratio)
 47     return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
 48   }
 49 
 50  public:
 51   class PtrList;
 52 
 53  private:
 54   // ResourceHashtable SIZE is specified at compile time so we
 55   // use 1031 which is the first prime after 1024.
 56   typedef ResourceHashtable<void*, PtrList*, 1031, AnyObj::C_HEAP, mtThread,
 57                             &ObjectMonitorsHashtable::ptr_hash> PtrTable;
 58   PtrTable* _ptrs;
 59   size_t _key_count;
 60   size_t _om_count;
 61 
 62  public:
 63   // ResourceHashtable is passed to various functions and populated in
 64   // different places so we allocate it using C_HEAP to make it immune
 65   // from any ResourceMarks that happen to be in the code paths.
 66   ObjectMonitorsHashtable() : _ptrs(new (mtThread) PtrTable), _key_count(0), _om_count(0) {}
 67 
 68   ~ObjectMonitorsHashtable();
 69 
 70   void add_entry(void* key, ObjectMonitor* om);
 71 
 72   void add_entry(void* key, PtrList* list) {
 73     _ptrs->put(key, list);
 74     _key_count++;
 75   }
 76 
 77   PtrList* get_entry(void* key) {
 78     PtrList** listpp = _ptrs->get(key);
 79     return (listpp == nullptr) ? nullptr : *listpp;
 80   }
 81 
 82   bool has_entry(void* key, ObjectMonitor* om);
 83 
 84   size_t key_count() { return _key_count; }
 85   size_t om_count() { return _om_count; }
 86 };
 87 
 88 class MonitorList {
 89   friend class VMStructs;
 90 
 91 private:
 92   ObjectMonitor* volatile _head;
 93   volatile size_t _count;
 94   volatile size_t _max;
 95 
 96 public:
 97   void add(ObjectMonitor* monitor);
 98   size_t unlink_deflated(Thread* current, LogStream* ls, elapsedTimer* timer_p,

 99                          GrowableArray<ObjectMonitor*>* unlinked_list);
100   size_t count() const;
101   size_t max() const;
102 
103   class Iterator;
104   Iterator iterator() const;
105 };
106 
107 class MonitorList::Iterator {
108   ObjectMonitor* _current;
109 
110 public:
111   Iterator(ObjectMonitor* head) : _current(head) {}
112   bool has_next() const { return _current != nullptr; }
113   ObjectMonitor* next();
114 };
115 
116 class ObjectSynchronizer : AllStatic {
117   friend class VMStructs;
118 

155   static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
156 
157   // Inflate light weight monitor to heavy weight monitor
158   static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
159   // This version is only for internal use
160   static void inflate_helper(oop obj);
161   static const char* inflate_cause_name(const InflateCause cause);
162 
163   // Returns the identity hash value for an oop
164   // NOTE: It may cause monitor inflation
165   static intptr_t FastHashCode(Thread* current, oop obj);
166 
167   // java.lang.Thread support
168   static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
169 
170   static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
171 
172   // JNI detach support
173   static void release_monitors_owned_by_thread(JavaThread* current);
174 









175   // Iterate ObjectMonitors where the owner == thread; this does NOT include
176   // ObjectMonitors where owner is set to a stack lock address in thread:
177   //
178   // This version of monitors_iterate() works with the in-use monitor list.
179   static void monitors_iterate(MonitorClosure* m, JavaThread* thread);
180   // This version of monitors_iterate() works with the specified linked list.
181   static void monitors_iterate(MonitorClosure* closure,
182                                ObjectMonitorsHashtable::PtrList* list,
183                                JavaThread* thread);
184 
185   // Initialize the gInflationLocks
186   static void initialize();
187 
188   // GC: we currently use aggressive monitor deflation policy
189   // Basically we try to deflate all monitors that are not busy.
190   static size_t deflate_idle_monitors(ObjectMonitorsHashtable* table);
191 
192   // Deflate idle monitors:
193   static void chk_for_block_req(JavaThread* current, const char* op_name,
194                                 const char* cnt_name, size_t cnt, LogStream* ls,
195                                 elapsedTimer* timer_p);
196   static size_t deflate_monitor_list(Thread* current, LogStream* ls, elapsedTimer* timer_p,
197                                      ObjectMonitorsHashtable* table);
198   static size_t in_use_list_ceiling();
199   static void dec_in_use_list_ceiling();
200   static void inc_in_use_list_ceiling();
201   static void set_in_use_list_ceiling(size_t new_value);
202   static bool is_async_deflation_needed();
203   static bool is_async_deflation_requested() { return _is_async_deflation_requested; }
204   static bool is_final_audit() { return _is_final_audit; }
205   static void set_is_final_audit() { _is_final_audit = true; }
206   static jlong last_async_deflation_time_ns() { return _last_async_deflation_time_ns; }
207   static bool request_deflate_idle_monitors();  // for whitebox test support

208   static void set_is_async_deflation_requested(bool new_value) { _is_async_deflation_requested = new_value; }
209   static jlong time_since_last_async_deflation_ms();
210 
211   // debugging
212   static void audit_and_print_stats(bool on_exit);
213   static void chk_in_use_list(outputStream* out, int* error_cnt_p);
214   static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
215                                int* error_cnt_p);
216   static void do_final_audit_and_print_stats();
217   static void log_in_use_monitor_details(outputStream* out);
218 
219  private:
220   friend class SynchronizerTest;
221 
222   static MonitorList _in_use_list;
223   static volatile bool _is_async_deflation_requested;
224   static volatile bool _is_final_audit;
225   static jlong         _last_async_deflation_time_ns;
226 
227   // Support for SynchronizerTest access to GVars fields:
228   static u_char* get_gvars_addr();
229   static u_char* get_gvars_hc_sequence_addr();
230   static size_t get_gvars_size();
231   static u_char* get_gvars_stw_random_addr();
232 
233   static void handle_sync_on_value_based_class(Handle obj, JavaThread* current);
234 };
235 
236 // ObjectLocker enforces balanced locking and can never throw an
237 // IllegalMonitorStateException. However, a pending exception may
238 // have to pass through, and we must also be able to deal with
239 // asynchronous exceptions. The caller is responsible for checking
240 // the thread's pending exception if needed.
241 class ObjectLocker : public StackObj {
242  private:
243   JavaThread* _thread;
244   Handle      _obj;
245   BasicLock   _lock;
246  public:
247   ObjectLocker(Handle obj, JavaThread* current);
248   ~ObjectLocker();
249 
250   // Monitor behavior
251   void wait(TRAPS)  { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
252   void notify_all(TRAPS)  { ObjectSynchronizer::notifyall(_obj, CHECK); }
253 };
254 







255 #endif // SHARE_RUNTIME_SYNCHRONIZER_HPP

 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 ThreadsList;
 38 

















































 39 class MonitorList {
 40   friend class VMStructs;
 41 
 42 private:
 43   ObjectMonitor* volatile _head;
 44   volatile size_t _count;
 45   volatile size_t _max;
 46 
 47 public:
 48   void add(ObjectMonitor* monitor);
 49   size_t unlink_deflated(Thread* current, LogStream* ls, elapsedTimer* timer_p,
 50                          size_t deflated_count,
 51                          GrowableArray<ObjectMonitor*>* unlinked_list);
 52   size_t count() const;
 53   size_t max() const;
 54 
 55   class Iterator;
 56   Iterator iterator() const;
 57 };
 58 
 59 class MonitorList::Iterator {
 60   ObjectMonitor* _current;
 61 
 62 public:
 63   Iterator(ObjectMonitor* head) : _current(head) {}
 64   bool has_next() const { return _current != nullptr; }
 65   ObjectMonitor* next();
 66 };
 67 
 68 class ObjectSynchronizer : AllStatic {
 69   friend class VMStructs;
 70 

107   static bool quick_enter(oop obj, JavaThread* current, BasicLock* Lock);
108 
109   // Inflate light weight monitor to heavy weight monitor
110   static ObjectMonitor* inflate(Thread* current, oop obj, const InflateCause cause);
111   // This version is only for internal use
112   static void inflate_helper(oop obj);
113   static const char* inflate_cause_name(const InflateCause cause);
114 
115   // Returns the identity hash value for an oop
116   // NOTE: It may cause monitor inflation
117   static intptr_t FastHashCode(Thread* current, oop obj);
118 
119   // java.lang.Thread support
120   static bool current_thread_holds_lock(JavaThread* current, Handle h_obj);
121 
122   static JavaThread* get_lock_owner(ThreadsList * t_list, Handle h_obj);
123 
124   // JNI detach support
125   static void release_monitors_owned_by_thread(JavaThread* current);
126 
127   // Iterate over all ObjectMonitors.
128   template <typename Function>
129   static void monitors_iterate(Function function);
130 
131   // Iterate ObjectMonitors owned by any thread and where the owner `filter`
132   // returns true.
133   template <typename OwnerFilter>
134   static void owned_monitors_iterate_filtered(MonitorClosure* closure, OwnerFilter filter);
135 
136   // Iterate ObjectMonitors where the owner == thread; this does NOT include
137   // ObjectMonitors where owner is set to a stack lock address in thread.
138   static void owned_monitors_iterate(MonitorClosure* m, JavaThread* thread);
139 
140   // Iterate ObjectMonitors owned by any thread.
141   static void owned_monitors_iterate(MonitorClosure* closure);



142 
143   // Initialize the gInflationLocks
144   static void initialize();
145 
146   // We currently use aggressive monitor deflation policy;
147   // basically we try to deflate all monitors that are not busy.
148   static size_t deflate_idle_monitors();
149 
150   // Deflate idle monitors:
151   static void chk_for_block_req(JavaThread* current, const char* op_name,
152                                 const char* cnt_name, size_t cnt, LogStream* ls,
153                                 elapsedTimer* timer_p);
154   static size_t deflate_monitor_list(Thread* current, LogStream* ls, 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 void request_deflate_idle_monitors();
165   static bool request_deflate_idle_monitors_from_wb();  // for whitebox test support
166   static void set_is_async_deflation_requested(bool new_value) { _is_async_deflation_requested = new_value; }
167   static jlong time_since_last_async_deflation_ms();
168 
169   // debugging
170   static void audit_and_print_stats(bool on_exit);
171   static void chk_in_use_list(outputStream* out, int* error_cnt_p);
172   static void chk_in_use_entry(ObjectMonitor* n, outputStream* out,
173                                int* error_cnt_p);
174   static void do_final_audit_and_print_stats();
175   static void log_in_use_monitor_details(outputStream* out, bool log_all);
176 
177  private:
178   friend class SynchronizerTest;
179 
180   static MonitorList _in_use_list;
181   static volatile bool _is_async_deflation_requested;
182   static volatile bool _is_final_audit;
183   static jlong         _last_async_deflation_time_ns;
184 
185   // Support for SynchronizerTest access to GVars fields:
186   static u_char* get_gvars_addr();
187   static u_char* get_gvars_hc_sequence_addr();
188   static size_t get_gvars_size();
189   static u_char* get_gvars_stw_random_addr();
190 
191   static void handle_sync_on_value_based_class(Handle obj, JavaThread* current);
192 };
193 
194 // ObjectLocker enforces balanced locking and can never throw an
195 // IllegalMonitorStateException. However, a pending exception may
196 // have to pass through, and we must also be able to deal with
197 // asynchronous exceptions. The caller is responsible for checking
198 // the thread's pending exception if needed.
199 class ObjectLocker : public StackObj {
200  private:
201   JavaThread* _thread;
202   Handle      _obj;
203   BasicLock   _lock;
204  public:
205   ObjectLocker(Handle obj, JavaThread* current);
206   ~ObjectLocker();
207 
208   // Monitor behavior
209   void wait(TRAPS)  { ObjectSynchronizer::wait(_obj, 0, CHECK); } // wait forever
210   void notify_all(TRAPS)  { ObjectSynchronizer::notifyall(_obj, CHECK); }
211 };
212 
213 // Interface to visit monitors
214 class ObjectMonitorsView {
215 public:
216   // Visit monitors that belong to the given thread
217   virtual void visit(MonitorClosure* closure, JavaThread* thread) = 0;
218 };
219 
220 #endif // SHARE_RUNTIME_SYNCHRONIZER_HPP
< prev index next >