hotspot/agent/src/share/classes/sun/jvm/hotspot/runtime/Threads.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
import sun.jvm.hotspot.runtime.solaris_sparc.SolarisSPARCJavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.runtime.solaris_x86.SolarisX86JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.runtime.solaris_amd64.SolarisAMD64JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.runtime.win32_amd64.Win32AMD64JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
import sun.jvm.hotspot.runtime.win32_x86.Win32X86JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
import sun.jvm.hotspot.runtime.win32_ia64.Win32IA64JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
import sun.jvm.hotspot.runtime.linux_x86.LinuxX86JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
import sun.jvm.hotspot.runtime.linux_ia64.LinuxIA64JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
import sun.jvm.hotspot.runtime.linux_amd64.LinuxAMD64JavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
import sun.jvm.hotspot.runtime.linux_sparc.LinuxSPARCJavaThreadPDAccess;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
public class Threads {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    private static JavaThreadFactory threadFactory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    private static AddressField      threadListField;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    private static VirtualConstructor virtualConstructor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    private static JavaThreadPDAccess access;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
        VM.registerVMInitializedObserver(new Observer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
            public void update(Observable o, Object data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
                initialize(VM.getVM().getTypeDataBase());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
        });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    private static synchronized void initialize(TypeDataBase db) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        Type type = db.lookupType("Threads");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
        threadListField = type.getAddressField("_thread_list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
        // Instantiate appropriate platform-specific JavaThreadFactory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        String os  = VM.getVM().getOS();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        String cpu = VM.getVM().getCPU();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
        access = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        // FIXME: find the platform specific PD class by reflection?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
        if (os.equals("solaris")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
            if (cpu.equals("sparc")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
                access = new SolarisSPARCJavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
            } else if (cpu.equals("x86")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                access = new SolarisX86JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
            } else if (cpu.equals("amd64")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
                access = new SolarisAMD64JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        } else if (os.equals("win32")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
            if (cpu.equals("x86")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                access =  new Win32X86JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
            } else if (cpu.equals("amd64")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
                access =  new Win32AMD64JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
            } else if (cpu.equals("ia64")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                access =  new Win32IA64JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
        } else if (os.equals("linux")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
            if (cpu.equals("x86")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                access = new LinuxX86JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
            } else if (cpu.equals("ia64")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
                access = new LinuxIA64JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
            } else if (cpu.equals("amd64")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
                access = new LinuxAMD64JavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
            } else if (cpu.equals("sparc")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
                access = new LinuxSPARCJavaThreadPDAccess();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        if (access == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
            throw new RuntimeException("OS/CPU combination " + os + "/" + cpu +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
            " not yet supported");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
        virtualConstructor = new VirtualConstructor(db);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
        // Add mappings for all known thread types
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
        virtualConstructor.addMapping("JavaThread", JavaThread.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
        if (!VM.getVM().isCore()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
            virtualConstructor.addMapping("CompilerThread", CompilerThread.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
        // for now, use JavaThread itself. fix it later with appropriate class if needed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
        virtualConstructor.addMapping("SurrogateLockerThread", JavaThread.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
        virtualConstructor.addMapping("JvmtiAgentThread", JvmtiAgentThread.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        virtualConstructor.addMapping("LowMemoryDetectorThread", LowMemoryDetectorThread.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    public Threads() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    /** NOTE: this returns objects of type JavaThread, CompilerThread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      JvmtiAgentThread, and LowMemoryDetectorThread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
      The latter four are subclasses of the former. Most operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      (fetching the top frame, etc.) are only allowed to be performed on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      a "pure" JavaThread. For this reason, {@link
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      sun.jvm.hotspot.runtime.JavaThread#isJavaThread} has been
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      changed from the definition in the VM (which returns true for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      all of these thread types) to return true for JavaThreads and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      false for the three subclasses. FIXME: should reconsider the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      inheritance hierarchy; see {@link
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      sun.jvm.hotspot.runtime.JavaThread#isJavaThread}. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    public JavaThread first() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
        Address threadAddr = threadListField.getValue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
        if (threadAddr == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
            return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
        return createJavaThreadWrapper(threadAddr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    /** Routine for instantiating appropriately-typed wrapper for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      JavaThread. Currently needs to be public for OopUtilities to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      access it. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    public JavaThread createJavaThreadWrapper(Address threadAddr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
            JavaThread thread = (JavaThread)virtualConstructor.instantiateWrapperFor(threadAddr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
            thread.setThreadPDAccess(access);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
            return thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        } catch (Exception e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
            throw new RuntimeException("Unable to deduce type of thread from address " + threadAddr +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
            " (expected type JavaThread, CompilerThread, LowMemoryDetectorThread, JvmtiAgentThread, or SurrogateLockerThread)", e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    /** Memory operations */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    public void oopsDo(AddressVisitor oopVisitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        // FIXME: add more of VM functionality
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        for (JavaThread thread = first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
            thread.oopsDo(oopVisitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    // refer to Threads::owning_thread_from_monitor_owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    public JavaThread owningThreadFromMonitor(Address o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
        if (o == null) return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        for (JavaThread thread = first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            if (o.equals(thread.threadObjectAddress())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
                return thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
        for (JavaThread thread = first(); thread != null; thread = thread.next()) {
2526
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 1
diff changeset
   168
          if (thread.isLockOwned(o))
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 1
diff changeset
   169
            return thread;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        }
2526
39a58a50be35 6699669: Hotspot server leaves synchronized block with monitor in bad state
xlu
parents: 1
diff changeset
   171
        return null;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    public JavaThread owningThreadFromMonitor(ObjectMonitor monitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
        return owningThreadFromMonitor(monitor.owner());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    // refer to Threads::get_pending_threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    // Get list of Java threads that are waiting to enter the specified monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    public List getPendingThreads(ObjectMonitor monitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
        List pendingThreads = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
        for (JavaThread thread = first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
            if (thread.isCompilerThread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
                continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
            ObjectMonitor pending = thread.getCurrentPendingMonitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
            if (monitor.equals(pending)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
                pendingThreads.add(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
        return pendingThreads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    // Get list of Java threads that have called Object.wait on the specified monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    public List getWaitingThreads(ObjectMonitor monitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
        List pendingThreads = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
        for (JavaThread thread = first(); thread != null; thread = thread.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
            ObjectMonitor waiting = thread.getCurrentWaitingMonitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
            if (monitor.equals(waiting)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
                pendingThreads.add(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        return pendingThreads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    // FIXME: add other accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
}