diff --git a/hal/src/main/native/athena/Notifier.cpp b/hal/src/main/native/athena/Notifier.cpp index a74718a41e..d684031208 100644 --- a/hal/src/main/native/athena/Notifier.cpp +++ b/hal/src/main/native/athena/Notifier.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -27,6 +28,8 @@ static constexpr int32_t kTimerInterruptNumber = 28; static wpi::mutex notifierMutex; static std::unique_ptr notifierAlarm; static std::thread notifierThread; +static HAL_Bool notifierThreadRealTime = false; +static int32_t notifierThreadPriority = 0; static uint64_t closestTrigger{UINT64_MAX}; namespace { @@ -147,6 +150,21 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { std::scoped_lock lock(notifierMutex); notifierRunning = true; notifierThread = std::thread(notifierThreadMain); + + auto native = notifierThread.native_handle(); + HAL_SetThreadPriority(&native, notifierThreadRealTime, + notifierThreadPriority, status); + if (*status == HAL_THREAD_PRIORITY_ERROR) { + *status = 0; + fmt::print("{}: HAL Notifier thread\n", + HAL_THREAD_PRIORITY_ERROR_MESSAGE); + } + if (*status == HAL_THREAD_PRIORITY_RANGE_ERROR) { + *status = 0; + fmt::print("{}: HAL Notifier thread\n", + HAL_THREAD_PRIORITY_RANGE_ERROR_MESSAGE); + } + notifierAlarm.reset(tAlarm::create(status)); } @@ -161,8 +179,15 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { 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); + std::scoped_lock lock(notifierMutex); + notifierThreadRealTime = realTime; + notifierThreadPriority = priority; + if (notifierThread.joinable()) { + auto native = notifierThread.native_handle(); + return HAL_SetThreadPriority(&native, realTime, priority, status); + } else { + return true; + } } void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, diff --git a/hal/src/main/native/include/hal/Errors.h b/hal/src/main/native/include/hal/Errors.h index 316c4b8fbd..724a60c19d 100644 --- a/hal/src/main/native/include/hal/Errors.h +++ b/hal/src/main/native/include/hal/Errors.h @@ -119,7 +119,7 @@ #define HAL_THREAD_PRIORITY_ERROR -1152 #define HAL_THREAD_PRIORITY_ERROR_MESSAGE \ - "HAL: Getting or setting the priority of a thread has failed"; + "HAL: Getting or setting the priority of a thread has failed" #define HAL_THREAD_PRIORITY_RANGE_ERROR -1153 #define HAL_THREAD_PRIORITY_RANGE_ERROR_MESSAGE \ @@ -136,14 +136,13 @@ "HAL: Use HAL_GetLastError(status) to get last error" #define HAL_SETUID_ERROR -1157 -#define HAL_SETUID_ERROR_MESSAGE \ - "HAL: Setting the effective user ID has failed"; +#define HAL_SETUID_ERROR_MESSAGE "HAL: Setting the effective user ID has failed" #define HAL_CAN_BUFFER_OVERRUN -35007 #define HAL_CAN_BUFFER_OVERRUN_MESSAGE \ "HAL: CAN Output Buffer Full. Ensure a device is attached" -#define VI_ERROR_SYSTEM_ERROR_MESSAGE "HAL - VISA: System Error"; +#define VI_ERROR_SYSTEM_ERROR_MESSAGE "HAL - VISA: System Error" #define VI_ERROR_INV_OBJECT_MESSAGE "HAL - VISA: Invalid Object" #define VI_ERROR_RSRC_LOCKED_MESSAGE "HAL - VISA: Resource Locked" #define VI_ERROR_RSRC_NFOUND_MESSAGE "HAL - VISA: Resource Not Found"