annotate Implab/FuncChainTaskBase.cs @ 167:96681e9d0cea ref20160224

sync
author cin
date Wed, 02 Mar 2016 00:20:48 +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 FuncChainTaskBase<TResult> : AbstractPromise<TResult> {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
6 readonly Func<Exception, IPromise<TResult>> m_error;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
7 readonly Func<Exception, IPromise<TResult>> 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 FuncChainTaskBase( Func<Exception, IPromise<TResult>> error, Func<Exception, IPromise<TResult>> 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()) {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
25 if (m_cancel != null) {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
26 try {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
27 m_cancel(reason).On(SetResult, HandleErrorInternal, SetCancelled);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
28 } catch (Exception err) {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
29 HandleErrorInternal(err);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
30 }
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
31 } else {
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
32 SetCancelled(reason);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35
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 protected void HandleErrorInternal(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
39 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
40 try {
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
41 var operation = m_error(error);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
42
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
43 operation.On(SetResult, SetError, SetCancelled);
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
44 CancellationRequested(operation.Cancel);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 SetError(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 } else {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49 SetError(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
50 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 protected bool LockCancelation() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
55 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
56 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
57 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
58