Skip to main content
summaryrefslogtreecommitdiffstats
blob: 167f00db0fb49ed07e4a1e1e5f63cfdad4fa6869 (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
/*******************************************************************************
 * Copyright (c) 1997-2007 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.impl.tpt.threadpool;

import java.security.AccessControlContext;
import java.security.AccessController;
import org.eclipse.equinox.internal.util.UtilActivator;
import org.eclipse.equinox.internal.util.impl.tpt.ServiceFactoryImpl;
import org.eclipse.equinox.internal.util.ref.Log;
import org.eclipse.equinox.internal.util.threadpool.ThreadPoolFactory;
import org.eclipse.equinox.internal.util.threadpool.ThreadPoolManager;

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

public class ThreadPoolFactoryImpl extends ServiceFactoryImpl implements ThreadPoolManager, ThreadPoolFactory {

	public static ThreadPoolManagerImpl threadPool;
	private int limit;
	private int used = 0;
	private Job queue;
	private static int defaultPercent;;

	public ThreadPoolFactoryImpl(String bundleName, Log log) {

		super(bundleName, log);
		threadPool = ThreadPoolManagerImpl.getThreadPool();
		defaultPercent = UtilActivator.getInteger("equinox.util.threadpool.percent", 30);
		limit = (ThreadPoolManagerImpl.tMaximum * defaultPercent) / 100;
		if (limit == 0)
			limit = 1;
		queue = new Job();
	}

	public ThreadPoolFactoryImpl(String bundleName, int size) {
		super(bundleName);
		limit = size;
		if (limit == 0)
			limit = 1;
		queue = new Job();
	}

	public ThreadPoolFactoryImpl(String bundleName) {
		this(bundleName, (ThreadPoolManagerImpl.tMaximum * defaultPercent) / 100);
	}

	public Object getInstance(String bundleName) {
		if (threadPool == null)
			throw new RuntimeException("ServiceFactory is currently off!");
		return new ThreadPoolFactoryImpl(bundleName);
	}

	public static void stopThreadPool() {
		ThreadPoolManagerImpl tmp = threadPool;
		threadPool = null;
		tmp.clear();
	}

	public ThreadPoolManager getThreadPool(int size, boolean sizeIsInPercents) {
		if (threadPool == null)
			throw new RuntimeException("[ThreadPool] ThreadPool is inaccessible");

		if (sizeIsInPercents) {
			size = (ThreadPoolManagerImpl.tMaximum * size) / 100;
		}
		if (size <= 0) {
			size = 1;
		}
		return new ThreadPoolFactoryImpl(bundleName, size);
	}

	public void execute(Runnable job, String name) {
		execute(job, Thread.NORM_PRIORITY, name);
	}

	public void execute0(Runnable job, int priority, String name, AccessControlContext acc) {
		if (job == null || name == null) {
			throw new IllegalArgumentException("the job or the name parameter is/are null");
		}

		if (ServiceFactoryImpl.useNames)
			name = name + bundleName;

		ThreadPoolManagerImpl tmp = threadPool;

		if (tmp != null) {
			synchronized (tmp.getSyncMonitor()) {
				if (used >= limit) {
					if (UtilActivator.LOG_DEBUG) {
						UtilActivator.log.debug("In Bundle Queue: " + name + ", bundle queue size: " + queue.counter, null);
					}
					queue.addJob(job, name, priority, this, acc);

					return;
				}

				used++;
			}

			tmp.execute(job, priority, name, this, acc

			);
		} else
			throw new RuntimeException("[ThreadPool] ThreadPool is inaccessible");
	}

	public void execute(Runnable job, int priority, String name, AccessControlContext acc) {
		execute0(job, priority, name, acc

		);
	}

	public void execute(Runnable job, int priority, String name) {
		execute0(job, priority, name, (Log.security() ? AccessController.getContext() : null));
	}

	public Executor getExecutor() {
		ThreadPoolManagerImpl tmp = threadPool;
		if (tmp != null) {
			synchronized (tmp.getSyncMonitor()) {
				if (used < limit) {
					Executor ex = tmp.getExecutor();
					if (ex != null)
						used++;
					return ex;
				}
			}
		}
		return null;
	}

	void finished() {
		Job job = queue.getJob();

		if (job != null) {
			if (UtilActivator.LOG_DEBUG) {
				UtilActivator.log.debug("To threadpool queue: " + job.name + ", queue size: " + threadPool.waiting.counter, null);
			}
			threadPool.waiting.addJob(job);
		} else {
			used--;
		}
	}

	public void reset() {
		if (threadPool != null) {
			threadPool.reset();
		}
	}
}

Back to the top