1 11 package org.eclipse.jdt.internal.debug.ui.monitors; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.PlatformObject; 18 import org.eclipse.debug.core.DebugEvent; 19 import org.eclipse.debug.core.DebugException; 20 import org.eclipse.debug.core.DebugPlugin; 21 import org.eclipse.debug.core.ILaunch; 22 import org.eclipse.debug.core.model.IDebugTarget; 23 import org.eclipse.debug.core.model.IThread; 24 import org.eclipse.jdt.debug.core.IJavaObject; 25 import org.eclipse.jdt.debug.core.IJavaThread; 26 27 import com.sun.jdi.IncompatibleThreadStateException; 28 29 32 public class JavaMonitorThread extends PlatformObject { 33 34 37 private IJavaThread fThread; 38 39 private IThread fOriginalThread; 40 41 44 private JavaMonitor fContendedMonitor; 45 48 private JavaMonitor[] fOwnedMonitors= new JavaMonitor[0]; 49 52 private boolean fIsInDeadlock; 53 57 private boolean fToUpdate= true; 58 59 62 private List fElements= new ArrayList (); 63 64 68 private JavaWaitingThread fBaseWaitingThread; 69 73 private JavaOwningThread fBaseOwningThread; 74 75 public JavaMonitorThread(IJavaThread underlyingThread, IThread originalThread) { 76 fThread= underlyingThread; 77 fOriginalThread= originalThread; 78 } 79 80 public IJavaThread getThread() { 81 return fThread; 82 } 83 84 public IThread getOriginalThread() { 85 return fOriginalThread; 86 } 87 88 protected void setOriginalThread(IThread originalThread) { 89 fOriginalThread= originalThread; 90 } 91 92 95 public String getModelIdentifier() { 96 return fThread.getModelIdentifier(); 97 } 98 99 102 public IDebugTarget getDebugTarget() { 103 return fThread.getDebugTarget(); 104 } 105 106 109 public ILaunch getLaunch() { 110 return fThread.getLaunch(); 111 } 112 113 116 public boolean isSuspended() { 117 return fThread.isSuspended(); 118 } 119 120 124 public JavaContendedMonitor getContendedMonitor() { 125 if (fBaseOwningThread == null) { 126 fBaseOwningThread= new JavaOwningThread(this, null); 127 } 128 return fBaseOwningThread.getContendedMonitor(); 129 } 130 131 135 public JavaOwnedMonitor[] getOwnedMonitors() { 136 if (fBaseWaitingThread == null) { 137 fBaseWaitingThread= new JavaWaitingThread(this, null); 138 } 139 return fBaseWaitingThread.getOwnedMonitors(); 140 } 141 142 145 protected JavaMonitor getContendedMonitor0() { 146 if (fToUpdate) { 147 update(); 148 } 149 return fContendedMonitor; 150 } 151 152 155 protected JavaMonitor[] getOwnedMonitors0() { 156 if (fToUpdate) { 157 update(); 158 } 159 return fOwnedMonitors; 160 } 161 162 167 private boolean update() { 168 boolean changed= false; 169 synchronized(this) { 170 if (!fToUpdate) { 171 return false; 172 } 173 try { 174 IJavaObject contendedMonitor= fThread.getContendedMonitor(); 176 if (contendedMonitor == null) { 177 changed= fContendedMonitor != null; 178 fContendedMonitor= null; 179 } else { 180 changed= fContendedMonitor == null || !contendedMonitor.equals(fContendedMonitor.getMonitor()); 181 fContendedMonitor= ThreadMonitorManager.getDefault().getJavaMonitor(contendedMonitor); 182 } 183 IJavaObject[] ownedMonitors= fThread.getOwnedMonitors(); 185 if (ownedMonitors == null || ownedMonitors.length == 0) { 186 changed= fOwnedMonitors != null && fOwnedMonitors.length != 0; 188 fOwnedMonitors= new JavaMonitor[0]; 189 } else { 190 JavaMonitor[] tmp= new JavaMonitor[ownedMonitors.length]; 191 ThreadMonitorManager threadMonitorManager= ThreadMonitorManager.getDefault(); 192 if (changed || fOwnedMonitors.length != ownedMonitors.length) { 193 for (int i= 0; i < ownedMonitors.length; i++) { 195 tmp[i]= threadMonitorManager.getJavaMonitor(ownedMonitors[i]); 196 } 197 changed= true; 198 } else { 199 int sameMonitor= 0; 202 for (int i= 0; i < ownedMonitors.length; i++) { 203 for (int j= 0; j < fOwnedMonitors.length; j++) { 204 if (ownedMonitors[i].equals(fOwnedMonitors[i].getMonitor())) { 205 sameMonitor++; 206 break; 207 } 208 } 209 tmp[i]= threadMonitorManager.getJavaMonitor(ownedMonitors[i]); 210 } 211 changed= sameMonitor != ownedMonitors.length; 212 } 213 fOwnedMonitors= tmp; 214 } 215 } catch (DebugException e) { 216 Throwable cause= e.getStatus().getException(); 217 if (!(cause instanceof IncompatibleThreadStateException)) { 218 fContendedMonitor= null; 222 changed= fOwnedMonitors != null && fOwnedMonitors.length != 0; 223 fOwnedMonitors= new JavaMonitor[0]; 224 } 225 } finally { 226 fToUpdate= false; 227 } 228 } 229 if (changed) { 230 fireChangeEvent(DebugEvent.CONTENT); 231 } 232 return changed; 233 } 234 235 239 private void fireChangeEvent(int detail) { 240 Object [] elements= fElements.toArray(); 241 List changedElement= new ArrayList (); 242 if (fOriginalThread != null) { 243 changedElement.add(fOriginalThread); 244 } 245 for (int i= 0; i < elements.length; i++) { 246 Object element= elements[i]; 247 if (element != fBaseOwningThread && element != fBaseWaitingThread) { 250 changedElement.add(element); 251 } 252 } 253 DebugEvent[] changeEvents= new DebugEvent[changedElement.size()]; 254 int i= 0; 255 for (Iterator iter= changedElement.iterator(); iter.hasNext();) { 256 changeEvents[i++]= new DebugEvent(iter.next(), DebugEvent.CHANGE, detail); 257 } 258 DebugPlugin.getDefault().fireDebugEventSet(changeEvents); 259 } 260 261 public synchronized void setToUpdate() { 262 if (!fToUpdate) { 263 fToUpdate= true; 264 if (fContendedMonitor != null) { 265 fContendedMonitor.setToUpdate(); 266 } 267 if (fOwnedMonitors != null) { 268 for (int i= 0; i < fOwnedMonitors.length; i++) { 269 fOwnedMonitors[i].setToUpdate(); 270 } 271 } 272 } 273 } 274 275 protected void addElement(JavaOwningThread thread) { 276 fElements.add(thread); 277 } 278 279 protected void addElement(JavaWaitingThread thread) { 280 fElements.add(thread); 281 } 282 283 public void refresh() { 284 if (fToUpdate && !update()) { 285 if (fContendedMonitor != null) { 286 fContendedMonitor.refresh(); 287 } 288 for (int i= 0; i < fOwnedMonitors.length; i++) { 289 fOwnedMonitors[i].refresh(); 290 } 291 } 292 } 293 294 297 public boolean isInDeadlock() { 298 return fIsInDeadlock; 299 } 300 303 public void setInDeadlock(boolean isInDeadlock) { 304 boolean oldValue= fIsInDeadlock; 305 fIsInDeadlock = isInDeadlock; 306 if (oldValue != isInDeadlock) { 307 fireChangeEvent(DebugEvent.STATE); 308 } 309 } 310 } 311 | Popular Tags |