From 3b8d8a367b49bea28d5423dca53f913c13ff6936 Mon Sep 17 00:00:00 2001 From: Dean Brettle Date: Mon, 27 May 2024 07:58:03 -0700 Subject: [PATCH] [wpilibj] Make CallbackStore.close() idempotent (#6666) This is "strongly recommended" in the javadocs for the AutoCloseable.close(). --- .../edu/wpi/first/wpilibj/simulation/CallbackStore.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/CallbackStore.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/CallbackStore.java index 89a460fb7e..69daa87ff8 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/CallbackStore.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/CallbackStore.java @@ -69,6 +69,7 @@ public class CallbackStore implements AutoCloseable { private CancelCallbackFunc m_cancelCallback; private CancelCallbackChannelFunc m_cancelCallbackChannel; private CancelCallbackNoIndexFunc m_cancelCallbackNoIndex; + private static final int kAlreadyCancelled = -1; private static final int kNormalCancel = 0; private static final int kChannelCancel = 1; private static final int kNoIndexCancel = 2; @@ -78,6 +79,9 @@ public class CallbackStore implements AutoCloseable { @Override public void close() { switch (m_cancelType) { + case kAlreadyCancelled: + // Already cancelled so do nothing so that close() is idempotent. + return; case kNormalCancel: m_cancelCallback.cancel(m_index, m_uid); break; @@ -91,6 +95,6 @@ public class CallbackStore implements AutoCloseable { assert false; break; } - m_cancelType = -1; + m_cancelType = kAlreadyCancelled; } }