annotate Implab/ActionTaskBase.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 ActionTaskBase : AbstractPromise {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
6 readonly Action<Exception> m_cancel;
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
7 readonly Action<Exception> m_error;
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 ActionTaskBase( Action<Exception> error, Action<Exception> 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 Safe.ArgumentNotNull(error, "error");
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
18 if (LockCancelation())
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
19 HandleErrorInternal(error);
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 protected void HandleErrorInternal(Exception error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
23 if (m_error != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
24 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
25 m_error(error);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
26 SetResult();
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 SetError(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 } else {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
31 SetError(error);
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
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
35 public override void CancelOperation(Exception reason) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
36 if (LockCancelation()) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
37 if (m_cancel != null) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
38 try {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
39 m_cancel(reason);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
40 SetResult();
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
41 } catch (Exception err) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
42 HandleErrorInternal(err);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
43 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
44 } else {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
45 SetCancelled(reason);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
46 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
47 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
48 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
49
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
50 protected bool LockCancelation() {
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
51 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
706fccb85524 RC: cancellation support for promises + tests
cin
parents:
diff changeset
52 }
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