-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathYiedFinishedThreadedDetector.cs
More file actions
115 lines (102 loc) · 3.87 KB
/
YiedFinishedThreadedDetector.cs
File metadata and controls
115 lines (102 loc) · 3.87 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
using System;
using System.Threading;
namespace kOS.Safe.Execution
{
public abstract class YiedFinishedThreadedDetector : YieldFinishedDetector
{
private ManualResetEvent childThreadEvent;
private Thread childThread;
private Exception childException;
protected SafeSharedObjects shared;
public override void Begin(SafeSharedObjects sharedObj)
{
if (childThread != null)
throw new Exceptions.KOSException("Error calling Begin, childThread is not null.");
shared = sharedObj;
childThreadEvent = new ManualResetEvent(false);
ThreadInitialize(sharedObj);
if (RunOnCaller())
{
DoThread();
}
else
{
childThread = new Thread(DoThread);
childThread.IsBackground = true;
childThread.Start();
}
}
public override bool IsFinished()
{
if (childThreadEvent.WaitOne(0))
{
if (childThread != null)
{
childThread.Join();
}
if (childException == null)
{
ThreadFinish();
}
else
{
// If there was an error in the child thread, break execution and then
// throw the exception. Because we're still executing the same opcode
// throwing will be caught and logged exactly the same as any other error
// in normal code. It's important to break execution to ensure the
// instruction pointer skips the current opcode that throws the exception.
shared.Cpu.BreakExecution(false);
throw childException;
}
childThread = null;
return true;
}
return false;
}
private void DoThread()
{
try
{
ThreadExecute();
}
catch (Exception ex)
{
childException = ex;
}
childThreadEvent.Set();
}
/// <summary>
/// This method is executed before starting the child thread. It is called from the main thread and is not required
/// to be thread safe with respect to KSP.
/// </summary>
/// <param name="shared"></param>
public abstract void ThreadInitialize(SafeSharedObjects shared);
/// <summary>
/// <para>
/// This method is what actually executes in the background. It should do the majority of the work.
/// Remember that the KSP and Unity APIs are not thread safe, so calls to either API should be avoided in this
/// method.
/// </para>
/// <para>
/// WARNING: THIS METHOD EXECUTES ON A SEPARATE THREAD. TAKE CARE TO ENSURE THAT IMPLEMENTATIONS ARE THREAD SAFE
/// </para>
/// </summary>
/// <param name="shared"></param>
public abstract void ThreadExecute();
/// <summary>
/// This method is executed after the child thread is finished, when the CPU checks IsFinished. It is called from
/// the main thread and is not required to be thread safe with respect to KSP.
/// </summary>
public abstract void ThreadFinish();
/// <summary>
/// Determines if it must execute DoThread() when Begin() is invoked using caller thread.
/// <br/>
/// This method method is used by descendants to tell whether they want to freeze a game.
/// </summary>
/// <returns>false to run in separate thread, true to freeze a game</returns>
protected virtual bool RunOnCaller()
{
return true;
}
}
}