src/jdk.jdi/share/classes/com/sun/tools/jdi/VMState.java
changeset 52068 218b5b64f102
parent 47216 71c04702a3d5
equal deleted inserted replaced
52067:2e72562697bf 52068:218b5b64f102
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.jdi;
    26 package com.sun.tools.jdi;
    27 
    27 
    28 import java.lang.ref.WeakReference;
    28 import java.lang.ref.WeakReference;
    29 import java.util.ArrayList;
    29 import java.util.*;
    30 import java.util.Arrays;
       
    31 import java.util.Iterator;
       
    32 import java.util.List;
       
    33 
    30 
    34 import com.sun.jdi.ThreadGroupReference;
    31 import com.sun.jdi.ThreadGroupReference;
    35 import com.sun.jdi.ThreadReference;
    32 import com.sun.jdi.ThreadReference;
    36 import com.sun.jdi.VirtualMachine;
    33 import com.sun.jdi.VirtualMachine;
    37 
    34 
    42     private final List<WeakReference<VMListener>> listeners = new ArrayList<>(); // synchronized (this)
    39     private final List<WeakReference<VMListener>> listeners = new ArrayList<>(); // synchronized (this)
    43     private boolean notifyingListeners = false;  // synchronized (this)
    40     private boolean notifyingListeners = false;  // synchronized (this)
    44 
    41 
    45     /*
    42     /*
    46      * Certain information can be cached only when the entire VM is
    43      * Certain information can be cached only when the entire VM is
    47      * suspended and there are no pending resumes. The fields below
    44      * suspended and there are no pending resumes. The field below
    48      * are used to track whether there are pending resumes. (There
    45      * is used to track whether there are pending resumes.
    49      * is an assumption that JDWP command ids are increasing over time.)
    46      */
    50      */
    47     private final Set<Integer> pendingResumeCommands = Collections.synchronizedSet(new HashSet<>());
    51     private int lastCompletedCommandId = 0;   // synchronized (this)
       
    52     private int lastResumeCommandId = 0;      // synchronized (this)
       
    53 
    48 
    54     // This is cached only while the VM is suspended
    49     // This is cached only while the VM is suspended
    55     private static class Cache {
    50     private static class Cache {
    56         List<ThreadGroupReference> groups = null;  // cached Top Level ThreadGroups
    51         List<ThreadGroupReference> groups = null;  // cached Top Level ThreadGroups
    57         List<ThreadReference> threads = null; // cached Threads
    52         List<ThreadReference> threads = null; // cached Threads
    95 
    90 
    96     /*
    91     /*
    97      * A JDWP command has been completed (reply has been received).
    92      * A JDWP command has been completed (reply has been received).
    98      * Update data that tracks pending resume commands.
    93      * Update data that tracks pending resume commands.
    99      */
    94      */
   100     synchronized void notifyCommandComplete(int id) {
    95     void notifyCommandComplete(int id) {
   101         lastCompletedCommandId = id;
    96         pendingResumeCommands.remove(id);
   102     }
    97     }
   103 
    98 
   104     synchronized void freeze() {
    99     synchronized void freeze() {
   105         if (cache == null && (lastCompletedCommandId >= lastResumeCommandId)) {
   100         if (cache == null && (pendingResumeCommands.isEmpty())) {
   106             /*
   101             /*
   107              * No pending resumes to worry about. The VM is suspended
   102              * No pending resumes to worry about. The VM is suspended
   108              * and additional state can be cached. Notify all
   103              * and additional state can be cached. Notify all
   109              * interested listeners.
   104              * interested listeners.
   110              */
   105              */
   113         }
   108         }
   114     }
   109     }
   115 
   110 
   116     synchronized PacketStream thawCommand(CommandSender sender) {
   111     synchronized PacketStream thawCommand(CommandSender sender) {
   117         PacketStream stream = sender.send();
   112         PacketStream stream = sender.send();
   118         lastResumeCommandId = stream.id();
   113         pendingResumeCommands.add(stream.id());
   119         thaw();
   114         thaw();
   120         return stream;
   115         return stream;
   121     }
   116     }
   122 
   117 
   123     /**
   118     /**