-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJobThread.cs
More file actions
290 lines (243 loc) · 5.23 KB
/
JobThread.cs
File metadata and controls
290 lines (243 loc) · 5.23 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
// from http://answers.unity3d.com/questions/754873/c-how-to-make-a-job-queue-for-noise-function.html
public abstract class JobPool<JOBTYPE>
{
class TJob
{
public JOBTYPE job;
public ManualResetEvent doneEvent; // a flag to signal when the work is complete
public JobPool<JOBTYPE> parent;
public TJob(JOBTYPE _job,JobPool<JOBTYPE> _parent)
{
job = _job;
parent = _parent;
doneEvent = new ManualResetEvent(false);
doneEvent.Reset();
}
public void ThreadPoolCallback (System.Object o)
{
doneEvent.Reset();
parent.DoExecuteJob (this.job);
parent.RemoveJob (this);
doneEvent.Set ();
}
};
public bool debug = true;
public float jobProgress
{
get
{
return jobsCompleted / (float)Mathf.Max(1,jobsPending+jobsCompleted);
}
}
public int jobsCompleted
{
get {
return jobsCompletedCount + jobsExceptionCount;
}
}
public int jobsPending
{
get
{
return (jobs!=null) ? jobs.Count : 0;
}
}
int jobsCompletedCount = 0;
int jobsExceptionCount = 0;
List<TJob> jobs = new List<TJob>();
List<ManualResetEvent> jobDoneEvents = new List<ManualResetEvent> ();
//bool run = true;
//Thread thread;
protected abstract void ExecuteJob (JOBTYPE Job);
private void DoExecuteJob(JOBTYPE Job)
{
try {
ExecuteJob (Job);
jobsCompletedCount++;
} catch {
jobsExceptionCount++;
}
}
public void PushJob(JOBTYPE Job)
{
var NewJob = new TJob (Job, this);
lock (jobDoneEvents) {
jobs.Add (NewJob);
jobDoneEvents.Add (NewJob.doneEvent);
}
ThreadPool.QueueUserWorkItem ( NewJob.ThreadPoolCallback );
}
private void RemoveJob(TJob Job)
{
lock (jobDoneEvents) {
jobDoneEvents.Remove (Job.doneEvent);
jobs.Remove (Job);
}
}
public void WaitForJobs(int TimeoutMs)
{
var NonNullEvents = new List<ManualResetEvent> ();
int NullCount = 0;
lock (jobDoneEvents) {
foreach (var e in jobDoneEvents) {
if (e != null)
NonNullEvents.Add (e);
NullCount++;
}
}
var WaitHandles = NonNullEvents.ToArray ();
Debug.Log ("Wait for jobs x" + WaitHandles.Length + " (" + NullCount + " nulls");
if (WaitHandles == null)
return;
if ( TimeoutMs == -1 )
WaitHandle.WaitAll (WaitHandles);
else
WaitHandle.WaitAll (WaitHandles,TimeoutMs);
}
public void Shutdown()
{
WaitForJobs (-1);
}
};
public class JobPool_Action : JobPool<System.Action>
{
protected override void ExecuteJob (System.Action Job)
{
if (Job != null)
Job.Invoke ();
}
}
public abstract class JobThread_Base<JOBTYPE>
{
public bool debug = true;
public float jobProgress
{
get
{
return jobsCompleted / (float)Mathf.Max(1,jobsPending+jobsCompleted);
}
}
public int jobsCompleted
{
get {
return jobsCompletedCount;
}
}
public int jobsPending
{
get
{
return (jobs!=null) ? jobs.Count : 0;
}
}
int jobsCompletedCount = 0;
List<JOBTYPE> jobs = new List<JOBTYPE>();
bool run = true;
Thread thread;
ManualResetEvent IsIdle = new ManualResetEvent(true);
public abstract void ExecuteJob (JOBTYPE Job);
public JobThread_Base()
{
}
public void PushJob(JOBTYPE Job)
{
jobs.Add (Job);
IsIdle.Reset (); // no longer idle
Wake();
}
public void Shutdown()
{
run = false;
if (thread != null) {
Debug.Log ("Shutdown Worker thread joining...");
thread.Join ();
thread = null;
Debug.Log ("Shutdown Worker thread null'd");
}
}
void Wake()
{
// thread has finished or thrown exception
if ( thread != null && !thread.IsAlive) {
// hiccup? mark as idle so WaitFor doesn't block
OnIdle();
if ( debug )
Debug.Log ("Worker thread not alive: " + jobs.Count + " jobs");
if ( debug )
Debug.Log ("Worker thread joining...");
thread.Join ();
thread = null;
if ( debug )
Debug.Log ("Worker thread null'd");
}
if ( thread == null && run )
{
thread = new Thread (new ThreadStart (Iterator));
thread.Start();
// spin to startup thread
while (!thread.IsAlive) {
if ( debug )
Debug.Log ("Spinning to startup thread");
}
}
}
void Iterator()
{
try
{
while ( run )
//while (true)
{
if (jobs.Count == 0) {
OnIdle ();
// wait for job notification here
Thread.Sleep (1);
continue;
}
// run through all the jobs, lock so we know they're busy
if ( debug )
Debug.Log(jobs.Count + " Jobs todo");
while (jobs.Count > 0) {
var Job0 = jobs [0];
ExecuteJob( Job0 );
jobsCompletedCount++;
jobs.RemoveAt (0);
}
}
}
catch(System.Exception e) {
Debug.Log ("Worker thread exception... clearing " + jobs.Count + " jobs");
Debug.LogException (e);
jobs.Clear ();
OnIdle ();
}
}
void OnIdle()
{
IsIdle.Set ();
}
public void WaitForJobs(int TimeoutMs=0)
{
Wake ();
if ( debug )
Debug.Log ("Waiting for " + jobs.Count + " jobs.");
if (jobs.Count > 0) {
if ( TimeoutMs == 0 )
IsIdle.WaitOne ();
else
IsIdle.WaitOne (TimeoutMs);
}
}
}
public class JobThread : JobThread_Base<System.Action>
{
public override void ExecuteJob (System.Action Job)
{
if (Job != null)
Job.Invoke ();
}
}