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 36e98ca616..25628d6ab0 100644 --- a/hal/src/main/java/edu/wpi/first/hal/NotifierJNI.java +++ b/hal/src/main/java/edu/wpi/first/hal/NotifierJNI.java @@ -14,6 +14,9 @@ public class NotifierJNI extends JNIWrapper { /** Initializes the notifier. */ public static native int initializeNotifier(); + /** Sets the HAL notifier thread priority. */ + public static native boolean setHALThreadPriority(boolean realTime, int priority); + /** Sets the name of the notifier. */ public static native void setNotifierName(int notifierHandle, String name); diff --git a/hal/src/main/native/athena/Notifier.cpp b/hal/src/main/native/athena/Notifier.cpp index e672a96e78..2069e69da4 100644 --- a/hal/src/main/native/athena/Notifier.cpp +++ b/hal/src/main/native/athena/Notifier.cpp @@ -17,6 +17,7 @@ #include "hal/ChipObject.h" #include "hal/Errors.h" #include "hal/HAL.h" +#include "hal/Threads.h" #include "hal/handles/UnlimitedHandleResource.h" using namespace hal; @@ -156,6 +157,12 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { return handle; } +HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority, + int32_t* status) { + auto native = notifierThread.native_handle(); + return HAL_SetThreadPriority(&native, realTime, priority, status); +} + void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, int32_t* status) {} diff --git a/hal/src/main/native/cpp/jni/NotifierJNI.cpp b/hal/src/main/native/cpp/jni/NotifierJNI.cpp index 012174b55a..4e7069e694 100644 --- a/hal/src/main/native/cpp/jni/NotifierJNI.cpp +++ b/hal/src/main/native/cpp/jni/NotifierJNI.cpp @@ -36,6 +36,19 @@ Java_edu_wpi_first_hal_NotifierJNI_initializeNotifier return (jint)notifierHandle; } +/* + * Class: edu_wpi_first_hal_NotifierJNI + * Method: setHALThreadPriority + * Signature: (ZI)Z + */ +JNIEXPORT jboolean JNICALL +Java_edu_wpi_first_hal_NotifierJNI_setHALThreadPriority + (JNIEnv* env, jclass, jboolean realTime, jint priority) +{ + int32_t status = 0; + return HAL_SetNotifierThreadPriority(realTime, priority, &status); +} + /* * Class: edu_wpi_first_hal_NotifierJNI * Method: setNotifierName diff --git a/hal/src/main/native/include/hal/Notifier.h b/hal/src/main/native/include/hal/Notifier.h index 62b6242cf3..f835b884cf 100644 --- a/hal/src/main/native/include/hal/Notifier.h +++ b/hal/src/main/native/include/hal/Notifier.h @@ -30,6 +30,25 @@ extern "C" { */ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status); +/** + * Sets the HAL notifier thread priority. + * + * The HAL notifier thread is responsible for managing the FPGA's notifier + * interrupt and waking up user's Notifiers when it's their time to run. + * Giving the HAL notifier thread real-time priority helps ensure the user's + * real-time Notifiers, if any, are notified to run in a timely manner. + * + * @param realTime Set to true to set a real-time priority, false for standard + * priority. + * @param priority Priority to set the thread to. For real-time, this is 1-99 + * with 99 being highest. For non-real-time, this is forced to + * 0. See "man 7 sched" for more details. + * @param status Error status variable. 0 on success. + * @return True on success. + */ +HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority, + int32_t* status); + /** * Sets the name of a notifier. * diff --git a/hal/src/main/native/sim/Notifier.cpp b/hal/src/main/native/sim/Notifier.cpp index 3459b6cdce..49f1c80514 100644 --- a/hal/src/main/native/sim/Notifier.cpp +++ b/hal/src/main/native/sim/Notifier.cpp @@ -174,6 +174,11 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { return handle; } +HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority, + int32_t* status) { + return true; +} + void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, int32_t* status) { auto notifier = notifierHandles->Get(notifierHandle); diff --git a/wpilibc/src/main/native/cpp/Notifier.cpp b/wpilibc/src/main/native/cpp/Notifier.cpp index 5b6b56f8aa..72f8a1d08c 100644 --- a/wpilibc/src/main/native/cpp/Notifier.cpp +++ b/wpilibc/src/main/native/cpp/Notifier.cpp @@ -200,3 +200,8 @@ void Notifier::UpdateAlarm(uint64_t triggerTime) { void Notifier::UpdateAlarm() { UpdateAlarm(static_cast(m_expirationTime * 1e6)); } + +bool Notifier::SetHALThreadPriority(bool realTime, int32_t priority) { + int32_t status = 0; + return HAL_SetNotifierThreadPriority(realTime, priority, &status); +} diff --git a/wpilibc/src/main/native/include/frc/Notifier.h b/wpilibc/src/main/native/include/frc/Notifier.h index 44142a4151..df8c37ca53 100644 --- a/wpilibc/src/main/native/include/frc/Notifier.h +++ b/wpilibc/src/main/native/include/frc/Notifier.h @@ -143,6 +143,23 @@ class Notifier : public ErrorBase { */ void Stop(); + /** + * Sets the HAL notifier thread priority. + * + * The HAL notifier thread is responsible for managing the FPGA's notifier + * interrupt and waking up user's Notifiers when it's their time to run. + * Giving the HAL notifier thread real-time priority helps ensure the user's + * real-time Notifiers, if any, are notified to run in a timely manner. + * + * @param realTime Set to true to set a real-time priority, false for standard + * priority. + * @param priority Priority to set the thread to. For real-time, this is 1-99 + * with 99 being highest. For non-real-time, this is forced to + * 0. See "man 7 sched" for more details. + * @return True on success. + */ + static bool SetHALThreadPriority(bool realTime, int32_t priority); + private: /** * Update the HAL alarm time. 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 ead2102b6f..e6b1790f49 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Notifier.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Notifier.java @@ -213,4 +213,21 @@ public class Notifier implements AutoCloseable { m_processLock.unlock(); } } + + /** + * Sets the HAL notifier thread priority. + * + *

The HAL notifier thread is responsible for managing the FPGA's notifier interrupt and waking + * up user's Notifiers when it's their time to run. Giving the HAL notifier thread real-time + * priority helps ensure the user's real-time Notifiers, if any, are notified to run in a timely + * manner. + * + * @param realTime Set to true to set a real-time priority, false for standard priority. + * @param priority Priority to set the thread to. For real-time, this is 1-99 with 99 being + * highest. For non-real-time, this is forced to 0. See "man 7 sched" for more details. + * @return True on success. + */ + public static boolean setHALThreadPriority(boolean realTime, int priority) { + return NotifierJNI.setHALThreadPriority(realTime, priority); + } }