aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_RUNTIME_OBJECTMONITOR_HPP aoqi@0: #define SHARE_VM_RUNTIME_OBJECTMONITOR_HPP aoqi@0: aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "runtime/park.hpp" aoqi@0: #include "runtime/perfData.hpp" aoqi@0: aoqi@0: // ObjectWaiter serves as a "proxy" or surrogate thread. aoqi@0: // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific aoqi@0: // ParkEvent instead. Beware, however, that the JVMTI code aoqi@0: // knows about ObjectWaiters, so we'll have to reconcile that code. aoqi@0: // See next_waiter(), first_waiter(), etc. aoqi@0: aoqi@0: class ObjectWaiter : public StackObj { aoqi@0: public: aoqi@0: enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ; aoqi@0: enum Sorted { PREPEND, APPEND, SORTED } ; aoqi@0: ObjectWaiter * volatile _next; aoqi@0: ObjectWaiter * volatile _prev; aoqi@0: Thread* _thread; aoqi@0: jlong _notifier_tid; aoqi@0: ParkEvent * _event; aoqi@0: volatile int _notified ; aoqi@0: volatile TStates TState ; aoqi@0: Sorted _Sorted ; // List placement disposition aoqi@0: bool _active ; // Contention monitoring is enabled aoqi@0: public: aoqi@0: ObjectWaiter(Thread* thread); aoqi@0: aoqi@0: void wait_reenter_begin(ObjectMonitor *mon); aoqi@0: void wait_reenter_end(ObjectMonitor *mon); aoqi@0: }; aoqi@0: aoqi@0: // forward declaration to avoid include tracing.hpp aoqi@0: class EventJavaMonitorWait; aoqi@0: aoqi@0: // WARNING: aoqi@0: // This is a very sensitive and fragile class. DO NOT make any aoqi@0: // change unless you are fully aware of the underlying semantics. aoqi@0: aoqi@0: // This class can not inherit from any other class, because I have aoqi@0: // to let the displaced header be the very first word. Otherwise I aoqi@0: // have to let markOop include this file, which would export the aoqi@0: // monitor data structure to everywhere. aoqi@0: // aoqi@0: // The ObjectMonitor class is used to implement JavaMonitors which have aoqi@0: // transformed from the lightweight structure of the thread stack to a aoqi@0: // heavy weight lock due to contention aoqi@0: aoqi@0: // It is also used as RawMonitor by the JVMTI aoqi@0: aoqi@0: aoqi@0: class ObjectMonitor { aoqi@0: public: aoqi@0: enum { aoqi@0: OM_OK, // no error aoqi@0: OM_SYSTEM_ERROR, // operating system error aoqi@0: OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException aoqi@0: OM_INTERRUPTED, // Thread.interrupt() aoqi@0: OM_TIMED_OUT // Object.wait() timed out aoqi@0: }; aoqi@0: aoqi@0: public: aoqi@0: // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ... aoqi@0: // ByteSize would also be an appropriate type. aoqi@0: static int header_offset_in_bytes() { return offset_of(ObjectMonitor, _header); } aoqi@0: static int object_offset_in_bytes() { return offset_of(ObjectMonitor, _object); } aoqi@0: static int owner_offset_in_bytes() { return offset_of(ObjectMonitor, _owner); } aoqi@0: static int count_offset_in_bytes() { return offset_of(ObjectMonitor, _count); } aoqi@0: static int recursions_offset_in_bytes() { return offset_of(ObjectMonitor, _recursions); } aoqi@0: static int cxq_offset_in_bytes() { return offset_of(ObjectMonitor, _cxq) ; } aoqi@0: static int succ_offset_in_bytes() { return offset_of(ObjectMonitor, _succ) ; } aoqi@0: static int EntryList_offset_in_bytes() { return offset_of(ObjectMonitor, _EntryList); } aoqi@0: static int FreeNext_offset_in_bytes() { return offset_of(ObjectMonitor, FreeNext); } aoqi@0: static int WaitSet_offset_in_bytes() { return offset_of(ObjectMonitor, _WaitSet) ; } aoqi@0: static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible);} aoqi@0: static int Spinner_offset_in_bytes() { return offset_of(ObjectMonitor, _Spinner); } aoqi@0: aoqi@0: public: aoqi@0: // Eventaully we'll make provisions for multiple callbacks, but aoqi@0: // now one will suffice. aoqi@0: static int (*SpinCallbackFunction)(intptr_t, int) ; aoqi@0: static intptr_t SpinCallbackArgument ; aoqi@0: aoqi@0: aoqi@0: public: aoqi@0: markOop header() const; aoqi@0: void set_header(markOop hdr); aoqi@0: aoqi@0: intptr_t is_busy() const { aoqi@0: // TODO-FIXME: merge _count and _waiters. aoqi@0: // TODO-FIXME: assert _owner == null implies _recursions = 0 aoqi@0: // TODO-FIXME: assert _WaitSet != null implies _count > 0 aoqi@0: return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList ) ; aoqi@0: } aoqi@0: aoqi@0: intptr_t is_entered(Thread* current) const; aoqi@0: aoqi@0: void* owner() const; aoqi@0: void set_owner(void* owner); aoqi@0: aoqi@0: intptr_t waiters() const; aoqi@0: aoqi@0: intptr_t count() const; aoqi@0: void set_count(intptr_t count); aoqi@0: intptr_t contentions() const ; aoqi@0: intptr_t recursions() const { return _recursions; } aoqi@0: aoqi@0: // JVM/DI GetMonitorInfo() needs this aoqi@0: ObjectWaiter* first_waiter() { return _WaitSet; } aoqi@0: ObjectWaiter* next_waiter(ObjectWaiter* o) { return o->_next; } aoqi@0: Thread* thread_of_waiter(ObjectWaiter* o) { return o->_thread; } aoqi@0: aoqi@0: // initialize the monitor, exception the semaphore, all other fields aoqi@0: // are simple integers or pointers aoqi@0: ObjectMonitor() { aoqi@0: _header = NULL; aoqi@0: _count = 0; aoqi@0: _waiters = 0, aoqi@0: _recursions = 0; aoqi@0: _object = NULL; aoqi@0: _owner = NULL; aoqi@0: _WaitSet = NULL; aoqi@0: _WaitSetLock = 0 ; aoqi@0: _Responsible = NULL ; aoqi@0: _succ = NULL ; aoqi@0: _cxq = NULL ; aoqi@0: FreeNext = NULL ; aoqi@0: _EntryList = NULL ; aoqi@0: _SpinFreq = 0 ; aoqi@0: _SpinClock = 0 ; aoqi@0: OwnerIsThread = 0 ; aoqi@0: _previous_owner_tid = 0; aoqi@0: } aoqi@0: aoqi@0: ~ObjectMonitor() { aoqi@0: // TODO: Add asserts ... aoqi@0: // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 aoqi@0: // _count == 0 _EntryList == NULL etc aoqi@0: } aoqi@0: aoqi@0: private: aoqi@0: void Recycle () { aoqi@0: // TODO: add stronger asserts ... aoqi@0: // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0 aoqi@0: // _count == 0 EntryList == NULL aoqi@0: // _recursions == 0 _WaitSet == NULL aoqi@0: // TODO: assert (is_busy()|_recursions) == 0 aoqi@0: _succ = NULL ; aoqi@0: _EntryList = NULL ; aoqi@0: _cxq = NULL ; aoqi@0: _WaitSet = NULL ; aoqi@0: _recursions = 0 ; aoqi@0: _SpinFreq = 0 ; aoqi@0: _SpinClock = 0 ; aoqi@0: OwnerIsThread = 0 ; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: void* object() const; aoqi@0: void* object_addr(); aoqi@0: void set_object(void* obj); aoqi@0: aoqi@0: bool check(TRAPS); // true if the thread owns the monitor. aoqi@0: void check_slow(TRAPS); aoqi@0: void clear(); aoqi@0: #ifndef PRODUCT aoqi@0: void verify(); aoqi@0: void print(); aoqi@0: #endif aoqi@0: aoqi@0: bool try_enter (TRAPS) ; aoqi@0: void enter(TRAPS); aoqi@0: void exit(bool not_suspended, TRAPS); aoqi@0: void wait(jlong millis, bool interruptable, TRAPS); aoqi@0: void notify(TRAPS); aoqi@0: void notifyAll(TRAPS); aoqi@0: aoqi@0: // Use the following at your own risk aoqi@0: intptr_t complete_exit(TRAPS); aoqi@0: void reenter(intptr_t recursions, TRAPS); aoqi@0: aoqi@0: private: aoqi@0: void AddWaiter (ObjectWaiter * waiter) ; aoqi@0: static void DeferredInitialize(); aoqi@0: aoqi@0: ObjectWaiter * DequeueWaiter () ; aoqi@0: void DequeueSpecificWaiter (ObjectWaiter * waiter) ; aoqi@0: void EnterI (TRAPS) ; aoqi@0: void ReenterI (Thread * Self, ObjectWaiter * SelfNode) ; aoqi@0: void UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ; aoqi@0: int TryLock (Thread * Self) ; aoqi@0: int NotRunnable (Thread * Self, Thread * Owner) ; aoqi@0: int TrySpin_Fixed (Thread * Self) ; aoqi@0: int TrySpin_VaryFrequency (Thread * Self) ; aoqi@0: int TrySpin_VaryDuration (Thread * Self) ; aoqi@0: void ctAsserts () ; aoqi@0: void ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ; aoqi@0: bool ExitSuspendEquivalent (JavaThread * Self) ; aoqi@0: void post_monitor_wait_event(EventJavaMonitorWait * event, aoqi@0: jlong notifier_tid, aoqi@0: jlong timeout, aoqi@0: bool timedout); aoqi@0: aoqi@0: private: aoqi@0: friend class ObjectSynchronizer; aoqi@0: friend class ObjectWaiter; aoqi@0: friend class VMStructs; aoqi@0: aoqi@0: // WARNING: this must be the very first word of ObjectMonitor aoqi@0: // This means this class can't use any virtual member functions. aoqi@0: // TODO-FIXME: assert that offsetof(_header) is 0 or get rid of the aoqi@0: // implicit 0 offset in emitted code. aoqi@0: aoqi@0: volatile markOop _header; // displaced object header word - mark aoqi@0: void* volatile _object; // backward object pointer - strong root aoqi@0: aoqi@0: double SharingPad [1] ; // temp to reduce false sharing aoqi@0: aoqi@0: // All the following fields must be machine word aligned aoqi@0: // The VM assumes write ordering wrt these fields, which can be aoqi@0: // read from other threads. aoqi@0: aoqi@0: protected: // protected for jvmtiRawMonitor aoqi@0: void * volatile _owner; // pointer to owning thread OR BasicLock aoqi@0: volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor aoqi@0: volatile intptr_t _recursions; // recursion count, 0 for first entry aoqi@0: private: aoqi@0: int OwnerIsThread ; // _owner is (Thread *) vs SP/BasicLock aoqi@0: ObjectWaiter * volatile _cxq ; // LL of recently-arrived threads blocked on entry. aoqi@0: // The list is actually composed of WaitNodes, acting aoqi@0: // as proxies for Threads. aoqi@0: protected: aoqi@0: ObjectWaiter * volatile _EntryList ; // Threads blocked on entry or reentry. aoqi@0: private: aoqi@0: Thread * volatile _succ ; // Heir presumptive thread - used for futile wakeup throttling aoqi@0: Thread * volatile _Responsible ; aoqi@0: int _PromptDrain ; // rqst to drain cxq into EntryList ASAP aoqi@0: aoqi@0: volatile int _Spinner ; // for exit->spinner handoff optimization aoqi@0: volatile int _SpinFreq ; // Spin 1-out-of-N attempts: success rate aoqi@0: volatile int _SpinClock ; aoqi@0: volatile int _SpinDuration ; aoqi@0: volatile intptr_t _SpinState ; // MCS/CLH list of spinners aoqi@0: aoqi@0: // TODO-FIXME: _count, _waiters and _recursions should be of aoqi@0: // type int, or int32_t but not intptr_t. There's no reason aoqi@0: // to use 64-bit fields for these variables on a 64-bit JVM. aoqi@0: aoqi@0: volatile intptr_t _count; // reference count to prevent reclaimation/deflation aoqi@0: // at stop-the-world time. See deflate_idle_monitors(). aoqi@0: // _count is approximately |_WaitSet| + |_EntryList| aoqi@0: protected: aoqi@0: volatile intptr_t _waiters; // number of waiting threads aoqi@0: private: aoqi@0: protected: aoqi@0: ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor aoqi@0: private: aoqi@0: volatile int _WaitSetLock; // protects Wait Queue - simple spinlock aoqi@0: aoqi@0: public: aoqi@0: int _QMix ; // Mixed prepend queue discipline aoqi@0: ObjectMonitor * FreeNext ; // Free list linkage aoqi@0: intptr_t StatA, StatsB ; aoqi@0: aoqi@0: public: aoqi@0: static void Initialize () ; aoqi@0: static PerfCounter * _sync_ContendedLockAttempts ; aoqi@0: static PerfCounter * _sync_FutileWakeups ; aoqi@0: static PerfCounter * _sync_Parks ; aoqi@0: static PerfCounter * _sync_EmptyNotifications ; aoqi@0: static PerfCounter * _sync_Notifications ; aoqi@0: static PerfCounter * _sync_SlowEnter ; aoqi@0: static PerfCounter * _sync_SlowExit ; aoqi@0: static PerfCounter * _sync_SlowNotify ; aoqi@0: static PerfCounter * _sync_SlowNotifyAll ; aoqi@0: static PerfCounter * _sync_FailedSpins ; aoqi@0: static PerfCounter * _sync_SuccessfulSpins ; aoqi@0: static PerfCounter * _sync_PrivateA ; aoqi@0: static PerfCounter * _sync_PrivateB ; aoqi@0: static PerfCounter * _sync_MonInCirculation ; aoqi@0: static PerfCounter * _sync_MonScavenged ; aoqi@0: static PerfCounter * _sync_Inflations ; aoqi@0: static PerfCounter * _sync_Deflations ; aoqi@0: static PerfLongVariable * _sync_MonExtant ; aoqi@0: aoqi@0: public: aoqi@0: static int Knob_Verbose; aoqi@0: static int Knob_SpinLimit; aoqi@0: void* operator new (size_t size) throw() { aoqi@0: return AllocateHeap(size, mtInternal); aoqi@0: } aoqi@0: void* operator new[] (size_t size) throw() { aoqi@0: return operator new (size); aoqi@0: } aoqi@0: void operator delete(void* p) { aoqi@0: FreeHeap(p, mtInternal); aoqi@0: } aoqi@0: void operator delete[] (void *p) { aoqi@0: operator delete(p); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #undef TEVENT aoqi@0: #define TEVENT(nom) {if (SyncVerbose) FEVENT(nom); } aoqi@0: aoqi@0: #define FEVENT(nom) { static volatile int ctr = 0 ; int v = ++ctr ; if ((v & (v-1)) == 0) { ::printf (#nom " : %d \n", v); ::fflush(stdout); }} aoqi@0: aoqi@0: #undef TEVENT aoqi@0: #define TEVENT(nom) {;} aoqi@0: aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_OBJECTMONITOR_HPP