Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa8e18d05407410fb0e2eab25bf1d808c2287b00 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*******************************************************************************
 * Copyright (c) 1997, 2018 by ProSyst Software GmbH
 * http://www.prosyst.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.util.event;

import java.security.AccessController;
import java.security.PrivilegedAction;
import org.eclipse.equinox.internal.util.UtilActivator;

/**
 * Abstract class for asynchronous event dispatching
 *
 * @author Pavlin Dobrev
 * @version 1.0
 */

public abstract class EventThread implements Runnable {

	/**
	 * The last callbacked listener. If the events thread is not responding,
	 * subclasses can take the appropriate actions - remove the listener for
	 * example
	 */
	public Object bad;

	/**
	 * The event queue. This object must be used for synchronization with the
	 * events thread's state
	 */
	protected Queue queue;

	/**
	 * The state of the thread.
	 * <li> bit 0: 0 = started / 1 = stopped;
	 * <li> bit 1: 0 not waiting / 1 = waiting
	 */
	protected int state;

	/**
	 * The time spent in the current callback, or 0 if the thread is not in a
	 * callback
	 */
	protected long time = 0;
	/**
	 * Instancies counter. Subclasses must not modify it.
	 */
	protected int counter = 1;

	/**
	 * The event to be dispatched
	 */
	protected Object element;
	protected String baseName;
	protected String name;
	protected Thread thread;
	protected ThreadGroup group;
	private static PrivilegedActionImpl privilegedAction = null;

	/**
	 * Constructs the first instance of the EventThread
	 * 
	 * @param group
	 *            The ThreadGroup of the thread, or null for the current thread
	 *            group
	 * @param name
	 *            The base name of the thread. The <code> counter  </code> value
	 *            will be added at the end of the string to construct the full
	 *            name.
	 * @param size
	 *            The initial number of elements of the events queue
	 */
	public EventThread(ThreadGroup group, String name, int size) {
		makeThread(this.group = group, this.name = name + '0');
		baseName = name;
		queue = new Queue(size);
		int priority = getThreadPriority();
		if (priority != Thread.NORM_PRIORITY)
			thread.setPriority(priority);
	}

	/**
	 * Constructs the first instance of the EventThread
	 * 
	 * @param group
	 *            The ThreadGroup of the thread, or null for the current thread
	 *            group
	 * @param name
	 *            The base name of the thread. The <code> counter  </code> value
	 *            will be added at the end of the string to construct the full
	 *            name.
	 * @param queue
	 *            The events queue
	 */
	public EventThread(ThreadGroup group, String name, Queue queue) {
		makeThread(this.group = group, this.name = name + '0');
		baseName = name;
		this.queue = queue;
		int priority = getThreadPriority();
		if (priority != Thread.NORM_PRIORITY)
			thread.setPriority(priority);
	}

	/**
	 * Constructs a new EventThread, after the <code> old </code> event thread
	 * has stopped responding
	 * 
	 * @param old
	 *            The previous instance
	 */
	protected EventThread(EventThread old) {
		makeThread(group = old.thread.getThreadGroup(), name = old.baseName + old.counter++);
		baseName = old.baseName;
		counter = old.counter;
		queue = old.queue;
		int priority = getThreadPriority();
		if (priority != Thread.NORM_PRIORITY)
			thread.setPriority(priority);
	}

	public void start() {
		thread.start();
	}

	/**
	 * Adds an event in the event queue. The method must be synchronized
	 * outside, on the <code> queue </code> field.
	 * 
	 * @param event
	 *            The event to add
	 * @param check
	 *            If true, the method will check if the EventThread is still
	 *            responding
	 */
	public void addEvent(Object event, boolean check) {
		try {
			queue.put(event);
		} catch (Throwable t) {
			print(t);
			return;
		}
		if ((state & 2) != 0)
			queue.notify();
		else if (check && checkTime())
			try {
				state |= 1;
				newEventDispatcher(); // must call start
			} catch (Throwable t) {
				print(t);
				state &= 254;
			}
	}

	/**
	 * Processes the event queue. Sets the event to be dispathed in the
	 * <code> element </code> field and calls <cope> processEvent </code>
	 */
	@Override
	public void run() {
		synchronized (queue) {
			queue.notifyAll();
		}
		while (true) {
			try {
				synchronized (queue) {
					if ((state & 1) != 0)
						return; // closed
					while ((element = queue.get()) == null)
						try {
							state |= 2; // waiting
							queue.wait();
							if ((state & 1) != 0)
								return; // closed
							state &= 253; // not waiting
						} catch (InterruptedException ie) {
						}
				}
				processEvent();

			} catch (Throwable t) {
				print(t);
				try { // fix memory leak
					throw new Exception();
				} catch (Exception _) {
				}
			}
		}
	}

	private void makeThread(ThreadGroup threadGroup, String threadName) {
		try {
			if (privilegedAction == null) {
				privilegedAction = new PrivilegedActionImpl();
			}
			privilegedAction.set(threadGroup, this, threadName);
			thread = AccessController.doPrivileged(privilegedAction);
			// thread = new Thread(group, this, name);
			// thread.setDaemon(false);
			// if (!disableContextClassLoader)
			// thread.setContextClassLoader(null);
		} catch (RuntimeException re) {
			throw re;
		} catch (Exception exc) {
			throw new RuntimeException(exc.toString());
		}
	}

	public Thread getThread() {
		return thread;
	}

	public String getName() {
		return name;
	}

	/**
	 * Returns the desired thread priority. Called in the constructors of the
	 * class in order to set the returned value to the thread.
	 * 
	 * @return priority of the thread
	 */
	public abstract int getThreadPriority();

	/**
	 * Performs the actual event delivery. The event is stored in the
	 * <code> element </code> field. The method is supposed to perform the
	 * following for every listener:
	 * <li> synchronized on <code> queue </code> check the state of the thread -
	 * if it si closed - return
	 * <li> set the fields <code> bad and time <code> 
	 * <li> callback 
	 * <li> set bad to null and time to 0
	 */
	public abstract void processEvent();

	/**
	 * Checks if the thread is still active. The fields <code> time </code> and
	 * <code> bad </code> must be used. The method is called from the addEvent
	 * method - thus should be synchronizes on the <code> queue </code> field
	 * outside and additional synchronization is not needed.
	 */
	public abstract boolean checkTime();

	/**
	 * The method must create a new EventThread instance, using
	 * <code> super.EventThread(this) </code> and start it.
	 */
	public abstract void newEventDispatcher();

	/**
	 * Logs the error.
	 * 
	 * @param t
	 */
	public abstract void print(Throwable t);
}

class PrivilegedActionImpl implements PrivilegedAction<Thread> {
	private ThreadGroup group;
	private Runnable runnable;
	private String name;

	private boolean locked = false;
	private int waiting = 0;

	void set(ThreadGroup group, Runnable runnable, String name) {
		lock();
		this.group = group;
		this.runnable = runnable;
		this.name = name;
	}

	@Override
	public Thread run() {
		ThreadGroup group1 = this.group;
		Runnable runnable1 = this.runnable;
		String name1 = this.name;
		unlock();
		Thread th = new Thread(group1, runnable1, name1);
		if (!UtilActivator.getBoolean("equinox.disableContextClassLoader")) //$NON-NLS-1$
			th.setContextClassLoader(null);
		th.setDaemon(false);
		return th;
	}

	private synchronized void lock() {
		while (locked)
			try {
				waiting++;
				wait();
				waiting--;
			} catch (Exception exc) {
			}
		locked = true;
	}

	private synchronized void unlock() {
		locked = false;
		group = null;
		runnable = null;
		name = null;
		if (waiting > 0)
			notifyAll();
	}
}

Back to the top