annotate Implab/ActionChainTaskBase.cs @ 146:e03ccec4a08d v2

minor changes
author cin
date Mon, 09 Mar 2015 17:21:20 +0300
parents 706fccb85524
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 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
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
11 protected ActionChainTaskBase( Func<Exception, IPromise> error, Func<Exception, IPromise> 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
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) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
24 if (m_cancel != null && LockCancelation()) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
26 Observe(m_cancel(reason));
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
27 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
28 HandleErrorInternal(err);
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 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
33
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
34 protected void HandleErrorInternal(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 Observe(m_error(error));
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 } catch(Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
39 SetError(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
40 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
41 } else {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
42 SetError(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 protected void Observe(IPromise operation) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 if (operation == null)
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 throw new NullReferenceException("The task returned null promise");
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
50 // pass operation results to the current promise
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51 operation.On(SetResult, SetError, SetCancelled);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
53 // pass the cancelation request
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
54 CancellationRequested(operation.Cancel);
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 protected bool LockCancelation() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
58 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
59 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
60 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
61 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
62