diff --git a/hal/src/main/java/edu/wpi/first/hal/NotifierJNI.java b/hal/src/main/java/edu/wpi/first/hal/NotifierJNI.java index bf92c37c1a..000c7d6041 100644 --- a/hal/src/main/java/edu/wpi/first/hal/NotifierJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/NotifierJNI.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -19,6 +19,11 @@ public class NotifierJNI extends JNIWrapper { */ public static native int initializeNotifier(); + /** + * Sets the name of the notifier. + */ + public static native void setNotifierName(int notifierHandle, String name); + /** * Wakes up the waiter with time=0. Note: after this function is called, all * calls to waitForNotifierAlarm() will immediately start returning 0. diff --git a/hal/src/main/native/athena/Notifier.cpp b/hal/src/main/native/athena/Notifier.cpp index 662b04e155..c30e8d1048 100644 --- a/hal/src/main/native/athena/Notifier.cpp +++ b/hal/src/main/native/athena/Notifier.cpp @@ -139,6 +139,9 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { return handle; } +void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, + int32_t* status) {} + void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { auto notifier = notifierHandles->Get(notifierHandle); if (!notifier) return; diff --git a/hal/src/main/native/cpp/jni/NotifierJNI.cpp b/hal/src/main/native/cpp/jni/NotifierJNI.cpp index 588614e8be..874d750a82 100644 --- a/hal/src/main/native/cpp/jni/NotifierJNI.cpp +++ b/hal/src/main/native/cpp/jni/NotifierJNI.cpp @@ -10,6 +10,8 @@ #include #include +#include + #include "HALUtil.h" #include "edu_wpi_first_hal_NotifierJNI.h" #include "hal/Notifier.h" @@ -37,6 +39,21 @@ Java_edu_wpi_first_hal_NotifierJNI_initializeNotifier return (jint)notifierHandle; } +/* + * Class: edu_wpi_first_hal_NotifierJNI + * Method: setNotifierName + * Signature: (ILjava/lang/String;)V + */ +JNIEXPORT void JNICALL +Java_edu_wpi_first_hal_NotifierJNI_setNotifierName + (JNIEnv* env, jclass cls, jint notifierHandle, jstring name) +{ + int32_t status = 0; + HAL_SetNotifierName((HAL_NotifierHandle)notifierHandle, + wpi::java::JStringRef{env, name}.c_str(), &status); + CheckStatus(env, status); +} + /* * Class: edu_wpi_first_hal_NotifierJNI * Method: stopNotifier diff --git a/hal/src/main/native/include/hal/Notifier.h b/hal/src/main/native/include/hal/Notifier.h index 27f20e39ca..6b8e39ffb9 100644 --- a/hal/src/main/native/include/hal/Notifier.h +++ b/hal/src/main/native/include/hal/Notifier.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -31,6 +31,15 @@ extern "C" { */ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status); +/** + * Sets the name of a notifier. + * + * @param notifierHandle the notifier handle + * @param name name + */ +void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, + int32_t* status); + /** * Stops a notifier from running. * diff --git a/hal/src/main/native/sim/Notifier.cpp b/hal/src/main/native/sim/Notifier.cpp index 579540c2c6..21f24b8280 100644 --- a/hal/src/main/native/sim/Notifier.cpp +++ b/hal/src/main/native/sim/Notifier.cpp @@ -8,6 +8,7 @@ #include "hal/Notifier.h" #include +#include #include #include @@ -20,6 +21,7 @@ namespace { struct Notifier { + std::string name; uint64_t waitTime; bool updatedAlarm = false; bool active = true; @@ -71,6 +73,14 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { return handle; } +void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, + int32_t* status) { + auto notifier = notifierHandles->Get(notifierHandle); + if (!notifier) return; + std::scoped_lock lock(notifier->mutex); + notifier->name = name; +} + void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { auto notifier = notifierHandles->Get(notifierHandle); if (!notifier) return; diff --git a/wpilibc/src/main/native/cpp/Notifier.cpp b/wpilibc/src/main/native/cpp/Notifier.cpp index 0fc746555c..a7fa0381bd 100644 --- a/wpilibc/src/main/native/cpp/Notifier.cpp +++ b/wpilibc/src/main/native/cpp/Notifier.cpp @@ -11,6 +11,7 @@ #include #include +#include #include "frc/Timer.h" #include "frc/Utility.h" @@ -91,6 +92,13 @@ Notifier& Notifier::operator=(Notifier&& rhs) { return *this; } +void Notifier::SetName(const wpi::Twine& name) { + wpi::SmallString<64> nameBuf; + int32_t status = 0; + HAL_SetNotifierName(m_notifier, + name.toNullTerminatedStringRef(nameBuf).data(), &status); +} + void Notifier::SetHandler(std::function handler) { std::scoped_lock lock(m_processMutex); m_handler = handler; diff --git a/wpilibc/src/main/native/cpp/TimedRobot.cpp b/wpilibc/src/main/native/cpp/TimedRobot.cpp index b69a68f86c..a9358dde31 100644 --- a/wpilibc/src/main/native/cpp/TimedRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimedRobot.cpp @@ -60,6 +60,7 @@ TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) { int32_t status = 0; m_notifier = HAL_InitializeNotifier(&status); wpi_setHALError(status); + HAL_SetNotifierName(m_notifier, "TimedRobot", &status); HAL_Report(HALUsageReporting::kResourceType_Framework, HALUsageReporting::kFramework_Timed); diff --git a/wpilibc/src/main/native/include/frc/Notifier.h b/wpilibc/src/main/native/include/frc/Notifier.h index f2f37f1154..24ffba35d3 100644 --- a/wpilibc/src/main/native/include/frc/Notifier.h +++ b/wpilibc/src/main/native/include/frc/Notifier.h @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -46,6 +47,13 @@ class Notifier : public ErrorBase { Notifier(Notifier&& rhs); Notifier& operator=(Notifier&& rhs); + /** + * Sets the name of the notifier. Used for debugging purposes only. + * + * @param name Name + */ + void SetName(const wpi::Twine& name); + /** * Change the handler function. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Notifier.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Notifier.java index e1d6bb69b3..806bfa9899 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Notifier.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Notifier.java @@ -138,6 +138,16 @@ public class Notifier implements AutoCloseable { m_thread.start(); } + /** + * Sets the name of the notifier. Used for debugging purposes only. + * + * @param name Name + */ + public void setName(String name) { + m_thread.setName(name); + NotifierJNI.setNotifierName(m_notifier.get(), name); + } + /** * Change the handler function. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java index 2167fbc2d6..b6b6b25d41 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/TimedRobot.java @@ -43,6 +43,7 @@ public class TimedRobot extends IterativeRobotBase { */ protected TimedRobot(double period) { super(period); + NotifierJNI.setNotifierName(m_notifier, "TimedRobot"); HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Timed); }