diff --git a/hal/build.gradle b/hal/build.gradle index 07d2d39b1d..d5af97f0ae 100644 --- a/hal/build.gradle +++ b/hal/build.gradle @@ -17,6 +17,7 @@ model { targetPlatform 'roborio-arm' binaries.all { tasks.withType(CppCompile) { + cppCompiler.args "-DNAMESPACED_PRIORITY" addNiLibraryLinks(linker, targetPlatform) addWpiUtilLibraryLinks(it, linker, targetPlatform) } diff --git a/hal/include/HAL/cpp/SerialHelper.h b/hal/include/HAL/cpp/SerialHelper.h index f416818f0a..c143d1cec0 100644 --- a/hal/include/HAL/cpp/SerialHelper.h +++ b/hal/include/HAL/cpp/SerialHelper.h @@ -45,7 +45,7 @@ class SerialHelper { int32_t m_resourceHandle; - static priority_mutex m_nameMutex; + static hal::priority_mutex m_nameMutex; static std::string m_usbNames[2]; }; } // namespace hal diff --git a/hal/include/HAL/cpp/priority_condition_variable.h b/hal/include/HAL/cpp/priority_condition_variable.h index 12aeba9c8b..9d76b3d65b 100644 --- a/hal/include/HAL/cpp/priority_condition_variable.h +++ b/hal/include/HAL/cpp/priority_condition_variable.h @@ -22,6 +22,8 @@ #include "priority_mutex.h" +namespace hal { + class priority_condition_variable { typedef std::chrono::system_clock clock; @@ -130,3 +132,10 @@ class priority_condition_variable { Lock& m_lock; }; }; + +} // namespace hal + +// For backwards compatibility +#ifndef NAMESPACED_PRIORITY +using priority_condition_variable = hal::priority_condition_variable; // NOLINT +#endif diff --git a/hal/include/HAL/cpp/priority_mutex.h b/hal/include/HAL/cpp/priority_mutex.h index c156bedbe2..8488836315 100644 --- a/hal/include/HAL/cpp/priority_mutex.h +++ b/hal/include/HAL/cpp/priority_mutex.h @@ -11,14 +11,18 @@ #include #if defined(FRC_SIMULATOR) || defined(_WIN32) +namespace hal { // We do not want to use pthreads if in the simulator; however, in the // simulator, we do not care about priority inversion. typedef std::mutex priority_mutex; typedef std::recursive_mutex priority_recursive_mutex; +} // namespace hal #else // Covers rest of file. #include +namespace hal { + class priority_recursive_mutex { public: typedef pthread_mutex_t* native_handle_type; @@ -77,5 +81,12 @@ class priority_mutex { pthread_mutex_t m_mutex = {{0, 0, 0, 0x20, 0, {0}}}; #endif }; +} // namespace hal #endif // FRC_SIMULATOR + +// For backwards compatibility +#ifndef NAMESPACED_PRIORITY +using priority_mutex = hal::priority_mutex; // NOLINT +using priority_recursive_mutex = hal::priority_recursive_mutex; // NOLINT +#endif diff --git a/hal/include/HAL/handles/DigitalHandleResource.h b/hal/include/HAL/handles/DigitalHandleResource.h index 653011e04c..ebe573cab7 100644 --- a/hal/include/HAL/handles/DigitalHandleResource.h +++ b/hal/include/HAL/handles/DigitalHandleResource.h @@ -47,7 +47,7 @@ class DigitalHandleResource { private: std::array, size> m_structures; - std::array m_handleMutexes; + std::array m_handleMutexes; }; template @@ -58,7 +58,7 @@ THandle DigitalHandleResource::Allocate( *status = RESOURCE_OUT_OF_RANGE; return HAL_kInvalidHandle; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // check for allocation, otherwise allocate and return a valid handle if (m_structures[index] != nullptr) { *status = RESOURCE_IS_ALLOCATED; @@ -76,7 +76,7 @@ std::shared_ptr DigitalHandleResource::Get( if (index < 0 || index >= size) { return nullptr; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // return structure. Null will propogate correctly, so no need to manually // check. return m_structures[index]; @@ -89,7 +89,7 @@ void DigitalHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) return; // lock and deallocated handle - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); m_structures[index].reset(); } } // namespace hal diff --git a/hal/include/HAL/handles/IndexedClassedHandleResource.h b/hal/include/HAL/handles/IndexedClassedHandleResource.h index 0f69b1d5ff..8316ea186f 100644 --- a/hal/include/HAL/handles/IndexedClassedHandleResource.h +++ b/hal/include/HAL/handles/IndexedClassedHandleResource.h @@ -52,7 +52,7 @@ class IndexedClassedHandleResource { private: // Dynamic array to shrink HAL file size. std::unique_ptr[]> m_structures; - std::unique_ptr m_handleMutexes; + std::unique_ptr m_handleMutexes; }; template ::IndexedClassedHandleResource() { m_structures = std::make_unique[]>(size); - m_handleMutexes = std::make_unique(size); + m_handleMutexes = std::make_unique(size); } template ::Allocate( *status = RESOURCE_OUT_OF_RANGE; return HAL_kInvalidHandle; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // check for allocation, otherwise allocate and return a valid handle if (m_structures[index] != nullptr) { *status = RESOURCE_IS_ALLOCATED; @@ -92,7 +92,7 @@ std::shared_ptr IndexedClassedHandleResource< if (index < 0 || index >= size) { return nullptr; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // return structure. Null will propogate correctly, so no need to manually // check. return m_structures[index]; @@ -106,7 +106,7 @@ void IndexedClassedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) return; // lock and deallocated handle - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); m_structures[index].reset(); } } // namespace hal diff --git a/hal/include/HAL/handles/IndexedHandleResource.h b/hal/include/HAL/handles/IndexedHandleResource.h index cfe06688bc..4b36e86d30 100644 --- a/hal/include/HAL/handles/IndexedHandleResource.h +++ b/hal/include/HAL/handles/IndexedHandleResource.h @@ -48,7 +48,7 @@ class IndexedHandleResource { private: std::array, size> m_structures; - std::array m_handleMutexes; + std::array m_handleMutexes; }; template ::Allocate( *status = RESOURCE_OUT_OF_RANGE; return HAL_kInvalidHandle; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // check for allocation, otherwise allocate and return a valid handle if (m_structures[index] != nullptr) { *status = RESOURCE_IS_ALLOCATED; @@ -79,7 +79,7 @@ IndexedHandleResource::Get(THandle handle) { if (index < 0 || index >= size) { return nullptr; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // return structure. Null will propogate correctly, so no need to manually // check. return m_structures[index]; @@ -93,7 +93,7 @@ void IndexedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) return; // lock and deallocated handle - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); m_structures[index].reset(); } } // namespace hal diff --git a/hal/include/HAL/handles/LimitedClassedHandleResource.h b/hal/include/HAL/handles/LimitedClassedHandleResource.h index 67ce8b1f2d..419dd95a1d 100644 --- a/hal/include/HAL/handles/LimitedClassedHandleResource.h +++ b/hal/include/HAL/handles/LimitedClassedHandleResource.h @@ -47,8 +47,8 @@ class LimitedClassedHandleResource { private: std::array, size> m_structures; - std::array m_handleMutexes; - priority_mutex m_allocateMutex; + std::array m_handleMutexes; + hal::priority_mutex m_allocateMutex; }; template ::Allocate( std::shared_ptr toSet) { // globally lock to loop through indices - std::lock_guard sync(m_allocateMutex); + std::lock_guard sync(m_allocateMutex); for (int16_t i = 0; i < size; i++) { if (m_structures[i] == nullptr) { // if a false index is found, grab its specific mutex // and allocate it. - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i] = toSet; return static_cast(createHandle(i, enumValue)); } @@ -79,7 +79,7 @@ std::shared_ptr LimitedClassedHandleResource< if (index < 0 || index >= size) { return nullptr; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // return structure. Null will propogate correctly, so no need to manually // check. return m_structures[index]; @@ -93,8 +93,8 @@ void LimitedClassedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) return; // lock and deallocated handle - std::lock_guard sync(m_allocateMutex); - std::lock_guard lock(m_handleMutexes[index]); + std::lock_guard sync(m_allocateMutex); + std::lock_guard lock(m_handleMutexes[index]); m_structures[index].reset(); } } // namespace hal diff --git a/hal/include/HAL/handles/LimitedHandleResource.h b/hal/include/HAL/handles/LimitedHandleResource.h index 7b365208aa..eb20cce874 100644 --- a/hal/include/HAL/handles/LimitedHandleResource.h +++ b/hal/include/HAL/handles/LimitedHandleResource.h @@ -45,20 +45,20 @@ class LimitedHandleResource { private: std::array, size> m_structures; - std::array m_handleMutexes; - priority_mutex m_allocateMutex; + std::array m_handleMutexes; + hal::priority_mutex m_allocateMutex; }; template THandle LimitedHandleResource::Allocate() { // globally lock to loop through indices - std::lock_guard sync(m_allocateMutex); + std::lock_guard sync(m_allocateMutex); for (int16_t i = 0; i < size; i++) { if (m_structures[i] == nullptr) { // if a false index is found, grab its specific mutex // and allocate it. - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i] = std::make_shared(); return static_cast(createHandle(i, enumValue)); } @@ -75,7 +75,7 @@ LimitedHandleResource::Get(THandle handle) { if (index < 0 || index >= size) { return nullptr; } - std::lock_guard sync(m_handleMutexes[index]); + std::lock_guard sync(m_handleMutexes[index]); // return structure. Null will propogate correctly, so no need to manually // check. return m_structures[index]; @@ -89,8 +89,8 @@ void LimitedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) return; // lock and deallocated handle - std::lock_guard sync(m_allocateMutex); - std::lock_guard lock(m_handleMutexes[index]); + std::lock_guard sync(m_allocateMutex); + std::lock_guard lock(m_handleMutexes[index]); m_structures[index].reset(); } } // namespace hal diff --git a/hal/include/HAL/handles/UnlimitedHandleResource.h b/hal/include/HAL/handles/UnlimitedHandleResource.h index ab77914a28..ba93176b8b 100644 --- a/hal/include/HAL/handles/UnlimitedHandleResource.h +++ b/hal/include/HAL/handles/UnlimitedHandleResource.h @@ -47,13 +47,13 @@ class UnlimitedHandleResource { private: std::vector> m_structures; - priority_mutex m_handleMutex; + hal::priority_mutex m_handleMutex; }; template THandle UnlimitedHandleResource::Allocate( std::shared_ptr structure) { - std::lock_guard sync(m_handleMutex); + std::lock_guard sync(m_handleMutex); size_t i; for (i = 0; i < m_structures.size(); i++) { if (m_structures[i] == nullptr) { @@ -71,7 +71,7 @@ template std::shared_ptr UnlimitedHandleResource::Get(THandle handle) { int16_t index = getHandleTypedIndex(handle, enumValue); - std::lock_guard sync(m_handleMutex); + std::lock_guard sync(m_handleMutex); if (index < 0 || index >= static_cast(m_structures.size())) return nullptr; return m_structures[index]; @@ -81,7 +81,7 @@ template void UnlimitedHandleResource::Free( THandle handle) { int16_t index = getHandleTypedIndex(handle, enumValue); - std::lock_guard sync(m_handleMutex); + std::lock_guard sync(m_handleMutex); if (index < 0 || index >= static_cast(m_structures.size())) return; m_structures[index].reset(); } diff --git a/hal/lib/athena/DigitalInternal.cpp b/hal/lib/athena/DigitalInternal.cpp index 01e6c16f74..a66e3d5b19 100644 --- a/hal/lib/athena/DigitalInternal.cpp +++ b/hal/lib/athena/DigitalInternal.cpp @@ -30,7 +30,7 @@ std::unique_ptr pwmSystem; std::unique_ptr spiSystem; static std::atomic digitalSystemsInitialized{false}; -static priority_mutex initializeMutex; +static hal::priority_mutex initializeMutex; DigitalHandleResource @@ -43,7 +43,7 @@ void initializeDigital(int32_t* status) { // Initial check, as if it's true initialization has finished if (digitalSystemsInitialized) return; - std::lock_guard lock(initializeMutex); + std::lock_guard lock(initializeMutex); // Second check in case another thread was waiting if (digitalSystemsInitialized) return; diff --git a/hal/lib/athena/FRCDriverStation.cpp b/hal/lib/athena/FRCDriverStation.cpp index 284db425bb..f78d13aaf7 100644 --- a/hal/lib/athena/FRCDriverStation.cpp +++ b/hal/lib/athena/FRCDriverStation.cpp @@ -25,9 +25,9 @@ struct HAL_JoystickAxesInt { int16_t axes[HAL_kMaxJoystickAxes]; }; -static priority_mutex msgMutex; -static priority_condition_variable newDSDataAvailableCond; -static priority_mutex newDSDataAvailableMutex; +static hal::priority_mutex msgMutex; +static hal::priority_condition_variable newDSDataAvailableCond; +static hal::priority_mutex newDSDataAvailableMutex; static int newDSDataAvailableCounter{0}; extern "C" { @@ -42,7 +42,7 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, // Avoid flooding console by keeping track of previous 5 error // messages and only printing again if they're longer than 1 second old. static constexpr int KEEP_MSGS = 5; - std::lock_guard lock(msgMutex); + std::lock_guard lock(msgMutex); static std::string prevMsg[KEEP_MSGS]; static std::chrono::time_point prevMsgTime[KEEP_MSGS]; @@ -254,7 +254,7 @@ bool HAL_IsNewControlData(void) { thread_local int lastCount{-1}; int currentCount = 0; { - std::unique_lock lock(newDSDataAvailableMutex); + std::unique_lock lock(newDSDataAvailableMutex); currentCount = newDSDataAvailableCounter; } if (lastCount == currentCount) return false; @@ -276,7 +276,7 @@ HAL_Bool HAL_WaitForDSDataTimeout(double timeout) { auto timeoutTime = std::chrono::steady_clock::now() + std::chrono::duration(timeout); - std::unique_lock lock(newDSDataAvailableMutex); + std::unique_lock lock(newDSDataAvailableMutex); int currentCount = newDSDataAvailableCounter; while (newDSDataAvailableCounter == currentCount) { if (timeout > 0) { @@ -302,7 +302,7 @@ static int32_t newDataOccur(uint32_t refNum) { // Since we could get other values, require our specific handle // to signal our threads if (refNum != refNumber) return 0; - std::lock_guard lock(newDSDataAvailableMutex); + std::lock_guard lock(newDSDataAvailableMutex); // Nofify all threads newDSDataAvailableCounter++; newDSDataAvailableCond.notify_all(); @@ -316,11 +316,11 @@ static int32_t newDataOccur(uint32_t refNum) { */ void HAL_InitializeDriverStation(void) { static std::atomic_bool initialized{false}; - static priority_mutex initializeMutex; + static hal::priority_mutex initializeMutex; // Initial check, as if it's true initialization has finished if (initialized) return; - std::lock_guard lock(initializeMutex); + std::lock_guard lock(initializeMutex); // Second check in case another thread was waiting if (initialized) return; diff --git a/hal/lib/athena/HAL.cpp b/hal/lib/athena/HAL.cpp index efb8e66d9d..af7967b187 100644 --- a/hal/lib/athena/HAL.cpp +++ b/hal/lib/athena/HAL.cpp @@ -35,7 +35,7 @@ using namespace hal; static std::unique_ptr global; static std::unique_ptr watchdog; -static priority_mutex timeMutex; +static hal::priority_mutex timeMutex; static uint32_t timeEpoch = 0; static uint32_t prevFPGATime = 0; static HAL_NotifierHandle rolloverNotifier = 0; @@ -224,7 +224,7 @@ uint64_t HAL_GetFPGATime(int32_t* status) { *status = NiFpga_Status_ResourceNotInitialized; return 0; } - std::lock_guard lock(timeMutex); + std::lock_guard lock(timeMutex); uint32_t fpgaTime = global->readLocalTime(status); if (*status != 0) return 0; // check for rollover @@ -270,11 +270,11 @@ static void timerRollover(uint64_t currentTime, HAL_NotifierHandle handle) { void HAL_BaseInitialize(int32_t* status) { static std::atomic_bool initialized{false}; - static priority_mutex initializeMutex; + static hal::priority_mutex initializeMutex; // Initial check, as if it's true initialization has finished if (initialized) return; - std::lock_guard lock(initializeMutex); + std::lock_guard lock(initializeMutex); // Second check in case another thread was waiting if (initialized) return; // image 4; Fixes errors caused by multiple processes. Talk to NI about this diff --git a/hal/lib/athena/Notifier.cpp b/hal/lib/athena/Notifier.cpp index 3bedb6897f..fc33c44c42 100644 --- a/hal/lib/athena/Notifier.cpp +++ b/hal/lib/athena/Notifier.cpp @@ -26,7 +26,7 @@ using namespace hal; static const int32_t kTimerInterruptNumber = 28; -static priority_mutex notifierInterruptMutex; +static hal::priority_mutex notifierInterruptMutex; static priority_recursive_mutex notifierMutex; static std::unique_ptr notifierAlarm; static std::unique_ptr notifierManager; @@ -182,7 +182,7 @@ HAL_NotifierHandle HAL_InitializeNotifier(HAL_NotifierProcessFunction process, if (!notifierAtexitRegistered.test_and_set()) std::atexit(cleanupNotifierAtExit); if (notifierRefCount.fetch_add(1) == 0) { - std::lock_guard sync(notifierInterruptMutex); + std::lock_guard sync(notifierInterruptMutex); // create manager and alarm if not already created if (!notifierManager) { notifierManager = std::make_unique( @@ -254,7 +254,7 @@ void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { } if (notifierRefCount.fetch_sub(1) == 1) { - std::lock_guard sync(notifierInterruptMutex); + std::lock_guard sync(notifierInterruptMutex); // if this was the last notifier, clean up alarm and manager if (notifierAlarm) { notifierAlarm->writeEnable(false, status); diff --git a/hal/lib/athena/cpp/SerialHelper.cpp b/hal/lib/athena/cpp/SerialHelper.cpp index 5b7e8be9ff..4ba8757dbb 100644 --- a/hal/lib/athena/cpp/SerialHelper.cpp +++ b/hal/lib/athena/cpp/SerialHelper.cpp @@ -285,7 +285,7 @@ done: int32_t SerialHelper::GetIndexForPort(HAL_SerialPort port, int32_t* status) { // Hold lock whenever we're using the names array - std::lock_guard lock(m_nameMutex); + std::lock_guard lock(m_nameMutex); std::string portString = m_usbNames[port - 2]; diff --git a/hal/lib/athena/cpp/priority_mutex.cpp b/hal/lib/athena/cpp/priority_mutex.cpp index 1213898733..e8f04ee101 100644 --- a/hal/lib/athena/cpp/priority_mutex.cpp +++ b/hal/lib/athena/cpp/priority_mutex.cpp @@ -7,6 +7,8 @@ #include "HAL/cpp/priority_mutex.h" +using namespace hal; + void priority_recursive_mutex::lock() { pthread_mutex_lock(&m_mutex); } void priority_recursive_mutex::unlock() { pthread_mutex_unlock(&m_mutex); } diff --git a/myRobotCpp/build.gradle b/myRobotCpp/build.gradle index 8e436e27e3..f278ef692d 100644 --- a/myRobotCpp/build.gradle +++ b/myRobotCpp/build.gradle @@ -13,6 +13,7 @@ model { targetPlatform 'roborio-arm' binaries.all { tasks.withType(CppCompile) { + cppCompiler.args "-DNAMESPACED_PRIORITY" addNiLibraryLinks(linker, targetPlatform) addNetworkTablesLibraryLinks(it, linker, targetPlatform) addCsCoreLibraryLinks(it, linker, targetPlatform) diff --git a/wpilibc/athena.gradle b/wpilibc/athena.gradle index 17c04e24b5..3b5d876295 100644 --- a/wpilibc/athena.gradle +++ b/wpilibc/athena.gradle @@ -14,6 +14,7 @@ model { tasks.withType(CppCompile) { dependsOn generateCppVersion cppCompiler.args "-DNAMESPACED_WPILIB" + cppCompiler.args "-DNAMESPACED_PRIORITY" addNiLibraryLinks(linker, targetPlatform) addNetworkTablesLibraryLinks(it, linker, targetPlatform) addCsCoreLibraryLinks(it, linker, targetPlatform) diff --git a/wpilibc/athena/include/DigitalGlitchFilter.h b/wpilibc/athena/include/DigitalGlitchFilter.h index f5c882c05d..0d94451f95 100644 --- a/wpilibc/athena/include/DigitalGlitchFilter.h +++ b/wpilibc/athena/include/DigitalGlitchFilter.h @@ -49,7 +49,7 @@ class DigitalGlitchFilter : public SensorBase { void DoAdd(DigitalSource* input, int requested_index); int m_channelIndex = -1; - static priority_mutex m_mutex; + static hal::priority_mutex m_mutex; static ::std::array m_filterAllocated; }; diff --git a/wpilibc/athena/include/DriverStation.h b/wpilibc/athena/include/DriverStation.h index e5bdf5c8d9..2d078177ed 100644 --- a/wpilibc/athena/include/DriverStation.h +++ b/wpilibc/athena/include/DriverStation.h @@ -117,7 +117,7 @@ class DriverStation : public SensorBase, public RobotStateInterface { std::thread m_dsThread; std::atomic m_isRunning{false}; - mutable priority_mutex m_joystickDataMutex; + mutable hal::priority_mutex m_joystickDataMutex; // Robot state status variables bool m_userInDisabled = false; @@ -128,7 +128,7 @@ class DriverStation : public SensorBase, public RobotStateInterface { // Control word variables mutable HAL_ControlWord m_controlWordCache; mutable std::chrono::steady_clock::time_point m_lastControlWordUpdate; - mutable priority_mutex m_controlWordMutex; + mutable hal::priority_mutex m_controlWordMutex; double m_nextMessageTime = 0; }; diff --git a/wpilibc/athena/include/MotorSafetyHelper.h b/wpilibc/athena/include/MotorSafetyHelper.h index 829a9dba82..16c1888dee 100644 --- a/wpilibc/athena/include/MotorSafetyHelper.h +++ b/wpilibc/athena/include/MotorSafetyHelper.h @@ -37,13 +37,13 @@ class MotorSafetyHelper : public ErrorBase { // the FPGA clock value when this motor has expired double m_stopTime; // protect accesses to the state for this object - mutable priority_recursive_mutex m_syncMutex; + mutable hal::priority_recursive_mutex m_syncMutex; // the object that is using the helper MotorSafety* m_safeObject; // List of all existing MotorSafetyHelper objects. static std::set m_helperList; // protect accesses to the list of helpers - static priority_recursive_mutex m_listMutex; + static hal::priority_recursive_mutex m_listMutex; }; } // namespace frc diff --git a/wpilibc/athena/include/Notifier.h b/wpilibc/athena/include/Notifier.h index 0ea50ff4b1..7767a11dde 100644 --- a/wpilibc/athena/include/Notifier.h +++ b/wpilibc/athena/include/Notifier.h @@ -44,9 +44,9 @@ class Notifier : public ErrorBase { static void Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle); // used to constrain execution between destructors and callback - static priority_mutex m_destructorMutex; + static hal::priority_mutex m_destructorMutex; // held while updating process information - priority_mutex m_processMutex; + hal::priority_mutex m_processMutex; // HAL handle, atomic for proper destruction std::atomic m_notifier{0}; // address of the handler diff --git a/wpilibc/athena/src/DigitalGlitchFilter.cpp b/wpilibc/athena/src/DigitalGlitchFilter.cpp index 287ff8890c..3a71eb5cc3 100644 --- a/wpilibc/athena/src/DigitalGlitchFilter.cpp +++ b/wpilibc/athena/src/DigitalGlitchFilter.cpp @@ -22,10 +22,10 @@ using namespace frc; std::array DigitalGlitchFilter::m_filterAllocated = { {false, false, false}}; -priority_mutex DigitalGlitchFilter::m_mutex; +hal::priority_mutex DigitalGlitchFilter::m_mutex; DigitalGlitchFilter::DigitalGlitchFilter() { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); auto index = std::find(m_filterAllocated.begin(), m_filterAllocated.end(), false); wpi_assert(index != m_filterAllocated.end()); @@ -38,7 +38,7 @@ DigitalGlitchFilter::DigitalGlitchFilter() { DigitalGlitchFilter::~DigitalGlitchFilter() { if (m_channelIndex >= 0) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_filterAllocated[m_channelIndex] = false; } } diff --git a/wpilibc/athena/src/DriverStation.cpp b/wpilibc/athena/src/DriverStation.cpp index bcb9abba92..2b2a712530 100644 --- a/wpilibc/athena/src/DriverStation.cpp +++ b/wpilibc/athena/src/DriverStation.cpp @@ -90,7 +90,7 @@ double DriverStation::GetStickAxis(int stick, int axis) { wpi_setWPIError(BadJoystickIndex); return 0; } - std::unique_lock lock(m_joystickDataMutex); + std::unique_lock lock(m_joystickDataMutex); if (axis >= m_joystickAxes[stick].count) { // Unlock early so error printing isn't locked. m_joystickDataMutex.unlock(); @@ -116,7 +116,7 @@ int DriverStation::GetStickPOV(int stick, int pov) { wpi_setWPIError(BadJoystickIndex); return -1; } - std::unique_lock lock(m_joystickDataMutex); + std::unique_lock lock(m_joystickDataMutex); if (pov >= m_joystickPOVs[stick].count) { // Unlock early so error printing isn't locked. lock.unlock(); @@ -142,7 +142,7 @@ int DriverStation::GetStickButtons(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return m_joystickButtons[stick].buttons; } @@ -163,7 +163,7 @@ bool DriverStation::GetStickButton(int stick, int button) { "ERROR: Button indexes begin at 1 in WPILib for C++ and Java"); return false; } - std::unique_lock lock(m_joystickDataMutex); + std::unique_lock lock(m_joystickDataMutex); if (button > m_joystickButtons[stick].count) { // Unlock early so error printing isn't locked. lock.unlock(); @@ -187,7 +187,7 @@ int DriverStation::GetStickAxisCount(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return m_joystickAxes[stick].count; } @@ -202,7 +202,7 @@ int DriverStation::GetStickPOVCount(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return m_joystickPOVs[stick].count; } @@ -217,7 +217,7 @@ int DriverStation::GetStickButtonCount(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return m_joystickButtons[stick].count; } @@ -232,7 +232,7 @@ bool DriverStation::GetJoystickIsXbox(int stick) const { wpi_setWPIError(BadJoystickIndex); return false; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return static_cast(m_joystickDescriptor[stick].isXbox); } @@ -247,7 +247,7 @@ int DriverStation::GetJoystickType(int stick) const { wpi_setWPIError(BadJoystickIndex); return -1; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return static_cast(m_joystickDescriptor[stick].type); } @@ -261,7 +261,7 @@ std::string DriverStation::GetJoystickName(int stick) const { if (stick >= kJoystickPorts) { wpi_setWPIError(BadJoystickIndex); } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); std::string retVal(m_joystickDescriptor[stick].name); return retVal; } @@ -277,7 +277,7 @@ int DriverStation::GetJoystickAxisType(int stick, int axis) const { wpi_setWPIError(BadJoystickIndex); return -1; } - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); return m_joystickDescriptor[stick].axisTypes[axis]; } @@ -528,7 +528,7 @@ void DriverStation::GetData() { UpdateControlWord(true, controlWord); // Obtain a write lock on the data, swap the cached data into the // main data arrays - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickAxes.swap(m_joystickAxesCache); m_joystickPOVs.swap(m_joystickPOVsCache); m_joystickButtons.swap(m_joystickButtonsCache); @@ -629,7 +629,7 @@ void DriverStation::Run() { void DriverStation::UpdateControlWord(bool force, HAL_ControlWord& controlWord) const { auto now = std::chrono::steady_clock::now(); - std::lock_guard lock(m_controlWordMutex); + std::lock_guard lock(m_controlWordMutex); // Update every 50 ms or on force. if ((now - m_lastControlWordUpdate > std::chrono::milliseconds(50)) || force) { diff --git a/wpilibc/athena/src/MotorSafetyHelper.cpp b/wpilibc/athena/src/MotorSafetyHelper.cpp index 9f1ee89471..c42be31695 100644 --- a/wpilibc/athena/src/MotorSafetyHelper.cpp +++ b/wpilibc/athena/src/MotorSafetyHelper.cpp @@ -17,7 +17,7 @@ using namespace frc; std::set MotorSafetyHelper::m_helperList; -priority_recursive_mutex MotorSafetyHelper::m_listMutex; +hal::priority_recursive_mutex MotorSafetyHelper::m_listMutex; /** * The constructor for a MotorSafetyHelper object. @@ -37,12 +37,12 @@ MotorSafetyHelper::MotorSafetyHelper(MotorSafety* safeObject) m_expiration = DEFAULT_SAFETY_EXPIRATION; m_stopTime = Timer::GetFPGATimestamp(); - std::lock_guard sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.insert(this); } MotorSafetyHelper::~MotorSafetyHelper() { - std::lock_guard sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.erase(this); } @@ -51,7 +51,7 @@ MotorSafetyHelper::~MotorSafetyHelper() { * Resets the timer on this object that is used to do the timeouts. */ void MotorSafetyHelper::Feed() { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_stopTime = Timer::GetFPGATimestamp() + m_expiration; } @@ -60,7 +60,7 @@ void MotorSafetyHelper::Feed() { * @param expirationTime The timeout value in seconds. */ void MotorSafetyHelper::SetExpiration(double expirationTime) { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_expiration = expirationTime; } @@ -69,7 +69,7 @@ void MotorSafetyHelper::SetExpiration(double expirationTime) { * @return the timeout value in seconds. */ double MotorSafetyHelper::GetExpiration() const { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_expiration; } @@ -79,7 +79,7 @@ double MotorSafetyHelper::GetExpiration() const { * timed out. */ bool MotorSafetyHelper::IsAlive() const { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return !m_enabled || m_stopTime > Timer::GetFPGATimestamp(); } @@ -93,7 +93,7 @@ void MotorSafetyHelper::Check() { DriverStation& ds = DriverStation::GetInstance(); if (!m_enabled || ds.IsDisabled() || ds.IsTest()) return; - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); if (m_stopTime < Timer::GetFPGATimestamp()) { std::ostringstream desc; m_safeObject->GetDescription(desc); @@ -109,7 +109,7 @@ void MotorSafetyHelper::Check() { * @param enabled True if motor safety is enforced for this object */ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_enabled = enabled; } @@ -119,7 +119,7 @@ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { * @return True if motor safety is enforced for this device */ bool MotorSafetyHelper::IsSafetyEnabled() const { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_enabled; } @@ -129,7 +129,7 @@ bool MotorSafetyHelper::IsSafetyEnabled() const { * any that have timed out. */ void MotorSafetyHelper::CheckMotors() { - std::lock_guard sync(m_listMutex); + std::lock_guard sync(m_listMutex); for (auto elem : m_helperList) { elem->Check(); } diff --git a/wpilibc/athena/src/Notifier.cpp b/wpilibc/athena/src/Notifier.cpp index 67abe37180..0905b2ea23 100644 --- a/wpilibc/athena/src/Notifier.cpp +++ b/wpilibc/athena/src/Notifier.cpp @@ -14,7 +14,7 @@ using namespace frc; -priority_mutex Notifier::m_destructorMutex; +hal::priority_mutex Notifier::m_destructorMutex; /** * Create a Notifier for timer event notification. @@ -44,8 +44,8 @@ Notifier::~Notifier() { /* Acquire the mutex; this makes certain that the handler is not being * executed by the interrupt manager. */ - std::lock_guard lockStatic(Notifier::m_destructorMutex); - std::lock_guard lock(m_processMutex); + std::lock_guard lockStatic(Notifier::m_destructorMutex); + std::lock_guard lock(m_processMutex); } /** @@ -68,7 +68,7 @@ void Notifier::Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle) { Notifier* notifier; { // Lock static mutex to grab the notifier param - std::lock_guard lock(Notifier::m_destructorMutex); + std::lock_guard lock(Notifier::m_destructorMutex); int32_t status = 0; auto notifierPointer = HAL_GetNotifierParam(handle, &status); if (notifierPointer == nullptr) return; @@ -95,7 +95,7 @@ void Notifier::Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle) { * @param delay Seconds to wait before the handler is called. */ void Notifier::StartSingle(double delay) { - std::lock_guard sync(m_processMutex); + std::lock_guard sync(m_processMutex); m_periodic = false; m_period = delay; m_expirationTime = GetClock() + m_period; @@ -113,7 +113,7 @@ void Notifier::StartSingle(double delay) { * after the call to this method. */ void Notifier::StartPeriodic(double period) { - std::lock_guard sync(m_processMutex); + std::lock_guard sync(m_processMutex); m_periodic = true; m_period = period; m_expirationTime = GetClock() + m_period; @@ -136,6 +136,6 @@ void Notifier::Stop() { // Wait for a currently executing handler to complete before returning from // Stop() - std::lock_guard lockStatic(Notifier::m_destructorMutex); - std::lock_guard lock(m_processMutex); + std::lock_guard lockStatic(Notifier::m_destructorMutex); + std::lock_guard lock(m_processMutex); } diff --git a/wpilibc/athena/src/PIDController.cpp b/wpilibc/athena/src/PIDController.cpp index d81d6815f7..3d5a76524f 100644 --- a/wpilibc/athena/src/PIDController.cpp +++ b/wpilibc/athena/src/PIDController.cpp @@ -90,7 +90,7 @@ void PIDController::Calculate() { PIDOutput* pidOutput; { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); pidInput = m_pidInput; pidOutput = m_pidOutput; enabled = m_enabled; @@ -100,7 +100,7 @@ void PIDController::Calculate() { if (pidOutput == nullptr) return; if (enabled) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); double input = pidInput->PIDGet(); double result; PIDOutput* pidOutput; @@ -197,7 +197,7 @@ double PIDController::CalculateFeedForward() { */ void PIDController::SetPID(double p, double i, double d) { { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_P = p; m_I = i; m_D = d; @@ -222,7 +222,7 @@ void PIDController::SetPID(double p, double i, double d) { */ void PIDController::SetPID(double p, double i, double d, double f) { { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_P = p; m_I = i; m_D = d; @@ -243,7 +243,7 @@ void PIDController::SetPID(double p, double i, double d, double f) { * @return proportional coefficient */ double PIDController::GetP() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_P; } @@ -253,7 +253,7 @@ double PIDController::GetP() const { * @return integral coefficient */ double PIDController::GetI() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_I; } @@ -263,7 +263,7 @@ double PIDController::GetI() const { * @return differential coefficient */ double PIDController::GetD() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_D; } @@ -273,7 +273,7 @@ double PIDController::GetD() const { * @return Feed forward coefficient */ double PIDController::GetF() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_F; } @@ -285,7 +285,7 @@ double PIDController::GetF() const { * @return the latest calculated output */ double PIDController::Get() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_result; } @@ -299,7 +299,7 @@ double PIDController::Get() const { * @param continuous true turns on continuous, false turns off continuous */ void PIDController::SetContinuous(bool continuous) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_continuous = continuous; } @@ -311,7 +311,7 @@ void PIDController::SetContinuous(bool continuous) { */ void PIDController::SetInputRange(double minimumInput, double maximumInput) { { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_minimumInput = minimumInput; m_maximumInput = maximumInput; } @@ -326,7 +326,7 @@ void PIDController::SetInputRange(double minimumInput, double maximumInput) { * @param maximumOutput the maximum value to write to the output */ void PIDController::SetOutputRange(double minimumOutput, double maximumOutput) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_minimumOutput = minimumOutput; m_maximumOutput = maximumOutput; } @@ -340,7 +340,7 @@ void PIDController::SetOutputRange(double minimumOutput, double maximumOutput) { */ void PIDController::SetSetpoint(double setpoint) { { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_maximumInput > m_minimumInput) { if (setpoint > m_maximumInput) @@ -369,7 +369,7 @@ void PIDController::SetSetpoint(double setpoint) { * @return the current setpoint */ double PIDController::GetSetpoint() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_setpoint; } @@ -379,7 +379,7 @@ double PIDController::GetSetpoint() const { * @return the change in setpoint over time */ double PIDController::GetDeltaSetpoint() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get(); } @@ -391,7 +391,7 @@ double PIDController::GetDeltaSetpoint() const { double PIDController::GetError() const { double setpoint = GetSetpoint(); { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return GetContinuousError(setpoint - m_pidInput->PIDGet()); } } @@ -422,7 +422,7 @@ PIDSourceType PIDController::GetPIDSourceType() const { double PIDController::GetAvgError() const { double avgError = 0; { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); // Don't divide by zero. if (m_buf.size()) avgError = m_bufTotal / m_buf.size(); } @@ -436,7 +436,7 @@ double PIDController::GetAvgError() const { * @param percentage error which is tolerable */ void PIDController::SetTolerance(double percent) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -448,7 +448,7 @@ void PIDController::SetTolerance(double percent) { * @param percentage error which is tolerable */ void PIDController::SetAbsoluteTolerance(double absTolerance) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_toleranceType = kAbsoluteTolerance; m_tolerance = absTolerance; } @@ -460,7 +460,7 @@ void PIDController::SetAbsoluteTolerance(double absTolerance) { * @param percentage error which is tolerable */ void PIDController::SetPercentTolerance(double percent) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -476,7 +476,7 @@ void PIDController::SetPercentTolerance(double percent) { * @param bufLength Number of previous cycles to average. Defaults to 1. */ void PIDController::SetToleranceBuffer(int bufLength) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_bufLength = bufLength; // Cut the buffer down to size if needed. @@ -498,7 +498,7 @@ void PIDController::SetToleranceBuffer(int bufLength) { * This will return false until at least one input value has been computed. */ bool PIDController::OnTarget() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_buf.size() == 0) return false; double error = GetAvgError(); switch (m_toleranceType) { @@ -521,7 +521,7 @@ bool PIDController::OnTarget() const { */ void PIDController::Enable() { { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_enabled = true; } @@ -535,7 +535,7 @@ void PIDController::Enable() { */ void PIDController::Disable() { { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_pidOutput->PIDWrite(0); m_enabled = false; } @@ -549,7 +549,7 @@ void PIDController::Disable() { * Return true if PIDController is enabled. */ bool PIDController::IsEnabled() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return m_enabled; } @@ -559,7 +559,7 @@ bool PIDController::IsEnabled() const { void PIDController::Reset() { Disable(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_prevError = 0; m_totalError = 0; m_result = 0; diff --git a/wpilibc/athena/src/Timer.cpp b/wpilibc/athena/src/Timer.cpp index 04e7428d72..d17ff4ad6b 100644 --- a/wpilibc/athena/src/Timer.cpp +++ b/wpilibc/athena/src/Timer.cpp @@ -80,7 +80,7 @@ double Timer::Get() const { double result; double currentTime = GetFPGATimestamp(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_running) { // If the current time is before the start time, then the FPGA clock // rolled over. Compensate by adding the ~71 minutes that it takes @@ -104,7 +104,7 @@ double Timer::Get() const { * now. */ void Timer::Reset() { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_accumulatedTime = 0; m_startTime = GetFPGATimestamp(); } @@ -116,7 +116,7 @@ void Timer::Reset() { * relative to the system clock. */ void Timer::Start() { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (!m_running) { m_startTime = GetFPGATimestamp(); m_running = true; @@ -133,7 +133,7 @@ void Timer::Start() { void Timer::Stop() { double temp = Get(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_running) { m_accumulatedTime = temp; m_running = false; @@ -150,7 +150,7 @@ void Timer::Stop() { */ bool Timer::HasPeriodPassed(double period) { if (Get() > period) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); // Advance the start time by the period. m_startTime += period; // Don't set it to the current time... we want to avoid drift. diff --git a/wpilibc/shared/include/Commands/Scheduler.h b/wpilibc/shared/include/Commands/Scheduler.h index eb749aa2b4..c250ef1d2e 100644 --- a/wpilibc/shared/include/Commands/Scheduler.h +++ b/wpilibc/shared/include/Commands/Scheduler.h @@ -53,11 +53,11 @@ class Scheduler : public ErrorBase, public NamedSendable { void ProcessCommandAddition(Command* command); Command::SubsystemSet m_subsystems; - priority_mutex m_buttonsLock; + hal::priority_mutex m_buttonsLock; typedef std::vector ButtonVector; ButtonVector m_buttons; typedef std::vector CommandVector; - priority_mutex m_additionsLock; + hal::priority_mutex m_additionsLock; CommandVector m_additions; typedef std::set CommandSet; CommandSet m_commands; diff --git a/wpilibc/shared/include/ErrorBase.h b/wpilibc/shared/include/ErrorBase.h index 652e8eab9e..55329e0c7f 100644 --- a/wpilibc/shared/include/ErrorBase.h +++ b/wpilibc/shared/include/ErrorBase.h @@ -113,7 +113,7 @@ class ErrorBase { protected: mutable Error m_error; // TODO: Replace globalError with a global list of all errors. - static priority_mutex _globalErrorMutex; + static hal::priority_mutex _globalErrorMutex; static Error _globalError; }; diff --git a/wpilibc/shared/include/PIDController.h b/wpilibc/shared/include/PIDController.h index a304f9ccfa..a03c95d7db 100644 --- a/wpilibc/shared/include/PIDController.h +++ b/wpilibc/shared/include/PIDController.h @@ -136,7 +136,7 @@ class PIDController : public LiveWindowSendable, std::queue m_buf; double m_bufTotal = 0; - mutable priority_recursive_mutex m_mutex; + mutable hal::priority_recursive_mutex m_mutex; std::unique_ptr m_controlLoop; Timer m_setpointTimer; diff --git a/wpilibc/shared/include/Resource.h b/wpilibc/shared/include/Resource.h index d42ce50cd3..b7af7948d8 100644 --- a/wpilibc/shared/include/Resource.h +++ b/wpilibc/shared/include/Resource.h @@ -43,9 +43,9 @@ class Resource : public ErrorBase { private: std::vector m_isAllocated; - priority_recursive_mutex m_allocateLock; + hal::priority_recursive_mutex m_allocateLock; - static priority_recursive_mutex m_createLock; + static hal::priority_recursive_mutex m_createLock; }; } // namespace frc diff --git a/wpilibc/shared/include/Timer.h b/wpilibc/shared/include/Timer.h index c71e3c8104..dda86cce48 100644 --- a/wpilibc/shared/include/Timer.h +++ b/wpilibc/shared/include/Timer.h @@ -51,7 +51,7 @@ class Timer { double m_startTime = 0.0; double m_accumulatedTime = 0.0; bool m_running = false; - mutable priority_mutex m_mutex; + mutable hal::priority_mutex m_mutex; }; } // namespace frc diff --git a/wpilibc/shared/src/Commands/Scheduler.cpp b/wpilibc/shared/src/Commands/Scheduler.cpp index e2ccb00851..a421af872f 100644 --- a/wpilibc/shared/src/Commands/Scheduler.cpp +++ b/wpilibc/shared/src/Commands/Scheduler.cpp @@ -41,7 +41,7 @@ void Scheduler::SetEnabled(bool enabled) { m_enabled = enabled; } * @param command The command to be scheduled */ void Scheduler::AddCommand(Command* command) { - std::lock_guard sync(m_additionsLock); + std::lock_guard sync(m_additionsLock); if (std::find(m_additions.begin(), m_additions.end(), command) != m_additions.end()) return; @@ -49,7 +49,7 @@ void Scheduler::AddCommand(Command* command) { } void Scheduler::AddButton(ButtonScheduler* button) { - std::lock_guard sync(m_buttonsLock); + std::lock_guard sync(m_buttonsLock); m_buttons.push_back(button); } @@ -115,7 +115,7 @@ void Scheduler::Run() { { if (!m_enabled) return; - std::lock_guard sync(m_buttonsLock); + std::lock_guard sync(m_buttonsLock); for (auto rButtonIter = m_buttons.rbegin(); rButtonIter != m_buttons.rend(); rButtonIter++) { (*rButtonIter)->Execute(); @@ -138,7 +138,7 @@ void Scheduler::Run() { // Add the new things { - std::lock_guard sync(m_additionsLock); + std::lock_guard sync(m_additionsLock); for (auto additionsIter = m_additions.begin(); additionsIter != m_additions.end(); additionsIter++) { ProcessCommandAddition(*additionsIter); diff --git a/wpilibc/shared/src/ErrorBase.cpp b/wpilibc/shared/src/ErrorBase.cpp index 3323fe21c9..36141e4227 100644 --- a/wpilibc/shared/src/ErrorBase.cpp +++ b/wpilibc/shared/src/ErrorBase.cpp @@ -18,7 +18,7 @@ using namespace frc; -priority_mutex ErrorBase::_globalErrorMutex; +hal::priority_mutex ErrorBase::_globalErrorMutex; Error ErrorBase::_globalError; /** @@ -62,7 +62,7 @@ void ErrorBase::SetErrnoError(llvm::StringRef contextMessage, m_error.Set(-1, err, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -90,7 +90,7 @@ void ErrorBase::SetImaqError(int success, llvm::StringRef contextMessage, m_error.Set(success, err.str(), filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -115,7 +115,7 @@ void ErrorBase::SetError(Error::Code code, llvm::StringRef contextMessage, m_error.Set(code, contextMessage, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -153,7 +153,7 @@ void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange, delete[] buf; // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -179,7 +179,7 @@ void ErrorBase::SetWPIError(llvm::StringRef errorMessage, Error::Code code, m_error.Set(code, err, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -201,7 +201,7 @@ void ErrorBase::SetGlobalError(Error::Code code, llvm::StringRef contextMessage, llvm::StringRef function, int lineNumber) { // If there was an error if (code != 0) { - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); // Set the current error information for this object. _globalError.Set(code, contextMessage, filename, function, lineNumber, @@ -215,7 +215,7 @@ void ErrorBase::SetGlobalWPIError(llvm::StringRef errorMessage, llvm::StringRef function, int lineNumber) { std::string err = errorMessage.str() + ": " + contextMessage.str(); - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() != 0) { _globalError.Clear(); } @@ -226,6 +226,6 @@ void ErrorBase::SetGlobalWPIError(llvm::StringRef errorMessage, * Retrieve the current global error. */ Error& ErrorBase::GetGlobalError() { - std::lock_guard mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); return _globalError; } diff --git a/wpilibc/shared/src/Resource.cpp b/wpilibc/shared/src/Resource.cpp index 725d8a4d6f..17c7b349ba 100644 --- a/wpilibc/shared/src/Resource.cpp +++ b/wpilibc/shared/src/Resource.cpp @@ -12,7 +12,7 @@ using namespace frc; -priority_recursive_mutex Resource::m_createLock; +hal::priority_recursive_mutex Resource::m_createLock; /** * Allocate storage for a new instance of Resource. @@ -22,7 +22,7 @@ priority_recursive_mutex Resource::m_createLock; * elements - 1]. */ Resource::Resource(uint32_t elements) { - std::lock_guard sync(m_createLock); + std::lock_guard sync(m_createLock); m_isAllocated = std::vector(elements, false); } @@ -39,7 +39,7 @@ Resource::Resource(uint32_t elements) { */ /*static*/ void Resource::CreateResourceObject(std::unique_ptr& r, uint32_t elements) { - std::lock_guard sync(m_createLock); + std::lock_guard sync(m_createLock); if (!r) { r = std::make_unique(elements); } @@ -53,7 +53,7 @@ Resource::Resource(uint32_t elements) { * allocated. */ uint32_t Resource::Allocate(const std::string& resourceDesc) { - std::lock_guard sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); for (uint32_t i = 0; i < m_isAllocated.size(); i++) { if (!m_isAllocated[i]) { m_isAllocated[i] = true; @@ -71,7 +71,7 @@ uint32_t Resource::Allocate(const std::string& resourceDesc) { * verified unallocated, then returned. */ uint32_t Resource::Allocate(uint32_t index, const std::string& resourceDesc) { - std::lock_guard sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); if (index >= m_isAllocated.size()) { wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); return std::numeric_limits::max(); @@ -92,7 +92,7 @@ uint32_t Resource::Allocate(uint32_t index, const std::string& resourceDesc) { * be reused somewhere else in the program. */ void Resource::Free(uint32_t index) { - std::unique_lock sync(m_allocateLock); + std::unique_lock sync(m_allocateLock); if (index == std::numeric_limits::max()) return; if (index >= m_isAllocated.size()) { wpi_setWPIError(NotAllocated); diff --git a/wpilibc/sim/include/MotorSafetyHelper.h b/wpilibc/sim/include/MotorSafetyHelper.h index b300a84ba3..ff98c36b15 100644 --- a/wpilibc/sim/include/MotorSafetyHelper.h +++ b/wpilibc/sim/include/MotorSafetyHelper.h @@ -37,12 +37,12 @@ class MotorSafetyHelper : public ErrorBase { // the FPGA clock value when this motor has expired double m_stopTime; // protect accesses to the state for this object - mutable priority_recursive_mutex m_syncMutex; + mutable hal::priority_recursive_mutex m_syncMutex; MotorSafety* m_safeObject; // the object that is using the helper // List of all existing MotorSafetyHelper objects. static std::set m_helperList; // protect accesses to the list of helpers - static priority_recursive_mutex m_listMutex; + static hal::priority_recursive_mutex m_listMutex; }; } // namespace frc diff --git a/wpilibc/sim/include/Notifier.h b/wpilibc/sim/include/Notifier.h index 2e9f88f776..a7def04d86 100644 --- a/wpilibc/sim/include/Notifier.h +++ b/wpilibc/sim/include/Notifier.h @@ -39,8 +39,8 @@ class Notifier : public ErrorBase { private: static std::list timerQueue; - static priority_recursive_mutex queueMutex; - static priority_mutex halMutex; + static hal::priority_recursive_mutex queueMutex; + static hal::priority_mutex halMutex; static void* m_notifier; static std::atomic refcount; @@ -67,7 +67,7 @@ class Notifier : public ErrorBase { // Indicates if this entry is queued bool m_queued = false; // Held by interrupt manager task while handler call is in progress - priority_mutex m_handlerMutex; + hal::priority_mutex m_handlerMutex; static std::thread m_task; static std::atomic m_stopped; static void Run(); diff --git a/wpilibc/sim/src/DriverStation.cpp b/wpilibc/sim/src/DriverStation.cpp index 468cfdaf7d..44b167c3e3 100644 --- a/wpilibc/sim/src/DriverStation.cpp +++ b/wpilibc/sim/src/DriverStation.cpp @@ -295,7 +295,7 @@ bool DriverStation::WaitForData(double timeout) { std::chrono::steady_clock::now() + std::chrono::duration(timeout); #endif - std::unique_lock lock(m_waitForDataMutex); + std::unique_lock lock(m_waitForDataMutex); while (!m_updatedControlLoopData) { if (timeout > 0) { auto timedOut = m_waitForDataCond.wait_until(lock, timeoutTime); @@ -368,7 +368,7 @@ void DriverStation::stateCallback( *state = *msg; } { - std::lock_guard lock(m_waitForDataMutex); + std::lock_guard lock(m_waitForDataMutex); m_updatedControlLoopData = true; } m_waitForDataCond.notify_all(); diff --git a/wpilibc/sim/src/MotorSafetyHelper.cpp b/wpilibc/sim/src/MotorSafetyHelper.cpp index df4ecdead2..83604d941f 100644 --- a/wpilibc/sim/src/MotorSafetyHelper.cpp +++ b/wpilibc/sim/src/MotorSafetyHelper.cpp @@ -17,7 +17,7 @@ using namespace frc; std::set MotorSafetyHelper::m_helperList; -priority_recursive_mutex MotorSafetyHelper::m_listMutex; +hal::priority_recursive_mutex MotorSafetyHelper::m_listMutex; /** * The constructor for a MotorSafetyHelper object. @@ -37,12 +37,12 @@ MotorSafetyHelper::MotorSafetyHelper(MotorSafety* safeObject) m_expiration = DEFAULT_SAFETY_EXPIRATION; m_stopTime = Timer::GetFPGATimestamp(); - std::lock_guard sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.insert(this); } MotorSafetyHelper::~MotorSafetyHelper() { - std::lock_guard sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.erase(this); } @@ -52,7 +52,7 @@ MotorSafetyHelper::~MotorSafetyHelper() { * Resets the timer on this object that is used to do the timeouts. */ void MotorSafetyHelper::Feed() { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_stopTime = Timer::GetFPGATimestamp() + m_expiration; } @@ -62,7 +62,7 @@ void MotorSafetyHelper::Feed() { * @param expirationTime The timeout value in seconds. */ void MotorSafetyHelper::SetExpiration(double expirationTime) { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_expiration = expirationTime; } @@ -72,7 +72,7 @@ void MotorSafetyHelper::SetExpiration(double expirationTime) { * @return the timeout value in seconds. */ double MotorSafetyHelper::GetExpiration() const { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_expiration; } @@ -83,7 +83,7 @@ double MotorSafetyHelper::GetExpiration() const { * timed out. */ bool MotorSafetyHelper::IsAlive() const { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return !m_enabled || m_stopTime > Timer::GetFPGATimestamp(); } @@ -98,7 +98,7 @@ void MotorSafetyHelper::Check() { DriverStation& ds = DriverStation::GetInstance(); if (!m_enabled || ds.IsDisabled() || ds.IsTest()) return; - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); if (m_stopTime < Timer::GetFPGATimestamp()) { std::ostringstream desc; m_safeObject->GetDescription(desc); @@ -116,7 +116,7 @@ void MotorSafetyHelper::Check() { * @param enabled True if motor safety is enforced for this object */ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_enabled = enabled; } @@ -128,7 +128,7 @@ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { * @return True if motor safety is enforced for this device */ bool MotorSafetyHelper::IsSafetyEnabled() const { - std::lock_guard sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_enabled; } @@ -139,7 +139,7 @@ bool MotorSafetyHelper::IsSafetyEnabled() const { * any that have timed out. */ void MotorSafetyHelper::CheckMotors() { - std::lock_guard sync(m_listMutex); + std::lock_guard sync(m_listMutex); for (auto elem : m_helperList) { elem->Check(); } diff --git a/wpilibc/sim/src/Notifier.cpp b/wpilibc/sim/src/Notifier.cpp index 7950dae7c6..32b68e16ac 100644 --- a/wpilibc/sim/src/Notifier.cpp +++ b/wpilibc/sim/src/Notifier.cpp @@ -14,7 +14,7 @@ using namespace frc; std::list Notifier::timerQueue; -priority_recursive_mutex Notifier::queueMutex; +hal::priority_recursive_mutex Notifier::queueMutex; std::atomic Notifier::refcount{0}; std::thread Notifier::m_task; std::atomic Notifier::m_stopped(false); @@ -34,7 +34,7 @@ Notifier::Notifier(TimerEventHandler handler) { m_period = 0; m_queued = false; { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); // do the first time intialization of static variables if (refcount.fetch_add(1) == 0) { m_task = std::thread(Run); @@ -50,7 +50,7 @@ Notifier::Notifier(TimerEventHandler handler) { */ Notifier::~Notifier() { { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); DeleteFromQueue(); // Delete the static variables when the last one is going away @@ -62,7 +62,7 @@ Notifier::~Notifier() { // Acquire the semaphore; this makes certain that the handler is // not being executed by the interrupt manager. - std::lock_guard lock(m_handlerMutex); + std::lock_guard lock(m_handlerMutex); } /** @@ -89,7 +89,7 @@ void Notifier::ProcessQueue(int mask, void* params) { // keep processing events until no more while (true) { { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); double currentTime = GetClock(); if (timerQueue.empty()) { @@ -120,7 +120,7 @@ void Notifier::ProcessQueue(int mask, void* params) { current->m_handlerMutex.unlock(); } // reschedule the first item in the queue - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); UpdateAlarm(); } @@ -202,7 +202,7 @@ void Notifier::DeleteFromQueue() { * @param delay Seconds to wait before the handler is called. */ void Notifier::StartSingle(double delay) { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); m_periodic = false; m_period = delay; DeleteFromQueue(); @@ -220,7 +220,7 @@ void Notifier::StartSingle(double delay) { * the call to this method. */ void Notifier::StartPeriodic(double period) { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); m_periodic = true; m_period = period; DeleteFromQueue(); @@ -237,12 +237,12 @@ void Notifier::StartPeriodic(double period) { */ void Notifier::Stop() { { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); DeleteFromQueue(); } // Wait for a currently executing handler to complete before returning from // Stop() - std::lock_guard sync(m_handlerMutex); + std::lock_guard sync(m_handlerMutex); } void Notifier::Run() { @@ -250,13 +250,13 @@ void Notifier::Run() { Notifier::ProcessQueue(0, nullptr); bool isEmpty; { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); isEmpty = timerQueue.empty(); } if (!isEmpty) { double expirationTime; { - std::lock_guard sync(queueMutex); + std::lock_guard sync(queueMutex); expirationTime = timerQueue.front()->m_expirationTime; } Wait(expirationTime - GetClock()); diff --git a/wpilibc/sim/src/PIDController.cpp b/wpilibc/sim/src/PIDController.cpp index 0231eb88b3..50d3003d14 100644 --- a/wpilibc/sim/src/PIDController.cpp +++ b/wpilibc/sim/src/PIDController.cpp @@ -103,7 +103,7 @@ void PIDController::Calculate() { PIDSource* pidInput; { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_pidInput == 0) return; if (m_pidOutput == 0) return; enabled = m_enabled; @@ -116,7 +116,7 @@ void PIDController::Calculate() { PIDOutput* pidOutput; { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_error = m_setpoint - input; if (m_continuous) { if (std::fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { @@ -212,7 +212,7 @@ double PIDController::CalculateFeedForward() { */ void PIDController::SetPID(double p, double i, double d) { { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_P = p; m_I = i; m_D = d; @@ -237,7 +237,7 @@ void PIDController::SetPID(double p, double i, double d) { */ void PIDController::SetPID(double p, double i, double d, double f) { { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_P = p; m_I = i; m_D = d; @@ -258,7 +258,7 @@ void PIDController::SetPID(double p, double i, double d, double f) { * @return proportional coefficient */ double PIDController::GetP() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_P; } @@ -268,7 +268,7 @@ double PIDController::GetP() const { * @return integral coefficient */ double PIDController::GetI() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_I; } @@ -278,7 +278,7 @@ double PIDController::GetI() const { * @return differential coefficient */ double PIDController::GetD() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_D; } @@ -288,7 +288,7 @@ double PIDController::GetD() const { * @return Feed forward coefficient */ double PIDController::GetF() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_F; } @@ -300,7 +300,7 @@ double PIDController::GetF() const { * @return the latest calculated output */ double PIDController::Get() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_result; } @@ -314,7 +314,7 @@ double PIDController::Get() const { * @param continuous Set to true turns on continuous, false turns off continuous */ void PIDController::SetContinuous(bool continuous) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_continuous = continuous; } @@ -326,7 +326,7 @@ void PIDController::SetContinuous(bool continuous) { */ void PIDController::SetInputRange(double minimumInput, double maximumInput) { { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_minimumInput = minimumInput; m_maximumInput = maximumInput; } @@ -341,7 +341,7 @@ void PIDController::SetInputRange(double minimumInput, double maximumInput) { * @param maximumOutput the maximum value to write to the output */ void PIDController::SetOutputRange(double minimumOutput, double maximumOutput) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_minimumOutput = minimumOutput; m_maximumOutput = maximumOutput; } @@ -353,7 +353,7 @@ void PIDController::SetOutputRange(double minimumOutput, double maximumOutput) { */ void PIDController::SetSetpoint(double setpoint) { { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_maximumInput > m_minimumInput) { if (setpoint > m_maximumInput) @@ -378,7 +378,7 @@ void PIDController::SetSetpoint(double setpoint) { * @return the current setpoint */ double PIDController::GetSetpoint() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_setpoint; } @@ -388,7 +388,7 @@ double PIDController::GetSetpoint() const { * @return the change in setpoint over time */ double PIDController::GetDeltaSetpoint() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get(); } @@ -400,7 +400,7 @@ double PIDController::GetDeltaSetpoint() const { double PIDController::GetError() const { double setpoint = GetSetpoint(); { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); return GetContinuousError(setpoint - m_pidInput->PIDGet()); } } @@ -432,7 +432,7 @@ PIDSourceType PIDController::GetPIDSourceType() const { double PIDController::GetAvgError() const { double avgError = 0; { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); // Don't divide by zero. if (m_buf.size()) avgError = m_bufTotal / m_buf.size(); } @@ -446,7 +446,7 @@ double PIDController::GetAvgError() const { * @param percent percentage error which is tolerable */ void PIDController::SetTolerance(double percent) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -458,7 +458,7 @@ void PIDController::SetTolerance(double percent) { * @param absTolerance absolute error which is tolerable */ void PIDController::SetAbsoluteTolerance(double absTolerance) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_toleranceType = kAbsoluteTolerance; m_tolerance = absTolerance; } @@ -470,7 +470,7 @@ void PIDController::SetAbsoluteTolerance(double absTolerance) { * @param percent percentage error which is tolerable */ void PIDController::SetPercentTolerance(double percent) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -486,7 +486,7 @@ void PIDController::SetPercentTolerance(double percent) { * @param bufLength Number of previous cycles to average. Defaults to 1. */ void PIDController::SetToleranceBuffer(int bufLength) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_bufLength = bufLength; // Cut the buffer down to size if needed. @@ -506,7 +506,7 @@ void PIDController::SetToleranceBuffer(int bufLength) { * period of time. */ bool PIDController::OnTarget() const { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_buf.size() == 0) return false; double error = GetError(); switch (m_toleranceType) { @@ -528,7 +528,7 @@ bool PIDController::OnTarget() const { */ void PIDController::Enable() { { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_enabled = true; } @@ -542,7 +542,7 @@ void PIDController::Enable() { */ void PIDController::Disable() { { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_pidOutput->PIDWrite(0); m_enabled = false; } @@ -556,7 +556,7 @@ void PIDController::Disable() { * Return true if PIDController is enabled. */ bool PIDController::IsEnabled() const { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_enabled; } @@ -566,7 +566,7 @@ bool PIDController::IsEnabled() const { void PIDController::Reset() { Disable(); - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_prevError = 0; m_totalError = 0; m_result = 0; diff --git a/wpilibc/sim/src/Timer.cpp b/wpilibc/sim/src/Timer.cpp index 93e33e2fe1..131d555346 100644 --- a/wpilibc/sim/src/Timer.cpp +++ b/wpilibc/sim/src/Timer.cpp @@ -98,7 +98,7 @@ double Timer::Get() const { double result; double currentTime = GetFPGATimestamp(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_running) { // This math won't work if the timer rolled over (71 minutes after boot). // TODO: Check for it and compensate. @@ -117,7 +117,7 @@ double Timer::Get() const { * now. */ void Timer::Reset() { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_accumulatedTime = 0; m_startTime = GetFPGATimestamp(); } @@ -129,7 +129,7 @@ void Timer::Reset() { * relative to the system clock. */ void Timer::Start() { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (!m_running) { m_startTime = GetFPGATimestamp(); m_running = true; @@ -146,7 +146,7 @@ void Timer::Start() { void Timer::Stop() { double temp = Get(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_running) { m_accumulatedTime = temp; m_running = false; @@ -163,7 +163,7 @@ void Timer::Stop() { */ bool Timer::HasPeriodPassed(double period) { if (Get() > period) { - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); // Advance the start time by the period. // Don't set it to the current time... we want to avoid drift. m_startTime += period; diff --git a/wpilibcIntegrationTests/build.gradle b/wpilibcIntegrationTests/build.gradle index c37bbc7298..e722e692b3 100644 --- a/wpilibcIntegrationTests/build.gradle +++ b/wpilibcIntegrationTests/build.gradle @@ -15,6 +15,7 @@ model { binaries.all { tasks.withType(CppCompile) { cppCompiler.args "-DNAMESPACED_WPILIB" + cppCompiler.args "-DNAMESPACED_PRIORITY" addNiLibraryLinks(linker, targetPlatform) addNetworkTablesLibraryLinks(it, linker, targetPlatform) addCsCoreLibraryLinks(it, linker, targetPlatform) diff --git a/wpilibcIntegrationTests/src/ConditionVariableTest.cpp b/wpilibcIntegrationTests/src/ConditionVariableTest.cpp index e4e12aa080..9d75421371 100644 --- a/wpilibcIntegrationTests/src/ConditionVariableTest.cpp +++ b/wpilibcIntegrationTests/src/ConditionVariableTest.cpp @@ -23,13 +23,13 @@ namespace testing { // does work. class ConditionVariableTest : public ::testing::Test { protected: - typedef std::unique_lock priority_lock; + typedef std::unique_lock priority_lock; // Condition variable to test. - priority_condition_variable m_cond; + hal::priority_condition_variable m_cond; // Mutex to pass to condition variable when waiting. - priority_mutex m_mutex; + hal::priority_mutex m_mutex; // flags for testing when threads are completed. std::atomic m_done1{false}, m_done2{false}; diff --git a/wpilibcIntegrationTests/src/MutexTest.cpp b/wpilibcIntegrationTests/src/MutexTest.cpp index 8658388bce..64755b5a14 100644 --- a/wpilibcIntegrationTests/src/MutexTest.cpp +++ b/wpilibcIntegrationTests/src/MutexTest.cpp @@ -25,21 +25,21 @@ class Notification { public: // Efficiently waits until the Notification has been notified once. void Wait() { - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); while (!m_set) { m_condition.wait(lock); } } // Sets the condition to true, and wakes all waiting threads. void Notify() { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_set = true; m_condition.notify_all(); } private: - // priority_mutex used for the notification and to protect the bit. - priority_mutex m_mutex; + // hal::priority_mutex used for the notification and to protect the bit. + hal::priority_mutex m_mutex; // Condition for threads to sleep on. std::condition_variable_any m_condition; // Bool which is true when the notification has been notified. @@ -87,7 +87,7 @@ class LowPriorityThread { bool success() { return m_success.load(); } private: - // priority_mutex to grab and release. + // hal::priority_mutex to grab and release. MutexType* m_mutex; Notification m_started; // Atomic type used to signal when the thread should unlock the mutex. @@ -226,7 +226,7 @@ class InversionTestRunner { // Priority inversion test. TEST(MutexTest, DISABLED_PriorityInversionTest) { - InversionTestRunner runner; + InversionTestRunner runner; std::thread runner_thread(std::ref(runner)); runner_thread.join(); EXPECT_TRUE(runner.success()); @@ -242,7 +242,7 @@ TEST(MutexTest, DISABLED_StdMutexPriorityInversionTest) { // Smoke test to make sure that mutexes lock and unlock. TEST(MutexTest, TryLock) { - priority_mutex m; + hal::priority_mutex m; m.lock(); EXPECT_FALSE(m.try_lock()); m.unlock(); @@ -251,7 +251,7 @@ TEST(MutexTest, TryLock) { // Priority inversion test. TEST(MutexTest, DISABLED_ReentrantPriorityInversionTest) { - InversionTestRunner runner; + InversionTestRunner runner; std::thread runner_thread(std::ref(runner)); runner_thread.join(); EXPECT_TRUE(runner.success()); @@ -259,7 +259,7 @@ TEST(MutexTest, DISABLED_ReentrantPriorityInversionTest) { // Smoke test to make sure that mutexes lock and unlock. TEST(MutexTest, ReentrantTryLock) { - priority_recursive_mutex m; + hal::priority_recursive_mutex m; m.lock(); EXPECT_TRUE(m.try_lock()); m.unlock(); diff --git a/wpilibj/athena.gradle b/wpilibj/athena.gradle index 8cf22de1b0..539a787e8a 100644 --- a/wpilibj/athena.gradle +++ b/wpilibj/athena.gradle @@ -34,6 +34,7 @@ model { binaries.all { tasks.withType(CppCompile) { dependsOn jniHeaders + cppCompiler.args "-DNAMESPACED_PRIORITY" addNiLibraryLinks(linker, targetPlatform) addWpiUtilLibraryLinks(it, linker, targetPlatform) }