ygerzhedovich commented on code in PR #4615:
URL: https://github.com/apache/ignite-3/pull/4615#discussion_r1832263447


##########
modules/api/src/main/java/org/apache/ignite/lang/CancelHandleImpl.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.lang;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Implementation of {@link CancelHandle}. */
+final class CancelHandleImpl implements CancelHandle {
+
+    private final CompletableFuture<Void> cancelFut = new 
CompletableFuture<>();
+
+    private final CancellationTokenImpl token;
+
+    CancelHandleImpl() {
+        this.token = new CancellationTokenImpl(this);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void cancel() {
+        doCancelAsync();
+
+        cancelFut.join();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<Void> cancelAsync() {
+        doCancelAsync();
+
+        return cancelFut;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean isCancelled() {
+        return token.isCancelled();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CancellationToken token() {
+        return token;
+    }
+
+    private void doCancelAsync() {
+        token.cancel().whenComplete((r, t) -> {
+            if (t != null) {
+                cancelFut.completeExceptionally(t);
+            } else {
+                cancelFut.complete(null);
+            }
+        });
+    }
+
+    static final class CancellationTokenImpl implements CancellationToken {
+
+        private final CancelHandleImpl handle;
+
+        // Actions that trigger cancellations - action that triggers a 
cancellation + a future that completes, when a resource is closed.
+        private final ConcurrentLinkedQueue<Cancellation> cancellations = new 
ConcurrentLinkedQueue<>();
+
+        private final AtomicReference<CompletableFuture<Void>> cancelFutRef = 
new AtomicReference<>();
+
+        CancellationTokenImpl(CancelHandleImpl handle) {
+            this.handle = handle;
+        }
+
+        boolean isCancelled() {
+            return cancelFutRef.get() != null;
+        }
+
+        void addCancelAction(Runnable cancelAction, CompletableFuture<?> 
completionFut) {
+            assert cancelAction != null : "cancelAction must be not null";
+            assert completionFut != null : "completionFut must be not null";
+
+            Cancellation cancellation = new Cancellation(cancelAction, 
completionFut);
+            if (handle.isCancelled()) {
+                cancellation.run();
+            } else {
+                cancellations.add(cancellation);

Review Comment:
   It looks like we can add a new cancelation right after the cancellation is 
invoked and lose the new action



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to