mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[hal] Update waitForProgramStart to optionally wait for first notifier (#8932)
This addresses a race condition caused by TimedRobot and other frameworks not creating their first notifier alarm until after signaling program start. Default this to true because it's the most common desired use case when combined with TimedRobot.
This commit is contained in:
@@ -141,13 +141,13 @@ Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_setRuntimeType
|
||||
/*
|
||||
* Class: org_wpilib_hardware_hal_simulation_SimulatorJNI
|
||||
* Method: waitForProgramStart
|
||||
* Signature: ()V
|
||||
* Signature: (Z)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_org_wpilib_hardware_hal_simulation_SimulatorJNI_waitForProgramStart
|
||||
(JNIEnv*, jclass)
|
||||
(JNIEnv*, jclass, jboolean waitForFirstNotifier)
|
||||
{
|
||||
HALSIM_WaitForProgramStart();
|
||||
HALSIM_WaitForProgramStart(waitForFirstNotifier);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -13,7 +13,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
void HALSIM_SetRuntimeType(HAL_RuntimeType type);
|
||||
void HALSIM_WaitForProgramStart(void);
|
||||
void HALSIM_WaitForProgramStart(HAL_Bool waitForFirstNotifier);
|
||||
void HALSIM_SetProgramStarted(HAL_Bool started);
|
||||
HAL_Bool HALSIM_GetProgramStarted(void);
|
||||
void HALSIM_SetProgramState(HAL_ControlWord controlWord);
|
||||
|
||||
@@ -77,7 +77,7 @@ bool GetProgramStarted() {
|
||||
using namespace wpi::hal;
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_WaitForProgramStart(void) {
|
||||
void HALSIM_WaitForProgramStart(HAL_Bool waitForFirstNotifier) {
|
||||
int count = 0;
|
||||
while (!programStarted) {
|
||||
count++;
|
||||
@@ -86,6 +86,17 @@ void HALSIM_WaitForProgramStart(void) {
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
// Frameworks observe program start before arming their first notifier alarm.
|
||||
// Wait for that alarm so a following StepTiming() can see and service it.
|
||||
while (waitForFirstNotifier &&
|
||||
HALSIM_GetNextNotifierTimeout() == UINT64_MAX) {
|
||||
count++;
|
||||
if (count % 10 == 0) {
|
||||
wpi::util::print("Waiting for first notifier alarm: {}\n", count);
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
void HALSIM_SetProgramStarted(HAL_Bool started) {
|
||||
|
||||
@@ -8,7 +8,7 @@ extern "C" {
|
||||
|
||||
void HALSIM_SetRuntimeType(HAL_RuntimeType type) {}
|
||||
|
||||
void HALSIM_WaitForProgramStart(void) {}
|
||||
void HALSIM_WaitForProgramStart(HAL_Bool waitForFirstNotifier) {}
|
||||
|
||||
void HALSIM_SetProgramStarted(HAL_Bool started) {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user