1 /* 2 * Copyright (c) 1998, 2013, 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_VM_RUNTIME_OBJECTMONITOR_HPP 26 #define SHARE_VM_RUNTIME_OBJECTMONITOR_HPP 27 28 #include "runtime/os.hpp" 29 #include "runtime/park.hpp" 30 #include "runtime/perfData.hpp" 31 32 // ObjectWaiter serves as a "proxy" or surrogate thread. 33 // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific 34 // ParkEvent instead. Beware, however, that the JVMTI code 35 // knows about ObjectWaiters, so we'll have to reconcile that code. 36 // See next_waiter(), first_waiter(), etc. 37 38 class ObjectWaiter : public StackObj { 39 public: 40 enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ; 41 enum Sorted { PREPEND, APPEND, SORTED } ; 42 ObjectWaiter * volatile _next; 43 ObjectWaiter * volatile _prev; 44 Thread* _thread; 45 jlong _notifier_tid; 46 ParkEvent * _event; 47 volatile int _notified ; 48 volatile TStates TState ; 49 Sorted _Sorted ; // List placement disposition 50 bool _active ; // Contention monitoring is enabled 51 public: 52 ObjectWaiter(Thread* thread); 53 54 void wait_reenter_begin(ObjectMonitor *mon); 55 void wait_reenter_end(ObjectMonitor *mon); 56 }; 57 58 // forward declaration to avoid include tracing.hpp 59 class EventJavaMonitorWait; 60 61 // WARNING: 62 // This is a very sensitive and fragile class. DO NOT make any 63 // change unless you are fully aware of the underlying semantics. 64 65 // This class can not inherit from any other class, because I have 66 // to let the displaced header be the very first word. Otherwise I 67 // have to let markOop include this file, which would export the 68 // monitor data structure to everywhere. 69 // 70 // The ObjectMonitor class is used to implement JavaMonitors which have 71 // transformed from the lightweight structure of the thread stack to a 72 // heavy weight lock due to contention 73 74 // It is also used as RawMonitor by the JVMTI 75 76 77 class ObjectMonitor { 78 public: 79 enum { 80 OM_OK, // no error 81 OM_SYSTEM_ERROR, // operating system error 82 OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException 83 OM_INTERRUPTED, // Thread.interrupt() 84 OM_TIMED_OUT // Object.wait() timed out 85 }; 86 87 public: 88 // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ... 89 // ByteSize would also be an appropriate type. 90 static int header_offset_in_bytes() { return offset_of(ObjectMonitor, _header); } 91 static int object_offset_in_bytes() { return offset_of(ObjectMonitor, _object); } 92 static int owner_offset_in_bytes() { return offset_of(ObjectMonitor, _owner); } 93 static int count_offset_in_bytes() { return offset_of(ObjectMonitor, _count); } 94 static int recursions_offset_in_bytes() { return offset_of(ObjectMonitor, _recursions); } 95 static int cxq_offset_in_bytes() { return offset_of(ObjectMonitor, _cxq) ; } 96 static int succ_offset_in_bytes() { return offset_of(ObjectMonitor, _succ) ; } 97 static int EntryList_offset_in_bytes() { return offset_of(ObjectMonitor, _EntryList); } 98 static int FreeNext_offset_in_bytes() { return offset_of(ObjectMonitor, FreeNext); } 99 static int WaitSet_offset_in_bytes() { return offset_of(ObjectMonitor, _WaitSet) ; } 100 static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible);} 101 static int Spinner_offset_in_bytes() { return offset_of(ObjectMonitor, _Spinner); } 102 static int trace_exit_stack_offset_in_bytes() { return offset_of(ObjectMonitor, _trace_exit_stack); } 103 104 public: 105 // Eventaully we'll make provisions for multiple callbacks, but 106 // now one will suffice. 107 static int (*SpinCallbackFunction)(intptr_t, int) ; 108 static intptr_t SpinCallbackArgument ; 109 110 111 public: 112 markOop header() const; 113 void set_header(markOop hdr); 114 115 intptr_t is_busy() const { 116 // TODO-FIXME: merge _count and _waiters. 117 // TODO-FIXME: assert _owner == null implies _recursions = 0 118 // TODO-FIXME: assert _WaitSet != null implies _count > 0 119 return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList ) ; 120 } 121 122 intptr_t is_entered(Thread* current) const; 123 124 void* owner() const; 125 void set_owner(void* owner); 126 127 intptr_t waiters() const; 128 129 intptr_t count() const; 130 void set_count(intptr_t count); 131 intptr_t contentions() const ; 132 intptr_t recursions() const { return _recursions; } 133 134 intptr_t next_trace_seq() { return Atomic::add_ptr(1, &_trace_current_seq); } 135 136 // JVM/DI GetMonitorInfo() needs this 137 ObjectWaiter* first_waiter() { return _WaitSet; } 138 ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; } 139 Thread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; } 140 141 // initialize the monitor, exception the semaphore, all other fields 142 // are simple integers or pointers 143 ObjectMonitor() { 144 _header = NULL; 145 _count = 0; 146 _waiters = 0, 147 _recursions = 0; 148 _object = NULL; 149 _owner = NULL; 150 _WaitSet = NULL; 151 _WaitSetLock = 0 ; 152 _Responsible = NULL ; 153 _succ = NULL ; 154 _cxq = NULL ; 155 FreeNext = NULL ; 156 _EntryList = NULL ; 157 _SpinFreq = 0 ; 158 _SpinClock = 0 ; 159 OwnerIsThread = 0 ; 160 _previous_owner_tid = 0; 161 _trace_current_seq = 0; 162 _trace_exit_stack = 0; 163 } 164 165 ~ObjectMonitor() { 166 // TODO: Add asserts ... 167 // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 168 // _count == 0 _EntryList == NULL etc 169 } 170 171 private: 172 void Recycle () { 173 // TODO: add stronger asserts ... 174 // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 175 // _count == 0 EntryList == NULL 176 // _recursions == 0 _WaitSet == NULL 177 // TODO: assert (is_busy()|_recursions) == 0 178 _succ = NULL ; 179 _EntryList = NULL ; 180 _cxq = NULL ; 181 _WaitSet = NULL ; 182 _recursions = 0 ; 183 _SpinFreq = 0 ; 184 _SpinClock = 0 ; 185 OwnerIsThread = 0 ; 186 // tracing: do not reset _trace_current_seq, we also use it 187 // to detect pending events before a monitor is recycled 188 } 189 190 public: 191 192 void* object() const; 193 void* object_addr(); 194 void set_object(void* obj); 195 196 bool check(TRAPS); // true if the thread owns the monitor. 197 void check_slow(TRAPS); 198 void clear(); 199 #ifndef PRODUCT 200 void verify(); 201 void print(); 202 #endif 203 204 bool try_enter (TRAPS) ; 205 void enter(TRAPS); 206 void exit(bool not_suspended, TRAPS); 207 void wait(jlong millis, bool interruptable, TRAPS); 208 void notify(TRAPS); 209 void notifyAll(TRAPS); 210 211 // Use the following at your own risk 212 void complete_exit(intptr_t *saved_recursions, intptr_t *saved_trace_exit_stack, TRAPS); 213 void reenter(intptr_t saved_recursions, intptr_t saved_trace_exit_stack, TRAPS); 214 215 private: 216 void AddWaiter (ObjectWaiter * waiter) ; 217 static void DeferredInitialize(); 218 219 ObjectWaiter * DequeueWaiter () ; 220 void DequeueSpecificWaiter (ObjectWaiter * waiter) ; 221 void enter (int after_wait, TRAPS); 222 void exit(intptr_t *exit_stack_id_for_wait, bool not_suspended, TRAPS); 223 int EnterI (TRAPS) ; 224 void ReenterI (Thread * Self, ObjectWaiter * SelfNode) ; 225 void UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ; 226 int TryLock (Thread * Self) ; 227 int NotRunnable (Thread * Self, Thread * Owner) ; 228 int TrySpin_Fixed (Thread * Self) ; 229 int TrySpin_VaryFrequency (Thread * Self) ; 230 int TrySpin_VaryDuration (Thread * Self) ; 231 void ctAsserts () ; 232 void ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ; 233 bool ExitSuspendEquivalent (JavaThread * Self) ; 234 void post_monitor_wait_event(EventJavaMonitorWait * event, 235 jlong notifier_tid, 236 jlong timeout, 237 bool timedout); 238 239 private: 240 friend class ObjectSynchronizer; 241 friend class ObjectWaiter; 242 friend class VMStructs; 243 244 // WARNING: this must be the very first word of ObjectMonitor 245 // This means this class can't use any virtual member functions. 246 // TODO-FIXME: assert that offsetof(_header) is 0 or get rid of the 247 // implicit 0 offset in emitted code. 248 249 volatile markOop _header; // displaced object header word - mark 250 void* volatile _object; // backward object pointer - strong root 251 252 double SharingPad [1] ; // temp to reduce false sharing 253 254 // All the following fields must be machine word aligned 255 // The VM assumes write ordering wrt these fields, which can be 256 // read from other threads. 257 258 protected: // protected for jvmtiRawMonitor 259 void * volatile _owner; // pointer to owning thread OR BasicLock 260 volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor 261 volatile intptr_t _recursions; // recursion count, 0 for first entry 262 private: 263 int OwnerIsThread ; // _owner is (Thread *) vs SP/BasicLock 264 ObjectWaiter * volatile _cxq ; // LL of recently-arrived threads blocked on entry. 265 // The list is actually composed of WaitNodes, acting 266 // as proxies for Threads. 267 protected: 268 ObjectWaiter * volatile _EntryList ; // Threads blocked on entry or reentry. 269 private: 270 Thread * volatile _succ ; // Heir presumptive thread - used for futile wakeup throttling 271 Thread * volatile _Responsible ; 272 int _PromptDrain ; // rqst to drain cxq into EntryList ASAP 273 274 volatile int _Spinner ; // for exit->spinner handoff optimization 275 volatile int _SpinFreq ; // Spin 1-out-of-N attempts: success rate 276 volatile int _SpinClock ; 277 volatile int _SpinDuration ; 278 volatile intptr_t _SpinState ; // MCS/CLH list of spinners 279 280 // TODO-FIXME: _count, _waiters and _recursions should be of 281 // type int, or int32_t but not intptr_t. There's no reason 282 // to use 64-bit fields for these variables on a 64-bit JVM. 283 284 volatile intptr_t _count; // reference count to prevent reclaimation/deflation 285 // at stop-the-world time. See deflate_idle_monitors(). 286 // _count is approximately |_WaitSet| + |_EntryList| 287 288 // TODO: this class appears to be sensitive to false sharing. 289 // there might be a better location to place this field. 290 volatile intptr_t _trace_current_seq; 291 volatile intptr_t _trace_exit_stack; 292 protected: 293 volatile intptr_t _waiters; // number of waiting threads 294 private: 295 protected: 296 ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor 297 private: 298 volatile int _WaitSetLock; // protects Wait Queue - simple spinlock 299 300 public: 301 int _QMix ; // Mixed prepend queue discipline 302 ObjectMonitor * FreeNext ; // Free list linkage 303 intptr_t StatA, StatsB ; 304 305 public: 306 static void Initialize () ; 307 static PerfCounter * _sync_ContendedLockAttempts ; 308 static PerfCounter * _sync_FutileWakeups ; 309 static PerfCounter * _sync_Parks ; 310 static PerfCounter * _sync_EmptyNotifications ; 311 static PerfCounter * _sync_Notifications ; 312 static PerfCounter * _sync_SlowEnter ; 313 static PerfCounter * _sync_SlowExit ; 314 static PerfCounter * _sync_SlowNotify ; 315 static PerfCounter * _sync_SlowNotifyAll ; 316 static PerfCounter * _sync_FailedSpins ; 317 static PerfCounter * _sync_SuccessfulSpins ; 318 static PerfCounter * _sync_PrivateA ; 319 static PerfCounter * _sync_PrivateB ; 320 static PerfCounter * _sync_MonInCirculation ; 321 static PerfCounter * _sync_MonScavenged ; 322 static PerfCounter * _sync_Inflations ; 323 static PerfCounter * _sync_Deflations ; 324 static PerfLongVariable * _sync_MonExtant ; 325 326 public: 327 static int Knob_Verbose; 328 static int Knob_SpinLimit; 329 void* operator new (size_t size) throw() { 330 return AllocateHeap(size, mtInternal); 331 } 332 void* operator new[] (size_t size) throw() { 333 return operator new (size); 334 } 335 void operator delete(void* p) { 336 FreeHeap(p, mtInternal); 337 } 338 void operator delete[] (void *p) { 339 operator delete(p); 340 } 341 }; 342 343 #undef TEVENT 344 #define TEVENT(nom) {if (SyncVerbose) FEVENT(nom); } 345 346 #define FEVENT(nom) { static volatile int ctr = 0 ; int v = ++ctr ; if ((v & (v-1)) == 0) { ::printf (#nom " : %d \n", v); ::fflush(stdout); }} 347 348 #undef TEVENT 349 #define TEVENT(nom) {;} 350 351 352 #endif // SHARE_VM_RUNTIME_OBJECTMONITOR_HPP