Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3adcc83c04526faa0bab57d38a84e5ce6c4595f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*******************************************************************************
 * Copyright (c) 2003, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM - Initial API and implementation
 *     Jeremiah Lott (jeremiah.lott@timesys.com) - fix for deadlock bug 76378
 *
 *******************************************************************************/
package org.eclipse.ui.internal;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.LockListener;
import org.eclipse.swt.widgets.Display;

/**
 * The UI lock listener is used to prevent the UI thread from deadlocking on a
 * lock when the thread owning the lock is attempting to syncExec.
 */
public class UILockListener extends LockListener {

	/**
	 * The Queue is the construct that keeps track of Semaphores.
	 */
	public static class Queue {
		private static final int BASE_SIZE = 8;

		protected PendingSyncExec[] elements = new PendingSyncExec[BASE_SIZE];

		protected int head = 0;

		protected int tail = 0;

		/**
		 * Add the semaphore to the queue.
		 * 
		 * @param element
		 */
		public synchronized void add(PendingSyncExec element) {
			int newTail = increment(tail);
			if (newTail == head) {
				grow();
				newTail = tail + 1;
			}
			elements[tail] = element;
			tail = newTail;
		}

		private void grow() {
			int newSize = elements.length * 2;
			PendingSyncExec[] newElements = new PendingSyncExec[newSize];
			if (tail >= head) {
				System.arraycopy(elements, head, newElements, head, size());
			} else {
				int newHead = newSize - (elements.length - head);
				System.arraycopy(elements, 0, newElements, 0, tail + 1);
				System.arraycopy(elements, head, newElements, newHead, (newSize - newHead));
				head = newHead;
			}
			elements = newElements;
		}

		private int increment(int index) {
			return (index == (elements.length - 1)) ? 0 : index + 1;
		}

		/**
		 * Remove the next semaphore to be woken up.
		 * 
		 * @return
		 */
		public synchronized PendingSyncExec remove() {
			if (tail == head) {
				return null;
			}
			PendingSyncExec result = elements[head];
			elements[head] = null;
			head = increment(head);
			// reset the queue if it is empty and it has grown
			if (tail == head && elements.length > BASE_SIZE) {
				elements = new PendingSyncExec[BASE_SIZE];
				tail = head = 0;
			}
			return result;
		}

		private int size() {
			return tail > head ? (tail - head) : ((elements.length - head) + tail);
		}
	}

	protected Display display;

	protected final Queue pendingWork = new Queue();

	protected PendingSyncExec currentWork = null;

	/**
	 * Points to the UI thread if it is currently waiting on a lock or null
	 */
	protected volatile Thread ui;

	/**
	 * Create a new instance of the receiver.
	 * 
	 * @param display
	 */
	public UILockListener(Display display) {
		this.display = display;
	}

	@Override
	public void aboutToRelease() {
		if (isUI()) {
			ui = null;
		}
	}

	@Override
	public boolean aboutToWait(Thread lockOwner) {
		if (isUI()) {
			// If a syncExec was executed from the current operation, it
			// has already acquired the lock. So, just return true.
			if (currentWork != null && currentWork.getOperationThread() == lockOwner) {
				return true;
			}
			ui = Thread.currentThread();
			try {
				doPendingWork();
			} finally {
				// UI field may be nulled if there is a nested wait during execution
				// of pending work, so make sure it is assigned before we start waiting
				ui = Thread.currentThread();
			}
		}
		return false;
	}

	void addPendingWork(PendingSyncExec work) {
		pendingWork.add(work);
	}

	@Override
	public boolean canBlock() {
		return !isUI();
	}

	/**
	 * Should always be called from the UI thread.
	 */
	void doPendingWork() {
		// Clear the interrupt flag that we may have set in interruptUI()
		Thread.interrupted();
		PendingSyncExec work;
		while ((work = pendingWork.remove()) != null) {
			// Remember the old current work before replacing, to handle
			// the nested waiting case (bug 76378)
			PendingSyncExec oldWork = currentWork;
			try {
				currentWork = work;
				work.run();
			} finally {
				currentWork = oldWork;
			}
		}
	}

	void interruptUI(Runnable runnable) {
		reportInterruption(runnable);
		display.getThread().interrupt();
	}

	boolean isLockOwner() {
		return isLockOwnerThread();
	}

	boolean isUI() {
		return (!display.isDisposed()) && (display.getThread() == Thread.currentThread());
	}

	boolean isUIWaiting() {
		Thread localUi = ui;
		return (localUi != null) && (Thread.currentThread() != localUi);
	}

	/**
	 * Adds a 'UI thread interrupted' message to the log with extra lock state and
	 * thread stack information.
	 */
	private void reportInterruption(Runnable runnable) {
		Thread nonUiThread = Thread.currentThread();

		String msg = "To avoid deadlock while executing Display.syncExec() with argument: " //$NON-NLS-1$
				+ runnable + ", thread " + nonUiThread.getName() //$NON-NLS-1$
				+ " will interrupt UI thread."; //$NON-NLS-1$
		MultiStatus main = new MultiStatus(WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, msg, null);

		ThreadInfo[] threads = ManagementFactory.getThreadMXBean()
				.getThreadInfo(new long[] { nonUiThread.getId(), display.getThread().getId() }, true, true);

		for (ThreadInfo info : threads) {
			String childMsg;
			if (info.getThreadId() == nonUiThread.getId()) {
				// see org.eclipse.core.internal.jobs.LockManager.isLockOwner()
				childMsg = nonUiThread.getName() + " thread is an instance of Worker or owns an ILock"; //$NON-NLS-1$
			} else {
				childMsg = "UI thread waiting on a job or lock."; //$NON-NLS-1$
			}
			Exception childEx = new IllegalStateException("Call stack for thread " + info.getThreadName()); //$NON-NLS-1$
			childEx.setStackTrace(info.getStackTrace());
			Status child = new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, childMsg, childEx);
			main.add(child);
		}

		WorkbenchPlugin.log(main);
	}
}

Back to the top