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