[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

@@ -24,22 +24,6 @@ public class NotifierJNI extends JNIWrapper {
*/
public static native int createNotifier();
/**
* Sets the HAL notifier thread priority.
*
* <p>The HAL notifier thread is responsible for managing the system'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.
* @see "HAL_SetNotifierThreadPriority"
*/
public static native boolean setHALThreadPriority(boolean realTime, int priority);
/**
* Sets the name of the notifier.
*

View File

@@ -11,32 +11,27 @@ package org.wpilib.hardware.hal;
*/
public class ThreadsJNI extends JNIWrapper {
/**
* Gets 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, 1-99 are real-time, and 99 is
* highest priority. See "man 7 sched" for details.
*
* @return The current thread's priority.
* @see "HAL_GetCurrentThreadPriority"
*/
public static native int getCurrentThreadPriority();
/**
* Gets the real-time status for the current thread.
* Sets the current thread's priority.
*
* @return Set to true if thread is real-time, otherwise false.
* @see "HAL_GetCurrentThreadPriority"
*/
public static native boolean 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 more details.
* @param priority The priority.
* @return True on success.
* @see "HAL_SetCurrentThreadPriority"
*/
public static native boolean setCurrentThreadPriority(boolean realTime, int priority);
public static native boolean setCurrentThreadPriority(int priority);
/** Utility class. */
private ThreadsJNI() {}

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"

View File

@@ -30,25 +30,6 @@ extern "C" {
*/
HAL_NotifierHandle HAL_CreateNotifier(int32_t* status);
/**
* Sets the HAL notifier thread priority.
*
* The HAL notifier thread is responsible for managing the system'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[in] realTime Set to true to set a real-time priority, false for
* standard priority.
* @param[in] 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.
* @param[out] status Error status variable. 0 on success.
* @return True on success.
*/
HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status);
/**
* Sets the name of a notifier.
*

View File

@@ -4,6 +4,8 @@
#pragma once
#include <stdint.h>
#include "wpi/hal/Types.h"
/**
@@ -19,58 +21,50 @@ extern "C" {
#endif
/**
* Gets the thread priority for the specified thread.
* Gets the specified thread's priority.
*
* @param[in] handle Native handle pointer to the thread to get the
* priority for.
* @param[out] isRealTime Set to true if thread is real-time, otherwise false.
* @param[out] status Error status variable. 0 on success.
* @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.
* 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[in] handle Native thread handle.
* @param[out] priority The specified thread's priority.
* @return Error status variable. 0 on success.
*/
int32_t HAL_GetThreadPriority(NativeThreadHandle handle, HAL_Bool* isRealTime,
int32_t* status);
HAL_Status HAL_GetThreadPriority(NativeThreadHandle handle, int32_t* priority);
/**
* Gets the thread priority for the current thread.
* Gets the current thread's priority.
*
* @param[out] isRealTime Set to true if thread is real-time, otherwise false.
* @param[out] status Error status variable. 0 on success.
* @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.
* 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[out] priority The current thread's priority.
* @return Error status variable. 0 on success.
*/
int32_t HAL_GetCurrentThreadPriority(HAL_Bool* isRealTime, int32_t* status);
HAL_Status HAL_GetCurrentThreadPriority(int32_t* priority);
/**
* Sets the thread priority for the specified thread.
* Sets the specified thread's priority.
*
* @param[in] handle Reference to the thread to set the priority of.
* @param[in] realTime Set to true to set a real-time priority, false for
* standard priority.
* @param[in] 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.
* @param[out] status Error status variable. 0 on success.
* @return True on success.
* 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[in] handle The native thread handle.
* @param[in] priority The priority.
* @return Error status variable. 0 on success.
*/
HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
int32_t priority, int32_t* status);
HAL_Status HAL_SetThreadPriority(NativeThreadHandle handle, int32_t priority);
/**
* Sets the thread priority for the current thread.
* Sets the current thread's priority.
*
* @param[in] realTime Set to true to set a real-time priority, false for
* standard priority.
* @param[in] 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.
* @param[out] status Error status variable. 0 on success.
* @return True on success.
* 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[in] priority The priority.
* @return Error status variable. 0 on success.
*/
HAL_Bool HAL_SetCurrentThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status);
HAL_Status HAL_SetCurrentThreadPriority(int32_t priority);
#ifdef __cplusplus
} // extern "C"

View File

@@ -224,12 +224,6 @@ HAL_NotifierHandle HAL_CreateNotifier(int32_t* status) {
return handle;
}
HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status) {
auto native = notifierInstance->owner.GetNativeThreadHandle();
return HAL_SetThreadPriority(&native, realTime, priority, status);
}
void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle,
const WPI_String* name, int32_t* status) {
auto thr = notifierInstance->owner.GetThread();

View File

@@ -4,22 +4,28 @@
#include "wpi/hal/Threads.h"
#include <stdint.h>
#include "wpi/hal/Types.h"
namespace wpi::hal::init {
void InitializeThreads() {}
} // namespace wpi::hal::init
int32_t HAL_GetThreadPriority(NativeThreadHandle handle, HAL_Bool* isRealTime,
int32_t* status) {
HAL_Status HAL_GetThreadPriority(NativeThreadHandle handle, int32_t* priority) {
*priority = 0;
return 0;
}
int32_t HAL_GetCurrentThreadPriority(HAL_Bool* isRealTime, int32_t* status) {
HAL_Status HAL_GetCurrentThreadPriority(int32_t* priority) {
*priority = 0;
return 0;
}
HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
int32_t priority, int32_t* status) {
return true;
HAL_Status HAL_SetThreadPriority(NativeThreadHandle handle, int32_t priority) {
return 0;
}
HAL_Bool HAL_SetCurrentThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status) {
return true;
HAL_Status HAL_SetCurrentThreadPriority(int32_t priority) {
return 0;
}

View File

@@ -149,9 +149,7 @@ void CANStreamStorage::CheckFrame(const HAL_CANStreamMessage& message) {
bool SocketCanState::InitializeBuses() {
bool success = true;
readLoopRunner.ExecSync([this, &success](wpi::net::uv::Loop& loop) {
int32_t status = 0;
HAL_SetCurrentThreadPriority(true, 50, &status);
if (status != 0) {
if (HAL_SetCurrentThreadPriority(50) != 0) {
wpi::util::print("Failed to set CAN thread priority\n");
}

View File

@@ -20,6 +20,7 @@
#include "wpi/hal/handles/UnlimitedHandleResource.hpp"
#include "wpi/util/SafeThread.hpp"
#include "wpi/util/Synchronization.hpp"
#include "wpi/util/print.hpp"
#include "wpi/util/priority_queue.hpp"
#include "wpi/util/string.hpp"
@@ -74,6 +75,10 @@ void InitializeNotifier() {
} // namespace wpi::hal::init
void NotifierThread::Main() {
if (HAL_SetCurrentThreadPriority(40) != 0) {
wpi::util::print("Failed to set HAL Notifier thread priority\n");
}
std::unique_lock lock(m_mutex);
while (m_active) {
if (m_alarmQueue.empty()) {
@@ -150,12 +155,6 @@ HAL_NotifierHandle HAL_CreateNotifier(int32_t* status) {
return handle;
}
HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status) {
auto native = notifierInstance->owner.GetNativeThreadHandle();
return HAL_SetThreadPriority(&native, realTime, priority, status);
}
void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle,
const WPI_String* name, int32_t* status) {
auto thr = notifierInstance->owner.GetThread();

View File

@@ -4,10 +4,13 @@
#include "wpi/hal/Threads.h"
#include <stdint.h>
#include <pthread.h>
#include <sched.h>
#include "wpi/hal/Errors.h"
#include "wpi/hal/Types.h"
namespace wpi::hal::init {
void InitializeThreads() {}
@@ -15,76 +18,55 @@ void InitializeThreads() {}
extern "C" {
int32_t HAL_GetThreadPriority(NativeThreadHandle handle, HAL_Bool* isRealTime,
int32_t* status) {
HAL_Status HAL_GetThreadPriority(NativeThreadHandle handle, int32_t* priority) {
if (handle == nullptr) {
return NULL_PARAMETER;
}
sched_param sch;
int policy;
int success = pthread_getschedparam(
*reinterpret_cast<const pthread_t*>(handle), &policy, &sch);
if (success == 0) {
*status = 0;
} else {
*status = HAL_THREAD_PRIORITY_ERROR;
return -1;
}
if (policy == SCHED_FIFO || policy == SCHED_RR) {
*isRealTime = true;
return sch.sched_priority;
} else {
*isRealTime = false;
// 0 is the only supported priority for non-real-time
if (pthread_getschedparam(*reinterpret_cast<const pthread_t*>(handle),
&policy, &sch) == 0) {
*priority = sch.sched_priority;
return 0;
} else {
return HAL_THREAD_PRIORITY_ERROR;
}
}
int32_t HAL_GetCurrentThreadPriority(HAL_Bool* isRealTime, int32_t* status) {
HAL_Status HAL_GetCurrentThreadPriority(int32_t* priority) {
auto thread = pthread_self();
return HAL_GetThreadPriority(&thread, isRealTime, status);
return HAL_GetThreadPriority(&thread, priority);
}
HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
int32_t priority, int32_t* status) {
HAL_Status HAL_SetThreadPriority(NativeThreadHandle handle, int32_t priority) {
if (handle == nullptr) {
*status = NULL_PARAMETER;
return false;
return NULL_PARAMETER;
}
int scheduler = realTime ? SCHED_FIFO : SCHED_OTHER;
if (realTime) {
// We don't support setting priorities for non RT threads
// so we don't need to check for proper range
if (priority < sched_get_priority_min(scheduler) ||
priority > sched_get_priority_max(scheduler)) {
*status = HAL_THREAD_PRIORITY_RANGE_ERROR;
return false;
}
if (priority < 0 || priority > 99) {
return HAL_THREAD_PRIORITY_RANGE_ERROR;
}
int scheduler = priority > 0 ? SCHED_RR : SCHED_OTHER;
sched_param sch;
int policy;
pthread_getschedparam(*reinterpret_cast<const pthread_t*>(handle), &policy,
&sch);
if (scheduler == SCHED_FIFO || scheduler == SCHED_RR) {
sch.sched_priority = priority;
} else {
// Only need to set 0 priority for non RT thread
sch.sched_priority = 0;
}
sch.sched_priority = priority;
if (pthread_setschedparam(*reinterpret_cast<const pthread_t*>(handle),
scheduler, &sch)) {
*status = HAL_THREAD_PRIORITY_ERROR;
return false;
return HAL_THREAD_PRIORITY_ERROR;
} else {
*status = 0;
return true;
return 0;
}
}
HAL_Bool HAL_SetCurrentThreadPriority(HAL_Bool realTime, int32_t priority,
int32_t* status) {
HAL_Status HAL_SetCurrentThreadPriority(int32_t priority) {
auto thread = pthread_self();
return HAL_SetThreadPriority(&thread, realTime, priority, status);
return HAL_SetThreadPriority(&thread, priority);
}
} // extern "C"

View File

@@ -10,4 +10,3 @@ functions:
HAL_CancelNotifierAlarm:
HAL_GetNotifierOverrun:
HAL_AcknowledgeNotifierAlarm:
HAL_SetNotifierThreadPriority: