blob: ef19b8bff595d5f715b6416dd371c1d89864bacb (
plain)
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
|
using System;
using System.Collections.Generic;
using ST = System.Threading;
namespace SharpCifs.Util.Sharpen
{
class ThreadPoolExecutor
{
ThreadFactory _tf;
int _corePoolSize;
int _maxPoolSize;
List<Thread> _pool = new List<Thread> ();
int _runningThreads;
int _freeThreads;
bool _shutdown;
Queue<IRunnable> _pendingTasks = new Queue<IRunnable> ();
public ThreadPoolExecutor (int corePoolSize, ThreadFactory factory)
{
this._corePoolSize = corePoolSize;
_maxPoolSize = corePoolSize;
_tf = factory;
}
public void SetMaximumPoolSize (int size)
{
_maxPoolSize = size;
}
public bool IsShutdown ()
{
return _shutdown;
}
public virtual bool IsTerminated ()
{
lock (_pendingTasks) {
return _shutdown && _pendingTasks.Count == 0;
}
}
public virtual bool IsTerminating ()
{
lock (_pendingTasks) {
return _shutdown && !IsTerminated ();
}
}
public int GetCorePoolSize ()
{
return _corePoolSize;
}
public void PrestartAllCoreThreads ()
{
lock (_pendingTasks) {
while (_runningThreads < _corePoolSize)
StartPoolThread ();
}
}
public void SetThreadFactory (ThreadFactory f)
{
_tf = f;
}
public void Execute (IRunnable r)
{
InternalExecute (r, true);
}
internal void InternalExecute (IRunnable r, bool checkShutdown)
{
lock (_pendingTasks) {
if (_shutdown && checkShutdown)
throw new InvalidOperationException ();
if (_runningThreads < _corePoolSize) {
StartPoolThread ();
}
else if (_freeThreads > 0) {
_freeThreads--;
}
else if (_runningThreads < _maxPoolSize) {
StartPoolThread ();
}
_pendingTasks.Enqueue (r);
ST.Monitor.PulseAll (_pendingTasks);
}
}
void StartPoolThread ()
{
_runningThreads++;
_pool.Add (_tf.NewThread (new RunnableAction (RunPoolThread)));
}
public void RunPoolThread ()
{
while (!IsTerminated ()) {
try {
IRunnable r = null;
lock (_pendingTasks) {
_freeThreads++;
while (!IsTerminated () && _pendingTasks.Count == 0)
ST.Monitor.Wait (_pendingTasks);
if (IsTerminated ())
break;
r = _pendingTasks.Dequeue ();
}
if (r != null)
r.Run ();
}
//supress all errors, anyway
//catch (ST.ThreadAbortException) {
// // Do not catch a thread abort. If we've been aborted just let the thread die.
// // Currently reseting an abort which was issued because the appdomain is being
// // torn down results in the process living forever and consuming 100% cpu time.
// return;
//}
catch {
}
}
}
public virtual void Shutdown ()
{
lock (_pendingTasks) {
_shutdown = true;
ST.Monitor.PulseAll (_pendingTasks);
}
}
public virtual List<IRunnable> ShutdownNow ()
{
lock (_pendingTasks) {
_shutdown = true;
foreach (var t in _pool) {
try {
t.Abort ();
} catch {}
}
_pool.Clear ();
_freeThreads = 0;
_runningThreads = 0;
var res = new List<IRunnable> (_pendingTasks);
_pendingTasks.Clear ();
return res;
}
}
}
class RunnableAction: IRunnable
{
Action _action;
public RunnableAction (Action a)
{
_action = a;
}
public void Run ()
{
_action ();
}
}
}
|