Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ae2b1d074497b0a16a399aacd49b0a74a719cbb (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
/*******************************************************************************
 * Copyright (c) 1997-2009 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.ds;

import org.eclipse.equinox.internal.util.event.Queue;
import org.eclipse.equinox.internal.util.ref.TimerRef;
import org.eclipse.equinox.internal.util.timer.TimerListener;
import org.eclipse.osgi.util.NLS;
import org.osgi.service.cm.ConfigurationEvent;

/**
 * @author Stoyan Boshev
 * @author Pavlin Dobrev
 */

public class WorkThread implements Runnable, TimerListener {

	boolean processBundle = false;

	public static int IDLE_TIMEOUT = 1000;
	public static int BLOCK_TIMEOUT = 30000;
	private SCRManager mgr;
	private Object objectToProcess;
	boolean running = true;
	Thread processingThread;

	int waiting = 0;

	public WorkThread(SCRManager mgr) {
		this.mgr = mgr;
	}

	/**
	 * While the event queue has elements - they are processed, i.e.
	 * ManagedService(Factories) are informed for the event.
	 */
	public void run() {
		processingThread = Thread.currentThread();
		do {
			try {
				Queue queue = mgr.queue;
				synchronized (queue) {
					if (mgr.stopped) {
						mgr.running = false;
						break;
					}
					if (Activator.DEBUG) {
						Activator.log.debug("WorkThread.run()", null); //$NON-NLS-1$
					}
					if (queue.size() == 0) { // wait for more events
						try {
							waiting++;
							queue.wait(IDLE_TIMEOUT);
						} catch (Exception ignore) {
							//ignore
						}
						waiting--;
						if (mgr.stopped || queue.size() == 0) {
							mgr.running = false;
							break;
						}
					}
					objectToProcess = queue.get();

					if (objectToProcess != null) {
						if (Activator.DEBUG) {
							Activator.log.debug("WorkThread.run(): object to process " + objectToProcess.toString(), null); //$NON-NLS-1$
						}
					} else {
						continue;
					}
				}
				if (TimerRef.timer != null) {
					TimerRef.notifyAfter(this, BLOCK_TIMEOUT, 1);
				} else {
					if (Activator.DEBUG) {
						Activator.log.debug(Messages.TIMER_SERVICE_UNAVAILABLE, null);
					}
				}
				if (objectToProcess instanceof SCRManager.QueuedJob) {
					((SCRManager.QueuedJob) objectToProcess).dispatch();
				} else if (objectToProcess instanceof ConfigurationEvent) {
					mgr.processConfigurationEvent((ConfigurationEvent) objectToProcess);
				}
			} catch (Throwable t) {
				// just for any case. Must not happen in order to keep thread alive
				Activator.log.error(Messages.UNEXPECTED_EXCEPTION, t);
			} finally {
				TimerRef.removeListener(this, 1);
			}
		} while (running);
		objectToProcess = null;
		processingThread = null;
	}

	public void timer(int event) {
		Activator.log.warning(NLS.bind(Messages.TIMEOUT_PROCESSING, objectToProcess), null);
		running = false;
		objectToProcess = null;
		mgr.queueBlocked();
	}

}

Back to the top