src/share/vm/runtime/objectMonitor.hpp

Fri, 22 Oct 2010 15:59:34 -0400

author
acorn
date
Fri, 22 Oct 2010 15:59:34 -0400
changeset 2233
fa83ab460c54
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

6988353: refactor contended sync subsystem
Summary: reduce complexity by factoring synchronizer.cpp
Reviewed-by: dholmes, never, coleenp

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

mercurial