|
31 | 31 | import consulo.project.ui.notification.Notification; |
32 | 32 | import consulo.project.ui.notification.NotificationType; |
33 | 33 | import consulo.project.ui.notification.event.NotificationListener; |
34 | | - |
35 | 34 | import org.jspecify.annotations.Nullable; |
| 35 | + |
36 | 36 | import javax.swing.event.HyperlinkEvent; |
37 | 37 | import java.util.Collection; |
38 | 38 | import java.util.HashSet; |
|
42 | 42 | /** |
43 | 43 | * @author egor |
44 | 44 | */ |
45 | | -public class ThreadBlockedMonitor |
46 | | -{ |
47 | | - private static final Logger LOG = Logger.getInstance(ThreadBlockedMonitor.class); |
| 45 | +public class ThreadBlockedMonitor { |
| 46 | + private static final Logger LOG = Logger.getInstance(ThreadBlockedMonitor.class); |
48 | 47 |
|
49 | | - private final Collection<ThreadReferenceProxy> myWatchedThreads = new HashSet<ThreadReferenceProxy>(); |
| 48 | + private final Collection<ThreadReferenceProxy> myWatchedThreads = new HashSet<ThreadReferenceProxy>(); |
50 | 49 |
|
51 | | - private ScheduledFuture<?> myTask; |
52 | | - private final DebugProcessImpl myProcess; |
| 50 | + private ScheduledFuture<?> myTask; |
| 51 | + private final DebugProcessImpl myProcess; |
53 | 52 |
|
54 | | - public ThreadBlockedMonitor(DebugProcessImpl process, Disposable disposable) |
55 | | - { |
56 | | - myProcess = process; |
57 | | - Disposer.register(disposable, this::cancelTask); |
58 | | - } |
| 53 | + public ThreadBlockedMonitor(DebugProcessImpl process, Disposable disposable) { |
| 54 | + myProcess = process; |
| 55 | + Disposer.register(disposable, this::cancelTask); |
| 56 | + } |
59 | 57 |
|
60 | | - public void startWatching(@Nullable ThreadReferenceProxy thread) |
61 | | - { |
62 | | - DebuggerManagerThreadImpl.assertIsManagerThread(); |
63 | | - if(thread != null) |
64 | | - { |
65 | | - myWatchedThreads.add(thread); |
66 | | - if(myTask == null) |
67 | | - { |
68 | | - myTask = AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(this::checkBlockingThread, 5, 5, TimeUnit.SECONDS); |
69 | | - } |
70 | | - } |
71 | | - } |
| 58 | + public void startWatching(@Nullable ThreadReferenceProxy thread) { |
| 59 | + DebuggerManagerThreadImpl.assertIsManagerThread(); |
| 60 | + if (thread != null) { |
| 61 | + myWatchedThreads.add(thread); |
| 62 | + if (myTask == null) { |
| 63 | + myTask = AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(this::checkBlockingThread, 5, 5, TimeUnit.SECONDS); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
72 | 67 |
|
73 | | - public void stopWatching(@Nullable ThreadReferenceProxy thread) |
74 | | - { |
75 | | - DebuggerManagerThreadImpl.assertIsManagerThread(); |
76 | | - if(thread != null) |
77 | | - { |
78 | | - myWatchedThreads.remove(thread); |
79 | | - } |
80 | | - else |
81 | | - { |
82 | | - myWatchedThreads.clear(); |
83 | | - } |
84 | | - if(myWatchedThreads.isEmpty()) |
85 | | - { |
86 | | - cancelTask(); |
87 | | - } |
88 | | - } |
| 68 | + public void stopWatching(@Nullable ThreadReferenceProxy thread) { |
| 69 | + DebuggerManagerThreadImpl.assertIsManagerThread(); |
| 70 | + if (thread != null) { |
| 71 | + myWatchedThreads.remove(thread); |
| 72 | + } |
| 73 | + else { |
| 74 | + myWatchedThreads.clear(); |
| 75 | + } |
| 76 | + if (myWatchedThreads.isEmpty()) { |
| 77 | + cancelTask(); |
| 78 | + } |
| 79 | + } |
89 | 80 |
|
90 | | - private void cancelTask() |
91 | | - { |
92 | | - if(myTask != null) |
93 | | - { |
94 | | - myTask.cancel(true); |
95 | | - myTask = null; |
96 | | - } |
97 | | - } |
| 81 | + private void cancelTask() { |
| 82 | + if (myTask != null) { |
| 83 | + myTask.cancel(true); |
| 84 | + myTask = null; |
| 85 | + } |
| 86 | + } |
98 | 87 |
|
99 | | - private static void onThreadBlocked(final ThreadReference blockedThread, final ThreadReference blockingThread, final DebugProcessImpl process) |
100 | | - { |
101 | | - XDebuggerUIConstants.NOTIFICATION_GROUP.createNotification(DebuggerBundle.message("status.thread.blocked.by", blockedThread.name(), blockingThread.name()), DebuggerBundle.message("status" + "" |
102 | | - + ".thread" + ".blocked.by.resume", blockingThread.name()), NotificationType.INFORMATION, new NotificationListener() |
103 | | - { |
104 | | - @Override |
105 | | - public void hyperlinkUpdate(Notification notification, HyperlinkEvent event) |
106 | | - { |
107 | | - if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) |
108 | | - { |
109 | | - notification.expire(); |
110 | | - process.getManagerThread().schedule(new DebuggerCommandImpl() |
111 | | - { |
112 | | - @Override |
113 | | - protected void action() throws Exception |
114 | | - { |
115 | | - ThreadReferenceProxyImpl threadProxy = process.getVirtualMachineProxy().getThreadReferenceProxy(blockingThread); |
116 | | - SuspendContextImpl suspendingContext = SuspendManagerUtil.getSuspendingContext(process.getSuspendManager(), threadProxy); |
117 | | - process.getManagerThread().invoke(process.createResumeThreadCommand(suspendingContext, threadProxy)); |
118 | | - } |
119 | | - }); |
120 | | - } |
121 | | - } |
122 | | - }).notify(process.getProject()); |
123 | | - } |
| 88 | + private static void onThreadBlocked(final ThreadReference blockedThread, final ThreadReference blockingThread, final DebugProcessImpl process) { |
| 89 | + XDebuggerUIConstants.NOTIFICATION_GROUP.createNotification(DebuggerBundle.message("status.thread.blocked.by", blockedThread.name(), blockingThread.name()), DebuggerBundle.message("status" + "" |
| 90 | + + ".thread" + ".blocked.by.resume", blockingThread.name()), NotificationType.INFORMATION, new NotificationListener() { |
| 91 | + @Override |
| 92 | + public void hyperlinkActivated(Notification notification, consulo.ui.event.HyperlinkEvent event) { |
| 93 | + notification.expire(); |
| 94 | + process.getManagerThread().schedule(new DebuggerCommandImpl() { |
| 95 | + @Override |
| 96 | + protected void action() throws Exception { |
| 97 | + ThreadReferenceProxyImpl threadProxy = process.getVirtualMachineProxy().getThreadReferenceProxy(blockingThread); |
| 98 | + SuspendContextImpl suspendingContext = SuspendManagerUtil.getSuspendingContext(process.getSuspendManager(), threadProxy); |
| 99 | + process.getManagerThread().invoke(process.createResumeThreadCommand(suspendingContext, threadProxy)); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + }).notify(process.getProject()); |
| 104 | + } |
124 | 105 |
|
125 | | - private ThreadReference getCurrentThread() |
126 | | - { |
127 | | - ThreadReferenceProxyImpl threadProxy = myProcess.getDebuggerContext().getThreadProxy(); |
128 | | - return threadProxy != null ? threadProxy.getThreadReference() : null; |
129 | | - } |
| 106 | + private ThreadReference getCurrentThread() { |
| 107 | + ThreadReferenceProxyImpl threadProxy = myProcess.getDebuggerContext().getThreadProxy(); |
| 108 | + return threadProxy != null ? threadProxy.getThreadReference() : null; |
| 109 | + } |
130 | 110 |
|
131 | | - private void checkBlockingThread() |
132 | | - { |
133 | | - myProcess.getManagerThread().schedule(new DebuggerCommandImpl() |
134 | | - { |
135 | | - @Override |
136 | | - protected void action() throws Exception |
137 | | - { |
138 | | - if(myWatchedThreads.isEmpty()) |
139 | | - { |
140 | | - return; |
141 | | - } |
142 | | - VirtualMachineProxyImpl vmProxy = myProcess.getVirtualMachineProxy(); |
143 | | - //TODO: can we do fast check without suspending all |
144 | | - vmProxy.getVirtualMachine().suspend(); |
145 | | - try |
146 | | - { |
147 | | - for(ThreadReferenceProxy thread : myWatchedThreads) |
148 | | - { |
149 | | - ObjectReference waitedMonitor = vmProxy.canGetCurrentContendedMonitor() ? thread.getThreadReference().currentContendedMonitor() : null; |
150 | | - if(waitedMonitor != null && vmProxy.canGetMonitorInfo()) |
151 | | - { |
152 | | - ThreadReference blockingThread = waitedMonitor.owningThread(); |
153 | | - if(blockingThread != null && blockingThread.suspendCount() > 1 && getCurrentThread() != blockingThread) |
154 | | - { |
155 | | - onThreadBlocked(thread.getThreadReference(), blockingThread, myProcess); |
156 | | - } |
157 | | - } |
158 | | - } |
159 | | - } |
160 | | - catch(IncompatibleThreadStateException e) |
161 | | - { |
162 | | - LOG.info(e); |
163 | | - } |
164 | | - finally |
165 | | - { |
166 | | - vmProxy.getVirtualMachine().resume(); |
167 | | - } |
168 | | - } |
169 | | - }); |
170 | | - } |
| 111 | + private void checkBlockingThread() { |
| 112 | + myProcess.getManagerThread().schedule(new DebuggerCommandImpl() { |
| 113 | + @Override |
| 114 | + protected void action() throws Exception { |
| 115 | + if (myWatchedThreads.isEmpty()) { |
| 116 | + return; |
| 117 | + } |
| 118 | + VirtualMachineProxyImpl vmProxy = myProcess.getVirtualMachineProxy(); |
| 119 | + //TODO: can we do fast check without suspending all |
| 120 | + vmProxy.getVirtualMachine().suspend(); |
| 121 | + try { |
| 122 | + for (ThreadReferenceProxy thread : myWatchedThreads) { |
| 123 | + ObjectReference waitedMonitor = vmProxy.canGetCurrentContendedMonitor() ? thread.getThreadReference().currentContendedMonitor() : null; |
| 124 | + if (waitedMonitor != null && vmProxy.canGetMonitorInfo()) { |
| 125 | + ThreadReference blockingThread = waitedMonitor.owningThread(); |
| 126 | + if (blockingThread != null && blockingThread.suspendCount() > 1 && getCurrentThread() != blockingThread) { |
| 127 | + onThreadBlocked(thread.getThreadReference(), blockingThread, myProcess); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + catch (IncompatibleThreadStateException e) { |
| 133 | + LOG.info(e); |
| 134 | + } |
| 135 | + finally { |
| 136 | + vmProxy.getVirtualMachine().resume(); |
| 137 | + } |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
171 | 141 | } |
0 commit comments