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