src/share/vm/runtime/objectMonitor.hpp

Sun, 03 Feb 2013 22:43:57 +0100

author
ewendeli
date
Sun, 03 Feb 2013 22:43:57 +0100
changeset 4703
b5cb079ecaa4
parent 2325
c760f78e0a53
child 5103
f9be75d21404
permissions
-rw-r--r--

Merge

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

mercurial