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