annotate Implab/Diagnostics/OperationContext.cs @ 129:471f596b2603 v2

Added SharedLock to synchronization routines
author cin
date Thu, 29 Jan 2015 18:31:06 +0300
parents dc4942d09e74
children 04d4c92d0f28
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 readonly LogicalOperation m_initial;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
4 public readonly static OperationContext EMPTY = new OperationContext(LogicalOperation.EMPTY, false);
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
5 LogicalOperation m_current;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
6 readonly bool m_ownership;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
7
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
8 public OperationContext(LogicalOperation operation, bool ownership) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
9 Safe.ArgumentNotNull(operation, "operation");
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
10
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
11 m_initial = operation;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
12 m_current = operation;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
13 m_ownership = ownership;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
14 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
15
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
16 public LogicalOperation CurrentOperation {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
17 get { return m_current; }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
18 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
19
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
20 public void BeginLogicalOperation(string name) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
21 m_current = new LogicalOperation(name, m_current);
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
22 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
23
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
24 public LogicalOperation DetachLogicalOperation() {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
25 var detached = m_current;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
26 if (m_current != LogicalOperation.EMPTY) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
27 if (m_current != m_initial)
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
28 m_current = m_current.Parent;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
29 else if (m_ownership)
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
30 m_current = LogicalOperation.EMPTY;
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
31 else {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
32 TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context");
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
33 detached = LogicalOperation.EMPTY;
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
34 }
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
35 } else {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
36 TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context");
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
37 }
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
38
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
39 return detached;
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
40 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
41
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
42 public void EndLogicalOperation() {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
43 if (m_current != m_initial) {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
44 m_current = m_current.Parent;
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
45 } else if (m_current != LogicalOperation.EMPTY && m_ownership) {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
46 m_current = LogicalOperation.EMPTY;
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
47 } else {
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
48 TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context");
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
49 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
50 }
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
51
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
52 public void Leave() {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
53
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
54 if ((m_ownership && m_current != LogicalOperation.EMPTY) || (!m_ownership && m_current != m_initial) )
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
55 TraceLog.TraceWarning("Trying to leave unfinished logical operation {0}", m_current.Name);
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
56 }
92
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
57 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
58 }
4c0e5ef99986 rewritten tracing
cin
parents:
diff changeset
59