annotate Implab/ActionChainTaskBase.cs @ 186:75103928da09 ref20160224

working on cancelation and error handling
author cin
date Tue, 19 Apr 2016 00:50:14 +0300
parents eb793fbbe4ea
children dd4a3590f9c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
1 using System;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
2 using System.Threading;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
3
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
4 namespace Implab {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
5 public class ActionChainTaskBase : AbstractPromise {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
6 readonly Func<Exception, IPromise> m_error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
7 readonly Func<Exception, IPromise> m_cancel;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
8
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
9 int m_cancelationLock;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
10
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
11 protected ActionChainTaskBase(Func<Exception, IPromise> error, Func<Exception, IPromise> cancel, bool autoCancellable) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
12 m_error = error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
13 m_cancel = cancel;
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
14 if (autoCancellable)
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
15 CancellationRequested(CancelOperation);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
16 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
17
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 public void Reject(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19 if (LockCancelation())
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20 HandleErrorInternal(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
21 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
22
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
23 public override void CancelOperation(Exception reason) {
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
24 if (LockCancelation()) {
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
25 if (!(reason is OperationCanceledException))
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
26 reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
27
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
28 if (m_cancel != null) {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
29 try {
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
30 m_cancel(reason).On(SetResult, HandleErrorInternal, HandleCancelInternal);
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
31 } catch (Exception err) {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
32 HandleErrorInternal(err);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
33 }
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
34 } else {
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
35 HandleErrorInternal(reason);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
39
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
40 void HandleCancelInternal(Exception reason) {
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
41 if (!(reason is OperationCanceledException))
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
42 reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException();
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
43 HandleErrorInternal(reason);
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
44 }
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
45
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
46 void HandleErrorInternal(Exception error) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 try {
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
49 var p = m_error(error);
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
50 p.On(SetResult, SetError, SetCancelled);
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
51 CancellationRequested(p.Cancel);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
52 } catch (Exception err) {
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
53 error = err;
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
55 } else {
186
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
56 SetErrorInternal(error);
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
57 }
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
58 }
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
59
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
60 void SetErrorInternal(Exception error) {
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
61 while (error is PromiseTransientException)
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
62 error = error.InnerException;
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
63
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
64 if (error is OperationCanceledException)
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
65 SetCancelled(error);
75103928da09 working on cancelation and error handling
cin
parents: 149
diff changeset
66 else
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
67 SetError(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
68 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
69
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
70 protected bool LockCancelation() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
71 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
72 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
73 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
74 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
75