diff Implab/AbstractEvent.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents 8c0b95069066
children e6d4b41f0101
line wrap: on
line diff
--- a/Implab/AbstractEvent.cs	Fri Mar 06 15:45:26 2015 +0300
+++ b/Implab/AbstractEvent.cs	Sun Mar 08 02:52:27 2015 +0300
@@ -4,7 +4,7 @@
 using System.Reflection;
 
 namespace Implab {
-    public abstract class AbstractEvent<THandler> : ICancelationToken, ICancellable { 
+    public abstract class AbstractEvent<THandler> : ICancellationToken, ICancellable { 
 
         const int UNRESOLVED_SATE = 0;
         const int TRANSITIONAL_STATE = 1;
@@ -280,31 +280,34 @@
             }
         }
 
-        public bool AcceptIfRequested() {
-            if (IsCancelRequested)
-                CancelOperation(CancelReason);
+        public bool CancelOperationIfRequested() {
+            if (IsCancellationRequested) {
+                CancelOperation(CancellationReason);
+                return true;
+            }
+            return false;
         }
 
         public virtual void CancelOperation(Exception reason) {
             SetCancelled(reason);
         }
 
-        public void CancelationRequested(Action<Exception> handler) {
+        public void CancellationRequested(Action<Exception> handler) {
             Safe.ArgumentNotNull(handler, "handler");
-            if (IsCancelRequested)
-                handler(CancelReason);
+            if (IsCancellationRequested)
+                handler(CancellationReason);
 
             if (m_cancelationHandlers == null)
                 Interlocked.CompareExchange(ref m_cancelationHandlers, new MTQueue<Action<Exception>>(), null);
 
             m_cancelationHandlers.Enqueue(handler);
 
-            if (IsCancelRequested && m_cancelationHandlers.TryDequeue(out handler))
+            if (IsCancellationRequested && m_cancelationHandlers.TryDequeue(out handler))
                 // TryDeque implies MemoryBarrier()
                 handler(m_cancelationReason);
         }
 
-        public bool IsCancelRequested {
+        public bool IsCancellationRequested {
             get {
                 do {
                     if (m_cancelRequest == CANCEL_NOT_REQUESTED)
@@ -316,7 +319,7 @@
             }
         }
 
-        public Exception CancelReason {
+        public Exception CancellationReason {
             get {
                 do {
                     Thread.MemoryBarrier();
@@ -333,7 +336,7 @@
         }
 
         public void Cancel(Exception reason) {
-            if (CANCEL_NOT_REQUESTED == Interlocked.CompareExchange(ref m_cancelRequest, CANCEL_REQUESTING)) {
+            if (CANCEL_NOT_REQUESTED == Interlocked.CompareExchange(ref m_cancelRequest, CANCEL_REQUESTING, CANCEL_NOT_REQUESTED)) {
                 m_cancelationReason = reason;
                 m_cancelRequest = CANCEL_REQUESTED;
                 if (m_cancelationHandlers != null) {