forked from bepu/bepuphysics1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultithreadedProcessingStage.cs
97 lines (87 loc) · 2.6 KB
/
MultithreadedProcessingStage.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
using System;
using BEPUutilities.Threading;
namespace BEPUphysics
{
///<summary>
/// Superclass of processing systems which can use multiple threads.
///</summary>
public abstract class MultithreadedProcessingStage
{
///<summary>
/// Gets or sets whether or not the processing stage is active.
///</summary>
public virtual bool Enabled { get; set; }
///<summary>
/// Gets or sets whether or not the processing stage should allow multithreading.
///</summary>
public bool AllowMultithreading { get; set; }
///<summary>
/// Gets or sets the multithreaded loop provider used by the stage.
///</summary>
public IParallelLooper ParallelLooper { get; set; }
///<summary>
/// Fires when the processing stage begins working.
///</summary>
public event Action Starting;
/// <summary>
/// Fires when the processing stage finishes working.
/// </summary>
public event Action Finishing;
protected bool ShouldUseMultithreading
{
get
{
return AllowMultithreading && ParallelLooper != null && ParallelLooper.ThreadCount > 1;
}
}
#if PROFILE
/// <summary>
/// Gets the time elapsed in the previous execution of this stage, not including any hooked Starting or Finishing events.
/// </summary>
public double Time
{
get
{
return (end - start) / (double)Stopwatch.Frequency;
}
}
long start, end;
private void StartClock()
{
start = Stopwatch.GetTimestamp();
}
private void StopClock()
{
end = Stopwatch.GetTimestamp();
}
#endif
///<summary>
/// Runs the processing stage.
///</summary>
public void Update()
{
if (!Enabled)
return;
if (Starting != null)
Starting();
#if PROFILE
StartClock();
#endif
if (ShouldUseMultithreading)
{
UpdateMultithreaded();
}
else
{
UpdateSingleThreaded();
}
#if PROFILE
StopClock();
#endif
if (Finishing != null)
Finishing();
}
protected abstract void UpdateMultithreaded();
protected abstract void UpdateSingleThreaded();
}
}