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