-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFlowState.cs
More file actions
85 lines (72 loc) · 1.84 KB
/
FlowState.cs
File metadata and controls
85 lines (72 loc) · 1.84 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
using System.Threading;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Storage;
namespace Cleipnir.ResilientFunctions.CoreRuntime;
public class FlowState
{
private readonly Lock _lock = new();
private readonly TaskCompletionSource _suspendedTcs = new();
public StoredId Id { get; }
public int Subflows { get; private set; }
public int WaitingSubflows { get; private set; }
public FlowTimeouts Timeouts { get; }
public AsyncSignal InterruptSignal { get; } = new();
public bool Suspended { get; private set; }
public Task SuspendedTask { get; }
public FlowState(
StoredId id,
int subflows,
int waitingSubflows,
FlowTimeouts timeouts)
{
Id = id;
Subflows = subflows;
WaitingSubflows = waitingSubflows;
Timeouts = timeouts;
SuspendedTask = _suspendedTcs.Task;
}
public void SubflowStarted()
{
lock (_lock)
Subflows++;
}
public void SubflowCompleted()
{
lock (_lock)
Subflows--;
}
public void SubflowWaiting()
{
lock (_lock)
WaitingSubflows++;
}
public bool TryResumeSubflow()
{
lock (_lock)
if (Suspended)
return false;
else
WaitingSubflows--;
return true;
}
public void Interrupt()
{
lock (_lock)
if (Suspended)
return;
else
WaitingSubflows = 0;
InterruptSignal.Fire();
}
public bool Suspend()
{
lock (_lock)
if (Subflows == WaitingSubflows)
Suspended = true;
else
return false;
_suspendedTcs.TrySetResult();
return true;
}
}