From f121ccff0dfb57015a4ae846d067549e5e894fd6 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Tue, 8 Jan 2019 21:37:59 -0600 Subject: [PATCH] Avoid Watchdog thread clobbering m_isExpired flag after callback (#1527) --- wpilibc/src/main/native/cpp/Watchdog.cpp | 7 ++++++- wpilibj/src/main/java/edu/wpi/first/wpilibj/Watchdog.java | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/wpilibc/src/main/native/cpp/Watchdog.cpp b/wpilibc/src/main/native/cpp/Watchdog.cpp index af83617e46..b67f94dfd1 100644 --- a/wpilibc/src/main/native/cpp/Watchdog.cpp +++ b/wpilibc/src/main/native/cpp/Watchdog.cpp @@ -58,10 +58,15 @@ void Watchdog::Thread::Main() { << "s\n"; } } + + // Set expiration flag before calling the callback so any manipulation + // of the flag in the callback (e.g., calling Disable()) isn't + // clobbered. + watchdog->m_isExpired = true; + lock.unlock(); watchdog->m_callback(); lock.lock(); - watchdog->m_isExpired = true; } // Otherwise, a Watchdog removed itself from the queue (it notifies the // scheduler of this) or a spurious wakeup occurred, so just rewait with diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Watchdog.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Watchdog.java index 1a6af68511..7ff8937b6c 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Watchdog.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Watchdog.java @@ -247,10 +247,15 @@ public class Watchdog implements Closeable, Comparable { System.out.format("Watchdog not fed within %.6fs\n", watchdog.m_timeout / 1.0e6); } } + + // Set expiration flag before calling the callback so any + // manipulation of the flag in the callback (e.g., calling + // Disable()) isn't clobbered. + watchdog.m_isExpired = true; + m_queueMutex.unlock(); watchdog.m_callback.run(); m_queueMutex.lock(); - watchdog.m_isExpired = true; } // Otherwise, a Watchdog removed itself from the queue (it notifies // the scheduler of this) or a spurious wakeup occurred, so just