diff Implab/AbstractTaskT.cs @ 190:1c2a16d071a7 v2

Слияние с ref20160224
author cin
date Fri, 22 Apr 2016 13:08:08 +0300
parents dd4a3590f9c6
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/AbstractTaskT.cs	Fri Apr 22 13:08:08 2016 +0300
@@ -0,0 +1,36 @@
+using System;
+using System.Threading;
+
+namespace Implab {
+    public abstract class AbstractTask<T> : AbstractPromise<T> {
+        int m_cancelationLock;
+
+        /// <summary>
+        /// Получает эксклюзивное право отмены задания, используется для отмены задания до начала его выполнения.
+        /// </summary>
+        /// <returns><c>true</c>, if cancelation was locked, <c>false</c> otherwise.</returns>
+        protected bool LockCancelation() {
+            return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
+        }
+
+
+
+        protected void SetErrorInternal(Exception error) {
+            // unwrap 
+            while (error is PromiseTransientException && error.InnerException != null)
+                error = error.InnerException;
+
+            if (error is OperationCanceledException)
+                SetCancelled(error);
+            else
+                SetError(error);
+        }
+
+        protected void SetCancelledInternal(Exception reason) {
+            SetCancelled(
+                reason == null ? new OperationCanceledException() : reason is OperationCanceledException ? reason : new OperationCanceledException(null, reason)
+            );
+        }
+    }
+}
+