src/share/vm/runtime/objectMonitor.hpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
   1 /*
   2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
  26 #define SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
  27 
  28 #include "runtime/os.hpp"
  29 #include "runtime/park.hpp"
  30 #include "runtime/perfData.hpp"
  31 
  32 
  33 // ObjectWaiter serves as a "proxy" or surrogate thread.
  34 // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific
  35 // ParkEvent instead.  Beware, however, that the JVMTI code
  36 // knows about ObjectWaiters, so we'll have to reconcile that code.
  37 // See next_waiter(), first_waiter(), etc.
  38 
  39 class ObjectWaiter : public StackObj {
  40  public:
  41   enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ;
  42   enum Sorted  { PREPEND, APPEND, SORTED } ;
  43   ObjectWaiter * volatile _next;
  44   ObjectWaiter * volatile _prev;
  45   Thread*       _thread;

  46   ParkEvent *   _event;
  47   volatile int  _notified ;
  48   volatile TStates TState ;
  49   Sorted        _Sorted ;           // List placement disposition
  50   bool          _active ;           // Contention monitoring is enabled
  51  public:
  52   ObjectWaiter(Thread* thread);
  53 
  54   void wait_reenter_begin(ObjectMonitor *mon);
  55   void wait_reenter_end(ObjectMonitor *mon);
  56 };
  57 



  58 // WARNING:
  59 //   This is a very sensitive and fragile class. DO NOT make any
  60 // change unless you are fully aware of the underlying semantics.
  61 
  62 //   This class can not inherit from any other class, because I have
  63 // to let the displaced header be the very first word. Otherwise I
  64 // have to let markOop include this file, which would export the
  65 // monitor data structure to everywhere.
  66 //
  67 // The ObjectMonitor class is used to implement JavaMonitors which have
  68 // transformed from the lightweight structure of the thread stack to a
  69 // heavy weight lock due to contention
  70 
  71 // It is also used as RawMonitor by the JVMTI
  72 
  73 
  74 class ObjectMonitor {
  75  public:
  76   enum {
  77     OM_OK,                    // no error


 134 
 135   // initialize the monitor, exception the semaphore, all other fields
 136   // are simple integers or pointers
 137   ObjectMonitor() {
 138     _header       = NULL;
 139     _count        = 0;
 140     _waiters      = 0,
 141     _recursions   = 0;
 142     _object       = NULL;
 143     _owner        = NULL;
 144     _WaitSet      = NULL;
 145     _WaitSetLock  = 0 ;
 146     _Responsible  = NULL ;
 147     _succ         = NULL ;
 148     _cxq          = NULL ;
 149     FreeNext      = NULL ;
 150     _EntryList    = NULL ;
 151     _SpinFreq     = 0 ;
 152     _SpinClock    = 0 ;
 153     OwnerIsThread = 0 ;

 154   }
 155 
 156   ~ObjectMonitor() {
 157    // TODO: Add asserts ...
 158    // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 159    // _count == 0 _EntryList  == NULL etc
 160   }
 161 
 162 private:
 163   void Recycle () {
 164     // TODO: add stronger asserts ...
 165     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 166     // _count == 0 EntryList  == NULL
 167     // _recursions == 0 _WaitSet == NULL
 168     // TODO: assert (is_busy()|_recursions) == 0
 169     _succ          = NULL ;
 170     _EntryList     = NULL ;
 171     _cxq           = NULL ;
 172     _WaitSet       = NULL ;
 173     _recursions    = 0 ;


 175     _SpinClock     = 0 ;
 176     OwnerIsThread  = 0 ;
 177   }
 178 
 179 public:
 180 
 181   void*     object() const;
 182   void*     object_addr();
 183   void      set_object(void* obj);
 184 
 185   bool      check(TRAPS);       // true if the thread owns the monitor.
 186   void      check_slow(TRAPS);
 187   void      clear();
 188 #ifndef PRODUCT
 189   void      verify();
 190   void      print();
 191 #endif
 192 
 193   bool      try_enter (TRAPS) ;
 194   void      enter(TRAPS);
 195   void      exit(TRAPS);
 196   void      wait(jlong millis, bool interruptable, TRAPS);
 197   void      notify(TRAPS);
 198   void      notifyAll(TRAPS);
 199 
 200 // Use the following at your own risk
 201   intptr_t  complete_exit(TRAPS);
 202   void      reenter(intptr_t recursions, TRAPS);
 203 
 204  private:
 205   void      AddWaiter (ObjectWaiter * waiter) ;
 206   static    void DeferredInitialize();
 207 
 208   ObjectWaiter * DequeueWaiter () ;
 209   void      DequeueSpecificWaiter (ObjectWaiter * waiter) ;
 210   void      EnterI (TRAPS) ;
 211   void      ReenterI (Thread * Self, ObjectWaiter * SelfNode) ;
 212   void      UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ;
 213   int       TryLock (Thread * Self) ;
 214   int       NotRunnable (Thread * Self, Thread * Owner) ;
 215   int       TrySpin_Fixed (Thread * Self) ;
 216   int       TrySpin_VaryFrequency (Thread * Self) ;
 217   int       TrySpin_VaryDuration  (Thread * Self) ;
 218   void      ctAsserts () ;
 219   void      ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ;
 220   bool      ExitSuspendEquivalent (JavaThread * Self) ;




 221 
 222  private:
 223   friend class ObjectSynchronizer;
 224   friend class ObjectWaiter;
 225   friend class VMStructs;
 226 
 227   // WARNING: this must be the very first word of ObjectMonitor
 228   // This means this class can't use any virtual member functions.
 229   // TODO-FIXME: assert that offsetof(_header) is 0 or get rid of the
 230   // implicit 0 offset in emitted code.
 231 
 232   volatile markOop   _header;       // displaced object header word - mark
 233   void*     volatile _object;       // backward object pointer - strong root
 234 
 235   double SharingPad [1] ;           // temp to reduce false sharing
 236 
 237   // All the following fields must be machine word aligned
 238   // The VM assumes write ordering wrt these fields, which can be
 239   // read from other threads.
 240 
 241  protected:                         // protected for jvmtiRawMonitor
 242   void *  volatile _owner;          // pointer to owning thread OR BasicLock

 243   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
 244  private:
 245   int OwnerIsThread ;               // _owner is (Thread *) vs SP/BasicLock
 246   ObjectWaiter * volatile _cxq ;    // LL of recently-arrived threads blocked on entry.
 247                                     // The list is actually composed of WaitNodes, acting
 248                                     // as proxies for Threads.
 249  protected:
 250   ObjectWaiter * volatile _EntryList ;     // Threads blocked on entry or reentry.
 251  private:
 252   Thread * volatile _succ ;          // Heir presumptive thread - used for futile wakeup throttling
 253   Thread * volatile _Responsible ;
 254   int _PromptDrain ;                // rqst to drain cxq into EntryList ASAP
 255 
 256   volatile int _Spinner ;           // for exit->spinner handoff optimization
 257   volatile int _SpinFreq ;          // Spin 1-out-of-N attempts: success rate
 258   volatile int _SpinClock ;
 259   volatile int _SpinDuration ;
 260   volatile intptr_t _SpinState ;    // MCS/CLH list of spinners
 261 
 262   // TODO-FIXME: _count, _waiters and _recursions should be of


   1 /*
   2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
  26 #define SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
  27 
  28 #include "runtime/os.hpp"
  29 #include "runtime/park.hpp"
  30 #include "runtime/perfData.hpp"
  31 

  32 // ObjectWaiter serves as a "proxy" or surrogate thread.
  33 // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific
  34 // ParkEvent instead.  Beware, however, that the JVMTI code
  35 // knows about ObjectWaiters, so we'll have to reconcile that code.
  36 // See next_waiter(), first_waiter(), etc.
  37 
  38 class ObjectWaiter : public StackObj {
  39  public:
  40   enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ;
  41   enum Sorted  { PREPEND, APPEND, SORTED } ;
  42   ObjectWaiter * volatile _next;
  43   ObjectWaiter * volatile _prev;
  44   Thread*       _thread;
  45   jlong         _notifier_tid;
  46   ParkEvent *   _event;
  47   volatile int  _notified ;
  48   volatile TStates TState ;
  49   Sorted        _Sorted ;           // List placement disposition
  50   bool          _active ;           // Contention monitoring is enabled
  51  public:
  52   ObjectWaiter(Thread* thread);
  53 
  54   void wait_reenter_begin(ObjectMonitor *mon);
  55   void wait_reenter_end(ObjectMonitor *mon);
  56 };
  57 
  58 // forward declaration to avoid include tracing.hpp
  59 class EventJavaMonitorWait;
  60 
  61 // WARNING:
  62 //   This is a very sensitive and fragile class. DO NOT make any
  63 // change unless you are fully aware of the underlying semantics.
  64 
  65 //   This class can not inherit from any other class, because I have
  66 // to let the displaced header be the very first word. Otherwise I
  67 // have to let markOop include this file, which would export the
  68 // monitor data structure to everywhere.
  69 //
  70 // The ObjectMonitor class is used to implement JavaMonitors which have
  71 // transformed from the lightweight structure of the thread stack to a
  72 // heavy weight lock due to contention
  73 
  74 // It is also used as RawMonitor by the JVMTI
  75 
  76 
  77 class ObjectMonitor {
  78  public:
  79   enum {
  80     OM_OK,                    // no error


 137 
 138   // initialize the monitor, exception the semaphore, all other fields
 139   // are simple integers or pointers
 140   ObjectMonitor() {
 141     _header       = NULL;
 142     _count        = 0;
 143     _waiters      = 0,
 144     _recursions   = 0;
 145     _object       = NULL;
 146     _owner        = NULL;
 147     _WaitSet      = NULL;
 148     _WaitSetLock  = 0 ;
 149     _Responsible  = NULL ;
 150     _succ         = NULL ;
 151     _cxq          = NULL ;
 152     FreeNext      = NULL ;
 153     _EntryList    = NULL ;
 154     _SpinFreq     = 0 ;
 155     _SpinClock    = 0 ;
 156     OwnerIsThread = 0 ;
 157     _previous_owner_tid = 0;
 158   }
 159 
 160   ~ObjectMonitor() {
 161    // TODO: Add asserts ...
 162    // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 163    // _count == 0 _EntryList  == NULL etc
 164   }
 165 
 166 private:
 167   void Recycle () {
 168     // TODO: add stronger asserts ...
 169     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
 170     // _count == 0 EntryList  == NULL
 171     // _recursions == 0 _WaitSet == NULL
 172     // TODO: assert (is_busy()|_recursions) == 0
 173     _succ          = NULL ;
 174     _EntryList     = NULL ;
 175     _cxq           = NULL ;
 176     _WaitSet       = NULL ;
 177     _recursions    = 0 ;


 179     _SpinClock     = 0 ;
 180     OwnerIsThread  = 0 ;
 181   }
 182 
 183 public:
 184 
 185   void*     object() const;
 186   void*     object_addr();
 187   void      set_object(void* obj);
 188 
 189   bool      check(TRAPS);       // true if the thread owns the monitor.
 190   void      check_slow(TRAPS);
 191   void      clear();
 192 #ifndef PRODUCT
 193   void      verify();
 194   void      print();
 195 #endif
 196 
 197   bool      try_enter (TRAPS) ;
 198   void      enter(TRAPS);
 199   void      exit(bool not_suspended, TRAPS);
 200   void      wait(jlong millis, bool interruptable, TRAPS);
 201   void      notify(TRAPS);
 202   void      notifyAll(TRAPS);
 203 
 204 // Use the following at your own risk
 205   intptr_t  complete_exit(TRAPS);
 206   void      reenter(intptr_t recursions, TRAPS);
 207 
 208  private:
 209   void      AddWaiter (ObjectWaiter * waiter) ;
 210   static    void DeferredInitialize();
 211 
 212   ObjectWaiter * DequeueWaiter () ;
 213   void      DequeueSpecificWaiter (ObjectWaiter * waiter) ;
 214   void      EnterI (TRAPS) ;
 215   void      ReenterI (Thread * Self, ObjectWaiter * SelfNode) ;
 216   void      UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ;
 217   int       TryLock (Thread * Self) ;
 218   int       NotRunnable (Thread * Self, Thread * Owner) ;
 219   int       TrySpin_Fixed (Thread * Self) ;
 220   int       TrySpin_VaryFrequency (Thread * Self) ;
 221   int       TrySpin_VaryDuration  (Thread * Self) ;
 222   void      ctAsserts () ;
 223   void      ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ;
 224   bool      ExitSuspendEquivalent (JavaThread * Self) ;
 225   void      post_monitor_wait_event(EventJavaMonitorWait * event,
 226                                                    jlong notifier_tid,
 227                                                    jlong timeout,
 228                                                    bool timedout);
 229 
 230  private:
 231   friend class ObjectSynchronizer;
 232   friend class ObjectWaiter;
 233   friend class VMStructs;
 234 
 235   // WARNING: this must be the very first word of ObjectMonitor
 236   // This means this class can't use any virtual member functions.
 237   // TODO-FIXME: assert that offsetof(_header) is 0 or get rid of the
 238   // implicit 0 offset in emitted code.
 239 
 240   volatile markOop   _header;       // displaced object header word - mark
 241   void*     volatile _object;       // backward object pointer - strong root
 242 
 243   double SharingPad [1] ;           // temp to reduce false sharing
 244 
 245   // All the following fields must be machine word aligned
 246   // The VM assumes write ordering wrt these fields, which can be
 247   // read from other threads.
 248 
 249  protected:                         // protected for jvmtiRawMonitor
 250   void *  volatile _owner;          // pointer to owning thread OR BasicLock
 251   volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor
 252   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
 253  private:
 254   int OwnerIsThread ;               // _owner is (Thread *) vs SP/BasicLock
 255   ObjectWaiter * volatile _cxq ;    // LL of recently-arrived threads blocked on entry.
 256                                     // The list is actually composed of WaitNodes, acting
 257                                     // as proxies for Threads.
 258  protected:
 259   ObjectWaiter * volatile _EntryList ;     // Threads blocked on entry or reentry.
 260  private:
 261   Thread * volatile _succ ;          // Heir presumptive thread - used for futile wakeup throttling
 262   Thread * volatile _Responsible ;
 263   int _PromptDrain ;                // rqst to drain cxq into EntryList ASAP
 264 
 265   volatile int _Spinner ;           // for exit->spinner handoff optimization
 266   volatile int _SpinFreq ;          // Spin 1-out-of-N attempts: success rate
 267   volatile int _SpinClock ;
 268   volatile int _SpinDuration ;
 269   volatile intptr_t _SpinState ;    // MCS/CLH list of spinners
 270 
 271   // TODO-FIXME: _count, _waiters and _recursions should be of