src/share/vm/runtime/objectMonitor.hpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/objectMonitor.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,208 @@
     1.4 +/*
     1.5 + * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// WARNING:
    1.29 +//   This is a very sensitive and fragile class. DO NOT make any
    1.30 +// change unless you are fully aware of the underlying semantics.
    1.31 +
    1.32 +//   This class can not inherit from any other class, because I have
    1.33 +// to let the displaced header be the very first word. Otherwise I
    1.34 +// have to let markOop include this file, which would export the
    1.35 +// monitor data structure to everywhere.
    1.36 +//
    1.37 +// The ObjectMonitor class is used to implement JavaMonitors which have
    1.38 +// transformed from the lightweight structure of the thread stack to a
    1.39 +// heavy weight lock due to contention
    1.40 +
    1.41 +// It is also used as RawMonitor by the JVMTI
    1.42 +
    1.43 +
    1.44 +class ObjectWaiter;
    1.45 +
    1.46 +class ObjectMonitor {
    1.47 + public:
    1.48 +  enum {
    1.49 +    OM_OK,                    // no error
    1.50 +    OM_SYSTEM_ERROR,          // operating system error
    1.51 +    OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException
    1.52 +    OM_INTERRUPTED,           // Thread.interrupt()
    1.53 +    OM_TIMED_OUT              // Object.wait() timed out
    1.54 +  };
    1.55 +
    1.56 + public:
    1.57 +  // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ...
    1.58 +  // ByteSize would also be an appropriate type.
    1.59 +  static int header_offset_in_bytes()      { return offset_of(ObjectMonitor, _header);     }
    1.60 +  static int object_offset_in_bytes()      { return offset_of(ObjectMonitor, _object);     }
    1.61 +  static int owner_offset_in_bytes()       { return offset_of(ObjectMonitor, _owner);      }
    1.62 +  static int count_offset_in_bytes()       { return offset_of(ObjectMonitor, _count);      }
    1.63 +  static int recursions_offset_in_bytes()  { return offset_of(ObjectMonitor, _recursions); }
    1.64 +  static int cxq_offset_in_bytes()         { return offset_of(ObjectMonitor, _cxq) ;       }
    1.65 +  static int succ_offset_in_bytes()        { return offset_of(ObjectMonitor, _succ) ;      }
    1.66 +  static int EntryList_offset_in_bytes()   { return offset_of(ObjectMonitor, _EntryList);  }
    1.67 +  static int FreeNext_offset_in_bytes()    { return offset_of(ObjectMonitor, FreeNext);    }
    1.68 +  static int WaitSet_offset_in_bytes()     { return offset_of(ObjectMonitor, _WaitSet) ;   }
    1.69 +  static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible);}
    1.70 +  static int Spinner_offset_in_bytes()     { return offset_of(ObjectMonitor, _Spinner);    }
    1.71 +
    1.72 + public:
    1.73 +  // Eventaully we'll make provisions for multiple callbacks, but
    1.74 +  // now one will suffice.
    1.75 +  static int (*SpinCallbackFunction)(intptr_t, int) ;
    1.76 +  static intptr_t SpinCallbackArgument ;
    1.77 +
    1.78 +
    1.79 + public:
    1.80 +  ObjectMonitor();
    1.81 +  ~ObjectMonitor();
    1.82 +
    1.83 +  markOop   header() const;
    1.84 +  void      set_header(markOop hdr);
    1.85 +
    1.86 +  intptr_t  is_busy() const;
    1.87 +  intptr_t  is_entered(Thread* current) const;
    1.88 +
    1.89 +  void*     owner() const;
    1.90 +  void      set_owner(void* owner);
    1.91 +
    1.92 +  intptr_t  waiters() const;
    1.93 +
    1.94 +  intptr_t  count() const;
    1.95 +  void      set_count(intptr_t count);
    1.96 +  intptr_t  contentions() const ;
    1.97 +
    1.98 +  // JVM/DI GetMonitorInfo() needs this
    1.99 +  Thread *  thread_of_waiter (ObjectWaiter *) ;
   1.100 +  ObjectWaiter * first_waiter () ;
   1.101 +  ObjectWaiter * next_waiter(ObjectWaiter* o);
   1.102 +
   1.103 +  intptr_t  recursions() const { return _recursions; }
   1.104 +
   1.105 +  void*     object() const;
   1.106 +  void*     object_addr();
   1.107 +  void      set_object(void* obj);
   1.108 +
   1.109 +  bool      check(TRAPS);       // true if the thread owns the monitor.
   1.110 +  void      check_slow(TRAPS);
   1.111 +  void      clear();
   1.112 +#ifndef PRODUCT
   1.113 +  void      verify();
   1.114 +  void      print();
   1.115 +#endif
   1.116 +
   1.117 +  bool      try_enter (TRAPS) ;
   1.118 +  void      enter(TRAPS);
   1.119 +  void      exit(TRAPS);
   1.120 +  void      wait(jlong millis, bool interruptable, TRAPS);
   1.121 +  void      notify(TRAPS);
   1.122 +  void      notifyAll(TRAPS);
   1.123 +
   1.124 +// Use the following at your own risk
   1.125 +  intptr_t  complete_exit(TRAPS);
   1.126 +  void      reenter(intptr_t recursions, TRAPS);
   1.127 +
   1.128 +  int       raw_enter(TRAPS);
   1.129 +  int       raw_exit(TRAPS);
   1.130 +  int       raw_wait(jlong millis, bool interruptable, TRAPS);
   1.131 +  int       raw_notify(TRAPS);
   1.132 +  int       raw_notifyAll(TRAPS);
   1.133 +
   1.134 + private:
   1.135 +  // JVMTI support -- remove ASAP
   1.136 +  int       SimpleEnter (Thread * Self) ;
   1.137 +  int       SimpleExit  (Thread * Self) ;
   1.138 +  int       SimpleWait  (Thread * Self, jlong millis) ;
   1.139 +  int       SimpleNotify (Thread * Self, bool All) ;
   1.140 +
   1.141 + private:
   1.142 +  void      Recycle () ;
   1.143 +  void      AddWaiter (ObjectWaiter * waiter) ;
   1.144 +
   1.145 +  ObjectWaiter * DequeueWaiter () ;
   1.146 +  void      DequeueSpecificWaiter (ObjectWaiter * waiter) ;
   1.147 +  void      EnterI (TRAPS) ;
   1.148 +  void      ReenterI (Thread * Self, ObjectWaiter * SelfNode) ;
   1.149 +  void      UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ;
   1.150 +  int       TryLock (Thread * Self) ;
   1.151 +  int       NotRunnable (Thread * Self, Thread * Owner) ;
   1.152 +  int       TrySpin_Fixed (Thread * Self) ;
   1.153 +  int       TrySpin_VaryFrequency (Thread * Self) ;
   1.154 +  int       TrySpin_VaryDuration  (Thread * Self) ;
   1.155 +  void      ctAsserts () ;
   1.156 +  void      ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ;
   1.157 +  bool      ExitSuspendEquivalent (JavaThread * Self) ;
   1.158 +
   1.159 + private:
   1.160 +  friend class ObjectSynchronizer;
   1.161 +  friend class ObjectWaiter;
   1.162 +  friend class VMStructs;
   1.163 +
   1.164 +  // WARNING: this must be the very first word of ObjectMonitor
   1.165 +  // This means this class can't use any virtual member functions.
   1.166 +  // TODO-FIXME: assert that offsetof(_header) is 0 or get rid of the
   1.167 +  // implicit 0 offset in emitted code.
   1.168 +
   1.169 +  volatile markOop   _header;       // displaced object header word - mark
   1.170 +  void*     volatile _object;       // backward object pointer - strong root
   1.171 +
   1.172 +  double SharingPad [1] ;           // temp to reduce false sharing
   1.173 +
   1.174 +  // All the following fields must be machine word aligned
   1.175 +  // The VM assumes write ordering wrt these fields, which can be
   1.176 +  // read from other threads.
   1.177 +
   1.178 +  void *  volatile _owner;          // pointer to owning thread OR BasicLock
   1.179 +  volatile intptr_t  _recursions;   // recursion count, 0 for first entry
   1.180 +  int OwnerIsThread ;               // _owner is (Thread *) vs SP/BasicLock
   1.181 +  ObjectWaiter * volatile _cxq ;    // LL of recently-arrived threads blocked on entry.
   1.182 +                                    // The list is actually composed of WaitNodes, acting
   1.183 +                                    // as proxies for Threads.
   1.184 +  ObjectWaiter * volatile _EntryList ;     // Threads blocked on entry or reentry.
   1.185 +  Thread * volatile _succ ;          // Heir presumptive thread - used for futile wakeup throttling
   1.186 +  Thread * volatile _Responsible ;
   1.187 +  int _PromptDrain ;                // rqst to drain cxq into EntryList ASAP
   1.188 +
   1.189 +  volatile int _Spinner ;           // for exit->spinner handoff optimization
   1.190 +  volatile int _SpinFreq ;          // Spin 1-out-of-N attempts: success rate
   1.191 +  volatile int _SpinClock ;
   1.192 +  volatile int _SpinDuration ;
   1.193 +  volatile intptr_t _SpinState ;    // MCS/CLH list of spinners
   1.194 +
   1.195 +  // TODO-FIXME: _count, _waiters and _recursions should be of
   1.196 +  // type int, or int32_t but not intptr_t.  There's no reason
   1.197 +  // to use 64-bit fields for these variables on a 64-bit JVM.
   1.198 +
   1.199 +  volatile intptr_t  _count;        // reference count to prevent reclaimation/deflation
   1.200 +                                    // at stop-the-world time.  See deflate_idle_monitors().
   1.201 +                                    // _count is approximately |_WaitSet| + |_EntryList|
   1.202 +  volatile intptr_t  _waiters;      // number of waiting threads
   1.203 +  ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor
   1.204 +  volatile int _WaitSetLock;        // protects Wait Queue - simple spinlock
   1.205 +
   1.206 + public:
   1.207 +  int _QMix ;                       // Mixed prepend queue discipline
   1.208 +  ObjectMonitor * FreeNext ;        // Free list linkage
   1.209 +  intptr_t StatA, StatsB ;
   1.210 +
   1.211 +};

mercurial