hotspot/agent/src/share/classes/sun/jvm/hotspot/runtime/Thread.java
author xlu
Mon, 06 Apr 2009 15:47:39 -0700
changeset 2526 39a58a50be35
parent 1 489c9b5090e2
child 3261 c7d5aae8d3f7
permissions -rw-r--r--
6699669: Hotspot server leaves synchronized block with monitor in bad state Summary: Remove usage of _highest_lock field in Thread so that is_lock_owned won't depend on the correct update of that field. Reviewed-by: never, dice, acorn
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 2000-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
package sun.jvm.hotspot.runtime;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.types.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
public class Thread extends VMObject {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  private static long tlabFieldOffset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  private static CIntegerField suspendFlagsField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  // Thread::SuspendFlags enum constants
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  private static int EXTERNAL_SUSPEND;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  private static int EXT_SUSPENDED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  private static int HAS_ASYNC_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  private static AddressField activeHandlesField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  private static AddressField currentPendingMonitorField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  private static AddressField currentWaitingMonitorField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    VM.registerVMInitializedObserver(new Observer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
        public void update(Observable o, Object data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
          initialize(VM.getVM().getTypeDataBase());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
      });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  private static synchronized void initialize(TypeDataBase db) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    Type type = db.lookupType("Thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    suspendFlagsField = type.getCIntegerField("_suspend_flags");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    EXTERNAL_SUSPEND = db.lookupIntConstant("Thread::_external_suspend").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    EXT_SUSPENDED = db.lookupIntConstant("Thread::_ext_suspended").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    HAS_ASYNC_EXCEPTION = db.lookupIntConstant("Thread::_has_async_exception").intValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    tlabFieldOffset    = type.getField("_tlab").getOffset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    activeHandlesField = type.getAddressField("_active_handles");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    currentPendingMonitorField = type.getAddressField("_current_pending_monitor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    currentWaitingMonitorField = type.getAddressField("_current_waiting_monitor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  public Thread(Address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    super(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  public int suspendFlags() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    return (int) suspendFlagsField.getValue(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  public boolean isExternalSuspend() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    return (suspendFlags() & EXTERNAL_SUSPEND) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  public boolean isExtSuspended() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    return (suspendFlags() & EXT_SUSPENDED) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  public boolean isBeingExtSuspended() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    return isExtSuspended() || isExternalSuspend();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // historical usage: checked for VM or external suspension
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  public boolean isAnySuspended() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    return isExtSuspended();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  public boolean hasAsyncException() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    return (suspendFlags() & HAS_ASYNC_EXCEPTION) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  public ThreadLocalAllocBuffer tlab() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    return new ThreadLocalAllocBuffer(addr.addOffsetTo(tlabFieldOffset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  public JNIHandleBlock activeHandles() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    Address a = activeHandlesField.getAddress(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    if (a == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return new JNIHandleBlock(a);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  public boolean   isVMThread()                { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  public boolean   isJavaThread()              { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  public boolean   isCompilerThread()          { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  public boolean   isHiddenFromExternalView()  { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  public boolean   isJvmtiAgentThread()        { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  public boolean   isWatcherThread()           { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  public boolean   isConcurrentMarkSweepThread() { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  public boolean   isLowMemoryDetectorThread() { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  /** Memory operations */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  public void oopsDo(AddressVisitor oopVisitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    // FIXME: Empty for now; will later traverse JNI handles and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    // pending exception
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  public ObjectMonitor getCurrentPendingMonitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    Address monitorAddr = currentPendingMonitorField.getValue(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    if (monitorAddr == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    return new ObjectMonitor(monitorAddr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  public ObjectMonitor getCurrentWaitingMonitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    Address monitorAddr = currentWaitingMonitorField.getValue(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    if (monitorAddr == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    return new ObjectMonitor(monitorAddr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  public boolean isLockOwned(Address lock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    if (isInStack(lock)) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  public boolean isInStack(Address a) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    // In the Serviceability Agent we need access to the thread's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    // stack pointer to be able to answer this question. Since it is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    // only a debugging system at the moment we need access to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    // underlying thread, which is only present for Java threads; see
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    // JavaThread.java.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  /** Assistance for ObjectMonitor implementation */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  Address threadObjectAddress() { return addr; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
}