[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

@@ -3,9 +3,9 @@
// the WPILib BSD license file in the root directory of this project.
#include <jni.h>
#include <stdint.h>
#include <cassert>
#include <cstdio>
#include "HALUtil.hpp"
#include "org_wpilib_hardware_hal_NotifierJNI.h"
@@ -33,20 +33,7 @@ Java_org_wpilib_hardware_hal_NotifierJNI_createNotifier
return 0; // something went wrong in HAL
}
return (jint)notifierHandle;
}
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: setHALThreadPriority
* Signature: (ZI)Z
*/
JNIEXPORT jboolean JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_setHALThreadPriority
(JNIEnv* env, jclass, jboolean realTime, jint priority)
{
int32_t status = 0;
return HAL_SetNotifierThreadPriority(realTime, priority, &status);
return static_cast<jint>(notifierHandle);
}
/*
@@ -140,7 +127,7 @@ Java_org_wpilib_hardware_hal_NotifierJNI_getNotifierOverrun
CheckStatus(env, status);
return (jint)count;
return static_cast<jint>(count);
}
} // extern "C"

View File

@@ -3,6 +3,7 @@
// the WPILib BSD license file in the root directory of this project.
#include <jni.h>
#include <stdint.h>
#include <cassert>
@@ -13,6 +14,7 @@
using namespace wpi::hal;
extern "C" {
/*
* Class: org_wpilib_hardware_hal_ThreadsJNI
* Method: getCurrentThreadPriority
@@ -22,43 +24,25 @@ JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_ThreadsJNI_getCurrentThreadPriority
(JNIEnv* env, jclass)
{
int32_t status = 0;
HAL_Bool isRT = false;
auto ret = HAL_GetCurrentThreadPriority(&isRT, &status);
int32_t priority = 0;
HAL_Status status = HAL_GetCurrentThreadPriority(&priority);
CheckStatus(env, status);
return static_cast<jint>(ret);
}
/*
* Class: org_wpilib_hardware_hal_ThreadsJNI
* Method: getCurrentThreadIsRealTime
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL
Java_org_wpilib_hardware_hal_ThreadsJNI_getCurrentThreadIsRealTime
(JNIEnv* env, jclass)
{
int32_t status = 0;
HAL_Bool isRT = false;
HAL_GetCurrentThreadPriority(&isRT, &status);
CheckStatus(env, status);
return static_cast<jboolean>(isRT);
return static_cast<jint>(priority);
}
/*
* Class: org_wpilib_hardware_hal_ThreadsJNI
* Method: setCurrentThreadPriority
* Signature: (ZI)Z
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL
Java_org_wpilib_hardware_hal_ThreadsJNI_setCurrentThreadPriority
(JNIEnv* env, jclass, jboolean realTime, jint priority)
(JNIEnv* env, jclass, jint priority)
{
int32_t status = 0;
auto ret = HAL_SetCurrentThreadPriority(
static_cast<HAL_Bool>(realTime), static_cast<int32_t>(priority), &status);
HAL_Status status =
HAL_SetCurrentThreadPriority(static_cast<int32_t>(priority));
CheckStatus(env, status, false);
return static_cast<jboolean>(ret);
return static_cast<jboolean>(status);
}
} // extern "C"