Fri, 12 Feb 2010 15:27:36 -0800
Merge
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 1998-2007 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | // WARNING: |
duke@435 | 26 | // This is a very sensitive and fragile class. DO NOT make any |
duke@435 | 27 | // change unless you are fully aware of the underlying semantics. |
duke@435 | 28 | |
duke@435 | 29 | // This class can not inherit from any other class, because I have |
duke@435 | 30 | // to let the displaced header be the very first word. Otherwise I |
duke@435 | 31 | // have to let markOop include this file, which would export the |
duke@435 | 32 | // monitor data structure to everywhere. |
duke@435 | 33 | // |
duke@435 | 34 | // The ObjectMonitor class is used to implement JavaMonitors which have |
duke@435 | 35 | // transformed from the lightweight structure of the thread stack to a |
duke@435 | 36 | // heavy weight lock due to contention |
duke@435 | 37 | |
duke@435 | 38 | // It is also used as RawMonitor by the JVMTI |
duke@435 | 39 | |
duke@435 | 40 | |
duke@435 | 41 | class ObjectWaiter; |
duke@435 | 42 | |
duke@435 | 43 | class ObjectMonitor { |
duke@435 | 44 | public: |
duke@435 | 45 | enum { |
duke@435 | 46 | OM_OK, // no error |
duke@435 | 47 | OM_SYSTEM_ERROR, // operating system error |
duke@435 | 48 | OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException |
duke@435 | 49 | OM_INTERRUPTED, // Thread.interrupt() |
duke@435 | 50 | OM_TIMED_OUT // Object.wait() timed out |
duke@435 | 51 | }; |
duke@435 | 52 | |
duke@435 | 53 | public: |
duke@435 | 54 | // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ... |
duke@435 | 55 | // ByteSize would also be an appropriate type. |
duke@435 | 56 | static int header_offset_in_bytes() { return offset_of(ObjectMonitor, _header); } |
duke@435 | 57 | static int object_offset_in_bytes() { return offset_of(ObjectMonitor, _object); } |
duke@435 | 58 | static int owner_offset_in_bytes() { return offset_of(ObjectMonitor, _owner); } |
duke@435 | 59 | static int count_offset_in_bytes() { return offset_of(ObjectMonitor, _count); } |
duke@435 | 60 | static int recursions_offset_in_bytes() { return offset_of(ObjectMonitor, _recursions); } |
duke@435 | 61 | static int cxq_offset_in_bytes() { return offset_of(ObjectMonitor, _cxq) ; } |
duke@435 | 62 | static int succ_offset_in_bytes() { return offset_of(ObjectMonitor, _succ) ; } |
duke@435 | 63 | static int EntryList_offset_in_bytes() { return offset_of(ObjectMonitor, _EntryList); } |
duke@435 | 64 | static int FreeNext_offset_in_bytes() { return offset_of(ObjectMonitor, FreeNext); } |
duke@435 | 65 | static int WaitSet_offset_in_bytes() { return offset_of(ObjectMonitor, _WaitSet) ; } |
duke@435 | 66 | static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible);} |
duke@435 | 67 | static int Spinner_offset_in_bytes() { return offset_of(ObjectMonitor, _Spinner); } |
duke@435 | 68 | |
duke@435 | 69 | public: |
duke@435 | 70 | // Eventaully we'll make provisions for multiple callbacks, but |
duke@435 | 71 | // now one will suffice. |
duke@435 | 72 | static int (*SpinCallbackFunction)(intptr_t, int) ; |
duke@435 | 73 | static intptr_t SpinCallbackArgument ; |
duke@435 | 74 | |
duke@435 | 75 | |
duke@435 | 76 | public: |
duke@435 | 77 | ObjectMonitor(); |
duke@435 | 78 | ~ObjectMonitor(); |
duke@435 | 79 | |
duke@435 | 80 | markOop header() const; |
duke@435 | 81 | void set_header(markOop hdr); |
duke@435 | 82 | |
duke@435 | 83 | intptr_t is_busy() const; |
duke@435 | 84 | intptr_t is_entered(Thread* current) const; |
duke@435 | 85 | |
duke@435 | 86 | void* owner() const; |
duke@435 | 87 | void set_owner(void* owner); |
duke@435 | 88 | |
duke@435 | 89 | intptr_t waiters() const; |
duke@435 | 90 | |
duke@435 | 91 | intptr_t count() const; |
duke@435 | 92 | void set_count(intptr_t count); |
duke@435 | 93 | intptr_t contentions() const ; |
duke@435 | 94 | |
duke@435 | 95 | // JVM/DI GetMonitorInfo() needs this |
duke@435 | 96 | Thread * thread_of_waiter (ObjectWaiter *) ; |
duke@435 | 97 | ObjectWaiter * first_waiter () ; |
duke@435 | 98 | ObjectWaiter * next_waiter(ObjectWaiter* o); |
duke@435 | 99 | |
duke@435 | 100 | intptr_t recursions() const { return _recursions; } |
duke@435 | 101 | |
duke@435 | 102 | void* object() const; |
duke@435 | 103 | void* object_addr(); |
duke@435 | 104 | void set_object(void* obj); |
duke@435 | 105 | |
duke@435 | 106 | bool check(TRAPS); // true if the thread owns the monitor. |
duke@435 | 107 | void check_slow(TRAPS); |
duke@435 | 108 | void clear(); |
duke@435 | 109 | #ifndef PRODUCT |
duke@435 | 110 | void verify(); |
duke@435 | 111 | void print(); |
duke@435 | 112 | #endif |
duke@435 | 113 | |
duke@435 | 114 | bool try_enter (TRAPS) ; |
duke@435 | 115 | void enter(TRAPS); |
duke@435 | 116 | void exit(TRAPS); |
duke@435 | 117 | void wait(jlong millis, bool interruptable, TRAPS); |
duke@435 | 118 | void notify(TRAPS); |
duke@435 | 119 | void notifyAll(TRAPS); |
duke@435 | 120 | |
duke@435 | 121 | // Use the following at your own risk |
duke@435 | 122 | intptr_t complete_exit(TRAPS); |
duke@435 | 123 | void reenter(intptr_t recursions, TRAPS); |
duke@435 | 124 | |
duke@435 | 125 | int raw_enter(TRAPS); |
duke@435 | 126 | int raw_exit(TRAPS); |
duke@435 | 127 | int raw_wait(jlong millis, bool interruptable, TRAPS); |
duke@435 | 128 | int raw_notify(TRAPS); |
duke@435 | 129 | int raw_notifyAll(TRAPS); |
duke@435 | 130 | |
duke@435 | 131 | private: |
duke@435 | 132 | // JVMTI support -- remove ASAP |
duke@435 | 133 | int SimpleEnter (Thread * Self) ; |
duke@435 | 134 | int SimpleExit (Thread * Self) ; |
duke@435 | 135 | int SimpleWait (Thread * Self, jlong millis) ; |
duke@435 | 136 | int SimpleNotify (Thread * Self, bool All) ; |
duke@435 | 137 | |
duke@435 | 138 | private: |
duke@435 | 139 | void Recycle () ; |
duke@435 | 140 | void AddWaiter (ObjectWaiter * waiter) ; |
duke@435 | 141 | |
duke@435 | 142 | ObjectWaiter * DequeueWaiter () ; |
duke@435 | 143 | void DequeueSpecificWaiter (ObjectWaiter * waiter) ; |
duke@435 | 144 | void EnterI (TRAPS) ; |
duke@435 | 145 | void ReenterI (Thread * Self, ObjectWaiter * SelfNode) ; |
duke@435 | 146 | void UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ; |
duke@435 | 147 | int TryLock (Thread * Self) ; |
duke@435 | 148 | int NotRunnable (Thread * Self, Thread * Owner) ; |
duke@435 | 149 | int TrySpin_Fixed (Thread * Self) ; |
duke@435 | 150 | int TrySpin_VaryFrequency (Thread * Self) ; |
duke@435 | 151 | int TrySpin_VaryDuration (Thread * Self) ; |
duke@435 | 152 | void ctAsserts () ; |
duke@435 | 153 | void ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ; |
duke@435 | 154 | bool ExitSuspendEquivalent (JavaThread * Self) ; |
duke@435 | 155 | |
duke@435 | 156 | private: |
duke@435 | 157 | friend class ObjectSynchronizer; |
duke@435 | 158 | friend class ObjectWaiter; |
duke@435 | 159 | friend class VMStructs; |
duke@435 | 160 | |
duke@435 | 161 | // WARNING: this must be the very first word of ObjectMonitor |
duke@435 | 162 | // This means this class can't use any virtual member functions. |
duke@435 | 163 | // TODO-FIXME: assert that offsetof(_header) is 0 or get rid of the |
duke@435 | 164 | // implicit 0 offset in emitted code. |
duke@435 | 165 | |
duke@435 | 166 | volatile markOop _header; // displaced object header word - mark |
duke@435 | 167 | void* volatile _object; // backward object pointer - strong root |
duke@435 | 168 | |
duke@435 | 169 | double SharingPad [1] ; // temp to reduce false sharing |
duke@435 | 170 | |
duke@435 | 171 | // All the following fields must be machine word aligned |
duke@435 | 172 | // The VM assumes write ordering wrt these fields, which can be |
duke@435 | 173 | // read from other threads. |
duke@435 | 174 | |
duke@435 | 175 | void * volatile _owner; // pointer to owning thread OR BasicLock |
duke@435 | 176 | volatile intptr_t _recursions; // recursion count, 0 for first entry |
duke@435 | 177 | int OwnerIsThread ; // _owner is (Thread *) vs SP/BasicLock |
duke@435 | 178 | ObjectWaiter * volatile _cxq ; // LL of recently-arrived threads blocked on entry. |
duke@435 | 179 | // The list is actually composed of WaitNodes, acting |
duke@435 | 180 | // as proxies for Threads. |
duke@435 | 181 | ObjectWaiter * volatile _EntryList ; // Threads blocked on entry or reentry. |
duke@435 | 182 | Thread * volatile _succ ; // Heir presumptive thread - used for futile wakeup throttling |
duke@435 | 183 | Thread * volatile _Responsible ; |
duke@435 | 184 | int _PromptDrain ; // rqst to drain cxq into EntryList ASAP |
duke@435 | 185 | |
duke@435 | 186 | volatile int _Spinner ; // for exit->spinner handoff optimization |
duke@435 | 187 | volatile int _SpinFreq ; // Spin 1-out-of-N attempts: success rate |
duke@435 | 188 | volatile int _SpinClock ; |
duke@435 | 189 | volatile int _SpinDuration ; |
duke@435 | 190 | volatile intptr_t _SpinState ; // MCS/CLH list of spinners |
duke@435 | 191 | |
duke@435 | 192 | // TODO-FIXME: _count, _waiters and _recursions should be of |
duke@435 | 193 | // type int, or int32_t but not intptr_t. There's no reason |
duke@435 | 194 | // to use 64-bit fields for these variables on a 64-bit JVM. |
duke@435 | 195 | |
duke@435 | 196 | volatile intptr_t _count; // reference count to prevent reclaimation/deflation |
duke@435 | 197 | // at stop-the-world time. See deflate_idle_monitors(). |
duke@435 | 198 | // _count is approximately |_WaitSet| + |_EntryList| |
duke@435 | 199 | volatile intptr_t _waiters; // number of waiting threads |
duke@435 | 200 | ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor |
duke@435 | 201 | volatile int _WaitSetLock; // protects Wait Queue - simple spinlock |
duke@435 | 202 | |
duke@435 | 203 | public: |
duke@435 | 204 | int _QMix ; // Mixed prepend queue discipline |
duke@435 | 205 | ObjectMonitor * FreeNext ; // Free list linkage |
duke@435 | 206 | intptr_t StatA, StatsB ; |
duke@435 | 207 | |
duke@435 | 208 | }; |