annotate Implab/Diagnostics/OperationContext.cs @ 209:a867536c68fc v2

Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise
author cin
date Wed, 16 Nov 2016 03:06:08 +0300
parents ea485487a424
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
1 namespace Implab.Diagnostics {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
2 struct OperationContext {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
3 public readonly static OperationContext EMPTY = new OperationContext(LogicalOperation.EMPTY, false);
134
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
4
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
5 LogicalOperation m_initial;
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
6 LogicalOperation m_current;
134
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
7 bool m_ownership;
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
8
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
9 public OperationContext(LogicalOperation operation, bool ownership) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
10 Safe.ArgumentNotNull(operation, "operation");
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
11
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
12 m_initial = operation;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
13 m_current = operation;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
14 m_ownership = ownership;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
15 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
16
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
17 public LogicalOperation CurrentOperation {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
18 get { return m_current; }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
19 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
20
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
21 public void BeginLogicalOperation(string name) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
22 m_current = new LogicalOperation(name, m_current);
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
23 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
24
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
25 public LogicalOperation DetachLogicalOperation() {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
26 var detached = m_current;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
27 if (m_current != LogicalOperation.EMPTY) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
28 if (m_current != m_initial)
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
29 m_current = m_current.Parent;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
30 else if (m_ownership)
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
31 m_current = LogicalOperation.EMPTY;
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
32 else {
195
ea485487a424 minor changes
cin
parents: 134
diff changeset
33 TraceLog.TraceWarning("DetachLogicalOperation can't be performed in the current context");
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
34 detached = LogicalOperation.EMPTY;
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
35 }
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
36 } else {
195
ea485487a424 minor changes
cin
parents: 134
diff changeset
37 TraceLog.TraceWarning("DetachLogicalOperation can't be performed in the current context");
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
38 }
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
39
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
40 return detached;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
41 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
42
134
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
43 public LogicalOperation EndLogicalOperation() {
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
44 var current = m_current;
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
45 if (m_current != LogicalOperation.EMPTY && (m_current != m_initial || m_ownership)) {
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
46 m_current = m_current.Parent;
134
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
47 if (current == m_initial) {
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
48 // we have complete the owned operation
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
49 m_initial = m_current;
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
50 m_ownership = false;
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
51 }
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
52 } else {
195
ea485487a424 minor changes
cin
parents: 134
diff changeset
53 TraceLog.TraceWarning("EndLogicalOperation can't be performed in the current context");
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
54 }
134
04d4c92d0f28 Improved logging
cin
parents: 93
diff changeset
55 return current;
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
56 }
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
57
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
58 public void Leave() {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
59
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
60 if ((m_ownership && m_current != LogicalOperation.EMPTY) || (!m_ownership && m_current != m_initial) )
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
61 TraceLog.TraceWarning("Trying to leave unfinished logical operation {0}", m_current.Name);
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
62 }
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
63 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
64 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
65