-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathThreadedResultComputeOp.cs
200 lines (157 loc) · 6.4 KB
/
ThreadedResultComputeOp.cs
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
// Copyright (c) Ryan Schmidt ([email protected]) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using g3;
namespace gs
{
public class ThreadedResultComputeOp<T> : BaseModelingOperator, ResultComputeOp<T>
{
ResultSourceOp<T> result_source;
public ResultSourceOp<T> ResultSource {
get { return result_source; }
set {
if (result_source != null) {
result_source.OperatorModified -= on_source_modified;
result_source.OperatorException -= on_source_exception;
}
result_source = value;
if (result_source != null) {
result_source.OperatorModified += on_source_modified;
result_source.OperatorException += on_source_exception;
}
invalidate();
}
}
bool result_valid = false;
int result_timestamp = 1;
int last_exception_timestamp = 0;
object compute_thread_lock;
bool computing = false;
T computed_result;
public ThreadedResultComputeOp()
{
compute_thread_lock = new object();
}
protected virtual void on_source_modified(ModelingOperator op)
{
invalidate();
PostOnOperatorModified();
}
void invalidate()
{
result_valid = false;
result_timestamp++;
// [RMS] disabled this. It causes too many problems to immediately spawn the compute
// on invalidate(). We want the result to be "pulled", not to "push" it.
// [TODO] maybe this should be an option?
// kick off new compute, if we are not already computing (this fn will check)
//spawn_recompute();
}
class thread_timestamp
{
public int timestamp;
}
thread_timestamp active_thread_data = null;
void spawn_recompute()
{
Thread computer = null;
lock (compute_thread_lock) {
if (computing == false) {
computing = true;
active_thread_data = new thread_timestamp() { timestamp = this.result_timestamp };
ActiveComputeExceptions = new List<ModelingOpException>(); // maybe should go in active_thread_data ?
computer = new Thread(ComputeThreadFunc);
}
}
if (computer != null) {
computer.Start(active_thread_data);
}
}
public bool IsComputing {
get { return computing; }
}
public bool IsResultAvailable {
get { return computing == false && result_valid == false; }
}
public bool ResultConsumed {
get { return computing == false && result_valid == true; }
}
List<ModelingOpException> ActiveComputeExceptions;
List<ModelingOpException> LastComputeExceptions;
protected void on_source_exception(ModelingOpException mopex)
{
ActiveComputeExceptions.Add(mopex);
}
public OpResultOutputStatus<T> CheckForNewResult()
{
if (computing) {
return OpResultOutputStatus<T>.Computing();
} else if (result_valid) {
return OpResultOutputStatus<T>.Unavailable();
} else if (last_exception_timestamp == result_timestamp) {
return OpResultOutputStatus<T>.Unavailable();
} else {
// the compute we were waiting for has finished, extract the resulting mesh
T returnResult = default(T);
bool spawn_new_compute = false;
lock (compute_thread_lock) {
returnResult = computed_result;
computed_result = default(T);
// situations where we will discard this result:
// 1) it came back null. lets hope this is temporary...
// 2) active_thread_data was null. This means we hadn't actually spawned
// the compute thread yet. [TODO] maybe this should happen another way?
// 3) the timestamp increment since we spawned the current comput thread.
// Discard the result and spawn a new compute.
bool use_result = (returnResult != null) &&
(active_thread_data != null) &&
(active_thread_data.timestamp == result_timestamp);
if ( use_result ) {
result_valid = true;
} else {
result_valid = false;
spawn_new_compute = true;
}
active_thread_data = null;
LastComputeExceptions = ActiveComputeExceptions;
ActiveComputeExceptions = null;
}
if ( spawn_new_compute )
spawn_recompute();
if (returnResult == null) {
return (computing) ? OpResultOutputStatus<T>.Computing() : OpResultOutputStatus<T>.Unavailable();
} else {
return OpResultOutputStatus<T>.Ready(returnResult, LastComputeExceptions);
}
}
}
void ComputeThreadFunc(object thread_data)
{
thread_timestamp data = thread_data as thread_timestamp;
try {
T result = ResultSource.ExtractResult();
lock (compute_thread_lock) {
computed_result = result;
computing = false;
}
} catch(Exception e) {
background_exception = e;
computing = false;
last_exception_timestamp = data.timestamp;
}
}
Exception background_exception = null;
public bool HaveBackgroundException {
get { return background_exception != null; }
}
public Exception ExtractBackgroundException()
{
var r = background_exception;
background_exception = null;
return r;
}
}
}