[hal] Refactor threads API (#8701)

Since sched_setscheduler() requires non-RT priorities to be 0, we can
use that as a sentinel value for disabling RT and condense the Java API
to just two functions with fewer parameters. The thread priority setter
is deprecated since only experts should use it.

The HAL Notifier thread priority setter was replaced with setting the
priority in the thread itself.

The C++ Notifier non-RT and RT constructors were deduplicated.

The real-time scheduler was changed from SCHED_FIFO to SCHED_RR, which
is SCHED_FIFO with threads allowed to run for a maximum time quantum
before yielding (100 ms by default).
This commit is contained in:
Tyler Veness
2026-04-06 08:49:43 -07:00
committed by GitHub
parent cc56c42d4c
commit 173ecd3d02
27 changed files with 188 additions and 388 deletions

View File

@@ -16,7 +16,6 @@ import org.wpilib.math.util.MathSharedStore;
import org.wpilib.networktables.MultiSubscriber;
import org.wpilib.networktables.NetworkTableEvent;
import org.wpilib.networktables.NetworkTableInstance;
import org.wpilib.system.Notifier;
import org.wpilib.system.RuntimeType;
import org.wpilib.system.Timer;
import org.wpilib.system.WPILibVersion;
@@ -413,10 +412,6 @@ public abstract class RobotBase implements AutoCloseable {
HAL.reportUsage("Language", "Java");
HAL.reportUsage("WPILibVersion", WPILibVersion.Version);
if (!Notifier.setHALThreadPriority(true, 40)) {
DriverStation.reportWarning("Setting HAL Notifier RT priority to 40 failed", false);
}
if (HAL.hasMain()) {
Thread thread =
new Thread(

View File

@@ -64,8 +64,8 @@ package org.wpilib.framework;
* <p>If the robot periodic functions and the controller periodic functions have a lot of scheduling
* jitter that cause them to occasionally overlap with later timeslices, consider giving the main
* robot thread a real-time priority using {@link
* org.wpilib.system.Threads#setCurrentThreadPriority(boolean,int)}. An RT priority of 15 is a
* reasonable choice.
* org.wpilib.system.Threads#setCurrentThreadPriority(int)}. An RT priority of 15 is a reasonable
* choice.
*
* <p>If you do enable RT though, <i>make sure your periodic functions do not block</i>. If they do,
* the operating system will lock up, and you'll have to boot the roboRIO into safe mode and delete

View File

@@ -209,21 +209,4 @@ public class Notifier implements AutoCloseable {
public void stop() {
NotifierJNI.cancelNotifierAlarm(m_notifier.get(), false);
}
/**
* Sets the HAL notifier thread priority.
*
* <p>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);
}
}

View File

@@ -9,34 +9,31 @@ import org.wpilib.hardware.hal.ThreadsJNI;
/** Thread utility functions. */
public final class Threads {
/**
* Get the thread priority for the current thread.
* Gets the current thread's priority.
*
* @return The current thread priority. For real-time, this is 1-99 with 99 being highest. For
* non-real-time, this is 0. See "man 7 sched" for details.
* <p>Priorities range from 0 to 99 where 0 is non-real-time, and 1-99 are real-time, and 99 is
* highest priority. See "man 7 sched" for details.
*
* @return The current thread's priority.
*/
public static int getCurrentThreadPriority() {
return ThreadsJNI.getCurrentThreadPriority();
}
/**
* Get if the current thread is real-time.
* Sets the current thread's priority.
*
* @return If the current thread is real-time.
*/
public static boolean getCurrentThreadIsRealTime() {
return ThreadsJNI.getCurrentThreadIsRealTime();
}
/**
* Sets the thread priority for the current thread.
* <p>Priorities range from 0 to 99 where 0 is non-real-time, 1-99 are real-time, and 99 is
* highest priority. See "man 7 sched" for details.
*
* @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 details.
* @param priority The priority.
* @return True on success.
* @deprecated Incorrect usage of real-time priority can lead to system lockups. Only use this
* function if you are trained in real-time software development.
*/
public static boolean setCurrentThreadPriority(boolean realTime, int priority) {
return ThreadsJNI.setCurrentThreadPriority(realTime, priority);
@Deprecated
public static boolean setCurrentThreadPriority(int priority) {
return ThreadsJNI.setCurrentThreadPriority(priority);
}
private Threads() {}