[hal] Revamp notifiers (#8424)

This changes the HAL notifier interface to:
- Use wpiutil signal objects. This means waiting is done through the
`WPI_WaitObject` API instead of a dedicated function and allows for
higher level code to simultaneously wait on notifiers and other events.
- Interval timers are supported at the HAL layer
- Handlers are now required to acknowledge notifications. This is
invisible to users unless they're directly using the HAL API.
- For interval timers, an overrun count is maintained to detect if the
handler didn't acknowledge

The underlying implementation still uses condition variables for the
actual waiting. In basic testing using this approach seemed to be lower
jitter than timerfd.

Currently, the simulation and systemcore implementations are nearly
identical except for a few additional sim hook bits. This could be
refactored, but keeping them separate may make sense to keep the
systemcore implementation easy to read and reason about, or if we ever
choose to use a different underlying timer implementation on systemcore.

The simulation side API is unchanged in form but does change in
function--waiting for notifiers now only waits for currently running (or
newly signaled) notifiers to acknowledge. To avoid a race condition in
sim stepTiming, users of the low level API must make any alarm updates
(especially for one-shot alarms) prior to acknowledging the previous
alarm.

The only current use of the interval timer feature is the `Notifier`
class. The `TimedRobot` implementation still uses a single notifier and
its own interval timing logic to ensure consistent callback order. Using
separate notifiers for each user-level interval would substantially
increase complexity. `Watchdog` also doesn't use the interval timer, as
it's looking for an amount of time since the last `set` call rather than
a recurring interval time.

To reduce flicker, the sim GUI uses a fade out when a timeout goes from
set to unset.

This fixes tsan for wpilib and commands, and also fixes some spurious
test failures.
This commit is contained in:
Peter Johnson
2025-11-29 11:00:18 -08:00
committed by GitHub
parent 704b6ccaee
commit 02c8d5c9db
22 changed files with 738 additions and 712 deletions

View File

@@ -11,6 +11,7 @@
#include "org_wpilib_hardware_hal_NotifierJNI.h"
#include "wpi/hal/Notifier.h"
#include "wpi/util/jni_util.hpp"
#include "wpi/util/string.h"
using namespace wpi::hal;
@@ -18,15 +19,15 @@ extern "C" {
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: initializeNotifier
* Method: createNotifier
* Signature: ()I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_initializeNotifier
Java_org_wpilib_hardware_hal_NotifierJNI_createNotifier
(JNIEnv* env, jclass)
{
int32_t status = 0;
HAL_NotifierHandle notifierHandle = HAL_InitializeNotifier(&status);
HAL_NotifierHandle notifierHandle = HAL_CreateNotifier(&status);
if (notifierHandle <= 0 || !CheckStatusForceThrow(env, status)) {
return 0; // something went wrong in HAL
@@ -58,51 +59,40 @@ Java_org_wpilib_hardware_hal_NotifierJNI_setNotifierName
(JNIEnv* env, jclass cls, jint notifierHandle, jstring name)
{
int32_t status = 0;
HAL_SetNotifierName((HAL_NotifierHandle)notifierHandle,
wpi::util::java::JStringRef{env, name}.c_str(), &status);
wpi::util::java::JStringRef jname{env, name};
WPI_String wpiName = wpi::util::make_string(jname);
HAL_SetNotifierName((HAL_NotifierHandle)notifierHandle, &wpiName, &status);
CheckStatus(env, status);
}
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: stopNotifier
* Method: destroyNotifier
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_stopNotifier
(JNIEnv* env, jclass cls, jint notifierHandle)
{
int32_t status = 0;
HAL_StopNotifier((HAL_NotifierHandle)notifierHandle, &status);
CheckStatus(env, status);
}
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: cleanNotifier
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_cleanNotifier
Java_org_wpilib_hardware_hal_NotifierJNI_destroyNotifier
(JNIEnv* env, jclass, jint notifierHandle)
{
if (notifierHandle != HAL_kInvalidHandle) {
HAL_CleanNotifier((HAL_NotifierHandle)notifierHandle);
HAL_DestroyNotifier((HAL_NotifierHandle)notifierHandle);
}
}
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: updateNotifierAlarm
* Signature: (IJ)V
* Method: setNotifierAlarm
* Signature: (IJJZ)V
*/
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_updateNotifierAlarm
(JNIEnv* env, jclass cls, jint notifierHandle, jlong triggerTime)
Java_org_wpilib_hardware_hal_NotifierJNI_setNotifierAlarm
(JNIEnv* env, jclass cls, jint notifierHandle, jlong alarmTime,
jlong intervalTime, jboolean absolute)
{
int32_t status = 0;
HAL_UpdateNotifierAlarm((HAL_NotifierHandle)notifierHandle,
static_cast<uint64_t>(triggerTime), &status);
HAL_SetNotifierAlarm((HAL_NotifierHandle)notifierHandle,
static_cast<uint64_t>(alarmTime),
static_cast<uint64_t>(intervalTime), absolute, &status);
CheckStatus(env, status);
}
@@ -122,20 +112,35 @@ Java_org_wpilib_hardware_hal_NotifierJNI_cancelNotifierAlarm
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: waitForNotifierAlarm
* Signature: (I)J
* Method: acknowledgeNotifierAlarm
* Signature: (I)V
*/
JNIEXPORT jlong JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_waitForNotifierAlarm
JNIEXPORT void JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_acknowledgeNotifierAlarm
(JNIEnv* env, jclass cls, jint notifierHandle)
{
int32_t status = 0;
uint64_t time =
HAL_WaitForNotifierAlarm((HAL_NotifierHandle)notifierHandle, &status);
HAL_AcknowledgeNotifierAlarm((HAL_NotifierHandle)notifierHandle, &status);
CheckStatus(env, status);
}
/*
* Class: org_wpilib_hardware_hal_NotifierJNI
* Method: getNotifierOverrun
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_org_wpilib_hardware_hal_NotifierJNI_getNotifierOverrun
(JNIEnv* env, jclass cls, jint notifierHandle)
{
int32_t status = 0;
int32_t count =
HAL_GetNotifierOverrun((HAL_NotifierHandle)notifierHandle, &status);
CheckStatus(env, status);
return (jlong)time;
return (jint)count;
}
} // extern "C"