annotate Implab/FuncTaskBase.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
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
1 using System;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
2 using System.Threading;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
3
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
4 namespace Implab {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
5 public class FuncTaskBase<TResult> : AbstractPromise<TResult> {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
6 readonly Func<Exception, TResult> m_cancel;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
7 readonly Func<Exception, TResult> m_error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
8
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
9 int m_cancelationLock;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
10
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 144
diff changeset
11 protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel, bool autoCancellable) {
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
12 m_error = error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
13 m_cancel = cancel;
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 144
diff changeset
14 if (autoCancellable)
eb793fbbe4ea fixed promises cancellation
cin
parents: 144
diff changeset
15 CancellationRequested(CancelOperation);
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
16 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
17
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
18 public void Reject(Exception error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
19 Safe.ArgumentNotNull(error, "error");
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
20 if (LockCancelation())
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
21 HandleErrorInternal(error);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
22 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
23
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
24 protected void HandleErrorInternal(Exception error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
25 if (m_error != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
26 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
27 SetResult(m_error(error));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
28 } catch(Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
29 SetError(err);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
30 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
31 } else {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
32 SetError(error);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
33 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
34 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
35
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
36 public override void CancelOperation(Exception reason) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
37 if (LockCancelation()) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
38 if (m_cancel != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
39 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
40 SetResult(m_cancel(reason));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
41 } catch (Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
42 HandleErrorInternal(err);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
43 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
44 } else {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
45 SetCancelled(reason);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
46 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
47 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
48 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
49
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
50 protected bool LockCancelation() {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
51 return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
52 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
53 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
54 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
55