annotate Implab/FuncChainTaskBase.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents
children eb793fbbe4ea
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
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
11 protected FuncChainTaskBase( Func<Exception, IPromise<TResult>> error, Func<Exception, IPromise<TResult>> cancel) {
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;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
14 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
15
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
16 public void Reject(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
17 if (LockCancelation())
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 HandleErrorInternal(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
20
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
21 public override void CancelOperation(Exception reason) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
22 if (m_cancel != null && LockCancelation()) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
23 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
24 Observe(m_cancel(reason));
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
26 HandleErrorInternal(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
27 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
28 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
29
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
30 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
31
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
32 protected void HandleErrorInternal(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35 Observe(m_error(error));
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 SetError(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
39 } else {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
40 SetError(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
41 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
42 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 protected void Observe(IPromise<TResult> operation) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45 if (operation == null)
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 throw new NullReferenceException("The task returned null promise");
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 // pass operation results to the current promise
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49 operation.On(SetResult, SetError, SetCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
50
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51 // pass the cancelation request
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52 CancellationRequested(operation.Cancel);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
55 protected bool LockCancelation() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
56 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
57 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
58 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
59 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
60