-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathThreadPool.hx
180 lines (143 loc) · 4.61 KB
/
ThreadPool.hx
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
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package hx.concurrent.thread;
#if threads
import haxe.ds.StringMap;
import hx.concurrent.Service.ServiceBase;
import hx.concurrent.atomic.AtomicInt;
import hx.concurrent.collection.Queue;
import hx.concurrent.thread.Threads;
class ThreadPool extends ServiceBase {
/**
* The default amount of time a worker sleeps waiting for work
*/
public static final DEFAULT_POLL_PERIOD = 0.001;
static final _threadIDs = new AtomicInt();
var _spawnedThreadCount = new AtomicInt(0);
var _workingThreadCount = new AtomicInt(0);
final _workQueue = new Queue<Task>();
public final threadCount:Int;
/**
* The amount of time a worker sleeps waiting for work
*/
public var pollPeriod(default, set):Float = DEFAULT_POLL_PERIOD;
inline function set_pollPeriod(value:Float) {
if (value <= 0)
throw "[value] must be >= 0";
return pollPeriod = value;
}
/**
* Number of tasks currently executed in parallel.
*/
public var executingTasks(get, never):Int;
inline function get_executingTasks():Int return _workingThreadCount;
/**
* Number of tasks waiting for execution.
*/
public var pendingTasks(get, never):Int;
inline function get_pendingTasks():Int return _workQueue.length;
public function new(numThreads:Int, autostart = true) {
if (numThreads < 1)
throw "[numThreads] must be > 0";
super();
threadCount = numThreads;
if (autostart)
start();
}
/**
* Waits for all submitted tasks being executed.
*
* If <code>timeoutMS</code> is set 0, the function immediatly returns.
* If <code>timeoutMS</code> is set to value > 0, this function waits for the given time until a result is available.
* If <code>timeoutMS</code> is set to `-1`, this function waits indefinitely until a result is available.
* If <code>timeoutMS</code> is set to value lower than -1, results in an exception.
*
* @return <code>true</code> if all submitted tasks are done otherwise <code>false</code>.
*/
public function awaitCompletion(timeoutMS:Int):Bool
return Threads.await(() -> _workQueue.length == 0 && _workingThreadCount == 0, timeoutMS);
/**
* @return the number of cancelled tasks
*/
public function cancelPendingTasks():Int {
var canceled = 0;
while (true) {
if (_workQueue.pop() == null)
break;
canceled++;
}
return canceled;
}
override //
function onStart() {
state = RUNNING;
/*
* start worker threads
*/
for (i in 0...threadCount) {
Threads.spawn(function() {
_spawnedThreadCount++;
final context = new ThreadContext(_threadIDs.incrementAndGet());
trace('[$this] Spawned thread $_spawnedThreadCount/$threadCount with ID ${context.id}.');
while (true) {
final task = _workQueue.pop();
if (task == null) {
if(state != RUNNING)
break;
Sys.sleep(pollPeriod);
} else {
try {
_workingThreadCount++;
task(context);
} catch (ex) {
trace(ex);
}
_workingThreadCount--;
}
}
trace('[$this] Stopped thread with ID ${context.id}.');
_spawnedThreadCount--;
if (_spawnedThreadCount == 0)
_stateLock.execute(() -> state = STOPPED);
});
}
}
/**
* Submits a task for immediate execution in a thread.
*/
public function submit(task:Task):Void {
if (task == null)
throw "[task] must not be null";
_stateLock.execute(function() {
if (state != RUNNING)
throw 'ThreadPool is not in requried state [RUNNING] but [$state]';
_workQueue.push(task);
});
}
/**
* Initiates a graceful shutdown of this executor canceling execution of all queued tasks.
*/
override //
public function stop():Void
_stateLock.execute(function() {
if (state == RUNNING) {
state = STOPPING;
}
});
}
typedef Task=ThreadContext->Void;
class ThreadContext {
/**
* ID of the current thread
*/
public final id:Int;
public final vars = new StringMap<Dynamic>();
inline //
public function new(id:Int) {
this.id = id;
}
}
#end