Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7109e3e0ad2133dfc61d28eb2aa8a06c1088a9fa (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
/*******************************************************************************
 * Copyright (c) 1997, 2018 by ProSyst Software GmbH
 * http://www.prosyst.com
 *
 * 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:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.util.impl.tpt.threadpool;

import java.security.*;
import org.eclipse.equinox.internal.util.UtilActivator;
import org.eclipse.equinox.internal.util.impl.tpt.ServiceFactoryImpl;
import org.eclipse.equinox.internal.util.pool.ObjectPool;
import org.eclipse.equinox.internal.util.timer.TimerListener;

/**
 * @author Pavlin Dobrev
 * @version 1.0
 */

public class ThreadPoolManagerImpl extends ObjectPool implements TimerListener, PrivilegedAction<Executor> {

	static ThreadPoolManagerImpl threadPool = null;

	static ObjectPool jobPool;

	private int used = 0;
	static int tMaximum = 0;

	Job waiting = new Job();

	private static String pAutoMaximum = "equinox.util.threadpool.autoMaximum";

	private static String pMin = "equinox.util.threadpool.minThreads";

	private static String pMax = "equinox.util.threadpool.maxThreads";

	private static String pIgnoreMax = "equinox.util.threadpool.ignoreMaximum";

	private static int defMin = 1;

	private static int defMax = 48;

	private static int MAX_WAITING = 20;

	private static float MAX_OVERLOAD = 0.10F;

	private static boolean ignoreMax;
	private static boolean autoMax;

	private ThreadPoolManagerImpl(int i, int j, int m) {
		super((Class<?>) null, i, j, m);
		tMaximum = i * j;
		ignoreMax = UtilActivator.getBoolean(pIgnoreMax);
		autoMax = UtilActivator.getBoolean(pAutoMaximum);
		jobPool = new ObjectPool(waiting, 5, 8, 4);
	}

	public static ThreadPoolManagerImpl getThreadPool() {
		if (threadPool == null) {
			int intSize = UtilActivator.getInteger(pMin, defMin);
			int minFill = intSize;
			int factor = UtilActivator.getInteger(pMax, defMax);
			intSize = intSize < 2 ? 2 : intSize;
			if (intSize > factor) {
				factor = (int) (intSize * 1.5 + 0.5);
			}
			threadPool = new ThreadPoolManagerImpl(intSize, (factor / intSize), minFill);
		}
		return threadPool;
	}

	@Override
	public void clear() {
		shrink(-1);
		threadPool = null;
	}

	@Override
	public Object getInstance() throws Exception {
		if (ServiceFactoryImpl.privileged()) {
			return AccessController.doPrivileged(this);
		}

		return new Executor();
	}

	@Override
	public Executor run() {
		return new Executor();
	}

	@Override
	public Object getObject() {
		try {
			return super.getObject();
		} catch (Throwable tw) {
			if (ServiceFactoryImpl.log != null) {
				ServiceFactoryImpl.log.error("Unable to create more threads!\r\nActive Thread Pool tasks: " + used, tw);
			}
		}
		return null;
	}

	public Executor getExecutor() {
		synchronized (getSyncMonitor()) {
			if (used < tMaximum || ignoreMax) {
				Executor e = (Executor) getObject();
				if (e != null) {
					used++;
					return e;
				}
			}
		}
		return null;
	}

	@Override
	protected void shrink(int count) {
		synchronized (getSyncMonitor()) {
			dontExtend = true;
			int x, y;
			for (; nextFree > count; nextFree--) {
				x = nextFree / factor;
				y = nextFree % factor;
				Executor e = (Executor) buff[x][y];
				buff[x][y] = null;
				e.terminate();
			}
		}
	}

	@Override
	public void shrink() {
		shrink(minimumFill - 1);
		dontExtend = false;
	}

	@Override
	public boolean releaseObject(Object obj) {
		Job tmp = null;
		Executor x = (Executor) obj;

		synchronized (getSyncMonitor()) {
			x.factory.finished();

			if (used <= tMaximum || ignoreMax) {
				tmp = waiting.getJob();
			}
		}

		if (tmp == null) {
			used--;
			x.clear();
			x.setPriorityI(Thread.NORM_PRIORITY);
			return super.releaseObject(obj);
		}
		if (UtilActivator.LOG_DEBUG) {
			UtilActivator.log.debug(0x0100, 10005, tmp.name, null, false);
		}
		x.setPriorityI(tmp.priority);

		x.setRunnable(tmp.run, tmp.name, tmp.factory, tmp.acc);
		tmp.fullClear();
		jobPool.releaseObject(tmp);
		return true;
	}

	@Override
	public void timer(int event) {
		int count = 0;
		int all = 0;
		synchronized (getSyncMonitor()) {
			for (int i = 0; i < buff.length; i++) {
				if (buff[i] != null) {
					for (int j = 0; j < buff[i].length; j++) {
						Executor e = (Executor) buff[i][j];
						if (e != null) {
							all++;
							if (!e.accessed)
								count++;
							else
								e.accessed = false;
						}
					}
				}
			}
			if (count > 0 && all > minimumFill - 1 && all > count) {
				/*
				 * keep in mind current thread - shrinking one more, since the
				 * current thread will be back in pool
				 */
				if (count > minimumFill)
					shrink(count - 2);
				else
					shrink(minimumFill - 2);
				dontExtend = false;
			}
		}
	}

	public void execute(Runnable job, int priority, String name, ThreadPoolFactoryImpl factory, AccessControlContext acc) {
		Executor ex = null;
		synchronized (getSyncMonitor()) {
			if (used < tMaximum || ignoreMax) {
				ex = (Executor) getObject();
			}
			if (ex != null) {
				used++;
			} else {
				addInTasksQueue(job, name, priority, factory, acc);

				return;
			}
		}

		ex.setPriorityI(priority);
		ex.setRunnable(job, name, factory, acc);
	}

	private void addInTasksQueue(Runnable job, String name, int priority, ThreadPoolFactoryImpl factory, AccessControlContext acc) {

		waiting.addJob(job, name, priority, factory, acc);
		if (UtilActivator.LOG_DEBUG) {
			UtilActivator.log.debug("In Threadpool Queue: " + name + ", queue size:" + waiting.counter, null);
		}
		if (autoMax && waiting.counter > MAX_WAITING) {
			Executor ex = (Executor) getObject();
			if (ex != null) {
				tMaximum += MAX_WAITING;
				MAX_WAITING += (int) (MAX_WAITING * MAX_OVERLOAD);
				for (Job j = waiting.getJob(); j != null; j = waiting.getJob()) {
					if (ex == null) {
						ex = (Executor) getObject();
					}
					if (ex != null) {
						used++;
						ex.setPriorityI(j.priority);
						ex.setRunnable(j.run, j.name, factory, acc);
						ex = null;
					} else {
						waiting.addJob(j.run, j.name, j.priority, j.factory, acc);
						break;
					}
				}
			}
		}
	}

	public void reset() {
		shrink(-1);
		dontExtend = false;
	}

	public Object getSyncMonitor() {
		return buff;
	}

}

Back to the top