src/share/vm/runtime/objectMonitor.hpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2233
fa83ab460c54
child 2325
c760f78e0a53
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

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

mercurial