Use compareAndExchange() to set closeFuture 72/109272/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 9 Dec 2023 10:51:14 +0000 (11:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 10 Dec 2023 10:44:02 +0000 (11:44 +0100)
Java 9+ gives us a convenient way to ensure null-safety in this CAS
operation -- compareAndExchange() returns the witness value, i.e.
we get the value instead of a plain boolean false.

Change-Id: I636b0f084170d42bbd99490577917afefe5145e2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit b61b30a4b2be38c8629bf9c5d66d05609db3be1c)

singleton-service/mdsal-singleton-dom-impl/src/main/java/org/opendaylight/mdsal/singleton/dom/impl/ClusterSingletonServiceGroupImpl.java

index 37fa755c88a70ff7eb6781b57bd6712d23ddd42e..d73df7b3858ec74b36065584acdb1192fc37fc31 100644 (file)
@@ -307,9 +307,10 @@ final class ClusterSingletonServiceGroupImpl<P extends HierarchicalIdentifier<P>
     }
 
     private synchronized @NonNull ListenableFuture<?> destroyGroup() {
-        final SettableFuture<Void> future = SettableFuture.create();
-        if (!closeFuture.compareAndSet(null, future)) {
-            return verifyNotNull(closeFuture.get());
+        final var future = SettableFuture.<Void>create();
+        final var witness = closeFuture.compareAndExchange(null, future);
+        if (witness != null) {
+            return witness;
         }
 
         if (serviceEntityReg != null) {